2023-03-07 15:10:32 +00:00
#!/bin/ash
2018-05-11 19:27:50 +00:00
#
2023-03-07 15:10:32 +00:00
# NOTE: This script is used on legacy-flash boards and runs with busybox ash,
# not bash
2018-05-11 19:27:50 +00:00
set -e -o pipefail
2023-03-07 15:10:32 +00:00
. /etc/ash_functions
2018-12-06 23:24:28 +00:00
. /tmp/config
2018-05-11 19:27:50 +00:00
2023-06-23 16:21:49 +00:00
echo
2023-02-20 16:01:17 +00:00
TRACE "Under /bin/flash.sh"
2023-02-18 17:58:43 +00:00
2024-09-01 19:02:42 +00:00
case " $CONFIG_FLASH_OPTIONS " in
"" )
2024-09-11 01:12:56 +00:00
die "ERROR: No flash options have been configured!\n\nEach board requires specific CONFIG_FLASH_OPTIONS options configured. It's unsafe to flash without them.\n\nAborting."
2018-05-11 21:08:31 +00:00
; ;
2018-05-11 19:27:50 +00:00
* )
2024-09-01 19:02:42 +00:00
DEBUG " Flash options detected: $CONFIG_FLASH_OPTIONS "
echo " Board $CONFIG_BOARD detected with flash options configured. Continuing... "
2018-05-11 19:27:50 +00:00
; ;
esac
flash_rom( ) {
ROM = $1
2018-05-15 23:24:24 +00:00
if [ " $READ " -eq 1 ] ; then
2024-09-01 19:02:42 +00:00
$CONFIG_FLASH_OPTIONS -r " ${ ROM } " \
2024-09-01 22:44:52 +00:00
|| recovery " Backup to $ROM failed "
2018-05-15 23:24:24 +00:00
else
cp " $ROM " /tmp/${ CONFIG_BOARD } .rom
sha256sum /tmp/${ CONFIG_BOARD } .rom
if [ " $CLEAN " -eq 0 ] ; then
preserve_rom /tmp/${ CONFIG_BOARD } .rom \
2024-09-01 22:44:52 +00:00
|| recovery " $ROM : Config preservation failed "
2018-05-15 23:24:24 +00:00
fi
2019-08-01 03:53:04 +00:00
# persist serial number from CBFS
2022-10-09 21:24:16 +00:00
if cbfs.sh -r serial_number > /tmp/serial 2>/dev/null; then
2019-08-01 03:53:04 +00:00
echo "Persisting system serial"
2022-10-09 21:24:16 +00:00
cbfs.sh -o /tmp/${ CONFIG_BOARD } .rom -d serial_number 2>/dev/null || true
cbfs.sh -o /tmp/${ CONFIG_BOARD } .rom -a serial_number -f /tmp/serial
2019-08-01 03:53:04 +00:00
fi
2020-10-18 18:48:25 +00:00
# persist PCHSTRP9 from flash descriptor
if [ " $CONFIG_BOARD " = "librem_l1um" ] ; then
echo "Persisting PCHSTRP9"
2024-09-01 19:02:42 +00:00
$CONFIG_FLASH_OPTIONS -r /tmp/ifd.bin --ifd -i fd >/dev/null 2>& 1 \
2020-10-18 18:48:25 +00:00
|| die "Failed to read flash descriptor"
dd if = /tmp/ifd.bin bs = 1 count = 4 skip = 292 of = /tmp/pchstrp9.bin >/dev/null 2>& 1
dd if = /tmp/pchstrp9.bin bs = 1 count = 4 seek = 292 of = /tmp/${ CONFIG_BOARD } .rom conv = notrunc >/dev/null 2>& 1
fi
2018-05-11 19:27:50 +00:00
2024-08-24 16:49:10 +00:00
warn "Do not power off computer. Updating firmware, this will take a few minutes"
2024-09-01 19:02:42 +00:00
$CONFIG_FLASH_OPTIONS -w /tmp/${ CONFIG_BOARD } .rom 2>& 1 \
2024-08-24 16:49:10 +00:00
|| recovery " $ROM : Flash failed "
2018-05-15 23:24:24 +00:00
fi
2018-05-11 19:27:50 +00:00
}
2018-05-15 23:24:24 +00:00
if [ " $1 " = = "-c" ] ; then
2018-05-11 21:08:31 +00:00
CLEAN = 1
2018-05-15 23:24:24 +00:00
READ = 0
ROM = " $2 "
elif [ " $1 " = = "-r" ] ; then
CLEAN = 0
READ = 1
2018-05-11 21:08:31 +00:00
ROM = " $2 "
2018-05-15 23:24:24 +00:00
touch $ROM
2018-05-11 19:27:50 +00:00
else
2018-05-11 21:08:31 +00:00
CLEAN = 0
2018-05-15 23:24:24 +00:00
READ = 0
2018-05-11 21:08:31 +00:00
ROM = " $1 "
fi
if [ ! -e " $ROM " ] ; then
2022-11-05 22:13:05 +00:00
die " Usage: $0 [-c|-r] <path/to/image.(rom|tgz)> "
fi
if [ " $READ " -eq 0 ] && [ " ${ ROM ##*. } " = tgz ] ; then
if [ " ${ CONFIG_BOARD %_* } " = talos-2 ] ; then
rm -rf /tmp/verified_rom
mkdir /tmp/verified_rom
2022-11-14 23:05:32 +00:00
tar -C /tmp/verified_rom -xf $ROM || die " Rom archive $ROM could not be extracted "
2023-11-15 15:00:37 +00:00
if ! ( cd /tmp/verified_rom/ && sha256sum -cs sha256sum.txt) ; then
2022-11-05 22:13:05 +00:00
die "Provided tgz image did not pass hash verification"
fi
echo "Reading current flash and building an update image"
2024-09-01 19:02:42 +00:00
$CONFIG_FLASH_OPTIONS -r /tmp/flash.sh.bak \
2024-09-01 22:44:52 +00:00
|| recovery "Read of flash has failed"
2022-11-05 22:13:05 +00:00
# ROM and bootblock already have ECC
bootblock = $( echo /tmp/verified_rom/*.bootblock)
rom = $( echo /tmp/verified_rom/*.rom)
kernel = $( echo /tmp/verified_rom/*-zImage.bundled)
pnor /tmp/flash.sh.bak -aw HBB < $bootblock
pnor /tmp/flash.sh.bak -aw HBI < $rom
pnor /tmp/flash.sh.bak -aw BOOTKERNEL < $kernel
rm -rf /tmp/verified_rom
ROM = /tmp/flash.sh.bak
else
die " $CONFIG_BOARD doesn't support tgz image format "
fi
2018-05-11 19:27:50 +00:00
fi
2018-05-11 21:08:31 +00:00
flash_rom $ROM
2022-11-05 22:13:05 +00:00
# don't leave temporary files lying around
rm -f /tmp/flash.sh.bak
2018-05-11 19:27:50 +00:00
exit 0