From 8f4f0e00ec3c307599f7f27777e3e92c1f9f6e4e Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Thu, 24 Aug 2023 00:30:07 +0100 Subject: use the new coding style in scripts there were stragglers left over from the last audit, and these stragglers still exist even after all the major re-factoring as of late the new style is: bsd-like coding style and error handling. verbose yet simple error handling. we use an "err" function in a way reminiscent of most C programs that you see in openbsd base (err.h) this style is very clean, resulting in readable code Signed-off-by: Leah Rowe --- resources/scripts/build/clean/cbutils | 52 ++++++++++++++++++++-------------- resources/scripts/build/clean/crossgcc | 7 ++++- resources/scripts/build/clean/seabios | 27 ++++++++++++------ resources/scripts/build/clean/u-boot | 33 ++++++++++++++------- resources/scripts/build/coreboot/utils | 28 +++++++++--------- resources/scripts/build/grub/payload | 34 +++++++++++++--------- resources/scripts/build/src/for | 14 +++++++-- resources/scripts/misc/versioncheck | 2 +- 8 files changed, 125 insertions(+), 72 deletions(-) diff --git a/resources/scripts/build/clean/cbutils b/resources/scripts/build/clean/cbutils index dec8d944..067c2cb2 100755 --- a/resources/scripts/build/clean/cbutils +++ b/resources/scripts/build/clean/cbutils @@ -24,26 +24,36 @@ [ "x${DEBUG+set}" = 'xset' ] && set -v set -u -e -# clean coreboot utilities (dependencies for 'build'): - -printf "Cleaning the previous build of coreboot and its utilities\n" - -rm -Rf cbutils - -[ ! -d "coreboot/" ] && exit 0 - -for tree in coreboot/*; do - [ "${tree##*/}" = "coreboot" ] && continue - [ -d "${tree}" ] || continue - - # Clean coreboot, of course - make -C "${tree}/" distclean - - # Clean its utilities as well - for util in cbfstool ifdtool nvramtool cbmem; do - make distclean -C "${tree}/util/${util}/" +. "include/err.sh" + +main() +{ + printf "Cleaning the previous build of coreboot and its utilities\n" + + rm -Rf cbutils || err "cannot remove cbutils/" + [ ! -d "coreboot/" ] && exit 0 + + clean_cbutils +} + +clean_cbutils() +{ + for tree in coreboot/*; do + [ "${tree##*/}" = "coreboot" ] && continue + [ -d "${tree}" ] || continue + + # Clean coreboot, of course + make -C "${tree}/" distclean || \ + err "cannot distclean coreboot tree, ${tree}" + + # Clean its utilities as well + for util in cbfstool ifdtool nvramtool cbmem; do + make distclean -C "${tree}/util/${util}/" || \ + err "cannot clean util, ${util}, at ${tree}" + done + make distclean -C "${tree}/payloads/libpayload/" || \ + err "cannot distclean libpayload, at ${tree}" done - make distclean -C "${tree}/payloads/libpayload/" -done +} -printf "\n\n" +main $@ diff --git a/resources/scripts/build/clean/crossgcc b/resources/scripts/build/clean/crossgcc index 76de1e19..7b1765f8 100755 --- a/resources/scripts/build/clean/crossgcc +++ b/resources/scripts/build/clean/crossgcc @@ -29,9 +29,14 @@ main() printf "Cleaning crossgcc builds in all coreboot archives\n" [ ! -d "coreboot/" ] && exit 0 + clean_crossgcc +} + +clean_crossgcc() +{ for board in coreboot/*; do + [ -d "${board}" ] || continue [ "${board##*/}" = "coreboot" ] && continue - [ ! -d "${board}" ] && continue make -C "${board}/" crossgcc-clean || err "make-clean" done } diff --git a/resources/scripts/build/clean/seabios b/resources/scripts/build/clean/seabios index e9b11e5b..c6759e83 100755 --- a/resources/scripts/build/clean/seabios +++ b/resources/scripts/build/clean/seabios @@ -2,7 +2,7 @@ # helper script: clean the dependencies that were built in seabios # -# Copyright (C) 2015, 2020, 2021 Leah Rowe +# Copyright (C) 2015,2020,2021,2023 Leah Rowe # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -26,13 +26,22 @@ set -u -e # clean bucts # -------------------------------------------------------- -printf "Cleaning the previous builds of seabios\n" +main() +{ + [ ! -d "elf/seabios" ] || rm -Rf elf/seabios || \ + err "cannot remove elf/seabios" + [ ! -d "seabios/" ] && exit 0 -[ ! -d "elf/seabios" ] || rm -Rf elf/seabios || err "cannot remove elf/seabios" -[ ! -d "seabios/" ] && exit 0 + clean_seabios +} -for x in seabios/*; do - [ "${x}" = "seabios/seabios" ] && continue - [ ! -d "${x}" ] && continue - make -C "${x}" distclean || err "cannot distclean tree, ${x}" -done +clean_seabios() +{ + for x in seabios/*; do + [ ! -d "${x}" ] && continue + [ "${x}" = "seabios/seabios" ] && continue + make -C "${x}" distclean || err "cannot distclean tree, ${x}" + done +} + +main $@ diff --git a/resources/scripts/build/clean/u-boot b/resources/scripts/build/clean/u-boot index 6a59a5c1..7ba3a4a9 100755 --- a/resources/scripts/build/clean/u-boot +++ b/resources/scripts/build/clean/u-boot @@ -2,7 +2,7 @@ # helper script: clean the u-boot builds # -# Copyright (C) 2014, 2015, 2016, 2020 Leah Rowe +# Copyright (C) 2014-2016, 2020, 2023 Leah Rowe # Copyright (C) 2015 Klemens Nanni # Copyright (C) 2022 Alper Nebi Yasak # @@ -23,17 +23,28 @@ [ "x${DEBUG+set}" = 'xset' ] && set -v set -u -e -printf "Cleaning u-boot builds for all boards\n" +. "include/err.sh" -for board in u-boot/*; do - if [ "${board##*/}" = "u-boot" ] || [ ! -d "${board}" ]; then - continue - fi +main() +{ + printf "Cleaning u-boot builds for all boards\n" + clean_uboot +} - make -C "${board}/" distclean +clean_uboot() +{ + for board in u-boot/*; do + if [ "${board##*/}" = "u-boot" ] || [ ! -d "${board}" ]; then + continue + fi - [ -e "${board}/.git" ] && \ - git -C "${board}" clean -fdx -done + make -C "${board}/" distclean || \ + err "cannot distclean u-boot dir, ${board}" -printf "\n\n" + [ ! -e "${board}/.git" ] || \ + git -C "${board}" clean -fdx || \ + err "cannot clean u-boot git repo, ${board}" + done +} + +main $@ diff --git a/resources/scripts/build/coreboot/utils b/resources/scripts/build/coreboot/utils index e24378d3..d381f9e2 100755 --- a/resources/scripts/build/coreboot/utils +++ b/resources/scripts/build/coreboot/utils @@ -21,10 +21,12 @@ [ "x${DEBUG+set}" = 'xset' ] && set -v set -u -e -printf "Building coreboot utils\n" +. "include/err.sh" main() { + printf "Building coreboot utils\n" + if [ $# -gt 0 ]; then for board in "${@}"; do build_for_mainboard ${board} || \ @@ -45,28 +47,28 @@ build_for_mainboard() { [ -f "resources/coreboot/${board}/target.cfg" ] || continue tree="undefined" . "resources/coreboot/${board}/target.cfg" # source - if [ "${tree}" = "undefined" ]; then - printf "build/cbutils: improper tree definition for '%s'" \ - "${board}" - return 1 - fi - buildutils "${tree}" || return 1 + [ "${tree}" = "undefined" ] && \ + err "build/cbutils: improper tree definition for '${board}'" + buildutils "${tree}" } buildutils() { tree="${1}" [ -d "coreboot/${tree}/" ] || \ - ./fetch_trees coreboot $tree || return 1 + ./fetch_trees coreboot $tree || \ + err "cannot fetch ${tree}" for util in cbfstool ifdtool; do [ -f "cbutils/${tree}/${util}" ] && continue [ -d "cbutils/${tree}" ] || \ - mkdir -p "cbutils/${tree}" || return 1 + mkdir -p "cbutils/${tree}" || \ + err "cannot create directory, cbutils/${tree}" utildir="coreboot/${tree}/util/${util}" - make distclean -C "${utildir}" || return 1 - make -j$(nproc) -C "${utildir}" || return 1 - mv "${utildir}/${util}" "cbutils/${tree}" || return 1 - make distclean -C "${utildir}" || return 1 + make distclean -C "${utildir}" || err "cannot clean ${utildir}" + make -j$(nproc) -C "${utildir}" || err "cannot build ${utildir}" + cp "${utildir}/${util}" "cbutils/${tree}" || \ + err "cannot copy util, ${util}, to cbutils/${tree}/" + make distclean -C "${utildir}" || err "can't clean ${utildir}" done } diff --git a/resources/scripts/build/grub/payload b/resources/scripts/build/grub/payload index 4886a874..7f1d239f 100755 --- a/resources/scripts/build/grub/payload +++ b/resources/scripts/build/grub/payload @@ -21,6 +21,8 @@ [ "x${DEBUG+set}" = 'xset' ] && set -v set -u -e +. "include/err.sh" + grubcfgsdir="resources/grub" keymap="" @@ -30,26 +32,29 @@ main() { printf "Creating GRUB payloads and configuration files\n" - [ ! -d "grub/" ] && \ - ./fetch grub - [ ! -f "grub/grub-mkstandalone" ] && \ - ./build grub utils - [ ! -d "elf/" ] && \ - mkdir -p elf/ - [ ! -d "elf/grub" ] && \ - mkdir -p elf/grub/ - - rm -f elf/grub/* + handle_dependencies # Separate GRUB payload per keymap to save space in ROM. - for keylayoutfile in ${grubcfgsdir}/keymap/*.gkb; do + [ -f "${keylayoutfile}" ] || continue build_grub_payloads "${keylayoutfile}" done printf "Done! Check elf/grub/ to see the files.\n\n" } +handle_dependencies() +{ + [ -d "grub/" ] || \ + ./fetch grub || err "cannot fetch grub" + [ -f "grub/grub-mkstandalone" ] || \ + ./build grub utils || err "cannot build grub utils" + [ -d "elf/grub" ] || \ + mkdir -p elf/grub || err "cannot create directory, elf/grub" + + rm -f elf/grub/* || err "cannot delete files in directory, elf/grub/" +} + build_grub_payloads() { keylayoutfile=${1} @@ -79,15 +84,16 @@ build_grub_elf() --fonts= --themes= --locales= \ --modules="${grub_modules}" \ --install-modules="${grub_install_modules}" \ - ${gcfg} ${grubk} + ${gcfg} ${grubk} || \ + err "cannot create grub payload (grub-mkstandalone)" } create_grub_config() { sed "s/usqwerty/${keymap}/" < ${grubcfgsdir}/config/grub.cfg \ - > elf/grub/grub_${keymap}.cfg + > elf/grub/grub_${keymap}.cfg || err "sed failed: grub.cfg" sed "s/grubtest.cfg/grub.cfg/" < elf/grub/grub_${keymap}.cfg \ - > elf/grub/grub_${keymap}_test.cfg + > elf/grub/grub_${keymap}_test.cfg || err "sed failed: grubtest.cfg" } main $@ diff --git a/resources/scripts/build/src/for b/resources/scripts/build/src/for index 57459952..6945b9f9 100755 --- a/resources/scripts/build/src/for +++ b/resources/scripts/build/src/for @@ -46,8 +46,14 @@ main() [ -z "${project}" ] && err "project name not specified" [ "${project}" = "ich9utils" ] && project="util/ich9utils" - [ -d "${project}" ] || ./fetch "${project%/*}" \ - || err "Cannot download project, ${project%/*}" + handle_dependencies + run_make_command +} + +handle_dependencies() +{ + [ -d "${project}" ] || ./fetch "${project%/*}" || \ + err "Cannot download project, ${project%/*}" [ -d "${project}" ] || err "Project, ${project%/*}, not downloaded" if [ "${project}" = "uefitool" ]; then @@ -56,6 +62,10 @@ main() cmake UEFIExtract/ || err "cannot cmake UEFIExtract" ) fi +} + +run_make_command() +{ [ ! -z "${mode}" ] || \ make -C "${project}" || err "'make' failed (${project})" [ -z "${mode}" ] || \ diff --git a/resources/scripts/misc/versioncheck b/resources/scripts/misc/versioncheck index 01e69231..9ca82091 100755 --- a/resources/scripts/misc/versioncheck +++ b/resources/scripts/misc/versioncheck @@ -17,7 +17,7 @@ # [ "x${DEBUG+set}" = 'xset' ] && set -v -set -u -e +set -u version="version-unknown" [ -f version ] && version="$(cat version)" -- cgit v1.2.1