#!/bin/bash
bus=14
address=0x53
offset=0x08
biosversion=$(busctl get-property xyz.openbmc_project.Software.BMC.Updater /xyz/openbmc_project/software/bios_active xyz.openbmc_project.Software.Version Version)
ret=$?
if [ $ret != 0 ]; then
    echo "Failed to get BIOS version from dbus"
    exit 1
fi
biosversion=$(echo "$biosversion" |cut -d "\"" -f2)
echo "BIOS version got from dbus:$biosversion"
if [ "$biosversion" == "null" ]; then
    echo "BIOS version is null, skip"
else
    tmp=$(i2ctransfer -y -f $bus w2@$address $offset 0x00 r32)
    for str in $tmp;do
        if [[ $str != 0x00 ]]; then
            str=$(printf "%d" "$str")
            version=$(echo "$str" | awk '{printf("%c", $str)}')
            version_str="$version_str$version"
        fi
    done
    echo "BIOS version got from epprom:$version_str"
    if [ "$biosversion" != "$version_str" ]; then
        for ((j=0;j < 32;j++));do
            hexArr[$j]=0
        done
        for ((i=0;i < 14;i++));do
            ver=${biosversion:$i:1}
            hexArr[$i]=$(printf "%d" "'$ver")
        done
        i2ctransfer -y -f $bus w34@$address $offset 0x00 "${hexArr[@]}"
        echo "Write BIOS version to epprom:${biosversion}"
    else
        echo "BIOS version same, skip writing BIOS version"
    fi
fi

