#!/bin/bash
if [ ! -n "$1" ]
then 
   echo "Error:Have not input BIOS image!"
   exit 0
fi
echo "==================================================="
echo "ME reflash Begin......"
echo "==================================================="

ipmitool raw 0x3a 0x32 0x01 > reflash.log
while [ $? -ne 0 ];do
	ipmitool raw 0x3a 0x32 0x01 > reflash.log
done
echo "3a 32 02"
echo "Waiting for ME to reflash now,please wait a moment......"
sleep 3
ipmitool raw 0x3a 0x32 0x02 > getme.log
while [ $? -ne 0 ];do
	ipmitool raw 0x3a 0x32 0x02 > getme.log
done
status1=`cat getme.log`
#echo $status1

if [ "$status1" == " 01" ];then
echo "==================================================="
	echo "ME reflashing now......"
echo "==================================================="
	./afulnx_64 $1 /b /p /n /x /k /l /ME
	if [ $? -eq 0 ]; then
        	echo "success" > reflash.log
	else
        	echo "Error: Please run tool again!"    | tee -a reflash.log
        	exit 0
	fi
else 
	echo "Error: Please run reflash tool again!" | tee -a reflash.log
	exit 0
fi


echo "3a 32 00"
echo "Please wait a moment......"
ipmitool raw 0x3a 0x32 0x00 > reflash.log
while [ $? -ne 0 ];do
        ipmitool raw 0x3a 0x32 0x00 > reflash.log
done
sleep 2
ipmitool raw 0x3a 0x32 0x02 > getme1.log
while [ $? -ne 0 ];do
        ipmitool raw 0x3a 0x32 0x02 > getme1.log
done
status2=`cat getme1.log`
#echo $status2
if [ "$status2" == " 00" ];then
        rm getme.log -f
        rm getme1.log -f
        rm reflash.log -f
	echo "ME reflash finished!"
	echo "System must AC power off to have the changes take effect!"
	
else
	echo "Error:ME is not in  normal mode ,please run 'ipmitool raw 0x3a 0x32 0x00' again" > reflash.log
	exit 0
fi
