#!/bin/bash
#
# Script to build the following firmwares:
# - icss dualemac
# - icss switch
#
# Toolchain:
# -----------
# Download latest PRU toolchain from:
# latest toolchain as of July 2020 is 2_3_2:
#
# PDK for dual_emac and switch firmware:
# --------------------------------------
# Branch: legacy_master
#

# Change below 3 paths accrodingly
export CL_PRU_INSTALL_PATH=$HOME/ti/ti-cgt-pru_2.3.2
PDK=$HOME/ti/pdk

VALID_FIRMWARES="emac switch"
outdir='ti-pruss'

# Firmware source path within the repositories
declare -A firmware_source
firmware_source["emac"]="$PDK/packages/ti/drv/icss_emac/firmware/icss_dualemac"
firmware_source["switch"]="$PDK/packages/ti/drv/icss_emac/firmware/icss_switch"

# Firmware output paths in firmware repos
declare -A firmware_op
firmware_op["emac"]='elf/REV'
firmware_op["switch"]='elf/REV'

# Firmware prefixes that are generated by firmware repos
declare -A firmware_prefix
firmware_prefix["emac"]='icss_dualemac_PRU'
firmware_prefix["switch"]='icss_stp_switch_PRU'

# SoCs corresponding to Firmware revisions
declare -A firmware_rev
firmware_rev[1]='am437x'

# Firmware postfixes that are expected by Linux
declare -A output_postfix
output_postfix["emac"]='-prueth-fw.elf'
output_postfix["switch"]='-prusw-fw.elf'

declare -A options_help
usage () {
	if [ -n "$*" ]; then
		echo "ERROR: $*"
	fi
	echo -n "Usage: $0 "
	for option in "${!options_help[@]}"
	do
		arg=`echo ${options_help[$option]}|cut -d ':' -f1`
		if [ -n "$arg" ]; then
			arg=" $arg"
		fi
		echo -n "[-$option$arg] "
	done
	echo
	echo -e "\nWhere:"
	for option in "${!options_help[@]}"
	do
		arg=`echo ${options_help[$option]}|cut -d ':' -f1`
		txt=`echo ${options_help[$option]}|cut -d ':' -f2`
		tb="\t\t\t"
		if [ -n "$arg" ]; then
			arg=" $arg"
			tb="\t"
		fi
		echo -e "   -$option$arg:$tb$txt"
	done
	echo
	echo "Examples of usage:-"
	echo "# Build dual emac firmware"
	echo "    $0 -f emac"
	echo "# Build switch firmware"
	echo "    $0 -f switch"
}

options_help[f]="firmware:Firmware to be built. Available firmware types are '$VALID_FIRMWARES'"
options_help[o]="output_directory:Output directory where firmware images are copied. Default dir is '$outdir'"

while getopts "f:o:h" opt
do
	case $opt in
	f)
		firmware=$OPTARG
	;;
	o)
		outdir=$OPTARG
	;;
	h)
		usage
		exit 0
	;;
	\?)
		usage "Invalid option '-$OPTARG'"
		exit 1
	;;
	:)
		usage "Option '-$OPTARG' Needs an argument."
		exit 1
	;;
	esac
done

# Valid firmware?
valid_firmware=0
for f in $VALID_FIRMWARES
do
	if [ "$f" == "$firmware" ]; then
		valid_firmware=1
	fi
done
if [ $valid_firmware == 0 ]; then
	usage "Invalid firmware name $firmware"
	exit 1
fi



# Build firmware.
( cd ${firmware_source["$firmware"]} ; gmake clean ; gmake -j32 )
# ToDo: Check if build is successful.
mkdir -p $outdir

echo
echo "-----------------------------------------"
echo " $firmware Firmware is built successfully"
echo "-----------------------------------------"
echo " Copying Firmware images to $outdir"
echo "-----------------------------------------"

# For each firmware revision
for v in ${!firmware_rev[@]}
do
	# for each SoC
	for soc in ${firmware_rev[$v]}
	do
		# for each PRU instance
		for i in 0 1
		do
			cp -vf ${firmware_source[$firmware]}/${firmware_op[$firmware]}$v/${firmware_prefix[$firmware]}$i.elf $outdir/$soc-pru$i${output_postfix[$firmware]}
		done
	done
done

