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