#!/bin/sh

# parameters check
USAGE="Usage: $0 -c <cmd> [-r <addr> -f <file> -o <spi/sd>]"
flag="false"

if [ $# -eq 8 ];then
    if [ "$1" = "-c"  -a "$3" = "-r" -a "$5" = "-f" -a "$7" = "-o" ];then
        flag="true"
    fi
fi

if [ $# -eq 2 ];then
    if [ "$1" = "-c" ];then
        flag="ture"
    fi
fi

if [ "$flag" = "false" ];then
    echo "$USAGE"
    echo "Options:"
    echo "      -c     uboot instructions, string types"
    echo "      -r     CP DDR addr, optional"
    echo "      -f     image file, optional"
    echo "Example: "
    echo "      $0 -c \"mmc part\""
    echo "      $0 -c \"mmc write 0x4000000 0x0 0x30000\" -r 0x4000000 -f disk.img -o spi"
    exit 1
fi

BIFCLI="bifrw"
path=$(dirname $0)

AP_CP_COMM_ADDR="0x30FC000"
AP_BUFFER_ADDR="0x30FC200"
CP_BUFFER_ADDR="0x30FC300"
MAGIC_ADDR="0x30FC000"
# CMD_READY_ADDR="0x30FC010"
CMD_STATUS_ADDR="0x30FC010"
CMD_RESULT_ADDR="0x30FC020"

echo "---------- check magic and CP status ----------"
# check communication magic
touch tmp_buffile
tmp=$($BIFCLI --file -r $MAGIC_ADDR -f tmp_buffile -n 4)
msg_magic="0x"$(hexdump -Cv tmp_buffile | awk '{print $5$4$3$2}')
rm -rf tmp_buffile

if [ "$msg_magic" != "0xfefefefe"  ];then
    echo "Error: check magic failed! Not AP-CP communication mode."
    exit 1
else
    echo "    magic check success!"
fi

# check CP status
touch tmp_buffile
tmp=$($BIFCLI --file -r $CMD_STATUS_ADDR -f tmp_buffile -n 4)
msg_cp_status="0x"$(hexdump -Cv tmp_buffile | awk '{print $5$4$3$2}')
rm -rf tmp_buffile

if [ "$msg_cp_status" != "0x00000000" ];then
    echo "Error: CP is running cmd ......"
    exit 1
else
    echo "    CP status check success!"
fi

# write image to CP memory
if [ $# -eq 8 ];then
    echo "---------- write image to CP ----------"
    file_addr=$4
    file_name=$6
    bif_mode=$8

    # align 16 bytes
    size=$(ls -l $file_name | awk '{print $5}')
    if [ $(($size & 0xF)) -ne 0 ];then
        tmpsize=$((($size+15)/16*16))
        tmp=$(dd if=/dev/zero of=$file_name seek=$size bs=1 count=$(($tmpsize - $size)))
    fi

    # write image
    $BIFCLI --file -w $file_addr -f $file_name -o $bif_mode

    echo "    write $file_name to $file_addr success!"
fi

# get cmd_file
cmd=$2
echo "input command: $cmd"
touch cmd_tmp
echo "$cmd" > cmd_tmp

# file align 16 bytes
file_name="cmd_tmp"
size=$(ls -l $file_name | awk '{print $5}')
if [ $(($size & 0xF)) -ne 0 ];then
    tmpsize=$((($size+15)/16*16))
    tmp=$(dd if=/dev/zero of=$file_name seek=$size bs=1 count=$(($tmpsize - $size)))
fi

# write cmd to CP buffer
echo "---------- send cmd to CP ----------"
echo
$BIFCLI --file -w $AP_BUFFER_ADDR -f $file_name
rm -f cmd_tmp

echo "---------- send ready flag to CP --------"
echo
# update cmd_status flag: cmd_ready = 0x1
$BIFCLI --memory -w $CMD_STATUS_ADDR -v 0x01000000

# gets the result of the instruction
echo "--------- cmd is running...... ---------"
for i in `seq 1 1000`;do
    # query delay 1 second
    sleep 1

    # read cmd_status
    touch tmp_buffile
    tmp=$($BIFCLI --file -r $CMD_STATUS_ADDR -f tmp_buffile -n 4)
    msg_cp_status="0x"$(hexdump -Cv tmp_buffile | awk '{print $5$4$3$2}')

    if [ "$msg_cp_status" = "0x00000000" ];then
        if [ $i -lt 10 ];then
            echo "cp_status: free"
        fi
    elif [ "$msg_cp_status" = "0x00000001" ];then
        if [ $i -lt 10 ];then
            echo "cp_status: ready"
        fi
    elif [ "$msg_cp_status" = "0x00000002" ];then
        if [ $i -lt 10 ];then
            echo "cp_status: running"
        fi
    elif [ "$msg_cp_status" = "0x00000003" ];then
        echo "cp_status: finish"
    else
        echo "cp_status: error, no support"
        tmp=$($BIFCLI --memory -w $CMD_STATUS_ADDR -v 0x00000000)
        rm -rf tmp_buffile
        exit 1
    fi
    rm -rf tmp_buffile

    if [ "$msg_cp_status" = "0x00000003" ];then
        # get CP return msg
        touch msg_cp_buffer
        tmp=$($BIFCLI --file -r $CP_BUFFER_ADDR -f msg_cp_buffer -n 256)
        msg=$(cat msg_cp_buffer)
        echo "--------- CP finish the cmd  ---------"
        echo "    result: $msg"
        rm -rf msg_cp_buffer

        # set status
        tmp=$($BIFCLI --memory -w $CMD_STATUS_ADDR -v 0x00000000)
        break
    fi
done
exit 0
