Home | History | Annotate | Line # | Download | only in arch
i386.c revision 1.23
      1  1.23    dsl /* $NetBSD: i386.c,v 1.23 2007/01/06 10:21:24 dsl 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.23    dsl __RCSID("$NetBSD: i386.c,v 1.23 2007/01/06 10:21:24 dsl Exp $");
     46  1.13  lukem #endif /* !__lint */
     47   1.1    dsl 
     48   1.1    dsl #include <sys/param.h>
     49  1.23    dsl #ifndef HAVE_NBTOOL_CONFIG_H
     50  1.23    dsl #include <sys/ioctl.h>
     51  1.23    dsl #include <sys/dkio.h>
     52  1.23    dsl #endif
     53   1.1    dsl 
     54   1.1    dsl #include <assert.h>
     55  1.23    dsl #include <errno.h>
     56   1.1    dsl #include <err.h>
     57   1.5  bjh21 #include <md5.h>
     58   1.1    dsl #include <stddef.h>
     59   1.1    dsl #include <stdio.h>
     60   1.1    dsl #include <stdlib.h>
     61   1.1    dsl #include <string.h>
     62   1.1    dsl #include <unistd.h>
     63   1.1    dsl 
     64   1.1    dsl #include "installboot.h"
     65   1.1    dsl 
     66  1.19    dsl #define nelem(x) (sizeof (x)/sizeof *(x))
     67  1.19    dsl 
     68  1.19    dsl static const char *const console_names[] = {
     69  1.19    dsl 	"pc", "com0", "com1", "com2", "com3",
     70  1.19    dsl 	"com0kbd", "com1kbd", "com2kbd", "com3kbd",
     71  1.19    dsl 	NULL };
     72  1.19    dsl 
     73  1.22    dsl static int i386_setboot(ib_params *);
     74  1.22    dsl static int i386_editboot(ib_params *);
     75  1.22    dsl 
     76  1.22    dsl struct ib_mach ib_mach_i386 =
     77  1.22    dsl 	{ "i386", i386_setboot, no_clearboot, i386_editboot,
     78  1.22    dsl 		IB_RESETVIDEO | IB_CONSOLE | IB_CONSPEED | IB_CONSADDR |
     79  1.22    dsl 		IB_KEYMAP | IB_PASSWORD | IB_TIMEOUT };
     80  1.22    dsl 
     81  1.22    dsl struct ib_mach ib_mach_amd64 =
     82  1.22    dsl 	{ "amd64", i386_setboot, no_clearboot, i386_editboot,
     83  1.22    dsl 		IB_RESETVIDEO | IB_CONSOLE | IB_CONSPEED | IB_CONSADDR |
     84  1.22    dsl 		IB_KEYMAP | IB_PASSWORD | IB_TIMEOUT };
     85  1.22    dsl 
     86  1.23    dsl /*
     87  1.23    dsl  * Attempting to write the 'labelsector' (or a sector near it - within 8k?)
     88  1.23    dsl  * using the non-raw disk device fails silently.  This can be detected (today)
     89  1.23    dsl  * by doing a fsync() and a read back.
     90  1.23    dsl  * This is very likely to affect installboot, indeed the code may need to
     91  1.23    dsl  * be written into the 'labelsector' itself - especially on non-512 byte media.
     92  1.23    dsl  * We do all writes with a read verify.
     93  1.23    dsl  * If EROFS is returned we also try to enable writes to the label sector.
     94  1.23    dsl  * (Maybe these functions should be in the generic part of installboot.)
     95  1.23    dsl  */
     96  1.23    dsl static int
     97  1.23    dsl pwrite_validate(int fd, const void *buf, size_t n_bytes, off_t offset)
     98  1.23    dsl {
     99  1.23    dsl 	void *r_buf;
    100  1.23    dsl 	ssize_t rv;
    101  1.23    dsl 
    102  1.23    dsl 	r_buf = malloc(n_bytes);
    103  1.23    dsl 	if (r_buf == NULL)
    104  1.23    dsl 		return -1;
    105  1.23    dsl 	rv = pwrite(fd, buf, n_bytes, offset);
    106  1.23    dsl 	if (rv == -1) {
    107  1.23    dsl 		free(r_buf);
    108  1.23    dsl 		return -1;
    109  1.23    dsl 	}
    110  1.23    dsl 	fsync(fd);
    111  1.23    dsl 	if (pread(fd, r_buf, rv, offset) == rv && memcmp(r_buf, buf, rv) == 0)
    112  1.23    dsl 		return rv;
    113  1.23    dsl 	errno = EROFS;
    114  1.23    dsl 	return -1;
    115  1.23    dsl }
    116  1.23    dsl 
    117  1.23    dsl static int
    118  1.23    dsl write_boot_area(ib_params *params, void *v_buf, int len)
    119  1.23    dsl {
    120  1.23    dsl 	int rv, i;
    121  1.23    dsl 	uint8_t *buf = v_buf;
    122  1.23    dsl 
    123  1.23    dsl 	/*
    124  1.23    dsl 	 * Writing the 'label' sector (likely to be bytes 512-1023) could
    125  1.23    dsl 	 * fail, so we try to avoid writing that area.
    126  1.23    dsl 	 * Unfortunately, if we are accessing the raw disk, and the sector
    127  1.23    dsl 	 * size is larger than 512 bytes that is also doomed.
    128  1.23    dsl 	 * See how we get on....
    129  1.23    dsl 	 *
    130  1.23    dsl 	 * NB: Even if the physical sector size is not 512, the space for
    131  1.23    dsl 	 * the label is 512 bytes from the start of the disk.
    132  1.23    dsl 	 * So all the '512' constants in these functions are correct.
    133  1.23    dsl 	 */
    134  1.23    dsl 
    135  1.23    dsl 	/* Write out first 512 bytes - the pbr code */
    136  1.23    dsl 	rv = pwrite_validate(params->fsfd, buf, 512, 0);
    137  1.23    dsl 	if (rv == 512) {
    138  1.23    dsl 		/* That worked, do the rest */
    139  1.23    dsl 		if (len == 512)
    140  1.23    dsl 			return 1;
    141  1.23    dsl 		len -= 512 * 2;
    142  1.23    dsl 		rv = pwrite_validate(params->fsfd, buf + 512 * 2, len, 512 * 2);
    143  1.23    dsl 		if (rv != len)
    144  1.23    dsl 			goto bad_write;
    145  1.23    dsl 		return 1;
    146  1.23    dsl 	}
    147  1.23    dsl 	if (rv != -1 || (errno != EINVAL && errno != EROFS))
    148  1.23    dsl 		goto bad_write;
    149  1.23    dsl 
    150  1.23    dsl 	if (errno == EINVAL) {
    151  1.23    dsl 		/* Assume the failure was due to to the sector size > 512 */
    152  1.23    dsl 		rv = pwrite_validate(params->fsfd, buf, len, 0);
    153  1.23    dsl 		if (rv == len)
    154  1.23    dsl 			return 1;
    155  1.23    dsl 		if (rv != -1 || (errno != EROFS))
    156  1.23    dsl 			goto bad_write;
    157  1.23    dsl 	}
    158  1.23    dsl 
    159  1.23    dsl #ifdef DIOCWLABEL
    160  1.23    dsl 	/* Pesky label is protected, try to unprotect it */
    161  1.23    dsl 	i = 1;
    162  1.23    dsl 	rv = ioctl(params->fsfd, DIOCWLABEL, &i);
    163  1.23    dsl 	if (rv != 0) {
    164  1.23    dsl 		warn("Cannot enable writes to the label sector");
    165  1.23    dsl 		return 0;
    166  1.23    dsl 	}
    167  1.23    dsl 	/* Try again with label write-enabled */
    168  1.23    dsl 	rv = pwrite_validate(params->fsfd, buf, len, 0);
    169  1.23    dsl 
    170  1.23    dsl 	/* Reset write-protext */
    171  1.23    dsl 	i = 0;
    172  1.23    dsl 	ioctl(params->fsfd, DIOCWLABEL, &i);
    173  1.23    dsl 	if (rv == len)
    174  1.23    dsl 		return 1;
    175  1.23    dsl #endif
    176  1.23    dsl 
    177  1.23    dsl   bad_write:
    178  1.23    dsl 	if (rv == -1)
    179  1.23    dsl 		warn("Writing `%s'", params->filesystem);
    180  1.23    dsl 	else
    181  1.23    dsl 		warnx("Writing `%s': short write, %u bytes",
    182  1.23    dsl 			params->filesystem, rv);
    183  1.23    dsl 	return 0;
    184  1.23    dsl }
    185  1.23    dsl 
    186  1.19    dsl static void
    187  1.19    dsl show_i386_boot_params(struct x86_boot_params  *bpp)
    188  1.19    dsl {
    189  1.19    dsl 	uint32_t i;
    190  1.19    dsl 
    191  1.19    dsl 	printf("Boot options:        ");
    192  1.19    dsl 	printf("timeout %d, ", le32toh(bpp->bp_timeout));
    193  1.19    dsl 	printf("flags %x, ", le32toh(bpp->bp_flags));
    194  1.19    dsl 	printf("speed %d, ", le32toh(bpp->bp_conspeed));
    195  1.19    dsl 	printf("ioaddr %x, ", le32toh(bpp->bp_consaddr));
    196  1.19    dsl 	i = le32toh(bpp->bp_consdev);
    197  1.19    dsl 	if (i < nelem(console_names) - 1)
    198  1.19    dsl 		printf("console %s\n", console_names[i]);
    199  1.19    dsl 	else
    200  1.19    dsl 		printf("console %d\n", i);
    201  1.19    dsl 	if (bpp->bp_keymap[0])
    202  1.19    dsl 		printf("                     keymap %s\n", bpp->bp_keymap);
    203  1.19    dsl }
    204  1.19    dsl 
    205  1.19    dsl static int
    206  1.23    dsl is_zero(const uint8_t *p, unsigned int len)
    207  1.23    dsl {
    208  1.23    dsl 	return len == 0 || (p[0] == 0 && memcmp(p, p + 1, len - 1) == 0);
    209  1.23    dsl }
    210  1.23    dsl 
    211  1.23    dsl static int
    212  1.19    dsl update_i386_boot_params(ib_params *params, struct x86_boot_params  *bpp)
    213  1.19    dsl {
    214  1.19    dsl 	struct x86_boot_params bp;
    215  1.19    dsl 	int bplen;
    216  1.19    dsl 	int i;
    217  1.19    dsl 
    218  1.19    dsl 	bplen = le32toh(bpp->bp_length);
    219  1.19    dsl 	if (bplen > sizeof bp)
    220  1.19    dsl 		/* Ignore pad space in bootxx */
    221  1.19    dsl 		bplen = sizeof bp;
    222  1.19    dsl 
    223  1.19    dsl 	/* Take (and update) local copy so we handle size mismatches */
    224  1.19    dsl 	memset(&bp, 0, sizeof bp);
    225  1.19    dsl 	memcpy(&bp, bpp, bplen);
    226  1.19    dsl 
    227  1.19    dsl 	if (params->flags & IB_TIMEOUT)
    228  1.19    dsl 		bp.bp_timeout = htole32(params->timeout);
    229  1.19    dsl 	if (params->flags & IB_RESETVIDEO)
    230  1.19    dsl 		bp.bp_flags ^= htole32(X86_BP_FLAGS_RESET_VIDEO);
    231  1.19    dsl 	if (params->flags & IB_CONSPEED)
    232  1.19    dsl 		bp.bp_conspeed = htole32(params->conspeed);
    233  1.19    dsl 	if (params->flags & IB_CONSADDR)
    234  1.19    dsl 		bp.bp_consaddr = htole32(params->consaddr);
    235  1.19    dsl 	if (params->flags & IB_CONSOLE) {
    236  1.19    dsl 		for (i = 0; ; i++) {
    237  1.19    dsl 			if (console_names[i] == NULL) {
    238  1.19    dsl 				warnx("invalid console name, valid names are:");
    239  1.19    dsl 				fprintf(stderr, "\t%s", console_names[0]);
    240  1.19    dsl 				for (i = 1; console_names[i] != NULL; i++)
    241  1.19    dsl 					fprintf(stderr, ", %s", console_names[i]);
    242  1.19    dsl 				fprintf(stderr, "\n");
    243  1.19    dsl 				return 1;
    244  1.19    dsl 			}
    245  1.19    dsl 			if (strcmp(console_names[i], params->console) == 0)
    246  1.19    dsl 				break;
    247  1.19    dsl 		}
    248  1.19    dsl 		bp.bp_consdev = htole32(i);
    249  1.19    dsl 	}
    250  1.19    dsl 	if (params->flags & IB_PASSWORD) {
    251  1.19    dsl 		if (params->password[0]) {
    252  1.19    dsl 			MD5_CTX md5ctx;
    253  1.19    dsl 			MD5Init(&md5ctx);
    254  1.19    dsl 			MD5Update(&md5ctx, params->password,
    255  1.19    dsl 			    strlen(params->password));
    256  1.19    dsl 			MD5Final(bp.bp_password, &md5ctx);
    257  1.19    dsl 			bp.bp_flags |= htole32(X86_BP_FLAGS_PASSWORD);
    258  1.19    dsl 		} else {
    259  1.19    dsl 			memset(&bp.bp_password, 0, sizeof bp.bp_password);
    260  1.19    dsl 			bp.bp_flags &= ~htole32(X86_BP_FLAGS_PASSWORD);
    261  1.19    dsl 		}
    262  1.19    dsl 	}
    263  1.19    dsl 	if (params->flags & IB_KEYMAP)
    264  1.19    dsl 		strlcpy(bp.bp_keymap, params->keymap, sizeof bp.bp_keymap);
    265  1.19    dsl 
    266  1.19    dsl 	if (params->flags & (IB_NOWRITE | IB_VERBOSE))
    267  1.19    dsl 		show_i386_boot_params(&bp);
    268  1.19    dsl 
    269  1.19    dsl 	/* Check we aren't trying to set anything we can't save */
    270  1.23    dsl 	if (!is_zero((char *)&bp + bplen, sizeof bp - bplen)) {
    271  1.19    dsl 		warnx("Patch area in stage1 bootstrap is too small");
    272  1.19    dsl 		return 1;
    273  1.19    dsl 	}
    274  1.19    dsl 	memcpy(bpp, &bp, bplen);
    275  1.19    dsl 	return 0;
    276  1.19    dsl }
    277  1.19    dsl 
    278  1.22    dsl static int
    279   1.1    dsl i386_setboot(ib_params *params)
    280   1.1    dsl {
    281  1.23    dsl 	unsigned int	u;
    282   1.1    dsl 	ssize_t		rv;
    283  1.23    dsl 	uint32_t	*magic, expected_magic;
    284  1.23    dsl 	union {
    285  1.23    dsl 	    struct mbr_sector	mbr;
    286  1.23    dsl 	    uint8_t		b[8192];
    287  1.23    dsl 	} disk_buf, bootstrap;
    288   1.1    dsl 
    289   1.1    dsl 	assert(params != NULL);
    290   1.1    dsl 	assert(params->fsfd != -1);
    291   1.1    dsl 	assert(params->filesystem != NULL);
    292   1.1    dsl 	assert(params->s1fd != -1);
    293   1.1    dsl 	assert(params->stage1 != NULL);
    294   1.1    dsl 
    295   1.2    dsl 	/*
    296   1.2    dsl 	 * There is only 8k of space in a UFSv1 partition (and ustarfs)
    297   1.2    dsl 	 * so ensure we don't splat over anything important.
    298   1.2    dsl 	 */
    299  1.23    dsl 	if (params->s1stat.st_size > sizeof bootstrap) {
    300  1.23    dsl 		warnx("stage1 bootstrap `%s' (%u bytes) is larger than 8192 bytes",
    301  1.23    dsl 			params->stage1, (unsigned int)params->s1stat.st_size);
    302  1.23    dsl 		return 0;
    303  1.23    dsl 	}
    304  1.23    dsl 	if (params->s1stat.st_size < 3 * 512 && params->s1stat.st_size != 512) {
    305  1.23    dsl 		warnx("stage1 bootstrap `%s' (%u bytes) is too small",
    306  1.23    dsl 			params->stage1, (unsigned int)params->s1stat.st_size);
    307  1.23    dsl 		return 0;
    308   1.9  lukem 	}
    309   1.9  lukem 
    310  1.23    dsl 	/* Read in the existing disk header and boot code */
    311  1.23    dsl 	rv = pread(params->fsfd, &disk_buf, sizeof (disk_buf), 0);
    312  1.23    dsl 	if (rv != sizeof (disk_buf)) {
    313  1.23    dsl 		if (rv == -1)
    314  1.23    dsl 			warn("Reading `%s'", params->filesystem);
    315  1.23    dsl 		else
    316  1.23    dsl 			warnx("Reading `%s': short read, %d bytes",
    317  1.23    dsl 				    params->filesystem, rv);
    318  1.23    dsl 		return 0;
    319   1.1    dsl 	}
    320  1.23    dsl 
    321  1.23    dsl 	if (disk_buf.mbr.mbr_magic != le16toh(MBR_MAGIC)) {
    322   1.9  lukem 		if (params->flags & IB_VERBOSE) {
    323   1.9  lukem 			printf(
    324  1.23    dsl 		    "Ignoring PBR with invalid magic in sector 0 of `%s'\n",
    325   1.9  lukem 			    params->filesystem);
    326   1.9  lukem 		}
    327  1.23    dsl 		memset(&disk_buf, 0, 512);
    328   1.9  lukem 	}
    329   1.9  lukem 
    330  1.23    dsl 	/* Read the new bootstrap code. */
    331  1.23    dsl 	rv = pread(params->s1fd, &bootstrap, params->s1stat.st_size, 0);
    332  1.23    dsl 	if (rv != params->s1stat.st_size) {
    333  1.23    dsl 		if (rv == -1)
    334  1.23    dsl 			warn("Reading `%s'", params->stage1);
    335  1.23    dsl 		else
    336  1.23    dsl 			warnx("Reading `%s': short read, %d bytes",
    337  1.23    dsl 				params->stage1, rv);
    338  1.23    dsl 		return 0;
    339   1.1    dsl 	}
    340   1.1    dsl 
    341   1.9  lukem 	/*
    342  1.23    dsl 	 * The bootstrap code is either 512 bytes for booting FAT16, or best
    343  1.23    dsl 	 * part of 8k (with bytes 512-1023 all zeros).
    344  1.23    dsl 	 */
    345  1.23    dsl 	if (params->s1stat.st_size == 512) {
    346  1.23    dsl 		/* Magic number is at end of pbr code */
    347  1.23    dsl 		magic = (void *)(bootstrap.b + 512 - 16 + 4);
    348  1.23    dsl 		expected_magic = htole32(X86_BOOT_MAGIC_FAT);
    349  1.23    dsl 	} else {
    350  1.23    dsl 		/* Magic number is at start of sector following label */
    351  1.23    dsl 		magic = (void *)(bootstrap.b + 512 * 2 + 4);
    352  1.23    dsl 		expected_magic = htole32(X86_BOOT_MAGIC_1);
    353  1.23    dsl 		/*
    354  1.23    dsl 		 * For a variety of reasons we restrict our 'normal' partition
    355  1.23    dsl 		 * boot code to a size which enable it to be used as mbr code.
    356  1.23    dsl 		 * IMHO this is bugus (dsl).
    357  1.23    dsl 		 */
    358  1.23    dsl 		if (!is_zero(bootstrap.b + 512-2-64, 64)) {
    359  1.23    dsl 			warnx("Data in mbr partition table of new bootstrap");
    360  1.23    dsl 			return 0;
    361  1.23    dsl 		}
    362  1.23    dsl 		if (!is_zero(bootstrap.b + 512, 512)) {
    363  1.23    dsl 			warnx("Data in label part of new bootstrap");
    364  1.23    dsl 			return 0;
    365  1.23    dsl 		}
    366  1.23    dsl 		/* Copy mbr table and label from existing disk buffer */
    367  1.23    dsl 		memcpy(bootstrap.b + 512-2-64, disk_buf.b + 512-2-64, 64);
    368  1.23    dsl 		memcpy(bootstrap.b + 512, disk_buf.b + 512, 512);
    369   1.1    dsl 	}
    370   1.1    dsl 
    371  1.23    dsl 	/* Validate the 'magic number' that marks the parameter block */
    372  1.23    dsl 	if (*magic != expected_magic) {
    373  1.20    dsl 		warnx("Invalid magic in stage1 bootstrap %x != %x",
    374  1.23    dsl 				*magic, expected_magic);
    375  1.23    dsl 		return 0;
    376   1.2    dsl 	}
    377   1.2    dsl 
    378   1.9  lukem 	/*
    379  1.23    dsl 	 * For FAT compatibility, the pbr code starts 'jmp xx; nop' followed
    380  1.23    dsl 	 * by the BIOS Parameter Block (BPB).
    381  1.23    dsl 	 * The 2nd byte (jump offset) is the size of the nop + BPB.
    382  1.11  lukem 	 */
    383  1.23    dsl 	if (bootstrap.b[0] != 0xeb || bootstrap.b[2] != 0x90) {
    384  1.23    dsl 		warnx("No BPB in new bootstrap %02x:%02x:%02x",
    385  1.23    dsl 			bootstrap.b[0], bootstrap.b[1], bootstrap.b[2]);
    386  1.23    dsl 		return 0;
    387  1.23    dsl 	}
    388  1.11  lukem 
    389  1.23    dsl 	/* Find size of old BPB, and copy into new bootcode */
    390  1.23    dsl 	if (!is_zero(disk_buf.b + 3 + 8, disk_buf.b[1] - 1 - 8)) {
    391  1.23    dsl 		struct mbr_bpbFAT16 *bpb = (void *)(disk_buf.b + 3 + 8);
    392  1.23    dsl 		/* Check enough space before first FAT for the bootcode */
    393  1.23    dsl 		u = le16toh(bpb->bpbBytesPerSec) * le16toh(bpb->bpbResSectors);
    394  1.23    dsl 		if (u != 0 && u < params->s1stat.st_size) {
    395  1.23    dsl 			warnx("Insufficient reserved space (%u bytes)", u);
    396  1.23    dsl 			return 0;
    397  1.23    dsl 		}
    398  1.23    dsl 		/* Check we have enough space for the old bpb */
    399  1.23    dsl 		if (disk_buf.b[1] > bootstrap.b[1]) {
    400  1.23    dsl 			/* old BPB is larger, allow if extra zeros */
    401  1.23    dsl 			if (!is_zero(disk_buf.b + 2 + bootstrap.b[1],
    402  1.23    dsl 			    disk_buf.b[1] - bootstrap.b[1])) {
    403  1.23    dsl 				warnx("Old BPB too big");
    404  1.23    dsl 				    return 0;
    405  1.23    dsl 			}
    406  1.23    dsl 			u = bootstrap.b[1];
    407  1.23    dsl 		} else {
    408  1.23    dsl 			/* Old BPB is shorter, leave zero filled */
    409  1.23    dsl 			u = disk_buf.b[1];
    410   1.9  lukem 		}
    411  1.23    dsl 		memcpy(bootstrap.b + 2, disk_buf.b + 2, u);
    412   1.9  lukem 	}
    413   1.9  lukem 
    414   1.9  lukem 	/*
    415  1.15  lukem 	 * Fill in any user-specified options into the
    416  1.17    dsl 	 *      struct x86_boot_params
    417  1.23    dsl 	 * that follows the magic number.
    418  1.15  lukem 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
    419   1.9  lukem 	 */
    420  1.23    dsl 	if (update_i386_boot_params(params, (void *)(magic + 1)))
    421  1.23    dsl 		return 0;
    422   1.1    dsl 
    423   1.1    dsl 	if (params->flags & IB_NOWRITE) {
    424  1.23    dsl 		return 1;
    425   1.1    dsl 	}
    426   1.1    dsl 
    427  1.23    dsl 	/* Copy new bootstrap data into disk buffer, ignoring label area */
    428  1.23    dsl 	memcpy(&disk_buf, &bootstrap, 512);
    429  1.23    dsl 	if (params->s1stat.st_size > 512 * 2) {
    430  1.23    dsl 		memcpy(disk_buf.b + 2 * 512, bootstrap.b + 2 * 512,
    431  1.23    dsl 		    params->s1stat.st_size - 2 * 512);
    432  1.23    dsl 		/* Zero pad to 512 byte sector boundary */
    433  1.23    dsl 		memset(disk_buf.b + params->s1stat.st_size, 0,
    434  1.23    dsl 			(8192 - params->s1stat.st_size) & 511);
    435   1.1    dsl 	}
    436   1.1    dsl 
    437  1.23    dsl 	return write_boot_area(params, &disk_buf, sizeof disk_buf);
    438   1.1    dsl }
    439  1.19    dsl 
    440  1.22    dsl static int
    441  1.19    dsl i386_editboot(ib_params *params)
    442  1.19    dsl {
    443  1.19    dsl 	int		retval;
    444  1.19    dsl 	uint8_t		buf[512];
    445  1.19    dsl 	ssize_t		rv;
    446  1.19    dsl 	uint32_t	magic;
    447  1.19    dsl 	uint32_t	offset;
    448  1.19    dsl 	struct x86_boot_params	*bpp;
    449  1.19    dsl 
    450  1.19    dsl 	assert(params != NULL);
    451  1.19    dsl 	assert(params->fsfd != -1);
    452  1.19    dsl 	assert(params->filesystem != NULL);
    453  1.19    dsl 
    454  1.19    dsl 	retval = 0;
    455  1.19    dsl 
    456  1.19    dsl 	/*
    457  1.19    dsl 	 * Read in the existing bootstrap.
    458  1.21    dsl 	 * Look in any of the first 4 sectors.
    459  1.19    dsl 	 */
    460  1.19    dsl 
    461  1.19    dsl 	bpp = NULL;
    462  1.19    dsl 	for (offset = 0; offset < 4 * 512; offset += 512) {
    463  1.19    dsl 		rv = pread(params->fsfd, &buf, sizeof buf, offset);
    464  1.19    dsl 		if (rv == -1) {
    465  1.19    dsl 			warn("Reading `%s'", params->filesystem);
    466  1.19    dsl 			goto done;
    467  1.19    dsl 		} else if (rv != sizeof buf) {
    468  1.19    dsl 			warnx("Reading `%s': short read", params->filesystem);
    469  1.19    dsl 			goto done;
    470  1.19    dsl 		}
    471  1.19    dsl 
    472  1.21    dsl 		/* Magic number is 4 bytes in (to allow for a jmps) */
    473  1.21    dsl 		/* Also allow any of the magic numbers. */
    474  1.21    dsl 		magic = le32toh(*(uint32_t *)(buf + 4)) | 0xf;
    475  1.21    dsl 		if (magic != (X86_BOOT_MAGIC_1 | 0xf))
    476  1.19    dsl 			continue;
    477  1.21    dsl 
    478  1.21    dsl 		/* The parameters are just after the magic number */
    479  1.19    dsl 		bpp = (void *)(buf + 8);
    480  1.19    dsl 		break;
    481  1.19    dsl 	}
    482  1.19    dsl 	if (bpp == NULL) {
    483  1.21    dsl 		warnx("Invalid magic in existing bootstrap");
    484  1.19    dsl 		goto done;
    485  1.19    dsl 	}
    486  1.19    dsl 
    487  1.19    dsl 	/*
    488  1.19    dsl 	 * Fill in any user-specified options into the
    489  1.19    dsl 	 *      struct x86_boot_params
    490  1.19    dsl 	 * that's 8 bytes in from the start of the third sector.
    491  1.19    dsl 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
    492  1.19    dsl 	 */
    493  1.19    dsl 	if (update_i386_boot_params(params, bpp))
    494  1.19    dsl 		goto done;
    495  1.19    dsl 
    496  1.19    dsl 	if (params->flags & IB_NOWRITE) {
    497  1.19    dsl 		retval = 1;
    498  1.19    dsl 		goto done;
    499  1.19    dsl 	}
    500  1.19    dsl 
    501  1.19    dsl 	/*
    502  1.19    dsl 	 * Write boot code back
    503  1.19    dsl 	 */
    504  1.19    dsl 	rv = pwrite(params->fsfd, buf, sizeof buf, offset);
    505  1.19    dsl 	if (rv == -1) {
    506  1.19    dsl 		warn("Writing `%s'", params->filesystem);
    507  1.19    dsl 		goto done;
    508  1.19    dsl 	} else if (rv != sizeof buf) {
    509  1.19    dsl 		warnx("Writing `%s': short write", params->filesystem);
    510  1.19    dsl 		goto done;
    511  1.19    dsl 	}
    512  1.19    dsl 
    513  1.19    dsl 	retval = 1;
    514  1.19    dsl 
    515  1.19    dsl  done:
    516  1.19    dsl 	return retval;
    517  1.19    dsl }
    518