* [PATCH v2 1/2] console: add console_flush_stdin()
@ 2026-04-01 12:40 Gregor Herburger
2026-04-01 12:40 ` [PATCH v2 2/2] cli: flush stdin before enabling cli Gregor Herburger
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Gregor Herburger @ 2026-04-01 12:40 UTC (permalink / raw)
To: u-boot, Quentin Schulz
Cc: Gregor Herburger, Alexander Sverdlin, Andre Przywara,
Casey Connolly, David Zang, Heinrich Schuchardt, Ilias Apalodimas,
Sam Protsenko, Simon Glass, Sughosh Ganu, Tom Rini
Add a common helper console_flush_stdin() to drain all pending
characters from stdin. This consolidates the open-coded
while (tstc()) getchar() pattern that appeared in multiple places
across the tree.
Signed-off-by: Gregor Herburger <gregor.herburger@linutronix.de>
---
(no changes since v1)
cmd/conitrace.c | 4 ++--
cmd/eficonfig.c | 6 +++---
cmd/eficonfig_sbkey.c | 4 ++--
common/autoboot.c | 11 +----------
common/console.c | 12 ++++++++++--
include/console.h | 5 +++++
lib/efi_loader/efi_console.c | 7 +++----
7 files changed, 26 insertions(+), 23 deletions(-)
diff --git a/cmd/conitrace.c b/cmd/conitrace.c
index 6cc113328eb..aef094e03df 100644
--- a/cmd/conitrace.c
+++ b/cmd/conitrace.c
@@ -6,6 +6,7 @@
* Copyright (c) 2018, Heinrich Schuchardt <xypron.glpk@gmx.de>
*/
#include <command.h>
+#include <console.h>
#include <linux/delay.h>
static int do_conitrace(struct cmd_tbl *cmdtp, int flag, int argc,
@@ -17,8 +18,7 @@ static int do_conitrace(struct cmd_tbl *cmdtp, int flag, int argc,
printf("To terminate type 'x'\n");
/* Empty input buffer */
- while (tstc())
- getchar();
+ console_flush_stdin();
for (;;) {
int c = getchar();
diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
index d8d946c87ac..60c39bc3a99 100644
--- a/cmd/eficonfig.c
+++ b/cmd/eficonfig.c
@@ -6,8 +6,9 @@
*/
#include <ansi.h>
-#include <cli.h>
#include <charset.h>
+#include <cli.h>
+#include <console.h>
#include <efi_device_path.h>
#include <efi_loader.h>
#include <efi_load_initrd.h>
@@ -167,8 +168,7 @@ static void eficonfig_menu_adjust(struct efimenu *efi_menu, bool add)
void eficonfig_print_msg(char *msg)
{
/* Flush input */
- while (tstc())
- getchar();
+ console_flush_stdin();
printf(ANSI_CURSOR_HIDE
ANSI_CLEAR_CONSOLE
diff --git a/cmd/eficonfig_sbkey.c b/cmd/eficonfig_sbkey.c
index b3325a540f9..a6c5416d3a5 100644
--- a/cmd/eficonfig_sbkey.c
+++ b/cmd/eficonfig_sbkey.c
@@ -7,6 +7,7 @@
#include <ansi.h>
#include <charset.h>
+#include <console.h>
#include <hexdump.h>
#include <log.h>
#include <malloc.h>
@@ -288,8 +289,7 @@ static efi_status_t eficonfig_process_show_siglist(void *data)
}
}
- while (tstc())
- getchar();
+ console_flush_stdin();
printf("\n\n Press any key to continue");
getchar();
diff --git a/common/autoboot.c b/common/autoboot.c
index 1783ef92c94..4b80ddb5b28 100644
--- a/common/autoboot.c
+++ b/common/autoboot.c
@@ -315,15 +315,6 @@ static int passwd_abort_key(uint64_t etime)
return abort;
}
-/**
- * flush_stdin() - drops all pending characters from stdin
- */
-static void flush_stdin(void)
-{
- while (tstc())
- (void)getchar();
-}
-
/**
* fallback_to_sha256() - check whether we should fall back to sha256
* password checking
@@ -354,7 +345,7 @@ static int abortboot_key_sequence(int bootdelay)
uint64_t etime = endtick(bootdelay);
if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
- flush_stdin();
+ console_flush_stdin();
# ifdef CONFIG_AUTOBOOT_PROMPT
/*
* CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.
diff --git a/common/console.c b/common/console.c
index 48586fd2166..90b437fd1fc 100644
--- a/common/console.c
+++ b/common/console.c
@@ -643,6 +643,15 @@ int tstc(void)
return serial_tstc();
}
+/**
+ * console_flush_stdin() - drops all pending characters from stdin
+ */
+void console_flush_stdin(void)
+{
+ while (tstc())
+ (void)getchar();
+}
+
#define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
#define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
@@ -914,8 +923,7 @@ int confirm_yesno(void)
char str_input[5];
/* Flush input */
- while (tstc())
- getchar();
+ console_flush_stdin();
i = 0;
while (i < sizeof(str_input)) {
str_input[i] = getchar();
diff --git a/include/console.h b/include/console.h
index 8d0d7bb8a4c..01a04f28f31 100644
--- a/include/console.h
+++ b/include/console.h
@@ -202,6 +202,11 @@ int console_clear(void);
*/
int console_remove_by_name(const char *name);
+/**
+ * console_flush_stdin() - drops all pending characters from stdin
+ */
+void console_flush_stdin(void);
+
/*
* CONSOLE multiplexing.
*/
diff --git a/lib/efi_loader/efi_console.c b/lib/efi_loader/efi_console.c
index 068d1a0a7b7..f83415f2ed3 100644
--- a/lib/efi_loader/efi_console.c
+++ b/lib/efi_loader/efi_console.c
@@ -9,6 +9,7 @@
#include <ansi.h>
#include <charset.h>
+#include <console.h>
#include <efi_device_path.h>
#include <malloc.h>
#include <time.h>
@@ -299,8 +300,7 @@ static int query_console_serial(int *rows, int *cols)
int n[2];
/* Empty input buffer */
- while (tstc())
- getchar();
+ console_flush_stdin();
/*
* Not all terminals understand CSI [18t for querying the console size.
@@ -957,8 +957,7 @@ static void efi_cin_check(void)
*/
static void efi_cin_empty_buffer(void)
{
- while (tstc())
- getchar();
+ console_flush_stdin();
key_available = false;
}
--
2.47.3
base-commit: c704af3c8b0f37929bce8c2a4bba27d6e89919c7
branch: cmd-flush-stdin
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v2 2/2] cli: flush stdin before enabling cli
2026-04-01 12:40 [PATCH v2 1/2] console: add console_flush_stdin() Gregor Herburger
@ 2026-04-01 12:40 ` Gregor Herburger
2026-04-02 0:15 ` [v2,2/2] " Simon Glass
2026-04-01 13:23 ` [PATCH v2 1/2] console: add console_flush_stdin() Sverdlin, Alexander
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Gregor Herburger @ 2026-04-01 12:40 UTC (permalink / raw)
To: u-boot, Quentin Schulz
Cc: Gregor Herburger, Andrew Goodbody, Heiko Schocher,
Heinrich Schuchardt, Ilias Apalodimas, Jerome Forissier,
Kory Maincent (TI.com), Mattijs Korpershoek, Mikhail Kshevetskiy,
Tom Rini
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@linutronix.de>
---
Changes in v2:
- add a common console_flush_stdin to console.c
- replace all caller of while(getchar()) with function call
cmd/Kconfig | 6 ++++++
common/cli.c | 4 ++++
2 files changed, 10 insertions(+)
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 322ebe600c5..3dfba977544 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -47,6 +47,12 @@ 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.
+
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: c704af3c8b0f37929bce8c2a4bba27d6e89919c7
branch: cmd-flush-stdin
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [v2,2/2] cli: flush stdin before enabling cli
2026-04-01 12:40 ` [PATCH v2 2/2] cli: flush stdin before enabling cli Gregor Herburger
@ 2026-04-02 0:15 ` Simon Glass
2026-04-02 8:06 ` Gregor Herburger
0 siblings, 1 reply; 8+ messages in thread
From: Simon Glass @ 2026-04-02 0:15 UTC (permalink / raw)
To: gregor.herburger
Cc: u-boot, Quentin Schulz, Andrew Goodbody, Heiko Schocher,
Heinrich Schuchardt, Ilias Apalodimas, Jerome Forissier,
Kory Maincent (TI.com), Mattijs Korpershoek, Mikhail Kshevetskiy,
Tom Rini
Hi Gregor,
On 2026-04-01T12:40:44, Gregor Herburger <gregor.herburger@linutronix.de> wrote:
> cli: flush stdin before enabling cli
> cli: flush stdin before enabling cli
>
> 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@linutronix.de>
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> @@ -47,6 +47,12 @@ config HUSH_SELECTABLE
> +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.
Just to check, does this option also need 'depends on !SANDBOX' like
AUTOBOOT_FLUSH_STDIN does? That option notes that flushing stdin
breaks autoboot unit tests. I suspect the CLI entry path might not be
exercised by those tests, but it would be good to confirm.
Also, please can you expand the help text to mention the use case? For
example, 'This is useful when stdin is changed during boot (e.g. from
nulldev to serial) and junk characters may be pending in the buffer.'
Regards,
Simon
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [v2,2/2] cli: flush stdin before enabling cli
2026-04-02 0:15 ` [v2,2/2] " Simon Glass
@ 2026-04-02 8:06 ` Gregor Herburger
2026-04-03 13:22 ` Simon Glass
0 siblings, 1 reply; 8+ messages in thread
From: Gregor Herburger @ 2026-04-02 8:06 UTC (permalink / raw)
To: Simon Glass
Cc: u-boot, Quentin Schulz, Andrew Goodbody, Heiko Schocher,
Heinrich Schuchardt, Ilias Apalodimas, Jerome Forissier,
Kory Maincent (TI.com), Mattijs Korpershoek, Mikhail Kshevetskiy,
Tom Rini
Hi Simon,
> Just to check, does this option also need 'depends on !SANDBOX' like
> AUTOBOOT_FLUSH_STDIN does? That option notes that flushing stdin
> breaks autoboot unit tests. I suspect the CLI entry path might not be
> exercised by those tests, but it would be good to confirm.
I now tested a bit with the sandbox and I couldn't find any issues. I
ran some unit tests with/without this patch series and with
CMDLINE_FLUSH_STDIN enabled/disabled and couldn't find any issues. I
also ran `u-boot -c bdinfo` which seems to be executed after the
cli_init and therefore after the flush but I could't see any issues
here.
But this is the first time for me using the sandbox so I don't know what
I could test further. If anybody has an idea what tests could be run let
me know. Otherwise I will send a v3 with an updated help text.
Best regards
Gregor
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [v2,2/2] cli: flush stdin before enabling cli
2026-04-02 8:06 ` Gregor Herburger
@ 2026-04-03 13:22 ` Simon Glass
0 siblings, 0 replies; 8+ messages in thread
From: Simon Glass @ 2026-04-03 13:22 UTC (permalink / raw)
To: Gregor Herburger
Cc: u-boot, Quentin Schulz, Andrew Goodbody, Heiko Schocher,
Heinrich Schuchardt, Ilias Apalodimas, Jerome Forissier,
Kory Maincent (TI.com), Mattijs Korpershoek, Mikhail Kshevetskiy,
Tom Rini
Hi Gregor,
On Thu, 2 Apr 2026 at 02:06, Gregor Herburger
<gregor.herburger@linutronix.de> wrote:
>
> Hi Simon,
> > Just to check, does this option also need 'depends on !SANDBOX' like
> > AUTOBOOT_FLUSH_STDIN does? That option notes that flushing stdin
> > breaks autoboot unit tests. I suspect the CLI entry path might not be
> > exercised by those tests, but it would be good to confirm.
>
> I now tested a bit with the sandbox and I couldn't find any issues. I
> ran some unit tests with/without this patch series and with
> CMDLINE_FLUSH_STDIN enabled/disabled and couldn't find any issues. I
> also ran `u-boot -c bdinfo` which seems to be executed after the
> cli_init and therefore after the flush but I could't see any issues
> here.
>
> But this is the first time for me using the sandbox so I don't know what
> I could test further. If anybody has an idea what tests could be run let
> me know. Otherwise I will send a v3 with an updated help text.
OK that's good, thanks.
Regards,
Simon
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/2] console: add console_flush_stdin()
2026-04-01 12:40 [PATCH v2 1/2] console: add console_flush_stdin() Gregor Herburger
2026-04-01 12:40 ` [PATCH v2 2/2] cli: flush stdin before enabling cli Gregor Herburger
@ 2026-04-01 13:23 ` Sverdlin, Alexander
2026-04-01 13:45 ` Quentin Schulz
2026-04-02 0:14 ` [v2,1/2] " Simon Glass
3 siblings, 0 replies; 8+ messages in thread
From: Sverdlin, Alexander @ 2026-04-01 13:23 UTC (permalink / raw)
To: u-boot@lists.denx.de, gregor.herburger@linutronix.de
Hi Gregor!
On Wed, 2026-04-01 at 14:40 +0200, Gregor Herburger wrote:
> Add a common helper console_flush_stdin() to drain all pending
> characters from stdin. This consolidates the open-coded
> while (tstc()) getchar() pattern that appeared in multiple places
> across the tree.
>
> Signed-off-by: Gregor Herburger <gregor.herburger@linutronix.de>
Reviewed-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
> ---
>
> (no changes since v1)
>
> cmd/conitrace.c | 4 ++--
> cmd/eficonfig.c | 6 +++---
> cmd/eficonfig_sbkey.c | 4 ++--
> common/autoboot.c | 11 +----------
> common/console.c | 12 ++++++++++--
> include/console.h | 5 +++++
> lib/efi_loader/efi_console.c | 7 +++----
> 7 files changed, 26 insertions(+), 23 deletions(-)
--
Alexander Sverdlin
Siemens AG
www.siemens.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 1/2] console: add console_flush_stdin()
2026-04-01 12:40 [PATCH v2 1/2] console: add console_flush_stdin() Gregor Herburger
2026-04-01 12:40 ` [PATCH v2 2/2] cli: flush stdin before enabling cli Gregor Herburger
2026-04-01 13:23 ` [PATCH v2 1/2] console: add console_flush_stdin() Sverdlin, Alexander
@ 2026-04-01 13:45 ` Quentin Schulz
2026-04-02 0:14 ` [v2,1/2] " Simon Glass
3 siblings, 0 replies; 8+ messages in thread
From: Quentin Schulz @ 2026-04-01 13:45 UTC (permalink / raw)
To: Gregor Herburger, u-boot
Cc: Alexander Sverdlin, Andre Przywara, Casey Connolly, David Zang,
Heinrich Schuchardt, Ilias Apalodimas, Sam Protsenko, Simon Glass,
Sughosh Ganu, Tom Rini
Hi Gregor,
On 4/1/26 2:40 PM, Gregor Herburger wrote:
> Add a common helper console_flush_stdin() to drain all pending
> characters from stdin. This consolidates the open-coded
> while (tstc()) getchar() pattern that appeared in multiple places
> across the tree.
>
Reviewed-by: Quentin Schulz <quentin.schulz@cherry.de>
Thanks!
Quentin
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [v2,1/2] console: add console_flush_stdin()
2026-04-01 12:40 [PATCH v2 1/2] console: add console_flush_stdin() Gregor Herburger
` (2 preceding siblings ...)
2026-04-01 13:45 ` Quentin Schulz
@ 2026-04-02 0:14 ` Simon Glass
3 siblings, 0 replies; 8+ messages in thread
From: Simon Glass @ 2026-04-02 0:14 UTC (permalink / raw)
To: gregor.herburger
Cc: u-boot, Quentin Schulz, Alexander Sverdlin, Andre Przywara,
Casey Connolly, David Zang, Heinrich Schuchardt, Ilias Apalodimas,
Sam Protsenko, Simon Glass, Sughosh Ganu, Tom Rini
On 2026-04-01T12:40:44, Gregor Herburger <gregor.herburger@linutronix.de> wrote:
> console: add console_flush_stdin()
> console: add console_flush_stdin()
>
> Add a common helper console_flush_stdin() to drain all pending
> characters from stdin. This consolidates the open-coded
> while (tstc()) getchar() pattern that appeared in multiple places
> across the tree.
>
> Signed-off-by: Gregor Herburger <gregor.herburger@linutronix.de>
> Reviewed-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
> Reviewed-by: Quentin Schulz <quentin.schulz@cherry.de>
>
> cmd/conitrace.c | 4 ++--
> cmd/eficonfig.c | 6 +++---
> cmd/eficonfig_sbkey.c | 4 ++--
> common/autoboot.c | 11 +----------
> common/console.c | 12 ++++++++++--
> include/console.h | 5 +++++
> lib/efi_loader/efi_console.c | 7 +++----
> 7 files changed, 26 insertions(+), 23 deletions(-)
Reviewed-by: Simon Glass <sjg@chromium.org>
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-04-03 13:23 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-01 12:40 [PATCH v2 1/2] console: add console_flush_stdin() Gregor Herburger
2026-04-01 12:40 ` [PATCH v2 2/2] cli: flush stdin before enabling cli Gregor Herburger
2026-04-02 0:15 ` [v2,2/2] " Simon Glass
2026-04-02 8:06 ` Gregor Herburger
2026-04-03 13:22 ` Simon Glass
2026-04-01 13:23 ` [PATCH v2 1/2] console: add console_flush_stdin() Sverdlin, Alexander
2026-04-01 13:45 ` Quentin Schulz
2026-04-02 0:14 ` [v2,1/2] " Simon Glass
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox