#!/sbin/sh -
# %W% %G% %U% - %Q%
#ident "%Z%unixvm:%M% %I%"

# Copyright (c) 2000 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
# 
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF VERITAS SOFTWARE.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF VERITAS SOFTWARE.
# 
#               RESTRICTED RIGHTS LEGEND
# USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT IS
# SUBJECT TO RESTRICTIONS AS SET FORTH IN SUBPARAGRAPH
# (C) (1) (ii) OF THE RIGHTS IN TECHNICAL DATA AND
# COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013.
#               VERITAS SOFTWARE
# 1600 PLYMOUTH STREET, MOUNTAIN VIEW, CA 94043


: ${VOLROOT_DIR:=$__VXVM_ROOT_DIR}
. ${VOL_SCRIPTS_LIB:-$VOLROOT_DIR/usr/lib/vxvm/lib}/vxcommon

prog=vxeeprom
tag=vxvm:$prog
export prog tag

usage()
{
	export prog; egettxt >&2 \
'Usage:	$prog [-m] devalias [aliasname [device-alias]]
	$prog devunalias aliasname ...
	$prog boot-device [boot-device]
	$prog version
	$prog supported
	$prog enable' \
	vxvmshm:445
	exit 1
}

# doit - execute a command, printing the command in verbose mode
doit() {
	[ "$verbose" ] && cat <<-% >&4
			! $*
			%
	"$@"
}

tab='	'
nl='
'
bl="[$tab ]"
I_ignore_bad_prom_version=
m_match_multiple=
verbose=
ssa_bootdev=
ide_bootdev=
pci_bootdev=

while getopts :Imv c
do
	case $c in
	I)	I_ignore_bad_prom_version=yes;;
	m)	m_match_multiple=yes;;
	v)	verbose=yes;;
	?)	usage;;
	esac
