#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
#
enable_extension "marvell-tools"

# The C++ compiler is no longer included by default in the Armbian build system (but required for mox related stuff)
# Enable the extension that brings includes it. It could've been an inline extension (eg: the function right here),
# but having a core extension allows it to be pre-included in the Dockerfile generation and thus in the Docker images.
enable_extension "c-plus-plus-compiler"

# The BLx stuff requires 32-bit compiler, add it using an inline hook.
# This is only used for non-Docker, since the Docker image already has it, since it includes compilers for all architectures.
function add_host_dependencies__mvebu64_add_32_bit_c_compiler() {
	display_alert "Adding armhf C compiler to host dependencies" "for mvebu64 BLx compile" "debug"
	declare -g EXTRA_BUILD_DEPS="${EXTRA_BUILD_DEPS} gcc-arm-linux-gnueabi" # @TODO: convert to array later
}

ARCH=arm64
BOOTBRANCH='tag:v2023.01'
BOOTPATCHDIR='v2023.01'
BOOTENV_FILE='mvebu64.txt'
BOOTSCRIPT_OUTPUT='boot.scr'
ATFSOURCE='https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git'
ATFDIR='arm-trusted-firmware-espressobin'
ATFBRANCH='tag:v2.8'
ATF_USE_GCC='> 7.2'

if [[ "${SKIP_EXTERNAL_TOOLCHAINS}" == "yes" ]]; then
	ATF_COMPILER='aarch64-linux-gnu-'
else
	ATF_COMPILER='aarch64-none-linux-gnu-'
fi

if [[ $BOARD = macchiatobin-doubleshot ]]; then
	export SCP_BL2=$SRC/cache/sources/marvell-binaries/mrvl_scp_bl2.img # ATTENTION! This is actually a required 'export', since it's picked up by ATF's make.

	ATF_TARGET_MAP="USE_COHERENT_MEM=0 LOG_LEVEL=20 MV_DDR_PATH=$SRC/cache/sources/marvell-ddr SECURE=0 PLAT=a80x0_mcbin;;build/a80x0_mcbin/release/bl31.bin"
	UBOOT_TARGET_MAP="DEVICE_TREE=armada-8040-mcbin ;;flash-image.bin"

	BOOTSCRIPT="boot-macchiatobin-doubleshot.cmd:boot.cmd"
	SERIALCON='ttyS0'
else
	ATF_TARGET_MAP="USE_COHERENT_MEM=0 LOG_LEVEL=20 SECURE=0 CLOCKSPRESET=CPU_800_DDR_800 DDR_TOPOLOGY=2 BOOTDEV=SPINOR PARTNUM=0 PLAT=a3700;;build/a3700/release/bl31.bin"
	UBOOT_TARGET_MAP="DEVICE_TREE=armada-3720-espressobin ;;flash-image-*.bin"

	BOOTSCRIPT="boot-espressobin.cmd:boot.cmd"
	SERIALCON='ttyMV0'
fi

case $BRANCH in

	current)

		declare -g KERNEL_MAJOR_MINOR="5.15" # Major and minor versions of this kernel.
		KERNELBRANCH='branch:linux-5.15.y'

		;;

	edge)
		declare -g KERNEL_MAJOR_MINOR="6.6" # Major and minor versions of this kernel.
		KERNELBRANCH='branch:linux-6.6.y'
		;;

esac

CPUMIN=200000
CPUMAX=1300000
GOVERNOR=ondemand

NM_IGNORE_DEVICES="interface-name:eth*,interface-name:wan*,interface-name:lan*,interface-name:br*"

write_uboot_platform() {
	if [[ $BOARD = macchiatobin-doubleshot ]]; then
		dd if=$1/flash-image.bin of=$2 bs=512 seek=1 status=noxfer > /dev/null 2>&1
	else
		# flashing process via u-boot only for now https://www.armbian.com/espressobin/
		/bin/true
	fi
}

family_tweaks() {
	[[ -f $SDCARD/etc/netplan/armbian-default.yaml ]] && sed -i "s/^  renderer.*/  renderer: networkd/" $SDCARD/etc/netplan/armbian-default.yaml
	cp $SRC/packages/bsp/mvebu64/networkd/10* $SDCARD/etc/systemd/network/
	if [[ $BOARD = "espressobin" ]]; then
		echo "#Marvell Espressobin Console" >> $SDCARD/etc/securetty
		echo "ttyMV0" >> $SDCARD/etc/securetty
	fi
}

