# Template file for 'pinephone-uboot'
pkgname=pinephone-uboot
version=0.0.20200904
revision=1
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-04
_commit_armtf=2bdb4611ad6efdb3da2ff0d2de7fee4c43e95aeb
_commit_crust=e63106c70bc4ddfa3575e4f1f3667de0795252a8

_uboot=u-boot-$_tag_uboot
_armtf=arm-trusted-firmware-$_commit_armtf
_crust=crust-$_commit_crust

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

checksum="06eb107aced5b41da3cd0cfdcc2ad90b7c51fcbec7aac5c7388e30c58e2536b8
 48291b621a4bc38c7956964407130edd2d02d8b3789fce4193ac1b747a5b37db
 6fc2d68725a78b1ca6e7c6602286fc2aa2ddcdea28e83a85f6b988fcc5a513b8"

do_patch() {
	for _patch in $PATCHESDIR/armtf/*.patch; do
		patch -d $_armtf -slNp1 -i $_patch
	done
}

# 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-"

do_configure() {
	unset CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
	local _cross _hostcc

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

	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
	local _cross _hostcc

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

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

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

	make -C $_uboot $_cross -j $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
}
