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