#!/bin/bash
#
#    Copyright (c) 1996-2015 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#      Initialization script to start/stop Fabric OS services and interfaces
#      on different platform.
#


#
# Set a signal handler for the script.
#
trap sig_handler SIGBUS
export LD_LIBRARY_PATH=/lib:/usr/lib:/fabos/lib:/usr/confd/lib

#
# Signal handler for this script.
#
sig_handler()
{
	echo "**********************************************************"
	echo "RECEIVED UNEXPECTED SIGNAL. PROCESS INFORMATION:"
	echo "**********************************************************"
	ps auxw
	exit 1
}

#
# unsupported <platform> <exit status>
#
# Throw an error message indicating that this platform is not
# supported and exit the script with the specified status code.
#
unsupported()
{
	echo "Platform code ${1} isn't supported" >&2
	exit ${2}
}

#
# get_cmodel_server
#
# Check if c-model emulation is on or off
#
get_cmodel_server() {

	# File name of cmodel server ip
	# Read the file /etc/fabos/cmodel_server to check configured server ip
	# If not present local server will be started
	CMS_FILE=/etc/fabos/cmodel_server
	if [ -a $CMS_FILE ]; then
		CMS_SERVER=`cat $CMS_FILE`
		echo "Start cmodel server at" $CMS_SERVER
		return  1
	else	
		CMS_SERVER= 0;
		return  0 
	fi
}

#
# pic_config
#
# Routine selects the blade profile using file /etc/config/pic_conf
#

pic_config() {

CONF_FILE=/etc/config/pic_conf

	if [ -s $CONF_FILE ]; then
		FIPS=`cat $CONF_FILE|sed -n -e 's/^\([[:digit:]]\{1,\}\).*$/\1/gp'`
	else
		FIPS=0
	fi
#	echo "**Rigel2 PIC cnfiguration $FIPS**"
    return $FIPS
}



#
# swprofile
#
# Routine selects the blade profile using file /etc/config/swprofile
#

swprofile() {

MIN_PROF=0
MAX_PROF=10

SWPROF_FILE=/etc/config/swprofile

FIPS_FLAG=0

	if [ -f $SWPROF_FILE ]; then
		if [ -s $SWPROF_FILE ]; then
			FIPS_FLAG=`cat $SWPROF_FILE|sed -n -e 's/^\([[:digit:]]\{1,\}\).*$/\1/gp'`
			if [[ "$FIPS_FLAG >= 0$MIN_PROF" && "$FIPS_FLAG < 0$MAX_PROF" ]]; then
				FIPS=$FIPS_FLAG
			else
				FIPS=0
			fi
		else
			FIPS=0
		fi
	else
		FIPS=0
	fi
	echo "************** Returning Profile ID $FIPS ***************"
	return $FIPS
}
# Function compares versions of both partitions and returns
# 0 if both partitions have same firmware.
# 1 if both paritions have same firmware but different release label
# 2 if primary is higher
# 3 if secondary is higher

comp_firmware_ver() {
	VER_P=`/sbin/getfabosver /fabos/share/version base`
	VER_S=`/sbin/getfabosver /mnt/fabos/share/version base`

	MAJOR_P=`echo $VER_P |cut -d '.' -f 1`
	MINOR_P=`echo $VER_P | cut -d '.' -f 2`
	PATCH_P_STR=`echo $VER_P | cut -d '.' -f 3`
	PATCH_P=${PATCH_P_STR:0:1}

	MAJOR_S=`echo $VER_S |cut -d '.' -f 1`
	MINOR_S=`echo $VER_S | cut -d '.' -f 2`
	PATCH_S_STR=`echo $VER_S | cut -d '.' -f 3`
	PATCH_S=${PATCH_S_STR:0:1}

	# Check for major
	if [ $MAJOR_P -gt $MAJOR_S ]; then
		ret=2
	elif [ $MAJOR_P -lt $MAJOR_S ]; then
		ret=3
	else
		# Check for Minor
		if [ $MINOR_P -gt $MINOR_S ]; then
			ret=2
		elif [ $MINOR_P -lt $MINOR_S ]; then
			ret=3
		else
			# Check for Patch
			if [ $PATCH_P -gt $PATCH_S ]; then
				ret=2
			elif [ $PATCH_P -lt $PATCH_S ]; then
				ret=3
			else
				# Major, Minor and Patch are same. Check for the version label
				if [ $VER_P = $VER_S ]; then
					ret=0
				else
					ret=1
				fi
			fi
		fi
	fi

	return $ret
}

#
# cmodel_status
#
# Check if c-model emulation is on or off
#
cmodel_status() {

	# File name of cmodel emulation
	# Read the file /etc/fabos/cmodel to check if CMODEL EMULATION MODE is on
	# or off. Absence of the file or a zero written to it means cmodel
	# emulation is off and we can
	# chkconfig cmodel off, otherwise cmodel emulation is enabled.
	CMS_FILE=/etc/config/cmodel
	if [ -a $CMS_FILE ]; then
		CMS_FLAG=`cat $CMS_FILE`
		if [ "$CMS_FLAG" = "on" ]
		then
			CMODEL=1
		else
			CMODEL=0
		fi
	else	
		CMODEL=0
	fi
	return $CMODEL
}

# Function returns
# 0 if both partitions have same firmware
# 1 if both paritions have same firmware but different version label
# 2 if primary is higher
# 3 if secondary is higher

comp_firmware_ver() {
	VER_P=`getfabosver /fabos/share/version base`
	VER_S=`getfabosver /mnt/fabos/share/version base`

	MAJOR_P=`echo $VER_P |cut -d '.' -f 1`
	MINOR_P=`echo $VER_P | cut -d '.' -f 2`
	PATCH_P_STR=`echo $VER_P | cut -d '.' -f 3`
	PATCH_P=${PATCH_P_STR:0:1}

	MAJOR_S=`echo $VER_S |cut -d '.' -f 1`
	MINOR_S=`echo $VER_S | cut -d '.' -f 2`
	PATCH_S_STR=`echo $VER_S | cut -d '.' -f 3`
	PATCH_S=${PATCH_S_STR:0:1}

	# Check for major
	if [ $MAJOR_P -gt $MAJOR_S ]; then
		ret=2
	elif [ $MAJOR_P -lt $MAJOR_S ]; then
		ret=3
	else
		# Check for Minor
		if [ $MINOR_P -gt $MINOR_S ]; then
			ret=2
		elif [ $MINOR_P -lt $MINOR_S ]; then
			ret=3
		else
			# Check for Patch
			if [ $PATCH_P -gt $PATCH_S ]; then
				ret=2
			elif [ $PATCH_P -lt $PATCH_S ]; then
				ret=3
			else
				# Major, Minor and Patch are same. Check for the version label
				if [ $VER_P = $VER_S ]; then
					ret=0
				else
					ret=1
				fi
			fi
		fi
	fi

	return $ret
}
#
# cbr_revb_status
#
# Check if Cobra rev-b emulation is on or off
#
cbr_revb_status() {
	# Read the file /etc/fabos/cbr_revb to check if Cobra rev-b emulation
	# mode is on or off.
	REVB_FILE=/etc/config/cbr_revb
	if [ -a $REVB_FILE ]; then
		REVB_FLAG=`cat $REVB_FILE`
		if [ "$REVB_FLAG" = "on" ]
		then
			REVB=1
		else
			REVB=0
		fi
	else	
		REVB=0
	fi
	return $REVB
}

#
# emulation_status
#
# Check if emulation is on or off
#
emulation_status() {

	# File name of emulation
	# Read the file /etc/fabos/emulator to check if EMULATION MODE is on or off.
	# Absence of the file or a zero written to it means emulation is off and we can
	# chkconfig emulator off, otherwise emulation is enabled.
	EMUL_FILE=/etc/config/emulator
	if [ -a $EMUL_FILE ]; then
		EMUL_FLAG=`cat $EMUL_FILE`
		if [ "$EMUL_FLAG" = "on" ]
		then
			EMULATION=1
		else
			EMULATION=0
		fi
	else	
		EMULATION=0
	fi
	return $EMULATION
}

#
# blade driver 40G portmap
#
# Check if bitmask is set
#
get_portmap() {

	# File name of bladedriver bitmask 
	# Read the file /etc/fabos/portmap to check portmap.
	# Absence of the file or a zero written to it Port map update not required
	# Bitmap setting of that port suggest port will be updated from 40G to 10G or
	# vice versa 
	BITMAP_FILE=/etc/fabos/portmap
	if [ -a $BITMAP_FILE ]; then
		BITMAP=`cat $BITMAP_FILE`
		PORT_MAP_0=`echo $BITMAP | awk '{ print $1; }'`
		PORT_MAP_1=`echo $BITMAP | sed "s/[^\w]* //"`
	else	
		PORT_MAP_0=0
		PORT_MAP_1=0
	fi
	return 0 
}

#
# Check Pizzabox Airflow Direction Change 
#
#
sys_airflow_change_info() {
	# Read the file /etc/fabos/cbr_revb to check if Cobra rev-b emulation
	# mode is on or off.
	AIRFLOW_FILE=/var/log/sysairflow.1
	if [ -a $AIRFLOW_FILE ]; then
	NewFlowDir=`cat $AIRFLOW_FILE`
	case ${SWBD##SWBD} in
		'153' | '154' | '131' | '137' | '151' | '164')
			/fabos/cliexec/errlogtest -i EM-1038 -a $NewFlowDir
			rm -rf $AIRFLOW_FILE 2> /dev/null
			;;

		*)
			;;
	esac
	fi
}


# Source common library
. /etc/init.d/functions

# PATH and POSTGRES_VERSION are defined in functions script

FABOSHOME=/fabos
export FABOSHOME
#ulimit -c 1000000000

export 	ROLE_ID=root
export	CURRENT_AD=0
export	HOME_VF=128
export	CURRENT_VF=128
export	CHASSIS_ROLEID=0
export	VFROLEMAP=-1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0

export OPENSSL_CONF="/etc/fabos/openssl.cnf"

# Determine the system platform identifier.
SWBD=`sin | swbd 2> ${NULL}`

MY_EMUL_FILE=/users/emulator
EMUSWBD=""
if [ -a $MY_EMUL_FILE ]; then
	EMUSWBD=`cat $MY_EMUL_FILE`
	echo $EMUSWBD ${EMUSWBD##SWBD}
	case ${EMUSWBD##SWBD} in
		'1' )
			echo "Emulation $EMUSWBD/153 Platform"
			SWBD="SWBD153"
			;;
		'153' | '154' | '164' )
			echo "Emulation $EMUSWBD Platform"
			SWBD=$EMUSWBD
			;;
		*)
			echo "  "
			;;
	esac
fi


CPID=`sin | cpid 2> ${NULL}`
MODE="0x0`bootenv startup_mode 2> ${NULL}`"
FABOS_START=`bootenv fabos 2> ${NULL}`
SIM_ASICLESS=`bootenv simasic 2> ${NULL}`
SYSLOCK_DEBUG=`bootenv syslock_debug 2> ${NULL}`
if [ "$SYSLOCK_DEBUG" != "1" ] ; then
   SYSLOCK_DEBUG="0"
fi
PARTITION=1
if [ "`cat /proc/device-tree/label 2>${NULL}`" = "p2-linux" ]; then
        PARTITION=2
fi
# Determine the linux version
LINUXVERSION=`uname -r`

# Stores the main CP IP address on Zentron 
MAINCPIPADDR="127.1.xx.xx"

# Determine the state-sync transport based on the platform identifier.
case ${SWBD##SWBD} in
	'1000' | '1001' | '1002' | '1003')
		XPT="XCP"
		;;

	*)
		XPT="XLO"
		;;
esac

