common: add AUTOBOOT_FLUSH_STDIN option
authorSteffen Jaeckel <jaeckel-floss@eyet-services.de>
Thu, 8 Jul 2021 13:57:38 +0000 (15:57 +0200)
committerTom Rini <trini@konsulko.com>
Fri, 23 Jul 2021 17:36:20 +0000 (13:36 -0400)
The key-sequence based unlock mechanisms are sensitive to junk symbols
that could have been sent to stdin and are still waiting to be retrieved.
Enabling this option will read all symbols off stdin before displaying the
autoboot prompt (and starting to read the password from stdin).

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
common/Kconfig.boot
common/autoboot.c

index f9551b2..e0cca22 100644 (file)
@@ -791,6 +791,15 @@ config AUTOBOOT_KEYED
          U-Boot automatic booting process and bring the device
          to the U-Boot prompt for user input.
 
+config AUTOBOOT_FLUSH_STDIN
+       bool "Enable flushing stdin before starting to read the password"
+       depends on AUTOBOOT_KEYED && !SANDBOX
+       help
+         When this option is enabled stdin buffer will be flushed before
+         starting to read the password.
+         This can't be enabled for the sandbox as flushing stdin would
+         break the autoboot unit tests.
+
 config AUTOBOOT_PROMPT
        string "Autoboot stop prompt"
        depends on AUTOBOOT_KEYED
index 2564ef8..35ef526 100644 (file)
@@ -297,6 +297,15 @@ 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();
+}
+
 /***************************************************************************
  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
@@ -306,6 +315,8 @@ static int abortboot_key_sequence(int bootdelay)
        int abort;
        uint64_t etime = endtick(bootdelay);
 
+       if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
+               flush_stdin();
 #  ifdef CONFIG_AUTOBOOT_PROMPT
        /*
         * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.