[Concept,18/20] fdt: Provide a command to sync the reserve-memory node

Message ID 20250828020732.981415-19-sjg@u-boot.org
State New
Headers
Series efi: App and devicetree improvements |

Commit Message

Simon Glass Aug. 28, 2025, 2:07 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

Provide a command to sync reserved-memory with EFI's version of reserved
memory.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 cmd/efi.c             | 22 ++++++++++++++++++++--
 doc/usage/cmd/efi.rst | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)
  

Patch

diff --git a/cmd/efi.c b/cmd/efi.c
index c59219ddf01..c329e6eede8 100644
--- a/cmd/efi.c
+++ b/cmd/efi.c
@@ -198,9 +198,26 @@  static int do_efi_tables(struct cmd_tbl *cmdtp, int flag, int argc,
 	return 0;
 }
 
+static int do_efi_memsync(struct cmd_tbl *cmdtp, int flag, int argc,
+			  char *const argv[])
+{
+	bool verbose = false;
+	int ret;
+
+	if (argc > 1 && !strcmp(argv[1], "-v"))
+		verbose = true;
+
+	ret = efi_mem_reserved_sync(working_fdt, verbose);
+	if (ret < 0)
+		return CMD_RET_FAILURE;
+
+	return 0;
+}
+
 static struct cmd_tbl efi_commands[] = {
 	U_BOOT_CMD_MKENT(image, 1, 1, do_efi_image, "", ""),
 	U_BOOT_CMD_MKENT(mem, 1, 1, do_efi_mem, "", ""),
+	U_BOOT_CMD_MKENT(memsync, 2, 1, do_efi_memsync, "", ""),
 	U_BOOT_CMD_MKENT(tables, 1, 1, do_efi_tables, "", ""),
 };
 
@@ -223,9 +240,10 @@  static int do_efi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 }
 
 U_BOOT_CMD(
-	efi,     3,      1,      do_efi,
+	efi,     4,      1,      do_efi,
 	"EFI access",
 	"image            Dump loaded-image info\n"
 	"mem [all]        Dump memory information [include boot services]\n"
-	"tables               Dump tables"
+	"memsync [-v]     Sync EFI memory map with DT reserved-memory\n"
+	"tables           Dump tables"
 );
diff --git a/doc/usage/cmd/efi.rst b/doc/usage/cmd/efi.rst
index e5d8913c94b..f91a2bd7bec 100644
--- a/doc/usage/cmd/efi.rst
+++ b/doc/usage/cmd/efi.rst
@@ -14,6 +14,7 @@  Synopsis
 
     efi image
     efi mem [all]
+    efi memsync [-v]
     efi tables
 
 Description
@@ -69,6 +70,24 @@  Attributes
     Shows a code for memory attributes. The key for this is shown below the
     table.
 
+efi memsync
+~~~~~~~~~~~
+
+This synchronizes EFI reserved memory regions with the device tree's
+reserved-memory nodes. When running as an EFI application, U-Boot can access
+the EFI memory map to identify regions that EFI considers reserved (such as
+runtime services code/data, ACPI tables, etc.). This command compares these
+EFI reserved regions with the device tree's /reserved-memory nodes and adds
+any missing regions to prevent Linux from using memory that EFI has reserved.
+
+Use the optional '-v' flag for verbose output showing the detailed memory
+map analysis. Without this flag, only errors are displayed.
+
+This is useful for ensuring proper memory management when transitioning from
+EFI boot services to the operating system, particularly in complex firmware
+environments where EFI may have reserved memory regions not explicitly
+documented in the device tree.
+
 efi tables
 ~~~~~~~~~~
 
@@ -234,3 +253,20 @@  Example
     000000001fb7e000  eb9d2d30-2d88-11d3-9a16-0090273fc14d  EFI_GUID_EFI_ACPI1
     000000001fb7e014  8868e871-e4f1-11d3-bc22-0080c73c8881  ACPI table
     000000001e654018  dcfa911d-26eb-469f-a220-38b7dc461220  (unknown)
+
+    => efi memsync
+    Regions added: 2
+
+    => efi memsync -v
+    Comparing EFI memory-map with reserved-memory
+    EFI Memory Map Analysis:
+    ID   Type               Start              End                In DT?
+    ------------------------------------------------------------------------
+    0    EFI_RUNTIME_SERVICES_CODE 0x001f9ef000       0x001faef000       no -> adding
+    1    EFI_ACPI_RECLAIM_MEMORY   0x001fb6f000       0x001fb7f000       no -> adding
+    2    EFI_ACPI_MEMORY_NVS       0x001fb7f000       0x001fbff000       yes
+    Regions added: 2
+    Reserved memory regions:
+    0    tcg_event_log        0x100000           0x2000
+    1    efi-runtime-services-code@1f9ef000 0x1f9ef000       0x100000
+    2    efi-acpi-reclaim@1fb6f000 0x1fb6f000        0x10000