#!/bin/bash

#仅针对业务进程生效,V5产品（x86）软件包，仅针对业务进程生效（第2、第3 加载点设置MALLOC_ARANA_MAX为1
export MALLOC_ARENA_MAX=1

#---------tcp_mem------------#
#2800 V3@48G
#5300 V3@16G
#5500 V3@24G
#5500 V3@48G
#5600 V3@32G
#5600 V3@64G
#5800 V3@64G
#5800 V3@128G
#6800 V3@512G
#6900 V3@256G
#18500 V3@128G
#18800 V3@256G
                
#硬件类型 内存大小（BYTE） TCP MEMORY的三个值
G_TCP_MEM_SIZE=("2600 17179869184#28800;38400;57600"
                "2600 34359738368#28800;38400;57600"
				"2800 51539607552#235560;314080;471120"
                "5300 17179869184#27480;36640;54960"
                "5500 25769803776#30600;40800;61200"
                "5500 51539607552#40560;54080;81120"
                "5600 34359738368#19800;26400;39600"
                "5600 68719476736#41880;55840;83760"
                "5800 68719476736#36120;48160;72240"
                "5800 137438953472#71820;95760;143640"
                "6800 549755813888#306360;408480;612720"
                "6900 274877906944#135600;180800;271200"
                "18500 137438953472#68700;91600;137400"
                "18800 274877906944#145560;194080;291120")

source /OSM/script/os_load_base.sh

