Home | History | Annotate | Line # | Download | only in arch
i386.c revision 1.3
      1 /* $NetBSD: i386.c,v 1.3 2003/04/15 14:35:57 dsl 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 __RCSID("$NetBSD: i386.c,v 1.3 2003/04/15 14:35:57 dsl Exp $");
     41 
     42 #if HAVE_CONFIG_H
     43 #include "config.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 
     48 #include <assert.h>
     49 #include <err.h>
     50 #include <stddef.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 #include <sys/md5.h>
     56 
     57 #include "installboot.h"
     58 
     59 int
     60 i386_setboot(ib_params *params)
     61 {
     62 	int		retval;
     63 	char		*bootstrapbuf;
     64 	uint		bootstrapsize;
     65 	ssize_t		rv;
     66 	uint32_t	magic;
     67 	struct i386_boot_params *bp;
     68 	int		i;
     69 
     70 	assert(params != NULL);
     71 	assert(params->fsfd != -1);
     72 	assert(params->filesystem != NULL);
     73 	assert(params->s1fd != -1);
     74 	assert(params->stage1 != NULL);
     75 
     76 	retval = 0;
     77 	bootstrapbuf = NULL;
     78 
     79 	/*
     80 	 * There is only 8k of space in a UFSv1 partition (and ustarfs)
     81 	 * so ensure we don't splat over anything important.
     82 	 */
     83 	if (params->s1stat.st_size > 8192) {
     84 		warnx("stage1 bootstrap `%s' is larger than 8192 bytes",
     85 			params->stage1);
     86 		return 0;
     87 	}
     88 	/*
     89 	 * Allocate a buffer, with space to round up the input file
     90 	 * to the next block size boundary, and with space for the boot
     91 	 * block.
     92 	 */
     93 	bootstrapsize = roundup(params->s1stat.st_size, 512);
     94 
     95 	bootstrapbuf = malloc(bootstrapsize);
     96 	if (bootstrapbuf == NULL) {
     97 		warn("Allocating %u bytes",  bootstrapsize);
     98 		goto done;
     99 	}
    100 	memset(bootstrapbuf, 0, bootstrapsize);
    101 
    102 	/* read the file into the buffer */
    103 	rv = pread(params->s1fd, bootstrapbuf, params->s1stat.st_size, 0);
    104 	if (rv == -1) {
    105 		warn("Reading `%s'", params->stage1);
    106 		return 0;
    107 	} else if (rv != params->s1stat.st_size) {
    108 		warnx("Reading `%s': short read", params->stage1);
    109 		return 0;
    110 	}
    111 
    112 	magic = *(uint32_t *)(bootstrapbuf + 512 * 2 + 4);
    113 	if (magic != X86_BOOT_MAGIC_1) {
    114 		warnx("Invalid magic in stage1 boostrap %x != %x",
    115 			magic, X86_BOOT_MAGIC_1);
    116 		goto done;
    117 	}
    118 
    119 	/* Fill in any user-specified options */
    120 	bp = (void *)(bootstrapbuf + 512 * 2 + 8);
    121 	if (le32toh(bp->bp_length) < sizeof *bp) {
    122 		warnx("Patch area in stage1 bootstrap is too small");
    123 		goto done;
    124 	}
    125 	if (params->flags & IB_TIMEOUT)
    126 		bp->bp_timeout = htole32(params->timeout);
    127 	if (params->flags & IB_RESETVIDEO)
    128 		bp->bp_flags |= htole32(BP_RESET_VIDEO);
    129 	if (params->flags & IB_CONSPEED)
    130 		bp->bp_conspeed = htole32(params->conspeed);
    131 	if (params->flags & IB_CONSOLE) {
    132 		static const char *names[] = {
    133 			"pc", "com0", "com1", "com2", "com3",
    134 			"com0kbd", "com1kbd", "com2kbd", "com3kbd",
    135 			NULL };
    136 		for (i = 0; ; i++) {
    137 			if (names[i] == NULL) {
    138 				warnx("invalid console name, valid names are:");
    139 				fprintf(stderr, "\t%s", names[0]);
    140 				for (i = 1; names[i] != NULL; i++)
    141 					fprintf(stderr, ", %s", names[i]);
    142 				fprintf(stderr, "\n", names[0]);
    143 				goto done;
    144 			}
    145 			if (strcmp(names[i], params->console) == 0)
    146 				break;
    147 		}
    148 		bp->bp_consdev = htole32(i);
    149 	}
    150 	if (params->flags & IB_PASSWORD) {
    151 		MD5_CTX md5ctx;
    152 		MD5Init(&md5ctx);
    153 		MD5Update(&md5ctx, params->password, strlen(params->password));
    154 		MD5Final(bp->bp_password, &md5ctx);
    155 		bp->bp_flags |= htole32(BP_PASSWORD);
    156 	}
    157 
    158 	if (params->flags & IB_NOWRITE) {
    159 		retval = 1;
    160 		goto done;
    161 	}
    162 
    163 	/* Write pbr code to sector zero */
    164 	rv = pwrite(params->fsfd, bootstrapbuf, 512, 0);
    165 	if (rv == -1) {
    166 		warn("Writing `%s'", params->filesystem);
    167 		goto done;
    168 	} else if (rv != 512) {
    169 		warnx("Writing `%s': short write", params->filesystem);
    170 		goto done;
    171 	}
    172 
    173 	/* Skip disklabel and write bootxx to sectors 2 + */
    174 	rv = pwrite(params->fsfd, bootstrapbuf + 512 * 2,
    175 		    bootstrapsize - 512 * 2, 512 * 2);
    176 	if (rv == -1) {
    177 		warn("Writing `%s'", params->filesystem);
    178 		goto done;
    179 	} else if (rv != bootstrapsize - 512 * 2) {
    180 		warnx("Writing `%s': short write", params->filesystem);
    181 		goto done;
    182 	}
    183 
    184 	retval = 1;
    185 
    186  done:
    187 	if (bootstrapbuf)
    188 		free(bootstrapbuf);
    189 	return retval;
    190 }
    191