done
shift `expr $OPTIND - 1`
[ $# -gt 0 ] || usage

cmd=$1
shift

PROM_VERSION=
eval "`vxparms`"

if [ "X$cmd" = Xversion ]
then
	[ $# -eq 0 ] || usage
	if [ -z "$PROM_VERSION" ]
	then
		export tag; egettxt '$tag: Unknown prom version' vxvmshm:60 >&2
	else
		echo $PROM_VERSION
	fi
	exit 0
fi
if [ "X$cmd" = Xsupported ]
then
	[ $# -eq 0 ] || usage
	[ -n "$PROM_VERSION" -a "$PROM_VERSION" -ge 2 ]
	exit $?
fi

[ -n "$PROM_VERSION" -a "$PROM_VERSION" -ge 2 ] || {
	[ -z "$I_ignore_bad_prom_version" ] && {
		v=$PROM_VERSION
		export tag v; egettxt >&2 \
			'$tag: Prom version $v not supported' \
			vxvmshm:59
	}
	exit 3
}

getnvramrc()
{
	eeprom nvramrc 2> /dev/null | sed -e "1s/nvramrc=//p" -e 1d
}
setnvramrc()
{
	doit eeprom nvramrc="`cat`"
}

# check if this is an SSA device path and if so fix it to be a bootable path
ssa_boot_path()
{
	devices_path=$1
	ssa_devices_path=
	
	# if this an SSA interface controller type device 
	if echo $devices_path | grep "\/SUNW,pln@" > /dev/null 2>&1
	then
		# check the path is already correct	
		ssa_addr=`basename $devices_path`
		if echo $ssa_addr | grep "SUNW,ssd@" > /dev/null 2>&1
		then
			return 1
		else # the path is not correct for bootability 
			# incert "SUNW," 
			ssd_addr=`basename $devices_path`
			devices_dir=`dirname $devices_path`
			ssa_devices_path=$devices_dir/"SUNW,"$ssd_addr
		fi
	else  # not an SSA interface controller type device
		return 1
	fi		
			
	ssa_bootdev=$ssa_devices_path
	return 0		
}

# check if this is an ide device path and if so fix it to be a bootable path
ide_boot_path()
{
	devices_path=$1
	ide_devices_path=

	# pci device path looks like:
	#	/devices/pci@1f,0/pci@1,1/ide@3/dad@0,0

	dev_path=`dirname $devices_path`
	ide_path=`basename $dev_path`
	# if this an ide interface controller type device 
	if echo $ide_path | grep "ide@" > /dev/null 2>&1
	then
		# check the path is already correct	
		ide_device=`basename $devices_path`
		if echo $ide_device | grep "disk@" > /dev/null 2>&1
		then
			return 1
		else # the path is not correct for bootability 
			# substitute "dad" of "dad@0,0" with "disk" 
			ide_addr=`echo $ide_device | cut -d@ -f2`
			devices_dir=`dirname $devices_path`
			ide_devices_path=$devices_dir/"disk"@"$ide_addr"
		fi
	else  # not an ide interface controller type device
		return 1
	fi		
			
	ide_bootdev=$ide_devices_path
	return 0		
}

# check if this is an pci device path and if so fix it to be a bootable path
pci_boot_path()
{
	devices_path=$1
	pci_devices_path=

	# pci device path looks like:
	#	/devices/pci@1f,4000/scsi@3/sd@0,0:a

	dev_path=`dirname $devices_path`
	pci_path=`basename $dev_path`
	# if this an pci interface controller type device 
	if echo $pci_path | egrep "^scsi@|^fp@" > /dev/null 2>&1
	then
		# check the path is already correct	
		pci_device=`basename $devices_path`
		if echo $pci_device | grep "disk@" > /dev/null 2>&1
		then
			return 1
		else # the path is not correct for bootability 
			# substitute "sd/ssd" of "sd@0,0" with "disk" 
			pci_addr=`echo $pci_device | cut -d@ -f2`
			devices_dir=`dirname $devices_path`
			pci_devices_path=$devices_dir/"disk"@"$pci_addr"
		fi
	else  # not an pci interface controller type device
		return 1
	fi		
			
	pci_bootdev=$pci_devices_path
	return 0		
}

case $cmd in
getnvramrc)
	getnvramrc;;
setnvramrc)
	setnvramrc;;
devalias)
	case $# in
	0)	getnvramrc | sed -n \
		"s/^$bl*devalias$bl$bl*\([^$tab ]*\)$bl$bl*\(.*\)$bl*\$/\1 \2/p"
		;;
	1)	if [ -n "$m_match_multiple" ]
		then
			getnvramrc |
			sed -n \
		       "s/^$bl*devalias$bl$bl*\($1\)$bl$bl*\(.*\)$bl*\$/\1 \2/p"
		else
			getnvramrc |
			sed -n \
			    "s/^$bl*devalias$bl$bl*$1$bl$bl*\(.*\)$bl*\$/\1/p" |
			tail -1
		fi
		;;
	2)	case $2 in
		/devices/*)
			device="`expr "\$2" : '/devices\(/.*\)'"
			ssa_boot_path $device
			[ $? -eq 0 ] && device=$ssa_bootdev
			ide_boot_path $device
			[ $? -eq 0 ] && device=$ide_bootdev
			pci_boot_path $device
			[ $? -eq 0 ] && device=$pci_bootdev
			;;
		/*)	
			if [ ! -h "$2" ]
			then
				device=$2
			else
				link="`ls -l "\$2" 2> /dev/null`"
				device="`expr "\$link" : \
					'.* -> \.\./\.\./devices\(.*\)'`"
			fi
			ssa_boot_path $device
			[ $? -eq 0 ] && device=$ssa_bootdev
			ide_boot_path $device
			[ $? -eq 0 ] && device=$ide_bootdev
			pci_boot_path $device
			[ $? -eq 0 ] && device=$pci_bootdev
			;;
		*)	
			link="`ls -l "/dev/dsk/\$2" 2> /dev/null`"
			device="`expr "\$link" : \
				'.* -> \.\./\.\./devices\(/.*\)'`"
			ssa_boot_path $device
			[ $? -eq 0 ] && device=$ssa_bootdev
			ide_boot_path $device
			[ $? -eq 0 ] && device=$ide_bootdev
			pci_boot_path $device
			[ $? -eq 0 ] && device=$pci_bootdev
			;;
		esac
		[ -n "$device" ] || {
			spec=$2
			export spec tag; egettxt >&2 \
				'$tag: Invalid device specification: $spec' \
				vxvmshm:58
			exit 2
		}
		getnvramrc |
		(
			sed -e "/^$bl*devalias$bl$bl*$1$bl/d"
			cat <<-!
			devalias $1 $device
			!
			) |
		setnvramrc
		;;
	*)	usage;;
	esac
	;;

devunalias)
	case $# in
	0)	usage;;
	*)	sedcmd="sed"
		for alias in "$@"
		do
			sedcmd="$sedcmd -e '/$bl*devalias$bl$bl*$alias$bl/d'"
		done
		getnvramrc | eval "$sedcmd" | setnvramrc
		;;
	esac
	;;

boot-device)
	case $# in
	0)	eeprom boot-device 2> /dev/null | sed "s/boot-device=$bl*//";;
	1)	device="$1"
		ssa_boot_path $device
		[ $? -eq 0 ] && device=$ssa_bootdev
		ide_boot_path $device
		[ $? -eq 0 ] && device=$ide_bootdev
		pci_boot_path $device
		[ $? -eq 0 ] && device=$pci_bootdev
		eeprom boot-device="$device"
		;;
	*)	usage;;
	esac
	;;

enable)	case `eeprom 'use-nvramrc?'` in
	*=false)	doit eeprom 'use-nvramrc?=true';;
	esac
	;;

*)	usage;;
esac

exit 0
