#!/bin/bash
#dmi在minisystem下的命令脚本

# **************************************************************************** #
# Description: 获取devm进程的进程号；进程融合版本返回App_data的进程号
# OutPut: ${devm_pro_num}
# **************************************************************************** #
get_devm_pro_num()
{ 
#首先尝试获取devm的进程号
ret=$(pgrep "devm")
if [ -n "$ret" ];then
	devm_pro_num=21
else
#没有devm进程，则可能是进程融合版本，获取app_data的进程号
	devm_pro_num=12
fi

}

#打印的帮助信息
function cmd_help()
{
    echo -e "dmi.sh showbbustatus                                    : show bbu status."
    echo -e "dmi.sh setintfcardinfo slotid serv speed workmode       : set info to the intf card as an escape way,the maximum execution time of this command may exceed 5 minutes"
    echo -e "       para info: slotid for V6 share card[0-27], serv[front:1, backend:2, scaleout:3],speed[8G:10,10G:11,16G:13,40G:16,25G:18,32G:19,100G:21],workmode[FC:1, Eth:4, RoCE:5]"
    echo -e "dmi.sh getsn fruid deviceid                                      : show ctrlencl or intfboard sn, eg: dmi.sh getsn fruid 0"
    echo -e "dmi.sh getmac fruid deviceid                                     : show ctrlencl or intfboard mac, eg: dmi.sh getmac fruid 0"
    echo -e "dmi.sh getmodel enclfruid deviceid                               : show ctrlencl or intfboard model, eg: dmi.sh getmodel fruid 0"
    echo -e "dmi.sh setsn enclfruid deviceid sn                               : set ctrlencl or intfboard sn, eg: dmi.sh setsn fruid 0 XXXXX"
    echo -e "dmi.sh setmac enclfruid deviceid mac[0 0 0 0 0 0]                : set ctrlencl or intfboard mac, eg: dmi.sh setmac fruid 0 1 2 3 4 5 6"
    echo -e "dmi.sh setmodel enclfruid deviceid model                         : set ctrlencl or intfboard model, eg: dmi.sh setmodel fruid 0 XXXX"
    echo -e "dmi.sh resetmcu fruid deviceid                                   : reset mcu, eg: dmi.sh resetmcu fruid 0"
    echo -e "dmi.sh bspcoldreboot                                             : cold reboot."
    echo -e "dmi.sh bspsetmadcswitch operate[0-1]                             : set madc switch[0:off,1:on], eg: dmi.sh bspsetmadcswitch 1"
    echo -e "dmi.sh upgradeend                                                : notify control upgrade end, eg: dmi.sh upgradeend"
    echo -e "dmi.sh resetbmc                                                  : reset bmc, eg: dmi.sh resetbmc"
    echo -e "dmi.sh repaircpld                                                : repair cpld, eg: dmi.sh repaircpld"
    echo -e "dmi.sh clearisolatestate slotid                                  : clear card isolate state, eg:dmi.sh clearisolatestate slotid"
    echo -e "dmi.sh bspsetbootid                                              : set bootid [3:heartbeat boot] eg: dmi.sh bspsetbootid 3"
    echo -e "dmi.sh forcepoweron                                              : force power on system, eg: dmi.sh forcepoweron"
    echo -e "dmi.sh setfanmode fruid deveiceid mode                           : set fan mode [0:auto,1:manual], eg: dmi.sh setfanmode 0x4000 0 1"
    echo -e "dmi.sh setfanlevel fruid deveiceid level                         : set fan level [0x1-0x20](level1-level32), before excute this command must set fanmode manually , eg: dmi.sh setfanlevel 0x4000 0 0x20"
	echo -e "dmi.sh getdimmfrusoftswitch                                      : get dimm fru soft switch, eg: dmi.sh getdimmfrusoftswitch"
    echo -e "dmi.sh getdimmfruhardswitch                                      : get dimm fru hard switch, eg: dmi.sh getdimmfruhardswitch"
    echo -e "dmi.sh getdimmfruswitch                                      	  : get dimm fru switch, eg: dmi.sh getdimmfruswitch"
    echo -e "dmi.sh forcerollbackbios                                         : force rollback bios, eg: dmi.sh forcerollbackbios"
}

#查询BBU状态信息
DMI_SHOWBBUINFO()
{
    L_EXEC_FLAG="dpl showbbustatus"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}
#查询设备SN
DMI_GETSN()
{
    L_EXEC_FLAG="mpa bmc getsn $1 $2"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}

#查询设备mac
DMI_GETMAC()
{
    L_EXEC_FLAG="mpa bmc getmac $1 $2"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}

#查询设备MODEL
DMI_GETMODEL()
{
    L_EXEC_FLAG="mpa bmc getmodel $1 $2"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}

#设置SN
DMI_SETSN()
{
    L_EXEC_FLAG="mpa bmc setsn $1 $2 $3"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}

#设置MAC
DMI_SETMAC()
{
    L_EXEC_FLAG="mpa bmc setmac $1 $2 $3 $4 $5 $6 $7 $8"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}

