1 /* $NetBSD: exec.c,v 1.46 2011/02/06 23:16:05 jmcneill 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) 1982, 1986, 1990, 1993 31 * The Regents of the University of California. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. Neither the name of the University nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 * 57 * @(#)boot.c 8.1 (Berkeley) 6/10/93 58 */ 59 60 /* 61 * Copyright (c) 1996 62 * Matthias Drochner. All rights reserved. 63 * Copyright (c) 1996 64 * Perry E. Metzger. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 75 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 76 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 77 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 78 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 79 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 80 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 81 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 82 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 83 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 84 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 85 * SUCH DAMAGE. 86 * 87 * @(#)boot.c 8.1 (Berkeley) 6/10/93 88 */ 89 90 /* 91 * starts NetBSD a.out kernel 92 * needs lowlevel startup from startprog.S 93 * This is a special version of exec.c to support use of XMS. 94 */ 95 96 #include <sys/param.h> 97 #include <sys/reboot.h> 98 #include <sys/reboot.h> 99 100 #include <machine/multiboot.h> 101 #include <machine/stdarg.h> 102 103 #include <lib/libsa/stand.h> 104 #include <lib/libkern/libkern.h> 105 106 #include "loadfile.h" 107 #include "libi386.h" 108 #include "bootinfo.h" 109 #include "bootmod.h" 110 #include "vbe.h" 111 #ifdef SUPPORT_PS2 112 #include "biosmca.h" 113 #endif 114 115 #define BOOT_NARGS 6 116 117 #ifndef PAGE_SIZE 118 #define PAGE_SIZE 4096 119 #endif 120 121 #define MODULE_WARNING_SEC 5 122 123 extern struct btinfo_console btinfo_console; 124 125 boot_module_t *boot_modules; 126 bool boot_modules_enabled = true; 127 bool kernel_loaded; 128 129 static struct btinfo_framebuffer btinfo_framebuffer; 130 131 static struct btinfo_modulelist *btinfo_modulelist; 132 static size_t btinfo_modulelist_size; 133 static uint32_t image_end; 134 static char module_base[64] = "/"; 135 static int howto; 136 137 static void module_init(const char *); 138 static void module_add_common(char *, uint8_t); 139 140 void 141 framebuffer_configure(struct btinfo_framebuffer *fb) 142 { 143 if (fb) 144 btinfo_framebuffer = *fb; 145 else { 146 btinfo_framebuffer.physaddr = 0; 147 btinfo_framebuffer.flags = 0; 148 } 149 } 150 151 void 152 module_add(char *name) 153 { 154 return module_add_common(name, BM_TYPE_KMOD); 155 } 156 157 void 158 splash_add(char *name) 159 { 160 return module_add_common(name, BM_TYPE_IMAGE); 161 } 162 163 static void 164 module_add_common(char *name, uint8_t type) 165 { 166 boot_module_t *bm, *bmp; 167 size_t len; 168 char *str; 169 170 while (*name == ' ' || *name == '\t') 171 ++name; 172 173 bm = alloc(sizeof(boot_module_t)); 174 len = strlen(name) + 1; 175 str = alloc(len); 176 if (bm == NULL || str == NULL) { 177 printf("couldn't allocate module\n"); 178 return; 179 } 180 memcpy(str, name, len); 181 bm->bm_path = str; 182 bm->bm_next = NULL; 183 bm->bm_type = type; 184 if (boot_modules == NULL) 185 boot_modules = bm; 186 else { 187 for (bmp = boot_modules; bmp->bm_next; 188 bmp = bmp->bm_next) 189 ; 190 bmp->bm_next = bm; 191 } 192 } 193 194 static int 195 common_load_kernel(const char *file, u_long *basemem, u_long *extmem, 196 physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX]) 197 { 198 int fd; 199 #ifdef XMS 200 u_long xmsmem; 201 physaddr_t origaddr = loadaddr; 202 #endif 203 204 *extmem = getextmem(); 205 *basemem = getbasemem(); 206 207 #ifdef XMS 208 if ((getextmem1() == 0) && (xmsmem = checkxms())) { 209 u_long kernsize; 210 211 /* 212 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because 213 * getextmem() is getextmem1(). Without, the "smart" 214 * methods could fail to report all memory as well. 215 * xmsmem is a few kB less than the actual size, but 216 * better than nothing. 217 */ 218 if (xmsmem > *extmem) 219 *extmem = xmsmem; 220 /* 221 * Get the size of the kernel 222 */ 223 marks[MARK_START] = loadaddr; 224 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1) 225 return EIO; 226 close(fd); 227 228 kernsize = marks[MARK_END]; 229 kernsize = (kernsize + 1023) / 1024; 230 231 loadaddr = xmsalloc(kernsize); 232 if (!loadaddr) 233 return ENOMEM; 234 } 235 #endif 236 marks[MARK_START] = loadaddr; 237 if ((fd = loadfile(file, marks, 238 LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1) 239 return EIO; 240 241 close(fd); 242 243 /* Now we know the root fs type, load modules for it. */ 244 module_add(fsmod); 245 if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0) 246 module_add(fsmod2); 247 248 /* 249 * Gather some information for the kernel. Do this after the 250 * "point of no return" to avoid memory leaks. 251 * (but before DOS might be trashed in the XMS case) 252 */ 253 #ifdef PASS_BIOSGEOM 254 bi_getbiosgeom(); 255 #endif 256 #ifdef PASS_MEMMAP 257 bi_getmemmap(); 258 #endif 259 260 #ifdef XMS 261 if (loadaddr != origaddr) { 262 /* 263 * We now have done our last DOS IO, so we may 264 * trash the OS. Copy the data from the temporary 265 * buffer to its real address. 266 */ 267 marks[MARK_START] -= loadaddr; 268 marks[MARK_END] -= loadaddr; 269 marks[MARK_SYM] -= loadaddr; 270 marks[MARK_END] -= loadaddr; 271 ppbcopy(loadaddr, origaddr, marks[MARK_END]); 272 } 273 #endif 274 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & 275 (-sizeof(int)); 276 image_end = marks[MARK_END]; 277 kernel_loaded = true; 278 279 return 0; 280 } 281 282 int 283 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy, 284 void (*callback)(void)) 285 { 286 u_long boot_argv[BOOT_NARGS]; 287 u_long marks[MARK_MAX]; 288 struct btinfo_symtab btinfo_symtab; 289 u_long extmem; 290 u_long basemem; 291 292 #ifdef DEBUG 293 printf("exec: file=%s loadaddr=0x%lx\n", 294 file ? file : "NULL", loadaddr); 295 #endif 296 297 BI_ALLOC(32); /* ??? */ 298 299 BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console)); 300 301 howto = boothowto; 302 303 if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks)) 304 goto out; 305 306 boot_argv[0] = boothowto; 307 boot_argv[1] = 0; 308 boot_argv[2] = vtophys(bootinfo); /* old cyl offset */ 309 boot_argv[3] = marks[MARK_END]; 310 boot_argv[4] = extmem; 311 boot_argv[5] = basemem; 312 313 /* pull in any modules if necessary */ 314 if (boot_modules_enabled) { 315 module_init(file); 316 if (btinfo_modulelist) { 317 BI_ADD(btinfo_modulelist, BTINFO_MODULELIST, 318 btinfo_modulelist_size); 319 } 320 } 321 322 #ifdef DEBUG 323 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY], 324 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]); 325 #endif 326 327 btinfo_symtab.nsym = marks[MARK_NSYM]; 328 btinfo_symtab.ssym = marks[MARK_SYM]; 329 btinfo_symtab.esym = marks[MARK_END]; 330 BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab)); 331 332 /* set new video mode if necessary */ 333 vbe_commit(); 334 BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER, 335 sizeof(struct btinfo_framebuffer)); 336 337 if (callback != NULL) 338 (*callback)(); 339 startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv, 340 x86_trunc_page(basemem*1024)); 341 panic("exec returned"); 342 343 out: 344 BI_FREE(); 345 bootinfo = 0; 346 return -1; 347 } 348 349 static void 350 extract_device(const char *path, char *buf, size_t buflen) 351 { 352 int i; 353 354 if (strchr(path, ':') != NULL) { 355 for (i = 0; i < buflen - 2 && path[i] != ':'; i++) 356 buf[i] = path[i]; 357 buf[i++] = ':'; 358 buf[i] = '\0'; 359 } else 360 buf[0] = '\0'; 361 } 362 363 static const char * 364 module_path(boot_module_t *bm, const char *kdev) 365 { 366 static char buf[256]; 367 char name_buf[256], dev_buf[64]; 368 const char *name, *name2, *p; 369 370 name = bm->bm_path; 371 for (name2 = name; *name2; ++name2) { 372 if (*name2 == ' ' || *name2 == '\t') { 373 strlcpy(name_buf, name, sizeof(name_buf)); 374 if (name2 - name < sizeof(name_buf)) 375 name_buf[name2 - name] = '\0'; 376 name = name_buf; 377 break; 378 } 379 } 380 if ((p = strchr(name, ':')) != NULL) { 381 /* device specified, use it */ 382 if (p[1] == '/') 383 snprintf(buf, sizeof(buf), "%s", name); 384 else { 385 p++; 386 extract_device(name, dev_buf, sizeof(dev_buf)); 387 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod", 388 dev_buf, module_base, p, p); 389 } 390 } else { 391 /* device not specified; load from kernel device if known */ 392 if (name[0] == '/') 393 snprintf(buf, sizeof(buf), "%s%s", kdev, name); 394 else 395 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod", 396 kdev, module_base, name, name); 397 } 398 399 return buf; 400 } 401 402 static int 403 module_open(boot_module_t *bm, int mode, const char *kdev, bool doload) 404 { 405 int fd; 406 const char *path; 407 408 /* check the expanded path first */ 409 path = module_path(bm, kdev); 410 fd = open(path, mode); 411 if (fd != -1) { 412 if ((howto & AB_SILENT) == 0 && doload) 413 printf("Loading %s ", path); 414 } else { 415 /* now attempt the raw path provided */ 416 fd = open(bm->bm_path, mode); 417 if (fd != -1 && (howto & AB_SILENT) == 0 && doload) 418 printf("Loading %s ", bm->bm_path); 419 } 420 if (!doload && fd == -1) { 421 printf("WARNING: couldn't open %s", bm->bm_path); 422 if (strcmp(bm->bm_path, path) != 0) 423 printf(" (%s)", path); 424 printf("\n"); 425 } 426 return fd; 427 } 428 429 static void 430 module_init(const char *kernel_path) 431 { 432 struct bi_modulelist_entry *bi; 433 struct stat st; 434 const char *machine; 435 char kdev[64]; 436 char *buf; 437 boot_module_t *bm; 438 size_t len; 439 off_t off; 440 int err, fd, nfail = 0; 441 442 extract_device(kernel_path, kdev, sizeof(kdev)); 443 444 switch (netbsd_elf_class) { 445 case ELFCLASS32: 446 machine = "i386"; 447 break; 448 case ELFCLASS64: 449 machine = "amd64"; 450 break; 451 default: 452 machine = "generic"; 453 break; 454 } 455 if (netbsd_version / 1000000 % 100 == 99) { 456 /* -current */ 457 snprintf(module_base, sizeof(module_base), 458 "/stand/%s/%d.%d.%d/modules", machine, 459 netbsd_version / 100000000, 460 netbsd_version / 1000000 % 100, 461 netbsd_version / 100 % 100); 462 } else if (netbsd_version != 0) { 463 /* release */ 464 snprintf(module_base, sizeof(module_base), 465 "/stand/%s/%d.%d/modules", machine, 466 netbsd_version / 100000000, 467 netbsd_version / 1000000 % 100); 468 } 469 470 /* First, see which modules are valid and calculate btinfo size */ 471 len = sizeof(struct btinfo_modulelist); 472 for (bm = boot_modules; bm; bm = bm->bm_next) { 473 fd = module_open(bm, 0, kdev, false); 474 if (fd == -1) { 475 bm->bm_len = -1; 476 ++nfail; 477 continue; 478 } 479 err = fstat(fd, &st); 480 if (err == -1 || st.st_size == -1) { 481 printf("WARNING: couldn't stat %s\n", bm->bm_path); 482 close(fd); 483 bm->bm_len = -1; 484 ++nfail; 485 continue; 486 } 487 bm->bm_len = st.st_size; 488 close(fd); 489 len += sizeof(struct bi_modulelist_entry); 490 } 491 492 /* Allocate the module list */ 493 btinfo_modulelist = alloc(len); 494 if (btinfo_modulelist == NULL) { 495 printf("WARNING: couldn't allocate module list\n"); 496 wait_sec(MODULE_WARNING_SEC); 497 return; 498 } 499 memset(btinfo_modulelist, 0, len); 500 btinfo_modulelist_size = len; 501 502 /* Fill in btinfo structure */ 503 buf = (char *)btinfo_modulelist; 504 btinfo_modulelist->num = 0; 505 off = sizeof(struct btinfo_modulelist); 506 507 for (bm = boot_modules; bm; bm = bm->bm_next) { 508 if (bm->bm_len == -1) 509 continue; 510 fd = module_open(bm, 0, kdev, true); 511 if (fd == -1) 512 continue; 513 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); 514 len = pread(fd, (void *)image_end, SSIZE_MAX); 515 if (len < bm->bm_len) { 516 if ((howto & AB_SILENT) != 0) 517 printf("Loading %s ", bm->bm_path); 518 printf(" FAILED\n"); 519 } else { 520 btinfo_modulelist->num++; 521 bi = (struct bi_modulelist_entry *)(buf + off); 522 off += sizeof(struct bi_modulelist_entry); 523 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1); 524 bi->base = image_end; 525 bi->len = len; 526 bi->type = bm->bm_type == BM_TYPE_KMOD ? 527 BI_MODULE_ELF : BI_MODULE_IMAGE; 528 if ((howto & AB_SILENT) == 0) 529 printf(" \n"); 530 } 531 if (len > 0) 532 image_end += len; 533 close(fd); 534 } 535 btinfo_modulelist->endpa = image_end; 536 537 if (nfail > 0) { 538 printf("WARNING: %d module%s failed to load\n", 539 nfail, nfail == 1 ? "" : "s"); 540 #if notyet 541 wait_sec(MODULE_WARNING_SEC); 542 #endif 543 } 544 } 545 546 int 547 exec_multiboot(const char *file, char *args) 548 { 549 struct multiboot_info *mbi; 550 struct multiboot_module *mbm; 551 struct bi_modulelist_entry *bim; 552 int i, len; 553 u_long marks[MARK_MAX]; 554 u_long extmem; 555 u_long basemem; 556 char *cmdline; 557 558 mbi = alloc(sizeof(struct multiboot_info)); 559 mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY; 560 561 if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks)) 562 goto out; 563 564 mbi->mi_mem_upper = extmem; 565 mbi->mi_mem_lower = basemem; 566 567 if (args) { 568 mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE; 569 len = strlen(file) + 1 + strlen(args) + 1; 570 cmdline = alloc(len); 571 snprintf(cmdline, len, "%s %s", file, args); 572 mbi->mi_cmdline = (char *) vtophys(cmdline); 573 } 574 575 /* pull in any modules if necessary */ 576 if (boot_modules_enabled) { 577 module_init(file); 578 if (btinfo_modulelist) { 579 mbm = alloc(sizeof(struct multiboot_module) * 580 btinfo_modulelist->num); 581 582 bim = (struct bi_modulelist_entry *) 583 (((char *) btinfo_modulelist) + 584 sizeof(struct btinfo_modulelist)); 585 for (i = 0; i < btinfo_modulelist->num; i++) { 586 mbm[i].mmo_start = bim->base; 587 mbm[i].mmo_end = bim->base + bim->len; 588 mbm[i].mmo_string = (char *)vtophys(bim->path); 589 mbm[i].mmo_reserved = 0; 590 bim++; 591 } 592 mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS; 593 mbi->mi_mods_count = btinfo_modulelist->num; 594 mbi->mi_mods_addr = vtophys(mbm); 595 } 596 } 597 598 #ifdef DEBUG 599 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY], 600 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]); 601 #endif 602 603 604 #if 0 605 if (btinfo_symtab.nsym) { 606 mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS; 607 mbi->mi_elfshdr_addr = marks[MARK_SYM]; 608 btinfo_symtab.nsym = marks[MARK_NSYM]; 609 btinfo_symtab.ssym = marks[MARK_SYM]; 610 btinfo_symtab.esym = marks[MARK_END]; 611 #endif 612 613 multiboot(marks[MARK_ENTRY], vtophys(mbi), 614 x86_trunc_page(mbi->mi_mem_lower*1024)); 615 panic("exec returned"); 616 617 out: 618 dealloc(mbi, 0); 619 return -1; 620 } 621 622 void 623 x86_progress(const char *fmt, ...) 624 { 625 va_list ap; 626 627 if ((howto & AB_SILENT) != 0) 628 return; 629 va_start(ap, fmt); 630 vprintf(fmt, ap); 631 va_end(ap); 632 } 633