#!/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=2

TO_MAJOR=$1
TO_MINOR=$2
TO_PATCH=$3
FWNAME=$4
NEW_PREINST_FILE=$5
CUR_PREINST_FILE=/sbin/preinst

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


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
}

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

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
	#commenting it out since  "sin" based command to differentiate will be known later
#check_secure_chewbacca_support

exit $rc
