#
# 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/rockchip64_common.inc"
#BOOTPATCHDIR="u-boot-rockchip64"
BOOTENV_FILE='rockpis.txt'
OVERLAY_PREFIX='rk3308'
BOOTSCRIPT=boot-rockpis.cmd:boot.cmd

#comment out line below for an image that will NOT boot from the built-in SDNAND
IDBLOADER_BLOB=$SRC/packages/blobs/rockchip/rk3308_idbloader_ddr589MHz_uart0_m0_v2.06.136sd.bin

#The SDNAND capabilty IDBLOADER_BLOB requires the U-Boot and Linux serial console on UART0
#Linux will hang on reboot if the console remains on UART2

#Note:  IDBLOADER_BLOB is derived from
#  https://dl.radxa.com/rockpis/images/loader/rk3308_loader_ddr589MHz_uart0_m0_v2.06.136sd.bin
#by using the rkdeveloptool to "upgrade" the previous DDR_BLOB loader on the SDNAND
#To recreate it, build the image with IDBLOADER_BLOB unset and boot Rock PI-S in MASKROM mode
#On your host (connected to the RockPi-S's USB-C port):
#  rdeveloptool db rk3308_loader_ddr589MHz_uart0_m0_v2.06.136sd.bin
#  rdeveloptool wl 0 newly_built_image.img
#  rdeveloptool ul rk3308_loader_ddr589MHz_uart0_m0_v2.06.136sd.bin  #this writes 280 sectors

#Then, reset the RockPi-S to boot from SDNAND.  Using that running image:
#  dd if=/dev/mmcblk0 of=rk3308_idbloader_ddr589MHz_uart0_m0_v2.06.136sd.bin skip=64 count=280

case $BRANCH in

	legacy)
		BOOTCONFIG="rockpi-s-rk3308_defconfig"
		BOOTSOURCE='https://github.com/piter75/rockchip-u-boot.git'
		BOOTBRANCH='branch:rockpis-next-dev'
		BOOTPATCHDIR="legacy"
		UBOOT_COMPILER="aarch64-linux-gnu-"
		BOOTSCRIPT='boot-rockpis.cmd:boot.cmd'
		UBOOT_USE_GCC='< 8.0'
		BOOTDIR='u-boot-rockchip64'
		KERNELSOURCE='https://github.com/piter75/rockchip-kernel'
		declare -g KERNEL_MAJOR_MINOR="4.4" # Major and minor versions of this kernel.
		KERNELBRANCH='branch:rockpis-develop-4.4'
		unset IDBLOADER_BLOB

		;;

esac

prepare_boot_configuration

uboot_custom_postprocess() {
	# TODO: remove this diversion from common caused by different loaderimage params
	loaderimage --pack --uboot ./u-boot-dtb.bin uboot.img 0x600000 --size 1024 1 &&
		if [ -r "$IDBLOADER_BLOB" ]; then
			echo "Installing $IDBLOADER_BLOB"
			echo "Capable of booting from built-in SDNAND"
			cp $IDBLOADER_BLOB idbloader.bin
		else
			[ "$IDBLOADER_BLOB" ] && echo "Missing $IDBLOADER_BLOB"
			echo "WARNING:  This image will not boot from built-in SDNAND"
			tools/mkimage -n rk3308 -T rksd -d $RKBIN_DIR/$DDR_BLOB idbloader.bin &&
				cat $RKBIN_DIR/$MINILOADER_BLOB >> idbloader.bin
		fi &&
		trust_merger --replace bl31.elf $RKBIN_DIR/$BL31_BLOB trust.ini
}

family_tweaks_bsp() { #Install udev script that derives fixed, unique MAC addresses for net interfaces
	#that are assigned random ones -- like RockPI-S's WiFi network interfaces
	bsp=$SRC/packages/bsp/rockpis
	rules=etc/udev/rules.d

	install -m 755 $bsp/lib/udev/fixEtherAddr $destination/lib/udev &&
		install -m 644 $bsp/$rules/05-fixMACaddress.rules $destination/$rules
}
