Home | History | Annotate | Line # | Download | only in arch
i386.c revision 1.10
      1 /* $NetBSD: i386.c,v 1.10 2003/10/10 01:50:47 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 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(__lint)
     41 __RCSID("$NetBSD: i386.c,v 1.10 2003/10/10 01:50:47 lukem Exp $");
     42 #endif /* __RCSID && !__lint */
     43 
     44 #if HAVE_CONFIG_H
     45 #include "config.h"
     46 #endif
     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;
     65 	char		*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 	 * Ensure bootxx hasn't got code (i.e, non-zero bytes) in
    146 	 * the BIOS Parameter Block (BPB) or the partition table.
    147 	 */
    148 	for (i = 0; i < sizeof(mbr.mbr_bpb); i++) {
    149 		if (*(uint8_t *)(bootstrapbuf + MBR_BPB_OFFSET + i) != 0) {
    150 			warnx("BPB has non-zero byte at offset %d in `%s'",
    151 			    MBR_BPB_OFFSET + i, params->stage1);
    152 			goto done;
    153 		}
    154 	}
    155 	for (i = 0; i < sizeof(mbr.mbr_parts); i++) {
    156 		if (*(uint8_t *)(bootstrapbuf + MBR_PART_OFFSET + i) != 0) {
    157 			warnx(
    158 		    "Partition table has non-zero byte at offset %d in `%s'",
    159 			    MBR_PART_OFFSET + i, params->stage1);
    160 			goto done;
    161 		}
    162 	}
    163 
    164 	/*
    165 	 * Copy the BPB and the partition table from the original MBR to the
    166 	 * temporary buffer so that they're written back to the fs.
    167 	 */
    168 	memcpy(bootstrapbuf + MBR_BPB_OFFSET, &mbr.mbr_bpb,
    169 	    sizeof(mbr.mbr_bpb));
    170 	memcpy(bootstrapbuf + MBR_PART_OFFSET, &mbr.mbr_parts,
    171 	    sizeof(mbr.mbr_parts));
    172 
    173 	/*
    174 	 * Fill in any user-specified options.
    175 	 */
    176 	bp = (void *)(bootstrapbuf + 512 * 2 + 8);
    177 	if (le32toh(bp->bp_length) < sizeof *bp) {
    178 		warnx("Patch area in stage1 bootstrap is too small");
    179 		goto done;
    180 	}
    181 	if (params->flags & IB_TIMEOUT)
    182 		bp->bp_timeout = htole32(params->timeout);
    183 	if (params->flags & IB_RESETVIDEO)
    184 		bp->bp_flags |= htole32(X86_BP_FLAGS_RESET_VIDEO);
    185 	if (params->flags & IB_CONSPEED)
    186 		bp->bp_conspeed = htole32(params->conspeed);
    187 	if (params->flags & IB_CONSOLE) {
    188 		static const char *names[] = {
    189 			"pc", "com0", "com1", "com2", "com3",
    190 			"com0kbd", "com1kbd", "com2kbd", "com3kbd",
    191 			NULL };
    192 		for (i = 0; ; i++) {
    193 			if (names[i] == NULL) {
    194 				warnx("invalid console name, valid names are:");
    195 				fprintf(stderr, "\t%s", names[0]);
    196 				for (i = 1; names[i] != NULL; i++)
    197 					fprintf(stderr, ", %s", names[i]);
    198 				fprintf(stderr, "\n");
    199 				goto done;
    200 			}
    201 			if (strcmp(names[i], params->console) == 0)
    202 				break;
    203 		}
    204 		bp->bp_consdev = htole32(i);
    205 	}
    206 	if (params->flags & IB_PASSWORD) {
    207 		MD5_CTX md5ctx;
    208 		MD5Init(&md5ctx);
    209 		MD5Update(&md5ctx, params->password, strlen(params->password));
    210 		MD5Final(bp->bp_password, &md5ctx);
    211 		bp->bp_flags |= htole32(X86_BP_FLAGS_PASSWORD);
    212 	}
    213 
    214 	if (params->flags & IB_NOWRITE) {
    215 		retval = 1;
    216 		goto done;
    217 	}
    218 
    219 	/*
    220 	 * Write MBR code to sector zero.
    221 	 */
    222 	rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
    223 	if (rv == -1) {
    224 		warn("Writing `%s'", params->filesystem);
    225 		goto done;
    226 	} else if (rv != 512) {
    227 		warnx("Writing `%s': short write", params->filesystem);
    228 		goto done;
    229 	}
    230 
    231 	/*
    232 	 * Skip disklabel in sector 1 and write bootxx to sectors 2..N.
    233 	 */
    234 	rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
    235 		    bootstrapsize - 512 * 2, 512 * 2);
    236 	if (rv == -1) {
    237 		warn("Writing `%s'", params->filesystem);
    238 		goto done;
    239 	} else if (rv != bootstrapsize - 512 * 2) {
    240 		warnx("Writing `%s': short write", params->filesystem);
    241 		goto done;
    242 	}
    243 
    244 	retval = 1;
    245 
    246  done:
    247 	if (bootstrapbuf)
    248 		free(bootstrapbuf);
    249 	return retval;
    250 }
    251