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