18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * FDT related Helper functions used by the EFI stub on multiple
48c2ecf20Sopenharmony_ci * architectures. This should be #included by the EFI stub
58c2ecf20Sopenharmony_ci * implementation files.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright 2013 Linaro Limited; author Roy Franz
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/efi.h>
118c2ecf20Sopenharmony_ci#include <linux/libfdt.h>
128c2ecf20Sopenharmony_ci#include <asm/efi.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "efistub.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define EFI_DT_ADDR_CELLS_DEFAULT 2
178c2ecf20Sopenharmony_ci#define EFI_DT_SIZE_CELLS_DEFAULT 2
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic void fdt_update_cell_size(void *fdt)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	int offset;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	offset = fdt_path_offset(fdt, "/");
248c2ecf20Sopenharmony_ci	/* Set the #address-cells and #size-cells values for an empty tree */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	fdt_setprop_u32(fdt, offset, "#address-cells", EFI_DT_ADDR_CELLS_DEFAULT);
278c2ecf20Sopenharmony_ci	fdt_setprop_u32(fdt, offset, "#size-cells",    EFI_DT_SIZE_CELLS_DEFAULT);
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic efi_status_t update_fdt(void *orig_fdt, unsigned long orig_fdt_size,
318c2ecf20Sopenharmony_ci			       void *fdt, int new_fdt_size, char *cmdline_ptr)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	int node, num_rsv;
348c2ecf20Sopenharmony_ci	int status;
358c2ecf20Sopenharmony_ci	u32 fdt_val32;
368c2ecf20Sopenharmony_ci	u64 fdt_val64;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* Do some checks on provided FDT, if it exists: */
398c2ecf20Sopenharmony_ci	if (orig_fdt) {
408c2ecf20Sopenharmony_ci		if (fdt_check_header(orig_fdt)) {
418c2ecf20Sopenharmony_ci			efi_err("Device Tree header not valid!\n");
428c2ecf20Sopenharmony_ci			return EFI_LOAD_ERROR;
438c2ecf20Sopenharmony_ci		}
448c2ecf20Sopenharmony_ci		/*
458c2ecf20Sopenharmony_ci		 * We don't get the size of the FDT if we get if from a
468c2ecf20Sopenharmony_ci		 * configuration table:
478c2ecf20Sopenharmony_ci		 */
488c2ecf20Sopenharmony_ci		if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) {
498c2ecf20Sopenharmony_ci			efi_err("Truncated device tree! foo!\n");
508c2ecf20Sopenharmony_ci			return EFI_LOAD_ERROR;
518c2ecf20Sopenharmony_ci		}
528c2ecf20Sopenharmony_ci	}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (orig_fdt) {
558c2ecf20Sopenharmony_ci		status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
568c2ecf20Sopenharmony_ci	} else {
578c2ecf20Sopenharmony_ci		status = fdt_create_empty_tree(fdt, new_fdt_size);
588c2ecf20Sopenharmony_ci		if (status == 0) {
598c2ecf20Sopenharmony_ci			/*
608c2ecf20Sopenharmony_ci			 * Any failure from the following function is
618c2ecf20Sopenharmony_ci			 * non-critical:
628c2ecf20Sopenharmony_ci			 */
638c2ecf20Sopenharmony_ci			fdt_update_cell_size(fdt);
648c2ecf20Sopenharmony_ci		}
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	if (status != 0)
688c2ecf20Sopenharmony_ci		goto fdt_set_fail;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/*
718c2ecf20Sopenharmony_ci	 * Delete all memory reserve map entries. When booting via UEFI,
728c2ecf20Sopenharmony_ci	 * kernel will use the UEFI memory map to find reserved regions.
738c2ecf20Sopenharmony_ci	 */
748c2ecf20Sopenharmony_ci	num_rsv = fdt_num_mem_rsv(fdt);
758c2ecf20Sopenharmony_ci	while (num_rsv-- > 0)
768c2ecf20Sopenharmony_ci		fdt_del_mem_rsv(fdt, num_rsv);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	node = fdt_subnode_offset(fdt, 0, "chosen");
798c2ecf20Sopenharmony_ci	if (node < 0) {
808c2ecf20Sopenharmony_ci		node = fdt_add_subnode(fdt, 0, "chosen");
818c2ecf20Sopenharmony_ci		if (node < 0) {
828c2ecf20Sopenharmony_ci			/* 'node' is an error code when negative: */
838c2ecf20Sopenharmony_ci			status = node;
848c2ecf20Sopenharmony_ci			goto fdt_set_fail;
858c2ecf20Sopenharmony_ci		}
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (cmdline_ptr != NULL && strlen(cmdline_ptr) > 0) {
898c2ecf20Sopenharmony_ci		status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
908c2ecf20Sopenharmony_ci				     strlen(cmdline_ptr) + 1);
918c2ecf20Sopenharmony_ci		if (status)
928c2ecf20Sopenharmony_ci			goto fdt_set_fail;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* Add FDT entries for EFI runtime services in chosen node. */
968c2ecf20Sopenharmony_ci	node = fdt_subnode_offset(fdt, 0, "chosen");
978c2ecf20Sopenharmony_ci	fdt_val64 = cpu_to_fdt64((u64)(unsigned long)efi_system_table);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	status = fdt_setprop_var(fdt, node, "linux,uefi-system-table", fdt_val64);
1008c2ecf20Sopenharmony_ci	if (status)
1018c2ecf20Sopenharmony_ci		goto fdt_set_fail;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	fdt_val64 = U64_MAX; /* placeholder */
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-start", fdt_val64);
1068c2ecf20Sopenharmony_ci	if (status)
1078c2ecf20Sopenharmony_ci		goto fdt_set_fail;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	fdt_val32 = U32_MAX; /* placeholder */
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-size", fdt_val32);
1128c2ecf20Sopenharmony_ci	if (status)
1138c2ecf20Sopenharmony_ci		goto fdt_set_fail;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-desc-size", fdt_val32);
1168c2ecf20Sopenharmony_ci	if (status)
1178c2ecf20Sopenharmony_ci		goto fdt_set_fail;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-desc-ver", fdt_val32);
1208c2ecf20Sopenharmony_ci	if (status)
1218c2ecf20Sopenharmony_ci		goto fdt_set_fail;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
1248c2ecf20Sopenharmony_ci		efi_status_t efi_status;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci		efi_status = efi_get_random_bytes(sizeof(fdt_val64),
1278c2ecf20Sopenharmony_ci						  (u8 *)&fdt_val64);
1288c2ecf20Sopenharmony_ci		if (efi_status == EFI_SUCCESS) {
1298c2ecf20Sopenharmony_ci			status = fdt_setprop_var(fdt, node, "kaslr-seed", fdt_val64);
1308c2ecf20Sopenharmony_ci			if (status)
1318c2ecf20Sopenharmony_ci				goto fdt_set_fail;
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/* Shrink the FDT back to its minimum size: */
1368c2ecf20Sopenharmony_ci	fdt_pack(fdt);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return EFI_SUCCESS;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cifdt_set_fail:
1418c2ecf20Sopenharmony_ci	if (status == -FDT_ERR_NOSPACE)
1428c2ecf20Sopenharmony_ci		return EFI_BUFFER_TOO_SMALL;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	return EFI_LOAD_ERROR;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	int node = fdt_path_offset(fdt, "/chosen");
1508c2ecf20Sopenharmony_ci	u64 fdt_val64;
1518c2ecf20Sopenharmony_ci	u32 fdt_val32;
1528c2ecf20Sopenharmony_ci	int err;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (node < 0)
1558c2ecf20Sopenharmony_ci		return EFI_LOAD_ERROR;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	fdt_val64 = cpu_to_fdt64((unsigned long)map->map);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-start", fdt_val64);
1608c2ecf20Sopenharmony_ci	if (err)
1618c2ecf20Sopenharmony_ci		return EFI_LOAD_ERROR;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	fdt_val32 = cpu_to_fdt32(map->map_size);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-size", fdt_val32);
1668c2ecf20Sopenharmony_ci	if (err)
1678c2ecf20Sopenharmony_ci		return EFI_LOAD_ERROR;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	fdt_val32 = cpu_to_fdt32(map->desc_size);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-desc-size", fdt_val32);
1728c2ecf20Sopenharmony_ci	if (err)
1738c2ecf20Sopenharmony_ci		return EFI_LOAD_ERROR;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	fdt_val32 = cpu_to_fdt32(map->desc_ver);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-desc-ver", fdt_val32);
1788c2ecf20Sopenharmony_ci	if (err)
1798c2ecf20Sopenharmony_ci		return EFI_LOAD_ERROR;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return EFI_SUCCESS;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistruct exit_boot_struct {
1858c2ecf20Sopenharmony_ci	struct efi_boot_memmap	*boot_memmap;
1868c2ecf20Sopenharmony_ci	efi_memory_desc_t	*runtime_map;
1878c2ecf20Sopenharmony_ci	int			runtime_entry_count;
1888c2ecf20Sopenharmony_ci	void			*new_fdt_addr;
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct exit_boot_struct *p = priv;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	p->boot_memmap = map;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/*
1988c2ecf20Sopenharmony_ci	 * Update the memory map with virtual addresses. The function will also
1998c2ecf20Sopenharmony_ci	 * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
2008c2ecf20Sopenharmony_ci	 * entries so that we can pass it straight to SetVirtualAddressMap()
2018c2ecf20Sopenharmony_ci	 */
2028c2ecf20Sopenharmony_ci	efi_get_virtmap(map->map, map->map_size, map->desc_size,
2038c2ecf20Sopenharmony_ci			p->runtime_map, &p->runtime_entry_count);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	return update_fdt_memmap(p->new_fdt_addr, map);
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci#ifndef MAX_FDT_SIZE
2098c2ecf20Sopenharmony_ci# define MAX_FDT_SIZE SZ_2M
2108c2ecf20Sopenharmony_ci#endif
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/*
2138c2ecf20Sopenharmony_ci * Allocate memory for a new FDT, then add EFI and commandline related fields
2148c2ecf20Sopenharmony_ci * to the FDT.  This routine increases the FDT allocation size until the
2158c2ecf20Sopenharmony_ci * allocated memory is large enough.  EFI allocations are in EFI_PAGE_SIZE
2168c2ecf20Sopenharmony_ci * granules, which are fixed at 4K bytes, so in most cases the first allocation
2178c2ecf20Sopenharmony_ci * should succeed.  EFI boot services are exited at the end of this function.
2188c2ecf20Sopenharmony_ci * There must be no allocations between the get_memory_map() call and the
2198c2ecf20Sopenharmony_ci * exit_boot_services() call, so the exiting of boot services is very tightly
2208c2ecf20Sopenharmony_ci * tied to the creation of the FDT with the final memory map in it.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_cistatic
2238c2ecf20Sopenharmony_ciefi_status_t allocate_new_fdt_and_exit_boot(void *handle,
2248c2ecf20Sopenharmony_ci					    efi_loaded_image_t *image,
2258c2ecf20Sopenharmony_ci					    unsigned long *new_fdt_addr,
2268c2ecf20Sopenharmony_ci					    char *cmdline_ptr)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	unsigned long desc_size;
2298c2ecf20Sopenharmony_ci	u32 desc_ver;
2308c2ecf20Sopenharmony_ci	efi_status_t status;
2318c2ecf20Sopenharmony_ci	struct exit_boot_struct priv;
2328c2ecf20Sopenharmony_ci	unsigned long fdt_addr = 0;
2338c2ecf20Sopenharmony_ci	unsigned long fdt_size = 0;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if (!efi_novamap) {
2368c2ecf20Sopenharmony_ci		status = efi_alloc_virtmap(&priv.runtime_map, &desc_size,
2378c2ecf20Sopenharmony_ci					   &desc_ver);
2388c2ecf20Sopenharmony_ci		if (status != EFI_SUCCESS) {
2398c2ecf20Sopenharmony_ci			efi_err("Unable to retrieve UEFI memory map.\n");
2408c2ecf20Sopenharmony_ci			return status;
2418c2ecf20Sopenharmony_ci		}
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/*
2458c2ecf20Sopenharmony_ci	 * Unauthenticated device tree data is a security hazard, so ignore
2468c2ecf20Sopenharmony_ci	 * 'dtb=' unless UEFI Secure Boot is disabled.  We assume that secure
2478c2ecf20Sopenharmony_ci	 * boot is enabled if we can't determine its state.
2488c2ecf20Sopenharmony_ci	 */
2498c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) ||
2508c2ecf20Sopenharmony_ci	    efi_get_secureboot() != efi_secureboot_mode_disabled) {
2518c2ecf20Sopenharmony_ci		if (strstr(cmdline_ptr, "dtb="))
2528c2ecf20Sopenharmony_ci			efi_err("Ignoring DTB from command line.\n");
2538c2ecf20Sopenharmony_ci	} else {
2548c2ecf20Sopenharmony_ci		status = efi_load_dtb(image, &fdt_addr, &fdt_size);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci		if (status != EFI_SUCCESS && status != EFI_NOT_READY) {
2578c2ecf20Sopenharmony_ci			efi_err("Failed to load device tree!\n");
2588c2ecf20Sopenharmony_ci			goto fail;
2598c2ecf20Sopenharmony_ci		}
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	if (fdt_addr) {
2638c2ecf20Sopenharmony_ci		efi_info("Using DTB from command line\n");
2648c2ecf20Sopenharmony_ci	} else {
2658c2ecf20Sopenharmony_ci		/* Look for a device tree configuration table entry. */
2668c2ecf20Sopenharmony_ci		fdt_addr = (uintptr_t)get_fdt(&fdt_size);
2678c2ecf20Sopenharmony_ci		if (fdt_addr)
2688c2ecf20Sopenharmony_ci			efi_info("Using DTB from configuration table\n");
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	if (!fdt_addr)
2728c2ecf20Sopenharmony_ci		efi_info("Generating empty DTB\n");
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	efi_info("Exiting boot services...\n");
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	status = efi_allocate_pages(MAX_FDT_SIZE, new_fdt_addr, ULONG_MAX);
2778c2ecf20Sopenharmony_ci	if (status != EFI_SUCCESS) {
2788c2ecf20Sopenharmony_ci		efi_err("Unable to allocate memory for new device tree.\n");
2798c2ecf20Sopenharmony_ci		goto fail;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	status = update_fdt((void *)fdt_addr, fdt_size,
2838c2ecf20Sopenharmony_ci			    (void *)*new_fdt_addr, MAX_FDT_SIZE, cmdline_ptr);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	if (status != EFI_SUCCESS) {
2868c2ecf20Sopenharmony_ci		efi_err("Unable to construct new device tree.\n");
2878c2ecf20Sopenharmony_ci		goto fail_free_new_fdt;
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	priv.new_fdt_addr = (void *)*new_fdt_addr;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (status == EFI_SUCCESS) {
2958c2ecf20Sopenharmony_ci		efi_set_virtual_address_map_t *svam;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci		if (efi_novamap)
2988c2ecf20Sopenharmony_ci			return EFI_SUCCESS;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci		/* Install the new virtual address map */
3018c2ecf20Sopenharmony_ci		svam = efi_system_table->runtime->set_virtual_address_map;
3028c2ecf20Sopenharmony_ci		status = svam(priv.runtime_entry_count * desc_size, desc_size,
3038c2ecf20Sopenharmony_ci			      desc_ver, priv.runtime_map);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci		/*
3068c2ecf20Sopenharmony_ci		 * We are beyond the point of no return here, so if the call to
3078c2ecf20Sopenharmony_ci		 * SetVirtualAddressMap() failed, we need to signal that to the
3088c2ecf20Sopenharmony_ci		 * incoming kernel but proceed normally otherwise.
3098c2ecf20Sopenharmony_ci		 */
3108c2ecf20Sopenharmony_ci		if (status != EFI_SUCCESS) {
3118c2ecf20Sopenharmony_ci			efi_memory_desc_t *p;
3128c2ecf20Sopenharmony_ci			int l;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci			/*
3158c2ecf20Sopenharmony_ci			 * Set the virtual address field of all
3168c2ecf20Sopenharmony_ci			 * EFI_MEMORY_RUNTIME entries to U64_MAX. This will
3178c2ecf20Sopenharmony_ci			 * signal the incoming kernel that no virtual
3188c2ecf20Sopenharmony_ci			 * translation has been installed.
3198c2ecf20Sopenharmony_ci			 */
3208c2ecf20Sopenharmony_ci			for (l = 0; l < priv.boot_memmap->map_size;
3218c2ecf20Sopenharmony_ci			     l += priv.boot_memmap->desc_size) {
3228c2ecf20Sopenharmony_ci				p = (void *)priv.boot_memmap->map + l;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci				if (p->attribute & EFI_MEMORY_RUNTIME)
3258c2ecf20Sopenharmony_ci					p->virt_addr = U64_MAX;
3268c2ecf20Sopenharmony_ci			}
3278c2ecf20Sopenharmony_ci		}
3288c2ecf20Sopenharmony_ci		return EFI_SUCCESS;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	efi_err("Exit boot services failed.\n");
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cifail_free_new_fdt:
3348c2ecf20Sopenharmony_ci	efi_free(MAX_FDT_SIZE, *new_fdt_addr);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cifail:
3378c2ecf20Sopenharmony_ci	efi_free(fdt_size, fdt_addr);
3388c2ecf20Sopenharmony_ci	if (!efi_novamap)
3398c2ecf20Sopenharmony_ci		efi_bs_call(free_pool, priv.runtime_map);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	return EFI_LOAD_ERROR;
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ciefi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
3458c2ecf20Sopenharmony_ci			     unsigned long kernel_addr, char *cmdline_ptr)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	unsigned long fdt_addr;
3488c2ecf20Sopenharmony_ci	efi_status_t status;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	status = allocate_new_fdt_and_exit_boot(handle, image, &fdt_addr,
3518c2ecf20Sopenharmony_ci						cmdline_ptr);
3528c2ecf20Sopenharmony_ci	if (status != EFI_SUCCESS) {
3538c2ecf20Sopenharmony_ci		efi_err("Failed to update FDT and exit boot services\n");
3548c2ecf20Sopenharmony_ci		return status;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_ARM))
3588c2ecf20Sopenharmony_ci		efi_handle_post_ebs_state();
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	efi_enter_kernel(kernel_addr, fdt_addr, fdt_totalsize((void *)fdt_addr));
3618c2ecf20Sopenharmony_ci	/* not reached */
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_civoid *get_fdt(unsigned long *fdt_size)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	void *fdt;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	fdt = get_efi_config_table(DEVICE_TREE_GUID);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	if (!fdt)
3718c2ecf20Sopenharmony_ci		return NULL;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (fdt_check_header(fdt) != 0) {
3748c2ecf20Sopenharmony_ci		efi_err("Invalid header detected on UEFI supplied FDT, ignoring ...\n");
3758c2ecf20Sopenharmony_ci		return NULL;
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci	*fdt_size = fdt_totalsize(fdt);
3788c2ecf20Sopenharmony_ci	return fdt;
3798c2ecf20Sopenharmony_ci}
380