# Seperate u-boot package for the pine64-pinebookpro because this includes
# a driver for the display and supports efi loading operating systems
maintainer="Quade Curry <qc8086@qc8086.com>"
pkgname=u-boot-pinebookpro
pkgver=2020.07
pkgrel=5
pkgdesc="u-boot bootloader for the rk3399"
url="https://gitlab.denx.de/u-boot/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
	arm-trusted-firmware
	bc
	bison
	dtc
	flex
	openssl-dev
	python3-dev
	swig
	u-boot-tools
	"
options="!check"
source="
	ftp://ftp.denx.de/pub/u-boot/u-boot-${pkgver//_/-}.tar.bz2
	0001-enable-rockchip-rng.patch
	0002-enable-usb-keyboard.patch
	0003-display-support.patch
	0004-rk3399-light-pinebook-power-and-standby-leds.patch
	0005-support-SPI-flash-boot.patch
	0006-implement-boot-menu.patch
	0007-rockchip-boot-order.patch
	0008-rockchip-boot-order-add-usb.patch
	0009-rk3399-support-compressed-kernel.patch
	pine64.bmp
	"
builddir="$srcdir"/u-boot-${pkgver//_/-}

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/rk3399/bl31.elf"
	export BUILD_DIR="$builddir"/build
	mkdir -p "$BUILD_DIR"
	make O="$BUILD_DIR" HOSTCC=gcc ARCH=arm LOGO_BMP=$srcdir/pine64.bmp pinebook-pro-rk3399_defconfig
	make O="$BUILD_DIR" HOSTCC=gcc ARCH=arm LOGO_BMP=$srcdir/pine64.bmp all

	mkimage -n rk3399 -T rkspi -d build/tpl/u-boot-tpl-dtb.bin:build/spl/u-boot-spl-dtb.bin spl.bin
	dd if=spl.bin bs=512K conv=sync of=u-boot.spiflash.bin
	cat u-boot.spiflash.bin build/u-boot.itb > build/u-boot.spiflash.bin
	# Make the image the exact size of the spi flash so flashrom does what it's supposed to
	truncate -s 16777216 build/u-boot.spiflash.bin

	truncate -s 50MB bootable.img
	dd if=build/idbloader.img of=bootable.img bs=512 seek=64 conv=notrunc
	dd if=build/u-boot.itb of=bootable.img bs=512 seek=16384 conv=notrunc
}

package() {
	install -D -m644 build/u-boot-rockchip.bin \
		"$pkgdir"/usr/share/u-boot/pine64-rockpro64/u-boot-rockchip.bin
	install -D -m644 build/u-boot.spiflash.bin \
		"$pkgdir"/usr/share/u-boot/pine64-rockpro64/u-boot.spiflash.bin
	install -D -m644 bootable.img \
		"$pkgdir"/usr/share/u-boot/pine64-rockpro64/bootable.img
}


sha512sums="df91264fe8a42e676c3f2aa09d644c6a3035cc8169bf52a8ab95efe7e542cfc758a0ef150949b0d9f03c7dec7d95f8d20a2efe2dbb60c2ef1a61c8063770478d  u-boot-2020.07.tar.bz2
058408fdf8921bcde5f9ca9ea03fdad6d87dd94f09bd6768792d72af43e64e801dfd8cb3f16e83be14153d9c38b3e3e23fe949778d9347ff72bada996bd82cfe  0001-enable-rockchip-rng.patch
8e454e486a7c9fe486a1bdbececf8c6b163b90b7b57a3822afb2a30f42ba9552439360d08a6ecc19a2ebe807c6c15961a73e0fe47dce9d2a7f86ce4742cb2510  0002-enable-usb-keyboard.patch
7292dab0af5b0127663eac7966499fc5af87c544485168b5269cdf16a226cd8a215b76a4745cf19afb751ea6fea680e26b86adfdaab54e3ec7fc8fd8bec85c6b  0003-display-support.patch
5188c54bb1efe18471b3e0ff8113b0d46d6ecad2be222416c7fb89021e76f2816771793d3efaba561602064a8079cf0da66e25228e132d44a0f6b14658f93ffb  0004-rk3399-light-pinebook-power-and-standby-leds.patch
ee51dca25a5a00011186c7f750e20ed08061a5a64a67bd81ca6d96cefdb139a15078d48c2c5b6ecb09b4ab5d778e11f3b62ff3daa74ffc7d73963b15e4eee215  0005-support-SPI-flash-boot.patch
8d989d200639204c8f7fb2620209bd1e096e8c5ea04085f68486b71490deb60e89e2d54704508bfe22c006b0d331dcc0c5cbfac6c0e02328a01b0f0b611d6b58  0006-implement-boot-menu.patch
3a91ae0f499ca668ac95cc24c3b2794b0884acfcba2a76493cb2329b8e00d84eb1bbfdcdb2475064cc5b0fb0b52df849afc59b6399de44699db9dfda4d0c5f3c  0007-rockchip-boot-order.patch
b655dfd83c12925d5e90f317c97d8bca9f047df337202627bfe40e0ebaba79d8adc429575a04f5af531bcc2626521b517ea7b9026dbdffb54ba97316d1674cf9  0008-rockchip-boot-order-add-usb.patch
cc03b87e42ce0ade1d3ddd9eebc76304451c01c4ee2ea2f52aa572ab8a30d84960df4d47f14315d96943467793fccee2052f53ace9fbac059e30c86c749d7ccc  0009-rk3399-support-compressed-kernel.patch
e317f8527dbb3b8a40a45d20da6c42f8cfd98b968060d6b4f1fa5453736441d36d1048bdc4359af47c2129fa86b6d43b3e263ef282f5c7b2e54f702d8b6bd8b1  pine64.bmp"
