###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# This program 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 program 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 program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

###############################################################################
# Definitions
###############################################################################

include Config

VER        = 2022.10

THISAPP    = u-boot-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(THISAPP)-$(MKIMAGE)
SUP_ARCH   = aarch64

CFLAGS    := $(patsubst -fstack-protector-strong,,$(CFLAGS))
LDFLAGS   += --no-warn-rwx-segments

ATF_VER    = 2.7

###############################################################################
# Top-level Rules
###############################################################################

objects = $(DL_FILE) arm-trusted-firmware-$(ATF_VER).tar.gz arm-trusted-firmware-$(ATF_VER)-rk3399-binary.tar.xz

$(DL_FILE) = $(DL_FROM)/$(DL_FILE)
arm-trusted-firmware-$(ATF_VER).tar.gz = $(DL_FROM)/arm-trusted-firmware-$(ATF_VER).tar.gz
arm-trusted-firmware-$(ATF_VER)-rk3399-binary.tar.xz = $(DL_FROM)/arm-trusted-firmware-$(ATF_VER)-rk3399-binary.tar.xz

$(DL_FILE)_BLAKE2 = 42aa7a6f131735888939982e579de4342e3909e064ab896b0df6f1ff56c20ed6cb11d25286da7c052a5f67dcef6fa7a746944d8df6dd504586f5a71502d157e1
arm-trusted-firmware-$(ATF_VER).tar.gz_BLAKE2 = 4fc4d5646e272200d40081902e17f0be32956f451622f011a0d568c8cf26e15ab165fe16a69cf222241f7ba1d443add562d6d382277eb4fb2b49c3918cabdbad
arm-trusted-firmware-$(ATF_VER)-rk3399-binary.tar.xz_BLAKE2 = 04424a7dcda0771f469c8e918a24aed75017c1a28c1b8c2c794e3ce31afbc01e7906ccab6faf1459d69a5ec1ef4fdde2bab1011b883980efeae7372013f2570e

install : $(TARGET)

check : $(patsubst %,$(DIR_CHK)/%,$(objects))

download :$(patsubst %,$(DIR_DL)/%,$(objects))

b2 : $(subst %,%_BLAKE2,$(objects))

###############################################################################
# Downloading, checking, b2sum
###############################################################################

$(patsubst %,$(DIR_CHK)/%,$(objects)) :
	@$(CHECK)

$(patsubst %,$(DIR_DL)/%,$(objects)) :
	@$(LOAD)

$(subst %,%_BLAKE2,$(objects)) :
	@$(B2SUM)

dist:
	@$(PAK)

###############################################################################
# Installation Details
###############################################################################

