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 21:42:05 +00:00
# A brand can override the extension used for update packages if desired
UPDATE_PKG_EXT = " ${ CONFIG_BRAND_UPDATE_PKG_EXT :- zip } "
# 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-13 21:42:05 +00:00
--yesno " You will need to insert a USB drive containing your BIOS image (*. $UPDATE_PKG_EXT , *.rom, or *.tgz).\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-13 21:42:05 +00:00
find /media ! -path '*/\.*' -type f \( -name '*.rom' -o -name '*.tgz' -o -type f -name " *. $UPDATE_PKG_EXT " \) | sort >/tmp/filelist.txt
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?
if [ -z " ${ PKG_FILE ##*. $UPDATE_PKG_EXT } " ] ; then
# 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.
if ! PACKAGE_ROM = " $( single_glob " $PKG_EXTRACT / " *.rom) " ; then
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
# 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-13 21:42:05 +00:00
--yesno " You have provided a *.rom 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 *. $UPDATE_PKG_EXT 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
exit 1
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