#!/bin/sh
#
#
#    Copyright (c) 1996-2013 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      bpflashfix - Fix Compact Flash Anchor block
# 
# SYNOPSIS
#      bpflashfix slotno
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This command will fix the corruption in Anchor block of WD CF 3625 model.
#

# cpid
#
# Retrieve the current Control Processor ID
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin
SLOT_NO=$1
NULL=/dev/null

cpid() {
    sed -n -e 's/^Control.\+No: \([[:digit:]]\{1,\}\)$/\1/gp'
}

# swbd
#
# Retrieve the current system platform name, of the form "SWBDn", where n
# is cardinal number, assuming a sin/hinv input stream.
#
swbd() {
    sed -n -e 's/^.\+\(SWBD[[:digit:]]\{1,\}\).\+$/\1/gp'
}

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

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

cpid_slot() {
    case ${SWBD##SWBD} in
    '62')
        printf $((6 + CPID))
        ;;
    '77')
        printf $((4 + CPID))
        ;;
	 *)
		printf 0
		;;
    esac
}
CP_SLOT=`cpid_slot`



IDENTIFY="cat /proc/ide/ide0/hda/identify"
MODEL="cat /proc/ide/hda/model"
INSMOD="/sbin/insmod /fabos/modules/wd.ko force=1"
checkId="3334 312d 3032 3330"
CHKFILE="ls /fabos/modules | grep -c wd.ko"
REBOOT="/sbin/init 6"


check_and_fix () {

RSHCMD="/usr/bin/rsh 127.1.$((10+$CP_SLOT)).$(($SLOT_NO+1)) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0"
RIDENTIFY="$RSHCMD $IDENTIFY"
RMODEL="$RSHCMD $MODEL"
RINSMOD="$RSHCMD $INSMOD"
RCHKFILE="$RSHCMD $CHKFILE"
RREBOOT="$RSHCMD $REBOOT"
	check_file=$($RCHKFILE)
	if [ $check_file -eq 0 ]; then
		echo ""
    	echo "Firmware running on the blade has no support for this command." 
    	exit
	fi
	bp_has_wd=$($RMODEL | grep -E "2GB|1GB|512MB" | grep -c 3625)

	if [ $bp_has_wd -ne 0 ]; then
		bp_needs_fix=$($RIDENTIFY | awk '{ if (NR == 3) { printf "%s ", $0 } if (NR == 4) { print $0 } }' | grep -c "$checkId")
		if [ $bp_needs_fix -eq 0 ]; then
			echo ""
			read -p "Flash fix required for this slot. continue fix and reboot BP.? (y/n) " RES
			if [ "$RES" = "y" ]; then
				mod_ins=$($RSHCMD lsmod | grep -c "wd ")		
				if [ $mod_ins -eq 0 ]; then
					($RINSMOD)
					mod_ins=$($RSHCMD lsmod | grep -c "wd ")
					if [ $mod_ins -eq 0 ]; then
						echo "Command failed. Please contact switch vendor."
						exit
					fi
				fi
				echo "Rebooting the BP now to apply flash fix.."
				($RREBOOT)
			else 
				echo "Flash fix was not applied..."
			fi
		else 
			echo "Flash is already fixed. Exiting.."
		fi
	else 
		echo "No flash fix needed for this blade. Exiting.."
	fi
}

#
# Main()
#



if [ "$SWBD" != "SWBD67" ]; then

	if [[ -z "$@" || !($1 != *[^0-9]*) ]]; then
		echo "Usage: bpflashfix <slot_no>"
		exit
	fi

	if [ "$SWBD" == "SWBD62" ]; then
		if [ $SLOT_NO -lt 1 -o $SLOT_NO -gt 12 ]; then
			echo "Invalid Slot number."
			exit
		fi
	elif [ "$SWBD" == "SWBD77" ]; then
		if [ $SLOT_NO -lt 1 -o $SLOT_NO -gt 8 ]; then
			echo "Invalid Slot number."
			exit
		fi
	else
		echo "Command Not supported on this platform."
		exit
	fi

	ha_state=$(/fabos/cliexec/hashow | grep -c "synchronized")
	if [ $ha_state -eq 0 ]; then
		echo "HA state is not in sync. Please try later."
		exit
	fi
	blade=$(/fabos/link_bin/slotshow | grep " $SLOT_NO " | grep -E "75|74|43" | wc -l)
	if [ $blade -eq 0 ]; then
		echo "Empty slot or unsupported blade. Only supported on FS8-18,"
		echo "FX8-24 and FCOE10-24 blades."
		exit
	fi
else
	if [[ -z "$@" ]]; then
		SLOT_NO=0
	else
		echo "Usage: bpflashfix"
		exit
	fi
fi
	read -p "This command will install the flash fix as needed
and would require a reboot of the BP. Continue. (y/n) " RESP

	if [ "$RESP" = "y" ]; then
  		check_and_fix
	else
  		echo "Nothing to do.. Exiting.."
	fi
