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

###【使用说明】###
#1、此脚本用于正常安装/删除补丁过程中的1822固件和配套表替换以及固件版本的升级
#2、此脚本涉及的修改点如下：
#1）BASE_PATCH_PKG_VER，产品软件版本号，决定了固件所在盘上的位置，在打补丁时传递，修改对应REPLACE/ROLLBACK_FILE函数中的数字版本号，应对apollo版本切换
#2）FW_1822_VER版本号，当前此脚本支持C+安全卡、C+备件卡、C卡(高端/低端)热升级，不同卡出不同的固件对应不同的版本号
#3）目前此脚本只支持高中低端不同类型的硬件形态，无论什么形态，针对同一类型的1822卡件（C+安全/C+非安全/C）均为同一固件
#4）FW_1822_PATCH_FILE：用户提供的固件包里的1822固件，需要包含当前产品环境能适配的1822卡件对应的固件
#5）如果需要支持V6 C+新增的形态，需要同时增加新的硬件形态定义并在GET_PLATFORM_TYPE函数中增加硬件形态所属（高中低端）适配
#6）从SPH107开始回退和删除补丁通过判断"$ROLLBACK_DIR" 环境变量来决定，如果"$ROLLBACK_DIR" == "None"则为删除补丁，否则则为回退补丁.因此只用保留最新补丁脚本的回退函数即可
#7）补丁安装升级过程中失败后，调用脚本做自回退操作（注意：只回退备份文件，不做升级，并最后返回错误码）
#8）改写系统盘文件时，不再等PXE等服务准备就绪，改步骤由升级组件保障（删除补丁脚本中的等待服务事件）
#9）改写系统盘文件遵循CP+MV操作流程步骤

patch_path=$(readlink -f "$(dirname "$0")")
#patch_path=/home/ibc_os_hs/tmp
OS_echo "[1822_hot_patch]V5R7C73SPH110 cur patch_path $patch_path"
#热补丁相关临时文件
UPG_HP_TMP_PATCH_FILE="/tmp/hotpatch_tmp_file"

##适配的硬件形态定义
#高端
PRODUCTVER_4U4C="PANGEA_V6R1C00_HI1620_4U4C"
PANGEA_V6R3C00_HI1620_4U4C="PANGEA_V6R3C00_HI1620_4U4C"
PANGEA_V6R3C10_HI1620_4U4C="PANGEA_V6R3C10_HI1620_4U4C"
#中端
PRODUCTVER_2U2C_PALM="PANGEA_V6R1C00_HI1620_2U2C_PALM"
PRODUCTVER_2U2C_SAS="PANGEA_V6R1C00_HI1620_2U2C_SAS"
PANGEA_V6R3C00_HI1620_2U2C_PALM="PANGEA_V6R3C00_HI1620_2U2C_PALM"
PANGEA_V6R3C00_HI1620_2U2C_SAS="PANGEA_V6R3C00_HI1620_2U2C_SAS"
#低端
PRODUCTVER_HI1620S_2U2C_SAS="PANGEA_V6R1C00_HI1620S_2U2C_SAS"
PANGEA_V6R3C00_HI1620S_2U2C_NVME="PANGEA_V6R3C00_HI1620S_2U2C_NVME"
PANGEA_V6R3C00_HI1620S_2U2C_SAS="PANGEA_V6R3C00_HI1620S_2U2C_SAS"
PANGEA_V6R3C30_HI1620S_1P1H_2U2C_SAS="PANGEA_V6R3C30_HI1620S_1P1H_2U2C_SAS"
PANGEA_V6R3C30_HI1620S_1P_2U2C_SAS="PANGEA_V6R3C30_HI1620S_1P_2U2C_SAS"
PANGEA_V6R3C30_HI1620S_1P1L_2U2C_SAS="PANGEA_V6R3C30_HI1620S_1P1L_2U2C_SAS"

#1822固件升级模式
FW_1822_MODE=4
#1822固件补丁文件
FW_1822_PATCH_FILE=USER_V5R7C73SPC100FW_1822.tgz
#1822固件版本号
FW_1822_VER_HIGH=2.0.23.81.0701
FW_1822_VER_CP=2.1.3.81.0701
FW_1822_VER_CP_NSB=2.1.2.81.0701

#产品版本编号
PKG_VERSION=6200819073

FW_1822_VER=$FW_1822_VER_HIGH
PLATFORM_HIGH=1
SPECIAL_UPGRADE=0
INSTALL_HOTPATCH=0
#使用临时变量存储全局变量ROLLBACK_DIR值
rollback_path=$ROLLBACK_DIR

#平台类型
PANGEA_LINK=`cat /proc/osp/bsp | awk '/Product/ {print $4}'`

