#!/bin/bash
# Build ARM Trusted Firmware for Slackware ARM and AArch64
# Created By: Brenton Earl <brent@exitstatusone.com>
# Date: 3/21/2021
# Built and tested on Slackware64-current
# Modifed by MoZes@slackware for use within the Slackware ARM build environment.
# Reference: https://src.fedoraproject.org/rpms/arm-trusted-firmware/blob/f34/f/arm-trusted-firmware.spec
#########################################################################################################
# Install a cross compiler for arm and aarch64 (x-toolchain)
# cross compiler: ftp://ftp.arm.slackware.com/slackwarearm/slackwarearm-devtools/x-toolchain/
#########################################################################################################
# Note: This can only be run on the x86 using the x-toolchain cross compiler that's used
#       to build SlackwareARM/AArch64.  This is because the Secure Monitor (BL31 ATF stage) requires
#       32-bit mode, thus cross toolchain. We don't have such a toolchain included within Slackware
#       AArch64, so we'll just use the x-toolchain and build this on an x86_64.
#       This works for us because building ATF is outside of the regular Slackware ARM build process,
#       and is launched manually.
#########################################################################################################

CWD=$PWD
TMP=/tmp/build-arm-trusted-firmware
BINS=$PWD/../bin/

rm -rf $TMP
mkdir -p $TMP

export NUMJOBS=-j$( nproc )

# Set path to include your cross compiler, aarch64 first, then arm
export PATH="/devel/build-servers/x86_64/aarch64/bin:/devel/build-servers/x86_64/arm/bin:$PATH"
# The 'SOCCHIPSET' is the chipset name, where as 'SOCS' may be the chipset
# name and/or the marketing name.
# The ARM Trusted Firmware uses the marketing names.
# For consistency with the Kernel module loader, we'll be naming our binaries
# using the chipset name.
SOCS[0]="rk3399"
SOCCHIPSET[0]="rk3399"
# The RPI4 doesn't need this:
#SOCS[1]="rpi3"
#SOCCHIPSET[1]="bcm2837"
#SOCS[2]="rpi4"
#SOCCHIPSET[2]="bcm2711"

# Extract source:
cd $TMP
tar xf $CWD/sources/arm-trusted-firmware-*.tar.xz
cd trusted-firmware*/ || exit 1

for (( i=0 ; i <= $((${#SOCS[@]}-1)) ; i++ )); do
   echo "Building ATF for: ${SOCS[$i]} ( ${SOCCHIPSET[$i]} )"
   rm -rf $BINS/${SOCCHIPSET[$i]}
   mkdir -p $BINS/${SOCCHIPSET[$i]}
   make realclean
   # This needs a 32bit toolchain because ATF's initial stages are run in 32-bit mode.
   # Patch the toolchain to use the Slackware name:
   grep -Flr -- 'arm-none-eabi-' .    | xargs sed -i 's?arm-none-eabi-?arm-slackware-linux-gnueabihf-?g'
   grep -Flr -- 'aarch64-none-elf-' . | xargs sed -i 's?aarch64-none-elf-?aarch64-slackware-linux-gnu-?g'

   # Older builds require this, new ATF versions don't - so comment it if it won't build:
   LDFLAGS="--no-warn-rwx-segment" \
   V=1 DEBUG=1 \
   CROSS_COMPILE="aarch64-slackware-linux-gnu-" \
   make $NUMJOBS PLAT=${SOCS[$i]} bl31 || exit 1
   # Copy licences and documentation:
   install -vpm644 readme.rst docs/license.rst $BINS/${SOCCHIPSET[$i]}

   # Rockchips wants the bl31.elf, plus rk3399 wants power management co-processor bits
   echo "*** There may be install errors here due to missing files - it's probably ok ;-) "
   for file in bl31.bin bl31.elf rk3399m0.bin rk3399m0.elf ; do
      # Store it within the bins directory for this platform:
      install -vpm644 $( find . -name $file ) $BINS/${SOCCHIPSET[$i]}
   done
done
