maintainer="Antoine Martin (ayakael) <dev@ayakael.net>"
pkgname=u-boot-radxa-cm5
pkgver=2017.09
pkgrel=0
pkgdesc="U-Boot fork for the Radxa CM5"
arch="aarch64"
_carch="arm"
license="GPL-2.0"
url="https://github.com/radxa/u-boot"
makedepends="
	arm-trusted-firmware
	bash
	bc
	bison
	coreutils
	dtc
	flex
	gnutls-dev
	openssl-dev
	python3
	py3-setuptools
	python3-dev
	swig
	u-boot-tools
"
options="!check !tracedeps pmb:crossdirect"
_targets="rpi-cm4-io io"
_repository="u-boot"
_commit="575d1a114c66ad09e0d9d9f478c993fc243f5aec"
source="
	$pkgname-$_commit.tar.gz::https://github.com/radxa/$_repository/archive/$_commit.tar.gz
	0001-menu-fix-timeout-duration.patch
	0002-rockchip-move-crypto-functions-to-rk3588s.patch
	0003-rk3588-support-compressed-kernel.patch
	disable-spl-spi-load.patch
	use-python3.patch
	https://github.com/radxa/rkbin/raw/efaf8526fe85521ac86f4e88b0a6a6c6cf2563a1/bin/rk35/rk3588_ddr_lp4_2112MHz_lp5_2400MHz_v1.19.bin
"

builddir="$srcdir/$_repository-$_commit"

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

	for target in $_targets; do
		export BUILD_DIR="$builddir"/build_$target

		export ROCKCHIP_TPL="$srcdir"/rk3588_ddr_lp4_2112MHz_lp5_2400MHz_v1.19.bin
		export UBOOTVERSION=rk2410-2017.09-1
		export BL31="/usr/share/arm-trusted-firmware/rk3588/bl31.elf"

		# python scripts for BL31 management doesn't get copied over
		mkdir -p "$BUILD_DIR"/arch/arm/mach-rockchip
		cp "$builddir"/arch/arm/mach-rockchip/*.py "$BUILD_DIR"/arch/arm/mach-rockchip/.

		make O="$BUILD_DIR" ARCH="$_carch" radxa-cm5-$target-rk3588s_defconfig
		make O="$BUILD_DIR" ARCH="$_carch" all u-boot.itb

		# make idbloader.img
		mkimage -n rk3588 -T rksd -d "$ROCKCHIP_TPL:$BUILD_DIR"/spl/u-boot-spl.bin "$BUILD_DIR"/idbloader.img
	done
}

package() {
	for target in $_targets; do
		install -Dm644 "$builddir/build_$target/u-boot-dtb.bin" "$pkgdir/usr/share/u-boot/radxa-cm5/$target/u-boot.bin"
		install -Dm644 "$builddir/build_$target/idbloader.img" "$pkgdir/usr/share/u-boot/radxa-cm5/$target/idbloader.img"
		install -Dm644 "$builddir/build_$target/u-boot.itb" "$pkgdir/usr/share/u-boot/radxa-cm5/$target/u-boot.itb"
	done
}

sha512sums="
4805d0224b8c4ffecdaf560cf493ea06df9e195b7009aa7c30457cb33ecaeea92a64e7b2004ec1903d20fecc0e711905c710bca53072eefececd41a46b4176a2  u-boot-radxa-cm5-575d1a114c66ad09e0d9d9f478c993fc243f5aec.tar.gz
36baf3b8618928e8927229353157ccecb5477e6ed868b9425a04acc2b297026084bd5ed96785292ab2ccb74b9f6328fe032b55505749b103cd68e25777dc3334  0001-menu-fix-timeout-duration.patch
364a8701898e34f6235d2de486316413b5beec95578ee450e504e9a6f5c9feec773642a8129a91eef264e9466206c082950d0793a6a3e97f638edac42e4a70bf  0002-rockchip-move-crypto-functions-to-rk3588s.patch
d80fbde984311b97b0dbd3d1bf7e740350f7c85f3bc8c22fdac93f9200c5b7c722c956dbcc3ec0f4422a72fe5c46b14c0770433efa0f6faf01520ad2ac5f9eca  0003-rk3588-support-compressed-kernel.patch
11a1e30ad0002148f32314dc60c95120d45ffe462755e0bbc7d52ee4001e3283ffb87ba6f58d41feb0c40d456ef42c03857f98f8921c9a7bb810bbf404abedb3  disable-spl-spi-load.patch
1164247343518c9820ce2bf1302f18079b9af81f6ef164e5b9d2c5cbad00ac20e1943bb44d48862c0d4a2f0b7448f9fa35d02d2f66e90c34033d999b3a968443  use-python3.patch
0b529e57a7206471a66dcdb98dab3f8deeff333d587c2ebdd08febb40ab9b44e2d51de364d2383ed80dd2b38764ccb552a48f9e34da43607f1e08d385af3f161  rk3588_ddr_lp4_2112MHz_lp5_2400MHz_v1.19.bin
"