family_tweaks_bsp() {
	if [[ $BOARD = "espressobin" ]]; then
		cp "$SRC/packages/bsp/mvebu64/initramfs/99-uboot-fit" "$destination/etc/initramfs/post-update.d/"
	fi
}

atf_custom_postprocess() {
	# prepare compilers for postprocess
	ubootdir="$SRC/cache/sources/u-boot-worktree/$BOOTDIR/${BOOTBRANCH##*:}"
	declare -g ATF1=$toolchain/$ATF_COMPILER
	if [[ "${SKIP_EXTERNAL_TOOLCHAINS}" == "yes" ]]; then
		declare -g TOOLCHAIN_NAME="arm-linux-gnueabi-"
	else
		declare -g TOOLCHAIN_NAME="arm-none-linux-gnueabihf-"
	fi
	declare -g ATF2=$(find_toolchain "$TOOLCHAIN_NAME" "> 10.0")/$TOOLCHAIN_NAME
	declare -g BL33=$ubootdir"/u-boot.bin"
}

uboot_custom_postprocess() {
	# clean previous
	run_host_command_logged rm -f "$ubootdir"/flash-image*
	local atfdir="$SRC/cache/sources/$ATFDIR/${ATFBRANCH##*:}"
	local ubootdir="$SRC/cache/sources/u-boot-worktree/$BOOTDIR/${BOOTBRANCH##*:}"
	local moxbootdir="$SRC/cache/sources/mox-boot"
	cd $atfdir

	if [[ $BOARD = macchiatobin-doubleshot ]]; then
		run_host_command_logged make distclean

		FILENAME="flash-image.bin"
		display_alert "Building $FILENAME" "" "info"
		# http://wiki.macchiatobin.net/tiki-index.php?page=Build+from+source+-+Bootloader#Building_ATF
		run_host_command_logged make distclean
		run_host_command_logged "CFLAGS=-fdiagnostics-color=always" pipetty make USE_COHERENT_MEM=0 LOG_LEVEL=20 MV_DDR_PATH=$SRC/cache/sources/marvell-ddr PLAT=a80x0_mcbin CROSS_COMPILE=$ATF1 BL33=$BL33 mrvl_flash $CTHREADS
		run_host_command_logged cp -pv build/a80x0_mcbin/release/flash-image.bin $ubootdir/$FILENAME
	else
		cd $moxbootdir
		make clean # @TODO: rpardini: why?
		run_host_command_logged "CFLAGS=-fdiagnostics-color=always" pipetty make CROSS_CM3=$ATF2 wtmi_app.bin
		cd $atfdir

		clocks=(600_600 800_800 1000_800 1200_750)
		topology=(512m_1cs_0 1g_2cs_2 1g_1cs_4 2g_2cs_7 1g_1cs_5 2g_2cs_6)
		for i in "${clocks[@]}"; do
			for j in "${topology[@]}"; do
				if [[ $j = *cs_5 || $j = *cs_6 ]]; then local ddrname="DDR4"; else ddrname="DDR3"; fi
				FILENAME="flash-image-$ddrname-$j-$i.bin"
				DDR_TOPOLOGY="${j##*_}"
				CLOCKSPRESET="CPU_${i%_*}_DDR_${i##*_}"
				display_alert "Building $FILENAME" "mvebu64" "info"
				run_host_command_logged make distclean
				run_host_command_logged "CFLAGS=-fdiagnostics-color=always" pipetty make CROSS_COMPILE=$ATF1 \
					CROSS_CM3=$ATF2 \
					USE_COHERENT_MEM=0 \
					PLAT=a3700 \
					CLOCKSPRESET=$CLOCKSPRESET DDR_TOPOLOGY=$DDR_TOPOLOGY \
					MV_DDR_PATH=$SRC/cache/sources/marvell-ddr \
					WTP=$SRC/cache/sources/marvell-tools \
					CRYPTOPP_PATH=$SRC/cache/sources/cryptopp \
					BL33=$BL33 \
					WTMI_IMG=$moxbootdir/wtmi_app.bin \
					BOOTDEV=SPINOR PARTNUM=0 \
					LOG_LEVEL=20 all fip mrvl_flash $CTHREADS
				run_host_command_logged cp -pv build/a3700/release/flash-image.bin $ubootdir/$FILENAME
			done
		done
	fi

	cd $ubootdir
}
