[PATCH 2/3] fdt: Start a test for the fdt command

Simon Glass sjg at chromium.org
Wed Jul 13 14:06:59 CEST 2022


Add a basic test of the 'fdt addr' command, to kick things off.

This includes a new convenience function to run a command from a printf()
string.

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

 common/cli.c          |  15 +++++
 include/command.h     |  10 +++
 include/test/suites.h |   1 +
 test/cmd/Makefile     |   1 +
 test/cmd/fdt.c        | 142 ++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c         |   6 ++
 6 files changed, 175 insertions(+)
 create mode 100644 test/cmd/fdt.c

diff --git a/common/cli.c b/common/cli.c
index a7e3d84b68f..a47d6a3f2b4 100644
--- a/common/cli.c
+++ b/common/cli.c
@@ -126,6 +126,21 @@ int run_command_list(const char *cmd, int len, int flag)
 	return rcode;
 }
 
+int run_commandf(const char *fmt, ...)
+{
+	va_list args;
+	char cmd[128];
+	int i, ret;
+
+	va_start(args, fmt);
+	i = vsnprintf(cmd, sizeof(cmd), fmt, args);
+	va_end(args);
+
+	ret = run_command(cmd, 0);
+
+	return ret;
+}
+
 /****************************************************************************/
 
 #if defined(CONFIG_CMD_RUN)
diff --git a/include/command.h b/include/command.h
index 0cf12fde396..44c91f655d4 100644
--- a/include/command.h
+++ b/include/command.h
@@ -257,6 +257,16 @@ int board_run_command(const char *cmdline);
 int run_command(const char *cmd, int flag);
 int run_command_repeatable(const char *cmd, int flag);
 
