[PATCH v5 8/9] efi: Reserve some memory for initial use

Simon Glass sjg at chromium.org
Sun Dec 1 16:28:10 CET 2024


The 'point of cooperation' is where U-Boot starts allowing EFI to use
memory outside of the U-Boot region. Until that point, it is desirable
to keep more below U-Boot free for loading images.

Reserve a small region for this purpose.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

(no changes since v1)

 include/asm-generic/global_data.h |  6 ++++++
 lib/efi_loader/efi_memory.c       | 21 +++++++++++++++++++++
 test/py/tests/test_event_dump.py  |  1 +
 3 files changed, 28 insertions(+)

diff --git a/include/asm-generic/global_data.h b/include/asm-generic/global_data.h
index 26277b93976..5df3cdd419d 100644
--- a/include/asm-generic/global_data.h
+++ b/include/asm-generic/global_data.h
@@ -442,6 +442,12 @@ struct global_data {
 	 */
 	struct upl *upl;
 #endif
+#if CONFIG_IS_ENABLED(EFI_LOADER)
+	/**
+	 * @efi_region: Start of EFI's early-memory region
+	 */
+	ulong efi_region;
+#endif
 };
 #ifndef DO_DEPS_ONLY
 static_assert(sizeof(struct global_data) == GD_SIZE);
diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
index 76cf908c2de..344656c4eb0 100644
--- a/lib/efi_loader/efi_memory.c
+++ b/lib/efi_loader/efi_memory.c
@@ -26,6 +26,12 @@ DECLARE_GLOBAL_DATA_PTR;
 /* Magic number identifying memory allocated from pool */
 #define EFI_ALLOC_POOL_MAGIC 0x1fe67ddf6491caa2
 
+/*
+ * Amount of memory to reserve for EFI before relocation. This must be a
+ * multiple of EFI_PAGE_SIZE
+ */
+#define EFI_EARLY_REGION_SIZE	SZ_256K
+
 efi_uintn_t efi_memory_map_key;
 
 /**
@@ -844,3 +850,18 @@ int efi_memory_init(void)
 
 	return 0;
 }
+
+static int reserve_efi_region(void)
+{
+	/*
+	 * Reserve some memory for EFI. Since pool allocations consume 4KB each
+	 * and there are three allocations, allow 16KB of memory, enough for
+	 * four. This can be increased as needed.
+	 */
+	gd->efi_region = ALIGN_DOWN(gd->start_addr_sp - EFI_EARLY_REGION_SIZE,
+				    SZ_4K);
+	gd->start_addr_sp = gd->efi_region;
+
+	return 0;
+}
+EVENT_SPY_SIMPLE(EVT_RESERVE, reserve_efi_region);
diff --git a/test/py/tests/test_event_dump.py b/test/py/tests/test_event_dump.py
index 45143c1c7d9..cc802583d3c 100644
--- a/test/py/tests/test_event_dump.py
+++ b/test/py/tests/test_event_dump.py
@@ -21,5 +21,6 @@ EVT_FT_FIXUP          bootmeth_vbe_simple_ft_fixup    .*boot/vbe_simple_os.c:.*
 EVT_LAST_STAGE_INIT   alloc_write_acpi_tables         .*lib/acpi/acpi_table.c:.*
 EVT_LAST_STAGE_INIT   install_smbios_table            .*lib/efi_loader/efi_smbios.c:.*
 EVT_MISC_INIT_F       sandbox_early_getopt_check      .*arch/sandbox/cpu/start.c:.*
+EVT_RESERVE           reserve_efi_region              .*lib/efi_loader/efi_memory.c:.*
 EVT_TEST              h_adder_simple                  .*test/common/event.c:'''
     assert re.match(expect, out, re.MULTILINE) is not None
-- 
2.43.0



More information about the U-Boot mailing list