landisk.c revision 1.4 1 1.4 lukem /* $NetBSD: landisk.c,v 1.4 2009/04/05 11:55:39 lukem Exp $ */
2 1.1 uwe
3 1.1 uwe /*-
4 1.1 uwe * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 uwe * All rights reserved.
6 1.1 uwe *
7 1.1 uwe * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uwe * by David Laight.
9 1.1 uwe *
10 1.1 uwe * Redistribution and use in source and binary forms, with or without
11 1.1 uwe * modification, are permitted provided that the following conditions
12 1.1 uwe * are met:
13 1.1 uwe * 1. Redistributions of source code must retain the above copyright
14 1.1 uwe * notice, this list of conditions and the following disclaimer.
15 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uwe * notice, this list of conditions and the following disclaimer in the
17 1.1 uwe * documentation and/or other materials provided with the distribution.
18 1.1 uwe *
19 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 uwe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 uwe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 uwe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 uwe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 uwe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 uwe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 uwe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 uwe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 uwe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 uwe * POSSIBILITY OF SUCH DAMAGE.
30 1.1 uwe */
31 1.1 uwe
32 1.1 uwe #if HAVE_NBTOOL_CONFIG_H
33 1.1 uwe #include "nbtool_config.h"
34 1.1 uwe #endif
35 1.1 uwe
36 1.1 uwe #include <sys/cdefs.h>
37 1.1 uwe #if !defined(__lint)
38 1.4 lukem __RCSID("$NetBSD: landisk.c,v 1.4 2009/04/05 11:55:39 lukem Exp $");
39 1.1 uwe #endif /* !__lint */
40 1.1 uwe
41 1.1 uwe #include <sys/param.h>
42 1.1 uwe
43 1.1 uwe #include <assert.h>
44 1.1 uwe #include <err.h>
45 1.1 uwe #include <md5.h>
46 1.1 uwe #include <stddef.h>
47 1.1 uwe #include <stdio.h>
48 1.1 uwe #include <stdlib.h>
49 1.1 uwe #include <string.h>
50 1.1 uwe #include <unistd.h>
51 1.1 uwe
52 1.1 uwe #include "installboot.h"
53 1.1 uwe
54 1.1 uwe static int landisk_setboot(ib_params *);
55 1.1 uwe
56 1.1 uwe struct ib_mach ib_mach_landisk =
57 1.1 uwe { "landisk", landisk_setboot, no_clearboot, no_editboot,
58 1.1 uwe IB_TIMEOUT };
59 1.1 uwe
60 1.1 uwe static int
61 1.1 uwe landisk_setboot(ib_params *params)
62 1.1 uwe {
63 1.1 uwe struct mbr_sector mbr;
64 1.1 uwe struct landisk_boot_params bp, *bpp;
65 1.1 uwe uint8_t *bootstrapbuf;
66 1.1 uwe ssize_t rv;
67 1.1 uwe uint32_t magic;
68 1.4 lukem size_t bootstrapsize;
69 1.1 uwe int retval, i;
70 1.4 lukem uint32_t bplen;
71 1.1 uwe int bpbsize;
72 1.1 uwe
73 1.1 uwe assert(params != NULL);
74 1.1 uwe assert(params->fsfd != -1);
75 1.1 uwe assert(params->filesystem != NULL);
76 1.1 uwe assert(params->s1fd != -1);
77 1.1 uwe assert(params->stage1 != NULL);
78 1.1 uwe
79 1.1 uwe retval = 0;
80 1.1 uwe bootstrapbuf = NULL;
81 1.1 uwe
82 1.1 uwe /*
83 1.1 uwe * There is only 8k of space in a UFSv1 partition (and ustarfs)
84 1.1 uwe * so ensure we don't splat over anything important.
85 1.1 uwe */
86 1.1 uwe if (params->s1stat.st_size > 8192) {
87 1.1 uwe warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
88 1.1 uwe params->stage1);
89 1.1 uwe goto done;
90 1.1 uwe }
91 1.1 uwe
92 1.1 uwe /*
93 1.1 uwe * Read in the existing MBR.
94 1.1 uwe */
95 1.1 uwe rv = pread(params->fsfd, &mbr, sizeof(mbr), MBR_BBSECTOR);
96 1.1 uwe if (rv == -1) {
97 1.1 uwe warn("Reading `%s'", params->filesystem);
98 1.1 uwe goto done;
99 1.1 uwe } else if (rv != sizeof(mbr)) {
100 1.1 uwe warnx("Reading `%s': short read", params->filesystem);
101 1.1 uwe goto done;
102 1.1 uwe }
103 1.1 uwe if (mbr.mbr_magic != le16toh(MBR_MAGIC)) {
104 1.1 uwe if (params->flags & IB_VERBOSE) {
105 1.1 uwe printf(
106 1.1 uwe "Ignoring MBR with invalid magic in sector 0 of `%s'\n",
107 1.1 uwe params->filesystem);
108 1.1 uwe }
109 1.1 uwe memset(&mbr, 0, sizeof(mbr));
110 1.1 uwe }
111 1.1 uwe
112 1.1 uwe /*
113 1.1 uwe * Allocate a buffer, with space to round up the input file
114 1.1 uwe * to the next block size boundary, and with space for the boot
115 1.1 uwe * block.
116 1.1 uwe */
117 1.1 uwe bootstrapsize = roundup(params->s1stat.st_size, 512);
118 1.1 uwe
119 1.1 uwe bootstrapbuf = malloc(bootstrapsize);
120 1.1 uwe if (bootstrapbuf == NULL) {
121 1.4 lukem warn("Allocating %zu bytes", bootstrapsize);
122 1.1 uwe goto done;
123 1.1 uwe }
124 1.1 uwe memset(bootstrapbuf, 0, bootstrapsize);
125 1.1 uwe
126 1.1 uwe /*
127 1.1 uwe * Read the file into the buffer.
128 1.1 uwe */
129 1.1 uwe rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
130 1.1 uwe if (rv == -1) {
131 1.1 uwe warn("Reading `%s'", params->stage1);
132 1.1 uwe goto done;
133 1.1 uwe } else if (rv != params->s1stat.st_size) {
134 1.1 uwe warnx("Reading `%s': short read", params->stage1);
135 1.1 uwe goto done;
136 1.1 uwe }
137 1.1 uwe
138 1.1 uwe magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
139 1.1 uwe if (magic != htole32(LANDISK_BOOT_MAGIC_1)) {
140 1.1 uwe warnx("Invalid magic in stage1 boostrap %x != %x",
141 1.1 uwe magic, htole32(LANDISK_BOOT_MAGIC_1));
142 1.1 uwe goto done;
143 1.1 uwe }
144 1.1 uwe
145 1.1 uwe /*
146 1.1 uwe * Determine size of BIOS Parameter Block (BPB) to copy from
147 1.1 uwe * original MBR to the temporary buffer by examining the first
148 1.1 uwe * few instruction in the new bootblock. Supported values:
149 1.1 uwe * 2b a0 11 jmp ENDOF(mbr_bpbFAT32)+1, nop
150 1.1 uwe * (anything else) ; don't preserve
151 1.1 uwe */
152 1.1 uwe bpbsize = 0;
153 1.1 uwe #if 0
154 1.1 uwe if (bootstrapbuf[1] == 0xa0 && bootstrapbuf[2] == 0x11 &&
155 1.1 uwe (bootstrapbuf[0] == 0x2b /*|| bootstrapbuf[0] == 0x1d*/)) {
156 1.1 uwe bpbsize = bootstrapbuf[0] + 2 - MBR_BPB_OFFSET;
157 1.1 uwe }
158 1.1 uwe #endif
159 1.1 uwe
160 1.1 uwe /*
161 1.1 uwe * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
162 1.1 uwe * the partition table.
163 1.1 uwe */
164 1.4 lukem for (i = 0; i < (int)sizeof(mbr.mbr_parts); i++) {
165 1.1 uwe if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) {
166 1.1 uwe warnx(
167 1.1 uwe "Partition table has non-zero byte at offset %d in `%s'",
168 1.1 uwe MBR_PART_OFFSET + i, params->stage1);
169 1.1 uwe goto done;
170 1.1 uwe }
171 1.1 uwe }
172 1.1 uwe
173 1.2 christos #if 0
174 1.1 uwe /*
175 1.1 uwe * Copy the BPB and the partition table from the original MBR to the
176 1.1 uwe * temporary buffer so that they're written back to the fs.
177 1.1 uwe */
178 1.1 uwe if (bpbsize != 0) {
179 1.1 uwe if (params->flags & IB_VERBOSE)
180 1.1 uwe printf("Preserving %d (%#x) bytes of the BPB\n",
181 1.1 uwe bpbsize, bpbsize);
182 1.2 christos (void)memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb,
183 1.2 christos bpbsize);
184 1.1 uwe }
185 1.2 christos #endif
186 1.1 uwe memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts,
187 1.1 uwe sizeof(mbr.mbr_parts));
188 1.1 uwe
189 1.1 uwe /*
190 1.1 uwe * Fill in any user-specified options into the
191 1.1 uwe * struct landisk_boot_params
192 1.1 uwe * that's 8 bytes in from the start of the third sector.
193 1.1 uwe * See sys/arch/landisk/stand/bootxx/bootxx.S for more information.
194 1.1 uwe */
195 1.1 uwe bpp = (void *)(bootstrapbuf + 512 * 2 + 8);
196 1.1 uwe bplen = le32toh(bpp->bp_length);
197 1.1 uwe if (bplen > sizeof bp)
198 1.1 uwe /* Ignore pad space in bootxx */
199 1.1 uwe bplen = sizeof bp;
200 1.1 uwe /* Take (and update) local copy so we handle size mismatches */
201 1.1 uwe memset(&bp, 0, sizeof bp);
202 1.1 uwe memcpy(&bp, bpp, bplen);
203 1.1 uwe if (params->flags & IB_TIMEOUT)
204 1.1 uwe bp.bp_timeout = htole32(params->timeout);
205 1.1 uwe /* Check we aren't trying to set anything we can't save */
206 1.1 uwe if (bplen < sizeof bp && memcmp((char *)&bp + bplen,
207 1.1 uwe (char *)&bp + bplen + 1,
208 1.1 uwe sizeof bp - bplen - 1) != 0) {
209 1.1 uwe warnx("Patch area in stage1 bootstrap is too small");
210 1.1 uwe goto done;
211 1.1 uwe }
212 1.1 uwe memcpy(bpp, &bp, bplen);
213 1.1 uwe
214 1.1 uwe if (params->flags & IB_NOWRITE) {
215 1.1 uwe retval = 1;
216 1.1 uwe goto done;
217 1.1 uwe }
218 1.1 uwe
219 1.1 uwe /*
220 1.1 uwe * Write MBR code to sector zero.
221 1.1 uwe */
222 1.1 uwe rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
223 1.1 uwe if (rv == -1) {
224 1.1 uwe warn("Writing `%s'", params->filesystem);
225 1.1 uwe goto done;
226 1.1 uwe } else if (rv != 512) {
227 1.1 uwe warnx("Writing `%s': short write", params->filesystem);
228 1.1 uwe goto done;
229 1.1 uwe }
230 1.1 uwe
231 1.1 uwe /*
232 1.1 uwe * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
233 1.1 uwe */
234 1.1 uwe rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
235 1.1 uwe bootstrapsize - 512 * 2, 512 * 2);
236 1.1 uwe if (rv == -1) {
237 1.1 uwe warn("Writing `%s'", params->filesystem);
238 1.1 uwe goto done;
239 1.4 lukem } else if ((size_t)rv != bootstrapsize - 512 * 2) {
240 1.1 uwe warnx("Writing `%s': short write", params->filesystem);
241 1.1 uwe goto done;
242 1.1 uwe }
243 1.1 uwe
244 1.1 uwe retval = 1;
245 1.1 uwe
246 1.1 uwe done:
247 1.1 uwe if (bootstrapbuf)
248 1.1 uwe free(bootstrapbuf);
249 1.1 uwe return retval;
250 1.1 uwe }
251