INIT_FW_PATCH()
{
    #系统版本号 考虑到产品环境会切A包（A0/A1）固件补丁均兼容，接收传入的具体系统版本号（数字）到指定系统包
    BASE_PATCH_PKG_VER=$1 
    OS_echo "[1822_hot_patch]cur base_patch_pkg_ver is $BASE_PATCH_PKG_VER"
    #版本配套表
    VERSION_FILE=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/${PANGEA_LINK}/product_versions.${PANGEA_LINK}.conf
    #1822固件
    FW_1822=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/port/Hi1822_fw.bin
    FW_1822_CP=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/cplus/port/Hi1822_cp_fw.bin
    FW_1822_CP_NSB=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/cplus_nsb/port/Hi1822_cp_nsb_fw.bin

    #校验和
    CHECKSUM_SYNC=/startup_disk/image/$BASE_PATCH_PKG_VER/system/sha256sum_sync

    #【重要】版本配套表备份文件，此备份文件为1822接口卡版本号备份，文件命名需要独立备份。
    VERSION_FILE_ORIGIN=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/${PANGEA_LINK}/product_versions.${PANGEA_LINK}.1822_origin.conf
    VERSION_FILE_PRE=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/${PANGEA_LINK}/product_versions.${PANGEA_LINK}.1822_pre.conf

    #1822固件备份文件
    FW_1822_ORIGIN=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/port/Hi1822_fw_origin.bin
    FW_1822_PRE=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/port/Hi1822_fw_pre.bin

    FW_1822_CP_ORIGIN=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/cplus/port/Hi1822_cp_fw_origin.bin
    FW_1822_CP_PRE=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/cplus/port/Hi1822_cp_fw_pre.bin

    FW_1822_CP_NSB_ORIGIN=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/cplus_nsb/port/Hi1822_cp_nsb_fw_origin.bin
    FW_1822_CP_NSB_PRE=/startup_disk/image/$BASE_PATCH_PKG_VER/system/firmware/cplus_nsb/port/Hi1822_cp_nsb_fw_pre.bin
    
    #1822固件临时备份文件
    FW_1822_ORIGIN_TEMP=/OSM/modules/1822temp/Hi1822_fw_origin_temp.bin
    FW_1822_PRE_TEMP=/OSM/modules/1822temp/Hi1822_fw_pre_temp.bin

    FW_1822_CP_ORIGIN_TEMP=/OSM/modules/1822temp/Hi1822_cp_fw_origin_temp.bin
    FW_1822_CP_PRE_TEMP=/OSM/modules/1822temp/Hi1822_cp_fw_pre_temp.bin

    FW_1822_CP_NSB_ORIGIN_TEMP=/OSM/modules/1822temp/Hi1822_cp_nsb_fw_origin_temp.bin
    FW_1822_CP_NSB_PRE_TEMP=/OSM/modules/1822temp/Hi1822_cp_nsb_fw_pre_temp.bin
    #临时备份配套表
    VERSION_FILE_ORIGIN_TEMP=/OSM/modules/1822temp/product_versions.${PANGEA_LINK}.1822_origin_temp.conf
    VERSION_FILE_PRE_TEMP=/OSM/modules/1822temp/product_versions.${PANGEA_LINK}.1822_pre_temp.conf
}

GET_PLATFORM_TYPE()
{
    #检查当前运行环境硬件形态所属-->高/中/低端
    if [[ ${PANGEA_LINK} == $PRODUCTVER_4U4C || ${PANGEA_LINK} == $PANGEA_V6R3C00_HI1620_4U4C || ${PANGEA_LINK} == $PANGEA_V6R3C10_HI1620_4U4C ]];then
        PLATFORM_HIGH=1
        #FW_1822_VER=$FW_1822_VER_HIGH
        OS_echo "[1822_hot_patch]platform is high: $PANGEA_LINK, $FW_1822_VER"
    elif [[ ${PANGEA_LINK} == $PRODUCTVER_2U2C_PALM || ${PANGEA_LINK} == $PRODUCTVER_2U2C_SAS || ${PANGEA_LINK} == $PANGEA_V6R3C00_HI1620_2U2C_PALM || ${PANGEA_LINK} == $PANGEA_V6R3C00_HI1620_2U2C_SAS ]];then
        PLATFORM_HIGH=0
        #FW_1822_VER=$FW_1822_VER_MIDDLE_LOW
        OS_echo "[1822_hot_patch]platform is middle:$PANGEA_LINK, $FW_1822_VER"
    elif [[ ${PANGEA_LINK} == $PRODUCTVER_HI1620S_2U2C_SAS || ${PANGEA_LINK} == $PANGEA_V6R3C00_HI1620S_2U2C_NVME || ${PANGEA_LINK} == $PANGEA_V6R3C00_HI1620S_2U2C_SAS ]];then
        PLATFORM_HIGH=0
        #FW_1822_VER=$FW_1822_VER_MIDDLE_LOW
        OS_echo "[1822_hot_patch]platform is low:$PANGEA_LINK, $FW_1822_VER"
    elif [[ ${PANGEA_LINK} == $PANGEA_V6R3C30_HI1620S_1P1H_2U2C_SAS || ${PANGEA_LINK} == $PANGEA_V6R3C30_HI1620S_1P_2U2C_SAS || ${PANGEA_LINK} == $PANGEA_V6R3C30_HI1620S_1P1L_2U2C_SAS ]];then
        PLATFORM_HIGH=0
        #FW_1822_VER=$FW_1822_VER_MIDDLE_LOW
        OS_echo "[1822_hot_patch]platform is low:$PANGEA_LINK, $FW_1822_VER"
    else
        OS_echo "[1822_hot_patch]platform is unknown:$PANGEA_LINK"
        exit 0
    fi
}



PANGEA_FwUploadStart_high()
{
swm_diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
fw upload 2 2
quit
EOF
}

PANGEA_FwUpgradeStart_high()
{
swm_diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
fw upgrade 2 2
quit
EOF
}

PANGEA_FwUploadStart_middle_low()
{
swm_diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
fw upload 1 2
quit
EOF
}

