#!/bin/sh

# ident "@(#)dpt_flashupdate 1.23     01/03/27 SMI"

# SRC/P Flash Update script
# Copyright (c) 2000-2001 by Sun Microsystems, Inc.
# All rights reserved.
#

RUN_LEVEL=`/usr/bin/who -r | awk '{print $3}'`
case $RUN_LEVEL in
S)      ;;
1)      ;;
*)      
	echo
	echo "Must be run from single-user mode  - \"$0\" cannot be run!"
	echo
	exit 0 ;;
esac

clear
echo
echo
echo "Setup environment for SRC/P Flash Update - Please wait..."

/usr/sbin/modinfo | grep dpti2o >/dev/null 2>&1
if test $? != "0"
then
	echo
	echo "dpti2o driver not loaded - \"$0\" cannot be run!"
	echo
	exit 0
fi

/usr/bin/pkginfo | grep SUNWhwrdg >/dev/null 2>&1
if test $? != "0"
then
	echo
	echo "SUNWhwrdg package not loaded - \"$0\" cannot be run!"
	echo
	exit 0
fi

if test ! -f 3754*.sum
then
        if test -f /opt/SUNWhwrdg/3754*.sum
        then
                cd /opt/SUNWhwrdg
        else
                echo
                echo "Cannot find firmware checksum file!"
                echo
                exit 0
        fi
fi

#
# Check for firmware, NVRAM and FCODE.  If they exist, do checksum.
#
PATCH_FW=0
PATCH_FCODE=0
PATCH_NVR=0

ls *.sum >/tmp/sum.list

for i in `cat /tmp/sum.list`
do
	IMAGE_NAME=`echo $i | sed 's/.sum//'`
	if [ $PATCH_FW = "0" ]; then
		if [ -s $i ] ; then 
			echo $IMAGE_NAME |grep 3754 >/dev/null 2>&1
			if [ $? = "0" ] ; then
				PATCH_FW=$IMAGE_NAME.ima
				/usr/bin/sum $PATCH_FW >/tmp/patchfw.sum
				/usr/bin/cmp /tmp/patchfw.sum $IMAGE_NAME.sum >/dev/null 2>&1
				if test $? != "0"
				then
					echo
					echo "$PATCH_FW checksum error!"
					echo
					exit 1
				fi
			fi
		fi
		continue
	fi

	if [ -f $IMAGE_NAME.rom ] ; then
		PATCH_FCODE=$IMAGE_NAME.rom
		PATCH_FCODE_SUM=$IMAGE_NAME.sum
		/usr/bin/sum $PATCH_FCODE >/tmp/patchfw.sum
		/usr/bin/cmp /tmp/patchfw.sum $IMAGE_NAME.sum >/dev/null 2>&1
		if test $? != "0"
		then
			echo
			echo "$PATCH_FCODE checksum error!"
			echo
			exit 1
		fi
	else
		if [ -f $IMAGE_NAME.nvr ] ; then
			PATCH_NVR=$IMAGE_NAME.nvr
			PATCH_NVR_SUM=$IMAGE_NAME.sum
			/usr/bin/sum $PATCH_NVR >/tmp/patchfw.sum
			/usr/bin/cmp /tmp/patchfw.sum $IMAGE_NAME.sum >/dev/null 2>&1
			if test $? != "0"
			then
				echo
				echo "$PATCH_NVR checksum error!"
				echo
				exit 1
			fi
		fi
	fi
done


if test -f /opt/SUNWhwrdg/dptutil
then
	CMD_PATH=/opt/SUNWhwrdg
	PATH=$PATH:/opt/SUNWhwrdg
	export PATH
else
	echo "/opt/SUNWhwrdg/dptutil not found - \"$0\" cannot be run!"
	exit 0
fi

ynget()	{ # Prompt for yes/no answer - return non-zero for no 
	while 	echo "$* (y/n)? \c" >& 2
		do	read yn rest
			case $yn in
			[y])	return 0	;;
			[n])	return 1	;;
			*)	echo "Please enter y or n" >&2 ;;
			esac
		done
	}

stopper (){	# 
		echo
		printf "Hit \"Enter\" key to continue: "
		read ANS
		echo
	 }


