#!/bin/sh

if [ $# -ne 2 ] && [ $# -ne 3 ]; then
    echo "param number must be 2 or 3"
    echo "Usage: $(basename $0) <file> <memsize> [spi]"
    echo "Example: $(basename $0) mem.img \$((1024*1024*1024))"
    exit 1
fi
if [ $# -eq 3 ];then
    if [ "$3" != "spi" ];then
        echo "Usage: $(basename $0) <file> <memsize> [spi]"
        exit 1
    fi
fi

file=$1
memsize=$2
if [ $# -eq 2 ];then
    bif_type="sd"
else
    bif_type="spi"
fi
BIFRW="bifrw"

if [ $memsize -le 512 ]; then
    echo "memsize is too small: $memsize"
    exit 1
fi

if [ -x $BIFRW ];then
    echo "$BIFRW is not executable"
    exit 1
fi

# if panic, do not want reboot in 5 sec, run cmd below in CP
#echo 0 > /proc/sys/kernel/panic

# bifsd cannot read 0x0 address
echo "---read first 512 byte through bifspi"
$BIFRW --file -o spi -r 0x0 -f ${file} -n 512

echo "---read $(($memsize-512)) byte through bif$bif_type"
$BIFRW --file -o $bif_type -r 512 -f ${file} -n $(($memsize-512)) -a

echo "Success, file is $file"