PANGEA_FwUpgradeStart_middle_low()
{
swm_diagnose --auto << EOF > $UPG_HP_TMP_PATCH_FILE
fw upgrade 1 2
EOF
}

PANGEA_FwUpgradeGetStatus()
{
    echo "success" > $UPG_HP_TMP_PATCH_FILE
}

PANGEA_FwUpgradeVerCheck()
{
    echo "success" > $UPG_HP_TMP_PATCH_FILE
}

# 只在补丁安装失败的场景，进行自回退操作，若是回滚或者删除补丁失败则不进行自回滚操作
AUTO_ROLLBACK()
{
    if [[ $INSTALL_HOTPATCH == 1 ]];then
        INSTALL_HOTPATCH=0
        SPECIAL_UPGRADE=1
        rollback_path="auto_rollback"
        PATCH_ROLLBACK
    fi
    return
}

PANGEA_FwUpgradeMmlCmd()
{
    FW_1822_VER=`cat $VERSION_FILE | awk -v RS="Hi1822_fw.bin" '{print $1}' | awk -F= '/Version/ {print $2}'`

    if [[ $PLATFORM_HIGH == 1 ]];then
        PANGEA_FwUploadStart_high
    else
        PANGEA_FwUploadStart_middle_low
    fi

    checkResult=`cat $UPG_HP_TMP_PATCH_FILE | grep -i "success"`
    OS_echo "[1822_hot_patch]PANGEA_FwUploadStart:$checkResult"
    if [ -z "$checkResult" ]; then
        OS_echo "[1822_hot_patch]hotupgradeupload $FW_1822_VER $FW_1822_MODE failed."
        AUTO_ROLLBACK
        exit 1
    fi
    OS_echo "[1822_hot_patch]hotupgradeupload $FW_1822 $FW_1822_MODE sucessed."

    if [[ $PLATFORM_HIGH == 1 ]];then
        PANGEA_FwUpgradeStart_high
    else
        PANGEA_FwUpgradeStart_middle_low
    fi

    checkResult=`cat $UPG_HP_TMP_PATCH_FILE | grep -i "success"`
    OS_echo "[1822_hot_patch]PANGEA_FwUpgradeStart:$checkResult"
    if [ -z "$checkResult" ]; then
        OS_echo "[1822_hot_patch]hotupgradestart $FW_1822_VER $FW_1822_MODE failed."
        AUTO_ROLLBACK
        exit 1
    fi
    OS_echo "[1822_hot_patch]hotupgradestart $FW_1822 $FW_1822_MODE sucessed."

    PANGEA_FwUpgradeGetStatus
    getstatus=`cat $UPG_HP_TMP_PATCH_FILE | grep "failed"`
    if [ -n "$getstatus" ]; then
        OS_echo "[1822_hot_patch]hotupgradegetstatus failed,getstatus:$getstatus."
        AUTO_ROLLBACK
        exit 1
    fi
    OS_echo "[1822_hot_patch]hotupgradegetstatus sucessed."
    rm $UPG_HP_TMP_PATCH_FILE

    PANGEA_FwUpgradeVerCheck
    vercheck=`cat $UPG_HP_TMP_PATCH_FILE | grep "failed."`
    if [ -n "$vercheck" ]; then
        OS_echo "[1822_hot_patch]hotupgradevercheck failed: $vercheck."
        AUTO_ROLLBACK
        exit 1
    fi
    rm $UPG_HP_TMP_PATCH_FILE
    OS_echo "[1822_hot_patch]hotupgradevercheck sucessed."
    return 0
}

CHECK_PATCH_CONDITION()
{
    return
}

MOVE_FILES()
{
    #FW0
    if [ -f ${FW_1822_PRE_TEMP} ]; then
        mv ${FW_1822_PRE_TEMP} ${FW_1822_PRE}
        L_CP_RES=$?
        if [ ${L_CP_RES} != "0" ];then
            OS_echo "[1822_hot_patch] copy[12] mv firmware fail:$L_CP_RES"
        fi
    fi

    if [ -f ${FW_1822_ORIGIN_TEMP} ]; then
        mv ${FW_1822_ORIGIN_TEMP} ${FW_1822_ORIGIN}
        L_CP_RES=$?
        if [ ${L_CP_RES} != "0" ];then
            OS_echo "[1822_hot_patch] copy[13] mv firmware fail:$L_CP_RES"
        fi
    fi
    #FW3
    if [ -f ${FW_1822_CP_ORIGIN_TEMP} ]; then
        mv ${FW_1822_CP_ORIGIN_TEMP} ${FW_1822_CP_ORIGIN}
        L_CP_RES=$?
        if [ ${L_CP_RES} != "0" ];then
            OS_echo "[1822_hot_patch] copy[14] mv firmware fail:$L_CP_RES"
        fi
    fi
    
    if [ -f ${FW_1822_CP_PRE_TEMP} ]; then
        mv ${FW_1822_CP_PRE_TEMP} ${FW_1822_CP_PRE}
        L_CP_RES=$?
        if [ ${L_CP_RES} != "0" ];then
            OS_echo "[1822_hot_patch] copy[15] mv firmware fail:$L_CP_RES"
        fi
    fi

    #FW1
    if [ -f ${FW_1822_CP_NSB_ORIGIN_TEMP} ]; then
        mv ${FW_1822_CP_NSB_ORIGIN_TEMP} ${FW_1822_CP_NSB_ORIGIN}
        L_CP_RES=$?
        if [ ${L_CP_RES} != "0" ];then
            OS_echo "[1822_hot_patch] copy[16] mv firmware fail:$L_CP_RES"
        fi
    fi
    
    if [ -f ${FW_1822_CP_NSB_PRE_TEMP} ]; then
        mv ${FW_1822_CP_NSB_PRE_TEMP} ${FW_1822_CP_NSB_PRE}
        L_CP_RES=$?
        if [ ${L_CP_RES} != "0" ];then
            OS_echo "[1822_hot_patch] copy[17] mv firmware fail:$L_CP_RES"
        fi
    fi
    #配套表
    if [ -f ${VERSION_FILE_PRE_TEMP} ]; then
        mv ${VERSION_FILE_PRE_TEMP} ${VERSION_FILE_PRE}
        L_CP_RES=$?
        if [ ${L_CP_RES} != "0" ];then
            OS_echo "[1822_hot_patch] copy[18] mv file fail:$L_CP_RES"
        fi
    fi

    if [ -f ${VERSION_FILE_ORIGIN_TEMP} ]; then
        mv ${VERSION_FILE_ORIGIN_TEMP} ${VERSION_FILE_ORIGIN}
        L_CP_RES=$?
        if [ ${L_CP_RES} != "0" ];then
            OS_echo "[1822_hot_patch] copy[19] mv file fail:$L_CP_RES"
        fi
    fi

    return
}

