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