#!/bin/sh

cd $(dirname $0)

DEVICE=$1
WRITE_ALL=$2

if [ -z "$DEVICE" ]; then
  DEVICE=0
fi

if [ -z "$WRITE_ALL" ]; then
  WRITE_ALL=0
fi

SYSTEM=../01_M30_firm/system_fw.bin
SYSTEM2=../01_M30_firm/system_fw2.bin
SYSTEM_CFG_TXT=./config_system.txt
SYSTEM_CFG_BIN=./config_system.bin
UPDATE_FLAG=./write_flag.bin

rm -f $SYSTEM_CFG_BIN

echo "========================================"
echo " Flash Write Start (SYSTEM_FW)"
echo "----------------------------------------"
echo " make system fw "
echo "----------------------------------------"
if [ ! -f $SYSTEM_CFG_TXT ]; then
    echo "[ERR] $SYSTEM_CFG_TXT file not found."
    exit 1
fi
if [ ! -f $SYSTEM ]; then
    echo "[ERR] $SYSTEM file not found."
    exit 1
fi
if [ ! -f $SYSTEM2 ]; then
    echo "[ERR] $SYSTEM2 file not found."
    exit 1
fi
if [ ! -f $UPDATE_FLAG ]; then
    echo "[ERR] $UPDATE_FLAG file not found."
    exit 1
fi

./m3x_config_system $SYSTEM_CFG_TXT > /dev/null
if [ $? -ge 1 ]; then
    exit 1
fi

echo "success"

echo "----------------------------------------"
echo " check mode "
echo "----------------------------------------"
./m3x_mainte check 0
case $? in
0)
    MODE=0
    ;;
1)
    STATE=`./m3x_mainte br32 0 0 0x02000200`
    if [ $? -ge 2 ]; then
        exit 1
    fi

    if [ "`echo $STATE | grep -e "=> 0x00000003" -e "=> 0xFFFFFFFF"`" ]; then
        echo ""
        echo "<< Need to change the MCPU to boot/initial state >>"
        echo ""
        exit 1
    fi

    MODE=1
    ;;
*)
    exit 1
    ;;
esac

if [ $WRITE_ALL -eq 0 ]; then
    echo "----------------------------------------"
    echo " load flash function "
    echo "----------------------------------------"
    ./m3x_mainte boot 0
    if [ $? -ge 2 ]; then
        exit 1
    fi

    ./m3x_mainte load 0 flash
    if [ $? -ge 2 ]; then
        ./m3x_mainte stop 0
        exit 1
    fi

    echo "----------------------------------------"
    echo " start sector erase "
    echo "----------------------------------------"
    ./m3x_mainte flash 0 sector_erase 0x00E40000 0x00040000
    if [ $? -ge 2 ]; then
        ./m3x_mainte stop 0
        exit 1
    fi

    ./m3x_mainte flash 0 sector_erase 0x00040000 0x001C0000
    if [ $? -ge 2 ]; then
        ./m3x_mainte stop 0
        exit 1
    fi


    ./m3x_mainte flash 0 sector_erase 0x00E80000 0x00040000
    if [ $? -ge 2 ]; then
        ./m3x_mainte stop 0
        exit 1
    fi

    ./m3x_mainte flash 0 sector_erase 0x00EC0000 0x00040000
    if [ $? -ge 2 ]; then
        ./m3x_mainte stop 0
        exit 1
    fi
    ./m3x_mainte flash 0 sector_erase 0x00DC0000 0x00040000
    if [ $? -ge 2 ]; then
        ./m3x_mainte stop 0
        exit 1
    fi
fi

echo "----------------------------------------"
echo " start program "
echo "----------------------------------------"
echo "Writing ${SYSTEM##*/}... "
./m3x_mainte flash 0 program 0x00040000 $SYSTEM
if [ $? -ge 2 ]; then
    ./m3x_mainte stop 0
    exit 1
fi
echo "Writing ${UPDATE_FLAG##*/}... "
./m3x_mainte flash 0 program 0x00E40000 $UPDATE_FLAG
if [ $? -ge 2 ]; then
    ./m3x_mainte stop 0
    exit 1
fi

echo "Writing ${SYSTEM2##*/}... "
./m3x_mainte flash 0 program 0x00DC0000 $SYSTEM2
if [ $? -ge 2 ]; then
    ./m3x_mainte stop 0
    exit 1
fi

echo "Writing ${SYSTEM_CFG_BIN##*/}... "
./m3x_mainte flash 0 program 0x00EC0000 $SYSTEM_CFG_BIN
if [ $? -ge 2 ]; then
    ./m3x_mainte stop 0
    exit 1
fi

echo "Writing ${UPDATE_FLAG##*/}... "
./m3x_mainte flash 0 program 0x00E80000 $UPDATE_FLAG
if [ $? -ge 2 ]; then
    ./m3x_mainte stop 0
    exit 1
fi

if [ $WRITE_ALL -eq 0 ]; then
    echo "----------------------------------------"
    echo " stop flash function "
    echo "----------------------------------------"
    ./m3x_mainte stop 0
    if [ $? -ge 1 ]; then
        exit 1
    fi

    echo "----------------------------------------"
    echo " program end "
    echo "========================================"

    if [ $MODE -eq 1 ]; then
        echo ""
        echo "<<                   Need to restart the host PC                    >>"
        echo "<< It is also possible to restart after updating the other firmware >>"
        echo ""
    fi
fi

exit 0
