#! /bin/sh
# sometimes we need the binary echo, not the shell builtin
ECHO=`which echo`

Usage()
{
	echo "-g : Forces generation of the rootfs image file, default is to reuse the one from the last run"
	echo "-h : Prints this message"
	echo ""
	echo "-r : T20: Selects RAM size (256 | 512)"
	echo "-v : T20: Selects colibri version (V1_1 | V1_2)"
	echo "-b : T20: Selects boot device (hsmmc/nand) (default: nand)"
	echo ""
	echo "-c : Selects configblock partition only update"
	echo "-d : Selects debug boot loader without flashing"
	echo "-k : Selects kernel partition only update"
	echo "-u : Selects boot loader partition only update"
	echo ""
	echo "Example \"./update.sh -r 512 -v V1_2\" flashes everything on a Colibri T20 V1.2 with 512MB RAM"
	echo "        \"./update.sh\" flashes everything on Colibri T30 or Apalis T30"
	echo ""
}

# autotect MODTYPE from from rootfs directory
CNT=`grep -c "T20" rootfs/etc/issue || true`
if [ ${CNT} -ge 1 ] ; then
	echo "Colibri T20 rootfs detected"
	MODTYPE=colibri-t20
	BOOT_DEVICE=nand
else
	CNT=`grep -c "T30" rootfs/etc/issue || true`
	if [ ${CNT} -ge 1 ] ; then
		CNT=`grep -c "Apalis" rootfs/etc/issue || true`
		if [ ${CNT} -ge 1 ] ; then
			echo "Apalis T30 rootfs detected"
			MODTYPE=apalis-t30
			BOOT_DEVICE=hsmmc
		else
			echo "Colibri T30 rootfs detected"
			MODTYPE=colibri-t30
			BOOT_DEVICE=hsmmc
		fi
	else
		echo "can not detect modulue type from ./rootfs/etc/issue"
		echo "exiting"
		exit 1
	fi
fi
BINARIES=${MODTYPE}_bin

