2023-03-07 15:10:32 +00:00
|
|
|
#!/bin/ash
|
2018-05-11 19:27:50 +00:00
|
|
|
#
|
2018-05-11 21:08:31 +00:00
|
|
|
# based off of flashrom-x230
|
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
|
|
|
|
2020-02-19 21:33:43 +00:00
|
|
|
case "$CONFIG_FLASHROM_OPTIONS" in
|
2020-02-19 17:04:56 +00:00
|
|
|
-* )
|
|
|
|
echo "Board $CONFIG_BOARD detected, continuing..."
|
2018-05-11 21:08:31 +00:00
|
|
|
;;
|
2018-05-11 19:27:50 +00:00
|
|
|
* )
|
2018-05-11 21:08:31 +00:00
|
|
|
die "ERROR: No board has been configured!\n\nEach board requires specific flashrom options and it's unsafe to flash without them.\n\nAborting."
|
2018-05-11 19:27:50 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2019-12-24 04:49:54 +00:00
|
|
|
flashrom_progress() {
|
2023-08-31 21:11:45 +00:00
|
|
|
# The ichspi programmer now spews register status lines constantly that are
|
|
|
|
# brutally slow to feed through the parser in flashrom_progress_tokenize.
|
|
|
|
# Filter the input with grep for only lines containing at least one token
|
|
|
|
# that we care about.
|
|
|
|
grep -E -e 'contents\.\.\.' -e 'done\.' -e '0x[0-9a-f]+-(0x[0-9a-f]+):' \
|
|
|
|
-e 'identical' -e 'VERIFIED\.' -e 'FAILED' | \
|
|
|
|
tr ' ' '\n' | flashrom_progress_tokenize "$1"
|
2023-06-23 16:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print_flashing_progress() {
|
|
|
|
local spaces=' '
|
|
|
|
local hashes='##################################################'
|
|
|
|
local percent pct1 pct2 progressbar progressbar2
|
|
|
|
percent="$1"
|
|
|
|
pct1=$((percent / 2))
|
|
|
|
pct2=$((50 - percent / 2))
|
|
|
|
progressbar=${hashes:0:$pct1}
|
|
|
|
progressbar2=${spaces:0:$pct2}
|
|
|
|
echo -ne "Flashing: [${progressbar}${spin:$spin_idx:1}${progressbar2}] (${percent}%)\\r"
|
|
|
|
}
|
|
|
|
|
|
|
|
flashrom_progress_tokenize() {
|
2019-12-24 04:49:54 +00:00
|
|
|
local current=0
|
2023-06-23 16:21:49 +00:00
|
|
|
local total_bytes="$1"
|
2019-12-24 04:49:54 +00:00
|
|
|
local percent=0
|
|
|
|
local IN=''
|
|
|
|
local spin='-\|/'
|
|
|
|
local spin_idx=0
|
|
|
|
local status='init'
|
|
|
|
local prev_word=''
|
|
|
|
local prev_prev_word=''
|
|
|
|
|
2023-06-23 16:21:49 +00:00
|
|
|
echo "Initializing Flash Programmer"
|
2019-12-24 04:49:54 +00:00
|
|
|
while true ; do
|
|
|
|
prev_prev_word=$prev_word
|
|
|
|
prev_word=$IN
|
2023-08-31 21:11:45 +00:00
|
|
|
IFS= read -r -t 0.2 IN
|
2023-06-23 16:21:49 +00:00
|
|
|
spin_idx=$(( (spin_idx+1) %4 ))
|
2019-12-24 04:49:54 +00:00
|
|
|
if [ "$status" == "init" ]; then
|
|
|
|
if [ "$IN" == "contents..." ]; then
|
|
|
|
status="reading"
|
|
|
|
echo "Reading old flash contents. Please wait..."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ "$status" == "reading" ]; then
|
|
|
|
if echo "${IN}" | grep "done." > /dev/null ; then
|
|
|
|
status="writing"
|
2023-06-23 16:21:49 +00:00
|
|
|
IN=
|
2019-12-24 04:49:54 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ "$status" == "writing" ]; then
|
2023-06-23 16:21:49 +00:00
|
|
|
# walk_eraseblocks() prints info for each block, of the form
|
|
|
|
# , 0xAAAAAA-0xBBBBBB:X
|
|
|
|
# The 'X' is a char indicating the action, but the debug from actually erasing
|
|
|
|
# and writing is mixed into the output so it may be separated. It can also be
|
|
|
|
# interrupted occasionally, so only match a complete token.
|
|
|
|
current=$(echo "$IN" | sed -nE 's/^0x[0-9a-f]+-(0x[0-9a-f]+):.*$/\1/p')
|
|
|
|
if [ "$current" != "" ]; then
|
|
|
|
percent=$((100 * (current + 1) / total_bytes))
|
|
|
|
fi
|
|
|
|
print_flashing_progress "$percent"
|
|
|
|
if [ "$IN" == "done." ]; then
|
2019-12-24 04:49:54 +00:00
|
|
|
status="verifying"
|
2023-06-23 16:21:49 +00:00
|
|
|
IN=
|
|
|
|
print_flashing_progress 100
|
2019-12-24 04:49:54 +00:00
|
|
|
echo ""
|
|
|
|
echo "Verifying flash contents. Please wait..."
|
|
|
|
fi
|
2023-06-23 16:21:49 +00:00
|
|
|
# This appears before "Erase/write done."; skip the verifying state
|
|
|
|
if [ "$IN" == "identical" ]; then
|
2019-12-24 04:49:54 +00:00
|
|
|
status="done"
|
2023-06-23 16:21:49 +00:00
|
|
|
IN=
|
|
|
|
print_flashing_progress 100
|
2022-10-26 19:14:29 +00:00
|
|
|
echo ""
|
2019-12-24 04:49:54 +00:00
|
|
|
echo "The flash contents are identical to the image being flashed."
|
2022-10-26 19:14:29 +00:00
|
|
|
break
|
2019-12-24 04:49:54 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ "$status" == "verifying" ]; then
|
|
|
|
if echo "${IN}" | grep "VERIFIED." > /dev/null ; then
|
|
|
|
status="done"
|
|
|
|
echo "The flash contents were verified and the image was flashed correctly."
|
2022-10-26 19:14:29 +00:00
|
|
|
break
|
|
|
|
elif echo "${IN}" | grep "FAILED" > /dev/null ; then
|
|
|
|
echo 'Error while verifying flash content'
|
|
|
|
break
|
2019-12-24 04:49:54 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo ""
|
|
|
|
if [ "$status" == "done" ]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
echo 'Error flashing coreboot -- see timestampped flashrom log in /tmp for more info'
|
|
|
|
echo ""
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:27:50 +00:00
|
|
|
flash_rom() {
|
|
|
|
ROM=$1
|
2018-05-15 23:24:24 +00:00
|
|
|
if [ "$READ" -eq 1 ]; then
|
2022-10-26 19:14:29 +00:00
|
|
|
flashrom $CONFIG_FLASHROM_OPTIONS -r "${ROM}" \
|
|
|
|
|| die "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 \
|
|
|
|
|| die "$ROM: Config preservation failed"
|
|
|
|
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"
|
|
|
|
flashrom $CONFIG_FLASHROM_OPTIONS -r /tmp/ifd.bin --ifd -i fd >/dev/null 2>&1 \
|
|
|
|
|| 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
|
|
|
|
2020-02-19 21:33:43 +00:00
|
|
|
flashrom $CONFIG_FLASHROM_OPTIONS -w /tmp/${CONFIG_BOARD}.rom \
|
2023-06-23 16:21:49 +00:00
|
|
|
-V -o "/tmp/flashrom-$(date '+%Y%m%d-%H%M%S').log" 2>&1 | \
|
|
|
|
flashrom_progress "$(stat -c %s "/tmp/${CONFIG_BOARD}.rom")" \
|
2019-12-24 04:49:54 +00:00
|
|
|
|| die "$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"
|
2022-11-05 22:13:05 +00:00
|
|
|
if ! (cd /tmp/verified_rom/ && sha256sum -cs hashes.txt); then
|
|
|
|
die "Provided tgz image did not pass hash verification"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Reading current flash and building an update image"
|
|
|
|
flashrom $CONFIG_FLASHROM_OPTIONS -r /tmp/flash.sh.bak \
|
|
|
|
|| die "Read of flash has failed"
|
|
|
|
|
|
|
|
# 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
|