cbmk/script/roms
Leah Rowe 3ddc7eb50f correction
Signed-off-by: Leah Rowe <info@minifree.org>
2024-06-11 22:28:36 +01:00

250 lines
7.8 KiB
Bash
Executable file

#!/usr/bin/env sh
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (c) 2014-2016,2020-2021,2023-2024 Leah Rowe <leah@libreboot.org>
# Copyright (c) 2021-2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
# Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
# Copyright (c) 2022-2023 Alper Nebi Yasak <alpernebiyasak@gmail.com>
# Copyright (c) 2023 Riku Viitanen <riku.viitanen@protonmail.com>
set -u -e
. "include/lib.sh"
seavgabiosrom="elf/seabios/default/libgfxinit/vgabios.bin"
grub_background="background1280x800.png"
cfgsdir="config/coreboot"
pv="payload_grub_withseabios payload_seabios t"
pv="$pv payload_seabios_withgrub payload_seabios_grubonly payload_grub"
v="romdir cbrom initmode displaymode cbcfg targetdir tree keymaps release"
v="$v grub_timeout board grub_scan_disk grubtree grubelf"
eval "$(setvars "n" $pv) $(setvars "" $v)"
main()
{
[ $# -lt 1 ] && for x in config/coreboot/*; do
[ -e "$x" ] && handle_coreboot_target "${x##*/}"; continue
done && return 0
handle_coreboot_target $@
}
handle_coreboot_target()
{
eval "$(setvars "n" $pv) $(setvars "" $v)"
grub_background="background1280x800.png"
board="${1%% *}"
configure_target && [ "$board" != "$tree" ] && build_payloads && \
build_target_mainboard && [ $# -gt 1 ] && shift 1 && \
handle_coreboot_target $@; return 0
}
configure_target()
{
targetdir="$cfgsdir/$board"
[ -f "$targetdir/target.cfg" ] || $err "$board: target.cfg missing"
# Override the above defaults using target.cfg
. "$targetdir/target.cfg"
[ -z "$grub_scan_disk" ] && grub_scan_disk="ahci ata"
[ -n "$grubtree" ] || grubtree="default"
grubelf="elf/grub/$grubtree/payload/grub.elf"
[ -z "$tree" ] && $err "$board: tree not defined"
[ "$payload_grub_withseabios" = "y" ] && payload_grub="y"
[ "$payload_grub_withseabios" = "y" ] && \
eval "$(setvars "y" payload_seabios payload_seabios_withgrub)"
[ "$payload_seabios_withgrub" = "y" ] && payload_seabios="y"
[ "$payload_seabios_grubonly" = "y" ] && payload_seabios="y"
[ "$payload_seabios_grubonly" = "y" ] && payload_seabios_withgrub="y"
[ "$payload_grub" != "y" ] && [ "$payload_seabios" != "y" ] && \
return 1; return 0
}
build_payloads()
{
romdir="bin/$board"
cbdir="src/coreboot/$board"
[ "$board" = "$tree" ] || cbdir="src/coreboot/$tree"
cbfstool="elf/cbfstool/$tree/cbfstool"
cbrom="$cbdir/build/coreboot.rom"
[ -f "$cbfstool" ] || x_ ./update trees -b coreboot utils $tree
[ "$payload_seabios" = "y" ] && x_ ./update trees -b seabios
if [ "$payload_grub" = "y" ] || [ "$payload_seabios_withgrub" = "y" ] \
|| [ "$payload_seabios_grubonly" = "y" ]; then build_grub_payload
fi
}
build_grub_payload()
{
for keymapfile in "$grubdata/keymap/"*.gkb; do
[ -f "$keymapfile" ] && keymaps="$keymaps $keymapfile"
done
[ -f "$grubelf" ] || x_ ./update trees -b grub $grubtree; return 0
}
build_target_mainboard()
{
x_ rm -Rf "$romdir"
for x in "normal" "vgarom" "libgfxinit"; do
initmode="$x"
hmode="vesafb"
[ "$initmode" = "vgarom" ] || hmode="corebootfb"
modes="$hmode txtmode"
for y in $modes; do
displaymode="$y"
[ "$initmode" = "normal" ] && \
[ "$displaymode" != "txtmode" ] && continue
cbcfg="$targetdir/config/${initmode}_$displaymode"
[ "$initmode" = "normal" ] && cbcfg="${cbcfg%_*}"
build_roms "$cbcfg"; x_ rm -f "$cbrom"
done
done
}
build_roms()
{
cbcfg="$1"
e "$cbcfg" f not && return 0
x_ ./update trees -b coreboot $board
_cbrom="elf/coreboot_nopayload_DO_NOT_FLASH"
_cbrom="$_cbrom/$board/${initmode}_$displaymode"
[ "$initmode" = "normal" ] && _cbrom="${_cbrom%"_$displaymode"}"
_cbrom="$_cbrom/coreboot.rom"
cbrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
x_ cp "$_cbrom" "$cbrom"
[ "$payload_seabios" = "y" ] && build_seabios_roms
[ "$payload_grub" != "y" ] || x_ build_grub_roms "$cbrom" "grub"
x_ cp "$_cbrom" "$cbrom"
}
build_seabios_roms()
{
if [ "$payload_seabios_withgrub" = "y" ]; then
t="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
x_ cp "$cbrom" "$t"
x_ build_grub_roms "$t" "seabios_withgrub"
else
t="$(mkSeabiosRom "$cbrom" "fallback/payload")" || \
$err "build_seabios_roms: cannot build tmprom"
newrom="$romdir/seabios_${board}_${initmode}_$displaymode"
[ "$initmode" = "normal" ] && newrom="$romdir/seabios" \
&& newrom="${newrom}_${board}_$initmode"
x_ cprom "$t" "$newrom.rom"
fi
x_ rm -f "$t"
}
# Make separate ROM images with GRUB payload, for each supported keymap
build_grub_roms()
{
tmprom="$1"
payload1="$2" # allow values: grub, seabios, seabios_withgrub
grub_cbfs="fallback/payload"
if [ "$payload1" = "grub" ] && [ "$payload_grub_withseabios" = "y" ]
then
_tmpmvrom="$(mkSeabiosRom "$tmprom" "seabios.elf")" || \
$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"
x_ mv "$_tmpmvrom" "$tmprom"
fi
# we only need insert grub.elf once, for each coreboot config:
x_ "$cbfstool" "$tmprom" add-payload -f "$grubelf" \
-n "$grub_cbfs" -c lzma
# we only need insert background.png once, for each coreboot config:
if [ "$displaymode" = "vesafb" ] || \
[ "$displaymode" = "corebootfb" ]; then
backgroundfile="$grubdata/background/$grub_background"
"$cbfstool" "$tmprom" add -f "$backgroundfile" -n \
background.png -t raw || $err "!bg, $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"
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"
[ -z "$grub_timeout" ] || x_ "$cbfstool" "$tmprom" add \
-f "$tmpcfg" -n timeout.cfg -t raw
x_ rm -f "$tmpcfg"
for keymapfile in $keymaps; do
[ -f "$keymapfile" ] || continue
keymap="${keymapfile##*/}"
keymap="${keymap%.gkb}"
tmpgrubrom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
x_ cp "$tmprom" "$tmpgrubrom"
x_ "$cbfstool" "$tmpgrubrom" add -f "$keymapfile" \
-n keymap.gkb -t raw
newrom="$romdir/${payload1}_${board}_${initmode}_"
newrom="$newrom${displaymode}_$keymap.rom"
[ "$initmode" = "normal" ] && newrom="$romdir/${payload1}_" \
&& newrom="$newrom${board}_${initmode}_$keymap.rom"
x_ cprom "$tmpgrubrom" "$newrom"
if [ "$payload_seabios_withgrub" = "y" ] && \
[ "$payload1" != "grub" ]; then
x_ "$cbfstool" "$tmpgrubrom" add \
-f "$grubdata/bootorder" -n bootorder -t raw
x_ cprom "$tmpgrubrom" "${newrom%.rom}_grubfirst.rom"
if [ "$payload_seabios_grubonly" = "y" ]; then
x_ "$cbfstool" "$tmpgrubrom" add-int -i 0 \
-n etc/show-boot-menu
x_ cprom "$tmpgrubrom" \
"${newrom%.rom}_grubonly.rom"
fi
fi
x_ rm -f "$tmpgrubrom"
done
}
# make a rom in /tmp/ and then print the path of that ROM
mkSeabiosRom() {
_cbrom="$1" # rom to insert seabios in. will not be touched
# (a tmpfile will be made instead)
_seabios_cbfs_path="$2" # e.g. fallback/payload
_seabioself="elf/seabios/default/$initmode/bios.bin.elf"
tmprom="$(mktemp -t coreboot_rom.XXXXXXXXXX)"
x_ cp "$_cbrom" "$tmprom"
x_ "$cbfstool" "$tmprom" add-payload -f "$_seabioself" \
-n "$_seabios_cbfs_path" -c lzma
x_ "$cbfstool" "$tmprom" add-int -i 3000 -n etc/ps2-keyboard-spinup
z="2"; [ "$initmode" = "vgarom" ] && z="0"
x_ "$cbfstool" "$tmprom" add-int -i $z -n etc/pci-optionrom-exec
x_ "$cbfstool" "$tmprom" add-int -i 0 -n etc/optionroms-checksum
[ "$initmode" != "libgfxinit" ] || x_ "$cbfstool" "$tmprom" add -f \
"$seavgabiosrom" -n vgaroms/seavgabios.bin -t raw
printf "%s\n" "$tmprom"
}
cprom()
{
x_ mkdir -p "${2%/*}"
x_ cp "$1" "$2"
}
main $@