#!/bin/sh
#    Copyright (c) 1996-2011 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#      Install script for 2nd LC partition from fwdl context

echo "Start to install the second partition"

do_command()
{
	$*
	if [ $? -ne 0 ]; then
		echo "failed on $*"
		exit 1
	fi
}

# create block device node sdb
/bin/rm -f /dev/sdb /dev/sdb1 /dev/sdb2 /dev/nvram2
do_command /bin/mknod /dev/sdb b 8 16
do_command /bin/mknod /dev/sdb1 b 8 17
do_command /bin/mknod /dev/sdb2 b 8 18
/bin/mkdir -p /mnt2

# make file system
for dev in /dev/sdb1 /dev/sdb2; do
	do_command /sbin/mke2fs -t ext4 -g32768 -b4096 -j $dev
	do_command /sbin/tune2fs -c 0 -i 0 $dev
	do_command /bin/mount -t ext4 $dev /mnt2
	do_command /bin/rsync -rlpgoDxt --rsh=/usr/bin/rsh --delete --ignore-errors --timeout=60 /mnt/ /mnt2
	/bin/umount /mnt2
done

# create second partition bootenv device node
do_command /bin/mknod /dev/nvram2 c 10 146

Linux_Load_Addr="0x78000000"
fslHV_Load_Addr="0x2000000"
HVdtb_Load_Addr="0x3000000"
HWdtb_Load_Addr="0x4000000"

# correct first partition bootenv to support dual-partitions
/sbin/bootenv hvcmd "makesinrec 0x79fff000;ext2load usb 0:\$prt $HWdtb_Load_Addr boot/silkworm.dtb;ext2load usb 0:\$prt $fslHV_Load_Addr boot/hv.uImage;ext2load usb 0:\$prt $HVdtb_Load_Addr boot/silkworm_hct_2part.dtb;bootm $fslHV_Load_Addr - $HWdtb_Load_Addr"
/sbin/bootenv hvcmd_fast "makesinrec 0x79fff000;ext2load usb 0:\$prt 1000 /boot/fastloading_2p.mdt ; usb fastload 1000;bootm $fslHV_Load_Addr - $HWdtb_Load_Addr"

# copy the first partition bootenv to the second partition
/bin/cat /dev/nvram0 > /dev/nvram2

# correct the difference
/bin/rm -f /dev/bootenv
/bin/ln -s /dev/nvram2 /dev/bootenv
/sbin/bootenv devinit "usb reset 1"
/sbin/bootenv gosaddr 0x7a000000
/sbin/bootenv -u SoftUpgrade
/bin/rm -f /dev/bootenv
/bin/ln -s /dev/nvram0 /dev/bootenv

/bin/rm -r /mnt2
/bin/rm /dev/nvram2
echo "The second partition is installed"

exit 0
