#!/bin/sh
#
#    Copyright (c) 2004-2022 Brocade Communications Systems LLC.
#    All rights reserved.
#
#    Description:
#
#        This pre-clean install script will be run before firmware
#        clean install starts.
#

# Increment is version# for every release in which a new check is added.
PRE_CLEAN_INSTALL_VER=3

TO_MAJOR=$1
TO_MINOR=$2
TO_PATCH=$3
TO_RELEASE_STR=$4
if [ "$TO_RELEASE_STR" == "Empty_Release_Str" ]; then
	TO_RELEASE_STR=""
fi

NEW_PREINST_FILE=$5
CUR_PREINST_FILE=/sbin/preinst

FROM_VER=`/bin/rpm -q fabos | /usr/bin/cut -f 2 -d '-'`
FROM_MAJOR=`echo $FROM_VER|/usr/bin/cut -d '.' -f 1`
FROM_MINOR=`echo $FROM_VER|/usr/bin/cut -d '.' -f 2`
FROM_PATCH_STR=`echo $FROM_VER|/usr/bin/cut -d '.' -f 3`
FROM_PATCH=${FROM_PATCH_STR:0:1}
FROM_RELEASE_STR=`/bin/cat /etc/version.conf | /bin/grep "Release" | /bin/awk '{print $2}'`
FROM_RELEASE=${FROM_RELEASE_STR:0:1}
TO_RELEASE=${TO_RELEASE_STR:0:1}
TO_VERSION="$TO_MAJOR$TO_MINOR$TO_PATCH$TO_RELEASE"
FROM_VERSION="$FROM_MAJOR$FROM_MINOR$FROM_PATCH$FROM_RELEASE"

#echo "TO_VER:" $TO_VERSION
#echo "FROM_VER:" $FROM_VERSION

STATUS_FILE=/tmp/fwdl_err.txt
BNA_STATUS_FILE=/tmp/bna_fwdl_err.txt

STS_OK=0
STS_ERR=255

rc=$STS_OK

PATH=/bin:/usr/bin:/sbin:/usr/sbin

################################################################################
#
# check for Gen6 Kenobi Support
# Downgrade to a release that does not support this feature is not allowed

GEN6_KENOBI_FWDL_BLOCK="Downgrade is not allowed as the destination firmware does not support this Chassis configuration."

check_gen6_kenobi_support()
{
	# If the NEW_PREINST_FILE is not available, no need to perform the following check
	if [ "$NEW_PREINST_FILE" == "" ]; then
		return $STS_OK
	fi

	gen6_kenobi_supported=`grep -c "GEN6_KENOBI_SUPPORTED" $NEW_PREINST_FILE`

	if [ $gen6_kenobi_supported -ne 0 ]; then
		# The new release supports this feature so ok to proceed.
        	return $STS_OK
    	fi

	#
    # Check if we are running as Gen6 Kenobi. If so then block the downgrade.
	
	board_override=`sin -c | grep -c "Board ID Override: 1"`
	if [ $board_override -eq 0 ]; then
		return $STS_OK
	else
		let rc=$rc+1
		echo -e $rc:	$GEN6_KENOBI_FWDL_BLOCK  >> $STATUS_FILE
		echo -e $rc:	$GEN6_KENOBI_FWDL_BLOCK  >> $BNA_STATUS_FILE
		return $STS_ERR
	fi
}

################################################################################
#
# check for Non Secure Vesper Support
# Downgrade to a release that does not support this feature is not allowed

NON_SECURE_VESPER_FWDL_BLOCK="Downgrade is not allowed as the destination firmware does not support this platform."

check_non_secure_vesper_support()
{
	# If the NEW_PREINST_FILE is not available, no need to perform the following check
	if [ "$NEW_PREINST_FILE" == "" ]; then
		return $STS_OK
	fi

	non_secure_vesper_supported=`grep -c "NON_SECURE_VESPER_SUPPORTED" $NEW_PREINST_FILE`

	if [ $non_secure_vesper_supported -ne 0 ]; then
		# The new release supports this feature so ok to proceed.
		return $STS_OK
	fi

	#
    # Check if we are running as Non Secure Vesper. If so then block the downgrade.

	non_secure_vesper_identifier=`sin -c | grep -c "Secure Mode: 1"`

	if [ $non_secure_vesper_identifier -eq 0 ]; then
		return $STS_OK
	else
		let rc=$rc+1
		echo -e $rc:	$NON_SECURE_VESPER_FWDL_BLOCK  >> $STATUS_FILE
		echo -e $rc:	$NON_SECURE_VESPER_FWDL_BLOCK  >> $BNA_STATUS_FILE
		return $STS_ERR
	fi
}

