#!/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

MCPU=../01_M30_firm/mcpu_fw.bin
UPDATE_FLAG=./write_flag.bin

echo "========================================"
echo " Flash Write Start (MCPU_FW)"
echo "----------------------------------------"
echo " make main fw "
echo "----------------------------------------"
if [ ! -f $MCPU ]; then
    echo "[ERR] $MCPU file not found."
    exit 1
fi
if [ ! -f $UPDATE_FLAG ]; then
    echo "[ERR] $UPDATE_FLAG file not found."
    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 0x00F00000 0x00040000
    if [ $? -ge 2 ]; then
        ./m3x_mainte stop 0
        exit 1
    fi
    
    ./m3x_mainte flash 0 sector_erase 0x01000000 0x01000000
    if [ $? -ge 2 ]; then
        ./m3x_mainte stop 0
        exit 1
    fi
fi

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

echo "Writing ${UPDATE_FLAG##*/}... "
./m3x_mainte flash 0 program 0x00F00000 $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 ""
        read -p "Reboot the MCPU with the updated firmware? [yes/no] " REBOOT

        case $REBOOT in
        yes|y)
            ./m3x_mainte w32 0 0xFFF48030 2 > /dev/null
            if [ $? -ge 2 ]; then
                exit 1
            fi

            for i in `seq 5`
            do
                echo -n "."

                sleep 1

                STATE=`./m3x_mainte br32 0 0 0x02000200`
                if [ $? -ge 2 ]; then
                    exit 1
                fi

                if [ "`echo $STATE | grep -e "=> 0x00000001"`" ]; then
                    echo ""
                    echo "reboot success"
                    exit 0
                fi
            done

            echo "reboot error"
            ./m3x_mainte r32 0 0xFFF48038
            exit 1
            ;;
        *)
            ;;
        esac
    fi
fi

exit 0
