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