#
# stop_daemons <switch_number>
# Stop daemons related to a particular switch.
#
stop_daemons()
{
	pid=`cat /var/run/*.pid 2> ${NULL}`

	if [ "$pid" != "" ] ; then
		kill -TERM $pid > ${NULL} 2>&1
		sleep 2
		pid=`ps -eaf | grep "\-s $1" | /usr/bin/tr -s " " : | /usr/bin/cut -d: -f2`
		kill -KILL $pid > ${NULL} 2>&1
	fi

	sleep 1
	# remove pid file if any.
	rm -f /var/run/*$1.pid
}

#
# killproc <switch no>
# Kill the switch specific daemons
#
killproc()
{
	# kill switch specific daemons
	pid=`cat /var/run/*$1.pid 2> ${NULL}`
	if [ "$pid" != "" ] ; then
		kill -TERM $pid > ${NULL} 2>&1
		sleep 1
		kill -KILL $pid > ${NULL} 2>&1
	fi

	# remove pid file if any.
	rm -f /var/run/*$1.pid
}

#
# kill httpd
#
killhttp()
{
	# kill webtools for this switch
	if [ -f '/tmp/webtools/logs/httpd.pid.$1' ] ; then
		pid=`cat /tmp/webtools/logs/httpd.pid.$1 2> ${NULL}`
		if [ "$pid" != "" ] ; then
			kill -TERM $pid > ${NULL} 2>&1
		fi
	fi
}

#
# setDbg block
#
doSetDbg()
{
	#
	# In order to turn on setdbg for the following, you need
	# to do chkconfig fabosSetDbg on, which will be preserved
	# across firmwaredownloads and reboots.
	#
	# Please don't add any setdbg outside of this check even as
	# a temporary measure, which lead to chaos in some customer
	# sites. It's easy to forget to remove this kind of stuff.
	#
	if $CHECK fabosSetDbg; then
		/fabos/cliexec/setdbg BLOOM 4
		/fabos/cliexec/setdbg TRUNK 4
		/fabos/cliexec/setdbg FLTR 4
		/fabos/cliexec/setdbg INTR 4
		/fabos/cliexec/setdbg PHAN 4
		/fabos/cliexec/setdbg LOOP 4
		/fabos/cliexec/setdbg SPEED 4
		/fabos/cliexec/setdbg BLADE 1
		/fabos/cliexec/setdbg FSSK 4
		/fabos/cliexec/setdbg SYNC_SWC 4
		/fabos/cliexec/setdbg RCVY_SWC 4

	fi
		# setDbg BLOOM	4
		# setDbg FLTR	4
		# setDbg INTR	4
		# setDbg RCVY_SWC	4
}

#
# modutil <load | unload> <module name>
#
# Load a kernel module, which may be compressed
#
modutil()
{
	cmd=$1
	file=$2
	shift 2
	case $cmd in
	("load")
		if [ -r /fabos/modules/$file.ko ]
			then if insmod /fabos/modules/$file.ko "$@" > ${NULL} 2>&1
			then return
		else RETURN_VALUE=$?
	    fi
		elif [ -r /fabos/modules/$file.ko.gz ]
			then if zcat /fabos/modules/$file.ko.gz | insmod - "$@" > ${NULL} 2>&1
			then return
		else RETURN_VALUE=$?
	    fi
		else RETURN_VALUE=1 ; echo "modutil: load $file does not exist"
		fi
		echo "Error $RETURN_VALUE returned by insmod while loading $file" >&2
		return $RETURN_VALUE
		;;
	(*) ;;
	esac
}

#
# linuxmodutil <load | unload> <module name>
#
# Load a linux kernel module
#
LINUXINSMOD_SUCESS=0
linuxmodutil()
{
	case $1 in
	load)
		insmod $2 $3 $4 $5 $6> ${NULL} 2>&1
		RETURN_VALUE=$?
		if [ $RETURN_VALUE -ne $LINUXINSMOD_SUCESS ]; then
			echo "Error " $RETURN_VALUE " returned by insmod while loading " $2
		fi
		;;

	*)
		;;
	esac
	return 0
}

load_switch_module()
{
	CPULOAD=$(/fabos/cliexec/config get system.cpuLoad 2)
	CPULOAD=${CPULOAD:-121}
	CPULOAD=$(($CPULOAD*2048/100))
	modutil load switch-module sw_cpu_load=$CPULOAD
}

#
# load_eanvil_module
# 
# Selectively load the eAnvil asic module based on PCI revision Id.
# Note: this assumes that all chips are of the same revision and that
# eAnvil is only installed in pizza box systems where the Id is known
# at boot time (i.e. not during hot plug).
#
load_eanvil_module()
{
	EANV_ID=1657:001a

	# 
	# Examine the PCI revision Id of the first eAnvil chip.
	# \( and \) delimits the rev Id to retrieve and is referenced by \1
	#
	eanvil_rev=`cat /proc/pci | grep $EANV_ID | head -1 | \
		sed 's/.*rev \([0-9][0-9]*\).*/\1/'`

	case $eanvil_rev in
		0) echo "eAnvil rev A found";
			modutil load eanvil_rev_a-module
		    ;;

		1) echo "eAnvil rev B found";
			modutil load eanvil-module
		    ;;

	 	*) echo "Unrecognized eAnvil version '$eanvil_rev', defaulting to revB";
		   modutil load eanvil-module
		   ;;
	esac
}

#
# load_cobra2_module
#
load_cobra2_module()
{
	modutil load cbr2_a-module
}

load_cobra_mac_simulation_module()
{
	modutil load cbr_rev-module
	if [ -r /proc/driver/cbr_rev ]; then
        cbr_rev=`cat /proc/driver/cbr_rev`
	case $cbr_rev in
		1.1) echo "Cobra rev A found";
                touch /etc/fabos/cbr_rev_tmp
                echo 1.1 >/etc/fabos/cbr_rev_tmp
                mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
		modutil load cbrmac_a-module
		modutil load cbrsim_a-module $@
			;;
		2.0) echo "Cobra rev B found";
                touch /etc/fabos/cbr_rev_tmp
                echo 2.0 >/etc/fabos/cbr_rev_tmp
                mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
		modutil load cbrmac_b-module 
		modutil load cbrsim_b-module cmodel_enable=$cmodel_enable
			;;
		*) echo "Unrecognized Cobra version '$cbr_rev', defaulting to rev A"
                touch /etc/fabos/cbr_rev_a
                touch /etc/fabos/cbr_rev_tmp
                echo 1.1 >/etc/fabos/cbr_rev_tmp
                mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
		modutil load cbrmac_a-module
		modutil load cbrsim_a-module $@
                ;;
        esac
	fi
}

load_cobra2_mac_chip_module()
{
	modutil load cbr_rev-module
	if [ -r /proc/driver/cbr_rev ]; then
        cbr_rev=`cat /proc/driver/cbr_rev`
	case $cbr_rev in
		1.1) echo "Cobra rev A found";
                touch /etc/fabos/cbr_rev_tmp
                echo 1.1 >/etc/fabos/cbr_rev_tmp
                mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
		modutil load cbrmac_a-module
			;;
		2.0) echo "Cobra rev B found";
                touch /etc/fabos/cbr_rev_tmp
                echo 2.0 >/etc/fabos/cbr_rev_tmp
                mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
		modutil load cbrmac_b-module 
			;;
		*) echo "Unrecognized Cobra version '$cbr_rev', defaulting to rev A"
                touch /etc/fabos/cbr_rev_a
                touch /etc/fabos/cbr_rev_tmp
                echo 1.1 >/etc/fabos/cbr_rev_tmp
                mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
		modutil load cbrmac_a-module
                ;;
        esac
	elif [ "$SIM_ASICLESS" = 'true' ]; then
        touch /etc/fabos/cbr_rev_a
        touch /etc/fabos/cbr_rev_tmp
        echo 1.1 >/etc/fabos/cbr_rev_tmp
        mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
		modutil load cbrmac_a-module
	fi 
	
}

#
# load_cobra_module
#
load_cobra_module()
{
	SLOT=`ifconfig $INTERFACE | grep 127.2 | awk '{print $2}'| cut -d ":" -f 2 | sed 's}.*\.}}'`
	part_state=`cat /proc/device-tree/chosen/status`
    case $part_state in
        active)
	# first check if cbr version is already detected
		if [ -r /var/.cbr_rev ]; then
			cbr_rev=`cat /var/.cbr_rev`
	# If not found then, load the rev module and read the proc.  
		else
	modutil load cbr_rev-module
	if [ -r /proc/driver/cbr_rev ]; then
        cbr_rev=`cat /proc/driver/cbr_rev`
    else
           echo "cbr_rev-module NOT FOUND!" 
    fi
		fi
	# Based on rev, load the cobra module, and creat .cbr_rev in /var,
	# so that we dont need to load rev module on every reboot/powercycle.
	# if cbr_rev not found, then load the cbr_b module by default.	
		case $cbr_rev in
		1.1) 
			echo "Cobra rev A found";
			touch /etc/fabos/cbr_rev_tmp
			echo 1.1 >/etc/fabos/cbr_rev_tmp
			mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
			modutil load cbr_a-module $@
			echo 1.1 >/var/.cbr_rev
        		echo 1.1 >/mnt/var/.cbr_rev
		;;
		2.0) 
			echo "Cobra rev B found";
			touch /etc/fabos/cbr_rev_tmp
			echo 2.0 >/etc/fabos/cbr_rev_tmp
			mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
			modutil load cbr_b-module $@
			echo 2.0 >/var/.cbr_rev
	        	echo 2.0 >/mnt/var/.cbr_rev
		;;
		*) 
			echo "Unrecognized Cobra version '$cbr_rev', defaulting to rev B"
			touch /etc/fabos/cbr_rev_tmp
			echo 2.0 >/etc/fabos/cbr_rev_tmp
			mv /etc/fabos/cbr_rev_tmp /etc/fabos/cbr_rev
			modutil load cbr_b-module $@
		;;
		esac
        ;;
	standby)
	# first check if cbr version is already detected
		if [ -r /var/.cbr_rev ]; then
			cbr_rev=`cat /var/.cbr_rev`
	# If not found then, this might be netinstall case, so rcp from active.
		else
	#below code decides the IP from where cbr_rev needs to be pulled in cool boot and in warm boot.
	if [ "$PARTITION" == 2 ]; then
		(( PARTITION = $PARTITION - 1 ))
		IP=127.2.$PARTITION.$SLOT
	else
		(( PARTITION = $PARTITION + 1 ))
		IP=127.2.$PARTITION.$SLOT	
	fi
       COUNT=10
       rcp root@$IP:/etc/fabos/cbr_rev /etc/fabos  > ${NULL} 2>&1
       while [ $COUNT -gt 0 ]
       do
           if [ -f /etc/fabos/cbr_rev ];then
               cbr_rev=`cat /etc/fabos/cbr_rev`
					break	
				else
					(( COUNT = $COUNT - 1 ))
					echo "Waiting Cobra rev file...";
					sleep 2
					rcp root@$IP:/etc/fabos/cbr_rev /etc/fabos  > ${NULL} 2>&1
					if [ $COUNT -eq 0 ];then
						cbr_rev=3 	
					fi
				fi
			done
		fi
	# Based on rev, load the cobra module, and creat .cbr_rev in /var,
	# so that we dont need to load rev module on every reboot/powercycle.
	# if cbr_rev not found, then load the cbr_b module by default.
		case $cbr_rev in
		1.1) 
			echo "Cobra rev A found";
			modutil load cbr_a-module $@
			echo 1.1 >/var/.cbr_rev
        		echo 1.1 >/mnt/var/.cbr_rev
		;;
		2.0) 
			echo "Cobra rev B found";
			modutil load cbr_b-module $@
			echo 2.0 >/var/.cbr_rev
	        	echo 2.0 >/mnt/var/.cbr_rev
		;;
		*) 
			echo "Unrecognized Cobra version '$cbr_rev', defaulting to rev B"
			modutil load cbr_b-module $@
		;;
		esac
	;;
        esac
}