REPLACE_1822FW_FILE()
{
    OS_echo "[1822_hot_patch]replace 1822FW file start."

    rm -rf /OSM/modules/1822temp
    mkdir -p /OSM/modules/1822temp
    tar -zxvf ${patch_path}/${FW_1822_PATCH_FILE} -C /OSM/modules/1822temp

    MODIFY_CONF=0
    #看解压的包中有哪些文件
    #FW0
    if [ -f /OSM/modules/1822temp/Hi1822_fw.bin ] ; then
        FW_TYPE_CH=1
        VERSION_TYPE_CH=1
        cmp ${FW_1822} /OSM/modules/1822temp/Hi1822_fw.bin
        L_CMP_RES=$?
        if [ "${L_CMP_RES}" == "0" ] ; then
            OS_echo "[1822_hot_patch]firmware C already replaced."
            rm /OSM/modules/1822temp/Hi1822_fw.bin
            FW_TYPE_CH=0
        fi

        fwver=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
        if [ $fwver == $FW_1822_VER ] ; then
            OS_echo "[1822_hot_patch]C conf version already replaced."
            MODIFY_CONF=1
            VERSION_TYPE_CH=0
        fi
    fi

    #FW3
    if [ -f /OSM/modules/1822temp/Hi1822_cp_fw.bin ] ; then
        FW_TYPE_CP=1
        VERSION_TYPE_CP=1
        cmp ${FW_1822_CP} /OSM/modules/1822temp/Hi1822_cp_fw.bin
        L_CMP_RES=$?
        if [ "${L_CMP_RES}" == "0" ] ; then
            OS_echo "[1822_hot_patch]firmware CP already replaced."
            rm /OSM/modules/1822temp/Hi1822_cp_fw.bin
            FW_TYPE_CP=0
        fi
        
        fwver=`grep -n -A 2 "HI1822_CP_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
        if [ $fwver == $FW_1822_VER_CP ] ; then
            OS_echo "[1822_hot_patch]CP conf version already replaced."
            MODIFY_CONF=1
            VERSION_TYPE_CP=0
        fi
    fi

    #FW1
    if [ -f /OSM/modules/1822temp/Hi1822_cp_nsb_fw.bin ] ; then
        FW_TYPE_CP_NSB=1
        VERSION_TYPE_CP_NSB=1   #标记版本号是否需要修改，1需要修改，0不需要
        cmp ${FW_1822_CP_NSB} /OSM/modules/1822temp/Hi1822_cp_nsb_fw.bin
        L_CMP_RES=$?
        if [ "${L_CMP_RES}" == "0" ] ; then
            OS_echo "[1822_hot_patch]firmware CP_NSB already replaced."
            rm /OSM/modules/1822temp/Hi1822_cp_nsb_fw.bin
            FW_TYPE_CP_NSB=0
        fi

        fwver=`grep -n -A 2 "HI1822_CP_NSB_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
        if [ $fwver == $FW_1822_VER_CP_NSB ] ; then
            OS_echo "[1822_hot_patch]CP_NSB conf version already replaced."
            MODIFY_CONF=1
            VERSION_TYPE_CP_NSB=0
        fi
    fi

    #pxe_server服务跟ncram服务也会在上电的时候remount系统盘，如果发现有人占住了系统盘会把系统盘杀掉，导致补丁恢复失败；这里等待这2个服务加载完成后再进行操作
    #local pxe_status=
    #local nvram_log_status=
    #local try_times=60
    #while [ $try_times -gt 0 ] ; do
    #   pxe_status=`systemctl status pxe_server | grep -E "FAILURE|SUCCESS"`
    #   nvram_log_status=`systemctl status nvram_log | grep -E "FAILURE|SUCCESS"`
    #   [ -n "$pxe_status" -a -n "$nvram_log_status" ] && break
    #   sleep 5
    #   let try_times=$try_times-1
    #done

    mount -o remount,rw /startup_disk/image

    #比较文件，判断是否需要替换
    if [[ ${FW_TYPE_CH} == "0" && ${FW_TYPE_CP} == "0" &&  ${FW_TYPE_CP_NSB} == "0" ]] ; then
        OS_echo "[1822_hot_patch]Firmware already replaced."
    else
        # 备份1822固件和版本配套表如果是第一次备份，则备份到origin，否则备份到pre目录
        #FW0 备份1822固件
        if [[ ${FW_TYPE_CH} == "1" ]] ; then
            if [ -f ${FW_1822_ORIGIN} ] ; then
                cp -af ${FW_1822} ${FW_1822_PRE_TEMP}
                L_CP_RES=$?
                if [[ ${L_CP_RES} != "0" ]] ; then
                    OS_echo "[1822_hot_patch]copy[1] high C backup firmware fail:$L_CP_RES"
                    exit 1
                fi
                OS_echo "[1822_hot_patch]backup $FW_1822_PRE"
            else
                cp -af ${FW_1822} ${FW_1822_ORIGIN_TEMP}
                L_CP_RES=$?
                if [[ ${L_CP_RES} != "0" ]] ; then
                    OS_echo "[1822_hot_patch]copy[2] high C backup firmware fail:$L_CP_RES"
                    exit 1
                fi
                OS_echo "[1822_hot_patch]backup $FW_1822_ORIGIN"
            fi
        fi
        #FW3 备份1822固件
        if [[ ${FW_TYPE_CP} == "1" ]] ; then
            if [ -f ${FW_1822_CP_ORIGIN} ] ; then
                cp -af ${FW_1822_CP} ${FW_1822_CP_PRE_TEMP}
                L_CP_RES=$?
                if [[ ${L_CP_RES} != "0" ]] ; then
                    OS_echo "[1822_hot_patch]copy[3] CP backup firmware fail:$L_CP_RES"
                    exit 1
                fi
                OS_echo "[1822_hot_patch]backup $FW_1822_CP_PRE"
            else
                cp -af ${FW_1822_CP} ${FW_1822_CP_ORIGIN_TEMP}
                L_CP_RES=$?
                if [[ ${L_CP_RES} != "0" ]] ; then
                    OS_echo "[1822_hot_patch]copy[4] CP backup firmware fail:$L_CP_RES"
                    exit 1
                fi
                OS_echo "[1822_hot_patch]backup $FW_1822_CP_ORIGIN"
            fi
        fi
        #FW1 备份1822固件
        if [[ ${FW_TYPE_CP_NSB} == "1" ]] ; then
            if [ -f ${FW_1822_CP_NSB_ORIGIN} ] ; then
                cp -af ${FW_1822_CP_NSB} ${FW_1822_CP_NSB_PRE_TEMP}
                L_CP_RES=$?
                if [[ ${L_CP_RES} != "0" ]] ; then
                    OS_echo "[1822_hot_patch]copy[5] CP_NSB backup firmware fail:$L_CP_RES"
                    exit 1
                fi
                OS_echo "[1822_hot_patch]backup $FW_1822_CP_NSB_PRE"
            else
                cp -af ${FW_1822_CP_NSB} ${FW_1822_CP_NSB_ORIGIN_TEMP}
                L_CP_RES=$?
                if [[ ${L_CP_RES} != "0" ]] ; then
                    OS_echo "[1822_hot_patch]copy[6] CP_NSB backup firmware fail:$L_CP_RES"
                    exit 1
                fi
                OS_echo "[1822_hot_patch]backup $FW_1822_CP_NSB_ORIGIN"
            fi
        fi
    fi
    
    #判断版本号是否需要修改
    if [ ${MODIFY_CONF} == "0" ] ; then
        OS_echo "[1822_hot_patch]backup conf."

        #备份版本配套表
        if [ -f ${VERSION_FILE_ORIGIN} ]; then
            cp -af ${VERSION_FILE} ${VERSION_FILE_PRE_TEMP}
            L_CP_RES=$?
            if [[ ${L_CP_RES} != "0" ]] ; then
                OS_echo "[1822_hot_patch]copy[7] backup version file fail:$L_CP_RES"
                exit 1
            fi
            OS_echo "[1822_hot_patch]backup $VERSION_FILE_PRE"
        else
            cp -af ${VERSION_FILE} ${VERSION_FILE_ORIGIN_TEMP}
            L_CP_RES=$?
            if [[ ${L_CP_RES} != "0" ]] ; then
                OS_echo "[1822_hot_patch]copy[8] backup version file fail:$L_CP_RES"
                exit 1
            fi
            OS_echo "[1822_hot_patch]backup $VERSION_FILE_ORIGIN"
        fi
    fi

    # 修改校验和文件，替换固件文件，修改版本配套表
    sed -i "/product_versions.${PANGEA_LINK}.conf/d" $CHECKSUM_SYNC

    
    MOVE_FILES
    #拷贝bin FW0,删除对应校验和
    if [[ ${FW_TYPE_CH} == "1" ]] ; then
        sed -i "/Hi1822_fw.bin/d" $CHECKSUM_SYNC
        mv -f /OSM/modules/1822temp/Hi1822_fw.bin ${FW_1822}
        L_CP_RES=$?
        if [[ ${L_CP_RES} != "0" ]] ; then
            OS_echo "[1822_hot_patch]copy[9] replace high C firmware fail:$L_CP_RES"
            exit 1
        fi
    fi


    #拷贝bin FW3
    if [[ ${FW_TYPE_CP} == "1" ]] ; then
        sed -i "/Hi1822_cp_fw.bin/d" $CHECKSUM_SYNC
        sed -i "/100g_eth_fe.bin/d" $CHECKSUM_SYNC
        sed -i "/100g_rc_be.bin/d" $CHECKSUM_SYNC
        sed -i "/100g_rc_so.bin/d" $CHECKSUM_SYNC
        sed -i "/ctl_eth.bin/d" $CHECKSUM_SYNC
        sed -i "/ge_eth.bin/d" $CHECKSUM_SYNC
        sed -i "/smt_eth_fe.bin/d" $CHECKSUM_SYNC
        sed -i "/smt_fc_fe.bin/d" $CHECKSUM_SYNC
        sed -i "/smt_rc_so.bin/d" $CHECKSUM_SYNC
        mv -f /OSM/modules/1822temp/Hi1822_cp_fw.bin ${FW_1822_CP}
        L_CP_RES=$?
        if [[ ${L_CP_RES} != "0" ]] ; then
            OS_echo "[1822_hot_patch]copy[10] replace CP firmware fail:$L_CP_RES"
            exit 1
        fi
    fi

    #拷贝bin FW1
    if [[ ${FW_TYPE_CP_NSB} == "1" ]] ; then
        sed -i "/Hi1822_cp_nsb_fw.bin/d" $CHECKSUM_SYNC
        sed -i "/100g_eth_fe_n.bin/d" $CHECKSUM_SYNC
        sed -i "/100g_rc_be_n.bin/d" $CHECKSUM_SYNC
        sed -i "/100g_rc_so_n.bin/d" $CHECKSUM_SYNC
        sed -i "/ge_eth_n.bin/d" $CHECKSUM_SYNC
        sed -i "/smt_eth_fe_n.bin/d" $CHECKSUM_SYNC
        sed -i "/smt_fc_fe_n.bin/d" $CHECKSUM_SYNC
        sed -i "/smt_rc_so_n.bin/d" $CHECKSUM_SYNC
        mv -f /OSM/modules/1822temp/Hi1822_cp_nsb_fw.bin ${FW_1822_CP_NSB}
        L_CP_RES=$?
        if [[ ${L_CP_RES} != "0" ]] ; then
            OS_echo "[1822_hot_patch]copy[11] replace CP_NSB firmware fail:$L_CP_RES"
            exit 1
        fi
    fi


    OS_echo "[1822_hot_patch]copy firmware complete"

    #修改版本号
    #FW0
    if [[ ${VERSION_TYPE_CH} == "1" ]] ; then
        #行号
        fwnum=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
        #fw版本
        fwver=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
        #替换版本号
        sed -i "$fwnum s/$fwver/$FW_1822_VER/" $VERSION_FILE

        OS_echo "[1822_hot_patch]modify 1822 C version to ${FW_1822_VER}."
    fi

    #FW3
    if [[ ${VERSION_TYPE_CP} == "1" ]] ; then
        #fw版本
        fwver=`grep -n -A 2 "HI1822_CP_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
        #行号
        for fwnum in `grep -n -A 2 "HI1822" "$VERSION_FILE" | grep "Version=2.1.3" | awk -F "-" '{print $1}'` ; do
            #替换版本号
            sed -i "$fwnum s/$fwver/$FW_1822_VER_CP/" $VERSION_FILE
        done
        OS_echo "[1822_hot_patch]modify 1822 CP version to ${FW_1822_VER_CP}."
    fi
    #FW1
    if [[ ${VERSION_TYPE_CP_NSB} == "1" ]] ; then
        #fw版本
        fwver=`grep -n -A 2 "HI1822_CP_NSB_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
        #行号
        for fwnum in `grep -n -A 2 "HI1822" "$VERSION_FILE" | grep "Version=2.1.2" | awk -F "-" '{print $1}'` ; do
            #替换版本号
            sed -i "$fwnum s/$fwver/$FW_1822_VER_CP_NSB/" $VERSION_FILE
        done
        OS_echo "[1822_hot_patch]modify 1822 CP_NSB version to ${FW_1822_VER_CP_NSB}."
    fi

    mount -o remount,ro /startup_disk/image
    rm -rf /OSM/modules/1822temp
    OS_echo "[1822_hot_patch]replace firmware and modify sys version file succ, start to update firmware."
}

