Greatly simplify error handling in shell scripts

Instead of having detailed error messages, run most
commands through a function that calls err() under
fault conditions.

Where detail is still required, err() is still called
manually. Where it isn't, the error message is simply
whatever command was executed to cause the error.

This results in a massive sloccount reduction for lbmk;
specifically, 178 sloc reduction, or a 8.1% reduction.
The total sloccount is now 2022, for shell scripts.

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2023-10-01 06:33:43 +01:00
parent 5f914a4d00
commit 8c03b886c4
19 changed files with 278 additions and 461 deletions
+7 -11
View File
@@ -14,15 +14,14 @@ extract_mrc()
_file="${_file%.zip}"
(
cd "${appdir}" || err "extract_mrc: !cd ${appdir}"
x_ cd "${appdir}"
extract_partition ROOT-A "${_file}" root-a.ext2
extract_shellball root-a.ext2 chromeos-firmwareupdate-${MRC_board}
extract_coreboot chromeos-firmwareupdate-${MRC_board}
)
"${cbfstool}" "${appdir}/"coreboot-*.bin extract -n mrc.bin \
-f "${_dest}" -r RO_SECTION || \
err "extract_mrc: could not fetch mrc.bin"
x_ "${cbfstool}" "${appdir}/"coreboot-*.bin extract -n mrc.bin \
-f "${_dest}" -r RO_SECTION
}
extract_partition()
@@ -39,9 +38,8 @@ extract_partition()
START=$(( $( echo ${ROOTP} | cut -f2 -d\ | tr -d "B" ) ))
SIZE=$(( $( echo ${ROOTP} | cut -f4 -d\ | tr -d "B" ) ))
dd if="${FILE}" of="${ROOTFS}" bs=${_bs} skip=$(( ${START} / ${_bs} )) \
count=$(( ${SIZE} / ${_bs} )) || \
err "extract_partition: can't extract root file system"
x_ dd if="${FILE}" of="${ROOTFS}" bs=${_bs} \
skip=$(( ${START} / ${_bs} )) count=$(( ${SIZE} / ${_bs} ))
}
extract_shellball()
@@ -62,8 +60,7 @@ extract_coreboot()
printf "Extracting coreboot image\n"
[ -f "${_shellball}" ] || \
err "extract_coreboot: shellball missing in google cros image"
sh "${_shellball}" --unpack "${_unpacked}" || \
err "extract_coreboot: shellball exits with non-zero status"
x_ sh "${_shellball}" --unpack "${_unpacked}"
# TODO: audit the f* out of that shellball, for each mrc version.
# it has to be updated for each mrc update. we should ideally
@@ -75,6 +72,5 @@ extract_coreboot()
_version=$( cat "${_unpacked}/VERSION" | grep BIOS\ version: | \
cut -f2 -d: | tr -d \ )
cp "${_unpacked}/bios.bin" "coreboot-${_version}.bin" || \
err "extract_coreboot: cannot copy google cros rom"
x_ cp "${_unpacked}/bios.bin" "coreboot-${_version}.bin"
}