function replace_bios_firmware()
{
#查看内存是否初始化，生成标记位。
insmod /OSM/modules/bsp_is_meminit_patch.ko
rmmod /OSM/modules/bsp_is_meminit_patch.ko
#从内存中获取板类型
BOARD_TYPE="`grep "^Product Name is" /proc/osp/bsp | awk '{print $4}'`"

BOOT_LINK=`ls -l /startup_disk/image | grep -a "boot ->" |awk '{print $11}'` #启动目录
OS_echo "[bios] boot dir is $BOOT_LINK"

PANGEA_LINK=`ls -l /startup_disk/image/$BOOT_LINK/update_pkg | grep -i "PANGEA" |awk '{print $9}'`
OS_echo "[bios] pangea link dir is $PANGEA_LINK"

if [[ "$BOARD_TYPE" == PANGEA_V2R5C00 || "$BOARD_TYPE" == PANGEA_V2R5C00_2U ]] ; then        
    if [ -f /startup_disk/conf/conf_local/V500R007C61SPH018ResumeBiosFlag ] ; then
	    #默认未生效或者回退场景
        if [ -f /startup_disk/image/$BOOT_LINK/update_pkg/$PANGEA_LINK/bios/bios_bak.ROM ] ; then 
            OS_SetStartUpDiskProperty /startup_disk/image/ rw  
            cd /startup_disk/image/$BOOT_LINK/
            #回退固件
            mv ./update_pkg/$PANGEA_LINK/bios/bios_bak.ROM ./update_pkg/$PANGEA_LINK/bios/bios.ROM
            sync
            cp ./update_pkg/$PANGEA_LINK/bios/bios.ROM /OSM/modules/
            sync

            #都要计算 bios的md5和sha256
            local_sum=`md5sum ./update_pkg/$PANGEA_LINK/bios/bios.ROM`
            OS_echo "$local_sum"
            sed -i "/bios.ROM/c$local_sum" ./md5sum_sync
            
            local_sum=`sha256sum ./update_pkg/$PANGEA_LINK/bios/bios.ROM`
            OS_echo "$local_sum"
            sed -i "/bios.ROM/c$local_sum" ./sha256sum_sync
            
            local_sum=`sha256sum ./md5sum_sync`
            OS_echo "$local_sum"
            sed -i "/md5sum_sync/c$local_sum" ./sha256sum_sync
            sync
            
            OS_SetStartUpDiskProperty /startup_disk/image/ ro
	    
            if [ -f /startup_disk/conf/conf_local/V500R007C61SPH018BiosFlag ] ; then 
                rm -f /startup_disk/conf/conf_local/V500R007C61SPH018BiosFlag
                OS_echo "[bios] delete V500R007C61SPH018BiosFlag"
            fi
			
            if [ -f /startup_disk/conf/conf_local/V500R007C61SPH018ResumeBiosFlag ] ; then 
                rm -f /startup_disk/conf/conf_local/V500R007C61SPH018ResumeBiosFlag
                OS_echo "[bios] delete V500R007C61SPH018ResumeBiosFlag"
            fi
            sync
        else
            OS_echo "[bios] ------origin bios ------"
            sync
        fi
    elif [ -f /startup_disk/conf/conf_local/V500R007C61SPH018BiosFlag ] ; then 
        if [ -f /OSM/coffer_data/bios_firmware.tar.gz ] ; then 
            OS_echo "[bios] ------patch replace_bios_firmware begain .------"
            cd /OSM/coffer_data/
            tar zxvf bios_firmware.tar.gz
            #如果已经替换固件，则需要替换内存BIOS文件
            if [ -f /startup_disk/image/$BOOT_LINK/update_pkg/$PANGEA_LINK/bios/bios_bak.ROM ] ; then 
                cp /OSM/coffer_data/bios_firmware/$PANGEA_LINK/bios.ROM  /OSM/modules/
                OS_echo "[bios] ------bios.ROM is exit in $BOOT_LINK.------"
             else
                 OS_SetStartUpDiskProperty /startup_disk/image/ rw        
                 OS_echo "[bios] ------cp bios.ROM to $PANGEA_LINK .------"
                 cd /startup_disk/image/$BOOT_LINK/
                 #拷贝补丁文件到内存目录 版本配套表和bios固件 版本配套表不需要替换
                 	
                 #备份bios文件
                 cp ./update_pkg/$PANGEA_LINK/bios/bios.ROM ./update_pkg/$PANGEA_LINK/bios/bios_bak.ROM
                 sync
                 	
                 #替换BIOS文件
                 cp /OSM/coffer_data/bios_firmware/$PANGEA_LINK/bios.ROM  ./update_pkg/$PANGEA_LINK/bios/
                 cp /OSM/coffer_data/bios_firmware/$PANGEA_LINK/bios.ROM  /OSM/modules/
                 sync
                 
                 #都要计算 bios的md5和sha256
                 local_sum=`md5sum ./update_pkg/$PANGEA_LINK/bios/bios.ROM`
                 OS_echo "$local_sum"
                 sed -i "/bios.ROM/c$local_sum" ./md5sum_sync
                 		
                 local_sum=`sha256sum ./update_pkg/$PANGEA_LINK/bios/bios.ROM`
                 OS_echo "$local_sum"
                 sed -i "/bios.ROM/c$local_sum" ./sha256sum_sync
				
                 local_sum=`sha256sum ./md5sum_sync`
                 OS_echo "$local_sum"
                 sed -i "/md5sum_sync/c$local_sum" ./sha256sum_sync
                 sync
                 OS_SetStartUpDiskProperty /startup_disk/image/ ro
             fi

            rm -f /OSM/coffer_data/bios_firmware.tar.gz
            rm -f /OSM/coffer_data/bios_firmware/ -r
            sync
        fi
    else
        if [ -f /startup_disk/image/$BOOT_LINK/update_pkg/$PANGEA_LINK/bios/bios_bak.ROM ] ; then 
            cp /startup_disk/image/$BOOT_LINK/update_pkg/$PANGEA_LINK/bios/bios.ROM  /OSM/modules/
            touch -f /startup_disk/conf/conf_local/V500R007C61SPH018BiosFlag
            sync
        fi
	    OS_echo "[bios] ------origin bios ------"
    fi
fi
return 0
}
function replace_hi1821_firmware()
{
    #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 "[hi1821fw]------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 "[hi1821fw]------PANGEA link is $PANGEA_LINK."

    #current fw patch version
    ioc_new_ver=1.20T18
	ioc_old_ver_120T13=1.20T13

    if [ -f /startup_disk/conf/conf_local/UpdateiocFwFlagSPH050Flag ]; then
        if [ -f /startup_disk/image/$BOOT_LINK/update_pkg/port/ioc_fw_bak${ioc_new_ver}.bin ]; then
            cp /startup_disk/image/$BOOT_LINK/update_pkg/port/ioc_fw.bin /OSM/modules/
            cp /startup_disk/image/$BOOT_LINK/versions.conf /OSM/conf/
            OS_echo "[hi1821fw] ------The firmware is already update."
        else
            fwpackage="hi1821_firmware_SPH050_DTS2022081400679"
            if [ -f /OSM/coffer_data/${fwpackage}.tar.gz ]; then
                cd /OSM/coffer_data/
                tar zxvf ${fwpackage}.tar.gz >/dev/null
                #change the ioc_fw version in /OSM/conf/versions.conf
                cd /OSM/conf/
                    orifwver_c=`cat ./versions.conf | grep "ioc_fw.bin" | tail -1 | awk -F "," '{print $2}'`
                    if [ $orifwver_c != $ioc_new_ver ]; then
                          OS_echo "[hi1821fw] ------the original fwver in conf is $orifwver_c when upgrade."
                          #修改原versions.conf文件中ioc_fw版本号，取代原来从tar.gz中拷贝覆盖的方式
                          fwnum1=`grep -n -A 1 "\[SmartIO\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          fwnum2=`grep -n -A 1 "\[IOC_HD\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          fwnum3=`grep -n -A 1 "\[IOC_HD_16\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          fwnum4=`grep -n -A 1 "\[IOC_10GE\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          fwnum5=`grep -n -A 1 "\[IOC_8G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          fwnum6=`grep -n -A 1 "\[IOC_16G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                          sed -i "/ioc_fw/ s/$orifwver_c/$ioc_new_ver/" versions.conf
                          sed -i "$fwnum1 s/$orifwver_c/$ioc_new_ver/" versions.conf
                          sed -i "$fwnum2 s/$orifwver_c/$ioc_new_ver/" versions.conf
                          sed -i "$fwnum3 s/$orifwver_c/$ioc_new_ver/" versions.conf
                          sed -i "$fwnum4 s/$orifwver_c/$ioc_new_ver/" versions.conf
                          sed -i "$fwnum5 s/$orifwver_c/$ioc_new_ver/" versions.conf
                          sed -i "$fwnum6 s/$orifwver_c/$ioc_new_ver/" versions.conf
                    fi
                    
                cp /OSM/coffer_data/${fwpackage}/ioc_fw.bin /OSM/modules/
                sync

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

                cd /startup_disk/image/$BOOT_LINK/

                #backup disk file
                cp ./versions.conf ./versions_bak${ioc_new_ver}.conf
                #cp ./product_versions.${PANGEA_LINK}.conf ./product_versions.${PANGEA_LINK}_bak${ioc_new_ver}.conf
                cp ./update_pkg/port/ioc_fw.bin ./update_pkg/port/ioc_fw_bak${ioc_new_ver}.bin
                sync

                #copy file to disk
                #cp /OSM/conf/versions.conf ./
                orifwver_d=`cat ./versions.conf | grep "ioc_fw.bin" | tail -1 | awk -F "," '{print $2}'`
                if [ $orifwver_d != $ioc_new_ver ]; then
                    OS_echo "[hi1821fw] ------the original fwver in boot_osp is $orifwver_d when upgrade."
                    #修改原versions.conf文件中ioc_fw版本号，取代原来从tar.gz中拷贝覆盖的方式
                    fwnum1=`grep -n -A 1 "\[SmartIO\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum2=`grep -n -A 1 "\[IOC_HD\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum3=`grep -n -A 1 "\[IOC_HD_16\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum4=`grep -n -A 1 "\[IOC_10GE\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum5=`grep -n -A 1 "\[IOC_8G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum6=`grep -n -A 1 "\[IOC_16G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    sed -i "/ioc_fw/ s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum1 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum2 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum3 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum4 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum5 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum6 s/$orifwver_d/$ioc_new_ver/" versions.conf
                fi
                cp /OSM/coffer_data/${fwpackage}/ioc_fw.bin ./update_pkg/port/
                sync

                #reload checksum
                #local_sum=`sha256sum ./product_versions.${PANGEA_LINK}.conf`
                #sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./sha256sum_sync

                #local_sum=`md5sum ./product_versions.${PANGEA_LINK}.conf`
                #sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./md5sum_sync

                local_sum=`sha256sum ./update_pkg/port/ioc_fw.bin`
                sed -i "/ioc_fw.bin/c$local_sum" ./sha256sum_sync

                local_sum=`md5sum ./update_pkg/port/ioc_fw.bin`
                sed -i "/ioc_fw.bin/c$local_sum" ./md5sum_sync

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

                #change disk to Readonly
                #OS_SetStartUpDiskProperty /startup_disk/image/ ro

                OS_echo "[hi1821fw] ------replace firmware complete."              
            else
                OS_echo "[hi1821fw] ------Can not find ${fwpackage}.tar.gz file."
            fi
        fi
    elif [ -f /startup_disk/conf/conf_local/ResumeiocFwFlagSPH050Flag ]; then
        if [ -f /startup_disk/image/$BOOT_LINK/update_pkg/port/ioc_fw_bak${ioc_new_ver}.bin ]; then
            #change disk to Readwrite
            OS_SetStartUpDiskProperty /startup_disk/image/ rw

            cd /startup_disk/image/$BOOT_LINK/

            #resume backup file to disk
            #mv ./versions_bak${ioc_new_ver}.conf ./versions.conf
            #mv ./product_versions.${PANGEA_LINK}_bak${ioc_new_ver}.conf ./product_versions.${PANGEA_LINK}.conf
            oldfwver_d=`cat ./versions_bak${ioc_new_ver}.conf | grep "ioc_fw.bin" | tail -1 | awk -F "," '{print $2}'`
            OS_echo "[hi1821fw] ------the original fwver in boot_osp is $oldfwver_d when rollback."
            fwnum1=`grep -n -A 1 "\[SmartIO\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum2=`grep -n -A 1 "\[IOC_HD\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum3=`grep -n -A 1 "\[IOC_HD_16\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum4=`grep -n -A 1 "\[IOC_10GE\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum5=`grep -n -A 1 "\[IOC_8G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum6=`grep -n -A 1 "\[IOC_16G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            sed -i "/ioc_fw/ s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum1 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum2 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum3 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum4 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum5 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum6 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            
            mv ./update_pkg/port/ioc_fw_bak${ioc_new_ver}.bin ./update_pkg/port/ioc_fw.bin
            sync

            #reload checksum
            #local_sum=`sha256sum ./product_versions.${PANGEA_LINK}.conf`
            #sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./sha256sum_sync

            #local_sum=`md5sum ./product_versions.${PANGEA_LINK}.conf`
            #sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./md5sum_sync

            local_sum=`sha256sum ./update_pkg/port/ioc_fw.bin`
            sed -i "/ioc_fw.bin/c$local_sum" ./sha256sum_sync

            local_sum=`md5sum ./update_pkg/port/ioc_fw.bin`
            sed -i "/ioc_fw.bin/c$local_sum" ./md5sum_sync

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

            #change disk to Readonly
            #OS_SetStartUpDiskProperty /startup_disk/image/ ro
            fwpackage="hi1821_firmware_SPH050_DTS2022081400679"
            
            cd /OSM/conf/
            #修改原versions.conf文件中ioc_fw版本号，取代原来从tar.gz中拷贝覆盖的方式
            fwnum1=`grep -n -A 1 "\[SmartIO\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum2=`grep -n -A 1 "\[IOC_HD\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum3=`grep -n -A 1 "\[IOC_HD_16\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum4=`grep -n -A 1 "\[IOC_10GE\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum5=`grep -n -A 1 "\[IOC_8G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            fwnum6=`grep -n -A 1 "\[IOC_16G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
            sed -i "/ioc_fw/ s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum1 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum2 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum3 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum4 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum5 s/$ioc_new_ver/$oldfwver_d/" versions.conf
            sed -i "$fwnum6 s/$ioc_new_ver/$oldfwver_d/" 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/ResumeiocFwFlagSPH050Flag ]; then 
                rm -f /startup_disk/conf/conf_local/ResumeiocFwFlagSPH050Flag
            fi

            if [ -f /startup_disk/image/$BOOT_LINK/ioc_fw_bak${ioc_new_ver}.bin ]; then 
                rm -f /startup_disk/image/$BOOT_LINK/ioc_fw_bak${ioc_new_ver}.bin
            fi

            if [ -f /startup_disk/image/$BOOT_LINK/versions_bak${ioc_new_ver}.conf ]; then 
                rm -f /startup_disk/image/$BOOT_LINK/versions_bak${ioc_new_ver}.conf
            fi
            
            sync

            OS_echo "[hi1821fw] ------Resume firmware complete."
        else
            OS_echo "[hi1821fw] ------The firmware no need to resume."
        fi
    else
        # 1.B控无标记文件（当A控已打补丁，执行了enableiocfw命令并上下电，B控更换未打补丁的控制器备件，新B控不会同步标志文件，同步的固件是补丁固件）
            # 2.B控无标记文件（当A控已打补丁，执行了enableiocfw命令并上下电，再执行disableiocfw命令并不上下电，B控插入未打补丁的控制器备件，新B控不会同步标志文件，同步的固件是补丁固件）
        if [ -f /startup_disk/image/boot_osp/update_pkg/port/ioc_fw_bak${ioc_new_ver}.bin -o -f /startup_disk/image/boot_osp_bak/update_pkg/port/ioc_fw_bak${ioc_new_ver}.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/port/ioc_fw.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 "[Hi1821] ------Copy ioc_fw.bin and versions.conf file from boot_osp_bak to boot_osp."
                    cp ./update_pkg/port/ioc_fw.bin /startup_disk/image/boot_osp/update_pkg/port/
                    #cp ./versions.conf /startup_disk/image/boot_osp/
                    #cp ./product_versions.${PANGEA_LINK}.conf /startup_disk/image/boot_osp/
                    sync

                    #change working directory
                    cd /startup_disk/image/boot_osp/
                    orifwver_d=`cat ./versions.conf | grep "ioc_fw.bin" | tail -1 | awk -F "," '{print $2}'`
                    OS_echo "[hi1821fw] ------kuorong tongbu boot_osp, the 1821 ver is $orifwver_d."
                    fwnum1=`grep -n -A 1 "\[SmartIO\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum2=`grep -n -A 1 "\[IOC_HD\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum3=`grep -n -A 1 "\[IOC_HD_16\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum4=`grep -n -A 1 "\[IOC_10GE\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum5=`grep -n -A 1 "\[IOC_8G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum6=`grep -n -A 1 "\[IOC_16G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    sed -i "/ioc_fw/ s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum1 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum2 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum3 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum4 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum5 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    sed -i "$fwnum6 s/$orifwver_d/$ioc_new_ver/" versions.conf
                    #reload checksum
                    #local_sum=`sha256sum ./product_versions.${PANGEA_LINK}.conf`
                    #sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./sha256sum_sync

                    #local_sum=`md5sum ./product_versions.${PANGEA_LINK}.conf`
                    #sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./md5sum_sync

                    local_sum=`sha256sum ./update_pkg/port/ioc_fw.bin`
                    sed -i "/ioc_fw.bin/c$local_sum" ./sha256sum_sync                   

                    local_sum=`md5sum ./update_pkg/port/ioc_fw.bin`
                    sed -i "/ioc_fw.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/UpdateiocFwFlagSPH050Flag
				if [ -f /startup_disk/image/boot_osp/update_pkg/port/ioc_fw_bak${ioc_old_ver_120T13}.bin -o -f /startup_disk/image/boot_osp_bak/update_pkg/port/ioc_fw_bak${ioc_old_ver_120T13}.bin ] ; then
					touch -f /startup_disk/conf/conf_local/UpdateiocFwFlagSPH007Flag
				fi
            sync

            OS_echo "[hi1821fw] ------patch Hi1821 firmware."
        elif [ -f /startup_disk/image/boot_osp/update_pkg/port/ioc_fw_bak${ioc_old_ver_120T13}.bin -o -f /startup_disk/image/boot_osp_bak/update_pkg/port/ioc_fw_bak${ioc_old_ver_120T13}.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/port/ioc_fw.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 "[Hi1821] ------Copy ioc_fw.bin and versions.conf file from boot_osp_bak to boot_osp."
                    cp ./update_pkg/port/ioc_fw.bin /startup_disk/image/boot_osp/update_pkg/port/
                    #cp ./versions.conf /startup_disk/image/boot_osp/
                    #cp ./product_versions.${PANGEA_LINK}.conf /startup_disk/image/boot_osp/
                    sync

                    #change working directory
                    cd /startup_disk/image/boot_osp/
                    orifwver_d=`cat ./versions.conf | grep "ioc_fw.bin" | tail -1 | awk -F "," '{print $2}'`
                    OS_echo "[hi1821fw] ------kuorong tongbu boot_osp, the 1821 ver is $orifwver_d."
                    fwnum1=`grep -n -A 1 "\[SmartIO\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum2=`grep -n -A 1 "\[IOC_HD\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum3=`grep -n -A 1 "\[IOC_HD_16\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum4=`grep -n -A 1 "\[IOC_10GE\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum5=`grep -n -A 1 "\[IOC_8G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    fwnum6=`grep -n -A 1 "\[IOC_16G\]" "versions.conf" | tail -1 | awk -F "-" '{print $1}'`
                    sed -i "/ioc_fw/ s/$orifwver_d/$ioc_old_ver_120T13/" versions.conf
                    sed -i "$fwnum1 s/$orifwver_d/$ioc_old_ver_120T13/" versions.conf
                    sed -i "$fwnum2 s/$orifwver_d/$ioc_old_ver_120T13/" versions.conf
                    sed -i "$fwnum3 s/$orifwver_d/$ioc_old_ver_120T13/" versions.conf
                    sed -i "$fwnum4 s/$orifwver_d/$ioc_old_ver_120T13/" versions.conf
                    sed -i "$fwnum5 s/$orifwver_d/$ioc_old_ver_120T13/" versions.conf
                    sed -i "$fwnum6 s/$orifwver_d/$ioc_old_ver_120T13/" versions.conf
                    #reload checksum
                    #local_sum=`sha256sum ./product_versions.${PANGEA_LINK}.conf`
                    #sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./sha256sum_sync

                    #local_sum=`md5sum ./product_versions.${PANGEA_LINK}.conf`
                    #sed -i "/product_versions.${PANGEA_LINK}.conf/c$local_sum" ./md5sum_sync

                    local_sum=`sha256sum ./update_pkg/port/ioc_fw.bin`
                    sed -i "/ioc_fw.bin/c$local_sum" ./sha256sum_sync                   

                    local_sum=`md5sum ./update_pkg/port/ioc_fw.bin`
                    sed -i "/ioc_fw.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/UpdateiocFwFlagSPH007Flag
            sync

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

    return 0
}
# **************************************************************************** #
# Function Name: OS_SET_TCP_MEM()
# Description: set tcp_mem
# Parameter:
# Return:
# Author:
# **************************************************************************** #
OS_SET_TCP_MEM()
{
    PRODUCT_TYPE=$(cat /OSM/conf/product.ini | awk '/ProductName =/ {print $(NF-1)}' | tr 'a-z' 'A-Z')
    MEM_SIZE=$(cat /proc/osp/bsp | grep "System have" | awk {'print $3'})
    num=${#G_TCP_MEM_SIZE[*]}
    for((i=0;i<$num;i++))
    do
       L_TCP_MEM_INFO=(${G_TCP_MEM_SIZE[$i]})
       L_TYPE=`echo ${L_TCP_MEM_INFO} |awk {'print $1'}`
       if [ "${L_TYPE}" == "${PRODUCT_TYPE}" ];then
          for L_MEM in ${L_TCP_MEM_INFO[@]}
          do
             if [ "${L_MEM}" == "${L_TYPE}" ];then
                  continue
             fi
             L_SYS_SIZE_INFO=`echo ${L_MEM} |awk -F# {'print $1'}`
             if [ ${MEM_SIZE} -ge ${L_SYS_SIZE_INFO} ];then
                L_SIZE=`echo ${L_MEM} |awk -F'#' {'print $2'}`
                L_VALUE1=`echo $L_SIZE |awk -F';' {'print $1'}`
                L_VALUE2=`echo ${L_SIZE} |awk -F';' {'print $2'}`
                L_VALUE3=`echo ${L_SIZE} |awk -F';' {'print $3'}`
                echo ${L_VALUE1} ${L_VALUE2} ${L_VALUE3} > /proc/sys/net/ipv4/tcp_mem
                return 0
             fi
          done
          return 0
       fi
    done
}
####-----------------main--------------------####
if [ -f /startup_disk/image/boot/thirdpauseflag ]; then
    OS_echo "product_third_drv_insmod pause flag is set. Do not insmod modules!"
    exit 0;
fi

ldconfig &>/dev/null
replace_hi1821_firmware
replace_bios_firmware
/OSM/script/goall.sh &>/dev/kmsg

L_TMP=$?

OS_SET_TCP_MEM

CMM_INSMOD=`lsmod | grep ^"cmm_dump" | awk '{print $1}'`
if [ "$(OS_GET_PRODUCT_TYPE)" = "$G_PRODUCT_TYPE_S6900" -a "${CMM_INSMOD}" != "" ];then
    OS_EXPORT_CACHE_DUMP
fi

##export cmm memory
/OSM/script/cmm_dump_mem.sh
exit "$L_TMP"
