Skip to content

Commit 84034b2

Browse files
committed
WIP: Refactor sanity check
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@3mdeb.com>
1 parent 2472a3b commit 84034b2

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

include/dts-functions.sh

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -688,17 +688,55 @@ set_flashrom_update_params() {
688688
fi
689689
}
690690

691-
# A final check for locked regions before flashing via flashrom.
692-
# Decide whether we can proceed if any regions are locked.
693-
flashrom_sanity_check() {
691+
# Does combined check to assess whether the board has region and if its locked.
692+
is_region_locked() {
693+
local region="$1"
694+
local has_var rw_var
695+
696+
# Uppercase
697+
region="${region^^}"
698+
699+
has_var="BOARD_HAS_${region}_REGION"
700+
rw_var="BOARD_${region}_REGION_RW"
701+
702+
[[ "${!has_var}" -eq 1 && "${!rw_var}" -eq 0 ]]
703+
}
704+
705+
# Helper function to check whether a certain region is to be flashed.
706+
# 0 if found, 1 otherwise.
707+
flashrom_check_for_region() {
708+
local region="$1"
709+
local -n _args="$2"
710+
local i
711+
712+
# Iterate only up to length-1 since we inspect pairs (i, i+1)
713+
for ((i = 0; i < ${#_args[@]} - 1; i++)); do
714+
if [[ "${_args[i]}" == "-i" && "${_args[i + 1]}" == "$region" ]]; then
715+
return 0
716+
fi
717+
done
718+
719+
return 1
720+
}
721+
722+
# Does a final check for all flashrom parameters if FD or ME are to be flashed
723+
# and if they're locked. Decide whether we can proceed if any regions are locked.
724+
flashrom_region_check() {
725+
local -n args="$1"
694726
local locked_regions=()
695727
local region_list verb
696728

697-
if [ "$BOARD_FD_REGION_RW" -eq 0 ]; then
729+
# If switching to heads, always check the region, otherwise check region only
730+
# if specified in params.
731+
# The reason is we want to handle both "overwrites" form metadata as well as
732+
# dynamically added params from set_intel_regions_update_params()
733+
if { flashrom_check_for_region fd args || [[ "$SWITCHING_TO" == "heads" ]]; } &&
734+
is_region_locked fd; then
698735
locked_regions+=("FD")
699736
fi
700737

701-
if [ "$BOARD_ME_REGION_RW" -eq 0 ]; then
738+
if { flashrom_check_for_region me args || [[ "$SWITCHING_TO" == "heads" ]]; } &&
739+
is_region_locked me; then
702740
locked_regions+=("ME")
703741
fi
704742

@@ -716,10 +754,11 @@ flashrom_sanity_check() {
716754

717755
if [[ "$SWITCHING_TO" == "heads" ]]; then
718756
print_error "Cannot proceed with heads update when $region_list $verb locked!"
757+
print_error "Refer to: https://docs.dasharo.com/guides/firmware-update/#known-issues"
719758
return 1
720759
fi
721760

722-
print_warning "Proceeding without $region_list $verb flashing, as they $verb not critical."
761+
print_warning "Proceeding without $region_list flashing, as it is not critical."
723762
return 0
724763
}
725764

scripts/dasharo-deploy.sh

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,11 @@ deploy_firmware() {
880880
local _mode
881881
local _jobs=() # List of scheduled job indices
882882
local _messages=() # List of error messages
883-
# _job_args_<i> # List of flashrom params per job indice
883+
# _job_args_<i> # List of flashrom params per job indice
884884
# These are created dynamically in schedule_job() and referenced via nameref.
885885
local _jobs_total=0
886+
local _all_flashrom_args=() # An array for all flashrom args from all jobs
887+
local n i # Loop iterators
886888
_mode="$1"
887889

888890
# Helper function to schedule a flashrom job
@@ -909,23 +911,6 @@ deploy_firmware() {
909911
args_ref=("$@")
910912
}
911913

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-
929914
if [ "$_mode" == "update" ]; then
930915
echo "Updating Dasharo firmware..."
931916
print_warning "This may take several minutes. Please be patient and do not"
@@ -1033,7 +1018,7 @@ deploy_firmware() {
10331018
# shellcheck disable=SC2178
10341019
local -n args_ref="_job_args_$i"
10351020

1036-
if check_for_fd args_ref; then
1021+
if flashrom_check_for_region fd args_ref; then
10371022
echo "Scheduling dedicated FD update..."
10381023
schedule_job "Failed to flash FD" \
10391024
-p "$PROGRAMMER_BIOS" \
@@ -1050,8 +1035,19 @@ deploy_firmware() {
10501035
fi
10511036
done
10521037

1053-
# Last restort check before flashing
1054-
if ! flashrom_sanity_check; then
1038+
# Create array of all flashrom params to call flashrom_region_check only once
1039+
for n in "${!_jobs[@]}"; do
1040+
i="${_jobs[$n]}"
1041+
# _job_args_$i is a dynamically named (runtime-created) global array holding
1042+
# flashrom arguments for a single job.
1043+
# shellcheck disable=SC2178
1044+
local -n args_ref="_job_args_$i"
1045+
1046+
_all_flashrom_args+=("${args_ref[@]}")
1047+
done
1048+
1049+
# Last resort check before flashing
1050+
if ! flashrom_region_check _all_flashrom_args; then
10551051
return 1
10561052
fi
10571053

0 commit comments

Comments
 (0)