Home | History | Annotate | Line # | Download | only in efiboot
efifdt.c revision 1.8
      1  1.8  jmcneill /* $NetBSD: efifdt.c,v 1.8 2018/09/07 17:30:58 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20  1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  jmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include "efiboot.h"
     30  1.1  jmcneill #include "efifdt.h"
     31  1.3  jmcneill #include "efiblock.h"
     32  1.1  jmcneill 
     33  1.1  jmcneill #include <libfdt.h>
     34  1.1  jmcneill 
     35  1.1  jmcneill #define FDT_TABLE_GUID	\
     36  1.1  jmcneill 	{ 0xb1b621d5, 0xf19c, 0x41a5, { 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0 } }
     37  1.1  jmcneill static EFI_GUID FdtTableGuid = FDT_TABLE_GUID;
     38  1.1  jmcneill 
     39  1.1  jmcneill #define	FDT_MEMORY_NODE_PATH	"/memory"
     40  1.1  jmcneill #define	FDT_MEMORY_NODE_NAME	"memory"
     41  1.1  jmcneill #define	FDT_CHOSEN_NODE_PATH	"/chosen"
     42  1.3  jmcneill #define	FDT_CHOSEN_NODE_NAME	"chosen"
     43  1.1  jmcneill 
     44  1.1  jmcneill #define	FDT_MEMORY_USABLE(_md)	\
     45  1.1  jmcneill 	((_md)->Type == EfiLoaderCode || (_md)->Type == EfiLoaderData || \
     46  1.1  jmcneill 	 (_md)->Type == EfiBootServicesCode || (_md)->Type == EfiBootServicesData || \
     47  1.1  jmcneill 	 (_md)->Type == EfiConventionalMemory)
     48  1.1  jmcneill 
     49  1.1  jmcneill static void *fdt_data = NULL;
     50  1.1  jmcneill 
     51  1.1  jmcneill int
     52  1.1  jmcneill efi_fdt_probe(void)
     53  1.1  jmcneill {
     54  1.1  jmcneill 	EFI_STATUS status;
     55  1.1  jmcneill 
     56  1.1  jmcneill 	status = LibGetSystemConfigurationTable(&FdtTableGuid, &fdt_data);
     57  1.1  jmcneill 	if (EFI_ERROR(status))
     58  1.1  jmcneill 		return EIO;
     59  1.1  jmcneill 
     60  1.1  jmcneill 	if (fdt_check_header(fdt_data) != 0) {
     61  1.1  jmcneill 		fdt_data = NULL;
     62  1.1  jmcneill 		return EINVAL;
     63  1.1  jmcneill 	}
     64  1.1  jmcneill 
     65  1.1  jmcneill 	return 0;
     66  1.1  jmcneill }
     67  1.1  jmcneill 
     68  1.1  jmcneill void *
     69  1.1  jmcneill efi_fdt_data(void)
     70  1.1  jmcneill {
     71  1.1  jmcneill 	return fdt_data;
     72  1.1  jmcneill }
     73  1.1  jmcneill 
     74  1.1  jmcneill int
     75  1.1  jmcneill efi_fdt_size(void)
     76  1.1  jmcneill {
     77  1.1  jmcneill 	return fdt_data == NULL ? 0 : fdt_totalsize(fdt_data);
     78  1.1  jmcneill }
     79  1.1  jmcneill 
     80  1.1  jmcneill void
     81  1.8  jmcneill efi_fdt_init(u_long addr, u_long len)
     82  1.8  jmcneill {
     83  1.8  jmcneill 	int error;
     84  1.8  jmcneill 
     85  1.8  jmcneill 	error = fdt_open_into(fdt_data, (void *)addr, len);
     86  1.8  jmcneill 	if (error < 0)
     87  1.8  jmcneill 		panic("fdt_open_into failed: %d", error);
     88  1.8  jmcneill 
     89  1.8  jmcneill 	fdt_data = (void *)addr;
     90  1.8  jmcneill }
     91  1.8  jmcneill 
     92  1.8  jmcneill void
     93  1.8  jmcneill efi_fdt_fini(void)
     94  1.8  jmcneill {
     95  1.8  jmcneill 	int error;
     96  1.8  jmcneill 
     97  1.8  jmcneill 	error = fdt_pack(fdt_data);
     98  1.8  jmcneill 	if (error < 0)
     99  1.8  jmcneill 		panic("fdt_pack failed: %d", error);
    100  1.8  jmcneill }
    101  1.8  jmcneill 
    102  1.8  jmcneill void
    103  1.7  jmcneill efi_fdt_show(void)
    104  1.7  jmcneill {
    105  1.7  jmcneill 	const char *model, *compat;
    106  1.7  jmcneill 	int n, ncompat;
    107  1.7  jmcneill 
    108  1.7  jmcneill 	if (fdt_data == NULL)
    109  1.7  jmcneill 		return;
    110  1.7  jmcneill 
    111  1.7  jmcneill 	model = fdt_getprop(fdt_data, fdt_path_offset(fdt_data, "/"), "model", NULL);
    112  1.7  jmcneill 	if (model)
    113  1.7  jmcneill 		printf("FDT: %s [", model);
    114  1.7  jmcneill 	ncompat = fdt_stringlist_count(fdt_data, fdt_path_offset(fdt_data, "/"), "compatible");
    115  1.7  jmcneill 	for (n = 0; n < ncompat; n++) {
    116  1.7  jmcneill 		compat = fdt_stringlist_get(fdt_data, fdt_path_offset(fdt_data, "/"),
    117  1.7  jmcneill 		    "compatible", n, NULL);
    118  1.7  jmcneill 		printf("%s%s", n == 0 ? "" : ", ", compat);
    119  1.7  jmcneill 	}
    120  1.7  jmcneill 	printf("]\n");
    121  1.7  jmcneill }
    122  1.7  jmcneill 
    123  1.7  jmcneill void
    124  1.1  jmcneill efi_fdt_memory_map(void)
    125  1.1  jmcneill {
    126  1.1  jmcneill 	UINTN nentries = 0, mapkey, descsize;
    127  1.2  jmcneill 	EFI_MEMORY_DESCRIPTOR *md, *memmap;
    128  1.1  jmcneill 	UINT32 descver;
    129  1.2  jmcneill 	UINT64 phys_start, phys_size;
    130  1.1  jmcneill 	int n, memory;
    131  1.1  jmcneill 
    132  1.1  jmcneill 	memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
    133  1.1  jmcneill 	if (memory < 0)
    134  1.1  jmcneill 		memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
    135  1.1  jmcneill 	if (memory < 0)
    136  1.1  jmcneill 		panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
    137  1.1  jmcneill 
    138  1.1  jmcneill 	fdt_delprop(fdt_data, memory, "reg");
    139  1.2  jmcneill 	while (fdt_num_mem_rsv(fdt_data) > 0) {
    140  1.2  jmcneill 		if (fdt_del_mem_rsv(fdt_data, 0) < 0)
    141  1.2  jmcneill 			panic("FDT: Failed to remove reserved memory map entry");
    142  1.2  jmcneill 	}
    143  1.1  jmcneill 
    144  1.1  jmcneill 	const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    145  1.1  jmcneill 	const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    146  1.1  jmcneill 
    147  1.2  jmcneill 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    148  1.2  jmcneill 	for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    149  1.6  jmcneill #ifdef EFI_MEMORY_DEBUG
    150  1.6  jmcneill 		printf("MEM: %u: Type 0x%x Attr 0x%lx Phys 0x%lx Virt 0x%lx Size 0x%lx\n",
    151  1.6  jmcneill 		    n, md->Type, md->Attribute,
    152  1.6  jmcneill 		    md->PhysicalStart, md->VirtualStart,
    153  1.6  jmcneill 		    (u_long)md->NumberOfPages * EFI_PAGE_SIZE);
    154  1.6  jmcneill #endif
    155  1.1  jmcneill 		if ((md->Attribute & EFI_MEMORY_WB) == 0)
    156  1.1  jmcneill 			continue;
    157  1.1  jmcneill 		if (!FDT_MEMORY_USABLE(md))
    158  1.1  jmcneill 			continue;
    159  1.2  jmcneill 		if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
    160  1.2  jmcneill 			continue;
    161  1.2  jmcneill 		if (md->NumberOfPages <= 1)
    162  1.2  jmcneill 			continue;
    163  1.2  jmcneill 
    164  1.2  jmcneill 		phys_start = md->PhysicalStart;
    165  1.2  jmcneill 		phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
    166  1.2  jmcneill 
    167  1.2  jmcneill 		if (phys_start & EFI_PAGE_MASK) {
    168  1.2  jmcneill 			/* UEFI spec says these should be 4KB aligned, but U-Boot doesn't always.. */
    169  1.2  jmcneill 			phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
    170  1.2  jmcneill 			phys_size -= (EFI_PAGE_SIZE * 2);
    171  1.2  jmcneill 			if (phys_size == 0)
    172  1.2  jmcneill 				continue;
    173  1.2  jmcneill 		}
    174  1.1  jmcneill 
    175  1.1  jmcneill 		if (address_cells == 1)
    176  1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    177  1.2  jmcneill 			    "reg", (uint32_t)phys_start);
    178  1.1  jmcneill 		else
    179  1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    180  1.2  jmcneill 			    "reg", phys_start);
    181  1.1  jmcneill 
    182  1.1  jmcneill 		if (size_cells == 1)
    183  1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    184  1.2  jmcneill 			    "reg", (uint32_t)phys_size);
    185  1.1  jmcneill 		else
    186  1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    187  1.2  jmcneill 			    "reg", phys_size);
    188  1.1  jmcneill 	}
    189  1.1  jmcneill }
    190  1.1  jmcneill 
    191  1.1  jmcneill void
    192  1.1  jmcneill efi_fdt_bootargs(const char *bootargs)
    193  1.1  jmcneill {
    194  1.3  jmcneill 	struct efi_block_part *bpart = efi_block_boot_part();
    195  1.3  jmcneill 	int chosen;
    196  1.3  jmcneill 
    197  1.3  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    198  1.3  jmcneill 	if (chosen < 0)
    199  1.3  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    200  1.3  jmcneill 	if (chosen < 0)
    201  1.4     alnsn 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    202  1.3  jmcneill 
    203  1.3  jmcneill 	if (*bootargs)
    204  1.3  jmcneill 		fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
    205  1.3  jmcneill 
    206  1.3  jmcneill 	if (bpart) {
    207  1.3  jmcneill 		switch (bpart->type) {
    208  1.3  jmcneill 		case EFI_BLOCK_PART_DISKLABEL:
    209  1.3  jmcneill 			fdt_setprop(fdt_data, chosen, "netbsd,mbr",
    210  1.3  jmcneill 			    bpart->hash, sizeof(bpart->hash));
    211  1.3  jmcneill 			fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
    212  1.3  jmcneill 			    bpart->index);
    213  1.3  jmcneill 			break;
    214  1.3  jmcneill 		default:
    215  1.3  jmcneill 			break;
    216  1.3  jmcneill 		}
    217  1.3  jmcneill 	}
    218  1.1  jmcneill }
    219  1.8  jmcneill 
    220  1.8  jmcneill void
    221  1.8  jmcneill efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
    222  1.8  jmcneill {
    223  1.8  jmcneill 	int chosen;
    224  1.8  jmcneill 
    225  1.8  jmcneill 	if (initrd_size == 0)
    226  1.8  jmcneill 		return;
    227  1.8  jmcneill 
    228  1.8  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    229  1.8  jmcneill 	if (chosen < 0)
    230  1.8  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    231  1.8  jmcneill 	if (chosen < 0)
    232  1.8  jmcneill 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    233  1.8  jmcneill 
    234  1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
    235  1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
    236  1.8  jmcneill }
    237