build/boot/roms_helper: shorten variable names

also: further reduce the number of arguments passed,
to certain functions as and when feasible, in cases
where those are global variables that never change.

the cbfstool argument in mkUbootRom wasn't even used.
that function was only using the global variable, which
again is only set once.

i also shortened a few messages, removed a few errant
line breaks and reduced sloccount by exactly 1 in main()
by re-arranging how the shift command is used.

it's mainly about shortening variable names, to then
reduce the number of line breaks, but it's a surgical
code size reduction in build/boot/roms.

Signed-off-by: Leah Rowe <leah@libreboot.org>
btrfsvols
Leah Rowe 2023-10-01 02:12:42 +01:00
parent 2a6fcf7022
commit 92c6da7b21
3 changed files with 69 additions and 93 deletions

View File

@ -3,7 +3,7 @@
# SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com> # SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
# SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org> # SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org>
eval "$(setvars "" first board boards _displaymode _payload _keyboard_layout)" eval "$(setvars "" first board boards _displaymode _payload _keyboard)"
main() main()
{ {
@ -16,21 +16,17 @@ main()
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case ${1} in case ${1} in
-d) -d) _displaymode="${2}" ;;
_displaymode="${2}" -p) _payload="${2}" ;;
shift ;; -k) _keyboard="${2}" ;;
-p)
_payload="${2}"
shift ;;
-k)
_keyboard_layout="${2}"
shift ;;
all) all)
first="all" ;; first="all"
continue ;;
*) *)
boards="${1} ${boards}" ;; boards="${1} ${boards}"
continue ;;
esac esac
shift shift 2
done done
handle_targets handle_targets

View File

