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