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