#!/bin/sh
#
# Copyright (c) 2019-2022 Citrix Systems, Inc. All rights reserved.
# This software and documentation contain valuable trade
# secrets and proprietary property belonging to Citrix Systems, Inc.
# None of this software and documentation may be copied,
# duplicated or disclosed without the express
# written permission of Citrix Systems, Inc.
#
# 07/18/22
#
# This file contains shell script code needed for
# Fortville NIC firmware update.
#
# usage:  sh fortville_fw_update_intel_1 <target firmware version>
#

SPECIFIED_FW_VERSION=${1}

BASE_DIR=/var/tmp/Fortville_Silicom_Intel
FW_UPDATE_SUBR="./fortville_fw_update_subr"

#
# Load the subroutine file
#
if [ -f ${FW_UPDATE_SUBR} ]
then
	. ${FW_UPDATE_SUBR}
else
	echo "Missing ${FW_UPDATE_SUBR} file!"
	exit 4
fi

#
# Make sure a firmware version argument is passed in
#
if [ "x${SPECIFIED_FW_VERSION}" = "x" ]
then
	log_msg ">>> Intel target firmware version not specified!"
	exit 5
else
	log_msg ">>> Intel target firmware version specified: " \
	    "${SPECIFIED_FW_VERSION}"
fi


# Print FW version and EETRACK ID for each Fortville NIC.
fw_eetrack
# Allow the above to flush out to console, log file.
sleep 4

#
# If the firmware version is known, set the corresponding parameters.
#
case ${SPECIFIED_FW_VERSION} in
505|5.05 )
	FW_VERSION=505
	FW_DIRECTORY=${BASE_DIR}/Intel/to_505_from_4r53_6r01
	TOOLS_DIR=${FW_DIRECTORY}
	NVM_UPDATE=nvmupdate64e_6.01
	CFG_FILE=nvmupdate.cfg
	OPTS="-u"
	;;
601|6.01 )
	FW_VERSION=601
	FW_DIRECTORY=${BASE_DIR}/Intel/to_6r01_from_4r53_5rxx
	TOOLS_DIR=${FW_DIRECTORY}
	NVM_UPDATE=nvmupdate64e
	CFG_FILE=nvmupdate.cfg
	OPTS="-u"
	;;
700|7.00 )
	FW_VERSION=700
	FW_DIRECTORY=${BASE_DIR}/Intel/to_7r00_from_4rxx_5rxx_6r0x
	TOOLS_DIR=${BASE_DIR}/tools
	NVM_UPDATE=nvmupdate64e_1.38.13.1
	CFG_FILE=nvmupdate.cfg
	OPTS="-u"
	;;
870|8.70 )
	FW_VERSION=870
	FW_DIRECTORY=${BASE_DIR}/Intel/to_8r70_from_xrxx
	TOOLS_DIR=${BASE_DIR}/tools
	NVM_UPDATE=nvmupdate64e_1.38.13.1
	CFG_FILE=nvmupdate.cfg
	OPTS="-u"
	;;
*   )
	log_msg ">>> Intel target firmware version ${SPECIFIED_FW_VERSION}" \
	    "is not available."
	# Must return 0 to prevent loop on nonexistent firmware
	return 0
	;;
esac

log_msg ">>> Intel target firmware version:  ${FW_VERSION}"

NVM_UPDATE_FULL_PATH=${TOOLS_DIR}/${NVM_UPDATE}

#
# Make sure the nvmupdate64e binary exists
#
if [ ! -e ${NVM_UPDATE_FULL_PATH} ] ||
   [ ! -f ${NVM_UPDATE_FULL_PATH} ] ||
   [ ! -s ${NVM_UPDATE_FULL_PATH} ]
then
	log_msg ">>> ${NVM_UPDATE_FULL_PATH} does not exist!"
	sleep 1
	# Must return 0 to prevent loop on nonexistent nvmupdate64e
	return 0
fi

CFG_FILE_FULL_PATH=${FW_DIRECTORY}/${CFG_FILE}

#
# Make sure the configuration file exists
#
if [ ! -e ${CFG_FILE_FULL_PATH} ] ||
   [ ! -f ${CFG_FILE_FULL_PATH} ] ||
   [ ! -s ${CFG_FILE_FULL_PATH} ]
then
	log_msg ">>> ${CFG_FILE_FULL_PATH} does not exist!"
	sleep 1
	# Must return 0 to prevent loop on nonexistent config file
	return 0
fi

nic_ixl_numbers

#
# Enable firmware update capability.
#
for VALUE in ${VALUES}
do
	#echo ${VALUE};
	#sleep 1

	is_intel_nic_port_0 ${VALUE}
	if [ $? -eq 0 ]
	then
		#echo "is Intel nic, port 0"
		#sleep 1
		sysctl dev.ixl.${VALUE}.en_firmware_update=1 > /dev/null
		#sleep 1
	fi
done

#
# Needed for compatibility with certain nvmupdate64e versions.
# Uncomment this to enable renaming feature.
#
# rename_interfaces

#
# Program firmware on Silicom Fortville NICs.
#
OLDPATH=`pwd`
cd ${FW_DIRECTORY}
# Make sure we have execute permission on the firmware update tool.
chmod 544 ${TOOLS_DIR}/${NVM_UPDATE}
# Update the firmware.
${TOOLS_DIR}/${NVM_UPDATE} ${OPTS} -c ${CFG_FILE}
NVMUPD_RET_CODE=$?
log_msg ">>> nvmupdate64e return code:  ${NVMUPD_RET_CODE}"
log_msg ""
sleep 2
cd $OLDPATH

#
# Disable firmware update capability.
#
for VALUE in ${VALUES}
do
	#echo ${VALUE};
	#sleep 1

	is_intel_nic_port_0 ${VALUE}
	if [ $? -eq 0 ]
	then
		#echo "is Intel nic, port 0"
		#sleep 1
		sysctl dev.ixl.${VALUE}.en_firmware_update=0 > /dev/null
		#sleep 1
	fi
done

# Print FW version and EETRACK ID for each Fortville NIC.
fw_eetrack
# Allow the above to flush out to console, log file.
sleep 4

return ${NVMUPD_RET_CODE}

