[PATCH v2 22/45] sandbox: Support setting up the other FDT for testing

Simon Glass sjg at chromium.org
Wed Sep 7 04:27:10 CEST 2022


Provide a way to copy over the 'other' FDT when running tests. This loads
it and allocates memory for the copy, if not done already, then does the
copy.

Avoid using U-Boot's malloc() pool for these copies, at least for now,
since they are part of the test system.

Tidy up the cpu.c header files while here.

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

(no changes since v1)

 arch/sandbox/cpu/cpu.c          | 34 ++++++++++++++++++++++++++++++---
 arch/sandbox/include/asm/test.h | 19 ++++++++++++++++++
 include/test/test.h             | 19 ++++++++++++++++--
 3 files changed, 67 insertions(+), 5 deletions(-)

diff --git a/arch/sandbox/cpu/cpu.c b/arch/sandbox/cpu/cpu.c
index d077948dd7b..636d3545b95 100644
--- a/arch/sandbox/cpu/cpu.c
+++ b/arch/sandbox/cpu/cpu.c
@@ -3,19 +3,22 @@
  * Copyright (c) 2011 The Chromium OS Authors.
  */
 
+#define LOG_CATEGORY	LOGC_SANDBOX
+
 #include <common.h>
 #include <bootstage.h>
 #include <cpu_func.h>
 #include <errno.h>
 #include <log.h>
-#include <asm/global_data.h>
-#include <linux/delay.h>
-#include <linux/libfdt.h>
 #include <os.h>
+#include <asm/global_data.h>
 #include <asm/io.h>
 #include <asm/malloc.h>
 #include <asm/setjmp.h>
 #include <asm/state.h>
+#include <dm/ofnode.h>
+#include <linux/delay.h>
+#include <linux/libfdt.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -373,3 +376,28 @@ ulong timer_get_boot_us(void)
 
 	return (count - base_count) / 1000;
 }
+
+int sandbox_load_other_fdt(void **fdtp, int *sizep)
+{
+	const char *orig;
+	int ret, size;
+	void *fdt = *fdtp;
+
+	ret = state_load_other_fdt(&orig, &size);
+	if (ret) {
+		log_err("Cannot read other FDT\n");
+		return log_msg_ret("ld", ret);
+	}
+
+	if (!*fdtp) {
+		fdt = os_malloc(size);
+		if (!fdt)
+			return log_msg_ret("mem", -ENOMEM);
+		*sizep = size;
+	}
+
+	memcpy(fdt, orig, *sizep);
+	*fdtp = fdt;
+
+	return 0;
+}
diff --git a/arch/sandbox/include/asm/test.h b/arch/sandbox/include/asm/test.h
index 53a036b3abf..0406085917f 100644
--- a/arch/sandbox/include/asm/test.h
+++ b/arch/sandbox/include/asm/test.h
@@ -11,6 +11,8 @@
 #include <video.h>
 #include <pci_ids.h>
 
+struct unit_test_state;
+
 /* The sandbox driver always permits an I2C device with this address */
 #define SANDBOX_I2C_TEST_ADDR		0x59
 
@@ -315,4 +317,21 @@ int sandbox_sdl_set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp);
  */
 void sandbox_set_fake_efi_mgr_dev(struct udevice *dev, bool fake_dev);
 
+/**
+ * sandbox_load_other_fdt() - load the 'other' FDT into the test state
+ *
+ * This copies the other.dtb file into the test state, so that a fresh version
+ * can be used for a test that is about to run.
+ *
+ * If @uts->other_fdt is NULL, as it is when first set up, this allocates a
+ * buffer for the other FDT and sets @uts->other_fdt_size to its size.
+ *
+ * In any case, the other FDT is copied from the sandbox state into
+ * @uts->other_fdt ready for use.
+ *
+ * @uts: Unit test state
+ * @return 0 if OK, -ve on error
+ */
+int sandbox_load_other_fdt(void **fdtp, int *sizep);
+
 #endif
diff --git a/include/test/test.h b/include/test/test.h
index c028bbb8c89..d6149df0c6b 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -23,6 +23,8 @@
  * @fdt_chksum: crc8 of the device tree contents
  * @fdt_copy: Copy of the device tree
  * @fdt_size: Size of the device-tree copy
+ * @other_fdt: Buffer for the other FDT (UT_TESTF_OTHER_FDT)
+ * @other_fdt_size: Size of the other FDT (UT_TESTF_OTHER_FDT)
  * @expect_str: Temporary string used to hold expected string value
  * @actual_str: Temporary string used to hold actual string value
  */
@@ -38,6 +40,8 @@ struct unit_test_state {
 	uint fdt_chksum;
 	void *fdt_copy;
 	uint fdt_size;
+	void *other_fdt;
+	int other_fdt_size;
 	char expect_str[512];
 	char actual_str[512];
 };
@@ -130,13 +134,24 @@ enum {
  */
 struct udevice *testbus_get_clear_removed(void);
 
-static inline void arch_reset_for_test(void)
-{
 #ifdef CONFIG_SANDBOX
 #include <asm/state.h>
+#include <asm/test.h>
+#endif
 
+static inline void arch_reset_for_test(void)
+{
+#ifdef CONFIG_SANDBOX
 	state_reset_for_test(state_get_current());
 #endif
 }
+static inline int test_load_other_fdt(struct unit_test_state *uts)
+{
+	int ret = 0;
+#ifdef CONFIG_SANDBOX
+	ret = sandbox_load_other_fdt(&uts->other_fdt, &uts->other_fdt_size);
+#endif
+	return ret;
+}
 
 #endif /* __TEST_TEST_H */
-- 
2.37.2.789.g6183377224-goog



More information about the U-Boot mailing list