#!/bin/ksh

#
# Copyright 2004 Sun Microsystems, Inc
# All Rights Reserved
#

#
# Script to initiate memory speed setting command from Solaris
#
# This tool supports SPARC firmware 1.1.8 and onwards
# It supports CP2140 boards only
#

V_MAJOR=1
V_MIN=1
V_BUILD=8
BOARDNAME=CP2000
BOARDMODEL=140
BOARDNAME1=Netra
BOARDMODEL1=ct800
BOARDNAME2=Netra
BOARDMODEL2=ct400
BOARDNAME3=NULL
BOARDMODEL3=NULL

#
# Display the banner
#

BANNER="@(#) CP2140 Memory Speed Update Tool (c) 2004 Sun Microsystems, Inc. v"
VER="0.1"

echo
echo
echo $BANNER$VER
echo

#
# Check if we are super user
#

UTYPE1=`id`
UTYPE2=`id | grep root`
if [[ $UTYPE1 != $UTYPE2 ]]
then
	echo "### Must be super user to execute this tool ###"
	echo; echo
	exit 1
fi

echo
echo
echo "\t#####  This Tool Will Reboot The System To  #####"
echo "\t#####  Perform CP2140 Memory Speed Update   #####"
echo
echo "Do you want to continue [y/n]? \c"
read RSP
case $RSP in
	y|Y) echo;;
	n|N) echo; exit 1;;
	*)   echo; echo "Invalid input. Exiting..."; echo; exit 1;;
esac

#
# Get the existing binary information
#

echo
echo "Checking platform and firmware version information....\c"

PRTDIAG=/usr/platform/`uname -i`/sbin/prtdiag
PRTCONF=/usr/sbin/prtconf

CURRFWVER=`$PRTCONF -V | awk '{print $18}'`
CV_MAJOR=`$PRTCONF -V | awk '{print $18}' |sed "s/\./ /g" | awk '{ print $1 }'`
CV_MIN=`$PRTCONF -V | awk '{print $18}' |sed "s/\./ /g" | awk '{ print $2 }'`
CV_BUILD=`$PRTCONF -V | awk '{print $18}' |sed "s/\./ /g" | awk '{ print $3 }'`

CURRBOARDNAME=`$PRTDIAG | grep SPARC | awk '{print $6}'`

if [[ $CURRBOARDNAME == "Netra" ]] || [[ $CURRBOARDNAME == "Netra(tm)" ]]
then
    CURRBOARDMODEL=`$PRTDIAG | grep SPARC | awk '{print $7}'`
else
    CURRBOARDNAME=`$PRTDIAG | grep SPARC | awk '{print $7}'`
    CURRBOARDMODEL=`$PRTDIAG | grep SPARC | awk '{print $9}'`
fi

#
# Check the validity: board type, model, fw version etc.
#

if [[ $CURRBOARDNAME != $BOARDNAME ]] && [[ $CURRBOARDNAME != $BOARDNAME1 ]] && [[ $CURRBOARDNAME != $BOARDNAME2 ]] && [[ $CURRBOARDNAME != $BOARDNAME3 ]]
then
	echo; echo
	echo "Memory Speed Update Tool does not belong to this platform"
	echo "Board Name do not match. Cannot proceed!"
	exit 1
fi

if [[ $CURRBOARDMODEL != $BOARDMODEL ]] && [[ $CURRBOARDMODEL != $BOARDMODEL1 ]] && [[ $CURRBOARDMODEL != $BOARDMODEL2 ]] && [[ $CURRBOARDMODEL != $BOARDMODEL3 ]]
then
	echo; echo
	echo "Memory Speed Update Tool does not belong to this platform"
	echo "Board Model do not match. Cannot Proceed!"
	exit 1
fi

MESSAGE0="Start to update the memory speed setting..."
MESSAGE1="ERROR: The current FW version is not supported by this tool."
MESSAGE2="       The firmware version should be 1.1.8 and onwards."

typeset -i2 v_major
typeset -i2 v_min
typeset -i2 v_build
typeset -i2 cv_major
typeset -i2 cv_min
typeset -i2 cv_build

v_major=$V_MAJOR
v_min=$V_MIN
v_build=$V_BUILD
cv_major=$CV_MAJOR
cv_min=$CV_MIN
cv_build=$CV_BUILD

if [[ $cv_major -gt $v_major ]]
then
	echo "Done"; echo; echo
	echo "$MESSAGE0"
else
	if [[ $cv_major -lt $v_major ]]
	then
		echo; echo
		echo "$MESSAGE1"
		echo "$MESSAGE2"
		echo; echo
		exit 1
	else
		if [[ $cv_min -gt $v_min ]]
		then
			echo "Done"; echo
			echo "$MESSAGE0"
		else
			if [[ $cv_min -lt $v_min ]]
			then	
				echo; echo
				echo "$MESSAGE1"
				echo "$MESSAGE2"
				echo; echo
 				exit 1
			else
				if [[ $cv_build -gt $v_build ]]
				then
					echo "Done"; echo
					echo "$MESSAGE0"
				else
					if [[ $cv_build -lt $v_build ]]
					then
						echo; echo
						echo "$MESSAGE1"
						echo "$MESSAGE2"
						echo; echo
						exit 1
					fi
				fi
			fi
		fi
	fi
fi

#
# Get memory setting information
#

echo
echo
echo "\t0 .... Set default memory speed"
echo "\t1 .... Set high memory speed"
echo
echo "Select Memory Setting: \c"
read RSP
case $RSP in
	0) cmd=set-low-mem-speed;  echo;;
	1) cmd=set-high-mem-speed; echo;;
	*) echo; echo "Invalid input. Exiting..."; echo; exit 1;;
esac

#
# All the validation has been done. Lets initiate the update.
#

rebootpar="obp:$cmd"

echo
echo "Reboot parameter:  $rebootpar"
echo
echo

sync;sync;sync
/usr/sbin/reboot -- "$rebootpar"

#
# Control should not get here...
#

echo "Reboot Failed!!!"
echo
