#!/bin/ksh

# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

typeset -r ku_base_pid=118844-21
PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
PKGCOND=/usr/bin/pkgcond

CMD=backout
is_pcfs_boot=yes

BASEDIR=$ROOTDIR

kbtrans_patch=118860-01
	
rmpkg=SUNWckr
addpkg=SUNWusb
kbtrans=/kernel/misc/amd64/kbtrans
	
CheckZones()
{
        if [ "$ROOTDIR" = "/" -a -x /usr/bin/zonename ]; then
                ZONENAME=`/usr/bin/zonename`
                if [ ${ZONENAME} = "global" ]; then
                        GLOBAL_ZONE=true
                else
                        GLOBAL_ZONE=false
                fi
        else
                # Unable to determine zone
                GLOBAL_ZONE=true
        fi
}

LocalZones () {

  # Only for FCS
  return 0
}

	
last_patch() {

	## returns the number of patches installed at or above this rev.
	## if this is the first rev of the patch
	## 	prepatch postbackout will return 0
	##	postpatch, prebackout will return 1

	root_dir=${ROOTDIR:-/}

	## parse id and rev
	pid=`echo $1 | cut -d\- -f1`
	prev=`echo $1 | cut -d\- -f2`
	patch_cnt=0

	## get all installed refernces to the installed patch base id	
	installed_patches=`patchadd -p -R $root_dir -p | sed -n -e 's/Req.*//' -e 's/[a-zA-Z]*://g' -e 's/,//g' -e "/$pid/p"`
		
	for x in $installed_patches ; do
		base=`echo $x  | cut -d\- -f1`
		rev=`echo $x | cut -d\- -f2`
		if [ $pid -eq $base ] && [ $rev -ge $prev ] ; then 
			## count all installed patches includeing this patch
			patch_cnt=$(($patch_cnt + 1))
		fi
	done
	
	return $patch_cnt
}
	
	
restore_kbtrans() {
	
	## This patch will add kbtrans to SUNWckr.  Manually remove it from SUNWusb
	## if it exists
	##
	
	if [ ! -f $ROOTDIR/$kbtrans ] ; then
		return
	fi

	## last patch should be 1 if this is the last patch installed
	## at $kbtrans_patch or higher	
	last_patch $kbtrans_patch
	if [ "$?" = "0" ] ; then 
		pkg=`pkgchk -R $ROOTDIR -l -p $kbtrans | grep $rmpkg`
		if [ "$?" =  "0" ] ; then
			removef -R $ROOTDIR $rmpkg $ROOTDIR/$kbtrans 1>/dev/null 2>&1
			removef -R $ROOTDIR -f $rmpkg
			installf -R $ROOTDIR $addpkg $ROOTDIR/$kbtrans 1>/dev/null 2>&1
				installf -R $ROOTDIR -f $addpkg
			restore_kbtrans_pkgmap
		fi
	fi
}

##
## restore_kbtrans_pkgmap
##
## Address 6343544 by restoring the zones saved pkgmap entry to SUNWusb which 
## was removed by the prepatch script thanks to workaround for 6312956.

restore_kbtrans_pkgmap () {
mypatchid=${PatchNum}
savedpkgmap=$ROOTDIR/var/sadm/pkg/$addpkg/save/pspool/$addpkg/pkgmap
backoutpkgmap=$ROOTDIR/var/sadm/patch/$mypatchid/$addpkg.pkgmap.$mypatchid
kbtrans_entry=kernel/misc/amd64/kbtrans

	if test -s $backoutpkgmap
	then

    	## We did save something.  However, installf might have been fixed
    	## in the meantime, we check whether we still need the workaround.

    		if grep "$kbtrans_entry" $savedpkgmap >/dev/null; then
        	:
    		else
        		cat $backoutpkgmap >> $savedpkgmap
			rm -f $backoutpkgmap
    		fi
	fi

}

