#
# 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/
#
source "${BASH_SOURCE%/*}/include/meson64_common.inc"

CPUMIN=667000
CPUMAX=1800000
GOVERNOR=ondemand

case $BRANCH in

	#current)

	#	UBOOT_TARGET_MAP=";;build/u-boot.bin.sd.bin:u-boot.bin"
	#	BOOTPATCHDIR="v2015.01"
	#	BOOTBRANCH='branch:u-boot-2015.01'
	#	UBOOT_COMPILER="aarch64-linux-gnu-"
	#	UBOOT_USE_GCC='< 5.0'
	#	UBOOT_TOOLCHAIN2='arm-none-eabi-:5.0'

	#	KERNELBRANCH='branch:orange-pi-5.15-s905d3'
	#	KERNELPATCHDIR='rockchip-rk3588-legacy'
	#	LINUXCONFIG="linux-5.15-s905d3-current"
	#	KERNEL_USE_GCC='> 10.0'

	#	BOOT_FDT_FILE="meson-sm1-orangepi-3-plus.dtb"

	#	;;

	next)

		UBOOT_TARGET_MAP="u-boot-dtb.img;;u-boot.bin.sd.bin:u-boot.bin u-boot-dtb.img u-boot-spi.bin:u-boot-spi.bin"
		BOOTPATCHDIR="v2023.07-meson64"
		BOOTBRANCH='branch:v2023.07-meson64'

		KERNELBRANCH='branch:orange-pi-6.1-meson64'
		LINUXCONFIG="linux-6.1-meson64-next"

		#KERNELBRANCH='branch:orange-pi-6.5-meson64'
		#LINUXCONFIG="linux-6.5-meson64-next"

		KERNELPATCHDIR='meson64-next'
		KERNEL_USE_GCC='> 10.0'
		ASOUND_STATE='asound.state.orangepi3plus-next'

		;;
esac

write_uboot_platform_mtd() {
	dd if=$1/u-boot-spi.bin of=/dev/mtdblock0
}

