#!/bin/sh
# script to JTAG and program balloons 
# use arguments to skip sections:
#   nocpld - skip CPDL programming
#   nojflash - skip mini-bootldr jflashing
#   nobootldr - skip bootldr upload
#   nokernel - skip kernel upload
#   noroot - skip root fs upload

PORT=/dev/ttyS1
COMMANDS=/home/aleph1/armlinux/boards/balloon/bin
FILES=/home/aleph1/armlinux/boards/balloon/2.052

CPLD="070803_NAND_ground.xsvf"
SMALLBOOT="bootldr.small"
BOOTLOADER="bootldr.fast"
KERNEL="zImage"
ROOTFS="balloon-guralp.yaffs.gz"


function doJTAG
{
#$1 is user prompt, $2 is stage info, $3 is programming command
   #issue prompt with a beep
   echo -e "$1\a" 
   read KEY
   echo $2
   if $3; then
     echo "OK" 
   else 
     echo "Failed - Aborting"; exit 1;
   fi
}

function resetwait
{
  #wait for boot prompt"
  sleep 0.5
  #'press space' in case there is a kernel present
  # chat: verbose,to stderr, timeout 4 seconds. 
  # Send space (without newline) and wait for boot> prompt
  # if return is 3 (timeout) should tell user.
  chat -s -t 4 '' " \c" boot\> <$PORT >$PORT
#  echo -e " " > $PORT
#  sleep 5
#  echo -e "\r" > $PORT
  #we should have a prompt now - how likely is this to work? :-) 
}

function upload
{
# $1 is name for status prompt, $2 is bootldr command, 
# $3 is file to upload, $4 is pause to allow flash programming
  echo "Uploading $1:"
  chat -v -E -t 1 -s boot\>--boot\> "$2" 'ready for xmodem download..' '\c' <$PORT >$PORT  
#  echo -e "$2" > $PORT
# Xmodem send - timeout in 20 seconds
  if sx -t 200 -vv $3 > $PORT < $PORT; then
    echo "Uploaded $1 OK" 
  else 
    echo "Failed - Aborting script"; exit 1;
  fi
  #pause to program bootldr
  sleep $4
}

######### Program starts here #########

#set 115200,8N1, no hardware etc.
#stty 1:0:1cb2:0:3:1c:7f:15:4:5:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0 < $PORT

# if 'nocpld' on command line then skip
#needs audio-bodge code in here

if ! [ -n "$(echo $@ | grep 'nocpld')" ]; then
  doJTAG "**Check Power On, Connect CPLD JTAG - then hit Return**" \
   "Programming CPLD:" "$COMMANDS/playxsvf $FILES/$CPLD";
fi

# if 'nojflash' on command line then skip
if ! [ -n "$(echo $@ | grep 'nojflash')" ]; then
  doJTAG "**Change to main JTAG - then hit Return**" \
   "Jflashing Small Bootloader:" \
   "$COMMANDS/Jflash-balloon --noverify $FILES/$SMALLBOOT";
fi

#does bootldr reset automatically? assume not:
echo -e "**Check serial cable attached, Reset board - then hit Return**\a" 
read KEY
resetwait

if ! [ -n "$(echo $@ | grep 'nobootldr')" ]; then
  upload "Bootloader" "load bootldr" $FILES/$BOOTLOADER 5
# start new bootldr
  chat -v -t 1 -s boot\>--boot\> reset Rebooting...boot> '\c' <$PORT >$PORT  
#  echo -e "\rreset" > $PORT
  resetwait
fi

if ! [ -n "$(echo $@ | grep 'nokernel')" ]; then
  upload "Kernel" "yaffs write zImage" $FILES/$KERNEL 30
fi

if ! [ -n "$(echo $@ | grep 'noroot')" ]; then
  upload "Root Filesystem" "load root" $FILES/$ROOTFS 80
fi

echo
echo -e "**Balloon board programming complete**\a"
