* [PATCH] cli: flush stdin before enabling cli
@ 2026-03-31 11:03 Gregor Herburger
2026-03-31 18:11 ` Tom Rini
2026-04-01 9:28 ` Quentin Schulz
0 siblings, 2 replies; 6+ messages in thread
From: Gregor Herburger @ 2026-03-31 11:03 UTC (permalink / raw)
To: u-boot
Cc: Gregor Herburger, Andrew Goodbody, Heiko Schocher,
Heinrich Schuchardt, Ilias Apalodimas, Kory Maincent (TI.com),
Mikhail Kshevetskiy, Quentin Schulz, 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>
---
cmd/Kconfig | 7 +++++++
common/cli.c | 6 ++++++
2 files changed, 13 insertions(+)
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 322ebe600c5..e08fbf27358 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -47,6 +47,13 @@ config HUSH_SELECTABLE
default y if HUSH_OLD_PARSER && HUSH_MODERN_PARSER
endmenu
+config CMDLINE_FLUSH_STDIN
+ bool "Enable flushing input before starting cli"
+ default n
+ 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..42d27ed87e7 100644
--- a/common/cli.c
+++ b/common/cli.c
@@ -295,6 +295,12 @@ err:
void cli_loop(void)
{
bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
+
+ if (IS_ENABLED(CONFIG_CMDLINE_FLUSH_STDIN)) {
+ while (tstc())
+ (void)getchar();
+ }
+
#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] 6+ messages in thread
* Re: [PATCH] cli: flush stdin before enabling cli
2026-03-31 11:03 [PATCH] cli: flush stdin before enabling cli Gregor Herburger
@ 2026-03-31 18:11 ` Tom Rini
2026-04-01 5:32 ` Gregor Herburger
2026-04-01 9:28 ` Quentin Schulz
1 sibling, 1 reply; 6+ messages in thread
From: Tom Rini @ 2026-03-31 18:11 UTC (permalink / raw)
To: Gregor Herburger
Cc: u-boot, Andrew Goodbody, Heiko Schocher, Heinrich Schuchardt,
Ilias Apalodimas, Kory Maincent (TI.com), Mikhail Kshevetskiy,
Quentin Schulz
[-- Attachment #1: Type: text/plain, Size: 1832 bytes --]
On Tue, Mar 31, 2026 at 01:03:08PM +0200, Gregor Herburger wrote:
> 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>
Conceptually, good idea, thanks for doing it.
>
> ---
>
> cmd/Kconfig | 7 +++++++
> common/cli.c | 6 ++++++
> 2 files changed, 13 insertions(+)
>
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 322ebe600c5..e08fbf27358 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -47,6 +47,13 @@ config HUSH_SELECTABLE
> default y if HUSH_OLD_PARSER && HUSH_MODERN_PARSER
> endmenu
>
> +config CMDLINE_FLUSH_STDIN
> + bool "Enable flushing input before starting cli"
> + default n
We don't need default n as that's the default.
> + 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..42d27ed87e7 100644
> --- a/common/cli.c
> +++ b/common/cli.c
> @@ -295,6 +295,12 @@ err:
> void cli_loop(void)
> {
> bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
> +
> + if (IS_ENABLED(CONFIG_CMDLINE_FLUSH_STDIN)) {
> + while (tstc())
> + (void)getchar();
> + }
> +
> #if CONFIG_IS_ENABLED(HUSH_PARSER)
> if (gd->flags & GD_FLG_HUSH_MODERN_PARSER)
> parse_and_run_file();
This is flush_stdin() from common/autoboot.c, but that's a static
function. We should find a better spot for that perhaps and then call it
when required.
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] cli: flush stdin before enabling cli
2026-03-31 18:11 ` Tom Rini
@ 2026-04-01 5:32 ` Gregor Herburger
0 siblings, 0 replies; 6+ messages in thread
From: Gregor Herburger @ 2026-04-01 5:32 UTC (permalink / raw)
To: Tom Rini
Cc: u-boot, Andrew Goodbody, Heiko Schocher, Heinrich Schuchardt,
Ilias Apalodimas, Kory Maincent (TI.com), Mikhail Kshevetskiy,
Quentin Schulz
Hi Tom,
> Conceptually, good idea, thanks for doing it.
> >
> > ---
> >
> > cmd/Kconfig | 7 +++++++
> > common/cli.c | 6 ++++++
> > 2 files changed, 13 insertions(+)
> >
> > diff --git a/cmd/Kconfig b/cmd/Kconfig
> > index 322ebe600c5..e08fbf27358 100644
> > --- a/cmd/Kconfig
> > +++ b/cmd/Kconfig
> > @@ -47,6 +47,13 @@ config HUSH_SELECTABLE
> > default y if HUSH_OLD_PARSER && HUSH_MODERN_PARSER
> > endmenu
> >
> > +config CMDLINE_FLUSH_STDIN
> > + bool "Enable flushing input before starting cli"
> > + default n
>
> We don't need default n as that's the default.
> > + if (IS_ENABLED(CONFIG_CMDLINE_FLUSH_STDIN)) {
> > + while (tstc())
> > + (void)getchar();
> > + }
> > +
> > #if CONFIG_IS_ENABLED(HUSH_PARSER)
> > if (gd->flags & GD_FLG_HUSH_MODERN_PARSER)
> > parse_and_run_file();
>
> This is flush_stdin() from common/autoboot.c, but that's a static
> function. We should find a better spot for that perhaps and then call it
> when required.
Thanks for the review. I will send a v2 to change this.
Gregor
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] cli: flush stdin before enabling cli
2026-03-31 11:03 [PATCH] cli: flush stdin before enabling cli Gregor Herburger
2026-03-31 18:11 ` Tom Rini
@ 2026-04-01 9:28 ` Quentin Schulz
2026-04-01 10:46 ` Gregor Herburger
1 sibling, 1 reply; 6+ messages in thread
From: Quentin Schulz @ 2026-04-01 9:28 UTC (permalink / raw)
To: Gregor Herburger, u-boot
Cc: Andrew Goodbody, Heiko Schocher, Heinrich Schuchardt,
Ilias Apalodimas, Kory Maincent (TI.com), Mikhail Kshevetskiy,
Tom Rini
Hi Gregor,
On 3/31/26 1:03 PM, Gregor Herburger wrote:
> [You don't often get email from gregor.herburger@linutronix.de. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> 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.
>
Why does this need an option? I don't know enough (anything really)
about the cli in general but the wording seems to indicate this fixes an
issue or even a bug. Is there a case in which we wouldn't want to flush?
Cheers,
Quentin
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] cli: flush stdin before enabling cli
2026-04-01 9:28 ` Quentin Schulz
@ 2026-04-01 10:46 ` Gregor Herburger
2026-04-01 11:18 ` Quentin Schulz
0 siblings, 1 reply; 6+ messages in thread
From: Gregor Herburger @ 2026-04-01 10:46 UTC (permalink / raw)
To: Quentin Schulz
Cc: u-boot, Andrew Goodbody, Heiko Schocher, Heinrich Schuchardt,
Ilias Apalodimas, Kory Maincent (TI.com), 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.
> >
>
> Why does this need an option? I don't know enough (anything really) about
> the cli in general but the wording seems to indicate this fixes an issue or
> even a bug. Is there a case in which we wouldn't want to flush?
In our case the problem was in efi_console where query_console_serial
sent the escape sequence to stdout and stdin was set to nulldev and
therefore never received. In the bootcmd stdin was set to serial and the
response was seen on the cli. Thats how it was in our case. My first
approach was to check in efi_console to not send if stdin is nulldev.
But after more thoughts I implemented the current approach which flushes
stdin before starting cli. After all efi_console did exactly what was
configured in the environment.
I can think of two cases where stdin shouldn't be flushed:
* User typing commands before cli is reached.
* Some scripts sending commands before cli prompt is reached.
Imho there should be at least an option to let users decide on there
usecase. If the option is default or not I don't have a strong opinion on.
Gregor
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] cli: flush stdin before enabling cli
2026-04-01 10:46 ` Gregor Herburger
@ 2026-04-01 11:18 ` Quentin Schulz
0 siblings, 0 replies; 6+ messages in thread
From: Quentin Schulz @ 2026-04-01 11:18 UTC (permalink / raw)
To: Gregor Herburger
Cc: u-boot, Andrew Goodbody, Heiko Schocher, Heinrich Schuchardt,
Ilias Apalodimas, Kory Maincent (TI.com), Mikhail Kshevetskiy,
Tom Rini
Hi Gregor,
On 4/1/26 12:46 PM, Gregor Herburger wrote:
> [You don't often get email from gregor.herburger@linutronix.de. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
>>> 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.
>>>
>>
>> Why does this need an option? I don't know enough (anything really) about
>> the cli in general but the wording seems to indicate this fixes an issue or
>> even a bug. Is there a case in which we wouldn't want to flush?
>
> In our case the problem was in efi_console where query_console_serial
> sent the escape sequence to stdout and stdin was set to nulldev and
> therefore never received. In the bootcmd stdin was set to serial and the
> response was seen on the cli. Thats how it was in our case. My first
> approach was to check in efi_console to not send if stdin is nulldev.
> But after more thoughts I implemented the current approach which flushes
> stdin before starting cli. After all efi_console did exactly what was
> configured in the environment.
>
> I can think of two cases where stdin shouldn't be flushed:
> * User typing commands before cli is reached.
I see we have flush_stdin() in common/autoboot.c already, with the same
implementation. Guarded by CONFIG_AUTOBOOT_FLUSH_STDIN. So I guess if we
have a knob for that, another knob for the cli_loop() part will at least
be consistent so fine by me. I kinda fail to see why we would not want
to flush, but /me shrugs.
Cheers,
Quentin
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-04-01 12:56 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-31 11:03 [PATCH] cli: flush stdin before enabling cli Gregor Herburger
2026-03-31 18:11 ` Tom Rini
2026-04-01 5:32 ` Gregor Herburger
2026-04-01 9:28 ` Quentin Schulz
2026-04-01 10:46 ` Gregor Herburger
2026-04-01 11:18 ` Quentin Schulz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox