xref: /netbsd-src/usr.sbin/installboot/installboot.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: installboot.c,v 1.13 2003/05/08 20:33:44 petrov 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.13 2003/05/08 20:33:44 petrov 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 <stddef.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 #include "installboot.h"
57 
58 int		main(int, char *[]);
59 static	void	getmachine(ib_params *, const char *, const char *);
60 static	void	getfstype(ib_params *, const char *, const char *);
61 static	void	parseoptions(ib_params *, const char *);
62 static	void	usage(void);
63 static	void	options_usage(void);
64 static	void	machine_usage(void);
65 static	void	fstype_usage(void);
66 
67 static	ib_params	installboot_params;
68 
69 #define OFFSET(field)    offsetof(ib_params, field)
70 const struct option {
71 	const char	*name;		/* Name of option */
72 	ib_flags	flag;		/* Corresponding IB_xxx flag */
73 	enum {				/* Type of option value... */
74 		OPT_BOOL,		/* no value */
75 		OPT_INT,		/* numeric value */
76 		OPT_WORD,		/* space/tab/, terminated */
77 		OPT_STRING		/* null terminated */
78 	}		type;
79 	int		offset;		/* of field in ib_params */
80 } options[] = {
81 	{ "alphasum",	IB_ALPHASUM,	OPT_BOOL },
82 	{ "append",	IB_APPEND,	OPT_BOOL },
83 	{ "command",	IB_COMMAND,	OPT_STRING,	OFFSET(command) },
84 	{ "console",	IB_CONSOLE,	OPT_WORD,	OFFSET(console) },
85 	{ "password",	IB_PASSWORD,	OPT_WORD,	OFFSET(password) },
86 	{ "resetvideo",	IB_RESETVIDEO,	OPT_BOOL },
87 	{ "speed",	IB_CONSPEED,	OPT_INT,	OFFSET(conspeed) },
88 	{ "sunsum",	IB_SUNSUM,	OPT_BOOL },
89 	{ "timeout",	IB_TIMEOUT,	OPT_INT,	OFFSET(timeout) },
90 	{ NULL },
91 };
92 #undef OFFSET
93 #define OPTION(params, type, opt) (*(type *)((char *)(params) + (opt)->offset))
94 
95 int
96 main(int argc, char *argv[])
97 {
98 	struct utsname	utsname;
99 	ib_params	*params;
100 	unsigned long	lval;
101 	int		ch, rv, mode;
102 	char 		*p;
103 	const char	*op;
104 	ib_flags	unsupported_flags;
105 
106 	setprogname(argv[0]);
107 	params = &installboot_params;
108 	memset(params, 0, sizeof(*params));
109 	params->fsfd = -1;
110 	params->s1fd = -1;
111 	if ((p = getenv("MACHINE")) != NULL)
112 		getmachine(params, p, "$MACHINE");
113 
114 	while ((ch = getopt(argc, argv, "b:B:cm:no:t:v")) != -1) {
115 		switch (ch) {
116 
117 		case 'b':
118 		case 'B':
119 			if (*optarg == '\0')
120 				goto badblock;
121 			lval = strtoul(optarg, &p, 0);
122 			if (lval > UINT32_MAX || *p != '\0') {
123  badblock:
124 				errx(1, "Invalid block number `%s'", optarg);
125 			}
126 			if (ch == 'b') {
127 				params->s1start = (uint32_t)lval;
128 				params->flags |= IB_STAGE1START;
129 			} else {
130 				params->s2start = (uint32_t)lval;
131 				params->flags |= IB_STAGE2START;
132 			}
133 			break;
134 
135 		case 'c':
136 			params->flags |= IB_CLEAR;
137 			break;
138 
139 		case 'm':
140 			getmachine(params, optarg, "-m");
141 			break;
142 
143 		case 'n':
144 			params->flags |= IB_NOWRITE;
145 			break;
146 
147 		case 'o':
148 			parseoptions(params, optarg);
149 			break;
150 
151 		case 't':
152 			getfstype(params, optarg, "-t");
153 			break;
154 
155 		case 'v':
156 			params->flags |= IB_VERBOSE;
157 			break;
158 
159 		case '?':
160 		default:
161 			usage();
162 			/* NOTREACHED */
163 
164 		}
165 	}
166 	argc -= optind;
167 	argv += optind;
168 
169 	if (((params->flags & IB_CLEAR) != 0 && argc != 1) ||
170 	    ((params->flags & IB_CLEAR) == 0 && (argc < 2 || argc > 3)))
171 		usage();
172 
173 		/* set missing defaults */
174 	if (params->machine == NULL) {
175 		if (uname(&utsname) == -1)
176 			err(1, "Determine uname");
177 		getmachine(params, utsname.machine, "uname()");
178 	}
179 
180 	/* Check that options are supported by this system */
181 	unsupported_flags = params->flags & ~params->machine->valid_flags;
182 	unsupported_flags &= ~(IB_VERBOSE | IB_NOWRITE |IB_CLEAR);
183 	if (unsupported_flags != 0) {
184 		int ndx;
185 		for (ndx = 0; options[ndx].name != NULL; ndx++) {
186 			if (unsupported_flags & options[ndx].flag)
187 				warnx("`-o %s' is not supported for %s",
188 				    options[ndx].name, params->machine->name);
189 		}
190 		if (unsupported_flags & IB_STAGE1START)
191 			warnx("`-b bno' is not supported for %s",
192 			    params->machine->name);
193 		if (unsupported_flags & IB_STAGE2START)
194 			warnx("`-B bno' is not supported for %s",
195 			    params->machine->name);
196 		exit(1);
197 	}
198 	/* and some illegal combinations */
199 	if (params->flags & IB_STAGE1START && params->flags & IB_APPEND) {
200 		warnx("Can't use `-b bno' with `-o append'");
201 		exit(1);
202 	}
203 	if (params->flags & IB_CLEAR &&
204 	    params->flags & (IB_STAGE1START | IB_STAGE2START | IB_APPEND)) {
205 		warnx("Can't use `-b bno', `-B bno' or `-o append' with `-c'");
206 		exit(1);
207 	}
208 
209 	params->filesystem = argv[0];
210 	if (params->flags & IB_NOWRITE) {
211 		op = "only";
212 		mode = O_RDONLY;
213 	} else {
214 		op = "write";
215 		mode = O_RDWR;
216 	}
217 	if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1)
218 		err(1, "Opening file system `%s' read-%s",
219 		    params->filesystem, op);
220 	if (fstat(params->fsfd, &params->fsstat) == -1)
221 		err(1, "Examining file system `%s'", params->filesystem);
222 	if (params->fstype != NULL) {
223 		if (! params->fstype->match(params))
224 			err(1, "File system `%s' is not of type %s",
225 			    params->filesystem, params->fstype->name);
226 	} else {
227 		params->fstype = &fstypes[0];
228 		while (params->fstype->name != NULL &&
229 			! params->fstype->match(params))
230 			params->fstype++;
231 		if (params->fstype->name == NULL)
232 			errx(1, "File system `%s' is of an unknown type",
233 			    params->filesystem);
234 	}
235 
236 	if (argc >= 2) {
237 		params->stage1 = argv[1];
238 		if ((params->s1fd = open(params->stage1, O_RDONLY, 0600))
239 		    == -1)
240 			err(1, "Opening primary bootstrap `%s'",
241 			    params->stage1);
242 		if (fstat(params->s1fd, &params->s1stat) == -1)
243 			err(1, "Examining primary bootstrap `%s'",
244 			    params->stage1);
245 		if (!S_ISREG(params->s1stat.st_mode))
246 			err(1, "`%s' must be a regular file", params->stage1);
247 	}
248 	if (argc == 3) {
249 		params->stage2 = argv[2];
250 	}
251 	assert(params->machine != NULL);
252 
253 	if (params->flags & IB_VERBOSE) {
254 		printf("File system:         %s\n", params->filesystem);
255 		printf("File system type:    %s (blocksize %u, needswap %d)\n",
256 		    params->fstype->name,
257 		    params->fstype->blocksize, params->fstype->needswap);
258 		printf("Primary bootstrap:   %s\n",
259 		    (params->flags & IB_CLEAR) ? "(to be cleared)"
260 		    : params->stage1);
261 		if (params->stage2 != NULL)
262 			printf("Secondary bootstrap: %s\n", params->stage2);
263 	}
264 
265 	if (params->flags & IB_CLEAR) {
266 		op = "Clear";
267 		rv = params->machine->clearboot(params);
268 	} else {
269 		op = "Set";
270 		rv = params->machine->setboot(params);
271 	}
272 	if (rv == 0)
273 		errx(1, "%s bootstrap operation failed", op);
274 
275 	if (S_ISREG(params->fsstat.st_mode)) {
276 		if (fsync(params->fsfd) == -1)
277 			err(1, "Synchronising file system `%s'",
278 			    params->filesystem);
279 	} else {
280 		/* Sync filesystems (to clean in-memory superblock?) */
281 		sync();
282 	}
283 	if (close(params->fsfd) == -1)
284 		err(1, "Closing file system `%s'", params->filesystem);
285 	if (argc == 2)
286 		if (close(params->s1fd) == -1)
287 			err(1, "Closing primary bootstrap `%s'",
288 			    params->stage1);
289 
290 	exit(0);
291 	/* NOTREACHED */
292 }
293 
294 static void
295 parseoptions(ib_params *params, const char *option)
296 {
297 	char *cp;
298 	const struct option *opt;
299 	int len;
300 	unsigned long val;
301 
302 	assert(params != NULL);
303 	assert(option != NULL);
304 
305 	for (;; option += len) {
306 		option += strspn(option, ", \t");
307 		if (*option == 0)
308 			return;
309 		len = strcspn(option, "=,");
310 		for (opt = options; opt->name != NULL; opt++) {
311 			if (memcmp(option, opt->name, len) == 0
312 			    && opt->name[len] == 0)
313 				break;
314 		}
315 		if (opt->name == NULL) {
316 			len = strcspn(option, ",");
317 			warnx("Unknown option `-o %.*s'", len, option);
318 			break;
319 		}
320 		params->flags |= opt->flag;
321 		if (opt->type == OPT_BOOL) {
322 			if (option[len] != '=')
323 				continue;
324 			warnx("Option `%s' must not have a value", opt->name);
325 			break;
326 		}
327 		if (option[len] != '=') {
328 			warnx("Option `%s' must have a value", opt->name);
329 			break;
330 		}
331 		option += len + 1;
332 		len = strcspn(option, ",");
333 		switch (opt->type) {
334 		case OPT_STRING:
335 			len = strlen(option);
336 			/* FALLTHROUGH */
337 		case OPT_WORD:
338 			cp = strdup(option);
339 			if (cp == NULL)
340 				err(1, "strdup");
341 			cp[len] = 0;
342 			OPTION(params, char *, opt) = cp;
343 			continue;
344 		case OPT_INT:
345 			val = strtoul(option, &cp, 0);
346 			if (cp > option + len || (*cp != 0 && *cp != ','))
347 				break;
348 			OPTION(params, int, opt) = val;
349 			if (OPTION(params, int, opt) != val)
350 				/* value got truncated on int convertion */
351 				break;
352 			continue;
353 		default:
354 			errx(1, "Internal error: option `%s' has invalid type %d",
355 				opt->name, opt->type);
356 		}
357 		warnx("Invalid option value `%s=%.*s'", opt->name, len, option);
358 		break;
359 	}
360 	options_usage();
361 	exit(1);
362 }
363 
364 static void
365 options_usage(void)
366 {
367 	int ndx;
368 	const char *pfx;
369 
370 	warnx("Valid options are:");
371 	pfx = "\t";
372 	for (ndx = 0; options[ndx].name != 0; ndx++) {
373 		fprintf(stderr, "%s%s", pfx, options[ndx].name);
374 		switch (options[ndx].type) {
375 		case OPT_INT:
376 			fprintf(stderr, "=number");
377 			break;
378 		case OPT_WORD:
379 			fprintf(stderr, "=word");
380 			break;
381 		case OPT_STRING:
382 			fprintf(stderr, "=string");
383 			break;
384 		default:
385 			break;
386 		}
387 		if ((ndx % 5) == 4)
388 			pfx = ",\n\t";
389 		else
390 			pfx = ", ";
391 	}
392 	fprintf(stderr, "\n");
393 }
394 
395 int
396 no_setboot(ib_params *params)
397 {
398 
399 	assert(params != NULL);
400 
401 		/* bootstrap installation is not supported */
402 	warnx("%s: bootstrap installation is not supported",
403 	    params->machine->name);
404 	return (0);
405 }
406 
407 int
408 no_clearboot(ib_params *params)
409 {
410 
411 	assert(params != NULL);
412 
413 		/* bootstrap removal is not supported */
414 	warnx("%s: bootstrap removal is not supported",
415 	    params->machine->name);
416 	return (0);
417 }
418 
419 
420 static void
421 getmachine(ib_params *param, const char *mach, const char *provider)
422 {
423 	int	i;
424 
425 	assert(param != NULL);
426 	assert(mach != NULL);
427 	assert(provider != NULL);
428 
429 	for (i = 0; machines[i].name != NULL; i++) {
430 		if (strcmp(machines[i].name, mach) == 0) {
431 			param->machine = &machines[i];
432 			return;
433 		}
434 	}
435 	warnx("Invalid machine `%s' from %s", mach, provider);
436 	machine_usage();
437 	exit(1);
438 }
439 
440 static void
441 machine_usage(void)
442 {
443 	const char *prefix;
444 	int	i;
445 
446 	warnx("Supported machines are:");
447 #define MACHS_PER_LINE	9
448 	prefix="";
449 	for (i = 0; machines[i].name != NULL; i++) {
450 		if (i == 0)
451 			prefix="\t";
452 		else if (i % MACHS_PER_LINE)
453 			prefix=", ";
454 		else
455 			prefix=",\n\t";
456 		fprintf(stderr, "%s%s", prefix, machines[i].name);
457 	}
458 	fputs("\n", stderr);
459 }
460 
461 static void
462 getfstype(ib_params *param, const char *fstype, const char *provider)
463 {
464 	int i;
465 
466 	assert(param != NULL);
467 	assert(fstype != NULL);
468 	assert(provider != NULL);
469 
470 	for (i = 0; fstypes[i].name != NULL; i++) {
471 		if (strcmp(fstypes[i].name, fstype) == 0) {
472 			param->fstype = &fstypes[i];
473 			return;
474 		}
475 	}
476 	warnx("Invalid file system type `%s' from %s", fstype, provider);
477 	fstype_usage();
478 	exit(1);
479 }
480 
481 static void
482 fstype_usage(void)
483 {
484 	const char *prefix;
485 	int	i;
486 
487 	warnx("Supported file system types are:");
488 #define FSTYPES_PER_LINE	9
489 	prefix="";
490 	for (i = 0; fstypes[i].name != NULL; i++) {
491 		if (i == 0)
492 			prefix="\t";
493 		else if (i % FSTYPES_PER_LINE)
494 			prefix=", ";
495 		else
496 			prefix=",\n\t";
497 		fprintf(stderr, "%s%s", prefix, fstypes[i].name);
498 	}
499 	fputs("\n", stderr);
500 }
501 
502 static void
503 usage(void)
504 {
505 	const char	*prog;
506 
507 	prog = getprogname();
508 	fprintf(stderr,
509 "Usage: %s [-nv] [-m machine] [-o options] [-t fstype]\n"
510 "\t\t   [-b s1start] [-B s2start] filesystem primary [secondary]\n"
511 "Usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n",
512 	    prog, prog);
513 	machine_usage();
514 	fstype_usage();
515 	options_usage();
516 	exit(1);
517 }
518