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