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