#!/bin/sh

mnt_pt=/mnt/yaffs
#set -x
# Unmount NAND and erase
umount $mnt_pt

MTD_RAW_DEVICE=/dev/mtd3
if [ "$1" != "" ]; then
    MTD_RAW_DEVICE=$1
fi

echo "Using $MTD_RAW_DEVICE"
MTD_BLOCK_DEVICE=`echo $MTD_RAW_DEVICE | sed -e 's/mtd/mtdblock/'`
flash_eraseall $MTD_RAW_DEVICE

# Sleep for a few to allow output form flash_eraseall to show up before
# kernel messages from mount
sleep 5

# mount the flash
mkdir -p $mnt_pt
if [ -n "$2" ]; then
    options="-o $2"
fi
mount -t yaffs $options $MTD_BLOCK_DEVICE $mnt_pt
cd $mnt_pt

# Name of file to copy
fname=test-data

# Source of file
src=$mnt_pt

totalsize=`expr 1024 \* 1024`
pagesize=2048
numpages=`expr $totalsize / $pagesize`

srcfile=${src}/${fname}

echo "Total bytes $totalsize = $numpages pages"
echo "Creating $srcfile random test data, size=$totalsize"

dd if=/dev/urandom of=$srcfile bs=$pagesize count=$numpages

failed_cnt=0
cnt=1

# Calculate good md5sum of the card
md5=`cat $srcfile | md5sum`

newfile=
# Make initial copy from the SD card
# cp ${src}${fname} ${fname}${cnt}

# Loop through 100000 passes
while [ $cnt -lt 100000 -a $failed_cnt -lt 20 ]; do
    # Report the pass we're on
    echo "Pass: $cnt Failed: $failed_cnt"
    date

    # save new file since we'll remove it after the copy
    last_file=$newfile

    # Calculate next file index
    cnt2=`expr $cnt + 1`
    newfile=${fname}${cnt2}

    # copy the file
    echo "Copy $srcfile to $newfile"
    cp $srcfile /tmp/srcfile
    if [ $? -ne 0 ]
    then
	echo "Copy from $srcfile -> /tmp/srcfile failed; aborting"
	exit -1
    fi
    cp /tmp/srcfile $newfile
    if [ $? -ne 0 ]
    then
	echo "Copy from /tmp/srcfile -> $newfile failed; aborting"
	exit -1
    fi

    echo "Force unwritten blocks to MTD"
    # Force any unwritten buffers to NAND(flush the page cache)
    sync

    # Drop the page cahce (forces re-read of data)
    if [ -e /proc/sys/vm/drop_caches ]; then
	echo "Dropping page cache"
	echo 3 > /proc/sys/vm/drop_caches
    fi

    # Calculate the md5sum of the fresh copy
    echo "Calculate MD5 of fresh copy $newfile"
    cp $newfile /tmp/newfile
    test_md5=`cat /tmp/newfile | md5sum`

    # If its different, then punt
    if [ "$md5" != "$test_md5" ]; then
#	set -x
	failed_cnt=`expr $failed_cnt + 1`
	echo "md5sum: $newfile $test_md5 different than expected $md5"
	# find where they differ
	page=0
	while [ $page -lt $numpages ]; do
	    dd if=/tmp/srcfile of=/tmp/srcpage bs=$pagesize skip=$page count=1 2> /dev/null
	    dd if=/tmp/newfile of=/tmp/newpage bs=$pagesize skip=$page count=1 2> /dev/null
	    cmp -s /tmp/srcpage /tmp/newpage
	    if [ $? -ne 0 ]; then
		echo "Files differ in page $page:"
		hexdump -C /tmp/srcpage > /tmp/srcpage.dump
		hexdump -C /tmp/newpage > /tmp/newpage.dump
		echo "Source page:"
		cat /tmp/srcpage.dump
		echo "Mismatch page:"
		cat /tmp/newpage.dump
#		break
	    fi
	    page=`expr $page + 1`
	done
#	set +x
	continue
#	exit -1
    fi

    # remove the previous copy
    if [ -n "$last_file" ]; then
	echo "Removing $last_file"
	rm $last_file
    fi

    # srcfile=$newfile

    # update the count
    cnt=$cnt2

    # Dump out the YAFFS ecc counts
    cat /proc/yaffs > /tmp/yaffs.stats
    cat /tmp/yaffs.stats | grep '^n_ecc'
    cat /tmp/yaffs.stats | grep '^n_page'
    cat /tmp/yaffs.stats | grep '^n_eras'
    cat /tmp/yaffs.stats | grep '^n_ret'
    cat /tmp/yaffs.stats | grep '^n_stale'
    cat /tmp/yaffs.stats | grep 'strike'
    echo

done

exit 0