REPLACE_FILE()
{
    if [ -d "/startup_disk/image/${PKG_VERSION}" ]; then
        INIT_FW_PATCH "${PKG_VERSION}"
        REPLACE_1822FW_FILE
    fi
}

PATCH_START()
{
    OS_echo "[1822_hot_patch]1822 patch start."
    #获取当前环境硬件形态对应类型
    GET_PLATFORM_TYPE
    #备份替换1822固件相关文件
    REPLACE_FILE

    if [[ $SPECIAL_UPGRADE == 1 ]];then
        OS_echo "[1822_hot_patch]special upgrade succ."
        return
    fi

    OS_echo "[1822_hot_patch]start to update firmware."

    #检查app_data,sys_app进程和sdk驱动状态用于判断节点上电状态
    app_data=`ps -ef|grep app_data|wc -l`
    sys_app=`ps -ef|grep sys_app|wc -l`
    hifc_sdk=`lsmod | awk '{print $1}' | grep hifc_sdk | wc -l`

    if [[ $app_data -lt 2 && $sys_app -lt 2 && $hifc_sdk -lt 1 ]]; then
        OS_echo "[1822_hot_patch]system is during power on, return."
        return
    fi

    INSTALL_HOTPATCH=1
    #执行1822固件升级命令
    PANGEA_FwUpgradeMmlCmd

    OS_echo "[1822_hot_patch]1822 patch end."

    return
}

