Skip to content

Commit 25b3d80

Browse files
committed
initrd/etc/gui_functions pause_automatic_boot: show UTC timestamp + TOTP code while waiting for automatic boot when a default boot is set and hotp is valid
Signed-off-by: Thierry Laurion <[email protected]>
1 parent aaeb63d commit 25b3d80

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

initrd/etc/gui_functions

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,48 @@
55
# Pause for the configured timeout before booting automatically. Returns 0 to
66
# continue with automatic boot, nonzero if user interrupted.
77
pause_automatic_boot() {
8-
if IFS= read -t "$CONFIG_AUTO_BOOT_TIMEOUT" -s -n 1 -r -p \
9-
$'Automatic boot in '"$CONFIG_AUTO_BOOT_TIMEOUT"$' seconds unless interrupted by keypress...\n'; then
10-
return 1 # Interrupt automatic boot
11-
fi
8+
local remaining="$CONFIG_AUTO_BOOT_TIMEOUT"
9+
10+
echo "Automatic boot in $CONFIG_AUTO_BOOT_TIMEOUT seconds unless interrupted by keypress..."
11+
printf "\n" # Reserve space for the updating line
12+
13+
while [ $remaining -gt 0 ]; do
14+
# Record the start time of this loop iteration (epoch seconds)
15+
local loop_start now_str status_line elapsed sleep_time
16+
loop_start=$(date +%s)
17+
18+
now_str=$(date -u '+%Y-%m-%d %H:%M:%S UTC')
19+
status_line="$now_str | Booting in $remaining seconds..."
20+
21+
if [ "$CONFIG_TPM" = "y" ] && [ "$CONFIG_TOTP_SKIP_QRCODE" != "y" ]; then
22+
# Get current TOTP if available and not skipped
23+
local current_totp
24+
if ! current_totp=$(unseal-totp 2>/dev/null); then
25+
die "Failed to unseal TOTP"
26+
fi
27+
status_line="$status_line | TOTP: $current_totp"
28+
fi
29+
30+
# Update the status line in place (overwrites previous output)
31+
printf "\r%s" "$status_line"
32+
33+
# Calculate elapsed time for this loop iteration
34+
elapsed=$(( $(date +%s) - loop_start ))
35+
# Calculate how much time to wait to complete 1 second
36+
sleep_time=$(( 1 - elapsed ))
37+
[ $sleep_time -lt 0 ] && sleep_time=0
38+
39+
# Wait for keypress for the remaining time in this second
40+
# IFS= disables word splitting, -t sets timeout, -s disables echo, -n 1 reads one char, -r disables backslash escapes
41+
if IFS= read -t $sleep_time -s -n 1 -r; then
42+
printf "\n" # New line after interrupt
43+
return 1 # Interrupt automatic boot
44+
fi
45+
46+
remaining=$((remaining - 1))
47+
done
48+
49+
printf "\n" # New line after countdown
1250
return 0 # Continue with automatic boot
1351
}
1452

0 commit comments

Comments
 (0)