#
# load_cobra2_rev_module
#
load_cobra2_rev_module()
{
	SLOT=`ifconfig $INTERFACE | grep 127.2 | awk '{print $2}'| cut -d ":" -f 2 | sed 's}.*\.}}'`
	part_state=`cat /proc/device-tree/chosen/status`
    case $part_state in
        active)
	# first check if cbr version is already detected
		if [ -r /var/.cbr2_rev ]; then
			cbr2_rev=`cat /var/.cbr2_rev`
	# If not found then, load the rev module and read the proc.  
		else
	modutil load cbr2_rev-module
	if [ -r /proc/driver/cbr2_rev ]; then
        cbr2_rev=`cat /proc/driver/cbr2_rev`
    else
           echo "cbr2_rev-module NOT FOUND!" 
    fi
		fi
	# Based on rev, load the cobra module, and creat .cbr2_rev in /var,
	# so that we dont need to load rev module on every reboot/powercycle.
	# if cbr2_rev not found, then load the cbr_b module by default.	
		case $cbr2_rev in
		1.0) 
			echo "Cobra2 1.0 found";
			touch /etc/fabos/cbr2_rev_tmp
			echo 1.1 >/etc/fabos/cbr2_rev_tmp
			mv /etc/fabos/cbr2_rev_tmp /etc/fabos/cbr2_rev
			echo 1.1 >/var/.cbr2_rev
        	echo 1.1 >/mnt/var/.cbr2_rev
		;;
		1.1) 
			echo "Cobra2 1.1 found";
			touch /etc/fabos/cbr2_rev_tmp
			echo 1.1 >/etc/fabos/cbr2_rev_tmp
			mv /etc/fabos/cbr2_rev_tmp /etc/fabos/cbr2_rev
			echo 1.1 >/var/.cbr2_rev
        	echo 1.1 >/mnt/var/.cbr2_rev
		;;
		1.2) 
			echo "Cobra2 1.2 found";
			touch /etc/fabos/cbr2_rev_tmp
			echo 1.2 >/etc/fabos/cbr2_rev_tmp
			mv /etc/fabos/cbr2_rev_tmp /etc/fabos/cbr2_rev
			echo 1.2 >/var/.cbr2_rev
	        echo 1.2 >/mnt/var/.cbr2_rev
		;;
		1.3) 
			echo "Cobra2 1.3 found";
			touch /etc/fabos/cbr2_rev_tmp
			echo 1.3 >/etc/fabos/cbr2_rev_tmp
			mv /etc/fabos/cbr2_rev_tmp /etc/fabos/cbr2_rev
			echo 1.3 >/var/.cbr2_rev
	        echo 1.3 >/mnt/var/.cbr2_rev
		;;
		*) 
			echo "Unrecognized Cobra version '$cbr2_rev', defaulting to v1.3"
			touch /etc/fabos/cbr2_rev_tmp
			echo 1.3 >/etc/fabos/cbr2_rev_tmp
			mv /etc/fabos/cbr2_rev_tmp /etc/fabos/cbr2_rev
		;;
		esac
        ;;
	standby)
	# first check if cbr version is already detected
		if [ -r /var/.cbr2_rev ]; then
			cbr2_rev=`cat /var/.cbr2_rev`
	# If not found then, this might be netinstall case, so rcp from active.
		else
	#below code decides the IP from where cbr2_rev needs to be pulled in cool boot and in warm boot.
	if [ "$PARTITION" == 2 ]; then
		(( PARTITION = $PARTITION - 1 ))
		IP=127.2.$PARTITION.$SLOT
	else
		(( PARTITION = $PARTITION + 1 ))
		IP=127.2.$PARTITION.$SLOT	
	fi
       COUNT=10
       rcp root@$IP:/etc/fabos/cbr2_rev /etc/fabos  > ${NULL} 2>&1
       while [ $COUNT -gt 0 ]
       do
           if [ -f /etc/fabos/cbr2_rev ];then
               cbr2_rev=`cat /etc/fabos/cbr2_rev`
					break	
				else
					(( COUNT = $COUNT - 1 ))
					echo "Waiting Cobra rev file...";
					sleep 2
					rcp root@$IP:/etc/fabos/cbr2_rev /etc/fabos  > ${NULL} 2>&1
					if [ $COUNT -eq 0 ];then
						cbr2_rev=1.3 	
					fi
				fi
			done
		fi
	# Based on rev, load the cobra2 module, and creat .cbr2_rev in /var,
	# so that we dont need to load rev module on every reboot/powercycle.
	# if cbr2_rev not found, then load the cbr_b module by default.
		case $cbr2_rev in
		1.0) 
			echo "Cobra2 rev 1.0 found";
			echo 1.1 >/var/.cbr2_rev
        	echo 1.1 >/mnt/var/.cbr2_rev
		;;
		1.1) 
			echo "Cobra2 rev 1.1found";
			echo 1.1 >/var/.cbr2_rev
        	echo 1.1 >/mnt/var/.cbr2_rev
		;;
		1.2) 
			echo "Cobra2 rev 1.2 found";
			echo 1.2 >/var/.cbr2_rev
	        echo 1.2 >/mnt/var/.cbr2_rev
		;;
		1.3) 
			echo "Cobra2 rev 1.3 found";
			echo 1.3 >/var/.cbr2_rev
	        echo 1.3 >/mnt/var/.cbr2_rev
		;;
		*) 
			echo "Unrecognized Cobra version '$cbr2_rev', defaulting to v1.3"
			echo 1.3 >/var/.cbr2_rev
	        echo 1.3 >/mnt/var/.cbr2_rev
		;;
		esac
	;;
        esac
}
#
# Start module for Mercury M4 platform.
#
swbd1000_module()
{

	modutil load ki-module
	modutil load m4-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	load_switch_module
	modutil load chassis-module
	modutil load cee-cp-module
	modutil load mercury-module
	modutil load condor3-module
	modutil load fc-module
	modutil load fcoe-module
	modutil load routing-module
	modutil load diag-module
	modutil load dpci_pciehp
	modutil load trill-module
#	modutil load mm_spi-module
	modutil load ethswitch_reset-module

	return 0
}
#
# Start module for Mercury M8 platform.
#
swbd1001_module()
{

	modutil load ki-module
	modutil load m8-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	load_switch_module
	modutil load chassis-module
	modutil load cee-cp-module
	modutil load mercury-module
	modutil load condor3-module
	modutil load fc-module
	modutil load fcoe-module
	modutil load routing-module
	modutil load diag-module
	modutil load dpci_pciehp
	modutil load trill-module
#	modutil load mm_spi-module
	modutil load ethswitch_reset-module

	return 0
}
#
# Start module for Mercury M16 platform.
#
swbd1002_module()
{
	modutil load ki-module
	modutil load m16-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module ethsw_ha_enabled=$ETHSW_HA_ENABLE
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	load_switch_module
	modutil load chassis-module
	modutil load cee-cp-module
	modutil load pen-module
	modutil load condor3-module
	modutil load fc-module
	modutil load fcoe-module
	modutil load routing-module
	modutil load diag-module
	modutil load dpci_pciehp
	modutil load trill-module
#	modutil load mm_spi-module
	modutil load ethswitch_reset-module

	return 0
}


