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;
192 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
193 struct efi_boot_memmap *map,
196 struct exit_boot_struct *p = priv;
198 * Update the memory map with virtual addresses. The function will also
199 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
200 * entries so that we can pass it straight to SetVirtualAddressMap()
202 efi_get_virtmap(*map->map, *map->map_size, *map->desc_size,
203 p->runtime_map, p->runtime_entry_count);
209 * Allocate memory for a new FDT, then add EFI, commandline, and
210 * initrd related fields to the FDT. This routine increases the
211 * FDT allocation size until the allocated memory is large
212 * enough. EFI allocations are in EFI_PAGE_SIZE granules,
213 * which are fixed at 4K bytes, so in most cases the first
214 * allocation should succeed.
215 * EFI boot services are exited at the end of this function.
216 * There must be no allocations between the get_memory_map()
217 * call and the exit_boot_services() call, so the exiting of
218 * boot services is very tightly tied to the creation of the FDT
219 * with the final memory map in it.
222 efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
224 unsigned long *new_fdt_addr,
225 unsigned long max_addr,
226 u64 initrd_addr, u64 initrd_size,
228 unsigned long fdt_addr,
229 unsigned long fdt_size)
231 unsigned long map_size, desc_size, buff_size;
233 unsigned long mmap_key;
234 efi_memory_desc_t *memory_map, *runtime_map;
235 unsigned long new_fdt_size;
237 int runtime_entry_count = 0;
238 struct efi_boot_memmap map;
239 struct exit_boot_struct priv;
241 map.map = &runtime_map;
242 map.map_size = &map_size;
243 map.desc_size = &desc_size;
244 map.desc_ver = &desc_ver;
245 map.key_ptr = &mmap_key;
246 map.buff_size = &buff_size;
249 * Get a copy of the current memory map that we will use to prepare
250 * the input for SetVirtualAddressMap(). We don't have to worry about
251 * subsequent allocations adding entries, since they could not affect
252 * the number of EFI_MEMORY_RUNTIME regions.
254 status = efi_get_memory_map(sys_table, &map);
255 if (status != EFI_SUCCESS) {
256 pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n");
261 "Exiting boot services and installing virtual address map...\n");
263 map.map = &memory_map;
265 * Estimate size of new FDT, and allocate memory for it. We
266 * will allocate a bigger buffer if this ends up being too
267 * small, so a rough guess is OK here.
269 new_fdt_size = fdt_size + EFI_PAGE_SIZE;
271 status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN,
272 new_fdt_addr, max_addr);
273 if (status != EFI_SUCCESS) {
274 pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n");
278 status = update_fdt(sys_table,
279 (void *)fdt_addr, fdt_size,
280 (void *)*new_fdt_addr, new_fdt_size,
281 cmdline_ptr, initrd_addr, initrd_size);
283 /* Succeeding the first time is the expected case. */
284 if (status == EFI_SUCCESS)
287 if (status == EFI_BUFFER_TOO_SMALL) {
289 * We need to allocate more space for the new
290 * device tree, so free existing buffer that is
293 efi_free(sys_table, new_fdt_size, *new_fdt_addr);
294 new_fdt_size += EFI_PAGE_SIZE;
296 pr_efi_err(sys_table, "Unable to construct new device tree.\n");
297 goto fail_free_new_fdt;
301 priv.runtime_map = runtime_map;
302 priv.runtime_entry_count = &runtime_entry_count;
303 status = efi_exit_boot_services(sys_table, handle, &map, &priv,
306 if (status == EFI_SUCCESS) {
307 efi_set_virtual_address_map_t *svam;
309 status = update_fdt_memmap((void *)*new_fdt_addr, &map);
310 if (status != EFI_SUCCESS) {
312 * The kernel won't get far without the memory map, but
313 * may still be able to print something meaningful so
314 * return success here.
319 /* Install the new virtual address map */
320 svam = sys_table->runtime->set_virtual_address_map;
321 status = svam(runtime_entry_count * desc_size, desc_size,
322 desc_ver, runtime_map);
325 * We are beyond the point of no return here, so if the call to
326 * SetVirtualAddressMap() failed, we need to signal that to the
327 * incoming kernel but proceed normally otherwise.
329 if (status != EFI_SUCCESS) {
333 * Set the virtual address field of all
334 * EFI_MEMORY_RUNTIME entries to 0. This will signal
335 * the incoming kernel that no virtual translation has
338 for (l = 0; l < map_size; l += desc_size) {
339 efi_memory_desc_t *p = (void *)memory_map + l;
341 if (p->attribute & EFI_MEMORY_RUNTIME)
348 pr_efi_err(sys_table, "Exit boot services failed.\n");
351 efi_free(sys_table, new_fdt_size, *new_fdt_addr);
354 sys_table->boottime->free_pool(runtime_map);
355 return EFI_LOAD_ERROR;
358 void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size)
360 efi_guid_t fdt_guid = DEVICE_TREE_GUID;
361 efi_config_table_t *tables;
365 tables = (efi_config_table_t *) sys_table->tables;
368 for (i = 0; i < sys_table->nr_tables; i++)
369 if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
370 fdt = (void *) tables[i].table;
371 if (fdt_check_header(fdt) != 0) {
372 pr_efi_err(sys_table, "Invalid header detected on UEFI supplied FDT, ignoring ...\n");
375 *fdt_size = fdt_totalsize(fdt);