#!/usr/bin/env zsh
# Copyright (c) 2016-2021 Ivan J. <parazyd@dyne.org>
# This file is part of arm-sdk
#
# This source code is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this source code. If not, see <http://www.gnu.org/licenses/>.


## settings & config
vars+=(device_name arch size parted_type parted_boot parted_root bootfs inittab)
vars+=(gitkernel gitbranch)
arrs+=(custmodules)

device_name="rockpro64"
arch="arm64"
size=1891
inittab=("T1:12345:respawn:/sbin/agetty -L ttyS2 1500000 vt100")

parted_type="dos"
bootfs="ext2"
rootfs="ext4"
parted_boot="$bootfs 2048s 264191s"
parted_root="$rootfs 264192s 100%"

extra_packages+=()
custmodules=()

gitkernel="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git"
gitbranch="5.17.3"

atfgit="https://github.com/ARM-software/arm-trusted-firmware.git"
ubootgit="https://github.com/u-boot/u-boot"

prebuild() {
	fn prebuild
	req=(device_name)
	ckreq || return 1

	notice "executing $device_name prebuild"

	mkdir -p $R/tmp/kernels/$device_name
}

postbuild() {
	fn postbuild
	req=(device_name compiler loopdevice)
	ckreq || return 1

	notice "executing $device_name postbuild"

	copy-root-overlay

	notice "building arm-trusted-firmware"
	git clone --depth 1 "$atfgit" "$R/tmp/kernels/arm-trusted-firmware" || zerr
	pushd "$R/tmp/kernels/arm-trusted-firmware"
		make CROSS_COMPILE=$compiler PLAT=rk3399 DEBUG=1 bl31 || zerr
	popd

	notice "building u-boot"
	git clone --depth 1 "$ubootgit" "$R/tmp/kernels/u-boot-rockpro64" || zerr
	pushd "$R/tmp/kernels/u-boot-rockpro64"
		make $MAKEOPTS rockpro64-rk3399_defconfig
		cp "$R/tmp/kernels/arm-trusted-firmware/build/rk3399/debug/bl31/bl31.elf" ./bl31.bin
		make $MAKEOPTS ARCH=arm CROSS_COMPILE=$compiler || zerr
		mkdir -p "$R/dist"
		cat spl/u-boot-spl.bin u-boot.itb > "$R/dist/u-boot-rk3399-with-spl-rockpro64.bin"
	popd

	cat <<EOF | sudo tee "${strapdir}/boot/boot.txt"
setenv bootargs console=tty0 console=\${console} root=/dev/mmcblk0p2 rw rootwait rootfstype=ext4 fbcon=rotate:1
setenv kernel_addr_z 0x44080000

if load \${devtype} \${devnum}:\${distro_bootpart} \${kernel_addr_z} Image.gz; then
  unzip \${kernel_addr_z} \${kernel_addr_r}
  if load \${devtype} \${devnum}:\${distro_bootpart} \${fdt_addr_r} rk3399-rockpro64.dtb; then
    booti \${kernel_addr_r} - \${fdt_addr_r};
  fi;
fi
EOF
	pushd "${strapdir}/boot"
		sudo mkimage -C none -A arm -T script -d boot.txt boot.scr
	popd

	sudo dd if="$R/dist/u-boot-rk3399-with-spl-rockpro64.bin" of="${loopdevice}" seek=8 \
		bs=1024 conv=notrunc,nocreat

	postbuild-clean
}

build_kernel_arm64() {
	fn build_kernel_arm64
	req=(R arch device_name gitkernel gitbranch MAKEOPTS)
	req+=(strapdir)
	ckreq || return 1

	notice "building $arch kernel"

	prebuild || zerr

	get-kernel-sources
	pushd $R/tmp/kernels/$device_name/${device_name}-linux
		## Apply necessary kernel patches here.
		# wget  -O <patch-url> | patch -p1

		copy-kernel-config

		# compile kernel and modules
		make \
			$MAKEOPTS \
			ARCH=arm64 \
			CROSS_COMPILE=$compiler \
			Image.gz modules rockchip/rk3399-rockpro64.dtb || zerr

		# install kernel modules
		sudo -E PATH="$PATH" \
			make \
				$MAKEOPTS \
				ARCH=arm \
				CROSS_COMPILE=$compiler \
				INSTALL_MOD_PATH=$strapdir \
					modules_install || zerr

		sudo cp -v arch/arm64/boot/Image $strapdir/boot/ || zerr
		sudo cp -v arch/arm64/boot/dts/rockchip/rk3399-rockpro64.dtb \
			"$strapdir/boot/" || zerr
	popd

	postbuild || zerr
}
