1 /* $NetBSD: boot2.c,v 1.70 2017/11/14 09:55:41 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Copyright (c) 2003 31 * David Laight. All rights reserved 32 * Copyright (c) 1996, 1997, 1999 33 * Matthias Drochner. All rights reserved. 34 * Copyright (c) 1996, 1997 35 * Perry E. Metzger. All rights reserved. 36 * Copyright (c) 1997 37 * Jason R. Thorpe. All rights reserved 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgements: 49 * This product includes software developed for the NetBSD Project 50 * by Matthias Drochner. 51 * This product includes software developed for the NetBSD Project 52 * by Perry E. Metzger. 53 * 4. The names of the authors may not be used to endorse or promote products 54 * derived from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 57 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 58 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 59 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 60 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 61 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 62 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 63 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 64 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 65 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 66 */ 67 68 /* Based on stand/biosboot/main.c */ 69 70 #include <sys/types.h> 71 #include <sys/reboot.h> 72 #include <sys/bootblock.h> 73 74 #include <lib/libsa/stand.h> 75 #include <lib/libsa/bootcfg.h> 76 #include <lib/libsa/ufs.h> 77 #include <lib/libkern/libkern.h> 78 79 #include <libi386.h> 80 #include <bootmod.h> 81 #include <bootmenu.h> 82 #include <vbe.h> 83 #include "devopen.h" 84 85 #ifdef SUPPORT_PS2 86 #include <biosmca.h> 87 #endif 88 89 extern struct x86_boot_params boot_params; 90 91 extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[]; 92 93 int errno; 94 95 int boot_biosdev; 96 daddr_t boot_biossector; 97 98 static const char * const names[][2] = { 99 { "netbsd", "netbsd.gz" }, 100 { "onetbsd", "onetbsd.gz" }, 101 { "netbsd.old", "netbsd.old.gz" }, 102 }; 103 104 #define NUMNAMES (sizeof(names)/sizeof(names[0])) 105 #define DEFFILENAME names[0][0] 106 107 #define MAXDEVNAME 16 108 109 static char *default_devname; 110 static int default_unit, default_partition; 111 static const char *default_filename; 112 113 char *sprint_bootsel(const char *); 114 static void bootit(const char *, int); 115 void print_banner(void); 116 void boot2(int, uint64_t); 117 118 void command_help(char *); 119 #if LIBSA_ENABLE_LS_OP 120 void command_ls(char *); 121 #endif 122 void command_quit(char *); 123 void command_boot(char *); 124 void command_pkboot(char *); 125 void command_dev(char *); 126 void command_consdev(char *); 127 #ifndef SMALL 128 void command_menu(char *); 129 #endif 130 void command_modules(char *); 131 void command_multiboot(char *); 132 133 const struct bootblk_command commands[] = { 134 { "help", command_help }, 135 { "?", command_help }, 136 #if LIBSA_ENABLE_LS_OP 137 { "ls", command_ls }, 138 #endif 139 { "quit", command_quit }, 140 { "boot", command_boot }, 141 { "pkboot", command_pkboot }, 142 { "dev", command_dev }, 143 { "consdev", command_consdev }, 144 #ifndef SMALL 145 { "menu", command_menu }, 146 #endif 147 { "modules", command_modules }, 148 { "load", module_add }, 149 { "multiboot", command_multiboot }, 150 { "vesa", command_vesa }, 151 { "splash", splash_add }, 152 { "rndseed", rnd_add }, 153 { "fs", fs_add }, 154 { "userconf", userconf_add }, 155 { NULL, NULL }, 156 }; 157 158 int 159 parsebootfile(const char *fname, char **fsname, char **devname, 160 int *unit, int *partition, const char **file) 161 { 162 const char *col; 163 164 *fsname = "ufs"; 165 *devname = default_devname; 166 *unit = default_unit; 167 *partition = default_partition; 168 *file = default_filename; 169 170 if (fname == NULL) 171 return 0; 172 173 if ((col = strchr(fname, ':')) != NULL) { /* device given */ 174 static char savedevname[MAXDEVNAME+1]; 175 int devlen; 176 int u = 0, p = 0; 177 int i = 0; 178 179 devlen = col - fname; 180 if (devlen > MAXDEVNAME) 181 return EINVAL; 182 183 #define isvalidname(c) ((c) >= 'a' && (c) <= 'z') 184 if (!isvalidname(fname[i])) 185 return EINVAL; 186 do { 187 savedevname[i] = fname[i]; 188 i++; 189 } while (isvalidname(fname[i])); 190 savedevname[i] = '\0'; 191 192 #define isnum(c) ((c) >= '0' && (c) <= '9') 193 if (i < devlen) { 194 if (!isnum(fname[i])) 195 return EUNIT; 196 do { 197 u *= 10; 198 u += fname[i++] - '0'; 199 } while (isnum(fname[i])); 200 } 201 202 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z') 203 if (i < devlen) { 204 if (!isvalidpart(fname[i])) 205 return EPART; 206 p = fname[i++] - 'a'; 207 } 208 209 if (i != devlen) 210 return ENXIO; 211 212 *devname = savedevname; 213 *unit = u; 214 *partition = p; 215 fname = col + 1; 216 } 217 218 if (*fname) 219 *file = fname; 220 221 return 0; 222 } 223 224 char * 225 sprint_bootsel(const char *filename) 226 { 227 char *fsname, *devname; 228 int unit, partition; 229 const char *file; 230 static char buf[80]; 231 232 if (parsebootfile(filename, &fsname, &devname, &unit, 233 &partition, &file) == 0) { 234 snprintf(buf, sizeof(buf), "%s%d%c:%s", devname, unit, 235 'a' + partition, file); 236 return buf; 237 } 238 return "(invalid)"; 239 } 240 241 static void 242 clearit(void) 243 { 244 245 if (bootcfg_info.clear) 246 clear_pc_screen(); 247 } 248 249 static void 250 bootit(const char *filename, int howto) 251 { 252 if (howto & AB_VERBOSE) 253 printf("booting %s (howto 0x%x)\n", sprint_bootsel(filename), 254 howto); 255 256 if (exec_netbsd(filename, 0, howto, boot_biosdev < 0x80, clearit) < 0) 257 printf("boot: %s: %s\n", sprint_bootsel(filename), 258 strerror(errno)); 259 else 260 printf("boot returned\n"); 261 } 262 263 void 264 print_banner(void) 265 { 266 267 clearit(); 268 #ifndef SMALL 269 int n; 270 if (bootcfg_info.banner[0]) { 271 for (n = 0; n < BOOTCFG_MAXBANNER && bootcfg_info.banner[n]; 272 n++) 273 printf("%s\n", bootcfg_info.banner[n]); 274 } else { 275 #endif /* !SMALL */ 276 printf("\n" 277 ">> %s, Revision %s (from NetBSD %s)\n" 278 ">> Memory: %d/%d k\n", 279 bootprog_name, bootprog_rev, bootprog_kernrev, 280 getbasemem(), getextmem()); 281 282 #ifndef SMALL 283 } 284 #endif /* !SMALL */ 285 } 286 287 /* 288 * Called from the initial entry point boot_start in biosboot.S 289 * 290 * biosdev: BIOS drive number the system booted from 291 * biossector: Sector number of the NetBSD partition 292 */ 293 void 294 boot2(int biosdev, uint64_t biossector) 295 { 296 extern char twiddle_toggle; 297 int currname; 298 char c; 299 300 twiddle_toggle = 1; /* no twiddling until we're ready */ 301 302 initio(boot_params.bp_consdev); 303 304 #ifdef SUPPORT_PS2 305 biosmca(); 306 #endif 307 gateA20(); 308 309 boot_modules_enabled = !(boot_params.bp_flags 310 & X86_BP_FLAGS_NOMODULES); 311 if (boot_params.bp_flags & X86_BP_FLAGS_RESET_VIDEO) 312 biosvideomode(); 313 314 vbe_init(); 315 316 /* need to remember these */ 317 boot_biosdev = biosdev; 318 boot_biossector = biossector; 319 320 /* try to set default device to what BIOS tells us */ 321 bios2dev(biosdev, biossector, &default_devname, &default_unit, 322 &default_partition); 323 324 /* if the user types "boot" without filename */ 325 default_filename = DEFFILENAME; 326 327 #ifndef SMALL 328 if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) { 329 parsebootconf(BOOTCFG_FILENAME); 330 } else { 331 bootcfg_info.timeout = boot_params.bp_timeout; 332 } 333 334 335 /* 336 * If console set in boot.cfg, switch to it. 337 * This will print the banner, so we don't need to explicitly do it 338 */ 339 if (bootcfg_info.consdev) 340 command_consdev(bootcfg_info.consdev); 341 else 342 print_banner(); 343 344 /* Display the menu, if applicable */ 345 twiddle_toggle = 0; 346 if (bootcfg_info.nummenu > 0) { 347 /* Does not return */ 348 doboottypemenu(); 349 } 350 351 #else 352 twiddle_toggle = 0; 353 print_banner(); 354 #endif 355 356 printf("Press return to boot now, any other key for boot menu\n"); 357 for (currname = 0; currname < NUMNAMES; currname++) { 358 printf("booting %s - starting in ", 359 sprint_bootsel(names[currname][0])); 360 361 #ifdef SMALL 362 c = awaitkey(boot_params.bp_timeout, 1); 363 #else 364 c = awaitkey((bootcfg_info.timeout < 0) ? 0 365 : bootcfg_info.timeout, 1); 366 #endif 367 if ((c != '\r') && (c != '\n') && (c != '\0')) { 368 if ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0) { 369 /* do NOT ask for password */ 370 bootmenu(); /* does not return */ 371 } else { 372 /* DO ask for password */ 373 if (check_password((char *)boot_params.bp_password)) { 374 /* password ok */ 375 printf("type \"?\" or \"help\" for help.\n"); 376 bootmenu(); /* does not return */ 377 } else { 378 /* bad password */ 379 printf("Wrong password.\n"); 380 currname = 0; 381 continue; 382 } 383 } 384 } 385 386 /* 387 * try pairs of names[] entries, foo and foo.gz 388 */ 389 /* don't print "booting..." again */ 390 bootit(names[currname][0], 0); 391 /* since it failed, try compressed bootfile. */ 392 bootit(names[currname][1], AB_VERBOSE); 393 } 394 395 bootmenu(); /* does not return */ 396 } 397 398 /* ARGSUSED */ 399 void 400 command_help(char *arg) 401 { 402 403 printf("commands are:\n" 404 "boot [xdNx:][filename] [-12acdqsvxz]\n" 405 " (ex. \"hd0a:netbsd.old -s\")\n" 406 "pkboot [xdNx:][filename] [-12acdqsvxz]\n" 407 #if LIBSA_ENABLE_LS_OP 408 "ls [path]\n" 409 #endif 410 "dev xd[N[x]]:\n" 411 "consdev {pc|com[0123]|com[0123]kbd|auto}\n" 412 "vesa {modenum|on|off|enabled|disabled|list}\n" 413 #ifndef SMALL 414 "menu (reenters boot menu, if defined in boot.cfg)\n" 415 #endif 416 "modules {on|off|enabled|disabled}\n" 417 "load {path_to_module}\n" 418 "multiboot [xdNx:][filename] [<args>]\n" 419 "splash {path_to_image_file}\n" 420 "userconf {command}\n" 421 "rndseed {path_to_rndseed_file}\n" 422 "help|?\n" 423 "quit\n"); 424 } 425 426 #if LIBSA_ENABLE_LS_OP 427 void 428 command_ls(char *arg) 429 { 430 const char *save = default_filename; 431 432 default_filename = "/"; 433 ls(arg); 434 default_filename = save; 435 } 436 #endif 437 438 /* ARGSUSED */ 439 void 440 command_quit(char *arg) 441 { 442 443 printf("Exiting...\n"); 444 delay(1000000); 445 reboot(); 446 /* Note: we shouldn't get to this point! */ 447 panic("Could not reboot!"); 448 } 449 450 void 451 command_boot(char *arg) 452 { 453 char *filename; 454 int howto; 455 456 if (!parseboot(arg, &filename, &howto)) 457 return; 458 459 if (filename != NULL) { 460 bootit(filename, howto); 461 } else { 462 int i; 463 464 #ifndef SMALL 465 if (howto == 0) 466 bootdefault(); 467 #endif 468 for (i = 0; i < NUMNAMES; i++) { 469 bootit(names[i][0], howto); 470 bootit(names[i][1], howto); 471 } 472 } 473 } 474 475 void 476 command_pkboot(char *arg) 477 { 478 extern int has_prekern; 479 has_prekern = 1; 480 command_boot(arg); 481 has_prekern = 0; 482 } 483 484 void 485 command_dev(char *arg) 486 { 487 static char savedevname[MAXDEVNAME + 1]; 488 char *fsname, *devname; 489 const char *file; /* dummy */ 490 491 if (*arg == '\0') { 492 biosdisk_probe(); 493 printf("default %s%d%c\n", default_devname, default_unit, 494 'a' + default_partition); 495 return; 496 } 497 498 if (strchr(arg, ':') == NULL || 499 parsebootfile(arg, &fsname, &devname, &default_unit, 500 &default_partition, &file)) { 501 command_help(NULL); 502 return; 503 } 504 505 /* put to own static storage */ 506 strncpy(savedevname, devname, MAXDEVNAME + 1); 507 default_devname = savedevname; 508 } 509 510 static const struct cons_devs { 511 const char *name; 512 u_int tag; 513 } cons_devs[] = { 514 { "pc", CONSDEV_PC }, 515 { "com0", CONSDEV_COM0 }, 516 { "com1", CONSDEV_COM1 }, 517 { "com2", CONSDEV_COM2 }, 518 { "com3", CONSDEV_COM3 }, 519 { "com0kbd", CONSDEV_COM0KBD }, 520 { "com1kbd", CONSDEV_COM1KBD }, 521 { "com2kbd", CONSDEV_COM2KBD }, 522 { "com3kbd", CONSDEV_COM3KBD }, 523 { "auto", CONSDEV_AUTO }, 524 { NULL, 0 } 525 }; 526 527 void 528 command_consdev(char *arg) 529 { 530 const struct cons_devs *cdp; 531 532 for (cdp = cons_devs; cdp->name; cdp++) { 533 if (strcmp(arg, cdp->name) == 0) { 534 initio(cdp->tag); 535 print_banner(); 536 return; 537 } 538 } 539 printf("invalid console device.\n"); 540 } 541 542 #ifndef SMALL 543 /* ARGSUSED */ 544 void 545 command_menu(char *arg) 546 { 547 548 if (bootcfg_info.nummenu > 0) { 549 /* Does not return */ 550 doboottypemenu(); 551 } else { 552 printf("No menu defined in boot.cfg\n"); 553 } 554 } 555 #endif /* !SMALL */ 556 557 void 558 command_modules(char *arg) 559 { 560 561 if (strcmp(arg, "enabled") == 0 || 562 strcmp(arg, "on") == 0) 563 boot_modules_enabled = true; 564 else if (strcmp(arg, "disabled") == 0 || 565 strcmp(arg, "off") == 0) 566 boot_modules_enabled = false; 567 else 568 printf("invalid flag, must be 'enabled' or 'disabled'.\n"); 569 } 570 571 void 572 command_multiboot(char *arg) 573 { 574 char *filename; 575 576 filename = arg; 577 if (exec_multiboot(filename, gettrailer(arg)) < 0) 578 printf("multiboot: %s: %s\n", sprint_bootsel(filename), 579 strerror(errno)); 580 else 581 printf("boot returned\n"); 582 } 583 584