diff options
author | Leah Rowe <leah@libreboot.org> | 2024-03-27 01:19:39 +0000 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2024-03-27 01:50:31 +0000 |
commit | 6ebab10caa5be6fc1cfd244e745851687d4bd70d (patch) | |
tree | 0b983459c0cbc1b8998186975f9a571decb05592 /script/vendor/inject | |
parent | 6b11f1b055466982747f65665506173a0b22557b (diff) |
safer, simpler error handling in lbmk
in shell scripts, a function named the same as a program included in
the $PATH will override that program. for example, you could make a
function called ls() and this would override the standand "ls".
in lbmk, a part of it was first trying to run the "fail" command,
deferring to "err", because some scripts call fail() which does
some minor cleanup before calling err.
in most cases, fail() is not defined, and it's possible that the user
could have a program called "fail" in their $PATH, the behaviour of
which we could not determine, and it could have disastrous effects.
lbmk error handling has been re-engineered in such a way that the
err function is defined in a variable, which defaults to err_ which
calls err_, so defined under include/err.sh.
in functions that require cleanup prior to error handling, a fail()
function is still defined, and err is overridden, thus:
err="fail"
this change has made xx_() obsolete, so now only x_ is used. the x_
function is a wrapper that can be used to run a command and exit with
non-zero status (from lbmk) if the command fails. the xx_ command
did the same thing, but called fail() which would have called err();
now everything is $err
example:
rm -f "$filename" || err "could not delete file"
this would now be:
rm -f "$filename" || $err "could not delete file"
overriding of err= must be done *after* including err.sh. for
example:
err="fail"
. "include/err.sh"
^ this is wrong. instead, one must do:
. "include/err.sh"
err="fail"
this is because err is set as a global variable under err.sh
the new error handling is much cleaner, and safer. it also reduces
the chance of mistakes such as: calling err when you meant to
call fail. this is because the standard way is now to call $err,
so you set err="fail" at the top of the script and all is well.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'script/vendor/inject')
-rwxr-xr-x | script/vendor/inject | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/script/vendor/inject b/script/vendor/inject index 4ac2753f..dc12d7b5 100755 --- a/script/vendor/inject +++ b/script/vendor/inject @@ -13,7 +13,7 @@ eval "$(setvars "" archive rom modifygbe nukemode release new_mac tree)" main() { - [ $# -lt 1 ] && err "No options specified." + [ $# -lt 1 ] && $err "No options specified." [ "${1}" = "listboards" ] && eval "items config/coreboot || :; exit 0" archive="${1}" @@ -42,8 +42,8 @@ check_board() failcheck="n" check_release "${archive}" || failcheck="y" if [ "${failcheck}" = "y" ]; then - [ -f "$rom" ] || err "check_board \"$rom\": invalid path" - [ -z "${rom+x}" ] && err "check_board: no rom specified" + [ -f "$rom" ] || $err "check_board \"$rom\": invalid path" + [ -z "${rom+x}" ] && $err "check_board: no rom specified" [ -n "${board+x}" ] || board=$(detect_board "${rom}") else release="y" @@ -51,11 +51,11 @@ check_board() fi boarddir="${cbcfgsdir}/${board}" - [ -d "$boarddir" ] || err "check_board: board $board missing" + [ -d "$boarddir" ] || $err "check_board: board $board missing" [ -f "$boarddir/target.cfg" ] || \ - err "check_board $board: target.cfg missing" + $err "check_board $board: target.cfg missing" . "$boarddir/target.cfg" 2>/dev/null - [ -z "$tree" ] && err "check_board $board: tree undefined"; return 0 + [ -z "$tree" ] && $err "check_board $board: tree undefined"; return 0 } check_release() @@ -80,7 +80,7 @@ detect_board() _stripped_prefix=${filename#*_} board="${_stripped_prefix%.tar.xz}" ;; *) - err "detect_board $filename: could not detect board type" + $err "detect_board $filename: could not detect board type" esac printf "%s\n" "${board}" } @@ -109,7 +109,7 @@ patch_release_roms() _tmpdir="tmp/romdir" remkdir "${_tmpdir}" tar -xf "${archive}" -C "${_tmpdir}" || \ - err "patch_release_roms: !tar -xf \"$archive\" -C \"$_tmpdir\"" + $err "patch_release_roms: !tar -xf \"$archive\" -C \"$_tmpdir\"" for x in "${_tmpdir}"/bin/*/*.rom ; do printf "patching rom: %s\n" "$x" @@ -118,14 +118,14 @@ patch_release_roms() ( cd "${_tmpdir}/bin/"* || \ - err "patch_release_roms: !cd ${_tmpdir}/bin/*" + $err "patch_release_roms: !cd ${_tmpdir}/bin/*" # NOTE: For compatibility with older rom releases, defer to sha1 [ "${nukemode}" = "nuke" ] || sha512sum --status -c vendorhashes || \ sha1sum --status -c vendorhashes || sha512sum --status -c \ blobhashes || sha1sum --status -c blobhashes || \ - err "patch_release_roms: ROMs did not match expected hashes" - ) || err "can't verify vendor hashes" + $err "patch_release_roms: ROMs did not match expected hashes" + ) || $err "can't verify vendor hashes" [ "${modifygbe}" = "true" ] && \ for x in "${_tmpdir}"/bin/*/*.rom ; do @@ -143,7 +143,7 @@ patch_rom() { rom="${1}" - check_defconfig "$boarddir" && err "patch_rom $boarddir: no configs" + check_defconfig "$boarddir" && $err "patch_rom $boarddir: no configs" set -- "${boarddir}/config/"* . "${1}" 2>/dev/null @@ -174,18 +174,18 @@ patch_rom() inject() { [ $# -lt 3 ] && \ - err "inject $@, $rom: usage: inject name path type (offset)" + $err "inject $@, $rom: usage: inject name path type (offset)" eval "$(setvars "" cbfsname _dest _t _offset)" cbfsname="${1}" _dest="${2##*../}" _t="${3}" [ $# -gt 3 ] && _offset="-b ${4}" && [ -z "${4}" ] && \ - err "inject $@, $rom: offset passed, but empty (not defined)" + $err "inject $@, $rom: offset passed, but empty (not defined)" - [ -z "${_dest}" ] && err "inject $@, ${rom}: empty destination path" + [ -z "${_dest}" ] && $err "inject $@, ${rom}: empty destination path" [ ! -f "${_dest}" ] && [ "${nukemode}" != "nuke" ] && \ - err "inject_${dl_type}: file missing, ${_dest}" + $err "inject_${dl_type}: file missing, ${_dest}" [ "$nukemode" = "nuke" ] || \ printf "Inserting %s/%s in file: %s\n" "$cbfsname" "$_t" "$rom" @@ -193,18 +193,18 @@ inject() if [ "${_t}" = "GbE" ]; then x_ mkdir -p tmp cp "${_dest}" "tmp/gbe.bin" || \ - err "inject: !cp \"${_dest}\" \"tmp/gbe.bin\"" + $err "inject: !cp \"${_dest}\" \"tmp/gbe.bin\"" _dest="tmp/gbe.bin" "${nvmutil}" "${_dest}" setmac "${new_mac}" || \ - err "inject ${_dest}: can't change mac address" + $err "inject ${_dest}: can't change mac address" fi if [ "${cbfsname}" = "IFD" ]; then if [ "${nukemode}" != "nuke" ]; then "$ifdtool" -i ${_t}:${_dest} "$rom" -O "$rom" || \ - err "inject: can't insert $_t ($dest) into $rom" + $err "inject: can't insert $_t ($dest) into $rom" else "$ifdtool" --nuke $_t "$rom" -O "$rom" || \ - err "inject $rom: can't nuke $_t in IFD" + $err "inject $rom: can't nuke $_t in IFD" fi else if [ "${nukemode}" != "nuke" ]; then @@ -214,11 +214,11 @@ inject() else "$cbfstool" "$rom" add -f "$_dest" \ -n "$cbfsname" -t $_t $_offset || \ - err "$rom: can't insert $_t file $_dest" + $err "$rom: can't insert $_t file $_dest" fi else "$cbfstool" "$rom" remove -n "$cbfsname" || \ - err "inject $rom: can't remove $cbfsname" + $err "inject $rom: can't remove $cbfsname" fi fi } |