1 /* $NetBSD: main.c,v 1.14 2011/04/25 18:29:33 phx Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tohru Nishimura. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/reboot.h> 34 35 #include <lib/libsa/stand.h> 36 #include <lib/libsa/loadfile.h> 37 #include <lib/libkern/libkern.h> 38 39 #include <machine/bootinfo.h> 40 41 #include "globals.h" 42 43 static const struct bootarg { 44 const char *name; 45 int value; 46 } bootargs[] = { 47 { "multi", RB_AUTOBOOT }, 48 { "auto", RB_AUTOBOOT }, 49 { "ask", RB_ASKNAME }, 50 { "single", RB_SINGLE }, 51 { "ddb", RB_KDB }, 52 { "userconf", RB_USERCONF }, 53 { "norm", AB_NORMAL }, 54 { "quiet", AB_QUIET }, 55 { "verb", AB_VERBOSE }, 56 { "silent", AB_SILENT }, 57 { "debug", AB_DEBUG }, 58 { "altboot", -1 } 59 }; 60 61 void *bootinfo; /* low memory reserved to pass bootinfo structures */ 62 int bi_size; /* BOOTINFO_MAXSIZE */ 63 char *bi_next; 64 65 void bi_init(void *); 66 void bi_add(void *, int, int); 67 68 struct btinfo_memory bi_mem; 69 struct btinfo_console bi_cons; 70 struct btinfo_clock bi_clk; 71 struct btinfo_prodfamily bi_fam; 72 struct btinfo_bootpath bi_path; 73 struct btinfo_rootdevice bi_rdev; 74 struct btinfo_net bi_net; 75 struct btinfo_modulelist *btinfo_modulelist; 76 size_t btinfo_modulelist_size; 77 78 struct boot_module { 79 char *bm_kmod; 80 ssize_t bm_len; 81 struct boot_module *bm_next; 82 }; 83 struct boot_module *boot_modules; 84 char module_base[80]; 85 uint32_t kmodloadp; 86 int modules_enabled = 0; 87 88 void module_add(char *); 89 void module_load(char *); 90 int module_open(struct boot_module *); 91 92 void main(int, char **, char *, char *); 93 94 extern char bootprog_name[], bootprog_rev[]; 95 extern char newaltboot[], newaltboot_end[]; 96 97 struct pcidev lata[2]; 98 struct pcidev lnif[1]; 99 struct pcidev lusb[3]; 100 int nata, nnif, nusb; 101 102 int brdtype; 103 uint32_t busclock, cpuclock; 104 105 static int check_bootname(char *); 106 static int input_cmdline(char **, int); 107 static int parse_cmdline(char **, int, char *, char *); 108 static int is_space(char); 109 110 #define BNAME_DEFAULT "wd0:" 111 #define MAX_ARGS 10 112 113 void 114 main(int argc, char *argv[], char *bootargs_start, char *bootargs_end) 115 { 116 struct brdprop *brdprop; 117 unsigned long marks[MARK_MAX]; 118 char *new_argv[MAX_ARGS]; 119 ssize_t len; 120 int n, i, fd, howto; 121 char *bname; 122 123 printf("\n"); 124 printf(">> %s altboot, revision %s\n", bootprog_name, bootprog_rev); 125 126 brdprop = brd_lookup(brdtype); 127 printf(">> %s, cpu %u MHz, bus %u MHz, %dMB SDRAM\n", brdprop->verbose, 128 cpuclock / 1000000, busclock / 1000000, bi_mem.memsize >> 20); 129 130 nata = pcilookup(PCI_CLASS_IDE, lata, 2); 131 if (nata == 0) 132 nata = pcilookup(PCI_CLASS_RAID, lata, 2); 133 if (nata == 0) 134 nata = pcilookup(PCI_CLASS_MISCSTORAGE, lata, 2); 135 if (nata == 0) 136 nata = pcilookup(PCI_CLASS_SCSI, lata, 2); 137 nnif = pcilookup(PCI_CLASS_ETH, lnif, 1); 138 nusb = pcilookup(PCI_CLASS_USB, lusb, 3); 139 140 #ifdef DEBUG 141 if (nata == 0) 142 printf("No IDE/SATA found\n"); 143 else for (n = 0; n < nata; n++) { 144 int b, d, f, bdf, pvd; 145 bdf = lata[n].bdf; 146 pvd = lata[n].pvd; 147 pcidecomposetag(bdf, &b, &d, &f); 148 printf("%04x.%04x DSK %02d:%02d:%02d\n", 149 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f); 150 } 151 if (nnif == 0) 152 printf("no NET found\n"); 153 else { 154 int b, d, f, bdf, pvd; 155 bdf = lnif[0].bdf; 156 pvd = lnif[0].pvd; 157 pcidecomposetag(bdf, &b, &d, &f); 158 printf("%04x.%04x NET %02d:%02d:%02d\n", 159 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f); 160 } 161 if (nusb == 0) 162 printf("no USB found\n"); 163 else for (n = 0; n < nusb; n++) { 164 int b, d, f, bdf, pvd; 165 bdf = lusb[0].bdf; 166 pvd = lusb[0].pvd; 167 pcidecomposetag(bdf, &b, &d, &f); 168 printf("%04x.%04x USB %02d:%02d:%02d\n", 169 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f); 170 } 171 #endif 172 173 pcisetup(); 174 pcifixup(); 175 176 if (dskdv_init(&lata[0]) == 0 177 || (nata == 2 && dskdv_init(&lata[1]) == 0)) 178 printf("IDE/SATA device driver was not found\n"); 179 180 if (netif_init(&lnif[0]) == 0) 181 printf("no NET device driver was found\n"); 182 183 /* 184 * When argc is too big then it is probably a pointer, which could 185 * indicate that we were launched as a Linux kernel module using 186 * "bootm". 187 */ 188 if (argc > MAX_ARGS) { 189 if (argv != NULL) { 190 /* 191 * initrd image was loaded: assume extremely 192 * restricted firmware and boot default 193 */ 194 argc = 0; 195 } else { 196 /* parse standard Linux bootargs */ 197 argc = parse_cmdline(new_argv, MAX_ARGS, 198 bootargs_start, bootargs_end); 199 argv = new_argv; 200 } 201 } 202 203 /* wait 2s for user to enter interactive mode */ 204 for (n = 200; n >= 0; n--) { 205 if (n % 100 == 0) 206 printf("Hit any key to enter interactive mode: %d\r", 207 n / 100); 208 if (tstchar()) { 209 (void)getchar(); 210 argv = new_argv; 211 argc = input_cmdline(argv, MAX_ARGS); 212 break; 213 } 214 delay(10000); 215 } 216 putchar('\n'); 217 218 howto = RB_AUTOBOOT; /* default is autoboot = 0 */ 219 220 /* get boot options and determine bootname */ 221 for (n = 1; n < argc; n++) { 222 for (i = 0; i < sizeof(bootargs) / sizeof(bootargs[0]); i++) { 223 if (strncasecmp(argv[n], bootargs[i].name, 224 strlen(bootargs[i].name)) == 0) { 225 howto |= bootargs[i].value; 226 break; 227 } 228 } 229 if (i >= sizeof(bootargs) / sizeof(bootargs[0])) 230 break; /* break on first unknown string */ 231 } 232 if (n >= argc) 233 bname = BNAME_DEFAULT; 234 else { 235 bname = argv[n]; 236 if (check_bootname(bname) == 0) { 237 printf("%s not a valid bootname\n", bname); 238 goto loadfail; 239 } 240 } 241 242 if ((fd = open(bname, 0)) < 0) { 243 if (errno == ENOENT) 244 printf("\"%s\" not found\n", bi_path.bootpath); 245 goto loadfail; 246 } 247 printf("loading \"%s\" ", bi_path.bootpath); 248 marks[MARK_START] = 0; 249 250 if (howto == -1) { 251 /* load another altboot binary and replace ourselves */ 252 len = read(fd, (void *)0x100000, 0x1000000 - 0x100000); 253 if (len == -1) 254 goto loadfail; 255 close(fd); 256 netif_shutdown_all(); 257 258 memcpy((void *)0xf0000, newaltboot, 259 newaltboot_end - newaltboot); 260 __syncicache((void *)0xf0000, newaltboot_end - newaltboot); 261 printf("Restarting...\n"); 262 run((void *)1, argv, (void *)0x100000, (void *)len, 263 (void *)0xf0000); 264 } else if (fdloadfile(fd, marks, LOAD_KERNEL) < 0) 265 goto loadfail; 266 close(fd); 267 268 printf("entry=%p, ssym=%p, esym=%p\n", 269 (void *)marks[MARK_ENTRY], 270 (void *)marks[MARK_SYM], 271 (void *)marks[MARK_END]); 272 273 bootinfo = (void *)0x4000; 274 bi_init(bootinfo); 275 bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons)); 276 bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem)); 277 bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk)); 278 bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path)); 279 bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev)); 280 bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam)); 281 if (brdtype == BRD_SYNOLOGY || brdtype == BRD_DLINKDSM) { 282 /* need to set this MAC address in kernel driver later */ 283 bi_add(&bi_net, BTINFO_NET, sizeof(bi_net)); 284 } 285 286 if (modules_enabled) { 287 module_add(fsmod); 288 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0) 289 module_add(fsmod2); 290 kmodloadp = marks[MARK_END]; 291 btinfo_modulelist = NULL; 292 module_load(bname); 293 if (btinfo_modulelist != NULL && btinfo_modulelist->num > 0) 294 bi_add(btinfo_modulelist, BTINFO_MODULELIST, 295 btinfo_modulelist_size); 296 } 297 298 netif_shutdown_all(); 299 300 __syncicache((void *)marks[MARK_ENTRY], 301 (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]); 302 303 run((void *)marks[MARK_SYM], (void *)marks[MARK_END], 304 (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]); 305 306 /* should never come here */ 307 printf("exec returned. Restarting...\n"); 308 _rtt(); 309 310 loadfail: 311 printf("load failed. Restarting...\n"); 312 _rtt(); 313 } 314 315 void 316 bi_init(void *addr) 317 { 318 struct btinfo_magic bi_magic; 319 320 memset(addr, 0, BOOTINFO_MAXSIZE); 321 bi_next = (char *)addr; 322 bi_size = 0; 323 324 bi_magic.magic = BOOTINFO_MAGIC; 325 bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic)); 326 } 327 328 void 329 bi_add(void *new, int type, int size) 330 { 331 struct btinfo_common *bi; 332 333 if (bi_size + size > BOOTINFO_MAXSIZE) 334 return; /* XXX error? */ 335 336 bi = new; 337 bi->next = size; 338 bi->type = type; 339 memcpy(bi_next, new, size); 340 bi_next += size; 341 } 342 343 void 344 module_add(char *name) 345 { 346 struct boot_module *bm, *bmp; 347 348 while (*name == ' ' || *name == '\t') 349 ++name; 350 351 bm = alloc(sizeof(struct boot_module) + strlen(name) + 1); 352 if (bm == NULL) { 353 printf("couldn't allocate module %s\n", name); 354 return; 355 } 356 357 bm->bm_kmod = (char *)(bm + 1); 358 bm->bm_len = -1; 359 bm->bm_next = NULL; 360 strcpy(bm->bm_kmod, name); 361 if ((bmp = boot_modules) == NULL) 362 boot_modules = bm; 363 else { 364 while (bmp->bm_next != NULL) 365 bmp = bmp->bm_next; 366 bmp->bm_next = bm; 367 } 368 } 369 370 #define PAGE_SIZE 4096 371 #define alignpg(x) (((x)+PAGE_SIZE-1) & ~(PAGE_SIZE-1)) 372 373 void 374 module_load(char *kernel_path) 375 { 376 struct boot_module *bm; 377 struct bi_modulelist_entry *bi; 378 struct stat st; 379 char *p; 380 int size, fd; 381 382 strcpy(module_base, kernel_path); 383 if ((p = strchr(module_base, ':')) == NULL) 384 return; /* eeh?! */ 385 p += 1; 386 size = sizeof(module_base) - (p - module_base); 387 388 if (netbsd_version / 1000000 % 100 == 99) { 389 /* -current */ 390 snprintf(p, size, 391 "/stand/sandpoint/%d.%d.%d/modules", 392 netbsd_version / 100000000, 393 netbsd_version / 1000000 % 100, 394 netbsd_version / 100 % 100); 395 } 396 else if (netbsd_version != 0) { 397 /* release */ 398 snprintf(p, size, 399 "/stand/sandpoint/%d.%d/modules", 400 netbsd_version / 100000000, 401 netbsd_version / 1000000 % 100); 402 } 403 404 /* 405 * 1st pass; determine module existence 406 */ 407 size = 0; 408 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) { 409 fd = module_open(bm); 410 if (fd == -1) 411 continue; 412 if (fstat(fd, &st) == -1 || st.st_size == -1) { 413 printf("WARNING: couldn't stat %s\n", bm->bm_kmod); 414 close(fd); 415 continue; 416 } 417 bm->bm_len = (int)st.st_size; 418 close(fd); 419 size += sizeof(struct bi_modulelist_entry); 420 } 421 if (size == 0) 422 return; 423 424 size += sizeof(struct btinfo_modulelist); 425 btinfo_modulelist = alloc(size); 426 if (btinfo_modulelist == NULL) { 427 printf("WARNING: couldn't allocate module list\n"); 428 return; 429 } 430 btinfo_modulelist_size = size; 431 btinfo_modulelist->num = 0; 432 433 /* 434 * 2nd pass; load modules into memory 435 */ 436 kmodloadp = alignpg(kmodloadp); 437 bi = (struct bi_modulelist_entry *)(btinfo_modulelist + 1); 438 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) { 439 if (bm->bm_len == -1) 440 continue; /* already found unavailable */ 441 fd = module_open(bm); 442 printf("module \"%s\" ", bm->bm_kmod); 443 size = read(fd, (char *)kmodloadp, SSIZE_MAX); 444 if (size < bm->bm_len) 445 printf("WARNING: couldn't load"); 446 else { 447 snprintf(bi->kmod, sizeof(bi->kmod), bm->bm_kmod); 448 bi->type = BI_MODULE_ELF; 449 bi->len = size; 450 bi->base = kmodloadp; 451 btinfo_modulelist->num += 1; 452 printf("loaded at 0x%08x size 0x%x", kmodloadp, size); 453 kmodloadp += alignpg(size); 454 bi += 1; 455 } 456 printf("\n"); 457 close(fd); 458 } 459 btinfo_modulelist->endpa = kmodloadp; 460 } 461 462 int 463 module_open(struct boot_module *bm) 464 { 465 char path[80]; 466 int fd; 467 468 snprintf(path, sizeof(path), 469 "%s/%s/%s.kmod", module_base, bm->bm_kmod, bm->bm_kmod); 470 fd = open(path, 0); 471 return fd; 472 } 473 474 #if 0 475 static const char *cmdln[] = { 476 "console=ttyS0,115200 root=/dev/sda1 rw initrd=0x200000,2M", 477 "console=ttyS0,115200 root=/dev/nfs ip=dhcp" 478 }; 479 480 void 481 mkatagparams(unsigned addr, char *kcmd) 482 { 483 struct tag { 484 unsigned siz; 485 unsigned tag; 486 unsigned val[1]; 487 }; 488 struct tag *p; 489 #define ATAG_CORE 0x54410001 490 #define ATAG_MEM 0x54410002 491 #define ATAG_INITRD 0x54410005 492 #define ATAG_CMDLINE 0x54410009 493 #define ATAG_NONE 0x00000000 494 #define tagnext(p) (struct tag *)((unsigned *)(p) + (p)->siz) 495 #define tagsize(n) (2 + (n)) 496 497 p = (struct tag *)addr; 498 p->tag = ATAG_CORE; 499 p->siz = tagsize(3); 500 p->val[0] = 0; /* flags */ 501 p->val[1] = 0; /* pagesize */ 502 p->val[2] = 0; /* rootdev */ 503 p = tagnext(p); 504 p->tag = ATAG_MEM; 505 p->siz = tagsize(2); 506 p->val[0] = 64 * 1024 * 1024; 507 p->val[1] = 0; /* start */ 508 p = tagnext(p); 509 if (kcmd != NULL) { 510 p = tagnext(p); 511 p->tag = ATAG_CMDLINE; 512 p->siz = tagsize((strlen(kcmd) + 1 + 3) >> 2); 513 strcpy((void *)p->val, kcmd); 514 } 515 p = tagnext(p); 516 p->tag = ATAG_NONE; 517 p->siz = 0; 518 } 519 #endif 520 521 void * 522 allocaligned(size_t size, size_t align) 523 { 524 uint32_t p; 525 526 if (align-- < 2) 527 return alloc(size); 528 p = (uint32_t)alloc(size + align); 529 return (void *)((p + align) & ~align); 530 } 531 532 static int hex2nibble(char c) 533 { 534 535 if (c >= 'a') 536 c &= ~0x20; 537 if (c >= 'A' && c <= 'F') 538 c -= 'A' - ('9' + 1); 539 else if (c < '0' || c > '9') 540 return -1; 541 542 return c - '0'; 543 } 544 545 uint32_t 546 read_hex(const char *s) 547 { 548 int n; 549 uint32_t val; 550 551 val = 0; 552 while ((n = hex2nibble(*s++)) >= 0) 553 val = (val << 4) | n; 554 return val; 555 } 556 557 static int 558 check_bootname(char *s) 559 { 560 /* 561 * nfs: 562 * nfs:<bootfile> 563 * tftp: 564 * tftp:<bootfile> 565 * wd[N[P]]:<bootfile> 566 * mem:<address> 567 * 568 * net is a synonym of nfs. 569 */ 570 if (strncmp(s, "nfs:", 4) == 0 || strncmp(s, "net:", 4) == 0 || 571 strncmp(s, "tftp:", 5) == 0 || strncmp(s, "mem:", 4) == 0) 572 return 1; 573 if (s[0] == 'w' && s[1] == 'd') { 574 s += 2; 575 if (*s != ':' && *s >= '0' && *s <= '3') { 576 ++s; 577 if (*s != ':' && *s >= 'a' && *s <= 'p') 578 ++s; 579 } 580 return *s == ':'; 581 } 582 return 0; 583 } 584 585 static int input_cmdline(char **argv, int maxargc) 586 { 587 char *cmdline; 588 589 printf("\nbootargs> "); 590 cmdline = alloc(256); 591 gets(cmdline); 592 593 return parse_cmdline(argv, maxargc, cmdline, 594 cmdline + strlen(cmdline)); 595 } 596 597 static int 598 parse_cmdline(char **argv, int maxargc, char *p, char *end) 599 { 600 int argc; 601 602 argv[0] = ""; 603 for (argc = 1; argc < maxargc && p < end; argc++) { 604 while (is_space(*p)) 605 p++; 606 if (p >= end) 607 break; 608 argv[argc] = p; 609 while (!is_space(*p) && p < end) 610 p++; 611 *p++ = '\0'; 612 } 613 614 return argc; 615 } 616 617 static int 618 is_space(char c) 619 { 620 return c > '\0' && c <= ' '; 621 } 622