1 /* $NetBSD: boot.c,v 1.27 2011/01/22 19:19:24 joerg Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1999 Eduardo E. Horvath. All rights reserved. 5 * Copyright (c) 1997 Jason R. Thorpe. All rights reserved. 6 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 7 * Copyright (C) 1995, 1996 TooLs GmbH. 8 * All rights reserved. 9 * 10 * ELF support derived from NetBSD/alpha's boot loader, written 11 * by Christopher G. Demetriou. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by TooLs GmbH. 24 * 4. The name of TooLs GmbH may not be used to endorse or promote products 25 * derived from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 33 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 34 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 35 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * First try for the boot code 41 * 42 * Input syntax is: 43 * [promdev[{:|,}partition]]/[filename] [flags] 44 */ 45 46 #include <lib/libsa/stand.h> 47 #include <lib/libsa/loadfile.h> 48 #include <lib/libkern/libkern.h> 49 50 #include <sys/param.h> 51 #include <sys/reboot.h> 52 #include <sys/disklabel.h> 53 #include <sys/boot_flag.h> 54 55 #include <machine/cpu.h> 56 #include <machine/promlib.h> 57 #include <machine/bootinfo.h> 58 #include <sparc/stand/common/isfloppy.h> 59 60 #include "boot.h" 61 #include "ofdev.h" 62 #include "openfirm.h" 63 64 65 #define COMPAT_BOOT(marks) (marks[MARK_START] == marks[MARK_ENTRY]) 66 67 68 typedef void (*entry_t)(long o0, long bootargs, long bootsize, long o3, 69 long ofw); 70 71 /* 72 * Boot device is derived from ROM provided information, or if there is none, 73 * this list is used in sequence, to find a kernel. 74 */ 75 const char *kernelnames[] = { 76 "netbsd", 77 "netbsd.gz", 78 "netbsd.old", 79 "netbsd.old.gz", 80 "onetbsd", 81 "onetbsd.gz", 82 "vmunix ", 83 #ifdef notyet 84 "netbsd.pl ", 85 "netbsd.pl.gz ", 86 "netbsd.el ", 87 "netbsd.el.gz ", 88 #endif 89 NULL 90 }; 91 92 char bootdev[PROM_MAX_PATH]; 93 bool root_fs_quickseekable = true; /* unset for tftp boots */ 94 static bool bootinfo_pass_bootdev = false; 95 96 int debug = 0; 97 int compatmode = 0; 98 extern char twiddle_toggle; 99 100 #if 0 101 static void 102 prom2boot(char *dev) 103 { 104 char *cp, *lp = 0; 105 int handle; 106 char devtype[16]; 107 108 for (cp = dev; *cp; cp++) 109 if (*cp == ':') 110 lp = cp; 111 if (!lp) 112 lp = cp; 113 *lp = 0; 114 } 115 #endif 116 117 static int 118 bootoptions(const char *ap, char *loaddev, char *kernel, char *options) 119 { 120 int v = 0; 121 const char *start1 = NULL, *end1 = NULL, *start2 = NULL, *end2 = NULL; 122 const char *path; 123 char partition, *pp; 124 125 *kernel = '\0'; 126 *options = '\0'; 127 128 if (ap == NULL) { 129 return (0); 130 } 131 132 while (*ap == ' ') { 133 ap++; 134 } 135 136 if (*ap != '-') { 137 start1 = ap; 138 while (*ap != '\0' && *ap != ' ') { 139 ap++; 140 } 141 end1 = ap; 142 143 while (*ap != '\0' && *ap == ' ') { 144 ap++; 145 } 146 147 if (*ap != '-') { 148 start2 = ap; 149 while (*ap != '\0' && *ap != ' ') { 150 ap++; 151 } 152 end2 = ap; 153 while (*ap != '\0' && *ap == ' ') { 154 ap++; 155 } 156 } 157 } 158 if (end2 == start2) { 159 start2 = end2 = NULL; 160 } 161 if (end1 == start1) { 162 start1 = end1 = NULL; 163 } 164 165 if (start1 == NULL) { 166 /* only options */ 167 } else if (start2 == NULL) { 168 memcpy(kernel, start1, (end1 - start1)); 169 kernel[end1 - start1] = '\0'; 170 path = filename(kernel, &partition); 171 if (path == NULL) { 172 strcpy(loaddev, kernel); 173 kernel[0] = '\0'; 174 } else if (path != kernel) { 175 /* copy device part */ 176 memcpy(loaddev, kernel, path-kernel); 177 loaddev[path-kernel] = '\0'; 178 if (partition) { 179 pp = loaddev + strlen(loaddev); 180 pp[0] = ':'; 181 pp[1] = partition; 182 pp[2] = '\0'; 183 } 184 /* and kernel path */ 185 strcpy(kernel, path); 186 } 187 } else { 188 memcpy(loaddev, start1, (end1-start1)); 189 loaddev[end1-start1] = '\0'; 190 memcpy(kernel, start2, (end2 - start2)); 191 kernel[end2 - start2] = '\0'; 192 } 193 194 twiddle_toggle = 1; 195 strcpy(options, ap); 196 while (*ap != '\0' && *ap != ' ' && *ap != '\t' && *ap != '\n') { 197 BOOT_FLAG(*ap, v); 198 switch(*ap++) { 199 case 'D': 200 debug = 2; 201 break; 202 case 'C': 203 compatmode = 1; 204 break; 205 case 'T': 206 twiddle_toggle = 1 - twiddle_toggle; 207 break; 208 default: 209 break; 210 } 211 } 212 213 if (((v & RB_KDB) != 0) && (debug == 0)) { 214 debug = 1; 215 } 216 217 DPRINTF(("bootoptions: device='%s', kernel='%s', options='%s'\n", 218 loaddev, kernel, options)); 219 return (v); 220 } 221 222 /* 223 * The older (those relying on ofwboot v1.8 and earlier) kernels can't handle 224 * ksyms information unless it resides in a dedicated memory allocated from 225 * PROM and aligned on NBPG boundary. This is because the kernels calculate 226 * their ends on their own, they use address of 'end[]' reference which follows 227 * text segment. Ok, allocate some memory from PROM and copy symbol information 228 * over there. 229 */ 230 static void 231 ksyms_copyout(void **ssym, void **esym) 232 { 233 void *addr; 234 int kssize = (int)(long)(*esym - *ssym + 1); 235 236 DPRINTF(("ksyms_copyout(): ssym = %p, esym = %p, kssize = %d\n", 237 *ssym, *esym, kssize)); 238 239 if ( (addr = OF_claim(0, kssize, NBPG)) == (void *)-1) { 240 panic("ksyms_copyout(): no space for symbol table"); 241 } 242 243 memcpy(addr, *ssym, kssize); 244 *ssym = addr; 245 *esym = addr + kssize - 1; 246 247 DPRINTF(("ksyms_copyout(): ssym = %p, esym = %p\n", *ssym, *esym)); 248 } 249 250 /* 251 * Prepare boot information and jump directly to the kernel. 252 */ 253 static void 254 jump_to_kernel(u_long *marks, char *kernel, char *args, void *ofw) 255 { 256 extern char end[]; 257 int l, machine_tag; 258 long newargs[4]; 259 void *ssym, *esym; 260 vaddr_t bootinfo; 261 struct btinfo_symtab bi_sym; 262 struct btinfo_kernend bi_kend; 263 char *cp; 264 char bootline[PROM_MAX_PATH * 2]; 265 266 /* Compose kernel boot line. */ 267 strncpy(bootline, kernel, sizeof(bootline)); 268 cp = bootline + strlen(bootline); 269 if (*args) { 270 *cp++ = ' '; 271 strncpy(bootline, args, sizeof(bootline) - (cp - bootline)); 272 } 273 *cp = 0; args = bootline; 274 275 /* Record symbol information in the bootinfo. */ 276 bootinfo = bi_init(marks[MARK_END]); 277 bi_sym.nsym = marks[MARK_NSYM]; 278 bi_sym.ssym = marks[MARK_SYM]; 279 bi_sym.esym = marks[MARK_END]; 280 bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym)); 281 bi_kend.addr= bootinfo + BOOTINFO_SIZE; 282 bi_add(&bi_kend, BTINFO_KERNEND, sizeof(bi_kend)); 283 if (bootinfo_pass_bootdev) { 284 struct { 285 struct btinfo_common common; 286 char name[256]; 287 } info; 288 289 strcpy(info.name, bootdev); 290 bi_add(&info, BTINFO_BOOTDEV, strlen(bootdev) 291 +sizeof(struct btinfo_bootdev)); 292 } 293 294 sparc64_finalize_tlb(marks[MARK_DATA]); 295 sparc64_bi_add(); 296 297 ssym = (void*)(long)marks[MARK_SYM]; 298 esym = (void*)(long)marks[MARK_END]; 299 300 DPRINTF(("jump_to_kernel(): ssym = %p, esym = %p\n", ssym, esym)); 301 302 /* Adjust ksyms pointers, if needed. */ 303 if (COMPAT_BOOT(marks) || compatmode) { 304 ksyms_copyout(&ssym, &esym); 305 } 306 307 freeall(); 308 /* 309 * When we come in args consists of a pointer to the boot 310 * string. We need to fix it so it takes into account 311 * other params such as romp. 312 */ 313 314 /* 315 * Stash pointer to end of symbol table after the argument 316 * strings. 317 */ 318 l = strlen(args) + 1; 319 memcpy(args + l, &esym, sizeof(esym)); 320 l += sizeof(esym); 321 322 /* 323 * Tell the kernel we're an OpenFirmware system. 324 */ 325 machine_tag = SPARC_MACHINE_OPENFIRMWARE; 326 memcpy(args + l, &machine_tag, sizeof(machine_tag)); 327 l += sizeof(machine_tag); 328 329 /* 330 * Since we don't need the boot string (we can get it from /chosen) 331 * we won't pass it in. Just pass in esym and magic # 332 */ 333 newargs[0] = SPARC_MACHINE_OPENFIRMWARE; 334 newargs[1] = (long)esym; 335 newargs[2] = (long)ssym; 336 newargs[3] = (long)(void*)bootinfo; 337 args = (char *)newargs; 338 l = sizeof(newargs); 339 340 /* if -D is set then pause in the PROM. */ 341 if (debug > 1) callrom(); 342 343 /* 344 * Jump directly to the kernel. Solaris kernel and Sun PROM 345 * flash updates expect ROMP vector in %o0, so we do. Format 346 * of other parameters and their order reflect OF_chain() 347 * symantics since this is what older NetBSD kernels rely on. 348 * (see sparc64/include/bootinfo.h for specification). 349 */ 350 DPRINTF(("jump_to_kernel(%lx, %lx, %lx, %lx, %lx) @ %p\n", (long)ofw, 351 (long)args, (long)l, (long)ofw, (long)ofw, 352 (void*)marks[MARK_ENTRY])); 353 (*(entry_t)marks[MARK_ENTRY])((long)ofw, (long)args, (long)l, (long)ofw, 354 (long)ofw); 355 printf("Returned from kernel entry point!\n"); 356 } 357 358 static void 359 start_kernel(char *kernel, char *bootline, void *ofw, int isfloppy) 360 { 361 int fd; 362 u_long marks[MARK_MAX]; 363 int flags = LOAD_ALL; 364 if (isfloppy) 365 flags &= ~LOAD_BACKWARDS; 366 367 /* 368 * First, load headers using default allocator and check whether kernel 369 * entry address matches kernel text load address. If yes, this is the 370 * old kernel designed for ofwboot v1.8 and therefore it must be mapped 371 * by PROM. Otherwise, map the kernel with 4MB permanent pages. 372 */ 373 loadfile_set_allocator(LOADFILE_NOP_ALLOCATOR); 374 if ( (fd = loadfile(kernel, marks, LOAD_HDR|COUNT_TEXT)) != -1) { 375 if (COMPAT_BOOT(marks) || compatmode) { 376 (void)printf("[c] "); 377 loadfile_set_allocator(LOADFILE_OFW_ALLOCATOR); 378 } else { 379 loadfile_set_allocator(LOADFILE_MMU_ALLOCATOR); 380 } 381 (void)printf("Loading %s: ", kernel); 382 383 if (fdloadfile(fd, marks, flags) != -1) { 384 close(fd); 385 jump_to_kernel(marks, kernel, bootline, ofw); 386 } 387 } 388 (void)printf("Failed to load '%s'.\n", kernel); 389 } 390 391 static void 392 help(void) 393 { 394 printf( "enter a special command\n" 395 " halt\n" 396 " exit\n" 397 " to return to OpenFirmware\n" 398 " ?\n" 399 " help\n" 400 " to display this message\n" 401 "or a boot specification:\n" 402 " [device] [kernel] [options]\n" 403 "\n" 404 "for example:\n" 405 " disk:a netbsd -s\n"); 406 } 407 408 static void 409 do_config_command(const char *cmd, const char *arg) 410 { 411 DPRINTF(("do_config_command: %s\n", cmd)); 412 if (strcmp(cmd, "bootpartition") == 0) { 413 char *c; 414 415 DPRINTF(("switching boot partition to %s from %s\n", 416 arg, bootdev)); 417 c = strrchr(bootdev, ':'); 418 if (!c) return; 419 if (c[1] == 0) return; 420 if (strlen(arg) > strlen(c)) return; 421 strcpy(c, arg); 422 DPRINTF(("new boot device: %s\n", bootdev)); 423 bootinfo_pass_bootdev = true; 424 } 425 } 426 427 static void 428 parse_boot_config(char *cfg, size_t len) 429 { 430 const char *cmd = NULL, *arg = NULL; 431 432 while (len) { 433 if (isspace(*cfg)) { 434 cfg++; len--; continue; 435 } 436 if (*cfg == ';' || *cfg == '#') { 437 while (len && *cfg != '\r' && *cfg != '\n') { 438 cfg++; len--; 439 } 440 continue; 441 } 442 cmd = cfg; 443 while (len && !isspace(*cfg)) { 444 cfg++; len--; 445 } 446 *cfg = 0; 447 if (len > 0) { 448 cfg++; len--; 449 while (isspace(*cfg) && len) { 450 cfg++; len--; 451 } 452 if (len > 0 ) { 453 arg = cfg; 454 while (len && !isspace(*cfg)) { 455 cfg++; len--; 456 } 457 *cfg = 0; 458 } 459 } 460 do_config_command(cmd, arg); 461 if (len > 0) { 462 cfg++; len--; 463 } 464 } 465 } 466 467 static void 468 check_boot_config(void) 469 { 470 int fd, err, off, len; 471 struct stat st; 472 char *bc; 473 474 if (!root_fs_quickseekable) return; 475 DPRINTF(("checking for /boot.cfg...\n")); 476 fd = open("/boot.cfg", 0); 477 if (fd < 0) return; 478 DPRINTF(("found /boot.cfg\n")); 479 if (fstat(fd, &st) == -1 || st.st_size > 32*1024) { 480 close(fd); 481 return; 482 } 483 bc = alloc(st.st_size+1); 484 off = 0; 485 do { 486 len = read(fd, bc+off, 1024); 487 if (len <= 0) 488 break; 489 off += len; 490 } while (len > 0); 491 bc[off] = 0; 492 close(fd); 493 494 parse_boot_config(bc, off); 495 } 496 497 void 498 main(void *ofw) 499 { 500 int boothowto, i = 0, isfloppy; 501 502 char kernel[PROM_MAX_PATH]; 503 char bootline[PROM_MAX_PATH]; 504 505 /* Initialize OpenFirmware */ 506 romp = ofw; 507 prom_init(); 508 509 printf("\r>> %s, Revision %s\n", bootprog_name, bootprog_rev); 510 511 /* Figure boot arguments */ 512 strncpy(bootdev, prom_getbootpath(), sizeof(bootdev) - 1); 513 boothowto = bootoptions(prom_getbootargs(), bootdev, kernel, bootline); 514 isfloppy = bootdev_isfloppy(bootdev); 515 516 for (;; *kernel = '\0') { 517 if (boothowto & RB_ASKNAME) { 518 char *cp, cmdline[PROM_MAX_PATH]; 519 520 printf("Boot: "); 521 gets(cmdline); 522 523 if (!strcmp(cmdline,"exit") || 524 !strcmp(cmdline,"halt")) { 525 prom_halt(); 526 } else if (!strcmp(cmdline, "?") || 527 !strcmp(cmdline, "help")) { 528 help(); 529 continue; 530 } 531 532 boothowto = bootoptions(cmdline, bootdev, kernel, 533 bootline); 534 boothowto |= RB_ASKNAME; 535 i = 0; 536 } 537 538 if (*kernel == '\0') { 539 if (kernelnames[i] == NULL) { 540 boothowto |= RB_ASKNAME; 541 continue; 542 } 543 strncpy(kernel, kernelnames[i++], PROM_MAX_PATH); 544 } else if (i == 0) { 545 /* 546 * Kernel name was passed via command line -- ask user 547 * again if requested image fails to boot. 548 */ 549 boothowto |= RB_ASKNAME; 550 } 551 552 check_boot_config(); 553 start_kernel(kernel, bootline, ofw, isfloppy); 554 555 /* 556 * Try next name from kernel name list if not in askname mode, 557 * enter askname on reaching list's end. 558 */ 559 if ((boothowto & RB_ASKNAME) == 0 && (kernelnames[i] != NULL)) { 560 printf(": trying %s...\n", kernelnames[i]); 561 } else { 562 printf("\n"); 563 boothowto |= RB_ASKNAME; 564 } 565 } 566 567 (void)printf("Boot failed! Exiting to the Firmware.\n"); 568 prom_halt(); 569 } 570