Skip to content

Commit

Permalink
flash.sh: UX and logic fixes
Browse files Browse the repository at this point in the history
- Have Talos II supported by detecting correctly size of mtd chip (not internal: different flashrom output needs to be parsed)
- Read backup content only once: 66% speedup (TOCTOU? Don't think so, nothing should happen in parallel when flashing insingle user mode)
- Have the main loop not break, but breaks in subcases (otherwise, verifying step was breaking)
- Have flashrom depend on latest commit of dasharo/flashrom
  - Latest flashrom version has native --progress flag.
  - Consquently, we do not have to output flashrom in verbose and use bashisms to produce a progress bar/pourcentages
  - There is a bug opened on flashrom master: --progress can give 0% at erase completion: https://ticket.coreboot.org/issues/390

NOTE: This breaks kgpe-d16 internal flashing of BMC, since AST1100 patch is not applied.

Addresses #1222 sub-issue (flashing fails because verifying steps fails as of now in master)
  • Loading branch information
tlaurion committed Oct 26, 2022
1 parent 0389eca commit f79100b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 45 deletions.
64 changes: 22 additions & 42 deletions initrd/bin/flash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ case "$CONFIG_FLASHROM_OPTIONS" in
esac

flashrom_progress() {
local current=0
local total_bytes=0
local percent=0
local IN=''
local spin='-\|/'
local spin_idx=0
local progressbar=''
local progressbar2=''
local status='init'
local prev_word=''
local prev_prev_word=''
Expand All @@ -33,30 +28,16 @@ flashrom_progress() {
while true ; do
prev_prev_word=$prev_word
prev_word=$IN
read -r -d' ' IN || break
read -r -d' ' IN
if [ "$total_bytes" != "0" ]; then
current=$(echo "$IN" | grep -E -o '0x[0-9a-f]+-0x[0-9a-f]+:.*' | grep -E -o "0x[0-9a-f]+" | tail -n 1)
if [ "${current}" != "" ]; then
percent=$((100 * (current + 1) / total_bytes))
pct1=$((percent / 2))
pct2=$((49 - percent / 2))
progressbar=$(for i in `seq $pct1 2>/dev/null` ; do echo -ne '#' ; done)
progressbar2=$(for i in `seq $pct2 2>/dev/null` ; do echo -ne ' ' ; done)
fi
percent=$(echo "$IN" |grep -E -o '[0-9]+%')
else
if [ "$prev_prev_word" == "Reading" ] && [ "$IN" == "bytes" ]; then
# flashrom may read the descriptor first, so ensure total_bytes is at least 4MB
if [[ $prev_word -gt 4194303 ]]; then
total_bytes=$prev_word
echo "Total flash size : $total_bytes bytes"
fi
if [ "$IN" == "kB," ]; then
# Previous is is total size in kB
total_bytes=$(echo "$prev_word" | grep -E -o '[0-9]+')
echo "Total flash size : $total_bytes kB"
fi
fi
if [ "$percent" -gt 99 ]; then
spin_idx=4
else
spin_idx=$(( (spin_idx+1) %4 ))
fi
if [ "$status" == "init" ]; then
if [ "$IN" == "contents..." ]; then
status="reading"
Expand All @@ -67,25 +48,34 @@ flashrom_progress() {
if echo "${IN}" | grep "done." > /dev/null ; then
status="writing"
fi
echo -ne "Reading: $percent\\r"
fi
if [ "$status" == "writing" ]; then
echo -ne "Flashing: [${progressbar}${spin:$spin_idx:1}${progressbar2}] (${percent}%)\\r"
if echo "$IN" | grep "Verifying" > /dev/null ; then
status="verifying"
echo ""
echo "Verifying flash contents. Please wait..."
fi
if echo "$IN" | grep "identical" > /dev/null ; then
status="done"
echo ""
echo ""
echo "The flash contents are identical to the image being flashed."
break
fi
echo ""
echo "Flashing ROM content. Please wait..."
echo -ne "Flashing: $percent\\r"
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."
break
elif echo "${IN}" | grep "FAILED" > /dev/null ; then
echo 'Error while verifying flash content'
break
fi
echo ""
echo "Verifying flash contents. Please wait..."
echo -ne "Verifying: $percent\\r"
fi
done
echo ""
Expand All @@ -101,18 +91,8 @@ flashrom_progress() {
flash_rom() {
ROM=$1
if [ "$READ" -eq 1 ]; then
flashrom $CONFIG_FLASHROM_OPTIONS -r "${ROM}.1" \
|| die "$ROM: Read failed"
flashrom $CONFIG_FLASHROM_OPTIONS -r "${ROM}.2" \
|| die "$ROM: Read failed"
flashrom $CONFIG_FLASHROM_OPTIONS -r "${ROM}.3" \
|| die "$ROM: Read failed"
if [ `sha256sum ${ROM}.[123] | cut -f1 -d ' ' | uniq | wc -l` -eq 1 ]; then
mv ${ROM}.1 $ROM
rm ${ROM}.[23]
else
die "$ROM: Read inconsistent"
fi
flashrom $CONFIG_FLASHROM_OPTIONS -r "${ROM}" \
|| die "Backup to $ROM failed"
else
cp "$ROM" /tmp/${CONFIG_BOARD}.rom
sha256sum /tmp/${CONFIG_BOARD}.rom
Expand All @@ -136,7 +116,7 @@ flash_rom() {
fi

flashrom $CONFIG_FLASHROM_OPTIONS -w /tmp/${CONFIG_BOARD}.rom \
-V -o "/tmp/flashrom-$(date '+%Y%m%d-%H%M%S').log" 2>&1 | flashrom_progress \
--progress -o "/tmp/flashrom-$(date '+%Y%m%d-%H%M%S').log" 2>&1 | flashrom_progress \
|| die "$ROM: Flash failed"
fi
}
Expand Down
6 changes: 3 additions & 3 deletions modules/flashrom
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ modules-$(CONFIG_FLASHROM) += flashrom

flashrom_depends := pciutils $(musl_dep)

flashrom_version := b1f858f65b2abd276542650d8cb9e382da258967
flashrom_version := 9bddf6233d8eda26b0cbc508745fc9a7738c9e73
flashrom_dir := flashrom-$(flashrom_version)
flashrom_tar := $(flashrom_dir).tar.gz
flashrom_url := https://github.com/flashrom/flashrom/archive/$(flashrom_version).tar.gz
flashrom_hash := 4873ad50f500629c244fc3fbee64b56403a82307d7f555dfa235336a200c336c
flashrom_url := https://github.com/Dasharo/flashrom/archive/$(flashrom_version).tar.gz
flashrom_hash := cad1c9d1585b425eca39c7badc9b7735fc8d729039d8d19db7bc9d85f1c32781

flashrom_target := \
$(MAKE_JOBS) \
Expand Down

0 comments on commit f79100b

Please sign in to comment.