diff options
| author | Gregor Herburger <[email protected]> | 2026-04-13 17:24:53 +0200 |
|---|---|---|
| committer | Tom Rini <[email protected]> | 2026-04-22 14:23:49 -0600 |
| commit | 2c8fdd7aea1d3c6c380ea5b5080147dc7fe9c38f (patch) | |
| tree | 266330ba84691d7cbdabeab1f38ba7fd712c2aed | |
| parent | 3abc7c1d46de4211b2d11051033566c9ad2c2740 (diff) | |
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 <[email protected]>
Reviewed-by: Alexander Sverdlin <[email protected]>
Reviewed-by: Quentin Schulz <[email protected]>
Reviewed-by: Simon Glass <[email protected]>
| -rw-r--r-- | cmd/conitrace.c | 4 | ||||
| -rw-r--r-- | cmd/eficonfig.c | 6 | ||||
| -rw-r--r-- | cmd/eficonfig_sbkey.c | 4 | ||||
| -rw-r--r-- | common/autoboot.c | 11 | ||||
| -rw-r--r-- | common/console.c | 12 | ||||
| -rw-r--r-- | include/console.h | 5 | ||||
| -rw-r--r-- | 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 <[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(); 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 @@ -316,15 +316,6 @@ static int passwd_abort_key(uint64_t etime) } /** - * 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 22e554cf203..54d1249422d 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 a798d5604a3..8d076058280 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. @@ -960,8 +960,7 @@ static void efi_cin_check(void) */ static void efi_cin_empty_buffer(void) { - while (tstc()) - getchar(); + console_flush_stdin(); key_available = false; } |
