source "${BASH_SOURCE%/*}/include/meson64_common.inc"
ASOUND_STATE="asound.state.meson64"

[[ ${BRANCH} == legacy ]] && SERIALCON=ttyS0

if [[ -n ${USE_ODROID_UBOOT} ]] && [[ "${USE_ODROID_UBOOT}" == *"${BRANCH}"* ]]; then
	# Current BRANCH is listed in USE_ODROID_UBOOT; use _ODROID variants if set.
	BOOTCONFIG="${BOOTCONFIG_ODROID:-${BOOTCONFIG}}"
	BOOTSCRIPT="${BOOTSCRIPT_ODROID:-${BOOTSCRIPT}}"

	# Enable Odroid's legacy u-boot building. This requires old toolchains etc, but does NOT require FIP trees.
	UBOOT_TARGET_MAP=';;sd_fuse/u-boot.bin'
	UBOOT_USE_GCC='< 4.9'
	UBOOT_TOOLCHAIN2="arm-none-eabi-:< 5.0"
	UBOOT_COMPILER="aarch64-none-elf-"

	BOOTSOURCE='https://github.com/hardkernel/u-boot.git'
	BOOTBRANCH='branch:odroidg12-v2015.01'
	BOOTPATCHDIR='u-boot-odroid'
	BOOTDIR='u-boot-odroid'

	write_uboot_platform() {
		dd if=$1/u-boot.bin of=$2 bs=512 seek=1 conv=fsync >/dev/null 2>&1
	}

else
	# Mainline u-boot, everything is done by meson64_common.inc, we just need to handle FIP blobs

	# Handling of FIP blobs
	uboot_custom_postprocess() {
		# @TODO: these should come from FIP_TREE_BOARD/FIP_TREE_FAMILY vars in board.conf instead of hardcoded here
		if [[ $BOARD == odroidn2* ]]; then
			# FIP trees 'odroid-n2-plus' and 'odroid-n2' are identical.
			uboot_g12_postprocess "$SRC"/cache/sources/amlogic-boot-fip/odroid-n2 g12b
		elif [[ $BOARD == khadas-vim3 ]]; then
			# 'khadas-vim3' FIP tree contains 'lpddr3_1d.fw' which will trigger '--ddrfw9' in uboot_g12_postprocess
			uboot_g12_postprocess "$SRC"/cache/sources/amlogic-boot-fip/khadas-vim3 g12b
		elif [[ $BOARD == radxa-zero2 ]]; then
			uboot_g12_postprocess "$SRC"/cache/sources/amlogic-boot-fip/radxa-zero2 g12b
		else
			echo "Don't know how to handle FIP trees for board '${BOARD}'"
			exit 2
		fi
	}
fi