From 4c6c7d1088eb9dc0c9b2eeeb64febeeb78038583 Mon Sep 17 00:00:00 2001
From: Leah Rowe <leah@libreboot.org>
Date: Wed, 23 Aug 2023 19:56:01 +0100
Subject: scripts: never exit 1, always call err instead

this same change has been applied, selectively, to
certain return statements. the general rule is this:
the return statement should only be used to direct
logic within a script, where certain non-errors
states are used to skip certain actions; the exit
command should *never* be used to return non-zero,
except by err(). in so doing, we ensure easier
debugging of the build system

also: strip_rom_image in build/release/roms was
running "continue" when a rom file didn't exist,
despite not being a while/for loop. i make it
return (non-error condition) instead

it's ok for a script to exit 0, where appropriate,
but perhaps a function could also be written for it

Signed-off-by: Leah Rowe <leah@libreboot.org>
---
 resources/scripts/update/blobs/download | 10 ++++---
 resources/scripts/update/blobs/inject   | 52 +++++++++++++++------------------
 resources/scripts/update/blobs/mrc      | 23 +++++++--------
 3 files changed, 39 insertions(+), 46 deletions(-)

(limited to 'resources/scripts/update/blobs')

diff --git a/resources/scripts/update/blobs/download b/resources/scripts/update/blobs/download
index 9d623604..c1babd13 100755
--- a/resources/scripts/update/blobs/download
+++ b/resources/scripts/update/blobs/download
@@ -237,7 +237,7 @@ bruteforce_extract_blob_intel_me()
 
 	(
 	printf "Entering %s\n" "${cdir}"
-	cd "${cdir}" || exit 1
+	cd "${cdir}" || err "cannot enter directory, ${cdir}"
 	for i in *; do
 		if [ -f "${_me_destination}" ]; then
 			# me.bin found, so avoid needless further traversal
@@ -406,7 +406,8 @@ extract_sch5545ec()
 
 	mkdir -p "${appdir}/"
 	cp "${dl_path}" "${appdir}/"
-	python "${pfs_extract}" "${appdir}/${dlsum}" -e || exit 1
+	python "${pfs_extract}" "${appdir}/${dlsum}" -e || \
+	    err "cannot extract archive (dell, sch5545)"
 
 	# full system ROM (UEFI), to extract with UEFIExtract:
 	_bios="${appdir}/${dlsum}_extracted/Firmware"
@@ -418,9 +419,10 @@ extract_sch5545ec()
 	_sch5545ec_fw="${_sch5545ec_fw}/0 Raw section/body.bin" # <-- this!
 
 	# this makes the file defined by _sch5545ec_fw available to copy
-	"${uefiextract}" "${_bios}" || exit 1
+	"${uefiextract}" "${_bios}" || err "cannot extract dell uefi image"
 
-	cp "${_sch5545ec_fw}" "${_sch5545ec_destination}" || exit 1
+	cp "${_sch5545ec_fw}" "${_sch5545ec_destination}" || \
+	    err "cannot copy sch5545ec firmware file"
 }
 
 fetch_update()
diff --git a/resources/scripts/update/blobs/inject b/resources/scripts/update/blobs/inject
index 496d845c..a6f6c007 100755
--- a/resources/scripts/update/blobs/inject
+++ b/resources/scripts/update/blobs/inject
@@ -74,17 +74,16 @@ check_board()
 {
 	if ! check_release ${archive} ; then
 		[ -f "${rom}" ] || \
-			err "${rom} is not a valid path"
+			err "\"${rom}\" is not a valid path"
 		[ -z ${rom+x} ] && \
-			err 'no rom specified'
+			err "no rom specified"
 		[ ! -z ${board+x} ] || \
 			board=$(detect_board ${rom}) || \
-			    err 'no board specified'
+			    err "no board specified"
 	else
 		release=true
 		releasearchive="${archive}"
-		board=$(detect_board ${archive}) || \
-		    err 'Could not detect board type'
+		board=$(detect_board ${archive})
 	fi
 
 	boarddir="${cbcfgsdir}/${board}"
@@ -113,9 +112,10 @@ detect_board()
 		_stripped_prefix=${filename#*_}
 		board="${_stripped_prefix%.tar.xz}" ;;
 	*)
-		return 1
+		err "detect_board: could not detect board type"
 	esac	
-	[ -d "${boarddir}/" ] || return 1
+	[ -d "${boarddir}/" ] || \
+	    err "detect_board: dir, ${boarddir}, doesn't exist"
 	printf '%s\n' "${board}"
 }
 
@@ -216,7 +216,7 @@ inject_blob_intel_mrc()
 	# TODO: this logic should be tweaked to handle more platforms
 
 	${cbfstool} ${rom} add -f mrc/haswell/mrc.bin -n mrc.bin -t mrc \
-		-b 0xfffa0000 || exit 1
+		-b 0xfffa0000 || err "cannot insert mrc.bin"
 }
 
 inject_blob_intel_me()
@@ -231,7 +231,8 @@ inject_blob_intel_me()
 	[ ! -f "${_me_location}" ] && \
 		err "CONFIG_ME_BIN_PATH points to missing file"
 
-	${ifdtool} -i me:${_me_location} ${rom} -O ${rom} || exit 1
+	${ifdtool} -i me:${_me_location} ${rom} -O ${rom} || \
+	    err "cannot insert me.bin"
 }
 
 inject_blob_hp_kbc1126_ec()
@@ -246,24 +247,19 @@ inject_blob_hp_kbc1126_ec()
 	printf "adding hp kbc1126 ec firmware\n"
 
 	if [ "${_ec1_offset}" = "" ] || [ "${_ec1_offset}" = "" ]; then
