xref: /netbsd-src/usr.sbin/installboot/installboot.c (revision 317b3a6922e38df0b3f25b346dbc20c03d529c7d)
1*317b3a69Sbrook /*	$NetBSD: installboot.c,v 1.41 2022/07/10 19:28:00 brook Exp $	*/
207e46180Slukem 
307e46180Slukem /*-
407e46180Slukem  * Copyright (c) 2002 The NetBSD Foundation, Inc.
507e46180Slukem  * All rights reserved.
607e46180Slukem  *
707e46180Slukem  * This code is derived from software contributed to The NetBSD Foundation
807e46180Slukem  * by Luke Mewburn of Wasabi Systems.
907e46180Slukem  *
1007e46180Slukem  * Redistribution and use in source and binary forms, with or without
1107e46180Slukem  * modification, are permitted provided that the following conditions
1207e46180Slukem  * are met:
1307e46180Slukem  * 1. Redistributions of source code must retain the above copyright
1407e46180Slukem  *    notice, this list of conditions and the following disclaimer.
1507e46180Slukem  * 2. Redistributions in binary form must reproduce the above copyright
1607e46180Slukem  *    notice, this list of conditions and the following disclaimer in the
1707e46180Slukem  *    documentation and/or other materials provided with the distribution.
1807e46180Slukem  *
1907e46180Slukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2007e46180Slukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2107e46180Slukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2207e46180Slukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2307e46180Slukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2407e46180Slukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2507e46180Slukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2607e46180Slukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2707e46180Slukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2807e46180Slukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2907e46180Slukem  * POSSIBILITY OF SUCH DAMAGE.
3007e46180Slukem  */
3107e46180Slukem 
32b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
33b2f78261Sjmc #include "nbtool_config.h"
34b2f78261Sjmc #endif
35b2f78261Sjmc 
3607e46180Slukem #include <sys/cdefs.h>
37e5f39b5eStsutsui #if !defined(__lint)
38*317b3a69Sbrook __RCSID("$NetBSD: installboot.c,v 1.41 2022/07/10 19:28:00 brook Exp $");
3907e46180Slukem #endif	/* !__lint */
4007e46180Slukem 
4118bcb9c1Smlelstv #include <sys/param.h>
42b22a0afaSdsl #include <sys/ioctl.h>
4307e46180Slukem #include <sys/utsname.h>
4407e46180Slukem 
4507e46180Slukem #include <assert.h>
4607e46180Slukem #include <err.h>
4707e46180Slukem #include <fcntl.h>
48d024fa98Ssimonb #include <limits.h>
4907e46180Slukem #include <stdio.h>
5007e46180Slukem #include <stdlib.h>
51ed45ba76Sdsl #include <stddef.h>
5207e46180Slukem #include <string.h>
5307e46180Slukem #include <unistd.h>
54182e6e3bSmlelstv #if !HAVE_NBTOOL_CONFIG_H
5518bcb9c1Smlelstv #include <util.h>
56182e6e3bSmlelstv #endif
5707e46180Slukem 
5807e46180Slukem #include "installboot.h"
5902d67d10Sthorpej #include "evboards.h"
6007e46180Slukem 
61ed45ba76Sdsl static	void	getmachine(ib_params *, const char *, const char *);
62ed45ba76Sdsl static	void	getfstype(ib_params *, const char *, const char *);
63*317b3a69Sbrook static	void	getubootpaths(ib_params *, const char *);
64ed45ba76Sdsl static	void	parseoptions(ib_params *, const char *);
65bec77c5fSjoerg __dead static	void	usage(void);
66ed45ba76Sdsl static	void	options_usage(void);
67ed45ba76Sdsl static	void	machine_usage(void);
68ed45ba76Sdsl static	void	fstype_usage(void);
6907e46180Slukem 
7007e46180Slukem static	ib_params	installboot_params;
7107e46180Slukem 
72ed45ba76Sdsl #define OFFSET(field)    offsetof(ib_params, field)
73ed45ba76Sdsl const struct option {
74ed45ba76Sdsl 	const char	*name;		/* Name of option */
75ed45ba76Sdsl 	ib_flags	flag;		/* Corresponding IB_xxx flag */
76ed45ba76Sdsl 	enum {				/* Type of option value... */
77ed45ba76Sdsl 		OPT_BOOL,		/* no value */
78ed45ba76Sdsl 		OPT_INT,		/* numeric value */
79ed45ba76Sdsl 		OPT_WORD,		/* space/tab/, terminated */
80ed45ba76Sdsl 		OPT_STRING		/* null terminated */
81ed45ba76Sdsl 	}		type;
82ed45ba76Sdsl 	int		offset;		/* of field in ib_params */
83ed45ba76Sdsl } options[] = {
84f80d108fSchristos 	{ "alphasum",	IB_ALPHASUM,	OPT_BOOL,	0 },
85f80d108fSchristos 	{ "append",	IB_APPEND,	OPT_BOOL,	0 },
86ed45ba76Sdsl 	{ "command",	IB_COMMAND,	OPT_STRING,	OFFSET(command) },
87ed45ba76Sdsl 	{ "console",	IB_CONSOLE,	OPT_WORD,	OFFSET(console) },
880b10fdb3Sdsl 	{ "ioaddr",	IB_CONSADDR,	OPT_INT,	OFFSET(consaddr) },
897b024ef6Sdsl 	{ "keymap",	IB_KEYMAP,	OPT_WORD,	OFFSET(keymap) },
90ed45ba76Sdsl 	{ "password",	IB_PASSWORD,	OPT_WORD,	OFFSET(password) },
91f80d108fSchristos 	{ "resetvideo",	IB_RESETVIDEO,	OPT_BOOL,	0 },
92ed45ba76Sdsl 	{ "speed",	IB_CONSPEED,	OPT_INT,	OFFSET(conspeed) },
93f80d108fSchristos 	{ "sunsum",	IB_SUNSUM,	OPT_BOOL,	0 },
94ed45ba76Sdsl 	{ "timeout",	IB_TIMEOUT,	OPT_INT,	OFFSET(timeout) },
9579f027bbSdrochner 	{ "modules",	IB_MODULES,	OPT_BOOL,	0 },
9679f027bbSdrochner 	{ "bootconf",	IB_BOOTCONF,	OPT_BOOL,	0 },
9702d67d10Sthorpej 	{ "board",	IB_BOARD,	OPT_STRING,	OFFSET(board) },
9802d67d10Sthorpej 	{ "dtb",	IB_DTB,		OPT_STRING,	OFFSET(dtb) },
9902d67d10Sthorpej 	{ "media",	IB_MEDIA,	OPT_WORD,	OFFSET(media) },
100f80d108fSchristos 	{ .name = NULL },
101ed45ba76Sdsl };
102ed45ba76Sdsl #undef OFFSET
103ed45ba76Sdsl #define OPTION(params, type, opt) (*(type *)((char *)(params) + (opt)->offset))
104ed45ba76Sdsl 
10517ad8eceStsutsui #define DFL_SECSIZE	512	/* Don't use DEV_BSIZE. It's host's value. */
10617ad8eceStsutsui 
107*317b3a69Sbrook #ifndef DEFAULT_UBOOT_PKG_PATH
108*317b3a69Sbrook #define	DEFAULT_UBOOT_PKG_PATH	"/usr/pkg/share/u-boot"
109*317b3a69Sbrook #endif
110*317b3a69Sbrook 
111*317b3a69Sbrook #ifndef UBOOT_PATHS_ENV_VAR
112*317b3a69Sbrook #define	UBOOT_PATHS_ENV_VAR	"INSTALLBOOT_UBOOT_PATHS"
113*317b3a69Sbrook #endif
114*317b3a69Sbrook 
11507e46180Slukem int
main(int argc,char * argv[])11607e46180Slukem main(int argc, char *argv[])
11707e46180Slukem {
11807e46180Slukem 	struct utsname	utsname;
11907e46180Slukem 	ib_params	*params;
1208eb8919eSlukem 	unsigned long	lval;
12107e46180Slukem 	int		ch, rv, mode;
12207e46180Slukem 	char 		*p;
12307e46180Slukem 	const char	*op;
124ed45ba76Sdsl 	ib_flags	unsupported_flags;
125182e6e3bSmlelstv #if !HAVE_NBTOOL_CONFIG_H
12618bcb9c1Smlelstv 	char		specname[MAXPATHLEN];
12718bcb9c1Smlelstv 	char		rawname[MAXPATHLEN];
12818bcb9c1Smlelstv 	const char	*special, *raw;
129182e6e3bSmlelstv #endif
13007e46180Slukem 
13107e46180Slukem 	setprogname(argv[0]);
13207e46180Slukem 	params = &installboot_params;
13307e46180Slukem 	memset(params, 0, sizeof(*params));
1341bdd92eeSlukem 	params->fsfd = -1;
1351bdd92eeSlukem 	params->s1fd = -1;
13607e46180Slukem 	if ((p = getenv("MACHINE")) != NULL)
137ed45ba76Sdsl 		getmachine(params, p, "$MACHINE");
138*317b3a69Sbrook 	getubootpaths(params, DEFAULT_UBOOT_PKG_PATH);
139*317b3a69Sbrook 	if ((p = getenv(UBOOT_PATHS_ENV_VAR)) != NULL) {
140*317b3a69Sbrook 		getubootpaths(params, p);
141*317b3a69Sbrook 	}
14207e46180Slukem 
143*317b3a69Sbrook 	while ((ch = getopt(argc, argv, "b:B:cefm:no:t:u:v")) != -1) {
14407e46180Slukem 		switch (ch) {
14507e46180Slukem 
14607e46180Slukem 		case 'b':
1471bdd92eeSlukem 		case 'B':
14807e46180Slukem 			if (*optarg == '\0')
14907e46180Slukem 				goto badblock;
1508eb8919eSlukem 			lval = strtoul(optarg, &p, 0);
1518eb8919eSlukem 			if (lval > UINT32_MAX || *p != '\0') {
15207e46180Slukem  badblock:
15307e46180Slukem 				errx(1, "Invalid block number `%s'", optarg);
15407e46180Slukem 			}
1551bdd92eeSlukem 			if (ch == 'b') {
1561bdd92eeSlukem 				params->s1start = (uint32_t)lval;
1571bdd92eeSlukem 				params->flags |= IB_STAGE1START;
1581bdd92eeSlukem 			} else {
1591bdd92eeSlukem 				params->s2start = (uint32_t)lval;
1601bdd92eeSlukem 				params->flags |= IB_STAGE2START;
1611bdd92eeSlukem 			}
16207e46180Slukem 			break;
16307e46180Slukem 
16407e46180Slukem 		case 'c':
16507e46180Slukem 			params->flags |= IB_CLEAR;
16607e46180Slukem 			break;
16707e46180Slukem 
1688c893c96Sdsl 		case 'e':
1698c893c96Sdsl 			params->flags |= IB_EDIT;
1708c893c96Sdsl 			break;
1718c893c96Sdsl 
172486d4119Sdsl 		case 'f':
173486d4119Sdsl 			params->flags |= IB_FORCE;
174486d4119Sdsl 			break;
175486d4119Sdsl 
17607e46180Slukem 		case 'm':
177ed45ba76Sdsl 			getmachine(params, optarg, "-m");
17807e46180Slukem 			break;
17907e46180Slukem 
18007e46180Slukem 		case 'n':
18107e46180Slukem 			params->flags |= IB_NOWRITE;
18207e46180Slukem 			break;
18307e46180Slukem 
18407e46180Slukem 		case 'o':
185ed45ba76Sdsl 			parseoptions(params, optarg);
18607e46180Slukem 			break;
18707e46180Slukem 
18807e46180Slukem 		case 't':
189ed45ba76Sdsl 			getfstype(params, optarg, "-t");
19007e46180Slukem 			break;
19107e46180Slukem 
192*317b3a69Sbrook 		case 'u':
193*317b3a69Sbrook 			getubootpaths(params, optarg);
194*317b3a69Sbrook 			break;
195*317b3a69Sbrook 
19607e46180Slukem 		case 'v':
19707e46180Slukem 			params->flags |= IB_VERBOSE;
19807e46180Slukem 			break;
19907e46180Slukem 
20007e46180Slukem 		case '?':
20107e46180Slukem 		default:
20207e46180Slukem 			usage();
20307e46180Slukem 			/* NOTREACHED */
20407e46180Slukem 
20507e46180Slukem 		}
20607e46180Slukem 	}
20707e46180Slukem 	argc -= optind;
20807e46180Slukem 	argv += optind;
20907e46180Slukem 
2108c893c96Sdsl 	if (params->flags & IB_CLEAR && params->flags & IB_EDIT)
2118c893c96Sdsl 		usage();
2128c893c96Sdsl 	if (argc < 1 || argc + 2 * !!(params->flags & (IB_CLEAR | IB_EDIT)) > 3)
21307e46180Slukem 		usage();
21407e46180Slukem 
21507e46180Slukem 	/* set missing defaults */
21607e46180Slukem 	if (params->machine == NULL) {
21707e46180Slukem 		if (uname(&utsname) == -1)
21807e46180Slukem 			err(1, "Determine uname");
219ed45ba76Sdsl 		getmachine(params, utsname.machine, "uname()");
220ed45ba76Sdsl 	}
221ed45ba76Sdsl 
222ed45ba76Sdsl 	/* Check that options are supported by this system */
223ed45ba76Sdsl 	unsupported_flags = params->flags & ~params->machine->valid_flags;
224f2ccbd03Sdsl 	unsupported_flags &= ~(IB_VERBOSE | IB_NOWRITE | IB_CLEAR | IB_EDIT
225f2ccbd03Sdsl 				| IB_FORCE);
226ed45ba76Sdsl 	if (unsupported_flags != 0) {
227ed45ba76Sdsl 		int ndx;
228ed45ba76Sdsl 		for (ndx = 0; options[ndx].name != NULL; ndx++) {
229f2ccbd03Sdsl 			if (unsupported_flags & options[ndx].flag) {
230f2ccbd03Sdsl 				unsupported_flags &= ~options[ndx].flag;
231ed45ba76Sdsl 				warnx("`-o %s' is not supported for %s",
232ed45ba76Sdsl 				    options[ndx].name, params->machine->name);
233ed45ba76Sdsl 			}
234f2ccbd03Sdsl 		}
235ed45ba76Sdsl 		if (unsupported_flags & IB_STAGE1START)
236ed45ba76Sdsl 			warnx("`-b bno' is not supported for %s",
237ed45ba76Sdsl 			    params->machine->name);
238ed45ba76Sdsl 		if (unsupported_flags & IB_STAGE2START)
239ed45ba76Sdsl 			warnx("`-B bno' is not supported for %s",
240ed45ba76Sdsl 			    params->machine->name);
241f2ccbd03Sdsl 		unsupported_flags &= ~(IB_STAGE1START | IB_STAGE2START);
242f2ccbd03Sdsl 		if (unsupported_flags != 0)
243f2ccbd03Sdsl 			warnx("Unknown unsupported flag %#x (coding error!)",
244f2ccbd03Sdsl 			    unsupported_flags);
245ed45ba76Sdsl 		exit(1);
246ed45ba76Sdsl 	}
247ed45ba76Sdsl 	/* and some illegal combinations */
248ed45ba76Sdsl 	if (params->flags & IB_STAGE1START && params->flags & IB_APPEND) {
249ed45ba76Sdsl 		warnx("Can't use `-b bno' with `-o append'");
250ed45ba76Sdsl 		exit(1);
251ed45ba76Sdsl 	}
252ed45ba76Sdsl 	if (params->flags & IB_CLEAR &&
253ed45ba76Sdsl 	    params->flags & (IB_STAGE1START | IB_STAGE2START | IB_APPEND)) {
254ed45ba76Sdsl 		warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
25507e46180Slukem 		exit(1);
25607e46180Slukem 	}
25707e46180Slukem 
258e0e1f6caSdsl 	if (argc >= 3) {
259e0e1f6caSdsl 		params->stage2 = argv[2];
260e0e1f6caSdsl 	}
261e0e1f6caSdsl 
262182e6e3bSmlelstv #if !HAVE_NBTOOL_CONFIG_H
26318bcb9c1Smlelstv 	special = getfsspecname(specname, sizeof(specname), argv[0]);
2641cd8415fSmlelstv 	if (special == NULL)
2651cd8415fSmlelstv 		err(1, "%s: %s", argv[0], specname);
26618bcb9c1Smlelstv 	raw = getdiskrawname(rawname, sizeof(rawname), special);
26718bcb9c1Smlelstv 	if (raw != NULL)
26818bcb9c1Smlelstv 		special = raw;
26918bcb9c1Smlelstv 	params->filesystem = special;
270182e6e3bSmlelstv #else
271182e6e3bSmlelstv 	params->filesystem = argv[0];
272182e6e3bSmlelstv #endif
273182e6e3bSmlelstv 
27407e46180Slukem 	if (params->flags & IB_NOWRITE) {
27507e46180Slukem 		op = "only";
27607e46180Slukem 		mode = O_RDONLY;
27707e46180Slukem 	} else {
27807e46180Slukem 		op = "write";
27907e46180Slukem 		mode = O_RDWR;
28007e46180Slukem 	}
28117ad8eceStsutsui 	/* XXX should be specified via option */
28217ad8eceStsutsui 	params->sectorsize = DFL_SECSIZE;
28307e46180Slukem 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
28407e46180Slukem 		err(1, "Opening file system `%s' read-%s",
28507e46180Slukem 		    params->filesystem, op);
286932b7524Slukem 	if (fstat(params->fsfd, &params->fsstat) == -1)
287932b7524Slukem 		err(1, "Examining file system `%s'", params->filesystem);
2888eb8919eSlukem 	if (params->fstype != NULL) {
2898eb8919eSlukem 		if (! params->fstype->match(params))
290723dc0caSisaki 			errx(1, "File system `%s' is not of type %s",
2918eb8919eSlukem 			    params->filesystem, params->fstype->name);
2928eb8919eSlukem 	} else {
293e0e1f6caSdsl 		if (params->stage2 != NULL) {
2948eb8919eSlukem 			params->fstype = &fstypes[0];
2958eb8919eSlukem 			while (params->fstype->name != NULL &&
2968eb8919eSlukem 				    !params->fstype->match(params))
2978eb8919eSlukem 				params->fstype++;
2988eb8919eSlukem 			if (params->fstype->name == NULL)
2991a478e40Slukem 				errx(1, "File system `%s' is of an unknown type",
3008eb8919eSlukem 				    params->filesystem);
3018eb8919eSlukem 		}
302e0e1f6caSdsl 	}
30307e46180Slukem 
30402d67d10Sthorpej 	assert(params->machine != NULL);
30502d67d10Sthorpej 
306d2804426Slukem 	if (argc >= 2) {
30798a919c1Schristos 		if ((params->s1fd = open(argv[1], O_RDONLY, 0600)) == -1)
30898a919c1Schristos 			err(1, "Opening primary bootstrap `%s'", argv[1]);
309932b7524Slukem 		if (fstat(params->s1fd, &params->s1stat) == -1)
31098a919c1Schristos 			err(1, "Examining primary bootstrap `%s'", argv[1]);
31102d67d10Sthorpej 		if (!S_ISREG(params->s1stat.st_mode)) {
31202d67d10Sthorpej 			/*
31302d67d10Sthorpej 			 * If the platform uses u-boot, then the stage1
31402d67d10Sthorpej 			 * spec might be the directory where the u-boot
31502d67d10Sthorpej 			 * binaries for the system are located.
31602d67d10Sthorpej 			 */
31702d67d10Sthorpej 			if (params->machine->mach_flags & MF_UBOOT) {
31802d67d10Sthorpej 				if (!S_ISDIR(params->s1stat.st_mode)) {
31902d67d10Sthorpej 					errx(1, "`%s' must be a regular file "
32002d67d10Sthorpej 					     "or a directory", argv[1]);
32102d67d10Sthorpej 				}
32202d67d10Sthorpej 				(void) close(params->s1fd);
32302d67d10Sthorpej 				params->s1fd = -1;
32402d67d10Sthorpej 			} else {
32598a919c1Schristos 				errx(1, "`%s' must be a regular file", argv[1]);
32602d67d10Sthorpej 			}
32702d67d10Sthorpej 		}
32898a919c1Schristos 		params->stage1 = argv[1];
329d2804426Slukem 	}
33007e46180Slukem 
33107e46180Slukem 	if (params->flags & IB_VERBOSE) {
33207e46180Slukem 		printf("File system:         %s\n", params->filesystem);
333e0e1f6caSdsl 		if (params->fstype)
334e0e1f6caSdsl 			printf("File system type:    %s (blocksize %u, "
335e0e1f6caSdsl 				"needswap %d)\n",
336e0e1f6caSdsl 			    params->fstype->name, params->fstype->blocksize,
337e0e1f6caSdsl 			    params->fstype->needswap);
3388c893c96Sdsl 		if (!(params->flags & IB_EDIT))
33902d67d10Sthorpej 			printf("Primary bootstrap:   %s%s\n",
34007e46180Slukem 			    (params->flags & IB_CLEAR) ? "(to be cleared)"
34102d67d10Sthorpej 			    : params->stage1 ? params->stage1 : "(none)",
34202d67d10Sthorpej 			    S_ISDIR(params->s1stat.st_mode) ? " (directory)"
34302d67d10Sthorpej 			    				    : "");
344d2804426Slukem 		if (params->stage2 != NULL)
345d2804426Slukem 			printf("Secondary bootstrap: %s\n", params->stage2);
34607e46180Slukem 	}
34707e46180Slukem 
3488c893c96Sdsl 	if (params->flags & IB_EDIT) {
3498c893c96Sdsl 		op = "Edit";
3508c893c96Sdsl 		rv = params->machine->editboot(params);
3518c893c96Sdsl 	} else if (params->flags & IB_CLEAR) {
35207e46180Slukem 		op = "Clear";
35307e46180Slukem 		rv = params->machine->clearboot(params);
35407e46180Slukem 	} else {
35502d67d10Sthorpej 		if (argc < 2) {
35602d67d10Sthorpej 			/*
35702d67d10Sthorpej 			 * If the platform uses u-boot, then the stage1 spec is
35802d67d10Sthorpej 			 * optional iff they specified a board (because we can
35902d67d10Sthorpej 			 * infer a default location for u-boot binaries if the
36002d67d10Sthorpej 			 * board type is given).
36102d67d10Sthorpej 			 */
36202d67d10Sthorpej 			if (!(params->machine->mach_flags & MF_UBOOT)) {
36302d67d10Sthorpej 				errx(EXIT_FAILURE,
36402d67d10Sthorpej 				   "Please specify the primary bootstrap file");
36502d67d10Sthorpej 			}
36602d67d10Sthorpej 		}
36707e46180Slukem 		op = "Set";
36807e46180Slukem 		rv = params->machine->setboot(params);
36907e46180Slukem 	}
37007e46180Slukem 	if (rv == 0)
371d2804426Slukem 		errx(1, "%s bootstrap operation failed", op);
37207e46180Slukem 
373932b7524Slukem 	if (S_ISREG(params->fsstat.st_mode)) {
374050f6eecSlukem 		if (fsync(params->fsfd) == -1)
375932b7524Slukem 			err(1, "Synchronising file system `%s'",
376932b7524Slukem 			    params->filesystem);
377932b7524Slukem 	} else {
378932b7524Slukem 		/* Sync filesystems (to clean in-memory superblock?) */
379932b7524Slukem 		sync();
380932b7524Slukem 	}
38107e46180Slukem 	if (close(params->fsfd) == -1)
38207e46180Slukem 		err(1, "Closing file system `%s'", params->filesystem);
38302d67d10Sthorpej 	if (params->s1fd != -1)
384d2804426Slukem 		if (close(params->s1fd) == -1)
385d2804426Slukem 			err(1, "Closing primary bootstrap `%s'",
386d2804426Slukem 			    params->stage1);
38707e46180Slukem 
38807e46180Slukem 	exit(0);
38907e46180Slukem 	/* NOTREACHED */
39007e46180Slukem }
39107e46180Slukem 
392ed45ba76Sdsl static void
parseoptions(ib_params * params,const char * option)393ed45ba76Sdsl parseoptions(ib_params *params, const char *option)
39407e46180Slukem {
395ed45ba76Sdsl 	char *cp;
396ed45ba76Sdsl 	const struct option *opt;
397ed45ba76Sdsl 	int len;
398c4582ac8Sdsl 	unsigned long val;
39907e46180Slukem 
40007e46180Slukem 	assert(params != NULL);
40107e46180Slukem 	assert(option != NULL);
40207e46180Slukem 
403ed45ba76Sdsl 	for (;; option += len) {
404ed45ba76Sdsl 		option += strspn(option, ", \t");
405ed45ba76Sdsl 		if (*option == 0)
406ed45ba76Sdsl 			return;
407ed45ba76Sdsl 		len = strcspn(option, "=,");
408ed45ba76Sdsl 		for (opt = options; opt->name != NULL; opt++) {
409ed45ba76Sdsl 			if (memcmp(option, opt->name, len) == 0
410ed45ba76Sdsl 			    && opt->name[len] == 0)
411ed45ba76Sdsl 				break;
41207e46180Slukem 		}
413ed45ba76Sdsl 		if (opt->name == NULL) {
414ed45ba76Sdsl 			len = strcspn(option, ",");
415ed45ba76Sdsl 			warnx("Unknown option `-o %.*s'", len, option);
416ed45ba76Sdsl 			break;
41707e46180Slukem 		}
418ed45ba76Sdsl 		params->flags |= opt->flag;
419ed45ba76Sdsl 		if (opt->type == OPT_BOOL) {
420ed45ba76Sdsl 			if (option[len] != '=')
421ed45ba76Sdsl 				continue;
422ed45ba76Sdsl 			warnx("Option `%s' must not have a value", opt->name);
423ed45ba76Sdsl 			break;
424ed45ba76Sdsl 		}
425ed45ba76Sdsl 		if (option[len] != '=') {
426ed45ba76Sdsl 			warnx("Option `%s' must have a value", opt->name);
427ed45ba76Sdsl 			break;
428ed45ba76Sdsl 		}
429ed45ba76Sdsl 		option += len + 1;
430ed45ba76Sdsl 		len = strcspn(option, ",");
431ed45ba76Sdsl 		switch (opt->type) {
432ed45ba76Sdsl 		case OPT_STRING:
433ed45ba76Sdsl 			len = strlen(option);
434ed45ba76Sdsl 			/* FALLTHROUGH */
435ed45ba76Sdsl 		case OPT_WORD:
436ed45ba76Sdsl 			cp = strdup(option);
437ed45ba76Sdsl 			if (cp == NULL)
438ed45ba76Sdsl 				err(1, "strdup");
439ed45ba76Sdsl 			cp[len] = 0;
440ed45ba76Sdsl 			OPTION(params, char *, opt) = cp;
441ed45ba76Sdsl 			continue;
442ed45ba76Sdsl 		case OPT_INT:
443ed45ba76Sdsl 			val = strtoul(option, &cp, 0);
444ed45ba76Sdsl 			if (cp > option + len || (*cp != 0 && *cp != ','))
445ed45ba76Sdsl 				break;
4462b2f4703Slukem 			if (val > INT_MAX)
447ed45ba76Sdsl 				break;
4482b2f4703Slukem 			OPTION(params, int, opt) = (int)val;
449ed45ba76Sdsl 			continue;
450ed45ba76Sdsl 		default:
451674b7e97Spetrov 			errx(1, "Internal error: option `%s' has invalid type %d",
452ed45ba76Sdsl 				opt->name, opt->type);
453ed45ba76Sdsl 		}
454ed45ba76Sdsl 		warnx("Invalid option value `%s=%.*s'", opt->name, len, option);
455ed45ba76Sdsl 		break;
456ed45ba76Sdsl 	}
457ed45ba76Sdsl 	options_usage();
458ed45ba76Sdsl 	exit(1);
45907e46180Slukem }
46007e46180Slukem 
461ed45ba76Sdsl static void
options_usage(void)462ed45ba76Sdsl options_usage(void)
46338cd7942Slukem {
464ed45ba76Sdsl 	int ndx;
465ed45ba76Sdsl 	const char *pfx;
46638cd7942Slukem 
467ed45ba76Sdsl 	warnx("Valid options are:");
468ed45ba76Sdsl 	pfx = "\t";
469ed45ba76Sdsl 	for (ndx = 0; options[ndx].name != 0; ndx++) {
470ed45ba76Sdsl 		fprintf(stderr, "%s%s", pfx, options[ndx].name);
471ed45ba76Sdsl 		switch (options[ndx].type) {
472ed45ba76Sdsl 		case OPT_INT:
473ed45ba76Sdsl 			fprintf(stderr, "=number");
474ed45ba76Sdsl 			break;
475ed45ba76Sdsl 		case OPT_WORD:
476ed45ba76Sdsl 			fprintf(stderr, "=word");
477ed45ba76Sdsl 			break;
478ed45ba76Sdsl 		case OPT_STRING:
479ed45ba76Sdsl 			fprintf(stderr, "=string");
480ed45ba76Sdsl 			break;
481ed45ba76Sdsl 		default:
482ed45ba76Sdsl 			break;
483ed45ba76Sdsl 		}
484ed45ba76Sdsl 		if ((ndx % 5) == 4)
485ed45ba76Sdsl 			pfx = ",\n\t";
486ed45ba76Sdsl 		else
487ed45ba76Sdsl 			pfx = ", ";
488ed45ba76Sdsl 	}
489ed45ba76Sdsl 	fprintf(stderr, "\n");
49038cd7942Slukem }
49138cd7942Slukem 
49238cd7942Slukem int
no_setboot(ib_params * params)49338cd7942Slukem no_setboot(ib_params *params)
49438cd7942Slukem {
49538cd7942Slukem 
4961a478e40Slukem 	assert(params != NULL);
4971a478e40Slukem 
498d2804426Slukem 	warnx("%s: bootstrap installation is not supported",
49938cd7942Slukem 	    params->machine->name);
50038cd7942Slukem 	return (0);
50138cd7942Slukem }
50238cd7942Slukem 
50338cd7942Slukem int
no_clearboot(ib_params * params)50438cd7942Slukem no_clearboot(ib_params *params)
50538cd7942Slukem {
50638cd7942Slukem 
5071a478e40Slukem 	assert(params != NULL);
5081a478e40Slukem 
509d2804426Slukem 	warnx("%s: bootstrap removal is not supported",
51038cd7942Slukem 	    params->machine->name);
51138cd7942Slukem 	return (0);
51238cd7942Slukem }
51338cd7942Slukem 
5148c893c96Sdsl int
no_editboot(ib_params * params)5158c893c96Sdsl no_editboot(ib_params *params)
5168c893c96Sdsl {
5178c893c96Sdsl 
5188c893c96Sdsl 	assert(params != NULL);
5198c893c96Sdsl 
5208c893c96Sdsl 	warnx("%s: bootstrap editing is not supported",
5218c893c96Sdsl 	    params->machine->name);
5228c893c96Sdsl 	return (0);
5238c893c96Sdsl }
5248c893c96Sdsl 
52507e46180Slukem 
526ed45ba76Sdsl static void
getmachine(ib_params * param,const char * mach,const char * provider)52707e46180Slukem getmachine(ib_params *param, const char *mach, const char *provider)
52807e46180Slukem {
529ed45ba76Sdsl 	int	i;
530ed45ba76Sdsl 
531ed45ba76Sdsl 	assert(param != NULL);
532ed45ba76Sdsl 	assert(mach != NULL);
533ed45ba76Sdsl 	assert(provider != NULL);
534ed45ba76Sdsl 
535cce659e2Sdsl 	for (i = 0; machines[i] != NULL; i++) {
536b22a0afaSdsl 		if (machines[i]->name == NULL)
537b22a0afaSdsl 			continue;
538cce659e2Sdsl 		if (strcmp(machines[i]->name, mach) == 0) {
539cce659e2Sdsl 			param->machine = machines[i];
540ed45ba76Sdsl 			return;
541ed45ba76Sdsl 		}
542ed45ba76Sdsl 	}
543ed45ba76Sdsl 	warnx("Invalid machine `%s' from %s", mach, provider);
544ed45ba76Sdsl 	machine_usage();
545ed45ba76Sdsl 	exit(1);
546ed45ba76Sdsl }
547ed45ba76Sdsl 
548ed45ba76Sdsl static void
machine_usage(void)549ed45ba76Sdsl machine_usage(void)
550ed45ba76Sdsl {
5512a775954Slukem 	const char *prefix;
55207e46180Slukem 	int	i;
553b22a0afaSdsl 	int col, len;
554b22a0afaSdsl 	const char *name;
5554c1a8284Sdogcow 	int	wincol=80;
5564c1a8284Sdogcow #ifdef TIOCGWINSZ
557b22a0afaSdsl 	struct winsize win;
558b22a0afaSdsl 
55977a3a1eeSmartin 	if (ioctl(fileno(stderr), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
5604c1a8284Sdogcow 		wincol = win.ws_col;
5614c1a8284Sdogcow #endif
56207e46180Slukem 
5632a775954Slukem 	warnx("Supported machines are:");
5642a775954Slukem 	prefix="\t";
565b22a0afaSdsl 	col = 8 + 3;
566b22a0afaSdsl 	for (i = 0; machines[i] != NULL; i++) {
567b22a0afaSdsl 		name = machines[i]->name;
568b22a0afaSdsl 		if (name == NULL)
569b22a0afaSdsl 			continue;
570b22a0afaSdsl 		len = strlen(name);
5714c1a8284Sdogcow 		if (col + len > wincol) {
5722a775954Slukem 			prefix=",\n\t";
573b22a0afaSdsl 			col = -2 + 8 + 3;
574b22a0afaSdsl 		}
575b22a0afaSdsl 		col += fprintf(stderr, "%s%s", prefix, name);
576b22a0afaSdsl 		prefix=", ";
5772a775954Slukem 	}
57807e46180Slukem 	fputs("\n", stderr);
57907e46180Slukem }
58007e46180Slukem 
581ed45ba76Sdsl static void
getfstype(ib_params * param,const char * fstype,const char * provider)5828eb8919eSlukem getfstype(ib_params *param, const char *fstype, const char *provider)
5838eb8919eSlukem {
584ed45ba76Sdsl 	int i;
585ed45ba76Sdsl 
586ed45ba76Sdsl 	assert(param != NULL);
587ed45ba76Sdsl 	assert(fstype != NULL);
588ed45ba76Sdsl 	assert(provider != NULL);
589ed45ba76Sdsl 
590ed45ba76Sdsl 	for (i = 0; fstypes[i].name != NULL; i++) {
591ed45ba76Sdsl 		if (strcmp(fstypes[i].name, fstype) == 0) {
592ed45ba76Sdsl 			param->fstype = &fstypes[i];
593ed45ba76Sdsl 			return;
594ed45ba76Sdsl 		}
595ed45ba76Sdsl 	}
596ed45ba76Sdsl 	warnx("Invalid file system type `%s' from %s", fstype, provider);
597ed45ba76Sdsl 	fstype_usage();
598ed45ba76Sdsl 	exit(1);
599ed45ba76Sdsl }
600ed45ba76Sdsl 
601ed45ba76Sdsl static void
fstype_usage(void)602ed45ba76Sdsl fstype_usage(void)
603ed45ba76Sdsl {
60477a3a1eeSmartin #ifndef NO_STAGE2
6052a775954Slukem 	const char *prefix;
6068eb8919eSlukem 	int	i;
6078eb8919eSlukem 
6082a775954Slukem 	warnx("Supported file system types are:");
6092a775954Slukem #define FSTYPES_PER_LINE	9
6102a775954Slukem 	prefix="\t";
611b22a0afaSdsl 	for (i = 0; fstypes[i].name != NULL; i++) {
6124c1a8284Sdogcow 		if (i && (i % FSTYPES_PER_LINE) == 0)
6132a775954Slukem 			prefix=",\n\t";
6142a775954Slukem 		fprintf(stderr, "%s%s", prefix, fstypes[i].name);
615b22a0afaSdsl 		prefix=", ";
6162a775954Slukem 	}
6178eb8919eSlukem 	fputs("\n", stderr);
61877a3a1eeSmartin #endif
6198eb8919eSlukem }
6208eb8919eSlukem 
62107e46180Slukem static void
getubootpaths(ib_params * param,const char * paths)622*317b3a69Sbrook getubootpaths(ib_params *param, const char *paths)
623*317b3a69Sbrook {
624*317b3a69Sbrook 	assert(param != NULL);
625*317b3a69Sbrook 	assert(paths != NULL);
626*317b3a69Sbrook 
627*317b3a69Sbrook 	param->uboot_paths = paths;
628*317b3a69Sbrook }
629*317b3a69Sbrook 
630*317b3a69Sbrook static void
usage(void)63107e46180Slukem usage(void)
63207e46180Slukem {
63307e46180Slukem 	const char	*prog;
63407e46180Slukem 
63507e46180Slukem 	prog = getprogname();
63607e46180Slukem 	fprintf(stderr,
637486d4119Sdsl "usage: %s [-fnv] [-B s2bno] [-b s1bno] [-m machine] [-o options]\n"
638*317b3a69Sbrook "\t\t   [-t fstype] [-u uboot-paths] filesystem primary [secondary]\n"
639486d4119Sdsl "usage: %s -c [-fnv] [-m machine] [-o options] [-t fstype] filesystem\n"
640486d4119Sdsl "usage: %s -e [-fnv] [-m machine] [-o options] bootstrap\n",
6418c893c96Sdsl 	    prog, prog, prog);
642ed45ba76Sdsl 	machine_usage();
643ed45ba76Sdsl 	fstype_usage();
644ed45ba76Sdsl 	options_usage();
64502d67d10Sthorpej 	if (installboot_params.machine != NULL &&
64602d67d10Sthorpej 	    installboot_params.machine->usage != NULL)
64702d67d10Sthorpej 		installboot_params.machine->usage(&installboot_params);
64807e46180Slukem 	exit(1);
64907e46180Slukem }
650