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