-		printf "EC offsets not declared for board: %s\n" \
-		    "${board}"
-		exit 1
+		err "EC offsets not declared for board, ${board}"
 	fi
 	if [ "${_ec1_location}" = "" ] || [ "${_ec2_location}" = "" ]; then
-		printf "EC firmware path not declared for board: %s\n" \
-		    "${board}"
+		err "EC firmware path not declared for board, ${board}"
 	fi
 	if [ ! -f "${_ec1_location}" ] || [ ! -f "${_ec2_location}" ]; then
-		printf "EC firmware not downloaded for board: %s\n" \
-		    "${board}"
-		exit 1
+		err "EC firmware not downloaded for board: ${board}"
 	fi
 
 	${cbfstool} "${rom}" add -f ${_ec1_location} -n ecfw1.bin \
-	    -b ${_ec1_offset} -t raw || exit 1
+	    -b ${_ec1_offset} -t raw || err "cannot insert ecfw1.bin"
 	${cbfstool} "${rom}" add -f ${_ec2_location} -n ecfw2.bin \
-	    -b ${_ec2_offset} -t raw || exit 1
+	    -b ${_ec2_offset} -t raw || err "cannot insert ecfw2.bin"
 }
 
 inject_blob_dell_e6400_vgarom_nvidia()
@@ -277,17 +273,15 @@ inject_blob_dell_e6400_vgarom_nvidia()
 	printf "adding pci option rom\n"
 
 	if [ "${_vga_dir}" != "${pciromsdir}" ]; then
-		printf "Invalid PCI ROM directory: %s\n" ${_vga_dir}
-		exit 1
+		err "Invalid PCI ROM directory, ${_vga_dir}"
 	fi
 	if [ ! -f "${_vga_location}" ]; then
-		printf "No such file exists: %s\n" ${_vga_location}
-		exit 1
+		err "No such file exists, ${_vga_location}"
 	fi
 
 	${cbfstool} ${rom} add -f "${_vga_location}" \
-	    -n "pci${CONFIG_VGA_BIOS_ID}.rom" \
-	    -t optionrom || exit 1
+	    -n "pci${CONFIG_VGA_BIOS_ID}.rom" -t optionrom || \
+	    err "cannot insert e6400 nvidia rom"
 }
 
 inject_blob_smsc_sch5545_ec()
@@ -297,12 +291,11 @@ inject_blob_smsc_sch5545_ec()
 	_sch5545ec_location="${CONFIG_SMSC_SCH5545_EC_FW_FILE#../../}"
 
 	if [ ! -f "${_sch5545ec_location}" ]; then
-		printf "SCH5545 firmware file missing\n" 1>&2
-		exit 1
+		err "SCH5545 firmware file missing"
 	fi
 
 	"${cbfstool}" "${rom}" add -f "${_sch5545ec_location}" \
-	    -n sch5545_ecfw.bin -t raw || exit 1
+	    -n sch5545_ecfw.bin -t raw || err "cannot insert sch5545_ecfw.bin"
 }
 
 modify_gbe()
@@ -326,7 +319,8 @@ modify_gbe()
 	${nvmutil} "${_gbe_tmp}" setmac ${new_mac} || \
 	    err 'failed to modify mac address'
 
-	${ifdtool} -i GbE:${_gbe_tmp} "${rom}" -O "${rom}" || exit 1
+	${ifdtool} -i GbE:${_gbe_tmp} "${rom}" -O "${rom}" || \
+	    err "cannot insert modified gbe.bin"
 
 	rm -f ${_gbe_tmp}
 }
diff --git a/resources/scripts/update/blobs/mrc b/resources/scripts/update/blobs/mrc
index b4502c14..57cbede6 100755
--- a/resources/scripts/update/blobs/mrc
+++ b/resources/scripts/update/blobs/mrc
@@ -53,33 +53,33 @@ main()
 	sname=${0}
 	printf "Downloading Intel MRC blobs\n"
 
-	check_existing && exit 0
-	build_dependencies || err "could not build dependencies"
+	check_existing || return 0
+	build_dependencies
 	fetch_mrc || err "could not fetch mrc.bin"
 }
 
 check_existing()
 {
 	[ -f ${_mrc_complete} ] || \
-		return 1
+		return 0
 	printf 'found existing mrc.bin\n'
 	[ "$(sha1sum ${_mrc_complete} | awk '{print $1}')" \
 	    = "${_mrc_complete_hash}" ] && \
-		return 0
+		return 1
 	printf 'hashes did not match, starting over\n'
-	return 1
 }
 
 build_dependencies()
 {
-	[ -d "${cbdir}/" ] || ./fetch_trees coreboot default || return 1
-	./build coreboot utils default || return 1
-	return 0
+	[ -d "${cbdir}/" ] || ./fetch_trees coreboot default || \
+	    err "cannot fetch coreboot/default"
+	./build coreboot utils default || \
+	    err "cannot build cbutils/default"
 }
 
 fetch_mrc()
 {
-	mkdir -p mrc/haswell/ || return 1
+	mkdir -p mrc/haswell/ || err "cannot mkdir mrc/haswell"
 
 	(
 	cd mrc/haswell/
@@ -102,8 +102,6 @@ fetch_mrc()
 
 	printf "\n\nmrc.bin saved to ${_mrc_complete}\n\n"
 	)
-
-	return 0
 }
 
 download_image()
@@ -122,8 +120,7 @@ download_image()
 		return 0
 	fi
 	rm "${_file}.zip"
-	printf "Bad checksum. Recovery image deleted.\n"
-	return 1
+	err "Bad checksum. Recovery image deleted"
 }
 
 extract_partition()
-- 
cgit v1.2.1