#!/bin/sh
#
# Usg: mb5120_NvRamCountUty.sh <mode> <prm2> <prm3> <prm4>
#
# <mode>
#	CountUp = Count Up. <prm2>=Eth0 <prm3>=Eth1 <prm4>=Rtc
#	Display = Display. 表示
#	ChkErase= MTエラー無しの場合NVRAMをイレースする。
#  ret 0 = OK
#      1 = NG

MTDDEV=/tmp/testdev/mtd3
TEMPFILE=/tmp/tempfile
TEST_MARK="MTEST"
let RETCODE=0

PgMode=$1
Ram_ErrEth0=$2
Ram_ErrEth1=$3
Ram_ErrRTC=$4

#echo "Param PgMode=${PgMode} Ram_ErrEth0=${Ram_ErrEth0} Ram_ErrEth1=${Ram_ErrEth1} Ram_ErrRTC=${Ram_ErrRTC}"

#--------------------------------------
usage()
{
echo "Usg: mb5120_NvRamCountUty.sh <mode> <prm2> <prm3> <prm4>"
echo ""
echo " <mode>"
echo "	CountUp  : Count Up. <prm2>=Eth0 <prm3>=Eth1 <prm4>=Rtc"
echo "	Display  : Display."
echo "	ChkErase : MT Data check and Erase"
echo "	ErrorChk : Retern All add error count"
echo "	EraseAll : Erase Data area"
}
#--------------------------------------
NvRamGetMark()
{
	dd if=${MTDDEV} of=${TEMPFILE} bs=5 count=1 >/dev/null 2>&1
	Nv_Mark=""
	read  Nv_Mark  < ${TEMPFILE}
	#rm ${TEMPFILE}
}

#--------------------------------------
NvRamCountUp()
{
	NvRamGetMark
	if [ "X${Nv_Mark}" != "X${TEST_MARK}" ];then
		Nv_Mark=${TEST_MARK}
		let Nv_RunCount=0
		let Nv_ErrEth0=0
		let Nv_ErrEth1=0
		let Nv_ErrRTC=0
	else
		read  Nv_Mark Nv_RunCount Nv_ErrEth0 Nv_ErrEth1 Nv_ErrRTC < ${MTDDEV}
	fi
#	echo "${Nv_Mark} ${Nv_RunCount} ${Nv_ErrEth0} ${Nv_ErrEth1} ${Nv_ErrRTC}"

	let "Nv_RunCount += 1"
	let "Nv_ErrEth0 += ${Ram_ErrEth0}"
	let "Nv_ErrEth1 += ${Ram_ErrEth1}"
	let "Nv_ErrRTC  += ${Ram_ErrRTC}"

	eraseall ${MTDDEV}
	echo "${Nv_Mark} ${Nv_RunCount} ${Nv_ErrEth0} ${Nv_ErrEth1} ${Nv_ErrRTC}" > ${MTDDEV}
#	echo "${Nv_Mark} ${Nv_RunCount} ${Nv_ErrEth0} ${Nv_ErrEth1} ${Nv_ErrRTC}"
}
#--------------------------------------
NvRamDisplay()
{
	echo "/// MB5120 MANUFACTURE TEST RESULT /////"

	NvRamGetMark
	if [ "X${Nv_Mark}" != "X${TEST_MARK}" ];then
		echo "MANUFACTURE TEST DATA Not found."
	else
		read  Nv_Mark Nv_RunCount Nv_ErrEth0 Nv_ErrEth1 Nv_ErrRTC < ${MTDDEV}
		echo "RUN COUNT ............ ${Nv_RunCount}"
		echo "ETHER 0 ERROR COUNT .. ${Nv_ErrEth0}"
		echo "ETHER 1 ERROR COUNT .. ${Nv_ErrEth1}"
	fi
}
#--------------------------------------
#	MT Data check and erase
#
NvRamChkErase()
{
	NvRamGetMark
	if [ "X${Nv_Mark}" = "X${TEST_MARK}" ];then
		read  Nv_Mark Nv_RunCount Nv_ErrEth0 Nv_ErrEth1 Nv_ErrRTC < ${MTDDEV}
#		echo "${Nv_Mark} ${Nv_RunCount} ${Nv_ErrEth0} ${Nv_ErrEth1} ${Nv_ErrRTC}"
		if [ ${Nv_ErrEth0} -eq 0 -a ${Nv_ErrEth1} -eq 0 ];then
				echo "Erase MANUFACTURE TEST DATA "
				eraseall ${MTDDEV}
		fi
	fi
}
#--------------------------------------
#	MT Data check and erase
# Ret
# 0 =Non Error
# 0 !=Error 
NvRamErrorChk()
{
	RETCODE=0;
	NvRamGetMark
	if [ "X${Nv_Mark}" = "X${TEST_MARK}" ];then
		read  Nv_Mark Nv_RunCount Nv_ErrEth0 Nv_ErrEth1 Nv_ErrRTC < ${MTDDEV}
		let "RETCODE = ${Nv_ErrEth0} + ${Nv_ErrEth1} + ${Nv_ErrRTC}"
#		echo "${Nv_Mark} ${Nv_RunCount} ${Nv_ErrEth0} ${Nv_ErrEth1} ${Nv_ErrRTC}"
	fi
}

#--------------------------------------
# Main
#--------------------------------------
#echo Main
case "${PgMode}" in
	"CountUp")
		NvRamCountUp
		;;
		
	"Display")
		NvRamDisplay
		;;

	"ErrorChk")
		NvRamErrorChk
		;;

	"ChkErase")
		NvRamChkErase
		;;

	"EraseAll")
		echo "Erase MANUFACTURE TEST DATA "
		eraseall ${MTDDEV}
		;;
	*)
		usage
		;;
esac

exit ${RETCODE}
