1 /* $NetBSD: boot2.c,v 1.55 2011/06/23 12:07:00 mrg 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/ufs.h> 76 #include <lib/libkern/libkern.h> 77 78 #include <libi386.h> 79 #include <bootmod.h> 80 #include <bootmenu.h> 81 #include <vbe.h> 82 #include "devopen.h" 83 84 #ifdef SUPPORT_PS2 85 #include <biosmca.h> 86 #endif 87 88 extern struct x86_boot_params boot_params; 89 90 extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[]; 91 92 int errno; 93 94 int boot_biosdev; 95 daddr_t boot_biossector; 96 97 static const char * const names[][2] = { 98 { "netbsd", "netbsd.gz" }, 99 { "onetbsd", "onetbsd.gz" }, 100 { "netbsd.old", "netbsd.old.gz" }, 101 }; 102 103 #define NUMNAMES (sizeof(names)/sizeof(names[0])) 104 #define DEFFILENAME names[0][0] 105 106 #define MAXDEVNAME 16 107 108 static char *default_devname; 109 static int default_unit, default_partition; 110 static const char *default_filename; 111 112 char *sprint_bootsel(const char *); 113 void bootit(const char *, int, int); 114 void print_banner(void); 115 void boot2(int, uint64_t); 116 117 void command_help(char *); 118 void command_ls(char *); 119 void command_quit(char *); 120 void command_boot(char *); 121 void command_dev(char *); 122 void command_consdev(char *); 123 void command_modules(char *); 124 void command_multiboot(char *); 125 126 const struct bootblk_command commands[] = { 127 { "help", command_help }, 128 { "?", command_help }, 129 { "ls", command_ls }, 130 { "quit", command_quit }, 131 { "boot", command_boot }, 132 { "dev", command_dev }, 133 { "consdev", command_consdev }, 134 { "modules", command_modules }, 135 { "load", module_add }, 136 { "multiboot", command_multiboot }, 137 { "vesa", command_vesa }, 138 { "splash", splash_add }, 139 { "userconf", userconf_add }, 140 { NULL, NULL }, 141 }; 142 143 int 144 parsebootfile(const char *fname, char **fsname, char **devname, 145 int *unit, int *partition, const char **file) 146 { 147 const char *col; 148 149 *fsname = "ufs"; 150 *devname = default_devname; 151 *unit = default_unit; 152 *partition = default_partition; 153 *file = default_filename; 154 155 if (fname == NULL) 156 return 0; 157 158 if ((col = strchr(fname, ':')) != NULL) { /* device given */ 159 static char savedevname[MAXDEVNAME+1]; 160 int devlen; 161 int u = 0, p = 0; 162 int i = 0; 163 164 devlen = col - fname; 165 if (devlen > MAXDEVNAME) 166 return EINVAL; 167 168 #define isvalidname(c) ((c) >= 'a' && (c) <= 'z') 169 if (!isvalidname(fname[i])) 170 return EINVAL; 171 do { 172 savedevname[i] = fname[i]; 173 i++; 174 } while (isvalidname(fname[i])); 175 savedevname[i] = '\0'; 176 177 #define isnum(c) ((c) >= '0' && (c) <= '9') 178 if (i < devlen) { 179 if (!isnum(fname[i])) 180 return EUNIT; 181 do { 182 u *= 10; 183 u += fname[i++] - '0'; 184 } while (isnum(fname[i])); 185 } 186 187 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z') 188 if (i < devlen) { 189 if (!isvalidpart(fname[i])) 190 return EPART; 191 p = fname[i++] - 'a'; 192 } 193 194 if (i != devlen) 195 return ENXIO; 196 197 *devname = savedevname; 198 *unit = u; 199 *partition = p; 200 fname = col + 1; 201 } 202 203 if (*fname) 204 *file = fname; 205 206 return 0; 207 } 208 209 char * 210 sprint_bootsel(const char *filename) 211 { 212 char *fsname, *devname; 213 int unit, partition; 214 const char *file; 215 static char buf[80]; 216 217 if (parsebootfile(filename, &fsname, &devname, &unit, 218 &partition, &file) == 0) { 219 sprintf(buf, "%s%d%c:%s", devname, unit, 'a' + partition, file); 220 return buf; 221 } 222 return "(invalid)"; 223 } 224 225 static void 226 clearit(void) 227 { 228 229 if (bootconf.clear) 230 clear_pc_screen(); 231 } 232 233 void 234 bootit(const char *filename, int howto, int tell) 235 { 236 237 if (tell) { 238 printf("booting %s", sprint_bootsel(filename)); 239 if (howto) 240 printf(" (howto 0x%x)", howto); 241 printf("\n"); 242 } 243 244 if (exec_netbsd(filename, 0, howto, boot_biosdev < 0x80, clearit) < 0) 245 printf("boot: %s: %s\n", sprint_bootsel(filename), 246 strerror(errno)); 247 else 248 printf("boot returned\n"); 249 } 250 251 void 252 print_banner(void) 253 { 254 255 clearit(); 256 #ifndef SMALL 257 int n; 258 if (bootconf.banner[0]) { 259 for (n = 0; bootconf.banner[n] && n < MAXBANNER; n++) 260 printf("%s\n", bootconf.banner[n]); 261 } else { 262 #endif /* !SMALL */ 263 printf("\n" 264 ">> %s, Revision %s (from NetBSD %s)\n" 265 ">> Memory: %d/%d k\n", 266 bootprog_name, bootprog_rev, bootprog_kernrev, 267 getbasemem(), getextmem()); 268 269 #ifndef SMALL 270 } 271 #endif /* !SMALL */ 272 } 273 274 /* 275 * Called from the initial entry point boot_start in biosboot.S 276 * 277 * biosdev: BIOS drive number the system booted from 278 * biossector: Sector number of the NetBSD partition 279 */ 280 void 281 boot2(int biosdev, uint64_t biossector) 282 { 283 extern char twiddle_toggle; 284 int currname; 285 char c; 286 287 twiddle_toggle = 1; /* no twiddling until we're ready */ 288 289 initio(boot_params.bp_consdev); 290 291 #ifdef SUPPORT_PS2 292 biosmca(); 293 #endif 294 gateA20(); 295 296 boot_modules_enabled = !(boot_params.bp_flags 297 & X86_BP_FLAGS_NOMODULES); 298 if (boot_params.bp_flags & X86_BP_FLAGS_RESET_VIDEO) 299 biosvideomode(); 300 301 vbe_init(); 302 303 /* need to remember these */ 304 boot_biosdev = biosdev; 305 boot_biossector = biossector; 306 307 /* try to set default device to what BIOS tells us */ 308 bios2dev(biosdev, biossector, &default_devname, &default_unit, 309 &default_partition); 310 311 /* if the user types "boot" without filename */ 312 default_filename = DEFFILENAME; 313 314 #ifndef SMALL 315 if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) { 316 parsebootconf(BOOTCONF); 317 } else { 318 bootconf.timeout = boot_params.bp_timeout; 319 } 320 321 322 /* 323 * If console set in boot.cfg, switch to it. 324 * This will print the banner, so we don't need to explicitly do it 325 */ 326 if (bootconf.consdev) 327 command_consdev(bootconf.consdev); 328 else 329 print_banner(); 330 331 /* Display the menu, if applicable */ 332 twiddle_toggle = 0; 333 if (bootconf.nummenu > 0) { 334 /* Does not return */ 335 doboottypemenu(); 336 } 337 338 #else 339 twiddle_toggle = 0; 340 print_banner(); 341 #endif 342 343 printf("Press return to boot now, any other key for boot menu\n"); 344 for (currname = 0; currname < NUMNAMES; currname++) { 345 printf("booting %s - starting in ", 346 sprint_bootsel(names[currname][0])); 347 348 #ifdef SMALL 349 c = awaitkey(boot_params.bp_timeout, 1); 350 #else 351 c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1); 352 #endif 353 if ((c != '\r') && (c != '\n') && (c != '\0')) { 354 if ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0) { 355 /* do NOT ask for password */ 356 bootmenu(); /* does not return */ 357 } else { 358 /* DO ask for password */ 359 if (check_password((char *)boot_params.bp_password)) { 360 /* password ok */ 361 printf("type \"?\" or \"help\" for help.\n"); 362 bootmenu(); /* does not return */ 363 } else { 364 /* bad password */ 365 printf("Wrong password.\n"); 366 currname = 0; 367 continue; 368 } 369 } 370 } 371 372 /* 373 * try pairs of names[] entries, foo and foo.gz 374 */ 375 /* don't print "booting..." again */ 376 bootit(names[currname][0], 0, 0); 377 /* since it failed, try compressed bootfile. */ 378 bootit(names[currname][1], 0, 1); 379 } 380 381 bootmenu(); /* does not return */ 382 } 383 384 /* ARGSUSED */ 385 void 386 command_help(char *arg) 387 { 388 389 printf("commands are:\n" 390 "boot [xdNx:][filename] [-12acdqsvxz]\n" 391 " (ex. \"hd0a:netbsd.old -s\"\n" 392 "ls [path]\n" 393 "dev xd[N[x]]:\n" 394 "consdev {pc|com[0123]|com[0123]kbd|auto}\n" 395 "vesa {modenum|on|off|enabled|disabled|list}\n" 396 "modules {on|off|enabled|disabled}\n" 397 "load {path_to_module}\n" 398 "multiboot [xdNx:][filename] [<args>]\n" 399 "userconf {command}\n" 400 "help|?\n" 401 "quit\n"); 402 } 403 404 void 405 command_ls(char *arg) 406 { 407 const char *save = default_filename; 408 409 default_filename = "/"; 410 ufs_ls(arg); 411 default_filename = save; 412 } 413 414 /* ARGSUSED */ 415 void 416 command_quit(char *arg) 417 { 418 419 printf("Exiting...\n"); 420 delay(1000000); 421 reboot(); 422 /* Note: we shouldn't get to this point! */ 423 panic("Could not reboot!"); 424 } 425 426 void 427 command_boot(char *arg) 428 { 429 char *filename; 430 int howto; 431 432 if (parseboot(arg, &filename, &howto)) 433 bootit(filename, howto, (howto & AB_VERBOSE) != 0); 434 } 435 436 void 437 command_dev(char *arg) 438 { 439 static char savedevname[MAXDEVNAME + 1]; 440 char *fsname, *devname; 441 const char *file; /* dummy */ 442 443 if (*arg == '\0') { 444 biosdisk_probe(); 445 printf("default %s%d%c\n", default_devname, default_unit, 446 'a' + default_partition); 447 return; 448 } 449 450 if (strchr(arg, ':') == NULL || 451 parsebootfile(arg, &fsname, &devname, &default_unit, 452 &default_partition, &file)) { 453 command_help(NULL); 454 return; 455 } 456 457 /* put to own static storage */ 458 strncpy(savedevname, devname, MAXDEVNAME + 1); 459 default_devname = savedevname; 460 } 461 462 static const struct cons_devs { 463 const char *name; 464 u_int tag; 465 } cons_devs[] = { 466 { "pc", CONSDEV_PC }, 467 { "com0", CONSDEV_COM0 }, 468 { "com1", CONSDEV_COM1 }, 469 { "com2", CONSDEV_COM2 }, 470 { "com3", CONSDEV_COM3 }, 471 { "com0kbd", CONSDEV_COM0KBD }, 472 { "com1kbd", CONSDEV_COM1KBD }, 473 { "com2kbd", CONSDEV_COM2KBD }, 474 { "com3kbd", CONSDEV_COM3KBD }, 475 { "auto", CONSDEV_AUTO }, 476 { NULL, 0 } 477 }; 478 479 void 480 command_consdev(char *arg) 481 { 482 const struct cons_devs *cdp; 483 484 for (cdp = cons_devs; cdp->name; cdp++) { 485 if (strcmp(arg, cdp->name) == 0) { 486 initio(cdp->tag); 487 print_banner(); 488 return; 489 } 490 } 491 printf("invalid console device.\n"); 492 } 493 494 void 495 command_modules(char *arg) 496 { 497 498 if (strcmp(arg, "enabled") == 0 || 499 strcmp(arg, "on") == 0) 500 boot_modules_enabled = true; 501 else if (strcmp(arg, "disabled") == 0 || 502 strcmp(arg, "off") == 0) 503 boot_modules_enabled = false; 504 else 505 printf("invalid flag, must be 'enabled' or 'disabled'.\n"); 506 } 507 508 void 509 command_multiboot(char *arg) 510 { 511 char *filename; 512 513 filename = arg; 514 if (exec_multiboot(filename, gettrailer(arg)) < 0) 515 printf("multiboot: %s: %s\n", sprint_bootsel(filename), 516 strerror(errno)); 517 else 518 printf("boot returned\n"); 519 } 520 521