#!/bin/sh
source /OSM/script/os_load_base.sh

###【使用说明】###
#1、此脚本用于正常打补丁过程中的BMC以及配套表替换
#2、此脚本涉及的修改点如下：
#1）PRODUCT_SWOFWARE_VER，产品软件版本号，决定了固件所在盘上的位置，在打补丁时传递，修改BMC_START/BMC_ROLLBACK函数中的数字版本号
#2）FW_BMC_VER版本号，当前此脚本支持1710/1711 BMC热升级，同一套代码出同一版本号
#3）目前此脚本只支持V6 C/C+版本的BMC升级，无论什么形态，BMC均是同一固件
#   如：FW_BMC_PATH：1710 BMC，对应bmc.cramfs
#       FW_BMC_PATH_1711：1711 BMC，对应bmc_1711.bin
#       BK_FW_BMC_PATH/BK_FW_BMC_PATH_1711：BMC基线版本备份文件
#       PRE_FW_BMC_PATH/PRE_FW_BMC_PATH_1711：上一次补丁备份文件
#4）FW_PACKAGE_NAME：用户提供的固件包里的BMC固件，可以单独包含1710或者1711，或者同时包含2者
#5）如果需要支持V6 C+新增的形态，需要同时在REPLACE_BMCFW_VERSION/ROLLBACK_BMCFW_VERSION函数中适配
#6）融合V5R7C60SPC300版本以及之前的补丁略有区别，因为此版本存在2个bmc.cramfs实体（控制框和IP框出包分支不同），V5R7C60SPC300包含之前的版本不需要在控制器形态上执行COPY_CONFFILE_FOREXP/ROLLBACK_CONFFILE_FOREXP函数（即V5R7C60SPC300补丁控制器不需要同时替换磁盘框配套表，但是固件要单独备份和回退，参考FW_BMC_PATH_IP用法），DoradoV6版本以及V5R7C70SPC200之后的融合版本需要执行此2个函数（BMC固件实体归一）
#7）高端升级/回退管理板函数UPGRADE_MGMT_BMC，后续需要适配C/C+高端形态(V6R1/V6R3)，可以选择在脚本中升级MGT，如果升级已经支持后台排队升级，则在post脚本中执行即可，如：upg fw 4,3，此处不执行

#需要升级的BMC版本号
FW_BMC_VER=64.11.02T82
FW_PACKAGE_NAME=USER_V5R7C73SPC100FW_BMC
#平台类型
PANGEA_LINK=`cat /proc/osp/bsp | awk '/Product/ {print $4}'`

#高端升级管理板所需
PRODUCTVER_4U4C_C="PANGEA_V6R1C00_HI1620_4U4C"
PRODUCTVER_4U4C_CP="PANGEA_V6R3C00_HI1620_4U4C"
UPG_HP_TMP_PATCH_FILE="/tmp/hotpatch_tmp_file_bmc"

#判断是否需要替换
REPLACE_NEED_1711=0
REPLACE_NEED_1710=0

#是否为special场景
SPECIAL=0

