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