xref: /netbsd-src/usr.sbin/installboot/arch/i386.c (revision b5ffb2e0d69029911b676d573c889e1b9b7934b7)
1*b5ffb2e0Smsaitoh /* $NetBSD: i386.c,v 1.43 2021/12/05 04:47:18 msaitoh Exp $ */
2f14ef8a1Sdsl 
3f14ef8a1Sdsl /*-
4f14ef8a1Sdsl  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5f14ef8a1Sdsl  * All rights reserved.
6f14ef8a1Sdsl  *
7f14ef8a1Sdsl  * This code is derived from software contributed to The NetBSD Foundation
8f14ef8a1Sdsl  * by David Laight.
9f14ef8a1Sdsl  *
10f14ef8a1Sdsl  * Redistribution and use in source and binary forms, with or without
11f14ef8a1Sdsl  * modification, are permitted provided that the following conditions
12f14ef8a1Sdsl  * are met:
13f14ef8a1Sdsl  * 1. Redistributions of source code must retain the above copyright
14f14ef8a1Sdsl  *    notice, this list of conditions and the following disclaimer.
15f14ef8a1Sdsl  * 2. Redistributions in binary form must reproduce the above copyright
16f14ef8a1Sdsl  *    notice, this list of conditions and the following disclaimer in the
17f14ef8a1Sdsl  *    documentation and/or other materials provided with the distribution.
18f14ef8a1Sdsl  *
19f14ef8a1Sdsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f14ef8a1Sdsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f14ef8a1Sdsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f14ef8a1Sdsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f14ef8a1Sdsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f14ef8a1Sdsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f14ef8a1Sdsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f14ef8a1Sdsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f14ef8a1Sdsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f14ef8a1Sdsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f14ef8a1Sdsl  * POSSIBILITY OF SUCH DAMAGE.
30f14ef8a1Sdsl  */
31f14ef8a1Sdsl 
32171d6532Slukem #if HAVE_NBTOOL_CONFIG_H
33171d6532Slukem #include "nbtool_config.h"
34f14ef8a1Sdsl #endif
35f14ef8a1Sdsl 
36171d6532Slukem #include <sys/cdefs.h>
37171d6532Slukem #if !defined(__lint)
38*b5ffb2e0Smsaitoh __RCSID("$NetBSD: i386.c,v 1.43 2021/12/05 04:47:18 msaitoh Exp $");
39c176c568Slukem #endif /* !__lint */
40171d6532Slukem 
41f14ef8a1Sdsl #include <sys/param.h>
42eacd9633Sdsl #ifndef HAVE_NBTOOL_CONFIG_H
43eacd9633Sdsl #include <sys/ioctl.h>
44eacd9633Sdsl #include <sys/dkio.h>
45eacd9633Sdsl #endif
46f14ef8a1Sdsl 
47f14ef8a1Sdsl #include <assert.h>
48eacd9633Sdsl #include <errno.h>
49f14ef8a1Sdsl #include <err.h>
50360968bcSbjh21 #include <md5.h>
51f14ef8a1Sdsl #include <stddef.h>
52f14ef8a1Sdsl #include <stdio.h>
53f14ef8a1Sdsl #include <stdlib.h>
54f14ef8a1Sdsl #include <string.h>
55f14ef8a1Sdsl #include <unistd.h>
56f14ef8a1Sdsl 
57f14ef8a1Sdsl #include "installboot.h"
58f14ef8a1Sdsl 
591d3b6feaSchristos static const struct console_name {
601d3b6feaSchristos 	const char	*name;		/* Name of console selection */
611d3b6feaSchristos 	const int	dev;		/* value matching CONSDEV_* from sys/arch/i386/stand/lib/libi386.h */
621d3b6feaSchristos } consoles[] = {
631d3b6feaSchristos 	{ "pc",		0 /* CONSDEV_PC */ },
641d3b6feaSchristos 	{ "com0",	1 /* CONSDEV_COM0 */ },
651d3b6feaSchristos 	{ "com1",	2 /* CONSDEV_COM1 */ },
661d3b6feaSchristos 	{ "com2",	3 /* CONSDEV_COM2 */ },
671d3b6feaSchristos 	{ "com3",	4 /* CONSDEV_COM3 */ },
681d3b6feaSchristos 	{ "com0kbd",	5 /* CONSDEV_COM0KBD */ },
691d3b6feaSchristos 	{ "com1kbd",	6 /* CONSDEV_COM1KBD */ },
701d3b6feaSchristos 	{ "com2kbd",	7 /* CONSDEV_COM2KBD */ },
711d3b6feaSchristos 	{ "com3kbd",	8 /* CONSDEV_COM3KBD */ },
721d3b6feaSchristos 	{ "auto",	-1 /* CONSDEV_AUTO */ },
731d3b6feaSchristos };
748c893c96Sdsl 
75cce659e2Sdsl static int i386_setboot(ib_params *);
76cce659e2Sdsl static int i386_editboot(ib_params *);
77cce659e2Sdsl 
78e5c09b19Sthorpej struct ib_mach ib_mach_i386 = {
79e5c09b19Sthorpej 	.name		=	"i386",
80e5c09b19Sthorpej 	.setboot	=	i386_setboot,
81e5c09b19Sthorpej 	.clearboot	=	no_clearboot,
82e5c09b19Sthorpej 	.editboot	=	i386_editboot,
83e5c09b19Sthorpej 	.valid_flags	=	IB_RESETVIDEO | IB_CONSOLE | IB_CONSPEED |
84e5c09b19Sthorpej 				    IB_CONSADDR | IB_KEYMAP | IB_PASSWORD |
85e5c09b19Sthorpej 				    IB_TIMEOUT | IB_MODULES | IB_BOOTCONF |
86e5c09b19Sthorpej 				    IB_STAGE1START
87e5c09b19Sthorpej };
88cce659e2Sdsl 
89e5c09b19Sthorpej struct ib_mach ib_mach_amd64 = {
90e5c09b19Sthorpej 	.name		=	"amd64",
91e5c09b19Sthorpej 	.setboot	=	i386_setboot,
92e5c09b19Sthorpej 	.clearboot	=	no_clearboot,
93e5c09b19Sthorpej 	.editboot	=	i386_editboot,
94e5c09b19Sthorpej 	.valid_flags	=	IB_RESETVIDEO | IB_CONSOLE | IB_CONSPEED |
95e5c09b19Sthorpej 				    IB_CONSADDR | IB_KEYMAP | IB_PASSWORD |
96e5c09b19Sthorpej 				    IB_TIMEOUT | IB_MODULES | IB_BOOTCONF |
97e5c09b19Sthorpej 				    IB_STAGE1START
98e5c09b19Sthorpej };
99cce659e2Sdsl 
100eacd9633Sdsl /*
101eacd9633Sdsl  * Attempting to write the 'labelsector' (or a sector near it - within 8k?)
102eacd9633Sdsl  * using the non-raw disk device fails silently.  This can be detected (today)
103eacd9633Sdsl  * by doing a fsync() and a read back.
104eacd9633Sdsl  * This is very likely to affect installboot, indeed the code may need to
105eacd9633Sdsl  * be written into the 'labelsector' itself - especially on non-512 byte media.
106eacd9633Sdsl  * We do all writes with a read verify.
107eacd9633Sdsl  * If EROFS is returned we also try to enable writes to the label sector.
108eacd9633Sdsl  * (Maybe these functions should be in the generic part of installboot.)
109eacd9633Sdsl  */
110eacd9633Sdsl static int
pwrite_validate(int fd,const void * buf,size_t n_bytes,off_t offset)111eacd9633Sdsl pwrite_validate(int fd, const void *buf, size_t n_bytes, off_t offset)
112eacd9633Sdsl {
113eacd9633Sdsl 	void *r_buf;
114eacd9633Sdsl 	ssize_t rv;
115eacd9633Sdsl 
116eacd9633Sdsl 	r_buf = malloc(n_bytes);
117eacd9633Sdsl 	if (r_buf == NULL)
118eacd9633Sdsl 		return -1;
119eacd9633Sdsl 	rv = pwrite(fd, buf, n_bytes, offset);
120eacd9633Sdsl 	if (rv == -1) {
121eacd9633Sdsl 		free(r_buf);
122eacd9633Sdsl 		return -1;
123eacd9633Sdsl 	}
124eacd9633Sdsl 	fsync(fd);
125b86f9ffbSchristos 	if (pread(fd, r_buf, rv, offset) == rv && memcmp(r_buf, buf, rv) == 0) {
126b86f9ffbSchristos 		free(r_buf);
127eacd9633Sdsl 		return rv;
128b86f9ffbSchristos 	}
129b86f9ffbSchristos 	free(r_buf);
130eacd9633Sdsl 	errno = EROFS;
131eacd9633Sdsl 	return -1;
132eacd9633Sdsl }
133eacd9633Sdsl 
134eacd9633Sdsl static int
write_boot_area(ib_params * params,uint8_t * buf,size_t len)1357a3dc95eSchristos write_boot_area(ib_params *params, uint8_t *buf, size_t len)
136eacd9633Sdsl {
137eacd9633Sdsl 	int rv, i;
138eacd9633Sdsl 
139eacd9633Sdsl 	/*
140eacd9633Sdsl 	 * Writing the 'label' sector (likely to be bytes 512-1023) could
141eacd9633Sdsl 	 * fail, so we try to avoid writing that area.
142eacd9633Sdsl 	 * Unfortunately, if we are accessing the raw disk, and the sector
143eacd9633Sdsl 	 * size is larger than 512 bytes that is also doomed.
144eacd9633Sdsl 	 * See how we get on....
145eacd9633Sdsl 	 *
146eacd9633Sdsl 	 * NB: Even if the physical sector size is not 512, the space for
147eacd9633Sdsl 	 * the label is 512 bytes from the start of the disk.
148eacd9633Sdsl 	 * So all the '512' constants in these functions are correct.
149eacd9633Sdsl 	 */
150eacd9633Sdsl 
151eacd9633Sdsl 	/* Write out first 512 bytes - the pbr code */
152eacd9633Sdsl 	rv = pwrite_validate(params->fsfd, buf, 512, 0);
153eacd9633Sdsl 	if (rv == 512) {
154eacd9633Sdsl 		/* That worked, do the rest */
155eacd9633Sdsl 		if (len == 512)
156eacd9633Sdsl 			return 1;
157eacd9633Sdsl 		len -= 512 * 2;
158eacd9633Sdsl 		rv = pwrite_validate(params->fsfd, buf + 512 * 2, len, 512 * 2);
1597a3dc95eSchristos 		if (rv != (ssize_t)len)
160eacd9633Sdsl 			goto bad_write;
161eacd9633Sdsl 		return 1;
162eacd9633Sdsl 	}
163eacd9633Sdsl 	if (rv != -1 || (errno != EINVAL && errno != EROFS))
164eacd9633Sdsl 		goto bad_write;
165eacd9633Sdsl 
166eacd9633Sdsl 	if (errno == EINVAL) {
167eacd9633Sdsl 		/* Assume the failure was due to to the sector size > 512 */
168eacd9633Sdsl 		rv = pwrite_validate(params->fsfd, buf, len, 0);
1697a3dc95eSchristos 		if (rv == (ssize_t)len)
170eacd9633Sdsl 			return 1;
171eacd9633Sdsl 		if (rv != -1 || (errno != EROFS))
172eacd9633Sdsl 			goto bad_write;
173eacd9633Sdsl 	}
174eacd9633Sdsl 
175eacd9633Sdsl #ifdef DIOCWLABEL
176eacd9633Sdsl 	/* Pesky label is protected, try to unprotect it */
177eacd9633Sdsl 	i = 1;
178eacd9633Sdsl 	rv = ioctl(params->fsfd, DIOCWLABEL, &i);
179eacd9633Sdsl 	if (rv != 0) {
180eacd9633Sdsl 		warn("Cannot enable writes to the label sector");
181eacd9633Sdsl 		return 0;
182eacd9633Sdsl 	}
183eacd9633Sdsl 	/* Try again with label write-enabled */
184eacd9633Sdsl 	rv = pwrite_validate(params->fsfd, buf, len, 0);
185eacd9633Sdsl 
186eacd9633Sdsl 	/* Reset write-protext */
187eacd9633Sdsl 	i = 0;
188eacd9633Sdsl 	ioctl(params->fsfd, DIOCWLABEL, &i);
1897a3dc95eSchristos 	if (rv == (ssize_t)len)
190eacd9633Sdsl 		return 1;
191eacd9633Sdsl #endif
192eacd9633Sdsl 
193eacd9633Sdsl   bad_write:
194eacd9633Sdsl 	if (rv == -1)
195eacd9633Sdsl 		warn("Writing `%s'", params->filesystem);
196eacd9633Sdsl 	else
197eacd9633Sdsl 		warnx("Writing `%s': short write, %u bytes",
198eacd9633Sdsl 			params->filesystem, rv);
199eacd9633Sdsl 	return 0;
200eacd9633Sdsl }
201eacd9633Sdsl 
2028c893c96Sdsl static void
show_i386_boot_params(struct x86_boot_params * bpp)2038c893c96Sdsl show_i386_boot_params(struct x86_boot_params  *bpp)
2048c893c96Sdsl {
2051d3b6feaSchristos 	size_t i;
2068c893c96Sdsl 
2078c893c96Sdsl 	printf("Boot options:        ");
2088c893c96Sdsl 	printf("timeout %d, ", le32toh(bpp->bp_timeout));
2098c893c96Sdsl 	printf("flags %x, ", le32toh(bpp->bp_flags));
2108c893c96Sdsl 	printf("speed %d, ", le32toh(bpp->bp_conspeed));
2118c893c96Sdsl 	printf("ioaddr %x, ", le32toh(bpp->bp_consaddr));
2127a3dc95eSchristos 	for (i = 0; i < __arraycount(consoles); i++) {
2132b2f4703Slukem 		if (consoles[i].dev == (int)le32toh(bpp->bp_consdev))
2141d3b6feaSchristos 			break;
2151d3b6feaSchristos 	}
2167a3dc95eSchristos 	if (i == __arraycount(consoles))
2171d3b6feaSchristos 		printf("console %d\n", le32toh(bpp->bp_consdev));
2188c893c96Sdsl 	else
2191d3b6feaSchristos 		printf("console %s\n", consoles[i].name);
2208c893c96Sdsl 	if (bpp->bp_keymap[0])
2218c893c96Sdsl 		printf("                     keymap %s\n", bpp->bp_keymap);
2228c893c96Sdsl }
2238c893c96Sdsl 
2248c893c96Sdsl static int
is_zero(const uint8_t * p,unsigned int len)225eacd9633Sdsl is_zero(const uint8_t *p, unsigned int len)
226eacd9633Sdsl {
227eacd9633Sdsl 	return len == 0 || (p[0] == 0 && memcmp(p, p + 1, len - 1) == 0);
228eacd9633Sdsl }
229eacd9633Sdsl 
230eacd9633Sdsl static int
update_i386_boot_params(ib_params * params,struct x86_boot_params * bpp)2318c893c96Sdsl update_i386_boot_params(ib_params *params, struct x86_boot_params  *bpp)
2328c893c96Sdsl {
2338c893c96Sdsl 	struct x86_boot_params bp;
2342b2f4703Slukem 	uint32_t bplen;
2351d3b6feaSchristos 	size_t i;
2368c893c96Sdsl 
2378c893c96Sdsl 	bplen = le32toh(bpp->bp_length);
2388c893c96Sdsl 	if (bplen > sizeof bp)
2398c893c96Sdsl 		/* Ignore pad space in bootxx */
2408c893c96Sdsl 		bplen = sizeof bp;
2418c893c96Sdsl 
2428c893c96Sdsl 	/* Take (and update) local copy so we handle size mismatches */
2438c893c96Sdsl 	memset(&bp, 0, sizeof bp);
2448c893c96Sdsl 	memcpy(&bp, bpp, bplen);
2458c893c96Sdsl 
2468c893c96Sdsl 	if (params->flags & IB_TIMEOUT)
2478c893c96Sdsl 		bp.bp_timeout = htole32(params->timeout);
2488c893c96Sdsl 	if (params->flags & IB_RESETVIDEO)
2498c893c96Sdsl 		bp.bp_flags ^= htole32(X86_BP_FLAGS_RESET_VIDEO);
2508c893c96Sdsl 	if (params->flags & IB_CONSPEED)
2518c893c96Sdsl 		bp.bp_conspeed = htole32(params->conspeed);
2528c893c96Sdsl 	if (params->flags & IB_CONSADDR)
2538c893c96Sdsl 		bp.bp_consaddr = htole32(params->consaddr);
2548c893c96Sdsl 	if (params->flags & IB_CONSOLE) {
2557a3dc95eSchristos 		for (i = 0; i < __arraycount(consoles); i++)
2561d3b6feaSchristos 			if (strcmp(consoles[i].name, params->console) == 0)
2571d3b6feaSchristos 				break;
2581d3b6feaSchristos 
2597a3dc95eSchristos 		if (i == __arraycount(consoles)) {
2608c893c96Sdsl 			warnx("invalid console name, valid names are:");
2611d3b6feaSchristos 			(void)fprintf(stderr, "\t%s", consoles[0].name);
2624caffa5dSgson 			for (i = 1; i < __arraycount(consoles); i++)
2631d3b6feaSchristos 				(void)fprintf(stderr, ", %s", consoles[i].name);
2641d3b6feaSchristos 			(void)fprintf(stderr, "\n");
2658c893c96Sdsl 			return 1;
2668c893c96Sdsl 		}
2671d3b6feaSchristos 		bp.bp_consdev = htole32(consoles[i].dev);
2688c893c96Sdsl 	}
2698c893c96Sdsl 	if (params->flags & IB_PASSWORD) {
2708c893c96Sdsl 		if (params->password[0]) {
2718c893c96Sdsl 			MD5_CTX md5ctx;
2728c893c96Sdsl 			MD5Init(&md5ctx);
2738c893c96Sdsl 			MD5Update(&md5ctx, params->password,
2748c893c96Sdsl 			    strlen(params->password));
2758c893c96Sdsl 			MD5Final(bp.bp_password, &md5ctx);
2768c893c96Sdsl 			bp.bp_flags |= htole32(X86_BP_FLAGS_PASSWORD);
2778c893c96Sdsl 		} else {
2788c893c96Sdsl 			memset(&bp.bp_password, 0, sizeof bp.bp_password);
2798c893c96Sdsl 			bp.bp_flags &= ~htole32(X86_BP_FLAGS_PASSWORD);
2808c893c96Sdsl 		}
2818c893c96Sdsl 	}
2828c893c96Sdsl 	if (params->flags & IB_KEYMAP)
2838c893c96Sdsl 		strlcpy(bp.bp_keymap, params->keymap, sizeof bp.bp_keymap);
28479f027bbSdrochner 	if (params->flags & IB_MODULES)
28523174fa4Sdrochner 		bp.bp_flags ^= htole32(X86_BP_FLAGS_NOMODULES);
28679f027bbSdrochner 	if (params->flags & IB_BOOTCONF)
28723174fa4Sdrochner 		bp.bp_flags ^= htole32(X86_BP_FLAGS_NOBOOTCONF);
2888c893c96Sdsl 
2898c893c96Sdsl 	if (params->flags & (IB_NOWRITE | IB_VERBOSE))
2908c893c96Sdsl 		show_i386_boot_params(&bp);
2918c893c96Sdsl 
2928c893c96Sdsl 	/* Check we aren't trying to set anything we can't save */
293eacd9633Sdsl 	if (!is_zero((char *)&bp + bplen, sizeof bp - bplen)) {
2948c893c96Sdsl 		warnx("Patch area in stage1 bootstrap is too small");
2958c893c96Sdsl 		return 1;
2968c893c96Sdsl 	}
2978c893c96Sdsl 	memcpy(bpp, &bp, bplen);
2988c893c96Sdsl 	return 0;
2998c893c96Sdsl }
3008c893c96Sdsl 
301cce659e2Sdsl static int
i386_setboot(ib_params * params)302f14ef8a1Sdsl i386_setboot(ib_params *params)
303f14ef8a1Sdsl {
304eacd9633Sdsl 	unsigned int	u;
305f14ef8a1Sdsl 	ssize_t		rv;
306eacd9633Sdsl 	uint32_t	*magic, expected_magic;
307eacd9633Sdsl 	union {
3081c33b4e6Slukem 	    struct mbr_sector	mbr;
309eacd9633Sdsl 	    uint8_t		b[8192];
310eacd9633Sdsl 	} disk_buf, bootstrap;
311f14ef8a1Sdsl 
312f14ef8a1Sdsl 	assert(params != NULL);
313f14ef8a1Sdsl 	assert(params->fsfd != -1);
314f14ef8a1Sdsl 	assert(params->filesystem != NULL);
315f14ef8a1Sdsl 	assert(params->s1fd != -1);
316f14ef8a1Sdsl 	assert(params->stage1 != NULL);
317f14ef8a1Sdsl 
318ed45ba76Sdsl 	/*
3197c56c316Slukem 	 * There is only 8k of space in a FFSv1 partition (and ustarfs)
320ed45ba76Sdsl 	 * so ensure we don't splat over anything important.
321ed45ba76Sdsl 	 */
3222b2f4703Slukem 	if (params->s1stat.st_size > (off_t)(sizeof bootstrap)) {
323eacd9633Sdsl 		warnx("stage1 bootstrap `%s' (%u bytes) is larger than 8192 bytes",
324eacd9633Sdsl 			params->stage1, (unsigned int)params->s1stat.st_size);
325eacd9633Sdsl 		return 0;
326eacd9633Sdsl 	}
327eacd9633Sdsl 	if (params->s1stat.st_size < 3 * 512 && params->s1stat.st_size != 512) {
328eacd9633Sdsl 		warnx("stage1 bootstrap `%s' (%u bytes) is too small",
329eacd9633Sdsl 			params->stage1, (unsigned int)params->s1stat.st_size);
330eacd9633Sdsl 		return 0;
331f14ef8a1Sdsl 	}
3321c33b4e6Slukem 
333eacd9633Sdsl 	/* Read in the existing disk header and boot code */
334eacd9633Sdsl 	rv = pread(params->fsfd, &disk_buf, sizeof (disk_buf), 0);
335eacd9633Sdsl 	if (rv != sizeof(disk_buf)) {
336eacd9633Sdsl 		if (rv == -1)
3371c33b4e6Slukem 			warn("Reading `%s'", params->filesystem);
338eacd9633Sdsl 		else
339bee2705cSdogcow 			warnx("Reading `%s': short read, %ld bytes"
340bee2705cSdogcow 			    " (should be %ld)", params->filesystem, (long)rv,
341bee2705cSdogcow 			    (long)sizeof(disk_buf));
342eacd9633Sdsl 		return 0;
3431c33b4e6Slukem 	}
344eacd9633Sdsl 
345eacd9633Sdsl 	if (disk_buf.mbr.mbr_magic != le16toh(MBR_MAGIC)) {
3461c33b4e6Slukem 		if (params->flags & IB_VERBOSE) {
3471c33b4e6Slukem 			printf(
348eacd9633Sdsl 		    "Ignoring PBR with invalid magic in sector 0 of `%s'\n",
3491c33b4e6Slukem 			    params->filesystem);
3501c33b4e6Slukem 		}
351eacd9633Sdsl 		memset(&disk_buf, 0, 512);
3521c33b4e6Slukem 	}
3531c33b4e6Slukem 
354eacd9633Sdsl 	/* Read the new bootstrap code. */
355eacd9633Sdsl 	rv = pread(params->s1fd, &bootstrap, params->s1stat.st_size, 0);
356eacd9633Sdsl 	if (rv != params->s1stat.st_size) {
357eacd9633Sdsl 		if (rv == -1)
358f14ef8a1Sdsl 			warn("Reading `%s'", params->stage1);
359eacd9633Sdsl 		else
360bee2705cSdogcow 			warnx("Reading `%s': short read, %ld bytes"
361bee2705cSdogcow 			    " (should be %ld)", params->stage1, (long)rv,
362bee2705cSdogcow 			    (long)params->s1stat.st_size);
363eacd9633Sdsl 		return 0;
364f14ef8a1Sdsl 	}
365f14ef8a1Sdsl 
366eacd9633Sdsl 	/*
367eacd9633Sdsl 	 * The bootstrap code is either 512 bytes for booting FAT16, or best
368eacd9633Sdsl 	 * part of 8k (with bytes 512-1023 all zeros).
369eacd9633Sdsl 	 */
370eacd9633Sdsl 	if (params->s1stat.st_size == 512) {
371eacd9633Sdsl 		/* Magic number is at end of pbr code */
372eacd9633Sdsl 		magic = (void *)(bootstrap.b + 512 - 16 + 4);
373eacd9633Sdsl 		expected_magic = htole32(X86_BOOT_MAGIC_FAT);
374eacd9633Sdsl 	} else {
375eacd9633Sdsl 		/* Magic number is at start of sector following label */
376eacd9633Sdsl 		magic = (void *)(bootstrap.b + 512 * 2 + 4);
377eacd9633Sdsl 		expected_magic = htole32(X86_BOOT_MAGIC_1);
378eacd9633Sdsl 		/*
379eacd9633Sdsl 		 * For a variety of reasons we restrict our 'normal' partition
380eacd9633Sdsl 		 * boot code to a size which enable it to be used as mbr code.
381eacd9633Sdsl 		 * IMHO this is bugus (dsl).
382eacd9633Sdsl 		 */
383eacd9633Sdsl 		if (!is_zero(bootstrap.b + 512-2-64, 64)) {
384eacd9633Sdsl 			warnx("Data in mbr partition table of new bootstrap");
385eacd9633Sdsl 			return 0;
386eacd9633Sdsl 		}
387eacd9633Sdsl 		if (!is_zero(bootstrap.b + 512, 512)) {
388eacd9633Sdsl 			warnx("Data in label part of new bootstrap");
389eacd9633Sdsl 			return 0;
390eacd9633Sdsl 		}
391eacd9633Sdsl 		/* Copy mbr table and label from existing disk buffer */
392eacd9633Sdsl 		memcpy(bootstrap.b + 512-2-64, disk_buf.b + 512-2-64, 64);
393eacd9633Sdsl 		memcpy(bootstrap.b + 512, disk_buf.b + 512, 512);
394eacd9633Sdsl 	}
395eacd9633Sdsl 
396eacd9633Sdsl 	/* Validate the 'magic number' that marks the parameter block */
397eacd9633Sdsl 	if (*magic != expected_magic) {
3986c51ef9cSdsl 		warnx("Invalid magic in stage1 bootstrap %x != %x",
399eacd9633Sdsl 				*magic, expected_magic);
400eacd9633Sdsl 		return 0;
401f14ef8a1Sdsl 	}
402f14ef8a1Sdsl 
4031c33b4e6Slukem 	/*
404a72f7164Smbalmer 	 * If the partition has a FAT (or NTFS) filesystem, then we must
405486d4119Sdsl 	 * preserve the BIOS Parameter Block (BPB).
406486d4119Sdsl 	 * It is also very likely that there isn't 8k of space available
407486d4119Sdsl 	 * for (say) bootxx_msdos, and that blindly installing it will trash
408486d4119Sdsl 	 * the FAT filesystem.
409486d4119Sdsl 	 * To avoid this we check the number of 'reserved' sectors to ensure
410486d4119Sdsl 	 * there there is enough space.
411486d4119Sdsl 	 * Unfortunately newfs(8) doesn't (yet) splat the BPB (which is
412486d4119Sdsl 	 * effectively the FAT superblock) when a filesystem is initailised
413486d4119Sdsl 	 * so this code tends to complain rather too often,
414486d4119Sdsl 	 * Specifying 'installboot -f' will delete the old BPB info.
415486d4119Sdsl 	 */
416486d4119Sdsl 	if (!(params->flags & IB_FORCE)) {
417f8907c6fSdsl 		#define USE_F ", use -f (may invalidate filesystem)"
418486d4119Sdsl 		/*
419486d4119Sdsl 		 * For FAT compatibility, the pbr code starts 'jmp xx; nop'
420486d4119Sdsl 		 * followed by the BIOS Parameter Block (BPB).
421eacd9633Sdsl 		 * The 2nd byte (jump offset) is the size of the nop + BPB.
4221c33b4e6Slukem 		 */
423eacd9633Sdsl 		if (bootstrap.b[0] != 0xeb || bootstrap.b[2] != 0x90) {
424f8907c6fSdsl 			warnx("No BPB in new bootstrap %02x:%02x:%02x" USE_F,
425eacd9633Sdsl 				bootstrap.b[0], bootstrap.b[1], bootstrap.b[2]);
426eacd9633Sdsl 			return 0;
4271c33b4e6Slukem 		}
4281c33b4e6Slukem 
42949b4c5f8Skamil 		/*
43049b4c5f8Skamil 		 * Find size of old BPB, and copy into new bootcode
43149b4c5f8Skamil 		 *
43249b4c5f8Skamil 		 * The 2nd byte (b[1]) contains jmp short relative offset.
43349b4c5f8Skamil 		 * If it is zero or some invalid input that is smaller than 9,
43449b4c5f8Skamil 		 * it will cause overflow and call is_zero() with enormous size.
43549b4c5f8Skamil 		 * Add a paranoid check to prevent this scenario.
43649b4c5f8Skamil 		 *
43749b4c5f8Skamil 		 * Verify that b[0] contains JMP (0xeb) and b[2] NOP (0x90).
43849b4c5f8Skamil 		 */
43949b4c5f8Skamil 		if (disk_buf.b[0] == 0xeb && disk_buf.b[1] >= 9 &&
44049b4c5f8Skamil 		    disk_buf.b[2] == 0x90 &&
44149b4c5f8Skamil 		    !is_zero(disk_buf.b + 3 + 8, disk_buf.b[1] - 1 - 8)) {
442eacd9633Sdsl 			struct mbr_bpbFAT16 *bpb = (void *)(disk_buf.b + 3 + 8);
443486d4119Sdsl 			/* Check enough space before the FAT for the bootcode */
444486d4119Sdsl 			u = le16toh(bpb->bpbBytesPerSec)
445486d4119Sdsl 			    * le16toh(bpb->bpbResSectors);
446eacd9633Sdsl 			if (u != 0 && u < params->s1stat.st_size) {
447f8907c6fSdsl 				warnx("Insufficient reserved space before FAT "
448f8907c6fSdsl 					"(%u bytes available)" USE_F, u);
449eacd9633Sdsl 				return 0;
45010e10faeSlukem 			}
451eacd9633Sdsl 			/* Check we have enough space for the old bpb */
452eacd9633Sdsl 			if (disk_buf.b[1] > bootstrap.b[1]) {
453eacd9633Sdsl 				/* old BPB is larger, allow if extra zeros */
454eacd9633Sdsl 				if (!is_zero(disk_buf.b + 2 + bootstrap.b[1],
455eacd9633Sdsl 				    disk_buf.b[1] - bootstrap.b[1])) {
456f8907c6fSdsl 					warnx("Old BPB too big" USE_F);
457eacd9633Sdsl 					    return 0;
458eacd9633Sdsl 				}
459eacd9633Sdsl 				u = bootstrap.b[1];
460eacd9633Sdsl 			} else {
461eacd9633Sdsl 				/* Old BPB is shorter, leave zero filled */
462eacd9633Sdsl 				u = disk_buf.b[1];
463eacd9633Sdsl 			}
464095ac4e7Sdsl 			if (params->s1start != 0)
465*b5ffb2e0Smsaitoh 				/* Fixup physical offset of filesystem */
466095ac4e7Sdsl 				bpb->bpbHiddenSecs = htole32(params->s1start);
467eacd9633Sdsl 			memcpy(bootstrap.b + 2, disk_buf.b + 2, u);
468eacd9633Sdsl 		}
469f8907c6fSdsl 		#undef USE_F
470486d4119Sdsl 	}
4711c33b4e6Slukem 
4721c33b4e6Slukem 	/*
473709e718dSlukem 	 * Fill in any user-specified options into the
474709e718dSlukem 	 *      struct x86_boot_params
475eacd9633Sdsl 	 * that follows the magic number.
476709e718dSlukem 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
4771c33b4e6Slukem 	 */
478eacd9633Sdsl 	if (update_i386_boot_params(params, (void *)(magic + 1)))
479eacd9633Sdsl 		return 0;
480ed45ba76Sdsl 
481f14ef8a1Sdsl 	if (params->flags & IB_NOWRITE) {
482eacd9633Sdsl 		return 1;
483f14ef8a1Sdsl 	}
484f14ef8a1Sdsl 
485eacd9633Sdsl 	/* Copy new bootstrap data into disk buffer, ignoring label area */
486eacd9633Sdsl 	memcpy(&disk_buf, &bootstrap, 512);
487eacd9633Sdsl 	if (params->s1stat.st_size > 512 * 2) {
488eacd9633Sdsl 		memcpy(disk_buf.b + 2 * 512, bootstrap.b + 2 * 512,
489eacd9633Sdsl 		    params->s1stat.st_size - 2 * 512);
490eacd9633Sdsl 		/* Zero pad to 512 byte sector boundary */
491eacd9633Sdsl 		memset(disk_buf.b + params->s1stat.st_size, 0,
492eacd9633Sdsl 			(8192 - params->s1stat.st_size) & 511);
493f14ef8a1Sdsl 	}
494f14ef8a1Sdsl 
4957a3dc95eSchristos 	return write_boot_area(params, disk_buf.b, sizeof disk_buf.b);
496f14ef8a1Sdsl }
4978c893c96Sdsl 
498cce659e2Sdsl static int
i386_editboot(ib_params * params)4998c893c96Sdsl i386_editboot(ib_params *params)
5008c893c96Sdsl {
5018c893c96Sdsl 	int		retval;
5028c893c96Sdsl 	uint8_t		buf[512];
5038c893c96Sdsl 	ssize_t		rv;
5048c893c96Sdsl 	uint32_t	magic;
5058c893c96Sdsl 	uint32_t	offset;
5068c893c96Sdsl 	struct x86_boot_params	*bpp;
5078c893c96Sdsl 
5088c893c96Sdsl 	assert(params != NULL);
5098c893c96Sdsl 	assert(params->fsfd != -1);
5108c893c96Sdsl 	assert(params->filesystem != NULL);
5118c893c96Sdsl 
5128c893c96Sdsl 	retval = 0;
5138c893c96Sdsl 
5148c893c96Sdsl 	/*
5158c893c96Sdsl 	 * Read in the existing bootstrap.
51616ca1f6dSdsl 	 * Look in any of the first 4 sectors.
5178c893c96Sdsl 	 */
5188c893c96Sdsl 
5198c893c96Sdsl 	bpp = NULL;
5208c893c96Sdsl 	for (offset = 0; offset < 4 * 512; offset += 512) {
5218c893c96Sdsl 		rv = pread(params->fsfd, &buf, sizeof buf, offset);
5228c893c96Sdsl 		if (rv == -1) {
5238c893c96Sdsl 			warn("Reading `%s'", params->filesystem);
5248c893c96Sdsl 			goto done;
5258c893c96Sdsl 		} else if (rv != sizeof buf) {
5268c893c96Sdsl 			warnx("Reading `%s': short read", params->filesystem);
5278c893c96Sdsl 			goto done;
5288c893c96Sdsl 		}
5298c893c96Sdsl 
53016ca1f6dSdsl 		/* Magic number is 4 bytes in (to allow for a jmps) */
53116ca1f6dSdsl 		/* Also allow any of the magic numbers. */
53216ca1f6dSdsl 		magic = le32toh(*(uint32_t *)(buf + 4)) | 0xf;
53316ca1f6dSdsl 		if (magic != (X86_BOOT_MAGIC_1 | 0xf))
5348c893c96Sdsl 			continue;
53516ca1f6dSdsl 
53616ca1f6dSdsl 		/* The parameters are just after the magic number */
5378c893c96Sdsl 		bpp = (void *)(buf + 8);
5388c893c96Sdsl 		break;
5398c893c96Sdsl 	}
5408c893c96Sdsl 	if (bpp == NULL) {
54116ca1f6dSdsl 		warnx("Invalid magic in existing bootstrap");
5428c893c96Sdsl 		goto done;
5438c893c96Sdsl 	}
5448c893c96Sdsl 
5458c893c96Sdsl 	/*
5468c893c96Sdsl 	 * Fill in any user-specified options into the
5478c893c96Sdsl 	 *      struct x86_boot_params
5488c893c96Sdsl 	 * that's 8 bytes in from the start of the third sector.
5498c893c96Sdsl 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
5508c893c96Sdsl 	 */
5518c893c96Sdsl 	if (update_i386_boot_params(params, bpp))
5528c893c96Sdsl 		goto done;
5538c893c96Sdsl 
5548c893c96Sdsl 	if (params->flags & IB_NOWRITE) {
5558c893c96Sdsl 		retval = 1;
5568c893c96Sdsl 		goto done;
5578c893c96Sdsl 	}
5588c893c96Sdsl 
5598c893c96Sdsl 	/*
5608c893c96Sdsl 	 * Write boot code back
5618c893c96Sdsl 	 */
5628c893c96Sdsl 	rv = pwrite(params->fsfd, buf, sizeof buf, offset);
5638c893c96Sdsl 	if (rv == -1) {
5648c893c96Sdsl 		warn("Writing `%s'", params->filesystem);
5658c893c96Sdsl 		goto done;
5668c893c96Sdsl 	} else if (rv != sizeof buf) {
5677a3dc95eSchristos 		warnx("Writing `%s': short write, %zd bytes (should be %zu)",
5687a3dc95eSchristos 		    params->filesystem, rv, sizeof(buf));
5698c893c96Sdsl 		goto done;
5708c893c96Sdsl 	}
5718c893c96Sdsl 
5728c893c96Sdsl 	retval = 1;
5738c893c96Sdsl 
5748c893c96Sdsl  done:
5758c893c96Sdsl 	return retval;
5768c893c96Sdsl }
577