#
# 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/sunxi64_common.inc"

[[ -z $CPUMIN ]] && CPUMIN=480000
[[ -z $CPUMAX ]] && CPUMAX=1512000
GOVERNOR=ondemand

case $BRANCH in

	legacy)

		# Legacy kernel
		LINUXFAMILY=sun50iw9
		KERNELSOURCE='https://github.com/orangepi-xunlong/linux-orangepi.git'
		declare -g KERNEL_MAJOR_MINOR="4.9" # Major and minor versions of this kernel.
		KERNELBRANCH="branch:orange-pi-4.9-sun50iw9"
		KERNELPATCHDIR=${BOARDFAMILY}-${BRANCH}
		INITRD_ARCH=arm
		ASOUND_STATE='asound.state.sun50iw9-legacy'

		# This (legacy/vendor) kernel requires uImage manual conversion; do it via a 'pre_package_kernel_image' hook.
		declare -g NAME_KERNEL="uImage" # Even though that's a lie.
		function pre_package_kernel_image__orangepi_legacy_uImage_manual_conversion() {
			display_alert "Converting" "${BOARDFAMILY} - ${LINUXFAMILY} :: legacy :: uImage" "info"
			if [[ -z "${kernel_image_pre_package_path}" || ! -f "${kernel_image_pre_package_path}" ]]; then
				exit_with_error "kernel_image_pre_package_path ('${kernel_image_pre_package_path}') is not set or does not exist"
			fi
			run_host_command_logged mkimage -A arm -O linux -T kernel -C none -a "'0x40008000'" -e "'0x40008000'" -n "'Linux kernel'" -d "${kernel_image_pre_package_path}" "${kernel_image_pre_package_path}.uImage.tmp"
			run_host_command_logged mv -v "${kernel_image_pre_package_path}.uImage.tmp" "${kernel_image_pre_package_path}"
		}

		# Legacy u-boot
		BOOTSOURCE='https://github.com/orangepi-xunlong/u-boot-orangepi.git'
		BOOTBRANCH='branch:v2018.05-sun50iw9'
		BOOTPATCHDIR="legacy"
		UBOOT_TARGET_MAP=";;dts/${BOARD}-u-boot.dts boot0_sdcard.fex boot_package.fex u-boot.bin:u-boot.fex"
		UBOOT_COMPILER="arm-linux-gnueabi-"
		UBOOT_USE_GCC='> 6.0'
		BOOTENV_FILE=sun50iw9.txt
		BOOTSCRIPT='boot-sun50iw9.cmd:boot.cmd'
		PACK_UBOOT="yes"
		OFFSET=20
		ATFSOURCE=""
		ATF_COMPILE="no"

		# this overrides the one in sunxi64_common.inc
		function write_uboot_platform() {
			dd if=$1/boot0_sdcard.fex of=$2 bs=8k seek=1 conv=fsync
			dd if=$1/boot_package.fex of=$2 bs=8k seek=2050 conv=fsync
		}

		;;

	current | edge)
		ATFSOURCE='https://github.com/ARM-software/arm-trusted-firmware'
		ATFBRANCH='tag:lts-v2.10.2'
		ATF_PLAT="sun50i_h616"
		ATF_TARGET_MAP='PLAT=sun50i_h616 DEBUG=1 bl31;;build/sun50i_h616/debug/bl31.bin'
		BOOTSCRIPT='boot-sun50i-next.cmd:boot.cmd'

		;;
esac

# This build requires busybox (and dos2unix)
function add_host_dependencies__sun50iw9_add_busybox_hostdep() {
	display_alert "Adding busybox dep" "for ${BOARD} bootloader compile" "debug"
	declare -g EXTRA_BUILD_DEPS="${EXTRA_BUILD_DEPS} busybox"
}

function family_tweaks_s() {
	if [[ -f $SDCARD/lib/systemd/system/aw859a-bluetooth.service ]]; then
		# Enable Bluetooth
		chroot $SDCARD /bin/bash -c "systemctl --no-reload enable aw859a-bluetooth.service >/dev/null 2>&1"
	fi

	if [[ -f $SDCARD/lib/systemd/system/hdmi-audio.service ]]; then
		chroot $SDCARD /bin/bash -c "systemctl --no-reload enable hdmi-audio.service >/dev/null 2>&1"
	fi
}

