i386.c revision 1.18 1 1.18 yamt /* $NetBSD: i386.c,v 1.18 2004/08/16 05:57:52 yamt Exp $ */
2 1.1 dsl
3 1.1 dsl /*-
4 1.1 dsl * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 dsl * All rights reserved.
6 1.1 dsl *
7 1.1 dsl * This code is derived from software contributed to The NetBSD Foundation
8 1.1 dsl * by David Laight.
9 1.1 dsl *
10 1.1 dsl * Redistribution and use in source and binary forms, with or without
11 1.1 dsl * modification, are permitted provided that the following conditions
12 1.1 dsl * are met:
13 1.1 dsl * 1. Redistributions of source code must retain the above copyright
14 1.1 dsl * notice, this list of conditions and the following disclaimer.
15 1.1 dsl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dsl * notice, this list of conditions and the following disclaimer in the
17 1.1 dsl * documentation and/or other materials provided with the distribution.
18 1.1 dsl * 3. All advertising materials mentioning features or use of this software
19 1.1 dsl * must display the following acknowledgement:
20 1.1 dsl * This product includes software developed by the NetBSD
21 1.1 dsl * Foundation, Inc. and its contributors.
22 1.1 dsl * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 dsl * contributors may be used to endorse or promote products derived
24 1.1 dsl * from this software without specific prior written permission.
25 1.1 dsl *
26 1.1 dsl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 dsl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 dsl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 dsl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 dsl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 dsl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 dsl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 dsl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 dsl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 dsl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 dsl * POSSIBILITY OF SUCH DAMAGE.
37 1.1 dsl */
38 1.1 dsl
39 1.12 lukem #if HAVE_NBTOOL_CONFIG_H
40 1.12 lukem #include "nbtool_config.h"
41 1.12 lukem #endif
42 1.12 lukem
43 1.1 dsl #include <sys/cdefs.h>
44 1.12 lukem #if !defined(__lint)
45 1.18 yamt __RCSID("$NetBSD: i386.c,v 1.18 2004/08/16 05:57:52 yamt Exp $");
46 1.13 lukem #endif /* !__lint */
47 1.1 dsl
48 1.1 dsl #include <sys/param.h>
49 1.1 dsl
50 1.1 dsl #include <assert.h>
51 1.1 dsl #include <err.h>
52 1.5 bjh21 #include <md5.h>
53 1.1 dsl #include <stddef.h>
54 1.1 dsl #include <stdio.h>
55 1.1 dsl #include <stdlib.h>
56 1.1 dsl #include <string.h>
57 1.1 dsl #include <unistd.h>
58 1.1 dsl
59 1.1 dsl #include "installboot.h"
60 1.1 dsl
61 1.1 dsl int
62 1.1 dsl i386_setboot(ib_params *params)
63 1.1 dsl {
64 1.11 lukem int retval, i, bpbsize;
65 1.11 lukem uint8_t *bootstrapbuf;
66 1.18 yamt u_int bootstrapsize;
67 1.1 dsl ssize_t rv;
68 1.1 dsl uint32_t magic;
69 1.17 dsl struct x86_boot_params bp, *bpp;
70 1.17 dsl int bplen;
71 1.9 lukem struct mbr_sector mbr;
72 1.1 dsl
73 1.1 dsl assert(params != NULL);
74 1.1 dsl assert(params->fsfd != -1);
75 1.1 dsl assert(params->filesystem != NULL);
76 1.1 dsl assert(params->s1fd != -1);
77 1.1 dsl assert(params->stage1 != NULL);
78 1.1 dsl
79 1.1 dsl retval = 0;
80 1.1 dsl bootstrapbuf = NULL;
81 1.1 dsl
82 1.2 dsl /*
83 1.2 dsl * There is only 8k of space in a UFSv1 partition (and ustarfs)
84 1.2 dsl * so ensure we don't splat over anything important.
85 1.2 dsl */
86 1.2 dsl if (params->s1stat.st_size > 8192) {
87 1.2 dsl warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
88 1.2 dsl params->stage1);
89 1.9 lukem goto done;
90 1.9 lukem }
91 1.9 lukem
92 1.9 lukem /*
93 1.9 lukem * Read in the existing MBR.
94 1.9 lukem */
95 1.9 lukem rv = pread(params->fsfd, &mbr, sizeof(mbr), MBR_BBSECTOR);
96 1.9 lukem if (rv == -1) {
97 1.9 lukem warn("Reading `%s'", params->filesystem);
98 1.9 lukem goto done;
99 1.9 lukem } else if (rv != sizeof(mbr)) {
100 1.9 lukem warnx("Reading `%s': short read", params->filesystem);
101 1.9 lukem goto done;
102 1.1 dsl }
103 1.10 lukem if (mbr.mbr_magic != le16toh(MBR_MAGIC)) {
104 1.9 lukem if (params->flags & IB_VERBOSE) {
105 1.9 lukem printf(
106 1.9 lukem "Ignoring MBR with invalid magic in sector 0 of `%s'\n",
107 1.9 lukem params->filesystem);
108 1.9 lukem }
109 1.9 lukem memset(&mbr, 0, sizeof(mbr));
110 1.9 lukem }
111 1.9 lukem
112 1.1 dsl /*
113 1.1 dsl * Allocate a buffer, with space to round up the input file
114 1.1 dsl * to the next block size boundary, and with space for the boot
115 1.1 dsl * block.
116 1.1 dsl */
117 1.1 dsl bootstrapsize = roundup(params->s1stat.st_size, 512);
118 1.1 dsl
119 1.1 dsl bootstrapbuf = malloc(bootstrapsize);
120 1.1 dsl if (bootstrapbuf == NULL) {
121 1.1 dsl warn("Allocating %u bytes", bootstrapsize);
122 1.1 dsl goto done;
123 1.1 dsl }
124 1.1 dsl memset(bootstrapbuf, 0, bootstrapsize);
125 1.1 dsl
126 1.9 lukem /*
127 1.9 lukem * Read the file into the buffer.
128 1.9 lukem */
129 1.1 dsl rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
130 1.1 dsl if (rv == -1) {
131 1.1 dsl warn("Reading `%s'", params->stage1);
132 1.9 lukem goto done;
133 1.1 dsl } else if (rv != params->s1stat.st_size) {
134 1.1 dsl warnx("Reading `%s': short read", params->stage1);
135 1.9 lukem goto done;
136 1.1 dsl }
137 1.1 dsl
138 1.1 dsl magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
139 1.7 dsl if (magic != htole32(X86_BOOT_MAGIC_1)) {
140 1.1 dsl warnx("Invalid magic in stage1 boostrap %x != %x",
141 1.7 dsl magic, htole32(X86_BOOT_MAGIC_1));
142 1.2 dsl goto done;
143 1.2 dsl }
144 1.2 dsl
145 1.9 lukem /*
146 1.11 lukem * Determine size of BIOS Parameter Block (BPB) to copy from
147 1.11 lukem * original MBR to the temporary buffer by examining the first
148 1.11 lukem * few instruction in the new bootblock. Supported values:
149 1.11 lukem * eb 3c 90 jmp ENDOF(mbr_bpbFAT16)+1, nop
150 1.11 lukem * eb 58 90 jmp ENDOF(mbr_bpbFAT32)+1, nop
151 1.11 lukem * (anything else) ; don't preserve
152 1.11 lukem */
153 1.11 lukem bpbsize = 0;
154 1.11 lukem if (bootstrapbuf[0] == 0xeb && bootstrapbuf[2] == 0x90 &&
155 1.11 lukem (bootstrapbuf[1] == 0x3c || bootstrapbuf[1] == 0x58))
156 1.11 lukem bpbsize = bootstrapbuf[1] + 2 - MBR_BPB_OFFSET;
157 1.11 lukem
158 1.11 lukem /*
159 1.15 lukem * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
160 1.11 lukem * the partition table.
161 1.9 lukem */
162 1.9 lukem for (i = 0; i < sizeof(mbr.mbr_parts); i++) {
163 1.9 lukem if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) {
164 1.9 lukem warnx(
165 1.9 lukem "Partition table has non-zero byte at offset %d in `%s'",
166 1.9 lukem MBR_PART_OFFSET + i, params->stage1);
167 1.9 lukem goto done;
168 1.9 lukem }
169 1.9 lukem }
170 1.9 lukem
171 1.9 lukem /*
172 1.9 lukem * Copy the BPB and the partition table from the original MBR to the
173 1.9 lukem * temporary buffer so that they're written back to the fs.
174 1.9 lukem */
175 1.11 lukem if (bpbsize != 0) {
176 1.11 lukem if (params->flags & IB_VERBOSE)
177 1.11 lukem printf("Preserving %d (%#x) bytes of the BPB\n",
178 1.11 lukem bpbsize, bpbsize);
179 1.11 lukem memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb, bpbsize);
180 1.11 lukem }
181 1.9 lukem memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts,
182 1.9 lukem sizeof(mbr.mbr_parts));
183 1.9 lukem
184 1.9 lukem /*
185 1.15 lukem * Fill in any user-specified options into the
186 1.17 dsl * struct x86_boot_params
187 1.15 lukem * that's 8 bytes in from the start of the third sector.
188 1.15 lukem * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
189 1.9 lukem */
190 1.17 dsl bpp = (void *)(bootstrapbuf + 512 * 2 + 8);
191 1.17 dsl bplen = le32toh(bpp->bp_length);
192 1.17 dsl if (bplen > sizeof bp)
193 1.17 dsl /* Ignore pad space in bootxx */
194 1.17 dsl bplen = sizeof bp;
195 1.17 dsl /* Take (and update) local copy so we handle size mismatches */
196 1.17 dsl memset(&bp, 0, sizeof bp);
197 1.17 dsl memcpy(&bp, bpp, bplen);
198 1.2 dsl if (params->flags & IB_TIMEOUT)
199 1.17 dsl bp.bp_timeout = htole32(params->timeout);
200 1.2 dsl if (params->flags & IB_RESETVIDEO)
201 1.17 dsl bp.bp_flags |= htole32(X86_BP_FLAGS_RESET_VIDEO);
202 1.2 dsl if (params->flags & IB_CONSPEED)
203 1.17 dsl bp.bp_conspeed = htole32(params->conspeed);
204 1.17 dsl if (params->flags & IB_CONSADDR)
205 1.17 dsl bp.bp_consaddr = htole32(params->consaddr);
206 1.2 dsl if (params->flags & IB_CONSOLE) {
207 1.2 dsl static const char *names[] = {
208 1.2 dsl "pc", "com0", "com1", "com2", "com3",
209 1.2 dsl "com0kbd", "com1kbd", "com2kbd", "com3kbd",
210 1.2 dsl NULL };
211 1.2 dsl for (i = 0; ; i++) {
212 1.2 dsl if (names[i] == NULL) {
213 1.2 dsl warnx("invalid console name, valid names are:");
214 1.2 dsl fprintf(stderr, "\t%s", names[0]);
215 1.2 dsl for (i = 1; names[i] != NULL; i++)
216 1.2 dsl fprintf(stderr, ", %s", names[i]);
217 1.6 petrov fprintf(stderr, "\n");
218 1.2 dsl goto done;
219 1.2 dsl }
220 1.2 dsl if (strcmp(names[i], params->console) == 0)
221 1.2 dsl break;
222 1.2 dsl }
223 1.17 dsl bp.bp_consdev = htole32(i);
224 1.2 dsl }
225 1.2 dsl if (params->flags & IB_PASSWORD) {
226 1.2 dsl MD5_CTX md5ctx;
227 1.2 dsl MD5Init(&md5ctx);
228 1.2 dsl MD5Update(&md5ctx, params->password, strlen(params->password));
229 1.17 dsl MD5Final(bp.bp_password, &md5ctx);
230 1.17 dsl bp.bp_flags |= htole32(X86_BP_FLAGS_PASSWORD);
231 1.1 dsl }
232 1.14 dsl if (params->flags & IB_KEYMAP)
233 1.17 dsl strlcpy(bp.bp_keymap, params->keymap, sizeof bp.bp_keymap);
234 1.17 dsl /* Check we aren't trying to set anything we can't save */
235 1.17 dsl if (bplen < sizeof bp && memcmp((char *)&bp + bplen,
236 1.17 dsl (char *)&bp + bplen + 1,
237 1.17 dsl sizeof bp - bplen - 1) != 0) {
238 1.17 dsl warnx("Patch area in stage1 bootstrap is too small");
239 1.17 dsl goto done;
240 1.17 dsl }
241 1.17 dsl memcpy(bpp, &bp, bplen);
242 1.1 dsl
243 1.1 dsl if (params->flags & IB_NOWRITE) {
244 1.1 dsl retval = 1;
245 1.1 dsl goto done;
246 1.1 dsl }
247 1.1 dsl
248 1.9 lukem /*
249 1.9 lukem * Write MBR code to sector zero.
250 1.9 lukem */
251 1.1 dsl rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
252 1.1 dsl if (rv == -1) {
253 1.1 dsl warn("Writing `%s'", params->filesystem);
254 1.1 dsl goto done;
255 1.1 dsl } else if (rv != 512) {
256 1.1 dsl warnx("Writing `%s': short write", params->filesystem);
257 1.1 dsl goto done;
258 1.1 dsl }
259 1.1 dsl
260 1.9 lukem /*
261 1.9 lukem * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
262 1.9 lukem */
263 1.1 dsl rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
264 1.1 dsl bootstrapsize - 512 * 2, 512 * 2);
265 1.1 dsl if (rv == -1) {
266 1.1 dsl warn("Writing `%s'", params->filesystem);
267 1.1 dsl goto done;
268 1.1 dsl } else if (rv != bootstrapsize - 512 * 2) {
269 1.1 dsl warnx("Writing `%s': short write", params->filesystem);
270 1.1 dsl goto done;
271 1.1 dsl }
272 1.1 dsl
273 1.1 dsl retval = 1;
274 1.1 dsl
275 1.1 dsl done:
276 1.1 dsl if (bootstrapbuf)
277 1.1 dsl free(bootstrapbuf);
278 1.1 dsl return retval;
279 1.1 dsl }
280