From 6ebab10caa5be6fc1cfd244e745851687d4bd70d Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Wed, 27 Mar 2024 01:19:39 +0000 Subject: 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 --- script/build/roms | 36 ++++++++++++++++++------------------ script/build/serprog | 6 +++--- 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'script/build') diff --git a/script/build/roms b/script/build/roms index 9e7c69ee..a90e1daa 100755 --- a/script/build/roms +++ b/script/build/roms @@ -49,7 +49,7 @@ main() done [ "${all}" != "y" ] || boards=$(items config/coreboot) || \ - err "Cannot generate list of boards for building" + $err "Cannot generate list of boards for building" for x in ${boards}; do eval "$(setvars "n" ${pv}) $(setvars "" ${v})" @@ -62,7 +62,7 @@ main() targets="* bin/${board}\n${targets}" done - [ -z "${targets}" ] && err "No ROM images were compiled" + [ -z "${targets}" ] && $err "No ROM images were compiled" printf "\nROM images available in these directories:\n" eval "printf \"${targets}\"" printf "^^ ROM images available in these directories.\n\n" @@ -74,7 +74,7 @@ configure_target() { targetdir="${cfgsdir}/${board}" [ -f "${targetdir}/target.cfg" ] || \ - err "Missing target.cfg for target: ${board}" + $err "Missing target.cfg for target: ${board}" # Override the above defaults using target.cfg . "${targetdir}/target.cfg" @@ -84,7 +84,7 @@ configure_target() && [ "${grub_scan_disk}" != "ahci" ] && \ grub_scan_disk="both" - [ -z "$tree" ] && err "$board: tree not defined" + [ -z "$tree" ] && $err "$board: tree not defined" [ "${payload_memtest}" != "y" ] && payload_memtest="n" [ "${payload_grub_withseabios}" = "y" ] && payload_grub="y" @@ -101,7 +101,7 @@ configure_target() [ "${payload_uboot}" != "y" ] && \ for configfile in "${targetdir}/config/"*; do [ -e "${configfile}" ] || continue - err "target '${board}' defines no payload" + $err "target '${board}' defines no payload" done [ "$payload_uboot" != "n" ] && [ "$payload_uboot" != "y" ] && \ @@ -149,7 +149,7 @@ build_grub_payload() keymaps="${keymaps} ${keymapfile}" done [ -z "$_keyboard" ] || [ -f "$grubcfgsdir/keymap/$_keyboard.gkb" ] || \ - err "build_grub_payload: $_keyboard layout not defined" + $err "build_grub_payload: $_keyboard layout not defined" [ -n "$_keyboard" ] && keymaps="${grubcfgsdir}/keymap/${_keyboard}.gkb" [ -f "$grubelf" ] && return 0 [ -f "src/grub/grub-mkstandalone" ] || x_ ./update trees -b grub @@ -164,7 +164,7 @@ build_grub_payload() --install-modules="${grub_install_modules}" \ "/boot/grub/grub.cfg=${grubcfgsdir}/config/grub_memdisk.cfg" \ "/boot/grub/grub_default.cfg=${grubcfgsdir}/config/grub.cfg" || \ - err "could not generate grub.elf" + $err "could not generate grub.elf" } build_uboot_payload() @@ -175,12 +175,12 @@ build_uboot_payload() [ ! -f "${ubootelf}" ] && [ -f "${ubdir}/u-boot" ] && \ ubootelf="${ubdir}/u-boot" [ -f "${ubootelf}" ] && return 0 - err "Can't find u-boot build for board, $board"; + $err "Can't find u-boot build for board, $board"; } build_target_mainboard() { - rm -f "${romdir}/"* || err "!prepare, rm files, ${romdir}" + rm -f "${romdir}/"* || $err "!prepare, rm files, ${romdir}" for x in "normal" "vgarom" "libgfxinit"; do initmode="${x}" @@ -237,7 +237,7 @@ build_seabios_roms() x_ build_grub_roms "${t}" "seabios_withgrub" else t=$(mkSeabiosRom "${cbrom}" "fallback/payload") || \ - err "build_seabios_roms: cannot build tmprom" + $err "build_seabios_roms: cannot build tmprom" newrom="${romdir}/seabios_${board}_${initmode}_${displaymode}" [ "${initmode}" = "normal" ] && newrom="${romdir}/seabios" \ && newrom="${newrom}_${board}_${initmode}" @@ -256,13 +256,13 @@ build_grub_roms() if [ "$payload1" = "grub" ] && [ "$payload_grub_withseabios" = "y" ] then _tmpmvrom=$(mkSeabiosRom "$tmprom" "seabios.elf") || \ - err "build_grub_roms 1 $board: can't build tmprom" + $err "build_grub_roms 1 $board: can't build tmprom" x_ mv "$_tmpmvrom" "$tmprom" elif [ "$payload1" != "grub" ] && [ "$payload_seabios_withgrub" = "y" ] then grub_cbfs="img/grub2" _tmpmvrom=$(mkSeabiosRom "$tmprom" fallback/payload) || \ - err "build_grub_roms 2 $board: can't build tmprom" + $err "build_grub_roms 2 $board: can't build tmprom" x_ mv "$_tmpmvrom" "$tmprom" fi @@ -276,16 +276,16 @@ build_grub_roms() backgroundfile="config/grub/background/${grub_background}" "${cbfstool}" "${tmprom}" add -f ${backgroundfile} \ -n background.png -t raw || \ - err "insert background, ${backgroundfile}" + $err "insert background, ${backgroundfile}" fi tmpcfg=$(mktemp -t coreboot_rom.XXXXXXXXXX) printf "set grub_scan_disk=\"%s\"\n" "$grub_scan_disk" >"$tmpcfg" \ - || err "set grub_scandisk, $grub_scan_disk, $tmpcfg" + || $err "set grub_scandisk, $grub_scan_disk, $tmpcfg" [ "${grub_scan_disk}" = "both" ] || \ x_ "$cbfstool" "$tmprom" add -f "$tmpcfg" -n scan.cfg -t raw printf "set timeout=%s\n" "${grub_timeout}" > "${tmpcfg}" || \ - err "set timeout, ${grub_timeout}, ${tmpcfg}" + $err "set timeout, ${grub_timeout}, ${tmpcfg}" [ -z "${grub_timeout}" ] || x_ "${cbfstool}" "${tmprom}" add \ -f "${tmpcfg}" -n timeout.cfg -t raw x_ rm -f "${tmpcfg}" @@ -344,7 +344,7 @@ mkSeabiosGrubonlyRom() tmpbootorder=$(mktemp -t coreboot_rom.XXXXXXXXXX) # only load grub, by inserting a custom bootorder file - printf "/rom@img/grub2\n" > "$tmpbootorder" || err "printf bootorder" + printf "/rom@img/grub2\n" > "$tmpbootorder" || $err "printf bootorder" x_ "${cbfstool}" "${_grubrom}" \ add -f "${tmpbootorder}" -n bootorder -t raw x_ rm -f "${tmpbootorder}" @@ -356,7 +356,7 @@ mkSeabiosGrubonlyRom() build_uboot_roms() { tmprom=$(mkUbootRom "${cbrom}" "fallback/payload") || \ - err "build_uboot_roms $board: could not create tmprom" + $err "build_uboot_roms $board: could not create tmprom" newrom="${romdir}/uboot_payload_${board}_${initmode}_${displaymode}.rom" x_ moverom "${tmprom}" "${newrom}" x_ rm -f "${tmprom}" @@ -370,7 +370,7 @@ mkUbootRom() { _ubdir="elf/u-boot/${board}/${uboot_config}" _ubootelf="${_ubdir}/u-boot.elf" [ -f "${_ubootelf}" ] || _ubootelf="${_ubdir}/u-boot" - [ -f "$_ubootelf" ] || err "mkUbootRom: $board: cant find u-boot" + [ -f "$_ubootelf" ] || $err "mkUbootRom: $board: cant find u-boot" tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX) diff --git a/script/build/serprog b/script/build/serprog index 8b003add..ea2c02bf 100755 --- a/script/build/serprog +++ b/script/build/serprog @@ -17,8 +17,8 @@ usage="usage: ./build firmware serprog [board]" main() { - [ -z "${1+x}" ] && err "${usage}" - [ "$1" != "rp2040" ] && [ "$1" != "stm32" ] && err "$usage" + [ -z "${1+x}" ] && $err "${usage}" + [ "$1" != "rp2040" ] && [ "$1" != "stm32" ] && $err "$usage" if [ "${1}" = "rp2040" ]; then boards_dir=${pico_sdk_dir}/src/boards/include/boards [ -d "$pico_src_dir" ] || x_ ./update trees -f "pico-serprog" @@ -71,7 +71,7 @@ print_boards() list_boards() { - basename -a -s .h "${1}/"*.h || err "list_boards $1: can't list boards" + basename -a -s .h "${1}/"*.h || $err "list_boards $1: can't list boards" } main $@ -- cgit v1.2.1