check_firmware (){
	clear
	echo
	echo

	NEED_FLASH=0
	FCS_FIRMWARE=0

	# Display Controller's Firmware Version
	echo "Checking for Installed SRC/P RAID Controllers - Please wait..."
	echo
	$CMD_PATH/dptutil -L controller > /tmp/controllers.out
	clear
	echo
	echo "Installed SRC/P RAID Controllers:"
	echo
	cat /tmp/controllers.out| more


	FCS_FIRMWARE_FILE=$PATCH_FW
	FCS_FIRMWARE=`echo $PATCH_FW | sed 's/3754//' | sed 's/.ima//' | sed 's/.fwi//' | sed 's/sm/SM/' | sed 's/a/A/g' | sed 's/b/B/g' | sed 's/c/C/g' | sed 's/d/D/g' | sed 's/e/E/g' | sed 's/f/F/g'`

	if [ $FCS_FIRMWARE = "0" ] ; then
		stopper
		rm /tmp/controllers.out

		return
	fi

	grep "^d" /tmp/controllers.out > /tmp/ctrl_fw.list
	cat /tmp/ctrl_fw.list | awk '{print $8}' >/tmp/fw.list
	for i in `cat /tmp/fw.list`
	do
		if test $i != $FCS_FIRMWARE ; then
			printf "NOTE: SRC/P Controller Firmware (FW) should be at Revision \"$FCS_FIRMWARE\" or greater.\n\n"

			NEED_FLASH=1
			
			break
		fi
	done

	if [ "$NEED_FLASH" != "0" -a "$FCS_FIRMWARE" != "0" ] ; then
		ynget "Type \"y\" to update all cards to \"$FCS_FIRMWARE\" firmware or \"n\" to skip update" || return
	else
		printf "\nSRC/P firmware already at proper revision.\n"
		stopper

		return
	fi

	printf "\n\nAbout to update all cards to \"$FCS_FIRMWARE\" firmware via the following commands:\n"
	echo
	echo "	$CMD_PATH/dptutil -F ./$FCS_FIRMWARE_FILE"
	if [ $PATCH_FCODE != "0" ] ; then
		echo "	$CMD_PATH/dptutil -F ./$PATCH_FCODE"
	fi
	if [ $PATCH_NVR != "0" ] ; then
		echo "	$CMD_PATH/dptutil -F ./$PATCH_NVR"
	fi
	printf "\nYour system will be shutdown when the firmware update is complete!\n"
	printf "\nWARNING: Do not interrupt the flash update process!\n\n"

	ynget "Type \"y\" to continue with firmware update or \"n\" to skip update" || return
	
	echo
	echo "Flashing SRC/P RAID Controller(s) - Please wait..."
	echo

	trap "" 1 2 15

	echo "  $CMD_PATH/dptutil -F ./$FCS_FIRMWARE_FILE"
	$CMD_PATH/dptutil -F ./$FCS_FIRMWARE_FILE 2>&1 >/tmp/dptutil.out

	if [ $PATCH_FCODE != "0" ] ; then
		echo "  $CMD_PATH/dptutil -F ./$PATCH_FCODE"
		$CMD_PATH/dptutil -F ./$PATCH_FCODE 2>&1 >/tmp/dptutil.out
	fi

	if [ $PATCH_NVR != "0" ] ; then
		echo "  $CMD_PATH/dptutil -F ./$PATCH_NVR"
		$CMD_PATH/dptutil -F ./$PATCH_NVR 2>&1 >/tmp/dptutil.out

		echo
		grep Downloading /tmp/dptutil.out
		echo

		echo "Reset NVRAM defaults..."
		for i in 0 1 2 3 4 5 6 7 8 9
		do
			$CMD_PATH/dptutil -d$i -X >/tmp/dptutil.out

			grep "NVRAM Reset" /tmp/dptutil.out 2>&1 >/dev/null
			if [ $? = "0" ] ; then
				echo
				echo "SRC/P $i NVRAM Reset via \"$CMD_PATH/dptutil -d$i -X\"" 
			fi
		done
	else
		echo
                grep Downloading /tmp/dptutil.out
                echo
	fi

	trap 1 2 15

	printf "\nYour system will now be shutdown.\n\n"

	stopper

	echo
	echo "Your system is being shutdown - Please wait..."
	echo

	/usr/bin/sync

	sleep 30

	/usr/sbin/uadmin 2 0

}

check_firmware
