Home | History | Annotate | Line # | Download | only in installboot
installboot.c revision 1.4
      1 /*	$NetBSD: installboot.c,v 1.4 2002/04/12 03:15:20 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.4 2002/04/12 03:15:20 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 <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 
     55 #include "installboot.h"
     56 
     57 int		main(int, char *[]);
     58 static	int	getmachine(ib_params *, const char *, const char *);
     59 static	void	usage(void);
     60 
     61 static	ib_params	installboot_params;
     62 
     63 int
     64 main(int argc, char *argv[])
     65 {
     66 	struct utsname	utsname;
     67 	ib_params	*params;
     68 	long		lval;
     69 	int		ch, rv, mode;
     70 	char 		*p;
     71 	const char	*op;
     72 
     73 	setprogname(argv[0]);
     74 	params = &installboot_params;
     75 	memset(params, 0, sizeof(*params));
     76 	if ((p = getenv("MACHINE")) != NULL)
     77 		if (! getmachine(params, p, "$MACHINE"))
     78 			exit(1);
     79 
     80 	while ((ch = getopt(argc, argv, "b:cm:no:t:v")) != -1) {
     81 		switch (ch) {
     82 
     83 		case 'b':
     84 			if (*optarg == '\0')
     85 				goto badblock;
     86 			lval = strtol(optarg, &p, 0);
     87 			if (lval < 0 || lval >= LONG_MAX || *p != '\0') {
     88  badblock:
     89 				errx(1, "Invalid block number `%s'", optarg);
     90 			}
     91 			params->startblock = lval;
     92 			params->flags |= IB_STARTBLOCK;
     93 			break;
     94 
     95 		case 'c':
     96 			params->flags |= IB_CLEAR;
     97 			break;
     98 
     99 		case 'm':
    100 			if (! getmachine(params, optarg, "-m"))
    101 				exit(1);
    102 			break;
    103 
    104 		case 'n':
    105 			params->flags |= IB_NOWRITE;
    106 			break;
    107 
    108 		case 'o':
    109 			if (params->machine == NULL)
    110 				errx(1,
    111 				    "Machine needs to be specified before -o");
    112 			while ((p = strsep(&optarg, ",")) != NULL) {
    113 				if (*p == '\0')
    114 					errx(1, "Empty `-o' option");
    115 				if (! params->machine->parseopt(params, p))
    116 					exit(1);
    117 			}
    118 			break;
    119 
    120 		case 't':
    121 			params->fstype = optarg;	// XXX: validate?
    122 			break;
    123 
    124 		case 'v':
    125 			params->flags |= IB_VERBOSE;
    126 			break;
    127 
    128 		case '?':
    129 		default:
    130 			usage();
    131 			/* NOTREACHED */
    132 
    133 		}
    134 	}
    135 	argc -= optind;
    136 	argv += optind;
    137 
    138 	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
    139 	    ((params->flags & IB_CLEAR) == 0 && argc != 2))
    140 		usage();
    141 
    142 		/* set missing defaults */
    143 	if (params->machine == NULL) {
    144 		if (uname(&utsname) == -1)
    145 			err(1, "Determine uname");
    146 		if (! getmachine(params, utsname.machine, "uname()"))
    147 			exit(1);
    148 	}
    149 	// XXX: set default params->fstype
    150 
    151 	params->filesystem = argv[0];
    152 	if (params->flags & IB_NOWRITE) {
    153 		op = "only";
    154 		mode = O_RDONLY;
    155 	} else {
    156 		op = "write";
    157 		mode = O_RDWR;
    158 	}
    159 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
    160 		err(1, "Opening file system `%s' read-%s",
    161 		    params->filesystem, op);
    162 
    163 	if (argc == 2) {
    164 		params->bootblock = argv[1];
    165 		if ((params->bbfd = open(params->bootblock, O_RDONLY, 0600))
    166 		    == -1)
    167 			err(1, "Opening boot block `%s'", params->bootblock);
    168 	}
    169 	assert(params->machine != NULL);
    170 
    171 	if (params->flags & IB_VERBOSE) {
    172 		printf("File system: %s\n", params->filesystem);
    173 		printf("Boot block:  %s\n",
    174 		    (params->flags & IB_CLEAR) ? "(to be cleared)"
    175 		    : params->bootblock);
    176 	}
    177 
    178 	if (params->flags & IB_CLEAR) {
    179 		op = "Clear";
    180 		rv = params->machine->clearboot(params);
    181 	} else {
    182 		op = "Set";
    183 		rv = params->machine->setboot(params);
    184 	}
    185 	if (rv == 0)
    186 		errx(1, "%s boot block operation failed", op);
    187 
    188 	if (fsync(params->fsfd) == -1)
    189 		err(1, "Synchronising file system `%s'", params->filesystem);
    190 	if (close(params->fsfd) == -1)
    191 		err(1, "Closing file system `%s'", params->filesystem);
    192 	if (argc == 2)
    193 		if (close(params->bbfd) == -1)
    194 			err(1, "Closing boot block `%s'", params->bootblock);
    195 
    196 	exit(0);
    197 	/* NOTREACHED */
    198 }
    199 
    200 int
    201 parseoptionflag(ib_params *params, const char *option, ib_flags wantflags)
    202 {
    203 	struct {
    204 		const char	*name;
    205 		ib_flags	flag;
    206 	} flags[] = {
    207 		{ "alphasum",	IB_ALPHASUM },
    208 		{ "append",	IB_APPEND },
    209 		{ "sunsum",	IB_SUNSUM },
    210 		{ NULL,		0 },
    211 	};
    212 
    213 	int	i;
    214 
    215 	assert(params != NULL);
    216 	assert(option != NULL);
    217 
    218 	for (i = 0; flags[i].name != NULL; i++) {
    219 		if ((strcmp(flags[i].name, option) == 0) &&
    220 		    (wantflags & flags[i].flag)) {
    221 			params->flags |= flags[i].flag;
    222 			return (1);
    223 		}
    224 	}
    225 	return (0);
    226 }
    227 
    228 int
    229 no_parseopt(ib_params *params, const char *option)
    230 {
    231 
    232 		/* all options are unsupported */
    233 	warnx("Unsupported -o option `%s'", option);
    234 	return (0);
    235 }
    236 
    237 int
    238 no_setboot(ib_params *params)
    239 {
    240 
    241 		/* boot block installation is not supported */
    242 	warnx("%s: boot block installation is not supported",
    243 	    params->machine->name);
    244 	return (0);
    245 }
    246 
    247 int
    248 no_clearboot(ib_params *params)
    249 {
    250 
    251 		/* boot block removal is not supported */
    252 	warnx("%s: boot block removal is not supported",
    253 	    params->machine->name);
    254 	return (0);
    255 }
    256 
    257 
    258 static int
    259 getmachine(ib_params *param, const char *mach, const char *provider)
    260 {
    261 	int	i;
    262 
    263 	assert(param != NULL);
    264 	assert(mach != NULL);
    265 
    266 	for (i = 0; machines[i].name != NULL; i++) {
    267 		if (strcmp(machines[i].name, mach) == 0) {
    268 			param->machine = &machines[i];
    269 			return (1);
    270 		}
    271 	}
    272 	warnx("Invalid machine `%s' from %s", mach, provider);
    273 	warnx("Supported machines are:");
    274 #define MACHS_PER_LINE	10
    275 	for (i = 0; machines[i].name != NULL; i++) {
    276 		fputs((i % MACHS_PER_LINE) ? ", " : "\t", stderr);
    277 		fputs(machines[i].name, stderr);
    278 		if ((i % MACHS_PER_LINE) == (MACHS_PER_LINE - 1))
    279 			fputs("\n", stderr);
    280 	}
    281 	if ((i % MACHS_PER_LINE) != 0)
    282 		fputs("\n", stderr);
    283 	return (0);
    284 }
    285 
    286 static void
    287 usage(void)
    288 {
    289 	const char	*prog;
    290 
    291 	prog = getprogname();
    292 	fprintf(stderr,
    293 "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
    294 "\t\t   [-b block] filesystem bootstrap\n"
    295 "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
    296 	    prog, prog);
    297 	exit(1);
    298 }
    299