2018-02-20 23:35:37 +00:00
#!/bin/sh
# Boot from a local disk installation
2018-02-21 23:58:54 +00:00
CONFIG_BOOT_GUI_MENU_NAME='Heads Boot Menu'
2018-02-20 23:35:37 +00:00
. /etc/functions
. /etc/config
mount_boot()
{
2018-02-23 20:13:21 +00:00
# Mount local disk if it is not already mounted
if ! grep -q /boot /proc/mounts ; then
mount -o ro /boot \
|| recovery "Unable to mount /boot"
fi
2018-02-20 23:35:37 +00:00
}
2018-02-21 23:58:54 +00:00
last_half=X
2018-02-20 23:35:37 +00:00
while true; do
2018-02-23 20:13:21 +00:00
MAIN_MENU_OPTIONS=""
2018-02-20 23:35:37 +00:00
unset totp_confirm
2018-02-23 20:13:21 +00:00
# update the TOTP code every thirty seconds
date=`date "+%Y-%m-%d %H:%M:%S"`
seconds=`date "+%s"`
half=`expr \( $seconds % 60 \) / 30`
if [ "$CONFIG_TPM" = n ]; then
TOTP="NO TPM"
elif [ "$half" != "$last_half" ]; then
last_half=$half;
TOTP=`unseal-totp`
if [ $? -ne 0 ]; then
whiptail --clear --title "ERROR: TOTP Generation Failed!" \
2018-03-09 00:36:56 +00:00
--menu "ERROR: Heads couldn't generate the TOTP code.\n\nIf you have just reflashed your BIOS, you will need to generate a new TOTP secret.\n\nIf you have not just reflashed your BIOS, THIS COULD INDICATE TAMPERING!\n\nIf this is the first time the system has booted, you should reset the TPM and set your own password\n\nHow would you like to proceed?" 30 80 4 \
2018-02-23 20:13:21 +00:00
'g' ' Generate new TOTP secret' \
'i' ' Ignore error and continue to default boot menu' \
2018-03-09 00:36:56 +00:00
'p' ' Reset the TPM' \
2018-02-23 20:13:21 +00:00
'x' ' Exit to recovery shell' \
2>/tmp/whiptail || recovery "GUI menu failed"
totp_confirm=$(cat /tmp/whiptail)
fi
fi
if [ "$totp_confirm" = "i" -o -z "$totp_confirm" ]; then
whiptail --clear --title "$CONFIG_BOOT_GUI_MENU_NAME" \
2018-03-09 00:36:56 +00:00
--menu "$date\nTOTP code: $TOTP" 20 80 10 \
2018-02-23 20:13:21 +00:00
'y' ' Default boot' \
'r' ' TOTP does not match, refresh code' \
'n' ' TOTP does not match after refresh, troubleshoot' \
2018-03-14 17:14:22 +00:00
'o' ' Other Boot Options -->' \
'a' ' Advanced Settings -->' \
'x' ' Exit to recovery shell' \
2>/tmp/whiptail || recovery "GUI menu failed"
totp_confirm=$(cat /tmp/whiptail)
fi
if [ "$totp_confirm" = "o" ]; then
whiptail --clear --title "Other Boot Options" \
--menu "Select A Boot Option" 20 80 10 \
2018-02-23 20:13:21 +00:00
'm' ' Show OS boot menu' \
'u' ' USB boot' \
2018-03-05 22:46:15 +00:00
'i' ' Ignore tampering and force a boot (Unsafe!)' \
2018-03-14 17:14:22 +00:00
'r' ' <-- Return to main menu' \
2>/tmp/whiptail || recovery "GUI menu failed"
totp_confirm=$(cat /tmp/whiptail)
fi
if [ "$totp_confirm" = "a" ]; then
whiptail --clear --title "Advanced Settings" \
--menu "Configure Advanced Settings" 20 80 10 \
'g' ' Generate new TOTP secret' \
2018-03-09 00:36:56 +00:00
'p' ' Reset the TPM' \
2018-03-14 17:14:22 +00:00
's' ' Update checksums and sign all files in /boot' \
'r' ' <-- Return to main menu' \
2018-02-23 20:13:21 +00:00
2>/tmp/whiptail || recovery "GUI menu failed"
totp_confirm=$(cat /tmp/whiptail)
fi
if [ "$totp_confirm" = "x" ]; then
recovery "User requested recovery shell"
fi
if [ "$totp_confirm" = "r" ]; then
continue
fi
if [ "$totp_confirm" = "n" ]; then
if (whiptail --title "TOTP code mismatched" \
2018-03-09 00:36:56 +00:00
--yesno "TOTP code mismatches could indicate either TPM tampering or clock drift:\n\nTo correct clock drift: 'date -s HH:MM:SS'\nand save it to the RTC: 'hwclock -w'\nthen reboot and try again.\n\nWould you like to exit to a recovery console?" 30 80) then
2018-02-23 20:13:21 +00:00
echo ""
echo "To correct clock drift: 'date -s HH:MM:SS'"
echo "and save it to the RTC: 'hwclock -w'"
echo "then reboot and try again"
echo ""
recovery "TOTP mismatch"
else
continue
fi
fi
if [ "$totp_confirm" = "u" ]; then
exec /bin/usb-init
continue
fi
if [ "$totp_confirm" = "g" ]; then
if (whiptail --title 'Generate new TOTP secret' \
2018-03-09 00:36:56 +00:00
--yesno "This will erase your old secret and replace it with a new one!\n\nDo you want to proceed?" 16 80) then
echo "Scan the QR code to add the new TOTP secret"
/bin/seal-totp
echo "Once you have scanned the QR code, hit Enter to reboot"
read
/bin/reboot
else
echo "Returning to the main menu"
fi
continue
fi
if [ "$totp_confirm" = "p" ]; then
if (whiptail --title 'Reset the TPM' \
--yesno "This will clear the TPM, erase the old TPM password and replace it with a new one!\n\nDo you want to proceed?" 16 80) then
/bin/tpm-reset
2018-02-21 23:58:54 +00:00
echo "Scan the QR code to add the new TOTP secret"
2018-02-23 20:13:21 +00:00
/bin/seal-totp
echo "Once you have scanned the QR code, hit Enter to reboot"
2018-02-21 23:58:54 +00:00
read
2018-02-23 20:13:21 +00:00
/bin/reboot
else
echo "Returning to the main menu"
fi
continue
fi
if [ "$totp_confirm" = "m" ]; then
# Try to select a kernel from the menu
mount_boot
kexec-select-boot -m -b /boot -c "grub.cfg" -g
continue
fi
2018-03-05 22:46:15 +00:00
if [ "$totp_confirm" = "i" ]; then
# Run the menu selection in "force" mode, bypassing hash checks
if (whiptail --title 'Unsafe Forced Boot Selected!' \
2018-03-09 00:36:56 +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?" 16 80) then
2018-03-05 22:46:15 +00:00
mount_boot
kexec-select-boot -m -b /boot -c "grub.cfg" -g -f
else
echo "Returning to the main menu"
fi
continue
fi
2018-03-14 17:14:22 +00:00
if [ "$totp_confirm" = "s" ]; then
if (whiptail --title 'Update Checksums and sign all files in /boot' \
--yesno "You have chosen to update the checksums and sign all of the files in /boot.\n\nThis means that you trust that the files in /boot have not been tampered with.\n\nYou will need your GPG key to continue and this change will modify your disk.\n\nDo you want to continue?" 16 80) then
mount_boot
mount -o rw,remount /boot
cd /boot
find ./ -type f ! -name '*kexec*' | xargs sha256sum > /boot/kexec_hashes.txt
DEFAULT_FILES=$(cat /boot/kexec_default_hashes.txt | cut -f3 -d ' ')
echo $DEFAULT_FILES | xargs sha256sum > /boot/kexec_default_hashes.txt
# sign and auto-roll config counter
extparam=
if [ "$CONFIG_TPM" = "y" ]; then
extparam=-u
fi
2018-03-20 18:26:09 +00:00
kexec-sign-config -p /boot $extparam
if [ $? -ne 0 ]; then
mount -o ro,remount /boot
whiptail --title 'ERROR: Signing Failed' \
--msgbox "The signing process failed!\n\nReturning to main menu." 16 60
else
# switch back to ro mode
mount -o ro,remount /boot
fi
2018-03-14 17:14:22 +00:00
fi
continue
fi
2018-02-23 20:13:21 +00:00
if [ "$totp_confirm" = "y" -o -n "$totp_confirm" ]; then
# Try to boot the default
mount_boot
DEFAULT_FILE=`find /boot/kexec_default.*.txt 2>/dev/null | head -1`
if [ -r "$DEFAULT_FILE" ]; then
2018-03-08 19:41:44 +00:00
kexec-select-boot -b /boot -c "grub.cfg" -g \
2018-02-23 20:13:21 +00:00
|| recovery "Failed default boot"
else
if (whiptail --title 'No Default Boot Option Configured' \
2018-03-09 00:36:56 +00:00
--yesno "There is no default boot option configured yet. Would you like to load a menu of boot options? Otherwise you will return to the main menu." 16 80) then
2018-02-23 20:13:21 +00:00
kexec-select-boot -m -b /boot -c "grub.cfg" -g
else
echo "Returning to the main menu"
fi
continue
fi
fi
2018-02-20 23:35:37 +00:00
done
recovery "Something failed during boot"