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 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
,
23 efi_memory_desc_t
*memory_map
,
24 unsigned long map_size
, unsigned long desc_size
,
27 int node
, prev
, num_rsv
;
32 /* Do some checks on provided FDT, if it exists*/
34 if (fdt_check_header(orig_fdt
)) {
35 pr_efi_err(sys_table
, "Device Tree header not valid!\n");
36 return EFI_LOAD_ERROR
;
39 * We don't get the size of the FDT if we get if from a
40 * configuration table.
42 if (orig_fdt_size
&& fdt_totalsize(orig_fdt
) > orig_fdt_size
) {
43 pr_efi_err(sys_table
, "Truncated device tree! foo!\n");
44 return EFI_LOAD_ERROR
;
49 status
= fdt_open_into(orig_fdt
, fdt
, new_fdt_size
);
51 status
= fdt_create_empty_tree(fdt
, new_fdt_size
);
57 * Delete any memory nodes present. We must delete nodes which
58 * early_init_dt_scan_memory may try to use.
65 node
= fdt_next_node(fdt
, prev
, NULL
);
69 type
= fdt_getprop(fdt
, node
, "device_type", &len
);
70 if (type
&& strncmp(type
, "memory", len
) == 0) {
71 fdt_del_node(fdt
, node
);
79 * Delete all memory reserve map entries. When booting via UEFI,
80 * kernel will use the UEFI memory map to find reserved regions.
82 num_rsv
= fdt_num_mem_rsv(fdt
);
84 fdt_del_mem_rsv(fdt
, num_rsv
);
86 node
= fdt_subnode_offset(fdt
, 0, "chosen");
88 node
= fdt_add_subnode(fdt
, 0, "chosen");
90 status
= node
; /* node is error code when negative */
95 if ((cmdline_ptr
!= NULL
) && (strlen(cmdline_ptr
) > 0)) {
96 status
= fdt_setprop(fdt
, node
, "bootargs", cmdline_ptr
,
97 strlen(cmdline_ptr
) + 1);
102 /* Set initrd address/end in device tree, if present */
103 if (initrd_size
!= 0) {
104 u64 initrd_image_end
;
105 u64 initrd_image_start
= cpu_to_fdt64(initrd_addr
);
107 status
= fdt_setprop(fdt
, node
, "linux,initrd-start",
108 &initrd_image_start
, sizeof(u64
));
111 initrd_image_end
= cpu_to_fdt64(initrd_addr
+ initrd_size
);
112 status
= fdt_setprop(fdt
, node
, "linux,initrd-end",
113 &initrd_image_end
, sizeof(u64
));
118 /* Add FDT entries for EFI runtime services in chosen node. */
119 node
= fdt_subnode_offset(fdt
, 0, "chosen");
120 fdt_val64
= cpu_to_fdt64((u64
)(unsigned long)sys_table
);
121 status
= fdt_setprop(fdt
, node
, "linux,uefi-system-table",
122 &fdt_val64
, sizeof(fdt_val64
));
126 fdt_val64
= cpu_to_fdt64((u64
)(unsigned long)memory_map
);
127 status
= fdt_setprop(fdt
, node
, "linux,uefi-mmap-start",
128 &fdt_val64
, sizeof(fdt_val64
));
132 fdt_val32
= cpu_to_fdt32(map_size
);
133 status
= fdt_setprop(fdt
, node
, "linux,uefi-mmap-size",
134 &fdt_val32
, sizeof(fdt_val32
));
138 fdt_val32
= cpu_to_fdt32(desc_size
);
139 status
= fdt_setprop(fdt
, node
, "linux,uefi-mmap-desc-size",
140 &fdt_val32
, sizeof(fdt_val32
));
144 fdt_val32
= cpu_to_fdt32(desc_ver
);
145 status
= fdt_setprop(fdt
, node
, "linux,uefi-mmap-desc-ver",
146 &fdt_val32
, sizeof(fdt_val32
));
153 if (status
== -FDT_ERR_NOSPACE
)
154 return EFI_BUFFER_TOO_SMALL
;
156 return EFI_LOAD_ERROR
;
159 #ifndef EFI_FDT_ALIGN
160 #define EFI_FDT_ALIGN EFI_PAGE_SIZE
164 * Allocate memory for a new FDT, then add EFI, commandline, and
165 * initrd related fields to the FDT. This routine increases the
166 * FDT allocation size until the allocated memory is large
167 * enough. EFI allocations are in EFI_PAGE_SIZE granules,
168 * which are fixed at 4K bytes, so in most cases the first
169 * allocation should succeed.
170 * EFI boot services are exited at the end of this function.
171 * There must be no allocations between the get_memory_map()
172 * call and the exit_boot_services() call, so the exiting of
173 * boot services is very tightly tied to the creation of the FDT
174 * with the final memory map in it.
177 efi_status_t
allocate_new_fdt_and_exit_boot(efi_system_table_t
*sys_table
,
179 unsigned long *new_fdt_addr
,
180 unsigned long max_addr
,
181 u64 initrd_addr
, u64 initrd_size
,
183 unsigned long fdt_addr
,
184 unsigned long fdt_size
)
186 unsigned long map_size
, desc_size
;
188 unsigned long mmap_key
;
189 efi_memory_desc_t
*memory_map
, *runtime_map
;
190 unsigned long new_fdt_size
;
192 int runtime_entry_count
= 0;
195 * Get a copy of the current memory map that we will use to prepare
196 * the input for SetVirtualAddressMap(). We don't have to worry about
197 * subsequent allocations adding entries, since they could not affect
198 * the number of EFI_MEMORY_RUNTIME regions.
200 status
= efi_get_memory_map(sys_table
, &runtime_map
, &map_size
,
201 &desc_size
, &desc_ver
, &mmap_key
);
202 if (status
!= EFI_SUCCESS
) {
203 pr_efi_err(sys_table
, "Unable to retrieve UEFI memory map.\n");
208 "Exiting boot services and installing virtual address map...\n");
211 * Estimate size of new FDT, and allocate memory for it. We
212 * will allocate a bigger buffer if this ends up being too
213 * small, so a rough guess is OK here.
215 new_fdt_size
= fdt_size
+ EFI_PAGE_SIZE
;
217 status
= efi_high_alloc(sys_table
, new_fdt_size
, EFI_FDT_ALIGN
,
218 new_fdt_addr
, max_addr
);
219 if (status
!= EFI_SUCCESS
) {
220 pr_efi_err(sys_table
, "Unable to allocate memory for new device tree.\n");
225 * Now that we have done our final memory allocation (and free)
226 * we can get the memory map key needed for
227 * exit_boot_services().
229 status
= efi_get_memory_map(sys_table
, &memory_map
, &map_size
,
230 &desc_size
, &desc_ver
, &mmap_key
);
231 if (status
!= EFI_SUCCESS
)
232 goto fail_free_new_fdt
;
234 status
= update_fdt(sys_table
,
235 (void *)fdt_addr
, fdt_size
,
236 (void *)*new_fdt_addr
, new_fdt_size
,
237 cmdline_ptr
, initrd_addr
, initrd_size
,
238 memory_map
, map_size
, desc_size
, desc_ver
);
240 /* Succeeding the first time is the expected case. */
241 if (status
== EFI_SUCCESS
)
244 if (status
== EFI_BUFFER_TOO_SMALL
) {
246 * We need to allocate more space for the new
247 * device tree, so free existing buffer that is
248 * too small. Also free memory map, as we will need
249 * to get new one that reflects the free/alloc we do
250 * on the device tree buffer.
252 efi_free(sys_table
, new_fdt_size
, *new_fdt_addr
);
253 sys_table
->boottime
->free_pool(memory_map
);
254 new_fdt_size
+= EFI_PAGE_SIZE
;
256 pr_efi_err(sys_table
, "Unable to constuct new device tree.\n");
262 * Update the memory map with virtual addresses. The function will also
263 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
264 * entries so that we can pass it straight into SetVirtualAddressMap()
266 efi_get_virtmap(memory_map
, map_size
, desc_size
, runtime_map
,
267 &runtime_entry_count
);
269 /* Now we are ready to exit_boot_services.*/
270 status
= sys_table
->boottime
->exit_boot_services(handle
, mmap_key
);
272 if (status
== EFI_SUCCESS
) {
273 efi_set_virtual_address_map_t
*svam
;
275 /* Install the new virtual address map */
276 svam
= sys_table
->runtime
->set_virtual_address_map
;
277 status
= svam(runtime_entry_count
* desc_size
, desc_size
,
278 desc_ver
, runtime_map
);
281 * We are beyond the point of no return here, so if the call to
282 * SetVirtualAddressMap() failed, we need to signal that to the
283 * incoming kernel but proceed normally otherwise.
285 if (status
!= EFI_SUCCESS
) {
289 * Set the virtual address field of all
290 * EFI_MEMORY_RUNTIME entries to 0. This will signal
291 * the incoming kernel that no virtual translation has
294 for (l
= 0; l
< map_size
; l
+= desc_size
) {
295 efi_memory_desc_t
*p
= (void *)memory_map
+ l
;
297 if (p
->attribute
& EFI_MEMORY_RUNTIME
)
304 pr_efi_err(sys_table
, "Exit boot services failed.\n");
307 sys_table
->boottime
->free_pool(memory_map
);
310 efi_free(sys_table
, new_fdt_size
, *new_fdt_addr
);
313 sys_table
->boottime
->free_pool(runtime_map
);
314 return EFI_LOAD_ERROR
;
317 void *get_fdt(efi_system_table_t
*sys_table
, unsigned long *fdt_size
)
319 efi_guid_t fdt_guid
= DEVICE_TREE_GUID
;
320 efi_config_table_t
*tables
;
324 tables
= (efi_config_table_t
*) sys_table
->tables
;
327 for (i
= 0; i
< sys_table
->nr_tables
; i
++)
328 if (efi_guidcmp(tables
[i
].guid
, fdt_guid
) == 0) {
329 fdt
= (void *) tables
[i
].table
;
330 if (fdt_check_header(fdt
) != 0) {
331 pr_efi_err(sys_table
, "Invalid header detected on UEFI supplied FDT, ignoring ...\n");
334 *fdt_size
= fdt_totalsize(fdt
);