#
# Detect SVM root and return the list of raw devices under the mirror
#
get_rootdev_list()
{
	metadev=`grep -v "^#" $BASEDIR/etc/vfstab | \
		grep "[	 ]/[ 	]" | nawk '{print $2}'`
	if [[ $metadev = /dev/rdsk/* ]]; then
		rootdevlist=`echo "$metadev" | sed -e "s#/dev/rdsk/##"`
	elif [[ $metadev = /dev/md/rdsk/* ]]; then
		metavol=`echo "$metadev" | sed -e "s#/dev/md/rdsk/##"`
		rootdevlist=`metastat -p $metavol |\
		    grep -v "^$metavol[	 ]" | nawk '{print $4}'`
	fi
	for rootdev in $rootdevlist
	do
		echo /dev/rdsk/$rootdev
	done
}

#
# multiboot: install grub on the boot slice
#
install_grub()
{
	# Stage 2 blocks must remain untouched
	STAGE1=$BASEDIR/boot/grub/stage1
	STAGE2=$BASEDIR/boot/grub/stage2

	if [ $is_pcfs_boot = yes ]; then
		#
		# Note: /stubboot/boot/grub/stage2 must stay untouched.
		#
		mkdir -p $BASEDIR/stubboot/boot/grub
		cp $BASEDIR/boot/grub/menu.lst $BASEDIR/stubboot/boot/grub
		bootdev=`grep -v "^#" $BASEDIR/etc/vfstab | grep pcfs | \
			grep "[	 ]/stubboot[ 	]" | nawk '{print $1}'`
		rpcfsdev=`echo "$bootdev" | sed -e "s/dev\/dsk/dev\/rdsk/"`
		if [ X"$rpcfsdev" != X ]; then
			print "Installing grub on $rpcfsdev"
			$BASEDIR/sbin/installgrub $STAGE1 $STAGE2 $rpcfsdev
		fi
	fi

	get_rootdev_list | while read rootdev
	do
		if [ X"$rpcfsdev" != X ]; then
			echo "create GRUB menu in $BASEDIR/stubboot"
			$BASEDIR/sbin/bootadm update-menu -R $BASEDIR/stubboot \
				-o $rootdev,$BASEDIR
		else
			echo "Creating GRUB menu in $BASEDIR"
			$BASEDIR/sbin/bootadm update-menu -R ${BASEDIR:-/} -o $rootdev
		fi
		print "Installing grub on $rootdev"
		$BASEDIR/sbin/installgrub $STAGE1 $STAGE2 $rootdev
	done
}

restore_boot()
{
	if [ -z $rpcfsdev ]; then
		get_rootdev_list | while read rootdev
		do
			print "Restoring pboot on $rootdev"
			dd if=$BASEDIR/usr/platform/i86pc/lib/fs/ufs/pboot \
				of=${rootdev%??}s2 bs=512 count=1 > /dev/null 2>&1
		done
		return
	fi

	# handle pcfs boot partition
	num=1
	fdisk -W - ${rpcfsdev%:boot}  | grep -v '^*' | grep -v '^$' | \
	while read id act bhead bcyl ehead ecyl rsect numsect
	do
		# Ignore entry if not X86 /boot partition
		# ID '190' is the X86BOOT partition (see man fdisk(1M))
		if [ $id -ne "190" ] ; then
			num=$(expr $num + 1)
			continue
		fi

		# Found X86 boot partition - save contents
		BOOTPART=${rpcfsdev%p0:boot}p${num}

		echo "Restoring boot sector on pcfs device <${BOOTPART}>"
		dd if=$BASEDIR/boot/mdboot.patch.$PatchNum of=$BOOTPART bs=512 count=1 > /dev/null 2>&1
	done
}

#
# move pcfs from /stubboot to /boot, transfer contents accordingly
#
pcfs_grub_to_dca()
{
	bootdev=`grep -v "^#" $BASEDIR/etc/vfstab | grep pcfs \
		| grep "[ 	]/stubboot[	 ]" | nawk '{print $1}'`
	if [ X"$bootdev" = "X" ]; then
		is_pcfs_boot=no
		return		# nothing to do
	fi

	# move /stubboot to /boot
	rpcfsdev=`echo "$bootdev" | sed -e "s/dev\/dsk/dev\/rdsk/"`

	df -l | grep /stubboot >/dev/null 2>&1
	if [ $? != 0 ]; then
		mount -F pcfs $bootdev $BASEDIR/stubboot
	fi

	# copy everything from /boot to /stubboot
	( cd $BASEDIR/boot
	find . | cpio -pdum $BASEDIR/stubboot > /dev/null 2>&1
	rm -fr $BASEDIR/boot/* )

	echo "Unmounting $bootdev at $BASEDIR/stubboot"
	ERRMSG=$(umount $bootdev 2>&1)
	if [ $? -ne 0 ] ; then
		[ -n "${ERRMSG}" ] && echo "${ERRMSG}"
	fi

	# adjust /etc/vfstab
	cp $BASEDIR/etc/vfstab $BASEDIR/etc/vfstab-
	sed -e 	"s/[ 	]\/stubboot[ 	]/	\/boot	/" \
	    -e 	'/p0:boot/ {
			s/[	 ]yes[	 ]/	no	/
			}' \
	    <$BASEDIR/etc/vfstab- >$BASEDIR/etc/vfstab

	## shudong - the above sed may need some more entries?

	echo "Mounting $bootdev at $BASEDIR/boot"
	ERRMSG=$(mount -F pcfs $bootdev $BASEDIR/boot 2>&1)
	if [ $? -ne 0 ] ; then
		[ -n "${ERRMSG}" ] && echo "${ERRMSG}"
	fi
}

pcfs_dca_to_grub()
{

	bootdev=`grep -v "^#" $BASEDIR/etc/vfstab | grep pcfs \
		| grep "[ 	]/boot[	 ]" | nawk '{print $1}'`
	if [ X"$bootdev" = X ]; then
		is_pcfs_boot=no
		return		# nothing to do
	fi

	# move /boot to /stubboot
	rpcfsdev=`echo "$bootdev" | sed -e "s/dev\/dsk/dev\/rdsk/"`

	# Remount boot partition as /stubboot, set up new /boot
	mkdir -p $BASEDIR/stubboot
	echo "Unmounting $bootdev at $BASEDIR/boot"
	ERRMSG=$(umount $bootdev 2>&1)
	if [ $? -ne 0 ] ; then
		[ -n "${ERRMSG}" ] && echo "${ERRMSG}"
		echo "Unable to umount $bootdev."
		exit 1
	fi
	ERRMSG=$(mount -F pcfs $bootdev $BASEDIR/stubboot 2>&1)
	if [ $? -ne 0 ] ; then
		[ -n "${ERRMSG}" ] && echo "${ERRMSG}"
		echo "Unable to mount $bootdev on $BASEDIR/stubboot."
		exit 1
	fi

	# copy contents from /stubboot to /boot
	( cd $BASEDIR/stubboot
	find . | cpio -pdum /boot > /dev/null 2>&1 )

	# adjust /etc/vfstab
	cp $BASEDIR/etc/vfstab $BASEDIR/etc/vfstab-
	sed -e "s/[ 	]\/boot[ 	]/	\/stubboot	/"  \
	    -e	'/p0:boot/ {
			s/[	 ]no/	yes/
			}
		' <$BASEDIR/etc/vfstab- >$BASEDIR/etc/vfstab

	# save boot sector in mdboot.patch.$PatchNum
	num=1
	fdisk -W - ${rpcfsdev%:boot}  | grep -v '^*' | grep -v '^$' | \
	while read id act bhead bcyl ehead ecyl rsect numsect
	do
		# Ignore entry if not X86 /boot partition
		# ID '190' is the X86BOOT partition (see man fdisk(1M))
		if [ $id -ne "190" ] ; then
			num=$(expr $num + 1)
			continue
		fi

		# Found X86 boot partition - save contents 
		BOOTPART=${rpcfsdev%p0:boot}p${num}

		echo "Saving boot sector from ${BOOTPART} to mdboot.patch.$PatchNum"
		dd of=$BASEDIR/stubboot/mdboot.patch.$PatchNum if=$BOOTPART bs=512 count=1 > /dev/null 2>&1
	done
}

to_new_boot()
{
	# fix pcfs file system
	pcfs_dca_to_grub

	# install grub boot block and create grub menu
	install_grub

	# create boot archive
	$BASEDIR/boot/solaris/bin/create_ramdisk -R /$BASEDIR
}

to_old_boot()
{

	rm $BASEDIR/platform/i86pc/boot_archive

	# fix pcfs file system
	pcfs_grub_to_dca

	# restore pboot
	restore_boot

	## if this is the last patch installed, then return status will = 1
	# on first reboot, boot-archive is still active
	echo "exit 0" > $BASEDIR/lib/svc/method/boot-archive
	chmod +x $BASEDIR/lib/svc/method/boot-archive
}

newboot() {
	grep -v "^[ 	]*#" $BASEDIR/etc/vfstab | grep "[ 	]/[ 	]*nfs[ 	]" > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
        	# Check for diskless, alternate root case.
        	exit 0
	fi
	case $CMD in
		apply)
			to_new_boot
			;;
		backout)
			last_patch $ku_base_pid
			if [ "$?" = "0" ] ; then
				to_old_boot
			fi
			;;
	esac
}

ExecuteInProperEnvironment () {

   if $PKGCOND is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
       # Execute non-global whole root zone commands.
       return 0
   fi

   if $PKGCOND is_nonglobal_zone > /dev/null 2>&1 ; then
       # Execute non-global zone commands. Should be no action here
       return 0
   fi

   if $PKGCOND is_netinstall_image > /dev/null 2>&1 ; then
       # Execute commands applicable to patching the mini-root.
       restore_kbtrans
       newboot
       return 0
   fi

   if $PKGCOND is_mounted_miniroot > /dev/null 2>&1 ; then
       # Execute commands specific to the mini-root
       restore_kbtrans
       return 0
   fi

   if $PKGCOND is_diskless_client > /dev/null 2>&1 ; then
       # Execute commands specific to diskless client
       restore_kbtrans
       return 0
   fi

   if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
       # Execute commands specific to an alternate root
       restore_kbtrans
       newboot
       return 0
   fi

   if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
       # In a global zone and system is mounted on /.
       # Execute all commands.
       restore_kbtrans
       newboot
       return 0
   fi

   return 1
} 

##
## Fix links for all configurations
##

ZONENAME=global
[ -x /sbin/zonename ] && ZONENAME=`/sbin/zonename`

if [ -x "$PKGCOND" ] ; then
   ExecuteInProperEnvironment && exit 0 || exit 1
else
   CheckZones
   if [ "${GLOBAL_ZONE}" = "true" ]; then
	restore_kbtrans
	newboot
   else
        LocalZones
   fi
fi 
exit 0
