#!/bin/sh
## Finally test the TFTP connexion and flash the default firmwares
## 
##	The TFTP connexion might have been used previously but with 
##	this test we ensure that it correctly work 
##
## Authors: 
##	- Benoit Rat (Seven Solutions, www.sevensols.com)
##
## 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.
#######################################################################################################

printf "======: $0\n"
. 000-functions.sh

bin_dir="/tmp"


printf "Flashing at91bootstrap and barebox on dataflash: "
ret=0
checkLoadTFTP at91bootstrap.bin 1 ${bin_dir}
checkLoadTFTP barebox.bin 1 ${bin_dir}
if [ $ret -eq "0" ]; then 
	cat ${bin_dir}/at91bootstrap.bin > /dev/mtd2
	dd bs=33792 seek=1 if=${bin_dir}/barebox.bin of=/dev/mtd2
	printf "OK\n"
else
	printf "ERROR: While downloading\n"
fi
ret0=$ret




printf "Flashing kernel and filesystem on nandflash: "
ret=0
checkLoadTFTP zImage 1 ${bin_dir}
checkLoadTFTP wrs-image.jffs2.img 1 ${bin_dir}
if [ $ret -eq "0" ]; then 
	# Flashing kernel (without erasing barebox env)
	flash_erase /dev/mtd0 0x100000 0x40
	nandwrite --pad -s 0x00100000 /dev/mtd0  ${bin_dir}/zImage 

	# Flashing whole partiton for FS.
	flash_eraseall /dev/mtd1
	nandwrite --pad /dev/mtd1 ${bin_dir}/wrs-image.jffs2.img
	printf "OK\n"
else
	printf "ERROR: While downloading\n"
fi
ret1=$ret

exit_multierr $ret0 $ret1