PATCH_END()
{
    return
}


ROLLBACK_1822FW_FILE()
{
    OS_echo "[1822_hot_patch]rollback 1822FW file start"
    #回退版本配套表
    #修改版本号
    #行号
    
    if [ "$rollback_path" == "None" ]; then
        OS_echo "[1822_hot_patch]rollback action is delete, dir is $rollback_path."
        if [ -f ${VERSION_FILE_ORIGIN} ]; then
            #回退版本配套表版本号
            #C卡 fw版本
            fwnum=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            #fw版本
            fwver=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE_ORIGIN" | tail -1 | awk -F "=" '{print $2}'`

            sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE

            OS_echo "[1822_hot_patch]find origin file, modify high C version to $fwver_origin."
            
            #C+安全卡 fw版本
            fwver=`grep -n -A 2 "HI1822_CP_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_CP_FW" "$VERSION_FILE_ORIGIN" | tail -1 | awk -F "=" '{print $2}'`
            #替换版本号
            for fwnum in `grep -n -A 2 "HI1822" "$VERSION_FILE" | grep "Version=2.1.3" | awk -F "-" '{print $1}'` ; do
                sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE
            done
            OS_echo "[1822_hot_patch]find origin file, modify CP version to $fwver_origin."
            
            #C+非安全卡 fw版本
            fwver=`grep -n -A 2 "HI1822_CP_NSB_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_CP_NSB_FW" "$VERSION_FILE_ORIGIN" | tail -1 | awk -F "=" '{print $2}'`
            #替换版本号
            for fwnum in `grep -n -A 2 "HI1822" "$VERSION_FILE" | grep "Version=2.1.2" | awk -F "-" '{print $1}'` ; do
                sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE
            done
            OS_echo "[1822_hot_patch]find origin file, modify CP_NSB version to $fwver_origin."
        fi
        
        #回退固件
        if [ -f ${FW_1822_ORIGIN} ]; then
            mv $FW_1822_ORIGIN $FW_1822
            OS_echo "[1822_hot_patch]rollback high C FW $FW_1822_ORIGIN."
        fi
        if [ -f ${FW_1822_CP_ORIGIN} ]; then
            mv $FW_1822_CP_ORIGIN $FW_1822_CP
            OS_echo "[1822_hot_patch]rollback CP FW $FW_1822_CP_ORIGIN."
        fi
        if [ -f ${FW_1822_CP_NSB_ORIGIN} ]; then
            mv $FW_1822_CP_NSB_ORIGIN $FW_1822_CP_NSB
            OS_echo "[1822_hot_patch]rollback CP_NSB FW $FW_1822_CP_NSB_ORIGIN."
        fi
        
        #删除多余备份文件
        rm -rf $FW_1822_PRE
        rm -rf $FW_1822_CP_PRE
        rm -rf $FW_1822_CP_NSB_PRE
        rm -rf $VERSION_FILE_PRE
        rm -rf $VERSION_FILE_ORIGIN
        
    else
        OS_echo "[1822_hot_patch]rollback action is rollback, dir is $rollback_path."
        #FW0高端
        if [ -f ${FW_1822_PRE} ]; then
            fwnum=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            #fw版本
            fwver=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE_PRE" | tail -1 | awk -F "=" '{print $2}'`

            #替换版本号
            sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE

            REMOVE_PRE=1
            OS_echo "[1822_hot_patch]find pre file, modify high C version to $fwver_origin."
            #回退固件
            mv $FW_1822_PRE $FW_1822
            OS_echo "[1822_hot_patch]rollback high C FW $FW_1822_PRE."
        elif [ -f ${FW_1822_ORIGIN} ]; then
            fwnum=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "-" '{print $1}'`
            #fw版本
            fwver=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_FW" "$VERSION_FILE_ORIGIN" | tail -1 | awk -F "=" '{print $2}'`

            sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE

            REMOVE_ORIGIN=1
            OS_echo "[1822_hot_patch]find origin file, modify high C version to $fwver_origin."

            mv $FW_1822_ORIGIN $FW_1822
            OS_echo "[1822_hot_patch]rollback high C FW $FW_1822_ORIGIN."
        else
            OS_echo "[1822_hot_patch]Not find high C backup file."
        fi

        #FW3
        if [ -f ${FW_1822_CP_PRE} ]; then
            #fw版本
            fwver=`grep -n -A 2 "HI1822_CP_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_CP_FW" "$VERSION_FILE_PRE" | tail -1 | awk -F "=" '{print $2}'`

            #替换版本号
            for fwnum in `grep -n -A 2 "HI1822" "$VERSION_FILE" | grep "Version=2.1.3" | awk -F "-" '{print $1}'` ; do
                sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE
            done
            REMOVE_PRE=1
            OS_echo "[1822_hot_patch]find pre file, modify CP version to $fwver_origin."
            #回退固件
            mv $FW_1822_CP_PRE $FW_1822_CP
            OS_echo "[1822_hot_patch]rollback CP FW $FW_1822_CP_PRE."
        elif [ -f ${FW_1822_CP_ORIGIN} ]; then
            #fw版本
            fwver=`grep -n -A 2 "HI1822_CP_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_CP_FW" "$VERSION_FILE_ORIGIN" | tail -1 | awk -F "=" '{print $2}'`
            #替换版本号
            for fwnum in `grep -n -A 2 "HI1822" "$VERSION_FILE" | grep "Version=2.1.3" | awk -F "-" '{print $1}'` ; do
                sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE
            done
            REMOVE_ORIGIN=1
            OS_echo "[1822_hot_patch]find origin file, modify CP version to $fwver_origin."

            mv $FW_1822_CP_ORIGIN $FW_1822_CP
            OS_echo "[1822_hot_patch]rollback CP FW $FW_1822_CP_ORIGIN."
        else
            OS_echo "[1822_hot_patch]Not find CP backup file."
        fi
        #FW1
        if [ -f ${FW_1822_CP_NSB_PRE} ]; then
            #fw版本
            fwver=`grep -n -A 2 "HI1822_CP_NSB_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_CP_NSB_FW" "$VERSION_FILE_PRE" | tail -1 | awk -F "=" '{print $2}'`

            #替换版本号
            for fwnum in `grep -n -A 2 "HI1822" "$VERSION_FILE" | grep "Version=2.1.2" | awk -F "-" '{print $1}'` ; do
                sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE
            done
            REMOVE_PRE=1
            OS_echo "[1822_hot_patch]find pre file, modify CP_NSB version to $fwver_origin."
            #回退固件
            mv $FW_1822_CP_NSB_PRE $FW_1822_CP_NSB
            OS_echo "[1822_hot_patch]rollback CP_NSB FW $FW_1822_CP_NSB_PRE."
        elif [ -f ${FW_1822_CP_NSB_ORIGIN} ]; then
            #fw版本
            fwver=`grep -n -A 2 "HI1822_CP_NSB_FW" "$VERSION_FILE" | tail -1 | awk -F "=" '{print $2}'`
            fwver_origin=`grep -n -A 2 "HI1822_CP_NSB_FW" "$VERSION_FILE_ORIGIN" | tail -1 | awk -F "=" '{print $2}'`
            #替换版本号
            for fwnum in `grep -n -A 2 "HI1822" "$VERSION_FILE" | grep "Version=2.1.2" | awk -F "-" '{print $1}'` ; do
                sed -i "$fwnum s/$fwver/$fwver_origin/" $VERSION_FILE
            done
            REMOVE_ORIGIN=1
            OS_echo "[1822_hot_patch]find origin file, modify CP_NSB version to $fwver_origin."

            mv $FW_1822_CP_NSB_ORIGIN $FW_1822_CP_NSB
            OS_echo "[1822_hot_patch]rollback CP_NSB FW $FW_1822_CP_NSB_ORIGIN."
        else
            OS_echo "[1822_hot_patch]Not find CP_NSB backup file."
        fi
        if [[ $REMOVE_ORIGIN == 1 ]] ; then
            rm -f $VERSION_FILE_ORIGIN
        fi
        if [[ $REMOVE_PRE == 1 ]] ; then
            rm -f $VERSION_FILE_PRE
        fi
    fi

}

