2023-02-08 21:01:48 +00:00
#!/bin/bash
2018-02-20 23:35:37 +00:00
# Boot from a local disk installation
2020-10-23 22:18:20 +00:00
BOARD_NAME=${CONFIG_BOARD_NAME:-${CONFIG_BOARD}}
2023-06-21 18:19:41 +00:00
MAIN_MENU_TITLE="${BOARD_NAME} | $CONFIG_BRAND_NAME Boot Menu"
2021-09-23 17:56:17 +00:00
export BG_COLOR_MAIN_MENU=""
2018-02-21 23:58:54 +00:00
2018-02-20 23:35:37 +00:00
. /etc/functions
2022-01-07 19:30:57 +00:00
. /etc/gui_functions
2022-04-06 21:25:40 +00:00
. /etc/luks-functions
2018-12-06 23:24:28 +00:00
. /tmp/config
2018-02-20 23:35:37 +00:00
2022-11-23 18:55:09 +00:00
# skip_to_menu is set if the user selects "continue to the main menu" from any
# error, so we will indeed go to the main menu even if other errors occur. It's
# reset when we reach the main menu so the user can retry from the main menu and
# # see errors again.
skip_to_menu="false"
2018-02-20 23:35:37 +00:00
mount_boot()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2018-02-23 20:13:21 +00:00
# Mount local disk if it is not already mounted
2019-07-08 01:47:04 +00:00
while ! grep -q /boot /proc/mounts ; do
2020-10-23 23:35:39 +00:00
# try to mount if CONFIG_BOOT_DEV exists
if [ -e "$CONFIG_BOOT_DEV" ]; then
mount -o ro $CONFIG_BOOT_DEV /boot
[[ $? -eq 0 ]] && continue
2019-07-08 01:47:04 +00:00
fi
2020-10-23 23:35:39 +00:00
# CONFIG_BOOT_DEV doesn't exist or couldn't be mounted, so give user options
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=$BG_COLOR_ERROR
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_ERROR --title "ERROR: No Bootable OS Found!" \
2020-10-23 23:35:39 +00:00
--menu " No bootable OS was found on the default boot device $CONFIG_BOOT_DEV.
2022-11-09 16:51:27 +00:00
How would you like to proceed?" 0 80 4 \
2020-10-23 23:35:39 +00:00
'b' ' Select a new boot device' \
'u' ' Boot from USB' \
'm' ' Continue to the main menu' \
'x' ' Exit to recovery shell' \
2>/tmp/whiptail || recovery "GUI menu failed"
option=$(cat /tmp/whiptail)
case "$option" in
b )
2019-07-09 03:27:40 +00:00
config-gui.sh boot_device_select
2020-10-23 23:35:39 +00:00
if [ $? -eq 0 ]; then
# update CONFIG_BOOT_DEV
. /tmp/config
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=""
2020-10-23 23:35:39 +00:00
fi
;;
u )
exec /bin/usb-init
;;
m )
2022-11-23 18:55:09 +00:00
skip_to_menu="true"
2020-10-23 23:35:39 +00:00
break
;;
* )
recovery "User requested recovery shell"
;;
esac
2019-07-08 01:47:04 +00:00
done
2018-02-20 23:35:37 +00:00
}
2021-09-22 16:42:07 +00:00
2018-04-03 22:20:34 +00:00
verify_global_hashes()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2018-04-03 22:20:34 +00:00
# Check the hashes of all the files, ignoring signatures for now
check_config /boot force
TMP_HASH_FILE="/tmp/kexec/kexec_hashes.txt"
2022-12-31 17:41:24 +00:00
TMP_TREE_FILE="/tmp/kexec/kexec_tree.txt"
2018-04-03 22:20:34 +00:00
TMP_PACKAGE_TRIGGER_PRE="/tmp/kexec/kexec_package_trigger_pre.txt"
TMP_PACKAGE_TRIGGER_POST="/tmp/kexec/kexec_package_trigger_post.txt"
2022-12-31 17:41:24 +00:00
if verify_checksums /boot ; then
2018-04-03 22:20:34 +00:00
return 0
2022-12-31 17:41:24 +00:00
elif [[ ! -f "$TMP_HASH_FILE" || ! -f "$TMP_TREE_FILE" ]] ; then
2023-01-08 13:01:12 +00:00
if (whiptail $BG_COLOR_ERROR --title 'ERROR: Missing File!' \
--yesno "One of the files containing integrity information for /boot is missing!\n\nIf you are setting up heads for the first time or upgrading from an\nolder version, select Yes to create the missing files.\n\nOtherwise this could indicate a compromise and you should select No to\nreturn to the main menu.\n\nWould you like to create the missing files now?" 0 80) then
2021-09-24 20:05:14 +00:00
if update_checksums ; then
BG_COLOR_MAIN_MENU=""
return 0;
else
whiptail $BG_COLOR_ERROR --title 'ERROR' \
2022-11-09 16:51:27 +00:00
--msgbox "Failed to update checksums / sign default config" 0 80
2021-09-24 20:05:14 +00:00
fi
2018-04-03 22:20:34 +00:00
fi
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=$BG_COLOR_ERROR
2018-04-03 22:20:34 +00:00
return 1
else
2021-10-29 18:26:02 +00:00
CHANGED_FILES=$(grep -v 'OK$' /tmp/hash_output | cut -f1 -d ':' | tee -a /tmp/hash_output_mismatches)
CHANGED_FILES_COUNT=$(wc -l /tmp/hash_output_mismatches | cut -f1 -d ' ')
2018-04-03 22:20:34 +00:00
# if files changed before package manager started, show stern warning
if [ -f "$TMP_PACKAGE_TRIGGER_PRE" ]; then
PRE_CHANGED_FILES=$(grep '^CHANGED_FILES' $TMP_PACKAGE_TRIGGER_POST | cut -f 2 -d '=' | tr -d '"')
2023-06-21 18:19:41 +00:00
TEXT="The following files failed the verification process BEFORE package updates ran:\n${PRE_CHANGED_FILES}\n\nCompare against the files $CONFIG_BRAND_NAME has detected have changed:\n${CHANGED_FILES}\n\nThis could indicate a compromise!\n\nWould you like to update your checksums anyway?"
2018-04-03 22:20:34 +00:00
# if files changed after package manager started, probably caused by package manager
elif [ -f "$TMP_PACKAGE_TRIGGER_POST" ]; then
LAST_PACKAGE_LIST=$(grep -E "^(Install|Remove|Upgrade|Reinstall):" $TMP_PACKAGE_TRIGGER_POST)
UPDATE_INITRAMFS_PACKAGE=$(grep '^UPDATE_INITRAMFS_PACKAGE' $TMP_PACKAGE_TRIGGER_POST | cut -f 2 -d '=' | tr -d '"')
if [ "$UPDATE_INITRAMFS_PACKAGE" != "" ]; then
TEXT="The following files failed the verification process AFTER package updates ran:\n${CHANGED_FILES}\n\nThis is likely due to package triggers in$UPDATE_INITRAMFS_PACKAGE.\n\nYou will need to update your checksums for all files in /boot.\n\nWould you like to update your checksums now?"
else
TEXT="The following files failed the verification process AFTER package updates ran:\n${CHANGED_FILES}\n\nThis might be due to the following package updates:\n$LAST_PACKAGE_LIST.\n\nYou will need to update your checksums for all files in /boot.\n\nWould you like to update your checksums now?"
fi
else
2021-10-29 18:26:02 +00:00
if [ $CHANGED_FILES_COUNT -gt 10 ]; then
# drop to console to show full file list
whiptail $ERROR_BG_COLOR --title 'ERROR: Boot Hash Mismatch' \
2023-06-30 18:21:11 +00:00
--msgbox "${CHANGED_FILES_COUNT} files failed the verification process!\\n\nThis could indicate a compromise!\n\nHit OK to review the list of files.\n\nType \"q\" to exit the list and return." 0 80
2021-10-29 18:26:02 +00:00
echo "Type \"q\" to exit the list and return." >> /tmp/hash_output_mismatches
less /tmp/hash_output_mismatches
#move outdated hash mismatch list
mv /tmp/hash_output_mismatches /tmp/hash_output_mismatch_old
TEXT="Would you like to update your checksums now?"
else
TEXT="The following files failed the verification process:\n\n${CHANGED_FILES}\n\nThis could indicate a compromise!\n\nWould you like to update your checksums now?"
fi
2018-04-03 22:20:34 +00:00
fi
2022-11-15 20:11:58 +00:00
if (whiptail $BG_COLOR_ERROR --title 'ERROR: Boot Hash Mismatch' --yesno "$TEXT" 0 80) then
2021-09-24 20:05:14 +00:00
if update_checksums ; then
BG_COLOR_MAIN_MENU=""
return 0;
else
whiptail $BG_COLOR_ERROR --title 'ERROR' \
2022-11-09 16:51:27 +00:00
--msgbox "Failed to update checksums / sign default config" 0 80
2021-09-24 20:05:14 +00:00
fi
2018-04-03 22:20:34 +00:00
fi
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=$BG_COLOR_ERROR
2018-04-03 22:20:34 +00:00
return 1
fi
}
2021-09-22 16:42:07 +00:00
2019-07-05 22:04:00 +00:00
prompt_update_checksums()
2018-04-03 22:20:34 +00:00
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-23 17:56:17 +00:00
if (whiptail $BG_COLOR_WARNING --title 'Update Checksums and sign all files in /boot' \
2022-11-09 16:51:27 +00:00
--yesno "You have chosen to update the checksums and sign all of the files in /boot.\n\nThis means that you trust that these files have not been tampered with.\n\nYou will need your GPG key available, and this change will modify your disk.\n\nDo you want to continue?" 0 80) then
2021-09-24 20:05:14 +00:00
if ! update_checksums ; then
whiptail $BG_COLOR_ERROR --title 'ERROR' \
2022-11-09 16:51:27 +00:00
--msgbox "Failed to update checksums / sign default config" 0 80
2021-09-24 20:05:14 +00:00
fi
2018-04-03 22:20:34 +00:00
fi
}
2021-09-22 16:42:07 +00:00
2023-03-10 20:42:19 +00:00
generate_totp_hotp()
2018-06-19 19:27:27 +00:00
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2023-10-24 15:19:49 +00:00
tpm_owner_password="$1" # May be empty, will prompt if needed and empty
2019-08-21 20:40:36 +00:00
if [ "$CONFIG_TPM" != "y" ] && [ -x /bin/hotp_verification ]; then
2019-11-25 18:25:11 +00:00
echo "Generating new HOTP secret"
/bin/seal-hotpkey
2023-10-24 15:19:49 +00:00
elif echo -e "Generating new TOTP secret...\n\n" && /bin/seal-totp "$BOARD_NAME" "$tpm_owner_password"; then
2019-08-21 20:40:36 +00:00
echo
2023-01-19 19:58:21 +00:00
if [ -x /bin/hotp_verification ]; then
2023-06-23 12:18:59 +00:00
if [ "$CONFIG_TOTP_SKIP_QRCODE" != y ]; then
echo "Once you have scanned the QR code, hit Enter to configure your HOTP USB Security Dongle (e.g. Librem Key or Nitrokey)"
read
fi
2023-01-19 19:58:21 +00:00
/bin/seal-hotpkey
2023-06-23 12:18:59 +00:00
else
if [ "$CONFIG_TOTP_SKIP_QRCODE" != y ]; then
echo "Once you have scanned the QR code, hit Enter to continue"
read
fi
2023-01-19 19:58:21 +00:00
fi
# clear screen
printf "\033c"
2018-06-19 19:27:27 +00:00
else
2023-08-22 18:34:29 +00:00
warn "Unsealing TOTP/HOTP secret from previous sealed measurements failed"
2023-08-31 16:07:39 +00:00
warn 'Try "Generate new HOTP/TOTP secret" option if you updated firmware content'
2018-06-19 19:27:27 +00:00
fi
}
2018-02-20 23:35:37 +00:00
2021-09-22 17:17:04 +00:00
update_totp()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-27 19:22:45 +00:00
# update the TOTP code
2022-03-23 20:02:59 +00:00
date=`date "+%Y-%m-%d %H:%M:%S %Z"`
2023-07-10 15:53:32 +00:00
tries=0
tpm2-tools: Change sense of CONFIG_TPM to mean any TPM, not just TPM1.
Most logic throughout Heads doesn't need to know TPM1 versus TPM2 (and
shouldn't, the differences should be localized). Some checks were
incorrect and are fixed by this change. Most checks are now unchanged
relative to master.
There are not that many places outside of tpmr that need to
differentiate TPM1 and TPM2. Some of those are duplicate code that
should be consolidated (seal-hotpkey, unseal-totp, unseal-hotp), and
some more are probably good candidates for abstracting in tpmr so the
business logic doesn't have to know TPM1 vs. TPM2.
Previously, CONFIG_TPM could be variously 'y', 'n', or empty. Now it
is always 'y' or 'n', and 'y' means "any TPM". Board configs are
unchanged, setting CONFIG_TPM2_TOOLS=y implies CONFIG_TPM=y so this
doesn't have to be duplicated and can't be mistakenly mismatched.
There were a few checks for CONFIG_TPM = n that only coincidentally
worked for TPM2 because CONFIG_TPM was empty (not 'n'). This test is
now OK, but the checks were also cleaned up to '!= "y"' for robustness.
Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm>
2023-02-22 21:30:07 +00:00
if [ "$CONFIG_TPM" != "y" ]; then
2021-09-22 17:17:04 +00:00
TOTP="NO TPM"
2021-09-27 19:22:45 +00:00
else
2021-09-22 17:17:04 +00:00
TOTP=`unseal-totp`
2023-07-10 15:53:32 +00:00
# On platforms using CONFIG_BOOT_EXTRA_TTYS multiple processes may try to
# access TPM at the same time, failing with EBUSY. The order of execution
# is unpredictable, so the error may appear on main console, secondary one,
# or neither of them if the calls are sufficiently staggered. Try up to
# three times (including previous one) with small delays in case of error,
# instead of immediately scaring users with "you've been pwned" message.
while [ $? -ne 0 ] && [ $tries -lt 2 ]; do
sleep 0.5
((tries++))
TOTP=`unseal-totp`
done
2021-09-22 17:17:04 +00:00
if [ $? -ne 0 ]; then
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=$BG_COLOR_ERROR
2022-11-23 18:55:09 +00:00
if [ "$skip_to_menu" = "true" ]; then
return 1 # Already asked to skip to menu from a prior error
fi
2022-08-25 18:43:31 +00:00
2023-03-10 20:39:54 +00:00
DEBUG "CONFIG_TPM: $CONFIG_TPM"
DEBUG "CONFIG_TPM2_TOOLS: $CONFIG_TPM2_TOOLS"
DEBUG "Show PCRs"
DEBUG "$(pcrs)"
2022-08-25 18:43:31 +00:00
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_ERROR --title "ERROR: TOTP Generation Failed!" \
2023-06-21 18:19:41 +00:00
--menu " ERROR: $CONFIG_BRAND_NAME couldn't generate the TOTP code.\n
2021-09-22 17:17:04 +00:00
If you have just completed a Factory Reset, or just reflashed
your BIOS, you should generate a new HOTP/TOTP secret.\n
If this is the first time the system has booted, you should
reset the TPM and set your own password.\n
If you have not just reflashed your BIOS, THIS COULD INDICATE TAMPERING!\n
2022-11-09 16:51:27 +00:00
How would you like to proceed?" 0 80 4 \
2021-09-22 17:17:04 +00:00
'g' ' Generate new HOTP/TOTP secret' \
'i' ' Ignore error and continue to main menu' \
'p' ' Reset the TPM' \
'x' ' Exit to recovery shell' \
2>/tmp/whiptail || recovery "GUI menu failed"
2021-09-23 17:07:28 +00:00
option=$(cat /tmp/whiptail)
case "$option" in
g )
2021-09-23 17:56:17 +00:00
if (whiptail $BG_COLOR_WARNING --title 'Generate new TOTP/HOTP secret' \
2022-11-09 16:51:27 +00:00
--yesno "This will erase your old secret and replace it with a new one!\n\nDo you want to proceed?" 0 80) then
2023-10-23 19:18:28 +00:00
generate_totp_hotp && update_totp && BG_COLOR_MAIN_MENU="" && reseal_tpm_disk_decryption_key
2021-09-23 17:07:28 +00:00
fi
;;
i )
2022-11-23 18:55:09 +00:00
skip_to_menu="true"
2021-09-23 17:07:28 +00:00
return 1
;;
p )
2023-10-23 19:18:28 +00:00
reset_tpm && update_totp && BG_COLOR_MAIN_MENU="" && reseal_tpm_disk_decryption_key
2021-09-23 17:07:28 +00:00
;;
x )
recovery "User requested recovery shell"
;;
esac
2021-09-22 17:17:04 +00:00
fi
fi
}
update_hotp()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-22 17:17:04 +00:00
if [ -x /bin/hotp_verification ]; then
HOTP=`unseal-hotp`
if ! hotp_verification info ; then
2022-11-23 18:55:09 +00:00
if [ "$skip_to_menu" = "true" ]; then
return 1 # Already asked to skip to menu from a prior error
fi
2022-11-15 20:11:58 +00:00
if ! whiptail $BG_COLOR_WARNING \
2021-09-27 19:21:38 +00:00
--title "WARNING: Please Insert Your $HOTPKEY_BRANDING" \
--yes-button "Retry" --no-button "Skip" \
2022-11-09 16:51:27 +00:00
--yesno "Your $HOTPKEY_BRANDING was not detected.\n\nPlease insert your $HOTPKEY_BRANDING" 0 80 ; then
2021-09-27 19:21:38 +00:00
HOTP="Error checking code, Insert $HOTPKEY_BRANDING and retry"
BG_COLOR_MAIN_MENU=$BG_COLOR_WARNING
return
2021-09-22 17:17:04 +00:00
fi
2021-09-27 19:21:38 +00:00
fi
2021-09-22 17:17:04 +00:00
# Don't output HOTP codes to screen, so as to make replay attacks harder
2022-11-23 18:55:09 +00:00
hotp_verification check "$HOTP"
2021-09-22 17:17:04 +00:00
case "$?" in
0 )
HOTP="Success"
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=""
2021-09-22 17:17:04 +00:00
;;
2022-11-23 18:55:09 +00:00
4|7 ) # 4: code was incorrect, 7: code was not a valid HOTP code at all
2021-09-22 17:17:04 +00:00
HOTP="Invalid code"
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=$BG_COLOR_ERROR
2021-09-22 17:17:04 +00:00
;;
* )
HOTP="Error checking code, Insert $HOTPKEY_BRANDING and retry"
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=$BG_COLOR_WARNING
2021-09-22 17:17:04 +00:00
;;
esac
else
HOTP='N/A'
fi
2019-11-25 18:25:11 +00:00
if [[ "$CONFIG_TPM" = n && "$HOTP" = "Invalid code" ]]; then
whiptail $BG_COLOR_ERROR --title "ERROR: HOTP Validation Failed!" \
2023-06-21 18:19:41 +00:00
--menu "ERROR: $CONFIG_BRAND_NAME couldn't validate the HOTP code.\n\nIf you just reflashed your BIOS, you should generate a new TOTP/HOTP secret.\n\nIf you have not just reflashed your BIOS, THIS COULD INDICATE TAMPERING!\n\nHow would you like to proceed?" 0 80 4 \
2019-11-25 18:25:11 +00:00
'g' ' Generate new TOTP/HOTP secret' \
'i' ' Ignore error and continue to main menu' \
'x' ' Exit to recovery shell' \
2>/tmp/whiptail || recovery "GUI menu failed"
option=$(cat /tmp/whiptail)
case "$option" in
g )
if (whiptail $BG_COLOR_WARNING --title 'Generate new TOTP/HOTP secret' \
--yesno "This will erase your old secret and replace it with a new one!\n\nDo you want to proceed?" 0 80) then
2023-10-23 19:18:28 +00:00
generate_totp_hotp && BG_COLOR_MAIN_MENU="" && reseal_tpm_disk_decryption_key
2019-11-25 18:25:11 +00:00
fi
;;
i )
return 1
;;
x )
recovery "User requested recovery shell"
;;
esac
fi
2021-09-22 17:17:04 +00:00
}
2019-08-19 22:09:42 +00:00
clean_boot_check()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2019-08-19 22:09:42 +00:00
# assume /boot mounted
if ! grep -q /boot /proc/mounts ; then
return
fi
# check for any kexec files in /boot
kexec_files=`find /boot -name kexec*.txt`
[ ! -z "$kexec_files" ] && return
#check for GPG key in keyring
GPG_KEY_COUNT=`gpg -k 2>/dev/null | wc -l`
[ $GPG_KEY_COUNT -ne 0 ] && return
# check for USB security token
2022-03-03 23:42:28 +00:00
if [ -x /bin/hotp_verification ]; then
2020-02-19 22:27:57 +00:00
if ! gpg --card-status > /dev/null ; then
return
fi
2019-08-19 22:09:42 +00:00
fi
# OS is installed, no kexec files present, no GPG keys in keyring, security token present
# prompt user to run OEM factory reset
oem-factory-reset \
2022-03-10 14:55:08 +00:00
"Clean Boot Detected - Perform OEM Factory Reset / Re-Ownership?" "$BG_COLOR_WARNING"
2019-08-19 22:09:42 +00:00
}
2021-09-22 17:17:04 +00:00
check_gpg_key()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-22 17:17:04 +00:00
GPG_KEY_COUNT=`gpg -k 2>/dev/null | wc -l`
if [ $GPG_KEY_COUNT -eq 0 ]; then
2021-09-23 17:56:17 +00:00
BG_COLOR_MAIN_MENU=$BG_COLOR_ERROR
2022-11-23 18:55:09 +00:00
if [ "$skip_to_menu" = "true" ]; then
return 1 # Already asked to skip to menu from a prior error
fi
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_ERROR --title "ERROR: GPG keyring empty!" \
2023-06-21 18:19:41 +00:00
--menu "ERROR: $CONFIG_BRAND_NAME couldn't find any GPG keys in your keyring.\n\nIf this is the first time the system has booted,\nyou should add a public GPG key to the BIOS now.\n\nIf you just reflashed a new BIOS, you'll need to add at least one\npublic key to the keyring.\n\nIf you have not just reflashed your BIOS, THIS COULD INDICATE TAMPERING!\n\nHow would you like to proceed?" 0 80 4 \
2021-09-23 17:07:28 +00:00
'g' ' Add a GPG key to the running BIOS' \
2022-03-23 19:50:58 +00:00
'F' ' OEM Factory Reset / Re-Ownership' \
2021-09-22 17:17:04 +00:00
'i' ' Ignore error and continue to main menu' \
'x' ' Exit to recovery shell' \
2>/tmp/whiptail || recovery "GUI menu failed"
2021-09-23 17:07:28 +00:00
option=$(cat /tmp/whiptail)
case "$option" in
g )
2021-09-23 17:56:17 +00:00
gpg-gui.sh && BG_COLOR_MAIN_MENU=""
2021-09-23 17:07:28 +00:00
;;
i )
2022-11-23 18:55:09 +00:00
skip_to_menu="true"
2021-09-23 17:07:28 +00:00
return 1
;;
2022-03-23 19:50:58 +00:00
F )
oem-factory-reset
;;
2021-09-23 17:07:28 +00:00
x )
recovery "User requested recovery shell"
;;
esac
2021-09-22 17:17:04 +00:00
fi
}
prompt_auto_default_boot()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-22 17:17:04 +00:00
echo -e "\nHOTP verification success\n\n"
2022-11-09 19:00:53 +00:00
if pause_automatic_boot; then
2021-09-22 17:17:04 +00:00
echo -e "\n\nAttempting default boot...\n\n"
2021-09-23 17:07:28 +00:00
attempt_default_boot
2021-09-22 17:17:04 +00:00
fi
}
show_main_menu()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2022-03-23 20:02:59 +00:00
date=`date "+%Y-%m-%d %H:%M:%S %Z"`
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_MAIN_MENU --title "$MAIN_MENU_TITLE" \
2022-11-09 16:51:27 +00:00
--menu "$date\nTOTP: $TOTP | HOTP: $HOTP" 0 80 10 \
2021-09-23 17:07:28 +00:00
'd' ' Default boot' \
2021-09-22 17:17:04 +00:00
'r' ' Refresh TOTP/HOTP' \
2021-09-23 17:07:28 +00:00
'o' ' Options -->' \
's' ' System Info' \
'p' ' Power Off' \
2021-09-22 17:17:04 +00:00
2>/tmp/whiptail || recovery "GUI menu failed"
2021-09-23 17:07:28 +00:00
option=$(cat /tmp/whiptail)
case "$option" in
d )
attempt_default_boot
;;
r )
2023-03-09 18:28:04 +00:00
update_totp && update_hotp
2021-09-23 17:07:28 +00:00
;;
o )
show_options_menu
;;
s )
show_system_info
;;
p )
poweroff
;;
esac
2021-09-22 17:17:04 +00:00
}
show_options_menu()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2023-06-21 18:19:41 +00:00
whiptail $BG_COLOR_MAIN_MENU --title "$CONFIG_BRAND_NAME Options" \
2022-11-09 16:51:27 +00:00
--menu "" 0 80 10 \
2021-09-23 17:07:28 +00:00
'b' ' Boot Options -->' \
2021-09-22 17:17:04 +00:00
't' ' TPM/TOTP/HOTP Options -->' \
2021-09-23 17:07:28 +00:00
'u' ' Update checksums and sign all files in /boot' \
2021-09-22 17:17:04 +00:00
'c' ' Change configuration settings -->' \
'f' ' Flash/Update the BIOS -->' \
2021-09-23 17:07:28 +00:00
'g' ' GPG Options -->' \
2022-03-10 14:55:08 +00:00
'F' ' OEM Factory Reset / Re-Ownership -->' \
2021-09-03 21:20:46 +00:00
'C' ' Reencrypt LUKS container -->' \
'P' ' Change LUKS Disk Recovery Key passphrase ->' \
'R' ' Check/Update file hashes on root disk -->' \
2021-09-22 17:17:04 +00:00
'x' ' Exit to recovery shell' \
'r' ' <-- Return to main menu' \
2>/tmp/whiptail || recovery "GUI menu failed"
2021-09-23 17:07:28 +00:00
option=$(cat /tmp/whiptail)
case "$option" in
b )
show_boot_options_menu
;;
t )
show_tpm_totp_hotp_options_menu
;;
u )
prompt_update_checksums
;;
c )
config-gui.sh
;;
f )
flash-gui.sh
;;
g )
gpg-gui.sh
;;
F )
oem-factory-reset
;;
2021-09-03 21:20:46 +00:00
C )
2022-03-23 19:50:58 +00:00
luks_reencrypt
luks_secrets_cleanup
;;
2021-09-03 21:20:46 +00:00
P )
2022-03-23 19:50:58 +00:00
luks_change_passphrase
luks_secrets_cleanup
;;
2021-09-03 21:20:46 +00:00
R )
root-hashes-gui.sh
;;
2021-09-23 17:07:28 +00:00
x )
recovery "User requested recovery shell"
;;
r )
;;
esac
2021-09-22 17:17:04 +00:00
}
show_boot_options_menu()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_MAIN_MENU --title "Boot Options" \
2022-11-09 16:51:27 +00:00
--menu "Select A Boot Option" 0 80 10 \
2021-09-22 17:17:04 +00:00
'm' ' Show OS boot menu' \
'u' ' USB boot' \
'i' ' Ignore tampering and force a boot (Unsafe!)' \
'r' ' <-- Return to main menu' \
2>/tmp/whiptail || recovery "GUI menu failed"
2021-09-23 17:07:28 +00:00
option=$(cat /tmp/whiptail)
case "$option" in
m )
# select a kernel from the menu
select_os_boot_option
;;
u )
exec /bin/usb-init
;;
i )
force_unsafe_boot
;;
r )
;;
esac
2021-09-22 17:17:04 +00:00
}
show_tpm_totp_hotp_options_menu()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_MAIN_MENU --title "TPM/TOTP/HOTP Options" \
2022-11-09 16:51:27 +00:00
--menu "Select An Option" 0 80 10 \
2021-09-22 17:17:04 +00:00
'g' ' Generate new TOTP/HOTP secret' \
2021-09-23 17:07:28 +00:00
'r' ' Reset the TPM' \
't' ' TOTP/HOTP does not match after refresh, troubleshoot' \
'm' ' <-- Return to main menu' \
2021-09-22 17:17:04 +00:00
2>/tmp/whiptail || recovery "GUI menu failed"
2021-09-23 17:07:28 +00:00
option=$(cat /tmp/whiptail)
case "$option" in
g )
2023-10-23 19:18:28 +00:00
generate_totp_hotp && reseal_tpm_disk_decryption_key
2021-09-23 17:07:28 +00:00
;;
r )
2023-10-23 19:18:28 +00:00
reset_tpm && reseal_tpm_disk_decryption_key
2021-09-23 17:07:28 +00:00
;;
t )
prompt_totp_mismatch
;;
m )
;;
esac
2021-09-22 17:17:04 +00:00
}
prompt_totp_mismatch()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-22 17:17:04 +00:00
if (whiptail $BG_COLOR_WARNING --title "TOTP/HOTP code mismatched" \
2023-01-04 21:07:38 +00:00
--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
2021-09-22 17:17:04 +00:00
echo ""
2023-01-04 21:07:38 +00:00
echo "To correct clock drift: 'date -s yyyy-MM-DD hh:mm:ss' in UTC timezone"
2021-09-22 17:17:04 +00:00
echo "and save it to the RTC: 'hwclock -w'"
2023-01-04 21:07:38 +00:00
echo ""
echo "Alternatively you could do this automatically with an Ethernet cable connected to a functional network: 'network-init-recovery'"
echo ""
echo "Then reboot and try again"
2021-09-22 17:17:04 +00:00
echo ""
recovery "TOTP/HOTP mismatch"
fi
}
reset_tpm()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
tpm2-tools: Change sense of CONFIG_TPM to mean any TPM, not just TPM1.
Most logic throughout Heads doesn't need to know TPM1 versus TPM2 (and
shouldn't, the differences should be localized). Some checks were
incorrect and are fixed by this change. Most checks are now unchanged
relative to master.
There are not that many places outside of tpmr that need to
differentiate TPM1 and TPM2. Some of those are duplicate code that
should be consolidated (seal-hotpkey, unseal-totp, unseal-hotp), and
some more are probably good candidates for abstracting in tpmr so the
business logic doesn't have to know TPM1 vs. TPM2.
Previously, CONFIG_TPM could be variously 'y', 'n', or empty. Now it
is always 'y' or 'n', and 'y' means "any TPM". Board configs are
unchanged, setting CONFIG_TPM2_TOOLS=y implies CONFIG_TPM=y so this
doesn't have to be duplicated and can't be mistakenly mismatched.
There were a few checks for CONFIG_TPM = n that only coincidentally
worked for TPM2 because CONFIG_TPM was empty (not 'n'). This test is
now OK, but the checks were also cleaned up to '!= "y"' for robustness.
Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm>
2023-02-22 21:30:07 +00:00
if [ "$CONFIG_TPM" = "y" ]; then
2021-09-23 17:56:17 +00:00
if (whiptail $BG_COLOR_WARNING --title 'Reset the TPM' \
2022-11-09 16:51:27 +00:00
--yesno "This will clear the TPM and TPM password, replace them with new ones!\n\nDo you want to proceed?" 0 80) then
2023-03-10 20:36:24 +00:00
if ! prompt_new_owner_password; then
echo "Press Enter to return to the menu..."
read
echo
return 1
fi
2023-10-24 15:19:49 +00:00
tpmr reset "$tpm_owner_password"
2021-09-22 17:17:04 +00:00
# now that the TPM is reset, remove invalid TPM counter files
mount_boot
mount -o rw,remount /boot
2023-08-22 18:34:29 +00:00
warn "Removing rollback and primary handle hash under /boot"
2021-09-22 17:17:04 +00:00
rm -f /boot/kexec_rollback.txt
2023-02-21 22:16:11 +00:00
rm -f /boot/kexec_primhdl_hash.txt
2021-09-22 17:17:04 +00:00
# create Heads TPM counter before any others
2023-10-24 15:19:49 +00:00
check_tpm_counter /boot/kexec_rollback.txt "" "$tpm_owner_password" \
2021-09-22 17:17:04 +00:00
|| die "Unable to find/create tpm counter"
counter="$TPM_COUNTER"
increment_tpm_counter $counter \
|| die "Unable to increment tpm counter"
sha256sum /tmp/counter-$counter > /boot/kexec_rollback.txt \
|| die "Unable to create rollback file"
mount -o ro,remount /boot
2023-10-24 15:19:49 +00:00
generate_totp_hotp "$tpm_owner_password"
2021-09-22 17:17:04 +00:00
else
echo "Returning to the main menu"
fi
else
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_ERROR --title 'ERROR: No TPM Detected' --msgbox "This device does not have a TPM.\n\nPress OK to return to the Main Menu" 0 80
2021-09-22 17:17:04 +00:00
fi
}
select_os_boot_option()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-22 17:17:04 +00:00
mount_boot
if verify_global_hashes ; then
kexec-select-boot -m -b /boot -c "grub.cfg" -g
fi
}
attempt_default_boot()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-22 17:17:04 +00:00
mount_boot
if ! verify_global_hashes; then
return
fi
DEFAULT_FILE=`find /boot/kexec_default.*.txt 2>/dev/null | head -1`
if [ -r "$DEFAULT_FILE" ]; then
kexec-select-boot -b /boot -c "grub.cfg" -g \
|| recovery "Failed default boot"
2021-09-23 17:56:17 +00:00
elif (whiptail $BG_COLOR_WARNING --title 'No Default Boot Option Configured' \
2022-11-09 16:51:27 +00:00
--yesno "There is no default boot option configured yet.\nWould you like to load a menu of boot options?\nOtherwise you will return to the main menu." 0 80) then
2021-09-22 17:17:04 +00:00
kexec-select-boot -m -b /boot -c "grub.cfg" -g
fi
}
force_unsafe_boot()
{
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2022-10-25 22:09:15 +00:00
if [ "$CONFIG_RESTRICTED_BOOT" = y ]; then
2023-06-30 18:21:11 +00:00
whiptail $BG_COLOR_ERROR --title 'ERROR: Restricted Boot Enabled' --msgbox "Restricted Boot is Enabled, forced boot not allowed.\n\nPress OK to return to the Main Menu" 0 80
2022-10-25 22:09:15 +00:00
return
fi
2021-09-22 17:17:04 +00:00
# Run the menu selection in "force" mode, bypassing hash checks
if (whiptail $BG_COLOR_WARNING --title 'Unsafe Forced Boot Selected!' \
2022-11-09 16:51:27 +00:00
--yesno "WARNING: You have chosen to skip all tamper checks and boot anyway.\n\nThis is an unsafe option!\n\nDo you want to proceed?" 0 80) then
2021-09-22 17:17:04 +00:00
mount_boot && kexec-select-boot -m -b /boot -c "grub.cfg" -g -f
fi
}
# gui-init start
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2021-09-22 17:17:04 +00:00
2021-09-23 18:20:25 +00:00
# Use stored HOTP key branding
if [ -r /boot/kexec_hotp_key ]; then
HOTPKEY_BRANDING="$(cat /boot/kexec_hotp_key)"
else
HOTPKEY_BRANDING="HOTP USB Security Dongle"
fi
2022-03-03 23:42:28 +00:00
if [ -x /bin/hotp_verification ]; then
2022-03-01 02:03:26 +00:00
enable_usb
fi
2019-08-19 22:09:42 +00:00
if detect_boot_device ; then
# /boot device with installed OS found
clean_boot_check
else
# can't determine /boot device or no OS installed,
# so fall back to interactive selection
mount_boot
fi
2019-05-27 16:45:09 +00:00
2021-09-23 17:13:40 +00:00
# detect whether any GPG keys exist in the keyring, if not, initialize that first
2022-11-23 18:55:09 +00:00
check_gpg_key
# Even if GPG init fails, still try to update TOTP/HOTP so the main menu can
# show the correct status.
update_totp
update_hotp
2018-02-23 20:13:21 +00:00
2023-02-18 20:52:32 +00:00
if [ "$HOTP" = "Success" -a -n "$CONFIG_AUTO_BOOT_TIMEOUT" ]; then
2021-09-23 17:13:40 +00:00
prompt_auto_default_boot
fi
2018-02-20 23:35:37 +00:00
2021-09-23 17:13:40 +00:00
while true; do
2024-02-01 19:30:31 +00:00
TRACE_FUNC
2022-11-23 18:55:09 +00:00
skip_to_menu="false"
2021-09-23 17:07:28 +00:00
show_main_menu
2018-02-20 23:35:37 +00:00
done
recovery "Something failed during boot"