From 0b47e50344e406f9db49170054d3daad17a7a06b Mon Sep 17 00:00:00 2001
From: Ayush Singh <ayush@beagleboard.org>
Date: Tue, 8 Apr 2025 22:00:28 +0530
Subject: [PATCH] std: sys: stdio: uefi: Do not retry on NOT_READY

Since NOT_READY is now used to signal read 0. Remove the loop which
would always retry on NOT_READY status.

Instead, use the following algorithm:

1. Try reading any pending characters. Return if present.
2. If no pending character, NOT_READY is returned.
3. Wait for key.
4. Return the key or error.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
 library/std/src/sys/stdio/uefi.rs | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/library/std/src/sys/stdio/uefi.rs b/library/std/src/sys/stdio/uefi.rs
index 7fe445a7358..ce0db1666e6 100644
--- a/library/std/src/sys/stdio/uefi.rs
+++ b/library/std/src/sys/stdio/uefi.rs
@@ -184,13 +184,17 @@ unsafe fn simple_text_output(
 fn simple_text_input_read(
     stdin: *mut r_efi::protocols::simple_text_input::Protocol,
 ) -> io::Result<u16> {
-    loop {
-        match read_key_stroke(stdin) {
-            Ok(x) => return Ok(x.unicode_char),
-            Err(e) if e == r_efi::efi::Status::NOT_READY => wait_stdin(stdin)?,
-            Err(e) => return Err(io::Error::from_raw_os_error(e.as_usize())),
-        }
+    // Try reading any pending keys. Else wait for a new character
+    match read_key_stroke(stdin) {
+        Ok(x) => return Ok(x.unicode_char),
+        Err(e) if e == r_efi::efi::Status::NOT_READY => wait_stdin(stdin)?,
+        Err(e) => return Err(io::Error::from_raw_os_error(e.as_usize())),
     }
+
+    // Try reading a key after the wait.
+    read_key_stroke(stdin)
+        .map(|x| x.unicode_char)
+        .map_err(|e| io::Error::from_raw_os_error(e.as_usize()))
 }
 
 fn wait_stdin(stdin: *mut r_efi::protocols::simple_text_input::Protocol) -> io::Result<()> {