#
# 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/
#
BOOTSCRIPT='boot-mt7623.cmd:boot.cmd'
BOOTENV_FILE='mt7623.txt'
UBOOT_TARGET_MAP=";;$SRC/packages/blobs/mt7623n/BPI-R2-HEAD440-0k.img $SRC/packages/blobs/mt7623n/BPI-R2-HEAD1-512b.img $SRC/packages/blobs/mt7623n/BPI-R2-preloader-2k.img $SRC/packages/blobs/mt7623n/BPI-R2-EMMC-boot0-0K-0905.img u-boot.bin"
BOOTPATCHDIR='legacy'
BOOTBRANCH='tag:v2023.04'
ARCH=armhf
ATF_COMPILE="no"
EXTRAWIFI="no"

case $BRANCH in

	legacy)

		declare -g KERNEL_MAJOR_MINOR="4.19" # Major and minor versions of this kernel.
		KERNELBRANCH='branch:linux-4.19.y'

		;;

esac

CPUMIN=98000
CPUMAX=1300000
GOVERNOR="ondemand"

NM_IGNORE_DEVICES="interface-name:eth*,interface-name:wan*,interface-name:lan*,interface-name:br*"

write_uboot_platform() {
	if [[ $2 == /dev/mmcblk* && -b ${2}boot0 ]]; then
		local device=${2}boot0
		echo 0 > /sys/block/$(basename $device)/force_ro
		local is_emmc=1
	else
		local device=${2}
	fi

	if [[ $is_emmc == 1 ]]; then
		dd if=$1/BPI-R2-EMMC-boot0-0K-0905.img of=$2boot0 bs=1024 seek=0 status=noxfer > /dev/null 2>&1
		dd if=$1/u-boot.bin of=$2 bs=1k seek=320 status=noxfer > /dev/null 2>&1
		mmc bootpart enable 1 1 $2

	else
		dd if=$SRC/packages/blobs/mt7623n/BPI-R2-HEAD440-0k.img of=$2 bs=440 seek=0 count=1 status=noxfer > /dev/null 2>&1
		dd if=$SRC/packages/blobs/mt7623n/BPI-R2-HEAD1-512b.img of=$2 bs=512 seek=1 status=noxfer > /dev/null 2>&1
		dd if=$SRC/packages/blobs/mt7623n/BPI-R2-preloader-2k.img of=$2 bs=1k seek=2 status=noxfer > /dev/null 2>&1
		dd if=$1/u-boot.bin of=$2 bs=1k seek=320 status=noxfer > /dev/null 2>&1
	fi
}

family_tweaks() {
	[[ -f $SDCARD/etc/netplan/armbian-default.yaml ]] && sed -i "s/^  renderer.*/  renderer: networkd/" $SDCARD/etc/netplan/armbian-default.yaml
	cp $SRC/packages/bsp/mt7623/mt7623-wifi.service $SDCARD/lib/systemd/system/
	install -m 755 $SRC/packages/bsp/mt7623/mt7623-wifi.bash $SDCARD/usr/local/bin/mt7623-wifi.bash
	# very unstable wifi driver, disabled by default http://www.fw-web.de/dokuwiki/doku.php?id=en:bpi-r2:wlan#internal
	# chroot $SDCARD /bin/bash -c "systemctl --no-reload enable mt7623-wifi.service >/dev/null 2>&1"

	cp $SRC/packages/bsp/mt7623/10* $SDCARD/etc/systemd/network/
	cp $SRC/packages/blobs/mt7623n/wireless/{stp_uart_launcher,wmt_loader,wmt_loopback} $SDCARD/usr/local/bin
}
