# Reference: <https://u-boot.readthedocs.io/en/latest/board/amlogic>
# Co-Maintainer: exkc <exxxxkc@getgoogleoff.me>
# Co-Maintainer: hexaheximal <hexaheximal@proton.me>
maintainer="Ferass El Hafidi <funderscore@postmarketos.org>"
pkgname=u-boot-amlogic
pkgver=2026.04
pkgrel=0
pkgdesc="U-Boot bootloader for Amlogic AArch64-based devices"
url="https://u-boot.org/"
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
	crust
	dtc
	flex
	gxlimg
	gnutls-dev
	openssl-dev
	py3-setuptools
	python3-dev
	qemu-x86_64
	swig
	bash
"
options="!check"
# https://u-boot.readthedocs.io/en/latest/board/amlogic/pre-generated-fip.html
_amlogicblob_commit="8599bc77b17f38e69275f6145acc5792faab735e"
source="
	https://source.denx.de/u-boot/u-boot/-/archive/v$pkgver/u-boot-v$pkgver.tar.gz
	0001-add-x96-mini-support.patch
	amlogic-fip.tar.gz::https://github.com/LibreELEC/amlogic-boot-fip/archive/$_amlogicblob_commit.tar.gz
"
builddir="$srcdir/u-boot-v$pkgver"
provides="u-boot=$pkgver-r$pkgrel"
replaces="
	u-boot-amediatech-x96-mini
	u-boot-bananapi-bpi-cm4
	u-boot-beelink-gt1
	u-boot-librecomputer-lepotato
	u-boot-odroid-c4
	u-boot-odroid-n2plus
	u-boot-radxa-zero
"
_fip_builddir="$srcdir/amlogic-boot-fip-$_amlogicblob_commit"

# Format: <board>:<board u-boot defconfig>,<amlogic-boot-fip board files>
_gxl_boards="
	beelink-gt1:beelink-gt1-ultimate,beelink-gt1
	librecomputer-lepotato:libretech-cc,lepotato
"
_g12a_boards="
	bananapi-bpi-cm4-cm4io:bananapi-cm4-cm4io,bananapi-cm4io
	odroid-c4:odroid-c4,odroid-c4
	odroid-n2plus:odroid-n2,odroid-n2-plus
	radxa-zero:radxa-zero,radxa-zero
"
_boards="
	$_gxl_boards
	$_g12a_boards
"

for board in $_boards; do
	_board_codename=${board%%:*}
	subpackages="$subpackages $pkgname-$_board_codename:${_board_codename//-/_}"
done

prepare() {
	default_prepare
}

#
# Generic u-boot building
#
_build_u_boot() {
	board_config="$1"
	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 ${board_config}_defconfig all
}

#
# SoC-specific FIP building
#
_build_fip_gxl() {
	board_fip_dir="$1"
	u_boot="$2"

	# create amlogic fip
	python "$board_fip_dir/acs_tool.py" "$board_fip_dir/bl2.bin" "$board_fip_dir/bl2_acs.bin" "$board_fip_dir/acs.bin" 0
	"$board_fip_dir/blx_fix.sh" "$board_fip_dir/bl2_acs.bin" "$board_fip_dir/zero_tmp" \
		"$board_fip_dir/bl2_zero.bin" "$board_fip_dir/bl21.bin" \
		"$board_fip_dir/bl21_zero.bin" "$board_fip_dir/bl2_new.bin" bl2
	gxlimg -t bl2 -s "$board_fip_dir/bl2_new.bin" "$board_fip_dir/bl2.bin.sig"
	bl2bin="$board_fip_dir/bl2.bin.sig"

	# encrypt BL30 and BL31
	"./$board_fip_dir/blx_fix.sh" "$board_fip_dir/bl30.bin" "$board_fip_dir/zero_tmp" \
		"$board_fip_dir/bl30_zero.bin" "$board_fip_dir/bl301.bin" \
		"$board_fip_dir/bl301_zero.bin" "$board_fip_dir/bl30_new.bin" bl30
	gxlimg -t bl3x -c "$board_fip_dir/bl30_new.bin" "$board_fip_dir/bl30.bin.enc"
	gxlimg -t bl3x -c "$board_fip_dir/bl31.img" "$board_fip_dir/bl31.img.enc"

	bl30bin="$board_fip_dir/bl30.bin.enc"
	bl31bin="$board_fip_dir/bl31.img.enc"

	# encrypt u-boot
	gxlimg -t bl3x -c "$u_boot" "$board_fip_dir/u-boot.enc"

	# and finally create the final image
	gxlimg -t fip \
		--bl2 "$bl2bin" \
		--bl30 "$bl30bin" \
		--bl31 "$bl31bin" \
		--bl33 "$board_fip_dir/u-boot.enc" \
		"$board_fip_dir/u-boot.bin"
}

