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 #define EFI_DT_ADDR_CELLS_DEFAULT 2
20 #define EFI_DT_SIZE_CELLS_DEFAULT 2
22 static void fdt_update_cell_size(efi_system_table_t
*sys_table
, void *fdt
)
26 offset
= fdt_path_offset(fdt
, "/");
27 /* Set the #address-cells and #size-cells values for an empty tree */
29 fdt_setprop_u32(fdt
, offset
, "#address-cells",
30 EFI_DT_ADDR_CELLS_DEFAULT
);
32 fdt_setprop_u32(fdt
, offset
, "#size-cells", EFI_DT_SIZE_CELLS_DEFAULT
);
35 static efi_status_t
update_fdt(efi_system_table_t
*sys_table
, void *orig_fdt
,
36 unsigned long orig_fdt_size
,
37 void *fdt
, int new_fdt_size
, char *cmdline_ptr
,
38 u64 initrd_addr
, u64 initrd_size
)
45 /* Do some checks on provided FDT, if it exists*/
47 if (fdt_check_header(orig_fdt
)) {
48 pr_efi_err(sys_table
, "Device Tree header not valid!\n");
49 return EFI_LOAD_ERROR
;
52 * We don't get the size of the FDT if we get if from a
53 * configuration table.
55 if (orig_fdt_size
&& fdt_totalsize(orig_fdt
) > orig_fdt_size
) {
56 pr_efi_err(sys_table
, "Truncated device tree! foo!\n");
57 return EFI_LOAD_ERROR
;
62 status
= fdt_open_into(orig_fdt
, fdt
, new_fdt_size
);
64 status
= fdt_create_empty_tree(fdt
, new_fdt_size
);
67 * Any failure from the following function is non
70 fdt_update_cell_size(sys_table
, fdt
);
78 * Delete all memory reserve map entries. When booting via UEFI,
79 * kernel will use the UEFI memory map to find reserved regions.
81 num_rsv
= fdt_num_mem_rsv(fdt
);
83 fdt_del_mem_rsv(fdt
, num_rsv
);
85 node
= fdt_subnode_offset(fdt
, 0, "chosen");
87 node
= fdt_add_subnode(fdt
, 0, "chosen");
89 status
= node
; /* node is error code when negative */
94 if ((cmdline_ptr
!= NULL
) && (strlen(cmdline_ptr
) > 0)) {
95 status
= fdt_setprop(fdt
, node
, "bootargs", cmdline_ptr
,
96 strlen(cmdline_ptr
) + 1);
101 /* Set initrd address/end in device tree, if present */
102 if (initrd_size
!= 0) {
103 u64 initrd_image_end
;
104 u64 initrd_image_start
= cpu_to_fdt64(initrd_addr
);
106 status
= fdt_setprop(fdt
, node
, "linux,initrd-start",
107 &initrd_image_start
, sizeof(u64
));
110 initrd_image_end
= cpu_to_fdt64(initrd_addr
+ initrd_size
);
111 status
= fdt_setprop(fdt
, node
, "linux,initrd-end",
112 &initrd_image_end
, sizeof(u64
));
117 /* Add FDT entries for EFI runtime services in chosen node. */
118 node
= fdt_subnode_offset(fdt
, 0, "chosen");
119 fdt_val64
= cpu_to_fdt64((u64
)(unsigned long)sys_table
);
120 status
= fdt_setprop(fdt
, node
, "linux,uefi-system-table",
121 &fdt_val64
, sizeof(fdt_val64
));
125 fdt_val64
= U64_MAX
; /* placeholder */
126 status
= fdt_setprop(fdt
, node
, "linux,uefi-mmap-start",
127 &fdt_val64
, sizeof(fdt_val64
));
131 fdt_val32
= U32_MAX
; /* placeholder */
132 status
= fdt_setprop(fdt
, node
, "linux,uefi-mmap-size",
133 &fdt_val32
, sizeof(fdt_val32
));
137 status
= fdt_setprop(fdt
, node
, "linux,uefi-mmap-desc-size",
138 &fdt_val32
, sizeof(fdt_val32
));
142 status
= fdt_setprop(fdt
, node
, "linux,uefi-mmap-desc-ver",
143 &fdt_val32
, sizeof(fdt_val32
));
147 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE
)) {
148 efi_status_t efi_status
;
150 efi_status
= efi_get_random_bytes(sys_table
, sizeof(fdt_val64
),
152 if (efi_status
== EFI_SUCCESS
) {
153 status
= fdt_setprop(fdt
, node
, "kaslr-seed",
154 &fdt_val64
, sizeof(fdt_val64
));
157 } else if (efi_status
!= EFI_NOT_FOUND
) {
164 if (status
== -FDT_ERR_NOSPACE
)
165 return EFI_BUFFER_TOO_SMALL
;
167 return EFI_LOAD_ERROR
;
170 static efi_status_t
update_fdt_memmap(void *fdt
, struct efi_boot_memmap
*map
)
172 int node
= fdt_path_offset(fdt
, "/chosen");
178 return EFI_LOAD_ERROR
;
180 fdt_val64
= cpu_to_fdt64((unsigned long)*map
->map
);
181 err
= fdt_setprop_inplace(fdt
, node
, "linux,uefi-mmap-start",
182 &fdt_val64
, sizeof(fdt_val64
));
184 return EFI_LOAD_ERROR
;
186 fdt_val32
= cpu_to_fdt32(*map
->map_size
);
187 err
= fdt_setprop_inplace(fdt
, node
, "linux,uefi-mmap-size",
188 &fdt_val32
, sizeof(fdt_val32
));
190 return EFI_LOAD_ERROR
;
192 fdt_val32
= cpu_to_fdt32(*map
->desc_size
);
193 err
= fdt_setprop_inplace(fdt
, node
, "linux,uefi-mmap-desc-size",
194 &fdt_val32
, sizeof(fdt_val32
));
196 return EFI_LOAD_ERROR
;
198 fdt_val32
= cpu_to_fdt32(*map
->desc_ver
);
199 err
= fdt_setprop_inplace(fdt
, node
, "linux,uefi-mmap-desc-ver",
200 &fdt_val32
, sizeof(fdt_val32
));
202 return EFI_LOAD_ERROR
;
207 #ifndef EFI_FDT_ALIGN
208 #define EFI_FDT_ALIGN EFI_PAGE_SIZE
211 struct exit_boot_struct
{
212 efi_memory_desc_t
*runtime_map
;
213 int *runtime_entry_count
;
217 static efi_status_t
exit_boot_func(efi_system_table_t
*sys_table_arg
,
218 struct efi_boot_memmap
*map
,
221 struct exit_boot_struct
*p
= priv
;
223 * Update the memory map with virtual addresses. The function will also
224 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
225 * entries so that we can pass it straight to SetVirtualAddressMap()
227 efi_get_virtmap(*map
->map
, *map
->map_size
, *map
->desc_size
,
228 p
->runtime_map
, p
->runtime_entry_count
);
230 return update_fdt_memmap(p
->new_fdt_addr
, map
);
234 #define MAX_FDT_SIZE SZ_2M
238 * Allocate memory for a new FDT, then add EFI, commandline, and
239 * initrd related fields to the FDT. This routine increases the
240 * FDT allocation size until the allocated memory is large
241 * enough. EFI allocations are in EFI_PAGE_SIZE granules,
242 * which are fixed at 4K bytes, so in most cases the first
243 * allocation should succeed.
244 * EFI boot services are exited at the end of this function.
245 * There must be no allocations between the get_memory_map()
246 * call and the exit_boot_services() call, so the exiting of
247 * boot services is very tightly tied to the creation of the FDT
248 * with the final memory map in it.
251 efi_status_t
allocate_new_fdt_and_exit_boot(efi_system_table_t
*sys_table
,
253 unsigned long *new_fdt_addr
,
254 unsigned long max_addr
,
255 u64 initrd_addr
, u64 initrd_size
,
257 unsigned long fdt_addr
,
258 unsigned long fdt_size
)
260 unsigned long map_size
, desc_size
, buff_size
;
262 unsigned long mmap_key
;
263 efi_memory_desc_t
*memory_map
, *runtime_map
;
265 int runtime_entry_count
= 0;
266 struct efi_boot_memmap map
;
267 struct exit_boot_struct priv
;
269 map
.map
= &runtime_map
;
270 map
.map_size
= &map_size
;
271 map
.desc_size
= &desc_size
;
272 map
.desc_ver
= &desc_ver
;
273 map
.key_ptr
= &mmap_key
;
274 map
.buff_size
= &buff_size
;
277 * Get a copy of the current memory map that we will use to prepare
278 * the input for SetVirtualAddressMap(). We don't have to worry about
279 * subsequent allocations adding entries, since they could not affect
280 * the number of EFI_MEMORY_RUNTIME regions.
282 status
= efi_get_memory_map(sys_table
, &map
);
283 if (status
!= EFI_SUCCESS
) {
284 pr_efi_err(sys_table
, "Unable to retrieve UEFI memory map.\n");
289 "Exiting boot services and installing virtual address map...\n");
291 map
.map
= &memory_map
;
292 status
= efi_high_alloc(sys_table
, MAX_FDT_SIZE
, EFI_FDT_ALIGN
,
293 new_fdt_addr
, max_addr
);
294 if (status
!= EFI_SUCCESS
) {
295 pr_efi_err(sys_table
,
296 "Unable to allocate memory for new device tree.\n");
301 * Now that we have done our final memory allocation (and free)
302 * we can get the memory map key needed for exit_boot_services().
304 status
= efi_get_memory_map(sys_table
, &map
);
305 if (status
!= EFI_SUCCESS
)
306 goto fail_free_new_fdt
;
308 status
= update_fdt(sys_table
, (void *)fdt_addr
, fdt_size
,
309 (void *)*new_fdt_addr
, MAX_FDT_SIZE
, cmdline_ptr
,
310 initrd_addr
, initrd_size
);
312 if (status
!= EFI_SUCCESS
) {
313 pr_efi_err(sys_table
, "Unable to construct new device tree.\n");
314 goto fail_free_new_fdt
;
317 priv
.runtime_map
= runtime_map
;
318 priv
.runtime_entry_count
= &runtime_entry_count
;
319 priv
.new_fdt_addr
= (void *)*new_fdt_addr
;
320 status
= efi_exit_boot_services(sys_table
, handle
, &map
, &priv
,
323 if (status
== EFI_SUCCESS
) {
324 efi_set_virtual_address_map_t
*svam
;
326 /* Install the new virtual address map */
327 svam
= sys_table
->runtime
->set_virtual_address_map
;
328 status
= svam(runtime_entry_count
* desc_size
, desc_size
,
329 desc_ver
, runtime_map
);
332 * We are beyond the point of no return here, so if the call to
333 * SetVirtualAddressMap() failed, we need to signal that to the
334 * incoming kernel but proceed normally otherwise.
336 if (status
!= EFI_SUCCESS
) {
340 * Set the virtual address field of all
341 * EFI_MEMORY_RUNTIME entries to 0. This will signal
342 * the incoming kernel that no virtual translation has
345 for (l
= 0; l
< map_size
; l
+= desc_size
) {
346 efi_memory_desc_t
*p
= (void *)memory_map
+ l
;
348 if (p
->attribute
& EFI_MEMORY_RUNTIME
)
355 pr_efi_err(sys_table
, "Exit boot services failed.\n");
358 efi_free(sys_table
, MAX_FDT_SIZE
, *new_fdt_addr
);
361 sys_table
->boottime
->free_pool(runtime_map
);
362 return EFI_LOAD_ERROR
;
365 void *get_fdt(efi_system_table_t
*sys_table
, unsigned long *fdt_size
)
367 efi_guid_t fdt_guid
= DEVICE_TREE_GUID
;
368 efi_config_table_t
*tables
;
372 tables
= (efi_config_table_t
*) sys_table
->tables
;
375 for (i
= 0; i
< sys_table
->nr_tables
; i
++)
376 if (efi_guidcmp(tables
[i
].guid
, fdt_guid
) == 0) {
377 fdt
= (void *) tables
[i
].table
;
378 if (fdt_check_header(fdt
) != 0) {
379 pr_efi_err(sys_table
, "Invalid header detected on UEFI supplied FDT, ignoring ...\n");
382 *fdt_size
= fdt_totalsize(fdt
);