Skip to content

Commit 15e6c41

Browse files
authored
Merge pull request #130 from Dasharo/fd_first
Add flashrom job scheduling and flash fd first
2 parents e47d41c + 247164f commit 15e6c41

File tree

3 files changed

+185
-59
lines changed

3 files changed

+185
-59
lines changed

include/dts-functions.sh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ clear_line() {
4242
printf '\r\033[K'
4343
}
4444

45+
# Draws a simple progress bar
46+
# Example usage: draw_progress_bar "$((++TASK_NO))" "$TOTAL_TASKS"
47+
draw_progress_bar() {
48+
local current=$1
49+
local total=$2
50+
local BAR_WIDTH=67
51+
52+
# Clamp counter
53+
((current < 0)) && current=0
54+
((current > total)) && current=$total
55+
56+
# Calculate progress
57+
local filled=$((current * BAR_WIDTH / total))
58+
local empty=$((BAR_WIDTH - filled))
59+
60+
# Build bar
61+
local bar
62+
bar=$(printf "%0.s#" $(seq 1 $filled))
63+
if ((empty > 0)); then
64+
bar+=$(printf "%0.s " $(seq 1 $empty))
65+
fi
66+
67+
# Print with carriage return
68+
printf "\r[%s] %d/%d" "$bar" "$current" "$total"
69+
}
70+
4571
check_if_dasharo() {
4672
if [[ $BIOS_VENDOR == *$DASHARO_VENDOR* &&
4773
$BIOS_VERSION == *$DASHARO_NAME* ||
@@ -679,8 +705,7 @@ set_intel_regions_update_params() {
679705
FLASHROM_ADD_OPT_REGIONS+=" -i fd"
680706
else
681707
fd_me_locked="yes"
682-
print_error "The firmware binary to be flashed contains Flash Descriptor (FD), but FD is not writable!"
683-
print_warning "Proceeding without FD flashing, as it is not critical."
708+
print_warning "The firmware binary to be flashed contains Flash Descriptor (FD), but FD is not writable!"
684709
echo "The firmware binary contains Flash Descriptor (FD), but FD is not writable!" >>$ERR_LOG_FILE
685710
fi
686711
fi

reports/dasharo-hcl-report.sh

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,16 @@ source $DTS_FUNCS
1111
# shellcheck source=../include/hal/dts-hal.sh
1212
source $DTS_HAL
1313

14-
# Vars for controlling progress bar
15-
progress_bar_cntr=0
16-
PROGRESS_BAR_TASKS_TOTAL=30
17-
1814
# Helper vars
1915
FW_DUMP_DEFAULT_PATH="logs/rom.bin"
2016
fw_bin_path="$FW_DUMP_DEFAULT_PATH"
2117

22-
progress_bar_update() {
23-
local BAR_WIDTH=67
24-
25-
# Increment counter
26-
((progress_bar_cntr++))
27-
28-
# Clamp counter
29-
if ((progress_bar_cntr > PROGRESS_BAR_TASKS_TOTAL)); then
30-
progress_bar_cntr=$PROGRESS_BAR_TASKS_TOTAL
31-
fi
32-
33-
# Calculate progress
34-
local filled=$((progress_bar_cntr * BAR_WIDTH / PROGRESS_BAR_TASKS_TOTAL))
35-
local empty=$((BAR_WIDTH - filled))
36-
37-
# Build bar
38-
local bar
39-
bar=$(printf "%0.s#" $(seq 1 $filled))
40-
if ((empty > 0)); then
41-
bar+=$(printf "%0.s " $(seq 1 $empty))
42-
fi
18+
# Vars for controlling progress bar
19+
bar_cntr=0
20+
BAR_TASKS_TOTAL=30
4321

44-
# Print with carriage return
45-
printf "\r[%s] %d/%d" "$bar" "$progress_bar_cntr" "$PROGRESS_BAR_TASKS_TOTAL"
22+
progress_bar_update() {
23+
draw_progress_bar "$((++bar_cntr))" "$BAR_TASKS_TOTAL"
4624
}
4725

4826
update_result() {

scripts/dasharo-deploy.sh

Lines changed: 153 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,54 @@ deploy_firmware() {
878878
#
879879
# $DEPLOY_COMMAND $DEPLOY_ARGS &>> $LOGS_FILE
880880
local _mode
881+
local _jobs=() # List of scheduled job indices
882+
local _messages=() # List of error messages
883+
# _job_args_<i> # List of flashrom params per job indice
884+
# These are created dynamically in schedule_job() and referenced via nameref.
885+
local _jobs_total=0
881886
_mode="$1"
882887

888+
# Helper function to schedule a flashrom job
889+
# Each job consists of:
890+
# - a unique numeric index
891+
# - an associated error message
892+
# - a dedicated argument array holding flashrom parameters
893+
schedule_job() {
894+
local msg="$1"
895+
shift
896+
897+
# Use current job count as a unique job identifier
898+
local idx=${#_jobs[@]}
899+
# Track job order and corresponding error message
900+
_jobs+=("$idx")
901+
_messages+=("$msg")
902+
903+
# Create a per-job global array for flashrom arguments accessible to
904+
# mother-function
905+
declare -g -a "_job_args_$idx"
906+
# Bind a nameref to the per-job argument array and populate it
907+
local -n args_ref="_job_args_$idx"
908+
# Needed as bash does not support dynamic variable names expansion
909+
args_ref=("$@")
910+
}
911+
912+
# Helper function to check whether fd flashing is among arguments
913+
# 0 if found, 1 otherwise.
914+
check_for_fd() {
915+
local -n _args="$1"
916+
local i
917+
918+
# Scan argument array for the exact flashrom region selector "-i fd".
919+
# Iterate only up to length-1 since we always inspect pairs (i, i+1).
920+
for ((i = 0; i < ${#_args[@]} - 1; i++)); do
921+
if [[ "${_args[i]}" == "-i" && "${_args[i + 1]}" == "fd" ]]; then
922+
return 0
923+
fi
924+
done
925+
926+
return 1
927+
}
928+
883929
if [ "$_mode" == "update" ]; then
884930
echo "Updating Dasharo firmware..."
885931
print_warning "This may take several minutes. Please be patient and do not"
@@ -902,25 +948,32 @@ deploy_firmware() {
902948

903949
# FLASHROM_ADD_OPT_UPDATE_OVERRIDE takes priority over auto-detected update params.
904950
# It set only by platform-specific and firmware version-specific conditions
951+
echo "Scheduling main firmware update..."
905952
if [ -n "$FLASHROM_ADD_OPT_UPDATE_OVERRIDE" ]; then
906953
# To standardize the operation of the FLASHROM_ADD_OPT_UPDATE_OVERRIDE flag,
907954
# by default it contains only the bios section, below we verify the
908955
# downloaded binary and add more sections when they were detected after
909956
# using the `check_blobs_in_binary` function.
910957
set_intel_regions_update_params "$FLASHROM_ADD_OPT_UPDATE_OVERRIDE"
911958
FLASHROM_ADD_OPT_UPDATE_OVERRIDE="$FLASHROM_ADD_OPT_REGIONS"
912-
flashrom_write_and_check "Failed to update Dasharo firmware" \
913-
-p "$PROGRAMMER_BIOS" ${FLASH_CHIP_SELECT} ${FLASHROM_ADD_OPT_UPDATE_OVERRIDE} \
959+
schedule_job "Failed to update Dasharo firmware" \
960+
-p "$PROGRAMMER_BIOS" \
961+
${FLASH_CHIP_SELECT} \
962+
${FLASHROM_ADD_OPT_UPDATE_OVERRIDE} \
914963
-w "$BIOS_UPDATE_FILE"
915964
else
916965
set_intel_regions_update_params "-N --ifd"
917-
flashrom_write_and_check "Failed to update Dasharo firmware" \
918-
-p "$PROGRAMMER_BIOS" ${FLASH_CHIP_SELECT} ${FLASHROM_ADD_OPT_UPDATE} \
966+
schedule_job "Failed to update Dasharo firmware" \
967+
-p "$PROGRAMMER_BIOS" \
968+
${FLASH_CHIP_SELECT} \
969+
${FLASHROM_ADD_OPT_UPDATE} \
919970
-w "$BIOS_UPDATE_FILE"
920971
if [ $BINARY_HAS_RW_B -eq 0 ]; then
921-
echo "Updating second firmware partition..."
922-
flashrom_write_and_check "Failed to update second firmware partition" \
923-
-p "$PROGRAMMER_BIOS" ${FLASH_CHIP_SELECT} --fmap -N -i RW_SECTION_B \
972+
echo "Scheduling second firmware partition update..."
973+
schedule_job "Failed to update second firmware partition" \
974+
-p "$PROGRAMMER_BIOS" \
975+
${FLASH_CHIP_SELECT} \
976+
--fmap -N -i RW_SECTION_B \
924977
-w "$BIOS_UPDATE_FILE"
925978
fi
926979
fi
@@ -945,32 +998,107 @@ deploy_firmware() {
945998
if [ $UPDATE_ME -eq 0 ]; then
946999
UPDATE_STRING+="Management Engine"
9471000
fi
948-
echo "Updating $UPDATE_STRING"
949-
flashrom_write_and_check "Failed to update $UPDATE_STRING" \
950-
-p "$PROGRAMMER_BIOS" ${FLASH_CHIP_SELECT} ${FLASHROM_ADD_OPT_REGIONS} \
1001+
echo "Scheduling $UPDATE_STRING update..."
1002+
schedule_job "Failed to update $UPDATE_STRING" \
1003+
-p "$PROGRAMMER_BIOS" \
1004+
${FLASH_CHIP_SELECT} \
1005+
${FLASHROM_ADD_OPT_REGIONS} \
9511006
-w "$BIOS_UPDATE_FILE"
9521007
fi
953-
954-
return 0
9551008
elif [ "$_mode" == "install" ]; then
9561009
firmware_pre_installation_routine
9571010

958-
echo "Installing Dasharo firmware..."
1011+
echo "Scheduling Dasharo firmware installation..."
9591012
# FIXME: It seems we do not have an easy way to add some flasrhom extra args
9601013
# globally for specific platform and variant
961-
local _flashrom_extra_args=""
1014+
local _flashrom_extra_args=()
9621015
if [ "${BIOS_LINK}" = "${BIOS_LINK_DPP_SEABIOS}" ]; then
963-
_flashrom_extra_args="--fmap -i COREBOOT"
1016+
_flashrom_extra_args=(--fmap -i COREBOOT)
9641017
fi
965-
flashrom_write_and_check "Failed to install Dasharo firmware" \
966-
-p "$PROGRAMMER_BIOS" ${FLASH_CHIP_SELECT} ${FLASHROM_ADD_OPT_REGIONS} \
967-
-w "$BIOS_UPDATE_FILE" ${_flashrom_extra_args}
968-
print_ok "Successfully installed Dasharo firmware"
969-
return 0
1018+
schedule_job "Failed to install Dasharo firmware" \
1019+
-p "$PROGRAMMER_BIOS" \
1020+
${FLASH_CHIP_SELECT} \
1021+
${FLASHROM_ADD_OPT_REGIONS} \
1022+
-w "$BIOS_UPDATE_FILE" \
1023+
"${_flashrom_extra_args[@]}"
1024+
fi
1025+
1026+
# If any job flashes FD region, schedule a dedicated job just for that.
1027+
# The reason is, regions are FD dependent.
1028+
for i in "${_jobs[@]}"; do
1029+
# _job_args_$i is a dynamically named (runtime-created) global array holding
1030+
# flashrom arguments for a single job. The array's created in schedule_job().
1031+
# A nameref is used to reference that array as bash does not support
1032+
# dynamic variable expansion.
1033+
# shellcheck disable=SC2178
1034+
local -n args_ref="_job_args_$i"
1035+
1036+
if check_for_fd args_ref; then
1037+
echo "Scheduling dedicated FD update..."
1038+
schedule_job "Failed to flash FD" \
1039+
-p "$PROGRAMMER_BIOS" \
1040+
${FLASH_CHIP_SELECT} \
1041+
-N --ifd -i fd \
1042+
-w "$BIOS_UPDATE_FILE"
1043+
1044+
# fd_idx is the index of the newly added FD job
1045+
local fd_idx=$((${#_jobs[@]} - 1))
1046+
# Move the FD job to the front of the queue
1047+
_jobs=("$fd_idx" "${_jobs[@]:0:fd_idx}")
1048+
_messages=("${_messages[$fd_idx]}" "${_messages[@]:0:fd_idx}")
1049+
break
1050+
fi
1051+
done
1052+
1053+
_jobs_total=${#_jobs[@]}
1054+
1055+
# Execute scheduled tasks
1056+
for n in "${!_jobs[@]}"; do
1057+
# Current job ID
1058+
local i="${_jobs[$n]}"
1059+
# _job_args_$i is a dynamically named (runtime-created) global array holding
1060+
# flashrom arguments for a single job. The array's created in schedule_job().
1061+
# A nameref is used to reference that array as bash does not support
1062+
# dynamic variable expansion.
1063+
# shellcheck disable=SC2178
1064+
local -n args_ref="_job_args_$i"
1065+
1066+
draw_progress_bar "$((n + 1))" "$_jobs_total"
1067+
flashrom_write_and_check "${_messages[$i]}" "${args_ref[@]}"
1068+
done
1069+
1070+
echo
1071+
print_ok "All jobs completed successfully!"
1072+
1073+
return 0
1074+
}
1075+
1076+
# A helper function for transition/recovery flows.
1077+
# Makes sure if FD region is flashed, it gets a dedicated job first.
1078+
# The reason is, other regions are FD dependent.
1079+
flash_bios_fd_first() {
1080+
local rom_file="$1"
1081+
local operation="$2"
1082+
1083+
if [[ " ${FLASHROM_ADD_OPT_REGIONS} " == *" -i fd "* ]]; then
1084+
echo "Flashing FD region..."
1085+
1086+
flashrom_write_and_check \
1087+
"Failed to flash FD region" \
1088+
-p "$PROGRAMMER_BIOS" \
1089+
${FLASH_CHIP_SELECT} \
1090+
-N --ifd -i fd \
1091+
-w "$rom_file"
1092+
1093+
echo "Flashing remaining regions..."
9701094
fi
9711095

972-
# Must not get here.
973-
return 1
1096+
flashrom_write_and_check \
1097+
"Failed to ${operation} Dasharo firmware!" \
1098+
-p "$PROGRAMMER_BIOS" \
1099+
${FLASH_CHIP_SELECT} \
1100+
${FLASHROM_ADD_OPT_REGIONS} \
1101+
-w "$rom_file"
9741102
}
9751103

9761104
check_if_cpu_compatible() {
@@ -1291,10 +1419,7 @@ transition_firmware() {
12911419
firmware_pre_installation_routine
12921420

12931421
echo "Transitioning Dasharo firmware..."
1294-
# FIXME: It seems we do not have an easy way to add some flasrhom extra args
1295-
# globally for specific platform and variant
1296-
$FLASHROM -p "$PROGRAMMER_BIOS" ${FLASH_CHIP_SELECT} ${FLASHROM_ADD_OPT_REGIONS} -w "$BIOS_UPDATE_FILE" >>$FLASHROM_LOG_FILE 2>>"$ERR_LOG_FILE"
1297-
error_check "Failed to transition Dasharo firmware"
1422+
flash_bios_fd_first "$BIOS_UPDATE_FILE" "transition"
12981423
print_ok "Successfully transitioned Dasharo firmware"
12991424

13001425
return $OK
@@ -1391,8 +1516,7 @@ restore() {
13911516
check_blobs_in_binary /tmp/logs/rom.bin
13921517
check_if_me_disabled
13931518
set_intel_regions_update_params "-N --ifd -i bios"
1394-
$FLASHROM -p "$PROGRAMMER_BIOS" ${FLASH_CHIP_SELECT} ${FLASHROM_ADD_OPT_REGIONS} -w "/tmp/logs/rom.bin" >>$FLASHROM_LOG_FILE 2>>$ERR_LOG_FILE
1395-
error_check "Failed to restore BIOS firmware! You can try one more time."
1519+
flash_bios_fd_first "/tmp/logs/rom.bin" "restore"
13961520
print_ok "Successfully restored firmware"
13971521
echo "Returning to main menu..."
13981522
exit 0
@@ -1429,8 +1553,7 @@ restore() {
14291553
check_blobs_in_binary /tmp/logs/rom.bin
14301554
check_if_me_disabled
14311555
set_intel_regions_update_params "-N --ifd -i bios"
1432-
$FLASHROM -p "$PROGRAMMER_BIOS" ${FLASH_CHIP_SELECT} ${FLASHROM_ADD_OPT_REGIONS} -w "/tmp/logs/rom.bin" >>$FLASHROM_LOG_FILE 2>>$ERR_LOG_FILE
1433-
error_check "Failed to restore BIOS firmware! You can try one more time."
1556+
flash_bios_fd_first "/tmp/logs/rom.bin" "restore"
14341557
print_ok "Successfully restored firmware"
14351558
else
14361559
print_error "Report does not have firmware backup!"

0 commit comments

Comments
 (0)