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