# U-boot with patches to make the PinePhone boot faster and have control over the ddr clock speed
pkgname=u-boot-sourceparts-pocketpc
pkgver=2021.1
pkgrel=1
pkgdesc="U-Boot bootloader for the Sourceparts Pocket P.C."
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"
makedepends="$depends_dev bc dtc python3-dev py3-setuptools swig bison flex openssl-dev arm-trusted-firmware"
options="!check"
_ref="49694d39d856c9ab537c4f2ff1d0167f4e21d19b"
source="https://github.com/PopcornComputer/PocketPC-Uboot/archive/$_ref.tar.gz
	update-u-boot
	"
builddir="$srcdir/PocketPC-Uboot-$_ref"
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/arm-trusted-firmware/sun50i_a64/bl31.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/pocket_pc_defconfig
		cat configs/pocket_pc_defconfig | grep CONFIG_DRAM_CLK
		make O="$BUILD_DIR-$freq" HOSTCC=gcc ARCH=arm pocket_pc_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/sourceparts-pocketpc/u-boot-sunxi-with-spl-$freq.bin"
		install -D -m644 "build-$freq/u-boot-sunxi-with-spl.bin.sha512" \
			"$pkgdir/usr/share/u-boot/sourceparts-pocketpc/u-boot-sunxi-with-spl-$freq.bin.sha512"
	done
	install -D -m 755 "$srcdir"/update-u-boot "$pkgdir"/usr/sbin/update-u-boot
}

sha512sums="
39caa823655d38510d563b149265da1bba2e63efcd2ef5999a0a41a6b96775108c3ee86e6d9d6acc0fc11acb8dd66e534ec834c12fd8cd76057ca52c581557fe  49694d39d856c9ab537c4f2ff1d0167f4e21d19b.tar.gz
d9ebf59e37a2d8435d5a31884fde554f175390d1ce23511da8d38ba368f6b812b45f1399a0fa49821c37ef573dfb4500e895fcc993bb247fae88f63b3bb7804f  update-u-boot
"
