Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.38
      1  1.38   mlelstv /*	$NetBSD: installboot.c,v 1.38 2015/06/05 07:44:39 mlelstv 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  *
     19   1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     lukem  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     lukem  */
     31   1.1     lukem 
     32  1.16       jmc #if HAVE_NBTOOL_CONFIG_H
     33  1.16       jmc #include "nbtool_config.h"
     34  1.16       jmc #endif
     35  1.16       jmc 
     36   1.1     lukem #include <sys/cdefs.h>
     37  1.33   tsutsui #if !defined(__lint)
     38  1.38   mlelstv __RCSID("$NetBSD: installboot.c,v 1.38 2015/06/05 07:44:39 mlelstv Exp $");
     39   1.1     lukem #endif	/* !__lint */
     40   1.1     lukem 
     41  1.37   mlelstv #include <sys/param.h>
     42  1.24       dsl #include <sys/ioctl.h>
     43   1.1     lukem #include <sys/utsname.h>
     44   1.1     lukem 
     45   1.1     lukem #include <assert.h>
     46   1.1     lukem #include <err.h>
     47   1.1     lukem #include <fcntl.h>
     48   1.2    simonb #include <limits.h>
     49   1.1     lukem #include <stdio.h>
     50   1.1     lukem #include <stdlib.h>
     51  1.11       dsl #include <stddef.h>
     52   1.1     lukem #include <string.h>
     53   1.1     lukem #include <unistd.h>
     54  1.38   mlelstv #if !HAVE_NBTOOL_CONFIG_H
     55  1.37   mlelstv #include <util.h>
     56  1.38   mlelstv #endif
     57   1.1     lukem 
     58   1.1     lukem #include "installboot.h"
     59   1.1     lukem 
     60  1.11       dsl static	void	getmachine(ib_params *, const char *, const char *);
     61  1.11       dsl static	void	getfstype(ib_params *, const char *, const char *);
     62  1.11       dsl static	void	parseoptions(ib_params *, const char *);
     63  1.35     joerg __dead static	void	usage(void);
     64  1.11       dsl static	void	options_usage(void);
     65  1.11       dsl static	void	machine_usage(void);
     66  1.11       dsl static	void	fstype_usage(void);
     67   1.1     lukem 
     68   1.1     lukem static	ib_params	installboot_params;
     69   1.1     lukem 
     70  1.11       dsl #define OFFSET(field)    offsetof(ib_params, field)
     71  1.11       dsl const struct option {
     72  1.11       dsl 	const char	*name;		/* Name of option */
     73  1.11       dsl 	ib_flags	flag;		/* Corresponding IB_xxx flag */
     74  1.11       dsl 	enum {				/* Type of option value... */
     75  1.11       dsl 		OPT_BOOL,		/* no value */
     76  1.11       dsl 		OPT_INT,		/* numeric value */
     77  1.11       dsl 		OPT_WORD,		/* space/tab/, terminated */
     78  1.11       dsl 		OPT_STRING		/* null terminated */
     79  1.11       dsl 	}		type;
     80  1.11       dsl 	int		offset;		/* of field in ib_params */
     81  1.11       dsl } options[] = {
     82  1.27  christos 	{ "alphasum",	IB_ALPHASUM,	OPT_BOOL,	0 },
     83  1.27  christos 	{ "append",	IB_APPEND,	OPT_BOOL,	0 },
     84  1.11       dsl 	{ "command",	IB_COMMAND,	OPT_STRING,	OFFSET(command) },
     85  1.11       dsl 	{ "console",	IB_CONSOLE,	OPT_WORD,	OFFSET(console) },
     86  1.17       dsl 	{ "ioaddr",	IB_CONSADDR,	OPT_INT,	OFFSET(consaddr) },
     87  1.15       dsl 	{ "keymap",	IB_KEYMAP,	OPT_WORD,	OFFSET(keymap) },
     88  1.11       dsl 	{ "password",	IB_PASSWORD,	OPT_WORD,	OFFSET(password) },
     89  1.27  christos 	{ "resetvideo",	IB_RESETVIDEO,	OPT_BOOL,	0 },
     90  1.11       dsl 	{ "speed",	IB_CONSPEED,	OPT_INT,	OFFSET(conspeed) },
     91  1.27  christos 	{ "sunsum",	IB_SUNSUM,	OPT_BOOL,	0 },
     92  1.11       dsl 	{ "timeout",	IB_TIMEOUT,	OPT_INT,	OFFSET(timeout) },
     93  1.34  drochner 	{ "modules",	IB_MODULES,	OPT_BOOL,	0 },
     94  1.34  drochner 	{ "bootconf",	IB_BOOTCONF,	OPT_BOOL,	0 },
     95  1.27  christos 	{ .name = NULL },
     96  1.11       dsl };
     97  1.11       dsl #undef OFFSET
     98  1.11       dsl #define OPTION(params, type, opt) (*(type *)((char *)(params) + (opt)->offset))
     99  1.11       dsl 
    100  1.32   tsutsui #define DFL_SECSIZE	512	/* Don't use DEV_BSIZE. It's host's value. */
    101  1.32   tsutsui 
    102   1.1     lukem int
    103   1.1     lukem main(int argc, char *argv[])
    104   1.1     lukem {
    105   1.1     lukem 	struct utsname	utsname;
    106   1.1     lukem 	ib_params	*params;
    107   1.6     lukem 	unsigned long	lval;
    108   1.1     lukem 	int		ch, rv, mode;
    109   1.1     lukem 	char 		*p;
    110   1.1     lukem 	const char	*op;
    111  1.11       dsl 	ib_flags	unsupported_flags;
    112  1.38   mlelstv #if !HAVE_NBTOOL_CONFIG_H
    113  1.37   mlelstv 	char		specname[MAXPATHLEN];
    114  1.37   mlelstv 	char		rawname[MAXPATHLEN];
    115  1.37   mlelstv 	const char	*special, *raw;
    116  1.38   mlelstv #endif
    117   1.1     lukem 
    118   1.1     lukem 	setprogname(argv[0]);
    119   1.1     lukem 	params = &installboot_params;
    120   1.1     lukem 	memset(params, 0, sizeof(*params));
    121   1.8     lukem 	params->fsfd = -1;
    122   1.8     lukem 	params->s1fd = -1;
    123   1.1     lukem 	if ((p = getenv("MACHINE")) != NULL)
    124  1.11       dsl 		getmachine(params, p, "$MACHINE");
    125   1.1     lukem 
    126  1.28       dsl 	while ((ch = getopt(argc, argv, "b:B:cefm:no:t:v")) != -1) {
    127   1.1     lukem 		switch (ch) {
    128   1.1     lukem 
    129   1.1     lukem 		case 'b':
    130   1.8     lukem 		case 'B':
    131   1.1     lukem 			if (*optarg == '\0')
    132   1.1     lukem 				goto badblock;
    133   1.6     lukem 			lval = strtoul(optarg, &p, 0);
    134   1.6     lukem 			if (lval > UINT32_MAX || *p != '\0') {
    135   1.1     lukem  badblock:
    136   1.1     lukem 				errx(1, "Invalid block number `%s'", optarg);
    137   1.1     lukem 			}
    138   1.8     lukem 			if (ch == 'b') {
    139   1.8     lukem 				params->s1start = (uint32_t)lval;
    140   1.8     lukem 				params->flags |= IB_STAGE1START;
    141   1.8     lukem 			} else {
    142   1.8     lukem 				params->s2start = (uint32_t)lval;
    143   1.8     lukem 				params->flags |= IB_STAGE2START;
    144   1.8     lukem 			}
    145   1.1     lukem 			break;
    146   1.1     lukem 
    147   1.1     lukem 		case 'c':
    148   1.1     lukem 			params->flags |= IB_CLEAR;
    149   1.1     lukem 			break;
    150   1.1     lukem 
    151  1.19       dsl 		case 'e':
    152  1.19       dsl 			params->flags |= IB_EDIT;
    153  1.19       dsl 			break;
    154  1.19       dsl 
    155  1.28       dsl 		case 'f':
    156  1.28       dsl 			params->flags |= IB_FORCE;
    157  1.28       dsl 			break;
    158  1.28       dsl 
    159   1.1     lukem 		case 'm':
    160  1.11       dsl 			getmachine(params, optarg, "-m");
    161   1.1     lukem 			break;
    162   1.1     lukem 
    163   1.1     lukem 		case 'n':
    164   1.1     lukem 			params->flags |= IB_NOWRITE;
    165   1.1     lukem 			break;
    166   1.1     lukem 
    167   1.1     lukem 		case 'o':
    168  1.11       dsl 			parseoptions(params, optarg);
    169   1.1     lukem 			break;
    170   1.1     lukem 
    171   1.1     lukem 		case 't':
    172  1.11       dsl 			getfstype(params, optarg, "-t");
    173   1.1     lukem 			break;
    174   1.1     lukem 
    175   1.1     lukem 		case 'v':
    176   1.1     lukem 			params->flags |= IB_VERBOSE;
    177   1.1     lukem 			break;
    178   1.1     lukem 
    179   1.1     lukem 		case '?':
    180   1.1     lukem 		default:
    181   1.1     lukem 			usage();
    182   1.1     lukem 			/* NOTREACHED */
    183   1.1     lukem 
    184   1.1     lukem 		}
    185   1.1     lukem 	}
    186   1.1     lukem 	argc -= optind;
    187   1.1     lukem 	argv += optind;
    188   1.1     lukem 
    189  1.19       dsl 	if (params->flags & IB_CLEAR && params->flags & IB_EDIT)
    190  1.19       dsl 		usage();
    191  1.19       dsl 	if (argc < 1 || argc + 2 * !!(params->flags & (IB_CLEAR | IB_EDIT)) > 3)
    192   1.1     lukem 		usage();
    193   1.1     lukem 
    194  1.19       dsl 	/* set missing defaults */
    195   1.1     lukem 	if (params->machine == NULL) {
    196   1.1     lukem 		if (uname(&utsname) == -1)
    197   1.1     lukem 			err(1, "Determine uname");
    198  1.11       dsl 		getmachine(params, utsname.machine, "uname()");
    199  1.11       dsl 	}
    200  1.11       dsl 
    201  1.11       dsl 	/* Check that options are supported by this system */
    202  1.11       dsl 	unsupported_flags = params->flags & ~params->machine->valid_flags;
    203  1.29       dsl 	unsupported_flags &= ~(IB_VERBOSE | IB_NOWRITE | IB_CLEAR | IB_EDIT
    204  1.29       dsl 				| IB_FORCE);
    205  1.11       dsl 	if (unsupported_flags != 0) {
    206  1.11       dsl 		int ndx;
    207  1.11       dsl 		for (ndx = 0; options[ndx].name != NULL; ndx++) {
    208  1.29       dsl 			if (unsupported_flags & options[ndx].flag) {
    209  1.29       dsl 				unsupported_flags &= ~options[ndx].flag;
    210  1.11       dsl 				warnx("`-o %s' is not supported for %s",
    211  1.11       dsl 				    options[ndx].name, params->machine->name);
    212  1.29       dsl 			}
    213  1.11       dsl 		}
    214  1.11       dsl 		if (unsupported_flags & IB_STAGE1START)
    215  1.11       dsl 			warnx("`-b bno' is not supported for %s",
    216  1.11       dsl 			    params->machine->name);
    217  1.11       dsl 		if (unsupported_flags & IB_STAGE2START)
    218  1.11       dsl 			warnx("`-B bno' is not supported for %s",
    219  1.11       dsl 			    params->machine->name);
    220  1.29       dsl 		unsupported_flags &= ~(IB_STAGE1START | IB_STAGE2START);
    221  1.29       dsl 		if (unsupported_flags != 0)
    222  1.29       dsl 			warnx("Unknown unsupported flag %#x (coding error!)",
    223  1.29       dsl 			    unsupported_flags);
    224  1.11       dsl 		exit(1);
    225  1.11       dsl 	}
    226  1.11       dsl 	/* and some illegal combinations */
    227  1.11       dsl 	if (params->flags & IB_STAGE1START && params->flags & IB_APPEND) {
    228  1.11       dsl 		warnx("Can't use `-b bno' with `-o append'");
    229  1.11       dsl 		exit(1);
    230  1.11       dsl 	}
    231  1.11       dsl 	if (params->flags & IB_CLEAR &&
    232  1.11       dsl 	    params->flags & (IB_STAGE1START | IB_STAGE2START | IB_APPEND)) {
    233  1.11       dsl 		warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
    234  1.11       dsl 		exit(1);
    235   1.1     lukem 	}
    236   1.1     lukem 
    237  1.21       dsl 	if (argc >= 3) {
    238  1.21       dsl 		params->stage2 = argv[2];
    239  1.21       dsl 	}
    240  1.21       dsl 
    241  1.38   mlelstv #if !HAVE_NBTOOL_CONFIG_H
    242  1.37   mlelstv 	special = getfsspecname(specname, sizeof(specname), argv[0]);
    243  1.37   mlelstv 	raw = getdiskrawname(rawname, sizeof(rawname), special);
    244  1.37   mlelstv 	if (raw != NULL)
    245  1.37   mlelstv 		special = raw;
    246  1.38   mlelstv 	params->filesystem = special;
    247  1.38   mlelstv #else
    248  1.38   mlelstv 	params->filesystem = argv[0];
    249  1.38   mlelstv #endif
    250  1.37   mlelstv 
    251   1.1     lukem 	if (params->flags & IB_NOWRITE) {
    252   1.1     lukem 		op = "only";
    253   1.1     lukem 		mode = O_RDONLY;
    254   1.1     lukem 	} else {
    255   1.1     lukem 		op = "write";
    256   1.1     lukem 		mode = O_RDWR;
    257   1.1     lukem 	}
    258  1.32   tsutsui 	/* XXX should be specified via option */
    259  1.32   tsutsui 	params->sectorsize = DFL_SECSIZE;
    260   1.1     lukem 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    261   1.1     lukem 		err(1, "Opening file system `%s' read-%s",
    262   1.1     lukem 		    params->filesystem, op);
    263   1.9     lukem 	if (fstat(params->fsfd, &params->fsstat) == -1)
    264   1.9     lukem 		err(1, "Examining file system `%s'", params->filesystem);
    265   1.6     lukem 	if (params->fstype != NULL) {
    266   1.6     lukem 		if (! params->fstype->match(params))
    267  1.18     isaki 			errx(1, "File system `%s' is not of type %s",
    268   1.6     lukem 			    params->filesystem, params->fstype->name);
    269   1.6     lukem 	} else {
    270  1.21       dsl 		if (params->stage2 != NULL) {
    271  1.21       dsl 			params->fstype = &fstypes[0];
    272  1.21       dsl 			while (params->fstype->name != NULL &&
    273  1.21       dsl 				    !params->fstype->match(params))
    274  1.21       dsl 				params->fstype++;
    275  1.21       dsl 			if (params->fstype->name == NULL)
    276  1.21       dsl 				errx(1, "File system `%s' is of an unknown type",
    277  1.21       dsl 				    params->filesystem);
    278  1.21       dsl 		}
    279   1.6     lukem 	}
    280   1.1     lukem 
    281   1.5     lukem 	if (argc >= 2) {
    282  1.26  christos 		if ((params->s1fd = open(argv[1], O_RDONLY, 0600)) == -1)
    283  1.26  christos 			err(1, "Opening primary bootstrap `%s'", argv[1]);
    284   1.9     lukem 		if (fstat(params->s1fd, &params->s1stat) == -1)
    285  1.26  christos 			err(1, "Examining primary bootstrap `%s'", argv[1]);
    286   1.9     lukem 		if (!S_ISREG(params->s1stat.st_mode))
    287  1.26  christos 			errx(1, "`%s' must be a regular file", argv[1]);
    288  1.26  christos 		params->stage1 = argv[1];
    289   1.5     lukem 	}
    290   1.1     lukem 	assert(params->machine != NULL);
    291   1.1     lukem 
    292   1.1     lukem 	if (params->flags & IB_VERBOSE) {
    293   1.5     lukem 		printf("File system:         %s\n", params->filesystem);
    294  1.21       dsl 		if (params->fstype)
    295  1.21       dsl 			printf("File system type:    %s (blocksize %u, "
    296  1.21       dsl 				"needswap %d)\n",
    297  1.21       dsl 			    params->fstype->name, params->fstype->blocksize,
    298  1.21       dsl 			    params->fstype->needswap);
    299  1.19       dsl 		if (!(params->flags & IB_EDIT))
    300  1.19       dsl 			printf("Primary bootstrap:   %s\n",
    301  1.19       dsl 			    (params->flags & IB_CLEAR) ? "(to be cleared)"
    302  1.19       dsl 			    : params->stage1 ? params->stage1 : "(none)" );
    303   1.5     lukem 		if (params->stage2 != NULL)
    304   1.5     lukem 			printf("Secondary bootstrap: %s\n", params->stage2);
    305   1.1     lukem 	}
    306   1.1     lukem 
    307  1.19       dsl 	if (params->flags & IB_EDIT) {
    308  1.19       dsl 		op = "Edit";
    309  1.19       dsl 		rv = params->machine->editboot(params);
    310  1.19       dsl 	} else if (params->flags & IB_CLEAR) {
    311   1.1     lukem 		op = "Clear";
    312   1.1     lukem 		rv = params->machine->clearboot(params);
    313   1.1     lukem 	} else {
    314  1.22      jmmv 		if (argc < 2)
    315  1.22      jmmv 			errx(EXIT_FAILURE, "Please specify the primary "
    316  1.22      jmmv 			    "bootstrap file");
    317   1.1     lukem 		op = "Set";
    318   1.1     lukem 		rv = params->machine->setboot(params);
    319   1.1     lukem 	}
    320   1.1     lukem 	if (rv == 0)
    321   1.5     lukem 		errx(1, "%s bootstrap operation failed", op);
    322   1.1     lukem 
    323   1.9     lukem 	if (S_ISREG(params->fsstat.st_mode)) {
    324   1.9     lukem 		if (fsync(params->fsfd) == -1)
    325   1.9     lukem 			err(1, "Synchronising file system `%s'",
    326   1.9     lukem 			    params->filesystem);
    327   1.9     lukem 	} else {
    328   1.9     lukem 		/* Sync filesystems (to clean in-memory superblock?) */
    329   1.9     lukem 		sync();
    330   1.9     lukem 	}
    331   1.1     lukem 	if (close(params->fsfd) == -1)
    332   1.1     lukem 		err(1, "Closing file system `%s'", params->filesystem);
    333   1.1     lukem 	if (argc == 2)
    334   1.5     lukem 		if (close(params->s1fd) == -1)
    335   1.5     lukem 			err(1, "Closing primary bootstrap `%s'",
    336   1.5     lukem 			    params->stage1);
    337   1.1     lukem 
    338   1.1     lukem 	exit(0);
    339   1.1     lukem 	/* NOTREACHED */
    340   1.1     lukem }
    341   1.1     lukem 
    342  1.11       dsl static void
    343  1.11       dsl parseoptions(ib_params *params, const char *option)
    344   1.1     lukem {
    345  1.11       dsl 	char *cp;
    346  1.11       dsl 	const struct option *opt;
    347  1.11       dsl 	int len;
    348  1.12       dsl 	unsigned long val;
    349   1.1     lukem 
    350   1.1     lukem 	assert(params != NULL);
    351   1.1     lukem 	assert(option != NULL);
    352   1.1     lukem 
    353  1.11       dsl 	for (;; option += len) {
    354  1.11       dsl 		option += strspn(option, ", \t");
    355  1.11       dsl 		if (*option == 0)
    356  1.11       dsl 			return;
    357  1.11       dsl 		len = strcspn(option, "=,");
    358  1.11       dsl 		for (opt = options; opt->name != NULL; opt++) {
    359  1.11       dsl 			if (memcmp(option, opt->name, len) == 0
    360  1.11       dsl 			    && opt->name[len] == 0)
    361  1.11       dsl 				break;
    362  1.11       dsl 		}
    363  1.11       dsl 		if (opt->name == NULL) {
    364  1.11       dsl 			len = strcspn(option, ",");
    365  1.11       dsl 			warnx("Unknown option `-o %.*s'", len, option);
    366  1.11       dsl 			break;
    367  1.11       dsl 		}
    368  1.11       dsl 		params->flags |= opt->flag;
    369  1.11       dsl 		if (opt->type == OPT_BOOL) {
    370  1.11       dsl 			if (option[len] != '=')
    371  1.11       dsl 				continue;
    372  1.11       dsl 			warnx("Option `%s' must not have a value", opt->name);
    373  1.11       dsl 			break;
    374  1.11       dsl 		}
    375  1.11       dsl 		if (option[len] != '=') {
    376  1.11       dsl 			warnx("Option `%s' must have a value", opt->name);
    377  1.11       dsl 			break;
    378   1.1     lukem 		}
    379  1.11       dsl 		option += len + 1;
    380  1.11       dsl 		len = strcspn(option, ",");
    381  1.11       dsl 		switch (opt->type) {
    382  1.11       dsl 		case OPT_STRING:
    383  1.11       dsl 			len = strlen(option);
    384  1.11       dsl 			/* FALLTHROUGH */
    385  1.11       dsl 		case OPT_WORD:
    386  1.11       dsl 			cp = strdup(option);
    387  1.11       dsl 			if (cp == NULL)
    388  1.11       dsl 				err(1, "strdup");
    389  1.11       dsl 			cp[len] = 0;
    390  1.11       dsl 			OPTION(params, char *, opt) = cp;
    391  1.11       dsl 			continue;
    392  1.11       dsl 		case OPT_INT:
    393  1.11       dsl 			val = strtoul(option, &cp, 0);
    394  1.11       dsl 			if (cp > option + len || (*cp != 0 && *cp != ','))
    395  1.11       dsl 				break;
    396  1.31     lukem 			if (val > INT_MAX)
    397  1.11       dsl 				break;
    398  1.31     lukem 			OPTION(params, int, opt) = (int)val;
    399  1.11       dsl 			continue;
    400  1.11       dsl 		default:
    401  1.13    petrov 			errx(1, "Internal error: option `%s' has invalid type %d",
    402  1.11       dsl 				opt->name, opt->type);
    403  1.11       dsl 		}
    404  1.11       dsl 		warnx("Invalid option value `%s=%.*s'", opt->name, len, option);
    405  1.11       dsl 		break;
    406   1.1     lukem 	}
    407  1.11       dsl 	options_usage();
    408  1.11       dsl 	exit(1);
    409   1.3     lukem }
    410   1.3     lukem 
    411  1.11       dsl static void
    412  1.11       dsl options_usage(void)
    413   1.3     lukem {
    414  1.11       dsl 	int ndx;
    415  1.11       dsl 	const char *pfx;
    416   1.3     lukem 
    417  1.11       dsl 	warnx("Valid options are:");
    418  1.11       dsl 	pfx = "\t";
    419  1.11       dsl 	for (ndx = 0; options[ndx].name != 0; ndx++) {
    420  1.11       dsl 		fprintf(stderr, "%s%s", pfx, options[ndx].name);
    421  1.11       dsl 		switch (options[ndx].type) {
    422  1.11       dsl 		case OPT_INT:
    423  1.11       dsl 			fprintf(stderr, "=number");
    424  1.11       dsl 			break;
    425  1.11       dsl 		case OPT_WORD:
    426  1.11       dsl 			fprintf(stderr, "=word");
    427  1.11       dsl 			break;
    428  1.11       dsl 		case OPT_STRING:
    429  1.11       dsl 			fprintf(stderr, "=string");
    430  1.11       dsl 			break;
    431  1.11       dsl 		default:
    432  1.11       dsl 			break;
    433  1.11       dsl 		}
    434  1.11       dsl 		if ((ndx % 5) == 4)
    435  1.11       dsl 			pfx = ",\n\t";
    436  1.11       dsl 		else
    437  1.11       dsl 			pfx = ", ";
    438  1.11       dsl 	}
    439  1.11       dsl 	fprintf(stderr, "\n");
    440   1.3     lukem }
    441   1.3     lukem 
    442   1.3     lukem int
    443   1.3     lukem no_setboot(ib_params *params)
    444   1.3     lukem {
    445   1.3     lukem 
    446   1.7     lukem 	assert(params != NULL);
    447   1.7     lukem 
    448   1.5     lukem 	warnx("%s: bootstrap installation is not supported",
    449   1.3     lukem 	    params->machine->name);
    450   1.3     lukem 	return (0);
    451   1.3     lukem }
    452   1.3     lukem 
    453   1.3     lukem int
    454   1.3     lukem no_clearboot(ib_params *params)
    455   1.3     lukem {
    456   1.3     lukem 
    457   1.7     lukem 	assert(params != NULL);
    458   1.7     lukem 
    459   1.5     lukem 	warnx("%s: bootstrap removal is not supported",
    460   1.3     lukem 	    params->machine->name);
    461   1.1     lukem 	return (0);
    462   1.1     lukem }
    463   1.1     lukem 
    464  1.19       dsl int
    465  1.19       dsl no_editboot(ib_params *params)
    466  1.19       dsl {
    467  1.19       dsl 
    468  1.19       dsl 	assert(params != NULL);
    469  1.19       dsl 
    470  1.19       dsl 	warnx("%s: bootstrap editing is not supported",
    471  1.19       dsl 	    params->machine->name);
    472  1.19       dsl 	return (0);
    473  1.19       dsl }
    474  1.19       dsl 
    475   1.1     lukem 
    476  1.11       dsl static void
    477   1.1     lukem getmachine(ib_params *param, const char *mach, const char *provider)
    478   1.1     lukem {
    479   1.1     lukem 	int	i;
    480   1.1     lukem 
    481  1.11       dsl 	assert(param != NULL);
    482  1.11       dsl 	assert(mach != NULL);
    483  1.11       dsl 	assert(provider != NULL);
    484  1.11       dsl 
    485  1.23       dsl 	for (i = 0; machines[i] != NULL; i++) {
    486  1.24       dsl 		if (machines[i]->name == NULL)
    487  1.24       dsl 			continue;
    488  1.23       dsl 		if (strcmp(machines[i]->name, mach) == 0) {
    489  1.23       dsl 			param->machine = machines[i];
    490  1.11       dsl 			return;
    491   1.1     lukem 		}
    492   1.1     lukem 	}
    493  1.11       dsl 	warnx("Invalid machine `%s' from %s", mach, provider);
    494  1.11       dsl 	machine_usage();
    495  1.11       dsl 	exit(1);
    496  1.11       dsl }
    497  1.11       dsl 
    498  1.11       dsl static void
    499  1.11       dsl machine_usage(void)
    500  1.11       dsl {
    501  1.11       dsl 	const char *prefix;
    502  1.11       dsl 	int	i;
    503  1.24       dsl 	int col, len;
    504  1.24       dsl 	const char *name;
    505  1.25    dogcow 	int	wincol=80;
    506  1.25    dogcow #ifdef TIOCGWINSZ
    507  1.24       dsl 	struct winsize win;
    508  1.24       dsl 
    509  1.36    martin 	if (ioctl(fileno(stderr), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
    510  1.25    dogcow 		wincol = win.ws_col;
    511  1.25    dogcow #endif
    512  1.11       dsl 
    513   1.1     lukem 	warnx("Supported machines are:");
    514  1.24       dsl 	prefix="\t";
    515  1.24       dsl 	col = 8 + 3;
    516  1.23       dsl 	for (i = 0; machines[i] != NULL; i++) {
    517  1.24       dsl 		name = machines[i]->name;
    518  1.24       dsl 		if (name == NULL)
    519  1.24       dsl 			continue;
    520  1.24       dsl 		len = strlen(name);
    521  1.25    dogcow 		if (col + len > wincol) {
    522  1.10     lukem 			prefix=",\n\t";
    523  1.24       dsl 			col = -2 + 8 + 3;
    524  1.24       dsl 		}
    525  1.24       dsl 		col += fprintf(stderr, "%s%s", prefix, name);
    526  1.24       dsl 		prefix=", ";
    527   1.1     lukem 	}
    528  1.10     lukem 	fputs("\n", stderr);
    529   1.6     lukem }
    530   1.6     lukem 
    531  1.11       dsl static void
    532   1.6     lukem getfstype(ib_params *param, const char *fstype, const char *provider)
    533   1.6     lukem {
    534  1.11       dsl 	int i;
    535  1.11       dsl 
    536  1.11       dsl 	assert(param != NULL);
    537  1.11       dsl 	assert(fstype != NULL);
    538  1.11       dsl 	assert(provider != NULL);
    539  1.11       dsl 
    540  1.11       dsl 	for (i = 0; fstypes[i].name != NULL; i++) {
    541  1.11       dsl 		if (strcmp(fstypes[i].name, fstype) == 0) {
    542  1.11       dsl 			param->fstype = &fstypes[i];
    543  1.11       dsl 			return;
    544  1.11       dsl 		}
    545  1.11       dsl 	}
    546  1.11       dsl 	warnx("Invalid file system type `%s' from %s", fstype, provider);
    547  1.11       dsl 	fstype_usage();
    548  1.11       dsl 	exit(1);
    549  1.11       dsl }
    550  1.11       dsl 
    551  1.11       dsl static void
    552  1.11       dsl fstype_usage(void)
    553  1.11       dsl {
    554  1.36    martin #ifndef NO_STAGE2
    555  1.10     lukem 	const char *prefix;
    556   1.6     lukem 	int	i;
    557   1.6     lukem 
    558   1.6     lukem 	warnx("Supported file system types are:");
    559  1.10     lukem #define FSTYPES_PER_LINE	9
    560  1.24       dsl 	prefix="\t";
    561   1.6     lukem 	for (i = 0; fstypes[i].name != NULL; i++) {
    562  1.25    dogcow 		if (i && (i % FSTYPES_PER_LINE) == 0)
    563  1.10     lukem 			prefix=",\n\t";
    564  1.10     lukem 		fprintf(stderr, "%s%s", prefix, fstypes[i].name);
    565  1.24       dsl 		prefix=", ";
    566   1.6     lukem 	}
    567  1.10     lukem 	fputs("\n", stderr);
    568  1.36    martin #endif
    569   1.1     lukem }
    570   1.1     lukem 
    571   1.1     lukem static void
    572   1.1     lukem usage(void)
    573   1.1     lukem {
    574   1.1     lukem 	const char	*prog;
    575   1.1     lukem 
    576   1.1     lukem 	prog = getprogname();
    577   1.1     lukem 	fprintf(stderr,
    578  1.28       dsl "usage: %s [-fnv] [-B s2bno] [-b s1bno] [-m machine] [-o options]\n"
    579  1.20       wiz "\t\t   [-t fstype] filesystem primary [secondary]\n"
    580  1.28       dsl "usage: %s -c [-fnv] [-m machine] [-o options] [-t fstype] filesystem\n"
    581  1.28       dsl "usage: %s -e [-fnv] [-m machine] [-o options] bootstrap\n",
    582  1.19       dsl 	    prog, prog, prog);
    583  1.11       dsl 	machine_usage();
    584  1.11       dsl 	fstype_usage();
    585  1.11       dsl 	options_usage();
    586   1.1     lukem 	exit(1);
    587   1.1     lukem }
    588