#!/bin/sh

# ident "@(#)dpt_flashupdate 1.20     00/06/15 SMI"
# SRC/P Flash Update script
# Copyright (c) 2000 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

PATCH_FW=`/usr/bin/cat 3754*.sum | awk '{print $3}'`
/usr/bin/sum $PATCH_FW >/tmp/patchfw.sum
/usr/bin/cmp /tmp/patchfw.sum 3754*.sum >/dev/null 2>&1
if test $? != "0"
then
	echo
	echo "Firmware checksum error!"
	echo
	exit 1
fi

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 command:\n"
	echo
	echo "	$CMD_PATH/dptutil -F ./$FCS_FIRMWARE_FILE"
	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

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

	echo
	grep Downloading /tmp/dptutil.out
	echo

	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
