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