[PATCH v4 2/2] cli: flush stdin before enabling cli

Gregor Herburger gregor.herburger at linutronix.de
Mon Apr 13 17:24:54 CEST 2026


Currently there is no possibility to flush stdin after autocommands are
executed. If in the bootcmd the stdin is changed, e.g. from nulldev to
serial, it could happen that junk characters sit in the fifo and appear
on the cli.

Add a option to clear stdin before starting the CLI.

Signed-off-by: Gregor Herburger <gregor.herburger at linutronix.de>
Reviewed-by: Simon Glass <sjg at chromium.org>

---

Changes in v4:
- Collect tags

Changes in v3:
- Update helptext

Changes in v2:
- add a common console_flush_stdin to console.c
- replace all caller of while(getchar()) with function call

 cmd/Kconfig  | 8 ++++++++
 common/cli.c | 4 ++++
 2 files changed, 12 insertions(+)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 7bbeaad04ff..33a7952c9b8 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -47,6 +47,14 @@ config HUSH_SELECTABLE
 	default y if HUSH_OLD_PARSER && HUSH_MODERN_PARSER
 endmenu
 
+config CMDLINE_FLUSH_STDIN
+	bool "Enable flushing input before starting cli"
+	help
+	  When this option is enabled the stdin buffer will be flushed before
+	  starting the CLI. This is useful when stdin is changed during boot
+	  (e.g. from nulldev to serial) and junk characters may be pending
+	  in the buffer.
+
 config CMDLINE_EDITING
 	bool "Enable command line editing"
 	default y
diff --git a/common/cli.c b/common/cli.c
index 4694a35cd0e..bcc7264d51a 100644
--- a/common/cli.c
+++ b/common/cli.c
@@ -295,6 +295,10 @@ err:
 void cli_loop(void)
 {
 	bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
+
+	if (IS_ENABLED(CONFIG_CMDLINE_FLUSH_STDIN))
+		console_flush_stdin();
+
 #if CONFIG_IS_ENABLED(HUSH_PARSER)
 	if (gd->flags & GD_FLG_HUSH_MODERN_PARSER)
 		parse_and_run_file();
-- 
2.47.3

base-commit: e2fa3e570f83ab0f9ce667ddaec9dc738bcf05b9
branch: cmd-flush-stdin4


More information about the U-Boot mailing list