# Co-Maintainer: Jan Jasper de Kroon <jajadekroon@gmail.com>
# U-boot with patches to make the PinePhone boot faster and have control over the ddr clock speed
maintainer="Arnav Singh <me@arnavion.dev>"
pkgname=u-boot-pinephone
pkgver=2023.01
pkgrel=5
pkgdesc="U-Boot bootloader for the PINE64 PinePhone"
url="https://source.denx.de/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
	arm-trusted-firmware
	bc
	bison
	crust-pinephone
	dtc
	flex
	openssl-dev
	py3-setuptools
	python3-dev
	swig
	"
options="!check"
source="https://source.denx.de/u-boot/u-boot/-/archive/v$pkgver/u-boot-v$pkgver.tar.gz
	update-u-boot
	0001-common-expose-DRAM-clock-speed.patch
	0002-disable-bootdelay.patch
	0003-libfdt.i_shipped-use-SWIG_AppendOutput.patch
	0004-python-3.13-binman-migrate-from-pkg_resources-to-imp.patch
	"
builddir="$srcdir/u-boot-v$pkgver"
_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/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="
c25aae1b0d2f677d295587c6d318f23d52bc3a79e968bed9093645943a9840c56b1f6f44e5649d236e76e3dc85f0473c1da4a5205c02dcf437bca820acaf6fb8  u-boot-v2023.01.tar.gz
8dc0a8847ed26d91d3acc9b7b9dd300f95a68049207b09c3cfca1ec485f6f976aa65e0d101a4b46ecc4e85e943cbdec35e81ab0da07b1d5d0b68b4b10858b44b  update-u-boot
0a05291f5d544854b6e27c7c3290878c55382c50e0374c880d457e7c21425eaec23fd67047fd7e629dba3fa060aa041dec5880e7edc9e52dea897fb93af5d2aa  0001-common-expose-DRAM-clock-speed.patch
9b9a5bd2cb5c04715a5bb2d34bfc06e63817dba7ec40fa2b09000a0827590623e85ed5877d6b6bb39f2bb917e5e9d8e1379e3df9128e3304be06abc487c68df2  0002-disable-bootdelay.patch
016ef30dbbb1021dad4f58eeda3f5a5e89ff3a76dbc69e8e4cf6a3fc3983aef998cfe1087abb4b21bb2f859bd5cffe26c1da42d13f1c3d2e3c053c399f38720b  0003-libfdt.i_shipped-use-SWIG_AppendOutput.patch
c0545c6a1d17507f7e4bc337200fa3a4a1a89d62122cc6599ea00f27187b85f3e322d945683a84cdff98d981cf604187ded05d7b93bb5fae5a3fe7f9751077a2  0004-python-3.13-binman-migrate-from-pkg_resources-to-imp.patch
"
