#! /bin/bash
mkdir -p ../../output

SW_DIR=../../sw

OUTPUT_DIR=../output

board=zynq_spec7
defconfig=${board}_defconfig


cd $SW_DIR

for dir in $(ls)
do
	if [ "$dir" == "fsbl" ];then
		make -C  $dir
	fi

	if [ "$dir" == "spec7-ps-sw" ];then
                export CROSS_COMPILE=arm-linux-gnueabihf-
		export ARCH=arm
	        make -C $dir/uboot $defconfig
		make -C $dir/uboot
		cp $dir/uboot/u-boot.elf  $OUTPUT_DIR
        fi

done


[[ "$#" -ne 6 ]] && { echo "Usage :./Build.sh --t <tandem_gateware> --r <reference_gateware> --o <offset>" ; exit 1; }
[[ "$1" = "--t" ]] &&  TANDEM_GATEWARE=$2
[[ "$3" = "--r" ]] &&  REFERENCE_GATEWARE=$4
[[ "$5" = "--o" ]] &&  OFFSET=$6

cd $OUTPUT_DIR

rm $(ls|grep .bif)

echo "the_ROM_image:" >> $OUTPUT_DIR/spec7.bif
echo "{" >> $OUTPUT_DIR/spec7.bif
echo "[bootloader] zynq_fsbl.elf" >> $OUTPUT_DIR/spec7.bif
echo "$TANDEM_GATEWARE" >> $OUTPUT_DIR/spec7.bif
echo "u-boot.elf" >> $OUTPUT_DIR/spec7.bif
if [ "$REFRENCE_GATEWARE" == "" ]; then
	echo "[offset = $OFFSET] $REFERENCE_GATEWARE" >> $OUTPUT_DIR/spec7.bif 
fi
echo "}" >> $OUTPUT_DIR/spec7.bif

bootgen -image spec7.bif -arch zynq -o BOOT.bin -w
