# U-boot with patches to make the PinePhone boot faster and have control over the ddr clock speed
maintainer=""
pkgname=u-boot-sourceparts-pocketpc
pkgver=2021.1
pkgrel=3
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
	python-3.13-binman-migrate-from-pkg_resources-to-imp.patch
	libfdt-fix-invalid-version.patch
	libfdt.i_shipped-use-SWIG_AppendOutput.patch
	"
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
c0545c6a1d17507f7e4bc337200fa3a4a1a89d62122cc6599ea00f27187b85f3e322d945683a84cdff98d981cf604187ded05d7b93bb5fae5a3fe7f9751077a2  python-3.13-binman-migrate-from-pkg_resources-to-imp.patch
fa49de52d61e619991b05ed679f13bef6054d8740164b006a252f346993a2b471b266daa4cf320364f3f4bddf3882c39190f1a6916de8b1a92ff09242368f7ba  libfdt-fix-invalid-version.patch
016ef30dbbb1021dad4f58eeda3f5a5e89ff3a76dbc69e8e4cf6a3fc3983aef998cfe1087abb4b21bb2f859bd5cffe26c1da42d13f1c3d2e3c053c399f38720b  libfdt.i_shipped-use-SWIG_AppendOutput.patch
"
