1 /* $NetBSD: exec_multiboot2.c,v 1.5 2021/07/21 23:16:08 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2019 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 #include <sys/param.h> 30 #include <sys/reboot.h> 31 #include <sys/types.h> 32 33 #include <i386/multiboot2.h> 34 35 #include <dev/acpi/acpica.h> 36 #include <x86/acpi_machdep.h> 37 #include <dev/smbiosvar.h> 38 #include <x86/smbios_machdep.h> 39 40 #include <lib/libsa/stand.h> 41 #include <lib/libkern/libkern.h> 42 43 44 #include "loadfile.h" 45 #include "libi386.h" 46 #include "biosdisk.h" 47 #include "bootinfo.h" 48 #include "bootmod.h" 49 #include "vbe.h" 50 #ifdef EFIBOOT 51 #include "efiboot.h" 52 #endif 53 54 #define CGA_BUF 0xb8000 /* From isa_machdep.h */ 55 56 extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[]; 57 extern const uint8_t rasops_cmap[]; 58 extern struct btinfo_framebuffer btinfo_framebuffer; 59 extern struct btinfo_modulelist *btinfo_modulelist; 60 #ifdef EFIBOOT 61 extern struct btinfo_efimemmap *btinfo_efimemmap; 62 #else 63 extern struct btinfo_memmap *btinfo_memmap; 64 #endif 65 66 67 struct multiboot_package_priv { 68 struct multiboot_tag *mpp_mbi; 69 size_t mpp_mbi_len; 70 struct multiboot_header_tag_information_request*mpp_info_req; 71 struct multiboot_header_tag_address *mpp_address; 72 struct multiboot_header_tag_entry_address *mpp_entry; 73 struct multiboot_header_tag_console_flags *mpp_console; 74 struct multiboot_header_tag_framebuffer *mpp_framebuffer; 75 struct multiboot_header_tag *mpp_module_align; 76 struct multiboot_header_tag *mpp_efi_bs; 77 struct multiboot_header_tag_entry_address *mpp_entry_elf32; 78 struct multiboot_header_tag_entry_address *mpp_entry_elf64; 79 struct multiboot_header_tag_relocatable *mpp_relocatable; 80 }; 81 82 #ifndef NO_MULTIBOOT2 83 84 #ifdef MULTIBOOT2_DEBUG 85 static void 86 mbi_hexdump(char *addr, size_t len) 87 { 88 int i,j; 89 90 for (i = 0; i < len; i += 16) { 91 printf(" %p ", addr + i); 92 for (j = 0; j < 16 && i + j < len; j++) { 93 char *cp = addr + i + j; 94 printf("%s%s%x", 95 (i+j) % 4 ? "" : " ", 96 (unsigned char)*cp < 0x10 ? "0" : "", 97 (unsigned char)*cp); 98 } 99 printf("\n"); 100 } 101 102 return; 103 } 104 105 static const char * 106 mbi_tag_name(uint32_t type) 107 { 108 const char *tag_name; 109 110 switch (type) { 111 case MULTIBOOT_TAG_TYPE_END: 112 tag_name = "END"; break; 113 case MULTIBOOT_TAG_TYPE_CMDLINE: 114 tag_name = "CMDLINE"; break; 115 case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME: 116 tag_name = "BOOT_LOADER_NAME"; break; 117 case MULTIBOOT_TAG_TYPE_MODULE: 118 tag_name = "MODULE"; break; 119 case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: 120 tag_name = "BASIC_MEMINFO"; break; 121 case MULTIBOOT_TAG_TYPE_BOOTDEV: 122 tag_name = "BOOTDEV"; break; 123 case MULTIBOOT_TAG_TYPE_MMAP: 124 tag_name = "MMAP"; break; 125 case MULTIBOOT_TAG_TYPE_VBE: 126 tag_name = "VBE"; break; 127 case MULTIBOOT_TAG_TYPE_FRAMEBUFFER: 128 tag_name = "FRAMEBUFFER"; break; 129 case MULTIBOOT_TAG_TYPE_ELF_SECTIONS: 130 tag_name = "ELF_SECTIONS"; break; 131 case MULTIBOOT_TAG_TYPE_APM: 132 tag_name = "APM"; break; 133 case MULTIBOOT_TAG_TYPE_EFI32: 134 tag_name = "EFI32"; break; 135 case MULTIBOOT_TAG_TYPE_EFI64: 136 tag_name = "EFI64"; break; 137 case MULTIBOOT_TAG_TYPE_SMBIOS: 138 tag_name = "SMBIOS"; break; 139 case MULTIBOOT_TAG_TYPE_ACPI_OLD: 140 tag_name = "ACPI_OLD"; break; 141 case MULTIBOOT_TAG_TYPE_ACPI_NEW: 142 tag_name = "ACPI_NEW"; break; 143 case MULTIBOOT_TAG_TYPE_NETWORK: 144 tag_name = "NETWORK"; break; 145 case MULTIBOOT_TAG_TYPE_EFI_MMAP: 146 tag_name = "EFI_MMAP"; break; 147 case MULTIBOOT_TAG_TYPE_EFI_BS: 148 tag_name = "EFI_BS"; break; 149 case MULTIBOOT_TAG_TYPE_EFI32_IH: 150 tag_name = "EFI32_IH"; break; 151 case MULTIBOOT_TAG_TYPE_EFI64_IH: 152 tag_name = "EFI64_IH"; break; 153 case MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR: 154 tag_name = "LOAD_BASE_ADDR"; break; 155 default: 156 tag_name = "unknown"; break; 157 } 158 159 return tag_name; 160 } 161 162 static void 163 multiboot2_info_dump(uint32_t magic, char *mbi) 164 { 165 struct multiboot_tag *mbt; 166 char *cp; 167 uint32_t total_size; 168 uint32_t actual_size; 169 uint32_t reserved; 170 int i = 0; 171 172 printf("=== multiboot2 info dump start ===\n"); 173 174 if (magic != MULTIBOOT2_BOOTLOADER_MAGIC) { 175 printf("Unexpected multiboot2 magic number: 0x%x\n", magic); 176 goto out; 177 } 178 179 if (mbi != (char *)rounddown((vaddr_t)mbi, MULTIBOOT_TAG_ALIGN)) { 180 printf("mbi at %p is not properly aligned\n", mbi); 181 goto out; 182 } 183 184 total_size = *(uint32_t *)mbi; 185 reserved = *(uint32_t *)mbi + 1; 186 mbt = (struct multiboot_tag *)(uint32_t *)mbi + 2; 187 actual_size = (char *)mbt - mbi; 188 printf("mbi.total_size = %d\n", total_size); 189 printf("mbi.reserved = %d\n", reserved); 190 191 for (cp = mbi + sizeof(total_size) + sizeof(reserved); 192 cp - mbi < total_size; 193 cp = cp + roundup(mbt->size, MULTIBOOT_TAG_ALIGN)) { 194 mbt = (struct multiboot_tag *)cp; 195 actual_size += roundup(mbt->size, MULTIBOOT_TAG_ALIGN); 196 197 printf("mbi[%d].type = %d(%s), .size = %d ", 198 i++, mbt->type, mbi_tag_name(mbt->type), mbt->size); 199 200 switch (mbt->type) { 201 case MULTIBOOT_TAG_TYPE_CMDLINE: 202 printf(".string = \"%s\"\n", 203 ((struct multiboot_tag_string *)mbt)->string); 204 break; 205 case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME: 206 printf(".string = \"%s\"\n", 207 ((struct multiboot_tag_string *)mbt)->string); 208 break; 209 case MULTIBOOT_TAG_TYPE_MODULE: 210 printf(".mod_start = 0x%x, mod_end = 0x%x, " 211 "string = \"%s\"\n", 212 ((struct multiboot_tag_module *)mbt)->mod_start, 213 ((struct multiboot_tag_module *)mbt)->mod_end, 214 ((struct multiboot_tag_module *)mbt)->cmdline); 215 break; 216 case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: { 217 struct multiboot_tag_basic_meminfo *meminfo; 218 219 meminfo = (struct multiboot_tag_basic_meminfo *)mbt; 220 printf(".mem_lower = %uKB, .mem_upper = %uKB\n", 221 meminfo->mem_lower, meminfo->mem_upper); 222 break; 223 } 224 case MULTIBOOT_TAG_TYPE_BOOTDEV: 225 printf (".biosdev = 0x%x, .slice = %d, .part = %d\n", 226 ((struct multiboot_tag_bootdev *)mbt)->biosdev, 227 ((struct multiboot_tag_bootdev *)mbt)->slice, 228 ((struct multiboot_tag_bootdev *)mbt)->part); 229 break; 230 case MULTIBOOT_TAG_TYPE_MMAP: { 231 struct multiboot_tag_mmap *memmap; 232 multiboot_memory_map_t *mmap; 233 uint32_t entry_size; 234 uint32_t entry_version; 235 int j = 0; 236 237 memmap = (struct multiboot_tag_mmap *)mbt; 238 entry_size = memmap->entry_size; 239 entry_version = memmap->entry_version; 240 printf (".entry_size = %d, .entry_version = %d\n", 241 entry_size, entry_version); 242 243 for (mmap = ((struct multiboot_tag_mmap *)mbt)->entries; 244 (char *)mmap - (char *)mbt < mbt->size; 245 mmap = (void *)((char *)mmap + entry_size)) 246 printf(" entry[%d].addr = 0x%"PRIx64",\t" 247 ".len = 0x%"PRIx64",\t.type = 0x%x\n", 248 j++, (uint64_t)mmap->addr, 249 (uint64_t)mmap->len, 250 mmap->type); 251 break; 252 } 253 case MULTIBOOT_TAG_TYPE_FRAMEBUFFER: { 254 struct multiboot_tag_framebuffer *fb = (void *)mbt; 255 256 printf ("%dx%dx%d at 0x%"PRIx64"\n", 257 fb->common.framebuffer_width, 258 fb->common.framebuffer_height, 259 fb->common.framebuffer_bpp, 260 (uint64_t)fb->common.framebuffer_addr); 261 mbi_hexdump((char *)mbt, mbt->size); 262 break; 263 } 264 case MULTIBOOT_TAG_TYPE_ELF_SECTIONS: 265 printf(".num = %d, .entsize = %d, .shndx = %d\n", 266 ((struct multiboot_tag_elf_sections *)mbt)->num, 267 ((struct multiboot_tag_elf_sections *)mbt)->entsize, 268 ((struct multiboot_tag_elf_sections *)mbt)->shndx); 269 mbi_hexdump((char *)mbt, mbt->size); 270 break; 271 case MULTIBOOT_TAG_TYPE_APM: 272 printf(".version = %d, .cseg = 0x%x, .offset = 0x%x, " 273 ".cseg_16 = 0x%x, .dseg = 0x%x, .flags = 0x%x, " 274 ".cseg_len = %d, .cseg_16_len = %d, " 275 ".dseg_len = %d\n", 276 ((struct multiboot_tag_apm *)mbt)->version, 277 ((struct multiboot_tag_apm *)mbt)->cseg, 278 ((struct multiboot_tag_apm *)mbt)->offset, 279 ((struct multiboot_tag_apm *)mbt)->cseg_16, 280 ((struct multiboot_tag_apm *)mbt)->dseg, 281 ((struct multiboot_tag_apm *)mbt)->flags, 282 ((struct multiboot_tag_apm *)mbt)->cseg_len, 283 ((struct multiboot_tag_apm *)mbt)->cseg_16_len, 284 ((struct multiboot_tag_apm *)mbt)->dseg_len); 285 break; 286 case MULTIBOOT_TAG_TYPE_EFI32: 287 printf(".pointer = 0x%x\n", 288 ((struct multiboot_tag_efi32 *)mbt)->pointer); 289 break; 290 case MULTIBOOT_TAG_TYPE_EFI64: 291 printf(".pointer = 0x%"PRIx64"\n", (uint64_t) 292 ((struct multiboot_tag_efi64 *)mbt)->pointer); 293 break; 294 case MULTIBOOT_TAG_TYPE_SMBIOS: 295 printf(".major = %d, .minor = %d\n", 296 ((struct multiboot_tag_smbios *)mbt)->major, 297 ((struct multiboot_tag_smbios *)mbt)->minor); 298 mbi_hexdump((char *)mbt, mbt->size); 299 break; 300 case MULTIBOOT_TAG_TYPE_ACPI_OLD: 301 printf("\n"); 302 mbi_hexdump((char *)mbt, mbt->size); 303 break; 304 case MULTIBOOT_TAG_TYPE_ACPI_NEW: 305 printf("\n"); 306 mbi_hexdump((char *)mbt, mbt->size); 307 break; 308 case MULTIBOOT_TAG_TYPE_NETWORK: 309 printf("\n"); 310 mbi_hexdump((char *)mbt, mbt->size); 311 break; 312 case MULTIBOOT_TAG_TYPE_EFI_MMAP: 313 printf("\n"); 314 mbi_hexdump((char *)mbt, mbt->size); 315 break; 316 case MULTIBOOT_TAG_TYPE_EFI_BS: 317 printf("\n"); 318 break; 319 case MULTIBOOT_TAG_TYPE_EFI32_IH: 320 printf(".pointer = 0x%"PRIx32"\n", 321 ((struct multiboot_tag_efi32_ih *)mbt)->pointer); 322 break; 323 case MULTIBOOT_TAG_TYPE_EFI64_IH: 324 printf(".pointer = 0x%"PRIx64"\n", (uint64_t) 325 ((struct multiboot_tag_efi64_ih *)mbt)->pointer); 326 break; 327 case MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR: { 328 struct multiboot_tag_load_base_addr *ld = (void *)mbt; 329 printf(".load_base_addr = 0x%x\n", ld->load_base_addr); 330 break; 331 } 332 case MULTIBOOT_TAG_TYPE_END: 333 break; 334 default: 335 printf("\n"); 336 mbi_hexdump((char *)mbt, mbt->size); 337 break; 338 } 339 } 340 341 if (total_size != actual_size) 342 printf("Size mismatch: announded %d, actual %d\n", 343 total_size, actual_size); 344 345 out: 346 printf("=== multiboot2 info dump start ===\n"); 347 return; 348 } 349 350 #define MPP_OPT(flags) \ 351 (flags & MULTIBOOT_HEADER_TAG_OPTIONAL) ? " (opt)" : " (req)" 352 353 static 354 void multiboot2_header_dump(struct multiboot_package *mbp) 355 { 356 struct multiboot_package_priv *mpp = mbp->mbp_priv; 357 358 printf("=== multiboot2 header dump start ===\n"); 359 if (mpp->mpp_info_req) { 360 struct multiboot_header_tag_information_request *info_req; 361 size_t nreq; 362 int i; 363 364 info_req = mpp->mpp_info_req; 365 366 nreq = (info_req->size - sizeof(*info_req)) 367 / sizeof(info_req->requests[0]); 368 369 printf("Information tag request%s: ", 370 MPP_OPT(info_req->flags)); 371 for (i = 0; i < nreq; i++) 372 printf("%d(%s) ", 373 info_req->requests[i], 374 mbi_tag_name(info_req->requests[i])); 375 printf("\n"); 376 } 377 378 if (mpp->mpp_address) 379 printf("Addresses%s: header = %"PRIx32", load = %"PRIx32", " 380 "end = %"PRIx32", bss = %"PRIx32"\n", 381 MPP_OPT(mpp->mpp_address->flags), 382 mpp->mpp_address->header_addr, 383 mpp->mpp_address->load_addr, 384 mpp->mpp_address->load_end_addr, 385 mpp->mpp_address->bss_end_addr); 386 387 if (mpp->mpp_entry) 388 printf("Entry point%s: %"PRIx32"\n", 389 MPP_OPT(mpp->mpp_entry->flags), 390 mpp->mpp_entry->entry_addr); 391 392 if (mpp->mpp_console) { 393 int flags = mpp->mpp_console->console_flags; 394 char *req_flag = ""; 395 char *ega_flag = ""; 396 397 if (flags & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED) 398 ega_flag = " EGA"; 399 if (flags & MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED) 400 req_flag = " required"; 401 402 printf("Console flags%s: %s %s\n", 403 MPP_OPT(mpp->mpp_console->flags), 404 ega_flag, req_flag); 405 } 406 407 if (mpp->mpp_framebuffer) 408 printf("Framebuffer%s: width = %d, height = %d, depth = %d\n", 409 MPP_OPT(mpp->mpp_framebuffer->flags), 410 mpp->mpp_framebuffer->width, 411 mpp->mpp_framebuffer->height, 412 mpp->mpp_framebuffer->depth); 413 414 if (mpp->mpp_module_align) 415 printf("Module alignmenet%s\n", 416 MPP_OPT(mpp->mpp_module_align->flags)); 417 418 if (mpp->mpp_efi_bs) 419 printf("Do not call EFI Boot service exit%s\n", 420 MPP_OPT(mpp->mpp_efi_bs->flags)); 421 422 if (mpp->mpp_entry_elf32) 423 printf("EFI32 entry point%s: %"PRIx32"\n", 424 MPP_OPT(mpp->mpp_entry_elf32->flags), 425 mpp->mpp_entry_elf32->entry_addr); 426 427 if (mpp->mpp_entry_elf64) 428 printf("EFI64 entry point%s: %"PRIx32"\n", 429 MPP_OPT(mpp->mpp_entry_elf64->flags), 430 mpp->mpp_entry_elf64->entry_addr); 431 432 if (mpp->mpp_relocatable) { 433 char *pref; 434 435 switch (mpp->mpp_relocatable->preference) { 436 case MULTIBOOT_LOAD_PREFERENCE_NONE: pref = "none"; break; 437 case MULTIBOOT_LOAD_PREFERENCE_LOW: pref = "low"; break; 438 case MULTIBOOT_LOAD_PREFERENCE_HIGH: pref = "high"; break; 439 default: 440 pref = "(unknown)"; break; 441 } 442 printf("Relocatable%s: min_addr = %"PRIx32", " 443 "max_addr = %"PRIx32", align = %"PRIx32", pref %s\n", 444 MPP_OPT(mpp->mpp_relocatable->flags), 445 mpp->mpp_relocatable->min_addr, 446 mpp->mpp_relocatable->max_addr, 447 mpp->mpp_relocatable->align, pref); 448 } 449 450 printf("=== multiboot2 header dump end ===\n"); 451 return; 452 } 453 #endif /* MULTIBOOT2_DEBUG */ 454 455 static size_t 456 mbi_cmdline(struct multiboot_package *mbp, void *buf) 457 { 458 struct multiboot_tag_string *mbt = buf; 459 size_t cmdlen; 460 size_t len; 461 const char fmt[] = "%s %s"; 462 463 /* +1 for trailing \0 */ 464 cmdlen = snprintf(NULL, SIZE_T_MAX, fmt, mbp->mbp_file, mbp->mbp_args) 465 + 1; 466 len = sizeof(*mbt) + cmdlen; 467 468 if (mbt) { 469 mbt->type = MULTIBOOT_TAG_TYPE_CMDLINE; 470 mbt->size = len; 471 (void)snprintf(mbt->string, cmdlen, fmt, 472 mbp->mbp_file, mbp->mbp_args); 473 } 474 475 return roundup(len, MULTIBOOT_TAG_ALIGN); 476 } 477 478 static size_t 479 mbi_boot_loader_name(struct multiboot_package *mbp, void *buf) 480 { 481 struct multiboot_tag_string *mbt = buf; 482 size_t len; 483 size_t strlen; 484 const char fmt[] = "%s, Revision %s (from NetBSD %s)"; 485 486 487 /* +1 for trailing \0 */ 488 strlen = snprintf(NULL, SIZE_T_MAX, fmt, 489 bootprog_name, bootprog_rev, bootprog_kernrev) 490 + 1; 491 len = sizeof(*mbt) + strlen; 492 493 if (mbt) { 494 mbt->type = MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME; 495 mbt->size = len; 496 (void)snprintf(mbt->string, strlen, fmt, bootprog_name, 497 bootprog_rev, bootprog_kernrev); 498 } 499 500 return roundup(len, MULTIBOOT_TAG_ALIGN); 501 } 502 503 static size_t 504 mbi_modules(struct multiboot_package *mbp, void *buf) 505 { 506 struct multiboot_tag_module *mbt = buf; 507 struct bi_modulelist_entry *bim; 508 size_t len; 509 int i; 510 511 if (btinfo_modulelist == NULL) 512 return 0; 513 514 len = 0; 515 516 bim = (struct bi_modulelist_entry *)(btinfo_modulelist + 1); 517 for (i = 0; i < btinfo_modulelist->num; i++) { 518 size_t pathlen = strlen(bim->path) + 1; 519 size_t mbt_len = sizeof(*mbt) + pathlen; 520 size_t mbt_len_align = roundup(mbt_len, MULTIBOOT_TAG_ALIGN); 521 len += mbt_len_align; 522 523 if (mbt) { 524 mbt->type = MULTIBOOT_TAG_TYPE_MODULE; 525 mbt->size = mbt_len; 526 mbt->mod_start = bim->base; 527 mbt->mod_end = bim->base + bim->len; 528 strncpy(mbt->cmdline, bim->path, pathlen); 529 530 mbt = (struct multiboot_tag_module *) 531 ((char *)mbt + mbt_len_align); 532 } 533 } 534 535 return len; 536 } 537 538 static size_t 539 mbi_basic_meminfo(struct multiboot_package *mbp, void *buf) 540 { 541 struct multiboot_tag_basic_meminfo *mbt = buf; 542 size_t len; 543 544 len = sizeof(*mbt); 545 546 if (mbt) { 547 mbt->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO; 548 mbt->size = len; 549 mbt->mem_lower = mbp->mbp_basemem; 550 mbt->mem_upper = mbp->mbp_extmem; 551 } 552 553 return roundup(len, MULTIBOOT_TAG_ALIGN); 554 } 555 556 static size_t 557 mbi_bootdev(struct multiboot_package *mbp, void *buf) 558 { 559 struct multiboot_tag_bootdev *mbt = buf; 560 size_t len; 561 562 len = sizeof(*mbt); 563 564 /* 565 * According to the specification: 566 * - sub_partition is used for BSD disklabel. 567 * - Extendded MBR partitions are counted from 4 and increasing, 568 * with no subpartition. 569 */ 570 if (mbt) { 571 mbt->type = MULTIBOOT_TAG_TYPE_BOOTDEV; 572 mbt->size = len; 573 mbt->biosdev = bi_disk.biosdev; 574 mbt->slice = bi_disk.partition; 575 mbt->part = 0xFFFFFFFF; /* aka sub_partition, for disklabel */ 576 } 577 578 return roundup(len, MULTIBOOT_TAG_ALIGN); 579 return 0; 580 } 581 582 static size_t 583 mbi_mmap(struct multiboot_package *mbp, void *buf) 584 { 585 size_t len = 0; 586 struct multiboot_tag_mmap *mbt = buf; 587 struct bi_memmap_entry *memmap; 588 size_t num; 589 590 #ifndef EFIBOOT 591 bi_getmemmap(); 592 593 if (btinfo_memmap == NULL) 594 goto out; 595 596 memmap = btinfo_memmap->entry; 597 num = btinfo_memmap->num; 598 #else 599 if (efi_memory_get_memmap(&memmap, &num) != 0) 600 goto out; 601 #endif 602 603 len = sizeof(*mbt) + num * sizeof(mbt->entries[0]); 604 605 if (mbt) { 606 int i; 607 struct multiboot_mmap_entry *mbte; 608 609 mbt->type = MULTIBOOT_TAG_TYPE_MMAP; 610 mbt->size = len; 611 mbt->entry_size = sizeof(mbt->entries[0]); 612 mbt->entry_version = 0; 613 614 mbte = (struct multiboot_mmap_entry *)(mbt + 1); 615 for (i = 0; i < num; i++) { 616 mbte[i].addr = memmap[i].addr; 617 mbte[i].len = memmap[i].size; 618 switch(memmap[i].type) { 619 case BIM_Memory: 620 mbte[i].type = MULTIBOOT_MEMORY_AVAILABLE; 621 break; 622 case BIM_Reserved: 623 mbte[i].type = MULTIBOOT_MEMORY_RESERVED; 624 break; 625 case BIM_ACPI: 626 mbte[i].type = 627 MULTIBOOT_MEMORY_ACPI_RECLAIMABLE; 628 break; 629 case BIM_NVS: 630 mbte[i].type = MULTIBOOT_MEMORY_NVS; 631 break; 632 case BIM_Unusable: 633 mbte[i].type = MULTIBOOT_MEMORY_BADRAM; 634 break; 635 default: 636 mbte[i].type = MULTIBOOT_MEMORY_RESERVED; 637 break; 638 } 639 mbte[i].zero = 0; 640 } 641 } 642 #ifdef EFIBOOT 643 dealloc(memmap, num * sizeof(memmap)); 644 #endif 645 out: 646 return roundup(len, MULTIBOOT_TAG_ALIGN); 647 } 648 649 static size_t 650 mbi_vbe(struct multiboot_package *mbp, void *buf) 651 { 652 size_t len = 0; 653 654 #ifndef EFIBOOT 655 struct multiboot_tag_vbe *mbt = buf; 656 657 len = sizeof(*mbt); 658 659 if (mbt) { 660 mbt->type = MULTIBOOT_TAG_TYPE_VBE; 661 mbt->size = len; 662 mbt->vbe_mode = btinfo_framebuffer.vbemode; 663 mbt->vbe_interface_seg = 0; 664 mbt->vbe_interface_off = 0; 665 mbt->vbe_interface_len = 0; 666 biosvbe_info((struct vbeinfoblock *)&mbt->vbe_control_info); 667 biosvbe_get_mode_info(mbt->vbe_mode, 668 (struct modeinfoblock *)&mbt->vbe_mode_info); 669 } 670 #endif 671 return roundup(len, MULTIBOOT_TAG_ALIGN); 672 } 673 674 static size_t 675 mbi_framebuffer(struct multiboot_package *mbp, void *buf) 676 { 677 size_t len = 0; 678 struct multiboot_tag_framebuffer *mbt = buf; 679 struct btinfo_framebuffer *fb = &btinfo_framebuffer; 680 681 #ifndef EFIBOOT 682 struct modeinfoblock mi; 683 684 if (fb->physaddr != 0) { 685 int ret; 686 687 ret = biosvbe_get_mode_info(fb->vbemode, &mi); 688 if (ret != 0x004f) 689 return 0; 690 } 691 #endif 692 693 len = sizeof(*mbt); 694 695 if (mbt) { 696 mbt->common.type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER; 697 mbt->common.size = len; 698 mbt->common.reserved = 0; 699 700 /* 701 * No framebuffer, default to 80x25 console 702 */ 703 if (fb->physaddr == 0) { 704 int width = 80; 705 int height = 25; 706 int charlen = 2; 707 mbt->common.framebuffer_addr = CGA_BUF; 708 mbt->common.framebuffer_width = width; 709 mbt->common.framebuffer_height = height; 710 mbt->common.framebuffer_bpp = charlen * 8; 711 mbt->common.framebuffer_pitch = width * charlen; 712 mbt->common.framebuffer_type = 713 MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT; 714 } else { 715 mbt->common.framebuffer_addr = fb->physaddr; 716 mbt->common.framebuffer_pitch = fb->stride; 717 mbt->common.framebuffer_width = fb->width; 718 mbt->common.framebuffer_height = fb->height; 719 mbt->common.framebuffer_bpp = fb->depth; 720 mbt->common.framebuffer_type = 721 MULTIBOOT_FRAMEBUFFER_TYPE_RGB; 722 #ifndef EFIBOOT 723 if (mi.MemoryModel == 0x04) 724 mbt->common.framebuffer_type = 725 MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED; 726 #endif 727 } 728 729 switch (mbt->common.framebuffer_type) { 730 #ifndef EFIBOOT 731 case MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED: 732 mbt->framebuffer_palette_num_colors = 256; 733 734 for (int i = 0; i < 256; i++) { 735 mbt->framebuffer_palette[i].red = 736 rasops_cmap[3 * i]; 737 mbt->framebuffer_palette[i].green = 738 rasops_cmap[(3 * i) + 1]; 739 mbt->framebuffer_palette[i].blue = 740 rasops_cmap[(3 * i) + 2]; 741 } 742 break; 743 #endif 744 case MULTIBOOT_FRAMEBUFFER_TYPE_RGB: 745 mbt->framebuffer_red_field_position = fb->rpos; 746 mbt->framebuffer_red_mask_size = fb->rnum; 747 mbt->framebuffer_green_field_position = fb->gpos; 748 mbt->framebuffer_green_mask_size = fb->gnum; 749 mbt->framebuffer_blue_field_position = fb->bpos; 750 mbt->framebuffer_blue_mask_size = fb->bnum; 751 break; 752 default: 753 break; 754 } 755 } 756 757 return roundup(len, MULTIBOOT_TAG_ALIGN); 758 } 759 760 static size_t 761 mbi_acpi_old(struct multiboot_package *mbp, void *buf) 762 { 763 size_t len = 0; 764 struct multiboot_tag_old_acpi *mbt = buf; 765 ACPI_PHYSICAL_ADDRESS rsdp_phys = -1; 766 ACPI_RSDP_COMMON rsdp; 767 #ifdef EFIBOOT 768 const EFI_GUID acpi_table_guid = ACPI_TABLE_GUID; 769 int i; 770 771 if (ST == NULL) 772 goto out; 773 774 for (i = 0; i < ST->NumberOfTableEntries; i++) { 775 if (memcmp(&ST->ConfigurationTable[i].VendorGuid, 776 &acpi_table_guid, sizeof(acpi_table_guid)) == 0) { 777 rsdp_phys = (ACPI_PHYSICAL_ADDRESS) 778 ST->ConfigurationTable[i].VendorTable; 779 break; 780 } 781 } 782 #else 783 #ifdef notyet 784 rsdp_phys = acpi_md_OsGetRootPointer(); 785 pvbcopy((void *)(vaddr_t)rsdp_phys, &rsdp, sizeof(rsdp)); 786 787 /* Check ACPI 1.0 */ 788 if (rsdp.Revision != 0) 789 rsdp_phys = -1; 790 #endif 791 #endif 792 793 if (rsdp_phys == -1) 794 goto out; 795 796 len = sizeof(*mbt) + sizeof(rsdp); 797 if (mbt) { 798 mbt->type = MULTIBOOT_TAG_TYPE_ACPI_OLD; 799 mbt->size = len; 800 pvbcopy((void *)(vaddr_t)rsdp_phys, mbt->rsdp, sizeof(rsdp)); 801 } 802 out: 803 return roundup(len, MULTIBOOT_TAG_ALIGN); 804 } 805 806 static size_t 807 mbi_acpi_new(struct multiboot_package *mbp, void *buf) 808 { 809 size_t len = 0; 810 struct multiboot_tag_new_acpi *mbt = buf; 811 ACPI_PHYSICAL_ADDRESS rsdp_phys = -1; 812 ACPI_TABLE_RSDP rsdp; 813 #ifdef EFIBOOT 814 const EFI_GUID acpi_20_table_guid = ACPI_20_TABLE_GUID; 815 int i; 816 817 if (ST == NULL) 818 goto out; 819 820 for (i = 0; i < ST->NumberOfTableEntries; i++) { 821 if (memcmp(&ST->ConfigurationTable[i].VendorGuid, 822 &acpi_20_table_guid, sizeof(acpi_20_table_guid)) == 0) { 823 rsdp_phys = (ACPI_PHYSICAL_ADDRESS) 824 ST->ConfigurationTable[i].VendorTable; 825 break; 826 } 827 } 828 #else 829 #ifdef notyet 830 rsdp_phys = acpi_md_OsGetRootPointer(); 831 pvbcopy((void *)(vaddr_t)rsdp_phys, &rsdp, sizeof(rsdp)); 832 833 /* Check ACPI 2.0 */ 834 if (rsdp.Revision != 2) 835 rsdp_phys = -1; 836 #endif 837 #endif 838 if (rsdp_phys == -1) 839 goto out; 840 841 len = sizeof(*mbt) + sizeof(rsdp); 842 if (mbt) { 843 mbt->type = MULTIBOOT_TAG_TYPE_ACPI_NEW; 844 mbt->size = len; 845 pvbcopy((void *)(vaddr_t)rsdp_phys, mbt->rsdp, sizeof(rsdp)); 846 } 847 out: 848 return roundup(len, MULTIBOOT_TAG_ALIGN); 849 } 850 851 static size_t 852 mbi_apm(struct multiboot_package *mbp, void *buf) 853 { 854 size_t len = 0; 855 #ifdef notyet 856 struct multiboot_tag_apm *mbt = buf; 857 858 len = sizeof(*mbt): 859 860 if (mbt) { 861 mbt->type = MULTIBOOT_TAG_TYPE_A; 862 mbt->size = len; 863 mbt->version = 0; 864 mbt->cseg = 0; 865 mbt->offset = 0; 866 mbt->cseg_16 = 0; 867 mbt->dseg = 0; 868 mbt->flags = 0; 869 mbt->cseg_len = 0; 870 mbt->cseg_16_len = 0; 871 mbt->dseg_len = 0; 872 } 873 out: 874 #endif 875 return roundup(len, MULTIBOOT_TAG_ALIGN); 876 } 877 878 static size_t 879 mbi_smbios(struct multiboot_package *mbp, void *buf) 880 { 881 size_t len = 0; 882 struct multiboot_tag_smbios *mbt = buf; 883 void *smbios_phys; 884 struct smb3hdr *smbios3_phys = NULL; 885 struct smb3hdr smbios3; 886 struct smbhdr *smbios21_phys = NULL; 887 struct smbhdr smbios21; 888 size_t smbios_len; 889 int major; 890 int minor; 891 #ifdef EFIBOOT 892 const EFI_GUID smbios3_guid = SMBIOS3_TABLE_GUID; 893 const EFI_GUID smbios21_guid = SMBIOS_TABLE_GUID; 894 int i; 895 896 if (ST == NULL) 897 goto out; 898 899 for (i = 0; i < ST->NumberOfTableEntries; i++) { 900 if (memcmp(&ST->ConfigurationTable[i].VendorGuid, 901 &smbios3_guid, sizeof(smbios3_guid)) == 0) 902 smbios3_phys = ST->ConfigurationTable[i].VendorTable; 903 904 if (memcmp(&ST->ConfigurationTable[i].VendorGuid, 905 &smbios21_guid, sizeof(smbios21_guid)) == 0) 906 smbios21_phys = ST->ConfigurationTable[i].VendorTable; 907 } 908 #else 909 char *cp; 910 char line[16]; 911 const char *smbios21_anchor = "_SM_"; 912 const char *smbios3_anchor = "_SM3_"; 913 914 for (cp = (char *)SMBIOS_START; 915 cp < (char *)SMBIOS_END; 916 cp += sizeof(buf)) { 917 pvbcopy(cp, line, sizeof(line)); 918 if (memcmp(line, smbios3_anchor, strlen(smbios3_anchor)) == 0) 919 smbios3_phys = (struct smb3hdr *)cp; 920 if (memcmp(line, smbios21_anchor, strlen(smbios21_anchor)) == 0) 921 smbios21_phys = (struct smbhdr *)cp; 922 } 923 #endif 924 if (smbios3_phys != NULL) { 925 pvbcopy(smbios3_phys, &smbios3, sizeof(smbios3)); 926 smbios_len = smbios3.len; 927 major = smbios3.majrev; 928 minor = smbios3.minrev; 929 smbios_phys = smbios3_phys; 930 } else if (smbios21_phys != NULL) { 931 pvbcopy(smbios21_phys, &smbios21, sizeof(smbios21)); 932 smbios_len = smbios21.len; 933 major = smbios21.majrev; 934 minor = smbios21.minrev; 935 smbios_phys = smbios21_phys; 936 } else { 937 goto out; 938 } 939 940 len = sizeof(*mbt) + smbios_len; 941 if (mbt) { 942 mbt->type = MULTIBOOT_TAG_TYPE_SMBIOS; 943 mbt->size = len; 944 mbt->major = major; 945 mbt->minor = minor; 946 pvbcopy(smbios_phys, mbt->tables, smbios_len); 947 } 948 out: 949 return roundup(len, MULTIBOOT_TAG_ALIGN); 950 } 951 952 static size_t 953 mbi_network(struct multiboot_package *mbp, void *buf) 954 { 955 size_t len = 0; 956 #ifdef notyet 957 struct multiboot_tag_network *mbt = buf; 958 959 if (saved_dhcpack == NULL || saved_dhcpack_len == 0) 960 goto out; 961 962 len = sizeof(*mbt) + saved_dhcpack_len; 963 964 if (mbt) { 965 mbt->type = MULTIBOOT_TAG_TYPE_NETWORK; 966 mbt->size = len; 967 memcpy(mbt->dhcpack, saved_dhcpack, saved_dhcpack_len); 968 } 969 out: 970 #endif 971 return roundup(len, MULTIBOOT_TAG_ALIGN); 972 } 973 974 static size_t 975 mbi_elf_sections(struct multiboot_package *mbp, void *buf) 976 { 977 size_t len = 0; 978 struct multiboot_tag_elf_sections *mbt = buf; 979 Elf_Ehdr ehdr; 980 int class; 981 Elf32_Ehdr *ehdr32 = NULL; 982 Elf64_Ehdr *ehdr64 = NULL; 983 uint64_t shnum, shentsize, shstrndx, shoff; 984 size_t shdr_len; 985 986 if (mbp->mbp_marks[MARK_SYM] == 0) 987 goto out; 988 989 pvbcopy((void *)mbp->mbp_marks[MARK_SYM], &ehdr, sizeof(ehdr)); 990 991 /* 992 * Check this is a ELF header 993 */ 994 if (memcmp(&ehdr.e_ident, ELFMAG, SELFMAG) != 0) 995 goto out; 996 997 class = ehdr.e_ident[EI_CLASS]; 998 999 switch (class) { 1000 case ELFCLASS32: 1001 ehdr32 = (Elf32_Ehdr *)&ehdr; 1002 shnum = ehdr32->e_shnum; 1003 shentsize = ehdr32->e_shentsize; 1004 shstrndx = ehdr32->e_shstrndx; 1005 shoff = ehdr32->e_shoff; 1006 break; 1007 case ELFCLASS64: 1008 ehdr64 = (Elf64_Ehdr *)&ehdr; 1009 shnum = ehdr64->e_shnum; 1010 shentsize = ehdr64->e_shentsize; 1011 shstrndx = ehdr64->e_shstrndx; 1012 shoff = ehdr64->e_shoff; 1013 break; 1014 default: 1015 goto out; 1016 } 1017 1018 shdr_len = shnum * shentsize; 1019 if (shdr_len == 0) 1020 goto out; 1021 1022 len = sizeof(*mbt) + shdr_len; 1023 if (mbt) { 1024 char *shdr = (char *)mbp->mbp_marks[MARK_SYM] + shoff; 1025 1026 mbt->type = MULTIBOOT_TAG_TYPE_ELF_SECTIONS; 1027 mbt->size = len; 1028 mbt->num = shnum; 1029 mbt->entsize = shentsize; 1030 mbt->shndx = shstrndx; 1031 1032 pvbcopy((void *)shdr, mbt + 1, shdr_len); 1033 1034 /* 1035 * Adjust sh_addr for symtab and strtab 1036 * section that have been loaded. 1037 */ 1038 ksyms_addr_set(&ehdr, mbt + 1, 1039 (void *)mbp->mbp_marks[MARK_SYM]); 1040 } 1041 1042 out: 1043 return roundup(len, MULTIBOOT_TAG_ALIGN); 1044 } 1045 1046 static size_t 1047 mbi_end(struct multiboot_package *mbp, void *buf) 1048 { 1049 struct multiboot_tag *mbt = buf; 1050 size_t len = sizeof(*mbt); 1051 1052 if (mbt) { 1053 mbt->type = MULTIBOOT_TAG_TYPE_END; 1054 mbt->size = len; 1055 } 1056 1057 return roundup(len, MULTIBOOT_TAG_ALIGN); 1058 } 1059 1060 static size_t 1061 mbi_load_base_addr(struct multiboot_package *mbp, void *buf) 1062 { 1063 size_t len = 0; 1064 struct multiboot_tag_load_base_addr *mbt = buf; 1065 1066 len = sizeof(*mbt); 1067 1068 if (mbt) { 1069 mbt->type = MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR; 1070 mbt->size = len; 1071 mbt->load_base_addr = mbp->mbp_marks[MARK_START]; 1072 } 1073 return roundup(len, MULTIBOOT_TAG_ALIGN); 1074 } 1075 1076 #ifdef EFIBOOT 1077 /* Set if EFI ExitBootServices was not called */ 1078 static size_t 1079 mbi_efi_bs(struct multiboot_package *mbp, void *buf) 1080 { 1081 size_t len = 0; 1082 struct multiboot_tag *mbt = buf; 1083 1084 if (mbp->mbp_priv->mpp_efi_bs == NULL) 1085 goto out; 1086 1087 len = sizeof(*mbt); 1088 1089 if (mbt) { 1090 mbt->type = MULTIBOOT_TAG_TYPE_EFI_BS; 1091 mbt->size = len; 1092 } 1093 1094 out: 1095 return roundup(len, MULTIBOOT_TAG_ALIGN); 1096 } 1097 1098 1099 static size_t 1100 mbi_efi_mmap(struct multiboot_package *mbp, void *buf) 1101 { 1102 size_t len = 0; 1103 struct multiboot_tag_efi_mmap *mbt = buf; 1104 size_t memmap_len; 1105 1106 if (btinfo_efimemmap == NULL) 1107 goto out; 1108 1109 memmap_len = btinfo_efimemmap->num * btinfo_efimemmap->size; 1110 len = sizeof(*mbt) + memmap_len; 1111 1112 if (mbt) { 1113 mbt->type = MULTIBOOT_TAG_TYPE_EFI_MMAP; 1114 mbt->size = len; 1115 mbt->descr_size = btinfo_efimemmap->size; 1116 mbt->descr_vers = btinfo_efimemmap->version; 1117 memcpy(mbt + 1, btinfo_efimemmap->memmap, memmap_len); 1118 } 1119 1120 out: 1121 return roundup(len, MULTIBOOT_TAG_ALIGN); 1122 } 1123 1124 1125 1126 #ifndef __LP64__ 1127 static size_t 1128 mbi_efi32_ih(struct multiboot_package *mbp, void *buf) 1129 { 1130 size_t len = 0; 1131 struct multiboot_tag_efi32_ih *mbt = buf; 1132 1133 len = sizeof(*mbt); 1134 1135 if (mbt) { 1136 mbt->type = MULTIBOOT_TAG_TYPE_EFI32_IH; 1137 mbt->size = len; 1138 mbt->pointer = (multiboot_uint32_t)IH; 1139 } 1140 return roundup(len, MULTIBOOT_TAG_ALIGN); 1141 } 1142 1143 static size_t 1144 mbi_efi32(struct multiboot_package *mbp, void *buf) 1145 { 1146 size_t len = 0; 1147 struct multiboot_tag_efi32 *mbt = buf; 1148 1149 len = sizeof(*mbt); 1150 1151 if (mbt) { 1152 mbt->type = MULTIBOOT_TAG_TYPE_EFI32; 1153 mbt->size = len; 1154 mbt->pointer = (multiboot_uint32_t)ST; 1155 } 1156 return roundup(len, MULTIBOOT_TAG_ALIGN); 1157 } 1158 #endif 1159 1160 #ifdef __LP64__ 1161 static size_t 1162 mbi_efi64_ih(struct multiboot_package *mbp, void *buf) 1163 { 1164 size_t len = 0; 1165 struct multiboot_tag_efi64_ih *mbt = buf; 1166 1167 len = sizeof(*mbt); 1168 1169 if (mbt) { 1170 mbt->type = MULTIBOOT_TAG_TYPE_EFI64_IH; 1171 mbt->size = len; 1172 mbt->pointer = (multiboot_uint64_t)IH; 1173 } 1174 return roundup(len, MULTIBOOT_TAG_ALIGN); 1175 } 1176 1177 static size_t 1178 mbi_efi64(struct multiboot_package *mbp, void *buf) 1179 { 1180 size_t len = 0; 1181 struct multiboot_tag_efi64 *mbt = buf; 1182 1183 len = sizeof(*mbt); 1184 1185 if (mbt) { 1186 mbt->type = MULTIBOOT_TAG_TYPE_EFI64; 1187 mbt->size = len; 1188 mbt->pointer = (multiboot_uint64_t)ST; 1189 } 1190 return roundup(len, MULTIBOOT_TAG_ALIGN); 1191 } 1192 #endif /* __LP64__ */ 1193 #endif /* EFIBOOT */ 1194 1195 static bool 1196 is_tag_required(struct multiboot_package *mbp, uint16_t tag) 1197 { 1198 bool ret = false; 1199 int i; 1200 struct multiboot_header_tag_information_request *info_req; 1201 size_t nreq; 1202 1203 info_req = mbp->mbp_priv->mpp_info_req; 1204 1205 if (info_req == NULL) 1206 goto out; 1207 1208 if (info_req->flags & MULTIBOOT_HEADER_TAG_OPTIONAL) 1209 goto out; 1210 1211 nreq = (info_req->size - sizeof(*info_req)) 1212 / sizeof(info_req->requests[0]); 1213 1214 for (i = 0; i < nreq; i++) { 1215 if (info_req->requests[i] == tag) { 1216 ret = true; 1217 break; 1218 } 1219 } 1220 1221 out: 1222 return ret; 1223 } 1224 1225 static int 1226 mbi_dispatch(struct multiboot_package *mbp, uint16_t type, 1227 char *bp, size_t *total_len) 1228 { 1229 int ret = 0; 1230 size_t len = 0; 1231 1232 switch (type) { 1233 case MULTIBOOT_TAG_TYPE_END: 1234 len = mbi_end(mbp, bp); 1235 break; 1236 case MULTIBOOT_TAG_TYPE_CMDLINE: 1237 len = mbi_cmdline(mbp, bp); 1238 break; 1239 case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME: 1240 len = mbi_boot_loader_name(mbp, bp); 1241 break; 1242 case MULTIBOOT_TAG_TYPE_MODULE: 1243 len = mbi_modules(mbp, bp); 1244 break; 1245 case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: 1246 len = mbi_basic_meminfo(mbp, bp); 1247 break; 1248 case MULTIBOOT_TAG_TYPE_BOOTDEV: 1249 len = mbi_bootdev(mbp, bp); 1250 break; 1251 case MULTIBOOT_TAG_TYPE_MMAP: 1252 len = mbi_mmap(mbp, bp); 1253 break; 1254 case MULTIBOOT_TAG_TYPE_VBE: 1255 len = mbi_vbe(mbp, bp); 1256 break; 1257 case MULTIBOOT_TAG_TYPE_FRAMEBUFFER: 1258 len = mbi_framebuffer(mbp, bp); 1259 break; 1260 case MULTIBOOT_TAG_TYPE_ACPI_OLD: 1261 len = mbi_acpi_old(mbp, bp); 1262 break; 1263 case MULTIBOOT_TAG_TYPE_ACPI_NEW: 1264 len = mbi_acpi_new(mbp, bp); 1265 break; 1266 case MULTIBOOT_TAG_TYPE_ELF_SECTIONS: 1267 len = mbi_elf_sections(mbp, bp); 1268 break; 1269 case MULTIBOOT_TAG_TYPE_APM: 1270 len = mbi_apm(mbp, bp); 1271 break; 1272 case MULTIBOOT_TAG_TYPE_SMBIOS: 1273 len = mbi_smbios(mbp, bp); 1274 break; 1275 case MULTIBOOT_TAG_TYPE_NETWORK: 1276 len = mbi_network(mbp, bp); 1277 break; 1278 #ifdef EFIBOOT 1279 case MULTIBOOT_TAG_TYPE_EFI_MMAP: 1280 len = mbi_efi_mmap(mbp, bp); 1281 break; 1282 case MULTIBOOT_TAG_TYPE_EFI_BS: 1283 len = mbi_efi_bs(mbp, bp); 1284 break; 1285 #ifndef __LP64__ 1286 case MULTIBOOT_TAG_TYPE_EFI32_IH: 1287 len = mbi_efi32_ih(mbp, bp); 1288 break; 1289 case MULTIBOOT_TAG_TYPE_EFI32: 1290 len = mbi_efi32(mbp, bp); 1291 break; 1292 #else /* __LP64__ */ 1293 case MULTIBOOT_TAG_TYPE_EFI64_IH: 1294 len = mbi_efi64_ih(mbp, bp); 1295 break; 1296 case MULTIBOOT_TAG_TYPE_EFI64: 1297 len = mbi_efi64(mbp, bp); 1298 break; 1299 #endif /* __LP64__ */ 1300 #endif /* EFIBOOT */ 1301 case MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR: 1302 len = mbi_load_base_addr(mbp, bp); 1303 break; 1304 default: 1305 len = 0; 1306 break; 1307 } 1308 1309 if (len == 0 && is_tag_required(mbp, type)) 1310 ret = -1; 1311 1312 *total_len += len; 1313 return ret; 1314 } 1315 1316 static int 1317 exec_multiboot2(struct multiboot_package *mbp) 1318 { 1319 size_t len, alen; 1320 char *mbi = NULL; 1321 struct multiboot_package_priv *mpp = mbp->mbp_priv; 1322 uint16_t tags[] = { 1323 MULTIBOOT_TAG_TYPE_CMDLINE, 1324 MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME, 1325 MULTIBOOT_TAG_TYPE_MODULE, 1326 MULTIBOOT_TAG_TYPE_BASIC_MEMINFO, 1327 MULTIBOOT_TAG_TYPE_BOOTDEV, 1328 MULTIBOOT_TAG_TYPE_VBE, 1329 MULTIBOOT_TAG_TYPE_FRAMEBUFFER, 1330 MULTIBOOT_TAG_TYPE_ELF_SECTIONS, 1331 MULTIBOOT_TAG_TYPE_APM, 1332 MULTIBOOT_TAG_TYPE_SMBIOS, 1333 MULTIBOOT_TAG_TYPE_ACPI_OLD, 1334 MULTIBOOT_TAG_TYPE_ACPI_NEW, 1335 MULTIBOOT_TAG_TYPE_NETWORK, 1336 MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR, 1337 #ifdef EFIBOOT 1338 MULTIBOOT_TAG_TYPE_EFI_BS, 1339 #ifndef __LP64__ 1340 MULTIBOOT_TAG_TYPE_EFI32, 1341 MULTIBOOT_TAG_TYPE_EFI32_IH, 1342 #else 1343 MULTIBOOT_TAG_TYPE_EFI64, 1344 MULTIBOOT_TAG_TYPE_EFI64_IH, 1345 #endif /* __LP64__ */ 1346 /* 1347 * EFI_MMAP and MMAP at the end so that they 1348 * catch page allocation made for other tags. 1349 */ 1350 MULTIBOOT_TAG_TYPE_EFI_MMAP, 1351 #endif /* EFIGOOT */ 1352 MULTIBOOT_TAG_TYPE_MMAP, 1353 MULTIBOOT_TAG_TYPE_END, /* Must be last */ 1354 }; 1355 physaddr_t entry; 1356 int i; 1357 1358 BI_ALLOC(BTINFO_MAX); 1359 1360 /* set new video mode if text mode was not requested */ 1361 if (mpp->mpp_framebuffer == NULL || 1362 mpp->mpp_framebuffer->depth != 0) 1363 vbe_commit(); 1364 1365 len = 2 * sizeof(multiboot_uint32_t); 1366 for (i = 0; i < sizeof(tags) / sizeof(*tags); i++) { 1367 if (mbi_dispatch(mbp, tags[i], NULL, &len) != 0) 1368 goto fail; 1369 } 1370 1371 mpp->mpp_mbi_len = len + MULTIBOOT_TAG_ALIGN; 1372 mpp->mpp_mbi = alloc(mpp->mpp_mbi_len); 1373 mbi = (char *)roundup((vaddr_t)mpp->mpp_mbi, MULTIBOOT_TAG_ALIGN); 1374 1375 alen = 2 * sizeof(multiboot_uint32_t); 1376 for (i = 0; i < sizeof(tags) / sizeof(*tags); i++) { 1377 if (mbi_dispatch(mbp, tags[i], mbi + alen, &alen) != 0) 1378 goto fail; 1379 1380 /* 1381 * It may shrink because of failure when filling 1382 * structures, but it should not grow. 1383 */ 1384 if (alen > len) 1385 panic("multiboot2 info size mismatch"); 1386 } 1387 1388 1389 ((multiboot_uint32_t *)mbi)[0] = alen; /* total size */ 1390 ((multiboot_uint32_t *)mbi)[1] = 0; /* reserved */ 1391 1392 #if 0 1393 for (i = 0; i < len; i += 16) { 1394 printf("%p ", mbi + i); 1395 for (int j = 0; j < 16; j++) 1396 printf("%s%s%x", 1397 (i+j) % 4 ? "" : " ", 1398 (unsigned char)mbi[i+j] < 0x10 ? "0" : "", 1399 (unsigned char)(mbi[i+j])); 1400 printf("\n"); 1401 } 1402 #endif 1403 1404 printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", 1405 mbp->mbp_marks[MARK_ENTRY], 1406 mbp->mbp_marks[MARK_NSYM], 1407 mbp->mbp_marks[MARK_SYM], 1408 mbp->mbp_marks[MARK_END]); 1409 1410 #ifdef MULTIBOOT2_DEBUG 1411 multiboot2_info_dump(MULTIBOOT2_BOOTLOADER_MAGIC, mbi); 1412 #endif /* MULTIBOOT2_DEBUG */ 1413 1414 entry = mbp->mbp_marks[MARK_ENTRY]; 1415 1416 if (mpp->mpp_entry) 1417 entry = mpp->mpp_entry->entry_addr; 1418 #ifdef EFIBOOT 1419 #ifdef __LP64__ 1420 if (mpp->mpp_entry_elf64) 1421 entry = mpp->mpp_entry_elf64->entry_addr 1422 + efi_loadaddr; 1423 #else 1424 if (mpp->mpp_entry_elf32) 1425 entry = mpp->mpp_entry_elf32->entry_addr 1426 + efi_loadaddr; 1427 #endif /* __LP64__ */ 1428 if (mpp->mpp_efi_bs == NULL) 1429 efi_cleanup(); 1430 #endif /* EFIBOOT */ 1431 1432 /* Does not return */ 1433 multiboot(entry, vtophys(mbi), 1434 x86_trunc_page(mbp->mbp_basemem * 1024), 1435 MULTIBOOT2_BOOTLOADER_MAGIC); 1436 fail: 1437 return -1; 1438 } 1439 1440 static void 1441 cleanup_multiboot2(struct multiboot_package *mbp) 1442 { 1443 if (mbp->mbp_header) 1444 dealloc(mbp->mbp_header, mbp->mbp_header->header_length); 1445 if (mbp->mbp_priv && mbp->mbp_priv->mpp_mbi) 1446 dealloc(mbp->mbp_priv->mpp_mbi, mbp->mbp_priv->mpp_mbi_len); 1447 if (mbp->mbp_priv) 1448 dealloc(mbp->mbp_priv, sizeof(*mbp->mbp_priv)); 1449 1450 dealloc(mbp, sizeof(*mbp)); 1451 1452 return; 1453 } 1454 1455 static bool 1456 is_header_required(struct multiboot_header_tag *mbt) 1457 { 1458 bool ret = false; 1459 1460 if (mbt == NULL) 1461 goto out; 1462 1463 if (mbt->flags & MULTIBOOT_HEADER_TAG_OPTIONAL) 1464 goto out; 1465 1466 ret = true; 1467 out: 1468 return ret; 1469 } 1470 1471 #define NEXT_HEADER(mbt) ((struct multiboot_header_tag *) \ 1472 ((char *)mbt + roundup(mbt->size, MULTIBOOT_HEADER_ALIGN))) 1473 1474 struct multiboot_package * 1475 probe_multiboot2(const char *path) 1476 { 1477 int fd = -1; 1478 size_t i; 1479 char buf[MULTIBOOT_SEARCH + sizeof(struct multiboot_header)]; 1480 ssize_t readen; 1481 struct multiboot_package *mbp = NULL; 1482 struct multiboot_header *mbh; 1483 struct multiboot_header_tag *mbt; 1484 size_t mbh_len = 0; 1485 1486 if ((fd = open(path, 0)) == -1) 1487 goto out; 1488 1489 readen = read(fd, buf, sizeof(buf)); 1490 if (readen < sizeof(struct multiboot_header)) 1491 goto out; 1492 1493 for (i = 0; i < readen; i += MULTIBOOT_HEADER_ALIGN) { 1494 mbh = (struct multiboot_header *)(buf + i); 1495 1496 if (mbh->magic != MULTIBOOT2_HEADER_MAGIC) 1497 continue; 1498 1499 if (mbh->architecture != MULTIBOOT_ARCHITECTURE_I386) 1500 continue; 1501 1502 if (mbh->magic + mbh->architecture + 1503 mbh->header_length + mbh->checksum) 1504 continue; 1505 mbh_len = mbh->header_length; 1506 1507 mbp = alloc(sizeof(*mbp)); 1508 mbp->mbp_version = 2; 1509 mbp->mbp_file = path; 1510 mbp->mbp_header = alloc(mbh_len); 1511 mbp->mbp_priv = alloc(sizeof(*mbp->mbp_priv)); 1512 memset(mbp->mbp_priv, 0, sizeof (*mbp->mbp_priv)); 1513 mbp->mbp_probe = *probe_multiboot2; 1514 mbp->mbp_exec = *exec_multiboot2; 1515 mbp->mbp_cleanup = *cleanup_multiboot2; 1516 1517 break; 1518 } 1519 1520 if (mbp == NULL) 1521 goto out; 1522 1523 if (lseek(fd, i, SEEK_SET) != i) { 1524 printf("lseek failed"); 1525 mbp->mbp_cleanup(mbp); 1526 mbp = NULL; 1527 goto out; 1528 } 1529 1530 mbh = mbp->mbp_header; 1531 if (read(fd, mbh, mbh_len) != mbh_len) { 1532 printf("read failed"); 1533 mbp->mbp_cleanup(mbp); 1534 mbp = NULL; 1535 goto out; 1536 } 1537 1538 for (mbt = (struct multiboot_header_tag *)(mbh + 1); 1539 (char *)mbt - (char *)mbh < mbh_len; 1540 mbt = NEXT_HEADER(mbt)) { 1541 1542 switch(mbt->type) { 1543 case MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST: 1544 mbp->mbp_priv->mpp_info_req = (void *)mbt; 1545 break; 1546 case MULTIBOOT_HEADER_TAG_ADDRESS: 1547 mbp->mbp_priv->mpp_address = (void *)mbt; 1548 break; 1549 case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS: 1550 mbp->mbp_priv->mpp_entry = (void *)mbt; 1551 break; 1552 case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS: 1553 mbp->mbp_priv->mpp_console = (void *)mbt; 1554 1555 case MULTIBOOT_HEADER_TAG_FRAMEBUFFER: 1556 mbp->mbp_priv->mpp_framebuffer = (void *)mbt; 1557 break; 1558 case MULTIBOOT_HEADER_TAG_MODULE_ALIGN: 1559 mbp->mbp_priv->mpp_module_align = (void *)mbt; 1560 break; 1561 #ifdef EFIBOOT 1562 case MULTIBOOT_HEADER_TAG_EFI_BS: 1563 mbp->mbp_priv->mpp_efi_bs = (void *)mbt; 1564 break; 1565 case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32: 1566 mbp->mbp_priv->mpp_entry_elf32 = (void *)mbt; 1567 break; 1568 case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64: 1569 mbp->mbp_priv->mpp_entry_elf64 = (void *)mbt; 1570 break; 1571 #endif 1572 case MULTIBOOT_HEADER_TAG_RELOCATABLE: 1573 mbp->mbp_priv->mpp_relocatable = (void *)mbt; 1574 break; 1575 case MULTIBOOT_HEADER_TAG_END: /* FALLTHROUGH */ 1576 default: 1577 break; 1578 } 1579 } 1580 1581 #ifdef MULTIBOOT2_DEBUG 1582 multiboot2_header_dump(mbp); 1583 #endif /* MULTIBOOT2_DEBUG */ 1584 1585 /* 1586 * multiboot header fully supported 1587 * MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST 1588 * MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS 1589 * MULTIBOOT_HEADER_TAG_MODULE_ALIGN (we always load as page aligned) 1590 * MULTIBOOT_HEADER_TAG_EFI_BS 1591 * MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 1592 * MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 1593 * MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS (we always have a console) 1594 * 1595 * Not supported: 1596 * MULTIBOOT_HEADER_TAG_ADDRESS 1597 * MULTIBOOT_HEADER_TAG_FRAMEBUFFER (but spec says it is onty a hint) 1598 * MULTIBOOT_HEADER_TAG_RELOCATABLE 1599 */ 1600 1601 if (is_header_required((void *)mbp->mbp_priv->mpp_address)) { 1602 printf("Unsupported multiboot address header\n"); 1603 mbp->mbp_cleanup(mbp); 1604 mbp = NULL; 1605 goto out; 1606 } 1607 1608 #ifdef EFIBOOT 1609 /* 1610 * We do not fully support the relocatable header, but 1611 * at least we honour the alignment request. Xen requires 1612 * that to boot. 1613 */ 1614 struct multiboot_header_tag_relocatable *reloc = 1615 mbp->mbp_priv->mpp_relocatable; 1616 if (reloc) 1617 efi_loadaddr = roundup(efi_loadaddr, reloc->align); 1618 #endif 1619 1620 if (is_header_required((void *)mbp->mbp_priv->mpp_relocatable)) { 1621 printf("Unsupported multiboot relocatable header\n"); 1622 mbp->mbp_cleanup(mbp); 1623 mbp = NULL; 1624 goto out; 1625 } 1626 1627 out: 1628 1629 if (fd != -1) 1630 close(fd); 1631 1632 return mbp; 1633 } 1634 1635 #endif /* NO_MULTIBOOT2 */ 1636