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