#设置model
DMI_SETMODEL()
{
    L_EXEC_FLAG="mpa bmc setmodel $1 $2 $3"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}

#复位MCU
DMI_RESETMCU()
{
    L_EXEC_FLAG="mpa bmc resetmcu $1 $2"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}

#BSP冷复位
BSP_COLDREBOOT()
{
    echo -e "bsp coldreboot\nexit\n" | diagnose --auto
}

#BSP设置ADDEC开关
BSP_SETMADCSWITCH()
{
    L_EXEC_FLAG="bsp setmadcswitch $1"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#DMI通知升级结束
DMI_UPGRADE_END()
{
    L_EXEC_FLAG="dpl openallfruanddevroutineswitch"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
}

#清除接口卡隔离状态
PCIEMGT_CLEARCARDISOLATE()
{
    diagsh --attach="devm_${devm_pro_num}" --cmd="pciemgt clearcardisolate $1"
}

#复位BMC
DMI_RESETBMC()
{
    diagsh --attach="devm_${devm_pro_num}" --cmd="mpa bmc softresetbmc 200 0"
}

#设置接口卡应用场景,速率,工作模式
DMI_SET_SERV_SPEED_WORKMODE()
{
    slotId=$1
    serv=$2
    speed=$3
    workMode=$4
    cfgType=$5
    boardId=$1
    fruId=256
    flag=0
    productType=`cat /proc/osp/bsp |grep "Product Name"|awk -F " " '{print $4}'`
        
    #高端和中低端的boardid和fruid转换方式不一样
    if [ "PANGEA_V6R1C00_HI1620_4U4C" == "${productType}" ] ; then
        boardId=`expr 128 + $slotId`
        fruId=`expr 65536 + $slotId`
    else
        boardId=$slotId
        fruId=`expr 256 + $slotId`
    fi
    
    temp=`echo "obase=16;$fruId" | bc`
    fruId="0x$temp"
    temp=`echo "obase=16;$speed" | bc`
    speed="0x$temp"
    
    #先写应用场景
    L_EXEC_SET_SERV_SPEED="mpa bmc setiocardusageandspeed $fruId 0 $serv $speed $cfgType"
    echo -e "${L_EXEC_SET_SERV_SPEED}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto

    #反复查询接口卡的快照的健康状态，为normal后才向下走
    for((i=0; i<6; i++))
    do
        L_EXEC_GET_INTF_STATE="msc showsnap intf $boardId"
        result=`echo -e "$L_EXEC_GET_INTF_STATE\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto  |grep RunningStatus|awk -F ":" '{print $3}'|awk -F "(" '{print $1}'`
        if [ "2" = "$result" ] ; then
            flag=1
            sleep 30
            break;
        elif [ -z "$result" ] ; then
            echo "echo "slot:$slotId find no card!""
            exit 0
        else
            sleep 30
        fi
    done
    
    if [ "1" == "$flag" ] ; then
        L_EXEC_SET_INTF_WORKMODE="dpl setintfworkmode $slotId $workMode"
        echo -e "${L_EXEC_SET_INTF_WORKMODE}\nexit\n" | diagnose_usr --attach=${devm_pro_num} --auto
    else
        echo "slot:$slotId card RunningState state abnormal:$result"
    fi
}

#BSP设置启动ID
BSP_SETBOOTID()
{
    if [ $1 -eq 3 ];then
        echo -e "bsp setbootid $1\nexit\n" | diagnose --auto
    else
        echo "cmd para $1 err"
    fi
}
#修复CPLD
DMI_REPAIRCPLD()
{
    ret=`diagsh --attach="devm_${devm_pro_num}" --cmd="mpa bmc repaircpld 200 0"|grep success`
    echo $ret
    if [ -z "$ret" ] ; then
        echo "mpa repaircpld fail."
    else
        echo "You are about to restart the controller. This operation will cause the controller to fail to carry services."
        echo "Suggestion: Before performing this operation, ensure that the service port of the controller has multiple paths or services related to the controller have been stopped. Do not perform this operation when the controller is running properly."
        sh /OSM/script/os_rebootsys.sh 
    fi
}

#强制系统上电
DMI_FORCEPOWERON()
{
    diagsh --attach="devm_${devm_pro_num}" --cmd="mpa bmc forcepoweron 200 0"
}

#设置风扇模式
DMI_SETFANMODE()
{
    diagsh --attach="devm_${devm_pro_num}" --cmd="mpa bmc setfanmode $1 $2 $3"
}

#设置风扇档位
DMI_SETFANLEVEL()
{
    diagsh --attach="devm_${devm_pro_num}" --cmd="mpa bmc setfanlevel $1 $2 $3"
}

#查询内存FRU软件特性开关状态
DMI_GETDIMMFRUSOFTSWITCH()
{
	diagsh --attach="devm_${devm_pro_num}" --cmd="dpl getdimmfrusoftswitch"
}

#查询内存FRU硬件特性开关状态
DMI_GETDIMMFRUHARDSWITCH()
{
	diagsh --attach="devm_${devm_pro_num}" --cmd="dpl getdimmfruhardswitch"
}

#查询内存FRU特性开关状态
DMI_GETDIMMFRUSWITCH()
{
	diagsh --attach="devm_${devm_pro_num}" --cmd="dpl getdimmfruswitch"
}

DMI_FORCEROLLBACKBIOS()
{
    echo "DANGER: You are about to perform a danger command. The command will roll back BIOS of this controller and restart. You must run this command on each controller in sequence. You can run this command on other controllers only after confirming that the controller is restored. If you run this command on multiple controllers at the same time, data in the system memory will be lost."
    echo "Suggestion: Before you perform this operation, make sure the operation will not affect working of system."
    echo "Are you sure to restart and update BIOS of this controller?(y/n)"
    read -p "" L_INPUT
    if [ "${L_INPUT}" == "y" -o "${L_INPUT}" == "Y" ];then
        echo "This command will run for several minutes. Please wait."
        PANGEA_LINK=`cat /proc/osp/bsp | awk '/Product/ {print $4}'`
        BIOS_ROM=/startup_disk/image/6200819073/system/firmware/${PANGEA_LINK}/bios.ROM
        if [ ! -f "$BIOS_ROM" ]; then
            echo "The file $BIOS_ROM does not exist"
            exit 1
        fi
        L_INSMOD_UPDATEBIOS_KO=`lsmod |awk '{print $1}' | grep '^updatebios'`
        if [ -n "$L_INSMOD_UPDATEBIOS_KO" ];then
            OS_echo "[BIOS] rmmod updatebios."
            rmmod /OSM/modules/updatebios.ko
        fi
        OS_echo "[BIOS] update bios by $BIOS_ROM."
        insmod /OSM/modules/updatebios.ko fname=$BIOS_ROM upmode=8
        ret=`cat /OSM/modules/updateresult.bin | grep "bios:fail"`
        if [ -n "$ret" ] ; then
            echo "Update bios failed."
            rm -rf /OSM/modules/updateresult.bin
            exit 1
        fi
        echo "Update bios success and restart."
        /usr/bin/esyslog_poweroff_pre.sh 5
        reboot
    fi
}

while getopts ":ha" options
do
    case "$options" in
        h)
            cmd_help
            exit 0
            ;;
        \?)
            echo "usage: dmi.sh -h"
            exit 1
            ;;
    esac