INIT_FW_PATCH()
{
    PRODUCT_SWOFWARE_VER=$1
    OS_echo "[BMC]Now patch path is ${PRODUCT_SWOFWARE_VER}."

    #版本配套表
    SYS_VERSION_FILE=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/${PANGEA_LINK}/product_versions.${PANGEA_LINK}.conf
    #BMC固件
    FW_BMC_PATH=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/manage_chip/bmc.cramfs
    FW_BMC_PATH_1711=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/manage_chip/bmc_1711.bin
    #校验和
    CHECKSUM_SYNC=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/sha256sum_sync

    #版本配套表备份文件
    BK_SYS_VERSION_FILE=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/${PANGEA_LINK}/product_versions.${PANGEA_LINK}_bmcbak.conf
    #BMC固件备份文件
    BK_FW_BMC_PATH=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/manage_chip/bmc_bak.cramfs
    BK_FW_BMC_PATH_1711=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/manage_chip/bmc_1711_bak.bin

    #融合版本V5R7C60SPC300包含以及之前的BMC在磁盘框需要单独备份
    FW_BMC_PATH_IP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOD_SAS_EXP/manage_chip/bmc.cramfs
    BK_FW_BMC_PATH_IP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOD_SAS_EXP/manage_chip/bmc_bak_IP.cramfs
    PRE_FW_BMC_PATH_IP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOD_SAS_EXP/manage_chip/bmc_pre_IP.cramfs

    #磁盘框配套表
    JBOD_SAS_EXP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOD_SAS_EXP/product_versions.PANGEA_V6R1C00_JBOD_SAS_EXP.conf
    JBOF_PALM_EXP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOF_PALM_EXP/product_versions.PANGEA_V6R1C00_JBOF_PALM_EXP.conf
    BK_JBOD_SAS_EXP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOD_SAS_EXP/product_versions.PANGEA_V6R1C00_JBOD_SAS_EXP_bmcbak.conf
    BK_JBOF_PALM_EXP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOF_PALM_EXP/product_versions.PANGEA_V6R1C00_JBOF_PALM_EXP_bmcbak.conf
    PRODUCTVER_JBOD_EXP="PANGEA_V6R1C00_JBOD_SAS_EXP"
    PRODUCTVER_JBOF_EXP="PANGEA_V6R1C00_JBOF_PALM_EXP"

    #多次补丁替换备份，将当前的固件备份pre，补丁包中的固件作为当前的，升级反复打补丁，系统只会保留2份补丁
    PRE_FW_BMC_PATH=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/manage_chip/bmc_pre.cramfs
    PRE_FW_BMC_PATH_1711=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/manage_chip/bmc_1711_pre.bin
    PRE_SYS_VERSION_FILE=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/${PANGEA_LINK}/product_versions.${PANGEA_LINK}_bmcpre.conf
    PRE_JBOD_SAS_EXP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOD_SAS_EXP/product_versions.PANGEA_V6R1C00_JBOD_SAS_EXP_bmcpre.conf
    PRE_JBOF_PALM_EXP=/startup_disk/image/${PRODUCT_SWOFWARE_VER}/system/firmware/PANGEA_V6R1C00_JBOF_PALM_EXP/product_versions.PANGEA_V6R1C00_JBOF_PALM_EXP_bmcpre.conf
}

FILE_BACKUP()
{
    CUR=$1
    PRE=$2
    BAK=$3
    
    if [ -f $BAK ]; then
        cp $CUR $PRE
        OS_echo "[BMC]bak is exit, backup pre fw file"
    else
        if [ -f $PRE ]; then
            cp $PRE $BAK
            cp $CUR $PRE
            OS_echo "[BMC]pre is exit, modify to bak fw file"
        else    
            cp $CUR $BAK
            OS_echo "[BMC]pre/bak is not exit, backup bak fw file"
        fi
    fi
}

FILE_MV()
{
    CUR=$1
    PRE=$2
    BAK=$3
    
    if [ -f $BAK ]; then
        mv $CUR $PRE
        OS_echo "[BMC]bak is exit, backup pre fw file"
    else
        if [ -f $PRE ]; then
            mv $PRE $BAK
            mv $CUR $PRE
            OS_echo "[BMC]pre is exit, modify to bak fw file"
        else    
            mv $CUR $BAK
            OS_echo "[BMC]pre/bak is not exit, backup bak fw file"
        fi
    fi
}