function family_tweaks_bsp() {
	if [[ ${BOARD} == orangepizero2 && ${BRANCH} == legacy ]]; then
		install -m 755 $SRC/packages/blobs/bt/hciattach/hciattach_opi_$ARCH $destination/usr/bin/hciattach_opi
		cp $SRC/packages/bsp/sunxi/aw859a-bluetooth.service $destination/lib/systemd/system/

		install -m 755 $SRC/packages/blobs/sunxi/h616/inithdmiaudio $destination/usr/bin/
		cp $SRC/packages/blobs/sunxi/h616/libtinyalsa.so $destination/usr/lib/
		cp $SRC/packages/bsp/sunxi/hdmi-audio.service $destination/lib/systemd/system/

	fi
}

function uboot_custom_postprocess() {
	if [[ ${BRANCH} == legacy ]]; then
		display_alert "Post-processing U-Boot" "$BOARD - $BRANCH" "info"
		# "Vendor" code (by Igor) is at https://raw.githubusercontent.com/orangepi-xunlong/orangepi-build/main/external/config/sources/families/sun50iw9.conf

		run_host_command_logged rm -rfv dts/*.dts sys_config ${BOARD}-u-boot.dtb

		run_host_command_logged cp -rpv ${SRC}/packages/pack-uboot/${BOARDFAMILY}/bin/* .

		run_host_command_logged cp -pv sys_config/sys_config_${BOARD}.fex sys_config.fex
		run_host_command_logged cp -pv u-boot.bin u-boot.fex

		# [[ ${BOARD} =~ orangepizero2-b|orangepizero2-lts ]] && run_host_command_logged mv -v boot0_sdcard_new.fex boot0_sdcard.fex # From vendor code, they have multiple of those boards

		# make u-boot dtb
		run_host_command_logged dtc -p 2048 -W no-unit_address_vs_reg -@ -O dtb -o ${BOARD}-u-boot.dtb -b 0 dts/${BOARD}-u-boot.dts
		[[ ! -f "${BOARD}-u-boot.dtb" ]] && exit_with_error "dts compilation failed for ${BOARD}-u-boot.dtb"

		run_host_command_logged busybox unix2dos sys_config.fex

		run_host_command_logged $SRC/packages/pack-uboot/${BOARDFAMILY}/tools/script sys_config.fex
		run_host_command_logged cp -pv ${BOARD}-u-boot.dtb sunxi.fex

		run_host_command_logged $SRC/packages/pack-uboot/${BOARDFAMILY}/tools/update_dtb sunxi.fex 4096
		run_host_command_logged $SRC/packages/pack-uboot/${BOARDFAMILY}/tools/update_boot0 boot0_sdcard.fex sys_config.bin SDMMC_CARD

		# rpardini: this looks wrong, but isn't. look at vendor code.
		run_host_command_logged $SRC/packages/pack-uboot/${BOARDFAMILY}/tools/update_uboot -no_merge u-boot.fex sys_config.bin
		run_host_command_logged $SRC/packages/pack-uboot/${BOARDFAMILY}/tools/update_uboot -no_merge u-boot.bin sys_config.bin

		#pack boot package
		run_host_command_logged busybox unix2dos boot_package.cfg
		run_host_command_logged $SRC/packages/pack-uboot/${BOARDFAMILY}/tools/dragonsecboot -pack boot_package.cfg

		# From vendor code, we never had this.
		#if [[ ${MERGE_UBOOT} == yes ]]; then
		#	display_alert "Merge u-boot" "u-boot-${BOARD}-merged.bin" "info"
		#	dd if=/dev/zero of=u-boot-${BOARD}-merged.bin bs=1M count=20 > /dev/null 2>&1
		#	dd if=boot0_sdcard.fex of=u-boot-${BOARD}-merged.bin bs=8k seek=1 conv=fsync > /dev/null 2>&1
		#	dd if=boot_package.fex of=u-boot-${BOARD}-merged.bin bs=8k seek=2050 conv=fsync > /dev/null 2>&1
		#	mv u-boot-${BOARD}-merged.bin ${DEB_STORAGE}/u-boot/
		#fi
	fi

	return 0
}
