Home | History | Annotate | Line # | Download | only in efiboot
efifdt.c revision 1.21
      1  1.21     skrll /* $NetBSD: efifdt.c,v 1.21 2020/01/03 14:14:56 skrll 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.1  jmcneill 
     34   1.1  jmcneill #include <libfdt.h>
     35   1.1  jmcneill 
     36   1.1  jmcneill #define FDT_TABLE_GUID	\
     37   1.1  jmcneill 	{ 0xb1b621d5, 0xf19c, 0x41a5, { 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0 } }
     38   1.1  jmcneill static EFI_GUID FdtTableGuid = FDT_TABLE_GUID;
     39   1.1  jmcneill 
     40   1.1  jmcneill #define	FDT_MEMORY_NODE_PATH	"/memory"
     41   1.1  jmcneill #define	FDT_MEMORY_NODE_NAME	"memory"
     42   1.1  jmcneill #define	FDT_CHOSEN_NODE_PATH	"/chosen"
     43   1.3  jmcneill #define	FDT_CHOSEN_NODE_NAME	"chosen"
     44   1.1  jmcneill 
     45   1.1  jmcneill #define	FDT_MEMORY_USABLE(_md)	\
     46   1.1  jmcneill 	((_md)->Type == EfiLoaderCode || (_md)->Type == EfiLoaderData || \
     47   1.1  jmcneill 	 (_md)->Type == EfiBootServicesCode || (_md)->Type == EfiBootServicesData || \
     48   1.1  jmcneill 	 (_md)->Type == EfiConventionalMemory)
     49   1.1  jmcneill 
     50  1.21     skrll #ifdef _LP64
     51  1.21     skrll #define PRIdUINTN "ld"
     52  1.21     skrll #define PRIxUINTN "lx"
     53  1.21     skrll #else
     54  1.21     skrll #define PRIdUINTN "d"
     55  1.21     skrll #define PRIxUINTN "x"
     56  1.21     skrll #endif
     57   1.1  jmcneill static void *fdt_data = NULL;
     58   1.1  jmcneill 
     59   1.1  jmcneill int
     60   1.1  jmcneill efi_fdt_probe(void)
     61   1.1  jmcneill {
     62   1.1  jmcneill 	EFI_STATUS status;
     63   1.1  jmcneill 
     64   1.1  jmcneill 	status = LibGetSystemConfigurationTable(&FdtTableGuid, &fdt_data);
     65   1.1  jmcneill 	if (EFI_ERROR(status))
     66   1.1  jmcneill 		return EIO;
     67   1.1  jmcneill 
     68   1.1  jmcneill 	if (fdt_check_header(fdt_data) != 0) {
     69   1.1  jmcneill 		fdt_data = NULL;
     70   1.1  jmcneill 		return EINVAL;
     71   1.1  jmcneill 	}
     72   1.1  jmcneill 
     73   1.1  jmcneill 	return 0;
     74   1.1  jmcneill }
     75   1.1  jmcneill 
     76   1.9  jmcneill int
     77   1.9  jmcneill efi_fdt_set_data(void *data)
     78   1.9  jmcneill {
     79   1.9  jmcneill 	if (fdt_check_header(data) != 0)
     80   1.9  jmcneill 		return EINVAL;
     81   1.9  jmcneill 
     82   1.9  jmcneill 	fdt_data = data;
     83   1.9  jmcneill 	return 0;
     84   1.9  jmcneill }
     85   1.9  jmcneill 
     86   1.1  jmcneill void *
     87   1.1  jmcneill efi_fdt_data(void)
     88   1.1  jmcneill {
     89   1.1  jmcneill 	return fdt_data;
     90   1.1  jmcneill }
     91   1.1  jmcneill 
     92   1.1  jmcneill int
     93   1.1  jmcneill efi_fdt_size(void)
     94   1.1  jmcneill {
     95   1.1  jmcneill 	return fdt_data == NULL ? 0 : fdt_totalsize(fdt_data);
     96   1.1  jmcneill }
     97   1.1  jmcneill 
     98  1.15   thorpej bool
     99  1.15   thorpej efi_fdt_overlay_is_compatible(void *dtbo)
    100  1.15   thorpej {
    101  1.15   thorpej 	const int system_root = fdt_path_offset(fdt_data, "/");
    102  1.15   thorpej 	const int overlay_root = fdt_path_offset(dtbo, "/");
    103  1.15   thorpej 
    104  1.15   thorpej 	if (system_root < 0 || overlay_root < 0)
    105  1.15   thorpej 		return false;
    106  1.15   thorpej 
    107  1.15   thorpej 	const int system_ncompat = fdt_stringlist_count(fdt_data, system_root,
    108  1.15   thorpej 	    "compatible");
    109  1.15   thorpej 	const int overlay_ncompat = fdt_stringlist_count(dtbo, overlay_root,
    110  1.15   thorpej 	    "compatible");
    111  1.15   thorpej 
    112  1.15   thorpej 	if (system_ncompat <= 0 || overlay_ncompat <= 0)
    113  1.15   thorpej 		return false;
    114  1.15   thorpej 
    115  1.15   thorpej 	const char *system_compatible, *overlay_compatible;
    116  1.15   thorpej 	int si, oi;
    117  1.15   thorpej 
    118  1.15   thorpej 	for (si = 0; si < system_ncompat; si++) {
    119  1.15   thorpej 		system_compatible = fdt_stringlist_get(fdt_data,
    120  1.15   thorpej 		    system_root, "compatible", si, NULL);
    121  1.15   thorpej 		if (system_compatible == NULL)
    122  1.15   thorpej 			continue;
    123  1.15   thorpej 		for (oi = 0; oi < overlay_ncompat; oi++) {
    124  1.15   thorpej 			overlay_compatible = fdt_stringlist_get(dtbo,
    125  1.15   thorpej 			    overlay_root, "compatible", oi, NULL);
    126  1.15   thorpej 			if (overlay_compatible == NULL)
    127  1.15   thorpej 				continue;
    128  1.15   thorpej 			if (strcmp(system_compatible, overlay_compatible) == 0)
    129  1.15   thorpej 				return true;
    130  1.15   thorpej 		}
    131  1.15   thorpej 	}
    132  1.15   thorpej 
    133  1.15   thorpej 	return false;
    134  1.15   thorpej }
    135  1.15   thorpej 
    136  1.15   thorpej int
    137  1.15   thorpej efi_fdt_overlay_apply(void *dtbo, int *fdterr)
    138  1.15   thorpej {
    139  1.15   thorpej 	int err = fdt_overlay_apply(fdt_data, dtbo);
    140  1.15   thorpej 	if (fdterr)
    141  1.15   thorpej 		*fdterr = err;
    142  1.15   thorpej 	return err == 0 ? 0 : EIO;
    143  1.15   thorpej }
    144  1.15   thorpej 
    145   1.1  jmcneill void
    146   1.8  jmcneill efi_fdt_init(u_long addr, u_long len)
    147   1.8  jmcneill {
    148   1.8  jmcneill 	int error;
    149   1.8  jmcneill 
    150   1.8  jmcneill 	error = fdt_open_into(fdt_data, (void *)addr, len);
    151   1.8  jmcneill 	if (error < 0)
    152   1.8  jmcneill 		panic("fdt_open_into failed: %d", error);
    153   1.8  jmcneill 
    154   1.8  jmcneill 	fdt_data = (void *)addr;
    155   1.8  jmcneill }
    156   1.8  jmcneill 
    157   1.8  jmcneill void
    158   1.8  jmcneill efi_fdt_fini(void)
    159   1.8  jmcneill {
    160   1.8  jmcneill 	int error;
    161   1.8  jmcneill 
    162   1.8  jmcneill 	error = fdt_pack(fdt_data);
    163   1.8  jmcneill 	if (error < 0)
    164   1.8  jmcneill 		panic("fdt_pack failed: %d", error);
    165   1.8  jmcneill }
    166   1.8  jmcneill 
    167   1.8  jmcneill void
    168   1.7  jmcneill efi_fdt_show(void)
    169   1.7  jmcneill {
    170   1.7  jmcneill 	const char *model, *compat;
    171   1.7  jmcneill 	int n, ncompat;
    172   1.7  jmcneill 
    173   1.7  jmcneill 	if (fdt_data == NULL)
    174   1.7  jmcneill 		return;
    175   1.7  jmcneill 
    176   1.7  jmcneill 	model = fdt_getprop(fdt_data, fdt_path_offset(fdt_data, "/"), "model", NULL);
    177   1.7  jmcneill 	if (model)
    178   1.7  jmcneill 		printf("FDT: %s [", model);
    179   1.7  jmcneill 	ncompat = fdt_stringlist_count(fdt_data, fdt_path_offset(fdt_data, "/"), "compatible");
    180   1.7  jmcneill 	for (n = 0; n < ncompat; n++) {
    181   1.7  jmcneill 		compat = fdt_stringlist_get(fdt_data, fdt_path_offset(fdt_data, "/"),
    182   1.7  jmcneill 		    "compatible", n, NULL);
    183   1.7  jmcneill 		printf("%s%s", n == 0 ? "" : ", ", compat);
    184   1.7  jmcneill 	}
    185   1.7  jmcneill 	printf("]\n");
    186   1.7  jmcneill }
    187   1.7  jmcneill 
    188   1.7  jmcneill void
    189   1.1  jmcneill efi_fdt_memory_map(void)
    190   1.1  jmcneill {
    191   1.1  jmcneill 	UINTN nentries = 0, mapkey, descsize;
    192   1.2  jmcneill 	EFI_MEMORY_DESCRIPTOR *md, *memmap;
    193   1.1  jmcneill 	UINT32 descver;
    194   1.2  jmcneill 	UINT64 phys_start, phys_size;
    195  1.16  jmcneill 	int n, memory, chosen;
    196   1.1  jmcneill 
    197   1.1  jmcneill 	memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
    198   1.1  jmcneill 	if (memory < 0)
    199   1.1  jmcneill 		memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
    200   1.1  jmcneill 	if (memory < 0)
    201   1.1  jmcneill 		panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
    202   1.1  jmcneill 
    203  1.16  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    204  1.16  jmcneill 	if (chosen < 0)
    205  1.16  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    206  1.16  jmcneill 	if (chosen < 0)
    207  1.16  jmcneill 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    208  1.16  jmcneill 
    209   1.1  jmcneill 	fdt_delprop(fdt_data, memory, "reg");
    210   1.1  jmcneill 
    211   1.1  jmcneill 	const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    212   1.1  jmcneill 	const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    213   1.1  jmcneill 
    214   1.2  jmcneill 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    215   1.2  jmcneill 	for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    216  1.18  jmcneill 		fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "netbsd,uefi-memmap", md->Type);
    217  1.18  jmcneill 		fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "netbsd,uefi-memmap", md->PhysicalStart);
    218  1.18  jmcneill 		fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "netbsd,uefi-memmap", md->NumberOfPages);
    219  1.18  jmcneill 		fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "netbsd,uefi-memmap", md->Attribute);
    220  1.18  jmcneill 
    221  1.11  jmcneill 		if ((md->Attribute & EFI_MEMORY_RUNTIME) != 0)
    222  1.11  jmcneill 			continue;
    223  1.16  jmcneill 
    224   1.1  jmcneill 		if ((md->Attribute & EFI_MEMORY_WB) == 0)
    225   1.1  jmcneill 			continue;
    226   1.1  jmcneill 		if (!FDT_MEMORY_USABLE(md))
    227   1.1  jmcneill 			continue;
    228   1.2  jmcneill 		if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
    229   1.2  jmcneill 			continue;
    230   1.2  jmcneill 		if (md->NumberOfPages <= 1)
    231   1.2  jmcneill 			continue;
    232   1.2  jmcneill 
    233   1.2  jmcneill 		phys_start = md->PhysicalStart;
    234   1.2  jmcneill 		phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
    235   1.2  jmcneill 
    236   1.2  jmcneill 		if (phys_start & EFI_PAGE_MASK) {
    237   1.2  jmcneill 			/* UEFI spec says these should be 4KB aligned, but U-Boot doesn't always.. */
    238   1.2  jmcneill 			phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
    239   1.2  jmcneill 			phys_size -= (EFI_PAGE_SIZE * 2);
    240   1.2  jmcneill 			if (phys_size == 0)
    241   1.2  jmcneill 				continue;
    242   1.2  jmcneill 		}
    243   1.1  jmcneill 
    244   1.1  jmcneill 		if (address_cells == 1)
    245   1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    246   1.2  jmcneill 			    "reg", (uint32_t)phys_start);
    247   1.1  jmcneill 		else
    248   1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    249   1.2  jmcneill 			    "reg", phys_start);
    250   1.1  jmcneill 
    251   1.1  jmcneill 		if (size_cells == 1)
    252   1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    253   1.2  jmcneill 			    "reg", (uint32_t)phys_size);
    254   1.1  jmcneill 		else
    255   1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    256   1.2  jmcneill 			    "reg", phys_size);
    257   1.1  jmcneill 	}
    258   1.1  jmcneill }
    259   1.1  jmcneill 
    260   1.1  jmcneill void
    261  1.16  jmcneill efi_fdt_gop(void)
    262  1.16  jmcneill {
    263  1.16  jmcneill 	EFI_STATUS status;
    264  1.16  jmcneill 	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
    265  1.16  jmcneill 	EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode;
    266  1.16  jmcneill 	EFI_HANDLE *gop_handle;
    267  1.16  jmcneill 	UINTN ngop_handle, n;
    268  1.16  jmcneill 	char buf[48];
    269  1.16  jmcneill 	int fb;
    270  1.16  jmcneill 
    271  1.16  jmcneill 	status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL, &ngop_handle, &gop_handle);
    272  1.16  jmcneill 	if (EFI_ERROR(status) || ngop_handle == 0)
    273  1.16  jmcneill 		return;
    274  1.16  jmcneill 
    275  1.16  jmcneill 	for (n = 0; n < ngop_handle; n++) {
    276  1.16  jmcneill 		status = uefi_call_wrapper(BS->HandleProtocol, 3, gop_handle[n], &GraphicsOutputProtocol, (void **)&gop);
    277  1.16  jmcneill 		if (EFI_ERROR(status))
    278  1.16  jmcneill 			continue;
    279  1.16  jmcneill 
    280  1.16  jmcneill 		mode = gop->Mode;
    281  1.16  jmcneill 		if (mode == NULL)
    282  1.16  jmcneill 			continue;
    283  1.16  jmcneill 
    284  1.16  jmcneill #ifdef EFIBOOT_DEBUG
    285  1.21     skrll 		printf("GOP: FB @ 0x%" PRIx64 " size 0x%" PRIxUINTN "\n", mode->FrameBufferBase, mode->FrameBufferSize);
    286  1.16  jmcneill 		printf("GOP: Version %d\n", mode->Info->Version);
    287  1.16  jmcneill 		printf("GOP: HRes %d VRes %d\n", mode->Info->HorizontalResolution, mode->Info->VerticalResolution);
    288  1.16  jmcneill 		printf("GOP: PixelFormat %d\n", mode->Info->PixelFormat);
    289  1.16  jmcneill 		printf("GOP: PixelBitmask R 0x%x G 0x%x B 0x%x Res 0x%x\n",
    290  1.16  jmcneill 		    mode->Info->PixelInformation.RedMask,
    291  1.16  jmcneill 		    mode->Info->PixelInformation.GreenMask,
    292  1.16  jmcneill 		    mode->Info->PixelInformation.BlueMask,
    293  1.16  jmcneill 		    mode->Info->PixelInformation.ReservedMask);
    294  1.16  jmcneill 		printf("GOP: Pixels per scanline %d\n", mode->Info->PixelsPerScanLine);
    295  1.16  jmcneill #endif
    296  1.16  jmcneill 
    297  1.16  jmcneill 		if (mode->Info->PixelFormat == PixelBltOnly) {
    298  1.16  jmcneill 			printf("GOP: PixelBltOnly pixel format not supported\n");
    299  1.16  jmcneill 			continue;
    300  1.16  jmcneill 		}
    301  1.16  jmcneill 
    302  1.19  jmcneill 		fdt_setprop_u32(fdt_data,
    303  1.19  jmcneill 		    fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "#address-cells", 2);
    304  1.19  jmcneill 		fdt_setprop_u32(fdt_data,
    305  1.19  jmcneill 		    fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "#size-cells", 2);
    306  1.19  jmcneill 		fdt_setprop_empty(fdt_data,
    307  1.19  jmcneill 		    fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "ranges");
    308  1.19  jmcneill 
    309  1.17     skrll 		snprintf(buf, sizeof(buf), "framebuffer@%" PRIx64, mode->FrameBufferBase);
    310  1.19  jmcneill 		fb = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), buf);
    311  1.16  jmcneill 		if (fb < 0)
    312  1.16  jmcneill 			panic("FDT: Failed to create framebuffer node");
    313  1.16  jmcneill 
    314  1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "compatible", "simple-framebuffer");
    315  1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "status", "okay");
    316  1.16  jmcneill 		fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferBase);
    317  1.16  jmcneill 		fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferSize);
    318  1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "width", mode->Info->HorizontalResolution);
    319  1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "height", mode->Info->VerticalResolution);
    320  1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "stride", mode->Info->PixelsPerScanLine * 4);	/* XXX */
    321  1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "format", "a8b8g8r8");
    322  1.16  jmcneill 
    323  1.17     skrll 		snprintf(buf, sizeof(buf), "/chosen/framebuffer@%" PRIx64, mode->FrameBufferBase);
    324  1.16  jmcneill 		fdt_setprop_string(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH),
    325  1.16  jmcneill 		    "stdout-path", buf);
    326  1.16  jmcneill 
    327  1.16  jmcneill 		return;
    328  1.16  jmcneill 	}
    329  1.16  jmcneill }
    330  1.16  jmcneill 
    331  1.16  jmcneill void
    332   1.1  jmcneill efi_fdt_bootargs(const char *bootargs)
    333   1.1  jmcneill {
    334   1.3  jmcneill 	struct efi_block_part *bpart = efi_block_boot_part();
    335  1.14  jmcneill 	uint8_t macaddr[6];
    336   1.3  jmcneill 	int chosen;
    337   1.3  jmcneill 
    338   1.3  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    339   1.3  jmcneill 	if (chosen < 0)
    340   1.3  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    341   1.3  jmcneill 	if (chosen < 0)
    342   1.4     alnsn 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    343   1.3  jmcneill 
    344   1.3  jmcneill 	if (*bootargs)
    345   1.3  jmcneill 		fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
    346   1.3  jmcneill 
    347   1.3  jmcneill 	if (bpart) {
    348   1.3  jmcneill 		switch (bpart->type) {
    349   1.3  jmcneill 		case EFI_BLOCK_PART_DISKLABEL:
    350   1.3  jmcneill 			fdt_setprop(fdt_data, chosen, "netbsd,mbr",
    351   1.3  jmcneill 			    bpart->hash, sizeof(bpart->hash));
    352   1.3  jmcneill 			fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
    353   1.3  jmcneill 			    bpart->index);
    354   1.3  jmcneill 			break;
    355  1.12  jmcneill 		case EFI_BLOCK_PART_GPT:
    356  1.12  jmcneill 			if (bpart->gpt.ent.ent_name[0] == 0x0000) {
    357  1.12  jmcneill 				fdt_setprop(fdt_data, chosen, "netbsd,gpt-guid",
    358  1.12  jmcneill 				    bpart->hash, sizeof(bpart->hash));
    359  1.12  jmcneill 			} else {
    360  1.12  jmcneill 				char *label = NULL;
    361  1.12  jmcneill 				int rv = ucs2_to_utf8(bpart->gpt.ent.ent_name, &label);
    362  1.12  jmcneill 				if (rv == 0) {
    363  1.12  jmcneill 					fdt_setprop_string(fdt_data, chosen, "netbsd,gpt-label", label);
    364  1.12  jmcneill 					FreePool(label);
    365  1.12  jmcneill 				}
    366  1.12  jmcneill 			}
    367  1.12  jmcneill 			break;
    368   1.3  jmcneill 		default:
    369   1.3  jmcneill 			break;
    370   1.3  jmcneill 		}
    371  1.14  jmcneill 	} else if (efi_net_get_booted_macaddr(macaddr) == 0) {
    372  1.14  jmcneill 		fdt_setprop(fdt_data, chosen, "netbsd,booted-mac-address", macaddr, sizeof(macaddr));
    373   1.3  jmcneill 	}
    374   1.1  jmcneill }
    375   1.8  jmcneill 
    376   1.8  jmcneill void
    377   1.8  jmcneill efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
    378   1.8  jmcneill {
    379   1.8  jmcneill 	int chosen;
    380   1.8  jmcneill 
    381   1.8  jmcneill 	if (initrd_size == 0)
    382   1.8  jmcneill 		return;
    383   1.8  jmcneill 
    384   1.8  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    385   1.8  jmcneill 	if (chosen < 0)
    386   1.8  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    387   1.8  jmcneill 	if (chosen < 0)
    388   1.8  jmcneill 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    389   1.8  jmcneill 
    390   1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
    391   1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
    392   1.8  jmcneill }
    393  1.20  riastrad 
    394  1.20  riastrad void
    395  1.20  riastrad efi_fdt_rndseed(u_long rndseed_addr, u_long rndseed_size)
    396  1.20  riastrad {
    397  1.20  riastrad 	int chosen;
    398  1.20  riastrad 
    399  1.20  riastrad 	if (rndseed_size == 0)
    400  1.20  riastrad 		return;
    401  1.20  riastrad 
    402  1.20  riastrad 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    403  1.20  riastrad 	if (chosen < 0)
    404  1.20  riastrad 		chosen = fdt_add_subnode(fdt_data,
    405  1.20  riastrad 		    fdt_path_offset(fdt_data, "/"),
    406  1.20  riastrad 		    FDT_CHOSEN_NODE_NAME);
    407  1.20  riastrad 	if (chosen < 0)
    408  1.20  riastrad 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    409  1.20  riastrad 
    410  1.20  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-start",
    411  1.20  riastrad 	    rndseed_addr);
    412  1.20  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-end",
    413  1.20  riastrad 	    rndseed_addr + rndseed_size);
    414  1.20  riastrad }
    415