landisk.c revision 1.6 1 1.6 christos /* $NetBSD: landisk.c,v 1.6 2013/10/19 17:08:15 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 *
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.6 christos __RCSID("$NetBSD: landisk.c,v 1.6 2013/10/19 17:08:15 christos 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
72 1.1 uwe assert(params != NULL);
73 1.1 uwe assert(params->fsfd != -1);
74 1.1 uwe assert(params->filesystem != NULL);
75 1.1 uwe assert(params->s1fd != -1);
76 1.1 uwe assert(params->stage1 != NULL);
77 1.1 uwe
78 1.1 uwe retval = 0;
79 1.1 uwe bootstrapbuf = NULL;
80 1.1 uwe
81 1.1 uwe /*
82 1.5 lukem * There is only 8k of space in a FFSv1 partition (and ustarfs)
83 1.1 uwe * so ensure we don't splat over anything important.
84 1.1 uwe */
85 1.1 uwe if (params->s1stat.st_size > 8192) {
86 1.1 uwe warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
87 1.1 uwe params->stage1);
88 1.1 uwe goto done;
89 1.1 uwe }
90 1.1 uwe
91 1.1 uwe /*
92 1.1 uwe * Read in the existing MBR.
93 1.1 uwe */
94 1.1 uwe rv = pread(params->fsfd, &mbr, sizeof(mbr), MBR_BBSECTOR);
95 1.1 uwe if (rv == -1) {
96 1.1 uwe warn("Reading `%s'", params->filesystem);
97 1.1 uwe goto done;
98 1.1 uwe } else if (rv != sizeof(mbr)) {
99 1.1 uwe warnx("Reading `%s': short read", params->filesystem);
100 1.1 uwe goto done;
101 1.1 uwe }
102 1.1 uwe if (mbr.mbr_magic != le16toh(MBR_MAGIC)) {
103 1.1 uwe if (params->flags & IB_VERBOSE) {
104 1.1 uwe printf(
105 1.1 uwe "Ignoring MBR with invalid magic in sector 0 of `%s'\n",
106 1.1 uwe params->filesystem);
107 1.1 uwe }
108 1.1 uwe memset(&mbr, 0, sizeof(mbr));
109 1.1 uwe }
110 1.1 uwe
111 1.1 uwe /*
112 1.1 uwe * Allocate a buffer, with space to round up the input file
113 1.1 uwe * to the next block size boundary, and with space for the boot
114 1.1 uwe * block.
115 1.1 uwe */
116 1.1 uwe bootstrapsize = roundup(params->s1stat.st_size, 512);
117 1.1 uwe
118 1.1 uwe bootstrapbuf = malloc(bootstrapsize);
119 1.1 uwe if (bootstrapbuf == NULL) {
120 1.4 lukem warn("Allocating %zu bytes", bootstrapsize);
121 1.1 uwe goto done;
122 1.1 uwe }
123 1.1 uwe memset(bootstrapbuf, 0, bootstrapsize);
124 1.1 uwe
125 1.1 uwe /*
126 1.1 uwe * Read the file into the buffer.
127 1.1 uwe */
128 1.1 uwe rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
129 1.1 uwe if (rv == -1) {
130 1.1 uwe warn("Reading `%s'", params->stage1);
131 1.1 uwe goto done;
132 1.1 uwe } else if (rv != params->s1stat.st_size) {
133 1.1 uwe warnx("Reading `%s': short read", params->stage1);
134 1.1 uwe goto done;
135 1.1 uwe }
136 1.1 uwe
137 1.1 uwe magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
138 1.1 uwe if (magic != htole32(LANDISK_BOOT_MAGIC_1)) {
139 1.1 uwe warnx("Invalid magic in stage1 boostrap %x != %x",
140 1.1 uwe magic, htole32(LANDISK_BOOT_MAGIC_1));
141 1.1 uwe goto done;
142 1.1 uwe }
143 1.1 uwe
144 1.1 uwe /*
145 1.1 uwe * Determine size of BIOS Parameter Block (BPB) to copy from
146 1.1 uwe * original MBR to the temporary buffer by examining the first
147 1.1 uwe * few instruction in the new bootblock. Supported values:
148 1.1 uwe * 2b a0 11 jmp ENDOF(mbr_bpbFAT32)+1, nop
149 1.1 uwe * (anything else) ; don't preserve
150 1.1 uwe */
151 1.1 uwe #if 0
152 1.6 christos int bpbsize;
153 1.1 uwe if (bootstrapbuf[1] == 0xa0 && bootstrapbuf[2] == 0x11 &&
154 1.1 uwe (bootstrapbuf[0] == 0x2b /*|| bootstrapbuf[0] == 0x1d*/)) {
155 1.6 christos bpbsize = bootstrapbuf[0] + 2 - MBR_BPB_OFFSET;
156 1.1 uwe }
157 1.1 uwe #endif
158 1.1 uwe
159 1.1 uwe /*
160 1.1 uwe * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
161 1.1 uwe * the partition table.
162 1.1 uwe */
163 1.4 lukem for (i = 0; i < (int)sizeof(mbr.mbr_parts); i++) {
164 1.1 uwe if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) {
165 1.1 uwe warnx(
166 1.1 uwe "Partition table has non-zero byte at offset %d in `%s'",
167 1.1 uwe MBR_PART_OFFSET + i, params->stage1);
168 1.1 uwe goto done;
169 1.1 uwe }
170 1.1 uwe }
171 1.1 uwe
172 1.2 christos #if 0
173 1.1 uwe /*
174 1.1 uwe * Copy the BPB and the partition table from the original MBR to the
175 1.1 uwe * temporary buffer so that they're written back to the fs.
176 1.1 uwe */
177 1.1 uwe if (bpbsize != 0) {
178 1.1 uwe if (params->flags & IB_VERBOSE)
179 1.1 uwe printf("Preserving %d (%#x) bytes of the BPB\n",
180 1.1 uwe bpbsize, bpbsize);
181 1.2 christos (void)memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb,
182 1.2 christos bpbsize);
183 1.1 uwe }
184 1.2 christos #endif
185 1.1 uwe memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts,
186 1.1 uwe sizeof(mbr.mbr_parts));
187 1.1 uwe
188 1.1 uwe /*
189 1.1 uwe * Fill in any user-specified options into the
190 1.1 uwe * struct landisk_boot_params
191 1.1 uwe * that's 8 bytes in from the start of the third sector.
192 1.1 uwe * See sys/arch/landisk/stand/bootxx/bootxx.S for more information.
193 1.1 uwe */
194 1.1 uwe bpp = (void *)(bootstrapbuf + 512 * 2 + 8);
195 1.1 uwe bplen = le32toh(bpp->bp_length);
196 1.1 uwe if (bplen > sizeof bp)
197 1.1 uwe /* Ignore pad space in bootxx */
198 1.1 uwe bplen = sizeof bp;
199 1.1 uwe /* Take (and update) local copy so we handle size mismatches */
200 1.1 uwe memset(&bp, 0, sizeof bp);
201 1.1 uwe memcpy(&bp, bpp, bplen);
202 1.1 uwe if (params->flags & IB_TIMEOUT)
203 1.1 uwe bp.bp_timeout = htole32(params->timeout);
204 1.1 uwe /* Check we aren't trying to set anything we can't save */
205 1.1 uwe if (bplen < sizeof bp && memcmp((char *)&bp + bplen,
206 1.1 uwe (char *)&bp + bplen + 1,
207 1.1 uwe sizeof bp - bplen - 1) != 0) {
208 1.1 uwe warnx("Patch area in stage1 bootstrap is too small");
209 1.1 uwe goto done;
210 1.1 uwe }
211 1.1 uwe memcpy(bpp, &bp, bplen);
212 1.1 uwe
213 1.1 uwe if (params->flags & IB_NOWRITE) {
214 1.1 uwe retval = 1;
215 1.1 uwe goto done;
216 1.1 uwe }
217 1.1 uwe
218 1.1 uwe /*
219 1.1 uwe * Write MBR code to sector zero.
220 1.1 uwe */
221 1.1 uwe rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
222 1.1 uwe if (rv == -1) {
223 1.1 uwe warn("Writing `%s'", params->filesystem);
224 1.1 uwe goto done;
225 1.1 uwe } else if (rv != 512) {
226 1.1 uwe warnx("Writing `%s': short write", params->filesystem);
227 1.1 uwe goto done;
228 1.1 uwe }
229 1.1 uwe
230 1.1 uwe /*
231 1.1 uwe * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
232 1.1 uwe */
233 1.1 uwe rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
234 1.1 uwe bootstrapsize - 512 * 2, 512 * 2);
235 1.1 uwe if (rv == -1) {
236 1.1 uwe warn("Writing `%s'", params->filesystem);
237 1.1 uwe goto done;
238 1.4 lukem } else if ((size_t)rv != bootstrapsize - 512 * 2) {
239 1.1 uwe warnx("Writing `%s': short write", params->filesystem);
240 1.1 uwe goto done;
241 1.1 uwe }
242 1.1 uwe
243 1.1 uwe retval = 1;
244 1.1 uwe
245 1.1 uwe done:
246 1.1 uwe if (bootstrapbuf)
247 1.1 uwe free(bootstrapbuf);
248 1.1 uwe return retval;
249 1.1 uwe }
250