[PATCH RFC 07/14] efi: stub: add known memory to U-Boot's EFI memory map

Caleb Connolly caleb.connolly at linaro.org
Sun Nov 24 21:27:03 CET 2024


When running U-Boot as an EFI payload with CONFIG_EFI_STUB, the reserved
regions from the previous stage EFI bootloader should be carried over
since they may not fully align with the reserved-memory regions in
devicetree.

Implement a helper to map these pages when the EFI subsystem starts up.

Signed-off-by: Caleb Connolly <caleb.connolly at linaro.org>
---
 include/efi_stub.h          |  7 +++++++
 lib/efi/efi_info.c          | 31 +++++++++++++++++++++++++++++++
 lib/efi_loader/efi_memory.c |  5 +++++
 3 files changed, 43 insertions(+)

diff --git a/include/efi_stub.h b/include/efi_stub.h
index ff3befd4830b..343c84435970 100644
--- a/include/efi_stub.h
+++ b/include/efi_stub.h
@@ -56,5 +56,12 @@ int of_populate_from_efi(struct device_node *root);
  * EFI payload with CONFIG_EFI_STUB enabled.
  */
 int dram_init_banksize_from_efi(void);
 
+/**
+ * efi_add_known_memory_from_efi() - Add known memory pages from the memory map
+ * of the EFI bootloader that booted U-Boot. This is only applicable when running
+ * U-Boot as an EFI payload with CONFIG_EFI_STUB enabled.
+ */
+void efi_add_known_memory_from_efi(void);
+
 #endif /* _EFI_STUB_H */
diff --git a/lib/efi/efi_info.c b/lib/efi/efi_info.c
index f9743a3e7fad..ef4e646b082b 100644
--- a/lib/efi/efi_info.c
+++ b/lib/efi/efi_info.c
@@ -5,8 +5,9 @@
  * Access to the EFI information table
  */
 
 #include <efi.h>
+#include <efi_loader.h>
 #include <efi_stub.h>
 #include <errno.h>
 #include <mapmem.h>
 #include <asm/global_data.h>
@@ -177,4 +178,34 @@ int dram_init_banksize_from_efi(void)
 	}
 
 	return 0;
 }
+
+/* Called by U-Boot's EFI subsystem to add known memory. In our case
+ * we need to add some specific memory types from the original bootloaders
+ * EFI memory map
+ */
+void efi_add_known_memory_from_efi(void)
+{
+	struct efi_mem_desc *desc, *end;
+	struct efi_entry_memmap *map;
+	int ret, size;
+
+	EFI_PRINT("Adding known memory from previous stage EFI bootloader\n");
+
+	ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
+	if (ret) {
+		EFI_PRINT("%s: Missing memory map\n", __func__);
+		return;
+	}
+	end = (struct efi_mem_desc *)((ulong)map + size);
+
+	for (desc = map->desc; desc < end; desc = efi_get_next_mem_desc(desc, map->desc_size)) {
+		switch (desc->type) {
+		case EFI_RESERVED_MEMORY_TYPE:
+			efi_add_memory_map_pg(desc->physical_start, desc->num_pages, desc->type, false);
+			break;
+		default:
+			continue;
+		}
+	}
+}
diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
index d2f5d563f2a0..50b0010608e7 100644
--- a/lib/efi_loader/efi_memory.c
+++ b/lib/efi_loader/efi_memory.c
@@ -7,8 +7,9 @@
 
 #define LOG_CATEGORY LOGC_EFI
 
 #include <efi_loader.h>
+#include <efi_stub.h>
 #include <init.h>
 #include <lmb.h>
 #include <log.h>
 #include <malloc.h>
@@ -834,8 +835,12 @@ static void add_u_boot_and_runtime(void)
 int efi_memory_init(void)
 {
 	efi_add_known_memory();
 
+#ifdef CONFIG_EFI_STUB
+	efi_add_known_memory_from_efi();
+#endif
+
 	add_u_boot_and_runtime();
 
 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
 	/* Request a 32bit 64MB bounce buffer region */

-- 
2.47.0



More information about the U-Boot mailing list