Home | History | Annotate | Line # | Download | only in efiboot
efifdt.c revision 1.27.2.1
      1  1.27.2.1   thorpej /* $NetBSD: efifdt.c,v 1.27.2.1 2021/01/03 16:35:05 thorpej Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4      1.15   thorpej  * Copyright (c) 2019 Jason R. Thorpe
      5       1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      6       1.1  jmcneill  * All rights reserved.
      7       1.1  jmcneill  *
      8       1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      9       1.1  jmcneill  * modification, are permitted provided that the following conditions
     10       1.1  jmcneill  * are met:
     11       1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     12       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     13       1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     16       1.1  jmcneill  *
     17       1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     18       1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19       1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20       1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21       1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22       1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23       1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24       1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25       1.1  jmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26       1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27       1.1  jmcneill  * SUCH DAMAGE.
     28       1.1  jmcneill  */
     29       1.1  jmcneill 
     30       1.1  jmcneill #include "efiboot.h"
     31       1.1  jmcneill #include "efifdt.h"
     32       1.3  jmcneill #include "efiblock.h"
     33      1.25  jmcneill #include "efiacpi.h"
     34       1.1  jmcneill 
     35       1.1  jmcneill #include <libfdt.h>
     36       1.1  jmcneill 
     37       1.1  jmcneill #define FDT_TABLE_GUID	\
     38       1.1  jmcneill 	{ 0xb1b621d5, 0xf19c, 0x41a5, { 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0 } }
     39       1.1  jmcneill static EFI_GUID FdtTableGuid = FDT_TABLE_GUID;
     40       1.1  jmcneill 
     41       1.1  jmcneill #define	FDT_MEMORY_NODE_PATH	"/memory"
     42       1.1  jmcneill #define	FDT_MEMORY_NODE_NAME	"memory"
     43       1.1  jmcneill #define	FDT_CHOSEN_NODE_PATH	"/chosen"
     44       1.3  jmcneill #define	FDT_CHOSEN_NODE_NAME	"chosen"
     45       1.1  jmcneill 
     46       1.1  jmcneill #define	FDT_MEMORY_USABLE(_md)	\
     47       1.1  jmcneill 	((_md)->Type == EfiLoaderCode || (_md)->Type == EfiLoaderData || \
     48       1.1  jmcneill 	 (_md)->Type == EfiBootServicesCode || (_md)->Type == EfiBootServicesData || \
     49       1.1  jmcneill 	 (_md)->Type == EfiConventionalMemory)
     50       1.1  jmcneill 
     51      1.21     skrll #ifdef _LP64
     52      1.21     skrll #define PRIdUINTN "ld"
     53      1.21     skrll #define PRIxUINTN "lx"
     54      1.21     skrll #else
     55      1.21     skrll #define PRIdUINTN "d"
     56      1.21     skrll #define PRIxUINTN "x"
     57      1.21     skrll #endif
     58       1.1  jmcneill static void *fdt_data = NULL;
     59      1.25  jmcneill static size_t fdt_data_size = 512*1024;
     60       1.1  jmcneill 
     61       1.1  jmcneill int
     62       1.1  jmcneill efi_fdt_probe(void)
     63       1.1  jmcneill {
     64       1.1  jmcneill 	EFI_STATUS status;
     65       1.1  jmcneill 
     66       1.1  jmcneill 	status = LibGetSystemConfigurationTable(&FdtTableGuid, &fdt_data);
     67       1.1  jmcneill 	if (EFI_ERROR(status))
     68       1.1  jmcneill 		return EIO;
     69       1.1  jmcneill 
     70       1.1  jmcneill 	if (fdt_check_header(fdt_data) != 0) {
     71       1.1  jmcneill 		fdt_data = NULL;
     72       1.1  jmcneill 		return EINVAL;
     73       1.1  jmcneill 	}
     74       1.1  jmcneill 
     75       1.1  jmcneill 	return 0;
     76       1.1  jmcneill }
     77       1.1  jmcneill 
     78       1.9  jmcneill int
     79       1.9  jmcneill efi_fdt_set_data(void *data)
     80       1.9  jmcneill {
     81      1.25  jmcneill 	int err;
     82      1.25  jmcneill 
     83       1.9  jmcneill 	if (fdt_check_header(data) != 0)
     84       1.9  jmcneill 		return EINVAL;
     85       1.9  jmcneill 
     86      1.25  jmcneill 	fdt_data = alloc(fdt_data_size);
     87      1.25  jmcneill 	if (fdt_data == NULL)
     88      1.25  jmcneill 		return ENOMEM;
     89      1.25  jmcneill 	memset(fdt_data, 0, fdt_data_size);
     90      1.25  jmcneill 
     91      1.25  jmcneill 	err = fdt_open_into(data, fdt_data, fdt_data_size);
     92      1.25  jmcneill 	if (err != 0) {
     93      1.25  jmcneill 		dealloc(fdt_data, fdt_data_size);
     94      1.25  jmcneill 		fdt_data = NULL;
     95      1.25  jmcneill 		return ENXIO;
     96      1.25  jmcneill 	}
     97      1.25  jmcneill 
     98       1.9  jmcneill 	return 0;
     99       1.9  jmcneill }
    100       1.9  jmcneill 
    101       1.1  jmcneill void *
    102       1.1  jmcneill efi_fdt_data(void)
    103       1.1  jmcneill {
    104       1.1  jmcneill 	return fdt_data;
    105       1.1  jmcneill }
    106       1.1  jmcneill 
    107       1.1  jmcneill int
    108       1.1  jmcneill efi_fdt_size(void)
    109       1.1  jmcneill {
    110       1.1  jmcneill 	return fdt_data == NULL ? 0 : fdt_totalsize(fdt_data);
    111       1.1  jmcneill }
    112       1.1  jmcneill 
    113      1.15   thorpej bool
    114      1.15   thorpej efi_fdt_overlay_is_compatible(void *dtbo)
    115      1.15   thorpej {
    116      1.15   thorpej 	const int system_root = fdt_path_offset(fdt_data, "/");
    117      1.15   thorpej 	const int overlay_root = fdt_path_offset(dtbo, "/");
    118      1.15   thorpej 
    119      1.15   thorpej 	if (system_root < 0 || overlay_root < 0)
    120      1.15   thorpej 		return false;
    121      1.15   thorpej 
    122      1.15   thorpej 	const int system_ncompat = fdt_stringlist_count(fdt_data, system_root,
    123      1.15   thorpej 	    "compatible");
    124      1.15   thorpej 	const int overlay_ncompat = fdt_stringlist_count(dtbo, overlay_root,
    125      1.15   thorpej 	    "compatible");
    126      1.15   thorpej 
    127      1.15   thorpej 	if (system_ncompat <= 0 || overlay_ncompat <= 0)
    128      1.15   thorpej 		return false;
    129      1.15   thorpej 
    130      1.15   thorpej 	const char *system_compatible, *overlay_compatible;
    131      1.15   thorpej 	int si, oi;
    132      1.15   thorpej 
    133      1.15   thorpej 	for (si = 0; si < system_ncompat; si++) {
    134      1.15   thorpej 		system_compatible = fdt_stringlist_get(fdt_data,
    135      1.15   thorpej 		    system_root, "compatible", si, NULL);
    136      1.15   thorpej 		if (system_compatible == NULL)
    137      1.15   thorpej 			continue;
    138      1.15   thorpej 		for (oi = 0; oi < overlay_ncompat; oi++) {
    139      1.15   thorpej 			overlay_compatible = fdt_stringlist_get(dtbo,
    140      1.15   thorpej 			    overlay_root, "compatible", oi, NULL);
    141      1.15   thorpej 			if (overlay_compatible == NULL)
    142      1.15   thorpej 				continue;
    143      1.15   thorpej 			if (strcmp(system_compatible, overlay_compatible) == 0)
    144      1.15   thorpej 				return true;
    145      1.15   thorpej 		}
    146      1.15   thorpej 	}
    147      1.15   thorpej 
    148      1.15   thorpej 	return false;
    149      1.15   thorpej }
    150      1.15   thorpej 
    151      1.15   thorpej int
    152      1.15   thorpej efi_fdt_overlay_apply(void *dtbo, int *fdterr)
    153      1.15   thorpej {
    154      1.15   thorpej 	int err = fdt_overlay_apply(fdt_data, dtbo);
    155      1.15   thorpej 	if (fdterr)
    156      1.15   thorpej 		*fdterr = err;
    157      1.15   thorpej 	return err == 0 ? 0 : EIO;
    158      1.15   thorpej }
    159      1.15   thorpej 
    160       1.1  jmcneill void
    161       1.8  jmcneill efi_fdt_init(u_long addr, u_long len)
    162       1.8  jmcneill {
    163       1.8  jmcneill 	int error;
    164       1.8  jmcneill 
    165       1.8  jmcneill 	error = fdt_open_into(fdt_data, (void *)addr, len);
    166       1.8  jmcneill 	if (error < 0)
    167       1.8  jmcneill 		panic("fdt_open_into failed: %d", error);
    168       1.8  jmcneill 
    169       1.8  jmcneill 	fdt_data = (void *)addr;
    170       1.8  jmcneill }
    171       1.8  jmcneill 
    172       1.8  jmcneill void
    173       1.8  jmcneill efi_fdt_fini(void)
    174       1.8  jmcneill {
    175       1.8  jmcneill 	int error;
    176       1.8  jmcneill 
    177       1.8  jmcneill 	error = fdt_pack(fdt_data);
    178       1.8  jmcneill 	if (error < 0)
    179       1.8  jmcneill 		panic("fdt_pack failed: %d", error);
    180       1.8  jmcneill }
    181       1.8  jmcneill 
    182       1.8  jmcneill void
    183       1.7  jmcneill efi_fdt_show(void)
    184       1.7  jmcneill {
    185       1.7  jmcneill 	const char *model, *compat;
    186       1.7  jmcneill 	int n, ncompat;
    187       1.7  jmcneill 
    188       1.7  jmcneill 	if (fdt_data == NULL)
    189       1.7  jmcneill 		return;
    190       1.7  jmcneill 
    191       1.7  jmcneill 	model = fdt_getprop(fdt_data, fdt_path_offset(fdt_data, "/"), "model", NULL);
    192       1.7  jmcneill 	if (model)
    193       1.7  jmcneill 		printf("FDT: %s [", model);
    194       1.7  jmcneill 	ncompat = fdt_stringlist_count(fdt_data, fdt_path_offset(fdt_data, "/"), "compatible");
    195       1.7  jmcneill 	for (n = 0; n < ncompat; n++) {
    196       1.7  jmcneill 		compat = fdt_stringlist_get(fdt_data, fdt_path_offset(fdt_data, "/"),
    197       1.7  jmcneill 		    "compatible", n, NULL);
    198       1.7  jmcneill 		printf("%s%s", n == 0 ? "" : ", ", compat);
    199       1.7  jmcneill 	}
    200       1.7  jmcneill 	printf("]\n");
    201       1.7  jmcneill }
    202       1.7  jmcneill 
    203      1.23  riastrad static int
    204      1.23  riastrad efi_fdt_chosen(void)
    205      1.23  riastrad {
    206      1.23  riastrad 	int chosen;
    207      1.23  riastrad 
    208      1.23  riastrad 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    209      1.23  riastrad 	if (chosen < 0)
    210      1.23  riastrad 		chosen = fdt_add_subnode(fdt_data,
    211      1.23  riastrad 		    fdt_path_offset(fdt_data, "/"),
    212      1.23  riastrad 		    FDT_CHOSEN_NODE_NAME);
    213      1.23  riastrad 	if (chosen < 0)
    214      1.23  riastrad 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    215      1.23  riastrad 
    216      1.23  riastrad 	return chosen;
    217      1.23  riastrad }
    218      1.23  riastrad 
    219       1.7  jmcneill void
    220      1.25  jmcneill efi_fdt_system_table(void)
    221      1.25  jmcneill {
    222      1.25  jmcneill #ifdef EFIBOOT_RUNTIME_ADDRESS
    223      1.25  jmcneill 	int chosen;
    224      1.25  jmcneill 
    225      1.25  jmcneill 	chosen = efi_fdt_chosen();
    226      1.25  jmcneill 
    227      1.25  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "netbsd,uefi-system-table", (uint64_t)(uintptr_t)ST);
    228      1.25  jmcneill #endif
    229      1.25  jmcneill }
    230      1.25  jmcneill 
    231      1.25  jmcneill void
    232       1.1  jmcneill efi_fdt_memory_map(void)
    233       1.1  jmcneill {
    234       1.1  jmcneill 	UINTN nentries = 0, mapkey, descsize;
    235       1.2  jmcneill 	EFI_MEMORY_DESCRIPTOR *md, *memmap;
    236       1.1  jmcneill 	UINT32 descver;
    237       1.2  jmcneill 	UINT64 phys_start, phys_size;
    238  1.27.2.1   thorpej 	int n, memory;
    239       1.1  jmcneill 
    240       1.1  jmcneill 	memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
    241       1.1  jmcneill 	if (memory < 0)
    242       1.1  jmcneill 		memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
    243       1.1  jmcneill 	if (memory < 0)
    244       1.1  jmcneill 		panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
    245       1.1  jmcneill 
    246       1.1  jmcneill 	fdt_delprop(fdt_data, memory, "reg");
    247       1.1  jmcneill 
    248       1.1  jmcneill 	const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    249       1.1  jmcneill 	const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    250       1.1  jmcneill 
    251       1.2  jmcneill 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    252       1.2  jmcneill 	for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    253  1.27.2.1   thorpej 		/*
    254  1.27.2.1   thorpej 		 * create / find the chosen node for each iteration as it might have changed
    255  1.27.2.1   thorpej 		 * when adding to the memory node
    256  1.27.2.1   thorpej 		 */
    257  1.27.2.1   thorpej 		int chosen = efi_fdt_chosen();
    258  1.27.2.1   thorpej 		fdt_appendprop_u32(fdt_data, chosen, "netbsd,uefi-memmap", md->Type);
    259  1.27.2.1   thorpej 		fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->PhysicalStart);
    260  1.27.2.1   thorpej 		fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->NumberOfPages);
    261  1.27.2.1   thorpej 		fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->Attribute);
    262      1.18  jmcneill 
    263      1.11  jmcneill 		if ((md->Attribute & EFI_MEMORY_RUNTIME) != 0)
    264      1.11  jmcneill 			continue;
    265      1.16  jmcneill 
    266       1.1  jmcneill 		if ((md->Attribute & EFI_MEMORY_WB) == 0)
    267       1.1  jmcneill 			continue;
    268       1.1  jmcneill 		if (!FDT_MEMORY_USABLE(md))
    269       1.1  jmcneill 			continue;
    270       1.2  jmcneill 		if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
    271       1.2  jmcneill 			continue;
    272       1.2  jmcneill 		if (md->NumberOfPages <= 1)
    273       1.2  jmcneill 			continue;
    274       1.2  jmcneill 
    275       1.2  jmcneill 		phys_start = md->PhysicalStart;
    276       1.2  jmcneill 		phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
    277       1.2  jmcneill 
    278       1.2  jmcneill 		if (phys_start & EFI_PAGE_MASK) {
    279  1.27.2.1   thorpej 			/*
    280  1.27.2.1   thorpej 			 * UEFI spec says these should be 4KB aligned, but
    281  1.27.2.1   thorpej 			 * U-Boot doesn't always, so round up to the next
    282  1.27.2.1   thorpej 			 * page.
    283  1.27.2.1   thorpej 			 */
    284       1.2  jmcneill 			phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
    285       1.2  jmcneill 			phys_size -= (EFI_PAGE_SIZE * 2);
    286       1.2  jmcneill 			if (phys_size == 0)
    287       1.2  jmcneill 				continue;
    288       1.2  jmcneill 		}
    289       1.1  jmcneill 
    290  1.27.2.1   thorpej 		memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
    291       1.1  jmcneill 		if (address_cells == 1)
    292  1.27.2.1   thorpej 			fdt_appendprop_u32(fdt_data, memory, "reg",
    293  1.27.2.1   thorpej 			    (uint32_t)phys_start);
    294       1.1  jmcneill 		else
    295  1.27.2.1   thorpej 			fdt_appendprop_u64(fdt_data, memory, "reg",
    296  1.27.2.1   thorpej 			    phys_start);
    297       1.1  jmcneill 
    298       1.1  jmcneill 		if (size_cells == 1)
    299  1.27.2.1   thorpej 			fdt_appendprop_u32(fdt_data, memory, "reg",
    300  1.27.2.1   thorpej 			    (uint32_t)phys_size);
    301       1.1  jmcneill 		else
    302  1.27.2.1   thorpej 			fdt_appendprop_u64(fdt_data, memory, "reg",
    303  1.27.2.1   thorpej 			    phys_size);
    304       1.1  jmcneill 	}
    305       1.1  jmcneill }
    306       1.1  jmcneill 
    307       1.1  jmcneill void
    308      1.16  jmcneill efi_fdt_gop(void)
    309      1.16  jmcneill {
    310      1.16  jmcneill 	EFI_STATUS status;
    311      1.16  jmcneill 	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
    312      1.16  jmcneill 	EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode;
    313      1.16  jmcneill 	EFI_HANDLE *gop_handle;
    314      1.16  jmcneill 	UINTN ngop_handle, n;
    315      1.16  jmcneill 	char buf[48];
    316      1.25  jmcneill 	int fb, chosen;
    317      1.16  jmcneill 
    318      1.16  jmcneill 	status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL, &ngop_handle, &gop_handle);
    319      1.16  jmcneill 	if (EFI_ERROR(status) || ngop_handle == 0)
    320      1.16  jmcneill 		return;
    321      1.16  jmcneill 
    322      1.16  jmcneill 	for (n = 0; n < ngop_handle; n++) {
    323      1.16  jmcneill 		status = uefi_call_wrapper(BS->HandleProtocol, 3, gop_handle[n], &GraphicsOutputProtocol, (void **)&gop);
    324      1.16  jmcneill 		if (EFI_ERROR(status))
    325      1.16  jmcneill 			continue;
    326      1.16  jmcneill 
    327      1.16  jmcneill 		mode = gop->Mode;
    328      1.16  jmcneill 		if (mode == NULL)
    329      1.16  jmcneill 			continue;
    330      1.16  jmcneill 
    331      1.16  jmcneill #ifdef EFIBOOT_DEBUG
    332      1.21     skrll 		printf("GOP: FB @ 0x%" PRIx64 " size 0x%" PRIxUINTN "\n", mode->FrameBufferBase, mode->FrameBufferSize);
    333      1.16  jmcneill 		printf("GOP: Version %d\n", mode->Info->Version);
    334      1.16  jmcneill 		printf("GOP: HRes %d VRes %d\n", mode->Info->HorizontalResolution, mode->Info->VerticalResolution);
    335      1.16  jmcneill 		printf("GOP: PixelFormat %d\n", mode->Info->PixelFormat);
    336      1.16  jmcneill 		printf("GOP: PixelBitmask R 0x%x G 0x%x B 0x%x Res 0x%x\n",
    337      1.16  jmcneill 		    mode->Info->PixelInformation.RedMask,
    338      1.16  jmcneill 		    mode->Info->PixelInformation.GreenMask,
    339      1.16  jmcneill 		    mode->Info->PixelInformation.BlueMask,
    340      1.16  jmcneill 		    mode->Info->PixelInformation.ReservedMask);
    341      1.16  jmcneill 		printf("GOP: Pixels per scanline %d\n", mode->Info->PixelsPerScanLine);
    342      1.16  jmcneill #endif
    343      1.16  jmcneill 
    344      1.16  jmcneill 		if (mode->Info->PixelFormat == PixelBltOnly) {
    345      1.16  jmcneill 			printf("GOP: PixelBltOnly pixel format not supported\n");
    346      1.16  jmcneill 			continue;
    347      1.16  jmcneill 		}
    348      1.16  jmcneill 
    349      1.25  jmcneill 		chosen = efi_fdt_chosen();
    350      1.25  jmcneill 		fdt_setprop_u32(fdt_data, chosen, "#address-cells", 2);
    351      1.25  jmcneill 		fdt_setprop_u32(fdt_data, chosen, "#size-cells", 2);
    352      1.25  jmcneill 		fdt_setprop_empty(fdt_data, chosen, "ranges");
    353      1.19  jmcneill 
    354      1.27  jmcneill 		snprintf(buf, sizeof(buf), "framebuffer@%" PRIx64, mode->FrameBufferBase);
    355      1.25  jmcneill 		fb = fdt_add_subnode(fdt_data, chosen, buf);
    356      1.27  jmcneill 		if (fb < 0) {
    357      1.27  jmcneill 			/* Framebuffer node already exists. No need to create a new one! */
    358      1.27  jmcneill 			return;
    359      1.27  jmcneill 		}
    360      1.16  jmcneill 
    361      1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "compatible", "simple-framebuffer");
    362      1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "status", "okay");
    363      1.16  jmcneill 		fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferBase);
    364      1.16  jmcneill 		fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferSize);
    365      1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "width", mode->Info->HorizontalResolution);
    366      1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "height", mode->Info->VerticalResolution);
    367      1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "stride", mode->Info->PixelsPerScanLine * 4);	/* XXX */
    368      1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "format", "a8b8g8r8");
    369      1.16  jmcneill 
    370      1.25  jmcneill 		/*
    371      1.25  jmcneill 		 * In ACPI mode, use GOP as console.
    372      1.25  jmcneill 		 */
    373      1.25  jmcneill 		if (efi_acpi_available()) {
    374      1.25  jmcneill 			snprintf(buf, sizeof(buf), "/chosen/framebuffer@%" PRIx64, mode->FrameBufferBase);
    375      1.25  jmcneill 			fdt_setprop_string(fdt_data, chosen, "stdout-path", buf);
    376      1.25  jmcneill 		}
    377      1.16  jmcneill 
    378      1.16  jmcneill 		return;
    379      1.16  jmcneill 	}
    380      1.16  jmcneill }
    381      1.16  jmcneill 
    382      1.16  jmcneill void
    383       1.1  jmcneill efi_fdt_bootargs(const char *bootargs)
    384       1.1  jmcneill {
    385       1.3  jmcneill 	struct efi_block_part *bpart = efi_block_boot_part();
    386      1.14  jmcneill 	uint8_t macaddr[6];
    387       1.3  jmcneill 	int chosen;
    388       1.3  jmcneill 
    389      1.23  riastrad 	chosen = efi_fdt_chosen();
    390       1.3  jmcneill 
    391       1.3  jmcneill 	if (*bootargs)
    392       1.3  jmcneill 		fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
    393       1.3  jmcneill 
    394       1.3  jmcneill 	if (bpart) {
    395       1.3  jmcneill 		switch (bpart->type) {
    396       1.3  jmcneill 		case EFI_BLOCK_PART_DISKLABEL:
    397       1.3  jmcneill 			fdt_setprop(fdt_data, chosen, "netbsd,mbr",
    398       1.3  jmcneill 			    bpart->hash, sizeof(bpart->hash));
    399       1.3  jmcneill 			fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
    400       1.3  jmcneill 			    bpart->index);
    401       1.3  jmcneill 			break;
    402      1.12  jmcneill 		case EFI_BLOCK_PART_GPT:
    403      1.12  jmcneill 			if (bpart->gpt.ent.ent_name[0] == 0x0000) {
    404      1.12  jmcneill 				fdt_setprop(fdt_data, chosen, "netbsd,gpt-guid",
    405      1.12  jmcneill 				    bpart->hash, sizeof(bpart->hash));
    406      1.12  jmcneill 			} else {
    407      1.12  jmcneill 				char *label = NULL;
    408      1.12  jmcneill 				int rv = ucs2_to_utf8(bpart->gpt.ent.ent_name, &label);
    409      1.12  jmcneill 				if (rv == 0) {
    410      1.12  jmcneill 					fdt_setprop_string(fdt_data, chosen, "netbsd,gpt-label", label);
    411      1.12  jmcneill 					FreePool(label);
    412      1.12  jmcneill 				}
    413      1.12  jmcneill 			}
    414      1.12  jmcneill 			break;
    415       1.3  jmcneill 		default:
    416       1.3  jmcneill 			break;
    417       1.3  jmcneill 		}
    418      1.14  jmcneill 	} else if (efi_net_get_booted_macaddr(macaddr) == 0) {
    419      1.14  jmcneill 		fdt_setprop(fdt_data, chosen, "netbsd,booted-mac-address", macaddr, sizeof(macaddr));
    420       1.3  jmcneill 	}
    421       1.1  jmcneill }
    422       1.8  jmcneill 
    423       1.8  jmcneill void
    424       1.8  jmcneill efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
    425       1.8  jmcneill {
    426       1.8  jmcneill 	int chosen;
    427       1.8  jmcneill 
    428       1.8  jmcneill 	if (initrd_size == 0)
    429       1.8  jmcneill 		return;
    430       1.8  jmcneill 
    431      1.23  riastrad 	chosen = efi_fdt_chosen();
    432       1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
    433       1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
    434       1.8  jmcneill }
    435      1.20  riastrad 
    436      1.22  riastrad /* pass in the NetBSD on-disk random seed */
    437      1.20  riastrad void
    438      1.20  riastrad efi_fdt_rndseed(u_long rndseed_addr, u_long rndseed_size)
    439      1.20  riastrad {
    440      1.20  riastrad 	int chosen;
    441      1.20  riastrad 
    442      1.20  riastrad 	if (rndseed_size == 0)
    443      1.20  riastrad 		return;
    444      1.20  riastrad 
    445      1.23  riastrad 	chosen = efi_fdt_chosen();
    446      1.20  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-start",
    447      1.20  riastrad 	    rndseed_addr);
    448      1.20  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-end",
    449      1.20  riastrad 	    rndseed_addr + rndseed_size);
    450      1.20  riastrad }
    451      1.22  riastrad 
    452      1.22  riastrad /* pass in output from the EFI firmware's RNG from some unknown source */
    453      1.22  riastrad void
    454      1.22  riastrad efi_fdt_efirng(u_long efirng_addr, u_long efirng_size)
    455      1.22  riastrad {
    456      1.22  riastrad 	int chosen;
    457      1.22  riastrad 
    458      1.22  riastrad 	if (efirng_size == 0)
    459      1.22  riastrad 		return;
    460      1.22  riastrad 
    461      1.23  riastrad 	chosen = efi_fdt_chosen();
    462      1.22  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-start",
    463      1.22  riastrad 	    efirng_addr);
    464      1.22  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-end",
    465      1.22  riastrad 	    efirng_addr + efirng_size);
    466      1.22  riastrad }
    467      1.24  jmcneill 
    468      1.24  jmcneill /* pass in module information */
    469      1.24  jmcneill void
    470      1.24  jmcneill efi_fdt_module(const char *module_name, u_long module_addr, u_long module_size)
    471      1.24  jmcneill {
    472      1.24  jmcneill 	int chosen;
    473      1.24  jmcneill 
    474      1.24  jmcneill 	if (module_size == 0)
    475      1.24  jmcneill 		return;
    476      1.24  jmcneill 
    477      1.24  jmcneill 	chosen = efi_fdt_chosen();
    478      1.24  jmcneill 	fdt_appendprop_string(fdt_data, chosen, "netbsd,module-names", module_name);
    479      1.24  jmcneill 	fdt_appendprop_u64(fdt_data, chosen, "netbsd,modules", module_addr);
    480      1.24  jmcneill 	fdt_appendprop_u64(fdt_data, chosen, "netbsd,modules", module_size);
    481      1.24  jmcneill }
    482