Home | History | Annotate | Line # | Download | only in efiboot
efifdt.c revision 1.14
      1  1.14  jmcneill /* $NetBSD: efifdt.c,v 1.14 2018/11/15 23:52:33 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.1  jmcneill 
    150   1.1  jmcneill 	const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    151   1.1  jmcneill 	const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
    152   1.1  jmcneill 
    153   1.2  jmcneill 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    154   1.2  jmcneill 	for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    155  1.11  jmcneill 		if ((md->Attribute & EFI_MEMORY_RUNTIME) != 0)
    156  1.11  jmcneill 			continue;
    157   1.1  jmcneill 		if ((md->Attribute & EFI_MEMORY_WB) == 0)
    158   1.1  jmcneill 			continue;
    159   1.1  jmcneill 		if (!FDT_MEMORY_USABLE(md))
    160   1.1  jmcneill 			continue;
    161   1.2  jmcneill 		if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
    162   1.2  jmcneill 			continue;
    163   1.2  jmcneill 		if (md->NumberOfPages <= 1)
    164   1.2  jmcneill 			continue;
    165   1.2  jmcneill 
    166   1.2  jmcneill 		phys_start = md->PhysicalStart;
    167   1.2  jmcneill 		phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
    168   1.2  jmcneill 
    169   1.2  jmcneill 		if (phys_start & EFI_PAGE_MASK) {
    170   1.2  jmcneill 			/* UEFI spec says these should be 4KB aligned, but U-Boot doesn't always.. */
    171   1.2  jmcneill 			phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
    172   1.2  jmcneill 			phys_size -= (EFI_PAGE_SIZE * 2);
    173   1.2  jmcneill 			if (phys_size == 0)
    174   1.2  jmcneill 				continue;
    175   1.2  jmcneill 		}
    176   1.1  jmcneill 
    177   1.1  jmcneill 		if (address_cells == 1)
    178   1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    179   1.2  jmcneill 			    "reg", (uint32_t)phys_start);
    180   1.1  jmcneill 		else
    181   1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    182   1.2  jmcneill 			    "reg", phys_start);
    183   1.1  jmcneill 
    184   1.1  jmcneill 		if (size_cells == 1)
    185   1.1  jmcneill 			fdt_appendprop_u32(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    186   1.2  jmcneill 			    "reg", (uint32_t)phys_size);
    187   1.1  jmcneill 		else
    188   1.1  jmcneill 			fdt_appendprop_u64(fdt_data, fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH),
    189   1.2  jmcneill 			    "reg", phys_size);
    190   1.1  jmcneill 	}
    191   1.1  jmcneill }
    192   1.1  jmcneill 
    193   1.1  jmcneill void
    194   1.1  jmcneill efi_fdt_bootargs(const char *bootargs)
    195   1.1  jmcneill {
    196   1.3  jmcneill 	struct efi_block_part *bpart = efi_block_boot_part();
    197  1.14  jmcneill 	uint8_t macaddr[6];
    198   1.3  jmcneill 	int chosen;
    199   1.3  jmcneill 
    200   1.3  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    201   1.3  jmcneill 	if (chosen < 0)
    202   1.3  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    203   1.3  jmcneill 	if (chosen < 0)
    204   1.4     alnsn 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    205   1.3  jmcneill 
    206   1.3  jmcneill 	if (*bootargs)
    207   1.3  jmcneill 		fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
    208   1.3  jmcneill 
    209   1.3  jmcneill 	if (bpart) {
    210   1.3  jmcneill 		switch (bpart->type) {
    211   1.3  jmcneill 		case EFI_BLOCK_PART_DISKLABEL:
    212   1.3  jmcneill 			fdt_setprop(fdt_data, chosen, "netbsd,mbr",
    213   1.3  jmcneill 			    bpart->hash, sizeof(bpart->hash));
    214   1.3  jmcneill 			fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
    215   1.3  jmcneill 			    bpart->index);
    216   1.3  jmcneill 			break;
    217  1.12  jmcneill 		case EFI_BLOCK_PART_GPT:
    218  1.12  jmcneill 			if (bpart->gpt.ent.ent_name[0] == 0x0000) {
    219  1.12  jmcneill 				fdt_setprop(fdt_data, chosen, "netbsd,gpt-guid",
    220  1.12  jmcneill 				    bpart->hash, sizeof(bpart->hash));
    221  1.12  jmcneill 			} else {
    222  1.12  jmcneill 				char *label = NULL;
    223  1.12  jmcneill 				int rv = ucs2_to_utf8(bpart->gpt.ent.ent_name, &label);
    224  1.12  jmcneill 				if (rv == 0) {
    225  1.12  jmcneill 					fdt_setprop_string(fdt_data, chosen, "netbsd,gpt-label", label);
    226  1.12  jmcneill 					FreePool(label);
    227  1.12  jmcneill 				}
    228  1.12  jmcneill 			}
    229  1.12  jmcneill 			break;
    230   1.3  jmcneill 		default:
    231   1.3  jmcneill 			break;
    232   1.3  jmcneill 		}
    233  1.14  jmcneill 	} else if (efi_net_get_booted_macaddr(macaddr) == 0) {
    234  1.14  jmcneill 		fdt_setprop(fdt_data, chosen, "netbsd,booted-mac-address", macaddr, sizeof(macaddr));
    235   1.3  jmcneill 	}
    236   1.1  jmcneill }
    237   1.8  jmcneill 
    238   1.8  jmcneill void
    239   1.8  jmcneill efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
    240   1.8  jmcneill {
    241   1.8  jmcneill 	int chosen;
    242   1.8  jmcneill 
    243   1.8  jmcneill 	if (initrd_size == 0)
    244   1.8  jmcneill 		return;
    245   1.8  jmcneill 
    246   1.8  jmcneill 	chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
    247   1.8  jmcneill 	if (chosen < 0)
    248   1.8  jmcneill 		chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
    249   1.8  jmcneill 	if (chosen < 0)
    250   1.8  jmcneill 		panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
    251   1.8  jmcneill 
    252   1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
    253   1.8  jmcneill 	fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
    254   1.8  jmcneill }
    255