]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
console: add console_flush_stdin()
authorGregor Herburger <gregor.herburger@linutronix.de>
Mon, 13 Apr 2026 15:24:53 +0000 (17:24 +0200)
committerTom Rini <trini@konsulko.com>
Wed, 22 Apr 2026 20:23:49 +0000 (14:23 -0600)
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>
Reviewed-by: Simon Glass <sjg@chromium.org>
cmd/conitrace.c
cmd/eficonfig.c
cmd/eficonfig_sbkey.c
common/autoboot.c
common/console.c
include/console.h
lib/efi_loader/efi_console.c

index 6cc113328ebc85fac20790580e129503065b5694..aef094e03dfe527927b8d3730987d1b251c1722e 100644 (file)
@@ -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();
index d8d946c87ac8e6b9469275d3e945a89835ce98fa..60c39bc3a99b06f64da7fd6b565fb2add3ac7849 100644 (file)
@@ -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
index b3325a540f9a16ec626573a60d150b6ba54312e1..a6c5416d3a50c3477184acfa18331c76202dc360 100644 (file)
@@ -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();
index 1783ef92c94cbf6c0fd26c7bd73e3381fcf6ac0b..4b80ddb5b28fb8d5ee232e022187c1760779f69c 100644 (file)
@@ -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.
index 22e554cf203a5db33a274c96bcb0e4ff074a8a5a..54d1249422d44903a68a2fdb97cbbd9fb7dfa4e2 100644 (file)
@@ -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();
index 8d0d7bb8a4c26b617f9a834ec982024ee36cbd94..01a04f28f31e0a01e59006b7e6d1d59b149ef110 100644 (file)
@@ -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.
  */
index a798d5604a31a855c65daef76a44fc4fb3786f0e..8d0760582809510b70b31971e98728ea54c77645 100644 (file)
@@ -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;
 }