exec.c revision 1.76
1/* $NetBSD: exec.c,v 1.76 2020/04/04 19:50:54 christos 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 a NetBSD ELF kernel. The low level startup is done in startprog.S. 92 * This is a special version of exec.c to support use of XMS. 93 */ 94 95#include <sys/param.h> 96#include <sys/reboot.h> 97 98#include <lib/libsa/stand.h> 99#include <lib/libkern/libkern.h> 100 101#include "loadfile.h" 102#include "libi386.h" 103#include "bootinfo.h" 104#include "bootmod.h" 105#include "vbe.h" 106#ifdef SUPPORT_PS2 107#include "biosmca.h" 108#endif 109#ifdef EFIBOOT 110#include "efiboot.h" 111#undef DEBUG /* XXX */ 112#endif 113 114#define BOOT_NARGS 6 115 116#ifndef PAGE_SIZE 117#define PAGE_SIZE 4096 118#endif 119 120#define MODULE_WARNING_SEC 5 121 122#define MAXMODNAME 32 /* from <sys/module.h> */ 123 124extern struct btinfo_console btinfo_console; 125 126boot_module_t *boot_modules; 127bool boot_modules_enabled = true; 128bool kernel_loaded; 129 130typedef struct userconf_command { 131 char *uc_text; 132 size_t uc_len; 133 struct userconf_command *uc_next; 134} userconf_command_t; 135userconf_command_t *userconf_commands = NULL; 136 137struct btinfo_framebuffer btinfo_framebuffer; 138 139struct btinfo_modulelist *btinfo_modulelist; 140static size_t btinfo_modulelist_size; 141static uint32_t image_end; 142static char module_base[64] = "/"; 143static int howto; 144 145static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL; 146static size_t btinfo_userconfcommands_size = 0; 147 148static void module_init(const char *); 149static void module_add_common(const char *, uint8_t); 150 151static void userconf_init(void); 152 153static void extract_device(const char *, char *, size_t); 154static void module_base_path(char *, size_t, const char *); 155static int module_open(boot_module_t *, int, const char *, const char *, 156 bool); 157 158void 159framebuffer_configure(struct btinfo_framebuffer *fb) 160{ 161 if (fb) 162 btinfo_framebuffer = *fb; 163 else { 164 btinfo_framebuffer.physaddr = 0; 165 btinfo_framebuffer.flags = 0; 166 } 167} 168 169void 170module_add(char *name) 171{ 172 return module_add_common(name, BM_TYPE_KMOD); 173} 174 175void 176splash_add(char *name) 177{ 178 return module_add_common(name, BM_TYPE_IMAGE); 179} 180 181void 182rnd_add(char *name) 183{ 184 return module_add_common(name, BM_TYPE_RND); 185} 186 187void 188fs_add(char *name) 189{ 190 return module_add_common(name, BM_TYPE_FS); 191} 192 193/* 194 * Add a /-separated list of module names to the boot list 195 */ 196void 197module_add_split(const char *name, uint8_t type) 198{ 199 char mod_name[MAXMODNAME]; 200 int i; 201 const char *mp = name; 202 char *ep; 203 204 while (*mp) { /* scan list of module names */ 205 i = MAXMODNAME; 206 ep = mod_name; 207 while (--i) { /* scan for end of first name */ 208 *ep = *mp; 209 if (*ep == '/') /* NUL-terminate the name */ 210 *ep = '\0'; 211 212 if (*ep == 0 ) { /* add non-empty name */ 213 if (ep != mod_name) 214 module_add_common(mod_name, type); 215 break; 216 } 217 ep++; mp++; 218 } 219 if (*ep != 0) { 220 printf("module name too long\n"); 221 return; 222 } 223 if (*mp == '/') { /* skip separator if more */ 224 mp++; 225 } 226 } 227} 228 229static void 230module_add_common(const char *name, uint8_t type) 231{ 232 boot_module_t *bm, *bmp; 233 size_t len; 234 char *str; 235 236 while (*name == ' ' || *name == '\t') 237 ++name; 238 239 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) 240 if (bm->bm_type == type && strcmp(bm->bm_path, name) == 0) 241 return; 242 243 bm = alloc(sizeof(boot_module_t)); 244 len = strlen(name) + 1; 245 str = alloc(len); 246 if (bm == NULL || str == NULL) { 247 printf("couldn't allocate module\n"); 248 return; 249 } 250 memcpy(str, name, len); 251 bm->bm_path = str; 252 bm->bm_next = NULL; 253 bm->bm_type = type; 254 if (boot_modules == NULL) 255 boot_modules = bm; 256 else { 257 for (bmp = boot_modules; bmp->bm_next; 258 bmp = bmp->bm_next) 259 ; 260 bmp->bm_next = bm; 261 } 262} 263 264void 265userconf_add(char *cmd) 266{ 267 userconf_command_t *uc; 268 size_t len; 269 char *text; 270 271 while (*cmd == ' ' || *cmd == '\t') 272 ++cmd; 273 274 uc = alloc(sizeof(*uc)); 275 if (uc == NULL) { 276 printf("couldn't allocate command\n"); 277 return; 278 } 279 280 len = strlen(cmd) + 1; 281 text = alloc(len); 282 if (text == NULL) { 283 dealloc(uc, sizeof(*uc)); 284 printf("couldn't allocate command\n"); 285 return; 286 } 287 memcpy(text, cmd, len); 288 289 uc->uc_text = text; 290 uc->uc_len = len; 291 uc->uc_next = NULL; 292 293 if (userconf_commands == NULL) 294 userconf_commands = uc; 295 else { 296 userconf_command_t *ucp; 297 for (ucp = userconf_commands; ucp->uc_next != NULL; 298 ucp = ucp->uc_next) 299 ; 300 ucp->uc_next = uc; 301 } 302} 303 304struct btinfo_prekern bi_prekern; 305int has_prekern = 0; 306 307static int 308common_load_prekern(const char *file, u_long *basemem, u_long *extmem, 309 physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX]) 310{ 311 paddr_t kernpa_start, kernpa_end; 312 char prekernpath[] = "/prekern"; 313 u_long prekern_start; 314 int fd, flags; 315 316 *extmem = getextmem(); 317 *basemem = getbasemem(); 318 319 marks[MARK_START] = loadaddr; 320 321 /* Load the prekern (static) */ 322 flags = LOAD_KERNEL & ~(LOAD_HDR|LOAD_SYM); 323 if ((fd = loadfile(prekernpath, marks, flags)) == -1) 324 return errno; 325 close(fd); 326 327 prekern_start = marks[MARK_START]; 328 329 /* The kernel starts at 2MB. */ 330 marks[MARK_START] = loadaddr; 331 marks[MARK_END] = loadaddr + (1UL << 21); 332 kernpa_start = (1UL << 21); 333 334 /* Load the kernel (dynamic) */ 335 flags = (LOAD_KERNEL | LOAD_DYN) & ~(floppy ? LOAD_BACKWARDS : 0); 336 if ((fd = loadfile(file, marks, flags)) == -1) 337 return errno; 338 close(fd); 339 340 kernpa_end = marks[MARK_END] - loadaddr; 341 342 /* If the root fs type is unusual, load its module. */ 343 if (fsmod != NULL) 344 module_add_split(fsmod, BM_TYPE_KMOD); 345 346 bi_prekern.kernpa_start = kernpa_start; 347 bi_prekern.kernpa_end = kernpa_end; 348 BI_ADD(&bi_prekern, BTINFO_PREKERN, sizeof(struct btinfo_prekern)); 349 350 /* 351 * Gather some information for the kernel. Do this after the 352 * "point of no return" to avoid memory leaks. 353 * (but before DOS might be trashed in the XMS case) 354 */ 355#ifdef PASS_BIOSGEOM 356 bi_getbiosgeom(); 357#endif 358#ifdef PASS_MEMMAP 359 bi_getmemmap(); 360#endif 361 362 marks[MARK_START] = prekern_start; 363 marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(int) - 1)) & 364 (-sizeof(int)); 365 image_end = marks[MARK_END]; 366 kernel_loaded = true; 367 368 return 0; 369} 370 371static int 372common_load_kernel(const char *file, u_long *basemem, u_long *extmem, 373 physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX]) 374{ 375 int fd; 376#ifdef XMS 377 u_long xmsmem; 378 physaddr_t origaddr = loadaddr; 379#endif 380 381 *extmem = getextmem(); 382 *basemem = getbasemem(); 383 384#ifdef XMS 385 if ((getextmem1() == 0) && (xmsmem = checkxms())) { 386 u_long kernsize; 387 388 /* 389 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because 390 * getextmem() is getextmem1(). Without, the "smart" 391 * methods could fail to report all memory as well. 392 * xmsmem is a few kB less than the actual size, but 393 * better than nothing. 394 */ 395 if (xmsmem > *extmem) 396 *extmem = xmsmem; 397 /* 398 * Get the size of the kernel 399 */ 400 marks[MARK_START] = loadaddr; 401 if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1) 402 return errno; 403 close(fd); 404 405 kernsize = marks[MARK_END]; 406 kernsize = (kernsize + 1023) / 1024; 407 408 loadaddr = xmsalloc(kernsize); 409 if (!loadaddr) 410 return ENOMEM; 411 } 412#endif 413 marks[MARK_START] = loadaddr; 414 if ((fd = loadfile(file, marks, 415 LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1) 416 return errno; 417 418 close(fd); 419 420 /* If the root fs type is unusual, load its module. */ 421 if (fsmod != NULL) 422 module_add_split(fsmod, BM_TYPE_KMOD); 423 424 /* 425 * Gather some information for the kernel. Do this after the 426 * "point of no return" to avoid memory leaks. 427 * (but before DOS might be trashed in the XMS case) 428 */ 429#ifdef PASS_BIOSGEOM 430 bi_getbiosgeom(); 431#endif 432#ifdef PASS_MEMMAP 433 bi_getmemmap(); 434#endif 435 436#ifdef XMS 437 if (loadaddr != origaddr) { 438 /* 439 * We now have done our last DOS IO, so we may 440 * trash the OS. Copy the data from the temporary 441 * buffer to its real address. 442 */ 443 marks[MARK_START] -= loadaddr; 444 marks[MARK_END] -= loadaddr; 445 marks[MARK_SYM] -= loadaddr; 446 marks[MARK_END] -= loadaddr; 447 ppbcopy(loadaddr, origaddr, marks[MARK_END]); 448 } 449#endif 450 marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & 451 (-sizeof(int)); 452 image_end = marks[MARK_END]; 453 kernel_loaded = true; 454 455 return 0; 456} 457 458int 459exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy, 460 void (*callback)(void)) 461{ 462 uint32_t boot_argv[BOOT_NARGS]; 463 u_long marks[MARK_MAX]; 464 struct btinfo_symtab btinfo_symtab; 465 u_long extmem; 466 u_long basemem; 467 int error; 468#ifdef EFIBOOT 469 int i; 470#endif 471 472#ifdef DEBUG 473 printf("exec: file=%s loadaddr=0x%lx\n", file ? file : "NULL", 474 loadaddr); 475#endif 476 477 BI_ALLOC(BTINFO_MAX); 478 479 BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console)); 480 481 howto = boothowto; 482 483 memset(marks, 0, sizeof(marks)); 484 485 if (has_prekern) { 486 error = common_load_prekern(file, &basemem, &extmem, loadaddr, 487 floppy, marks); 488 } else { 489 error = common_load_kernel(file, &basemem, &extmem, loadaddr, 490 floppy, marks); 491 } 492 if (error) { 493 errno = error; 494 goto out; 495 } 496#ifdef EFIBOOT 497 /* adjust to the real load address */ 498 marks[MARK_START] -= efi_loadaddr; 499 marks[MARK_ENTRY] -= efi_loadaddr; 500 marks[MARK_DATA] -= efi_loadaddr; 501 /* MARK_NSYM */ 502 marks[MARK_SYM] -= efi_loadaddr; 503 marks[MARK_END] -= efi_loadaddr; 504#endif 505 506 boot_argv[0] = boothowto; 507 boot_argv[1] = 0; 508 boot_argv[2] = vtophys(bootinfo); /* old cyl offset */ 509 boot_argv[3] = marks[MARK_END]; 510 boot_argv[4] = extmem; 511 boot_argv[5] = basemem; 512 513 /* pull in any modules if necessary */ 514 if (boot_modules_enabled) { 515 module_init(file); 516 if (btinfo_modulelist) { 517#ifdef EFIBOOT 518 /* convert module loaded address to paddr */ 519 struct bi_modulelist_entry *bim; 520 bim = (void *)(btinfo_modulelist + 1); 521 for (i = 0; i < btinfo_modulelist->num; i++, bim++) 522 bim->base -= efi_loadaddr; 523 btinfo_modulelist->endpa -= efi_loadaddr; 524#endif 525 BI_ADD(btinfo_modulelist, BTINFO_MODULELIST, 526 btinfo_modulelist_size); 527 } 528 } 529 530 userconf_init(); 531 if (btinfo_userconfcommands != NULL) 532 BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS, 533 btinfo_userconfcommands_size); 534 535#ifdef DEBUG 536 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY], 537 marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]); 538#endif 539 540 btinfo_symtab.nsym = marks[MARK_NSYM]; 541 btinfo_symtab.ssym = marks[MARK_SYM]; 542 btinfo_symtab.esym = marks[MARK_END]; 543 BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab)); 544 545 /* set new video mode if necessary */ 546 vbe_commit(); 547 BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER, 548 sizeof(struct btinfo_framebuffer)); 549 550 if (callback != NULL) 551 (*callback)(); 552#ifdef EFIBOOT 553 /* Copy bootinfo to safe arena. */ 554 for (i = 0; i < bootinfo->nentries; i++) { 555 struct btinfo_common *bi = (void *)(u_long)bootinfo->entry[i]; 556 char *p = alloc(bi->len); 557 memcpy(p, bi, bi->len); 558 bootinfo->entry[i] = vtophys(p); 559 } 560 561 efi_kernel_start = marks[MARK_START]; 562 efi_kernel_size = image_end - (efi_loadaddr + efi_kernel_start); 563#endif 564 startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv, 565 x86_trunc_page(basemem * 1024)); 566 panic("exec returned"); 567 568out: 569 BI_FREE(); 570 bootinfo = NULL; 571 return -1; 572} 573 574static void 575extract_device(const char *path, char *buf, size_t buflen) 576{ 577 size_t i; 578 579 if (strchr(path, ':') != NULL) { 580 for (i = 0; i < buflen - 2 && path[i] != ':'; i++) 581 buf[i] = path[i]; 582 buf[i++] = ':'; 583 buf[i] = '\0'; 584 } else 585 buf[0] = '\0'; 586} 587 588static const char * 589module_path(boot_module_t *bm, const char *kdev, const char *base_path) 590{ 591 static char buf[256]; 592 char name_buf[256], dev_buf[64]; 593 const char *name, *name2, *p; 594 595 name = bm->bm_path; 596 for (name2 = name; *name2; ++name2) { 597 if (*name2 == ' ' || *name2 == '\t') { 598 strlcpy(name_buf, name, sizeof(name_buf)); 599 if ((uintptr_t)name2 - (uintptr_t)name < sizeof(name_buf)) 600 name_buf[name2 - name] = '\0'; 601 name = name_buf; 602 break; 603 } 604 } 605 if ((p = strchr(name, ':')) != NULL) { 606 /* device specified, use it */ 607 if (p[1] == '/') 608 snprintf(buf, sizeof(buf), "%s", name); 609 else { 610 p++; 611 extract_device(name, dev_buf, sizeof(dev_buf)); 612 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod", 613 dev_buf, base_path, p, p); 614 } 615 } else { 616 /* device not specified; load from kernel device if known */ 617 if (name[0] == '/') 618 snprintf(buf, sizeof(buf), "%s%s", kdev, name); 619 else 620 snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod", 621 kdev, base_path, name, name); 622 } 623 624 return buf; 625} 626 627static int 628module_open(boot_module_t *bm, int mode, const char *kdev, 629 const char *base_path, bool doload) 630{ 631 int fd; 632 const char *path; 633 634 /* check the expanded path first */ 635 path = module_path(bm, kdev, base_path); 636 fd = open(path, mode); 637 if (fd != -1) { 638 if ((howto & AB_SILENT) == 0 && doload) 639 printf("Loading %s ", path); 640 } else { 641 /* now attempt the raw path provided */ 642 fd = open(bm->bm_path, mode); 643 if (fd != -1 && (howto & AB_SILENT) == 0 && doload) 644 printf("Loading %s ", bm->bm_path); 645 } 646 if (!doload && fd == -1) { 647 printf("WARNING: couldn't open %s", bm->bm_path); 648 if (strcmp(bm->bm_path, path) != 0) 649 printf(" (%s)", path); 650 printf("\n"); 651 } 652 return fd; 653} 654 655static void 656module_base_path(char *buf, size_t bufsize, const char *kernel_path) 657{ 658#ifdef KERNEL_DIR 659 /* we cheat here, because %.* does not work with the mini printf */ 660 char *ptr = strrchr(kernel_path, '/'); 661 if (ptr) *ptr = '\0'; 662 snprintf(buf, bufsize, "%s/modules", kernel_path); 663 if (ptr) *ptr = '/'; 664#else 665 const char *machine; 666 667 switch (netbsd_elf_class) { 668 case ELFCLASS32: 669 machine = "i386"; 670 break; 671 case ELFCLASS64: 672 machine = "amd64"; 673 break; 674 default: 675 machine = "generic"; 676 break; 677 } 678 if (netbsd_version / 1000000 % 100 == 99) { 679 /* -current */ 680 snprintf(buf, bufsize, 681 "/stand/%s/%d.%d.%d/modules", machine, 682 netbsd_version / 100000000, 683 netbsd_version / 1000000 % 100, 684 netbsd_version / 100 % 100); 685 } else if (netbsd_version != 0) { 686 /* release */ 687 snprintf(buf, bufsize, 688 "/stand/%s/%d.%d/modules", machine, 689 netbsd_version / 100000000, 690 netbsd_version / 1000000 % 100); 691 } 692#endif 693} 694 695static void 696module_init(const char *kernel_path) 697{ 698 struct bi_modulelist_entry *bi; 699 struct stat st; 700 char kdev[64]; 701 char *buf; 702 boot_module_t *bm; 703 ssize_t len; 704 off_t off; 705 int err, fd, nfail = 0; 706 707 extract_device(kernel_path, kdev, sizeof(kdev)); 708 module_base_path(module_base, sizeof(module_base), kernel_path); 709 710 /* First, see which modules are valid and calculate btinfo size */ 711 len = sizeof(struct btinfo_modulelist); 712 for (bm = boot_modules; bm; bm = bm->bm_next) { 713 fd = module_open(bm, 0, kdev, module_base, false); 714 if (fd == -1) { 715 bm->bm_len = -1; 716 ++nfail; 717 continue; 718 } 719 err = fstat(fd, &st); 720 if (err == -1 || st.st_size == -1) { 721 printf("WARNING: couldn't stat %s\n", bm->bm_path); 722 close(fd); 723 bm->bm_len = -1; 724 ++nfail; 725 continue; 726 } 727 bm->bm_len = st.st_size; 728 close(fd); 729 len += sizeof(struct bi_modulelist_entry); 730 } 731 732 /* Allocate the module list */ 733 btinfo_modulelist = alloc(len); 734 if (btinfo_modulelist == NULL) { 735 printf("WARNING: couldn't allocate module list\n"); 736 wait_sec(MODULE_WARNING_SEC); 737 return; 738 } 739 memset(btinfo_modulelist, 0, len); 740 btinfo_modulelist_size = len; 741 742 /* Fill in btinfo structure */ 743 buf = (char *)btinfo_modulelist; 744 btinfo_modulelist->num = 0; 745 off = sizeof(struct btinfo_modulelist); 746 747 for (bm = boot_modules; bm; bm = bm->bm_next) { 748 if (bm->bm_len == -1) 749 continue; 750 fd = module_open(bm, 0, kdev, module_base, true); 751 if (fd == -1) 752 continue; 753 image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); 754 len = pread(fd, (void *)(uintptr_t)image_end, SSIZE_MAX); 755 if (len < bm->bm_len) { 756 if ((howto & AB_SILENT) != 0) 757 printf("Loading %s ", bm->bm_path); 758 printf(" FAILED\n"); 759 } else { 760 btinfo_modulelist->num++; 761 bi = (struct bi_modulelist_entry *)(buf + off); 762 off += sizeof(struct bi_modulelist_entry); 763 strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1); 764 bi->base = image_end; 765 bi->len = len; 766 switch (bm->bm_type) { 767 case BM_TYPE_KMOD: 768 bi->type = BI_MODULE_ELF; 769 break; 770 case BM_TYPE_IMAGE: 771 bi->type = BI_MODULE_IMAGE; 772 break; 773 case BM_TYPE_FS: 774 bi->type = BI_MODULE_FS; 775 break; 776 case BM_TYPE_RND: 777 default: 778 /* safest -- rnd checks the sha1 */ 779 bi->type = BI_MODULE_RND; 780 break; 781 } 782 if ((howto & AB_SILENT) == 0) 783 printf(" \n"); 784 } 785 if (len > 0) 786 image_end += len; 787 close(fd); 788 } 789 btinfo_modulelist->endpa = image_end; 790 791 if (nfail > 0) { 792 printf("WARNING: %d module%s failed to load\n", 793 nfail, nfail == 1 ? "" : "s"); 794#if notyet 795 wait_sec(MODULE_WARNING_SEC); 796#endif 797 } 798} 799 800static void 801userconf_init(void) 802{ 803 size_t count, len; 804 userconf_command_t *uc; 805 char *buf; 806 off_t off; 807 808 /* Calculate the userconf commands list size */ 809 count = 0; 810 for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) 811 count++; 812 len = sizeof(*btinfo_userconfcommands) + 813 count * sizeof(struct bi_userconfcommand); 814 815 /* Allocate the userconf commands list */ 816 btinfo_userconfcommands = alloc(len); 817 if (btinfo_userconfcommands == NULL) { 818 printf("WARNING: couldn't allocate userconf commands list\n"); 819 return; 820 } 821 memset(btinfo_userconfcommands, 0, len); 822 btinfo_userconfcommands_size = len; 823 824 /* Fill in btinfo structure */ 825 buf = (char *)btinfo_userconfcommands; 826 off = sizeof(*btinfo_userconfcommands); 827 btinfo_userconfcommands->num = 0; 828 for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) { 829 struct bi_userconfcommand *bi; 830 bi = (struct bi_userconfcommand *)(buf + off); 831 strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1); 832 833 off += sizeof(*bi); 834 btinfo_userconfcommands->num++; 835 } 836} 837 838int 839exec_multiboot(const char *file, char *args) 840{ 841 physaddr_t loadaddr = 0; 842 u_long marks[MARK_MAX]; 843 u_long extmem; 844 u_long basemem; 845 struct multiboot_package *mbp = NULL; 846 847#ifndef NO_MULTIBOOT2 848 if ((mbp = probe_multiboot2(file)) != NULL) 849 goto is_multiboot; 850#endif 851 852 if ((mbp = probe_multiboot1(file)) != NULL) { 853#ifdef EFIBOOT 854 printf("EFI boot requires multiboot 2 kernel\n"); 855 goto out; 856#else 857 goto is_multiboot; 858#endif 859 } 860 861#ifndef NO_MULTIBOOT2 862 printf("%s is not a multiboot kernel\n", file); 863#else 864 printf("%s is not a multiboot 1 kernel " 865 "(multiboot 2 support is not built in)\n", file); 866#endif 867 goto out; 868 869is_multiboot: 870#ifdef EFIBOOT 871 loadaddr = efi_loadaddr; 872#endif 873 if (common_load_kernel(file, &basemem, &extmem, loadaddr, 0, marks)) 874 goto out; 875 876 if (boot_modules_enabled) 877 module_init(file); 878 879 mbp->mbp_args = args; 880 mbp->mbp_basemem = basemem; 881 mbp->mbp_extmem = extmem; 882 mbp->mbp_loadaddr = loadaddr; 883 mbp->mbp_marks = marks; 884 885 /* Only returns on error */ 886 (void)mbp->mbp_exec(mbp); 887 888out: 889 if (mbp != NULL) 890 mbp->mbp_cleanup(mbp); 891 892 return -1; 893} 894 895void 896x86_progress(const char *fmt, ...) 897{ 898 va_list ap; 899 900 if ((howto & AB_SILENT) != 0) 901 return; 902 va_start(ap, fmt); 903 vprintf(fmt, ap); 904 va_end(ap); 905} 906