From 8c4252ae92af1bbc210f57b9341b5f89dab6f215 Mon Sep 17 00:00:00 2001
From: Andy Lau <liuyang01@kylinos.cn>
Date: Wed, 28 May 2025 16:14:37 +0800
Subject: [PATCH] lib/crypto: The echo_password environment variable determines
 whether to dispaly the password entered by the user

Implements character echo (e.g., '*') for user password input in GRUB.
Currently, there is no visual feedback during password entry, making it difficult for users
to confirm their input. therefore, using the echo_password boolean environment variable
allows users to decide for themselves whether to display their "password".
This change significantly improves the user experience.

Signed-off-by: Andy Lau <liuyang01@kylinos.cn>
---
 grub-core/lib/crypto.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/grub-core/lib/crypto.c b/grub-core/lib/crypto.c
index 396f76410..f28051778 100644
--- a/grub-core/lib/crypto.c
+++ b/grub-core/lib/crypto.c
@@ -455,6 +455,9 @@ grub_password_get (char buf[], unsigned buf_size)
 {
   unsigned cur_len = 0;
   int key;
+  bool echo_password = false;
+
+  echo_password = grub_env_get_bool ("echo_password", false);
 
   while (1)
     {
@@ -471,7 +474,10 @@ grub_password_get (char buf[], unsigned buf_size)
       if (key == '\b')
 	{
 	  if (cur_len)
-	    cur_len--;
+        {
+          cur_len--;
+          grub_printf ("\b \b");
+        }
 	  continue;
 	}
 
@@ -479,7 +485,12 @@ grub_password_get (char buf[], unsigned buf_size)
 	continue;
 
       if (cur_len + 2 < buf_size)
-	buf[cur_len++] = key;
+        {
+          buf[cur_len++] = key;
+          if (echo_password) {
+            grub_printf ("*");
+          }
+        }
     }
 
   grub_memset (buf + cur_len, 0, buf_size - cur_len);
-- 
2.34.1

