Skip to content

Commit c5e449d

Browse files
authored
Merge pull request #1748 from JonathonHall-Purism/change-time-improvements
Alexgithublab: change time, 3.0 (supersedes #1737)
2 parents dd15322 + 05b3d85 commit c5e449d

2 files changed

Lines changed: 79 additions & 10 deletions

File tree

initrd/bin/change-time.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
#change time using hwclock and date -s
3+
4+
clear
5+
6+
echo "The system time is: $(date "+%Y-%m-%d %H:%M:%S %Z")"
7+
echo
8+
echo "Please enter the current date and time in UTC"
9+
echo "To find the current date and time in UTC, please check https://time.is/UTC"
10+
echo
11+
12+
get_date () {
13+
local field_name min max
14+
field_name="$1"
15+
min="$2"
16+
max="$3"
17+
echo -n "Enter the current $field_name [$min-$max]: "
18+
read -r value
19+
echo
20+
21+
#must be a number between $2 and $3
22+
while [[ ! $value =~ ^[0-9]+$ ]] || [[ ${value#0} -lt $min ]] || [[ ${value#0} -gt $max ]];
23+
do
24+
echo "Please try again, it must be a number from $min to $max."
25+
echo -n "Enter the current $field_name [$min-$max]: "
26+
read -r value
27+
echo
28+
done
29+
30+
# Pad with zeroes to length of maximum value.
31+
# The "$((10#$value))" is needed to handle 08 and 09 correctly, which printf
32+
# would otherwise interpret as octal. This effectively strips the leading
33+
# zero by evaluating an arithmetic expression with the base set to 10.
34+
value="$(printf "%0${#max}u" "$((10#$value))")"
35+
}
36+
37+
enter_time_and_change()
38+
{
39+
get_date "year" "2024" "2200"
40+
year=$value
41+
get_date "month" "01" "12"
42+
month=$value
43+
get_date "day" "01" "31"
44+
day=$value
45+
get_date "hour" "00" "23"
46+
hour=$value
47+
get_date "minute" "00" "59"
48+
min=$value
49+
get_date "second" "00" "59"
50+
sec=$value
51+
52+
if ! date -s "$year-$month-$day $hour:$min:$sec" &>/dev/null; then
53+
return 1
54+
fi
55+
return 0
56+
}
57+
58+
while ! enter_time_and_change; do
59+
echo "Could not set the date to $year-$month-$day $hour:$min:$sec"
60+
read -rp "Try again? [Y/n]: " try_again_confirm
61+
if [ "${try_again_confirm^^}" = N ]; then
62+
exit 1
63+
fi
64+
echo
65+
done
66+
67+
hwclock -w
68+
echo "The system date has been sucessfully set to $year-$month-$day $hour:$min:$sec UTC"
69+
echo
70+
71+
echo "Press Enter to return to the menu"
72+
echo
73+
read -r nothing

initrd/bin/gui-init

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ show_options_menu()
430430
--menu "" 0 80 10 \
431431
'b' ' Boot Options -->' \
432432
't' ' TPM/TOTP/HOTP Options -->' \
433+
'h' ' Change system time' \
433434
'u' ' Update checksums and sign all files in /boot' \
434435
'c' ' Change configuration settings -->' \
435436
'f' ' Flash/Update the BIOS -->' \
@@ -450,6 +451,9 @@ show_options_menu()
450451
t )
451452
show_tpm_totp_hotp_options_menu
452453
;;
454+
h )
455+
change-time.sh
456+
;;
453457
u )
454458
prompt_update_checksums
455459
;;
@@ -543,16 +547,8 @@ prompt_totp_mismatch()
543547
{
544548
TRACE_FUNC
545549
if (whiptail_warning --title "TOTP/HOTP code mismatched" \
546-
--yesno "TOTP/HOTP code mismatches could indicate either TPM tampering or clock drift:\n\nTo correct clock drift: 'date -s yyyy-MM-DD hh:mm:ss' in UTC timezone\nand save it to the RTC: 'hwclock -w'\nthen reboot and try again.\n\nWould you like to exit to a recovery console?" 0 80) then
547-
echo ""
548-
echo "To correct clock drift: 'date -s yyyy-MM-DD hh:mm:ss' in UTC timezone"
549-
echo "and save it to the RTC: 'hwclock -w'"
550-
echo ""
551-
echo "Alternatively you could do this automatically with an Ethernet cable connected to a functional network: 'network-init-recovery'"
552-
echo ""
553-
echo "Then reboot and try again"
554-
echo ""
555-
recovery "TOTP/HOTP mismatch"
550+
--yesno "TOTP/HOTP code mismatches could indicate TPM tampering or clock drift.\n\nThe current UTC time is: $(date "+%Y-%m-%d %H:%M:%S")\nIf this is incorrect, set the correct time and check TOTP/HOTP again.\n\nDo you want to change the time?" 0 80) then
551+
change-time.sh
556552
fi
557553
}
558554

0 commit comments

Comments
 (0)