#!/bin/bash

source /OSM/script/os_load_base.sh

#DTS2021083021265 纽扣电池问题，替换SES固件和配套表 buglist：BUGLIST202109150007
function replace_ses_firmware()
{
# 增加一个KO判定
local insmoded=$(lsmod | grep "pciecore")
if [ "$insmoded" != "" ]; then
	OS_echo "[sesfw] after third script, do nothing"
	return 0
fi

#查版本类型，查启动路劲
    #get board type
    BOARD_TYPE="`grep "^Product Name is" /proc/osp/bsp | awk '{print $4}'`"                    
    #get startup_disk dir
    BOOT_LINK=`ls -l /startup_disk/image | grep -a "boot ->" | awk '{print $11}'`
    OS_echo "[sesfw]------Boot dir is $BOOT_LINK."

    #PANGEA_LINK=`ls -l /startup_disk/image/$BOOT_LINK/update_pkg | grep -i "PANGEA" |awk '{print $9}'`
    #OS_echo "Boot link is $PANGEA_LINK."

    #老硬件Dorado环境下,/startup_disk/image/boot_osp/update_pkg/下回存在多个PANGEA版本,导致后边用PANGEA_LINK是无法匹配,此处修改为直接用BOARD_TYPE
    PANGEA_LINK=$BOARD_TYPE
    OS_echo "[sesfw]------PANGEA link is $PANGEA_LINK."

    #判断当前的硬件形态 PANGEA_V2R1C00 IVB控制板  PANGEA_V2R2C10 ARM1610控制板，如果不是这两种 就跳出SES替换函数，执行下一个替换函数
    #current fw patch version
    ses_new_ver1=20.01.74T96
    ses_new_ver2=20.74T96
    if [[ "$BOARD_TYPE" != PANGEA_V2R1C00  && "$BOARD_TYPE" != PANGEA_V2R2C10 ]]; then
        OS_echo "[failed] ------Board type ($BOARD_TYPE) mismatch."
        return 1
    fi
    if [ -f /startup_disk/conf/conf_local/UpdatesesFwFlagSPH030Flag ]; then
        if [ -f /startup_disk/image/$BOOT_LINK/update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses_bak${ses_new_ver1}.bin ]; then
            cp /startup_disk/image/$BOOT_LINK/update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin /OSM/modules/manage_chip/
            cp /startup_disk/image/$BOOT_LINK/versions.conf /OSM/conf/
            OS_echo "[sesfw] ------The firmware is already update."
        else
            fwpackage="ses_firmware"
            if [ -f /OSM/coffer_data/${fwpackage}.tar.gz ]; then
                cd /OSM/coffer_data/
                tar zxvf ${fwpackage}.tar.gz >/dev/null
                #change the 12g_sas_ses version in /OSM/conf/versions.conf
                cd /OSM/conf/
                    oldsesfwver_c1=`cat ./versions.conf | grep "12g_sas_ses.bin" | tail -1 | awk -F "," '{print $2}'`        #替换SES版本1
                    oldsesfwver_c2=`cat ./versions.conf | grep "12g_sas_ses.bin" | head -n 2| tail -n +2 | awk -F "," '{print $2}'`   #替换ses版本2
                    if [ $oldsesfwver_c1 != $ses_new_ver1 ]; then
                          OS_echo "[sesfw] ------the original fwver in conf is $oldsesfwver_c1 when upgrade."
                          #修改原versions.conf文件中12g_sas_ses版本号，取代原来从tar.gz中拷贝覆盖的方式
                          fwnum1=`grep -n -A 1 "\[SES1\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          sed -i "/12g_sas_ses/ s/$oldsesfwver_c1/$ses_new_ver1/" versions.conf
                          sed -i "/12g_sas_ses/ s/$oldsesfwver_c2/$ses_new_ver2/" versions.conf
                          sed -i "$fwnum1 s/$oldsesfwver_c2/$ses_new_ver2/" versions.conf
                    fi
                    
                cp /OSM/coffer_data/${fwpackage}/12g_sas_ses.bin /OSM/modules/manage_chip/ 
                sync

                #change disk to R/M   
                OS_SetStartUpDiskProperty /startup_disk/image/ rw

                cd /startup_disk/image/$BOOT_LINK/

                #backup disk file 备份当前大包内的配置表+固件，固件路径修改为产品下SES固件的路径
                sync
                
                #改文件名要用CP ./格式
                cp ./versions.conf ./versions_bak${ses_new_ver1}.conf
                cp ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses_bak${ses_new_ver1}.bin                              
                #copy file to disk
                oldsesfwver_c1=`cat ./versions.conf | grep "12g_sas_ses.bin" | tail -1 | awk -F "," '{print $2}'`        #替换SES版本1
                oldsesfwver_c2=`cat ./versions.conf | grep "12g_sas_ses.bin" | head -n 2| tail -n +2 | awk -F "," '{print $2}'`   #替换ses版本2
                if [ $oldsesfwver_c1 != $ses_new_ver1 ]; then
                          OS_echo "[sesfw] ------the original fwver in conf is $oldsesfwver_c1 when upgrade."
                          #修改原versions.conf文件中12g_sas_ses版本号，取代原来从tar.gz中拷贝覆盖的方式
                          fwnum1=`grep -n -A 1 "\[SES1\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          sed -i "/12g_sas_ses/ s/$oldsesfwver_c1/$ses_new_ver1/" versions.conf
                          sed -i "/12g_sas_ses/ s/$oldsesfwver_c2/$ses_new_ver2/" versions.conf
                          sed -i "$fwnum1 s/$oldsesfwver_c2/$ses_new_ver2/" versions.conf
                fi
                cp /OSM/coffer_data/${fwpackage}/12g_sas_ses.bin /startup_disk/image/$BOOT_LINK/update_pkg/$BOARD_TYPE/manage_chip/
                sync

                #reload checksum
                local_sum=`sha256sum ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin`
                sed -i "/12g_sas_ses.bin/c$local_sum" ./sha256sum_sync

                local_sum=`md5sum ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin`
                sed -i "/12g_sas_ses.bin/c$local_sum" ./md5sum_sync

                local_sum=`sha256sum ./md5sum_sync`
                sed -i "/md5sum_sync/c$local_sum" ./sha256sum_sync
                sync

                OS_echo "[sesfw] ------replace firmware complete."              
            else
                OS_echo "[sesfw] ------Can not find ${fwpackage}.tar.gz file."
            fi
        fi
    elif [ -f /startup_disk/conf/conf_local/ResumesesFwFlagSPH030Flag ]; then
        if [ -f /startup_disk/image/$BOOT_LINK/update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses_bak${ses_new_ver1}.bin ]; then
            #change disk to Readwrite
            cd /OSM/conf/
            OS_SetStartUpDiskProperty /startup_disk/image/ rw

            cd /startup_disk/image/$BOOT_LINK/
            #resume backup file to disk 修改cd /startup_disk/image/$BOOT_LINK/路径下的配置文件 #把版本配套表刷回老版本的版本信息
            oldfwver_d1=`cat ./versions_bak${ses_new_ver1}.conf | grep "12g_sas_ses.bin" | tail -1 | awk -F "," '{print $2}'`
            oldfwver_d2=`cat ./versions_bak${ses_new_ver1}.conf | grep "12g_sas_ses.bin" | head -n 2| tail -n +2 | awk -F "," '{print $2}'`
            OS_echo "[sesfw] ------the original fwver in boot_osp is $oldfwver_d1 when rollback."                                                     
            fwnum1=`grep -n -A 1 "\[SES1\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            sed -i "/12g_sas_ses/ s/$ses_new_ver1/$oldfwver_d1/" versions.conf
            sed -i "/12g_sas_ses/ s/$ses_new_ver2/$oldfwver_d2/" versions.conf
            sed -i "$fwnum1 s/$ses_new_ver2/$oldfwver_d2/" versions.conf
            
            #固件替换为老固件 
            mv ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses_bak${ses_new_ver1}.bin ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin                               
            sync

            #reload checksum
            local_sum=`sha256sum ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin`
            sed -i "/12g_sas_ses.bin/c$local_sum" ./sha256sum_sync

            local_sum=`md5sum ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin`
            sed -i "/12g_sas_ses.bin/c$local_sum" ./md5sum_sync

            local_sum=`sha256sum ./md5sum_sync`
            sed -i "/md5sum_sync/c$local_sum" ./sha256sum_sync
            sync

            fwpackage="ses_firmware"
            
            cd /OSM/conf/
            #修改/OSM/conf/路径下的配置文件versions.conf文件中12g_sas_ses版本号，
            fwnum1=`grep -n -A 1 "\[SES1\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            sed -i "/12g_sas_ses/ s/$ses_new_ver1/$oldfwver_d1/" versions.conf
            sed -i "/12g_sas_ses/ s/$ses_new_ver2/$oldfwver_d2/" versions.conf
            sed -i "$fwnum1 s/$ses_new_ver2/$oldfwver_d2/" versions.conf

            #remove tmp file 删除所有临时文件
            if [ -d /OSM/coffer_data/${fwpackage} ]; then
                rm -rf /OSM/coffer_data/${fwpackage}
            fi

            if [ -f /startup_disk/conf/conf_local/ResumesesFwFlagSPH030Flag ]; then 
                rm -f /startup_disk/conf/conf_local/ResumesesFwFlagSPH030Flag
            fi

            
        #删除如果做备件时，同步的固件
            if [ -f /startup_disk/image/$BOOT_LINK/12g_sas_ses_bak${ses_new_ver1}.bin ]; then 
                rm -f /startup_disk/image/$BOOT_LINK/12g_sas_ses_bak${ses_new_ver1}.bin
            fi
            
            if [ -f /startup_disk/image/$BOOT_LINK/versions_bak${ses_new_ver1}.conf ]; then 
                rm -f /startup_disk/image/$BOOT_LINK/versions_bak${ses_new_ver1}.conf
            fi
 
            sync

            OS_echo "[sesfw] ------Resume firmware complete."
        else
            OS_echo "[sesfw] ------The firmware no need to resume."
        fi
    else
        # 1.B控无标记文件（当A控已打补丁，执行了enablesesfw命令并上下电，B控更换未打补丁的控制器备件，新B控不会同步标志文件，同步的固件是补丁固件）
        # 2.B控无标记文件（当A控已打补丁，执行了enablesesfw命令并上下电，再执行disablesesfw命令并不上下电，B控插入未打补丁的控制器备件，新B控不会同步标志文件，同步的固件是补丁固件）
        if [ -f /startup_disk/image/boot_osp/update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses_bak${ses_new_ver1}.bin -o -f /startup_disk/image/boot_osp_bak/update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses_bak${ses_new_ver1}.bin ] ; then
            BOOT_OSP_VERSION=`cat /startup_disk/image/boot_osp/versions.conf | grep -m 1 Version |awk -F "=" '{print $2}'`
            BOOT_OSP_BAK_VERSION=`cat /startup_disk/image/$BOOT_LINK/versions.conf | grep -m 1 Version |awk -F "=" '{print $2}'`
        # 检测到硬件版本不一致
            if [ $BOOT_OSP_VERSION -eq $BOOT_OSP_BAK_VERSION ]; then
                cp /startup_disk/image/$BOOT_LINK/update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin /OSM/modules/
                cp /startup_disk/image/$BOOT_LINK/versions.conf /OSM/conf/

                if [ "$BOOT_LINK" == "boot_osp_bak" ]; then
                    #change disk to read-write
                    OS_SetStartUpDiskProperty /startup_disk/image/ rw

                    #change working directory
                    cd /startup_disk/image/$BOOT_LINK

                    #copy file to disk
                    OS_echo "[sesfw] ------Copy 12g_sas_ses.bin and versions.conf file from boot_osp_bak to boot_osp."
                    cp ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin /startup_disk/image/boot_osp/update_pkg/$BOARD_TYPE/manage_chip/
                    sync

                    #change working directory ，替换startup_disk/image/boot_osp/路径下的配置文件内容
                    cd /startup_disk/image/boot_osp/
                    oldsesfwver_c1=`cat ./versions.conf | grep "12g_sas_ses.bin" | tail -1 | awk -F "," '{print $2}'`        #替换SES版本1
                    oldsesfwver_c2=`cat ./versions.conf | grep "12g_sas_ses.bin" | head -n 2| tail -n +2 | awk -F "," '{print $2}'`   #替换ses版本2
                    OS_echo "[sesfw] ------kuorong tongbu boot_osp, the ses ver is $oldsesfwver_c1."
                    fwnum1=`grep -n -A 1 "\[SES1\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    sed -i "/12g_sas_ses/ s/$oldsesfwver_c1/$ses_new_ver1/" versions.conf
                    sed -i "/12g_sas_ses/ s/$oldsesfwver_c2/$ses_new_ver2/" versions.conf
                    sed -i "$fwnum1 s/$oldsesfwver_c2/$ses_new_ver2/" versions.conf

                    #reload checksum
                    local_sum=`sha256sum ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin`
                    sed -i "/12g_sas_ses.bin/c$local_sum" ./sha256sum_sync                   

                    local_sum=`md5sum ./update_pkg/$BOARD_TYPE/manage_chip/12g_sas_ses.bin`
                    sed -i "/12g_sas_ses.bin/c$local_sum" ./md5sum_sync

                    local_sum=`sha256sum ./md5sum_sync`
                    sed -i "/md5sum_sync/c$local_sum" ./sha256sum_sync
                    sync

                    #Don't change disk to read-only, because the upgrade module will change it to read-only eventually(Suggested by zouchangwen 90006367)
                    #OS_SetStartUpDiskProperty /startup_disk/image/ ro
                fi
            fi
            touch -f /startup_disk/conf/conf_local/UpdatesesFwFlagSPH030Flag
            sync

            OS_echo "[sesfw] ------patch ses firmware."
        else
        # 1.A控无标记文件（当A控已打补丁，未执行enablesesfw命令）
        # 2.B控无标记文件（当A控已打补丁，执行了enablesesfw命令，A控未上下电，B控更换未打补丁的控制器备件，新B控不会同步标志文件，同步的固件不是补丁固件）
        OS_echo "[sesfw] ------origin ses firmware."
        fi
    fi

    return 0
}


function PATCH_START()
{
replace_ses_firmware
    return 
}
function PATCH_END()
{
    return
}
function PATCH_ROLLSTART()
{
    return
}
function PATCH_ROLLEND()
{
    return
}
#start：打补丁前需要执行的操作，脚本的执行时间点在替换文件前
#end：打补丁后需要执行的操作，脚本的执行时间点在替换文件后
#rollstart：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之前。
#rollend：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之后。

L_CMD=$1
OS_echo "command is $L_CMD"
case $L_CMD in
        "start") 
                PATCH_START
                ;;
        "end") 
                PATCH_END
                ;;
        "rollstart") 
                PATCH_ROLLSTART
                ;;
        "rollend") 
                PATCH_ROLLEND
                ;;
        *)
                OS_echo  "unsupported option."
esac
exit 0
