#!/bin/bash

#  helper script: build ROM images with depthcharge and put them in ./bin/
#
#	Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Paul Kocialkowski <contact@paulk.fr>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# This script assumes that the working directory is the
# root of libreboot_src or libreboot git.

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e

printf "Building ROM images with the depthcharge payload\n"

if [ ! -d "bin/" ]
then
	mkdir "bin/"
fi

# Build libpayload
# ------------------------------------------------------------------------------

buildlibpayload() {
	family="$1"

	cd "coreboot/payloads/libpayload"

	make distclean
	rm -Rf "install/"

	make KBUILD_DEFCONFIG="configs/config.$family" defconfig
	make -j"$(nproc)"
	make DESTDIR="install" install

	cd "../../../"
}

# Build depthcharge
# ------------------------------------------------------------------------------

builddepthcharge() {
	board="$1"

	cd "depthcharge/"

	make distclean
	rm -f "../coreboot/depthcharge.elf"

	make BOARD="$board" defconfig
	make BOARD="$board" LIBPAYLOAD_DIR="$(pwd)/../coreboot/payloads/libpayload/install/libpayload" VB_SOURCE="$(pwd)/../coreboot/3rdparty/vboot" -j"$(nproc)" depthcharge_unified

	cp "build/depthcharge.elf" "../coreboot/"

	cd "../"
}

# Build coreboot
# ------------------------------------------------------------------------------

buildcoreboot() {
	board="$1"

	cd "coreboot/"

	make distclean
	rm -f "lbversion"
	rm -f *.img

	if [ -f "../version" ]; then
		# _src release archive is being used
		version="$(cat ../version)"
	else
		# git repo is being used
		version="$(git describe --tags HEAD)"
	fi
	printf '%s\n' "${version}" > "lbversion"
	printf 'libreboot-%s' "${version}" > "ro-frid"

	make KBUILD_DEFCONFIG="$(pwd)/../resources/libreboot/config/depthcharge/${board}/config" defconfig
	make -j"$(nproc)"

	# Add version information to this image
	./util/cbfstool/cbfstool "build/coreboot.rom" add -f lbversion -n lbversion -t raw

	cbfs_size=$( grep CONFIG_CBFS_SIZE ".config" | sed "s/.*[[:space:]]*=[[:space:]]*//g" )
	cbfs_size=$( printf "%d\n" "$cbfs_size" )
	cbfs_size=$(( $cbfs_size / 1024 ))

	dd if="build/coreboot.rom" of="coreboot.img" bs=1024 count="$cbfs_size"

	objcopy -I binary -O binary --pad-to=0x100 --gap-fill=0x00 "ro-frid" "ro-frid.img"

	# prepare directory for new images
	rm -Rf "${board:?}/"
	mkdir "$board/"
	# move the images into the newly created directory
	mv "coreboot.img" "$board/"
	mv "ro-frid.img" "$board/"
	# copy the scripts too
	cp "../resources/libreboot/install/depthcharge/cros-flash-replace" "$board/"
	cp "../resources/libreboot/install/depthcharge/${board}/layout.txt" "$board/"
	# delete the old images from ../bin
	rm -Rf "../bin/depthcharge/${board}/"
	# now put the new images in ./bin/depthcharge/
	[ ! -d "../bin/depthcharge/" ] && mkdir -p "../bin/depthcharge/"
	mv "$board/" "../bin/depthcharge/"

	cd "../"
}

# Build ROM images for supported boards
buildrom() {
	board="$1"
	if [ -f "resources/libreboot/config/depthcharge/${board}/config" ]; then
		family=$( echo ${board} |sed "s/_.*//g" )

		buildlibpayload "$family"
		builddepthcharge "$board"
		buildcoreboot "$board"
	fi
}

if [ $# -gt 0 ]; then
	for board in "${@}"; do
		buildrom "$board"
	done
else
	for board in resources/libreboot/config/depthcharge/*; do
		buildrom "${board##*/}"
	done
fi
