#!/bin/sh
# reset the phone and run internal boot ROM in uart downloader mode
# then phone can be flashed using adequate tools

myname=`basename $0`

exit_ko()
{
	echo "${myname} KO"
	exit 1
}

exit_ok()
{
	echo "${myname} OK"
	exit 0
}

Usage()
{
   echo "Usage:  ${myname} <key>"
   echo "=> set the phone in uart downloader mode using internal boot rom"
   echo "This command is enabled only via console & no ssh session opened."
   echo "Alcatel-Lucent host tools are needed to continue."
   echo "Power off then on to recover normal state."
}

MAC=`CLISettings get CURRENT ENETCFG_MACADDR`
[[ $# -ne 0 ]] || {  Usage ; exit_ok;}
if [[ "$1" != "$MAC" ]]; then
	echo "Bad syntax command !"
	Usage
	exit_ko
fi

# accept only command from console
# no bootrom if at least one ssh session
count=`/bin/netstat -t 2>/dev/null | grep ":ssh" -c`
if [[ $count != 0 ]]; then
	echo "at least one ssh session is opened !";
	Usage
	exit_ko
fi

# do bootrom
echo "${myname} OK"
logger -t ${myname} -p local1.EMERG "Entering UART downloader mode via embedded command $myname"
sleep 2
sync
# write boot to internal rom then request soft reset 
dbg wl 0x8000008c 0x40 > /dev/null
dbg wl 0x80000080 0x1 > /dev/null
