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