* [PATCH 1/1] efi_loader: separate device-tree handling
@ 2020-10-06 22:10 Heinrich Schuchardt
2020-10-07 7:09 ` AKASHI Takahiro
0 siblings, 1 reply; 2+ messages in thread
From: Heinrich Schuchardt @ 2020-10-06 22:10 UTC (permalink / raw)
To: u-boot
Put UEFI related device tree handling into a separate C module.
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
cmd/bootefi.c | 277 ++-------------------------------
common/bootm_os.c | 15 +-
lib/efi_loader/Makefile | 3 +
lib/efi_loader/efi_fdt_fixup.c | 258 ++++++++++++++++++++++++++++++
4 files changed, 282 insertions(+), 271 deletions(-)
create mode 100644 lib/efi_loader/efi_fdt_fixup.c
diff --git a/cmd/bootefi.c b/cmd/bootefi.c
index fdf909f8da..3bf8dc0267 100644
--- a/cmd/bootefi.c
+++ b/cmd/bootefi.c
@@ -18,14 +18,9 @@
#include <image.h>
#include <log.h>
#include <malloc.h>
-#include <linux/libfdt.h>
-#include <linux/libfdt_env.h>
#include <mapmem.h>
#include <memalign.h>
#include <asm-generic/sections.h>
-#include <linux/linkage.h>
-
-DECLARE_GLOBAL_DATA_PTR;
static struct efi_device_path *bootefi_image_path;
static struct efi_device_path *bootefi_device_path;
@@ -64,265 +59,6 @@ static efi_status_t efi_env_set_load_options(efi_handle_t handle,
return ret;
}
-#if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
-
-/**
- * copy_fdt() - Copy the device tree to a new location available to EFI
- *
- * The FDT is copied to a suitable location within the EFI memory map.
- * Additional 12 KiB are added to the space in case the device tree needs to be
- * expanded later with fdt_open_into().
- *
- * @fdtp: On entry a pointer to the flattened device tree.
- * On exit a pointer to the copy of the flattened device tree.
- * FDT start
- * Return: status code
- */
-static efi_status_t copy_fdt(void **fdtp)
-{
- unsigned long fdt_ram_start = -1L, fdt_pages;
- efi_status_t ret = 0;
- void *fdt, *new_fdt;
- u64 new_fdt_addr;
- uint fdt_size;
- int i;
-
- for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
- u64 ram_start = gd->bd->bi_dram[i].start;
- u64 ram_size = gd->bd->bi_dram[i].size;
-
- if (!ram_size)
- continue;
-
- if (ram_start < fdt_ram_start)
- fdt_ram_start = ram_start;
- }
-
- /*
- * Give us at least 12 KiB of breathing room in case the device tree
- * needs to be expanded later.
- */
- fdt = *fdtp;
- fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
- fdt_size = fdt_pages << EFI_PAGE_SHIFT;
-
- /*
- * Safe fdt location is@127 MiB.
- * On the sandbox convert from the sandbox address space.
- */
- new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
- fdt_size, 0);
- ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
- EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
- &new_fdt_addr);
- if (ret != EFI_SUCCESS) {
- /* If we can't put it there, put it somewhere */
- new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
- ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
- EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
- &new_fdt_addr);
- if (ret != EFI_SUCCESS) {
- log_err("ERROR: Failed to reserve space for FDT\n");
- goto done;
- }
- }
- new_fdt = (void *)(uintptr_t)new_fdt_addr;
- memcpy(new_fdt, fdt, fdt_totalsize(fdt));
- fdt_set_totalsize(new_fdt, fdt_size);
-
- *fdtp = (void *)(uintptr_t)new_fdt_addr;
-done:
- return ret;
-}
-
-/**
- * efi_reserve_memory() - add reserved memory to memory map
- *
- * @addr: start address of the reserved memory range
- * @size: size of the reserved memory range
- * @nomap: indicates that the memory range shall not be accessed by the
- * UEFI payload
- */
-static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
-{
- int type;
- efi_uintn_t ret;
-
- /* Convert from sandbox address space. */
- addr = (uintptr_t)map_sysmem(addr, 0);
-
- if (nomap)
- type = EFI_RESERVED_MEMORY_TYPE;
- else
- type = EFI_BOOT_SERVICES_DATA;
-
- ret = efi_add_memory_map(addr, size, type);
- if (ret != EFI_SUCCESS)
- log_err("Reserved memory mapping failed addr %llx size %llx\n",
- addr, size);
-}
-
-/**
- * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
- *
- * The mem_rsv entries of the FDT are added to the memory map. Any failures are
- * ignored because this is not critical and we would rather continue to try to
- * boot.
- *
- * @fdt: Pointer to device tree
- */
-static void efi_carve_out_dt_rsv(void *fdt)
-{
- int nr_rsv, i;
- u64 addr, size;
- int nodeoffset, subnode;
-
- nr_rsv = fdt_num_mem_rsv(fdt);
-
- /* Look for an existing entry and add it to the efi mem map. */
- for (i = 0; i < nr_rsv; i++) {
- if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
- continue;
- efi_reserve_memory(addr, size, false);
- }
-
- /* process reserved-memory */
- nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
- if (nodeoffset >= 0) {
- subnode = fdt_first_subnode(fdt, nodeoffset);
- while (subnode >= 0) {
- fdt_addr_t fdt_addr;
- fdt_size_t fdt_size;
-
- /* check if this subnode has a reg property */
- fdt_addr = fdtdec_get_addr_size_auto_parent(
- fdt, nodeoffset, subnode,
- "reg", 0, &fdt_size, false);
- /*
- * The /reserved-memory node may have children with
- * a size instead of a reg property.
- */
- if (fdt_addr != FDT_ADDR_T_NONE &&
- fdtdec_get_is_enabled(fdt, subnode)) {
- bool nomap;
-
- nomap = !!fdt_getprop(fdt, subnode, "no-map",
- NULL);
- efi_reserve_memory(fdt_addr, fdt_size, nomap);
- }
- subnode = fdt_next_subnode(fdt, subnode);
- }
- }
-}
-
-/**
- * get_config_table() - get configuration table
- *
- * @guid: GUID of the configuration table
- * Return: pointer to configuration table or NULL
- */
-static void *get_config_table(const efi_guid_t *guid)
-{
- size_t i;
-
- for (i = 0; i < systab.nr_tables; i++) {
- if (!guidcmp(guid, &systab.tables[i].guid))
- return systab.tables[i].table;
- }
- return NULL;
-}
-
-#endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */
-
-/**
- * efi_install_fdt() - install device tree
- *
- * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
- * address will will be installed as configuration table, otherwise the device
- * tree located@the address indicated by environment variable fdt_addr or as
- * fallback fdtcontroladdr will be used.
- *
- * On architectures using ACPI tables device trees shall not be installed as
- * configuration table.
- *
- * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
- * the hardware device tree as indicated by environment variable
- * fdt_addr or as fallback the internal device tree as indicated by
- * the environment variable fdtcontroladdr
- * Return: status code
- */
-efi_status_t efi_install_fdt(void *fdt)
-{
- /*
- * The EBBR spec requires that we have either an FDT or an ACPI table
- * but not both.
- */
-#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
- if (fdt) {
- log_err("ERROR: can't have ACPI table and device tree.\n");
- return EFI_LOAD_ERROR;
- }
-#else
- bootm_headers_t img = { 0 };
- efi_status_t ret;
-
- if (fdt == EFI_FDT_USE_INTERNAL) {
- const char *fdt_opt;
- uintptr_t fdt_addr;
-
- /* Look for device tree that is already installed */
- if (get_config_table(&efi_guid_fdt))
- return EFI_SUCCESS;
- /* Check if there is a hardware device tree */
- fdt_opt = env_get("fdt_addr");
- /* Use our own device tree as fallback */
- if (!fdt_opt) {
- fdt_opt = env_get("fdtcontroladdr");
- if (!fdt_opt) {
- log_err("ERROR: need device tree\n");
- return EFI_NOT_FOUND;
- }
- }
- fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
- if (!fdt_addr) {
- log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
- return EFI_LOAD_ERROR;
- }
- fdt = map_sysmem(fdt_addr, 0);
- }
-
- /* Install device tree */
- if (fdt_check_header(fdt)) {
- log_err("ERROR: invalid device tree\n");
- return EFI_LOAD_ERROR;
- }
-
- /* Prepare device tree for payload */
- ret = copy_fdt(&fdt);
- if (ret) {
- log_err("ERROR: out of memory\n");
- return EFI_OUT_OF_RESOURCES;
- }
-
- if (image_setup_libfdt(&img, fdt, 0, NULL)) {
- log_err("ERROR: failed to process device tree\n");
- return EFI_LOAD_ERROR;
- }
-
- /* Create memory reservations as indicated by the device tree */
- efi_carve_out_dt_rsv(fdt);
-
- /* Install device tree as UEFI table */
- ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
- if (ret != EFI_SUCCESS) {
- log_err("ERROR: failed to install device tree\n");
- return ret;
- }
-#endif /* GENERATE_ACPI_TABLE */
-
- return EFI_SUCCESS;
-}
-
/**
* do_bootefi_exec() - execute EFI binary
*
@@ -647,7 +383,18 @@ static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
} else {
fdt = EFI_FDT_USE_INTERNAL;
}
- ret = efi_install_fdt(fdt);
+ if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
+ /*
+ * The EBBR spec requires that we have either an FDT or an ACPI table
+ * but not both.
+ */
+ if (fdt) {
+ log_err("ERROR: can't have ACPI table and device tree.\n");
+ return CMD_RET_FAILURE;
+ }
+ } else {
+ ret = efi_install_fdt(fdt);
+ }
if (ret == EFI_INVALID_PARAMETER)
return CMD_RET_USAGE;
else if (ret != EFI_SUCCESS)
diff --git a/common/bootm_os.c b/common/bootm_os.c
index e9aaddf3e6..340dceaa36 100644
--- a/common/bootm_os.c
+++ b/common/bootm_os.c
@@ -529,12 +529,15 @@ static int do_bootm_efi(int flag, int argc, char *const argv[],
}
/* Install device tree */
- efi_ret = efi_install_fdt(images->ft_len
- ? images->ft_addr : EFI_FDT_USE_INTERNAL);
- if (efi_ret != EFI_SUCCESS) {
- printf("## Failed to install device tree: r = %lu\n",
- efi_ret & ~EFI_ERROR_MASK);
- return 1;
+ if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
+ efi_ret = efi_install_fdt(images->ft_len ?
+ images->ft_addr :
+ EFI_FDT_USE_INTERNAL);
+ if (efi_ret != EFI_SUCCESS) {
+ printf("## Failed to install device tree: r = %lu\n",
+ efi_ret & ~EFI_ERROR_MASK);
+ return 1;
+ }
}
/* Run EFI image */
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index 9bad1d159b..af3ca7899f 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -27,6 +27,9 @@ obj-y += efi_console.o
obj-y += efi_device_path.o
obj-$(CONFIG_EFI_DEVICE_PATH_TO_TEXT) += efi_device_path_to_text.o
obj-y += efi_device_path_utilities.o
+ifeq ($(CONFIG_GENERATE_ACPI_TABLE),)
+obj-y += efi_fdt_fixup.o
+endif
obj-y += efi_file.o
obj-$(CONFIG_EFI_LOADER_HII) += efi_hii.o efi_hii_config.o
obj-y += efi_image_loader.o
diff --git a/lib/efi_loader/efi_fdt_fixup.c b/lib/efi_loader/efi_fdt_fixup.c
new file mode 100644
index 0000000000..2a1c9e5e87
--- /dev/null
+++ b/lib/efi_loader/efi_fdt_fixup.c
@@ -0,0 +1,258 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Fixup device tree
+ *
+ * Copyright (c) 2016-2020 Alexander Graf, Heinrich Schuchardt, et.al.
+ */
+
+#define LOG_CATEGORY LOGC_EFI
+
+#include <common.h>
+#include <efi_loader.h>
+#include <malloc.h>
+#include <mapmem.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/**
+ * copy_fdt() - Copy the device tree to a new location available to EFI
+ *
+ * The FDT is copied to a suitable location within the EFI memory map.
+ * Additional 12 KiB are added to the space in case the device tree needs to be
+ * expanded later with fdt_open_into().
+ *
+ * @fdtp: On entry a pointer to the flattened device tree.
+ * On exit a pointer to the copy of the flattened device tree.
+ * FDT start
+ * Return: status code
+ */
+static efi_status_t copy_fdt(void **fdtp)
+{
+ unsigned long fdt_ram_start = -1L, fdt_pages;
+ efi_status_t ret = 0;
+ void *fdt, *new_fdt;
+ u64 new_fdt_addr;
+ uint fdt_size;
+ int i;
+
+ for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
+ u64 ram_start = gd->bd->bi_dram[i].start;
+ u64 ram_size = gd->bd->bi_dram[i].size;
+
+ if (!ram_size)
+ continue;
+
+ if (ram_start < fdt_ram_start)
+ fdt_ram_start = ram_start;
+ }
+
+ /*
+ * Give us at least 12 KiB of breathing room in case the device tree
+ * needs to be expanded later.
+ */
+ fdt = *fdtp;
+ fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
+ fdt_size = fdt_pages << EFI_PAGE_SHIFT;
+
+ /*
+ * Safe fdt location is@127 MiB.
+ * On the sandbox convert from the sandbox address space.
+ */
+ new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
+ fdt_size, 0);
+ ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
+ EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
+ &new_fdt_addr);
+ if (ret != EFI_SUCCESS) {
+ /* If we can't put it there, put it somewhere */
+ new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
+ ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
+ EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
+ &new_fdt_addr);
+ if (ret != EFI_SUCCESS) {
+ log_err("ERROR: Failed to reserve space for FDT\n");
+ goto done;
+ }
+ }
+ new_fdt = (void *)(uintptr_t)new_fdt_addr;
+ memcpy(new_fdt, fdt, fdt_totalsize(fdt));
+ fdt_set_totalsize(new_fdt, fdt_size);
+
+ *fdtp = (void *)(uintptr_t)new_fdt_addr;
+done:
+ return ret;
+}
+
+/**
+ * efi_reserve_memory() - add reserved memory to memory map
+ *
+ * @addr: start address of the reserved memory range
+ * @size: size of the reserved memory range
+ * @nomap: indicates that the memory range shall not be accessed by the
+ * UEFI payload
+ */
+static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
+{
+ int type;
+ efi_uintn_t ret;
+
+ /* Convert from sandbox address space. */
+ addr = (uintptr_t)map_sysmem(addr, 0);
+
+ if (nomap)
+ type = EFI_RESERVED_MEMORY_TYPE;
+ else
+ type = EFI_BOOT_SERVICES_DATA;
+
+ ret = efi_add_memory_map(addr, size, type);
+ if (ret != EFI_SUCCESS)
+ log_err("Reserved memory mapping failed addr %llx size %llx\n",
+ addr, size);
+}
+
+/**
+ * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
+ *
+ * The mem_rsv entries of the FDT are added to the memory map. Any failures are
+ * ignored because this is not critical and we would rather continue to try to
+ * boot.
+ *
+ * @fdt: Pointer to device tree
+ */
+static void efi_carve_out_dt_rsv(void *fdt)
+{
+ int nr_rsv, i;
+ u64 addr, size;
+ int nodeoffset, subnode;
+
+ nr_rsv = fdt_num_mem_rsv(fdt);
+
+ /* Look for an existing entry and add it to the efi mem map. */
+ for (i = 0; i < nr_rsv; i++) {
+ if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
+ continue;
+ efi_reserve_memory(addr, size, false);
+ }
+
+ /* process reserved-memory */
+ nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
+ if (nodeoffset >= 0) {
+ subnode = fdt_first_subnode(fdt, nodeoffset);
+ while (subnode >= 0) {
+ fdt_addr_t fdt_addr;
+ fdt_size_t fdt_size;
+
+ /* check if this subnode has a reg property */
+ fdt_addr = fdtdec_get_addr_size_auto_parent(
+ fdt, nodeoffset, subnode,
+ "reg", 0, &fdt_size, false);
+ /*
+ * The /reserved-memory node may have children with
+ * a size instead of a reg property.
+ */
+ if (fdt_addr != FDT_ADDR_T_NONE &&
+ fdtdec_get_is_enabled(fdt, subnode)) {
+ bool nomap;
+
+ nomap = !!fdt_getprop(fdt, subnode, "no-map",
+ NULL);
+ efi_reserve_memory(fdt_addr, fdt_size, nomap);
+ }
+ subnode = fdt_next_subnode(fdt, subnode);
+ }
+ }
+}
+
+/**
+ * get_config_table() - get configuration table
+ *
+ * @guid: GUID of the configuration table
+ * Return: pointer to configuration table or NULL
+ */
+static void *get_config_table(const efi_guid_t *guid)
+{
+ size_t i;
+
+ for (i = 0; i < systab.nr_tables; i++) {
+ if (!guidcmp(guid, &systab.tables[i].guid))
+ return systab.tables[i].table;
+ }
+ return NULL;
+}
+
+/**
+ * efi_install_fdt() - install device tree
+ *
+ * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
+ * address will will be installed as configuration table, otherwise the device
+ * tree located@the address indicated by environment variable fdt_addr or as
+ * fallback fdtcontroladdr will be used.
+ *
+ * On architectures using ACPI tables device trees shall not be installed as
+ * configuration table.
+ *
+ * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
+ * the hardware device tree as indicated by environment variable
+ * fdt_addr or as fallback the internal device tree as indicated by
+ * the environment variable fdtcontroladdr
+ * Return: status code
+ */
+efi_status_t efi_install_fdt(void *fdt)
+{
+ bootm_headers_t img = { 0 };
+ efi_status_t ret;
+
+ if (fdt == EFI_FDT_USE_INTERNAL) {
+ const char *fdt_opt;
+ uintptr_t fdt_addr;
+
+ /* Look for device tree that is already installed */
+ if (get_config_table(&efi_guid_fdt))
+ return EFI_SUCCESS;
+ /* Check if there is a hardware device tree */
+ fdt_opt = env_get("fdt_addr");
+ /* Use our own device tree as fallback */
+ if (!fdt_opt) {
+ fdt_opt = env_get("fdtcontroladdr");
+ if (!fdt_opt) {
+ log_err("ERROR: need device tree\n");
+ return EFI_NOT_FOUND;
+ }
+ }
+ fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
+ if (!fdt_addr) {
+ log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
+ return EFI_LOAD_ERROR;
+ }
+ fdt = map_sysmem(fdt_addr, 0);
+ }
+
+ /* Install device tree */
+ if (fdt_check_header(fdt)) {
+ log_err("ERROR: invalid device tree\n");
+ return EFI_LOAD_ERROR;
+ }
+
+ /* Prepare device tree for payload */
+ ret = copy_fdt(&fdt);
+ if (ret) {
+ log_err("ERROR: out of memory\n");
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ if (image_setup_libfdt(&img, fdt, 0, NULL)) {
+ log_err("ERROR: failed to process device tree\n");
+ return EFI_LOAD_ERROR;
+ }
+
+ /* Create memory reservations as indicated by the device tree */
+ efi_carve_out_dt_rsv(fdt);
+
+ /* Install device tree as UEFI table */
+ ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
+ if (ret != EFI_SUCCESS) {
+ log_err("ERROR: failed to install device tree\n");
+ return ret;
+ }
+ return EFI_SUCCESS;
+}
--
2.28.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 1/1] efi_loader: separate device-tree handling
2020-10-06 22:10 [PATCH 1/1] efi_loader: separate device-tree handling Heinrich Schuchardt
@ 2020-10-07 7:09 ` AKASHI Takahiro
0 siblings, 0 replies; 2+ messages in thread
From: AKASHI Takahiro @ 2020-10-07 7:09 UTC (permalink / raw)
To: u-boot
On Wed, Oct 07, 2020 at 12:10:41AM +0200, Heinrich Schuchardt wrote:
> Put UEFI related device tree handling into a separate C module.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
> cmd/bootefi.c | 277 ++-------------------------------
> common/bootm_os.c | 15 +-
> lib/efi_loader/Makefile | 3 +
> lib/efi_loader/efi_fdt_fixup.c | 258 ++++++++++++++++++++++++++++++
> 4 files changed, 282 insertions(+), 271 deletions(-)
> create mode 100644 lib/efi_loader/efi_fdt_fixup.c
>
> diff --git a/cmd/bootefi.c b/cmd/bootefi.c
> index fdf909f8da..3bf8dc0267 100644
> --- a/cmd/bootefi.c
> +++ b/cmd/bootefi.c
> @@ -18,14 +18,9 @@
> #include <image.h>
> #include <log.h>
> #include <malloc.h>
> -#include <linux/libfdt.h>
> -#include <linux/libfdt_env.h>
> #include <mapmem.h>
> #include <memalign.h>
> #include <asm-generic/sections.h>
> -#include <linux/linkage.h>
> -
> -DECLARE_GLOBAL_DATA_PTR;
>
> static struct efi_device_path *bootefi_image_path;
> static struct efi_device_path *bootefi_device_path;
> @@ -64,265 +59,6 @@ static efi_status_t efi_env_set_load_options(efi_handle_t handle,
> return ret;
> }
>
> -#if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
> -
> -/**
> - * copy_fdt() - Copy the device tree to a new location available to EFI
> - *
> - * The FDT is copied to a suitable location within the EFI memory map.
> - * Additional 12 KiB are added to the space in case the device tree needs to be
> - * expanded later with fdt_open_into().
> - *
> - * @fdtp: On entry a pointer to the flattened device tree.
> - * On exit a pointer to the copy of the flattened device tree.
> - * FDT start
> - * Return: status code
> - */
> -static efi_status_t copy_fdt(void **fdtp)
> -{
> - unsigned long fdt_ram_start = -1L, fdt_pages;
> - efi_status_t ret = 0;
> - void *fdt, *new_fdt;
> - u64 new_fdt_addr;
> - uint fdt_size;
> - int i;
> -
> - for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
> - u64 ram_start = gd->bd->bi_dram[i].start;
> - u64 ram_size = gd->bd->bi_dram[i].size;
> -
> - if (!ram_size)
> - continue;
> -
> - if (ram_start < fdt_ram_start)
> - fdt_ram_start = ram_start;
> - }
> -
> - /*
> - * Give us at least 12 KiB of breathing room in case the device tree
> - * needs to be expanded later.
> - */
> - fdt = *fdtp;
> - fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
> - fdt_size = fdt_pages << EFI_PAGE_SHIFT;
> -
> - /*
> - * Safe fdt location is at 127 MiB.
> - * On the sandbox convert from the sandbox address space.
> - */
> - new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
> - fdt_size, 0);
> - ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
> - EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
> - &new_fdt_addr);
> - if (ret != EFI_SUCCESS) {
> - /* If we can't put it there, put it somewhere */
> - new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
> - ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
> - EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
> - &new_fdt_addr);
> - if (ret != EFI_SUCCESS) {
> - log_err("ERROR: Failed to reserve space for FDT\n");
> - goto done;
> - }
> - }
> - new_fdt = (void *)(uintptr_t)new_fdt_addr;
> - memcpy(new_fdt, fdt, fdt_totalsize(fdt));
> - fdt_set_totalsize(new_fdt, fdt_size);
> -
> - *fdtp = (void *)(uintptr_t)new_fdt_addr;
> -done:
> - return ret;
> -}
> -
> -/**
> - * efi_reserve_memory() - add reserved memory to memory map
> - *
> - * @addr: start address of the reserved memory range
> - * @size: size of the reserved memory range
> - * @nomap: indicates that the memory range shall not be accessed by the
> - * UEFI payload
> - */
> -static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
> -{
> - int type;
> - efi_uintn_t ret;
> -
> - /* Convert from sandbox address space. */
> - addr = (uintptr_t)map_sysmem(addr, 0);
> -
> - if (nomap)
> - type = EFI_RESERVED_MEMORY_TYPE;
> - else
> - type = EFI_BOOT_SERVICES_DATA;
> -
> - ret = efi_add_memory_map(addr, size, type);
> - if (ret != EFI_SUCCESS)
> - log_err("Reserved memory mapping failed addr %llx size %llx\n",
> - addr, size);
> -}
> -
> -/**
> - * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
> - *
> - * The mem_rsv entries of the FDT are added to the memory map. Any failures are
> - * ignored because this is not critical and we would rather continue to try to
> - * boot.
> - *
> - * @fdt: Pointer to device tree
> - */
> -static void efi_carve_out_dt_rsv(void *fdt)
> -{
> - int nr_rsv, i;
> - u64 addr, size;
> - int nodeoffset, subnode;
> -
> - nr_rsv = fdt_num_mem_rsv(fdt);
> -
> - /* Look for an existing entry and add it to the efi mem map. */
> - for (i = 0; i < nr_rsv; i++) {
> - if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
> - continue;
> - efi_reserve_memory(addr, size, false);
> - }
> -
> - /* process reserved-memory */
> - nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
> - if (nodeoffset >= 0) {
> - subnode = fdt_first_subnode(fdt, nodeoffset);
> - while (subnode >= 0) {
> - fdt_addr_t fdt_addr;
> - fdt_size_t fdt_size;
> -
> - /* check if this subnode has a reg property */
> - fdt_addr = fdtdec_get_addr_size_auto_parent(
> - fdt, nodeoffset, subnode,
> - "reg", 0, &fdt_size, false);
> - /*
> - * The /reserved-memory node may have children with
> - * a size instead of a reg property.
> - */
> - if (fdt_addr != FDT_ADDR_T_NONE &&
> - fdtdec_get_is_enabled(fdt, subnode)) {
> - bool nomap;
> -
> - nomap = !!fdt_getprop(fdt, subnode, "no-map",
> - NULL);
> - efi_reserve_memory(fdt_addr, fdt_size, nomap);
> - }
> - subnode = fdt_next_subnode(fdt, subnode);
> - }
> - }
> -}
> -
> -/**
> - * get_config_table() - get configuration table
> - *
> - * @guid: GUID of the configuration table
> - * Return: pointer to configuration table or NULL
> - */
> -static void *get_config_table(const efi_guid_t *guid)
> -{
> - size_t i;
> -
> - for (i = 0; i < systab.nr_tables; i++) {
> - if (!guidcmp(guid, &systab.tables[i].guid))
> - return systab.tables[i].table;
> - }
> - return NULL;
> -}
> -
> -#endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */
> -
> -/**
> - * efi_install_fdt() - install device tree
> - *
> - * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
> - * address will will be installed as configuration table, otherwise the device
> - * tree located at the address indicated by environment variable fdt_addr or as
> - * fallback fdtcontroladdr will be used.
> - *
> - * On architectures using ACPI tables device trees shall not be installed as
> - * configuration table.
> - *
> - * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
> - * the hardware device tree as indicated by environment variable
> - * fdt_addr or as fallback the internal device tree as indicated by
> - * the environment variable fdtcontroladdr
> - * Return: status code
> - */
> -efi_status_t efi_install_fdt(void *fdt)
> -{
> - /*
> - * The EBBR spec requires that we have either an FDT or an ACPI table
> - * but not both.
> - */
> -#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
> - if (fdt) {
> - log_err("ERROR: can't have ACPI table and device tree.\n");
> - return EFI_LOAD_ERROR;
> - }
> -#else
> - bootm_headers_t img = { 0 };
> - efi_status_t ret;
> -
> - if (fdt == EFI_FDT_USE_INTERNAL) {
> - const char *fdt_opt;
> - uintptr_t fdt_addr;
> -
> - /* Look for device tree that is already installed */
> - if (get_config_table(&efi_guid_fdt))
> - return EFI_SUCCESS;
> - /* Check if there is a hardware device tree */
> - fdt_opt = env_get("fdt_addr");
> - /* Use our own device tree as fallback */
> - if (!fdt_opt) {
> - fdt_opt = env_get("fdtcontroladdr");
> - if (!fdt_opt) {
> - log_err("ERROR: need device tree\n");
> - return EFI_NOT_FOUND;
> - }
> - }
> - fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
> - if (!fdt_addr) {
> - log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
> - return EFI_LOAD_ERROR;
> - }
> - fdt = map_sysmem(fdt_addr, 0);
> - }
> -
> - /* Install device tree */
> - if (fdt_check_header(fdt)) {
> - log_err("ERROR: invalid device tree\n");
> - return EFI_LOAD_ERROR;
> - }
> -
> - /* Prepare device tree for payload */
> - ret = copy_fdt(&fdt);
> - if (ret) {
> - log_err("ERROR: out of memory\n");
> - return EFI_OUT_OF_RESOURCES;
> - }
> -
> - if (image_setup_libfdt(&img, fdt, 0, NULL)) {
> - log_err("ERROR: failed to process device tree\n");
> - return EFI_LOAD_ERROR;
> - }
> -
> - /* Create memory reservations as indicated by the device tree */
> - efi_carve_out_dt_rsv(fdt);
> -
> - /* Install device tree as UEFI table */
> - ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
> - if (ret != EFI_SUCCESS) {
> - log_err("ERROR: failed to install device tree\n");
> - return ret;
> - }
> -#endif /* GENERATE_ACPI_TABLE */
> -
> - return EFI_SUCCESS;
> -}
> -
> /**
> * do_bootefi_exec() - execute EFI binary
> *
> @@ -647,7 +383,18 @@ static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
> } else {
> fdt = EFI_FDT_USE_INTERNAL;
> }
> - ret = efi_install_fdt(fdt);
> + if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
You added an extra logic here more than just moving fdt code around.
Please put the change in a separate patch.
> + /*
> + * The EBBR spec requires that we have either an FDT or an ACPI table
> + * but not both.
> + */
> + if (fdt) {
> + log_err("ERROR: can't have ACPI table and device tree.\n");
> + return CMD_RET_FAILURE;
> + }
> + } else {
> + ret = efi_install_fdt(fdt);
> + }
> if (ret == EFI_INVALID_PARAMETER)
> return CMD_RET_USAGE;
> else if (ret != EFI_SUCCESS)
> diff --git a/common/bootm_os.c b/common/bootm_os.c
> index e9aaddf3e6..340dceaa36 100644
> --- a/common/bootm_os.c
> +++ b/common/bootm_os.c
> @@ -529,12 +529,15 @@ static int do_bootm_efi(int flag, int argc, char *const argv[],
> }
>
> /* Install device tree */
> - efi_ret = efi_install_fdt(images->ft_len
> - ? images->ft_addr : EFI_FDT_USE_INTERNAL);
> - if (efi_ret != EFI_SUCCESS) {
> - printf("## Failed to install device tree: r = %lu\n",
> - efi_ret & ~EFI_ERROR_MASK);
> - return 1;
> + if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
ditto
-Takahiro Akashi
> + efi_ret = efi_install_fdt(images->ft_len ?
> + images->ft_addr :
> + EFI_FDT_USE_INTERNAL);
> + if (efi_ret != EFI_SUCCESS) {
> + printf("## Failed to install device tree: r = %lu\n",
> + efi_ret & ~EFI_ERROR_MASK);
> + return 1;
> + }
> }
>
> /* Run EFI image */
> diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
> index 9bad1d159b..af3ca7899f 100644
> --- a/lib/efi_loader/Makefile
> +++ b/lib/efi_loader/Makefile
> @@ -27,6 +27,9 @@ obj-y += efi_console.o
> obj-y += efi_device_path.o
> obj-$(CONFIG_EFI_DEVICE_PATH_TO_TEXT) += efi_device_path_to_text.o
> obj-y += efi_device_path_utilities.o
> +ifeq ($(CONFIG_GENERATE_ACPI_TABLE),)
> +obj-y += efi_fdt_fixup.o
> +endif
> obj-y += efi_file.o
> obj-$(CONFIG_EFI_LOADER_HII) += efi_hii.o efi_hii_config.o
> obj-y += efi_image_loader.o
> diff --git a/lib/efi_loader/efi_fdt_fixup.c b/lib/efi_loader/efi_fdt_fixup.c
> new file mode 100644
> index 0000000000..2a1c9e5e87
> --- /dev/null
> +++ b/lib/efi_loader/efi_fdt_fixup.c
> @@ -0,0 +1,258 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Fixup device tree
> + *
> + * Copyright (c) 2016-2020 Alexander Graf, Heinrich Schuchardt, et.al.
> + */
> +
> +#define LOG_CATEGORY LOGC_EFI
> +
> +#include <common.h>
> +#include <efi_loader.h>
> +#include <malloc.h>
> +#include <mapmem.h>
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +/**
> + * copy_fdt() - Copy the device tree to a new location available to EFI
> + *
> + * The FDT is copied to a suitable location within the EFI memory map.
> + * Additional 12 KiB are added to the space in case the device tree needs to be
> + * expanded later with fdt_open_into().
> + *
> + * @fdtp: On entry a pointer to the flattened device tree.
> + * On exit a pointer to the copy of the flattened device tree.
> + * FDT start
> + * Return: status code
> + */
> +static efi_status_t copy_fdt(void **fdtp)
> +{
> + unsigned long fdt_ram_start = -1L, fdt_pages;
> + efi_status_t ret = 0;
> + void *fdt, *new_fdt;
> + u64 new_fdt_addr;
> + uint fdt_size;
> + int i;
> +
> + for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
> + u64 ram_start = gd->bd->bi_dram[i].start;
> + u64 ram_size = gd->bd->bi_dram[i].size;
> +
> + if (!ram_size)
> + continue;
> +
> + if (ram_start < fdt_ram_start)
> + fdt_ram_start = ram_start;
> + }
> +
> + /*
> + * Give us at least 12 KiB of breathing room in case the device tree
> + * needs to be expanded later.
> + */
> + fdt = *fdtp;
> + fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
> + fdt_size = fdt_pages << EFI_PAGE_SHIFT;
> +
> + /*
> + * Safe fdt location is at 127 MiB.
> + * On the sandbox convert from the sandbox address space.
> + */
> + new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
> + fdt_size, 0);
> + ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
> + EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
> + &new_fdt_addr);
> + if (ret != EFI_SUCCESS) {
> + /* If we can't put it there, put it somewhere */
> + new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
> + ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
> + EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
> + &new_fdt_addr);
> + if (ret != EFI_SUCCESS) {
> + log_err("ERROR: Failed to reserve space for FDT\n");
> + goto done;
> + }
> + }
> + new_fdt = (void *)(uintptr_t)new_fdt_addr;
> + memcpy(new_fdt, fdt, fdt_totalsize(fdt));
> + fdt_set_totalsize(new_fdt, fdt_size);
> +
> + *fdtp = (void *)(uintptr_t)new_fdt_addr;
> +done:
> + return ret;
> +}
> +
> +/**
> + * efi_reserve_memory() - add reserved memory to memory map
> + *
> + * @addr: start address of the reserved memory range
> + * @size: size of the reserved memory range
> + * @nomap: indicates that the memory range shall not be accessed by the
> + * UEFI payload
> + */
> +static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
> +{
> + int type;
> + efi_uintn_t ret;
> +
> + /* Convert from sandbox address space. */
> + addr = (uintptr_t)map_sysmem(addr, 0);
> +
> + if (nomap)
> + type = EFI_RESERVED_MEMORY_TYPE;
> + else
> + type = EFI_BOOT_SERVICES_DATA;
> +
> + ret = efi_add_memory_map(addr, size, type);
> + if (ret != EFI_SUCCESS)
> + log_err("Reserved memory mapping failed addr %llx size %llx\n",
> + addr, size);
> +}
> +
> +/**
> + * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
> + *
> + * The mem_rsv entries of the FDT are added to the memory map. Any failures are
> + * ignored because this is not critical and we would rather continue to try to
> + * boot.
> + *
> + * @fdt: Pointer to device tree
> + */
> +static void efi_carve_out_dt_rsv(void *fdt)
> +{
> + int nr_rsv, i;
> + u64 addr, size;
> + int nodeoffset, subnode;
> +
> + nr_rsv = fdt_num_mem_rsv(fdt);
> +
> + /* Look for an existing entry and add it to the efi mem map. */
> + for (i = 0; i < nr_rsv; i++) {
> + if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
> + continue;
> + efi_reserve_memory(addr, size, false);
> + }
> +
> + /* process reserved-memory */
> + nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
> + if (nodeoffset >= 0) {
> + subnode = fdt_first_subnode(fdt, nodeoffset);
> + while (subnode >= 0) {
> + fdt_addr_t fdt_addr;
> + fdt_size_t fdt_size;
> +
> + /* check if this subnode has a reg property */
> + fdt_addr = fdtdec_get_addr_size_auto_parent(
> + fdt, nodeoffset, subnode,
> + "reg", 0, &fdt_size, false);
> + /*
> + * The /reserved-memory node may have children with
> + * a size instead of a reg property.
> + */
> + if (fdt_addr != FDT_ADDR_T_NONE &&
> + fdtdec_get_is_enabled(fdt, subnode)) {
> + bool nomap;
> +
> + nomap = !!fdt_getprop(fdt, subnode, "no-map",
> + NULL);
> + efi_reserve_memory(fdt_addr, fdt_size, nomap);
> + }
> + subnode = fdt_next_subnode(fdt, subnode);
> + }
> + }
> +}
> +
> +/**
> + * get_config_table() - get configuration table
> + *
> + * @guid: GUID of the configuration table
> + * Return: pointer to configuration table or NULL
> + */
> +static void *get_config_table(const efi_guid_t *guid)
> +{
> + size_t i;
> +
> + for (i = 0; i < systab.nr_tables; i++) {
> + if (!guidcmp(guid, &systab.tables[i].guid))
> + return systab.tables[i].table;
> + }
> + return NULL;
> +}
> +
> +/**
> + * efi_install_fdt() - install device tree
> + *
> + * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
> + * address will will be installed as configuration table, otherwise the device
> + * tree located at the address indicated by environment variable fdt_addr or as
> + * fallback fdtcontroladdr will be used.
> + *
> + * On architectures using ACPI tables device trees shall not be installed as
> + * configuration table.
> + *
> + * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
> + * the hardware device tree as indicated by environment variable
> + * fdt_addr or as fallback the internal device tree as indicated by
> + * the environment variable fdtcontroladdr
> + * Return: status code
> + */
> +efi_status_t efi_install_fdt(void *fdt)
> +{
> + bootm_headers_t img = { 0 };
> + efi_status_t ret;
> +
> + if (fdt == EFI_FDT_USE_INTERNAL) {
> + const char *fdt_opt;
> + uintptr_t fdt_addr;
> +
> + /* Look for device tree that is already installed */
> + if (get_config_table(&efi_guid_fdt))
> + return EFI_SUCCESS;
> + /* Check if there is a hardware device tree */
> + fdt_opt = env_get("fdt_addr");
> + /* Use our own device tree as fallback */
> + if (!fdt_opt) {
> + fdt_opt = env_get("fdtcontroladdr");
> + if (!fdt_opt) {
> + log_err("ERROR: need device tree\n");
> + return EFI_NOT_FOUND;
> + }
> + }
> + fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
> + if (!fdt_addr) {
> + log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
> + return EFI_LOAD_ERROR;
> + }
> + fdt = map_sysmem(fdt_addr, 0);
> + }
> +
> + /* Install device tree */
> + if (fdt_check_header(fdt)) {
> + log_err("ERROR: invalid device tree\n");
> + return EFI_LOAD_ERROR;
> + }
> +
> + /* Prepare device tree for payload */
> + ret = copy_fdt(&fdt);
> + if (ret) {
> + log_err("ERROR: out of memory\n");
> + return EFI_OUT_OF_RESOURCES;
> + }
> +
> + if (image_setup_libfdt(&img, fdt, 0, NULL)) {
> + log_err("ERROR: failed to process device tree\n");
> + return EFI_LOAD_ERROR;
> + }
> +
> + /* Create memory reservations as indicated by the device tree */
> + efi_carve_out_dt_rsv(fdt);
> +
> + /* Install device tree as UEFI table */
> + ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
> + if (ret != EFI_SUCCESS) {
> + log_err("ERROR: failed to install device tree\n");
> + return ret;
> + }
> + return EFI_SUCCESS;
> +}
> --
> 2.28.0
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-10-07 7:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-06 22:10 [PATCH 1/1] efi_loader: separate device-tree handling Heinrich Schuchardt
2020-10-07 7:09 ` AKASHI Takahiro
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox