Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.15
      1  1.15     dsl /*	$NetBSD: installboot.c,v 1.15 2004/03/13 22:51:50 dsl 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.15     dsl __RCSID("$NetBSD: installboot.c,v 1.15 2004/03/13 22:51:50 dsl 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.11     dsl #include <stddef.h>
     53   1.1   lukem #include <string.h>
     54   1.1   lukem #include <unistd.h>
     55   1.1   lukem 
     56   1.1   lukem #include "installboot.h"
     57   1.1   lukem 
     58   1.1   lukem int		main(int, char *[]);
     59  1.11     dsl static	void	getmachine(ib_params *, const char *, const char *);
     60  1.11     dsl static	void	getfstype(ib_params *, const char *, const char *);
     61  1.11     dsl static	void	parseoptions(ib_params *, const char *);
     62   1.1   lukem static	void	usage(void);
     63  1.11     dsl static	void	options_usage(void);
     64  1.11     dsl static	void	machine_usage(void);
     65  1.11     dsl static	void	fstype_usage(void);
     66   1.1   lukem 
     67   1.1   lukem static	ib_params	installboot_params;
     68   1.1   lukem 
     69  1.11     dsl #define OFFSET(field)    offsetof(ib_params, field)
     70  1.11     dsl const struct option {
     71  1.11     dsl 	const char	*name;		/* Name of option */
     72  1.11     dsl 	ib_flags	flag;		/* Corresponding IB_xxx flag */
     73  1.11     dsl 	enum {				/* Type of option value... */
     74  1.11     dsl 		OPT_BOOL,		/* no value */
     75  1.11     dsl 		OPT_INT,		/* numeric value */
     76  1.11     dsl 		OPT_WORD,		/* space/tab/, terminated */
     77  1.11     dsl 		OPT_STRING		/* null terminated */
     78  1.11     dsl 	}		type;
     79  1.11     dsl 	int		offset;		/* of field in ib_params */
     80  1.11     dsl } options[] = {
     81  1.11     dsl 	{ "alphasum",	IB_ALPHASUM,	OPT_BOOL },
     82  1.11     dsl 	{ "append",	IB_APPEND,	OPT_BOOL },
     83  1.11     dsl 	{ "command",	IB_COMMAND,	OPT_STRING,	OFFSET(command) },
     84  1.11     dsl 	{ "console",	IB_CONSOLE,	OPT_WORD,	OFFSET(console) },
     85  1.15     dsl 	{ "keymap",	IB_KEYMAP,	OPT_WORD,	OFFSET(keymap) },
     86  1.11     dsl 	{ "password",	IB_PASSWORD,	OPT_WORD,	OFFSET(password) },
     87  1.11     dsl 	{ "resetvideo",	IB_RESETVIDEO,	OPT_BOOL },
     88  1.11     dsl 	{ "speed",	IB_CONSPEED,	OPT_INT,	OFFSET(conspeed) },
     89  1.11     dsl 	{ "sunsum",	IB_SUNSUM,	OPT_BOOL },
     90  1.11     dsl 	{ "timeout",	IB_TIMEOUT,	OPT_INT,	OFFSET(timeout) },
     91  1.11     dsl 	{ NULL },
     92  1.11     dsl };
     93  1.11     dsl #undef OFFSET
     94  1.11     dsl #define OPTION(params, type, opt) (*(type *)((char *)(params) + (opt)->offset))
     95  1.11     dsl 
     96   1.1   lukem int
     97   1.1   lukem main(int argc, char *argv[])
     98   1.1   lukem {
     99   1.1   lukem 	struct utsname	utsname;
    100   1.1   lukem 	ib_params	*params;
    101   1.6   lukem 	unsigned long	lval;
    102   1.1   lukem 	int		ch, rv, mode;
    103   1.1   lukem 	char 		*p;
    104   1.1   lukem 	const char	*op;
    105  1.11     dsl 	ib_flags	unsupported_flags;
    106   1.1   lukem 
    107   1.1   lukem 	setprogname(argv[0]);
    108   1.1   lukem 	params = &installboot_params;
    109   1.1   lukem 	memset(params, 0, sizeof(*params));
    110   1.8   lukem 	params->fsfd = -1;
    111   1.8   lukem 	params->s1fd = -1;
    112   1.1   lukem 	if ((p = getenv("MACHINE")) != NULL)
    113  1.11     dsl 		getmachine(params, p, "$MACHINE");
    114   1.1   lukem 
    115   1.8   lukem 	while ((ch = getopt(argc, argv, "b:B:cm:no:t:v")) != -1) {
    116   1.1   lukem 		switch (ch) {
    117   1.1   lukem 
    118   1.1   lukem 		case 'b':
    119   1.8   lukem 		case 'B':
    120   1.1   lukem 			if (*optarg == '\0')
    121   1.1   lukem 				goto badblock;
    122   1.6   lukem 			lval = strtoul(optarg, &p, 0);
    123   1.6   lukem 			if (lval > UINT32_MAX || *p != '\0') {
    124   1.1   lukem  badblock:
    125   1.1   lukem 				errx(1, "Invalid block number `%s'", optarg);
    126   1.1   lukem 			}
    127   1.8   lukem 			if (ch == 'b') {
    128   1.8   lukem 				params->s1start = (uint32_t)lval;
    129   1.8   lukem 				params->flags |= IB_STAGE1START;
    130   1.8   lukem 			} else {
    131   1.8   lukem 				params->s2start = (uint32_t)lval;
    132   1.8   lukem 				params->flags |= IB_STAGE2START;
    133   1.8   lukem 			}
    134   1.1   lukem 			break;
    135   1.1   lukem 
    136   1.1   lukem 		case 'c':
    137   1.1   lukem 			params->flags |= IB_CLEAR;
    138   1.1   lukem 			break;
    139   1.1   lukem 
    140   1.1   lukem 		case 'm':
    141  1.11     dsl 			getmachine(params, optarg, "-m");
    142   1.1   lukem 			break;
    143   1.1   lukem 
    144   1.1   lukem 		case 'n':
    145   1.1   lukem 			params->flags |= IB_NOWRITE;
    146   1.1   lukem 			break;
    147   1.1   lukem 
    148   1.1   lukem 		case 'o':
    149  1.11     dsl 			parseoptions(params, optarg);
    150   1.1   lukem 			break;
    151   1.1   lukem 
    152   1.1   lukem 		case 't':
    153  1.11     dsl 			getfstype(params, optarg, "-t");
    154   1.1   lukem 			break;
    155   1.1   lukem 
    156   1.1   lukem 		case 'v':
    157   1.1   lukem 			params->flags |= IB_VERBOSE;
    158   1.1   lukem 			break;
    159   1.1   lukem 
    160   1.1   lukem 		case '?':
    161   1.1   lukem 		default:
    162   1.1   lukem 			usage();
    163   1.1   lukem 			/* NOTREACHED */
    164   1.1   lukem 
    165   1.1   lukem 		}
    166   1.1   lukem 	}
    167   1.1   lukem 	argc -= optind;
    168   1.1   lukem 	argv += optind;
    169   1.1   lukem 
    170   1.1   lukem 	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
    171   1.5   lukem 	    ((params->flags & IB_CLEAR) == 0 && (argc < 2 || argc > 3)))
    172   1.1   lukem 		usage();
    173   1.1   lukem 
    174   1.1   lukem 		/* set missing defaults */
    175   1.1   lukem 	if (params->machine == NULL) {
    176   1.1   lukem 		if (uname(&utsname) == -1)
    177   1.1   lukem 			err(1, "Determine uname");
    178  1.11     dsl 		getmachine(params, utsname.machine, "uname()");
    179  1.11     dsl 	}
    180  1.11     dsl 
    181  1.11     dsl 	/* Check that options are supported by this system */
    182  1.11     dsl 	unsupported_flags = params->flags & ~params->machine->valid_flags;
    183  1.11     dsl 	unsupported_flags &= ~(IB_VERBOSE | IB_NOWRITE |IB_CLEAR);
    184  1.11     dsl 	if (unsupported_flags != 0) {
    185  1.11     dsl 		int ndx;
    186  1.11     dsl 		for (ndx = 0; options[ndx].name != NULL; ndx++) {
    187  1.11     dsl 			if (unsupported_flags & options[ndx].flag)
    188  1.11     dsl 				warnx("`-o %s' is not supported for %s",
    189  1.11     dsl 				    options[ndx].name, params->machine->name);
    190  1.11     dsl 		}
    191  1.11     dsl 		if (unsupported_flags & IB_STAGE1START)
    192  1.11     dsl 			warnx("`-b bno' is not supported for %s",
    193  1.11     dsl 			    params->machine->name);
    194  1.11     dsl 		if (unsupported_flags & IB_STAGE2START)
    195  1.11     dsl 			warnx("`-B bno' is not supported for %s",
    196  1.11     dsl 			    params->machine->name);
    197  1.11     dsl 		exit(1);
    198  1.11     dsl 	}
    199  1.11     dsl 	/* and some illegal combinations */
    200  1.11     dsl 	if (params->flags & IB_STAGE1START && params->flags & IB_APPEND) {
    201  1.11     dsl 		warnx("Can't use `-b bno' with `-o append'");
    202  1.11     dsl 		exit(1);
    203  1.11     dsl 	}
    204  1.11     dsl 	if (params->flags & IB_CLEAR &&
    205  1.11     dsl 	    params->flags & (IB_STAGE1START | IB_STAGE2START | IB_APPEND)) {
    206  1.11     dsl 		warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
    207  1.11     dsl 		exit(1);
    208   1.1   lukem 	}
    209   1.1   lukem 
    210   1.1   lukem 	params->filesystem = argv[0];
    211   1.1   lukem 	if (params->flags & IB_NOWRITE) {
    212   1.1   lukem 		op = "only";
    213   1.1   lukem 		mode = O_RDONLY;
    214   1.1   lukem 	} else {
    215   1.1   lukem 		op = "write";
    216   1.1   lukem 		mode = O_RDWR;
    217   1.1   lukem 	}
    218   1.1   lukem 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    219   1.1   lukem 		err(1, "Opening file system `%s' read-%s",
    220   1.1   lukem 		    params->filesystem, op);
    221   1.9   lukem 	if (fstat(params->fsfd, &params->fsstat) == -1)
    222   1.9   lukem 		err(1, "Examining file system `%s'", params->filesystem);
    223   1.6   lukem 	if (params->fstype != NULL) {
    224   1.6   lukem 		if (! params->fstype->match(params))
    225   1.6   lukem 			err(1, "File system `%s' is not of type %s",
    226   1.6   lukem 			    params->filesystem, params->fstype->name);
    227   1.6   lukem 	} else {
    228   1.6   lukem 		params->fstype = &fstypes[0];
    229   1.6   lukem 		while (params->fstype->name != NULL &&
    230   1.6   lukem 			! params->fstype->match(params))
    231   1.6   lukem 			params->fstype++;
    232   1.6   lukem 		if (params->fstype->name == NULL)
    233   1.7   lukem 			errx(1, "File system `%s' is of an unknown type",
    234   1.6   lukem 			    params->filesystem);
    235   1.6   lukem 	}
    236   1.1   lukem 
    237   1.5   lukem 	if (argc >= 2) {
    238   1.5   lukem 		params->stage1 = argv[1];
    239   1.5   lukem 		if ((params->s1fd = open(params->stage1, O_RDONLY, 0600))
    240   1.1   lukem 		    == -1)
    241   1.5   lukem 			err(1, "Opening primary bootstrap `%s'",
    242   1.5   lukem 			    params->stage1);
    243   1.9   lukem 		if (fstat(params->s1fd, &params->s1stat) == -1)
    244   1.9   lukem 			err(1, "Examining primary bootstrap `%s'",
    245   1.9   lukem 			    params->stage1);
    246   1.9   lukem 		if (!S_ISREG(params->s1stat.st_mode))
    247   1.9   lukem 			err(1, "`%s' must be a regular file", params->stage1);
    248   1.5   lukem 	}
    249   1.5   lukem 	if (argc == 3) {
    250   1.5   lukem 		params->stage2 = argv[2];
    251   1.1   lukem 	}
    252   1.1   lukem 	assert(params->machine != NULL);
    253   1.1   lukem 
    254   1.1   lukem 	if (params->flags & IB_VERBOSE) {
    255   1.5   lukem 		printf("File system:         %s\n", params->filesystem);
    256   1.8   lukem 		printf("File system type:    %s (blocksize %u, needswap %d)\n",
    257   1.8   lukem 		    params->fstype->name,
    258   1.8   lukem 		    params->fstype->blocksize, params->fstype->needswap);
    259   1.5   lukem 		printf("Primary bootstrap:   %s\n",
    260   1.1   lukem 		    (params->flags & IB_CLEAR) ? "(to be cleared)"
    261   1.5   lukem 		    : params->stage1);
    262   1.5   lukem 		if (params->stage2 != NULL)
    263   1.5   lukem 			printf("Secondary bootstrap: %s\n", params->stage2);
    264   1.1   lukem 	}
    265   1.1   lukem 
    266   1.1   lukem 	if (params->flags & IB_CLEAR) {
    267   1.1   lukem 		op = "Clear";
    268   1.1   lukem 		rv = params->machine->clearboot(params);
    269   1.1   lukem 	} else {
    270   1.1   lukem 		op = "Set";
    271   1.1   lukem 		rv = params->machine->setboot(params);
    272   1.1   lukem 	}
    273   1.1   lukem 	if (rv == 0)
    274   1.5   lukem 		errx(1, "%s bootstrap operation failed", op);
    275   1.1   lukem 
    276   1.9   lukem 	if (S_ISREG(params->fsstat.st_mode)) {
    277   1.9   lukem 		if (fsync(params->fsfd) == -1)
    278   1.9   lukem 			err(1, "Synchronising file system `%s'",
    279   1.9   lukem 			    params->filesystem);
    280   1.9   lukem 	} else {
    281   1.9   lukem 		/* Sync filesystems (to clean in-memory superblock?) */
    282   1.9   lukem 		sync();
    283   1.9   lukem 	}
    284   1.1   lukem 	if (close(params->fsfd) == -1)
    285   1.1   lukem 		err(1, "Closing file system `%s'", params->filesystem);
    286   1.1   lukem 	if (argc == 2)
    287   1.5   lukem 		if (close(params->s1fd) == -1)
    288   1.5   lukem 			err(1, "Closing primary bootstrap `%s'",
    289   1.5   lukem 			    params->stage1);
    290   1.1   lukem 
    291   1.1   lukem 	exit(0);
    292   1.1   lukem 	/* NOTREACHED */
    293   1.1   lukem }
    294   1.1   lukem 
    295  1.11     dsl static void
    296  1.11     dsl parseoptions(ib_params *params, const char *option)
    297   1.1   lukem {
    298  1.11     dsl 	char *cp;
    299  1.11     dsl 	const struct option *opt;
    300  1.11     dsl 	int len;
    301  1.12     dsl 	unsigned long val;
    302   1.1   lukem 
    303   1.1   lukem 	assert(params != NULL);
    304   1.1   lukem 	assert(option != NULL);
    305   1.1   lukem 
    306  1.11     dsl 	for (;; option += len) {
    307  1.11     dsl 		option += strspn(option, ", \t");
    308  1.11     dsl 		if (*option == 0)
    309  1.11     dsl 			return;
    310  1.11     dsl 		len = strcspn(option, "=,");
    311  1.11     dsl 		for (opt = options; opt->name != NULL; opt++) {
    312  1.11     dsl 			if (memcmp(option, opt->name, len) == 0
    313  1.11     dsl 			    && opt->name[len] == 0)
    314  1.11     dsl 				break;
    315  1.11     dsl 		}
    316  1.11     dsl 		if (opt->name == NULL) {
    317  1.11     dsl 			len = strcspn(option, ",");
    318  1.11     dsl 			warnx("Unknown option `-o %.*s'", len, option);
    319  1.11     dsl 			break;
    320  1.11     dsl 		}
    321  1.11     dsl 		params->flags |= opt->flag;
    322  1.11     dsl 		if (opt->type == OPT_BOOL) {
    323  1.11     dsl 			if (option[len] != '=')
    324  1.11     dsl 				continue;
    325  1.11     dsl 			warnx("Option `%s' must not have a value", opt->name);
    326  1.11     dsl 			break;
    327  1.11     dsl 		}
    328  1.11     dsl 		if (option[len] != '=') {
    329  1.11     dsl 			warnx("Option `%s' must have a value", opt->name);
    330  1.11     dsl 			break;
    331   1.1   lukem 		}
    332  1.11     dsl 		option += len + 1;
    333  1.11     dsl 		len = strcspn(option, ",");
    334  1.11     dsl 		switch (opt->type) {
    335  1.11     dsl 		case OPT_STRING:
    336  1.11     dsl 			len = strlen(option);
    337  1.11     dsl 			/* FALLTHROUGH */
    338  1.11     dsl 		case OPT_WORD:
    339  1.11     dsl 			cp = strdup(option);
    340  1.11     dsl 			if (cp == NULL)
    341  1.11     dsl 				err(1, "strdup");
    342  1.11     dsl 			cp[len] = 0;
    343  1.11     dsl 			OPTION(params, char *, opt) = cp;
    344  1.11     dsl 			continue;
    345  1.11     dsl 		case OPT_INT:
    346  1.11     dsl 			val = strtoul(option, &cp, 0);
    347  1.11     dsl 			if (cp > option + len || (*cp != 0 && *cp != ','))
    348  1.11     dsl 				break;
    349  1.11     dsl 			OPTION(params, int, opt) = val;
    350  1.11     dsl 			if (OPTION(params, int, opt) != val)
    351  1.11     dsl 				/* value got truncated on int convertion */
    352  1.11     dsl 				break;
    353  1.11     dsl 			continue;
    354  1.11     dsl 		default:
    355  1.13  petrov 			errx(1, "Internal error: option `%s' has invalid type %d",
    356  1.11     dsl 				opt->name, opt->type);
    357  1.11     dsl 		}
    358  1.11     dsl 		warnx("Invalid option value `%s=%.*s'", opt->name, len, option);
    359  1.11     dsl 		break;
    360   1.1   lukem 	}
    361  1.11     dsl 	options_usage();
    362  1.11     dsl 	exit(1);
    363   1.3   lukem }
    364   1.3   lukem 
    365  1.11     dsl static void
    366  1.11     dsl options_usage(void)
    367   1.3   lukem {
    368  1.11     dsl 	int ndx;
    369  1.11     dsl 	const char *pfx;
    370   1.3   lukem 
    371  1.11     dsl 	warnx("Valid options are:");
    372  1.11     dsl 	pfx = "\t";
    373  1.11     dsl 	for (ndx = 0; options[ndx].name != 0; ndx++) {
    374  1.11     dsl 		fprintf(stderr, "%s%s", pfx, options[ndx].name);
    375  1.11     dsl 		switch (options[ndx].type) {
    376  1.11     dsl 		case OPT_INT:
    377  1.11     dsl 			fprintf(stderr, "=number");
    378  1.11     dsl 			break;
    379  1.11     dsl 		case OPT_WORD:
    380  1.11     dsl 			fprintf(stderr, "=word");
    381  1.11     dsl 			break;
    382  1.11     dsl 		case OPT_STRING:
    383  1.11     dsl 			fprintf(stderr, "=string");
    384  1.11     dsl 			break;
    385  1.11     dsl 		default:
    386  1.11     dsl 			break;
    387  1.11     dsl 		}
    388  1.11     dsl 		if ((ndx % 5) == 4)
    389  1.11     dsl 			pfx = ",\n\t";
    390  1.11     dsl 		else
    391  1.11     dsl 			pfx = ", ";
    392  1.11     dsl 	}
    393  1.11     dsl 	fprintf(stderr, "\n");
    394   1.3   lukem }
    395   1.3   lukem 
    396   1.3   lukem int
    397   1.3   lukem no_setboot(ib_params *params)
    398   1.3   lukem {
    399   1.3   lukem 
    400   1.7   lukem 	assert(params != NULL);
    401   1.7   lukem 
    402   1.5   lukem 		/* bootstrap installation is not supported */
    403   1.5   lukem 	warnx("%s: bootstrap installation is not supported",
    404   1.3   lukem 	    params->machine->name);
    405   1.3   lukem 	return (0);
    406   1.3   lukem }
    407   1.3   lukem 
    408   1.3   lukem int
    409   1.3   lukem no_clearboot(ib_params *params)
    410   1.3   lukem {
    411   1.3   lukem 
    412   1.7   lukem 	assert(params != NULL);
    413   1.7   lukem 
    414   1.5   lukem 		/* bootstrap removal is not supported */
    415   1.5   lukem 	warnx("%s: bootstrap removal is not supported",
    416   1.3   lukem 	    params->machine->name);
    417   1.1   lukem 	return (0);
    418   1.1   lukem }
    419   1.1   lukem 
    420   1.1   lukem 
    421  1.11     dsl static void
    422   1.1   lukem getmachine(ib_params *param, const char *mach, const char *provider)
    423   1.1   lukem {
    424   1.1   lukem 	int	i;
    425   1.1   lukem 
    426  1.11     dsl 	assert(param != NULL);
    427  1.11     dsl 	assert(mach != NULL);
    428  1.11     dsl 	assert(provider != NULL);
    429  1.11     dsl 
    430  1.11     dsl 	for (i = 0; machines[i].name != NULL; i++) {
    431  1.11     dsl 		if (strcmp(machines[i].name, mach) == 0) {
    432  1.11     dsl 			param->machine = &machines[i];
    433  1.11     dsl 			return;
    434   1.1   lukem 		}
    435   1.1   lukem 	}
    436  1.11     dsl 	warnx("Invalid machine `%s' from %s", mach, provider);
    437  1.11     dsl 	machine_usage();
    438  1.11     dsl 	exit(1);
    439  1.11     dsl }
    440  1.11     dsl 
    441  1.11     dsl static void
    442  1.11     dsl machine_usage(void)
    443  1.11     dsl {
    444  1.11     dsl 	const char *prefix;
    445  1.11     dsl 	int	i;
    446  1.11     dsl 
    447   1.1   lukem 	warnx("Supported machines are:");
    448  1.10   lukem #define MACHS_PER_LINE	9
    449  1.10   lukem 	prefix="";
    450   1.1   lukem 	for (i = 0; machines[i].name != NULL; i++) {
    451  1.10   lukem 		if (i == 0)
    452  1.10   lukem 			prefix="\t";
    453  1.10   lukem 		else if (i % MACHS_PER_LINE)
    454  1.10   lukem 			prefix=", ";
    455  1.10   lukem 		else
    456  1.10   lukem 			prefix=",\n\t";
    457  1.10   lukem 		fprintf(stderr, "%s%s", prefix, machines[i].name);
    458   1.1   lukem 	}
    459  1.10   lukem 	fputs("\n", stderr);
    460   1.6   lukem }
    461   1.6   lukem 
    462  1.11     dsl static void
    463   1.6   lukem getfstype(ib_params *param, const char *fstype, const char *provider)
    464   1.6   lukem {
    465  1.11     dsl 	int i;
    466  1.11     dsl 
    467  1.11     dsl 	assert(param != NULL);
    468  1.11     dsl 	assert(fstype != NULL);
    469  1.11     dsl 	assert(provider != NULL);
    470  1.11     dsl 
    471  1.11     dsl 	for (i = 0; fstypes[i].name != NULL; i++) {
    472  1.11     dsl 		if (strcmp(fstypes[i].name, fstype) == 0) {
    473  1.11     dsl 			param->fstype = &fstypes[i];
    474  1.11     dsl 			return;
    475  1.11     dsl 		}
    476  1.11     dsl 	}
    477  1.11     dsl 	warnx("Invalid file system type `%s' from %s", fstype, provider);
    478  1.11     dsl 	fstype_usage();
    479  1.11     dsl 	exit(1);
    480  1.11     dsl }
    481  1.11     dsl 
    482  1.11     dsl static void
    483  1.11     dsl fstype_usage(void)
    484  1.11     dsl {
    485  1.10   lukem 	const char *prefix;
    486   1.6   lukem 	int	i;
    487   1.6   lukem 
    488   1.6   lukem 	warnx("Supported file system types are:");
    489  1.10   lukem #define FSTYPES_PER_LINE	9
    490  1.10   lukem 	prefix="";
    491   1.6   lukem 	for (i = 0; fstypes[i].name != NULL; i++) {
    492  1.10   lukem 		if (i == 0)
    493  1.10   lukem 			prefix="\t";
    494  1.10   lukem 		else if (i % FSTYPES_PER_LINE)
    495  1.10   lukem 			prefix=", ";
    496  1.10   lukem 		else
    497  1.10   lukem 			prefix=",\n\t";
    498  1.10   lukem 		fprintf(stderr, "%s%s", prefix, fstypes[i].name);
    499   1.6   lukem 	}
    500  1.10   lukem 	fputs("\n", stderr);
    501   1.1   lukem }
    502   1.1   lukem 
    503   1.1   lukem static void
    504   1.1   lukem usage(void)
    505   1.1   lukem {
    506   1.1   lukem 	const char	*prog;
    507   1.1   lukem 
    508   1.1   lukem 	prog = getprogname();
    509   1.1   lukem 	fprintf(stderr,
    510  1.14    jmmv "usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
    511   1.8   lukem "\t\t   [-b s1start] [-B s2start] filesystem primary [secondary]\n"
    512  1.14    jmmv "usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
    513   1.1   lukem 	    prog, prog);
    514  1.11     dsl 	machine_usage();
    515  1.11     dsl 	fstype_usage();
    516  1.11     dsl 	options_usage();
    517   1.1   lukem 	exit(1);
    518   1.1   lukem }
    519