Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.10
      1  1.10   lukem /*	$NetBSD: installboot.c,v 1.10 2002/05/20 14:38:38 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.10   lukem __RCSID("$NetBSD: installboot.c,v 1.10 2002/05/20 14:38:38 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.6   lukem static	int	getfstype(ib_params *, const char *, const char *);
     60   1.1   lukem static	void	usage(void);
     61   1.1   lukem 
     62   1.1   lukem static	ib_params	installboot_params;
     63   1.1   lukem 
     64   1.1   lukem int
     65   1.1   lukem main(int argc, char *argv[])
     66   1.1   lukem {
     67   1.1   lukem 	struct utsname	utsname;
     68   1.1   lukem 	ib_params	*params;
     69   1.6   lukem 	unsigned long	lval;
     70   1.1   lukem 	int		ch, rv, mode;
     71   1.1   lukem 	char 		*p;
     72   1.1   lukem 	const char	*op;
     73   1.1   lukem 
     74   1.1   lukem 	setprogname(argv[0]);
     75   1.1   lukem 	params = &installboot_params;
     76   1.1   lukem 	memset(params, 0, sizeof(*params));
     77   1.8   lukem 	params->fsfd = -1;
     78   1.8   lukem 	params->s1fd = -1;
     79   1.1   lukem 	if ((p = getenv("MACHINE")) != NULL)
     80   1.1   lukem 		if (! getmachine(params, p, "$MACHINE"))
     81   1.1   lukem 			exit(1);
     82   1.1   lukem 
     83   1.8   lukem 	while ((ch = getopt(argc, argv, "b:B:cm:no:t:v")) != -1) {
     84   1.1   lukem 		switch (ch) {
     85   1.1   lukem 
     86   1.1   lukem 		case 'b':
     87   1.8   lukem 		case 'B':
     88   1.1   lukem 			if (*optarg == '\0')
     89   1.1   lukem 				goto badblock;
     90   1.6   lukem 			lval = strtoul(optarg, &p, 0);
     91   1.6   lukem 			if (lval > UINT32_MAX || *p != '\0') {
     92   1.1   lukem  badblock:
     93   1.1   lukem 				errx(1, "Invalid block number `%s'", optarg);
     94   1.1   lukem 			}
     95   1.8   lukem 			if (ch == 'b') {
     96   1.8   lukem 				params->s1start = (uint32_t)lval;
     97   1.8   lukem 				params->flags |= IB_STAGE1START;
     98   1.8   lukem 			} else {
     99   1.8   lukem 				params->s2start = (uint32_t)lval;
    100   1.8   lukem 				params->flags |= IB_STAGE2START;
    101   1.8   lukem 			}
    102   1.1   lukem 			break;
    103   1.1   lukem 
    104   1.1   lukem 		case 'c':
    105   1.1   lukem 			params->flags |= IB_CLEAR;
    106   1.1   lukem 			break;
    107   1.1   lukem 
    108   1.1   lukem 		case 'm':
    109   1.1   lukem 			if (! getmachine(params, optarg, "-m"))
    110   1.1   lukem 				exit(1);
    111   1.1   lukem 			break;
    112   1.1   lukem 
    113   1.1   lukem 		case 'n':
    114   1.1   lukem 			params->flags |= IB_NOWRITE;
    115   1.1   lukem 			break;
    116   1.1   lukem 
    117   1.1   lukem 		case 'o':
    118   1.1   lukem 			if (params->machine == NULL)
    119   1.1   lukem 				errx(1,
    120   1.1   lukem 				    "Machine needs to be specified before -o");
    121   1.1   lukem 			while ((p = strsep(&optarg, ",")) != NULL) {
    122   1.1   lukem 				if (*p == '\0')
    123   1.1   lukem 					errx(1, "Empty `-o' option");
    124   1.1   lukem 				if (! params->machine->parseopt(params, p))
    125   1.1   lukem 					exit(1);
    126   1.1   lukem 			}
    127   1.1   lukem 			break;
    128   1.1   lukem 
    129   1.1   lukem 		case 't':
    130   1.6   lukem 			if (! getfstype(params, optarg, "-t"))
    131   1.6   lukem 				exit(1);
    132   1.1   lukem 			break;
    133   1.1   lukem 
    134   1.1   lukem 		case 'v':
    135   1.1   lukem 			params->flags |= IB_VERBOSE;
    136   1.1   lukem 			break;
    137   1.1   lukem 
    138   1.1   lukem 		case '?':
    139   1.1   lukem 		default:
    140   1.1   lukem 			usage();
    141   1.1   lukem 			/* NOTREACHED */
    142   1.1   lukem 
    143   1.1   lukem 		}
    144   1.1   lukem 	}
    145   1.1   lukem 	argc -= optind;
    146   1.1   lukem 	argv += optind;
    147   1.1   lukem 
    148   1.1   lukem 	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
    149   1.5   lukem 	    ((params->flags & IB_CLEAR) == 0 && (argc < 2 || argc > 3)))
    150   1.1   lukem 		usage();
    151   1.1   lukem 
    152   1.1   lukem 		/* set missing defaults */
    153   1.1   lukem 	if (params->machine == NULL) {
    154   1.1   lukem 		if (uname(&utsname) == -1)
    155   1.1   lukem 			err(1, "Determine uname");
    156   1.1   lukem 		if (! getmachine(params, utsname.machine, "uname()"))
    157   1.1   lukem 			exit(1);
    158   1.1   lukem 	}
    159   1.1   lukem 
    160   1.1   lukem 	params->filesystem = argv[0];
    161   1.1   lukem 	if (params->flags & IB_NOWRITE) {
    162   1.1   lukem 		op = "only";
    163   1.1   lukem 		mode = O_RDONLY;
    164   1.1   lukem 	} else {
    165   1.1   lukem 		op = "write";
    166   1.1   lukem 		mode = O_RDWR;
    167   1.1   lukem 	}
    168   1.1   lukem 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    169   1.1   lukem 		err(1, "Opening file system `%s' read-%s",
    170   1.1   lukem 		    params->filesystem, op);
    171   1.9   lukem 	if (fstat(params->fsfd, &params->fsstat) == -1)
    172   1.9   lukem 		err(1, "Examining file system `%s'", params->filesystem);
    173   1.6   lukem 	if (params->fstype != NULL) {
    174   1.6   lukem 		if (! params->fstype->match(params))
    175   1.6   lukem 			err(1, "File system `%s' is not of type %s",
    176   1.6   lukem 			    params->filesystem, params->fstype->name);
    177   1.6   lukem 	} else {
    178   1.6   lukem 		params->fstype = &fstypes[0];
    179   1.6   lukem 		while (params->fstype->name != NULL &&
    180   1.6   lukem 			! params->fstype->match(params))
    181   1.6   lukem 			params->fstype++;
    182   1.6   lukem 		if (params->fstype->name == NULL)
    183   1.7   lukem 			errx(1, "File system `%s' is of an unknown type",
    184   1.6   lukem 			    params->filesystem);
    185   1.6   lukem 	}
    186   1.1   lukem 
    187   1.5   lukem 	if (argc >= 2) {
    188   1.5   lukem 		params->stage1 = argv[1];
    189   1.5   lukem 		if ((params->s1fd = open(params->stage1, O_RDONLY, 0600))
    190   1.1   lukem 		    == -1)
    191   1.5   lukem 			err(1, "Opening primary bootstrap `%s'",
    192   1.5   lukem 			    params->stage1);
    193   1.9   lukem 		if (fstat(params->s1fd, &params->s1stat) == -1)
    194   1.9   lukem 			err(1, "Examining primary bootstrap `%s'",
    195   1.9   lukem 			    params->stage1);
    196   1.9   lukem 		if (!S_ISREG(params->s1stat.st_mode))
    197   1.9   lukem 			err(1, "`%s' must be a regular file", params->stage1);
    198   1.5   lukem 	}
    199   1.5   lukem 	if (argc == 3) {
    200   1.5   lukem 		params->stage2 = argv[2];
    201   1.1   lukem 	}
    202   1.1   lukem 	assert(params->machine != NULL);
    203   1.1   lukem 
    204   1.1   lukem 	if (params->flags & IB_VERBOSE) {
    205   1.5   lukem 		printf("File system:         %s\n", params->filesystem);
    206   1.8   lukem 		printf("File system type:    %s (blocksize %u, needswap %d)\n",
    207   1.8   lukem 		    params->fstype->name,
    208   1.8   lukem 		    params->fstype->blocksize, params->fstype->needswap);
    209   1.5   lukem 		printf("Primary bootstrap:   %s\n",
    210   1.1   lukem 		    (params->flags & IB_CLEAR) ? "(to be cleared)"
    211   1.5   lukem 		    : params->stage1);
    212   1.5   lukem 		if (params->stage2 != NULL)
    213   1.5   lukem 			printf("Secondary bootstrap: %s\n", params->stage2);
    214   1.1   lukem 	}
    215   1.1   lukem 
    216   1.1   lukem 	if (params->flags & IB_CLEAR) {
    217   1.1   lukem 		op = "Clear";
    218   1.1   lukem 		rv = params->machine->clearboot(params);
    219   1.1   lukem 	} else {
    220   1.1   lukem 		op = "Set";
    221   1.1   lukem 		rv = params->machine->setboot(params);
    222   1.1   lukem 	}
    223   1.1   lukem 	if (rv == 0)
    224   1.5   lukem 		errx(1, "%s bootstrap operation failed", op);
    225   1.1   lukem 
    226   1.9   lukem 	if (S_ISREG(params->fsstat.st_mode)) {
    227   1.9   lukem 		if (fsync(params->fsfd) == -1)
    228   1.9   lukem 			err(1, "Synchronising file system `%s'",
    229   1.9   lukem 			    params->filesystem);
    230   1.9   lukem 	} else {
    231   1.9   lukem 		/* Sync filesystems (to clean in-memory superblock?) */
    232   1.9   lukem 		sync();
    233   1.9   lukem 	}
    234   1.1   lukem 	if (close(params->fsfd) == -1)
    235   1.1   lukem 		err(1, "Closing file system `%s'", params->filesystem);
    236   1.1   lukem 	if (argc == 2)
    237   1.5   lukem 		if (close(params->s1fd) == -1)
    238   1.5   lukem 			err(1, "Closing primary bootstrap `%s'",
    239   1.5   lukem 			    params->stage1);
    240   1.1   lukem 
    241   1.1   lukem 	exit(0);
    242   1.1   lukem 	/* NOTREACHED */
    243   1.1   lukem }
    244   1.1   lukem 
    245   1.1   lukem int
    246   1.1   lukem parseoptionflag(ib_params *params, const char *option, ib_flags wantflags)
    247   1.1   lukem {
    248   1.1   lukem 	struct {
    249   1.1   lukem 		const char	*name;
    250   1.1   lukem 		ib_flags	flag;
    251   1.1   lukem 	} flags[] = {
    252   1.1   lukem 		{ "alphasum",	IB_ALPHASUM },
    253   1.1   lukem 		{ "append",	IB_APPEND },
    254   1.1   lukem 		{ "sunsum",	IB_SUNSUM },
    255   1.1   lukem 		{ NULL,		0 },
    256   1.1   lukem 	};
    257   1.1   lukem 
    258   1.1   lukem 	int	i;
    259   1.1   lukem 
    260   1.1   lukem 	assert(params != NULL);
    261   1.1   lukem 	assert(option != NULL);
    262   1.1   lukem 
    263   1.1   lukem 	for (i = 0; flags[i].name != NULL; i++) {
    264   1.1   lukem 		if ((strcmp(flags[i].name, option) == 0) &&
    265   1.1   lukem 		    (wantflags & flags[i].flag)) {
    266   1.1   lukem 			params->flags |= flags[i].flag;
    267   1.1   lukem 			return (1);
    268   1.1   lukem 		}
    269   1.1   lukem 	}
    270   1.3   lukem 	return (0);
    271   1.3   lukem }
    272   1.3   lukem 
    273   1.3   lukem int
    274   1.3   lukem no_parseopt(ib_params *params, const char *option)
    275   1.3   lukem {
    276   1.3   lukem 
    277   1.7   lukem 	assert(params != NULL);
    278   1.7   lukem 	assert(option != NULL);
    279   1.7   lukem 
    280   1.3   lukem 		/* all options are unsupported */
    281   1.3   lukem 	warnx("Unsupported -o option `%s'", option);
    282   1.3   lukem 	return (0);
    283   1.3   lukem }
    284   1.3   lukem 
    285   1.3   lukem int
    286   1.3   lukem no_setboot(ib_params *params)
    287   1.3   lukem {
    288   1.3   lukem 
    289   1.7   lukem 	assert(params != NULL);
    290   1.7   lukem 
    291   1.5   lukem 		/* bootstrap installation is not supported */
    292   1.5   lukem 	warnx("%s: bootstrap installation is not supported",
    293   1.3   lukem 	    params->machine->name);
    294   1.3   lukem 	return (0);
    295   1.3   lukem }
    296   1.3   lukem 
    297   1.3   lukem int
    298   1.3   lukem no_clearboot(ib_params *params)
    299   1.3   lukem {
    300   1.3   lukem 
    301   1.7   lukem 	assert(params != NULL);
    302   1.7   lukem 
    303   1.5   lukem 		/* bootstrap removal is not supported */
    304   1.5   lukem 	warnx("%s: bootstrap removal is not supported",
    305   1.3   lukem 	    params->machine->name);
    306   1.1   lukem 	return (0);
    307   1.1   lukem }
    308   1.1   lukem 
    309   1.1   lukem 
    310   1.1   lukem static int
    311   1.1   lukem getmachine(ib_params *param, const char *mach, const char *provider)
    312   1.1   lukem {
    313  1.10   lukem 	const char *prefix;
    314   1.1   lukem 	int	i;
    315   1.1   lukem 
    316  1.10   lukem 	if (param != NULL && mach != NULL && provider != NULL) {
    317  1.10   lukem 		for (i = 0; machines[i].name != NULL; i++) {
    318  1.10   lukem 			if (strcmp(machines[i].name, mach) == 0) {
    319  1.10   lukem 				param->machine = &machines[i];
    320  1.10   lukem 				return (1);
    321  1.10   lukem 			}
    322   1.1   lukem 		}
    323  1.10   lukem 		warnx("Invalid machine `%s' from %s", mach, provider);
    324   1.1   lukem 	}
    325   1.1   lukem 	warnx("Supported machines are:");
    326  1.10   lukem #define MACHS_PER_LINE	9
    327  1.10   lukem 	prefix="";
    328   1.1   lukem 	for (i = 0; machines[i].name != NULL; i++) {
    329  1.10   lukem 		if (i == 0)
    330  1.10   lukem 			prefix="\t";
    331  1.10   lukem 		else if (i % MACHS_PER_LINE)
    332  1.10   lukem 			prefix=", ";
    333  1.10   lukem 		else
    334  1.10   lukem 			prefix=",\n\t";
    335  1.10   lukem 		fprintf(stderr, "%s%s", prefix, machines[i].name);
    336   1.1   lukem 	}
    337  1.10   lukem 	fputs("\n", stderr);
    338   1.6   lukem 	return (0);
    339   1.6   lukem }
    340   1.6   lukem 
    341   1.6   lukem static int
    342   1.6   lukem getfstype(ib_params *param, const char *fstype, const char *provider)
    343   1.6   lukem {
    344  1.10   lukem 	const char *prefix;
    345   1.6   lukem 	int	i;
    346   1.6   lukem 
    347  1.10   lukem 	if (param != NULL && fstype != NULL && provider != NULL) {
    348  1.10   lukem 		for (i = 0; fstypes[i].name != NULL; i++) {
    349  1.10   lukem 			if (strcmp(fstypes[i].name, fstype) == 0) {
    350  1.10   lukem 				param->fstype = &fstypes[i];
    351  1.10   lukem 				return (1);
    352  1.10   lukem 			}
    353   1.6   lukem 		}
    354  1.10   lukem 		warnx("Invalid file system type `%s' from %s",
    355  1.10   lukem 		    fstype, provider);
    356   1.6   lukem 	}
    357   1.6   lukem 	warnx("Supported file system types are:");
    358  1.10   lukem #define FSTYPES_PER_LINE	9
    359  1.10   lukem 	prefix="";
    360   1.6   lukem 	for (i = 0; fstypes[i].name != NULL; i++) {
    361  1.10   lukem 		if (i == 0)
    362  1.10   lukem 			prefix="\t";
    363  1.10   lukem 		else if (i % FSTYPES_PER_LINE)
    364  1.10   lukem 			prefix=", ";
    365  1.10   lukem 		else
    366  1.10   lukem 			prefix=",\n\t";
    367  1.10   lukem 		fprintf(stderr, "%s%s", prefix, fstypes[i].name);
    368   1.6   lukem 	}
    369  1.10   lukem 	fputs("\n", stderr);
    370   1.1   lukem 	return (0);
    371   1.1   lukem }
    372   1.1   lukem 
    373   1.1   lukem static void
    374   1.1   lukem usage(void)
    375   1.1   lukem {
    376   1.1   lukem 	const char	*prog;
    377   1.1   lukem 
    378   1.1   lukem 	prog = getprogname();
    379   1.1   lukem 	fprintf(stderr,
    380   1.1   lukem "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
    381   1.8   lukem "\t\t   [-b s1start] [-B s2start] filesystem primary [secondary]\n"
    382   1.1   lukem "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
    383   1.1   lukem 	    prog, prog);
    384  1.10   lukem 	getmachine(NULL, NULL, NULL);
    385  1.10   lukem 	getfstype(NULL, NULL, NULL);
    386   1.1   lukem 	exit(1);
    387   1.1   lukem }
    388