Home | History | Annotate | Line # | Download | only in installboot
      1  1.41     brook /*	$NetBSD: installboot.c,v 1.41 2022/07/10 19:28:00 brook 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.41     brook __RCSID("$NetBSD: installboot.c,v 1.41 2022/07/10 19:28:00 brook 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.40   thorpej #include "evboards.h"
     60   1.1     lukem 
     61  1.11       dsl static	void	getmachine(ib_params *, const char *, const char *);
     62  1.11       dsl static	void	getfstype(ib_params *, const char *, const char *);
     63  1.41     brook static	void	getubootpaths(ib_params *, const char *);
     64  1.11       dsl static	void	parseoptions(ib_params *, const char *);
     65  1.35     joerg __dead static	void	usage(void);
     66  1.11       dsl static	void	options_usage(void);
     67  1.11       dsl static	void	machine_usage(void);
     68  1.11       dsl static	void	fstype_usage(void);
     69   1.1     lukem 
     70   1.1     lukem static	ib_params	installboot_params;
     71   1.1     lukem 
     72  1.11       dsl #define OFFSET(field)    offsetof(ib_params, field)
     73  1.11       dsl const struct option {
     74  1.11       dsl 	const char	*name;		/* Name of option */
     75  1.11       dsl 	ib_flags	flag;		/* Corresponding IB_xxx flag */
     76  1.11       dsl 	enum {				/* Type of option value... */
     77  1.11       dsl 		OPT_BOOL,		/* no value */
     78  1.11       dsl 		OPT_INT,		/* numeric value */
     79  1.11       dsl 		OPT_WORD,		/* space/tab/, terminated */
     80  1.11       dsl 		OPT_STRING		/* null terminated */
     81  1.11       dsl 	}		type;
     82  1.11       dsl 	int		offset;		/* of field in ib_params */
     83  1.11       dsl } options[] = {
     84  1.27  christos 	{ "alphasum",	IB_ALPHASUM,	OPT_BOOL,	0 },
     85  1.27  christos 	{ "append",	IB_APPEND,	OPT_BOOL,	0 },
     86  1.11       dsl 	{ "command",	IB_COMMAND,	OPT_STRING,	OFFSET(command) },
     87  1.11       dsl 	{ "console",	IB_CONSOLE,	OPT_WORD,	OFFSET(console) },
     88  1.17       dsl 	{ "ioaddr",	IB_CONSADDR,	OPT_INT,	OFFSET(consaddr) },
     89  1.15       dsl 	{ "keymap",	IB_KEYMAP,	OPT_WORD,	OFFSET(keymap) },
     90  1.11       dsl 	{ "password",	IB_PASSWORD,	OPT_WORD,	OFFSET(password) },
     91  1.27  christos 	{ "resetvideo",	IB_RESETVIDEO,	OPT_BOOL,	0 },
     92  1.11       dsl 	{ "speed",	IB_CONSPEED,	OPT_INT,	OFFSET(conspeed) },
     93  1.27  christos 	{ "sunsum",	IB_SUNSUM,	OPT_BOOL,	0 },
     94  1.11       dsl 	{ "timeout",	IB_TIMEOUT,	OPT_INT,	OFFSET(timeout) },
     95  1.34  drochner 	{ "modules",	IB_MODULES,	OPT_BOOL,	0 },
     96  1.34  drochner 	{ "bootconf",	IB_BOOTCONF,	OPT_BOOL,	0 },
     97  1.40   thorpej 	{ "board",	IB_BOARD,	OPT_STRING,	OFFSET(board) },
     98  1.40   thorpej 	{ "dtb",	IB_DTB,		OPT_STRING,	OFFSET(dtb) },
     99  1.40   thorpej 	{ "media",	IB_MEDIA,	OPT_WORD,	OFFSET(media) },
    100  1.27  christos 	{ .name = NULL },
    101  1.11       dsl };
    102  1.11       dsl #undef OFFSET
    103  1.11       dsl #define OPTION(params, type, opt) (*(type *)((char *)(params) + (opt)->offset))
    104  1.11       dsl 
    105  1.32   tsutsui #define DFL_SECSIZE	512	/* Don't use DEV_BSIZE. It's host's value. */
    106  1.32   tsutsui 
    107  1.41     brook #ifndef DEFAULT_UBOOT_PKG_PATH
    108  1.41     brook #define	DEFAULT_UBOOT_PKG_PATH	"/usr/pkg/share/u-boot"
    109  1.41     brook #endif
    110  1.41     brook 
    111  1.41     brook #ifndef UBOOT_PATHS_ENV_VAR
    112  1.41     brook #define	UBOOT_PATHS_ENV_VAR	"INSTALLBOOT_UBOOT_PATHS"
    113  1.41     brook #endif
    114  1.41     brook 
    115   1.1     lukem int
    116   1.1     lukem main(int argc, char *argv[])
    117   1.1     lukem {
    118   1.1     lukem 	struct utsname	utsname;
    119   1.1     lukem 	ib_params	*params;
    120   1.6     lukem 	unsigned long	lval;
    121   1.1     lukem 	int		ch, rv, mode;
    122   1.1     lukem 	char 		*p;
    123   1.1     lukem 	const char	*op;
    124  1.11       dsl 	ib_flags	unsupported_flags;
    125  1.38   mlelstv #if !HAVE_NBTOOL_CONFIG_H
    126  1.37   mlelstv 	char		specname[MAXPATHLEN];
    127  1.37   mlelstv 	char		rawname[MAXPATHLEN];
    128  1.37   mlelstv 	const char	*special, *raw;
    129  1.38   mlelstv #endif
    130   1.1     lukem 
    131   1.1     lukem 	setprogname(argv[0]);
    132   1.1     lukem 	params = &installboot_params;
    133   1.1     lukem 	memset(params, 0, sizeof(*params));
    134   1.8     lukem 	params->fsfd = -1;
    135   1.8     lukem 	params->s1fd = -1;
    136   1.1     lukem 	if ((p = getenv("MACHINE")) != NULL)
    137  1.11       dsl 		getmachine(params, p, "$MACHINE");
    138  1.41     brook 	getubootpaths(params, DEFAULT_UBOOT_PKG_PATH);
    139  1.41     brook 	if ((p = getenv(UBOOT_PATHS_ENV_VAR)) != NULL) {
    140  1.41     brook 		getubootpaths(params, p);
    141  1.41     brook 	}
    142   1.1     lukem 
    143  1.41     brook 	while ((ch = getopt(argc, argv, "b:B:cefm:no:t:u:v")) != -1) {
    144   1.1     lukem 		switch (ch) {
    145   1.1     lukem 
    146   1.1     lukem 		case 'b':
    147   1.8     lukem 		case 'B':
    148   1.1     lukem 			if (*optarg == '\0')
    149   1.1     lukem 				goto badblock;
    150   1.6     lukem 			lval = strtoul(optarg, &p, 0);
    151   1.6     lukem 			if (lval > UINT32_MAX || *p != '\0') {
    152   1.1     lukem  badblock:
    153   1.1     lukem 				errx(1, "Invalid block number `%s'", optarg);
    154   1.1     lukem 			}
    155   1.8     lukem 			if (ch == 'b') {
    156   1.8     lukem 				params->s1start = (uint32_t)lval;
    157   1.8     lukem 				params->flags |= IB_STAGE1START;
    158   1.8     lukem 			} else {
    159   1.8     lukem 				params->s2start = (uint32_t)lval;
    160   1.8     lukem 				params->flags |= IB_STAGE2START;
    161   1.8     lukem 			}
    162   1.1     lukem 			break;
    163   1.1     lukem 
    164   1.1     lukem 		case 'c':
    165   1.1     lukem 			params->flags |= IB_CLEAR;
    166   1.1     lukem 			break;
    167   1.1     lukem 
    168  1.19       dsl 		case 'e':
    169  1.19       dsl 			params->flags |= IB_EDIT;
    170  1.19       dsl 			break;
    171  1.19       dsl 
    172  1.28       dsl 		case 'f':
    173  1.28       dsl 			params->flags |= IB_FORCE;
    174  1.28       dsl 			break;
    175  1.28       dsl 
    176   1.1     lukem 		case 'm':
    177  1.11       dsl 			getmachine(params, optarg, "-m");
    178   1.1     lukem 			break;
    179   1.1     lukem 
    180   1.1     lukem 		case 'n':
    181   1.1     lukem 			params->flags |= IB_NOWRITE;
    182   1.1     lukem 			break;
    183   1.1     lukem 
    184   1.1     lukem 		case 'o':
    185  1.11       dsl 			parseoptions(params, optarg);
    186   1.1     lukem 			break;
    187   1.1     lukem 
    188   1.1     lukem 		case 't':
    189  1.11       dsl 			getfstype(params, optarg, "-t");
    190   1.1     lukem 			break;
    191   1.1     lukem 
    192  1.41     brook 		case 'u':
    193  1.41     brook 			getubootpaths(params, optarg);
    194  1.41     brook 			break;
    195  1.41     brook 
    196   1.1     lukem 		case 'v':
    197   1.1     lukem 			params->flags |= IB_VERBOSE;
    198   1.1     lukem 			break;
    199   1.1     lukem 
    200   1.1     lukem 		case '?':
    201   1.1     lukem 		default:
    202   1.1     lukem 			usage();
    203   1.1     lukem 			/* NOTREACHED */
    204   1.1     lukem 
    205   1.1     lukem 		}
    206   1.1     lukem 	}
    207   1.1     lukem 	argc -= optind;
    208   1.1     lukem 	argv += optind;
    209   1.1     lukem 
    210  1.19       dsl 	if (params->flags & IB_CLEAR && params->flags & IB_EDIT)
    211  1.19       dsl 		usage();
    212  1.19       dsl 	if (argc < 1 || argc + 2 * !!(params->flags & (IB_CLEAR | IB_EDIT)) > 3)
    213   1.1     lukem 		usage();
    214   1.1     lukem 
    215  1.19       dsl 	/* set missing defaults */
    216   1.1     lukem 	if (params->machine == NULL) {
    217   1.1     lukem 		if (uname(&utsname) == -1)
    218   1.1     lukem 			err(1, "Determine uname");
    219  1.11       dsl 		getmachine(params, utsname.machine, "uname()");
    220  1.11       dsl 	}
    221  1.11       dsl 
    222  1.11       dsl 	/* Check that options are supported by this system */
    223  1.11       dsl 	unsupported_flags = params->flags & ~params->machine->valid_flags;
    224  1.29       dsl 	unsupported_flags &= ~(IB_VERBOSE | IB_NOWRITE | IB_CLEAR | IB_EDIT
    225  1.29       dsl 				| IB_FORCE);
    226  1.11       dsl 	if (unsupported_flags != 0) {
    227  1.11       dsl 		int ndx;
    228  1.11       dsl 		for (ndx = 0; options[ndx].name != NULL; ndx++) {
    229  1.29       dsl 			if (unsupported_flags & options[ndx].flag) {
    230  1.29       dsl 				unsupported_flags &= ~options[ndx].flag;
    231  1.11       dsl 				warnx("`-o %s' is not supported for %s",
    232  1.11       dsl 				    options[ndx].name, params->machine->name);
    233  1.29       dsl 			}
    234  1.11       dsl 		}
    235  1.11       dsl 		if (unsupported_flags & IB_STAGE1START)
    236  1.11       dsl 			warnx("`-b bno' is not supported for %s",
    237  1.11       dsl 			    params->machine->name);
    238  1.11       dsl 		if (unsupported_flags & IB_STAGE2START)
    239  1.11       dsl 			warnx("`-B bno' is not supported for %s",
    240  1.11       dsl 			    params->machine->name);
    241  1.29       dsl 		unsupported_flags &= ~(IB_STAGE1START | IB_STAGE2START);
    242  1.29       dsl 		if (unsupported_flags != 0)
    243  1.29       dsl 			warnx("Unknown unsupported flag %#x (coding error!)",
    244  1.29       dsl 			    unsupported_flags);
    245  1.11       dsl 		exit(1);
    246  1.11       dsl 	}
    247  1.11       dsl 	/* and some illegal combinations */
    248  1.11       dsl 	if (params->flags & IB_STAGE1START && params->flags & IB_APPEND) {
    249  1.11       dsl 		warnx("Can't use `-b bno' with `-o append'");
    250  1.11       dsl 		exit(1);
    251  1.11       dsl 	}
    252  1.11       dsl 	if (params->flags & IB_CLEAR &&
    253  1.11       dsl 	    params->flags & (IB_STAGE1START | IB_STAGE2START | IB_APPEND)) {
    254  1.11       dsl 		warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
    255  1.11       dsl 		exit(1);
    256   1.1     lukem 	}
    257   1.1     lukem 
    258  1.21       dsl 	if (argc >= 3) {
    259  1.21       dsl 		params->stage2 = argv[2];
    260  1.21       dsl 	}
    261  1.21       dsl 
    262  1.38   mlelstv #if !HAVE_NBTOOL_CONFIG_H
    263  1.37   mlelstv 	special = getfsspecname(specname, sizeof(specname), argv[0]);
    264  1.39   mlelstv 	if (special == NULL)
    265  1.39   mlelstv 		err(1, "%s: %s", argv[0], specname);
    266  1.37   mlelstv 	raw = getdiskrawname(rawname, sizeof(rawname), special);
    267  1.37   mlelstv 	if (raw != NULL)
    268  1.37   mlelstv 		special = raw;
    269  1.38   mlelstv 	params->filesystem = special;
    270  1.38   mlelstv #else
    271  1.38   mlelstv 	params->filesystem = argv[0];
    272  1.38   mlelstv #endif
    273  1.37   mlelstv 
    274   1.1     lukem 	if (params->flags & IB_NOWRITE) {
    275   1.1     lukem 		op = "only";
    276   1.1     lukem 		mode = O_RDONLY;
    277   1.1     lukem 	} else {
    278   1.1     lukem 		op = "write";
    279   1.1     lukem 		mode = O_RDWR;
    280   1.1     lukem 	}
    281  1.32   tsutsui 	/* XXX should be specified via option */
    282  1.32   tsutsui 	params->sectorsize = DFL_SECSIZE;
    283   1.1     lukem 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    284   1.1     lukem 		err(1, "Opening file system `%s' read-%s",
    285   1.1     lukem 		    params->filesystem, op);
    286   1.9     lukem 	if (fstat(params->fsfd, &params->fsstat) == -1)
    287   1.9     lukem 		err(1, "Examining file system `%s'", params->filesystem);
    288   1.6     lukem 	if (params->fstype != NULL) {
    289   1.6     lukem 		if (! params->fstype->match(params))
    290  1.18     isaki 			errx(1, "File system `%s' is not of type %s",
    291   1.6     lukem 			    params->filesystem, params->fstype->name);
    292   1.6     lukem 	} else {
    293  1.21       dsl 		if (params->stage2 != NULL) {
    294  1.21       dsl 			params->fstype = &fstypes[0];
    295  1.21       dsl 			while (params->fstype->name != NULL &&
    296  1.21       dsl 				    !params->fstype->match(params))
    297  1.21       dsl 				params->fstype++;
    298  1.21       dsl 			if (params->fstype->name == NULL)
    299  1.21       dsl 				errx(1, "File system `%s' is of an unknown type",
    300  1.21       dsl 				    params->filesystem);
    301  1.21       dsl 		}
    302   1.6     lukem 	}
    303   1.1     lukem 
    304  1.40   thorpej 	assert(params->machine != NULL);
    305  1.40   thorpej 
    306   1.5     lukem 	if (argc >= 2) {
    307  1.26  christos 		if ((params->s1fd = open(argv[1], O_RDONLY, 0600)) == -1)
    308  1.26  christos 			err(1, "Opening primary bootstrap `%s'", argv[1]);
    309   1.9     lukem 		if (fstat(params->s1fd, &params->s1stat) == -1)
    310  1.26  christos 			err(1, "Examining primary bootstrap `%s'", argv[1]);
    311  1.40   thorpej 		if (!S_ISREG(params->s1stat.st_mode)) {
    312  1.40   thorpej 			/*
    313  1.40   thorpej 			 * If the platform uses u-boot, then the stage1
    314  1.40   thorpej 			 * spec might be the directory where the u-boot
    315  1.40   thorpej 			 * binaries for the system are located.
    316  1.40   thorpej 			 */
    317  1.40   thorpej 			if (params->machine->mach_flags & MF_UBOOT) {
    318  1.40   thorpej 				if (!S_ISDIR(params->s1stat.st_mode)) {
    319  1.40   thorpej 					errx(1, "`%s' must be a regular file "
    320  1.40   thorpej 					     "or a directory", argv[1]);
    321  1.40   thorpej 				}
    322  1.40   thorpej 				(void) close(params->s1fd);
    323  1.40   thorpej 				params->s1fd = -1;
    324  1.40   thorpej 			} else {
    325  1.40   thorpej 				errx(1, "`%s' must be a regular file", argv[1]);
    326  1.40   thorpej 			}
    327  1.40   thorpej 		}
    328  1.26  christos 		params->stage1 = argv[1];
    329   1.5     lukem 	}
    330   1.1     lukem 
    331   1.1     lukem 	if (params->flags & IB_VERBOSE) {
    332   1.5     lukem 		printf("File system:         %s\n", params->filesystem);
    333  1.21       dsl 		if (params->fstype)
    334  1.21       dsl 			printf("File system type:    %s (blocksize %u, "
    335  1.21       dsl 				"needswap %d)\n",
    336  1.21       dsl 			    params->fstype->name, params->fstype->blocksize,
    337  1.21       dsl 			    params->fstype->needswap);
    338  1.19       dsl 		if (!(params->flags & IB_EDIT))
    339  1.40   thorpej 			printf("Primary bootstrap:   %s%s\n",
    340  1.19       dsl 			    (params->flags & IB_CLEAR) ? "(to be cleared)"
    341  1.40   thorpej 			    : params->stage1 ? params->stage1 : "(none)",
    342  1.40   thorpej 			    S_ISDIR(params->s1stat.st_mode) ? " (directory)"
    343  1.40   thorpej 			    				    : "");
    344   1.5     lukem 		if (params->stage2 != NULL)
    345   1.5     lukem 			printf("Secondary bootstrap: %s\n", params->stage2);
    346   1.1     lukem 	}
    347   1.1     lukem 
    348  1.19       dsl 	if (params->flags & IB_EDIT) {
    349  1.19       dsl 		op = "Edit";
    350  1.19       dsl 		rv = params->machine->editboot(params);
    351  1.19       dsl 	} else if (params->flags & IB_CLEAR) {
    352   1.1     lukem 		op = "Clear";
    353   1.1     lukem 		rv = params->machine->clearboot(params);
    354   1.1     lukem 	} else {
    355  1.40   thorpej 		if (argc < 2) {
    356  1.40   thorpej 			/*
    357  1.40   thorpej 			 * If the platform uses u-boot, then the stage1 spec is
    358  1.40   thorpej 			 * optional iff they specified a board (because we can
    359  1.40   thorpej 			 * infer a default location for u-boot binaries if the
    360  1.40   thorpej 			 * board type is given).
    361  1.40   thorpej 			 */
    362  1.40   thorpej 			if (!(params->machine->mach_flags & MF_UBOOT)) {
    363  1.40   thorpej 				errx(EXIT_FAILURE,
    364  1.40   thorpej 				   "Please specify the primary bootstrap file");
    365  1.40   thorpej 			}
    366  1.40   thorpej 		}
    367   1.1     lukem 		op = "Set";
    368   1.1     lukem 		rv = params->machine->setboot(params);
    369   1.1     lukem 	}
    370   1.1     lukem 	if (rv == 0)
    371   1.5     lukem 		errx(1, "%s bootstrap operation failed", op);
    372   1.1     lukem 
    373   1.9     lukem 	if (S_ISREG(params->fsstat.st_mode)) {
    374   1.9     lukem 		if (fsync(params->fsfd) == -1)
    375   1.9     lukem 			err(1, "Synchronising file system `%s'",
    376   1.9     lukem 			    params->filesystem);
    377   1.9     lukem 	} else {
    378   1.9     lukem 		/* Sync filesystems (to clean in-memory superblock?) */
    379   1.9     lukem 		sync();
    380   1.9     lukem 	}
    381   1.1     lukem 	if (close(params->fsfd) == -1)
    382   1.1     lukem 		err(1, "Closing file system `%s'", params->filesystem);
    383  1.40   thorpej 	if (params->s1fd != -1)
    384   1.5     lukem 		if (close(params->s1fd) == -1)
    385   1.5     lukem 			err(1, "Closing primary bootstrap `%s'",
    386   1.5     lukem 			    params->stage1);
    387   1.1     lukem 
    388   1.1     lukem 	exit(0);
    389   1.1     lukem 	/* NOTREACHED */
    390   1.1     lukem }
    391   1.1     lukem 
    392  1.11       dsl static void
    393  1.11       dsl parseoptions(ib_params *params, const char *option)
    394   1.1     lukem {
    395  1.11       dsl 	char *cp;
    396  1.11       dsl 	const struct option *opt;
    397  1.11       dsl 	int len;
    398  1.12       dsl 	unsigned long val;
    399   1.1     lukem 
    400   1.1     lukem 	assert(params != NULL);
    401   1.1     lukem 	assert(option != NULL);
    402   1.1     lukem 
    403  1.11       dsl 	for (;; option += len) {
    404  1.11       dsl 		option += strspn(option, ", \t");
    405  1.11       dsl 		if (*option == 0)
    406  1.11       dsl 			return;
    407  1.11       dsl 		len = strcspn(option, "=,");
    408  1.11       dsl 		for (opt = options; opt->name != NULL; opt++) {
    409  1.11       dsl 			if (memcmp(option, opt->name, len) == 0
    410  1.11       dsl 			    && opt->name[len] == 0)
    411  1.11       dsl 				break;
    412  1.11       dsl 		}
    413  1.11       dsl 		if (opt->name == NULL) {
    414  1.11       dsl 			len = strcspn(option, ",");
    415  1.11       dsl 			warnx("Unknown option `-o %.*s'", len, option);
    416  1.11       dsl 			break;
    417  1.11       dsl 		}
    418  1.11       dsl 		params->flags |= opt->flag;
    419  1.11       dsl 		if (opt->type == OPT_BOOL) {
    420  1.11       dsl 			if (option[len] != '=')
    421  1.11       dsl 				continue;
    422  1.11       dsl 			warnx("Option `%s' must not have a value", opt->name);
    423  1.11       dsl 			break;
    424  1.11       dsl 		}
    425  1.11       dsl 		if (option[len] != '=') {
    426  1.11       dsl 			warnx("Option `%s' must have a value", opt->name);
    427  1.11       dsl 			break;
    428   1.1     lukem 		}
    429  1.11       dsl 		option += len + 1;
    430  1.11       dsl 		len = strcspn(option, ",");
    431  1.11       dsl 		switch (opt->type) {
    432  1.11       dsl 		case OPT_STRING:
    433  1.11       dsl 			len = strlen(option);
    434  1.11       dsl 			/* FALLTHROUGH */
    435  1.11       dsl 		case OPT_WORD:
    436  1.11       dsl 			cp = strdup(option);
    437  1.11       dsl 			if (cp == NULL)
    438  1.11       dsl 				err(1, "strdup");
    439  1.11       dsl 			cp[len] = 0;
    440  1.11       dsl 			OPTION(params, char *, opt) = cp;
    441  1.11       dsl 			continue;
    442  1.11       dsl 		case OPT_INT:
    443  1.11       dsl 			val = strtoul(option, &cp, 0);
    444  1.11       dsl 			if (cp > option + len || (*cp != 0 && *cp != ','))
    445  1.11       dsl 				break;
    446  1.31     lukem 			if (val > INT_MAX)
    447  1.11       dsl 				break;
    448  1.31     lukem 			OPTION(params, int, opt) = (int)val;
    449  1.11       dsl 			continue;
    450  1.11       dsl 		default:
    451  1.13    petrov 			errx(1, "Internal error: option `%s' has invalid type %d",
    452  1.11       dsl 				opt->name, opt->type);
    453  1.11       dsl 		}
    454  1.11       dsl 		warnx("Invalid option value `%s=%.*s'", opt->name, len, option);
    455  1.11       dsl 		break;
    456   1.1     lukem 	}
    457  1.11       dsl 	options_usage();
    458  1.11       dsl 	exit(1);
    459   1.3     lukem }
    460   1.3     lukem 
    461  1.11       dsl static void
    462  1.11       dsl options_usage(void)
    463   1.3     lukem {
    464  1.11       dsl 	int ndx;
    465  1.11       dsl 	const char *pfx;
    466   1.3     lukem 
    467  1.11       dsl 	warnx("Valid options are:");
    468  1.11       dsl 	pfx = "\t";
    469  1.11       dsl 	for (ndx = 0; options[ndx].name != 0; ndx++) {
    470  1.11       dsl 		fprintf(stderr, "%s%s", pfx, options[ndx].name);
    471  1.11       dsl 		switch (options[ndx].type) {
    472  1.11       dsl 		case OPT_INT:
    473  1.11       dsl 			fprintf(stderr, "=number");
    474  1.11       dsl 			break;
    475  1.11       dsl 		case OPT_WORD:
    476  1.11       dsl 			fprintf(stderr, "=word");
    477  1.11       dsl 			break;
    478  1.11       dsl 		case OPT_STRING:
    479  1.11       dsl 			fprintf(stderr, "=string");
    480  1.11       dsl 			break;
    481  1.11       dsl 		default:
    482  1.11       dsl 			break;
    483  1.11       dsl 		}
    484  1.11       dsl 		if ((ndx % 5) == 4)
    485  1.11       dsl 			pfx = ",\n\t";
    486  1.11       dsl 		else
    487  1.11       dsl 			pfx = ", ";
    488  1.11       dsl 	}
    489  1.11       dsl 	fprintf(stderr, "\n");
    490   1.3     lukem }
    491   1.3     lukem 
    492   1.3     lukem int
    493   1.3     lukem no_setboot(ib_params *params)
    494   1.3     lukem {
    495   1.3     lukem 
    496   1.7     lukem 	assert(params != NULL);
    497   1.7     lukem 
    498   1.5     lukem 	warnx("%s: bootstrap installation is not supported",
    499   1.3     lukem 	    params->machine->name);
    500   1.3     lukem 	return (0);
    501   1.3     lukem }
    502   1.3     lukem 
    503   1.3     lukem int
    504   1.3     lukem no_clearboot(ib_params *params)
    505   1.3     lukem {
    506   1.3     lukem 
    507   1.7     lukem 	assert(params != NULL);
    508   1.7     lukem 
    509   1.5     lukem 	warnx("%s: bootstrap removal is not supported",
    510   1.3     lukem 	    params->machine->name);
    511   1.1     lukem 	return (0);
    512   1.1     lukem }
    513   1.1     lukem 
    514  1.19       dsl int
    515  1.19       dsl no_editboot(ib_params *params)
    516  1.19       dsl {
    517  1.19       dsl 
    518  1.19       dsl 	assert(params != NULL);
    519  1.19       dsl 
    520  1.19       dsl 	warnx("%s: bootstrap editing is not supported",
    521  1.19       dsl 	    params->machine->name);
    522  1.19       dsl 	return (0);
    523  1.19       dsl }
    524  1.19       dsl 
    525   1.1     lukem 
    526  1.11       dsl static void
    527   1.1     lukem getmachine(ib_params *param, const char *mach, const char *provider)
    528   1.1     lukem {
    529   1.1     lukem 	int	i;
    530   1.1     lukem 
    531  1.11       dsl 	assert(param != NULL);
    532  1.11       dsl 	assert(mach != NULL);
    533  1.11       dsl 	assert(provider != NULL);
    534  1.11       dsl 
    535  1.23       dsl 	for (i = 0; machines[i] != NULL; i++) {
    536  1.24       dsl 		if (machines[i]->name == NULL)
    537  1.24       dsl 			continue;
    538  1.23       dsl 		if (strcmp(machines[i]->name, mach) == 0) {
    539  1.23       dsl 			param->machine = machines[i];
    540  1.11       dsl 			return;
    541   1.1     lukem 		}
    542   1.1     lukem 	}
    543  1.11       dsl 	warnx("Invalid machine `%s' from %s", mach, provider);
    544  1.11       dsl 	machine_usage();
    545  1.11       dsl 	exit(1);
    546  1.11       dsl }
    547  1.11       dsl 
    548  1.11       dsl static void
    549  1.11       dsl machine_usage(void)
    550  1.11       dsl {
    551  1.11       dsl 	const char *prefix;
    552  1.11       dsl 	int	i;
    553  1.24       dsl 	int col, len;
    554  1.24       dsl 	const char *name;
    555  1.25    dogcow 	int	wincol=80;
    556  1.25    dogcow #ifdef TIOCGWINSZ
    557  1.24       dsl 	struct winsize win;
    558  1.24       dsl 
    559  1.36    martin 	if (ioctl(fileno(stderr), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
    560  1.25    dogcow 		wincol = win.ws_col;
    561  1.25    dogcow #endif
    562  1.11       dsl 
    563   1.1     lukem 	warnx("Supported machines are:");
    564  1.24       dsl 	prefix="\t";
    565  1.24       dsl 	col = 8 + 3;
    566  1.23       dsl 	for (i = 0; machines[i] != NULL; i++) {
    567  1.24       dsl 		name = machines[i]->name;
    568  1.24       dsl 		if (name == NULL)
    569  1.24       dsl 			continue;
    570  1.24       dsl 		len = strlen(name);
    571  1.25    dogcow 		if (col + len > wincol) {
    572  1.10     lukem 			prefix=",\n\t";
    573  1.24       dsl 			col = -2 + 8 + 3;
    574  1.24       dsl 		}
    575  1.24       dsl 		col += fprintf(stderr, "%s%s", prefix, name);
    576  1.24       dsl 		prefix=", ";
    577   1.1     lukem 	}
    578  1.10     lukem 	fputs("\n", stderr);
    579   1.6     lukem }
    580   1.6     lukem 
    581  1.11       dsl static void
    582   1.6     lukem getfstype(ib_params *param, const char *fstype, const char *provider)
    583   1.6     lukem {
    584  1.11       dsl 	int i;
    585  1.11       dsl 
    586  1.11       dsl 	assert(param != NULL);
    587  1.11       dsl 	assert(fstype != NULL);
    588  1.11       dsl 	assert(provider != NULL);
    589  1.11       dsl 
    590  1.11       dsl 	for (i = 0; fstypes[i].name != NULL; i++) {
    591  1.11       dsl 		if (strcmp(fstypes[i].name, fstype) == 0) {
    592  1.11       dsl 			param->fstype = &fstypes[i];
    593  1.11       dsl 			return;
    594  1.11       dsl 		}
    595  1.11       dsl 	}
    596  1.11       dsl 	warnx("Invalid file system type `%s' from %s", fstype, provider);
    597  1.11       dsl 	fstype_usage();
    598  1.11       dsl 	exit(1);
    599  1.11       dsl }
    600  1.11       dsl 
    601  1.11       dsl static void
    602  1.11       dsl fstype_usage(void)
    603  1.11       dsl {
    604  1.36    martin #ifndef NO_STAGE2
    605  1.10     lukem 	const char *prefix;
    606   1.6     lukem 	int	i;
    607   1.6     lukem 
    608   1.6     lukem 	warnx("Supported file system types are:");
    609  1.10     lukem #define FSTYPES_PER_LINE	9
    610  1.24       dsl 	prefix="\t";
    611   1.6     lukem 	for (i = 0; fstypes[i].name != NULL; i++) {
    612  1.25    dogcow 		if (i && (i % FSTYPES_PER_LINE) == 0)
    613  1.10     lukem 			prefix=",\n\t";
    614  1.10     lukem 		fprintf(stderr, "%s%s", prefix, fstypes[i].name);
    615  1.24       dsl 		prefix=", ";
    616   1.6     lukem 	}
    617  1.10     lukem 	fputs("\n", stderr);
    618  1.36    martin #endif
    619   1.1     lukem }
    620   1.1     lukem 
    621   1.1     lukem static void
    622  1.41     brook getubootpaths(ib_params *param, const char *paths)
    623  1.41     brook {
    624  1.41     brook 	assert(param != NULL);
    625  1.41     brook 	assert(paths != NULL);
    626  1.41     brook 
    627  1.41     brook 	param->uboot_paths = paths;
    628  1.41     brook }
    629  1.41     brook 
    630  1.41     brook static void
    631   1.1     lukem usage(void)
    632   1.1     lukem {
    633   1.1     lukem 	const char	*prog;
    634   1.1     lukem 
    635   1.1     lukem 	prog = getprogname();
    636   1.1     lukem 	fprintf(stderr,
    637  1.28       dsl "usage: %s [-fnv] [-B s2bno] [-b s1bno] [-m machine] [-o options]\n"
    638  1.41     brook "\t\t   [-t fstype] [-u uboot-paths] filesystem primary [secondary]\n"
    639  1.28       dsl "usage: %s -c [-fnv] [-m machine] [-o options] [-t fstype] filesystem\n"
    640  1.28       dsl "usage: %s -e [-fnv] [-m machine] [-o options] bootstrap\n",
    641  1.19       dsl 	    prog, prog, prog);
    642  1.11       dsl 	machine_usage();
    643  1.11       dsl 	fstype_usage();
    644  1.11       dsl 	options_usage();
    645  1.40   thorpej 	if (installboot_params.machine != NULL &&
    646  1.40   thorpej 	    installboot_params.machine->usage != NULL)
    647  1.40   thorpej 		installboot_params.machine->usage(&installboot_params);
    648   1.1     lukem 	exit(1);
    649   1.1     lukem }
    650