#sanity check for correct untared rootfs
HAS_DEV_FILES=0
for f in rootfs/dev/*
do
	if [ -b "$f" ]
	then
		HAS_DEV_FILES=1
		break
	fi
	if [ -c "$f" ]
	then
		HAS_DEV_FILES=1
		break
	fi
done
if [ $HAS_DEV_FILES -eq 0 ]
then
	$ECHO -e "./rootfs/dev/ does not contain block or character files, but it should!"
	$ECHO -e "\033[1mPlease unpack the tarball with root rights.\033[0m"
	$ECHO -e "e.g. sudo tar xjvf T20_LinuxImageV2.0_20130305.tar.bz2"
	exit 1
fi

#initialise options
CFGBLOCK_ONLY=0
DEBUG_ONLY=0
KERNEL_ONLY=0
UBOOT_ONLY=0
REGENERATE=0
#don't provide working defaults which may lead to wrong HW/SW combination
MODVERSION=Add_Version_-v
RAM_SIZE=Add_RAMsize_-r
CONFIGBLOCK_BINARY=configblock.bin

while getopts "b:cdghkr:v:u" Option ; do
	case $Option in
		h) Usage
			# Exit if only usage (-h) was specfied.
			if [[ $# -eq 1 ]] ; then
				exit 10
			fi
			exit 0
			;;
		b) BOOT_DEVICE=$OPTARG
			;;
		c) CFGBLOCK_ONLY=1
			;;
		d) DEBUG_ONLY=1
			;;
		g) REGENERATE=1
			;;
		k) KERNEL_ONLY=1
			;;
		r) RAM_SIZE=$OPTARG
			;;
		u) UBOOT_ONLY=1
			;;
		v) MODVERSION=$OPTARG
			;;
	esac
done

if [ "${MODTYPE}" = "colibri-t20" ] ; then
	#some sanity test, we really need RAM_SIZE and MODVERSION set
	echo ""
	echo "256, 512" | grep -q ${RAM_SIZE} 
	if [ $? -eq 1 ] ; then
		$ECHO -e "\033[1mplease specify your RAM size with the -r parameter\033[0m" 
	fi

	echo "V1_1, V1_2" | grep -q ${MODVERSION} 
	if [ $? -eq 1 ] ; then
		$ECHO -e "\033[1mplease specify your module version with the -v parameter\033[0m" 
		echo ""
		Usage
		exit 0
	fi

	echo "256, 512" | grep -q ${RAM_SIZE} 
	if [ $? -eq 1 ] ; then
		echo ""
		Usage
		exit 0
	fi

	#ext. SD boot, rootfs size is calculated from size(rootfs used) + MIN_PARTITION_FREE_SIZE
	MIN_PARTITION_FREE_SIZE=256

	LOCPATH="LD_LIBRARY_PATH=../t20_nvflash ../t20_nvflash"
	PART_CFG=lnx_${BOOT_DEVICE}.cfg
	ODMDATA="--odmdata 0x1"
	CONFIGBLOCK_BINARY=configblock_${RAM_SIZE}.bin
	if [ "${BOOT_DEVICE}" = "hsmmc" ] ; then
		LOADER=fastboot.bin
		U_BOOT_BINARY=u-boot-hsmmc.bin
		BCT=ColibriT20_333_${RAM_SIZE}_${BOOT_DEVICE}.bct
		IMAGEFILE=APP.ext3
	fi

	# use different u-boot binaries for hsmmc or nand, don't forget to also use the correct one in lnx_hsmmc.cfg!!
	if [ "${BOOT_DEVICE}" = "nand" ] ; then
		LOADER=loader.nb0
		U_BOOT_BINARY=u-boot.bin
		BCT=ColibriT20_333_${RAM_SIZE}_${MODVERSION}_${BOOT_DEVICE}.bct
		IMAGEFILE=USR.yaffs2
	fi
# Moduletype is Colibri T30 or Apalis T30
else
	#rootfs size is calculated from size(rootfs used) + MIN_PARTITION_FREE_SIZE
	MIN_PARTITION_FREE_SIZE=100

	export LOCPATH=../t30_nvflash
	if [ "${BOOT_DEVICE}" = "hsmmc" ] ; then
		PART_CFG=emmc-flash.cfg
		LOADER=bootloader.bin
		U_BOOT_BINARY=u-boot.bin
		ODMDATA="--odmdata 0x40080105"
		IMAGEFILE=APP.ext3

		if [ ${MODTYPE} = "colibri-t30" ] ; then
			# with new kernel, boot with 400MHz, then switch between 400 & 800
			BCT=colibri_t30_12MHz_NT5CC256M16CP-DI_400MHz.bct
#			BCT=colibri_t30_12MHz_NT5CC256M16CP-DI_533MHz.bct
		else
			BCT=apalis_t30_12MHz_MT41K512M8RH-125_533MHz.bct
		fi
	fi
fi

cd ${BINARIES}

#sanity check, can we execute nvflash, e.g. see the help text?
echo $LOCPATH
CNT=`sudo $LOCPATH/nvflash -h | grep -c cmdhelp`  
if [ ${CNT} -eq 0 ] ; then
	echo "The program nvflash can not be executed"
	echo "Are you on a 64bit Linux host without installed 32bit execution environment?"
	$ECHO -e  "\033[1mPlease install e.g. ia32-libs on 64-bit Ubuntu\033[0m"
	exit 1
fi

if [ ${DEBUG_ONLY} -eq 1 ] ; then
	# Debugging only, writes only u-boot to RAM and executes it, downloads also the BCT on target
	sudo  $LOCPATH/nvflash --bct ${BCT} --setbct --configfile ${PART_CFG} ${ODMDATA} --create --bl ${U_BOOT_BINARY} --go
	cd ..
	exit 0
fi

if [ ${CFGBLOCK_ONLY} -ne 1 ] && [ ${KERNEL_ONLY} -ne 1 ] && [ ${UBOOT_ONLY} -ne 1 ] ; then
	#check if we want to delete old rootfs image file, so it gets regenerated
	[ ${REGENERATE} -eq "0" ] || sudo rm -f ${IMAGEFILE} ${IMAGEFILE}_${RAM_SIZE}_${MODVERSION}

	# Prepare full flashing
	if [ "${BOOT_DEVICE}" = "nand" ] ; then
		if [ "${MODVERSION}" = "V1_1" ] ; then
			PAGE_BLOCK=4k512
		fi
		if [ "${MODVERSION}" = "intermediate" ] ; then
			PAGE_BLOCK=2k128
		fi
		if [ "${MODVERSION}" = "V1_2" ] ; then
			PAGE_BLOCK=4k256
		fi
		#build ${IMAGEFILE} if it does not exist
		[ -e "${IMAGEFILE}_${RAM_SIZE}_${MODVERSION}" ] || sudo $LOCPATH/mkyaffs2image ../rootfs ${IMAGEFILE}_${RAM_SIZE}_${MODVERSION} ${PAGE_BLOCK}
		sudo ln -sf ${IMAGEFILE}_${RAM_SIZE}_${MODVERSION} ${IMAGEFILE}
	fi

	if [ "${BOOT_DEVICE}" = "hsmmc" ] ; then
		#test for awk existence
		AWK=`which awk`
		if [ "${AWK}x" = "x" ] ; then
			echo "The program awk is needed"
			$ECHO -e  "\033[1mPlease install e.g. gawk on Ubuntu\033[0m"
			exit 1
		fi
		#build ${IMAGEFILE} if it does not exist
		#make the partition size size(rootfs used + MIN_PARTITION_FREE_SIZE)
		PART_SIZE=`sudo du -DsB1 ../rootfs | awk -v min=$MIN_PARTITION_FREE_SIZE '{rootfs_size=int($1/1024/1024); print (rootfs_size + min) }'`
		[ -e "${IMAGEFILE}" ] || sudo $LOCPATH/genext3fs.sh -d ../rootfs -b ${PART_SIZE} ${IMAGEFILE}
	fi

	# Full flashing
	sudo $LOCPATH/nvflash --bct ${BCT} --setbct --configfile ${PART_CFG} ${ODMDATA} --create --bl ${LOADER} --go
else
	# Partial flashing
	if ([ ${CFGBLOCK_ONLY} -eq 1 ] && [ ${KERNEL_ONLY} -ne 1 ] && [ ${UBOOT_ONLY} -ne 1 ]) || ([ ${CFGBLOCK_ONLY} -ne 1 ] && [ ${KERNEL_ONLY} -eq 1 ] && [ ${UBOOT_ONLY} -ne 1 ]) || ([ ${CFGBLOCK_ONLY} -ne 1 ] && [ ${KERNEL_ONLY} -ne 1 ] && [ ${UBOOT_ONLY} -eq 1 ]) ; then
		if [ ${UBOOT_ONLY} -eq 1 ] ; then
			# U-Boot flashing
			sudo $LOCPATH/nvflash --bl ${LOADER} --download 4 ${U_BOOT_BINARY}
		fi
		if [ ${KERNEL_ONLY} -eq 1 ] ; then
			# Linux kernel flashing
			sudo $LOCPATH/nvflash --bl ${LOADER} --download 7 uImage
		fi
		if [ ${CFGBLOCK_ONLY} -eq 1 ] ; then
			# Configuration block flashing
			sudo $LOCPATH/nvflash --bl ${LOADER} --download 8 ${CONFIGBLOCK_BINARY}
		fi
	else
		echo "Only one of -c, -k or -u is possible at any one time!"
	fi
fi