load_ip_crypto()
{
	IPSV4MODDIR=/lib/modules/$LINUXVERSION/kernel/net/ipv4
	IPSV6MODDIR=/lib/modules/$LINUXVERSION/kernel/net/ipv6
	CRYPTOMODDIR=/lib/modules/$LINUXVERSION/kernel/crypto
	KEYMODDIR=/lib/modules/$LINUXVERSION/kernel/net/key
	XFRMMODDIR=/lib/modules/$LINUXVERSION/kernel/net/xfrm

	linuxmodutil load $KEYMODDIR/af_key.ko
	linuxmodutil load $XFRMMODDIR/xfrm_user.ko

	# Make sure we only look at .ko files
	for i in ${IPSV4MODDIR}/*.ko ${IPSV6MODDIR}/*.ko ${CRYPTOMODDIR}/*.ko
	do
		linuxmodutil load $i
	done
}


#
# Create a gpio node which uses the misc device driver
# with a minor number of 185.
#
mk_gpio_device()
{

	# Create gpio driver node if the misc driver
	# is available, needed for lbist

	node="misc"
	# note that the usual (cut -d ' ' -f 1) doesn't work for
	# misc since it has leading blanks
	major=`grep $node\$ /proc/devices | cut -c 1-3`

	if [ $major ]; then
		# found it now create the gpio node (minor = 185)
		node="gpio"
		rm -f /dev/$node
		mknod --mode=644 /dev/$node c $major 185
	fi

	return 0
}

#
# Elara2 support
#
swbd91_module()
{
	modutil load elara2-module
	modutil load portlog-module
	modutil load ki-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load switch-module
	modutil load chassis-module
	modutil load geye2-module
	load_eanvil_module
	modutil load dce-blade-module
	modutil load fc-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}

#
# Elara support
#
swbd76_module()
{
	modutil load mi-module
	modutil load ki-module
	modutil load elara-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	load_switch_module
	modutil load chassis-module
	modutil load condor2-module

#
# Load the anvil revision module to determine whether this platform has
# pass 1 or 2 chips loaded and load the corresponding anvil module.
#
	modutil load anvil_rev-module

	if [ -r /proc/driver/anvil_rev ]; then 
		anvil_rev=`cat /proc/driver/anvil_rev`
	else
		anvil_rev=0
	fi

	case $anvil_rev in
		1*) modutil load anvil_pass1-module
			;;

		2*) modutil load anvil-module
			;;

	 	*) echo "Unsupported Anvil version $anvil_rev, defaulting to 1.x";
			modutil load anvil_pass1-module
			;;
	esac

	rev=`echo $anvil_rev | gawk ' { print $1} '`
	major=`echo $rev | cut -d"." -f1`
	minor=`echo $rev | cut -d"." -f2`
	if [[( $major -eq 2 && $minor -ge 2 ) || ( $major -gt 2 )]]; then
		echo "Anvil version 2.2 or higher found";
		anvil_allports=1
	else
		echo "Anvil version 2.1 or less found";
		anvil_allports=0
	fi
	modutil load dce-blade-module dce_anvil_allports=$anvil_allports

	modutil load zeus-module
	modutil load fc-module
	modutil load fcoe-module
	modutil load routing-module
	modutil load altera-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}


#
# Mercury LC
#
swbd1004_module()
{
	$ECHO "Loading mercury LC modules "

	modutil load ki-module
	modutil load lc48x10g-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load aq1402-module
	modutil load cee-bp-module cee_blade_dbg_lvl=0xF2B
	modutil load iu-module
	modutil load hsl-module
	modutil load hawk-module
	modutil load wlv-module
	modutil load fcoe-agent-module
	modutil load dpci_switch-module
}

#
# Mercury LC
#
swbd1005_module()
{
	$ECHO "Loading mercury LC modules "

	modutil load ki-module
	modutil load lc12x40g-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load aq1402-module
	modutil load cee-bp-module cee_blade_dbg_lvl=0xF2B
	modutil load iu-module
	modutil load hsl-module
	modutil load hawk-module
	modutil load wlv-module
	modutil load fcoe-agent-module
	modutil load dpci_switch-module
}

#
# Mercury LC - 72-port blade
#
swbd1006_module()
{
	$ECHO "Loading mercury LC modules "

	modutil load ki-module
	modutil load lc72x1g-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load phy-module
	modutil load cee-bp-module cee_blade_dbg_lvl=0xF2B
	modutil load iu-module
	modutil load hsl-module
	modutil load hawk-module
	modutil load wlv-module
	modutil load fcoe-agent-module
	modutil load dpci_switch-module
}


swbd1007_module()
{
    $ECHO "Loading mercury LC modules "

    modutil load ki-module
    modutil load lc48x10g-module
    modutil load portlog-module
    modutil load fss-module
    modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
    modutil load aq1402-module
    modutil load cee-bp-module cee_blade_dbg_lvl=0xF2B
    modutil load iu-module
    modutil load hsl-module
    modutil load hawk-module
    modutil load wlv-module
    modutil load fcoe-agent-module
    modutil load dpci_switch-module
}

#
# Mercury LC - 36-port blade (Hawk2)
#
swbd1008_module()
{
    $ECHO "Loading mercury LC modules "

    modutil load ki-module
    modutil load lc36x10g-module
    modutil load portlog-module
    modutil load fss-module
    modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
    modutil load haml-module
    modutil load aq1402-module
    modutil load cee-bp-module cee_blade_dbg_lvl=0xF2B
    modutil load iu-module
    modutil load hsl-module
    if [ -f /etc/fabos/hawk2_fix ]; then
      modutil load hawk2-module hawk2_fix=0
    else
      modutil load hawk2-module hawk2_fix=1
	fi
    modutil load wlv-module
    modutil load fcoe-agent-module
    modutil load dpci_switch-module
}

#
# Mercury LC - 6-port blade (Hawk2)
#
swbd1009_module()
{
    $ECHO "Loading mercury LC modules "

    modutil load ki-module
    modutil load lc6x100g-module
    modutil load portlog-module
    modutil load fss-module
    modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
    modutil load haml-module
    modutil load aq1402-module
    modutil load cee-bp-module cee_blade_dbg_lvl=0xF2B
    modutil load iu-module
    modutil load hsl-module
    if [ -f /etc/fabos/hawk2_fix ]; then
      modutil load hawk2-module hawk2_fix=0
    else
      modutil load hawk2-module hawk2_fix=1
	fi
    modutil load wlv-module
    modutil load fcoe-agent-module
    modutil load dpci_switch-module
    load_cobra_module mmap_support=0 syslock_debug=$SYSLOCK_DEBUG
    #modutil load cbr_a-module
}

#
# Mercury LC - 6-port blade (Hawk2)
#
swbd1010_module()
{
    $ECHO "Loading mercury LC modules "

    modutil load ki-module
    modutil load lc27x40g-module
    modutil load portlog-module
    modutil load fss-module
    modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
    modutil load haml-module
    modutil load aq1402-module
    modutil load cee-bp-module cee_blade_dbg_lvl=0xF2B
    modutil load iu-module
    modutil load hsl-module
    if [ -f /etc/fabos/hawk2_fix ]; then
      modutil load hawk2-module hawk2_fix=0
    else
      modutil load hawk2-module hawk2_fix=1
	fi
    modutil load wlv-module
    modutil load fcoe-agent-module
    modutil load dpci_switch-module
}

#
# Mercury LC - 480x10gt-port blade (Hawk2)
#
swbd1011_module()
{
    $ECHO "Loading mercury LC modules "

    modutil load ki-module
    modutil load lc48x10gt-module
    modutil load portlog-module
    modutil load fss-module
    modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
    modutil load haml-module
    modutil load aq1402-module
    modutil load cee-bp-module cee_blade_dbg_lvl=0xF2B
    modutil load iu-module
    modutil load hsl-module
    if [ -f /etc/fabos/hawk2_fix ]; then
      modutil load hawk2-module hawk2_fix=0
    else
      modutil load hawk2-module hawk2_fix=1
	fi
    modutil load wlv-module
    modutil load fcoe-agent-module
    modutil load dpci_switch-module
}

#
# Frisco support
#
swbd90_module()
{
	modutil load frisco-module
	modutil load portlog-module
	modutil load ki-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load geye2-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	load_eanvil_module
	modutil load dce-blade-module

	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}

#
# Frisco-TTV support
#
swbd99_module()
{
	modutil load frisco_ttv-module
	modutil load portlog-module
	modutil load ki-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load geye2-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	load_eanvil_module
	modutil load dce-blade-module

	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}

#
# Elara2e support
#
swbd95_module()
{
	modutil load ki-module
	modutil load elara2f-module 
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG 
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	load_eanvil_module
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}


#
# Elara2F support
#
swbd96_module()
{
	modutil load ki-module
	modutil load elara2f-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module  sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load condor2-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	load_eanvil_module
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}

#
# CallistoE support
#
swbd97_module()
{
	modutil load ki-module
	modutil load callisto-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	load_eanvil_module
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}

#
# CallistoF support
#
swbd107_module()
{
	modutil load ki-module
	modutil load callisto-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG 
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load condor2-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	load_eanvil_module
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}

#
# CarmeE support
#
swbd116_module()
{
	modutil load ki-module
	modutil load carme-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	load_eanvil_module
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dmdiobus-module
	return 0
}

#
# Katana support
#
swbd112_module()
{
	modutil load ki-module
    modutil load katana-module 
 	modutil load portlog-module
 	modutil load fss-module
 	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
 	modutil load iu-module
 	modutil load hsl-module
 	modutil load trill-module
 	modutil load switch-module
 	modutil load chassis-module
 	modutil load fcoe-module
 	modutil load fcoe-agent-module
 	load_eanvil_module
 	modutil load dce-blade-module
 	modutil load fc-module
 	modutil load routing-module
	modutil load diag-module
 	modutil load fdpci-module
	return 0
}

#
# Harrier support
#
swbd123_module()
{
	modutil load ki-module
	modutil load harrier-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module 
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load geye2-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	load_eanvil_module
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	return 0
}
#
# Castor support
#
swbd131_module()
{
	modutil load ki-module
		modutil load castor-module
	if [ $? == 1 ];
		then return 0
	fi
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG 
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
    get_portmap

	cmodel_status
	cmodel_enable=$?
	if [ $cmodel_enable -eq 1 ]; then
		echo "Loading cmodel cobra driver, cmodel_enable=$cmodel_enable ........."
		load_cobra_mac_simulation_module cmodel_enable=$cmodel_enable
		modutil load dce-blade-sim-module
		mknod /dev/cbr_cdev c 500 0 1>/dev/null 2>/dev/null
		if [ $cmodel_enable -eq 1 ]; then
			get_cmodel_server
			cmodel_server=$?
			if [ $cmodel_server -eq 0 ]; then
				echo "Start cmodel server on switch" $CMS_SERVER
				cmodel_server="127.0.0.1"
			else
				cmodel_server=`cat /etc/fabos/cmodel_server`
			fi
			echo "Starting cmodel daemon "
			/fabos/cliexec/cmsd -s $cmodel_server 1>/dev/null 2>/dev/null&
		else
			echo "Cmodel daemon not enabled. Reg read/writes from Simulated register space."
		fi
	else
		load_cobra_module syslock_debug=$SYSLOCK_DEBUG
		modutil load dce-blade-module
	fi

	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
	return 0
}
#
# Castor-T support
#
swbd137_module()
{
	modutil load ki-module
	modutil load castort-module
	if [ $? == 1 ];
                then return 0
        fi
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG 
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
	
	cmodel_status
	cmodel_enable=$?
	if [ $cmodel_enable -eq 1 ]; then
		echo "Loading cmodel cobra driver, cmodel_enable=$cmodel_enable ........."
		load_cobra_mac_simulation_module cmodel_enable=$cmodel_enable
		modutil load dce-blade-sim-module
		mknod /dev/cbr_cdev c 500 0 1>/dev/null 2>/dev/null
		if [ $cmodel_enable -eq 1 ]; then
			get_cmodel_server
			cmodel_server=$?
			if [ $cmodel_server -eq 0 ]; then
				echo "Start cmodel server on switch" $CMS_SERVER
				cmodel_server="127.0.0.1"
			else
				cmodel_server=`cat /etc/fabos/cmodel_server`
			fi
			echo "Starting cmodel daemon "
			/fabos/cliexec/cmsd -s $cmodel_server 1>/dev/null 2>/dev/null&
		else
			echo "Cmodel daemon not enabled. Reg read/writes from Simulated register space."
		fi
	else
		load_cobra_module syslock_debug=$SYSLOCK_DEBUG
		modutil load dce-blade-module 
	fi

	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
	return 0
}

#
# Blackbird
#
swbd138_module()
{
	modutil load ki-module
	modutil load envstr-module
	modutil load blackbird-module
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
	load_cobra_module syslock_debug=$SYSLOCK_DEBUG
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
	return 0
}

#
# Blackbird
#
swbd138_module()
{
	modutil load ki-module
	modutil load envstr-module
	modutil load blackbird-module
	if [ $? == 1 ];
                then return 0
        fi
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
	load_cobra_module syslock_debug=$SYSLOCK_DEBUG
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
	return 0
}

#
# Draco-T support
#
swbd151_module()
{
	modutil load ki-module
	modutil load dracot-module
	if [ $? == 1 ];
                then return 0
        fi
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG 
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
	load_cobra_module syslock_debug=$SYSLOCK_DEBUG
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
	return 0
}

#
# Toucan
#
swbd155_module()
{
	modutil load ki-module
	modutil load envstr-module
	modutil load toucan-module
	if [ $? == 1 ];
		then return 0
	fi
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
	load_cobra_module syslock_debug=$SYSLOCK_DEBUG
	modutil load dce-blade-module
	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
        return 0
}

#
# Rigel2 support
#
swbd154_module()
{
	MYSWBD=`sin | swbd 2> ${NULL}`
	modutil load ki-module
	
	emulation_status
	if [ $? -eq 0 -o "$MYSWBD" = "SWBD154" ]; then
		modutil load rigel2-module
	else 
		modutil load rigel1_sim-module
	fi 
	
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG
	
	# Workaround to set air flow direction through bootenv variable
	airflowdir=`bootenv sysairflowdir 2> /dev/null`
	if [ -n "$airflowdir" ]; then
		if [ $airflowdir -eq 0 -o $airflowdir -eq 1 ]; then
			echo "Setting air flow direction (sysairflowdir) to $airflowdir"
			/fabos/bin/sysairflowdir $airflowdir
		else
			echo "ERROR: air flow direction value (sysairflowdir) incorrect: $airflowdir"
		fi
		# Remove sysairflowdir bootenv
		bootenv -u sysairflowdir
		sync
		# Reboot
		/bin/partman -r
	fi
	
	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
	get_portmap

	load_cobra2_rev_module
	emulation_status
	if [ $? -eq 1 ]; then
		echo "============================================"
		echo "RIGEL1 Blade Emulation Mode ON"
		echo "============================================"

		cmodel_status
		cmodel_enable=$?

		echo "Loading cobra-2 simulation driver, and mac chip driver"
		load_cobra2_mac_chip_module 
		if [ "$SIM_ASICLESS" = 'true' ]; then
			modutil load cbr2sim_a-module cmodel_enable=0xF
		else
			modutil load cbr2sim_a-module cmodel_enable=$cmodel_enable
		fi

		modutil load dce-blade-sim-module dce_sim_blade_idx=160

		mknod /dev/cbr_cdev c 500 0

		if [ $cmodel_enable -eq 1 ]; then
			echo "Starting cmodel daemon "
			get_cmodel_server
			cmodel_server=$?
			if [ $cmodel_server -eq 0 ]; then
				echo "Start cmodel server on switch" $CMS_SERVER
				cmodel_server="127.0.0.1"
			else
				cmodel_server=`cat /etc/fabos/cmodel_server`
			fi
			echo "Starting cmodel daemon "
			/fabos/cliexec/cmsd -s $cmodel_server 1>/dev/null 2>/dev/null&
		else
			echo "Cmodel daemon not enabled. Reg read/writes from Simulated register space."
		fi
	else
		load_cobra2_module

		pic_config
		pic_configuration=$?
		modutil load dce-blade-module pic_conf=$pic_configuration
	fi

	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
	return 0
}

#
# Rigel MOR support
#
swbd164_module()
{
	MYSWBD=`sin | swbd 2> ${NULL}`
	modutil load ki-module
	
	emulation_status
	if [ $? -eq 0 -o "$MYSWBD" = "SWBD164" ]; then
		modutil load rigel_mor-module
	else 
		modutil load rigel1_sim-module
	fi 
	
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG

	# Workaround to set air flow direction through bootenv variable
	airflowdir=`bootenv sysairflowdir 2> /dev/null`
	if [ -n "$airflowdir" ]; then
		if [ $airflowdir -eq 0 -o $airflowdir -eq 1 ]; then
			echo "Setting air flow direction (sysairflowdir) to $airflowdir"
			/fabos/bin/sysairflowdir $airflowdir
		else
			echo "ERROR: air flow direction value (sysairflowdir) incorrect: $airflowdir"
		fi
		# Remove sysairflowdir bootenv
		bootenv -u sysairflowdir
		sync
		# Reboot
		/bin/partman -r
	fi

	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
	get_portmap

	emulation_status
	if [ $? -eq 1 ]; then
		echo "============================================"
		echo "RIGEL MOR Blade Emulation Mode ON"
		echo "============================================"

		cmodel_status
		cmodel_enable=$?

		echo "Loading cobra-2 simulation driver, and mac chip driver"
		load_cobra2_mac_chip_module 
		if [ "$SIM_ASICLESS" = 'true' ]; then
			modutil load cbr2sim_a-module cmodel_enable=0xF
		else
			modutil load cbr2sim_a-module cmodel_enable=$cmodel_enable
		fi

		modutil load dce-blade-sim-module dce_sim_blade_idx=174

		mknod /dev/cbr_cdev c 500 0

		if [ $cmodel_enable -eq 1 ]; then
			echo "Starting cmodel daemon "
			get_cmodel_server
			cmodel_server=$?
			if [ $cmodel_server -eq 0 ]; then
				echo "Start cmodel server on switch" $CMS_SERVER
				cmodel_server="127.0.0.1"
			else
				cmodel_server=`cat /etc/fabos/cmodel_server`
			fi
			echo "Starting cmodel daemon "
			/fabos/cliexec/cmsd -s $cmodel_server 1>/dev/null 2>/dev/null&
		else
			echo "Cmodel daemon not enabled. Reg read/writes from Simulated register space."
		fi
	else
		load_cobra2_module

		pic_config
		pic_configuration=$?
		modutil load dce-blade-module pic_conf=$pic_configuration
	fi

	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
	return 0
}

#
# Rigel1 support
#
swbd153_module()
{
	MYSWBD=`sin | swbd 2> ${NULL}`
	modutil load ki-module
	
	emulation_status
	if [ $? -eq 0 -o "$MYSWBD" = "SWBD153" ]; then
		modutil load rigel1-module
	else 
		modutil load rigel1_sim-module
	fi 
	
	modutil load portlog-module
	modutil load fss-module
	modutil load fabsys-module sysdrv_config=$SYSDRV_CONFIG

	# Workaround to set air flow direction through bootenv variable
	airflowdir=`bootenv sysairflowdir 2> /dev/null`
	if [ -n "$airflowdir" ]; then
		if [ $airflowdir -eq 0 -o $airflowdir -eq 1 ]; then
			echo "Setting air flow direction (sysairflowdir) to $airflowdir"
			/fabos/bin/sysairflowdir $airflowdir
		else
			echo "ERROR: air flow direction value (sysairflowdir) incorrect: $airflowdir"
		fi
		# Remove sysairflowdir bootenv
		bootenv -u sysairflowdir
		sync
		# Reboot
		/bin/partman -r
	fi

	modutil load haml-module
	modutil load iu-module
	modutil load hsl-module
	modutil load trill-module
	modutil load switch-module
	modutil load chassis-module
	modutil load fcoe-module
	modutil load fcoe-agent-module
	modutil load aq1402-module
	get_portmap

	load_cobra2_rev_module
	emulation_status
	if [ $? -eq 1 ]; then
		echo "============================================"
		echo "RIGEL Blade Emulation Mode ON"
		echo "============================================"

		cmodel_status
		cmodel_enable=$?

		echo "Loading cobra-2 simulation driver, and mac chip driver"
		load_cobra2_mac_chip_module 
		if [ "$SIM_ASICLESS" = 'true' ]; then
			modutil load cbr2sim_a-module cmodel_enable=0xF
		else
			modutil load cbr2sim_a-module cmodel_enable=$cmodel_enable
		fi
		echo $EMUSWBD in swbd153_module
		if [ "$MYSWBD" = "SWBD153" -o "$EMUSWBD" = "SWBD153" ]; then
			$ECHO "Real Rigel Blade ID= 0x9F (159)"
			modutil load dce-blade-sim-module dce_sim_blade_idx=159
		else 
			modutil load dce-blade-sim-module dce_sim_blade_idx=1
		fi

		mknod /dev/cbr_cdev c 500 0

		if [ $cmodel_enable -eq 1 ]; then
			echo "Starting cmodel daemon "
			get_cmodel_server
			cmodel_server=$?
			if [ $cmodel_server -eq 0 ]; then
				echo "Start cmodel server on switch" $CMS_SERVER
				cmodel_server="127.0.0.1"
			else
				cmodel_server=`cat /etc/fabos/cmodel_server`
			fi
			echo "Starting cmodel daemon "
			/fabos/cliexec/cmsd -s $cmodel_server 1>/dev/null 2>/dev/null&
		else
			echo "Cmodel daemon not enabled. Reg read/writes from Simulated register space."
		fi
	else
		load_cobra2_module
		modutil load dce-blade-module
	fi

	modutil load fc-module
	modutil load routing-module
	modutil load diag-module
	modutil load fdpci-module
	modutil load dpci_switch-module
	return 0
}

#
# Create node for each device driver.
# mk_device <module name> {<device name> <minor-dev-num>}
#
mk_device()
{
	set $@
	node=$1
	shift 1
	major=`grep $node\$ /proc/devices | cut -d ' ' -f 1`
	if [ ! $major ]; then
		$ECHO "No major device for node [$node]. Exiting"
		exit 1;
	fi
	while [ $# -gt 0 ]; do
		rm -f /dev/$1
		mknod --mode=666 /dev/$1 c $major $2
		shift 2
	done
	return 0
}

#
# Create node for each block device driver.
# mk_block_device <module name> {<device name> <minor-dev-num>}
#
mk_block_device()
{
	set $@
	node=$1
	shift 1
	major=`grep $node\$ /proc/devices | cut -d ' ' -f 1`
	if [ ! $major ]; then
		exit 1;
	fi
	rm -f /dev/$1
	mknod --mode=666 /dev/$1 b $major $2

	return 0
}


make_devices()
{

	# the value of the minor for ki device (31) has to match with ki code!!
	mk_device ki ki 31 ki0 0 ki1 1 ki2 2 ki3 3 ki4 4 ki5 5 ki6 6 ki7 7 \
		ki8 8 ki9 9 kia 10 kib 11 kic 12 kid 13 kie 14 kif 15

	mk_device ham ham 0
	mk_device swd swd 0

	$ECHO "WORKAROUND: BYPASS ATA_DUMPER"

#	mk_device ata_dumper ata_dumper 0

	mk_device fss_mgmt fssm 0
	mk_device fss_data fssd 0

	case ${SWBD##SWBD} in
	'1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')
		mk_device fabsys fabsys 0
		mk_device portlog portlog 0 portlog0 0 portlog1 1
		;;
	*)
		mk_device fabsys fabsys 0
		mk_device portlog portlog 0 portlog0 0 portlog1 1

		mk_device fc fc 0 fc0 0 fc1 1 fc2 2 fc3 3 fc4 4 fc5 5 fc6 6 fc7 7
		mk_device switch switch 0 switch0 0 switch1 1 switch2 2 \
		switch3 3 switch4 4 switch5 5 switch6 6 switch7 7
		mk_device dioc dioc 0

		;;
	esac

	case ${SWBD##SWBD} in
	'1000' | '1001' | '1002' | '1003')
		mk_device fcoe fcoe 0
		mk_device esw_hsl esw_hsl 0
		mk_device xsfp_hsl xsfp_hsl 0
		mk_device bfd_hsl bfd_hsl 0
		mk_device dpci_pciehp dpci_pciehp.mod 0
		mk_device eswitch_phy eswitch_phy 0
		mk_device hv_module hvdev 0
		mk_device fm1 fm1 0
		mk_device fm1 fm1-pcd 1
		mk_device fm1 fm1-port-rx2 11
		mk_device fm1 fm1-port-tx2 17
		;;
	esac

	case ${SWBD##SWBD} in
	'1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011' | '131' | '137' | '138' | '151' | '153' | '154' | '155' | '164')
		mk_device dpci_switch-module dpci_switch-module.mod 0
		mk_device xsfp_hsl xsfp_hsl 0
		mk_device bfd_hsl bfd_hsl 0
		;;
	esac

	#
	# Create device for gearbox which only exist on 1009
	#
	case ${SWBD##SWBD} in
	'1009' | '154' | '164')
		mk_device gearbox gearbox 0
		;;
	esac

	#
	# Create device for AQ1402 for LC48x10T  151   1011
	#
	case ${SWBD##SWBD} in
	'1011')
		mk_device aq1402_phy_drv aq1402_phy_drv 0
		;;
	esac


        # FPGA SPI driver for MM & LC
#	case ${SWBD##SWBD} in
#	'1000' | '1001' | '1002' | '1004' | '1005' | '1006' | '1007' | '1008' |
#	'1009' | '1010' | '1011')
#		mk_device fpga_spi fpga_spi 0
#		;;
#	esac

	# Elara has Altera configuration device to program Zeus
	# Add diag node
	case ${SWBD##SWBD} in
	'76')
		mk_device altera altera 0
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		;;
	# Spear/Spearlite/Spike
	'79' | '80')
		mk_device enetswch enetswitch 0
		;;
	90)	# Frisco (SWBD90) is somewhat like Elara
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		mk_device enetswch enetswitch 0
		# For now making sure no edge port configuration is present to avoid any side effects
		#if [ -f /etc/fabos/dcf.conf ]; then
		#	cp /etc/fabos/dcf.conf.default /etc/fabos/dcf.conf
		#	$ECHO "Reverting back DCF configuration to default on VCS branch"
		#fi
		;;
	'99')
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		;;
	91) # Elara2
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		;;
	95) # Elara2E
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		# Making sure no configuration is present as IMI still replays it the old way, if present
#		if [ -f /etc/fabos/dcf.conf ]; then
#			cp /etc/fabos/dcf.conf.default /etc/fabos/dcf.conf
#			$ECHO "Reverting back DCF configuration to default on VCS branch"
#		fi
		;;
	96) # Elara2F
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		# Making sure no configuration is present as IMI still replays it the old way, if present
#		if [ -f /etc/fabos/dcf.conf ]; then
#			cp /etc/fabos/dcf.conf.default /etc/fabos/dcf.conf
#			$ECHO "Reverting back DCF configuration to default on VCS branch"
#		fi
		;;
	97) # CallistoE
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		# Making sure no configuration is present as IMI still replays it the old way, if present
#		if [ -f /etc/fabos/dcf.conf ]; then
#			cp /etc/fabos/dcf.conf.default /etc/fabos/dcf.conf
#			$ECHO "Reverting back DCF configuration to default on VCS branch"
#		fi
		;;
	107) # CallistoF
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
      ;;
	112) # Katana
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
      ;;
    116) # CarmeE
        mk_device fcoe fcoe 0
        mk_device fdpci fdpci 0
        mk_device esw_hsl esw_hsl 0
        mk_device dmdiobus-module dmdiobus-module 0
	;;
	123) # Harrier
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
      ;;
	'131') # Castor
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		board_rev=`cat /proc/system/fpga | grep HW | awk '{print $3}' | cut -c8`
		if [ $board_rev -lt 4 ]; then
			cp /etc/fabos/emd/castor.emdesc_p2 /etc/fabos/emd/castor.emdesc
		fi
	;;
	'137' | '151') # Castor-T
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		mk_device aq1402_phy_drv aq1402_phy_drv 0
		board_rev=`cat /proc/system/fpga | grep HW | awk '{print $3}' | cut -c8`
	;;
	'138' | '155') # Blackbird, Toucan
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		board_rev=`cat /proc/system/fpga | grep HW | awk '{print $3}' | cut -c8`
	;;
	'153'| '154' | '164' ) # Rigel Family
		mk_device fcoe fcoe 0
		mk_device fdpci fdpci 0
		mk_device esw_hsl esw_hsl 0
		MYSWBD=`sin | swbd 2> ${NULL}`
		emulation_status
		if [ $? -eq 1 -a ! "$MYSWBD" = "SWBD153" ]; then
			board_rev=`cat /proc/system/fpga | grep HW | awk '{print $3}' | cut -c8`
			if [ $board_rev -lt 4 ]; then
				cp /etc/fabos/emd/castor.emdesc_p2 /etc/fabos/emd/castor.emdesc
			fi
		fi
	;;
	esac
	
	case ${SWBD##SWBD} in
	'109')
		mk_device fdpci fdpci 0
		;;
	esac
}

set_location() {
	case ${SWBD##SWBD} in
	'1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')
		echo `/fabos/sbin/nin -l` > /proc/mi/6location
		;;
	'1000' | '1001' | '1002')
		echo "0" > /proc/mi/6location
		;;
	*)
		# whatever default makes sense...
		echo "0" > /proc/mi/6location
		;;
	esac
}

#
# this function will load the "panic dump" related modules
# These modules need to be loaded first and we have to call
# "pdcheck.sh" to check for a panic dump from a previous reboot
# if found we will save them in CF (it is all done in pdcheck.sh)
#
load_infra()
{

    modutil load raslog-module
    modutil load basic-module
	max_dump_sz_val=`/sbin/tracecfg -i ${SWBD##SWBD}`
	modutil load trace-module platform=${SWBD##SWBD} max_dump_sz=$max_dump_sz_val
    mk_device raslog raslog 0
    mk_device trace trace 0 trace1 1
    /sbin/tracecfg -w ${SWBD##SWBD} > /var/log/tracecfg.log
}

  
 # Load ZVM/KMD tracking module
 #
 
 load_modzvm() {
 if [ -s /fabos/modules/modzvm.ko ]; then
 	cat /proc/kallsyms | grep zvm &>/dev/null
 	if [ $? -eq 0 ]; then
 		insmod /fabos/modules/modzvm.ko  zvmk_console_errors=50 zvmk_config='ZVM_DRACE_ON=0'
 	fi
 fi
 }

#
# Load other common brocade modules
#
load_common() {
    	
	modutil load syslock-module syslock_debug=$SYSLOCK_DEBUG
	case ${SWBD##SWBD} in
	'1000' | '1001' | '1002' | '1003')
		modutil load mi-module eth_ifname="eth2"
		modutil load hv-module
		;;
	'95' | '96' | '97' | '107' | '112' | '116' | '123')
		modutil load mi-module
		;;
	'131' | '137' | '138' | '151' | '153' | '154' | '155' | '164')
		modutil load mi-module eth_ifname="eth1"
		;;
	'1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')
		modutil load mi-module eth_ifname="eth0"
		;;
	esac
	modutil load fablog-module mtracer_enable=$MTRACER_ENABLE $TRACKER
	modutil load xprt-module
	modutil load isc-module
	modutil load tp-module
	modutil load hasm-module
	#modutil load rscmon-module
	modutil load ctm-module
	modutil load ds-module
	mk_device isc_mgmt iscm 0
	mk_device isc_data iscd 0
	mk_device tp tp 0
	#mk_device rscmon rscmon 0
	mk_device ctm ctm 0
	mk_device ds ds 0
}

#
# load Brocade ECC module on platforms for which we have a module for that processor.
# for now just freescale 8548 based platforms.
#
load_brcdeccmod() {
	case ${SWBD##SWBD} in
	'91' | '95' | '96' | '99' | '97' | '107' | '112' | '116' | '123' | '131' | '137' | '138' | '151' | '153' | '154' | '155' | '164')
		insmod /lib/modules/default/kernel/drivers/brcdecc/brcdeccmod.ko
		;;
	esac
}


save_ethernet_ip() {

	INTERFACE="eth0"
    
    ip=`ifconfig $INTERFACE | grep inet | awk '{print $2}' | cut -d ":" -f 2 2> ${NULL}`
    mask=`ifconfig $INTERFACE | grep inet | awk '{print $4}' | cut -d ":" -f 2 2>${NULL}`
    gw=`route | grep default | awk '{print $2}' 2>${NULL}`
    
	MASK_NAME="netmask"
        
    IPADDR=`bootenv ipaddr 2> ${NULL}`
    GATEWAY=`bootenv gatewayip 2> ${NULL}`
    NETMASK=`bootenv $MASK_NAME 2> ${NULL}`
        
	if test "$ip" != "$IPADDR"; then
		if [ -n "$ip" ]; then
			bootenv ipaddr "$ip"
		fi
	fi

	if test "$mask" != "$NETMASK"; then
		if [ -n "$mask" ]; then
			bootenv $MASK_NAME "$mask"
		fi
	fi

	if test "$gw" != "$GATEWAY"; then
		if [ -n "$gw" ]; then
			bootenv gatewayip "$gw"
		fi
	fi

}

#
# bconoff
#
# Retrieve the flag from bootcount.file
#
bconoff() {
	sed -n -e 's/Check Off Fabos:\([[:digit:]]\{1,\}\).*$/\1/gp'
}

#
# bcsecs
#
# Retrieve the secs from bootcount.file
#
bcsecs() {
	sed -n -e 's/Seconds:\([[:digit:]]\{1,\}\).*$/\1/gp'
}

#
# bcboots
#
# Retrieve the secs from bootcount.file
#
bcboots() {
	sed -n -e 's/^Boo.\+unts:\([[:digit:]]\{1,\}\).*$/\1/gp'
}

#
# fips_status
#
# Check if FIPS is on or off
#
fips_status() {

	# File name of FIPS
	# Read the file /etc/fabos/pwron_fips_status to check if FIPS is on or off.
	# Absence of the file or a zero written to it means FIPS is off and we can
	# chkconfig fabos off, otherwise FIPS is enabled.

	FIPS_FILE=/etc/fabos/pwron_fips_status

	if [ -a $FIPS_FILE ]; then
		FIPS_FLAG=`cat $FIPS_FILE|sed -n -e 's/^\([[:digit:]]\{1,\}\).*$/\1/gp'`
		if [ $FIPS_FLAG -eq 0 ]; then
			FIPS=0
		else
			FIPS=1
		fi
	else
		FIPS=0
	fi
	return $FIPS
}


fwinstall_set_ip() {

	case ${SWBD##SWBD} in
	'91'|'95'|'96'|'97'|'99'|'107'|'112'|'116'|'123'|'131'|'137'|'138'|'151'|'153'|'154'|'155'|'164'|'1000'|'1001'|'1002'|'1004'|'1005'|'1006'|'1007'|'1008'|'1009'|'1010'|'1011')
		MASK_NAME="netmask"
		;;
	*)
		MASK_NAME="submask"
		;;
	esac

	IPADDR=`bootenv ipaddr 2> ${NULL}`
	GATEWAY=`bootenv gatewayip 2> ${NULL}`
	NETMASK=`bootenv $MASK_NAME 2> ${NULL}`

	if [ -z "$NETMASK" ]
	then
		$ECHO "No Network connection"
		return
	fi

	/sbin/ifconfig eth0 $IPADDR netmask $NETMASK
	/sbin/route add default gw $GATEWAY
}

handle_rrd_recovery() {
	#Script to perform Firmwarecleaninstall or Copy Support or Reboot
	/etc/init.d/rrd
}

#
# bootcount
#
# bootcount control
#
boot_count() {
	#
	# bootcount utility only runs if FIPS if off
	#
	fips_status
	if [ $? -eq 0 ]; then
		$ECHO "FIPS off, RRD..."
		if $CHECK rrd; then
			bootcount -p ${SWBD##SWBD} -f 1
		else
			bootcount -p ${SWBD##SWBD} -f 0
		fi

		# special handling if RRD is detected
		if [ $? -eq 1 ]; then
			gos_state=`cat /proc/device-tree/chosen/status 2> ${NULL}`
			if [ "$gos_state" = "active" ]; then
				fwinstall_set_ip
			fi
			handle_rrd_recovery
			exit
		fi
	fi
}

cleaninstall_default_config() {

	#
	# Set the default context
	#
	export FABOS_SWITCHNO=0
	export CURRENT_VF=128
	export CHASSIS_ROLEID=0
	export ROLE_ID=root

	PATH_FWDL_CONF_FILE="/etc/fwdl.conf"
	# get default parameters
	fwdl_conf_rbridge_id=""
	fwdl_conf_vcs_id=""
	fwdl_conf_vcs_mode=""
	fwdl_conf_retain_base=""
	
	args=""

	if [ -f $PATH_FWDL_CONF_FILE ]; then
		fwdl_conf_rbridge_id=`sed -n 's/rbridge-id : \([0-9]\+\)/\1/p' $PATH_FWDL_CONF_FILE`
		fwdl_conf_vcs_id=`sed -n 's/vcs-id : \([0-9]\+\)/\1/p' $PATH_FWDL_CONF_FILE`
		fwdl_conf_vcs_mode=`sed -n 's/vcs-mode : \(\-\?[0-9]\+\)/\1/p' $PATH_FWDL_CONF_FILE`
		fwdl_conf_retain_base=`sed -n 's/keep-baseconfig : \(\-\?[0-9]\+\)/\1/p' $PATH_FWDL_CONF_FILE`
	fi

	echo "parameter keep-baseconfig:$fwdl_conf_retain_license] " >> /mnt/var/log/fwdl.log;

	if [ -n "$fwdl_conf_retain_base" ]; then
		echo "Saving Licenses.."
		cp -f /etc/fabos/lic* /mnt/etc/fabos
		cp -f /etc/fabos/lservrc* /mnt/etc/fabos
	fi
	

	echo "parameter keep-baseconfig:$fwdl_conf_retain_license] " >> /mnt/var/log/fwdl.log;

	if [ -n "$fwdl_conf_retain_base" ]; then
		echo "Saving Licenses.."
		cp -f /etc/fabos/lic* /mnt/etc/fabos
		cp -f /etc/fabos/lservrc* /mnt/etc/fabos
	fi

	if [ -z $fwdl_conf_vcs_mode ] && [ -z $fwdl_conf_vcs_id ] && [ -z $fwdl_conf_rbridge_id ]
		then 
		echo "No default configs present"
		return 0;
	fi

	echo "parameter [vcs-id:$fwdl_conf_vcs_id] " >> /mnt/var/log/fwdl.log;
	echo "parameter [rbridge-id:$fwdl_conf_rbridge_id] " >> /mnt/var/log/fwdl.log;
	echo "parameter [vcs-mode:$fwdl_conf_vcs_mode] " >> /mnt/var/log/fwdl.log;
	
	if [ -n "$fwdl_conf_vcs_id" ]; then
		args="-v $fwdl_conf_vcs_id"
	fi
	if [ -n "$fwdl_conf_rbridge_id" ]; then
		args="$args -r $fwdl_conf_rbridge_id "
	fi
	if [ -n "$fwdl_conf_vcs_mode" ]; then
		args="$args -m $fwdl_conf_vcs_mode"
	fi
	
	comp_firmware_ver
	if [ $? -eq 2 ]; then
		echo "Running cfgdefault in primary partition."
	/sbin/cfgdefault -p 1 -l "/mnt/var/log/fwdl.log" -u 0 $args 	
	else
		echo "Running cfgdefault in secondary partition."
		/mnt/sbin/cfgdefault -p 1 -l "/mnt/var/log/fwdl.log" -u 0 $args
	fi

	sync;

}

do_install() {
	echo ""
	echo "Firmware is being installed, please wait ..."
	echo ""

	MASK_NAME="netmask"

	IPADDR=`bootenv ipaddr 2> ${NULL}`
	SERVERIP=`bootenv serverip 2> ${NULL}`
	GATEWAY=`bootenv gatewayip 2> ${NULL}`
	NETMASK=`bootenv $MASK_NAME 2> ${NULL}`
	bootenv ethact ENET0
	/sbin/ifconfig eth0 $IPADDR netmask $NETMASK
	/sbin/route add default gw $GATEWAY

	P_TYPE=`grep -c "scp" /var/tmp/tmp_clean.0`
	if [ $P_TYPE -eq 1 ]; then
		PROTOCOL="scp"
	else
		PROTOCOL="ftp"
	fi

	# Small delay introduced before invoking install script as the
	# ethernet module on some platforms takes time to come up.
	sleep 10

	# run install script
	/var/tmp/install -t clean -p $PROTOCOL -f /var/tmp/tmp_clean.0 release
	if [ $? -ne 0 ]; then
		bootenv -u Upgrade
		echo ""
		echo "FirmwareCleaninstall failed, you can reboot to retry."
		echo ""
		exit 3
	fi

	bootenv Upgrade cleancommit
	echo ""
	echo "FirmwareCleaninstall finished loading the 1st Partition, rebooting the switch..."
	echo ""
	/bin/rm /var/tmp/install

	if [ -f /etc/fwdl.conf ]; then
		cp -f /etc/fwdl.conf /mnt/etc/fwdl.conf	
	fi

	cleaninstall_default_config
	sleep 6

	case ${SWBD##SWBD} in
		'95' | '96' | '97' | '107' | '112' | '116' | '123')
			/sbin/reboot;;
		*)
			/bin/partman -r;;
    esac

	exit 3

}

do_cleancommit() {
	echo ""
	echo "Doing firmwarecleanInstall commit on second partition."
	echo ""

	FS='ext3'
	FSOPTS='-f'

	#format the second partition
	MNT=`mount | grep mnt | awk '{printf("%s", $1);} '`
	umount /mnt
	case "$FS" in
	"ext3")
		/sbin/mke2fs -g32768 -b4096 -j $MNT
		/sbin/tune2fs -c 0 -i 0 $MNT
		;;
	* )
		/sbin/mkfs -t $FS $FSOPTS $MNT
		;;
	esac

	mount -o noatime $MNT /mnt

	echo ""
	echo "Commit started on the second partition, please wait..."
	echo ""
	/fabos/cliexec/firmwarecommit -c 2> ${NULL}
	COMMIT_STATUS=$?
	/bin/mkdir -p /mnt/tmp
	/bin/mkdir -p /mnt/proc
	/bin/mkdir -p /mnt/mnt
	chown root:root /dev/null /dev/zero
	bootenv -u Upgrade
	if [ $COMMIT_STATUS -eq 0 ]; then
		echo ""
		echo "FirmwareCleaninstall commit completed successfully."
		echo ""
		echo "FirmwareCleaninstall will complete after the reboot."
		echo "Rebooting the switch ..."
		echo ""
		sleep 6

		case ${SWBD##SWBD} in
			'95' | '96' | '97' | '107' | '112' | '116' | '123')
               /sbin/reboot;;
            *)
                partman -r;;
        esac

		exit 3
	else
		echo "FirmwareCleaninstall commit failed. Please do \"firmwarecommit -c\" after doing a reboot or retry firmwareCleanInstall."
	fi

}

set_kthreadd_priority() {
	kthreadd_pid=`ps aux | grep -v grep | grep kthreadd | awk '{print $2}'`
	renice -19 -p $kthreadd_pid > ${NULL}
}
set_raslogkthread_priority() {
	RASLOGK_TH_pid=`ps aux | grep -v grep | grep RASLOGK_TH | awk '{print $2}'`
	renice -19 -p $RASLOGK_TH_pid > ${NULL}
}
handle_clean_install() {
	UPGRADE=`bootenv Upgrade 2> ${NULL}`
	if test "$UPGRADE" = "clean"; then
		do_install
	fi

	if test "$UPGRADE" = "cleancommit"; then
		do_cleancommit
	fi

	if test "$UPGRADE" = "cocleancommit"; then
		do_cleancommit
	fi
}

handle_default_op() {
	DEF_MODE=`bootenv default_mode 2> {NULL}`
	if test "$DEF_MODE" = "factory"; then
		do_factory_default
		/sbin/bootenv -u default_mode 2>&1 >/dev/null
	fi
}

# Code to set the limit on the size of the corefile
# 
setulimit(){
	set -- $(/bin/df -k ${1:-/core_files})
	typeset -i size=${11}
	if ((size > 4000000))
	then    ulimit -c unlimited
	elif ((size < 0))
	then    ulimit -c 0
	else    ulimit -c $((size*7/10))
	fi
}



TRC_FILE_COUNT=5
#
# Rotate specified trace files
#
rotate_trc_file() {

	if [ "$2" == "trc" ];then
		ext=.gz
	else
		ext=.od
	fi
	COUNT=$TRC_FILE_COUNT
	rm -rf /var/trace/trace$1*.trc$TRC_FILE_COUNT$ext 2> /dev/null
	rm -rf /var/trace/trace$1*.trc$TRC_FILE_COUNT 2> /dev/null

	while [ $COUNT -gt 0 ]
	do
		(( COUNT1 = $COUNT - 1 ))
		FILE=`/bin/ls /var/trace/trace$1*trc$COUNT1$ext* 2> /dev/null`
		DATE=`echo $FILE | cut -d '.' -f 2`
		/bin/mv $FILE /var/trace/trace$1.$DATE.trc${COUNT}$ext 2> /dev/null
		(( COUNT = $COUNT - 1 ))
	done
}

#
# Rotate all trace files
#
trace_file_rotation() {

	if [ -f /var/trace/tracepd.*.trc0.od ]; then
		rotate_trc_file pd
	fi
	if [ -f /var/trace/traceboot.*.trc0.od ]; then
		rotate_trc_file boot
	fi

}

#
# Power off external USB
#
usb_power_off() {
    usb_drv=$1
    if [ -e /sys/bus/usb/drivers/hub/${usb_drv} ]; then
        echo ${usb_drv} > /sys/bus/usb/drivers/hub/unbind
	fi
}

blackbird_set_eth_mac()
{
        BBD_ETH_LOG=/tmp/bbd_eth_log
        blackbird_part_state=`cat /proc/device-tree/chosen/status`
        # In nos6.0.0 each GOS of Blackbird will be exposed only one
        # management interface. Assign the interface with pre nos6.0.0
        # internal ethernet MAC as IBM CMM doesnt like MAC change.
        ETH2ADDR=`bootenv eth2addr 2> ${NULL}`
        ETH3ADDR=`bootenv eth3addr 2> ${NULL}`
        rb_reason=`cat /var/rb_reason`
        mnt_rb_reason=`cat /mnt/var/rb_reason`
        case $blackbird_part_state in
        active)
                echo "Blackbird - setting ethernet in active GOS... `/bin/date`" >> $BBD_ETH_LOG
                /sbin/ifconfig eth0 down >> $BBD_ETH_LOG
                /sbin/ifconfig eth0 hw ether $ETH2ADDR >> $BBD_ETH_LOG
                /sbin/ifconfig eth0 up >> $BBD_ETH_LOG
                ;;
        standby)
                echo "Blackbird - setting ethernet in standby GOS... `/bin/date`" >> $BBD_ETH_LOG
                /sbin/ifconfig eth0 down >> $BBD_ETH_LOG
                /sbin/ifconfig eth0 hw ether $ETH3ADDR >> $BBD_ETH_LOG
                /sbin/ifconfig eth0 up >> $BBD_ETH_LOG
                if [ "p" == p"${rb_reason}" -o "pReset" == p"${rb_reason}" ]; then
                    if [ "pFirmwareDownload" == p"${mnt_rb_reason}" ]; then
                        echo "Standby is not sleeping as the reboot is due to fwdl"
                    else
                        # If not fwdl reboot make blackbird standby GOS
                        # sleep for 3 minutes to enable quick active GOS bringup
                        echo "Standby sleeping for 3 minutes"
                        sleep 3m
                    fi
                fi
                ;;
        esac
}

swap_eth_ports()
{
        BBD_ETH_LOG=/tmp/bbd_eth_log
        eth0_a=eth0
        eth0_b=eth1
        eth0_temp=eth_bbd0
        ETHA_CHECK=`/sbin/ifconfig $eth0_a 2>/dev/null`
        ETHB_CHECK=`/sbin/ifconfig $eth0_b 2>/dev/null`
        touch $BBD_ETH_LOG
        if [ -r /proc/system/fpga ]
        then
                test_fix_det=0x`/bin/cat /proc/system/fpga | /fabos/link_bin/grep "CMM_special_1" | /bin/awk '{print $3}'`
                echo "TEST_FIXTURE_DET=$test_fix_det" >> $BBD_ETH_LOG
                let "test_fix_det&=1"
                if [ $test_fix_det -eq 0 ]
                then
                        if [ "$ETHA_CHECK" != "" ] && [ "$ETHB_CHECK" != "" ]
                        then
                                echo "In chassis: connected to backplane Ethernet port." >> $BBD_ETH_LOG
                                # change the backplane Ethernet port to eth0
                                ip link set dev $eth0_a down >/dev/null 2>&1
                                ip link set dev $eth0_b down >/dev/null 2>&1
                                ip link set dev $eth0_a name $eth0_temp >/dev/null 2>&1
                                ip link set dev $eth0_b name $eth0_a >/dev/null 2>&1
                                ip link set dev $eth0_temp name $eth0_b >/dev/null 2>&1
                                ip link set dev $eth0_a up >/dev/null 2>&1
                        else
                                echo "$eth0_a or $eth0_b doesn't exist" >> $BBD_ETH_LOG
                                /sbin/ifconfig -a >> $BBD_ETH_LOG
                        fi
                else
                        echo "In test I/O card: connected to the front Ethernet port." >> $BBD_ETH_LOG
                fi
        else
                echo "Error: fpga is not accessible." >> $BBD_ETH_LOG
        fi
}


#
# Handle jitc
# In dcmd, it's handle in IpadmLocalObjectManager.cpp
#
handle_jitc() {
	if [ -f /etc/icmp.conf ]; then
		val=(`cat /etc/icmp.conf`)
		if [ ${#val[@]} -eq 6 ]; then
			$ECHO init icmp ${val[@]}
			echo ${val[0]} > /proc/sys/net/ipv4/conf/eth0/ip_unreachable
			echo ${val[1]} > /proc/sys/net/ipv4/conf/eth0/icmp_echo_ignore
			echo ${val[2]} > /proc/sys/net/ipv4/conf/eth0/icmp_rlimit_enable
			if [ ${val[2]} -gt 0 ] && [ 1000 -ne ${val[2]} ]; then
				echo $[0x7f939] > /proc/sys/net/ipv4/icmp_ratemask
			else
				echo 6169 > /proc/sys/net/ipv4/icmp_ratemask
			fi
			echo ${val[3]} > /proc/sys/net/ipv6/conf/eth0/ipv6_unreach
			echo ${val[4]} > /proc/sys/net/ipv6/conf/eth0/echo_ignore
			echo ${val[5]} > /proc/sys/net/ipv6/icmp/ratelimit
		fi
	else
		# File is not present. Could be a clean-up case. From NOS5.0.1x,
		# Icmp Unreachable is disabled by default.
		echo 0 > /proc/sys/net/ipv4/conf/eth0/ip_unreachable
		echo 0 > /proc/sys/net/ipv6/conf/eth0/ipv6_unreach
	fi
	if [ -f /etc/tcpburstrate.conf ]; then
		val=(`cat /etc/tcpburstrate.conf`)
		if [ ${#val[@]} -eq 2 ]; then
			$ECHO init tcp burstrate ${val[@]}
			echo ${val[0]} > /proc/sys/net/ipv4/tcp_rate_limit
			echo ${val[1]} > /proc/sys/net/ipv4/tcp_rate_limit_drop_time
		fi
	fi
	[ -f /etc/fabos/jitc.conf ] && /fabos/bin/jitccfg.sh
}

#
# Handle icmp
#
handle_icmp() {
	if [ -f /etc/icmp.conf ]; then
		val=(`cat /etc/icmp.conf`)
		if [ ${#val[@]} -eq 6 ]; then
			$ECHO init icmp ${val[@]}
			echo ${val[0]} > /proc/sys/net/ipv4/conf/eth0/ip_unreachable
			echo ${val[1]} > /proc/sys/net/ipv4/conf/eth0/icmp_echo_ignore
			echo ${val[2]} > /proc/sys/net/ipv4/conf/eth0/icmp_rlimit_enable
			if [ ${val[2]} -gt 0 ]; then
				echo $[0x7f939] > /proc/sys/net/ipv4/icmp_ratemask
			else
				echo 0 > /proc/sys/net/ipv4/icmp_ratemask
			fi
			echo ${val[3]} > /proc/sys/net/ipv6/conf/eth0/ipv6_unreach
			echo ${val[4]} > /proc/sys/net/ipv6/conf/eth0/echo_ignore
			echo ${val[5]} > /proc/sys/net/ipv6/icmp/ratelimit
		fi
	fi
}

start_srmd() {

	$ECHO Timed execution daemon 

	if [ -x /fabos/libexec/sysrmd ]; then
		/fabos/libexec/sysrmd &
		$ECHO sysrmd
	fi
	$ECHO started.
}

copy_dcmd_conf_files() {

        # Persist cluster.conf file and vcs.conf from previous partition in case of firmware upgrade
        if [ -f /etc/fabos/Dcmd/cluster.conf.fwdl ]
        then
            echo "Cluster Conf file copy ... `/bin/date`"
            cp -f /mnt/etc/fabos/Dcmd/cluster.conf /etc/fabos/Dcmd/cluster.conf
            rm -rf /etc/fabos/Dcmd/cluster.conf.fwdl
            echo "FABOS : cluster.conf = "`cat /etc/fabos/Dcmd/cluster.conf` >> /etc/fabos/Dcmd/fwdl_db_copy_log
        fi

		if [ -f /etc/fabos/Dcmd/vcs.conf.fwdl ]
        then
            echo "VCS Conf file copy ... `/bin/date`"
			cp -f /mnt/etc/fabos/Dcmd/vcs.conf /etc/fabos/Dcmd/vcs.conf

            echo "Dcmd Global Conf file copy ... `/bin/date`"
            dcm_handle_global_conf_file

            # check mode after vcs.conf is copied from secondary partition to first partition
            dcm_mode=$(get_dcm_mode)

            if [ $dcm_mode -eq 0 ]
            then
                cp -f /etc/fabos/Dcmd/vcs.conf.fwdl /etc/fabos/Ccmd/vcs.conf.fwdl.ccmd
                # remove vcs.conf.fwdl in dcmd.sh 
                # remove vcs.conf.fwdl.ccmd in ccmd.sh
            fi
        fi
		sync; sleep 1; sync ; sync
}

#
# main
#
case "$1" in
'start')
	##################################################################
	DEBUG=0
	if [ $DEBUG = 1 ]; then
    	ECHO='echo -e'
	else
    	ECHO=:
	fi
	export ECHO

	if $CHECK ald; then
	    $ECHO -n -e "LAUNCH ALD ..."
	    /fabos/libexec/ald &
	fi

	if [ "$FABOS_START" = 'off' ]; then
		$ECHO -n -e "##### NOS services switched off from bootprompt #####\n"
		exit 1;
	fi

	handle_clean_install
	handle_default_op
	handle_jitc
	# defect 469812, temperory disable icmp, wait for capricon rebased with leo patch
	# handle_icmp

	$ECHO -n "### Starting NOS Services... SWBD ${SWBD##SWBD} ### "
	$ECHO `/bin/date`

	set_kthreadd_priority

	case ${SWBD##SWBD} in
	1000 | 1001 | 1002 | 154 | 164)
        usb_power_off 1-0:1.0
		;;
    131 | 137 | 138 | 151 | 153| 155)
        if [ -d /nfabos/bin ]; then
            $ECHO -n "### Keep usb power on for NFDiag ### "

            # disable hotplug script to preserves sdb & sdb1
            # device file and keeps usb controller enable
            # when usb device is removed.
            echo /dev/null > /proc/sys/kernel/hotplug
        else
            usb_power_off 2-0:1.0
        fi
        ;;
    *)
    esac

	# use the real SWBD not emulator SWBD for this
	SWBD_SAVE=${SWBD}
	SWBD=`sin | swbd 2> ${NULL}`
	case ${SWBD##SWBD} in
	'1000' | '1001' | '1002' | '1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011' | '131' | '137' | '138' | '151' | '153' | '154' | '155' | '164')
		free_space=`/bin/df /| grep -v Filesystem | awk '{print $4}'`
		if [ $free_space -gt 358400 ];then
		trace_file_rotation
		fi

		# Get wmd_disable environmet variable
		WMD_DISABLE=`bootenv wmd_disable 2> ${NULL}`
		if [ -z $WMD_DISABLE ]; then
			WMD_DISABLE=0
		fi

		#echo "Loading Warm Memory Dumper module"
		modutil load wmdumper-module wmd_disable=$WMD_DISABLE

		#echo "Warm Memory Dumper module loaded"
		mk_device wm_dumper wm_dumper 0
		;;
	esac
	SWBD=${SWBD_SAVE}

	if $CHECK fabos; then
	load_modzvm
		# load basic-module before any other modules are loaded
		# (pd modules)

                # Needs to happen before basic-module is loaded
                # In case the file was created with wrong permission, remove it. 
                # delivered to external GA, should be removed.
                /bin/rm -f /etc/fabos/tb_ticks_per_jiffy
                load_infra
		set_raslogkthread_priority

                case ${SWBD##SWBD} in
		'1000' | '1001' | '1002' | '1003')
			# reboot needed in case of panic to make PCI devices
			# sane on MM
			if [ -e /tmp/rb_reason.alt ]; then
				grep -i Fault /tmp/rb_reason.alt 1>${NULL} 2>&1
				if [ $? -eq 0 ]; then
					echo "Panic Dump Finished - Reset MM"
					/bin/partman -r
				fi
			fi
			;;
		'131' | '137' | '138' | '151' | '153' | '154' | '155' | '164')
			if [ -e /tmp/rb_reason.alt ]; then
				grep -i Fault /tmp/rb_reason.alt 1>${NULL} 2>&1
				if [ $? -eq 0 ]; then
					echo "Panic Dump Finished"
					if [ $MODE = "0x0" ]; then
						echo "Reset system on active"
						/bin/partman -r
					fi
				fi
			fi
			;;
		*)
			;;
                esac
        fi

	case ${SWBD##SWBD} in
		36|47|50|52|55|57|68|79|80|81|85|88|89|1004|1005|1006|1007|1008|1009|1010|1011)
		;;
		*)
		boot_count
		;;
	esac

	if $CHECK fabos; then

		# pass this to fablog module
		if $CHECK mtracer; then
			MTRACER_ENABLE=1
		else
			MTRACER_ENABLE=0
		fi

		load_common

		# Trig LoadCommon for ald to complet trace_init and launch app
		/fabos/sbin/alt -iLoadCommon

		# load_brcdeccmod
		# if ECC module was loaded, start up the ECC reporting daemon
		if [ -f /proc/driver/ecc/model ]; then
			/fabos/libexec/eccd
		fi

		# Set-up diagnostics
		rm -rf /diag; ln -s /proc/diag /diag
		# Set the limit on the core file size
		setulimit

		# Initialize configuration bitmap passed to system driver.
		# The following configurations are supported. This bitmap definition
		# must match what is used in sys/include/fabos/fabsys.h

		SYSDRV_CONFIG=0
		SYS_CFG_HA_ENABLED=0x0
		SYS_CFG_VCS_ENABLED=0x2

		# Enable HA for DCE services based on whether or not
		# /etc/fabos/dce_ha/dceha.conf exists.
		# For discovery release disable HA always
		# For eureka HA enable HA always
		if [ -f /etc/fabos/dce_ha/dceha.conf ]; then
			SYSDRV_CONFIG=$(($SYSDRV_CONFIG | $SYS_CFG_HA_ENABLED))
		fi

		copy_dcmd_conf_files
		MCMODE=$(get_dcm_mode)
		echo " DCM MODE:$MODE"

		# configure fablog tracker
		if [ -f /etc/tracker.cfg ]; then
			. /etc/tracker.cfg
		fi

        echo "SWBD module launch ... `/bin/date`"
		swbd"${SWBD##SWBD}"_module
		if [ $? -ne 0 ]; then
			unsupported ${SWBD##SWBD} 1
		fi

                case ${SWBD##SWBD} in
                '138')
                    # ??? TODO for Toucan: blackbird_set_eth_mac
                      ;;
                    *)
                      ;;
                esac

		# Determine the system platform identifier.
		CPID=`sin | cpid 2> ${NULL}`
		CHASS=`sin -c| chassbd 2> ${NULL}`

		CPID=`sin | cpid 2> ${NULL}`


		make_devices

 		update_fabsys_with_dcm_config

		# Let us log errors that are saved earlier and log them
		# Map /var/panicdump
		/fabos/bin/pdcheck.sh
		/fabos/bin/pdcheck_errs.sh

		set_location
		CHASS=`sin -c| chassbd 2> ${NULL}`
		# Get the switch configuration based on the platform
		/fabos/sbin/nin -p $PARTITION # push partition#
		LNID=`/fabos/sbin/nin -n`

		case ${SWBD##SWBD} in
		'1004' | '98' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')
			CHASS=`sin -c| chassbd 2> ${NULL}`
			;;
		*)
			/fabos/libexec/ipadmd -d3
			/fabos/libexec/telnetmond

			# start dhcp server
			if [ -f /etc/startdhcpd ]; then
				/fabos/libexec/dhcpdctl.sh start
			fi

            # Create the hwrng device
			node="hwrng"
	                # note that the usual (cut -d ' ' -f 1) doesn't work for
	                # misc since it has leading blanks
	                major=`grep $node\$ /proc/devices | cut -c 1-3`

	                if [ $major ]; then
		        # found it now create the hwrng node (minor = 183)
		        rm -f /dev/$node
		        mknod --mode=644 /dev/$node c $major 183
	                fi
			;;
		esac

		# Setup virtual network interfaces for Frisco platform
		case ${SWBD##SWBD} in
		'90')
			# Do this only for chassis based frisco
			inFixture=`cat /proc/system/enclosure | head -1 | cut -d' ' -f2`
			if [[ $inFixture != "-1" ]]; then
				/sbin/vconfig add eth0 4095
				sleep 1
				ifconfig eth0 down
				sleep 1
				# Rename it as eth0_raw..
				ip link set dev eth0 name eth0_raw
				ip link set dev eth0.4095 name eth0
				# Bring up the original eth0 interface
				ifconfig eth0_raw up
			fi
			modutil load enetswch-module eth0name="eth0_raw"
			;;
		esac

#
# Determine if PCIe error IRQ should be enabled or not
#
		case ${SWBD##SWBD} in
		'1000' | '1001')
			if $CHECK pcieirq; then
				echo "Enable PCIe error IRQ"
				echo 1 > /proc/cp*/pcie/enable
			else
				echo "Disable PCIe error IRQ"
				echo 0 > /proc/cp*/pcie/enable
			fi
			# limit mi6 bandwidth usage to avoid
			# dropped fragments in the switch
			# between 10 gbps and 1 gbps
			echo "5000 5000" > /proc/mi/6bandwidth
			;;
		*)
			;;
		esac

		case ${SWBD##SWBD} in
		'1004' | '1005' | '1006' | '1008' | '1009' | '1010' | '1011')
			if $CHECK pcieirq; then
				echo "Enable PCIe error IRQ"
				echo 1 > /proc/system/pcie/enable
			else
				echo "Disable PCIe error IRQ"
				echo 0 > /proc/system/pcie/enable
			fi
			;;
		*)
			;;
		esac

        #Enable NOS DEBUG if configured
        /fabos/bin/nos_debug -v
		
		sys_airflow_change_info

		echo "Starting HASM ... `/bin/date`"
		if [ "$MODE" != "0x0" ]; then
			bootenv -u startup_mode
		fi
		HASMD="nice -n -10 hasmd "
		case ${SWBD##SWBD} in
		'1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')
			$ECHO -n "LAUNCH HASMD ${SWBD##SWBD} ${MODE} ${LNID}"
			$HASMD -p ${SWBD##SWBD} -d ${MODE} -n ${LNID} &
                        silicond &
			;;
		'1000' | '1001' | '1002' | '1003')
			$ECHO -n "LAUNCH HASMD ${SWBD##SWBD} ${MODE} ${LNID}"
			$HASMD -p ${SWBD##SWBD} -d ${MODE} -n ${LNID} -m ${MCMODE} &
			silicond &
			;;
		'154')
			# Temporary Check and setting LNID to 0 ------>>> TBD
			echo -n "LAUNCH HASMD ${SWBD##SWBD} ${MODE} ${LNID}"
			$HASMD -p ${SWBD##SWBD} -d ${MODE} -n 0 -m ${MCMODE} &
			;;
		'131' | '137' | '138' | '151' | '153' | '154' | '155' | '164')
			echo -n "LAUNCH HASMD ${SWBD##SWBD} ${MODE} ${LNID}"
			$HASMD -p ${SWBD##SWBD} -d ${MODE} -n ${LNID} -m ${MCMODE} &
			;;
		'95'| '96' | '99' | '97' | '107' | '112' | '116' | '123')
		   $HASMD -p ${SWBD##SWBD} -d ${MODE} -m ${MCMODE} &
		   rm -f /etc/fabos/port2BoundAreaMap.0
		     ;;
		'90' | '92')
			#
			# If superd config file is not present,
			# by default load superd. In stealth platform,
			# this is the case with release.plist where we
			# won't install daemons already part of superd
			# to reduce flash usage

			if [ ! -f '/etc/config/superd' ] ; then
				superd ${SWBD} $XPT ${MODE} ${LNID} &
			elif $CHECK superd ; then
				superd ${SWBD} $XPT ${MODE} ${LNID} &
			else
				$HASMD -p ${SWBD##SWBD} -d ${MODE} -n ${LNID} &
			fi
			;;
		*)
			if $CHECK superd ; then
				superd ${SWBD} $XPT ${MODE} ${LNID} &
			else
				$HASMD -p ${SWBD##SWBD} -d ${MODE} -n ${LNID} &
			fi
				;;
		esac

        #'1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')
		case ${SWBD##SWBD} in
		'1000' | '1001' | '1002' | '1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')

		# Upload any PD_MISC parameters in kernel that can be obtained
		# _AFTER_ the fabos is loaded full and running
		/fabos/bin/pdul_misc_post.sh &

		# set debug (maintained until GA) (Talk to Hor-May Chen)
		doSetDbg

		# set up flow control for the console
		;;
		*)
			;;
		esac

		# spmd, iked - ipsec
		status=`grep stat /etc/racoon2/sadb.conf 2> /dev/null | cut -d ' ' -f 2`
			SPMD=/sbin/spmd
		if [ "$status" != "" ] ; then
			if [ "$status" == "on" ]; then
			IKED=/sbin/iked
			IPSCFG=/etc/racoon2/racoon2.conf
			if test -x $SPMD; then
				$SPMD -f $IPSCFG > /dev/null
				sleep 2
				if test -x $IKED; then
					$IKED -f $IPSCFG > /dev/null
				fi
			fi
			else
				$SPMD -k
			fi
		fi

    case ${SWBD##SWBD} in
    '98' | '1004' | '1005' | '1006' | '1007' | '1008' | '1009' | '1010' | '1011')
        # Do not change bootenv for LC/BP
        ;;
    *)
        save_ethernet_ip
        ;;
    esac



	fi

	if [ -e /dev/ata_dumper ];then
		if [ -e /var/tracedump.dmp ];then
			/sbin/tracedecode -b 0 /var/tracedump.dmp
			if [ $? -eq 1 ];then
                free_space=`/bin/df /| grep -v Filesystem | awk '{print $4}'`
                if [ $free_space -gt 358400 ];then
				rotate_trc_file pd trc
				date_str=`/bin/date +"%Y%m%d%H%M"`
				/bin/cp /var/tracedump.dmp /var/trace/tracepd.$date_str.trc0
				if [ $? -eq 0 ];then
					/fabos/cliexec/errlogtest -i TRCE-5021 -a $date_str
				fi
				/bin/gzip /var/trace/tracepd.$date_str.trc0
				/sbin/tracedecode -b 1 /var/tracedump.dmp
			fi
		fi
        fi
	fi

	case ${SWBD##SWBD} in 
	'1000' | '1001' | '95' | '96' | '97' | '107' | '112' | '116' | '123' | '131' | '137' | '138' | '151' | '153' | '154' | '155' | '164')
		#Some sendmail tweaks
		#Clean the queue dir
		rm -rf /var/spool/clientmqueue/* &> /dev/null
		#Link the queue dir to var spool
		rm -fr /tmp/spool &> /dev/null
		ln -sf /var/spool /tmp/ &> /dev/null
#		if $CHECK sysrmd; then
#			start_srmd
#		fi
	esac	

	# Start FMC only if rate limit is enabled. 
    case ${SWBD##SWBD} in
    '1000'| '1001'| '1002'| '1003' )
		# It is applicable only for MMs of Mercury.
		if [ -f /etc/fmc/config/.rate_limit_enable ]; then
			fmc -c /etc/fmc/config/config.xml -p /etc/fmc/config/policy.xml -a
		fi
		;;
	*)
		;;
	esac

	case ${SWBD##SWBD} in
		'1000' | '1001' | '95' | '96' | '97' | '107' | '112' | '116' | '123' | '131' | '137' | '138' | '151' | '153' | '154' | '155' | '164')
		ln -sf /proc/self/fd /dev/fd
	esac

	$ECHO "."
	;;

'stop')
	$ECHO "Stopping Fabric OS services..."

	# stop swd and heartbeat
	/fabos/cliexec/hashutdown > ${NULL} 2>&1

	#stop uptime clock for both switches
	/fabos/cliexec/switchuptime stopall > ${NULL} 2>&1

	# stop system services first
	SYSDAEMONS='hasmd'
	${KILLALL} -TERM $SYSDAEMONS > ${NULL} 2>&1
	${KILLALL} $SYSDAEMONS > ${NULL} 2>&1

	# stop all the switch based daemons
	stop_daemons

	# stop all chassis based daemons
	DAEMONS='hmond proxy diagd porttestd logind'
	${KILLALL} -TERM $DAEMONS > ${NULL} 2>&1
	${KILLALL} $DAEMONS > ${NULL} 2>&1

	# stop spmd iked (ipsec)
	DAEMONS='spmd iked'
	${KILLALL} -TERM $DAEMONS > ${NULL} 2>&1
	${KILLALL} $DAEMONS > ${NULL} 2>&1

#	${KILLALL} srmd

	# Stop main CP- coCP shared service
	case ${SWBD##SWBD} in
	'63')
		/bin/umount /etc/fabos
		;;
	esac


	# Certain platforms need to insure portside ASICs stopped on reboot
	case ${SWBD##SWBD} in
	'46')
		# Sprint and Sabre CP side needs to reboot BP side
		(rsh 127.1.10.1 reboot -f &)
		sleep 2
		;;

	*)
		;;
	esac
	$ECHO "Done."
	;;

*)
	echo "Usage: $0 {start | stop}"
	;;

esac