COPY_CONFFILE_FOREXP()
{
    if [[ $PANGEA_LINK != $PRODUCTVER_JBOD_EXP && $PANGEA_LINK != $PRODUCTVER_JBOF_EXP ]] ; then
        #备份version文件
        FILE_BACKUP $JBOD_SAS_EXP $PRE_JBOD_SAS_EXP $BK_JBOD_SAS_EXP
        FILE_BACKUP $JBOF_PALM_EXP $PRE_JBOF_PALM_EXP $BK_JBOF_PALM_EXP

        #删除checksum
        sed -i "/product_versions.PANGEA_V6R1C00_JBOD_SAS_EXP.conf/d" $CHECKSUM_SYNC
        sed -i "/product_versions.PANGEA_V6R1C00_JBOF_PALM_EXP.conf/d" $CHECKSUM_SYNC

        if [ $REPLACE_NEED_1711 == 1 ]; then
            fwnumjbod1711=`grep -n -A 1 "bmc_1711.bin" "$JBOD_SAS_EXP" | tail -1 | awk -F "-" '{print $1}'`
            fwverjbod1711=`grep -n -A 1 "bmc_1711.bin" "$JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumjbod1711 s/$fwverjbod1711/$FW_BMC_VER/" $JBOD_SAS_EXP
            fwnumjbof1711=`grep -n -A 1 "bmc_1711.bin" "$JBOF_PALM_EXP" | tail -1 | awk -F "-" '{print $1}'`
            fwverjbof1711=`grep -n -A 1 "bmc_1711.bin" "$JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumjbof1711 s/$fwverjbof1711/$FW_BMC_VER/" $JBOF_PALM_EXP
        fi

        if [ $REPLACE_NEED_1710 == 1 ]; then
            fwnumjbod=`grep -n -A 1 "bmc.cramfs" "$JBOD_SAS_EXP" | tail -1 | awk -F "-" '{print $1}'`
            fwverjbod=`grep -n -A 1 "bmc.cramfs" "$JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumjbod s/$fwverjbod/$FW_BMC_VER/" $JBOD_SAS_EXP
            fwnumjbof=`grep -n -A 1 "bmc.cramfs" "$JBOF_PALM_EXP" | tail -1 | awk -F "-" '{print $1}'`
            fwverjbof=`grep -n -A 1 "bmc.cramfs" "$JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumjbof s/$fwverjbof/$FW_BMC_VER/" $JBOF_PALM_EXP
        fi

        #记录配套表的sha256
        local_sum_conf_jbod=`sha256sum ./system/firmware/PANGEA_V6R1C00_JBOD_SAS_EXP/product_versions.PANGEA_V6R1C00_JBOD_SAS_EXP.conf`
        OS_echo "jbod bmc version file local_sum_conf_jbod:${local_sum_conf_jbod:0:80}"
        local_sum_conf_jbof=`sha256sum ./system/firmware/PANGEA_V6R1C00_JBOF_PALM_EXP/product_versions.PANGEA_V6R1C00_JBOF_PALM_EXP.conf`
        OS_echo "jbof bmc version file local_sum_conf_jbof:${local_sum_conf_jbof:0:80}"
    fi

    return
}