_build_fip_g12a() {
	board_fip_dir="$1"
	u_boot="$2"

	# create amlogic fip
	"./$board_fip_dir/blx_fix.sh" "$board_fip_dir/bl2.bin" "$board_fip_dir/zero_tmp" \
		"$board_fip_dir/bl2_zero.bin" "$board_fip_dir/acs.bin" \
		"$board_fip_dir/bl21_zero.bin" "$board_fip_dir/bl2_new.bin" bl2
	gxlimg -t bl2 -s "$board_fip_dir/bl2_new.bin" "$board_fip_dir/bl2.bin.sig"
	bl2bin="$board_fip_dir/bl2.bin.sig"

	# encrypt BL30 and BL31
	"./$board_fip_dir/blx_fix.sh" "$board_fip_dir/bl30.bin" "$board_fip_dir/zero_tmp" \
		"$board_fip_dir/bl30_zero.bin" "$board_fip_dir/bl301.bin" \
		"$board_fip_dir/bl301_zero.bin" "$board_fip_dir/bl30_new.bin" bl30
	gxlimg -t bl30 -s "$board_fip_dir/bl30_new.bin" "$board_fip_dir/bl30.bin.enc"
	gxlimg -t bl3x -s "$board_fip_dir/bl31.img" "$board_fip_dir/bl31.img.enc"
	bl30bin="$board_fip_dir/bl30.bin.enc"
	bl31bin="$board_fip_dir/bl31.img.enc"

	# encrypt u-boot
	gxlimg -t bl3x -s "$builddir/u-boot.bin" "$board_fip_dir/u-boot.enc"

	ddrfw="
		--ddrfw $board_fip_dir/ddr4_1d.fw --ddrfw $board_fip_dir/ddr4_2d.fw --ddrfw $board_fip_dir/ddr3_1d.fw
		--ddrfw $board_fip_dir/piei.fw --ddrfw $board_fip_dir/lpddr4_1d.fw --ddrfw $board_fip_dir/lpddr4_2d.fw
		--ddrfw $board_fip_dir/diag_lpddr4.fw --ddrfw $board_fip_dir/aml_ddr.fw
	"

	if [ -e "$board_fip_dir"/lpddr3_1d.fw ]; then
		ddrfw="$ddrfw --ddrfw $board_fip_dir/lpddr3_1d.fw"
	fi

	# and finally create the final image
	gxlimg -t fip --rev v3 \
		--bl2 "$bl2bin" \
		--bl30 "$bl30bin" \
		--bl31 "$bl31bin" \
		--bl33 "$board_fip_dir/u-boot.enc" \
		$ddrfw \
		"$board_fip_dir/u-boot.bin"
}

build() {
	for board in $_boards; do
		local board_names=${board##*:} board_config board_codename="" board_fip_dir
		board_config=${board_names%%,*}
		board_codename=${board%%:*}
		board_fip_dir=${board##*,}

		msg "Building U-Boot for $board_codename (config: $board_config)"
		_build_u_boot $board_config

		cd $_fip_builddir
		mkdir -p ./build

		if echo $_gxl_boards | grep -q $board; then
			msg "Building Amlogic GXL firmware package for $board_codename (FIP: $board_fip_dir)"
			_build_fip_gxl "$board_fip_dir" "$builddir/u-boot.bin"
		elif echo $_g12a_boards | grep -q $board; then
			msg "Building Amlogic G12A firmware package for $board_codename (FIP: $board_fip_dir)"
			_build_fip_g12a "$board_fip_dir" "$builddir/u-boot.bin"
		fi

		dd if="$board_fip_dir/u-boot.bin" of="./build/u-boot-$board_codename.bin.sd-stripped.bin" conv=fsync,notrunc bs=512
		cd $builddir
	done
}

package() {
	mkdir -p "$pkgdir"
}

_board_package() {
	pkgdesc="U-Boot bootloader for Amlogic AArch64-based devices ($1)"
	depends="u-boot-amlogic"
	board_codename=$1
	board_fip_dir=$2
	mkdir -p "$subpkgdir/usr/share/u-boot/$board_codename/"
	install -D -m644 "$srcdir/amlogic-boot-fip-$_amlogicblob_commit/build/u-boot-$board_codename.bin.sd-stripped.bin" \
		"$subpkgdir/usr/share/u-boot/$board_codename/u-boot.bin.sd-stripped.bin"
}

for board in $_boards; do
	_board_codename=${board%%:*}
	_board_fip_dir=${board##*,}
	eval "${_board_codename//-/_}() { _board_package $_board_codename $_board_fip_dir; }"
done

sha512sums="
3e88bde53d54edc08b0ef108c5f87760fd57fd9932ce9d4a99f75bc1aa085f6d877801251f085adbf211351ca0a5d99dcc810bb8b4510fdc06159b9bf6ee850c  u-boot-v2026.04.tar.gz
415e81dedca92da59bb12025e64a02b14c8cd43c1a7d80b58e808db8f61402ecf396bf6c072c1cc18546254a5c88c9573c255c37496078a71c825abea87211be  0001-add-x96-mini-support.patch
74c57daff859e7f449bd2743c7a8c0358b8dbe5d321ddb3a5b242b4d35683890d500d23e769490b1419a18bed233aea03ee11103bb4d646c502e4018283ba975  amlogic-fip.tar.gz
"
