Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.1
      1  1.1  lukem /*	$NetBSD: installboot.c,v 1.1 2002/04/03 05:21:17 lukem Exp $	*/
      2  1.1  lukem 
      3  1.1  lukem /*-
      4  1.1  lukem  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  lukem  * All rights reserved.
      6  1.1  lukem  *
      7  1.1  lukem  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  lukem  * by Luke Mewburn of Wasabi Systems.
      9  1.1  lukem  *
     10  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     11  1.1  lukem  * modification, are permitted provided that the following conditions
     12  1.1  lukem  * are met:
     13  1.1  lukem  * 1. Redistributions of source code must retain the above copyright
     14  1.1  lukem  *    notice, this list of conditions and the following disclaimer.
     15  1.1  lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  lukem  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  lukem  *    documentation and/or other materials provided with the distribution.
     18  1.1  lukem  * 3. All advertising materials mentioning features or use of this software
     19  1.1  lukem  *    must display the following acknowledgement:
     20  1.1  lukem  *	This product includes software developed by the NetBSD
     21  1.1  lukem  *	Foundation, Inc. and its contributors.
     22  1.1  lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  lukem  *    contributors may be used to endorse or promote products derived
     24  1.1  lukem  *    from this software without specific prior written permission.
     25  1.1  lukem  *
     26  1.1  lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  lukem  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  lukem  */
     38  1.1  lukem 
     39  1.1  lukem #include <sys/cdefs.h>
     40  1.1  lukem #if defined(__RCSID) && !defined(__lint)
     41  1.1  lukem __RCSID("$NetBSD: installboot.c,v 1.1 2002/04/03 05:21:17 lukem Exp $");
     42  1.1  lukem #endif	/* !__lint */
     43  1.1  lukem 
     44  1.1  lukem #include <sys/utsname.h>
     45  1.1  lukem 
     46  1.1  lukem #include <assert.h>
     47  1.1  lukem #include <err.h>
     48  1.1  lukem #include <fcntl.h>
     49  1.1  lukem #include <stdio.h>
     50  1.1  lukem #include <stdlib.h>
     51  1.1  lukem #include <string.h>
     52  1.1  lukem #include <unistd.h>
     53  1.1  lukem 
     54  1.1  lukem #include "installboot.h"
     55  1.1  lukem 
     56  1.1  lukem int		main(int, char *[]);
     57  1.1  lukem static	int	getmachine(ib_params *, const char *, const char *);
     58  1.1  lukem static	void	usage(void);
     59  1.1  lukem 
     60  1.1  lukem static	ib_params	installboot_params;
     61  1.1  lukem 
     62  1.1  lukem int
     63  1.1  lukem main(int argc, char *argv[])
     64  1.1  lukem {
     65  1.1  lukem 	struct utsname	utsname;
     66  1.1  lukem 	ib_params	*params;
     67  1.1  lukem 	long		lval;
     68  1.1  lukem 	int		ch, rv, mode;
     69  1.1  lukem 	char 		*p;
     70  1.1  lukem 	const char	*op;
     71  1.1  lukem 
     72  1.1  lukem 	setprogname(argv[0]);
     73  1.1  lukem 	params = &installboot_params;
     74  1.1  lukem 	memset(params, 0, sizeof(*params));
     75  1.1  lukem 	if ((p = getenv("MACHINE")) != NULL)
     76  1.1  lukem 		if (! getmachine(params, p, "$MACHINE"))
     77  1.1  lukem 			exit(1);
     78  1.1  lukem 
     79  1.1  lukem 	while ((ch = getopt(argc, argv, "b:cm:no:t:v")) != -1) {
     80  1.1  lukem 		switch (ch) {
     81  1.1  lukem 
     82  1.1  lukem 		case 'b':
     83  1.1  lukem 			if (*optarg == '\0')
     84  1.1  lukem 				goto badblock;
     85  1.1  lukem 			lval = strtol(optarg, &p, 0);
     86  1.1  lukem 			if (lval < 0 || lval >= LONG_MAX || *p != '\0') {
     87  1.1  lukem  badblock:
     88  1.1  lukem 				errx(1, "Invalid block number `%s'", optarg);
     89  1.1  lukem 			}
     90  1.1  lukem 			params->startblock = lval;
     91  1.1  lukem 			params->flags |= IB_STARTBLOCK;
     92  1.1  lukem 			break;
     93  1.1  lukem 
     94  1.1  lukem 		case 'c':
     95  1.1  lukem 			params->flags |= IB_CLEAR;
     96  1.1  lukem 			break;
     97  1.1  lukem 
     98  1.1  lukem 		case 'm':
     99  1.1  lukem 			if (! getmachine(params, optarg, "-m"))
    100  1.1  lukem 				exit(1);
    101  1.1  lukem 			break;
    102  1.1  lukem 
    103  1.1  lukem 		case 'n':
    104  1.1  lukem 			params->flags |= IB_NOWRITE;
    105  1.1  lukem 			break;
    106  1.1  lukem 
    107  1.1  lukem 		case 'o':
    108  1.1  lukem 			if (params->machine == NULL)
    109  1.1  lukem 				errx(1,
    110  1.1  lukem 				    "Machine needs to be specified before -o");
    111  1.1  lukem 			while ((p = strsep(&optarg, ",")) != NULL) {
    112  1.1  lukem 				if (*p == '\0')
    113  1.1  lukem 					errx(1, "Empty `-o' option");
    114  1.1  lukem 				if (! params->machine->parseopt(params, p))
    115  1.1  lukem 					exit(1);
    116  1.1  lukem 			}
    117  1.1  lukem 			break;
    118  1.1  lukem 
    119  1.1  lukem 		case 't':
    120  1.1  lukem 			params->fstype = optarg;	// XXX: validate?
    121  1.1  lukem 			break;
    122  1.1  lukem 
    123  1.1  lukem 		case 'v':
    124  1.1  lukem 			params->flags |= IB_VERBOSE;
    125  1.1  lukem 			break;
    126  1.1  lukem 
    127  1.1  lukem 		case '?':
    128  1.1  lukem 		default:
    129  1.1  lukem 			usage();
    130  1.1  lukem 			/* NOTREACHED */
    131  1.1  lukem 
    132  1.1  lukem 		}
    133  1.1  lukem 	}
    134  1.1  lukem 	argc -= optind;
    135  1.1  lukem 	argv += optind;
    136  1.1  lukem 
    137  1.1  lukem 	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
    138  1.1  lukem 	    ((params->flags & IB_CLEAR) == 0 && argc != 2))
    139  1.1  lukem 		usage();
    140  1.1  lukem 
    141  1.1  lukem 		/* set missing defaults */
    142  1.1  lukem 	if (params->machine == NULL) {
    143  1.1  lukem 		if (uname(&utsname) == -1)
    144  1.1  lukem 			err(1, "Determine uname");
    145  1.1  lukem 		if (! getmachine(params, utsname.machine, "uname()"))
    146  1.1  lukem 			exit(1);
    147  1.1  lukem 	}
    148  1.1  lukem 	// XXX: set default params->fstype
    149  1.1  lukem 
    150  1.1  lukem 	params->filesystem = argv[0];
    151  1.1  lukem 	if (params->flags & IB_NOWRITE) {
    152  1.1  lukem 		op = "only";
    153  1.1  lukem 		mode = O_RDONLY;
    154  1.1  lukem 	} else {
    155  1.1  lukem 		op = "write";
    156  1.1  lukem 		mode = O_RDWR;
    157  1.1  lukem 	}
    158  1.1  lukem 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    159  1.1  lukem 		err(1, "Opening file system `%s' read-%s",
    160  1.1  lukem 		    params->filesystem, op);
    161  1.1  lukem 
    162  1.1  lukem 	if (argc == 2) {
    163  1.1  lukem 		params->bootblock = argv[1];
    164  1.1  lukem 		if ((params->bbfd = open(params->bootblock, O_RDONLY, 0600))
    165  1.1  lukem 		    == -1)
    166  1.1  lukem 			err(1, "Opening boot block `%s'", params->bootblock);
    167  1.1  lukem 	}
    168  1.1  lukem 	assert(params->machine != NULL);
    169  1.1  lukem 
    170  1.1  lukem 	if (params->flags & IB_VERBOSE) {
    171  1.1  lukem 		printf("File system: %s\n", params->filesystem);
    172  1.1  lukem 		printf("Boot block:  %s\n",
    173  1.1  lukem 		    (params->flags & IB_CLEAR) ? "(to be cleared)"
    174  1.1  lukem 		    : params->bootblock);
    175  1.1  lukem 	}
    176  1.1  lukem 
    177  1.1  lukem 	if (params->flags & IB_CLEAR) {
    178  1.1  lukem 		op = "Clear";
    179  1.1  lukem 		rv = params->machine->clearboot(params);
    180  1.1  lukem 	} else {
    181  1.1  lukem 		op = "Set";
    182  1.1  lukem 		rv = params->machine->setboot(params);
    183  1.1  lukem 	}
    184  1.1  lukem 	if (rv == 0)
    185  1.1  lukem 		errx(1, "%s boot block operation failed", op);
    186  1.1  lukem 
    187  1.1  lukem 	if (close(params->fsfd) == -1)
    188  1.1  lukem 		err(1, "Closing file system `%s'", params->filesystem);
    189  1.1  lukem 	if (argc == 2)
    190  1.1  lukem 		if (close(params->bbfd) == -1)
    191  1.1  lukem 			err(1, "Closing boot block `%s'", params->bootblock);
    192  1.1  lukem 
    193  1.1  lukem 	exit(0);
    194  1.1  lukem 	/* NOTREACHED */
    195  1.1  lukem }
    196  1.1  lukem 
    197  1.1  lukem int
    198  1.1  lukem parseoptionflag(ib_params *params, const char *option, ib_flags wantflags)
    199  1.1  lukem {
    200  1.1  lukem 	struct {
    201  1.1  lukem 		const char	*name;
    202  1.1  lukem 		ib_flags	flag;
    203  1.1  lukem 	} flags[] = {
    204  1.1  lukem 		{ "alphasum",	IB_ALPHASUM },
    205  1.1  lukem 		{ "append",	IB_APPEND },
    206  1.1  lukem 		{ "sunsum",	IB_SUNSUM },
    207  1.1  lukem 		{ NULL,		0 },
    208  1.1  lukem 	};
    209  1.1  lukem 
    210  1.1  lukem 	int	i;
    211  1.1  lukem 
    212  1.1  lukem 	assert(params != NULL);
    213  1.1  lukem 	assert(option != NULL);
    214  1.1  lukem 
    215  1.1  lukem 	for (i = 0; flags[i].name != NULL; i++) {
    216  1.1  lukem 		if ((strcmp(flags[i].name, option) == 0) &&
    217  1.1  lukem 		    (wantflags & flags[i].flag)) {
    218  1.1  lukem 			params->flags |= flags[i].flag;
    219  1.1  lukem 			return (1);
    220  1.1  lukem 		}
    221  1.1  lukem 	}
    222  1.1  lukem 	return (0);
    223  1.1  lukem }
    224  1.1  lukem 
    225  1.1  lukem 
    226  1.1  lukem static int
    227  1.1  lukem getmachine(ib_params *param, const char *mach, const char *provider)
    228  1.1  lukem {
    229  1.1  lukem 	int	i;
    230  1.1  lukem 
    231  1.1  lukem 	assert(param != NULL);
    232  1.1  lukem 	assert(mach != NULL);
    233  1.1  lukem 
    234  1.1  lukem 	for (i = 0; machines[i].name != NULL; i++) {
    235  1.1  lukem 		if (strcmp(machines[i].name, mach) == 0) {
    236  1.1  lukem 			param->machine = &machines[i];
    237  1.1  lukem 			return (1);
    238  1.1  lukem 		}
    239  1.1  lukem 	}
    240  1.1  lukem 	warnx("Invalid machine `%s' from %s", mach, provider);
    241  1.1  lukem 	warnx("Supported machines are:");
    242  1.1  lukem #define MACHS_PER_LINE	10
    243  1.1  lukem 	for (i = 0; machines[i].name != NULL; i++) {
    244  1.1  lukem 		fputs((i % MACHS_PER_LINE) ? ", " : "\t", stderr);
    245  1.1  lukem 		fputs(machines[i].name, stderr);
    246  1.1  lukem 		if ((i % MACHS_PER_LINE) == (MACHS_PER_LINE - 1))
    247  1.1  lukem 			fputs("\n", stderr);
    248  1.1  lukem 	}
    249  1.1  lukem 	if ((i % MACHS_PER_LINE) != 0)
    250  1.1  lukem 		fputs("\n", stderr);
    251  1.1  lukem 	return (0);
    252  1.1  lukem }
    253  1.1  lukem 
    254  1.1  lukem static void
    255  1.1  lukem usage(void)
    256  1.1  lukem {
    257  1.1  lukem 	const char	*prog;
    258  1.1  lukem 
    259  1.1  lukem 	prog = getprogname();
    260  1.1  lukem 	fprintf(stderr,
    261  1.1  lukem "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
    262  1.1  lukem "\t\t   [-b block] filesystem bootstrap\n"
    263  1.1  lukem "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
    264  1.1  lukem 	    prog, prog);
    265  1.1  lukem 	exit(1);
    266  1.1  lukem }
    267