Home | History | Annotate | Line # | Download | only in arch
i386.c revision 1.20
      1  1.20    dsl /* $NetBSD: i386.c,v 1.20 2006/01/24 18:35:18 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.20    dsl __RCSID("$NetBSD: i386.c,v 1.20 2006/01/24 18:35:18 dsl 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.19    dsl #define nelem(x) (sizeof (x)/sizeof *(x))
     62  1.19    dsl 
     63  1.19    dsl static const char *const console_names[] = {
     64  1.19    dsl 	"pc", "com0", "com1", "com2", "com3",
     65  1.19    dsl 	"com0kbd", "com1kbd", "com2kbd", "com3kbd",
     66  1.19    dsl 	NULL };
     67  1.19    dsl 
     68  1.19    dsl static void
     69  1.19    dsl show_i386_boot_params(struct x86_boot_params  *bpp)
     70  1.19    dsl {
     71  1.19    dsl 	uint32_t i;
     72  1.19    dsl 
     73  1.19    dsl 	printf("Boot options:        ");
     74  1.19    dsl 	printf("timeout %d, ", le32toh(bpp->bp_timeout));
     75  1.19    dsl 	printf("flags %x, ", le32toh(bpp->bp_flags));
     76  1.19    dsl 	printf("speed %d, ", le32toh(bpp->bp_conspeed));
     77  1.19    dsl 	printf("ioaddr %x, ", le32toh(bpp->bp_consaddr));
     78  1.19    dsl 	i = le32toh(bpp->bp_consdev);
     79  1.19    dsl 	if (i < nelem(console_names) - 1)
     80  1.19    dsl 		printf("console %s\n", console_names[i]);
     81  1.19    dsl 	else
     82  1.19    dsl 		printf("console %d\n", i);
     83  1.19    dsl 	if (bpp->bp_keymap[0])
     84  1.19    dsl 		printf("                     keymap %s\n", bpp->bp_keymap);
     85  1.19    dsl }
     86  1.19    dsl 
     87  1.19    dsl static int
     88  1.19    dsl update_i386_boot_params(ib_params *params, struct x86_boot_params  *bpp)
     89  1.19    dsl {
     90  1.19    dsl 	struct x86_boot_params bp;
     91  1.19    dsl 	int bplen;
     92  1.19    dsl 	int i;
     93  1.19    dsl 
     94  1.19    dsl 	bplen = le32toh(bpp->bp_length);
     95  1.19    dsl 	if (bplen > sizeof bp)
     96  1.19    dsl 		/* Ignore pad space in bootxx */
     97  1.19    dsl 		bplen = sizeof bp;
     98  1.19    dsl 
     99  1.19    dsl 	/* Take (and update) local copy so we handle size mismatches */
    100  1.19    dsl 	memset(&bp, 0, sizeof bp);
    101  1.19    dsl 	memcpy(&bp, bpp, bplen);
    102  1.19    dsl 
    103  1.19    dsl 	if (params->flags & IB_TIMEOUT)
    104  1.19    dsl 		bp.bp_timeout = htole32(params->timeout);
    105  1.19    dsl 	if (params->flags & IB_RESETVIDEO)
    106  1.19    dsl 		bp.bp_flags ^= htole32(X86_BP_FLAGS_RESET_VIDEO);
    107  1.19    dsl 	if (params->flags & IB_CONSPEED)
    108  1.19    dsl 		bp.bp_conspeed = htole32(params->conspeed);
    109  1.19    dsl 	if (params->flags & IB_CONSADDR)
    110  1.19    dsl 		bp.bp_consaddr = htole32(params->consaddr);
    111  1.19    dsl 	if (params->flags & IB_CONSOLE) {
    112  1.19    dsl 		for (i = 0; ; i++) {
    113  1.19    dsl 			if (console_names[i] == NULL) {
    114  1.19    dsl 				warnx("invalid console name, valid names are:");
    115  1.19    dsl 				fprintf(stderr, "\t%s", console_names[0]);
    116  1.19    dsl 				for (i = 1; console_names[i] != NULL; i++)
    117  1.19    dsl 					fprintf(stderr, ", %s", console_names[i]);
    118  1.19    dsl 				fprintf(stderr, "\n");
    119  1.19    dsl 				return 1;
    120  1.19    dsl 			}
    121  1.19    dsl 			if (strcmp(console_names[i], params->console) == 0)
    122  1.19    dsl 				break;
    123  1.19    dsl 		}
    124  1.19    dsl 		bp.bp_consdev = htole32(i);
    125  1.19    dsl 	}
    126  1.19    dsl 	if (params->flags & IB_PASSWORD) {
    127  1.19    dsl 		if (params->password[0]) {
    128  1.19    dsl 			MD5_CTX md5ctx;
    129  1.19    dsl 			MD5Init(&md5ctx);
    130  1.19    dsl 			MD5Update(&md5ctx, params->password,
    131  1.19    dsl 			    strlen(params->password));
    132  1.19    dsl 			MD5Final(bp.bp_password, &md5ctx);
    133  1.19    dsl 			bp.bp_flags |= htole32(X86_BP_FLAGS_PASSWORD);
    134  1.19    dsl 		} else {
    135  1.19    dsl 			memset(&bp.bp_password, 0, sizeof bp.bp_password);
    136  1.19    dsl 			bp.bp_flags &= ~htole32(X86_BP_FLAGS_PASSWORD);
    137  1.19    dsl 		}
    138  1.19    dsl 	}
    139  1.19    dsl 	if (params->flags & IB_KEYMAP)
    140  1.19    dsl 		strlcpy(bp.bp_keymap, params->keymap, sizeof bp.bp_keymap);
    141  1.19    dsl 
    142  1.19    dsl 	if (params->flags & (IB_NOWRITE | IB_VERBOSE))
    143  1.19    dsl 		show_i386_boot_params(&bp);
    144  1.19    dsl 
    145  1.19    dsl 	/* Check we aren't trying to set anything we can't save */
    146  1.19    dsl 	if (bplen < sizeof bp && memcmp((char *)&bp + bplen,
    147  1.19    dsl 					(char *)&bp + bplen + 1,
    148  1.19    dsl 					sizeof bp - bplen - 1) != 0) {
    149  1.19    dsl 		warnx("Patch area in stage1 bootstrap is too small");
    150  1.19    dsl 		return 1;
    151  1.19    dsl 	}
    152  1.19    dsl 	memcpy(bpp, &bp, bplen);
    153  1.19    dsl 	return 0;
    154  1.19    dsl }
    155  1.19    dsl 
    156   1.1    dsl int
    157   1.1    dsl i386_setboot(ib_params *params)
    158   1.1    dsl {
    159  1.11  lukem 	int		retval, i, bpbsize;
    160  1.11  lukem 	uint8_t		*bootstrapbuf;
    161  1.18   yamt 	u_int		bootstrapsize;
    162   1.1    dsl 	ssize_t		rv;
    163   1.1    dsl 	uint32_t	magic;
    164  1.19    dsl 	struct x86_boot_params	*bpp;
    165   1.9  lukem 	struct mbr_sector	mbr;
    166   1.1    dsl 
    167   1.1    dsl 	assert(params != NULL);
    168   1.1    dsl 	assert(params->fsfd != -1);
    169   1.1    dsl 	assert(params->filesystem != NULL);
    170   1.1    dsl 	assert(params->s1fd != -1);
    171   1.1    dsl 	assert(params->stage1 != NULL);
    172   1.1    dsl 
    173   1.1    dsl 	retval = 0;
    174   1.1    dsl 	bootstrapbuf = NULL;
    175   1.1    dsl 
    176   1.2    dsl 	/*
    177   1.2    dsl 	 * There is only 8k of space in a UFSv1 partition (and ustarfs)
    178   1.2    dsl 	 * so ensure we don't splat over anything important.
    179   1.2    dsl 	 */
    180   1.2    dsl 	if (params->s1stat.st_size > 8192) {
    181   1.2    dsl 		warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
    182   1.2    dsl 			params->stage1);
    183   1.9  lukem 		goto done;
    184   1.9  lukem 	}
    185   1.9  lukem 
    186   1.9  lukem 	/*
    187   1.9  lukem 	 * Read in the existing MBR.
    188   1.9  lukem 	 */
    189   1.9  lukem 	rv = pread(params->fsfd, &mbr, sizeof(mbr), MBR_BBSECTOR);
    190   1.9  lukem 	if (rv == -1) {
    191   1.9  lukem 		warn("Reading `%s'", params->filesystem);
    192   1.9  lukem 		goto done;
    193   1.9  lukem 	} else if (rv != sizeof(mbr)) {
    194   1.9  lukem 		warnx("Reading `%s': short read", params->filesystem);
    195   1.9  lukem 		goto done;
    196   1.1    dsl 	}
    197  1.10  lukem 	if (mbr.mbr_magic != le16toh(MBR_MAGIC)) {
    198   1.9  lukem 		if (params->flags & IB_VERBOSE) {
    199   1.9  lukem 			printf(
    200   1.9  lukem 		    "Ignoring MBR with invalid magic in sector 0 of `%s'\n",
    201   1.9  lukem 			    params->filesystem);
    202   1.9  lukem 		}
    203   1.9  lukem 		memset(&mbr, 0, sizeof(mbr));
    204   1.9  lukem 	}
    205   1.9  lukem 
    206   1.1    dsl 	/*
    207   1.1    dsl 	 * Allocate a buffer, with space to round up the input file
    208   1.1    dsl 	 * to the next block size boundary, and with space for the boot
    209   1.1    dsl 	 * block.
    210   1.1    dsl 	 */
    211   1.1    dsl 	bootstrapsize = roundup(params->s1stat.st_size, 512);
    212   1.1    dsl 
    213   1.1    dsl 	bootstrapbuf = malloc(bootstrapsize);
    214   1.1    dsl 	if (bootstrapbuf == NULL) {
    215   1.1    dsl 		warn("Allocating %u bytes",  bootstrapsize);
    216   1.1    dsl 		goto done;
    217   1.1    dsl 	}
    218   1.1    dsl 	memset(bootstrapbuf, 0, bootstrapsize);
    219   1.1    dsl 
    220   1.9  lukem 	/*
    221   1.9  lukem 	 * Read the file into the buffer.
    222   1.9  lukem 	 */
    223   1.1    dsl 	rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
    224   1.1    dsl 	if (rv == -1) {
    225   1.1    dsl 		warn("Reading `%s'", params->stage1);
    226   1.9  lukem 		goto done;
    227   1.1    dsl 	} else if (rv != params->s1stat.st_size) {
    228   1.1    dsl 		warnx("Reading `%s': short read", params->stage1);
    229   1.9  lukem 		goto done;
    230   1.1    dsl 	}
    231   1.1    dsl 
    232   1.1    dsl 	magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
    233   1.7    dsl 	if (magic != htole32(X86_BOOT_MAGIC_1)) {
    234  1.20    dsl 		warnx("Invalid magic in stage1 bootstrap %x != %x",
    235   1.7    dsl 			magic, htole32(X86_BOOT_MAGIC_1));
    236   1.2    dsl 		goto done;
    237   1.2    dsl 	}
    238   1.2    dsl 
    239   1.9  lukem 	/*
    240  1.11  lukem 	 * Determine size of BIOS Parameter Block (BPB) to copy from
    241  1.11  lukem 	 * original MBR to the temporary buffer by examining the first
    242  1.11  lukem 	 * few instruction in the new bootblock.  Supported values:
    243  1.11  lukem 	 *	eb 3c 90	jmp ENDOF(mbr_bpbFAT16)+1, nop
    244  1.11  lukem 	 *	eb 58 90	jmp ENDOF(mbr_bpbFAT32)+1, nop
    245  1.11  lukem 	 *      (anything else)	; don't preserve
    246  1.11  lukem 	 */
    247  1.11  lukem 	bpbsize = 0;
    248  1.11  lukem 	if (bootstrapbuf[0] == 0xeb && bootstrapbuf[2] == 0x90 &&
    249  1.11  lukem 	    (bootstrapbuf[1] == 0x3c || bootstrapbuf[1] == 0x58))
    250  1.11  lukem 		bpbsize = bootstrapbuf[1] + 2 - MBR_BPB_OFFSET;
    251  1.11  lukem 
    252  1.11  lukem 	/*
    253  1.15  lukem 	 * Ensure bootxx hasn't got any code or data (i.e, non-zero bytes) in
    254  1.11  lukem 	 * the partition table.
    255   1.9  lukem 	 */
    256   1.9  lukem 	for (i = 0; i < sizeof(mbr.mbr_parts); i++) {
    257   1.9  lukem 		if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) {
    258   1.9  lukem 			warnx(
    259   1.9  lukem 		    "Partition table has non-zero byte at offset %d in `%s'",
    260   1.9  lukem 			    MBR_PART_OFFSET + i, params->stage1);
    261   1.9  lukem 			goto done;
    262   1.9  lukem 		}
    263   1.9  lukem 	}
    264   1.9  lukem 
    265   1.9  lukem 	/*
    266   1.9  lukem 	 * Copy the BPB and the partition table from the original MBR to the
    267   1.9  lukem 	 * temporary buffer so that they're written back to the fs.
    268   1.9  lukem 	 */
    269  1.11  lukem 	if (bpbsize != 0) {
    270  1.11  lukem 		if (params->flags & IB_VERBOSE)
    271  1.11  lukem 			printf("Preserving %d (%#x) bytes of the BPB\n",
    272  1.11  lukem 			    bpbsize, bpbsize);
    273  1.11  lukem 		memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb, bpbsize);
    274  1.11  lukem 	}
    275   1.9  lukem 	memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts,
    276   1.9  lukem 	    sizeof(mbr.mbr_parts));
    277   1.9  lukem 
    278   1.9  lukem 	/*
    279  1.15  lukem 	 * Fill in any user-specified options into the
    280  1.17    dsl 	 *      struct x86_boot_params
    281  1.15  lukem 	 * that's 8 bytes in from the start of the third sector.
    282  1.15  lukem 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
    283   1.9  lukem 	 */
    284  1.17    dsl 	bpp = (void *)(bootstrapbuf + 512 * 2 + 8);
    285  1.19    dsl 	if (update_i386_boot_params(params, bpp))
    286  1.17    dsl 		goto done;
    287   1.1    dsl 
    288   1.1    dsl 	if (params->flags & IB_NOWRITE) {
    289   1.1    dsl 		retval = 1;
    290   1.1    dsl 		goto done;
    291   1.1    dsl 	}
    292   1.1    dsl 
    293   1.9  lukem 	/*
    294   1.9  lukem 	 * Write MBR code to sector zero.
    295   1.9  lukem 	 */
    296   1.1    dsl 	rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
    297   1.1    dsl 	if (rv == -1) {
    298   1.1    dsl 		warn("Writing `%s'", params->filesystem);
    299   1.1    dsl 		goto done;
    300   1.1    dsl 	} else if (rv != 512) {
    301   1.1    dsl 		warnx("Writing `%s': short write", params->filesystem);
    302   1.1    dsl 		goto done;
    303   1.1    dsl 	}
    304   1.1    dsl 
    305   1.9  lukem 	/*
    306   1.9  lukem 	 * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
    307   1.9  lukem 	 */
    308   1.1    dsl 	rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
    309   1.1    dsl 		    bootstrapsize - 512 * 2, 512 * 2);
    310   1.1    dsl 	if (rv == -1) {
    311   1.1    dsl 		warn("Writing `%s'", params->filesystem);
    312   1.1    dsl 		goto done;
    313   1.1    dsl 	} else if (rv != bootstrapsize - 512 * 2) {
    314   1.1    dsl 		warnx("Writing `%s': short write", params->filesystem);
    315   1.1    dsl 		goto done;
    316   1.1    dsl 	}
    317   1.1    dsl 
    318   1.1    dsl 	retval = 1;
    319   1.1    dsl 
    320   1.1    dsl  done:
    321   1.1    dsl 	if (bootstrapbuf)
    322   1.1    dsl 		free(bootstrapbuf);
    323   1.1    dsl 	return retval;
    324   1.1    dsl }
    325  1.19    dsl 
    326  1.19    dsl int
    327  1.19    dsl i386_editboot(ib_params *params)
    328  1.19    dsl {
    329  1.19    dsl 	int		retval;
    330  1.19    dsl 	uint8_t		buf[512];
    331  1.19    dsl 	ssize_t		rv;
    332  1.19    dsl 	uint32_t	magic;
    333  1.19    dsl 	uint32_t	offset;
    334  1.19    dsl 	struct x86_boot_params	*bpp;
    335  1.19    dsl 
    336  1.19    dsl 	assert(params != NULL);
    337  1.19    dsl 	assert(params->fsfd != -1);
    338  1.19    dsl 	assert(params->filesystem != NULL);
    339  1.19    dsl 
    340  1.19    dsl 	retval = 0;
    341  1.19    dsl 
    342  1.19    dsl 	/*
    343  1.19    dsl 	 * Read in the existing bootstrap.
    344  1.19    dsl 	 */
    345  1.19    dsl 
    346  1.19    dsl 	bpp = NULL;
    347  1.19    dsl 	for (offset = 0; offset < 4 * 512; offset += 512) {
    348  1.19    dsl 		rv = pread(params->fsfd, &buf, sizeof buf, offset);
    349  1.19    dsl 		if (rv == -1) {
    350  1.19    dsl 			warn("Reading `%s'", params->filesystem);
    351  1.19    dsl 			goto done;
    352  1.19    dsl 		} else if (rv != sizeof buf) {
    353  1.19    dsl 			warnx("Reading `%s': short read", params->filesystem);
    354  1.19    dsl 			goto done;
    355  1.19    dsl 		}
    356  1.19    dsl 
    357  1.19    dsl 		magic = *(uint32_t *)(buf + 4) | 0xf;
    358  1.19    dsl 		if (magic != htole32(X86_BOOT_MAGIC_1 | 0xf))
    359  1.19    dsl 			continue;
    360  1.19    dsl 		bpp = (void *)(buf + 8);
    361  1.19    dsl 		break;
    362  1.19    dsl 	}
    363  1.19    dsl 	if (bpp == NULL) {
    364  1.19    dsl 		warnx("Invalid magic in stage1 boostrap");
    365  1.19    dsl 		goto done;
    366  1.19    dsl 	}
    367  1.19    dsl 
    368  1.19    dsl 	/*
    369  1.19    dsl 	 * Fill in any user-specified options into the
    370  1.19    dsl 	 *      struct x86_boot_params
    371  1.19    dsl 	 * that's 8 bytes in from the start of the third sector.
    372  1.19    dsl 	 * See sys/arch/i386/stand/bootxx/bootxx.S for more information.
    373  1.19    dsl 	 */
    374  1.19    dsl 	if (update_i386_boot_params(params, bpp))
    375  1.19    dsl 		goto done;
    376  1.19    dsl 
    377  1.19    dsl 	if (params->flags & IB_NOWRITE) {
    378  1.19    dsl 		retval = 1;
    379  1.19    dsl 		goto done;
    380  1.19    dsl 	}
    381  1.19    dsl 
    382  1.19    dsl 	/*
    383  1.19    dsl 	 * Write boot code back
    384  1.19    dsl 	 */
    385  1.19    dsl 	rv = pwrite(params->fsfd, buf, sizeof buf, offset);
    386  1.19    dsl 	if (rv == -1) {
    387  1.19    dsl 		warn("Writing `%s'", params->filesystem);
    388  1.19    dsl 		goto done;
    389  1.19    dsl 	} else if (rv != sizeof buf) {
    390  1.19    dsl 		warnx("Writing `%s': short write", params->filesystem);
    391  1.19    dsl 		goto done;
    392  1.19    dsl 	}
    393  1.19    dsl 
    394  1.19    dsl 	retval = 1;
    395  1.19    dsl 
    396  1.19    dsl  done:
    397  1.19    dsl 	return retval;
    398  1.19    dsl }
    399