#!/bin/bash
#
# Simplify installation of new FPGA image by relieving the user of the
# necessity of knowing details such as the command line syntax of jtagpgm,
# the name and location of the image file, etc.
#
# In a live system the FPGA version appears in register 0. This script puts this
# version number into the variable FPGA_VERSION.
#
# There's no way to extract the version number from the new FPGA image,
# unfortunately. When the hardware team gives us a new file with a new version
# of the FPGA image, the first thing the software team needs to do is change
# this script's variable FILE_VERSION to match the value that the new image will
# show in register 0.
#
# At run-time, the first step is to compare the live system's FPGA version
# (FPGA_VERSION) to the value we would expect if the latest FPGA image were
# running (FILE_VERSION).
#
FILE_VERSION=03
FPGA_VERSION=`/usr/bin/head -2 /proc/system/fpga |
 /usr/bin/tail -1 |
 /usr/bin/cut -d' ' -f2`
echo "The current FPGA version is $FPGA_VERSION."
if [ "$FPGA_VERSION" = "$FILE_VERSION" ]
then
 echo "FPGA is up to date. No further action is required."
 exit 0
fi
#
# At this point we know the FPGA is out of date.
# Prompt the user to decide if now is the right time to do anything about it.
# Make it scary so the user won't make this decision lightly.
#
echo "FPGA version $FILE_VERSION is available."
echo "You should update the FPGA to the new version."
echo "Warning: FPGA update will disrupt traffic flowing through this switch."
echo "You will have to reseat the switch at the end of this procedure."
PROMPT="Do you want to update the FPGA to version $FILE_VERSION now? [N] "
while true
do
 read -ep "$PROMPT" REPLY
 case `echo $REPLY | /usr/bin/tr [:lower:] [:upper:]` in
  N|NO|"") exit 0 ;;
  Y|YES) break ;;
 esac
done
echo "Please avoid powering off this switch during programming."
echo ""
/fabos/share/jtagpgm -f /fabos/share/superhawk3_fpga_v${FILE_VERSION}.xsvf
#
# This script has done everything it can.
# The final step can only be done by the user.
#
echo ""
echo "The new FPGA image will not become active until you power-cycle the FPGA."
echo "Therefore you should remove and re-insert the switch."
