cmd: Add pause command
authorSamuel Dionne-Riel <samuel@dionne-riel.com>
Thu, 18 Aug 2022 19:44:04 +0000 (15:44 -0400)
committerTom Rini <trini@konsulko.com>
Fri, 2 Sep 2022 17:40:42 +0000 (13:40 -0400)
This command is being introduced with the goal of allowing user-friendly
"generic use case" U-Boot builds to pause until user input under some
situations.

The main use case would be when a boot failure happens, to pause until
the user has had time to acknowledge the current state.

Tested using:

    make && ./u-boot -v -T -c 'ut lib lib_test_hush_pause'

Signed-off-by: Samuel Dionne-Riel <samuel@dionne-riel.com>
Cc: Simon Glass <sjg@chromium.org>
cmd/Kconfig
cmd/Makefile
cmd/pause.c [new file with mode: 0644]
configs/sandbox64_defconfig
configs/sandbox_defconfig
doc/usage/cmd/pause.rst [new file with mode: 0644]
doc/usage/index.rst
test/cmd/Makefile
test/cmd/test_pause.c [new file with mode: 0644]

index 211ebe9..67c7d25 100644 (file)
@@ -1971,6 +1971,12 @@ config CMD_GETTIME
          milliseconds. See also the 'bootstage' command which provides more
          flexibility for boot timing.
 
+config CMD_PAUSE
+       bool "pause command"
+       help
+         Delay execution waiting for any user input.
+         Useful to allow the user to read a failure log.
+
 config CMD_RNG
        bool "rng command"
        depends on DM_RNG
index 6e87522..7abe1d3 100644 (file)
@@ -102,6 +102,7 @@ obj-$(CONFIG_CMD_MFSL) += mfsl.o
 obj-$(CONFIG_CMD_MII) += mii.o
 obj-$(CONFIG_CMD_MISC) += misc.o
 obj-$(CONFIG_CMD_MDIO) += mdio.o
+obj-$(CONFIG_CMD_PAUSE) += pause.o
 obj-$(CONFIG_CMD_SLEEP) += sleep.o
 obj-$(CONFIG_CMD_MMC) += mmc.o
 obj-$(CONFIG_CMD_OPTEE_RPMB) += optee_rpmb.o
diff --git a/cmd/pause.c b/cmd/pause.c
new file mode 100644 (file)
index 0000000..c97833c
--- /dev/null
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2021
+ * Samuel Dionne-Riel <samuel@dionne-riel.com>
+ */
+
+#include <command.h>
+#include <stdio.h>
+
+static int do_pause(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+       char *message = "Press any key to continue...";
+
+       if (argc == 2)
+               message = argv[1];
+
+       /* No newline, so it sticks to the bottom of the screen */
+       printf("%s", message);
+
+       /* Wait on "any" key... */
+       (void) getchar();
+
+       /* Since there was no newline, we need it now */
+       printf("\n");
+
+       return CMD_RET_SUCCESS;
+}
+
+U_BOOT_CMD(pause, 2, 1, do_pause,
+       "delay until user input",
+       "[prompt] - Wait until users presses any key. [prompt] can be used to customize the message.\n"
+);
index 4599db7..9a1b6e4 100644 (file)
@@ -67,6 +67,7 @@ CONFIG_CMD_BMP=y
 CONFIG_CMD_EFIDEBUG=y
 CONFIG_CMD_RTC=y
 CONFIG_CMD_TIME=y
+CONFIG_CMD_PAUSE=y
 CONFIG_CMD_TIMER=y
 CONFIG_CMD_SOUND=y
 CONFIG_CMD_QFW=y
index 4a0c51c..e6a1718 100644 (file)
@@ -96,6 +96,7 @@ CONFIG_CMD_BOOTCOUNT=y
 CONFIG_CMD_EFIDEBUG=y
 CONFIG_CMD_RTC=y
 CONFIG_CMD_TIME=y
+CONFIG_CMD_PAUSE=y
 CONFIG_CMD_TIMER=y
 CONFIG_CMD_SOUND=y
 CONFIG_CMD_QFW=y
diff --git a/doc/usage/cmd/pause.rst b/doc/usage/cmd/pause.rst
new file mode 100644 (file)
index 0000000..c79e399
--- /dev/null
@@ -0,0 +1,53 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later:
+
+pause command
+=============
+
+Synopsis
+--------
+
+::
+
+    pause [prompt]
+
+
+Description
+-----------
+
+The pause command delays execution waiting for any user input.
+
+It can accept a single parameter to change the prompt message.
+
+Examples
+--------
+
+Using with the default prompt:
+
+::
+
+    => pause
+    Press any key to continue...
+
+
+Using with a custom prompt:
+
+::
+
+    => pause 'Prompt for pause...'
+    Prompt for pause...
+
+Note that complex prompts require proper quoting:
+
+::
+
+    => pause Prompt for pause...
+    pause - delay until user input
+    
+    Usage:
+    pause [prompt] - Wait until users presses any key. [prompt] can be used to customize the message.
+
+Return value
+------------
+
+The return value $? is always set to 0 (true), unless invoked in an invalid
+manner.
index 28f9683..5170fa8 100644 (file)
@@ -52,6 +52,7 @@ Shell commands
    cmd/mbr
    cmd/md
    cmd/mmc
+   cmd/pause
    cmd/pinmux
    cmd/printenv
    cmd/pstore
index c331757..1bb02d9 100644 (file)
@@ -5,6 +5,9 @@
 ifdef CONFIG_HUSH_PARSER
 obj-$(CONFIG_CONSOLE_RECORD) += test_echo.o
 endif
+ifdef CONFIG_CONSOLE_RECORD
+obj-$(CONFIG_CMD_PAUSE) += test_pause.o
+endif
 obj-y += mem.o
 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
 obj-$(CONFIG_CMD_FDT) += fdt.o
diff --git a/test/cmd/test_pause.c b/test/cmd/test_pause.c
new file mode 100644 (file)
index 0000000..2b85cce
--- /dev/null
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for pause command
+ *
+ * Copyright 2022, Samuel Dionne-Riel <samuel@dionne-riel.com>
+ */
+
+#include <common.h>
+#include <asm/global_data.h>
+#include <test/lib.h>
+#include <test/ut.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int lib_test_hush_pause(struct unit_test_state *uts)
+{
+       /* Test default message */
+       console_record_reset_enable();
+       /* Cook a newline when the command is expected to pause */
+       console_in_puts("\n");
+       ut_assertok(run_command("pause", 0));
+       console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+       ut_asserteq_str("Press any key to continue...", uts->actual_str);
+       ut_assertok(ut_check_console_end(uts));
+
+       /* Test provided message */
+       console_record_reset_enable();
+       /* Cook a newline when the command is expected to pause */
+       console_in_puts("\n");
+       ut_assertok(run_command("pause 'Prompt for pause...'", 0));
+       console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+       ut_asserteq_str("Prompt for pause...", uts->actual_str);
+       ut_assertok(ut_check_console_end(uts));
+
+       /* Test providing more than one params */
+       console_record_reset_enable();
+       /* No newline cooked here since the command is expected to fail */
+       ut_asserteq(1, run_command("pause a b", 0));
+       console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+       ut_asserteq_str("pause - delay until user input", uts->actual_str);
+       ut_asserteq(1, ut_check_console_end(uts));
+
+       return 0;
+}
+LIB_TEST(lib_test_hush_pause, 0);