$(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar axf $(DIR_DL)/$(DL_FILE)

ifneq "$(MKIMAGE)" "1"
	# Raspberry Pi 3
	-mkdir -pv /usr/share/u-boot/rpi
	cd $(DIR_APP) && make CROSS_COMPILE="" rpi_3_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" RPi3 - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot.bin \
		/usr/share/u-boot/rpi/u-boot-rpi3.bin
	cd $(DIR_APP) && install u-boot.bin /boot/u-boot-rpi3.bin
	cd $(DIR_APP) && make distclean

	# Raspberry Pi 4
	-mkdir -pv /usr/share/u-boot/rpi
	cd $(DIR_APP) && make CROSS_COMPILE="" rpi_4_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" RPi4 - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot.bin \
		/usr/share/u-boot/rpi/u-boot-rpi4.bin
	cd $(DIR_APP) && install u-boot.bin /boot/u-boot-rpi4.bin
	cd $(DIR_APP) && make distclean

	# OrangePi Zero Plus
	cd $(DIR_APP) && rm -rf arm-trusted-firmware-$(ATF_VER)
	cd $(DIR_APP) && tar axf $(DIR_DL)/arm-trusted-firmware-$(ATF_VER).tar.gz
	cd $(DIR_APP)/arm-trusted-firmware-$(ATF_VER) && make PLAT=sun50i_a64 DEBUG=0 bl31 LDFLAGS="$(LDFLAGS)"
	cd $(DIR_APP) && cp arm-trusted-firmware-$(ATF_VER)/build/sun50i_a64/release/bl31.bin bl31.bin
	cd $(DIR_APP) && rm -rf arm-trusted-firmware-$(ATF_VER)
	-mkdir -pv /usr/share/u-boot/orangepi_zero_plus
	cd $(DIR_APP) && make CROSS_COMPILE="" orangepi_zero_plus_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING="  OrangePi Zero+  - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot-sunxi-with-spl.bin \
		/usr/share/u-boot/orangepi_zero_plus
	cd $(DIR_APP) && make distclean
	cd $(DIR_APP) && rm -f bl31.bin

	# Nanopi R2S
	cd $(DIR_APP) && rm -rf arm-trusted-firmware-$(ATF_VER)
	cd $(DIR_APP) && tar axf $(DIR_DL)/arm-trusted-firmware-$(ATF_VER).tar.gz
	cd $(DIR_APP)/arm-trusted-firmware-$(ATF_VER) && make PLAT=rk3328 ARCH=aarch64 DEBUG=0 bl31 LDFLAGS="$(LDFLAGS)"
	cd $(DIR_APP) && cp arm-trusted-firmware-$(ATF_VER)/build/rk3328/release/bl31/bl31.elf bl31.elf
	cd $(DIR_APP) && rm -rf arm-trusted-firmware-$(ATF_VER)
	-mkdir -pv /usr/share/u-boot/nanopi_r2s
	cd $(DIR_APP) && make CROSS_COMPILE="" nanopi-r2s-rk3328_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" Nanopi R2S - IPFire.org"!' .config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_BOOTCOMMAND=.*!CONFIG_BOOTCOMMAND="console=ttyS2,115200n8;run distro_bootcmd"!' .config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_BAUDRATE=.*!CONFIG_BAUDRATE=115200!' .config
	cd $(DIR_APP) && sed -i -e 's!.*CONFIG_ENV_OVERWRITE.*!CONFIG_ENV_OVERWRITE=y!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot-rockchip.bin \
		/usr/share/u-boot/nanopi_r2s/u-boot-rockchip.bin
	cd $(DIR_APP) && make distclean

	# Nanopi R4S
	# arm trusted firmware for rk3399 cannot build without cortex m0 gcc crosscompiler
	# it is build on ubuntu with make PLAT=rk3399 ARCH=aarch64 DEBUG=0 bl31
	cd $(DIR_APP) && tar axf $(DIR_DL)/arm-trusted-firmware-$(ATF_VER)-rk3399-binary.tar.xz
	-mkdir -pv /usr/share/u-boot/nanopi_r4s
	cd $(DIR_APP) && make CROSS_COMPILE="" nanopi-r4s-rk3399_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" Nanopi R4S - IPFire.org"!' .config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_BOOTCOMMAND=.*!CONFIG_BOOTCOMMAND="console=ttyS2,115200n8;run distro_bootcmd"!' .config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_BAUDRATE=.*!CONFIG_BAUDRATE=115200!' .config
	cd $(DIR_APP) && sed -i -e 's!.*CONFIG_ENV_OVERWRITE.*!CONFIG_ENV_OVERWRITE=y!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot-rockchip.bin \
		/usr/share/u-boot/nanopi_r4s/u-boot-rockchip.bin
	cd $(DIR_APP) && make distclean

	# create an empty 128 KB File for the u-boot env
	dd if=/dev/zero of=/boot/uboot.env bs=1K count=128

	# config (uEnv.txt and boot.*)
	cp -vf $(DIR_SRC)/config/u-boot/* /boot/
	# create signed binary boot script (boot.scr)
	mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr

	# patch real Kernel version to uEnv.txt
	sed -e "s/xxxKVERxxx/$(KVER)/g" -i /boot/uEnv.txt

else
	# mkimage
	cd $(DIR_APP) && make CROSS_COMPILE="" rpi_3_config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)" tools
	cd $(DIR_APP) && install -v -m 755 tools/mkimage /usr/bin
endif

	@rm -rf $(DIR_APP)
	@$(POSTBUILD)
