# Template file for 'pinephone-uboot'
pkgname=pinephone-uboot
version=0.0.20200917
revision=3
archs="aarch64*"
create_wrksrc=yes
hostmakedepends="flex cross-or1k-none-elf-gcc dtc python3 python3-devel bc swig"
depends="u-boot-tools"
conf_files="/etc/default/${pkgname}-config"
short_desc="U-Boot for Pinephone with Crust support"
maintainer="John Sullivan <jsullivan@csumb.edu>"
license="GPL-2.0-only, BSD-3-Clause"
homepage="http://www.denx.de/wiki/U-Boot/WebHome"

_tag_uboot=crust-2020-09-17
_ver_armtf=2.4
_ver_crust=0.3

_uboot=u-boot-$_tag_uboot
_armtf=arm-trusted-firmware-$_ver_armtf
_crust=crust-$_ver_crust

distfiles="https://gitlab.com/pine64-org/u-boot/-/archive/${_tag_uboot}/${_uboot}.tar.gz
 https://github.com/ARM-software/arm-trusted-firmware/archive/v${_ver_armtf}.tar.gz
 https://github.com/crust-firmware/crust/archive/v${_ver_crust}.tar.gz"

checksum="395e61790d4877e11c470445dfee834a017a4b5df5f7ee34ebcaa125713150dc
 4bfda9fdbe5022f2e88ad3344165f7d38a8ae4a0e2d91d44d9a1603425cc642d
 cf02f227b909f40bcfc33a94088fce12356dab41bc7459c22b7b333b305d4c72"

# Note about _cross/_hostcc
#
# The Allwinner A64 has an "embedded controller" of sorts for managing
# peripherals when the A53 cores are turned off. This EC is a custom CPU core
# that uses the OpenRISC 1000 ISA, and it's what the Crust firmware runs
# on. or1k is the gcc platform for OpenRISC, and it has nothing to do with any
# real Void system targets. This is why the two cross-compilers are needed, or1k
# is used ONLY for the crust portion of the firmware.

_or1k="CROSS_COMPILE=or1k-none-elf-"

if [ "$CROSS_BUILD" ]; then
	_cross="CROSS_COMPILE=${XBPS_CROSS_TRIPLET}-"
	_hostcc="HOST_COMPILE=${XBPS_CROSS_TRIPLET}-"
fi

do_configure() {
	unset CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

	make -C $_crust $_hostcc $_or1k pinephone_defconfig
	make -C $_uboot $_cross pinephone_defconfig
	echo 'CONFIG_IDENT_STRING=" VoidLinux"' >> $_uboot/.config
}

do_build() {
	unset CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

	make -C $_armtf $_cross PLAT=sun50i_a64 DEBUG=1 $makejobs bl31
	make -C $_crust $_hostcc $_or1k $makejobs scp

	export BL31=$wrksrc/$_armtf/build/sun50i_a64/debug/bl31.bin
	export SCP=$wrksrc/$_crust/build/scp/scp.bin

	make -C $_uboot $_cross $makejobs
}

do_install() {
	vmkdir boot
	vinstall $wrksrc/$_uboot/u-boot-sunxi-with-spl.bin 0644 boot

	vinstall $FILESDIR/uboot.default 0644 etc/default ${pkgname}-config
	vinstall $FILESDIR/kernel.d/uboot 0750 etc/kernel.d/post-install 60-uboot

	cd $wrksrc/$_uboot
	vlicense Licenses/Exceptions
	vlicense Licenses/OFL.txt
	vlicense Licenses/README
	vlicense Licenses/bsd-2-clause.txt
	vlicense Licenses/bsd-3-clause.txt
	vlicense Licenses/eCos-2.0.txt
	vlicense Licenses/gpl-2.0.txt
	vlicense Licenses/ibm-pibs.txt
	vlicense Licenses/isc.txt
	vlicense Licenses/lgpl-2.0.txt
	vlicense Licenses/lgpl-2.1.txt
	vlicense Licenses/r8a779x_usb3.txt
	vlicense Licenses/x11.txt
}
