Home | History | Annotate | Line # | Download | only in efiboot
efifdt.c revision 1.30
      1 /* $NetBSD: efifdt.c,v 1.30 2021/10/06 10:13:19 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2019 Jason R. Thorpe
      5  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include "efiboot.h"
     31 #include "efifdt.h"
     32 #include "efiblock.h"
     33 #include "overlay.h"
     34 #include "module.h"
     35 
     36 #ifdef EFIBOOT_ACPI
     37 #include "efiacpi.h"
     38 #endif
     39 
     40 #include <libfdt.h>
     41 
     42 #define FDT_TABLE_GUID	\
     43 	{ 0xb1b621d5, 0xf19c, 0x41a5, { 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0 } }
     44 static EFI_GUID FdtTableGuid = FDT_TABLE_GUID;
     45 
     46 #define	FDT_MEMORY_NODE_PATH	"/memory"
     47 #define	FDT_MEMORY_NODE_NAME	"memory"
     48 #define	FDT_CHOSEN_NODE_PATH	"/chosen"
     49 #define	FDT_CHOSEN_NODE_NAME	"chosen"
     50 
     51 #define	FDT_MEMORY_USABLE(_md)	\
     52 	((_md)->Type == EfiLoaderCode || (_md)->Type == EfiLoaderData || \
     53 	 (_md)->Type == EfiBootServicesCode || (_md)->Type == EfiBootServicesData || \
     54 	 (_md)->Type == EfiConventionalMemory)
     55 
     56 #define	FDT_SPACE	(4 * 1024 * 1024)
     57 #define	FDT_ALIGN	(2 * 1024 * 1024)
     58 
     59 #ifdef _LP64
     60 #define PRIdUINTN "ld"
     61 #define PRIxUINTN "lx"
     62 #else
     63 #define PRIdUINTN "d"
     64 #define PRIxUINTN "x"
     65 #endif
     66 static void *fdt_data = NULL;
     67 static size_t fdt_data_size = 512*1024;
     68 
     69 static EFI_PHYSICAL_ADDRESS initrd_addr, dtb_addr, rndseed_addr;
     70 static u_long initrd_size = 0, dtb_size = 0, rndseed_size = 0;
     71 
     72 /* exec.c */
     73 extern EFI_PHYSICAL_ADDRESS efirng_addr;
     74 extern u_long efirng_size;
     75 
     76 #ifdef EFIBOOT_ACPI
     77 #define ACPI_FDT_SIZE	(128 * 1024)
     78 static int efi_fdt_create_acpifdt(void);
     79 #endif
     80 
     81 int
     82 efi_fdt_probe(void)
     83 {
     84 	EFI_STATUS status;
     85 
     86 	status = LibGetSystemConfigurationTable(&FdtTableGuid, &fdt_data);
     87 	if (EFI_ERROR(status))
     88 		return EIO;
     89 
     90 	if (fdt_check_header(fdt_data) != 0) {
     91 		fdt_data = NULL;
     92 		return EINVAL;
     93 	}
     94 
     95 	return 0;
     96 }
     97 
     98 int
     99 efi_fdt_set_data(void *data)
    100 {
    101 	int err;
    102 
    103 	if (fdt_check_header(data) != 0)
    104 		return EINVAL;
    105 
    106 	fdt_data = alloc(fdt_data_size);
    107 	if (fdt_data == NULL)
    108 		return ENOMEM;
    109 	memset(fdt_data, 0, fdt_data_size);
    110 
    111 	err = fdt_open_into(data, fdt_data, fdt_data_size);
    112 	if (err != 0) {
    113 		dealloc(fdt_data, fdt_data_size);
    114 		fdt_data = NULL;
    115 		return ENXIO;
    116 	}
    117 
    118 	return 0;
    119 }
    120 
    121 void *
    122 efi_fdt_data(void)
    123 {
    124 	return fdt_data;
    125 }
    126 
    127 int
    128 efi_fdt_size(void)
    129 {
    130 	return fdt_data == NULL ? 0 : fdt_totalsize(fdt_data);
    131 }
    132 
    133 bool
    134 efi_fdt_overlay_is_compatible(void *dtbo)
    135 {
    136 	const int system_root = fdt_path_offset(fdt_data, "/");
    137 	const int overlay_root = fdt_path_offset(dtbo, "/");
    138 
    139 	if (system_root < 0 || overlay_root < 0)
    140 		return false;
    141 
    142 	const int system_ncompat = fdt_stringlist_count(fdt_data, system_root,
    143 	    "compatible");
    144 	const int overlay_ncompat = fdt_stringlist_count(dtbo, overlay_root,
    145 	    "compatible");
    146 
    147 	if (system_ncompat <= 0 || overlay_ncompat <= 0)
    148 		return false;
    149 
    150 	const char *system_compatible, *overlay_compatible;
    151 	int si, oi;
    152 
    153 	for (si = 0; si < system_ncompat; si++) {
    154 		system_compatible = fdt_stringlist_get(fdt_data,
    155 		    system_root, "compatible", si, NULL);
    156 		if (system_compatible == NULL)
    157 			continue;
    158 		for (oi = 0; oi < overlay_ncompat; oi++) {
    159 			overlay_compatible = fdt_stringlist_get(dtbo,
    160 			    overlay_root, "compatible", oi, NULL);
    161 			if (overlay_compatible == NULL)
    162 				continue;
    163 			if (strcmp(system_compatible, overlay_compatible) == 0)
    164 				return true;
    165 		}
    166 	}
    167 
    168 	return false;
    169 }
    170 
    171 int
    172 efi_fdt_overlay_apply(void *dtbo, int *fdterr)
    173 {
    174 	int err = fdt_overlay_apply(fdt_data, dtbo);
    175 	if (fdterr)
    176 		*fdterr = err;
    177 	return err == 0 ? 0 : EIO;
    178 }
    179 
    180 void
    181 efi_fdt_init(u_long addr, u_long len)
    182 {
    183 	int error;
    184 
    185 	error = fdt_open_into(fdt_data, (void *)addr, len);
    186 	if (error < 0)
    187 		panic("fdt_open_into failed: %d", error);
    188 
    189 	fdt_data = (void *)addr;
    190 }
    191 
    192 void
    193 efi_fdt_fini(void)
    194 {
    195 	int error;
    196 
    197 	error = fdt_pack(fdt_data);
    198 	if (error < 0)
    199 		panic("fdt_pack failed: %d", error);
    200 }
    201 
    202 void
    203 efi_fdt_show(void)
    204 {
    205 	const char *model, *compat;
    206 	int n, ncompat;
    207 
    208 	if (fdt_data == NULL)
    209 		return;
    210 
    211 	model = fdt_getprop(fdt_data, fdt_path_offset(fdt_data, "/"), "model", NULL);
    212 	if (model)
    213 		printf("FDT: %s [", model);
    214 	ncompat = fdt_stringlist_count(fdt_data, fdt_path_offset(fdt_data, "/"), "compatible");
    215 	for (n = 0; n < ncompat; n++) {
    216 		compat = fdt_stringlist_get(fdt_data, fdt_path_offset(fdt_data, "/"),
    217 		    "compatible", n, NULL);
    218 		printf("%s%s", n == 0 ? "" : ", ", compat);
    219 	}
    220 	printf("]\n");
    221 }
    222 
    223 static int
    224 efi_fdt_chosen(void)
    225 {
    226 	int chosen;
    227 
    228 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    229 	if (chosen < 0)
    230 		chosen = fdt_add_subnode(fdt_data,
    231 		    fdt_path_offset(fdt_data, "/"),
    232 		    FDT_CHOSEN_NODE_NAME);
    233 	if (chosen < 0)
    234 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    235 
    236 	return chosen;
    237 }
    238 
    239 void
    240 efi_fdt_system_table(void)
    241 {
    242 #ifdef EFIBOOT_RUNTIME_ADDRESS
    243 	int chosen;
    244 
    245 	chosen = efi_fdt_chosen();
    246 
    247 	fdt_setprop_u64(fdt_data, chosen, "netbsd,uefi-system-table", (uint64_t)(uintptr_t)ST);
    248 #endif
    249 }
    250 
    251 void
    252 efi_fdt_memory_map(void)
    253 {
    254 	UINTN nentries = 0, mapkey, descsize;
    255 	EFI_MEMORY_DESCRIPTOR *md, *memmap;
    256 	UINT32 descver;
    257 	UINT64 phys_start, phys_size;
    258 	int n, memory;
    259 
    260 	memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
    261 	if (memory < 0)
    262 		memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
    263 	if (memory < 0)
    264 		panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
    265 
    266 	fdt_delprop(fdt_data, memory, "reg");
    267 
    268 	const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    269 	const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    270 
    271 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    272 	for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    273 		/*
    274 		 * create / find the chosen node for each iteration as it might have changed
    275 		 * when adding to the memory node
    276 		 */
    277 		int chosen = efi_fdt_chosen();
    278 		fdt_appendprop_u32(fdt_data, chosen, "netbsd,uefi-memmap", md->Type);
    279 		fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->PhysicalStart);
    280 		fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->NumberOfPages);
    281 		fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->Attribute);
    282 
    283 		if ((md->Attribute & EFI_MEMORY_RUNTIME) != 0)
    284 			continue;
    285 
    286 		if ((md->Attribute & EFI_MEMORY_WB) == 0)
    287 			continue;
    288 		if (!FDT_MEMORY_USABLE(md))
    289 			continue;
    290 		if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
    291 			continue;
    292 		if (md->NumberOfPages <= 1)
    293 			continue;
    294 
    295 		phys_start = md->PhysicalStart;
    296 		phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
    297 
    298 		if (phys_start & EFI_PAGE_MASK) {
    299 			/*
    300 			 * UEFI spec says these should be 4KB aligned, but
    301 			 * U-Boot doesn't always, so round up to the next
    302 			 * page.
    303 			 */
    304 			phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
    305 			phys_size -= (EFI_PAGE_SIZE * 2);
    306 			if (phys_size == 0)
    307 				continue;
    308 		}
    309 
    310 		memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
    311 		if (address_cells == 1)
    312 			fdt_appendprop_u32(fdt_data, memory, "reg",
    313 			    (uint32_t)phys_start);
    314 		else
    315 			fdt_appendprop_u64(fdt_data, memory, "reg",
    316 			    phys_start);
    317 
    318 		if (size_cells == 1)
    319 			fdt_appendprop_u32(fdt_data, memory, "reg",
    320 			    (uint32_t)phys_size);
    321 		else
    322 			fdt_appendprop_u64(fdt_data, memory, "reg",
    323 			    phys_size);
    324 	}
    325 }
    326 
    327 void
    328 efi_fdt_gop(void)
    329 {
    330 	EFI_STATUS status;
    331 	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
    332 	EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode;
    333 	EFI_HANDLE *gop_handle;
    334 	UINTN ngop_handle, n;
    335 	char buf[48];
    336 	int fb, chosen;
    337 
    338 	status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL, &ngop_handle, &gop_handle);
    339 	if (EFI_ERROR(status) || ngop_handle == 0)
    340 		return;
    341 
    342 	for (n = 0; n < ngop_handle; n++) {
    343 		status = uefi_call_wrapper(BS->HandleProtocol, 3, gop_handle[n], &GraphicsOutputProtocol, (void **)&gop);
    344 		if (EFI_ERROR(status))
    345 			continue;
    346 
    347 		mode = gop->Mode;
    348 		if (mode == NULL)
    349 			continue;
    350 
    351 #ifdef EFIBOOT_DEBUG
    352 		printf("GOP: FB @ 0x%" PRIx64 " size 0x%" PRIxUINTN "\n", mode->FrameBufferBase, mode->FrameBufferSize);
    353 		printf("GOP: Version %d\n", mode->Info->Version);
    354 		printf("GOP: HRes %d VRes %d\n", mode->Info->HorizontalResolution, mode->Info->VerticalResolution);
    355 		printf("GOP: PixelFormat %d\n", mode->Info->PixelFormat);
    356 		printf("GOP: PixelBitmask R 0x%x G 0x%x B 0x%x Res 0x%x\n",
    357 		    mode->Info->PixelInformation.RedMask,
    358 		    mode->Info->PixelInformation.GreenMask,
    359 		    mode->Info->PixelInformation.BlueMask,
    360 		    mode->Info->PixelInformation.ReservedMask);
    361 		printf("GOP: Pixels per scanline %d\n", mode->Info->PixelsPerScanLine);
    362 #endif
    363 
    364 		if (mode->Info->PixelFormat == PixelBltOnly) {
    365 			printf("GOP: PixelBltOnly pixel format not supported\n");
    366 			continue;
    367 		}
    368 
    369 		chosen = efi_fdt_chosen();
    370 		fdt_setprop_u32(fdt_data, chosen, "#address-cells", 2);
    371 		fdt_setprop_u32(fdt_data, chosen, "#size-cells", 2);
    372 		fdt_setprop_empty(fdt_data, chosen, "ranges");
    373 
    374 		snprintf(buf, sizeof(buf), "framebuffer@%" PRIx64, mode->FrameBufferBase);
    375 		fb = fdt_add_subnode(fdt_data, chosen, buf);
    376 		if (fb < 0) {
    377 			/* Framebuffer node already exists. No need to create a new one! */
    378 			return;
    379 		}
    380 
    381 		fdt_appendprop_string(fdt_data, fb, "compatible", "simple-framebuffer");
    382 		fdt_appendprop_string(fdt_data, fb, "status", "okay");
    383 		fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferBase);
    384 		fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferSize);
    385 		fdt_appendprop_u32(fdt_data, fb, "width", mode->Info->HorizontalResolution);
    386 		fdt_appendprop_u32(fdt_data, fb, "height", mode->Info->VerticalResolution);
    387 		fdt_appendprop_u32(fdt_data, fb, "stride", mode->Info->PixelsPerScanLine * 4);	/* XXX */
    388 		fdt_appendprop_string(fdt_data, fb, "format", "a8b8g8r8");
    389 
    390 		/*
    391 		 * In ACPI mode, use GOP as console.
    392 		 */
    393 		if (efi_acpi_available() && efi_acpi_enabled()) {
    394 			snprintf(buf, sizeof(buf), "/chosen/framebuffer@%" PRIx64, mode->FrameBufferBase);
    395 			fdt_setprop_string(fdt_data, chosen, "stdout-path", buf);
    396 		}
    397 
    398 		return;
    399 	}
    400 }
    401 
    402 void
    403 efi_fdt_bootargs(const char *bootargs)
    404 {
    405 	struct efi_block_part *bpart = efi_block_boot_part();
    406 	uint8_t macaddr[6];
    407 	int chosen;
    408 
    409 	chosen = efi_fdt_chosen();
    410 
    411 	if (*bootargs)
    412 		fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
    413 
    414 	if (bpart) {
    415 		switch (bpart->type) {
    416 		case EFI_BLOCK_PART_DISKLABEL:
    417 			fdt_setprop(fdt_data, chosen, "netbsd,mbr",
    418 			    bpart->hash, sizeof(bpart->hash));
    419 			fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
    420 			    bpart->index);
    421 			break;
    422 		case EFI_BLOCK_PART_GPT:
    423 			if (bpart->gpt.ent.ent_name[0] == 0x0000) {
    424 				fdt_setprop(fdt_data, chosen, "netbsd,gpt-guid",
    425 				    bpart->hash, sizeof(bpart->hash));
    426 			} else {
    427 				char *label = NULL;
    428 				int rv = ucs2_to_utf8(bpart->gpt.ent.ent_name, &label);
    429 				if (rv == 0) {
    430 					fdt_setprop_string(fdt_data, chosen, "netbsd,gpt-label", label);
    431 					FreePool(label);
    432 				}
    433 			}
    434 			break;
    435 		default:
    436 			break;
    437 		}
    438 	} else if (efi_net_get_booted_macaddr(macaddr) == 0) {
    439 		fdt_setprop(fdt_data, chosen, "netbsd,booted-mac-address", macaddr, sizeof(macaddr));
    440 	}
    441 }
    442 
    443 void
    444 efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
    445 {
    446 	int chosen;
    447 
    448 	if (initrd_size == 0)
    449 		return;
    450 
    451 	chosen = efi_fdt_chosen();
    452 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
    453 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
    454 }
    455 
    456 /* pass in the NetBSD on-disk random seed */
    457 void
    458 efi_fdt_rndseed(u_long addr, u_long size)
    459 {
    460 	int chosen;
    461 
    462 	if (size == 0)
    463 		return;
    464 
    465 	chosen = efi_fdt_chosen();
    466 	fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-start", addr);
    467 	fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-end", addr + size);
    468 }
    469 
    470 /* pass in output from the EFI firmware's RNG from some unknown source */
    471 void
    472 efi_fdt_efirng(u_long efirng_addr, u_long efirng_size)
    473 {
    474 	int chosen;
    475 
    476 	if (efirng_size == 0)
    477 		return;
    478 
    479 	chosen = efi_fdt_chosen();
    480 	fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-start",
    481 	    efirng_addr);
    482 	fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-end",
    483 	    efirng_addr + efirng_size);
    484 }
    485 
    486 /* pass in module information */
    487 void
    488 efi_fdt_module(const char *module_name, u_long module_addr, u_long module_size)
    489 {
    490 	int chosen;
    491 
    492 	if (module_size == 0)
    493 		return;
    494 
    495 	chosen = efi_fdt_chosen();
    496 	fdt_appendprop_string(fdt_data, chosen, "netbsd,module-names", module_name);
    497 	fdt_appendprop_u64(fdt_data, chosen, "netbsd,modules", module_addr);
    498 	fdt_appendprop_u64(fdt_data, chosen, "netbsd,modules", module_size);
    499 }
    500 
    501 static void
    502 apply_overlay(const char *path, void *dtbo)
    503 {
    504 
    505 	if (!efi_fdt_overlay_is_compatible(dtbo)) {
    506 		printf("boot: %s: incompatible overlay\n", path);
    507 		return;
    508 	}
    509 
    510 	int fdterr;
    511 
    512 	if (efi_fdt_overlay_apply(dtbo, &fdterr) != 0) {
    513 		printf("boot: %s: error %d applying overlay\n", path, fdterr);
    514 	}
    515 }
    516 
    517 static void
    518 apply_overlay_file(const char *path)
    519 {
    520 	EFI_PHYSICAL_ADDRESS dtbo_addr;
    521 	u_long dtbo_size;
    522 
    523 	if (strlen(path) == 0)
    524 		return;
    525 
    526 	if (load_file(path, 0, false, &dtbo_addr, &dtbo_size) != 0 ||
    527 	    dtbo_addr == 0) {
    528 		/* Error messages have already been displayed. */
    529 		goto out;
    530 	}
    531 
    532 	apply_overlay(path, (void *)(uintptr_t)dtbo_addr);
    533 
    534 out:
    535 	if (dtbo_addr) {
    536 		uefi_call_wrapper(BS->FreePages, 2, dtbo_addr,
    537 		    EFI_SIZE_TO_PAGES(dtbo_size));
    538 	}
    539 }
    540 
    541 static void
    542 load_fdt_overlays(void)
    543 {
    544 	if (!dtoverlay_enabled)
    545 		return;
    546 
    547 	dtoverlay_foreach(apply_overlay_file);
    548 }
    549 
    550 static void
    551 load_module(const char *module_name)
    552 {
    553 	EFI_PHYSICAL_ADDRESS addr;
    554 	u_long size;
    555 	char path[PATH_MAX];
    556 
    557 	snprintf(path, sizeof(path), "%s/%s/%s.kmod", module_prefix,
    558 	    module_name, module_name);
    559 
    560 	if (load_file(path, 0, false, &addr, &size) != 0 || addr == 0 || size == 0)
    561 	    return;
    562 
    563 	efi_fdt_module(module_name, (u_long)addr, size);
    564 }
    565 
    566 static void
    567 load_modules(const char *kernel_name)
    568 {
    569 	if (!module_enabled)
    570 		return;
    571 
    572 	module_init(kernel_name);
    573 	module_foreach(load_module);
    574 }
    575 
    576 
    577 /*
    578  * Prepare kernel arguments and shutdown boot services.
    579  */
    580 int
    581 arch_prepare_boot(const char *fname, const char *args, u_long *marks)
    582 {
    583 	load_file(get_initrd_path(), 0, false, &initrd_addr, &initrd_size);
    584 	load_file(get_dtb_path(), 0, false, &dtb_addr, &dtb_size);
    585 
    586 #ifdef EFIBOOT_ACPI
    587 	/* ACPI support only works for little endian kernels */
    588 	efi_acpi_enable(netbsd_elf_data == ELFDATA2LSB);
    589 
    590 	if (efi_acpi_available() && efi_acpi_enabled()) {
    591 		int error = efi_fdt_create_acpifdt();
    592 		if (error != 0) {
    593 			return error;
    594 		}
    595 	} else
    596 #endif
    597 	if (dtb_addr && efi_fdt_set_data((void *)(uintptr_t)dtb_addr) != 0) {
    598 		printf("boot: invalid DTB data\n");
    599 		return EINVAL;
    600 	}
    601 
    602 	if (efi_fdt_size() > 0) {
    603 		/*
    604 		 * Load the rndseed as late as possible -- after we
    605 		 * have committed to using fdt and executing this
    606 		 * kernel -- so that it doesn't hang around in memory
    607 		 * if we have to bail or the kernel won't use it.
    608 		 */
    609 		load_file(get_rndseed_path(), 0, false,
    610 		    &rndseed_addr, &rndseed_size);
    611 
    612 		efi_fdt_init((marks[MARK_END] + FDT_ALIGN - 1) & -FDT_ALIGN, FDT_ALIGN);
    613 		load_modules(fname);
    614 		load_fdt_overlays();
    615 		efi_fdt_initrd(initrd_addr, initrd_size);
    616 		efi_fdt_rndseed(rndseed_addr, rndseed_size);
    617 		efi_fdt_efirng(efirng_addr, efirng_size);
    618 		efi_fdt_bootargs(args);
    619 		efi_fdt_system_table();
    620 		efi_fdt_gop();
    621 		efi_fdt_memory_map();
    622 	}
    623 
    624 	efi_cleanup();
    625 
    626 	if (efi_fdt_size() > 0) {
    627 		efi_fdt_fini();
    628 	}
    629 
    630 	return 0;
    631 }
    632 
    633 /*
    634  * Free memory after a failed boot.
    635  */
    636 void
    637 arch_cleanup_boot(void)
    638 {
    639 	if (rndseed_addr) {
    640 		uefi_call_wrapper(BS->FreePages, 2, rndseed_addr, EFI_SIZE_TO_PAGES(rndseed_size));
    641 		rndseed_addr = 0;
    642 		rndseed_size = 0;
    643 	}
    644 	if (initrd_addr) {
    645 		uefi_call_wrapper(BS->FreePages, 2, initrd_addr, EFI_SIZE_TO_PAGES(initrd_size));
    646 		initrd_addr = 0;
    647 		initrd_size = 0;
    648 	}
    649 	if (dtb_addr) {
    650 		uefi_call_wrapper(BS->FreePages, 2, dtb_addr, EFI_SIZE_TO_PAGES(dtb_size));
    651 		dtb_addr = 0;
    652 		dtb_size = 0;
    653 	}
    654 }
    655 
    656 size_t
    657 arch_alloc_size(void)
    658 {
    659 	return FDT_SPACE;
    660 }
    661 
    662 #ifdef EFIBOOT_ACPI
    663 int
    664 efi_fdt_create_acpifdt(void)
    665 {
    666 	void *acpi_root = efi_acpi_root();
    667 	void *smbios_table = efi_acpi_smbios();
    668 	void *fdt;
    669 	int error;
    670 
    671 	if (acpi_root == NULL)
    672 		return EINVAL;
    673 
    674 	fdt = AllocatePool(ACPI_FDT_SIZE);
    675 	if (fdt == NULL)
    676 		return ENOMEM;
    677 
    678 	error = fdt_create_empty_tree(fdt, ACPI_FDT_SIZE);
    679 	if (error)
    680 		return EIO;
    681 
    682 	const char *model = efi_acpi_get_model();
    683 
    684 	fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "compatible", "netbsd,generic-acpi");
    685 	fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "model", model);
    686 	fdt_setprop_cell(fdt, fdt_path_offset(fdt, "/"), "#address-cells", 2);
    687 	fdt_setprop_cell(fdt, fdt_path_offset(fdt, "/"), "#size-cells", 2);
    688 
    689 	fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "chosen");
    690 	fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,acpi-root-table", (uint64_t)(uintptr_t)acpi_root);
    691 	if (smbios_table)
    692 		fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,smbios-table", (uint64_t)(uintptr_t)smbios_table);
    693 
    694 	fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "acpi");
    695 	fdt_setprop_string(fdt, fdt_path_offset(fdt, "/acpi"), "compatible", "netbsd,acpi");
    696 
    697 	return efi_fdt_set_data(fdt);
    698 }
    699 #endif
    700 
    701 #ifdef EFIBOOT_RUNTIME_ADDRESS
    702 static uint64_t
    703 efi_fdt_runtime_alloc_va(uint64_t npages)
    704 {
    705 	static uint64_t va = EFIBOOT_RUNTIME_ADDRESS;
    706 	static uint64_t sz = EFIBOOT_RUNTIME_SIZE;
    707 	uint64_t nva;
    708 
    709 	if (sz < (npages * EFI_PAGE_SIZE)) {
    710 		panic("efi_acpi_alloc_va: couldn't allocate %" PRIu64 " pages",
    711 		    npages);
    712 	}
    713 
    714 	nva = va;
    715 	va += (npages * EFI_PAGE_SIZE);
    716 	sz -= (npages * EFI_PAGE_SIZE);
    717 
    718 	return nva;
    719 }
    720 
    721 void
    722 arch_set_virtual_address_map(EFI_MEMORY_DESCRIPTOR *memmap, UINTN nentries,
    723     UINTN mapkey, UINTN descsize, UINT32 descver)
    724 {
    725 	EFI_MEMORY_DESCRIPTOR *md, *vmd, *vmemmap;
    726 	EFI_STATUS status;
    727 	int n, nrt;
    728 	void *fdt;
    729 
    730 	fdt = efi_fdt_data();
    731 
    732 	vmemmap = alloc(nentries * descsize);
    733 	if (vmemmap == NULL)
    734 		panic("FATAL: couldn't allocate virtual memory map");
    735 
    736 	for (n = 0, nrt = 0, vmd = vmemmap, md = memmap;
    737 	     n < nentries;
    738 	     n++, md = NextMemoryDescriptor(md, descsize)) {
    739 
    740 		if ((md->Attribute & EFI_MEMORY_RUNTIME) == 0) {
    741 			continue;
    742 		}
    743 
    744 		md->VirtualStart = efi_fdt_runtime_alloc_va(md->NumberOfPages);
    745 
    746 		switch (md->Type) {
    747 		case EfiRuntimeServicesCode:
    748 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    749 			    "netbsd,uefi-runtime-code", md->PhysicalStart);
    750 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    751 			    "netbsd,uefi-runtime-code", md->VirtualStart);
    752 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    753 			    "netbsd,uefi-runtime-code",
    754 			    md->NumberOfPages * EFI_PAGE_SIZE);
    755 			break;
    756 		case EfiRuntimeServicesData:
    757 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    758 			    "netbsd,uefi-runtime-data", md->PhysicalStart);
    759 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    760 			    "netbsd,uefi-runtime-data", md->VirtualStart);
    761 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    762 			    "netbsd,uefi-runtime-data",
    763 			    md->NumberOfPages * EFI_PAGE_SIZE);
    764 			break;
    765 		case EfiMemoryMappedIO:
    766 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    767 			    "netbsd,uefi-runtime-mmio", md->PhysicalStart);
    768 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    769 			    "netbsd,uefi-runtime-mmio", md->VirtualStart);
    770 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
    771 			    "netbsd,uefi-runtime-mmio",
    772 			    md->NumberOfPages * EFI_PAGE_SIZE);
    773 			break;
    774 		default:
    775 			break;
    776 		}
    777 
    778 		*vmd = *md;
    779 		vmd = NextMemoryDescriptor(vmd, descsize);
    780 		++nrt;
    781 	}
    782 
    783 	status = uefi_call_wrapper(RT->SetVirtualAddressMap, 4, nrt * descsize,
    784 	    descsize, descver, vmemmap);
    785 	if (EFI_ERROR(status)) {
    786 		printf("WARNING: SetVirtualAddressMap failed\n");
    787 		return;
    788 	}
    789 }
    790 #endif
    791