2023-02-08 21:01:48 +00:00
#!/bin/bash
2018-05-11 21:08:31 +00:00
#
set -e -o pipefail
. /etc/functions
2020-07-10 17:58:32 +00:00
. /etc/gui_functions
2018-12-06 23:24:28 +00:00
. /tmp/config
2018-05-11 21:08:31 +00:00
2023-02-20 16:01:17 +00:00
TRACE "Under /bin/flash-gui.sh"
2023-02-18 17:58:43 +00:00
2022-10-25 22:09:15 +00:00
if [ " $CONFIG_RESTRICTED_BOOT " = y ] ; then
whiptail $BG_COLOR_ERROR --title 'Restricted Boot Active' \
2023-06-30 18:21:11 +00:00
--msgbox "Disable Restricted Boot to flash new firmware." 0 80
2022-10-25 22:09:15 +00:00
exit 1
fi
2023-11-13 22:17:07 +00:00
# Most boards use a .rom file as a "plain" update, contents of the BIOS flash
UPDATE_PLAIN_EXT = rom
# talos-2 uses a .tgz file for its "plain" update, contains other parts as well
# as its own integrity check. This isn't integrated with the "update package"
# workflow (as-is, a .tgz could be inside that package in theory) but more work
# would be needed to properly integrate it.
if [ " ${ CONFIG_BOARD %_* } " = talos-2 ] ; then
UPDATE_PLAIN_EXT = tgz
fi
2023-11-13 21:42:05 +00:00
# Check that a glob matches exactly one thing. If so, echoes the single value.
# Otherwise, fails. As always, do not quote the glob.
#
# E.g, locate a ROM with unknown version when only one should be present:
# if ROM_FILE="$(single_glob /media/heads-*.rom)"; then
# echo "ROM is $ROM_FILE"
# else
# echo "Failed to find a ROM" >&2
# fi
single_glob( ) {
if [ " $# " -eq 1 ] && [ -f " $1 " ] ; then
echo " $1 "
else
return 1
fi
}
2018-05-17 22:31:23 +00:00
while true; do
unset menu_choice
2022-11-15 20:11:58 +00:00
whiptail $BG_COLOR_MAIN_MENU --title "Firmware Management Menu" \
2022-11-09 16:51:27 +00:00
--menu "Select the firmware function to perform\n\nRetaining settings copies existing settings to the new firmware:\n* Keeps your GPG keyring\n* Keeps changes to the default /boot device\n\nErasing settings uses the new firmware as-is:\n* Erases any existing GPG keyring\n* Restores firmware to default factory settings\n* Clears out /boot signatures\n\nIf you are just updating your firmware, you probably want to retain\nyour settings." 0 80 10 \
2019-04-19 21:11:45 +00:00
'f' ' Flash the firmware with a new ROM, retain settings' \
'c' ' Flash the firmware with a new ROM, erase settings' \
2018-05-17 22:31:23 +00:00
'x' ' Exit' \
2>/tmp/whiptail || recovery "GUI menu failed"
menu_choice = $( cat /tmp/whiptail)
case " $menu_choice " in
2023-10-31 13:25:47 +00:00
"x" )
exit 0
2018-05-17 22:31:23 +00:00
; ;
2023-10-31 13:25:47 +00:00
f | c)
if ( whiptail $BG_COLOR_WARNING --title 'Flash the BIOS with a new ROM' \
2023-11-16 13:58:38 +00:00
--yesno " You will need to insert a USB drive containing your BIOS image (*.zip or\n*. $UPDATE_PLAIN_EXT ).\n\nAfter you select this file, this program will reflash your BIOS.\n\nDo you want to proceed? " 0 80) ; then
2023-10-31 13:25:47 +00:00
mount_usb
if grep -q /media /proc/mounts; then
2023-11-15 15:00:37 +00:00
if [ " ${ CONFIG_BOARD %_* } " = talos-2 ] ; then
find /media ! -path '*/\.*' -type f -name " *. $UPDATE_PLAIN_EXT " | sort >/tmp/filelist.txt
else
2023-11-16 13:58:38 +00:00
find /media ! -path '*/\.*' -type f \( -name " *. $UPDATE_PLAIN_EXT " -o -type f -name "*.zip" \) | sort >/tmp/filelist.txt
2023-11-15 15:00:37 +00:00
fi
2023-10-31 13:25:47 +00:00
file_selector "/tmp/filelist.txt" "Choose the ROM to flash"
if [ " $FILE " = = "" ] ; then
exit 1
else
2023-11-13 21:42:05 +00:00
PKG_FILE = $FILE
2023-10-31 13:25:47 +00:00
fi
2018-05-17 22:31:23 +00:00
2023-11-13 21:42:05 +00:00
# is an update package provided?
2023-11-16 13:58:38 +00:00
if [ -z " ${ PKG_FILE ##*.zip } " ] ; then
2023-11-13 21:42:05 +00:00
# Unzip the package
PKG_EXTRACT = "/tmp/flash_gui/update_package"
rm -rf " $PKG_EXTRACT "
mkdir -p " $PKG_EXTRACT "
# If extraction fails, delete everything and fall through to the
# integrity failure prompt. This is the most likely path if the ROM
# was actually corrupted in transit. Corrupting the ZIP in a way that
# still extracts is possible (the sha256sum detects this) but less
# likely.
unzip " $PKG_FILE " -d " $PKG_EXTRACT " || rm -rf " $PKG_EXTRACT "
# Older packages had /tmp/verified_rom hard-coded in the sha256sum.txt
# Remove that so it's a relative path to the ROM in the package.
# Ignore failure, if there is no sha256sum.txt the sha256sum will fail
sed -i -e 's| /tmp/verified_rom/\+| |g' " $PKG_EXTRACT /sha256sum.txt " || true
2023-10-31 13:25:47 +00:00
# check file integrity
2023-11-13 21:42:05 +00:00
if ! ( cd " $PKG_EXTRACT " && sha256sum -cs sha256sum.txt) ; then
2023-10-31 13:25:47 +00:00
whiptail --title 'ROM Integrity Check Failed! ' \
2023-11-13 21:42:05 +00:00
--msgbox " Integrity check failed in\n $PKG_FILE .\nDid not flash.\n\nPlease check your file (e.g. re-download).\n " 16 60
exit 1
fi
# The package must contain exactly one *.rom file, flash that.
2023-11-13 22:17:07 +00:00
if ! PACKAGE_ROM = " $( single_glob " $PKG_EXTRACT / " *." $UPDATE_PLAIN_EXT " ) " ; then
2023-11-13 21:42:05 +00:00
whiptail --title 'BIOS Image Not Found! ' \
--msgbox " A BIOS image was not found in\n $PKG_FILE .\n\nPlease check your file (e.g. re-download).\n " 16 60
exit 1
fi
if ! whiptail $BG_COLOR_WARNING --title 'Flash ROM?' \
--yesno " This will replace your current ROM with:\n\n ${ PKG_FILE # "/media/" } \n\nDo you want to proceed? " 0 80; then
exit 1
2021-12-11 14:33:02 +00:00
fi
2023-11-13 21:42:05 +00:00
# Continue on using the verified ROM
ROM = " $PACKAGE_ROM "
2023-10-31 13:25:47 +00:00
else
2023-11-15 15:00:37 +00:00
# talos-2 uses a .tgz file for its "plain" update, contains other parts as well, validated against hashes under flash.sh
# Skip prompt for hash validation for talos-2. Only method is through tgz or through bmc with individual parts
if [ " ${ CONFIG_BOARD %_* } " != talos-2 ] ; then
2023-10-31 13:25:47 +00:00
# a rom file was provided. exit if we shall not proceed
2023-11-13 21:42:05 +00:00
ROM = " $PKG_FILE "
2023-10-31 13:25:47 +00:00
ROM_HASH = $( sha256sum " $ROM " | awk '{print $1}' ) || die "Failed to hash ROM file"
if ! ( whiptail $CONFIG_ERROR_BG_COLOR --title 'Flash ROM without integrity check?' \
2023-11-16 13:58:38 +00:00
--yesno " You have provided a *. $UPDATE_PLAIN_EXT file. The integrity of the file can not be\nchecked automatically for this file type.\n\nROM: $ROM \nSHA256SUM: $ROM_HASH \n\nIf you do not know how to check the file integrity yourself,\nyou should use a *.zip file instead.\n\nIf the file is damaged, you will not be able to boot anymore.\nDo you want to proceed flashing without file integrity check? " 0 80) ; then
2023-11-13 21:42:05 +00:00
exit 1
2023-11-15 15:00:37 +00:00
fi
else
#We are on talos-2, so we have a tgz file. We will pass it directly to flash.sh which will take care of it
ROM = " $PKG_FILE "
2018-05-17 22:31:23 +00:00
fi
fi
2023-10-31 13:25:47 +00:00
if [ " $menu_choice " = = "c" ] ; then
/bin/flash.sh -c " $ROM "
# after flash, /boot signatures are now invalid so go ahead and clear them
if ls /boot/kexec* >/dev/null 2>& 1; then
(
mount -o remount,rw /boot 2>/dev/null
rm /boot/kexec* 2>/dev/null
mount -o remount,ro /boot 2>/dev/null
)
fi
else
/bin/flash.sh " $ROM "
fi
whiptail --title 'ROM Flashed Successfully' \
2023-11-13 21:42:05 +00:00
--msgbox " ${ PKG_FILE # "/media/" } \n\nhas been flashed successfully.\n\nPress Enter to reboot\n " 0 80
2023-10-31 13:25:47 +00:00
umount /media
/bin/reboot
2018-05-11 21:08:31 +00:00
fi
2023-10-31 13:25:47 +00:00
fi
2018-05-17 22:31:23 +00:00
; ;
esac
2018-05-11 21:08:31 +00:00
2018-05-17 22:31:23 +00:00
done
2018-05-11 21:08:31 +00:00
exit 0