#!/bin/sh
#
#    Copyright (c) 2004-2007 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#
#	The script changes the bootenv settings for the co-cpu
#	at the beginning and at the end of firmwaredownload.
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin

CMD=$1
TYPE=$2

mount_p=`cat /proc/mounts | grep " / " | grep -v rootfs |sed 's? .*??g;s?^.*dev/??g'`
mount_s=`cat /proc/mounts |grep "/mnt" | sed 's? .*??g;s?^.*dev/??g'`

kern_p=`bootenv OSLoader 2>/dev/null | cut -d ';' -f 1`
kern_s=`bootenv OSLoader 2>/dev/null | cut -d ';' -f 2`

upgrade=`bootenv Upgrade 2>/dev/null`
badrootdev=`bootenv BadRootDev 2>/dev/null`

swbd() {
    /bin/sed -n -e 's/^.*\(SWBD[[:digit:]]\{1,\}\).\+$/\1/gp'
}

SWBD=`sin | swbd 2> /dev/null`

#########################
# reset 	#
#########################
if [ "$CMD" == "reset" ];then
	if [ "$upgrade" != "" -a "$upgrade" == "/dev/$mount_s" ]; then
		bootenv -u Upgrade
		bootenv OSRootPartition "$mount_p;$mount_s"
		bootenv OSLoader "$kern_s;$kern_p"
	fi
	exit 0;
fi

#########################
# firmwarerestore 	#
#########################
if [ "$CMD" == "restore" ];then
	#
	# Restoring the PROM 
	#
	if [ -s /mnt/boot/bootrom.bin ]; then
		echo "Restoring the PROM image on the co-CPU..."
    		/usr/sbin/chroot /mnt /sbin/prominst
	fi
	
	#
	# set up the other bootenvs for firmwarerestore
	#
	bootenv OSRootPartition "$mount_s;$mount_p"
	bootenv OSLoader "$kern_s;$kern_p"
	bootenv Upgrade "/dev/$mount_s"
	bootenv SoftUpgrade "commit"
	exit 0;
fi

#########################
# swap partition        #
#########################
if [ "$CMD" == "swap" ];then
	#
	# Just swap the partitions
	#
	bootenv OSRootPartition "$mount_s;$mount_p"
	bootenv OSLoader "$kern_s;$kern_p"
	bootenv -u Upgrade >/dev/null 2>&1
        exit 0; 
fi

#########################
# cocpu netboot         #
#########################
if [ "$CMD" == "netboot" ];then
  if test $SWBD = "SWBD63"; then
	bootenv ethact ENET2
	bootenv loadaddr 0x1000000
	bootenv AutoLoadTimeout 1
	bootenv bootcmd "dhcp;setenv bootargs \${OSLoadOptions};bootm \${loadaddr}"
  fi
  exit 0;
fi

#########################
# cocpu ataboot         #
#########################
if [ "$CMD" == "ataboot" ];then
	if test $SWBD = "SWBD63"; then
		bootenv bootcmd "setenv bootargs mem=\${mem} \${OSLoadOptions};ataboot;bootm 0x400000"
	fi
	exit 0;
fi

#########################
# firmwaredownload 	#
#########################
	
#
# Set BadRootDev before firmwaredownload
#
if [ "$TYPE" == "pre" ]; then
	#
	# If upgrade is set, that means you have run a
	# firmwaredownload. Before rebooting the switch,
	# you are starting another firmwaredownload. In
	# this case, reset all of the bootenvs.
	#
	if [ "$upgrade" != "" -a "$upgrade" == "/dev/$mount_s" ]; then
		bootenv -u Upgrade
		bootenv OSRootPartition "$mount_p;$mount_s"
		bootenv OSLoader "$kern_s;ATA()$kern_new"
	fi

	bootenv BadRootDev $mount_s
	exit 0;
fi

#
# Swap the variables and clear BadRootDev after firmwaredownload
#
if [ "$upgrade" == "" ]; then
	kern_new=`/sbin/mapinst /mnt/boot/zImage.tree.initrd | /usr/bin/cut -d ':' -f 2`
	kern_new=`echo $kern_new`

	bootenv OSRootPartition "$mount_s;$mount_p"
	bootenv OSLoader "ATA()$kern_new;$kern_p"
	bootenv Upgrade "/dev/$mount_s"
	bootenv -u BadRootDev
fi

exit 0;
