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