Home | History | Annotate | Line # | Download | only in arch
i386.c revision 1.40
      1  1.40   msaitoh /* $NetBSD: i386.c,v 1.40 2013/06/14 03:54:43 msaitoh 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  *
     19   1.1       dsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1       dsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1       dsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1       dsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1       dsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1       dsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1       dsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1       dsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1       dsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1       dsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1       dsl  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       dsl  */
     31   1.1       dsl 
     32  1.12     lukem #if HAVE_NBTOOL_CONFIG_H
     33  1.12     lukem #include "nbtool_config.h"
     34  1.12     lukem #endif
     35  1.12     lukem 
     36   1.1       dsl #include <sys/cdefs.h>
     37  1.12     lukem #if !defined(__lint)
     38  1.40   msaitoh __RCSID("$NetBSD: i386.c,v 1.40 2013/06/14 03:54:43 msaitoh Exp $");
     39  1.13     lukem #endif /* !__lint */
     40   1.1       dsl 
     41   1.1       dsl #include <sys/param.h>
     42  1.23       dsl #ifndef HAVE_NBTOOL_CONFIG_H
     43  1.23       dsl #include <sys/ioctl.h>
     44  1.23       dsl #include <sys/dkio.h>
     45  1.23       dsl #endif
     46   1.1       dsl 
     47   1.1       dsl #include <assert.h>
     48  1.23       dsl #include <errno.h>
     49   1.1       dsl #include <err.h>
     50   1.5     bjh21 #include <md5.h>
     51   1.1       dsl #include <stddef.h>
     52   1.1       dsl #include <stdio.h>
     53   1.1       dsl #include <stdlib.h>
     54   1.1       dsl #include <string.h>
     55   1.1       dsl #include <unistd.h>
     56   1.1       dsl 
     57   1.1       dsl #include "installboot.h"
     58   1.1       dsl 
     59  1.26  christos static const struct console_name {
     60  1.26  christos 	const char	*name;		/* Name of console selection */
     61  1.26  christos 	const int	dev;		/* value matching CONSDEV_* from sys/arch/i386/stand/lib/libi386.h */
     62  1.26  christos } consoles[] = {
     63  1.26  christos 	{ "pc",		0 /* CONSDEV_PC */ },
     64  1.26  christos 	{ "com0",	1 /* CONSDEV_COM0 */ },
     65  1.26  christos 	{ "com1",	2 /* CONSDEV_COM1 */ },
     66  1.26  christos 	{ "com2",	3 /* CONSDEV_COM2 */ },
     67  1.26  christos 	{ "com3",	4 /* CONSDEV_COM3 */ },
     68  1.26  christos 	{ "com0kbd",	5 /* CONSDEV_COM0KBD */ },
     69  1.26  christos 	{ "com1kbd",	6 /* CONSDEV_COM1KBD */ },
     70  1.26  christos 	{ "com2kbd",	7 /* CONSDEV_COM2KBD */ },
     71  1.26  christos 	{ "com3kbd",	8 /* CONSDEV_COM3KBD */ },
     72  1.26  christos 	{ "auto",	-1 /* CONSDEV_AUTO */ },
     73  1.26  christos };
     74  1.19       dsl 
     75  1.22       dsl static int i386_setboot(ib_params *);
     76  1.22       dsl static int i386_editboot(ib_params *);
     77  1.22       dsl 
     78  1.22       dsl struct ib_mach ib_mach_i386 =
     79  1.22       dsl 	{ "i386", i386_setboot, no_clearboot, i386_editboot,
     80  1.22       dsl 		IB_RESETVIDEO | IB_CONSOLE | IB_CONSPEED | IB_CONSADDR |
     81  1.35  drochner 		IB_KEYMAP | IB_PASSWORD | IB_TIMEOUT |
     82  1.38       dsl 		IB_MODULES | IB_BOOTCONF |
     83  1.38       dsl 		IB_STAGE1START };
     84  1.22       dsl 
     85  1.22       dsl struct ib_mach ib_mach_amd64 =
     86  1.22       dsl 	{ "amd64", i386_setboot, no_clearboot, i386_editboot,
     87  1.22       dsl 		IB_RESETVIDEO | IB_CONSOLE | IB_CONSPEED | IB_CONSADDR |
     88  1.35  drochner 		IB_KEYMAP | IB_PASSWORD | IB_TIMEOUT |
     89  1.38       dsl 		IB_MODULES | IB_BOOTCONF |
     90  1.38       dsl 		IB_STAGE1START };
     91  1.22       dsl 
     92  1.23       dsl /*
     93  1.23       dsl  * Attempting to write the 'labelsector' (or a sector near it - within 8k?)
     94  1.23       dsl  * using the non-raw disk device fails silently.  This can be detected (today)
     95  1.23       dsl  * by doing a fsync() and a read back.
     96  1.23       dsl  * This is very likely to affect installboot, indeed the code may need to
     97  1.23       dsl  * be written into the 'labelsector' itself - especially on non-512 byte media.
     98  1.23       dsl  * We do all writes with a read verify.
     99  1.23       dsl  * If EROFS is returned we also try to enable writes to the label sector.
    100  1.23       dsl  * (Maybe these functions should be in the generic part of installboot.)
    101  1.23       dsl  */
    102  1.23       dsl static int
    103  1.23       dsl pwrite_validate(int fd, const void *buf, size_t n_bytes, off_t offset)
    104  1.23       dsl {
    105  1.23       dsl 	void *r_buf;
    106  1.23       dsl 	ssize_t rv;
    107  1.23       dsl 
    108  1.23       dsl 	r_buf = malloc(n_bytes);
    109  1.23       dsl 	if (r_buf == NULL)
    110  1.23       dsl 		return -1;
    111  1.23       dsl 	rv = pwrite(fd, buf, n_bytes, offset);
    112  1.23       dsl 	if (rv == -1) {
    113  1.23       dsl 		free(r_buf);
    114  1.23       dsl 		return -1;
    115  1.23       dsl 	}
    116  1.23       dsl 	fsync(fd);
    117  1.31  christos 	if (pread(fd, r_buf, rv, offset) == rv && memcmp(r_buf, buf, rv) == 0) {
    118  1.31  christos 		free(r_buf);
    119  1.23       dsl 		return rv;
    120  1.31  christos 	}
    121  1.31  christos 	free(r_buf);
    122  1.23       dsl 	errno = EROFS;
    123  1.23       dsl 	return -1;
    124  1.23       dsl }
    125  1.23       dsl 
    126  1.23       dsl static int
    127  1.37  christos write_boot_area(ib_params *params, uint8_t *buf, size_t len)
    128  1.23       dsl {
    129  1.23       dsl 	int rv, i;
    130  1.23       dsl 
    131  1.23       dsl 	/*
    132  1.23       dsl 	 * Writing the 'label' sector (likely to be bytes 512-1023) could
    133  1.23       dsl 	 * fail, so we try to avoid writing that area.
    134  1.23       dsl 	 * Unfortunately, if we are accessing the raw disk, and the sector
    135  1.23       dsl 	 * size is larger than 512 bytes that is also doomed.
    136  1.23       dsl 	 * See how we get on....
    137  1.23       dsl 	 *
    138  1.23       dsl 	 * NB: Even if the physical sector size is not 512, the space for
    139  1.23       dsl 	 * the label is 512 bytes from the start of the disk.
    140  1.23       dsl 	 * So all the '512' constants in these functions are correct.
    141  1.23       dsl 	 */
    142  1.23       dsl 
    143  1.23       dsl 	/* Write out first 512 bytes - the pbr code */
    144  1.23       dsl 	rv = pwrite_validate(params->fsfd, buf, 512, 0);
    145  1.23       dsl 	if (rv == 512) {
    146  1.23       dsl 		/* That worked, do the rest */
    147  1.23       dsl 		if (len == 512)
    148  1.23       dsl 			return 1;
    149  1.23       dsl 		len -= 512 * 2;
    150  1.23       dsl 		rv = pwrite_validate(params->fsfd, buf + 512 * 2, len, 512 * 2);
    151  1.37  christos 		if (rv != (ssize_t)len)
    152  1.23       dsl 			goto bad_write;
    153  1.23       dsl 		return 1;
    154  1.23       dsl 	}
    155  1.23       dsl 	if (rv != -1 || (errno != EINVAL && errno != EROFS))
    156  1.23       dsl 		goto bad_write;
    157  1.23       dsl 
    158  1.23       dsl 	if (errno == EINVAL) {
    159  1.23       dsl 		/* Assume the failure was due to to the sector size > 512 */
    160  1.23       dsl 		rv = pwrite_validate(params->fsfd, buf, len, 0);
    161  1.37  christos 		if (rv == (ssize_t)len)
    162  1.23       dsl 			return 1;
    163  1.23       dsl 		if (rv != -1 || (errno != EROFS))
    164  1.23       dsl 			goto bad_write;
    165  1.23       dsl 	}
    166  1.23       dsl 
    167  1.23       dsl #ifdef DIOCWLABEL
    168  1.23       dsl 	/* Pesky label is protected, try to unprotect it */
    169  1.23       dsl 	i = 1;
    170  1.23       dsl 	rv = ioctl(params->fsfd, DIOCWLABEL, &i);
    171  1.23       dsl 	if (rv != 0) {
    172  1.23       dsl 		warn("Cannot enable writes to the label sector");
    173  1.23       dsl 		return 0;
    174  1.23       dsl 	}
    175  1.23       dsl 	/* Try again with label write-enabled */
    176  1.23       dsl 	rv = pwrite_validate(params->fsfd, buf, len, 0);
    177  1.23       dsl 
    178  1.23       dsl 	/* Reset write-protext */
    179  1.23       dsl 	i = 0;
    180  1.23       dsl 	ioctl(params->fsfd, DIOCWLABEL, &i);
    181  1.37  christos 	if (rv == (ssize_t)len)
    182  1.23       dsl 		return 1;
    183  1.23       dsl #endif
    184  1.23       dsl 
    185  1.23       dsl   bad_write:
    186  1.23       dsl 	if (rv == -1)
    187  1.23       dsl 		warn("Writing `%s'", params->filesystem);
    188  1.40   msaitoh 	else
    189  1.23       dsl 		warnx("Writing `%s': short write, %u bytes",
    190  1.23       dsl 			params->filesystem, rv);
    191  1.23       dsl 	return 0;
    192  1.23       dsl }
    193  1.23       dsl 
    194  1.19       dsl static void
    195  1.19       dsl show_i386_boot_params(struct x86_boot_params  *bpp)
    196  1.19       dsl {
    197  1.26  christos 	size_t i;
    198  1.19       dsl 
    199  1.19       dsl 	printf("Boot options:        ");
    200  1.19       dsl 	printf("timeout %d, ", le32toh(bpp->bp_timeout));
    201  1.19       dsl 	printf("flags %x, ", le32toh(bpp->bp_flags));
    202  1.19       dsl 	printf("speed %d, ", le32toh(bpp->bp_conspeed));
    203  1.19       dsl 	printf("ioaddr %x, ", le32toh(bpp->bp_consaddr));
    204  1.37  christos 	for (i = 0; i < __arraycount(consoles); i++) {
    205  1.32     lukem 		if (consoles[i].dev == (int)le32toh(bpp->bp_consdev))
    206  1.26  christos 			break;
    207  1.26  christos 	}
    208  1.37  christos 	if (i == __arraycount(consoles))
    209  1.26  christos 		printf("console %d\n", le32toh(bpp->bp_consdev));
    210  1.19       dsl 	else
    211  1.26  christos 		printf("console %s\n", consoles[i].name);
    212  1.19       dsl 	if (bpp->bp_keymap[0])
    213  1.19       dsl 		printf("                     keymap %s\n", bpp->bp_keymap);
    214  1.19       dsl }
    215  1.19       dsl 
    216  1.19       dsl static int
    217  1.23       dsl is_zero(const uint8_t *p, unsigned int len)
    218  1.23       dsl {
    219  1.23       dsl 	return len == 0 || (p[0] == 0 && memcmp(p, p + 1, len - 1) == 0);
    220  1.23       dsl }
    221  1.23       dsl 
    222  1.23       dsl static int
    223  1.19       dsl update_i386_boot_params(ib_params *params, struct x86_boot_params  *bpp)
    224  1.19       dsl {
    225  1.19       dsl 	struct x86_boot_params bp;
    226  1.32     lukem 	uint32_t bplen;
    227  1.26  christos 	size_t i;
    228  1.19       dsl 
    229  1.19       dsl 	bplen = le32toh(bpp->bp_length);
    230  1.19       dsl 	if (bplen > sizeof bp)
    231  1.19       dsl 		/* Ignore pad space in bootxx */
    232  1.19       dsl 		bplen = sizeof bp;
    233  1.19       dsl 
    234  1.19       dsl 	/* Take (and update) local copy so we handle size mismatches */
    235  1.19       dsl 	memset(&bp, 0, sizeof bp);
    236  1.19       dsl 	memcpy(&bp, bpp, bplen);
    237  1.19       dsl 
    238  1.19       dsl 	if (params->flags & IB_TIMEOUT)
    239  1.19       dsl 		bp.bp_timeout = htole32(params->timeout);
    240  1.19       dsl 	if (params->flags & IB_RESETVIDEO)
    241  1.19       dsl 		bp.bp_flags ^= htole32(X86_BP_FLAGS_RESET_VIDEO);
    242  1.19       dsl 	if (params->flags & IB_CONSPEED)
    243  1.19       dsl 		bp.bp_conspeed = htole32(params->conspeed);
    244  1.19       dsl 	if (params->flags & IB_CONSADDR)
    245  1.19       dsl 		bp.bp_consaddr = htole32(params->consaddr);
    246  1.19       dsl 	if (params->flags & IB_CONSOLE) {
    247  1.37  christos 		for (i = 0; i < __arraycount(consoles); i++)
    248  1.26  christos 			if (strcmp(consoles[i].name, params->console) == 0)
    249  1.19       dsl 				break;
    250  1.26  christos 
    251  1.37  christos 		if (i == __arraycount(consoles)) {
    252  1.26  christos 			warnx("invalid console name, valid names are:");
    253  1.26  christos 			(void)fprintf(stderr, "\t%s", consoles[0].name);
    254  1.39      gson 			for (i = 1; i < __arraycount(consoles); i++)
    255  1.26  christos 				(void)fprintf(stderr, ", %s", consoles[i].name);
    256  1.26  christos 			(void)fprintf(stderr, "\n");
    257  1.26  christos 			return 1;
    258  1.19       dsl 		}
    259  1.26  christos 		bp.bp_consdev = htole32(consoles[i].dev);
    260  1.19       dsl 	}
    261  1.19       dsl 	if (params->flags & IB_PASSWORD) {
    262  1.19       dsl 		if (params->password[0]) {
    263  1.19       dsl 			MD5_CTX md5ctx;
    264  1.19       dsl 			MD5Init(&md5ctx);
    265  1.19       dsl 			MD5Update(&md5ctx, params->password,
    266  1.19       dsl 			    strlen(params->password));
    267  1.19       dsl 			MD5Final(bp.bp_password, &md5ctx);
    268  1.19       dsl 			bp.bp_flags |= htole32(X86_BP_FLAGS_PASSWORD);
    269  1.19       dsl 		} else {
    270  1.19       dsl 			memset(&bp.bp_password, 0, sizeof bp.bp_password);
    271  1.19       dsl 			bp.bp_flags &= ~htole32(X86_BP_FLAGS_PASSWORD);
    272  1.19       dsl 		}
    273  1.19       dsl 	}
    274  1.19       dsl 	if (params->flags & IB_KEYMAP)
    275  1.19       dsl 		strlcpy(bp.bp_keymap, params->keymap, sizeof bp.bp_keymap);
    276  1.35  drochner 	if (params->flags & IB_MODULES)
    277  1.36  drochner 		bp.bp_flags ^= htole32(X86_BP_FLAGS_NOMODULES);
    278  1.35  drochner 	if (params->flags & IB_BOOTCONF)
    279  1.36  drochner 		bp.bp_flags ^= htole32(X86_BP_FLAGS_NOBOOTCONF);
    280  1.19       dsl 
    281  1.19       dsl 	if (params->flags & (IB_NOWRITE | IB_VERBOSE))
    282  1.19       dsl 		show_i386_boot_params(&bp);
    283  1.19       dsl 
    284  1.19       dsl 	/* Check we aren't trying to set anything we can't save */
    285  1.23       dsl 	if (!is_zero((char *)&bp + bplen, sizeof bp - bplen)) {
    286  1.19       dsl 		warnx("Patch area in stage1 bootstrap is too small");
    287  1.19       dsl 		return 1;
    288  1.19       dsl 	}
    289  1.19       dsl 	memcpy(bpp, &bp, bplen);
    290  1.19       dsl 	return 0;
    291  1.19       dsl }
    292  1.19       dsl 
    293  1.22       dsl static int
    294   1.1       dsl i386_setboot(ib_params *params)
    295   1.1       dsl {
    296  1.23       dsl 	unsigned int	u;
    297   1.1       dsl 	ssize_t		rv;
    298  1.23       dsl 	uint32_t	*magic, expected_magic;
    299  1.23       dsl 	union {
    300  1.23       dsl 	    struct mbr_sector	mbr;
    301  1.23       dsl 	    uint8_t		b[8192];
    302  1.23       dsl 	} disk_buf, bootstrap;
    303   1.1       dsl 
    304   1.1       dsl 	assert(params != NULL);
    305   1.1       dsl 	assert(params->fsfd != -1);
    306   1.1       dsl 	assert(params->filesystem != NULL);
    307   1.1       dsl 	assert(params->s1fd != -1);
    308   1.1       dsl 	assert(params->stage1 != NULL);
    309   1.1       dsl 
    310   1.2       dsl 	/*
    311  1.33     lukem 	 * There is only 8k of space in a FFSv1 partition (and ustarfs)
    312   1.2       dsl 	 * so ensure we don't splat over anything important.
    313   1.2       dsl 	 */
    314  1.32     lukem 	if (params->s1stat.st_size > (off_t)(sizeof bootstrap)) {
    315  1.23       dsl 		warnx("stage1 bootstrap `%s' (%u bytes) is larger than 8192 bytes",
    316  1.23       dsl 			params->stage1, (unsigned int)params->s1stat.st_size);
    317  1.23       dsl 		return 0;
    318  1.23       dsl 	}
    319  1.23       dsl 	if (params->s1stat.st_size < 3 * 512 && params->s1stat.st_size != 512) {
    320  1.23       dsl 		warnx("stage1 bootstrap `%s' (%u bytes) is too small",
    321  1.23       dsl 			params->stage1, (unsigned int)params->s1stat.st_size);
    322  1.23       dsl 		return 0;
    323   1.9     lukem 	}
    324   1.9     lukem 
    325  1.23       dsl 	/* Read in the existing disk header and boot code */
    326  1.23       dsl 	rv = pread(params->fsfd, &disk_buf, sizeof (disk_buf), 0);
    327  1.24    dogcow 	if (rv != sizeof(disk_buf)) {
    328  1.23       dsl 		if (rv == -1)
    329  1.23       dsl 			warn("Reading `%s'", params->filesystem);
    330  1.23       dsl 		else
    331  1.24    dogcow 			warnx("Reading `%s': short read, %ld bytes"
    332  1.24    dogcow 			    " (should be %ld)", params->filesystem, (long)rv,
    333  1.24    dogcow 			    (long)sizeof(disk_buf));
    334  1.23       dsl 		return 0;
    335   1.1       dsl 	}
    336  1.23       dsl 
    337  1.23       dsl 	if (disk_buf.mbr.mbr_magic != le16toh(MBR_MAGIC)) {
    338   1.9     lukem 		if (params->flags & IB_VERBOSE) {
    339   1.9     lukem 			printf(
    340  1.23       dsl 		    "Ignoring PBR with invalid magic in sector 0 of `%s'\n",
    341   1.9     lukem 			    params->filesystem);
    342   1.9     lukem 		}
    343  1.23       dsl 		memset(&disk_buf, 0, 512);
    344   1.9     lukem 	}
    345   1.9     lukem 
    346  1.23       dsl 	/* Read the new bootstrap code. */
    347  1.23       dsl 	rv = pread(params->s1fd, &bootstrap, params->s1stat.st_size, 0);
    348  1.23       dsl 	if (rv != params->s1stat.st_size) {
    349  1.23       dsl 		if (rv == -1)
    350  1.23       dsl 			warn("Reading `%s'", params->stage1);
    351  1.23       dsl 		else
    352  1.24    dogcow 			warnx("Reading `%s': short read, %ld bytes"
    353  1.24    dogcow 			    " (should be %ld)", params->stage1, (long)rv,
    354  1.24    dogcow 			    (long)params->s1stat.st_size);
    355  1.23       dsl 		return 0;
    356   1.1       dsl 	}
    357   1.1       dsl 
    358   1.9     lukem 	/*
    359  1.23       dsl 	 * The bootstrap code is either 512 bytes for booting FAT16, or best
    360  1.23       dsl 	 * part of 8k (with bytes 512-1023 all zeros).
    361  1.23       dsl 	 */
    362  1.23       dsl 	if (params->s1stat.st_size == 512) {
    363  1.23       dsl 		/* Magic number is at end of pbr code */
    364  1.23       dsl 		magic = (void *)(bootstrap.b + 512 - 16 + 4);
    365  1.23       dsl 		expected_magic = htole32(X86_BOOT_MAGIC_FAT);
    366  1.23       dsl 	} else {
    367  1.23       dsl 		/* Magic number is at start of sector following label */
    368  1.23       dsl 		magic = (void *)(bootstrap.b + 512 * 2 + 4);
    369  1.23       dsl 		expected_magic = htole32(X86_BOOT_MAGIC_1);
    370  1.23       dsl 		/*
    371  1.23       dsl 		 * For a variety of reasons we restrict our 'normal' partition
    372  1.23       dsl 		 * boot code to a size which enable it to be used as mbr code.
    373  1.23       dsl 		 * IMHO this is bugus (dsl).
    374  1.23       dsl 		 */
    375  1.23       dsl 		if (!is_zero(bootstrap.b + 512-2-64, 64)) {
    376  1.23       dsl 			warnx("Data in mbr partition table of new bootstrap");
    377  1.23       dsl 			return 0;
    378  1.23       dsl 		}
    379  1.23       dsl 		if (!is_zero(bootstrap.b + 512, 512)) {
    380  1.23       dsl 			warnx("Data in label part of new bootstrap");
    381  1.23       dsl 			return 0;
    382  1.23       dsl 		}
    383  1.23       dsl 		/* Copy mbr table and label from existing disk buffer */
    384  1.23       dsl 		memcpy(bootstrap.b + 512-2-64, disk_buf.b + 512-2-64, 64);
    385  1.23       dsl 		memcpy(bootstrap.b + 512, disk_buf.b + 512, 512);
    386   1.1       dsl 	}
    387   1.1       dsl 
    388  1.23       dsl 	/* Validate the 'magic number' that marks the parameter block */
    389  1.23       dsl 	if (*magic != expected_magic) {
    390  1.20       dsl 		warnx("Invalid magic in stage1 bootstrap %x != %x",
    391  1.23       dsl 				*magic, expected_magic);
    392  1.23       dsl 		return 0;
    393   1.2       dsl 	}
    394   1.2       dsl 
    395   1.9     lukem 	/*
    396  1.34   mbalmer 	 * If the partition has a FAT (or NTFS) filesystem, then we must
    397  1.25       dsl 	 * preserve the BIOS Parameter Block (BPB).
    398  1.25       dsl 	 * It is also very likely that there isn't 8k of space available
    399  1.25       dsl 	 * for (say) bootxx_msdos, and that blindly installing it will trash
    400  1.25       dsl 	 * the FAT filesystem.
    401  1.25       dsl 	 * To avoid this we check the number of 'reserved' sectors to ensure
    402  1.25       dsl 	 * there there is enough space.
    403  1.25       dsl 	 * Unfortunately newfs(8) doesn't (yet) splat the BPB (which is
    404  1.25       dsl 	 * effectively the FAT superblock) when a filesystem is initailised
    405  1.25       dsl 	 * so this code tends to complain rather too often,
    406  1.25       dsl 	 * Specifying 'installboot -f' will delete the old BPB info.
    407  1.11     lukem 	 */
    408  1.25       dsl 	if (!(params->flags & IB_FORCE)) {
    409  1.29       dsl 		#define USE_F ", use -f (may invalidate filesystem)"
    410  1.25       dsl 		/*
    411  1.25       dsl 		 * For FAT compatibility, the pbr code starts 'jmp xx; nop'
    412  1.25       dsl 		 * followed by the BIOS Parameter Block (BPB).
    413  1.25       dsl 		 * The 2nd byte (jump offset) is the size of the nop + BPB.
    414  1.25       dsl 		 */
    415  1.25       dsl 		if (bootstrap.b[0] != 0xeb || bootstrap.b[2] != 0x90) {
    416  1.29       dsl 			warnx("No BPB in new bootstrap %02x:%02x:%02x" USE_F,
    417  1.25       dsl 				bootstrap.b[0], bootstrap.b[1], bootstrap.b[2]);
    418  1.23       dsl 			return 0;
    419  1.23       dsl 		}
    420  1.25       dsl 
    421  1.25       dsl 		/* Find size of old BPB, and copy into new bootcode */
    422  1.25       dsl 		if (!is_zero(disk_buf.b + 3 + 8, disk_buf.b[1] - 1 - 8)) {
    423  1.25       dsl 			struct mbr_bpbFAT16 *bpb = (void *)(disk_buf.b + 3 + 8);
    424  1.25       dsl 			/* Check enough space before the FAT for the bootcode */
    425  1.25       dsl 			u = le16toh(bpb->bpbBytesPerSec)
    426  1.25       dsl 			    * le16toh(bpb->bpbResSectors);
    427  1.25       dsl 			if (u != 0 && u < params->s1stat.st_size) {
    428  1.29       dsl 				warnx("Insufficient reserved space before FAT "
    429  1.29       dsl 					"(%u bytes available)" USE_F, u);
    430  1.25       dsl 				return 0;
    431  1.23       dsl 			}
    432  1.25       dsl 			/* Check we have enough space for the old bpb */
    433  1.25       dsl 			if (disk_buf.b[1] > bootstrap.b[1]) {
    434  1.25       dsl 				/* old BPB is larger, allow if extra zeros */
    435  1.25       dsl 				if (!is_zero(disk_buf.b + 2 + bootstrap.b[1],
    436  1.25       dsl 				    disk_buf.b[1] - bootstrap.b[1])) {
    437  1.29       dsl 					warnx("Old BPB too big" USE_F);
    438  1.25       dsl 					    return 0;
    439  1.25       dsl 				}
    440  1.25       dsl 				u = bootstrap.b[1];
    441  1.25       dsl 			} else {
    442  1.25       dsl 				/* Old BPB is shorter, leave zero filled */
    443  1.25       dsl 				u = disk_buf.b[1];
    444  1.25       dsl 			}
    445  1.38       dsl 			if (params->s1start != 0)
    446  1.38       dsl 				/* Fixup physical offset of filesytem */
    447  1.38       dsl 				bpb->bpbHiddenSecs = htole32(params->s1start);
    448  1.25       dsl 			memcpy(bootstrap.b + 2, disk_buf.b + 2, u);
    449   1.9     lukem 		}
    450  1.29       dsl 		#undef USE_F
    451   1.9     lukem 	}
    452   1.9     lukem 
    453   1.9     lukem 	/*
    454  1.15     lukem 	 * Fill in any user-specified options into the
    455  1.17       dsl 	 *      struct x86_boot_params
    456  1.23       dsl 	 * that follows the magic number.
    457  1.15     lukem 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
    458   1.9     lukem 	 */
    459  1.23       dsl 	if (update_i386_boot_params(params, (void *)(magic + 1)))
    460  1.23       dsl 		return 0;
    461   1.1       dsl 
    462   1.1       dsl 	if (params->flags & IB_NOWRITE) {
    463  1.23       dsl 		return 1;
    464   1.1       dsl 	}
    465   1.1       dsl 
    466  1.23       dsl 	/* Copy new bootstrap data into disk buffer, ignoring label area */
    467  1.23       dsl 	memcpy(&disk_buf, &bootstrap, 512);
    468  1.23       dsl 	if (params->s1stat.st_size > 512 * 2) {
    469  1.23       dsl 		memcpy(disk_buf.b + 2 * 512, bootstrap.b + 2 * 512,
    470  1.23       dsl 		    params->s1stat.st_size - 2 * 512);
    471  1.23       dsl 		/* Zero pad to 512 byte sector boundary */
    472  1.23       dsl 		memset(disk_buf.b + params->s1stat.st_size, 0,
    473  1.23       dsl 			(8192 - params->s1stat.st_size) & 511);
    474   1.1       dsl 	}
    475   1.1       dsl 
    476  1.37  christos 	return write_boot_area(params, disk_buf.b, sizeof disk_buf.b);
    477   1.1       dsl }
    478  1.19       dsl 
    479  1.22       dsl static int
    480  1.19       dsl i386_editboot(ib_params *params)
    481  1.19       dsl {
    482  1.19       dsl 	int		retval;
    483  1.19       dsl 	uint8_t		buf[512];
    484  1.19       dsl 	ssize_t		rv;
    485  1.19       dsl 	uint32_t	magic;
    486  1.19       dsl 	uint32_t	offset;
    487  1.19       dsl 	struct x86_boot_params	*bpp;
    488  1.19       dsl 
    489  1.19       dsl 	assert(params != NULL);
    490  1.19       dsl 	assert(params->fsfd != -1);
    491  1.19       dsl 	assert(params->filesystem != NULL);
    492  1.19       dsl 
    493  1.19       dsl 	retval = 0;
    494  1.19       dsl 
    495  1.19       dsl 	/*
    496  1.19       dsl 	 * Read in the existing bootstrap.
    497  1.21       dsl 	 * Look in any of the first 4 sectors.
    498  1.19       dsl 	 */
    499  1.19       dsl 
    500  1.19       dsl 	bpp = NULL;
    501  1.19       dsl 	for (offset = 0; offset < 4 * 512; offset += 512) {
    502  1.19       dsl 		rv = pread(params->fsfd, &buf, sizeof buf, offset);
    503  1.19       dsl 		if (rv == -1) {
    504  1.19       dsl 			warn("Reading `%s'", params->filesystem);
    505  1.19       dsl 			goto done;
    506  1.19       dsl 		} else if (rv != sizeof buf) {
    507  1.19       dsl 			warnx("Reading `%s': short read", params->filesystem);
    508  1.19       dsl 			goto done;
    509  1.19       dsl 		}
    510  1.19       dsl 
    511  1.21       dsl 		/* Magic number is 4 bytes in (to allow for a jmps) */
    512  1.21       dsl 		/* Also allow any of the magic numbers. */
    513  1.21       dsl 		magic = le32toh(*(uint32_t *)(buf + 4)) | 0xf;
    514  1.21       dsl 		if (magic != (X86_BOOT_MAGIC_1 | 0xf))
    515  1.19       dsl 			continue;
    516  1.21       dsl 
    517  1.21       dsl 		/* The parameters are just after the magic number */
    518  1.19       dsl 		bpp = (void *)(buf + 8);
    519  1.19       dsl 		break;
    520  1.19       dsl 	}
    521  1.19       dsl 	if (bpp == NULL) {
    522  1.21       dsl 		warnx("Invalid magic in existing bootstrap");
    523  1.19       dsl 		goto done;
    524  1.19       dsl 	}
    525  1.19       dsl 
    526  1.19       dsl 	/*
    527  1.19       dsl 	 * Fill in any user-specified options into the
    528  1.19       dsl 	 *      struct x86_boot_params
    529  1.19       dsl 	 * that's 8 bytes in from the start of the third sector.
    530  1.19       dsl 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
    531  1.19       dsl 	 */
    532  1.19       dsl 	if (update_i386_boot_params(params, bpp))
    533  1.19       dsl 		goto done;
    534  1.19       dsl 
    535  1.19       dsl 	if (params->flags & IB_NOWRITE) {
    536  1.19       dsl 		retval = 1;
    537  1.19       dsl 		goto done;
    538  1.19       dsl 	}
    539  1.19       dsl 
    540  1.19       dsl 	/*
    541  1.19       dsl 	 * Write boot code back
    542  1.19       dsl 	 */
    543  1.19       dsl 	rv = pwrite(params->fsfd, buf, sizeof buf, offset);
    544  1.19       dsl 	if (rv == -1) {
    545  1.19       dsl 		warn("Writing `%s'", params->filesystem);
    546  1.19       dsl 		goto done;
    547  1.19       dsl 	} else if (rv != sizeof buf) {
    548  1.37  christos 		warnx("Writing `%s': short write, %zd bytes (should be %zu)",
    549  1.37  christos 		    params->filesystem, rv, sizeof(buf));
    550  1.19       dsl 		goto done;
    551  1.19       dsl 	}
    552  1.19       dsl 
    553  1.19       dsl 	retval = 1;
    554  1.19       dsl 
    555  1.19       dsl  done:
    556  1.19       dsl 	return retval;
    557  1.19       dsl }
    558