Skip to content

Commit dc0d17c

Browse files
samueldrtrini
authored andcommitted
cmd: Add pause command
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 <[email protected]> Cc: Simon Glass <[email protected]>
1 parent d882607 commit dc0d17c

File tree

9 files changed

+143
-0
lines changed

9 files changed

+143
-0
lines changed

cmd/Kconfig

+6
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,12 @@ config CMD_GETTIME
19711971
milliseconds. See also the 'bootstage' command which provides more
19721972
flexibility for boot timing.
19731973

1974+
config CMD_PAUSE
1975+
bool "pause command"
1976+
help
1977+
Delay execution waiting for any user input.
1978+
Useful to allow the user to read a failure log.
1979+
19741980
config CMD_RNG
19751981
bool "rng command"
19761982
depends on DM_RNG

cmd/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ obj-$(CONFIG_CMD_MFSL) += mfsl.o
102102
obj-$(CONFIG_CMD_MII) += mii.o
103103
obj-$(CONFIG_CMD_MISC) += misc.o
104104
obj-$(CONFIG_CMD_MDIO) += mdio.o
105+
obj-$(CONFIG_CMD_PAUSE) += pause.o
105106
obj-$(CONFIG_CMD_SLEEP) += sleep.o
106107
obj-$(CONFIG_CMD_MMC) += mmc.o
107108
obj-$(CONFIG_CMD_OPTEE_RPMB) += optee_rpmb.o

cmd/pause.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* (C) Copyright 2021
4+
* Samuel Dionne-Riel <[email protected]>
5+
*/
6+
7+
#include <command.h>
8+
#include <stdio.h>
9+
10+
static int do_pause(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
11+
{
12+
char *message = "Press any key to continue...";
13+
14+
if (argc == 2)
15+
message = argv[1];
16+
17+
/* No newline, so it sticks to the bottom of the screen */
18+
printf("%s", message);
19+
20+
/* Wait on "any" key... */
21+
(void) getchar();
22+
23+
/* Since there was no newline, we need it now */
24+
printf("\n");
25+
26+
return CMD_RET_SUCCESS;
27+
}
28+
29+
U_BOOT_CMD(pause, 2, 1, do_pause,
30+
"delay until user input",
31+
"[prompt] - Wait until users presses any key. [prompt] can be used to customize the message.\n"
32+
);

configs/sandbox64_defconfig

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ CONFIG_CMD_BMP=y
6767
CONFIG_CMD_EFIDEBUG=y
6868
CONFIG_CMD_RTC=y
6969
CONFIG_CMD_TIME=y
70+
CONFIG_CMD_PAUSE=y
7071
CONFIG_CMD_TIMER=y
7172
CONFIG_CMD_SOUND=y
7273
CONFIG_CMD_QFW=y

configs/sandbox_defconfig

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ CONFIG_CMD_BOOTCOUNT=y
9696
CONFIG_CMD_EFIDEBUG=y
9797
CONFIG_CMD_RTC=y
9898
CONFIG_CMD_TIME=y
99+
CONFIG_CMD_PAUSE=y
99100
CONFIG_CMD_TIMER=y
100101
CONFIG_CMD_SOUND=y
101102
CONFIG_CMD_QFW=y

doc/usage/cmd/pause.rst

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
.. SPDX-License-Identifier: GPL-2.0-or-later:
2+
3+
pause command
4+
=============
5+
6+
Synopsis
7+
--------
8+
9+
::
10+
11+
pause [prompt]
12+
13+
14+
Description
15+
-----------
16+
17+
The pause command delays execution waiting for any user input.
18+
19+
It can accept a single parameter to change the prompt message.
20+
21+
Examples
22+
--------
23+
24+
Using with the default prompt:
25+
26+
::
27+
28+
=> pause
29+
Press any key to continue...
30+
31+
32+
Using with a custom prompt:
33+
34+
::
35+
36+
=> pause 'Prompt for pause...'
37+
Prompt for pause...
38+
39+
Note that complex prompts require proper quoting:
40+
41+
::
42+
43+
=> pause Prompt for pause...
44+
pause - delay until user input
45+
46+
Usage:
47+
pause [prompt] - Wait until users presses any key. [prompt] can be used to customize the message.
48+
49+
Return value
50+
------------
51+
52+
The return value $? is always set to 0 (true), unless invoked in an invalid
53+
manner.

doc/usage/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Shell commands
5252
cmd/mbr
5353
cmd/md
5454
cmd/mmc
55+
cmd/pause
5556
cmd/pinmux
5657
cmd/printenv
5758
cmd/pstore

test/cmd/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
ifdef CONFIG_HUSH_PARSER
66
obj-$(CONFIG_CONSOLE_RECORD) += test_echo.o
77
endif
8+
ifdef CONFIG_CONSOLE_RECORD
9+
obj-$(CONFIG_CMD_PAUSE) += test_pause.o
10+
endif
811
obj-y += mem.o
912
obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
1013
obj-$(CONFIG_CMD_FDT) += fdt.o

test/cmd/test_pause.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Tests for pause command
4+
*
5+
* Copyright 2022, Samuel Dionne-Riel <[email protected]>
6+
*/
7+
8+
#include <common.h>
9+
#include <asm/global_data.h>
10+
#include <test/lib.h>
11+
#include <test/ut.h>
12+
13+
DECLARE_GLOBAL_DATA_PTR;
14+
15+
static int lib_test_hush_pause(struct unit_test_state *uts)
16+
{
17+
/* Test default message */
18+
console_record_reset_enable();
19+
/* Cook a newline when the command is expected to pause */
20+
console_in_puts("\n");
21+
ut_assertok(run_command("pause", 0));
22+
console_record_readline(uts->actual_str, sizeof(uts->actual_str));
23+
ut_asserteq_str("Press any key to continue...", uts->actual_str);
24+
ut_assertok(ut_check_console_end(uts));
25+
26+
/* Test provided message */
27+
console_record_reset_enable();
28+
/* Cook a newline when the command is expected to pause */
29+
console_in_puts("\n");
30+
ut_assertok(run_command("pause 'Prompt for pause...'", 0));
31+
console_record_readline(uts->actual_str, sizeof(uts->actual_str));
32+
ut_asserteq_str("Prompt for pause...", uts->actual_str);
33+
ut_assertok(ut_check_console_end(uts));
34+
35+
/* Test providing more than one params */
36+
console_record_reset_enable();
37+
/* No newline cooked here since the command is expected to fail */
38+
ut_asserteq(1, run_command("pause a b", 0));
39+
console_record_readline(uts->actual_str, sizeof(uts->actual_str));
40+
ut_asserteq_str("pause - delay until user input", uts->actual_str);
41+
ut_asserteq(1, ut_check_console_end(uts));
42+
43+
return 0;
44+
}
45+
LIB_TEST(lib_test_hush_pause, 0);

0 commit comments

Comments
 (0)