]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
test: boot: add a fdt reserved region check
authorRandolph Sapp <rs@ti.com>
Thu, 4 Jun 2026 15:50:37 +0000 (10:50 -0500)
committerTom Rini <trini@konsulko.com>
Mon, 15 Jun 2026 17:04:39 +0000 (11:04 -0600)
Add a image_fdt suite and a check for boot_fdt_add_mem_rsv_regions. This
will ensure the user is properly informed of any reservation failures.
It will also validate that reservations are cleaned up correctly when
switching FDTs.

Signed-off-by: Randolph Sapp <rs@ti.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Acked-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
test/boot/Makefile
test/boot/image_fdt.c [new file with mode: 0644]
test/cmd_ut.c
test/py/tests/test_suite.py

index 89538d4f0a6755f6344e2dfde32123d1b9a64e61..12904f7f508a010a6cccdd512ba0da1be37b5e1d 100644 (file)
@@ -14,6 +14,9 @@ endif
 
 ifdef CONFIG_SANDBOX
 obj-$(CONFIG_$(PHASE_)CMDLINE) += bootm.o
+ifdef CONFIG_UT_DM
+obj-$(CONFIG_$(PHASE_)OF_LIBFDT) += image_fdt.o
+endif
 endif
 obj-$(CONFIG_MEASURED_BOOT) += measurement.o
 
diff --git a/test/boot/image_fdt.c b/test/boot/image_fdt.c
new file mode 100644 (file)
index 0000000..5417689
--- /dev/null
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2026 Texas Instruments Incorporated - https://www.ti.com/
+ */
+
+#include <config.h>
+
+#include <fdt_support.h>
+#include <image.h>
+#include <lmb.h>
+#include <malloc.h>
+
+#include <asm/global_data.h>
+
+#include <test/test.h>
+#include <test/ut.h>
+
+#define IMAGE_FDT_TEST(_name, _flags) UNIT_TEST(_name, _flags, image_fdt)
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/**
+ * test_boot_fdt_add_mem_rsv_regions - Make sure dt reservations are created and
+ *                                     destroyed correctly
+ * @uts: Test state
+ *
+ * This test depends on the UT_DM device tree and ensures the following
+ * statements hold true: The default reservation in test.dtb exists.
+ * Re-reserving that region will result in an error. Loading a new device tree
+ * will remove old reservations.
+ */
+static int test_boot_fdt_add_mem_rsv_regions(struct unit_test_state *uts)
+{
+       phys_addr_t start = CFG_SYS_SDRAM_BASE + 0x100000;
+       const void *old_blob = gd->fdt_blob;
+       int ret = CMD_RET_FAILURE;
+       ulong fdt_sz;
+       int nodeoffset;
+       void *new_blob;
+
+       /* Default reservation should exist */
+       ut_asserteq(1, lmb_is_reserved_flags(start, LMB_NOMAP));
+
+       /* Attempting to re-reserve should warn the user */
+       boot_fdt_add_mem_rsv_regions(gd->fdt_blob);
+       ut_assert_nextlinen("ERROR: reserving");
+       ut_assert_console_end();
+
+       /* Loading a new_blob device tree should be allowed */
+       fdt_sz = fdt_totalsize(gd->fdt_blob);
+       new_blob = malloc(fdt_sz);
+       ut_assertnonnull(new_blob);
+       memcpy(new_blob, gd->fdt_blob, fdt_sz);
+
+       nodeoffset = fdt_path_offset(new_blob, "/reserved-memory");
+       if (nodeoffset < 0)
+               goto free_blob;
+
+       if (fdt_del_node(new_blob, nodeoffset))
+               goto free_blob;
+
+       boot_fdt_add_mem_rsv_regions(new_blob);
+       gd->fdt_blob = new_blob;
+
+       if (ut_check_console_end(uts)) {
+               ut_failf(uts, __FILE__, __LINE__, __func__, "console",
+                        "Expected no more output, got '%s'", uts->actual_str);
+               goto switch_fdt;
+       }
+
+       /* Reservation should not exist now */
+       if (!lmb_is_reserved_flags(start, LMB_NOMAP))
+               ret = 0;
+
+       /* Cleanup */
+switch_fdt:
+       boot_fdt_add_mem_rsv_regions(old_blob);
+       gd->fdt_blob = old_blob;
+free_blob:
+       free(new_blob);
+       return ret;
+}
+IMAGE_FDT_TEST(test_boot_fdt_add_mem_rsv_regions, UTF_CONSOLE);
index 44e5fdfdaa6eaa21c770f137c8ae928871ffb05d..363ed4eab300e4da80e3e0582616452e3ce386f5 100644 (file)
@@ -61,6 +61,7 @@ SUITE_DECL(fdt);
 SUITE_DECL(fdt_overlay);
 SUITE_DECL(font);
 SUITE_DECL(hush);
+SUITE_DECL(image_fdt);
 SUITE_DECL(lib);
 SUITE_DECL(loadm);
 SUITE_DECL(log);
@@ -88,6 +89,7 @@ static struct suite suites[] = {
        SUITE(fdt_overlay, "device tree overlays"),
        SUITE(font, "font command"),
        SUITE(hush, "hush behaviour"),
+       SUITE(image_fdt, "image fdt parsing"),
        SUITE(lib, "library functions"),
        SUITE(loadm, "loadm command parameters and loading memory blob"),
        SUITE(log, "logging functions"),
index 7fe9a90dfd3969ce609f93736e5f623c608379ba..08285f12a5fda07c512db8d93fc61d1ca560b899 100644 (file)
@@ -8,7 +8,7 @@ import re
 EXPECTED_SUITES = [
     'addrmap', 'bdinfo', 'bloblist', 'bootm', 'bootstd',
     'cmd', 'common', 'dm', 'env', 'exit', 'fdt_overlay',
-    'fdt', 'font', 'hush', 'lib',
+    'fdt', 'font', 'hush', 'image_fdt', 'lib',
     'loadm', 'log', 'mbr', 'measurement', 'mem',
     'pci_mps', 'setexpr', 'upl',
     ]