family_tweaks() {

	#if [[ ${BRANCH} == current ]]; then
	#	sed -i 's/^debug_uart.*/debug_uart=ttyS0/' "${SDCARD}"/boot/orangepiEnv.txt
	#	SERIALCON="ttyS0"
	#fi

	install_wiringop

	rsync -a --chown=root:root --exclude=etc/pulse "${EXTER}"/packages/bsp/overlays_s905d3/* ${SDCARD}/

	chroot $SDCARD /bin/bash -c "apt-get -y -qq install usb-modeswitch tree dnsmasq v4l-utils swig python3-dev python3-setuptools bluez libncurses-dev" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1

	#install_balenaEtcher
}

uboot_custom_postprocess() {

	cd "${BOOTSOURCEDIR}"

	if [[ ${BOARD} == orangepi3plus && ${BRANCH} == next ]]; then

		# Preserve the u-boot produced (unsigned) u-boot.bin
		cp -v u-boot.bin u-boot.bin.orig

		# Sign using the old FIP blobs (amlogic-boot-fip-vim3-spi), for use exclusively with SPI booting.
		display_alert "Signing u-boot for SPI booting with old blobs" "${BOARD}" "info"
		uboot_g12_postprocess "${EXTER}"/cache/sources/amlogic-boot-fip-spi/${BOARD} g12a

		# Once done, store the resulting u-boot.bin as u-boot-spi.bin referenced in the UBOOT_TARGET_MAP above.
		cp -v u-boot.bin u-boot-spi.bin

		# Restore the original, so we can do everything again.
		cp -v u-boot.bin.orig u-boot.bin

		# Do the normal signing, using the regular/updated FIP blobs. This is for eMMC booting.
		display_alert "Signing u-boot for eMMC/SD booting with new blobs" "${BOARD}" "info"
		uboot_g12_postprocess "${EXTER}"/cache/sources/amlogic-boot-fip/${BOARD} g12a

		display_alert "Done postprocessing u-boot with new/old FIP blobs" "${BOARD}" "info"
	fi

	#if [[ ${BOARD} == orangepi3plus && ${BRANCH} == current ]]; then

	#	UBOOT_SRC_FOLDER="${BOOTSOURCEDIR}"

	#	source fip/variables.sh "${BOOTSOURCEDIR}"
	#	source fip/lib.sh
	#	source fip/build_bl2.sh
	#	source fip/build_bl30.sh
	#	source fip/build_bl31.sh
	#	source fip/build_bl32.sh
	#	source fip/build_bl33.sh
	#	source fip/build_bl40.sh

	#	# Cleanup old binaries
	#	rm -rf ${FIP_BUILD_FOLDER}

	#	# source uboot pre-build configs
	#	source ${SOURCE_FILE}

	#	CUR_SOC=${CONFIG_SYS_SOC}

	#	if [ "y" == "${CONFIG_NEED_BL32}" ]; then
	#		BLX_NEEDFUL[3]="true"
	#	fi

	#	export_variables

	#	source ${FIP_FOLDER}${CUR_SOC}/variable_soc.sh

	#	# compile fip tools for ddr_parse and map_tool
	#	prepare_tools > /dev/null 2>&1 || true

	#	# source soc package script
	#	source ${FIP_FOLDER}${CUR_SOC}/build.sh

	#	# source configs
	#	source ${CONFIG_FILE}
	#	if [ "y" == "${CONFIG_SUPPORT_CUSOTMER_BOARD}" ]; then
	#		BOARD_DIR="customer/board/${CONFIG_SYS_BOARD}"
	#	else
	#		BOARD_DIR="${CONFIG_BOARDDIR}"
	#	fi

	#	mkdir -p ${FIP_BUILD_FOLDER}

	#	cp ${FIP_FOLDER}${CUR_SOC}/bl2.bin $FIP_BUILD_FOLDER
	#	cp ${FIP_FOLDER}${CUR_SOC}/bl30.bin $FIP_BUILD_FOLDER
	#	cp ${FIP_FOLDER}${CUR_SOC}/bl31.bin $FIP_BUILD_FOLDER
	#	cp ${FIP_FOLDER}${CUR_SOC}/bl31.img $FIP_BUILD_FOLDER
	#	#cp ${FIP_FOLDER}${CUR_SOC}/bl32.bin $FIP_BUILD_FOLDER

	#	# cp bl33(uboot)
	#	copy_bl33

	#	# cp other firmwares(soc related)
	#	copy_other_soc

	#	# package final bootloader
	#	package

	#	if [ "y" != "${CONFIG_AML_SIGNED_UBOOT}" ]; then
	#		# copy bootloader to main folder
	#		mkdir -p ${BUILD_FOLDER}
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin ${BUILD_FOLDER}u-boot.bin
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin.encrypt ${BUILD_FOLDER}u-boot.bin.encrypt
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin.encrypt.efuse ${BUILD_FOLDER}u-boot.bin.encrypt.efuse
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin.encrypt.sd.bin ${BUILD_FOLDER}u-boot.bin.encrypt.sd.bin
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin.encrypt.usb.bl2 ${BUILD_FOLDER}u-boot.bin.encrypt.usb.bl2
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin.encrypt.usb.tpl ${BUILD_FOLDER}u-boot.bin.encrypt.usb.tpl
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin.sd.bin ${BUILD_FOLDER}u-boot.bin.sd.bin
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin.usb.bl2 ${BUILD_FOLDER}u-boot.bin.usb.bl2
	#		cp ${FIP_BUILD_FOLDER}u-boot.bin.usb.tpl ${BUILD_FOLDER}u-boot.bin.usb.tpl

	#		if [ "y" == "${CONFIG_AML_CRYPTO_IMG}" ]; then
	#			cp ${FIP_BUILD_FOLDER}boot.img.encrypt ${BUILD_FOLDER}boot.img.encrypt
	#		fi
	#	fi
	#fi
}
