Tim Crawford 968a612824 scripts: Address shellcheck issues
Report issues by shell files with:

    git ls-files '*.sh' | xargs shellcheck --exclude=SC2162

Address the following:

- SC1087: Use braces when expanding arrays
- SC1091: Not following
- SC2004: `$`/`${}` is unnecessary on arithmetic variables
- SC2024: `sudo` doesn't affect redirects
- SC2034: foo appears unused. Verify it or export it
- SC2086: Double quote to prevent globbing and word splitting
- SC2087: Quote `EOF`
- SC2115: Use `"${var:?}"` to ensure this never expands to `/*`
- SC2148: Add a shebang

Addresses (at least partially) some POSIX/dash issues:

- SC2113: `function` keyword is non-standard
- SC3010: In POSIX sh, `[[` `]]` is undefined
- SC3014: In POSIX sh, `==` in place of `=` is undefined
- SC3020: In POSIX sh, `&>` is undefined
- SC3046: In POSIX sh, `source` in place of `.` is undefined

Does not address:

- SC2162: `read` without `-r` will mangle backslashes
- Any other POSIX/dash-specific issues

Signed-off-by: Tim Crawford <tcrawford@system76.com>
2023-10-19 14:45:22 -06:00

114 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Copyright 2020 System76
# shellcheck disable=SC2024
set -e
SCRIPT_DIR=$(dirname "$0")
if [ -z "$1" ]
then
echo "$0 <model> [firmware.rom] [ec.rom]" >&2
exit 1
fi
MODEL="$1"
BIOS_IMAGE=
if [ -n "$2" ]
then
if [ ! -f "$2" ]
then
echo "Could not find BIOS image '$2'" >&2
exit 1
fi
BIOS_IMAGE=$(realpath "$2")
fi
EC_ROM=
if [ -n "$3" ]
then
if [ ! -f "$3" ]
then
echo "Could not find EC ROM '$3'" >&2
exit 1
fi
EC_ROM=$(realpath "$3")
fi
if [ ! -d "models/${MODEL}" ]
then
echo "Generating for new model '${MODEL}'"
mkdir "models/${MODEL}"
read -rp "Manufacturer: " _mfr
read -rp "Product name: " _name
read -rp "Product version: " _version
echo "# ${_mfr} ${_name} (${_version})" > "models/${MODEL}/README.md.in"
fi
MODEL_DIR=$(realpath "models/${MODEL}")
echo "Generating data for coreboot..."
pushd tools/coreboot-collector
cargo build --release
sudo target/release/coreboot-collector > "${MODEL_DIR}/coreboot-collector.txt"
popd
"${SCRIPT_DIR}/coreboot-gpio.sh" "${MODEL_DIR}/coreboot-collector.txt" > "${MODEL_DIR}/gpio.h"
"${SCRIPT_DIR}/coreboot-hda.sh" "${MODEL_DIR}/coreboot-collector.txt" > "${MODEL_DIR}/hda_verb.c"
if [ -n "${BIOS_IMAGE}" ]
then
# Get the flash descriptor and Intel ME blobs
make -C coreboot/util/ifdtool
coreboot/util/ifdtool/ifdtool -x "${BIOS_IMAGE}"
# TODO: Don't hardcode flash region index
mv flashregion_0_flashdescriptor.bin "${MODEL_DIR}/fd.rom"
mv flashregion_2_intel_me.bin "${MODEL_DIR}/me.rom"
rm -f flashregion_*.bin
fi
# Get the Video BIOS Table and GOP driver for Intel systems
if sudo [ -e /sys/kernel/debug/dri/0/i915_vbt ]
then
sudo cat /sys/kernel/debug/dri/0/i915_vbt > "${MODEL_DIR}/vbt.rom"
INTEL_GOP_DRIVER_GUID="7755CA7B-CA8F-43C5-889B-E1F59A93D575"
EXTRACT_DIR="extract"
if [ -n "${BIOS_IMAGE}" ]
then
if "${SCRIPT_DIR}/extract.sh" "${BIOS_IMAGE}" "${INTEL_GOP_DRIVER_GUID}" -o "${EXTRACT_DIR}" > /dev/null
then
cp -v "$(find "${EXTRACT_DIR}" | grep IntelGopDriver | grep PE32 | grep body.bin)" "${MODEL_DIR}/IntelGopDriver.efi"
rm -rf "${EXTRACT_DIR}"
else
echo "IntelGopDriver not present in firmware image"
fi
fi
fi
# XXX: More reliable way to determine if system has an EC?
DMI_CHASSIS_TYPE=$(cat /sys/class/dmi/id/chassis_type)
if [ "${DMI_CHASSIS_TYPE}" = "9" ] || [ "${DMI_CHASSIS_TYPE}" = "10" ]
then
if [ -n "${EC_ROM}" ]
then
echo "Using proprietary EC ROM file"
cp "${EC_ROM}" "${MODEL_DIR}/ec.rom"
else
echo "Generating output for System76 EC firmware"
pushd ec/ecspy
cargo build --release
# TODO: Set backlights and fans to max and restore after
sudo target/release/ecspy > "${MODEL_DIR}/ecspy.txt"
# Strip EC RAM entries from output
sed -i '/^0x/d' "${MODEL_DIR}/ecspy.txt"
popd
fi
fi
"${SCRIPT_DIR}/readmes.sh"