#!/bin/sh
 
__VERSION__=2011-03-07

if [ "$1" == "flash" ] && [ "$#" == "3" ]; then
    case "$2" in
    "bootloader")
        echo "not implenment"
	#flash_eraseall /dev/mtd0
	exit 0
	;;
    "kernel")
	echo "flashing kernel ..."
	flash_eraseall /dev/mtd1
	nandwrite -p /dev/mtd1	"$3"
	exit 0
	;;
    "rootfs")
	echo "flashing rootfs ..."
	ubiformat /dev/mtd2 -f "$3"
	exit 0
	;;
    "data")
	echo "flashing data partition  ..."
	ubiformat /dev/mtd3 -f "$3"
	exit 0
	;;
    esac
fi

if [ "$1" == "mount" ] && [ "$#" == "3" ]; then
    if [ "$2" == "rootfs" ] || [ "$2" == "data" ]; then
	MOUNT_POINT="$3"
	if [ "$2" == "rootfs" ]; then
	    PARTITION="2"
	elif [ "$2" == "data" ]; then
	    PARTITION="3"
	fi

	mkdir -p $MOUNT_POINT
	if [  "$?" != "0" ]; then 
	    echo "mkdir -p $MOUNT_POINT Fail"
	    exit 1
	fi

	ubiattach /dev/ubi_ctrl -m ${PARTITION}
	DEV_UBI=`dmesg | grep "UBI: attached mtd${PARTITION} to" | cut -d ":" -f 2 | cut -d " " -f 5`
	mount -t ubifs ${DEV_UBI}_0 $MOUNT_POINT
        exit 0
    fi
fi

if [ "$1" == "format_data_default" ]; then
    ubiformat /dev/mtd3 -y
    ubiattach /dev/ubi_ctrl -m 3
    ubimkvol /dev/ubi1 -s 1480MiB -N data
# test in xiangfu's nanonote. 1486 is the MAX size we can make
# so we using 1480 for now
    exit 0
fi

if [ "$1" == "fw_setenv_default" ]; then
    fw_setenv bootargs mem=32M console=tty0 console=ttyS0,57600n8 ubi.mtd=2 rootfstype=ubifs root=ubi0:rootfs rw rootwait
    fw_setenv bootcmd nand read 0x80600000 0x400000 0x200000\;bootm
    fw_setenv bootargsfromsd mem=32M console=tty0 console=ttyS0,57600n8 rootfstype=ext2 root=/dev/mmcblk0p1 rw rootwait
    fw_setenv bootcmdfromsd mmc init\; ext2load mmc 0 0x80600000 /boot/uImage\; bootm
    fw_setenv bootdelay 0
    fw_setenv baudrate 57600
    fw_setenv loads_echo 1
    fw_setenv stdin serial
    fw_setenv stderr serial
    fw_setenv stdout serial
    fw_setenv bootcmdf1 mmc init\; ext2load mmc 0:1 0x80600000 /boot/uImage\; bootm
    fw_setenv bootargsf1 mem=32M console=tty0 console=ttyS0,57600n8 rootfstype=ext2 root=/dev/mmcblk0p1 rw rootwait
    fw_setenv bootcmdf2 mmc init\; ext2load mmc 0:2 0x80600000 /boot/uImage\; bootm
    fw_setenv bootargsf2 mem=32M console=tty0 console=ttyS0,57600n8 rootfstype=ext2 root=/dev/mmcblk0p2 rw rootwait
    fw_setenv bootcmdf3 mmc init\; ext2load mmc 0:3 0x80600000 /boot/uImage\; bootm
    fw_setenv bootargsf3 mem=32M console=tty0 console=ttyS0,57600n8 rootfstype=ext2 root=/dev/mmcblk0p3 rw rootwait
    fw_setenv bootcmdf4 mtdparts default\;ubi part rootfs\;ubifsmount rootfs\;ubifsload 0x80600000 /boot/uImage\; bootm
    fw_setenv bootargsf4 mem=32M console=tty0 console=ttyS0,57600n8 ubi.mtd=2 rootfstype=ubifs root=ubi0:rootfs rw rootwait
    echo "fw_setenv finished, check u-boot env by fw_printenv"
    exit 0
fi

echo "\
Usage: $0 [ command ] [ <para1> <para2> <para3> ]
     flash bootloader|kernel|rootfs|data image_file
             flash nand partition using <image_file>

     mount rootfs|data    mount_point
             mount nand partition

     format_data_default
             lookinside for more detari
             NOTICE:
                     this command will format data
                     partition, create a default
                     1400MB volume, named 'data',
                     ALL data will lost after run.

     fw_setenv_default
             reset bootloader default variable
             lookinside for more detail
             NOTICE:
                     this command write hardcode
                     variables to nand, so it must 
                     run once before you want change
                     change it.

script file for Qi Hardware Ben NanoNote
written by: Xiangfu Liu (xiangfu@sharism.cc)
written with Emacs in Ben NanoNote
                             version: ${__VERSION__}
Report bugs to developer@lists.qi-hardware.com"
exit 0
