Home | History | Annotate | Line # | Download | only in efiboot
efifdt.c revision 1.9
      1  1.9  jmcneill /* $NetBSD: efifdt.c,v 1.9 2018/09/09 13:37:54 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.9  jmcneill int
     69  1.9  jmcneill efi_fdt_set_data(void *data)
     70  1.9  jmcneill {
     71  1.9  jmcneill 	if (fdt_check_header(data) != 0)
     72  1.9  jmcneill 		return EINVAL;
     73  1.9  jmcneill 
     74  1.9  jmcneill 	fdt_data = data;
     75  1.9  jmcneill 	return 0;
     76  1.9  jmcneill }
     77  1.9  jmcneill 
     78  1.1  jmcneill void *
     79  1.1  jmcneill efi_fdt_data(void)
     80  1.1  jmcneill {
     81  1.1  jmcneill 	return fdt_data;
     82  1.1  jmcneill }
     83  1.1  jmcneill 
     84  1.1  jmcneill int
     85  1.1  jmcneill efi_fdt_size(void)
     86  1.1  jmcneill {
     87  1.1  jmcneill 	return fdt_data == NULL ? 0 : fdt_totalsize(fdt_data);
     88  1.1  jmcneill }
     89  1.1  jmcneill 
     90  1.1  jmcneill void
     91  1.8  jmcneill efi_fdt_init(u_long addr, u_long len)
     92  1.8  jmcneill {
     93  1.8  jmcneill 	int error;
     94  1.8  jmcneill 
     95  1.8  jmcneill 	error = fdt_open_into(fdt_data, (void *)addr, len);
     96  1.8  jmcneill 	if (error < 0)
     97  1.8  jmcneill 		panic("fdt_open_into failed: %d", error);
     98  1.8  jmcneill 
     99  1.8  jmcneill 	fdt_data = (void *)addr;
    100  1.8  jmcneill }
    101  1.8  jmcneill 
    102  1.8  jmcneill void
    103  1.8  jmcneill efi_fdt_fini(void)
    104  1.8  jmcneill {
    105  1.8  jmcneill 	int error;
    106  1.8  jmcneill 
    107  1.8  jmcneill 	error = fdt_pack(fdt_data);
    108  1.8  jmcneill 	if (error < 0)
    109  1.8  jmcneill 		panic("fdt_pack failed: %d", error);
    110  1.8  jmcneill }
    111  1.8  jmcneill 
    112  1.8  jmcneill void
    113  1.7  jmcneill efi_fdt_show(void)
    114  1.7  jmcneill {
    115  1.7  jmcneill 	const char *model, *compat;
    116  1.7  jmcneill 	int n, ncompat;
    117  1.7  jmcneill 
    118  1.7  jmcneill 	if (fdt_data == NULL)
    119  1.7  jmcneill 		return;
    120  1.7  jmcneill 
    121  1.7  jmcneill 	model = fdt_getprop(fdt_data, fdt_path_offset(fdt_data, "/"), "model", NULL);
    122  1.7  jmcneill 	if (model)
    123  1.7  jmcneill 		printf("FDT: %s [", model);
    124  1.7  jmcneill 	ncompat = fdt_stringlist_count(fdt_data, fdt_path_offset(fdt_data, "/"), "compatible");
    125  1.7  jmcneill 	for (n = 0; n < ncompat; n++) {
    126  1.7  jmcneill 		compat = fdt_stringlist_get(fdt_data, fdt_path_offset(fdt_data, "/"),
    127  1.7  jmcneill 		    "compatible", n, NULL);
    128  1.7  jmcneill 		printf("%s%s", n == 0 ? "" : ", ", compat);
    129  1.7  jmcneill 	}
    130  1.7  jmcneill 	printf("]\n");
    131  1.7  jmcneill }
    132  1.7  jmcneill 
    133  1.7  jmcneill void
    134  1.1  jmcneill efi_fdt_memory_map(void)
    135  1.1  jmcneill {
    136  1.1  jmcneill 	UINTN nentries = 0, mapkey, descsize;
    137  1.2  jmcneill 	EFI_MEMORY_DESCRIPTOR *md, *memmap;
    138  1.1  jmcneill 	UINT32 descver;
    139  1.2  jmcneill 	UINT64 phys_start, phys_size;
    140  1.1  jmcneill 	int n, memory;
    141  1.1  jmcneill 
    142  1.1  jmcneill 	memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
    143  1.1  jmcneill 	if (memory < 0)
    144  1.1  jmcneill 		memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
    145  1.1  jmcneill 	if (memory < 0)
    146  1.1  jmcneill 		panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
    147  1.1  jmcneill 
    148  1.1  jmcneill 	fdt_delprop(fdt_data, memory, "reg");
    149  1.2  jmcneill 	while (fdt_num_mem_rsv(fdt_data) > 0) {
    150  1.2  jmcneill 		if (fdt_del_mem_rsv(fdt_data, 0) < 0)
    151  1.2  jmcneill 			panic("FDT: Failed to remove reserved memory map entry");
    152  1.2  jmcneill 	}
    153  1.1  jmcneill 
    154  1.1  jmcneill 	const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    155  1.1  jmcneill 	const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    156  1.1  jmcneill 
    157  1.2  jmcneill 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    158  1.2  jmcneill 	for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    159  1.6  jmcneill #ifdef EFI_MEMORY_DEBUG
    160  1.6  jmcneill 		printf("MEM: %u: Type 0x%x Attr 0x%lx Phys 0x%lx Virt 0x%lx Size 0x%lx\n",
    161  1.6  jmcneill 		    n, md->Type, md->Attribute,
    162  1.6  jmcneill 		    md->PhysicalStart, md->VirtualStart,
    163  1.6  jmcneill 		    (u_long)md->NumberOfPages * EFI_PAGE_SIZE);
    164  1.6  jmcneill #endif
    165  1.1  jmcneill 		if ((md->Attribute & EFI_MEMORY_WB) == 0)
    166  1.1  jmcneill 			continue;
    167  1.1  jmcneill 		if (!FDT_MEMORY_USABLE(md))
    168  1.1  jmcneill 			continue;
    169  1.2  jmcneill 		if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
    170  1.2  jmcneill 			continue;
    171  1.2  jmcneill 		if (md->NumberOfPages <= 1)
    172  1.2  jmcneill 			continue;
    173  1.2  jmcneill 
    174  1.2  jmcneill 		phys_start = md->PhysicalStart;
    175  1.2  jmcneill 		phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
    176  1.2  jmcneill 
    177  1.2  jmcneill 		if (phys_start & EFI_PAGE_MASK) {
    178  1.2  jmcneill 			/* UEFI spec says these should be 4KB aligned, but U-Boot doesn't always.. */
    179  1.2  jmcneill 			phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
    180  1.2  jmcneill 			phys_size -= (EFI_PAGE_SIZE * 2);
    181  1.2  jmcneill 			if (phys_size == 0)
    182  1.2  jmcneill 				continue;
    183  1.2  jmcneill 		}
    184  1.1  jmcneill 
    185  1.1  jmcneill 		if (address_cells == 1)
    186  1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    187  1.2  jmcneill 			    "reg", (uint32_t)phys_start);
    188  1.1  jmcneill 		else
    189  1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    190  1.2  jmcneill 			    "reg", phys_start);
    191  1.1  jmcneill 
    192  1.1  jmcneill 		if (size_cells == 1)
    193  1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    194  1.2  jmcneill 			    "reg", (uint32_t)phys_size);
    195  1.1  jmcneill 		else
    196  1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    197  1.2  jmcneill 			    "reg", phys_size);
    198  1.1  jmcneill 	}
    199  1.1  jmcneill }
    200  1.1  jmcneill 
    201  1.1  jmcneill void
    202  1.1  jmcneill efi_fdt_bootargs(const char *bootargs)
    203  1.1  jmcneill {
    204  1.3  jmcneill 	struct efi_block_part *bpart = efi_block_boot_part();
    205  1.3  jmcneill 	int chosen;
    206  1.3  jmcneill 
    207  1.3  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    208  1.3  jmcneill 	if (chosen < 0)
    209  1.3  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    210  1.3  jmcneill 	if (chosen < 0)
    211  1.4     alnsn 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    212  1.3  jmcneill 
    213  1.3  jmcneill 	if (*bootargs)
    214  1.3  jmcneill 		fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
    215  1.3  jmcneill 
    216  1.3  jmcneill 	if (bpart) {
    217  1.3  jmcneill 		switch (bpart->type) {
    218  1.3  jmcneill 		case EFI_BLOCK_PART_DISKLABEL:
    219  1.3  jmcneill 			fdt_setprop(fdt_data, chosen, "netbsd,mbr",
    220  1.3  jmcneill 			    bpart->hash, sizeof(bpart->hash));
    221  1.3  jmcneill 			fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
    222  1.3  jmcneill 			    bpart->index);
    223  1.3  jmcneill 			break;
    224  1.3  jmcneill 		default:
    225  1.3  jmcneill 			break;
    226  1.3  jmcneill 		}
    227  1.3  jmcneill 	}
    228  1.1  jmcneill }
    229  1.8  jmcneill 
    230  1.8  jmcneill void
    231  1.8  jmcneill efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
    232  1.8  jmcneill {
    233  1.8  jmcneill 	int chosen;
    234  1.8  jmcneill 
    235  1.8  jmcneill 	if (initrd_size == 0)
    236  1.8  jmcneill 		return;
    237  1.8  jmcneill 
    238  1.8  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    239  1.8  jmcneill 	if (chosen < 0)
    240  1.8  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    241  1.8  jmcneill 	if (chosen < 0)
    242  1.8  jmcneill 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    243  1.8  jmcneill 
    244  1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
    245  1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
    246  1.8  jmcneill }
    247