From bceb5f2eb47c9b00d8c0be52adca621588f180d0 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Fri, 12 May 2023 16:55:45 +0100 Subject: build/roms_helper: move logic into main() logic will be split from main into smaller functions, in follow-up commits --- resources/scripts/build/boot/roms_helper | 558 ++++++++++++++++--------------- 1 file changed, 287 insertions(+), 271 deletions(-) (limited to 'resources/scripts') diff --git a/resources/scripts/build/boot/roms_helper b/resources/scripts/build/boot/roms_helper index 115763ff..c0cf6123 100755 --- a/resources/scripts/build/boot/roms_helper +++ b/resources/scripts/build/boot/roms_helper @@ -36,36 +36,7 @@ kmapdir="resources/grub/keymap" displaymodes="" payloads="" keyboard_layouts="" -while [ $# -gt 0 ]; do - case ${1} in - -d) - displaymodes="${displaymodes}${2}" - shift ;; - -p) - payloads="${payloads}${2}" - shift ;; - -k) - keyboard_layouts="${keyboard_layouts}${2}" - shift ;; - *) - board=${1} ;; - esac - shift -done - -printf "board is %s , kb is %s , displaymode is %s , payloads is %s\n" \ - ${board} ${keyboard_layouts} ${displaymodes} ${payloads} - -if [ ! -d "${cbcfgdir}/${board}" ]; then - printf "build/roms: Target not defined: %s\n" ${board} - exit 1 -fi - -if [ ! -f "${cbcfgdir}/${board}/board.cfg" ]; then - printf "build/roms: Missing board.cfg for target: %s\n" ${board} - exit 1 -fi - +board="" grub_scan_disk="undefined" cbtree="undefined" @@ -75,227 +46,304 @@ arch="undefined" # Disable all payloads by default. # board.cfg files have to specifically enable [a] payload(s) payload_grub="n" -payload_grub_withseabios="n" # seabios chainloaded from grub +payload_grub_wseabios="n" # seabios chainloaded from grub payload_seabios="n" -payload_seabios_withgrub="n" # i386-coreboot grub from SeaBIOS boot menu +payload_seabios_wgrub="n" # i386-coreboot grub from SeaBIOS boot menu payload_memtest="n" payload_uboot="n" uboot_config="undefined" -# Override the above defaults using board.cfg -. "${cbcfgdir}/${board}/board.cfg" - -if [ "${grub_scan_disk}" = "undefined" ]; then - printf "build/roms: Target '%s' does not define grub_scan_disk. " \ - ${board} - printf "Defaulting to 'both'.\n" - grub_scan_disk="both" -fi - -if [ "${grub_scan_disk}" != "both" ] && \ - [ "${grub_scan_disk}" != "ata" ] && \ - [ "${grub_scan_disk}" != "ahci" ]; then - printf "build/roms: Target '%s' defines bad grub_scan_disk option. " \ - ${board} - printf "Defaulting to 'both'.\n" - grub_scan_disk="both" - # erroring out would be silly. just use the default -fi - -if [ "${cbtree}" = "undefined" ]; then - printf "build/roms: Target '%s' does not define a coreboot tree. " \ - ${board} - printf "Skipping build.\n" - exit 1 -fi -if [ "${arch}" = "undefined" ]; then - printf "build/roms: Target '%s' does not define a CPU type. " \ - ${board} - printf "Skipping build.\n" - exit 1 -fi - -if [ "${payload_memtest}" != "n" ] && \ - [ "${payload_memtest}" != "y" ]; then - payload_memtest="n" -fi -if [ "${payload_grub_withseabios}" = "y" ]; then - payload_grub="y" -fi -if [ "${payload_grub_withseabios}" = "y" ]; then - payload_seabios="y" - payload_seabios_withgrub="y" -fi -if [ "${payload_seabios_withgrub}" = "y" ]; then - payload_seabios="y" -fi -# NOTE: reverse logic must NOT be applied. If SeaBIOS-with-GRUB works, that -# doesn't necessarily mean GRUb-with-SeaBIOS will. For example, the board -# might have an external GPU, where it's recommended to boot SeaBIOS first -if [ "${payload_grub}" != "y" ] && [ "${payload_seabios}" != "y" ] \ - && [ "${payload_uboot}" != "y" ]; then - for configfile in "${cbcfgdir}/${board}/config/"*; do - if [ ! -e "${configfile}" ]; then - continue - fi - printf "ERROR build/roms: Target '%s' defines no payload. " \ + +romdir="" +cbdir="" +cbfstool="" +corebootrom="" +seavgabiosrom="" + +CROSS_COMPILE="" + +main() +{ + while [ $# -gt 0 ]; do + case ${1} in + -d) + displaymodes="${displaymodes}${2}" + shift ;; + -p) + payloads="${payloads}${2}" + shift ;; + -k) + keyboard_layouts="${keyboard_layouts}${2}" + shift ;; + *) + board=${1} ;; + esac + shift + done + + printf "board %s , kb %s , displaymode %s , payloads %s\n" \ + ${board} ${keyboard_layouts} ${displaymodes} \ + ${payloads} + + if [ "${board}" = "" ]; then + printf "build/roms: undefined board. Exiting\n" + exit 1 + fi + if [ ! -d "${cbcfgdir}/${board}" ]; then + printf "build/roms: Target not defined: %s\n" ${board} + exit 1 + fi + if [ ! -f "${cbcfgdir}/${board}/board.cfg" ]; then + printf "build/roms %s: Missing board.cfg\n" ${board} + exit 1 + fi + + # Override the above defaults using board.cfg + . "${cbcfgdir}/${board}/board.cfg" + + if [ "${grub_scan_disk}" = "undefined" ]; then + printf "build/roms '%s': grub_scan_disk is undefined. " \ + ${board} + printf "Defaulting to 'both'.\n" + grub_scan_disk="both" + fi + if [ "${grub_scan_disk}" != "both" ] && \ + [ "${grub_scan_disk}" != "ata" ] && \ + [ "${grub_scan_disk}" != "ahci" ]; then + printf "build/roms '%s': invalid grub_scan_disk config. " \ ${board} - printf "Exiting.\n" + printf "Defaulting to 'both'.\n" + grub_scan_disk="both" + # erroring out would be silly. just use the default + fi + + if [ "${cbtree}" = "undefined" ]; then + printf "build/roms '%s': undefined coreboot tree. " \ + ${board} + printf "Skipping build.\n" + exit 1 + fi + if [ "${arch}" = "undefined" ]; then + printf "build/roms '%s': undefined CPU type. " \ + ${board} + printf "Skipping build.\n" exit 1 - done -fi - -if [ "${payload_uboot}" != "n" ] && \ - [ "${payload_uboot}" != "y" ]; then - payload_uboot="n" -fi - -if [ "${payload_uboot}" = "y" ] && \ - [ "${uboot_config}" = "undefined" ]; then - uboot_config="default" -fi - -# Override all payload directives with cmdline args -if [ ! -z ${payloads} ]; then - echo "setting payloads $payloads" - payload_grub="n" - payload_grub_withseabios="n" # seabios chainloaded from grub - payload_seabios="n" - payload_seabios_withgrub="n" # i386-coreboot grub from SeaBIOS menu - payload_uboot="n" - payload_memtest="n" - - for payload in ${payloads} ; do - eval "payload_${payload}=y" - done -fi - -romdir="bin/${board}" -cbdir="coreboot/${board}" -if [ "${board}" != "${cbtree}" ]; then - cbdir="coreboot/${cbtree}" -fi -cbfstool="${cbdir}/util/cbfstool/cbfstool" -corebootrom="${cbdir}/build/coreboot.rom" -seavgabiosrom="payload/seabios/seavgabios.bin" - -if [ ! -d "${cbdir}" ]; then - ./download coreboot ${cbtree} -fi - -cat version > "${cbdir}/.coreboot-version" - -if [ "${arch}" = "x86_32" ] || [ "${arch}" = "x86_64" ]; then - if [ ! -d "${cbdir}/util/crossgcc/xgcc/i386-elf/" ]; then - # Even for 64-bit machines, coreboot builds 32-bit ROM - # images, so we only need to worry about i386-elf - make -C "${cbdir}" crossgcc-i386 CPUS=$(nproc) - fi - case "$(uname -m)" in - x86*|i*86|amd64) : ;; - *) export CROSS_COMPILE=i386-elf- ;; - esac -elif [ "${arch}" = "ARMv7" ]; then - if [ ! -d "${cbdir}/util/crossgcc/xgcc/arm-eabi/" ]; then - make -C "${cbdir}" crossgcc-arm CPUS=$(nproc) - fi - case "$(uname -m)" in - arm|arm32|armv6*|armv7*) : ;; - *) export CROSS_COMPILE=arm-eabi- ;; - esac -elif [ "${arch}" = "AArch64" ]; then - if [ ! -d "${cbdir}/util/crossgcc/xgcc/aarch64-elf/" ]; then - make -C "${cbdir}" crossgcc-aarch64 CPUS=$(nproc) - fi - # aarch64 also needs armv7 toolchain for arm-trusted-firmware - if [ ! -d "${cbdir}/util/crossgcc/xgcc/arm-eabi/" ]; then - make -C "${cbdir}" crossgcc-arm CPUS=$(nproc) - fi - case "$(uname -m)" in - arm64|aarch64) : ;; - *) export CROSS_COMPILE=aarch64-elf- ;; - esac -fi - -export PATH="$(pwd)/${cbdir}/util/crossgcc/xgcc/bin:$PATH" - -if [ ! -f "${cbfstool}" ]; then - ./build module cbutils ${cbtree} || exit 1 -fi - -if [ ! -f "${seavgabiosrom}" ] \ - || [ ! -f payload/seabios/seabios_libgfxinit.elf ] \ - || [ ! -f payload/seabios/seabios_vgarom.elf ] \ - || [ ! -f payload/seabios/seabios_normal.elf ]; then - if [ "${payload_seabios}" = "y" ]; then - ./build payload seabios - elif [ "${payload_grub}" = "y" ] \ - && [ "${payload_grub_withseabios}" = "y" ]; then - ./build payload seabios fi -fi -if [ "${payload_memtest}" = "y" ]; then - if [ ! -f "memtest86plus/memtest" ]; then - ./build module memtest86plus + if [ "${payload_memtest}" != "n" ] && \ + [ "${payload_memtest}" != "y" ]; then + payload_memtest="n" + fi + if [ "${payload_grub_wseabios}" = "y" ]; then + payload_grub="y" + fi + if [ "${payload_grub_wseabios}" = "y" ]; then + payload_seabios="y" + payload_seabios_wgrub="y" fi -fi + if [ "${payload_seabios_wgrub}" = "y" ]; then + payload_seabios="y" + fi + # NOTE: reverse logic must NOT be applied. If SeaBIOS-with-GRUB works, + # that doesn't mean GRUB-with-SeaBIOS will. For example, the board + # might have an external GPU, where SeaBIOS should be booted first + if [ "${payload_grub}" != "y" ] && [ "${payload_seabios}" != "y" ] \ + && [ "${payload_uboot}" != "y" ]; then + for configfile in "${cbcfgdir}/${board}/config/"*; do + if [ ! -e "${configfile}" ]; then + continue + fi + printf "build/roms %s: Payload undefined. Exiting.\n" \ + ${board} + exit 1 + done + fi + if [ "${payload_uboot}" != "n" ] && \ + [ "${payload_uboot}" != "y" ]; then + payload_uboot="n" + fi + if [ "${payload_uboot}" = "y" ] && \ + [ "${uboot_config}" = "undefined" ]; then + uboot_config="default" + fi + # Override all payload directives with cmdline args + if [ ! -z ${payloads} ]; then + echo "setting payloads $payloads" + payload_grub="n" + payload_grub_wseabios="n" # seabios chainloaded from grub + payload_seabios="n" + payload_seabios_wgrub="n" # grub from SeaBIOS menu + payload_uboot="n" + payload_memtest="n" + + for payload in ${payloads} ; do + eval "payload_${payload}=y" + done + fi -[ -d "${romdir}/" ] || mkdir -p "${romdir}/" -rm -f "${romdir}"/* + romdir="bin/${board}" + cbdir="coreboot/${board}" + if [ "${board}" != "${cbtree}" ]; then + cbdir="coreboot/${cbtree}" + fi + cbfstool="${cbdir}/util/cbfstool/cbfstool" + corebootrom="${cbdir}/build/coreboot.rom" + seavgabiosrom="payload/seabios/seavgabios.bin" + + if [ ! -d "${cbdir}" ]; then + ./download coreboot ${cbtree} + fi + cat version > "${cbdir}/.coreboot-version" -if [ "${payload_grub}" = "y" ] \ - || [ "${payload_seabios_withgrub}" = "y" ]; then - if [ -f "payload/grub/grub_usqwerty.cfg" ]; then - sha1sumcmd="sha1sum resources/grub/config/grub.cfg" - grubrefchecksum="$(${sha1sumcmd} | awk '{print $1}')" + if [ "${arch}" = "x86_32" ] || [ "${arch}" = "x86_64" ]; then + if [ ! -d "${cbdir}/util/crossgcc/xgcc/i386-elf/" ]; then + # Even for 64-bit machines, coreboot builds 32-bit ROM + # images, so we only need to worry about i386-elf + make -C "${cbdir}" crossgcc-i386 CPUS=$(nproc) + fi + case "$(uname -m)" in + x86*|i*86|amd64) : ;; + *) export CROSS_COMPILE=i386-elf- ;; + esac + elif [ "${arch}" = "ARMv7" ]; then + if [ ! -d "${cbdir}/util/crossgcc/xgcc/arm-eabi/" ]; then + make -C "${cbdir}" crossgcc-arm CPUS=$(nproc) + fi + case "$(uname -m)" in + arm|arm32|armv6*|armv7*) : ;; + *) export CROSS_COMPILE=arm-eabi- ;; + esac + elif [ "${arch}" = "AArch64" ]; then + if [ ! -d "${cbdir}/util/crossgcc/xgcc/aarch64-elf/" ]; then + make -C "${cbdir}" crossgcc-aarch64 CPUS=$(nproc) + fi + # aarch64 also needs armv7 toolchain for arm-trusted-firmware + if [ ! -d "${cbdir}/util/crossgcc/xgcc/arm-eabi/" ]; then + make -C "${cbdir}" crossgcc-arm CPUS=$(nproc) + fi + case "$(uname -m)" in + arm64|aarch64) : ;; + *) export CROSS_COMPILE=aarch64-elf- ;; + esac + fi - sha1sumcmd="sha1sum payload/grub/grub_usqwerty.cfg" - grubbuildchecksum="$(${sha1sumcmd} | awk '{print $1}')" + export PATH="$(pwd)/${cbdir}/util/crossgcc/xgcc/bin:$PATH" - if [ "${grubrefchecksum}" != "${grubbuildchecksum}" ]; then - rm -Rf payload/grub/ - printf "Changes detected to GRUB. Re-building now:\n" + if [ ! -f "${cbfstool}" ]; then + ./build module cbutils ${cbtree} || exit 1 + fi + if [ ! -f "${seavgabiosrom}" ] \ + || [ ! -f payload/seabios/seabios_libgfxinit.elf ] \ + || [ ! -f payload/seabios/seabios_vgarom.elf ] \ + || [ ! -f payload/seabios/seabios_normal.elf ]; then + if [ "${payload_seabios}" = "y" ]; then + ./build payload seabios + elif [ "${payload_grub}" = "y" ] \ + && [ "${payload_grub_wseabios}" = "y" ] + then + ./build payload seabios fi - else - printf "Required GRUB payloads not yet built. Building now:\n" - rm -Rf payload/grub/ # just in case fi - for keymapfile in ${kmapdir}/*; do - if [ ! -f "${keymapfile}" ]; then - continue + if [ "${payload_memtest}" = "y" ]; then + if [ ! -f "memtest86plus/memtest" ]; then + ./build module memtest86plus fi + fi - keymap="${keymapfile##*/}" - keymap="${keymap%.gkb}" + [ -d "${romdir}/" ] || mkdir -p "${romdir}/" + rm -f "${romdir}"/* + + if [ "${payload_grub}" = "y" ] \ + || [ "${payload_seabios_wgrub}" = "y" ]; then + if [ -f "payload/grub/grub_usqwerty.cfg" ]; then + sha1cmd="sha1sum resources/grub/config/grub.cfg" + grubrefchecksum="$(${sha1cmd} | awk '{print $1}')" - grubelf="payload/grub/grub_${keymap}.elf" - grubcfg="payload/grub/grub_${keymap}.cfg" - grubtestcfg="payload/grub/grub_${keymap}_test.cfg" + sha1cmd="sha1sum payload/grub/grub_usqwerty.cfg" + grubsha1="$(${sha1cmd} | awk '{print $1}')" - if [ ! -f "${grubelf}" ] || [ ! -f "${grubcfg}" ] || \ - [ ! -f "${grubtestcfg}" ]; then - ./build payload grub + if [ "${grubrefchecksum}" != "${grubsha1}" ]; then + rm -Rf payload/grub/ + printf "GRUB change detected. Rebuilding:\n" + fi + else + printf "GRUB payloads needed. Building:\n" + rm -Rf payload/grub/ # just in case fi - done -fi + for keymapfile in ${kmapdir}/*; do + if [ ! -f "${keymapfile}" ]; then + continue + fi -if [ "${payload_uboot}" = "y" ]; then - if [ "${uboot_config}" = "default" ]; then - ubdir="payload/u-boot/${board}" - else - ubdir="payload/u-boot/${board}/${uboot_config}" + keymap="${keymapfile##*/}" + keymap="${keymap%.gkb}" + + grubelf="payload/grub/grub_${keymap}.elf" + grubcfg="payload/grub/grub_${keymap}.cfg" + grubtestcfg="payload/grub/grub_${keymap}_test.cfg" + + if [ ! -f "${grubelf}" ] || [ ! -f "${grubcfg}" ] || \ + [ ! -f "${grubtestcfg}" ]; then + ./build payload grub + fi + done fi - if [ -f "${ubdir}/u-boot.elf" ]; then - ubootelf="${ubdir}/u-boot.elf" - elif [ -f "${ubdir}/u-boot" ]; then - ubootelf="${ubdir}/u-boot" + if [ "${payload_uboot}" = "y" ]; then + ubdir="" + if [ "${uboot_config}" = "default" ]; then + ubdir="payload/u-boot/${board}" + else + ubdir="payload/u-boot/${board}/${uboot_config}" + fi + + if [ -f "${ubdir}/u-boot.elf" ]; then + ubootelf="${ubdir}/u-boot.elf" + elif [ -f "${ubdir}/u-boot" ]; then + ubootelf="${ubdir}/u-boot" + else + printf "U-Boot needed. Building:\n" + rm -Rf "payload/u-boot/${board}" # just in case + ./build payload u-boot "${board}" + fi + fi + + if [ -z ${displaymodes} ]; then + initmode="libgfxinit" + for displaymode in corebootfb txtmode; do + _cbcfg="${cbcfgdir}/${board}/config/${initmode}" + _cbcfg="${_cbcfg}_${displaymode}" + mkRoms "${_cbcfg}" "${displaymode}" "${initmode}" + done + + initmode="vgarom" + for displaymode in vesafb txtmode; do + _cbcfg="${cbcfgdir}/${board}/config/${initmode}" + _cbcfg="${_cbcfg}_${displaymode}" + mkRoms "${_cbcfg}" "${displaymode}" "${initmode}" + done + + initmode="normal" + displaymode="txtmode" + _cbcfg="${cbcfgdir}/${board}/config/${initmode}" + mkRoms "${_cbcfg}" "${displaymode}" "${initmode}" + else - printf "Required U-Boot payload not yet built. Building now\n" - rm -Rf "payload/u-boot/${board}" # just in case - ./build payload u-boot "${board}" + echo "special displaymode defined as $displaymodes" + for initmode in vgarom libgfxinit; do + for displaymode in ${displaymodes}; do + _cbcfg="${cbcfgdir}/${board}/config/" + _cbcfg="${_cbcfg}${initmode}_${displaymode}" + mkRoms "${_cbcfg}" "${displaymode}" \ + "${initmode}" + done + done fi -fi + + ( + cd "${cbdir}" + make distclean # TODO: do make clean instead (avoid re-building utils) + ) +} # it is assumed that no other work will be done on the ROM # after calling this function. therefore this function is "final" @@ -350,10 +398,10 @@ moverom() mkCoreboot() { cbdir="${1}" # eg. coreboot/default - cbcfgpath="${2}" # eg. ${cbcfgdir}/e6400nvidia_4mb/config/normal - if [ ! -f "${cbcfgpath}" ]; then + _cbcfg="${2}" # eg. ${cbcfgdir}/e6400nvidia_4mb/config/normal + if [ ! -f "${_cbcfg}" ]; then printf "\nmkCoreboot: coreboot config '%s' does not exist. " \ - ${cbcfgpath} + ${_cbcfg} printf "Skipping build.\n" return 0 fi @@ -372,7 +420,7 @@ mkCoreboot() mv "${cbdir}/cbfstool" "${cbfstool}" fi ) - cp "${cbcfgpath}" "${cbdir}"/.config + cp "${_cbcfg}" "${cbdir}"/.config ./build module cbutils ${cbdir#coreboot/} || exit 1 ( cd "${cbdir}" @@ -512,15 +560,15 @@ mkRomsWithGrub() tmprompath="${1}" initmode="${2}" displaymode="${3}" - firstpayloadname="${4}" # allow values: grub, seabios, seabios_withgrub + firstpayloadname="${4}" # allow values: grub, seabios, seabios_wgrub x=${tmprompath} y=${initmode} - if [ "${payload_grub_withseabios}" = "y" ] \ + if [ "${payload_grub_wseabios}" = "y" ] \ && [ "${firstpayloadname}" = "grub" ]; then mv "$(mkSeabiosRom "${x}" "seabios.elf" "${y}")" \ "${tmprompath}" - elif [ "${payload_seabios_withgrub}" ] \ + elif [ "${payload_seabios_wgrub}" ] \ && [ "${firstpayloadname}" != "grub" ]; then mv "$(mkSeabiosRom "${x}" "fallback/payload" "${y}")" \ "${tmprompath}" @@ -569,18 +617,18 @@ mkRomsWithGrub() # Main ROM building function. This calls all other functions mkRoms() { - cbcfgpath="${1}" + _cbcfg="${1}" displaymode="${2}" initmode="${3}" - if [ ! -f "${cbcfgpath}" ]; then + if [ ! -f "${_cbcfg}" ]; then printf "'%s' does not exist. Skipping build for %s %s %s\n" \ - ${cbcfgpath} ${board} \ + ${_cbcfg} ${board} \ ${displaymode} ${initmode} return 0 fi - mkCoreboot "${cbdir}" "${cbcfgpath}" + mkCoreboot "${cbdir}" "${_cbcfg}" if [ "${displaymode}" = "txtmode" ] \ && [ "${payload_memtest}" = "y" ]; then @@ -590,7 +638,7 @@ mkRoms() fi if [ "${payload_seabios}" = "y" ]; then - if [ "${payload_seabios_withgrub}" = "n" ]; then + if [ "${payload_seabios_wgrub}" = "n" ]; then x=${corebootrom} y=${initmode} t=$(mkSeabiosRom "$x" "fallback/payload" "$y") @@ -633,36 +681,4 @@ mkRoms() fi } -if [ -z ${displaymodes} ]; then - initmode="libgfxinit" - for displaymode in corebootfb txtmode; do - cbcfgpath="${cbcfgdir}/${board}/config/${initmode}_$displaymode" - mkRoms "${cbcfgpath}" "${displaymode}" "${initmode}" - done - - initmode="vgarom" - for displaymode in vesafb txtmode; do - cbcfgpath="${cbcfgdir}/${board}/config/${initmode}_$displaymode" - mkRoms "${cbcfgpath}" "${displaymode}" "${initmode}" - done - - initmode="normal" - displaymode="txtmode" - cbcfgpath="${cbcfgdir}/${board}/config/${initmode}" - mkRoms "${cbcfgpath}" "${displaymode}" "${initmode}" - -else - echo "special displaymode defined as $displaymodes" - for initmode in vgarom libgfxinit; do - for displaymode in ${displaymodes}; do - cbcfgpath="${cbcfgdir}/${board}/config/${initmode}_" - cbcfgpath="${cbcfgpath}${displaymode}" - mkRoms "${cbcfgpath}" "${displaymode}" "${initmode}" - done - done -fi - -( -cd "${cbdir}" -make distclean -) +main $@ -- cgit v1.2.1