ROLLBACK_FILE()
{
    if [ -d "/startup_disk/image/${PKG_VERSION}" ]; then
        INIT_FW_PATCH "${PKG_VERSION}"
        ROLLBACK_1822FW_FILE
    fi
}

PATCH_ROLLBACK()
{
    OS_echo "[1822_hot_patch]rollback begin"
    #获取当前环境硬件形态对应类型
    GET_PLATFORM_TYPE

    rm -rf /OSM/modules/1822temp
    mkdir -p /OSM/modules/1822temp

    mount -o remount,rw /startup_disk/image
    
    #回退1822固件相关文件
    ROLLBACK_FILE

    mount -o remount,ro /startup_disk/image
    rm -rf /OSM/modules/1822temp

    if [[ $SPECIAL_UPGRADE == 1 ]];then
        OS_echo "[1822_hot_patch]special resume succ."
        return
    fi

    OS_echo "[1822_hot_patch]begin rollback fw"

    #检查app_data,sys_app进程和sdk驱动状态用于判断节点上电状态
    app_data=`ps -ef|grep app_data|wc -l`
    sys_app=`ps -ef|grep sys_app|wc -l`
    hifc_sdk=`lsmod | awk '{print $1}' | grep hifc_sdk | wc -l`

    if [[ $app_data -lt 2 && $sys_app -lt 2 && $hifc_sdk -lt 1 ]]; then
        OS_echo "[1822_hot_patch]system is during power on, return."
        return
    fi

    INSTALL_HOTPATCH=0
    #执行1822固件升级命令
    PANGEA_FwUpgradeMmlCmd

    OS_echo "[1822_hot_patch]rollback end"
    return
}

PATCH_ROLLEND()
{
    return
}

L_CMD=$1
case $L_CMD in
    "start")
        PATCH_START
        ;;
    "end")
        PATCH_END
        ;;
    "rollstart")
        PATCH_ROLLBACK
        CHECK_PATCH_CONDITION
        ;;
    "rollend")
        PATCH_ROLLEND
        ;;
    "special_upgrade")
        SPECIAL_UPGRADE=1
        PATCH_START
        ;;
    "special_resume")
        SPECIAL_UPGRADE=1
        PATCH_ROLLBACK
        ;;
    *)
        OS_echo "[1822_hot_patch]unsupported option."
esac

exit 0
