BOOTSCRIPT="boot-rk322x.cmd:boot.cmd"
BOOTENV_FILE='rk322x.txt'
OVERLAY_PREFIX='rk322x'
UBOOT_TARGET_MAP="all u-boot.itb;;u-boot-rk322x-with-spl.bin"
BOOTDELAY=0
BOOTBRANCH='tag:v2021.07'
ARCH=armhf
SERIALCON=ttyS2

case $BRANCH in

	legacy)

		KERNELSOURCE='https://github.com/armbian/linux'
		KERNELBRANCH='branch:stable-4.4-rk3288-linux-v2.x'
		KERNELDIR='linux-rockchip'

	;;

	current)

		KERNELBRANCH='branch:linux-5.15.y'

	;;

	edge)
		
		KERNELBRANCH='branch:linux-5.16.y'

	;;

esac

CPUMIN="600000"
CPUMAX="1500000"
GOVERNOR="ondemand"

write_uboot_platform()
{

	# Extract the Command Rate bit from existing loader.
	# Some DDR memories have issues with different Command Rate, so we
	# restore the same value after installing the new loader.
	# Note: this has to be done only on live installations (DIR variable
	# is present only during boot loader upgrade on running system)
	# while loop is only for convenient break syntax
	while [[ -n $DIR ]]; do

		# Find the signature "a7866565" in the first 128k of the stored running image
		SIGNATURE_OFFSET=$(dd if=$2 bs=128k count=1 2>/dev/null | od -A o -w4 -tx4 | grep 'a7866565' | cut -d " " -f 1)

		# Some command failed, skip the rest
		[[ $? -ne 0 ]] && break;

		# No signature found, skip the rest
		[[ -z $SIGNATURE_OFFSET ]] && break;

		# Command rate bit is 16 bytes before signature
		CMD_RATE_OFFSET=$(($SIGNATURE_OFFSET - 16))
		CR_BYTE=$(od -A n -t dI -j $CMD_RATE_OFFSET -N 1 $2)

		# No command rate byte for some reason, skip the rest
		[[ -z $CR_BYTE ]] && break;

		# Invalid command rate byte (should be 0 or 1), skip the rest
		[[ "$CR_BYTE" -ne 0 && "$CR_BYTE" -ne 1 ]] && break;

		# Proceed patching u-boot-rk322x-with-spl.bin, do find the 
		# cr bit there too to verify that the position of the CR bit is right
		SIGNATURE_OFFSET=$(dd if=$1/u-boot-rk322x-with-spl.bin bs=128k count=1 2>/dev/null | od -A o -w4 -tx4 | grep 'a7866565' | cut -d " " -f 1)

		# Some command failed, skip the rest
		[[ $? -ne 0 ]] && break;

		# No signature found, skip the rest
		[[ -z $SIGNATURE_OFFSET ]] && break

		# Command rate bit is 16 bytes before signature
                CMD_RATE_OFFSET=$(($SIGNATURE_OFFSET - 16))
       	        DST_BYTE=$(od -A n -t dI -j $CMD_RATE_OFFSET -N 1 $1/u-boot-rk322x-with-spl.bin)

		# Verify command rate byte is 0 or 1
		[[ "$DST_BYTE" -ne 0 && "$DST_BYTE" -ne 1 ]] && break;

		# Patch the file
		[[ "$CR_BYTE" -eq 0 ]] && HEX_CR="\x00"
		[[ "$CR_BYTE" -eq 1 ]] && HEX_CR="\x01"

		echo -e $HEX_CR | dd of=$1/u-boot-rk322x-with-spl.bin bs=1 seek=$CMD_RATE_OFFSET count=1 conv=notrunc >/dev/null 2>&1

		# always break the while loop
		break

	done

	dd if=/dev/zero of=$2 bs=1k count=1023 seek=1 status=noxfer > /dev/null 2>&1
	dd if=$1/u-boot-rk322x-with-spl.bin of=$2 bs=32k seek=1 conv=notrunc > /dev/null 2>&1

}

uboot_custom_postprocess()
{

	# We use the rockchip proprietary blob to initialize memory chips
	# instead of letting u-boot doing the job. Such devices, like xt-mx4vr-v01, have DDR2
	# or LPDDR2 memories and the blob is better suited for the job.
	# Also it may come handy when there are different memory flavours among boards, with
	# different timings, specs and frequencies.
	# The rockchip proprietary blob is a drop-in substitute for the u-boot tpl.
	# In case you want to use the u-boot TPL remember to set the proper timing
	# registers in device tree and this command line instead of the other one:
	#
	# 	tools/mkimage -n rk322x -T rksd -d tpl/u-boot-tpl.bin u-boot-rk322x-with-spl.bin
	#
	
	tools/mkimage -n rk322x -T rksd -d $SRC/packages/blobs/rockchip/rk322x_ddr_330MHz_v1.10_2t.bin u-boot-rk322x-with-spl.bin
	cat spl/u-boot-spl.bin >> u-boot-rk322x-with-spl.bin
	dd if=u-boot.itb of=u-boot-rk322x-with-spl.bin seek=$((0x200 - 0x40)) conv=notrunc

}

family_tweaks()
{

	true

}

family_tweaks_bsp()
{
	#Graphics and media
	mkdir -p $destination/etc/udev/rules.d
	mkdir -p $destination/usr/local/bin
	cp $SRC/packages/bsp/rockchip/hdmi.rules $destination/etc/udev/rules.d
	cp $SRC/packages/bsp/rockchip/50-hevc.rules $destination/etc/udev/rules.d
	cp $SRC/packages/bsp/rockchip/50-mali.rules $destination/etc/udev/rules.d
	cp $SRC/packages/bsp/rockchip/50-vpu.rules $destination/etc/udev/rules.d
	cp $SRC/packages/bsp/rk322x/50-rkvdec.rules $destination/etc/udev/rules.d
	cp $SRC/packages/bsp/rockchip/60-media.rules $destination/etc/udev/rules.d
	install -m 755 $SRC/packages/bsp/rockchip/hdmi-hotplug $destination/usr/local/bin

	# Peripheral access for specific groups
	addgroup --system --quiet --gid 997 gpio
	addgroup --system --quiet --gid 998 i2c
	cp $SRC/packages/bsp/rockchip/70-gpio.rules $destination/etc/udev/rules.d
	cp $SRC/packages/bsp/rockchip/71-i2c.rules $destination/etc/udev/rules.d

	# Bluetooth
	install -m 755 $SRC/packages/bsp/rk322x/rtk_hciattach $destination/usr/bin
	cp $SRC/packages/bsp/rk322x/rtk-bluetooth.service $destination/lib/systemd/system/

	# Sound
	cp $SRC/packages/bsp/rockchip/asound.conf $destination/etc/
	cp $SRC/packages/bsp/rockchip/89-pulseaudio-usb.rules $destination/etc/udev/rules.d

	# esp8089 wifi driver modprobe default configuration options
	mkdir -p $destination/etc/modprobe.d
	cp $SRC/packages/bsp/rk322x/esp8089.conf $destination/etc/modprobe.d/esp8089.conf

	# Board selection script
	install -m 755 $SRC/packages/bsp/rk322x/rk322x-config $destination/usr/sbin

	# Fix for bad device detection for x.org when lima driver is in use
	if [[ "$BRANCH" != "legacy" ]]; then
		mkdir -p $destination/etc/X11/xorg.conf.d
		cp $SRC/packages/bsp/rk322x/40-serverflags.conf $destination/etc/X11/xorg.conf.d
	fi

}
