Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.8
      1 /*	$NetBSD: installboot.c,v 1.8 2002/05/14 06:18:51 lukem 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(__lint)
     41 __RCSID("$NetBSD: installboot.c,v 1.8 2002/05/14 06:18:51 lukem Exp $");
     42 #endif	/* !__lint */
     43 
     44 #include <sys/utsname.h>
     45 
     46 #include <assert.h>
     47 #include <err.h>
     48 #include <fcntl.h>
     49 #include <limits.h>
     50 #include <stdio.h>
     51 #include <stdint.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "installboot.h"
     57 
     58 int		main(int, char *[]);
     59 static	int	getmachine(ib_params *, const char *, const char *);
     60 static	int	getfstype(ib_params *, const char *, const char *);
     61 static	void	usage(void);
     62 
     63 static	ib_params	installboot_params;
     64 
     65 int
     66 main(int argc, char *argv[])
     67 {
     68 	struct utsname	utsname;
     69 	ib_params	*params;
     70 	unsigned long	lval;
     71 	int		ch, rv, mode;
     72 	char 		*p;
     73 	const char	*op;
     74 
     75 	setprogname(argv[0]);
     76 	params = &installboot_params;
     77 	memset(params, 0, sizeof(*params));
     78 	params->fsfd = -1;
     79 	params->s1fd = -1;
     80 	if ((p = getenv("MACHINE")) != NULL)
     81 		if (! getmachine(params, p, "$MACHINE"))
     82 			exit(1);
     83 
     84 	while ((ch = getopt(argc, argv, "b:B:cm:no:t:v")) != -1) {
     85 		switch (ch) {
     86 
     87 		case 'b':
     88 		case 'B':
     89 			if (*optarg == '\0')
     90 				goto badblock;
     91 			lval = strtoul(optarg, &p, 0);
     92 			if (lval > UINT32_MAX || *p != '\0') {
     93  badblock:
     94 				errx(1, "Invalid block number `%s'", optarg);
     95 			}
     96 			if (ch == 'b') {
     97 				params->s1start = (uint32_t)lval;
     98 				params->flags |= IB_STAGE1START;
     99 			} else {
    100 				params->s2start = (uint32_t)lval;
    101 				params->flags |= IB_STAGE2START;
    102 			}
    103 			break;
    104 
    105 		case 'c':
    106 			params->flags |= IB_CLEAR;
    107 			break;
    108 
    109 		case 'm':
    110 			if (! getmachine(params, optarg, "-m"))
    111 				exit(1);
    112 			break;
    113 
    114 		case 'n':
    115 			params->flags |= IB_NOWRITE;
    116 			break;
    117 
    118 		case 'o':
    119 			if (params->machine == NULL)
    120 				errx(1,
    121 				    "Machine needs to be specified before -o");
    122 			while ((p = strsep(&optarg, ",")) != NULL) {
    123 				if (*p == '\0')
    124 					errx(1, "Empty `-o' option");
    125 				if (! params->machine->parseopt(params, p))
    126 					exit(1);
    127 			}
    128 			break;
    129 
    130 		case 't':
    131 			if (! getfstype(params, optarg, "-t"))
    132 				exit(1);
    133 			break;
    134 
    135 		case 'v':
    136 			params->flags |= IB_VERBOSE;
    137 			break;
    138 
    139 		case '?':
    140 		default:
    141 			usage();
    142 			/* NOTREACHED */
    143 
    144 		}
    145 	}
    146 	argc -= optind;
    147 	argv += optind;
    148 
    149 	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
    150 	    ((params->flags & IB_CLEAR) == 0 && (argc < 2 || argc > 3)))
    151 		usage();
    152 
    153 		/* set missing defaults */
    154 	if (params->machine == NULL) {
    155 		if (uname(&utsname) == -1)
    156 			err(1, "Determine uname");
    157 		if (! getmachine(params, utsname.machine, "uname()"))
    158 			exit(1);
    159 	}
    160 
    161 	params->filesystem = argv[0];
    162 	if (params->flags & IB_NOWRITE) {
    163 		op = "only";
    164 		mode = O_RDONLY;
    165 	} else {
    166 		op = "write";
    167 		mode = O_RDWR;
    168 	}
    169 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    170 		err(1, "Opening file system `%s' read-%s",
    171 		    params->filesystem, op);
    172 	if (params->fstype != NULL) {
    173 		if (! params->fstype->match(params))
    174 			err(1, "File system `%s' is not of type %s",
    175 			    params->filesystem, params->fstype->name);
    176 	} else {
    177 		params->fstype = &fstypes[0];
    178 		while (params->fstype->name != NULL &&
    179 			! params->fstype->match(params))
    180 			params->fstype++;
    181 		if (params->fstype->name == NULL)
    182 			errx(1, "File system `%s' is of an unknown type",
    183 			    params->filesystem);
    184 	}
    185 
    186 	if (argc >= 2) {
    187 		params->stage1 = argv[1];
    188 		if ((params->s1fd = open(params->stage1, O_RDONLY, 0600))
    189 		    == -1)
    190 			err(1, "Opening primary bootstrap `%s'",
    191 			    params->stage1);
    192 	}
    193 	if (argc == 3) {
    194 		params->stage2 = argv[2];
    195 	}
    196 	assert(params->machine != NULL);
    197 
    198 	if (params->flags & IB_VERBOSE) {
    199 		printf("File system:         %s\n", params->filesystem);
    200 		printf("File system type:    %s (blocksize %u, needswap %d)\n",
    201 		    params->fstype->name,
    202 		    params->fstype->blocksize, params->fstype->needswap);
    203 		printf("Primary bootstrap:   %s\n",
    204 		    (params->flags & IB_CLEAR) ? "(to be cleared)"
    205 		    : params->stage1);
    206 		if (params->stage2 != NULL)
    207 			printf("Secondary bootstrap: %s\n", params->stage2);
    208 	}
    209 
    210 	if (params->flags & IB_CLEAR) {
    211 		op = "Clear";
    212 		rv = params->machine->clearboot(params);
    213 	} else {
    214 		op = "Set";
    215 		rv = params->machine->setboot(params);
    216 	}
    217 	if (rv == 0)
    218 		errx(1, "%s bootstrap operation failed", op);
    219 
    220 	if (fsync(params->fsfd) == -1)
    221 		err(1, "Synchronising file system `%s'", params->filesystem);
    222 	if (close(params->fsfd) == -1)
    223 		err(1, "Closing file system `%s'", params->filesystem);
    224 	if (argc == 2)
    225 		if (close(params->s1fd) == -1)
    226 			err(1, "Closing primary bootstrap `%s'",
    227 			    params->stage1);
    228 
    229 	exit(0);
    230 	/* NOTREACHED */
    231 }
    232 
    233 int
    234 parseoptionflag(ib_params *params, const char *option, ib_flags wantflags)
    235 {
    236 	struct {
    237 		const char	*name;
    238 		ib_flags	flag;
    239 	} flags[] = {
    240 		{ "alphasum",	IB_ALPHASUM },
    241 		{ "append",	IB_APPEND },
    242 		{ "sunsum",	IB_SUNSUM },
    243 		{ NULL,		0 },
    244 	};
    245 
    246 	int	i;
    247 
    248 	assert(params != NULL);
    249 	assert(option != NULL);
    250 
    251 	for (i = 0; flags[i].name != NULL; i++) {
    252 		if ((strcmp(flags[i].name, option) == 0) &&
    253 		    (wantflags & flags[i].flag)) {
    254 			params->flags |= flags[i].flag;
    255 			return (1);
    256 		}
    257 	}
    258 	return (0);
    259 }
    260 
    261 int
    262 no_parseopt(ib_params *params, const char *option)
    263 {
    264 
    265 	assert(params != NULL);
    266 	assert(option != NULL);
    267 
    268 		/* all options are unsupported */
    269 	warnx("Unsupported -o option `%s'", option);
    270 	return (0);
    271 }
    272 
    273 int
    274 no_setboot(ib_params *params)
    275 {
    276 
    277 	assert(params != NULL);
    278 
    279 		/* bootstrap installation is not supported */
    280 	warnx("%s: bootstrap installation is not supported",
    281 	    params->machine->name);
    282 	return (0);
    283 }
    284 
    285 int
    286 no_clearboot(ib_params *params)
    287 {
    288 
    289 	assert(params != NULL);
    290 
    291 		/* bootstrap removal is not supported */
    292 	warnx("%s: bootstrap removal is not supported",
    293 	    params->machine->name);
    294 	return (0);
    295 }
    296 
    297 
    298 static int
    299 getmachine(ib_params *param, const char *mach, const char *provider)
    300 {
    301 	int	i;
    302 
    303 	assert(param != NULL);
    304 	assert(mach != NULL);
    305 	assert(provider != NULL);
    306 
    307 	for (i = 0; machines[i].name != NULL; i++) {
    308 		if (strcmp(machines[i].name, mach) == 0) {
    309 			param->machine = &machines[i];
    310 			return (1);
    311 		}
    312 	}
    313 	warnx("Invalid machine `%s' from %s", mach, provider);
    314 	warnx("Supported machines are:");
    315 #define MACHS_PER_LINE	10
    316 	for (i = 0; machines[i].name != NULL; i++) {
    317 		fputs((i % MACHS_PER_LINE) ? ", " : "\t", stderr);
    318 		fputs(machines[i].name, stderr);
    319 		if ((i % MACHS_PER_LINE) == (MACHS_PER_LINE - 1))
    320 			fputs("\n", stderr);
    321 	}
    322 	if ((i % MACHS_PER_LINE) != 0)
    323 		fputs("\n", stderr);
    324 	return (0);
    325 }
    326 
    327 static int
    328 getfstype(ib_params *param, const char *fstype, const char *provider)
    329 {
    330 	int	i;
    331 
    332 	assert(param != NULL);
    333 	assert(fstype != NULL);
    334 	assert(provider != NULL);
    335 
    336 	for (i = 0; fstypes[i].name != NULL; i++) {
    337 		if (strcmp(fstypes[i].name, fstype) == 0) {
    338 			param->fstype = &fstypes[i];
    339 			return (1);
    340 		}
    341 	}
    342 	warnx("Invalid file system type `%s' from %s", fstype, provider);
    343 	warnx("Supported file system types are:");
    344 #define FSTYPES_PER_LINE	10
    345 	for (i = 0; fstypes[i].name != NULL; i++) {
    346 		fputs((i % FSTYPES_PER_LINE) ? ", " : "\t", stderr);
    347 		fputs(fstypes[i].name, stderr);
    348 		if ((i % FSTYPES_PER_LINE) == (FSTYPES_PER_LINE - 1))
    349 			fputs("\n", stderr);
    350 	}
    351 	if ((i % FSTYPES_PER_LINE) != 0)
    352 		fputs("\n", stderr);
    353 	return (0);
    354 }
    355 
    356 static void
    357 usage(void)
    358 {
    359 	const char	*prog;
    360 
    361 	prog = getprogname();
    362 	fprintf(stderr,
    363 "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
    364 "\t\t   [-b s1start] [-B s2start] filesystem primary [secondary]\n"
    365 "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
    366 	    prog, prog);
    367 	exit(1);
    368 }
    369