1 /* $NetBSD: installboot.c,v 1.21 2005/11/12 09:35:31 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.21 2005/11/12 09:35:31 dsl 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 if (argc >= 3) { 221 params->stage2 = argv[2]; 222 } 223 224 params->filesystem = argv[0]; 225 if (params->flags & IB_NOWRITE) { 226 op = "only"; 227 mode = O_RDONLY; 228 } else { 229 op = "write"; 230 mode = O_RDWR; 231 } 232 if ((params->fsfd = open(params->filesystem, mode, 0600)) == -1) 233 err(1, "Opening file system `%s' read-%s", 234 params->filesystem, op); 235 if (fstat(params->fsfd, ¶ms->fsstat) == -1) 236 err(1, "Examining file system `%s'", params->filesystem); 237 if (params->fstype != NULL) { 238 if (! params->fstype->match(params)) 239 errx(1, "File system `%s' is not of type %s", 240 params->filesystem, params->fstype->name); 241 } else { 242 if (params->stage2 != NULL) { 243 params->fstype = &fstypes[0]; 244 while (params->fstype->name != NULL && 245 !params->fstype->match(params)) 246 params->fstype++; 247 if (params->fstype->name == NULL) 248 errx(1, "File system `%s' is of an unknown type", 249 params->filesystem); 250 } 251 } 252 253 if (argc >= 2) { 254 params->stage1 = argv[1]; 255 if ((params->s1fd = open(params->stage1, O_RDONLY, 0600)) == -1) 256 err(1, "Opening primary bootstrap `%s'", 257 params->stage1); 258 if (fstat(params->s1fd, ¶ms->s1stat) == -1) 259 err(1, "Examining primary bootstrap `%s'", 260 params->stage1); 261 if (!S_ISREG(params->s1stat.st_mode)) 262 errx(1, "`%s' must be a regular file", params->stage1); 263 } 264 assert(params->machine != NULL); 265 266 if (params->flags & IB_VERBOSE) { 267 printf("File system: %s\n", params->filesystem); 268 if (params->fstype) 269 printf("File system type: %s (blocksize %u, " 270 "needswap %d)\n", 271 params->fstype->name, params->fstype->blocksize, 272 params->fstype->needswap); 273 if (!(params->flags & IB_EDIT)) 274 printf("Primary bootstrap: %s\n", 275 (params->flags & IB_CLEAR) ? "(to be cleared)" 276 : params->stage1 ? params->stage1 : "(none)" ); 277 if (params->stage2 != NULL) 278 printf("Secondary bootstrap: %s\n", params->stage2); 279 } 280 281 if (params->flags & IB_EDIT) { 282 op = "Edit"; 283 rv = params->machine->editboot(params); 284 } else if (params->flags & IB_CLEAR) { 285 op = "Clear"; 286 rv = params->machine->clearboot(params); 287 } else { 288 op = "Set"; 289 rv = params->machine->setboot(params); 290 } 291 if (rv == 0) 292 errx(1, "%s bootstrap operation failed", op); 293 294 if (S_ISREG(params->fsstat.st_mode)) { 295 if (fsync(params->fsfd) == -1) 296 err(1, "Synchronising file system `%s'", 297 params->filesystem); 298 } else { 299 /* Sync filesystems (to clean in-memory superblock?) */ 300 sync(); 301 } 302 if (close(params->fsfd) == -1) 303 err(1, "Closing file system `%s'", params->filesystem); 304 if (argc == 2) 305 if (close(params->s1fd) == -1) 306 err(1, "Closing primary bootstrap `%s'", 307 params->stage1); 308 309 exit(0); 310 /* NOTREACHED */ 311 } 312 313 static void 314 parseoptions(ib_params *params, const char *option) 315 { 316 char *cp; 317 const struct option *opt; 318 int len; 319 unsigned long val; 320 321 assert(params != NULL); 322 assert(option != NULL); 323 324 for (;; option += len) { 325 option += strspn(option, ", \t"); 326 if (*option == 0) 327 return; 328 len = strcspn(option, "=,"); 329 for (opt = options; opt->name != NULL; opt++) { 330 if (memcmp(option, opt->name, len) == 0 331 && opt->name[len] == 0) 332 break; 333 } 334 if (opt->name == NULL) { 335 len = strcspn(option, ","); 336 warnx("Unknown option `-o %.*s'", len, option); 337 break; 338 } 339 params->flags |= opt->flag; 340 if (opt->type == OPT_BOOL) { 341 if (option[len] != '=') 342 continue; 343 warnx("Option `%s' must not have a value", opt->name); 344 break; 345 } 346 if (option[len] != '=') { 347 warnx("Option `%s' must have a value", opt->name); 348 break; 349 } 350 option += len + 1; 351 len = strcspn(option, ","); 352 switch (opt->type) { 353 case OPT_STRING: 354 len = strlen(option); 355 /* FALLTHROUGH */ 356 case OPT_WORD: 357 cp = strdup(option); 358 if (cp == NULL) 359 err(1, "strdup"); 360 cp[len] = 0; 361 OPTION(params, char *, opt) = cp; 362 continue; 363 case OPT_INT: 364 val = strtoul(option, &cp, 0); 365 if (cp > option + len || (*cp != 0 && *cp != ',')) 366 break; 367 OPTION(params, int, opt) = val; 368 if (OPTION(params, int, opt) != val) 369 /* value got truncated on int convertion */ 370 break; 371 continue; 372 default: 373 errx(1, "Internal error: option `%s' has invalid type %d", 374 opt->name, opt->type); 375 } 376 warnx("Invalid option value `%s=%.*s'", opt->name, len, option); 377 break; 378 } 379 options_usage(); 380 exit(1); 381 } 382 383 static void 384 options_usage(void) 385 { 386 int ndx; 387 const char *pfx; 388 389 warnx("Valid options are:"); 390 pfx = "\t"; 391 for (ndx = 0; options[ndx].name != 0; ndx++) { 392 fprintf(stderr, "%s%s", pfx, options[ndx].name); 393 switch (options[ndx].type) { 394 case OPT_INT: 395 fprintf(stderr, "=number"); 396 break; 397 case OPT_WORD: 398 fprintf(stderr, "=word"); 399 break; 400 case OPT_STRING: 401 fprintf(stderr, "=string"); 402 break; 403 default: 404 break; 405 } 406 if ((ndx % 5) == 4) 407 pfx = ",\n\t"; 408 else 409 pfx = ", "; 410 } 411 fprintf(stderr, "\n"); 412 } 413 414 int 415 no_setboot(ib_params *params) 416 { 417 418 assert(params != NULL); 419 420 warnx("%s: bootstrap installation is not supported", 421 params->machine->name); 422 return (0); 423 } 424 425 int 426 no_clearboot(ib_params *params) 427 { 428 429 assert(params != NULL); 430 431 warnx("%s: bootstrap removal is not supported", 432 params->machine->name); 433 return (0); 434 } 435 436 int 437 no_editboot(ib_params *params) 438 { 439 440 assert(params != NULL); 441 442 warnx("%s: bootstrap editing is not supported", 443 params->machine->name); 444 return (0); 445 } 446 447 448 static void 449 getmachine(ib_params *param, const char *mach, const char *provider) 450 { 451 int i; 452 453 assert(param != NULL); 454 assert(mach != NULL); 455 assert(provider != NULL); 456 457 for (i = 0; machines[i].name != NULL; i++) { 458 if (strcmp(machines[i].name, mach) == 0) { 459 param->machine = &machines[i]; 460 return; 461 } 462 } 463 warnx("Invalid machine `%s' from %s", mach, provider); 464 machine_usage(); 465 exit(1); 466 } 467 468 static void 469 machine_usage(void) 470 { 471 const char *prefix; 472 int i; 473 474 warnx("Supported machines are:"); 475 #define MACHS_PER_LINE 9 476 prefix=""; 477 for (i = 0; machines[i].name != NULL; i++) { 478 if (i == 0) 479 prefix="\t"; 480 else if (i % MACHS_PER_LINE) 481 prefix=", "; 482 else 483 prefix=",\n\t"; 484 fprintf(stderr, "%s%s", prefix, machines[i].name); 485 } 486 fputs("\n", stderr); 487 } 488 489 static void 490 getfstype(ib_params *param, const char *fstype, const char *provider) 491 { 492 int i; 493 494 assert(param != NULL); 495 assert(fstype != NULL); 496 assert(provider != NULL); 497 498 for (i = 0; fstypes[i].name != NULL; i++) { 499 if (strcmp(fstypes[i].name, fstype) == 0) { 500 param->fstype = &fstypes[i]; 501 return; 502 } 503 } 504 warnx("Invalid file system type `%s' from %s", fstype, provider); 505 fstype_usage(); 506 exit(1); 507 } 508 509 static void 510 fstype_usage(void) 511 { 512 const char *prefix; 513 int i; 514 515 warnx("Supported file system types are:"); 516 #define FSTYPES_PER_LINE 9 517 prefix=""; 518 for (i = 0; fstypes[i].name != NULL; i++) { 519 if (i == 0) 520 prefix="\t"; 521 else if (i % FSTYPES_PER_LINE) 522 prefix=", "; 523 else 524 prefix=",\n\t"; 525 fprintf(stderr, "%s%s", prefix, fstypes[i].name); 526 } 527 fputs("\n", stderr); 528 } 529 530 static void 531 usage(void) 532 { 533 const char *prog; 534 535 prog = getprogname(); 536 fprintf(stderr, 537 "usage: %s [-nv] [-B s2bno] [-b s1bno] [-m machine] [-o options]\n" 538 "\t\t [-t fstype] filesystem primary [secondary]\n" 539 "usage: %s -c [-nv] [-m machine] [-o options] [-t fstype] filesystem\n" 540 "usage: %s -e [-nv] [-m machine] [-o options] bootstrap\n", 541 prog, prog, prog); 542 machine_usage(); 543 fstype_usage(); 544 options_usage(); 545 exit(1); 546 } 547