#!/bin/bash

# Description: H3C R4900 G2 BMC Fw Update
# 2020-04-29: 拆分BMC/BIOS/RAID升级包（allin.han）

currentDir=`pwd`
resultLog="`pwd`/result.log"
ipmiStatus=0

source "${currentDir}/bmc.conf"

function ipmiStatus() {	
	#ipmitool mc reset cold
	sleep 180s
        testString=`ipmitool fru print 0` 

        if [ "${testString}" = "" ]; then
                ipmiStatus=1	
		exit 1
	fi	
}

#Check the dedicated nic's channel ID is 1, if not, change it.
function checekChannelIDAndUpdate() {
        isChannelOne=`ipmitool raw 0x36 0x05 0xa2 0x63 0x00 0x0c | awk '{print $4}'`
        if [ "${isChannelOne}" == "00" ]; then
		ipmitool raw 0x36 0x09 0xa2 0x63 0x00 0x26 0x01
        fi
}

#Update Board manufacturer and product manufacturer to H3C
#Update product name to simple name to R4900 G2.
function updateFru() {
        bash ${currentDir}/UpdateFru.sh -K BM "H3C"
        bash ${currentDir}/UpdateFru.sh -K PM "H3C"
        productName=`ipmitool fru print 0 | grep "Product Name" | cut -d':' -f 2| sed 's/^ //'`
        if [ "`echo ${productName} | grep 'R4900 G2'`" != "" ]; then
                bash ${currentDir}/UpdateFru.sh -K PN "R4900 G2"
                bash ${currentDir}/UpdateFru.sh -K CPN "H1-U2-V4"
                bash ${currentDir}/UpdateFru.sh -K PPN "H1-U2-V4"
        fi
}

function bmcCheckAndUpdate() {
        if [ ${ipmiStatus} -eq 1 ];then
                echo -e  "The ipmi service is failed, the hdm could not be updated." >> ${resultLog}
        else
                bmcVersion=`ipmitool mc info | grep -A 1 "Aux Firmware Rev Info"| tail -n 1 |awk '{print $1}'`
		if [ "${bmcVersion}" != "${BMC_VERSION}" ]; then
			ipmitool raw 0x32 0xaa 0x00
			sleep 5s
			ipmitool raw 0x36 0x09 0x20 0x14 0x00 0x1a 0x00 0x00
			sleep 5s

			cd ${currentDir}/yafu
			yes|./Yafuflash_v1.01.03 -cd -pc -d BMC ./${BMC_FILE}
		fi
	fi
}


function hdmSettingDefault() {
	
	if [ ${ipmiStatus} -eq 1 ];then
                echo -e  "The ipmi service is failed, the hdm setting could not be setted." >> ${resultLog}
	else
		#Close password check
		ipmitool raw 0x36 0x09 0x20 0x14 0x00 0x06 0x00 0x00 0x05 0x05 0x00 0x00 0x00 0x00 >/dev/null
		sleep 2s
		#Open cd function
		ipmitool raw 0x32 0x6a 0x04 0x00 0x00 0x00 0x01 0x62 0x6f 0x74 0x68 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x14 0x00 0x00 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x00 0x00
		sleep 2s
		#Open hd functioni
		ipmitool raw 0x32 0x6a 0x08 0x00 0x00 0x00 0x01 0x62 0x6f 0x74 0x68 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x14 0x00 0x00 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x00 0x00
		sleep 2s
		#Close NTP function
		ipmitool raw 0x32 0xa8 0x03 0x00  >/dev/null
		sleep 2s
		#Close DNS function
		ipmitool raw 0x32 0x6c 0x09 0x00 0x00  >/dev/null
		sleep 2s 
		#Close SSH function
		ipmitool raw 0x32 0x6a 0x20 0x00 0x00 0x00 0x00 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x00 0xff 0xff 0xff 0xff 0x16 0x00 0x00 0x00 0x58 0x02 0x00 0x00 0x00 0x00  >/dev/null
		sleep 2s
		#Close SNMP function
		ipmitool raw 0x32 0x6a 0x00 0x01 0x00 0x00 0x00 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x46 0x00 0xa1 0x00 0x00 0x00 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff  >/dev/null
		sleep 2s
		#Close Trap function
		ipmitool raw 0x32 0x5a 0x00 0xa2 0x00  >/dev/null
		sleep 2s
		#Open last screen
		ipmitool raw 0x32 0x94 0x0b 0x01  >/dev/null
		sleep 2s
		#set Fans speed
		ipmitool raw 0x36 0x03 0xa2 0x63 0x00 0x60 0x09  >/dev/null
		sleep 2s
		#set Virtual USB device
		ipmitool raw 0x32 0xaa 0x00  >/dev/null
		ipmitool raw 0x36 0x09 0x20 0x14 0x00 0x1a 0x00 0x00 > /dev/null
	fi
}

updateFru

sleep 5s
hdmSettingDefault

sleep 5s
bmcCheckAndUpdate

ipmiStatus

checekChannelIDAndUpdate