################################################################################
#
# check for Secure Chewbacca Support
# Downgrade to a release that does not support this feature is not allowed

SECURE_CHEWBACCA_FWDL_BLOCK="Firmware migration is not allowed as the destination firmware does not support this platform."

check_secure_chewbacca_support()
{	
	# If the NEW_PREINST_FILE is not available, no need to perform the following check
	if [ "$NEW_PREINST_FILE" == "" ]; then
		return $STS_OK
	fi

	secure_chewbacca_supported=`grep -c "SECURE_CHEWBACCA_SUPPORTED" $NEW_PREINST_FILE`
	if [ $secure_chewbacca_supported -ne 0 ]; then
		# The new release supports this feature so ok to proceed.
		return $STS_OK
	fi

	#
    # Check if we are running as Secure Chewbacca. If so then block the downgrade.

	secure_chewbacca_identifier=`sin -c | grep -c "Secure Mode: 0"`

	if [ $secure_chewbacca_identifier -eq 1 ]; then
		return $STS_OK
	else
		let rc=$rc+1
		echo -e $rc:	$SECURE_CHEWBACCA_FWDL_BLOCK  >> $STATUS_FILE
		echo -e $rc:	$SECURE_CHEWBACCA_FWDL_BLOCK  >> $BNA_STATUS_FILE
		return $STS_ERR
	fi
}

FWCLEANINSTALL_NOT_SUPPORTED="Firmwarecleaninstall to the target firmware version is not supported"

is_fwcleaninstall_allowed() {
	#Major,Minor, Patch and release  are used to compare versions. 
	if [[ "$TO_MAJOR" -gt "$FROM_MAJOR" ]]; then
		return 1
	elif [[ "$TO_MAJOR" -eq "$FROM_MAJOR" ]]; then
		if [[ "$TO_MINOR" -gt "$FROM_MINOR" ]]; then
			return 1
		elif [[ "$TO_MINOR" -eq "$FROM_MINOR" ]]; then
			if [[ "$TO_PATCH" -gt "$FROM_PATCH" ]]; then
				return 1
			elif [[ "$TO_PATCH" -eq "$FROM_PATCH" ]]; then
				if [[ "$TO_RELEASE" != "$FROM_RELEASE" ]]; then
					# To allow major RC build with empty release str to the same build of CVR/DVR/SQA/Nightly builds
					if [[ ( "$FROM_RELEASE" == "" && "$TO_RELEASE" == "_" ) || ( "$FROM_RELEASE" == "_" && "$TO_RELEASE" ==  "" ) ]]; then
						return 0
					else 
						return 1
					fi
				else
					return 0
				fi
			else
				# TO_PATCH is older
				return 1
			fi
		else
			return 1
		fi
	else
		# TO_MAJOR is older
		return 1
	fi
}

check_fwcleaninstall_allowed()
{
	is_fwcleaninstall_allowed
	RET=$?
	if [ $RET -eq 1 ]; then
		let rc=$rc+1
		echo -e $FWCLEANINSTALL_NOT_SUPPORTED  >> $STATUS_FILE
		echo -e $FWCLEANINSTALL_NOT_SUPPORTED  >> $BNA_STATUS_FILE
		# FOS-847213 - Added the below check to print the cleaninstall error log here
		# as we dont have the code to fetch the error string from fwdl_err.txt 
		# files for FW version <9.1.0.
		if [ "$FROM_MAJOR" -eq 9 ] && [ "$FROM_MINOR" -lt 1 ]; then
			echo -e "\nFirmwarecleaninstall to the target firmware version is not supported"
		fi
		return $STS_ERR
	else
		#echo "Firmwarecleaninstall to the target firmware version is supported\n"
		return $STS_OK
	fi
}

# main()
#
echo -n > $STATUS_FILE      # Clear the error file
echo -n > $BNA_STATUS_FILE  # Clear the BNA error file

check_fwcleaninstall_allowed
RET=$?
if [ $RET == $STS_ERR ];then
	exit $RET
fi

echo "The following item(s) need to be addressed before downloading the specified firmware:" >> $STATUS_FILE

check_gen6_kenobi_support
check_non_secure_vesper_support

exit $rc
