xref: /netbsd-src/usr.sbin/installboot/arch/i386.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /* $NetBSD: i386.c,v 1.16 2004/06/20 22:20:17 jmc 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.16 2004/06/20 22:20:17 jmc 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;
70 	struct mbr_sector	mbr;
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 UFSv1 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 		if (params->flags & IB_VERBOSE) {
104 			printf(
105 		    "Ignoring MBR with invalid magic in sector 0 of `%s'\n",
106 			    params->filesystem);
107 		}
108 		memset(&mbr, 0, sizeof(mbr));
109 	}
110 
111 	/*
112 	 * Allocate a buffer, with space to round up the input file
113 	 * to the next block size boundary, and with space for the boot
114 	 * block.
115 	 */
116 	bootstrapsize = roundup(params->s1stat.st_size, 512);
117 
118 	bootstrapbuf = malloc(bootstrapsize);
119 	if (bootstrapbuf == NULL) {
120 		warn("Allocating %u bytes",  bootstrapsize);
121 		goto done;
122 	}
123 	memset(bootstrapbuf, 0, bootstrapsize);
124 
125 	/*
126 	 * Read the file into the buffer.
127 	 */
128 	rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
129 	if (rv == -1) {
130 		warn("Reading `%s'", params->stage1);
131 		goto done;
132 	} else if (rv != params->s1stat.st_size) {
133 		warnx("Reading `%s': short read", params->stage1);
134 		goto done;
135 	}
136 
137 	magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
138 	if (magic != htole32(X86_BOOT_MAGIC_1)) {
139 		warnx("Invalid magic in stage1 boostrap %x != %x",
140 			magic, htole32(X86_BOOT_MAGIC_1));
141 		goto done;
142 	}
143 
144 	/*
145 	 * Determine size of BIOS Parameter Block (BPB) to copy from
146 	 * original MBR to the temporary buffer by examining the first
147 	 * few instruction in the new bootblock.  Supported values:
148 	 *	eb 3c 90	jmp ENDOF(mbr_bpbFAT16)+1, nop
149 	 *	eb 58 90	jmp ENDOF(mbr_bpbFAT32)+1, nop
150 	 *      (anything else)	; don't preserve
151 	 */
152 	bpbsize = 0;
153 	if (bootstrapbuf[0] == 0xeb && bootstrapbuf[2] == 0x90 &&
154 	    (bootstrapbuf[1] == 0x3c || bootstrapbuf[1] == 0x58))
155 		bpbsize = bootstrapbuf[1] + 2 - MBR_BPB_OFFSET;
156 
157 	/*
158 	 * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
159 	 * the partition table.
160 	 */
161 	for (i = 0; i < sizeof(mbr.mbr_parts); i++) {
162 		if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) {
163 			warnx(
164 		    "Partition table has non-zero byte at offset %d in `%s'",
165 			    MBR_PART_OFFSET + i, params->stage1);
166 			goto done;
167 		}
168 	}
169 
170 	/*
171 	 * Copy the BPB and the partition table from the original MBR to the
172 	 * temporary buffer so that they're written back to the fs.
173 	 */
174 	if (bpbsize != 0) {
175 		if (params->flags & IB_VERBOSE)
176 			printf("Preserving %d (%#x) bytes of the BPB\n",
177 			    bpbsize, bpbsize);
178 		memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb, bpbsize);
179 	}
180 	memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts,
181 	    sizeof(mbr.mbr_parts));
182 
183 	/*
184 	 * Fill in any user-specified options into the
185 	 *	struct x86_boot_params
186 	 * that's 8 bytes in from the start of the third sector.
187 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
188 	 */
189 	bp = (void *)(bootstrapbuf + 512 * 2 + 8);
190 	if (le32toh(bp->bp_length) < sizeof *bp) {
191 		warnx("Patch area in stage1 bootstrap is too small");
192 		goto done;
193 	}
194 	if (params->flags & IB_TIMEOUT)
195 		bp->bp_timeout = htole32(params->timeout);
196 	if (params->flags & IB_RESETVIDEO)
197 		bp->bp_flags |= htole32(X86_BP_FLAGS_RESET_VIDEO);
198 	if (params->flags & IB_CONSPEED)
199 		bp->bp_conspeed = htole32(params->conspeed);
200 	if (params->flags & IB_CONSOLE) {
201 		static const char *names[] = {
202 			"pc", "com0", "com1", "com2", "com3",
203 			"com0kbd", "com1kbd", "com2kbd", "com3kbd",
204 			NULL };
205 		for (i = 0; ; i++) {
206 			if (names[i] == NULL) {
207 				warnx("invalid console name, valid names are:");
208 				fprintf(stderr, "\t%s", names[0]);
209 				for (i = 1; names[i] != NULL; i++)
210 					fprintf(stderr, ", %s", names[i]);
211 				fprintf(stderr, "\n");
212 				goto done;
213 			}
214 			if (strcmp(names[i], params->console) == 0)
215 				break;
216 		}
217 		bp->bp_consdev = htole32(i);
218 	}
219 	if (params->flags & IB_PASSWORD) {
220 		MD5_CTX md5ctx;
221 		MD5Init(&md5ctx);
222 		MD5Update(&md5ctx, params->password, strlen(params->password));
223 		MD5Final(bp->bp_password, &md5ctx);
224 		bp->bp_flags |= htole32(X86_BP_FLAGS_PASSWORD);
225 	}
226 	if (params->flags & IB_KEYMAP)
227 		strlcpy(bp->bp_keymap, params->keymap, sizeof bp->bp_keymap);
228 
229 	if (params->flags & IB_NOWRITE) {
230 		retval = 1;
231 		goto done;
232 	}
233 
234 	/*
235 	 * Write MBR code to sector zero.
236 	 */
237 	rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
238 	if (rv == -1) {
239 		warn("Writing `%s'", params->filesystem);
240 		goto done;
241 	} else if (rv != 512) {
242 		warnx("Writing `%s': short write", params->filesystem);
243 		goto done;
244 	}
245 
246 	/*
247 	 * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
248 	 */
249 	rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
250 		    bootstrapsize - 512 * 2, 512 * 2);
251 	if (rv == -1) {
252 		warn("Writing `%s'", params->filesystem);
253 		goto done;
254 	} else if (rv != bootstrapsize - 512 * 2) {
255 		warnx("Writing `%s': short write", params->filesystem);
256 		goto done;
257 	}
258 
259 	retval = 1;
260 
261  done:
262 	if (bootstrapbuf)
263 		free(bootstrapbuf);
264 	return retval;
265 }
266