xref: /netbsd-src/usr.sbin/installboot/arch/i386.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: i386.c,v 1.18 2004/08/16 05:57:52 yamt 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42 
43 #include <sys/cdefs.h>
44 #if !defined(__lint)
45 __RCSID("$NetBSD: i386.c,v 1.18 2004/08/16 05:57:52 yamt Exp $");
46 #endif /* !__lint */
47 
48 #include <sys/param.h>
49 
50 #include <assert.h>
51 #include <err.h>
52 #include <md5.h>
53 #include <stddef.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #include "installboot.h"
60 
61 int
62 i386_setboot(ib_params *params)
63 {
64 	int		retval, i, bpbsize;
65 	uint8_t		*bootstrapbuf;
66 	u_int		bootstrapsize;
67 	ssize_t		rv;
68 	uint32_t	magic;
69 	struct x86_boot_params	bp, *bpp;
70 	int		bplen;
71 	struct mbr_sector	mbr;
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 UFSv1 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 %u 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(X86_BOOT_MAGIC_1)) {
140 		warnx("Invalid magic in stage1 boostrap %x != %x",
141 			magic, htole32(X86_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 	 *	eb 3c 90	jmp ENDOF(mbr_bpbFAT16)+1, nop
150 	 *	eb 58 90	jmp ENDOF(mbr_bpbFAT32)+1, nop
151 	 *      (anything else)	; don't preserve
152 	 */
153 	bpbsize = 0;
154 	if (bootstrapbuf[0] == 0xeb && bootstrapbuf[2] == 0x90 &&
155 	    (bootstrapbuf[1] == 0x3c || bootstrapbuf[1] == 0x58))
156 		bpbsize = bootstrapbuf[1] + 2 - MBR_BPB_OFFSET;
157 
158 	/*
159 	 * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
160 	 * the partition table.
161 	 */
162 	for (i = 0; i < sizeof(mbr.mbr_parts); i++) {
163 		if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) {
164 			warnx(
165 		    "Partition table has non-zero byte at offset %d in `%s'",
166 			    MBR_PART_OFFSET + i, params->stage1);
167 			goto done;
168 		}
169 	}
170 
171 	/*
172 	 * Copy the BPB and the partition table from the original MBR to the
173 	 * temporary buffer so that they're written back to the fs.
174 	 */
175 	if (bpbsize != 0) {
176 		if (params->flags & IB_VERBOSE)
177 			printf("Preserving %d (%#x) bytes of the BPB\n",
178 			    bpbsize, bpbsize);
179 		memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb, bpbsize);
180 	}
181 	memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts,
182 	    sizeof(mbr.mbr_parts));
183 
184 	/*
185 	 * Fill in any user-specified options into the
186 	 *      struct x86_boot_params
187 	 * that's 8 bytes in from the start of the third sector.
188 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
189 	 */
190 	bpp = (void *)(bootstrapbuf + 512 * 2 + 8);
191 	bplen = le32toh(bpp->bp_length);
192 	if (bplen > sizeof bp)
193 		/* Ignore pad space in bootxx */
194 		bplen = sizeof bp;
195 	/* Take (and update) local copy so we handle size mismatches */
196 	memset(&bp, 0, sizeof bp);
197 	memcpy(&bp, bpp, bplen);
198 	if (params->flags & IB_TIMEOUT)
199 		bp.bp_timeout = htole32(params->timeout);
200 	if (params->flags & IB_RESETVIDEO)
201 		bp.bp_flags |= htole32(X86_BP_FLAGS_RESET_VIDEO);
202 	if (params->flags & IB_CONSPEED)
203 		bp.bp_conspeed = htole32(params->conspeed);
204 	if (params->flags & IB_CONSADDR)
205 		bp.bp_consaddr = htole32(params->consaddr);
206 	if (params->flags & IB_CONSOLE) {
207 		static const char *names[] = {
208 			"pc", "com0", "com1", "com2", "com3",
209 			"com0kbd", "com1kbd", "com2kbd", "com3kbd",
210 			NULL };
211 		for (i = 0; ; i++) {
212 			if (names[i] == NULL) {
213 				warnx("invalid console name, valid names are:");
214 				fprintf(stderr, "\t%s", names[0]);
215 				for (i = 1; names[i] != NULL; i++)
216 					fprintf(stderr, ", %s", names[i]);
217 				fprintf(stderr, "\n");
218 				goto done;
219 			}
220 			if (strcmp(names[i], params->console) == 0)
221 				break;
222 		}
223 		bp.bp_consdev = htole32(i);
224 	}
225 	if (params->flags & IB_PASSWORD) {
226 		MD5_CTX md5ctx;
227 		MD5Init(&md5ctx);
228 		MD5Update(&md5ctx, params->password, strlen(params->password));
229 		MD5Final(bp.bp_password, &md5ctx);
230 		bp.bp_flags |= htole32(X86_BP_FLAGS_PASSWORD);
231 	}
232 	if (params->flags & IB_KEYMAP)
233 		strlcpy(bp.bp_keymap, params->keymap, sizeof bp.bp_keymap);
234 	/* Check we aren't trying to set anything we can't save */
235 	if (bplen < sizeof bp && memcmp((char *)&bp + bplen,
236 					(char *)&bp + bplen + 1,
237 					sizeof bp - bplen - 1) != 0) {
238 		warnx("Patch area in stage1 bootstrap is too small");
239 		goto done;
240 	}
241 	memcpy(bpp, &bp, bplen);
242 
243 	if (params->flags & IB_NOWRITE) {
244 		retval = 1;
245 		goto done;
246 	}
247 
248 	/*
249 	 * Write MBR code to sector zero.
250 	 */
251 	rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
252 	if (rv == -1) {
253 		warn("Writing `%s'", params->filesystem);
254 		goto done;
255 	} else if (rv != 512) {
256 		warnx("Writing `%s': short write", params->filesystem);
257 		goto done;
258 	}
259 
260 	/*
261 	 * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
262 	 */
263 	rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
264 		    bootstrapsize - 512 * 2, 512 * 2);
265 	if (rv == -1) {
266 		warn("Writing `%s'", params->filesystem);
267 		goto done;
268 	} else if (rv != bootstrapsize - 512 * 2) {
269 		warnx("Writing `%s': short write", params->filesystem);
270 		goto done;
271 	}
272 
273 	retval = 1;
274 
275  done:
276 	if (bootstrapbuf)
277 		free(bootstrapbuf);
278 	return retval;
279 }
280