Skip to contentSkip to conversation

console: add console_flush_stdin()

PATCH Superseded Gregor Herburger 2 participants 6 messages Apr 7 – Apr 13

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 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.

---

(no changes since v2)

 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(-)
cmd/conitrace.c +2 -2
index 6cc113328eb..aef094e03df 100644
@@ -6,6 +6,7 @@
  * Copyright (c) 2018, Heinrich Schuchardt <[email protected]>
  */
 #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();
cmd/eficonfig.c +3 -3
index d8d946c87ac..60c39bc3a99 100644
@@ -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
cmd/eficonfig_sbkey.c +2 -2
index b3325a540f9..a6c5416d3a5 100644
@@ -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();
common/autoboot.c +1 -10
index 1783ef92c94..4b80ddb5b28 100644
@@ -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.
common/console.c +10 -2
index 48586fd2166..90b437fd1fc 100644
@@ -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();
include/console.h +5 -0
index 8d0d7bb8a4c..01a04f28f31 100644
@@ -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.
  */
lib/efi_loader/efi_console.c +3 -4
index 068d1a0a7b7..f83415f2ed3 100644
@@ -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;
 }
 
Signature · 4 lines
2.47.3

base-commit: c704af3c8b0f37929bce8c2a4bba27d6e89919c7
branch: cmd-flush-stdin
#1 lore raw

PATCH v3 2/2 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.

---

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(+)
cmd/Kconfig +8 -0
index 322ebe600c5..214b050e511 100644
@@ -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
common/cli.c +4 -0
index 4694a35cd0e..bcc7264d51a 100644
@@ -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();
Signature · 4 lines
2.47.3

base-commit: c704af3c8b0f37929bce8c2a4bba27d6e89919c7
branch: cmd-flush-stdin
Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] cli: flush stdin before enabling cli

Hi Gregor,

Quoted 17 lines · Gregor Herburger

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.

cmd/Kconfig | 8 ++++++++

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

cmd/Kconfig config HUSH_SELECTABLE · line 52

Quoted 6 lines
cmd/Kconfig +6 -0
@@ -47,6 +47,14 @@ config HUSH_SELECTABLE
+config CMDLINE_FLUSH_STDIN
+     bool "Enable flushing input before starting cli"
+         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.

Thanks for expanding the help text!

#3 lore raw

Re: [PATCH v3 2/2] cli: flush stdin before enabling cli

Hi Simon,

Quoted 29 lines · Simon Glass

Hi Gregor,

Quoted 17 lines · Gregor Herburger

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.

cmd/Kconfig | 8 ++++++++

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

cmd/Kconfig config HUSH_SELECTABLE · line 52

Quoted 6 lines
cmd/Kconfig +6 -0
@@ -47,6 +47,14 @@ config HUSH_SELECTABLE
+config CMDLINE_FLUSH_STDIN
+     bool "Enable flushing input before starting cli"
+         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.

Thanks for expanding the help text!

Quoted 1 line

I just realized I didn't collected the Reviewed-by Tags from v2. Should I send a v4 with these or does a maintainer add them?

Regards
Gregor

#4 lore raw

Re: [PATCH v3 2/2] cli: flush stdin before enabling cli

Hi Gregor,

Quoted 33 lines · Gregor Herburger

Hi Simon,

Quoted 29 lines · Simon Glass

Hi Gregor,

Quoted 17 lines · Gregor Herburger

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.

cmd/Kconfig | 8 ++++++++

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

cmd/Kconfig config HUSH_SELECTABLE · line 52

Quoted 6 lines
cmd/Kconfig +6 -0
@@ -47,6 +47,14 @@ config HUSH_SELECTABLE
+config CMDLINE_FLUSH_STDIN
+     bool "Enable flushing input before starting cli"
+         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.

Thanks for expanding the help text!

Quoted 4 lines
Quoted 1 line

I just realized I didn't collected the Reviewed-by Tags from v2. Should I send a v4 with these or does a maintainer add them?

For a particular version, patchwork collects the tags in its database and maintainers apply patches from there so the tags are colletced.

If you send a new version you must add the tags yourself (patman can do this automatically).

So in this case, yes, you should send a new version with any tags that were missed when you sent v3.

Regards,
Simon

Reviewed-by on v3 1/2
#5 lore raw
In reply to Gregor Herburger
Quoted 9 lines · Gregor Herburger

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.

Quoted 8 lines

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(-)