Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.4
      1  1.4   lukem /*	$NetBSD: installboot.c,v 1.4 2002/04/12 03:15:20 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.4   lukem __RCSID("$NetBSD: installboot.c,v 1.4 2002/04/12 03:15:20 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.2  simonb #include <limits.h>
     50  1.1   lukem #include <stdio.h>
     51  1.1   lukem #include <stdlib.h>
     52  1.1   lukem #include <string.h>
     53  1.1   lukem #include <unistd.h>
     54  1.1   lukem 
     55  1.1   lukem #include "installboot.h"
     56  1.1   lukem 
     57  1.1   lukem int		main(int, char *[]);
     58  1.1   lukem static	int	getmachine(ib_params *, const char *, const char *);
     59  1.1   lukem static	void	usage(void);
     60  1.1   lukem 
     61  1.1   lukem static	ib_params	installboot_params;
     62  1.1   lukem 
     63  1.1   lukem int
     64  1.1   lukem main(int argc, char *argv[])
     65  1.1   lukem {
     66  1.1   lukem 	struct utsname	utsname;
     67  1.1   lukem 	ib_params	*params;
     68  1.1   lukem 	long		lval;
     69  1.1   lukem 	int		ch, rv, mode;
     70  1.1   lukem 	char 		*p;
     71  1.1   lukem 	const char	*op;
     72  1.1   lukem 
     73  1.1   lukem 	setprogname(argv[0]);
     74  1.1   lukem 	params = &installboot_params;
     75  1.1   lukem 	memset(params, 0, sizeof(*params));
     76  1.1   lukem 	if ((p = getenv("MACHINE")) != NULL)
     77  1.1   lukem 		if (! getmachine(params, p, "$MACHINE"))
     78  1.1   lukem 			exit(1);
     79  1.1   lukem 
     80  1.1   lukem 	while ((ch = getopt(argc, argv, "b:cm:no:t:v")) != -1) {
     81  1.1   lukem 		switch (ch) {
     82  1.1   lukem 
     83  1.1   lukem 		case 'b':
     84  1.1   lukem 			if (*optarg == '\0')
     85  1.1   lukem 				goto badblock;
     86  1.1   lukem 			lval = strtol(optarg, &p, 0);
     87  1.1   lukem 			if (lval < 0 || lval >= LONG_MAX || *p != '\0') {
     88  1.1   lukem  badblock:
     89  1.1   lukem 				errx(1, "Invalid block number `%s'", optarg);
     90  1.1   lukem 			}
     91  1.1   lukem 			params->startblock = lval;
     92  1.1   lukem 			params->flags |= IB_STARTBLOCK;
     93  1.1   lukem 			break;
     94  1.1   lukem 
     95  1.1   lukem 		case 'c':
     96  1.1   lukem 			params->flags |= IB_CLEAR;
     97  1.1   lukem 			break;
     98  1.1   lukem 
     99  1.1   lukem 		case 'm':
    100  1.1   lukem 			if (! getmachine(params, optarg, "-m"))
    101  1.1   lukem 				exit(1);
    102  1.1   lukem 			break;
    103  1.1   lukem 
    104  1.1   lukem 		case 'n':
    105  1.1   lukem 			params->flags |= IB_NOWRITE;
    106  1.1   lukem 			break;
    107  1.1   lukem 
    108  1.1   lukem 		case 'o':
    109  1.1   lukem 			if (params->machine == NULL)
    110  1.1   lukem 				errx(1,
    111  1.1   lukem 				    "Machine needs to be specified before -o");
    112  1.1   lukem 			while ((p = strsep(&optarg, ",")) != NULL) {
    113  1.1   lukem 				if (*p == '\0')
    114  1.1   lukem 					errx(1, "Empty `-o' option");
    115  1.1   lukem 				if (! params->machine->parseopt(params, p))
    116  1.1   lukem 					exit(1);
    117  1.1   lukem 			}
    118  1.1   lukem 			break;
    119  1.1   lukem 
    120  1.1   lukem 		case 't':
    121  1.1   lukem 			params->fstype = optarg;	// XXX: validate?
    122  1.1   lukem 			break;
    123  1.1   lukem 
    124  1.1   lukem 		case 'v':
    125  1.1   lukem 			params->flags |= IB_VERBOSE;
    126  1.1   lukem 			break;
    127  1.1   lukem 
    128  1.1   lukem 		case '?':
    129  1.1   lukem 		default:
    130  1.1   lukem 			usage();
    131  1.1   lukem 			/* NOTREACHED */
    132  1.1   lukem 
    133  1.1   lukem 		}
    134  1.1   lukem 	}
    135  1.1   lukem 	argc -= optind;
    136  1.1   lukem 	argv += optind;
    137  1.1   lukem 
    138  1.1   lukem 	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
    139  1.1   lukem 	    ((params->flags & IB_CLEAR) == 0 && argc != 2))
    140  1.1   lukem 		usage();
    141  1.1   lukem 
    142  1.1   lukem 		/* set missing defaults */
    143  1.1   lukem 	if (params->machine == NULL) {
    144  1.1   lukem 		if (uname(&utsname) == -1)
    145  1.1   lukem 			err(1, "Determine uname");
    146  1.1   lukem 		if (! getmachine(params, utsname.machine, "uname()"))
    147  1.1   lukem 			exit(1);
    148  1.1   lukem 	}
    149  1.1   lukem 	// XXX: set default params->fstype
    150  1.1   lukem 
    151  1.1   lukem 	params->filesystem = argv[0];
    152  1.1   lukem 	if (params->flags & IB_NOWRITE) {
    153  1.1   lukem 		op = "only";
    154  1.1   lukem 		mode = O_RDONLY;
    155  1.1   lukem 	} else {
    156  1.1   lukem 		op = "write";
    157  1.1   lukem 		mode = O_RDWR;
    158  1.1   lukem 	}
    159  1.1   lukem 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    160  1.1   lukem 		err(1, "Opening file system `%s' read-%s",
    161  1.1   lukem 		    params->filesystem, op);
    162  1.1   lukem 
    163  1.1   lukem 	if (argc == 2) {
    164  1.1   lukem 		params->bootblock = argv[1];
    165  1.1   lukem 		if ((params->bbfd = open(params->bootblock, O_RDONLY, 0600))
    166  1.1   lukem 		    == -1)
    167  1.1   lukem 			err(1, "Opening boot block `%s'", params->bootblock);
    168  1.1   lukem 	}
    169  1.1   lukem 	assert(params->machine != NULL);
    170  1.1   lukem 
    171  1.1   lukem 	if (params->flags & IB_VERBOSE) {
    172  1.1   lukem 		printf("File system: %s\n", params->filesystem);
    173  1.1   lukem 		printf("Boot block:  %s\n",
    174  1.1   lukem 		    (params->flags & IB_CLEAR) ? "(to be cleared)"
    175  1.1   lukem 		    : params->bootblock);
    176  1.1   lukem 	}
    177  1.1   lukem 
    178  1.1   lukem 	if (params->flags & IB_CLEAR) {
    179  1.1   lukem 		op = "Clear";
    180  1.1   lukem 		rv = params->machine->clearboot(params);
    181  1.1   lukem 	} else {
    182  1.1   lukem 		op = "Set";
    183  1.1   lukem 		rv = params->machine->setboot(params);
    184  1.1   lukem 	}
    185  1.1   lukem 	if (rv == 0)
    186  1.1   lukem 		errx(1, "%s boot block operation failed", op);
    187  1.1   lukem 
    188  1.4   lukem 	if (fsync(params->fsfd) == -1)
    189  1.4   lukem 		err(1, "Synchronising file system `%s'", params->filesystem);
    190  1.1   lukem 	if (close(params->fsfd) == -1)
    191  1.1   lukem 		err(1, "Closing file system `%s'", params->filesystem);
    192  1.1   lukem 	if (argc == 2)
    193  1.1   lukem 		if (close(params->bbfd) == -1)
    194  1.1   lukem 			err(1, "Closing boot block `%s'", params->bootblock);
    195  1.1   lukem 
    196  1.1   lukem 	exit(0);
    197  1.1   lukem 	/* NOTREACHED */
    198  1.1   lukem }
    199  1.1   lukem 
    200  1.1   lukem int
    201  1.1   lukem parseoptionflag(ib_params *params, const char *option, ib_flags wantflags)
    202  1.1   lukem {
    203  1.1   lukem 	struct {
    204  1.1   lukem 		const char	*name;
    205  1.1   lukem 		ib_flags	flag;
    206  1.1   lukem 	} flags[] = {
    207  1.1   lukem 		{ "alphasum",	IB_ALPHASUM },
    208  1.1   lukem 		{ "append",	IB_APPEND },
    209  1.1   lukem 		{ "sunsum",	IB_SUNSUM },
    210  1.1   lukem 		{ NULL,		0 },
    211  1.1   lukem 	};
    212  1.1   lukem 
    213  1.1   lukem 	int	i;
    214  1.1   lukem 
    215  1.1   lukem 	assert(params != NULL);
    216  1.1   lukem 	assert(option != NULL);
    217  1.1   lukem 
    218  1.1   lukem 	for (i = 0; flags[i].name != NULL; i++) {
    219  1.1   lukem 		if ((strcmp(flags[i].name, option) == 0) &&
    220  1.1   lukem 		    (wantflags & flags[i].flag)) {
    221  1.1   lukem 			params->flags |= flags[i].flag;
    222  1.1   lukem 			return (1);
    223  1.1   lukem 		}
    224  1.1   lukem 	}
    225  1.3   lukem 	return (0);
    226  1.3   lukem }
    227  1.3   lukem 
    228  1.3   lukem int
    229  1.3   lukem no_parseopt(ib_params *params, const char *option)
    230  1.3   lukem {
    231  1.3   lukem 
    232  1.3   lukem 		/* all options are unsupported */
    233  1.3   lukem 	warnx("Unsupported -o option `%s'", option);
    234  1.3   lukem 	return (0);
    235  1.3   lukem }
    236  1.3   lukem 
    237  1.3   lukem int
    238  1.3   lukem no_setboot(ib_params *params)
    239  1.3   lukem {
    240  1.3   lukem 
    241  1.3   lukem 		/* boot block installation is not supported */
    242  1.3   lukem 	warnx("%s: boot block installation is not supported",
    243  1.3   lukem 	    params->machine->name);
    244  1.3   lukem 	return (0);
    245  1.3   lukem }
    246  1.3   lukem 
    247  1.3   lukem int
    248  1.3   lukem no_clearboot(ib_params *params)
    249  1.3   lukem {
    250  1.3   lukem 
    251  1.3   lukem 		/* boot block removal is not supported */
    252  1.3   lukem 	warnx("%s: boot block removal is not supported",
    253  1.3   lukem 	    params->machine->name);
    254  1.1   lukem 	return (0);
    255  1.1   lukem }
    256  1.1   lukem 
    257  1.1   lukem 
    258  1.1   lukem static int
    259  1.1   lukem getmachine(ib_params *param, const char *mach, const char *provider)
    260  1.1   lukem {
    261  1.1   lukem 	int	i;
    262  1.1   lukem 
    263  1.1   lukem 	assert(param != NULL);
    264  1.1   lukem 	assert(mach != NULL);
    265  1.1   lukem 
    266  1.1   lukem 	for (i = 0; machines[i].name != NULL; i++) {
    267  1.1   lukem 		if (strcmp(machines[i].name, mach) == 0) {
    268  1.1   lukem 			param->machine = &machines[i];
    269  1.1   lukem 			return (1);
    270  1.1   lukem 		}
    271  1.1   lukem 	}
    272  1.1   lukem 	warnx("Invalid machine `%s' from %s", mach, provider);
    273  1.1   lukem 	warnx("Supported machines are:");
    274  1.1   lukem #define MACHS_PER_LINE	10
    275  1.1   lukem 	for (i = 0; machines[i].name != NULL; i++) {
    276  1.1   lukem 		fputs((i % MACHS_PER_LINE) ? ", " : "\t", stderr);
    277  1.1   lukem 		fputs(machines[i].name, stderr);
    278  1.1   lukem 		if ((i % MACHS_PER_LINE) == (MACHS_PER_LINE - 1))
    279  1.1   lukem 			fputs("\n", stderr);
    280  1.1   lukem 	}
    281  1.1   lukem 	if ((i % MACHS_PER_LINE) != 0)
    282  1.1   lukem 		fputs("\n", stderr);
    283  1.1   lukem 	return (0);
    284  1.1   lukem }
    285  1.1   lukem 
    286  1.1   lukem static void
    287  1.1   lukem usage(void)
    288  1.1   lukem {
    289  1.1   lukem 	const char	*prog;
    290  1.1   lukem 
    291  1.1   lukem 	prog = getprogname();
    292  1.1   lukem 	fprintf(stderr,
    293  1.1   lukem "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
    294  1.1   lukem "\t\t   [-b block] filesystem bootstrap\n"
    295  1.1   lukem "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
    296  1.1   lukem 	    prog, prog);
    297  1.1   lukem 	exit(1);
    298  1.1   lukem }
    299