Replace if clauses with functions

This commit is contained in:
Fivel Sotheby 2019-11-03 03:41:46 +00:00
parent ccc61082d2
commit ac7387711c

107
cb-helper
View File

@ -1,22 +1,26 @@
#!/bin/bash
set -e
base_directory="$(dirname $0)"
# Download stuffs
if [ "$1" == "download_code" ]; then
function download_code() {
# Coreboot
if [ ! -d ./coreboot ]; then
printf "Downloading Coreboot\n"
git clone --recursive http://review.coreboot.org/coreboot.git ./coreboot
cd ./coreboot
# Checkout this specific version
git checkout 66a0f55c2e2c6e042c58ae423901d7e41c5a2c84
cd $base_directory
else
printf "Coreboot repository is already present\n"
fi
# GRUB
if [ ! -d ./grub ]; then
printf "Downloading GRUB\n"
git clone git://git.savannah.gnu.org/grub.git ./grub
# Checkout this specific version
cd ./grub
git checkout "tags/2.02"
@ -24,7 +28,6 @@ if [ "$1" == "download_code" ]; then
else
printf "GRUB repository is already present\n"
fi
# me_cleaner
if [ ! -d ./me_cleaner ]; then
printf "Downloading me_cleaner\n"
@ -32,40 +35,34 @@ if [ "$1" == "download_code" ]; then
else
printf "me_cleaner repository is already present\n"
fi
}
# Build Coreboot Utilities
elif [ "$1" == "build_utils" ]; then
function build_utils() {
if [ ! -d "coreboot/.git" ]; then
printf "No Coreboot repository found in coreboot/\nDownload the code first\n"
exit 1
fi
# Build ifdtool for stock BIOS splitting
cd coreboot/util/ifdtool
make -j${nproc}
cd ../../../
# Build cbfstool for managing Coreboot's filesystem
cd coreboot/util/cbfstool
make -j${nproc}
cd ../../../
}
# Split the stock rom and organize the parts
elif [ "$1" == "split_bios" ]; then
function split_bios() {
if [ ! -f "binaries/bios.bin" ]; then
printf "No stock bios (bios.bin) file found in binaries/\n"
exit 1
fi
if [ ! -f "coreboot/util/ifdtool/ifdtool" ]; then
printf "No ifdtool present, build the Coreboot utils first\n"
exit 1
fi
cd binaries/
../coreboot/util/ifdtool/ifdtool -x bios.bin
mv flashregion_0_flashdescriptor.bin descriptor.bin
@ -73,102 +70,82 @@ elif [ "$1" == "split_bios" ]; then
mv flashregion_2_intel_me.bin me.bin
mv flashregion_3_gbe.bin gbe.bin
cd ..
}
# Neuter Intel ME
elif [ "$1" == "neuter_me" ]; then
function neuter_me() {
if [ ! -f "binaries/me.bin" ]; then
printf "No Intel ME (me.bin) binary found in binaries/\n"
exit 1
fi
cp binaries/me.bin binaries/me_neutered.bin
python3 me_cleaner/me_cleaner.py binaries/me_neutered.bin
}
# Prepare Coreboot for compilation
elif [ "$1" == "pre_build_coreboot" ]; then
function pre_build_coreboot() {
# Copy the config
cp "config/coreboot.config" coreboot/.config
# Goto the Coreboot directory
cd coreboot
# Build toolchain
make crossgcc-i386 CPUS=${nproc} -b
# Build IASL
make -j${nproc} iasl
# Get back
cd ..
}
# Build Coreboot
elif [ "$1" == "build_coreboot" ]; then
function build_coreboot() {
# Goto the coreboot directory
cd coreboot
# Clean last build
make clean
rm ../out/coreboot.rom
# Just make
make -j${nproc} || make -j${nproc}
# Exit if failed
if [ $? -ne 0 ]; then
printf "Failed to build Coreboot.\nExiting...\n"
exit 1
fi
# Get back
cd ..
# Copy the resulting binary to a more accessible folder
if [ ! -d out/ ]; then mkdir out/; fi
mv coreboot/build/coreboot.rom out/coreboot.rom
}
# Build GRUB
elif [ "$1" == "build_grub" ]; then
function build_grub() {
# Check if the GRUB code is present
if [ ! -d "grub/.git" ]; then
printf "No GRUB repository found in grub/\nDownload the code first\n"
exit 1
fi
# Change title just for goofs
sed -i "s/_(\"GNU GRUB version %s\"), PACKAGE_VERSION/\"COREBOOT\"/g" grub/grub-core/normal/main.c
# Copy the config
cp "config/grub.config" "grub/.config"
# Clean last build
cd grub
make clean
cd ..
# Build GRUB
cd grub
./autogen.sh
./configure --with-platform=coreboot --disable-werror
make -j${nproc}
cd ..
}
# Assemble the GRUB payload
elif [ "$1" == "assemble_grub" ]; then
function assemble_grup() {
printf "Assembling the GRUB payload\n"
# Load modules config
source "config/grub_modules.conf"
# Assemble GRUB
grub/grub-mkstandalone \
--grub-mkimage="grub/grub-mkimage" \
@ -181,115 +158,95 @@ elif [ "$1" == "assemble_grub" ]; then
/boot/grub/grub.cfg="config/grub_memdisk.cfg" \
/dejavusansmono.pf2="misc/dejavusansmono_24bold.pf2" \
/boot/grub/layouts/usqwerty.gkb="misc/usqwerty.gkb"
}
# Configure SeaBIOS to chainload with GRUB
elif [ "$1" == "config_seabios" ]; then
function config_seabios() {
printf "Configure SeaBIOS\n"
if [ ! -f "out/coreboot.rom" ]; then
printf "No Coreboot image found.\nBuild Coreboot first.\n"
exit 1
fi
# Set GRUB as the default boot device
printf "/rom@img/grub2\n" > "out/bootorder"
coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add -f "out/bootorder" -n bootorder -t raw
rm -f "out/bootorder"
# Hide SeaBIOS
coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add-int -i 0 -n etc/show-boot-menu
# Don't load anything else
coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add-int -i 0 -n etc/pci-optionrom-exec
# Print the contents of the CBFS volume
coreboot/util/cbfstool/cbfstool "out/coreboot.rom" print
}
# Install and config GRUB
elif [ "$1" == "install_grub" ]; then
function install_grub() {
printf "Install GRUB in the CBFS volume\n"
if [ ! -f "out/coreboot.rom" ]; then
printf "No Coreboot image found.\nBuild Coreboot first.\n"
exit 1
fi
# Compress and add GRUB payload
coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add-payload -c lzma -f "out/grub.elf" -n img/grub2 && rm "out/grub.elf"
# Add grub.cfg
coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add -f "config/grub.cfg" -n grub.cfg -t raw
# Print the contents of the CBFS volume
coreboot/util/cbfstool/cbfstool "out/coreboot.rom" print
}
# Flashing activities
elif [ "$1" == "flash" ]; then
function flash() {
# Define which programmer to use
case "$3" in
# Internal
"internal" )
programmer="internal:laptop=force_I_want_a_brick"
;;
# Raspberry Pi
"rpi" )
programmer="linux_spi:dev=/dev/spidev0.0"
;;
# Arduino Boards with Xu2 USB chips
"u2" )
programmer="serprog:dev=/dev/ttyACM0:115200"
;;
# Arduino Boards with FTDI USB chips
"ftdi" )
programmer="serprog:dev=/dev/ttyUSB0:2000000"
;;
# Exit if no programmer is specified
* )
printf "You must specify the programmer\n"
exit 1
;;
esac
# Write to the flash chip
if [ "$2" == "write" ]; then
# Exit if Coreboot hasn't been successfully compiled yet
if [ ! -f "out/coreboot.rom" ]; then
printf "Build Coreboot first\n"
exit 1
fi
flashrom -p $programmer -w "out/coreboot.rom"
# Do consecutive reads of the flash chip and compare them
elif [ "$2" == "read" ]; then
mkdir binaries/reads
for i in {1..5}; do
flashrom -p $programmer -r "binaries/reads/bios$i.bin"
md5sum "binaries/reads/*.bin"
done
# Check if the flash chip is detected
elif [ "$2" == "check" ]; then
flashrom -p $programmer
fi
}
# Run operation
if [[ $1 != "" ]]; then
eval "$1"
# Exit if no operation is specified
else
printf "No operation specified\n"
exit 1
fi