2 * FDT related Helper functions used by the EFI stub on multiple
3 * architectures. This should be #included by the EFI stub
4 * implementation files.
6 * Copyright 2013 Linaro Limited; author Roy Franz
8 * This file is part of the Linux kernel, and is made available
9 * under the terms of the GNU General Public License version 2.
13 #include <linux/efi.h>
14 #include <linux/libfdt.h>
19 static efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt,
20 unsigned long orig_fdt_size,
21 void *fdt, int new_fdt_size, char *cmdline_ptr,
22 u64 initrd_addr, u64 initrd_size)
29 /* Do some checks on provided FDT, if it exists*/
31 if (fdt_check_header(orig_fdt)) {
32 pr_efi_err(sys_table, "Device Tree header not valid!\n");
33 return EFI_LOAD_ERROR;
36 * We don't get the size of the FDT if we get if from a
37 * configuration table.
39 if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) {
40 pr_efi_err(sys_table, "Truncated device tree! foo!\n");
41 return EFI_LOAD_ERROR;
46 status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
48 status = fdt_create_empty_tree(fdt, new_fdt_size);
54 * Delete all memory reserve map entries. When booting via UEFI,
55 * kernel will use the UEFI memory map to find reserved regions.
57 num_rsv = fdt_num_mem_rsv(fdt);
59 fdt_del_mem_rsv(fdt, num_rsv);
61 node = fdt_subnode_offset(fdt, 0, "chosen");
63 node = fdt_add_subnode(fdt, 0, "chosen");
65 status = node; /* node is error code when negative */
70 if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
71 status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
72 strlen(cmdline_ptr) + 1);
77 /* Set initrd address/end in device tree, if present */
78 if (initrd_size != 0) {
80 u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
82 status = fdt_setprop(fdt, node, "linux,initrd-start",
83 &initrd_image_start, sizeof(u64));
86 initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
87 status = fdt_setprop(fdt, node, "linux,initrd-end",
88 &initrd_image_end, sizeof(u64));
93 /* Add FDT entries for EFI runtime services in chosen node. */
94 node = fdt_subnode_offset(fdt, 0, "chosen");
95 fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table);
96 status = fdt_setprop(fdt, node, "linux,uefi-system-table",
97 &fdt_val64, sizeof(fdt_val64));
101 fdt_val64 = U64_MAX; /* placeholder */
102 status = fdt_setprop(fdt, node, "linux,uefi-mmap-start",
103 &fdt_val64, sizeof(fdt_val64));
107 fdt_val32 = U32_MAX; /* placeholder */
108 status = fdt_setprop(fdt, node, "linux,uefi-mmap-size",
109 &fdt_val32, sizeof(fdt_val32));
113 status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-size",
114 &fdt_val32, sizeof(fdt_val32));
118 status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver",
119 &fdt_val32, sizeof(fdt_val32));
123 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
124 efi_status_t efi_status;
126 efi_status = efi_get_random_bytes(sys_table, sizeof(fdt_val64),
128 if (efi_status == EFI_SUCCESS) {
129 status = fdt_setprop(fdt, node, "kaslr-seed",
130 &fdt_val64, sizeof(fdt_val64));
133 } else if (efi_status != EFI_NOT_FOUND) {
140 if (status == -FDT_ERR_NOSPACE)
141 return EFI_BUFFER_TOO_SMALL;
143 return EFI_LOAD_ERROR;
146 static efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map)
148 int node = fdt_path_offset(fdt, "/chosen");
154 return EFI_LOAD_ERROR;
156 fdt_val64 = cpu_to_fdt64((unsigned long)*map->map);
157 err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-start",
158 &fdt_val64, sizeof(fdt_val64));
160 return EFI_LOAD_ERROR;
162 fdt_val32 = cpu_to_fdt32(*map->map_size);
163 err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-size",
164 &fdt_val32, sizeof(fdt_val32));
166 return EFI_LOAD_ERROR;
168 fdt_val32 = cpu_to_fdt32(*map->desc_size);
169 err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-size",
170 &fdt_val32, sizeof(fdt_val32));
172 return EFI_LOAD_ERROR;
174 fdt_val32 = cpu_to_fdt32(*map->desc_ver);
175 err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-ver",
176 &fdt_val32, sizeof(fdt_val32));
178 return EFI_LOAD_ERROR;
183 #ifndef EFI_FDT_ALIGN
184 #define EFI_FDT_ALIGN EFI_PAGE_SIZE
187 struct exit_boot_struct {
188 efi_memory_desc_t *runtime_map;
189 int *runtime_entry_count;
193 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
194 struct efi_boot_memmap *map,
197 struct exit_boot_struct *p = priv;
199 * Update the memory map with virtual addresses. The function will also
200 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
201 * entries so that we can pass it straight to SetVirtualAddressMap()
203 efi_get_virtmap(*map->map, *map->map_size, *map->desc_size,
204 p->runtime_map, p->runtime_entry_count);
206 return update_fdt_memmap(p->new_fdt_addr, map);
210 * Allocate memory for a new FDT, then add EFI, commandline, and
211 * initrd related fields to the FDT. This routine increases the
212 * FDT allocation size until the allocated memory is large
213 * enough. EFI allocations are in EFI_PAGE_SIZE granules,
214 * which are fixed at 4K bytes, so in most cases the first
215 * allocation should succeed.
216 * EFI boot services are exited at the end of this function.
217 * There must be no allocations between the get_memory_map()
218 * call and the exit_boot_services() call, so the exiting of
219 * boot services is very tightly tied to the creation of the FDT
220 * with the final memory map in it.
223 efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
225 unsigned long *new_fdt_addr,
226 unsigned long max_addr,
227 u64 initrd_addr, u64 initrd_size,
229 unsigned long fdt_addr,
230 unsigned long fdt_size)
232 unsigned long map_size, desc_size, buff_size;
234 unsigned long mmap_key;
235 efi_memory_desc_t *memory_map, *runtime_map;
236 unsigned long new_fdt_size;
238 int runtime_entry_count = 0;
239 struct efi_boot_memmap map;
240 struct exit_boot_struct priv;
242 map.map = &runtime_map;
243 map.map_size = &map_size;
244 map.desc_size = &desc_size;
245 map.desc_ver = &desc_ver;
246 map.key_ptr = &mmap_key;
247 map.buff_size = &buff_size;
250 * Get a copy of the current memory map that we will use to prepare
251 * the input for SetVirtualAddressMap(). We don't have to worry about
252 * subsequent allocations adding entries, since they could not affect
253 * the number of EFI_MEMORY_RUNTIME regions.
255 status = efi_get_memory_map(sys_table, &map);
256 if (status != EFI_SUCCESS) {
257 pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n");
262 "Exiting boot services and installing virtual address map...\n");
264 map.map = &memory_map;
266 * Estimate size of new FDT, and allocate memory for it. We
267 * will allocate a bigger buffer if this ends up being too
268 * small, so a rough guess is OK here.
270 new_fdt_size = fdt_size + EFI_PAGE_SIZE;
272 status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN,
273 new_fdt_addr, max_addr);
274 if (status != EFI_SUCCESS) {
275 pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n");
279 status = update_fdt(sys_table,
280 (void *)fdt_addr, fdt_size,
281 (void *)*new_fdt_addr, new_fdt_size,
282 cmdline_ptr, initrd_addr, initrd_size);
284 /* Succeeding the first time is the expected case. */
285 if (status == EFI_SUCCESS)
288 if (status == EFI_BUFFER_TOO_SMALL) {
290 * We need to allocate more space for the new
291 * device tree, so free existing buffer that is
294 efi_free(sys_table, new_fdt_size, *new_fdt_addr);
295 new_fdt_size += EFI_PAGE_SIZE;
297 pr_efi_err(sys_table, "Unable to construct new device tree.\n");
298 goto fail_free_new_fdt;
302 priv.runtime_map = runtime_map;
303 priv.runtime_entry_count = &runtime_entry_count;
304 priv.new_fdt_addr = (void *)*new_fdt_addr;
305 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
308 if (status == EFI_SUCCESS) {
309 efi_set_virtual_address_map_t *svam;
311 /* Install the new virtual address map */
312 svam = sys_table->runtime->set_virtual_address_map;
313 status = svam(runtime_entry_count * desc_size, desc_size,
314 desc_ver, runtime_map);
317 * We are beyond the point of no return here, so if the call to
318 * SetVirtualAddressMap() failed, we need to signal that to the
319 * incoming kernel but proceed normally otherwise.
321 if (status != EFI_SUCCESS) {
325 * Set the virtual address field of all
326 * EFI_MEMORY_RUNTIME entries to 0. This will signal
327 * the incoming kernel that no virtual translation has
330 for (l = 0; l < map_size; l += desc_size) {
331 efi_memory_desc_t *p = (void *)memory_map + l;
333 if (p->attribute & EFI_MEMORY_RUNTIME)
340 pr_efi_err(sys_table, "Exit boot services failed.\n");
343 efi_free(sys_table, new_fdt_size, *new_fdt_addr);
346 sys_table->boottime->free_pool(runtime_map);
347 return EFI_LOAD_ERROR;
350 void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size)
352 efi_guid_t fdt_guid = DEVICE_TREE_GUID;
353 efi_config_table_t *tables;
357 tables = (efi_config_table_t *) sys_table->tables;
360 for (i = 0; i < sys_table->nr_tables; i++)
361 if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
362 fdt = (void *) tables[i].table;
363 if (fdt_check_header(fdt) != 0) {
364 pr_efi_err(sys_table, "Invalid header detected on UEFI supplied FDT, ignoring ...\n");
367 *fdt_size = fdt_totalsize(fdt);