Home | History | Annotate | Line # | Download | only in efiboot
efifdt.c revision 1.23
      1  1.23  riastrad /* $NetBSD: efifdt.c,v 1.23 2020/05/14 19:21:53 riastradh 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.23  riastrad static int
    189  1.23  riastrad efi_fdt_chosen(void)
    190  1.23  riastrad {
    191  1.23  riastrad 	int chosen;
    192  1.23  riastrad 
    193  1.23  riastrad 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    194  1.23  riastrad 	if (chosen < 0)
    195  1.23  riastrad 		chosen = fdt_add_subnode(fdt_data,
    196  1.23  riastrad 		    fdt_path_offset(fdt_data, "/"),
    197  1.23  riastrad 		    FDT_CHOSEN_NODE_NAME);
    198  1.23  riastrad 	if (chosen < 0)
    199  1.23  riastrad 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    200  1.23  riastrad 
    201  1.23  riastrad 	return chosen;
    202  1.23  riastrad }
    203  1.23  riastrad 
    204   1.7  jmcneill void
    205   1.1  jmcneill efi_fdt_memory_map(void)
    206   1.1  jmcneill {
    207   1.1  jmcneill 	UINTN nentries = 0, mapkey, descsize;
    208   1.2  jmcneill 	EFI_MEMORY_DESCRIPTOR *md, *memmap;
    209   1.1  jmcneill 	UINT32 descver;
    210   1.2  jmcneill 	UINT64 phys_start, phys_size;
    211  1.16  jmcneill 	int n, memory, chosen;
    212   1.1  jmcneill 
    213   1.1  jmcneill 	memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
    214   1.1  jmcneill 	if (memory < 0)
    215   1.1  jmcneill 		memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
    216   1.1  jmcneill 	if (memory < 0)
    217   1.1  jmcneill 		panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
    218   1.1  jmcneill 
    219  1.23  riastrad 	chosen = efi_fdt_chosen();
    220  1.16  jmcneill 
    221   1.1  jmcneill 	fdt_delprop(fdt_data, memory, "reg");
    222   1.1  jmcneill 
    223   1.1  jmcneill 	const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    224   1.1  jmcneill 	const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    225   1.1  jmcneill 
    226   1.2  jmcneill 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    227   1.2  jmcneill 	for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    228  1.18  jmcneill 		fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "netbsd,uefi-memmap", md->Type);
    229  1.18  jmcneill 		fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "netbsd,uefi-memmap", md->PhysicalStart);
    230  1.18  jmcneill 		fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "netbsd,uefi-memmap", md->NumberOfPages);
    231  1.18  jmcneill 		fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "netbsd,uefi-memmap", md->Attribute);
    232  1.18  jmcneill 
    233  1.11  jmcneill 		if ((md->Attribute & EFI_MEMORY_RUNTIME) != 0)
    234  1.11  jmcneill 			continue;
    235  1.16  jmcneill 
    236   1.1  jmcneill 		if ((md->Attribute & EFI_MEMORY_WB) == 0)
    237   1.1  jmcneill 			continue;
    238   1.1  jmcneill 		if (!FDT_MEMORY_USABLE(md))
    239   1.1  jmcneill 			continue;
    240   1.2  jmcneill 		if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
    241   1.2  jmcneill 			continue;
    242   1.2  jmcneill 		if (md->NumberOfPages <= 1)
    243   1.2  jmcneill 			continue;
    244   1.2  jmcneill 
    245   1.2  jmcneill 		phys_start = md->PhysicalStart;
    246   1.2  jmcneill 		phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
    247   1.2  jmcneill 
    248   1.2  jmcneill 		if (phys_start & EFI_PAGE_MASK) {
    249   1.2  jmcneill 			/* UEFI spec says these should be 4KB aligned, but U-Boot doesn't always.. */
    250   1.2  jmcneill 			phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
    251   1.2  jmcneill 			phys_size -= (EFI_PAGE_SIZE * 2);
    252   1.2  jmcneill 			if (phys_size == 0)
    253   1.2  jmcneill 				continue;
    254   1.2  jmcneill 		}
    255   1.1  jmcneill 
    256   1.1  jmcneill 		if (address_cells == 1)
    257   1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    258   1.2  jmcneill 			    "reg", (uint32_t)phys_start);
    259   1.1  jmcneill 		else
    260   1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    261   1.2  jmcneill 			    "reg", phys_start);
    262   1.1  jmcneill 
    263   1.1  jmcneill 		if (size_cells == 1)
    264   1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    265   1.2  jmcneill 			    "reg", (uint32_t)phys_size);
    266   1.1  jmcneill 		else
    267   1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    268   1.2  jmcneill 			    "reg", phys_size);
    269   1.1  jmcneill 	}
    270   1.1  jmcneill }
    271   1.1  jmcneill 
    272   1.1  jmcneill void
    273  1.16  jmcneill efi_fdt_gop(void)
    274  1.16  jmcneill {
    275  1.16  jmcneill 	EFI_STATUS status;
    276  1.16  jmcneill 	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
    277  1.16  jmcneill 	EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode;
    278  1.16  jmcneill 	EFI_HANDLE *gop_handle;
    279  1.16  jmcneill 	UINTN ngop_handle, n;
    280  1.16  jmcneill 	char buf[48];
    281  1.16  jmcneill 	int fb;
    282  1.16  jmcneill 
    283  1.16  jmcneill 	status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL, &ngop_handle, &gop_handle);
    284  1.16  jmcneill 	if (EFI_ERROR(status) || ngop_handle == 0)
    285  1.16  jmcneill 		return;
    286  1.16  jmcneill 
    287  1.16  jmcneill 	for (n = 0; n < ngop_handle; n++) {
    288  1.16  jmcneill 		status = uefi_call_wrapper(BS->HandleProtocol, 3, gop_handle[n], &GraphicsOutputProtocol, (void **)&gop);
    289  1.16  jmcneill 		if (EFI_ERROR(status))
    290  1.16  jmcneill 			continue;
    291  1.16  jmcneill 
    292  1.16  jmcneill 		mode = gop->Mode;
    293  1.16  jmcneill 		if (mode == NULL)
    294  1.16  jmcneill 			continue;
    295  1.16  jmcneill 
    296  1.16  jmcneill #ifdef EFIBOOT_DEBUG
    297  1.21     skrll 		printf("GOP: FB @ 0x%" PRIx64 " size 0x%" PRIxUINTN "\n", mode->FrameBufferBase, mode->FrameBufferSize);
    298  1.16  jmcneill 		printf("GOP: Version %d\n", mode->Info->Version);
    299  1.16  jmcneill 		printf("GOP: HRes %d VRes %d\n", mode->Info->HorizontalResolution, mode->Info->VerticalResolution);
    300  1.16  jmcneill 		printf("GOP: PixelFormat %d\n", mode->Info->PixelFormat);
    301  1.16  jmcneill 		printf("GOP: PixelBitmask R 0x%x G 0x%x B 0x%x Res 0x%x\n",
    302  1.16  jmcneill 		    mode->Info->PixelInformation.RedMask,
    303  1.16  jmcneill 		    mode->Info->PixelInformation.GreenMask,
    304  1.16  jmcneill 		    mode->Info->PixelInformation.BlueMask,
    305  1.16  jmcneill 		    mode->Info->PixelInformation.ReservedMask);
    306  1.16  jmcneill 		printf("GOP: Pixels per scanline %d\n", mode->Info->PixelsPerScanLine);
    307  1.16  jmcneill #endif
    308  1.16  jmcneill 
    309  1.16  jmcneill 		if (mode->Info->PixelFormat == PixelBltOnly) {
    310  1.16  jmcneill 			printf("GOP: PixelBltOnly pixel format not supported\n");
    311  1.16  jmcneill 			continue;
    312  1.16  jmcneill 		}
    313  1.16  jmcneill 
    314  1.19  jmcneill 		fdt_setprop_u32(fdt_data,
    315  1.19  jmcneill 		    fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "#address-cells", 2);
    316  1.19  jmcneill 		fdt_setprop_u32(fdt_data,
    317  1.19  jmcneill 		    fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "#size-cells", 2);
    318  1.19  jmcneill 		fdt_setprop_empty(fdt_data,
    319  1.19  jmcneill 		    fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "ranges");
    320  1.19  jmcneill 
    321  1.17     skrll 		snprintf(buf, sizeof(buf), "framebuffer@%" PRIx64, mode->FrameBufferBase);
    322  1.19  jmcneill 		fb = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), buf);
    323  1.16  jmcneill 		if (fb < 0)
    324  1.16  jmcneill 			panic("FDT: Failed to create framebuffer node");
    325  1.16  jmcneill 
    326  1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "compatible", "simple-framebuffer");
    327  1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "status", "okay");
    328  1.16  jmcneill 		fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferBase);
    329  1.16  jmcneill 		fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferSize);
    330  1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "width", mode->Info->HorizontalResolution);
    331  1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "height", mode->Info->VerticalResolution);
    332  1.16  jmcneill 		fdt_appendprop_u32(fdt_data, fb, "stride", mode->Info->PixelsPerScanLine * 4);	/* XXX */
    333  1.16  jmcneill 		fdt_appendprop_string(fdt_data, fb, "format", "a8b8g8r8");
    334  1.16  jmcneill 
    335  1.17     skrll 		snprintf(buf, sizeof(buf), "/chosen/framebuffer@%" PRIx64, mode->FrameBufferBase);
    336  1.16  jmcneill 		fdt_setprop_string(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH),
    337  1.16  jmcneill 		    "stdout-path", buf);
    338  1.16  jmcneill 
    339  1.16  jmcneill 		return;
    340  1.16  jmcneill 	}
    341  1.16  jmcneill }
    342  1.16  jmcneill 
    343  1.16  jmcneill void
    344   1.1  jmcneill efi_fdt_bootargs(const char *bootargs)
    345   1.1  jmcneill {
    346   1.3  jmcneill 	struct efi_block_part *bpart = efi_block_boot_part();
    347  1.14  jmcneill 	uint8_t macaddr[6];
    348   1.3  jmcneill 	int chosen;
    349   1.3  jmcneill 
    350  1.23  riastrad 	chosen = efi_fdt_chosen();
    351   1.3  jmcneill 
    352   1.3  jmcneill 	if (*bootargs)
    353   1.3  jmcneill 		fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
    354   1.3  jmcneill 
    355   1.3  jmcneill 	if (bpart) {
    356   1.3  jmcneill 		switch (bpart->type) {
    357   1.3  jmcneill 		case EFI_BLOCK_PART_DISKLABEL:
    358   1.3  jmcneill 			fdt_setprop(fdt_data, chosen, "netbsd,mbr",
    359   1.3  jmcneill 			    bpart->hash, sizeof(bpart->hash));
    360   1.3  jmcneill 			fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
    361   1.3  jmcneill 			    bpart->index);
    362   1.3  jmcneill 			break;
    363  1.12  jmcneill 		case EFI_BLOCK_PART_GPT:
    364  1.12  jmcneill 			if (bpart->gpt.ent.ent_name[0] == 0x0000) {
    365  1.12  jmcneill 				fdt_setprop(fdt_data, chosen, "netbsd,gpt-guid",
    366  1.12  jmcneill 				    bpart->hash, sizeof(bpart->hash));
    367  1.12  jmcneill 			} else {
    368  1.12  jmcneill 				char *label = NULL;
    369  1.12  jmcneill 				int rv = ucs2_to_utf8(bpart->gpt.ent.ent_name, &label);
    370  1.12  jmcneill 				if (rv == 0) {
    371  1.12  jmcneill 					fdt_setprop_string(fdt_data, chosen, "netbsd,gpt-label", label);
    372  1.12  jmcneill 					FreePool(label);
    373  1.12  jmcneill 				}
    374  1.12  jmcneill 			}
    375  1.12  jmcneill 			break;
    376   1.3  jmcneill 		default:
    377   1.3  jmcneill 			break;
    378   1.3  jmcneill 		}
    379  1.14  jmcneill 	} else if (efi_net_get_booted_macaddr(macaddr) == 0) {
    380  1.14  jmcneill 		fdt_setprop(fdt_data, chosen, "netbsd,booted-mac-address", macaddr, sizeof(macaddr));
    381   1.3  jmcneill 	}
    382   1.1  jmcneill }
    383   1.8  jmcneill 
    384   1.8  jmcneill void
    385   1.8  jmcneill efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
    386   1.8  jmcneill {
    387   1.8  jmcneill 	int chosen;
    388   1.8  jmcneill 
    389   1.8  jmcneill 	if (initrd_size == 0)
    390   1.8  jmcneill 		return;
    391   1.8  jmcneill 
    392  1.23  riastrad 	chosen = efi_fdt_chosen();
    393   1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
    394   1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
    395   1.8  jmcneill }
    396  1.20  riastrad 
    397  1.22  riastrad /* pass in the NetBSD on-disk random seed */
    398  1.20  riastrad void
    399  1.20  riastrad efi_fdt_rndseed(u_long rndseed_addr, u_long rndseed_size)
    400  1.20  riastrad {
    401  1.20  riastrad 	int chosen;
    402  1.20  riastrad 
    403  1.20  riastrad 	if (rndseed_size == 0)
    404  1.20  riastrad 		return;
    405  1.20  riastrad 
    406  1.23  riastrad 	chosen = efi_fdt_chosen();
    407  1.20  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-start",
    408  1.20  riastrad 	    rndseed_addr);
    409  1.20  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-end",
    410  1.20  riastrad 	    rndseed_addr + rndseed_size);
    411  1.20  riastrad }
    412  1.22  riastrad 
    413  1.22  riastrad /* pass in output from the EFI firmware's RNG from some unknown source */
    414  1.22  riastrad void
    415  1.22  riastrad efi_fdt_efirng(u_long efirng_addr, u_long efirng_size)
    416  1.22  riastrad {
    417  1.22  riastrad 	int chosen;
    418  1.22  riastrad 
    419  1.22  riastrad 	if (efirng_size == 0)
    420  1.22  riastrad 		return;
    421  1.22  riastrad 
    422  1.23  riastrad 	chosen = efi_fdt_chosen();
    423  1.22  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-start",
    424  1.22  riastrad 	    efirng_addr);
    425  1.22  riastrad 	fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-end",
    426  1.22  riastrad 	    efirng_addr + efirng_size);
    427  1.22  riastrad }
    428