@ -21,8 +21,7 @@ handle_targets()
{ {
[ -z "${_displaymode}" ] || _displaymode="-d ${_displaymode}" [ -z "${_displaymode}" ] || _displaymode="-d ${_displaymode}"
[ -z "${_payload}" ] || _payload="-p ${_payload}" [ -z "${_payload}" ] || _payload="-p ${_payload}"
[ -z "${_keyboard_layout}" ] || \ [ -z "${_keyboard}" ] || _keyboard="-k ${_keyboard}"
_keyboard_layout="-k ${_keyboard_layout}"
printf "Building %s ROM images\n" "${projectname}" printf "Building %s ROM images\n" "${projectname}"
@ -44,10 +43,9 @@ check_targets()
build_bootroms() build_bootroms()
{ {
opts="${_displaymode} ${_payload} ${_keyboard_layout}" opts="${_displaymode} ${_payload} ${_keyboard}"
for x in ${boards}; do for x in ${boards}; do
./build boot roms_helper ${_displaymode} ${_payload} \ ./build boot roms_helper ${opts} ${x} || \
${_keyboard_layout} ${x} || \
err "handle_targets ${opts} ${x}: build error" err "handle_targets ${opts} ${x}: build error"
[ -d "bin/${x}" ] && targets="${x} ${targets}" [ -d "bin/${x}" ] && targets="${x} ${targets}"
done done

View File

@ -25,7 +25,7 @@ kmapdir="config/grub/keymap"
eval "$(setvars "n" payload_grub payload_grub_withseabios payload_seabios \ eval "$(setvars "n" payload_grub payload_grub_withseabios payload_seabios \
payload_seabios_withgrub payload_memtest payload_uboot)" payload_seabios_withgrub payload_memtest payload_uboot)"
eval "$(setvars "" romdir cbdir cbfstool corebootrom initmode displaymode \ eval "$(setvars "" romdir cbdir cbfstool cbrom initmode displaymode \
cbcfg targetdir grub_timeout ubdir blobs_required)" cbcfg targetdir grub_timeout ubdir blobs_required)"
eval "$(setvars "undefined" grub_scan_disk tree arch uboot_config)" eval "$(setvars "undefined" grub_scan_disk tree arch uboot_config)"
@ -108,7 +108,7 @@ build_dependencies()
cbdir="coreboot/${board}" cbdir="coreboot/${board}"
[ "${board}" = "${tree}" ] || cbdir="coreboot/${tree}" [ "${board}" = "${tree}" ] || cbdir="coreboot/${tree}"
cbfstool="cbutils/${tree}/cbfstool" cbfstool="cbutils/${tree}/cbfstool"
corebootrom="${cbdir}/build/coreboot.rom" cbrom="${cbdir}/build/coreboot.rom"
./build coreboot utils ${tree} || err "cannot build cbutils/${tree}" ./build coreboot utils ${tree} || err "cannot build cbutils/${tree}"
@ -144,7 +144,7 @@ build_dependency_grub()
[ "${payload_seabios_withgrub}" != "y" ] && return 0 [ "${payload_seabios_withgrub}" != "y" ] && return 0
rebuild_grub="n" rebuild_grub="n"
[ ! -f "${grubelf}" ] && rebuild_grub="y" [ -f "${grubelf}" ] || rebuild_grub="y"
for keymapfile in "${kmapdir}"/*.gkb; do for keymapfile in "${kmapdir}"/*.gkb; do
[ "${rebuild_grub}" = "y" ] || break [ "${rebuild_grub}" = "y" ] || break
[ -f "${keymapfile}" ] || continue [ -f "${keymapfile}" ] || continue
@ -196,7 +196,6 @@ build_target()
build_roms() build_roms()
{ {
cbcfg="${1}" cbcfg="${1}"
[ ! -f "${cbcfg}" ] && \ [ ! -f "${cbcfg}" ] && \
printf "'%s' does not exist. Skipping build for %s %s %s\n" \ printf "'%s' does not exist. Skipping build for %s %s %s\n" \
"${cbcfg}" "${board}" "${displaymode}" "${initmode}" \ "${cbcfg}" "${board}" "${displaymode}" "${initmode}" \
@ -205,22 +204,22 @@ build_roms()
./handle make config -b coreboot ${board} || \ ./handle make config -b coreboot ${board} || \
err "build_roms: cannot build coreboot for target: ${board}" err "build_roms: cannot build coreboot for target: ${board}"
_corebootrom="elf/coreboot/${board}/${initmode}_${displaymode}" _cbrom="elf/coreboot/${board}/${initmode}_${displaymode}"
[ "${initmode}" = "normal" ] && \ [ "${initmode}" = "normal" ] && \
_corebootrom="${_corebootrom%_${displaymode}}" _cbrom="${_cbrom%_${displaymode}}"
_corebootrom="${_corebootrom}/coreboot.rom" _cbrom="${_cbrom}/coreboot.rom"
corebootrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)" cbrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
cp "${_corebootrom}" "${corebootrom}" || \ cp "${_cbrom}" "${cbrom}" || \
err "build_roms: cannot copy rom" err "build_roms: cannot copy rom"
[ "${payload_memtest}" != "y" ] || \ [ "${payload_memtest}" != "y" ] || \
"${cbfstool}" "${corebootrom}" add-payload \ "${cbfstool}" "${cbrom}" add-payload \
-f "${memtest_bin}" -n img/memtest -c lzma || \ -f "${memtest_bin}" -n img/memtest -c lzma || \
err "build_roms: cannot add img/memtest to coreboot rom" err "build_roms: cannot add img/memtest to coreboot rom"
[ "${payload_seabios}" = "y" ] && \ [ "${payload_seabios}" = "y" ] && \
build_seabios_roms build_seabios_roms
[ "${payload_grub}" != "y" ] || \ [ "${payload_grub}" != "y" ] || \
build_grub_roms "${corebootrom}" "grub" || \ build_grub_roms "${cbrom}" "grub" || \
err "build_roms: build_grub_roms failed" err "build_roms: build_grub_roms failed"
[ "${payload_uboot}" = "y" ] || return 0 [ "${payload_uboot}" = "y" ] || return 0
build_uboot_roms build_uboot_roms
@ -230,13 +229,13 @@ build_seabios_roms()
{ {
if [ "${payload_seabios_withgrub}" = "y" ]; then if [ "${payload_seabios_withgrub}" = "y" ]; then
tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX) tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
cp "${corebootrom}" "${tmprom}" || \ cp "${cbrom}" "${tmprom}" || \
err "build_seabios_roms: cannot copy to tmprom" err "build_seabios_roms: cannot copy to tmprom"
build_grub_roms "${tmprom}" "seabios_withgrub" || \ build_grub_roms "${tmprom}" "seabios_withgrub" || \
err "build_roms: cannot build grub roms, seabios w/grub" err "build_roms: cannot build grub roms, seabios w/grub"
rm -f "${tmprom}" || err "build_roms: can't remove tmprom" rm -f "${tmprom}" || err "build_roms: can't remove tmprom"
else else
t=$(mkSeabiosRom "${corebootrom}" "fallback/payload") || \ 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}" newrom="${romdir}/seabios_${board}_${initmode}_${displaymode}"
[ "${initmode}" = "normal" ] && newrom="${romdir}/seabios" \ [ "${initmode}" = "normal" ] && newrom="${romdir}/seabios" \
@ -250,33 +249,30 @@ build_seabios_roms()
# Make separate ROM images with GRUB payload, for each supported keymap # Make separate ROM images with GRUB payload, for each supported keymap
build_grub_roms() { build_grub_roms() {
tmprompath="${1}" tmprom="${1}"
firstpayloadname="${2}" # allow values: grub, seabios, seabios_withgrub payload1="${2}" # allow values: grub, seabios, seabios_withgrub
grubelf_cbfs="fallback/payload" grub_cbfs="fallback/payload"
if [ "${payload_grub_withseabios}" = "y" ] && \ if [ "${payload_grub_withseabios}" = "y" ] && \
[ "${firstpayloadname}" = "grub" ]; then [ "${payload1}" = "grub" ]; then
mv "$(mkSeabiosRom "${tmprompath}" "seabios.elf")" \ mv "$(mkSeabiosRom "${tmprom}" "seabios.elf")" "${tmprom}" || \
"${tmprompath}" || \
err "build_grub_roms: cannot move tmp rom (seabios.elf)" err "build_grub_roms: cannot move tmp rom (seabios.elf)"
elif [ "${payload_seabios_withgrub}" = "y" ] && \ elif [ "${payload_seabios_withgrub}" = "y" ] && \
[ "${firstpayloadname}" != "grub" ]; then [ "${payload1}" != "grub" ]; then
mv "$(mkSeabiosRom "${tmprompath}" "fallback/payload")" \ mv "$(mkSeabiosRom "${tmprom}" fallback/payload)" "${tmprom}" \
"${tmprompath}" || \ || err "build_grub_roms: cannot move SeaBIOS ROM to tmprom"
err "build_grub_roms: cannot move SeaBIOS ROM to tmprom" grub_cbfs="img/grub2"
grubelf_cbfs="img/grub2"
fi fi
# we only need insert grub.elf once, for each coreboot config: # we only need insert grub.elf once, for each coreboot config:
"${cbfstool}" "${tmprompath}" add-payload -f "${grubelf}" \ "${cbfstool}" "${tmprom}" add-payload -f "${grubelf}" -n ${grub_cbfs} \
-n ${grubelf_cbfs} -c lzma || \ -c lzma || err "build_grub_roms: cannot add grub payload to tmprom"
err "build_grub_roms: cannot add grub payload to tmprom"
# we only need insert background.png once, for each coreboot config: # we only need insert background.png once, for each coreboot config:
if [ "${displaymode}" = "vesafb" ] || \ if [ "${displaymode}" = "vesafb" ] || \
[ "${displaymode}" = "corebootfb" ]; then [ "${displaymode}" = "corebootfb" ]; then
backgroundfile="config/grub/background/${grub_background}" backgroundfile="config/grub/background/${grub_background}"
"${cbfstool}" "${tmprompath}" add -f ${backgroundfile} \ "${cbfstool}" "${tmprom}" add -f ${backgroundfile} \
-n background.png -t raw || \ -n background.png -t raw || \
err "build_grub_roms: cannot add background.png to tmprom" err "build_grub_roms: cannot add background.png to tmprom"
@ -287,38 +283,31 @@ build_grub_roms() {
printf "set grub_scan_disk=\"%s\"\n" "${grub_scan_disk}" > \ printf "set grub_scan_disk=\"%s\"\n" "${grub_scan_disk}" > \
"${tmpcfg}" || err "build_grub_roms: cannot create scan.cfg" "${tmpcfg}" || err "build_grub_roms: cannot create scan.cfg"
[ "${grub_scan_disk}" = "both" ] || \ [ "${grub_scan_disk}" = "both" ] || \
"${cbfstool}" "${tmprompath}" add -f "${tmpcfg}" -n scan.cfg \ "${cbfstool}" "${tmprom}" add -f "${tmpcfg}" -n scan.cfg -t \
-t raw || err "build_grub_roms: cannot insert scan.cfg into CBFS" raw || err "build_grub_roms: can't insert scan.cfg"
printf "set timeout=%s\n" "${grub_timeout}" > "${tmpcfg}" || \ printf "set timeout=%s\n" "${grub_timeout}" > "${tmpcfg}" || \
err "build_grub_roms: cannot insert into temporary timeout.cfg" err "build_grub_roms: can't create timeout.cfg"
[ -z "${grub_timeout}" ] || \ [ -z "${grub_timeout}" ] || \
"${cbfstool}" "${tmprompath}" add -f "${tmpcfg}" -n timeout.cfg \ "${cbfstool}" "${tmprom}" add -f "${tmpcfg}" -n timeout.cfg \
-t raw || err "build_grub_roms: cannot create timeout.cfg" -t raw || err "build_grub_roms: can't insert timeout.cfg"
rm -f "${tmpcfg}" || err "cannot delete tmpcfg" rm -f "${tmpcfg}" || err "build_grub_roms: can't delete tmpcfg"
keymaps="" keymaps=""
for kmapfile in "${kmapdir}"/*; do for kmapfile in "${kmapdir}"/*; do
keymaps="${keymaps} ${kmapfile}" keymaps="${keymaps} ${kmapfile}"
done done
[ -z ${_keyboard_layout} ] || \ [ -z ${_keyboard} ] || keymaps="${kmapdir}/${_keyboard}.gkb"
keymaps="${kmapdir}/${_keyboard_layout}.gkb"
for keymapfile in ${keymaps}; do for keymapfile in ${keymaps}; do
printf "keymaps is %s, keymapfile is %s\n" \
"${keymaps}" "${keymapfile}"
[ -f "${keymapfile}" ] || continue [ -f "${keymapfile}" ] || continue
keymap="${keymapfile##*/}" keymap="${keymapfile##*/}"
keymap="${keymap%.gkb}" keymap="${keymap%.gkb}"
tmpgrubrom="$(mkGrubRom "${keymap}" "${tmprom}")"
tmpgrubrom="$(mkGrubRom "${keymap}" "${tmprompath}")" newrom="${romdir}/${payload1}_${board}_${initmode}_"
newrom="${romdir}/${firstpayloadname}_${board}_${initmode}_"
newrom="${newrom}${displaymode}_${keymap}.rom" newrom="${newrom}${displaymode}_${keymap}.rom"
[ "${initmode}" = "normal" ] && \ [ "${initmode}" = "normal" ] && \
newrom="${romdir}/${firstpayloadname}_${board}_" && \ newrom="${romdir}/${payload1}_${board}_" && \
newrom="${newrom}${initmode}_${keymap}.rom" newrom="${newrom}${initmode}_${keymap}.rom"
moverom "${tmpgrubrom}" "${newrom}" || \ moverom "${tmpgrubrom}" "${newrom}" || \
err "build_grub_roms, moverom" err "build_grub_roms, moverom"
@ -328,15 +317,14 @@ build_grub_roms() {
# make a rom in /tmp/ and then print the path of that ROM # make a rom in /tmp/ and then print the path of that ROM
mkGrubRom() { mkGrubRom() {
target_keymap="${1}" _keymap="${1}"
target_cbrom="${2}" _cbrom="${2}"
keymapcfg="elf/grub/keymap_${target_keymap}.cfg" keymapcfg="elf/grub/keymap_${_keymap}.cfg"
tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX) || \ tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX) || \
err "mkGrubRom: cannot create tmprom" err "mkGrubRom: cannot create tmprom"
cp "${target_cbrom}" "${tmprom}" || \ cp "${_cbrom}" "${tmprom}" || err "mkGrubRom: cannot copy to tmprom"
err "mkGrubRom: cannot copy to tmprom"
"${cbfstool}" "${tmprom}" add -f "${keymapcfg}" -n keymap.cfg -t raw \ "${cbfstool}" "${tmprom}" add -f "${keymapcfg}" -n keymap.cfg -t raw \
|| err "mkGrubRom: cannot add keymap.cfg to tmprom" || err "mkGrubRom: cannot add keymap.cfg to tmprom"
@ -345,17 +333,17 @@ mkGrubRom() {
# make a rom in /tmp/ and then print the path of that ROM # make a rom in /tmp/ and then print the path of that ROM
mkSeabiosRom() { mkSeabiosRom() {
target_cbrom="${1}" # rom to insert seabios in. will not be touched _cbrom="${1}" # rom to insert seabios in. will not be touched
# (a tmpfile will be made instead) # (a tmpfile will be made instead)
target_seabios_cbfs_path="${2}" # e.g. fallback/payload _seabios_cbfs_path="${2}" # e.g. fallback/payload
target_seabioself="elf/seabios/default/${initmode}/bios.bin.elf" _seabioself="elf/seabios/default/${initmode}/bios.bin.elf"
tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX) tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
cp "${target_cbrom}" "${tmprom}" || \ cp "${_cbrom}" "${tmprom}" || \
err "mkSeabiosRom: cannot copy to tmprom" err "mkSeabiosRom: cannot copy to tmprom"
"${cbfstool}" "${tmprom}" add-payload -f "${target_seabioself}" \ "${cbfstool}" "${tmprom}" add-payload -f "${_seabioself}" \
-n ${target_seabios_cbfs_path} -c lzma || \ -n ${_seabios_cbfs_path} -c lzma || \
err "mkSeabiosRom: can't add payload, ${target_seabioself}, to rom" err "mkSeabiosRom: can't add payload, ${_seabioself}, to rom"
"${cbfstool}" "${tmprom}" add-int -i 3000 -n etc/ps2-keyboard-spinup \ "${cbfstool}" "${tmprom}" add-int -i 3000 -n etc/ps2-keyboard-spinup \
|| err "mkSeabiosRom: cbfs add-int etc/ps2-keyboard-spinup 3000" || err "mkSeabiosRom: cbfs add-int etc/ps2-keyboard-spinup 3000"
@ -374,8 +362,7 @@ mkSeabiosRom() {
build_uboot_roms() build_uboot_roms()
{ {
tmprom="$(mkUbootRom "${corebootrom}" "fallback/payload" \ tmprom="$(mkUbootRom "${cbrom}" "fallback/payload")"
"${uboot_config}" "${cbfstool}")"
newrom="${romdir}/uboot_payload_${board}_${initmode}_${displaymode}.rom" newrom="${romdir}/uboot_payload_${board}_${initmode}_${displaymode}.rom"
[ "${initmode}" = "normal" ] && \ [ "${initmode}" = "normal" ] && \
newrom="${romdir}/uboot_payload_${board}_${initmode}.rom" newrom="${romdir}/uboot_payload_${board}_${initmode}.rom"
@ -386,25 +373,21 @@ build_uboot_roms()
# make a rom in /tmp/ and then print the path of that ROM # make a rom in /tmp/ and then print the path of that ROM
mkUbootRom() { mkUbootRom() {
target_cbrom="${1}" # rom to insert u-boot in. it won't be touched _cbrom="${1}" # rom to insert u-boot in. it won't be touched
# (a tmpfile will be made instead) # (a tmpfile will be made instead)
target_uboot_cbfs_path="${2}" # e.g. fallback/payload _uboot_cbfs_path="${2}" # e.g. fallback/payload
target_uboot_config="${3}"
cbfstool_path="${4}"
target_ubdir="elf/u-boot/${board}/${target_uboot_config}" _ubdir="elf/u-boot/${board}/${uboot_config}"
target_ubootelf="${target_ubdir}/u-boot.elf" _ubootelf="${_ubdir}/u-boot.elf"
[ -f "${target_ubootelf}" ] || \ [ -f "${_ubootelf}" ] || _ubootelf="${_ubdir}/u-boot.bin"
target_ubootelf="${target_ubdir}/u-boot.bin" [ -f "${_ubootelf}" ] || err "mkUbootRom: ${board}: cant find u-boot"
[ -f "${target_ubootelf}" ] || \
err "mkUbootRom: ${board}: cant find u-boot build"
tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX) tmprom=$(mktemp -t coreboot_rom.XXXXXXXXXX)
cp "${target_cbrom}" "${tmprom}" || \ cp "${_cbrom}" "${tmprom}" || \
err "mkUbootRom: cannot copy to tmprom" err "mkUbootRom: cannot copy to tmprom"
"${cbfstool}" "${tmprom}" add-payload -f "${target_ubootelf}" \ "${cbfstool}" "${tmprom}" add-payload -f "${_ubootelf}" \
-n ${target_uboot_cbfs_path} -c lzma || \ -n ${_uboot_cbfs_path} -c lzma || \
err "mkUbootRom: cannot add u-boot to tmprom" err "mkUbootRom: cannot add u-boot to tmprom"
printf "%s\n" "${tmprom}" printf "%s\n" "${tmprom}"
@ -416,12 +399,11 @@ moverom() {
rompath="${1}" rompath="${1}"
newrom="${2}" newrom="${2}"
printf "\nCreating new ROM image: %s\n" "${newrom}" printf "Creating target image: %s\n" "${newrom}"
[ -d "${newrom%/*}" ] || mkdir -p "${newrom%/*}/" || \ [ -d "${newrom%/*}" ] || mkdir -p "${newrom%/*}/" || \
err "moverom: cannot create rom directory: \"${newrom%/*}\"" err "moverom: cannot create rom directory: \"${newrom%/*}\""
[ "${blobs_required}" = "n" ] && \ [ "${blobs_required}" = "n" ] && newrom="${newrom%.rom}_noblobs.rom"
newrom="${newrom%.rom}_noblobs.rom"
cp "${rompath}" "${newrom}" || err "moverom: can't copy rom" cp "${rompath}" "${newrom}" || err "moverom: can't copy rom"
} }