diff mbox series

[02/11] bootm: Add tests for fixup_silent_linux()

Message ID 20201019135602.3943835-3-sjg@chromium.org
State Superseded
Delegated to: Tom Rini
Headers show
Series bootm: Support substitions in bootargs and add tests | expand

Commit Message

Simon Glass Oct. 19, 2020, 1:55 p.m. UTC
This function currently has no tests. Export it so that we can implement
a simple test on sandbox. Use IS_ENABLED() to remove the unused code,
instead #ifdef.

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

 arch/Kconfig          |  1 +
 common/bootm.c        | 14 +++++-----
 include/bootm.h       |  3 +++
 include/test/suites.h |  1 +
 test/Makefile         |  1 +
 test/bootm.c          | 59 +++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c         |  1 +
 7 files changed, 73 insertions(+), 7 deletions(-)
 create mode 100644 test/bootm.c
diff mbox series

Patch

diff --git a/arch/Kconfig b/arch/Kconfig
index 683e3843190..6caf2338bcf 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -140,6 +140,7 @@  config SANDBOX
 	imply ACPI_PMC_SANDBOX
 	imply CMD_PMC
 	imply CMD_CLONE
+	imply SILENT_CONSOLE
 
 config SH
 	bool "SuperH architecture"
diff --git a/common/bootm.c b/common/bootm.c
index b3377490b3e..8e1e5337036 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -463,18 +463,21 @@  ulong bootm_disable_interrupts(void)
 	return iflag;
 }
 
-#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
-
 #define CONSOLE_ARG     "console="
 #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
 
-static void fixup_silent_linux(void)
+void fixup_silent_linux(void)
 {
 	char *buf;
 	const char *env_val;
-	char *cmdline = env_get("bootargs");
+	char *cmdline;
 	int want_silent;
 
+	if (!IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
+	    !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY))
+		return;
+	cmdline = env_get("bootargs");
+
 	/*
 	 * Only fix cmdline when requested. The environment variable can be:
 	 *
@@ -521,7 +524,6 @@  static void fixup_silent_linux(void)
 	debug("after silent fix-up: %s\n", env_val);
 	free(buf);
 }
-#endif /* CONFIG_SILENT_CONSOLE */
 
 /**
  * Execute selected states of the bootm command.
@@ -625,10 +627,8 @@  int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
 	if (!ret && (states & BOOTM_STATE_OS_BD_T))
 		ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
 	if (!ret && (states & BOOTM_STATE_OS_PREP)) {
-#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
 		if (images->os.os == IH_OS_LINUX)
 			fixup_silent_linux();
-#endif
 		ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
 	}
 
diff --git a/include/bootm.h b/include/bootm.h
index a812a6bf24f..6d675e64559 100644
--- a/include/bootm.h
+++ b/include/bootm.h
@@ -85,4 +85,7 @@  void arch_preboot_os(void);
  */
 void board_preboot_os(void);
 
+/* Adjust the 'bootargs' to ensure that Linux boots silently, if required */
+void fixup_silent_linux(void);
+
 #endif
diff --git a/include/test/suites.h b/include/test/suites.h
index ab7b3bd9cad..c1d8fa9a650 100644
--- a/include/test/suites.h
+++ b/include/test/suites.h
@@ -26,6 +26,7 @@  int cmd_ut_category(const char *name, const char *prefix,
 		    struct unit_test *tests, int n_ents,
 		    int argc, char *const argv[]);
 
+int do_ut_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc,
 		   char *const argv[]);
 int do_ut_compression(struct cmd_tbl *cmdtp, int flag, int argc,
diff --git a/test/Makefile b/test/Makefile
index 7c4039964e1..099f855aefe 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -3,6 +3,7 @@ 
 # (C) Copyright 2012 The Chromium Authors
 
 obj-$(CONFIG_SANDBOX) += bloblist.o
+obj-$(CONFIG_SANDBOX) += bootm.o
 obj-$(CONFIG_CMDLINE) += cmd/
 obj-$(CONFIG_UNIT_TEST) += cmd_ut.o
 obj-$(CONFIG_UNIT_TEST) += ut.o
diff --git a/test/bootm.c b/test/bootm.c
new file mode 100644
index 00000000000..59d16cb3df6
--- /dev/null
+++ b/test/bootm.c
@@ -0,0 +1,59 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for bootm routines
+ *
+ * Copyright 2020 Google LLC
+ */
+
+#include <common.h>
+#include <bootm.h>
+#include <test/suites.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define BOOTM_TEST(_name, _flags)	UNIT_TEST(_name, _flags, bootm_test)
+
+#define CONSOLE_STR	"console=/dev/ttyS0"
+
+/* Test silent processing in the bootargs variable */
+static int bootm_test_silent_var(struct unit_test_state *uts)
+{
+	/* 'silent_linux' not set should do nothing */
+	env_set("silent_linux", NULL);
+	env_set("bootargs", CONSOLE_STR);
+	fixup_silent_linux();
+	ut_asserteq_str(CONSOLE_STR, env_get("bootargs"));
+
+	env_set("bootargs", NULL);
+	fixup_silent_linux();
+	ut_assertnull(env_get("bootargs"));
+
+	ut_assertok(env_set("silent_linux", "no"));
+	env_set("bootargs", CONSOLE_STR);
+	fixup_silent_linux();
+	ut_asserteq_str(CONSOLE_STR, env_get("bootargs"));
+
+	ut_assertok(env_set("silent_linux", "yes"));
+	env_set("bootargs", CONSOLE_STR);
+	fixup_silent_linux();
+	ut_asserteq_str("console=", env_get("bootargs"));
+
+	/* Empty buffer should still add the string */
+	env_set("bootargs", NULL);
+	fixup_silent_linux();
+	ut_asserteq_str("console=", env_get("bootargs"));
+
+	return 0;
+}
+BOOTM_TEST(bootm_test_silent_var, 0);
+
+int do_ut_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct unit_test *tests = ll_entry_start(struct unit_test, bootm_test);
+	const int n_ents = ll_entry_count(struct unit_test, bootm_test);
+
+	return cmd_ut_category("bootm", "bootm_test_", tests, n_ents,
+			       argc, argv);
+}
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index 8f0bc688a22..8d5c28b4319 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -86,6 +86,7 @@  static struct cmd_tbl cmd_ut_sub[] = {
 			 "", ""),
 	U_BOOT_CMD_MKENT(bloblist, CONFIG_SYS_MAXARGS, 1, do_ut_bloblist,
 			 "", ""),
+	U_BOOT_CMD_MKENT(bootm, CONFIG_SYS_MAXARGS, 1, do_ut_bootm, "", ""),
 	U_BOOT_CMD_MKENT(str, CONFIG_SYS_MAXARGS, 1, do_ut_str,
 			 "", ""),
 #endif