ROLLBACK_CONFFILE_FOREXP()
{
    if [[ $PANGEA_LINK != $PRODUCTVER_JBOD_EXP && $PANGEA_LINK != $PRODUCTVER_JBOF_EXP ]] ; then
        if [ "$ROLLBACK_DIR" == "None" ]; then
            OS_echo "bmc rollback exp action is delete, dir is $ROLLBACK_DIR."
            if [ -f $BK_JBOD_SAS_EXP ]; then
                jbodnum=`grep -n -A 1 "bmc.cramfs" "$JBOD_SAS_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jbodfw=`grep -n -A 1 "bmc.cramfs" "$JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jbodorg=`grep -n -A 1 "bmc.cramfs" "$BK_JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbodnum s/$jbodfw/$jbodorg/" $JBOD_SAS_EXP

                jbodnum1711=`grep -n -A 1 "bmc_1711.bin" "$JBOD_SAS_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jbodfw1711=`grep -n -A 1 "bmc_1711.bin" "$JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jbodorg1711=`grep -n -A 1 "bmc_1711.bin" "$BK_JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbodnum1711 s/$jbodfw1711/$jbodorg1711/" $JBOD_SAS_EXP

                rm -rf $BK_JBOD_SAS_EXP
                rm -rf $PRE_JBOD_SAS_EXP
                OS_echo "[BMC]roll back bak jbod file."
            else
                OS_echo "bmc rollback jbod action is delete, but bak file is not here."
            fi

            if [ -f $BK_JBOF_PALM_EXP ]; then
                jbofnum=`grep -n -A 1 "bmc.cramfs" "$JBOF_PALM_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jboffw=`grep -n -A 1 "bmc.cramfs" "$JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jboforg=`grep -n -A 1 "bmc.cramfs" "$BK_JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbofnum s/$jboffw/$jboforg/" $JBOF_PALM_EXP

                jbofnum1711=`grep -n -A 1 "bmc_1711.bin" "$JBOF_PALM_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jboffw1711=`grep -n -A 1 "bmc_1711.bin" "$JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jboforg1711=`grep -n -A 1 "bmc_1711.bin" "$BK_JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbofnum1711 s/$jboffw1711/$jboforg1711/" $JBOF_PALM_EXP

                rm -rf $BK_JBOF_PALM_EXP
                rm -rf $PRE_JBOF_PALM_EXP
                OS_echo "[BMC]roll back bak jbof file."
            else
                OS_echo "bmc rollback jbof action is delete, but bak file is not here."
            fi
        else
            OS_echo "bmc rollback exp action is rollback, dir is $ROLLBACK_DIR."
            if [ -f $PRE_JBOD_SAS_EXP ]; then
                jbodnum=`grep -n -A 1 "bmc.cramfs" "$JBOD_SAS_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jbodfw=`grep -n -A 1 "bmc.cramfs" "$JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jbodorg=`grep -n -A 1 "bmc.cramfs" "$PRE_JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbodnum s/$jbodfw/$jbodorg/" $JBOD_SAS_EXP

                jbodnum1711=`grep -n -A 1 "bmc_1711.bin" "$JBOD_SAS_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jbodfw1711=`grep -n -A 1 "bmc_1711.bin" "$JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jbodorg1711=`grep -n -A 1 "bmc_1711.bin" "$PRE_JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbodnum1711 s/$jbodfw1711/$jbodorg1711/" $JBOD_SAS_EXP

                rm $PRE_JBOD_SAS_EXP
                OS_echo "[BMC]roll back pre jbod conf file."
            elif [ -f $BK_JBOD_SAS_EXP ]; then
                jbodnum=`grep -n -A 1 "bmc.cramfs" "$JBOD_SAS_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jbodfw=`grep -n -A 1 "bmc.cramfs" "$JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jbodorg=`grep -n -A 1 "bmc.cramfs" "$BK_JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbodnum s/$jbodfw/$jbodorg/" $JBOD_SAS_EXP

                jbodnum1711=`grep -n -A 1 "bmc_1711.bin" "$JBOD_SAS_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jbodfw1711=`grep -n -A 1 "bmc_1711.bin" "$JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jbodorg1711=`grep -n -A 1 "bmc_1711.bin" "$BK_JBOD_SAS_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbodnum1711 s/$jbodfw1711/$jbodorg1711/" $JBOD_SAS_EXP

                rm $BK_JBOD_SAS_EXP
                OS_echo "[BMC]roll back bak jbod conf file."
            else
                OS_echo "[BMC]Not find bmc jbod backup file."
            fi

            if [ -f $PRE_JBOF_PALM_EXP ]; then
                jbofnum=`grep -n -A 1 "bmc.cramfs" "$JBOF_PALM_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jboffw=`grep -n -A 1 "bmc.cramfs" "$JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jboforg=`grep -n -A 1 "bmc.cramfs" "$PRE_JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbofnum s/$jboffw/$jboforg/" $JBOF_PALM_EXP

                jbofnum1711=`grep -n -A 1 "bmc_1711.bin" "$JBOF_PALM_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jboffw1711=`grep -n -A 1 "bmc_1711.bin" "$JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jboforg1711=`grep -n -A 1 "bmc_1711.bin" "$PRE_JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbofnum1711 s/$jboffw1711/$jboforg1711/" $JBOF_PALM_EXP

                rm $PRE_JBOF_PALM_EXP
                OS_echo "[BMC]roll back pre jbof conf file."
            elif [ -f $BK_JBOF_PALM_EXP ]; then
                jbofnum=`grep -n -A 1 "bmc.cramfs" "$JBOF_PALM_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jboffw=`grep -n -A 1 "bmc.cramfs" "$JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jboforg=`grep -n -A 1 "bmc.cramfs" "$BK_JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbofnum s/$jboffw/$jboforg/" $JBOF_PALM_EXP

                jbofnum1711=`grep -n -A 1 "bmc_1711.bin" "$JBOF_PALM_EXP" | tail -1 | awk -F "-" '{print $1}'`
                jboffw1711=`grep -n -A 1 "bmc_1711.bin" "$JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                jboforg1711=`grep -n -A 1 "bmc_1711.bin" "$BK_JBOF_PALM_EXP" | tail -1 | awk -F "=" '{print $2}'`
                sed -i "$jbofnum1711 s/$jboffw1711/$jboforg1711/" $JBOF_PALM_EXP

                rm $BK_JBOF_PALM_EXP
                OS_echo "[BMC]roll back bak jbof conf file."
            else
                OS_echo "[BMC]Not find bmc jbof backup file."
            fi
        fi
    fi
    return
}

BMC_MGMT_FW_UPLOAD()
{
swm_diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
fw upload 7 0
quit
EOF
}

BMC_MGMT_FW_START()
{
swm_diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
fw upgrade 7 0
quit
EOF
}

UPGRADE_MGMT_BMC()
{
    if [[ $SPECIAL == 1 ]];then
        OS_echo "[bmc patch] special upgrade mgt succ."
        return 0
    fi

    #系统上电中，不做管理板的升级
    app_num=`ps -ef | grep app_data | wc -l`
    devm_num=`ps -ef | grep devm | wc -l`
    if [[ $app_num -lt 2 && $devm_num -lt 2 ]] ;then
        OS_echo "[BMC]system is during power on, return."
        return 0
    fi

    rm -rf $UPG_HP_TMP_PATCH_FILE
    if [[ ${PANGEA_LINK} == $PRODUCTVER_4U4C_C || ${PANGEA_LINK} == $PRODUCTVER_4U4C_CP ]] ; then
        OS_echo "[bmc] start to upload mgmt bmc."
        BMC_MGMT_FW_UPLOAD
        checkResult=`cat $UPG_HP_TMP_PATCH_FILE | grep -i "success"`
        if [ -z "$checkResult" ]; then
            OS_echo "[bmc]BMC_MGMT_FW_UPLOAD failed."
            rm -rf $UPG_HP_TMP_PATCH_FILE
            mount -o remount,ro /startup_disk/image
            exit 1
        fi

        OS_echo "[bmc] start to upgrade mgmt bmc."
        BMC_MGMT_FW_START
        checkResult=`cat $UPG_HP_TMP_PATCH_FILE | grep -i "success"`
        if [ -z "$checkResult" ]; then
            OS_echo "[bmc]BMC_MGMT_FW_START failed."
            rm -rf $UPG_HP_TMP_PATCH_FILE
            mount -o remount,ro /startup_disk/image
            exit 1
        fi
    fi

    rm -rf $UPG_HP_TMP_PATCH_FILE
    return 0
}

REPLACE_BMCFW_VERSION()
{
    patch_path=$(readlink -f "$(dirname "$0")")
    cp -rf ${patch_path}/${FW_PACKAGE_NAME}.tgz /OSM/${FW_PACKAGE_NAME}.tgz
    rm -rf /OSM/modules/BMCtemp
    mkdir -p /OSM/modules/BMCtemp
    tar -zxvf /OSM/${FW_PACKAGE_NAME}.tgz -C /OSM/modules/BMCtemp

    #比较C+固件
    if [ -f /OSM/modules/BMCtemp/bmc_1711.bin ]; then
        cmp ${FW_BMC_PATH_1711} /OSM/modules/BMCtemp/bmc_1711.bin
        if [ "$?" == "0" ] ; then 
            OS_echo "[bmc] BMC-1711 Firmware already replaced."
            REPLACE_NEED_1711=0
        else
            OS_echo "[bmc] BMC-1711 need replace."
            REPLACE_NEED_1711=1
        fi
    fi

    #比较C固件
    if [ -f /OSM/modules/BMCtemp/bmc.cramfs ]; then
        if [[ $PANGEA_LINK != $PRODUCTVER_JBOD_EXP && $PANGEA_LINK != $PRODUCTVER_JBOF_EXP ]] ; then
            cmp ${FW_BMC_PATH} /OSM/modules/BMCtemp/bmc.cramfs
            if [ "$?" == "0" ] ; then 
                OS_echo "[bmc] BMC-1710 ctrl Firmware already replaced."
                REPLACE_NEED_1710=0
            else
                OS_echo "[bmc] BMC-1710 ctrl need replace."
                REPLACE_NEED_1710=1
            fi
        fi

        #特殊处理，V5R7C60SPC300包含之前的版本需要独立备份磁盘框的固件
        if [[ $PANGEA_LINK == $PRODUCTVER_JBOD_EXP || $PANGEA_LINK == $PRODUCTVER_JBOF_EXP ]] ; then
            if [ ! -h $FW_BMC_PATH_IP ]; then 
                cmp ${FW_BMC_PATH_IP} /OSM/modules/BMCtemp/bmc.cramfs
                if [ "$?" == "0" ] ; then 
                    OS_echo "[bmc] BMC-1710 IP Firmware already replaced for SPC300."
                    REPLACE_NEED_1710=0
                else
                    OS_echo "[bmc] BMC-1710 IP need replace for SPC300."
                    REPLACE_NEED_1710=1
                fi
            else
                cmp ${FW_BMC_PATH} /OSM/modules/BMCtemp/bmc.cramfs
                if [ "$?" == "0" ] ; then 
                    OS_echo "[bmc] BMC-1710 IP Firmware already replaced."
                    REPLACE_NEED_1710=0
                else
                    OS_echo "[bmc] BMC-1710 IP need replace."
                    REPLACE_NEED_1710=1
                fi
            fi
        fi
    fi

    if [[ $REPLACE_NEED_1710 == 0 && $REPLACE_NEED_1711 == 0 ]] ; then
        OS_echo "[bmc] ALL Firmware already replaced, return."
        rm -rf /OSM/modules/BMCtemp
        return 0
    fi

    mount -o remount,rw /startup_disk/image
    #备份version文件
    FILE_BACKUP $SYS_VERSION_FILE $PRE_SYS_VERSION_FILE $BK_SYS_VERSION_FILE
    sed -i "/product_versions.${PANGEA_LINK}.conf/d" $CHECKSUM_SYNC

    #执行C的处理
    if [ $REPLACE_NEED_1710 == 1 ]; then
        #特殊处理，V5R7C60SPC300包含之前的版本，IP框固件和控制框不在一个目录
        if [[ $PANGEA_LINK != $PRODUCTVER_JBOD_EXP && $PANGEA_LINK != $PRODUCTVER_JBOF_EXP ]] ; then
            FILE_BACKUP $FW_BMC_PATH $PRE_FW_BMC_PATH $BK_FW_BMC_PATH
            cp /OSM/modules/BMCtemp/bmc.cramfs $FW_BMC_PATH
            OS_echo "[BMC]replace ctrl BMC firmware"
        fi

        sed -i "/bmc.cramfs/d" $CHECKSUM_SYNC
        #特殊处理，V5R7C60SPC300包含之前的版本需要独立备份磁盘框的固件
        if [[ $PANGEA_LINK == $PRODUCTVER_JBOD_EXP || $PANGEA_LINK == $PRODUCTVER_JBOF_EXP ]] ; then
            if [ ! -h $FW_BMC_PATH_IP ]; then 
                FILE_BACKUP $FW_BMC_PATH_IP $PRE_FW_BMC_PATH_IP $BK_FW_BMC_PATH_IP
                cp /OSM/modules/BMCtemp/bmc.cramfs $FW_BMC_PATH_IP
                OS_echo "[BMC]replace IP frame BMC firmware for SPC300."
            else
                FILE_BACKUP $FW_BMC_PATH $PRE_FW_BMC_PATH $BK_FW_BMC_PATH
                cp /OSM/modules/BMCtemp/bmc.cramfs $FW_BMC_PATH
                OS_echo "[BMC]replace IP frame BMC firmware normal."
            fi
        fi

        fwnum=`grep -n -A 1 "bmc.cramfs" "$SYS_VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
        fwver=`grep -n -A 1 "bmc.cramfs" "$SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
        sed -i "$fwnum s/$fwver/$FW_BMC_VER/" $SYS_VERSION_FILE
    fi

    #执行C+的处理
    if [ $REPLACE_NEED_1711 == 1 ]; then
        FILE_BACKUP $FW_BMC_PATH_1711 $PRE_FW_BMC_PATH_1711 $BK_FW_BMC_PATH_1711
        OS_echo "[BMC] ------cp bmc_1711.bin to startup_disk .------"
        mv /OSM/modules/BMCtemp/bmc_1711.bin $FW_BMC_PATH_1711
        sed -i "/bmc_1711.bin/d" $CHECKSUM_SYNC

        fwnum1711=`grep -n -A 1 "bmc_1711.bin" "$SYS_VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
        fwver1711=`grep -n -A 1 "bmc_1711.bin" "$SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
        sed -i "$fwnum1711 s/$fwver1711/$FW_BMC_VER/" $SYS_VERSION_FILE
    fi

    #记录BMC固件文件的sha256
    cd /startup_disk/image/${PRODUCT_SWOFWARE_VER}/
    local_sum_bmc=`sha256sum ./system/firmware/manage_chip/bmc.cramfs`
    OS_echo "bmc.cramfs local_sum_bmc:$local_sum_bmc"
    local_sum_bmc1711=`sha256sum ./system/firmware/manage_chip/bmc_1711.bin`
    OS_echo "bmc_1711.bin local_sum_bmc1711:$local_sum_bmc1711"
    #记录配套表的sha256
    local_sum_conf=`sha256sum ./system/firmware/${PANGEA_LINK}/product_versions.${PANGEA_LINK}.conf`
    OS_echo "bmc version local_sum_conf:${local_sum_conf:0:80}"

    #非磁盘框设备，需要同时替换磁盘框的配套表
    COPY_CONFFILE_FOREXP

    #升级管理板BMC，6.1.2管理板升级处于后台升级，不在脚本中执行
    #UPGRADE_MGMT_BMC

    cd -
    mount -o remount,ro /startup_disk/image
    rm -rf /OSM/modules/BMCtemp
    return
}

RESUME_FW()
{
    local backFile=$1
    local expectFileLen=$2
    local curFile=$3
    local fileLen=0

    #恢复固件的时候判断是否需要进行恢复
    #配套表存在,固件不存在或大小不足,说明备份的时候还没来得及备份完系统就异常复位了，此时的当前固件不需要回退
    if [ -f $backFile ]; then 
        fileLen=`stat -c %s $backFile`
        if [ $fileLen  -lt $expectFileLen ]; then
            OS_echo "[BMC] back file err, start to delete."
            rm $backFile
        else
            OS_echo "[BMC] back file OK, start to move."
            mv $backFile $curFile
            fi
    fi
}

ROLLBACK_BMCFW_VERSION()
{
    OS_echo "bmc rollback begin"
    mount -o remount,rw /startup_disk/image

    if [ "$ROLLBACK_DIR" == "None" ]; then
        OS_echo "bmc rollback action is delete, dir is $ROLLBACK_DIR."
        if [ -f $BK_SYS_VERSION_FILE ]; then
            fwnumcur=`grep -n -A 1 "bmc.cramfs" "$SYS_VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            fwvercur=`grep -n -A 1 "bmc.cramfs" "$SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwverorg=`grep -n -A 1 "bmc.cramfs" "$BK_SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumcur s/$fwvercur/$fwverorg/" $SYS_VERSION_FILE

            fwnumcur1711=`grep -n -A 1 "bmc_1711.bin" "$SYS_VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            fwvercur1711=`grep -n -A 1 "bmc_1711.bin" "$SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwverorg1711=`grep -n -A 1 "bmc_1711.bin" "$BK_SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumcur1711 s/$fwvercur1711/$fwverorg1711/" $SYS_VERSION_FILE

            rm $BK_SYS_VERSION_FILE
            mv $BK_FW_BMC_PATH $FW_BMC_PATH
            mv $BK_FW_BMC_PATH_1711 $FW_BMC_PATH_1711
            rm -rf $PRE_FW_BMC_PATH
            rm -rf $PRE_FW_BMC_PATH_1711
            rm -rf $PRE_SYS_VERSION_FILE
            OS_echo "[BMC]roll back bak fw/conf file."
        else
            OS_echo "bmc rollback action is delete, but bak file is not here."
        fi
    else
        OS_echo "bmc rollback action is rollback, dir is $ROLLBACK_DIR."
        if [ -f $PRE_SYS_VERSION_FILE ]; then
            fwnumcur=`grep -n -A 1 "bmc.cramfs" "$SYS_VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            fwvercur=`grep -n -A 1 "bmc.cramfs" "$SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwverorg=`grep -n -A 1 "bmc.cramfs" "$PRE_SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumcur s/$fwvercur/$fwverorg/" $SYS_VERSION_FILE

            fwnumcur1711=`grep -n -A 1 "bmc_1711.bin" "$SYS_VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            fwvercur1711=`grep -n -A 1 "bmc_1711.bin" "$SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwverorg1711=`grep -n -A 1 "bmc_1711.bin" "$PRE_SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumcur1711 s/$fwvercur1711/$fwverorg1711/" $SYS_VERSION_FILE 

            rm $PRE_SYS_VERSION_FILE
            mv $PRE_FW_BMC_PATH $FW_BMC_PATH
            mv $PRE_FW_BMC_PATH_1711 $FW_BMC_PATH_1711
            #RESUME_FW $PRE_FW_BMC_PATH 26000000 $FW_BMC_PATH
            #RESUME_FW $PRE_FW_BMC_PATH_1711 35000000 $FW_BMC_PATH_1711
            OS_echo "[BMC]roll back pre fw/conf file."
        elif [ -f $BK_SYS_VERSION_FILE ]; then
            fwnumcur=`grep -n -A 1 "bmc.cramfs" "$SYS_VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            fwvercur=`grep -n -A 1 "bmc.cramfs" "$SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwverorg=`grep -n -A 1 "bmc.cramfs" "$BK_SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumcur s/$fwvercur/$fwverorg/" $SYS_VERSION_FILE

            fwnumcur1711=`grep -n -A 1 "bmc_1711.bin" "$SYS_VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            fwvercur1711=`grep -n -A 1 "bmc_1711.bin" "$SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwverorg1711=`grep -n -A 1 "bmc_1711.bin" "$BK_SYS_VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            sed -i "$fwnumcur1711 s/$fwvercur1711/$fwverorg1711/" $SYS_VERSION_FILE

            rm $BK_SYS_VERSION_FILE
            mv $BK_FW_BMC_PATH $FW_BMC_PATH
            mv $BK_FW_BMC_PATH_1711 $FW_BMC_PATH_1711
            #RESUME_FW $BK_FW_BMC_PATH 26000000 $FW_BMC_PATH
            #RESUME_FW $BK_FW_BMC_PATH_1711 35000000 $FW_BMC_PATH_1711
            OS_echo "[BMC]roll back bak fw/conf file."
        else
            OS_echo "[BMC]Not find bmc backup file."
        fi
    fi

    #记录BMC固件的sha256
    cd /startup_disk/image/${PRODUCT_SWOFWARE_VER}/
    local_sum_bmc_back=`sha256sum ./system/firmware/manage_chip/bmc.cramfs`
    OS_echo "bmc.cramfs local_sum_bmc_back:$local_sum_bmc_back"
    local_sum_bmc_back1711=`sha256sum ./system/firmware/manage_chip/bmc_1711.bin`
    OS_echo "bmc_1711.bin local_sum_bmc_back1711:$local_sum_bmc_back1711"
    #记录配套表的sha256
    local_sum_conf_back=`sha256sum ./system/firmware/${PANGEA_LINK}/product_versions.${PANGEA_LINK}.conf`
    OS_echo "bmc version local_sum_conf_back:${local_sum_conf_back:0:80}"

    #非磁盘框设备，需要同时回退磁盘框的配套表
    ROLLBACK_CONFFILE_FOREXP

    #回退管理板BMC，6.1.2管理板升级处于后台升级，不在脚本中执行
    #UPGRADE_MGMT_BMC

    cd -
    mount -o remount,ro /startup_disk/image
    rm -rf /OSM/modules/BMCtemp
    OS_echo "bmc rollback end"
    return
}

PATCH_START()
{
    REPLACE_BMCFW_VERSION
    return
}

PATCH_END()
{
    return
}

PATCH_ROLLBACK()
{
    ROLLBACK_BMCFW_VERSION
    return
}

PATCH_ROLLEND()
{
    return
}

BMC_START()
{
    if [ -d "/startup_disk/image/6200819073" ]; then
        INIT_FW_PATCH "6200819073"
        PATCH_START
    fi
    if [ -d "/startup_disk/image/6200819074" ]; then
        INIT_FW_PATCH "6200819074"
        PATCH_START
    fi
}

BMC_ROLLBACK()
{
    if [ -d "/startup_disk/image/6200819073" ]; then
        INIT_FW_PATCH "6200819073"
        PATCH_ROLLBACK
    fi
    if [ -d "/startup_disk/image/6200819074" ]; then
        INIT_FW_PATCH "6200819074"
        PATCH_ROLLBACK
    fi
}

L_CMD=$1
case $L_CMD in
    "start")
        SPECIAL=0
        BMC_START
        ;;
    "end")
        PATCH_END
        ;;
    "rollstart")
        SPECIAL=0
        BMC_ROLLBACK
        ;;
    "rollend")
        PATCH_ROLLEND
        ;;
    "special_upgrade")
        SPECIAL=1
        BMC_START
        ;;
    "special_resume")
        SPECIAL=1
        BMC_ROLLBACK
        ;;
    *)
        OS_echo "[BMC patch]unsupported option."
esac
exit 0
