Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.6
      1 /*	$NetBSD: installboot.c,v 1.6 2002/04/19 07:08:52 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.6 2002/04/19 07:08:52 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 	if ((p = getenv("MACHINE")) != NULL)
     79 		if (! getmachine(params, p, "$MACHINE"))
     80 			exit(1);
     81 
     82 	while ((ch = getopt(argc, argv, "b:cm:no:t:v")) != -1) {
     83 		switch (ch) {
     84 
     85 		case 'b':
     86 			if (*optarg == '\0')
     87 				goto badblock;
     88 			lval = strtoul(optarg, &p, 0);
     89 			if (lval > UINT32_MAX || *p != '\0') {
     90  badblock:
     91 				errx(1, "Invalid block number `%s'", optarg);
     92 			}
     93 			params->startblock = (uint32_t)lval;
     94 			params->flags |= IB_STARTBLOCK;
     95 			break;
     96 
     97 		case 'c':
     98 			params->flags |= IB_CLEAR;
     99 			break;
    100 
    101 		case 'm':
    102 			if (! getmachine(params, optarg, "-m"))
    103 				exit(1);
    104 			break;
    105 
    106 		case 'n':
    107 			params->flags |= IB_NOWRITE;
    108 			break;
    109 
    110 		case 'o':
    111 			if (params->machine == NULL)
    112 				errx(1,
    113 				    "Machine needs to be specified before -o");
    114 			while ((p = strsep(&optarg, ",")) != NULL) {
    115 				if (*p == '\0')
    116 					errx(1, "Empty `-o' option");
    117 				if (! params->machine->parseopt(params, p))
    118 					exit(1);
    119 			}
    120 			break;
    121 
    122 		case 't':
    123 			if (! getfstype(params, optarg, "-t"))
    124 				exit(1);
    125 			break;
    126 
    127 		case 'v':
    128 			params->flags |= IB_VERBOSE;
    129 			break;
    130 
    131 		case '?':
    132 		default:
    133 			usage();
    134 			/* NOTREACHED */
    135 
    136 		}
    137 	}
    138 	argc -= optind;
    139 	argv += optind;
    140 
    141 	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
    142 	    ((params->flags & IB_CLEAR) == 0 && (argc < 2 || argc > 3)))
    143 		usage();
    144 
    145 		/* set missing defaults */
    146 	if (params->machine == NULL) {
    147 		if (uname(&utsname) == -1)
    148 			err(1, "Determine uname");
    149 		if (! getmachine(params, utsname.machine, "uname()"))
    150 			exit(1);
    151 	}
    152 
    153 	params->filesystem = argv[0];
    154 	if (params->flags & IB_NOWRITE) {
    155 		op = "only";
    156 		mode = O_RDONLY;
    157 	} else {
    158 		op = "write";
    159 		mode = O_RDWR;
    160 	}
    161 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    162 		err(1, "Opening file system `%s' read-%s",
    163 		    params->filesystem, op);
    164 	if (params->fstype != NULL) {
    165 		if (! params->fstype->match(params))
    166 			err(1, "File system `%s' is not of type %s",
    167 			    params->filesystem, params->fstype->name);
    168 	} else {
    169 		params->fstype = &fstypes[0];
    170 		while (params->fstype->name != NULL &&
    171 			! params->fstype->match(params))
    172 			params->fstype++;
    173 		if (params->fstype->name == NULL)
    174 			err(1, "File system `%s' is of an unknown type",
    175 			    params->filesystem);
    176 	}
    177 
    178 	if (argc >= 2) {
    179 		params->stage1 = argv[1];
    180 		if ((params->s1fd = open(params->stage1, O_RDONLY, 0600))
    181 		    == -1)
    182 			err(1, "Opening primary bootstrap `%s'",
    183 			    params->stage1);
    184 	}
    185 	if (argc == 3) {
    186 		params->stage2 = argv[2];
    187 	}
    188 	assert(params->machine != NULL);
    189 
    190 	if (params->flags & IB_VERBOSE) {
    191 		printf("File system:         %s\n", params->filesystem);
    192 		printf("Primary bootstrap:   %s\n",
    193 		    (params->flags & IB_CLEAR) ? "(to be cleared)"
    194 		    : params->stage1);
    195 		if (params->stage2 != NULL)
    196 			printf("Secondary bootstrap: %s\n", params->stage2);
    197 	}
    198 
    199 	if (params->flags & IB_CLEAR) {
    200 		op = "Clear";
    201 		rv = params->machine->clearboot(params);
    202 	} else {
    203 		op = "Set";
    204 		rv = params->machine->setboot(params);
    205 	}
    206 	if (rv == 0)
    207 		errx(1, "%s bootstrap operation failed", op);
    208 
    209 	if (fsync(params->fsfd) == -1)
    210 		err(1, "Synchronising file system `%s'", params->filesystem);
    211 	if (close(params->fsfd) == -1)
    212 		err(1, "Closing file system `%s'", params->filesystem);
    213 	if (argc == 2)
    214 		if (close(params->s1fd) == -1)
    215 			err(1, "Closing primary bootstrap `%s'",
    216 			    params->stage1);
    217 
    218 	exit(0);
    219 	/* NOTREACHED */
    220 }
    221 
    222 int
    223 parseoptionflag(ib_params *params, const char *option, ib_flags wantflags)
    224 {
    225 	struct {
    226 		const char	*name;
    227 		ib_flags	flag;
    228 	} flags[] = {
    229 		{ "alphasum",	IB_ALPHASUM },
    230 		{ "append",	IB_APPEND },
    231 		{ "sunsum",	IB_SUNSUM },
    232 		{ NULL,		0 },
    233 	};
    234 
    235 	int	i;
    236 
    237 	assert(params != NULL);
    238 	assert(option != NULL);
    239 
    240 	for (i = 0; flags[i].name != NULL; i++) {
    241 		if ((strcmp(flags[i].name, option) == 0) &&
    242 		    (wantflags & flags[i].flag)) {
    243 			params->flags |= flags[i].flag;
    244 			return (1);
    245 		}
    246 	}
    247 	return (0);
    248 }
    249 
    250 int
    251 no_parseopt(ib_params *params, const char *option)
    252 {
    253 
    254 		/* all options are unsupported */
    255 	warnx("Unsupported -o option `%s'", option);
    256 	return (0);
    257 }
    258 
    259 int
    260 no_setboot(ib_params *params)
    261 {
    262 
    263 		/* bootstrap installation is not supported */
    264 	warnx("%s: bootstrap installation is not supported",
    265 	    params->machine->name);
    266 	return (0);
    267 }
    268 
    269 int
    270 no_clearboot(ib_params *params)
    271 {
    272 
    273 		/* bootstrap removal is not supported */
    274 	warnx("%s: bootstrap removal is not supported",
    275 	    params->machine->name);
    276 	return (0);
    277 }
    278 
    279 
    280 static int
    281 getmachine(ib_params *param, const char *mach, const char *provider)
    282 {
    283 	int	i;
    284 
    285 	assert(param != NULL);
    286 	assert(mach != NULL);
    287 
    288 	for (i = 0; machines[i].name != NULL; i++) {
    289 		if (strcmp(machines[i].name, mach) == 0) {
    290 			param->machine = &machines[i];
    291 			return (1);
    292 		}
    293 	}
    294 	warnx("Invalid machine `%s' from %s", mach, provider);
    295 	warnx("Supported machines are:");
    296 #define MACHS_PER_LINE	10
    297 	for (i = 0; machines[i].name != NULL; i++) {
    298 		fputs((i % MACHS_PER_LINE) ? ", " : "\t", stderr);
    299 		fputs(machines[i].name, stderr);
    300 		if ((i % MACHS_PER_LINE) == (MACHS_PER_LINE - 1))
    301 			fputs("\n", stderr);
    302 	}
    303 	if ((i % MACHS_PER_LINE) != 0)
    304 		fputs("\n", stderr);
    305 	return (0);
    306 }
    307 
    308 static int
    309 getfstype(ib_params *param, const char *fstype, const char *provider)
    310 {
    311 	int	i;
    312 
    313 	assert(param != NULL);
    314 	assert(fstype != NULL);
    315 
    316 	for (i = 0; fstypes[i].name != NULL; i++) {
    317 		if (strcmp(fstypes[i].name, fstype) == 0) {
    318 			param->fstype = &fstypes[i];
    319 			return (1);
    320 		}
    321 	}
    322 	warnx("Invalid file system type `%s' from %s", fstype, provider);
    323 	warnx("Supported file system types are:");
    324 #define FSTYPES_PER_LINE	10
    325 	for (i = 0; fstypes[i].name != NULL; i++) {
    326 		fputs((i % FSTYPES_PER_LINE) ? ", " : "\t", stderr);
    327 		fputs(fstypes[i].name, stderr);
    328 		if ((i % FSTYPES_PER_LINE) == (FSTYPES_PER_LINE - 1))
    329 			fputs("\n", stderr);
    330 	}
    331 	if ((i % FSTYPES_PER_LINE) != 0)
    332 		fputs("\n", stderr);
    333 	return (0);
    334 }
    335 
    336 static void
    337 usage(void)
    338 {
    339 	const char	*prog;
    340 
    341 	prog = getprogname();
    342 	fprintf(stderr,
    343 "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
    344 "\t\t   [-b block] filesystem primary [secondary]\n"
    345 "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
    346 	    prog, prog);
    347 	exit(1);
    348 }
    349