# Reference: <https://u-boot.readthedocs.io/en/latest/board/amlogic/radxa-zero.html>
# Maintainer: Ferass El Hafidi <funderscore@postmarketos.org>
pkgname=u-boot-librecomputer-lepotato
pkgver=2023.04
pkgrel=1
pkgdesc="U-Boot bootloader for Libre Computer AML-S905X-CC-V1.0"
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 proprietary"
makedepends="$depends_dev
	bc
	bison
	dtc
	flex
	openssl-dev
	py3-setuptools
	python3-dev
	swig
	bash
	"
options="!check"
# https://u-boot.readthedocs.io/en/latest/board/amlogic/pre-generated-fip.html
_amlogicblob_commit="e96b6a694380ff07d5a9e4be644ffe254bd18512"
source="
	https://source.denx.de/u-boot/u-boot/-/archive/v$pkgver/u-boot-v$pkgver.tar.gz
	amlogicblobs.tar.gz::https://github.com/LibreELEC/amlogic-boot-fip/archive/$_amlogicblob_commit.tar.gz
"
builddir="$srcdir/u-boot-v$pkgver"
_fipbuilddir="$srcdir/amlogic-boot-fip-$_amlogicblob_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

	make libretech-cc_defconfig
	make all
	cd $_fipbuilddir
	mkdir build
	./build-fip.sh lepotato $builddir/u-boot.bin ./build
	cd ./build
	dd if=u-boot.bin.sd.bin of=u-boot.bin.sd-stripped.bin conv=fsync,notrunc bs=512 skip=1
}

package() {
	_installdir="$pkgdir/usr/share/u-boot/librecomputer-lepotato"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin.sd-stripped.bin" -t "$_installdir"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin.sd.bin" -t "$_installdir"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin" -t "$_installdir"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin.usb.bl2" -t "$_installdir"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin.usb.tpl" -t "$_installdir"
}

sha512sums="
52a81c3733c7b8a3e95227e1ad2f7315851c0eba14f44576ba4a320897cdc387b763323ec0a750ee111ddd82e9333e834f01e097bf4840d08eab9185427c3fba  u-boot-v2023.04.tar.gz
98ecbbc56e8326ce11db4e316a7bb3812d713fbe630235825ddb657b28419eb406f7117208a5c0360653ebdccb9af1a48357d0c257cd61f11a8c3cdaa78548b5  amlogicblobs.tar.gz
"