+/**
+ * run_commandf() - Run a command created by a format string
+ *
+ * The command cannot be larger than 127 characters
+ *
+ * @fmt: printf() format string
+ * @...: Arguments to use (flag is always 0)
+ */
+int run_commandf(const char *fmt, ...);
+
 /**
  * Run a list of commands separated by ; or even \0
  *
diff --git a/include/test/suites.h b/include/test/suites.h
index ddb8827fdb1..44025ccecd6 100644
--- a/include/test/suites.h
+++ b/include/test/suites.h
@@ -38,6 +38,7 @@ int do_ut_compression(struct cmd_tbl *cmdtp, int flag, int argc,
 		      char *const argv[]);
 int do_ut_dm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
+int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
diff --git a/test/cmd/Makefile b/test/cmd/Makefile
index 4b2d7df0d2e..c331757425e 100644
--- a/test/cmd/Makefile
+++ b/test/cmd/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_CONSOLE_RECORD) += test_echo.o
 endif
 obj-y += mem.o
 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
+obj-$(CONFIG_CMD_FDT) += fdt.o
 obj-$(CONFIG_CMD_LOADM) += loadm.o
 obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o
 obj-$(CONFIG_CMD_PINMUX) += pinmux.o
diff --git a/test/cmd/fdt.c b/test/cmd/fdt.c
new file mode 100644
index 00000000000..100a7ef5ebf
--- /dev/null
+++ b/test/cmd/fdt.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for fdt command
+ *
+ * Copyright 2022 Google LLCmap_to_sysmem(fdt));
+ */
+
+#include <common.h>
+#include <console.h>
+#include <fdt_support.h>
+#include <mapmem.h>
+#include <asm/global_data.h>
+#include <linux/libfdt.h>
+#include <test/suites.h>
+#include <test/ut.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Declare a new fdt test */
+#define FDT_TEST(_name, _flags)	UNIT_TEST(_name, _flags, fdt_test)
+
+/**
+ * make_test_fdt() - Create an FDT with just a root node
+ *
+ * The size is set to the minimum needed
+ *
+ * @uts: Test state
+ * @fdt: Place to write FDT
+ * @size: Maximum size of space for fdt
+ */
+static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size)
+{
+	ut_assertok(fdt_create(fdt, size));
+	ut_assertok(fdt_finish_reservemap(fdt));
+	ut_assert(fdt_begin_node(fdt, "") >= 0);
+	ut_assertok(fdt_end_node(fdt));
+	ut_assertok(fdt_finish(fdt));
+
+	return 0;
+}
+
+/* Test 'fdt addr' getting/setting address */
+static int fdt_test_addr(struct unit_test_state *uts)
+{
+	const void *fdt_blob, *new_fdt;
+	char fdt[256];
+	ulong addr;
+	int ret;
+
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_command("fdt addr -c", 0));
+	ut_assert_nextline("Control fdt: %08lx",
+			   (ulong)map_to_sysmem(gd->fdt_blob));
+	ut_assertok(ut_check_console_end(uts));
+
+	/* The working fdt is not set, so this should fail */
+	set_working_fdt_addr(0);
+	ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0));
+	ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Set up a working FDT and try again */
+	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
+	addr = map_to_sysmem(fdt);
+	set_working_fdt_addr(addr);
+	ut_assertok(run_command("fdt addr", 0));
+	ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt));
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Set the working FDT */
+	set_working_fdt_addr(0);
+	ut_assertok(run_commandf("fdt addr %08x", addr));
+	ut_asserteq(addr, map_to_sysmem(working_fdt));
+	ut_assertok(ut_check_console_end(uts));
+	set_working_fdt_addr(0);
+
+	/* Set the working FDT */
+	fdt_blob = gd->fdt_blob;
+	gd->fdt_blob = NULL;
+	ret = run_commandf("fdt addr -c %08x", addr);
+	new_fdt = gd->fdt_blob;
+	gd->fdt_blob = fdt_blob;
+	ut_assertok(ret);
+	ut_asserteq(addr, map_to_sysmem(new_fdt));
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test setting an invalid FDT */
+	fdt[0] = 123;
+	ut_asserteq(1, run_commandf("fdt addr %08x", addr));
+	ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Test detecting an invalid FDT */
+	fdt[0] = 123;
+	set_working_fdt_addr(addr);
+	ut_asserteq(1, run_commandf("fdt addr"));
+	ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
+	ut_assertok(ut_check_console_end(uts));
+
+	return 0;
+}
+FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC);
+
+/* Test 'fdt addr' resizing an fdt */
+static int fdt_test_resize(struct unit_test_state *uts)
+{
+	char fdt[256];
+	const int newsize = sizeof(fdt) / 2;
+	ulong addr;
+
+	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
+	addr = map_to_sysmem(fdt);
+	set_working_fdt_addr(addr);
+
+	/* Test setting and resizing the working FDT to a larger size */
+	ut_assertok(console_record_reset_enable());
+	ut_assertok(run_commandf("fdt addr %08x %x", addr, newsize));
+	ut_assertok(ut_check_console_end(uts));
+
+	/* Try shrinking it */
+	ut_assertok(run_commandf("fdt addr %08x %x", addr, sizeof(fdt) / 4));
+	ut_assert_nextline("New length %d < existing length %d, ignoring",
+			   (int)sizeof(fdt) / 4, newsize);
+	ut_assertok(ut_check_console_end(uts));
+
+	/* ...quietly */
+	ut_assertok(run_commandf("fdt addr -q %08x %x", addr, sizeof(fdt) / 4));
+	ut_assertok(ut_check_console_end(uts));
+
+	/* We cannot easily provoke errors in fdt_open_into(), so ignore that */
+
+	return 0;
+}
+FDT_TEST(fdt_test_resize, UT_TESTF_CONSOLE_REC);
+
+int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
+	const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test);
+
+	return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv);
+}
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index d70b72678ae..3789c6b784c 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -39,6 +39,9 @@ static struct cmd_tbl cmd_ut_sub[] = {
 #if defined(CONFIG_UT_ENV)
 	U_BOOT_CMD_MKENT(env, CONFIG_SYS_MAXARGS, 1, do_ut_env, "", ""),
 #endif
+#ifdef CONFIG_CMD_FDT
+	U_BOOT_CMD_MKENT(fdt, CONFIG_SYS_MAXARGS, 1, do_ut_fdt, "", ""),
+#endif
 #ifdef CONFIG_UT_OPTEE
 	U_BOOT_CMD_MKENT(optee, CONFIG_SYS_MAXARGS, 1, do_ut_optee, "", ""),
 #endif
@@ -131,6 +134,9 @@ static char ut_help_text[] =
 #ifdef CONFIG_UT_ENV
 	"ut env [test-name]\n"
 #endif
+#ifdef CONFIG_CMD_FDT
+	"ut fdt [test-name] - test of the fdt command\n"
+#endif
 #ifdef CONFIG_UT_LIB
 	"ut lib [test-name] - test library functions\n"
 #endif
-- 
2.37.0.144.g8ac04bfd2-goog



More information about the U-Boot mailing list