# U-boot with patches to make the PinePhone boot faster and have control over the ddr clock speed
pkgname=u-boot-pinephone
pkgver=2021.01_git20201228
pkgrel=4
# Last commit from "crust" branch, as used in "crust-meta":
# https://gitlab.com/pine64-org/crust-meta/-/blob/8886bcc829179bf77216fade71f0ede9bd014e67/Makefile#L78
_commit="7492749fec31b1086bc8933bf113a766aea021aa"
pkgdesc="u-boot bootloader for the PINE64 PinePhone"
url="https://gitlab.com/pine64-org/u-boot"
arch="aarch64"
license="GPL-2.0-or-later OFL-1.1 BSD-2-Clause BSD-3-Clause eCos-2.0 IBM-pibs
	ISC LGPL-2.0-only LGPL-2.1-only X11"
depends="linux-postmarketos-allwinner>=5.12"
makedepends="$depends_dev bc dtc python3-dev py3-setuptools swig bison flex openssl-dev arm-trusted-firmware-crust crust"
options="!check"
source="https://gitlab.com/pine64-org/u-boot/-/archive/$_commit/u-boot-$_commit.tar.gz
	0001-expose-DRAM-clock-speed.patch
	update-u-boot
	"
builddir="$srcdir/u-boot-$_commit"
install="$pkgname.post-upgrade"
frequencies='528 552 624'

build() {
	touch include/config.h
	LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > include/timestamp_autogenerated.h
	LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> include/timestamp_autogenerated.h

	export BL31="/usr/share/crust/arm-trusted-firmware/sun50i_a64/bl31.bin"
	export SCP="/usr/share/crust/pinephone/scp.bin"
	export BUILD_DIR="$builddir"/build
	
	for freq in $frequencies; do
		mkdir -p "$BUILD_DIR-$freq"
		sed -rie "s/^(CONFIG_DRAM_CLK=).*$/CONFIG_DRAM_CLK=$freq/" configs/pinephone_defconfig
		cat configs/pinephone_defconfig | grep CONFIG_DRAM_CLK
		make O="$BUILD_DIR-$freq" HOSTCC=gcc ARCH=arm pinephone_defconfig
		make O="$BUILD_DIR-$freq" HOSTCC=gcc ARCH=arm all
		sha512sum -b "$BUILD_DIR-$freq/u-boot-sunxi-with-spl.bin" > "$BUILD_DIR-$freq/u-boot-sunxi-with-spl.bin.sha512"
	done
}

package() {
	for freq in $frequencies; do
		install -D -m644 "build-$freq/u-boot-sunxi-with-spl.bin" \
			"$pkgdir/usr/share/u-boot/pine64-pinephone/u-boot-sunxi-with-spl-$freq.bin"
		install -D -m644 "build-$freq/u-boot-sunxi-with-spl.bin.sha512" \
			"$pkgdir/usr/share/u-boot/pine64-pinephone/u-boot-sunxi-with-spl-$freq.bin.sha512"
	done
	install -D -m 755 "$srcdir"/update-u-boot "$pkgdir"/usr/sbin/update-u-boot
}

sha512sums="
73b132cdc26113a83d060f0d243320ca989b4efa98fe95a2205526a42f615ced7046bb9d4878c67a9a550b04c2519d90be3c2272be97c878d759bf5d027cf536  u-boot-7492749fec31b1086bc8933bf113a766aea021aa.tar.gz
3cba8a0e899be8cb0b9260b7a86d4115f74c279a1bac8c1403ee669adf91dca8215d8d2d6395ebb3cd67ce000f37a51d6e033b62a8d616b346a9072fa58a4b70  0001-expose-DRAM-clock-speed.patch
4857d5f6e4f591f97b8842cc9fb7d1131d71af63d05f6bd8457f4323886fb2334614a00699a3efc8d1b32d149d130c3cca0205150eedb40e9da60e4508281e36  update-u-boot
"
