#!/bin/bash
# this test is private stuff, please dont run it on your phone unless wyrm
# asked you to do it.

START=262144 #0x40000
END=33423360 #0x1fe0000
TEST_SIZE=131072
BOOT_USB=./boot_usb

function boot_usb {
        $BOOT_USB $@ > /dev/null
        if [ $? != 0 ]; then
                echo boot_usb error
                exit 1
        fi
}

echo Downloading gen-blob..
wget http://people.openezx.org/wyrm/gen-blob/gen-blob -O /tmp/gen-blob-test > /dev/null 2>&1

echo Uploading gen-blob to phone..
boot_usb setflag usb
boot_usb /tmp/gen-blob-test 0
rm -f /tmp/gen-blob-test

echo "Last chance to quit, this test will overwrite all your flash"
echo "from $START to `expr $END + $TEST_SIZE`"
sleep 10 #some time is needed for usb reenumeration

echo Test started...
while [ $START -lt $END ];do
	echo -n "testing $TEST_SIZE on $START: "
	dd if=/dev/urandom bs=$TEST_SIZE count=1 of=/tmp/random-test > /dev/null 2>&1
	MD5_ORIG=`md5sum /tmp/random-test | cut -f1`
	boot_usb flash $START /tmp/random-test
	rm -f /tmp/random-test
	boot_usb read $START $TEST_SIZE /tmp/random-test
	MD5_FLASH=`md5sum /tmp/random-test | cut -f1`
	rm -f /tmp/random-test
	if [ "$MD5_ORIG" != "$MD5_FLASH" ];then
		echo "md5sum mismatch! $MD5_ORIG $MD5_FLASH"
		exit 2
	else
		echo "OK!"
	fi
	START=`expr $START + $TEST_SIZE`
done

echo Test passed!
boot_usb off