done
#先获取进程号；后续的diagnose命令中需要attach进程号
get_devm_pro_num
if [[ "${devm_pro_num}" == "" ]]; then
    echo "Can not get process id of devm and App_data"
    exit 1
fi

case "$1" in
    showbbustatus)
        DMI_SHOWBBUINFO 
        exit 0
    ;;
    setintfcardinfo)
        if [[ $# != 5 ]] && [[ $# != 6 ]] ; then
            echo "usage: dmi.sh -h"
            exit 1
        fi
        DMI_SET_SERV_SPEED_WORKMODE $2 $3 $4 $5 $6
        exit 0
    ;;
    getsn)
        DMI_GETSN $2 $3
        exit 0
    ;;
    getmac)
        DMI_GETMAC $2 $3
        exit 0
    ;;
    getmodel)
        DMI_GETMODEL $2 $3
        exit 0
    ;;
    setsn)
        DMI_SETSN $2 $3 $4
        exit 0
    ;;
    setmac)
        DMI_SETMAC $2 $3 $4 $5 $6 $7 $8 $9
        exit 0
    ;;
    setmodel)
        DMI_SETMODEL $2 $3 $4
        exit 0
    ;;
    resetmcu)
        DMI_RESETMCU $2 $3
        exit 0
    ;;
    bspcoldreboot)
        BSP_COLDREBOOT
        exit 0
    ;;
    bspsetmadcswitch)
        BSP_SETMADCSWITCH $2
        exit 0
    ;;	
    upgradeend)
        DMI_UPGRADE_END
        exit 0
    ;;
    clearisolatestate)
        PCIEMGT_CLEARCARDISOLATE $2
        exit 0
    ;;	
    bspsetbootid)
        BSP_SETBOOTID $2
        exit 0
    ;;
    resetbmc)
        DMI_RESETBMC
        exit 0
    ;;
    forcepoweron)
        DMI_FORCEPOWERON
        exit 0
    ;;
    repaircpld)
        DMI_REPAIRCPLD
        exit 0
    ;;
    setfanmode)
        DMI_SETFANMODE $2 $3 $4
        exit 0
    ;;
    setfanlevel)
        DMI_SETFANLEVEL $2 $3 $4
        exit 0
    ;;
    getdimmfrusoftswitch)
        DMI_GETDIMMFRUSOFTSWITCH 
        exit 0
	;;
    getdimmfruhardswitch)
        DMI_GETDIMMFRUHARDSWITCH
        exit 0
	;;
    getdimmfruswitch)
        DMI_GETDIMMFRUSWITCH
        exit 0
    ;;
    forcerollbackbios)
        DMI_FORCEROLLBACKBIOS
        exit 0
    ;;
    *)
        echo "usage: dmi.sh -h"
        exit 1
    ;;
esac

cmd_help

exit 1
