#!/bin/sh
#
#
#    Copyright (c) 1996-2006 Brocade Communications Systems, Inc.
#    All rights reserved.
#
# 
# NAME
#      setlabmode - set or display diagnostic labmode
# 
# SYNOPSIS
#      setlabmode [ mode | -show ]
# 
# AVAILABILITY
#      factory
# 
# DESCRIPTION
#      This  command  enables  lab mode if the mode operand is non-
#      zero and disables the lab mode if the mode operand is 0. The
#      mode  is  saved in flash memory and stays in that mode until
#      the next execution of setlabmode.
# 
#      The mode becomes active as soon as this command is executed.
#      It  does  not require a reboot to take effect except for the
#      EFD and HA related operations  which  do  require  a  reboot
#      since they can not be changed on the fly.
# 
#      Lab mode when enabled modifies the behavior of the switch so
#      that various automatic failure detection and recovery mecha-
#      nisms  are bypassed.  For instance the switch will not fault
#      blades when hardware failures are detected, or automatically
#      reboot  when  a  fatal software error occurs.  This makes it
#      easier to determine the cause of the errors, but should  not
#      be considered a substitute for correcting the original fail-
#      ure.  After a suppressed  error  event  occurs  it  is  very
#      likely  that  the  switch will no longer be able to continue
#      normal operation.
# 
#      (WARNING) Your  use  of  the  functionality  made  available
#      through the root or factory account is at your sole risk and
#      you assume all liability resulting from such use.
# 
# OPTIONS
#      mode      Specify the value of lab mode. 0 means to  disable
#                lab mode, in other words, enable normal user oper-
#                ation. Any other value will be  interpreted  as  a
#                bit  mask  to  enable various lab mode features as
#                follows:
# 
#                0x0  Normal user operation.
# 
#                0x1  Backwards  compatible  mode.   Will  suppress
#                     blade  faulting,  software  Panic, and any HA
#                     related reboots such as  SWD  and  Heartbeat.
#                     Setting  this  bit  is  equivalent to setting
#                     bits 0x5C.
# 
#                0x2  Hold switch in POST2.  No effect if  Post  is
#                     disabled.  If post is enabled then it will be
#                     skipped, but the blades will be held in POST2
#                     state for manual debug.  The default behavior
#                     when this bit is not set is to  fault  blades
#                     if  POST  fails  or  the backend ports do not
#                     come online.  In order to manually run  diag-
#                     nostics  to  determine  the  cause of a blade
#                     failure you will need to set this bit.
# 
#                0x4  Suppress blade faulting  by  kernel  modules.
#                     This  will  stop for instance TX parity error
#                     induced faulting of bad blades.
# 
#                0x8  Suppress Panic reboot.  When  set  this  will
#                     disable  the  reboot  following  an ASSERT or
#                     other fatal software error.
# 
#                0x10 Enable special ASIC driver debug mode.   When
#                     this  bit is set the ASIC driver will display
#                     additional diagnostic information  when  cer-
#                     tain ASIC hardware failures are detected.
# 
#                0x20 Suppress  EFD  mode  checking.  When set this
#                     bit will disable the Enhanced Failure  Detec-
#                     tion  mechanisms built into the ASIC hardware
#                     and driver code.  The EFD setting can not  be
#                     changed  on the fly so this mode will require
#                     a reboot to take effect.
# 
#                0x40 Suppress HA features.  When set this bit will
#                     disable  the  SWD, HWD, and fail-over HA fea-
#                     tures.  The  watchdog  settings  can  not  be
#                     changed  once  they  are enabled so this mode
#                     will require a reboot to take effect.
# 
#      -show     If specified or no mode is given,the  current  lab
#                mode will be displayed.
# 
# EXAMPLES
#      > setlabmode 0x28
#      Config commit Succeeded
#      Lab Mode is now 40.
# 
# NOTES
#      Switches  in  the  field  should never be left with lab mode
#      enabled or the ability to automatically recover from  errors
#      will be compromised.
# 

# Check RBAC permission on command


/fabos/libexec/rbac_check `/bin/basename $0`

if [ $? -ne 0 ]; then
	exit 127
fi

#
#
# Load library -- must be first.
#

home="/fabos/share"
util="diagcommon.sh"
ok=0
for f in "./$util" "$FABOSHOME/share/$util" "$home/$util" ; do
	if [ -r $f ] ; then
		. $f
		ok=1
		break;
	fi
done
if [ $ok -ne 1 ] ; then
	echo "Error -- could not locate $util"
	exit 3
fi

#
# Program customization
#

config_string="diag.mode.lab"	# config string to update
config_name="Lab Mode"		# User name of config.
config_default=0		# default value
config_mode=$INTEGER		# config mode

syntax="`/bin/basename $0` [ mode \| -show ]" ; export syntax

#
# setlabmode()
#
checkForShow "$config_name" "$config_string" "$config_mode" "$config_default" $1

# Force value to decimal for comparison.
val=`printf "%d" $1 2>/dev/null`
if [ $? != 0 ]
then 
    err "Invalid number"
    exit 3
fi

cur_val=`getConfig $config_string $config_mode $config_default`
if [ "$cur_val" != "$val" ]
then
    if [ `isMultiBlade` = TRUE ] ; then

	# To figure out if we are on standby CP or active one, check
	# "switchEnable" command is in link directory.
	#
	# [note] setlabmode must be callable before fabos loaded so here
	#        we need to use primitive way.
	if [ ! -f "/fabos/link_sbin/switchEnable" ] ; then
            echo "$config_name cannot be changed on standby CP."
            exit 1
        fi
    fi

    num_switches=`getNumSwitches`
    switch=0
    while [ $switch -lt $num_switches ]
    do
	if [ $num_switches -gt 1 ]
	then
	    switch_msg=" on switch $switch"
	else
	    switch_msg=""
	fi
	export FABOS_SWITCHNO=$switch
	setConfig $config_string $config_mode $val
	commitConfig
	echo "$config_name is now $1$switch_msg."
	switch=`expr $switch + 1`
    done
else
    echo "$config_name is still $1$switch_msg."
fi

exit 0
