[Concept,15/20] fdt: Add a function to show the reserved-memory nodes

Message ID 20250828020732.981415-16-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>

Add a new helper which lists the subnodes of the reserved-memory node.
This can be helpful when checking the devicetree before booting.

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

 boot/fdt_support.c    | 54 +++++++++++++++++++++++++++++++++++++++++++
 include/fdt_support.h |  7 ++++++
 2 files changed, 61 insertions(+)
  

Patch

diff --git a/boot/fdt_support.c b/boot/fdt_support.c
index 92f2f534ee0..f42eaa488de 100644
--- a/boot/fdt_support.c
+++ b/boot/fdt_support.c
@@ -2259,3 +2259,57 @@  int fdt_fixup_pmem_region(void *fdt, u64 pmem_start, u64 pmem_size)
 
 	return 0;
 }
+
+void fdt_print_reserved(void *fdt)
+{
+	int node, reserved, id;
+	int addr_cells, size_cells;
+
+	printf("%-4s %-20s %-18s %-18s\n", "ID", "Name", "Start", "Size");
+	printf("--------------------------------------------------------"
+	       "--------\n");
+
+	reserved = fdt_path_offset(fdt, "/reserved-memory");
+	if (reserved < 0) {
+		printf("No /reserved-memory node found in device tree\n");
+		return;
+	}
+
+	/* Get #address-cells and #size-cells from reserved-memory node */
+	fdt_support_default_count_cells(fdt, reserved, &addr_cells,
+					&size_cells);
+
+	id = 0;
+	fdt_for_each_subnode(node, fdt, reserved) {
+		const char *name = fdt_get_name(fdt, node, NULL);
+		const fdt32_t *reg;
+		u64 rsv_start = 0, rsv_size = 0;
+		int len;
+
+		reg = fdt_getprop(fdt, node, "reg", &len);
+		if (reg && len >= (addr_cells + size_cells) * sizeof(fdt32_t)) {
+			int cells = 0;
+
+			/* Parse address */
+			for (int i = 0; i < addr_cells; i++)
+				rsv_start = (rsv_start << 32) |
+					    fdt32_to_cpu(reg[cells++]);
+
+			/* Parse size */
+			for (int i = 0; i < size_cells; i++)
+				rsv_size = (rsv_size << 32) |
+					   fdt32_to_cpu(reg[cells++]);
+
+			printf("%-4d %-20s 0x%-16llx 0x%-16llx\n",
+			       id++, name ? name : "(unnamed)", rsv_start,
+			       rsv_size);
+		} else {
+			printf("%-4d %-20s %-18s %-18s\n",
+			       id++, name ? name : "(unnamed)", "(no reg)",
+			       "(no reg)");
+		}
+	}
+
+	if (!id)
+		printf("No reserved-memory regions found\n");
+}
diff --git a/include/fdt_support.h b/include/fdt_support.h
index eca9e7e3ac3..abd7504ad9b 100644
--- a/include/fdt_support.h
+++ b/include/fdt_support.h
@@ -529,4 +529,11 @@  int fdt_print(const void *fdt, int nodeoffset, int depth);
  */
 int fdt_print_path(const char *pathp, char *prop, int depth);
 
+/**
+ * fdt_print_reserved() - Print all device tree reserved-memory nodes
+ *
+ * @fdt: Device tree blob
+ */
+void fdt_print_reserved(void *fdt);
+
 #endif /* ifndef __FDT_SUPPORT_H */