#!/bin/sh
# A script to ease the use of the usb loader, possibly changing the mac address
#
# Authors: 
#	- Benoit Rat (Seven Solutions, www.sevensols.com)
# 	- Alessandro Rubini
#
# GNU Lesser General Public License Usage
# This file may be used under the terms of the GNU Lesser
# General Public License version 2.1 as published by the Free Software
# Foundation and appearing in the file LICENSE.LGPL included in the
# packaging of this file.  Please review the following information to
# ensure the GNU Lesser General Public License version 2.1 requirements
# will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
#########################################################################	


showhelp()
{
	printf "Usage: $0 [options] MAC [<firmware>.tar.gz] [DEV]\n\n"
	printf "MAC:\t MAC address in hexadecimal seperated by ':' (i.e, AB:CD:EF:01:23:45)\n"
	printf "<firmware>.tar.gz: Use the file in the firmware to flash the device\n"
	printf "DEV:\t The usb device (by default it is /dev/ttyACM0)\n"
	printf "Options: \n"
	printf "  -h|--help\t Show this help message\n"
	printf "  -m|--mode\t can be: default (df and nf), df (dataflash),\n\t\t nf (nandflash), ddr (ddr memories).\n"
	printf "  -e \t\t Completely erase the memory (Can erase your configuration)\n"
	printf "  -b|--build\t Use file that you have build in the WRS_OUTPUT_DIR\n"
	printf "  --test\t Use file for testing the switch (not available)\n"
	printf "  --silent\t Don't ask MAC or S/N and use default 02:0B:AD:C0:FF:EE\n\n"
	exit 0
}


checkExit()
{
	err=0;
	if [ $1 ]; then
		if [ -f $1 ]; then
			return 0;
		else
			echo "Can't find $1" >& 2;
		fi
	else
		echo "varname not set"
	fi
	exit 1
}

checkMAC()
{
    X="[0-9a-fA-F][0-9a-fA-F]"
    if echo $1 | grep "^${X}:${X}:${X}:${X}:${X}:${X}\$" > /dev/null; then
	return 0
    fi
    return 1
}

modifyMAC()
{
	origin=$1
	new=$2
	cp $origin $new
	
	# MAC address doesn't need any modification
	if [ $silent ]; then 
	    return 1;
	fi
	# Modify MAC address
	sed -i "s/02:0B:AD:C0:FF:EE/$MAC/" $new
	echo "MAC is now: $MAC"
	return 0
}

# Go to the top directory
topdir=$(dirname $0)/..

# Sanity checks
if [ -d ${topdir}/usb-loader ]; then true; else
    echo "$0: usb-loader is not found" >& 2
    exit 1
fi

# build flasher itself
if CC=cc make -s -C ${topdir}/usb-loader; then true; else
	echo "$0: Error compiling usb-loader" >&2; exit 1;
fi

# parse command line
MAC=""
DEV=""
FLAGS=""

at91bs="${topdir}/binaries/at91bootstrap.bin"
barebox="${topdir}/binaries/barebox.bin"
kernel="${WRS_OUTPUT_DIR}/images/zImage"
rootfs="${WRS_OUTPUT_DIR}/images/wrs-image.jffs2.img"

while [ $# -ge 1 ]; do
    case $1 in
	-b|--build) 
		at91bs=${WRS_OUTPUT_DIR}/images/at91bootstrap.bin;
		barebox=${WRS_OUTPUT_DIR}/images/barebox.bin
		kernel=${WRS_OUTPUT_DIR}/images/zImage
		rootfs=${WRS_OUTPUT_DIR}/images/wrs-image.jffs2.img
		shift;;
	*.tar.gz) 
		checkExit $1
		tar --directory /tmp -xzf $1	
		at91bs=/tmp/at91bootstrap.bin;
		barebox=/tmp/barebox.bin
		kernel=/tmp/zImage
		rootfs=/tmp/wrs-image.jffs2.img
		shift;;
	-h|--help) 	showhelp; shift;;
	-m|--memmode)
	    # verify memory mode
	    if [ "$2" != "nf" ] && [ "$2" != "df" ] && [ "$2" != "ddr" ] && [ "$2" != "default" ]; then
		echo "Error: Invalid memory mode"
		showhelp
		exit 1
	    fi
	    memmode="$2"
	    shift; shift;;
	--silent)
	    # set default the MAC address if user didn't set a MAC address
	    if [ "x$MAC" = "x" ]; then
		MAC="02:0B:AD:C0:FF:EE"
	    fi
	    silent=1;
	    shift;;
	/* ) DEV="-s $1"; shift ;;
	*:* ) MAC="$1"; shift ;;
	
	-*) FLAGS="${FLAGS} $1"; shift;;
	* ) echo "$0: Invalid argument \"$1\"" >&2; exit 1;;
    esac
done

#MAC address is a mandatory field
if [ "x$MAC" = "x" ]; then
    echo "Error: MAC address is mandatory"
    showhelp
    exit 1
else
    checkMAC $MAC
    if [ $? -eq 1 ];then
	echo "Error: MAC address is invalid ($MAC)"
	exit 1
    fi
fi

## Selecting the running memmode
if [ "x$memmode" = "xdf" ]; then
	df=1
elif [ "x$memmode" = "xnf" ]; then
	nf=1
elif [ "x$memmode" = "xddr" ]; then
	test=1
else 
	df=1
	nf=1
fi

# Check if atmel sam-ba is find by lusb
msg="Waiting for at91sam SAMBA bootloader on usb\nPlease check that the Dataflash is short-circuited!\n"
while true; do
	lsusb | grep "at91sam" > /dev/null
	if [ "$?" -eq "0" ]; then
		break    
	else
		if [ -n "$msg" ]; then
			printf "$msg"
			msg=""
		fi
		sleep 1
		printf "."
	fi
done
if [ -z "$msg" ]; then
	printf "\tOK\n"
	read -p "Remove the Dataflash jumper (Press any key)" notused
fi

## Flashing DataFlash
if [ $df ]; then

	checkExit $at91bs
	checkExit $barebox
	
	Tbarebox=$(mktemp /tmp/barebox.XXXXXX)
	modifyMAC ${barebox} ${Tbarebox}

	${topdir}/usb-loader/mch_flasher -m df $FLAGS $DEV ${at91bs} 0 ${Tbarebox} 33792		
fi

## Flashing NANDFlash
if [ $nf ]; then

	checkExit $kernel
	checkExit $rootfs

	${topdir}/usb-loader/mch_flasher -m nand $FLAGS $DEV ${kernel} 0x00100000 ${rootfs} 0x04000000	
fi

## Loading in DDR
if [ $test ]; then

	checkExit $barebox
	checkExit $kernel
	checkExit $rootfs
	
	Tbarebox=$(mktemp /tmp/barebox.XXXXXX)
	modifyMAC ${barebox} ${Tbarebox}

	${topdir}/usb-loader/mch_flasher -m ddr $FLAGS $DEV ${Tbarebox} 0x0 ${kernel} 0x1000000 ${rootfs} 0x2000000
fi


