aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass2022-07-13 06:06:59 -0600
committerSimon Glass2022-07-26 02:30:56 -0600
commit747244840b647991844556284aacdb3857440861 (patch)
treed6f43333ce2926a4acc8b36316eed21ba42938b4
parent281996110c1f42b5476e43040798cb38169b6119 (diff)
fdt: Start a test for the fdt command
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@chromium.org>
-rw-r--r--common/cli.c15
-rw-r--r--include/command.h10
-rw-r--r--include/test/suites.h1
-rw-r--r--test/cmd/Makefile1
-rw-r--r--test/cmd/fdt.c142
-rw-r--r--test/cmd_ut.c6
6 files changed, 175 insertions, 0 deletions
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
@@ -258,6 +258,16 @@ 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
*
* Note that if 'len' is not -1, then the command does not need to be nul
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