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