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