#!/sbin/sh
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#pragma ident	"@(#)install-discovery.sh	1.13	06/08/21 SMI"
#

#
# Initial setup of install miniroot, including:
# - Mounting of base filesystems
# - Configuring devices
# - Discovery of install parameters
#
# When this script is complete, the system is in a single-user state
# with its networking interfaces configured and operating.

PATH=/sbin:${PATH}
export PATH

PLATFORM=`/sbin/uname -p`
export PLATFORM

I386="i386"
SPARC="sparc"

ROOT=/tmp/root
LOGS=${ROOT}/var/sadm/system/logs

NETBOOTINFO=/usr/lib/inet/wanboot/netbootinfo

CDROOT=/tmp/.cdroot

# Do not allow the NFS daemons to start yet
NO_AUTOSTART_NFS_DAEMONS=; export NO_AUTOSTART_NFS_DAEMONS

Network="no"
FJS_BOOT="no"
DoDrvconfig="yes"
SINGLE_USER=0
USING_DHCP=0

#
# Functions
#

#
# use shell to snarf the base device name so we can make slice 0's name
# now we (only) do OBP style names
# INPUT: $1 - the rootfs device name
# OUTPUT: spits out on stdout the short device name of the cdrom device
#
basedevof() {
    D=$1    # Device
    T=$2    # Type

    # if we had expr on root, this is what we would simply do
    # U=`expr $D : '\(.*\):.*'
    # echo "${U}:a"

    # but we don't have expr and would like to live without it
    # so we use set to separate at all ":"
    IFS=":"
    set -- ${D}
    # get everything up till last ":" into U
    U=$1
    shift ;
    while [ $# -gt 1 ] ; do
        U=${U}:$1
        shift ;
    done
    # now we got everything except the stuff after the last ":",
    # which we assume was just a partition letter

    # x86 uses the p0 device for hsfs.  sparc uses s0 device.
    # These map to :q and :a respectively under /devices/...

    if [ "${PLATFORM}" = "${SPARC}" ]; then
        echo "${U}:a"
    else
        if [ "${T}" = "ufs" ]; then
            echo "${U}:b"
        else
            echo "${U}:q"
        fi
    fi
}

#
# Find and mount the CD image using bootinfo
#
manual_find_and_mount_cdrom() {
	# XXX lots more here: host-ip, router, hostname; subnet done below
	echo `${NETBOOTINFO} host-ip`

	ifname=${_INIT_NET_IF}

	# get and set the ip address
	ipaddress=`${NETBOOTINFO} host-ip 2>/dev/null`
	if [ -n "$ipaddress" ] ; then
		/sbin/ifconfig ${ifname} ${ipaddress} >/dev/null 2>&1
	fi

	# get and set the hostname
	hostname=`${NETBOOTINFO} hostname 2>/dev/null`
	if [ -n "$hostname" ] ; then
		/usr/bin/hostname ${hostname} >/dev/null 2>&1
	fi

	# get and set the server's netmask
	netmask=`${NETBOOTINFO} subnet-mask 2>/dev/null`
	if [ -n "$netmask" ]; then
		/sbin/ifconfig -a netmask ${netmask}  >/dev/null 2>&1
	fi
	/sbin/ifconfig ${ifname} up

	# get and set the default route
	default_route=`${NETBOOTINFO} router-ip 2>/dev/null`
	if [ -n "$default_route" ] ; then
		/usr/sbin/route add default $default_route >/dev/null 2>&1
	fi

	echo "${Rootfs} - / ${Roottype} - no ro" >> /etc/vfstab

	if [ -n "${Installfs}" ] ; then
	    echo "Manual configuration: using local media"
	    /sbin/mount -F ${Installtype} -o ro ${Installfs} \
		/cdrom >/dev/null 2>&1
	    if [ $? -ne 0 ]; then
		echo	"ERROR: ${Installfs} of type ${Installtype} does" \
		    	"not exist, unable to mount /cdrom"
		exec /sbin/sh
	    else
		# mount success.  Make vfstab entry.
		echo "${Installfs} - /cdrom ${Installtype} - no ro" >> \
			/etc/vfstab

		# make file for solaris install wizard(s)
		echo $Installfs > /tmp/.netmnt
	    fi

	    # Loopback mount the full version of /usr on the cdrom
	    # 
	    if [ -d /cdrom/Solaris_*/Tools/Boot/usr ]; then
		    /sbin/mount -F lofs /cdrom/Solaris_*/Tools/Boot/usr /usr
		    mount_return_code=$?
		    if [ $mount_return_code -ne 0 ]; then
			    echo	"Unable to mount" \
					"/cdrom/Solaris_*/Tools/Boot/usr"
			    exec /sbin/sh
		    fi
	    fi
	fi
}

#
# Find and mount the CD image using DHCP
#
dhcp_find_and_mount_cdrom() {
	/sbin/dhcpagent -a

	#
	# Look for the root boot parameters;
	# For dhcpinfo:	SrootNM  returns the root file system server name
	#		SrootPTH returns the root file system path
	# for non-NFS DHCP boots, force the use of the local media
	#
	if [ "X${Roottype}" = "Xnfs" ]; then
	    rs=`/sbin/dhcpinfo SrootNM`
	    rp=`/sbin/dhcpinfo SrootPTH`

	    # dhcpinfo returns success even if nothing is returned, so we
	    # must rely on the emptiness of stdout to determine if we have
	    # a valid root filesystem mounted
	    if [ -n "$rs" -a -n "$rp" ] ; then
		Rootfs=$rs:$rp
		echo "${Rootfs} - / ${Roottype} - no ro" >> /etc/vfstab
	    else
		    echo "ERROR: dhcpagent unable to access network " \
			 "information"
		    exec /sbin/sh
	    fi
	else
	    #
	    # We are not booting off of an NFS root filesystem.
	    # use pre-set Rootfs and Roottype.
	    #
	    echo "${Rootfs} - / ${Roottype} - no ro" >> /etc/vfstab
	fi

	#
	# Look for the install boot parameter;
	# For dhcpinfo:	SinstNM  returns the install file system server name
	#		SinstPTH returns the install file system path
	#
	is=`/sbin/dhcpinfo SinstNM`
	ip=`/sbin/dhcpinfo SinstPTH`

	# dhcpinfo returns success even if nothing is returned, so we
	# must rely on the emptiness of stdout to determine if we have
	# a valid distribution to mount
	if [ -n "$is" -a -n "$ip" ] ; then
	    Installfs=$is:$ip
	else
		# we are going to use local media, if present
		echo "DHCP: using local media"
		Installfs=""
	fi

	# Get the server IP address so we can complain about it by
	# name if need be
	DHCPServerAddr=`/sbin/dhcpinfo ServerID`

	# update the hosts file with the proper name to address
	# translation for our install server, if we are net DHCP booting
	if [ -n "$is" ]; then
	    echo "`/sbin/dhcpinfo SinstIP4`	$is" >> /etc/inet/hosts
	fi

	if [ -n "${Installfs}" ] ; then
	    /sbin/mount -F ${Installtype} -o ro ${Installfs} /cdrom \
		>/dev/null 2>&1
	    if [ $? -ne 0 ]; then
		if [ "${Network}" = "yes" ]; then
		    # Bad information was returned by this DHCP server
		    # display what information is available and 
		    # exit to allow the user to correct the situation
		    #
		    echo "received from DHCP server at $DHCPServerAddr"
		    echo "install info:  server $is, path $ip"
		    echo "root info:  server $rs, path $rp"
		    echo "ERROR:  Unable to NFS mount ${Installfs}"
		    echo "             Exiting to shell."
		    exec /sbin/sh
		else
		    # not networked, and mount failed.
		    echo "ERROR: ${Installfs} of type  ${Installtype}"
			 "does not exist, unable to mount /cdrom"
		    exec /sbin/sh
		fi
	    else
		# mount success.  Make vfstab entry.
		echo "${Installfs} - /cdrom ${Installtype} - no ro" >> \
			/etc/vfstab

		# make file for solaris install wizard(s)
		echo $Installfs > /tmp/.netmnt
	    fi
	fi

	# Loopback mount the full version of /usr on the cdrom
	# 
	if [ -d /cdrom/Solaris_*/Tools/Boot/usr -a \
		"${Roottype}" != "nfs" ]; then
		/sbin/mount -F lofs /cdrom/Solaris_*/Tools/Boot/usr /usr
		mount_return_code=$?
		if [ $mount_return_code -ne 0 ]; then
			echo "Unable to mount /cdrom/Solaris_*/Tools/Boot/usr"
			exec /sbin/sh
		fi
	fi
}

#
# try to find a corresponding install cd in a local drive
#
trymountcd()
{
	echo $Installfs | grep ramdisk > /dev/null
	if [ $? = 0 ] && [ ! -d /cdrom/Solaris_* ] ; then
		for Dev in /dev/dsk/*p0 ; do
			Typ=`/usr/sbin/fstyp $Dev 2> /dev/null`

			if [ "X${Typ}" = "Xhsfs" ] ; then
				mount -o ro -F hsfs $Dev /cdrom
				if [ -d /cdrom/Solaris_* ] ; then
					echo "Using install cd in $Dev"

					# save install cd for wizards
					echo  $Dev >> ${CDROOT}

					break
				else
					umount /cdrom 2> /dev/null
				fi
			fi
		done
	else
		if [ ! -d /cdrom/Solaris_* ] ; then
			# "this never happens" :-)
			echo "ERROR: The Solaris Distribution, ${Installfs} " \
			    "does not exist"
			echo "             Exiting to shell."
			/sbin/sh
		fi
	fi
}

#
# Try to mount the product area over nfs based on the install_media
# property in the form of <ip_addr>:<product_path>
#
trymountnet()
{
	nfspath=$1

	# set up local loopback
	/sbin/ifconfig lo0 plumb
	/sbin/ifconfig lo0 127.0.0.1 up

	/sbin/ifconfig -a plumb > /dev/null 2>&1

	# figure out netstrategy and dhcp if necesary
	strat=`/sbin/netstrategy | cut -f 3 -d " "`

	if [ _$strat = _dhcp ]  ; then
		IF=`/sbin/netstrategy | cut -f 2 -d " "`

		if [ -z "$IF" ] ; then
			pkill dial

			printf "No driver (or driver binding) was found for "
			printf "the interface used to boot. \nA driver "
			printf "(or driver binding) may be available "
			printf "in an update or patch from \nSun, "
			printf "or from the interface vendor.\n"
			printf "\nExiting to shell.\n\n"

			exec /bin/sh
		else
			/sbin/dhcpagent -a
		fi

		if [ $? = 0 ] ; then
			configured=yes
		fi
	else
		# configure interface based on properties
		#
		bootmac=`prtconf -v /devices | sed -n '/boot-mac/{;n;p;}' \
                    | cut -f 2 -d \'`
		hostip=`prtconf -v /devices | sed -n '/host-ip/{;n;p;}' \
                    | cut -f 2 -d \'`
		netmask=`prtconf -v /devices | sed -n '/subnet-mask/{;n;p;}' \
                    | cut -f 2 -d \'`
		if [ "X$netmask" = "X" ]; then
			netmask=255.255.255.0
		fi

		if [ X$bootmac != X -a X$hostip != X -a X$netmask != X ]; then
			echo "bootmac: $bootmac, hostip: $hostip, netmask: $netmask"
			old_ifs=$IFS
			IFS=":"
			set -- $net_device_list
			IFS=$old_ifs
			for i
			do
				ethers=`/sbin/ifconfig $i | grep ether | cut -d' ' -f2`
				if [ "X$ethers" != "X$bootmac" ]; then
					continue
				fi
				/sbin/ifconfig $i inet $hostip netmask $netmask
				ipaddr=`/sbin/ifconfig $i | awk '/inet/{print $2;}'`
				if [ "X$ipaddr" != "X0.0.0.0" ] ; then
					# The interface configured itself correctly
					echo "Configured interface $i"
					/sbin/ifconfig $i up
					configured=yes
				fi
				break;
			done
		fi
	fi

	if [ "X$configured" != "Xyes" ]; then
		echo "ERROR: Unable to configure network interface"
		echo "             Exiting to shell."
		pkill dial
		/sbin/sh
	fi

	mount -o ro $nfspath /cdrom
	echo $nfspath > /tmp/.netmnt
}

#
# Locate the install media based on the install_media property
# specified on the GRUB menu. It's either /cdrom or <ip>:<path>.
# Details are handled in trymountcd() and trymountnet(), respectively.
#
find_media()
{
	prtconf -v /devices | grep install_media > /dev/null
	if [ $? = 0 ] ; then
		MEDIA=`prtconf -v /devices | sed -n '/install_media/{;n;p;}' \
		    | cut -f 2 -d \'`
		if [ "${MEDIA}" = "cdrom" ] ; then
			trymountcd
		else
			trymountnet $MEDIA
		fi
	else
		echo "ERROR: no source install media defined"
		trymountcd
	fi

	# mount /boot if -t was specified in add_install_client
	#
	prtconf -v /devices | grep install_boot > /dev/null
	if [ $? = 0 ] ; then
		bootpath=`prtconf -v /devices | sed -n '/install_boot/{;n;p;}' \
		    | cut -f 2 -d \'`
		mount -o ro $bootpath /cdrom/boot
	fi
}

#
# Find and mount the CD image using bootparams
#
bootparams_find_and_mount_cdrom() {
	#
	# net_device_list contains a ":" delimited list
	# of network devices
	# do an auto-revarp on each of them with the
	# exception of the loopback device
	#
	old_ifs=$IFS
	IFS=":"
	set -- $net_device_list
	IFS=$old_ifs
	for i
	do
		#
		# skip the auto-revarp for the loopback device
		#
		if [ "$i" = "lo0" ]; then
			continue
		fi
		echo "Attempting to configure interface $i..."
		/sbin/ifconfig $i auto-revarp >/tmp/dev.$$ 2>&1
		ipaddr=`/sbin/ifconfig $i |grep inet |awk '{print $2;}'`
		if [ "X$ipaddr" != "X0.0.0.0" ] ; then
			# The interface configured itself correctly
			echo "Configured interface $i"
			/sbin/ifconfig $i up
		else
			echo "Skipped interface $i"
		fi
	done
	/sbin/hostconfig -p bootparams 2> /dev/null

	#
	# if not booting from the net add root entry to 
	# /etc/vfstab 
	#
	if [ "${Network}" != "yes" ]; then
		echo "${Rootfs} - / ${Roottype} - no ro" >> /etc/vfstab
	else
		#
		# Look for the root boot parameter;
		# bpgetfile returns $1 = server name, $2 = server IP addr
		# $3 = path
		#
		set -- ""
		set -- `/sbin/bpgetfile`
		SERVER_IPADDR=$2
		if [ $2"x" != "x" ]; then
			Rootfs=$1:$3
			echo "${Rootfs} - / ${Roottype} - no ro" >> /etc/vfstab
		else
			echo "ERROR: bpgetfile unable to access network"
			    "information"
			exec /sbin/sh
		fi

		# get the server's netmask
		if [ -x /sbin/get_netmask ]; then
			netmask=`/sbin/get_netmask $SERVER_IPADDR 2>/dev/null`
			if [ -n "$netmask" ]; then
				/sbin/ifconfig -a netmask 0x${netmask} \
				    >/dev/null 2>&1
			fi
		fi

		#
		# Look for the install boot parameter;
		# bpgetfile returns $1 = server name, $2 server IP addr
		# $3 = path
		#
		set -- ""
		set -- `/sbin/bpgetfile install`
		if [ $2"x" != "x" ]; then
			Installfs=$2:$3
		else
			echo "ERROR: bpgetfile unable to access network" \
			    "install information"
			exec /sbin/sh
		fi
	fi

	if [ -n "${Installfs}" ] ; then
	    /sbin/mount -F ${Installtype} -o ro ${Installfs} /cdrom \
		>/dev/null 2>&1
	    mnt_ret=$?
	    if [ $mnt_ret -ne 0 ]; then
		    # if the .netmnt file exists at this point, then we
		    # must have already booted once, and are booting again
		    # (i.e. Solaris Install CD).  In this case, we simply
		    # ignore the error and return, because the rest of
		    # this function tries to mount the Installfs, which is
		    # not needed (and will fail) when booting a Solaris
		    # Install CD "disk0" boot image from a cd0-based net
		    # boot.
		    if [ -f /tmp/.netmnt ] ; then
			return 0
		    fi

		    if [ "${Installtype}" = "hsfs" ]; then
			    Installfs=`basedevof ${Rootfs} ufs`
			    if [ -b ${Installfs} ] ; then
				    /sbin/mount -F ufs -o ro ${Installfs} \
					    /cdrom > /dev/null 2>&1
				    mnt_ret=$?
			    else
				    find_media
			    fi
		    else
			    # there is a bad entry in the bootparams map
			    # display what information is available and 
			    # exit to allow the user to correct the situation
			    #
			    echo "install entry: "`/sbin/bpgetfile install`
			    echo "root entry: "`/sbin/bpgetfile`
			    echo "ERROR:  Unable to NFS mount ${Installfs}"
			    echo "             Exiting to shell."
			    exec /sbin/sh
		    fi
	    fi

	    if [ $mnt_ret -eq 0 ] ; then
		    echo "${Installfs} - /cdrom ${Installtype} - no ro" >> \
			/etc/vfstab
		    #
		    # Loopback mount the full version of /usr on the cdrom
		    # 
		    if [ -d /cdrom/Solaris_*/Tools/Boot/usr -a \
			    "${Roottype}" != "nfs" ]; then
			    /sbin/mount -F lofs \
				/cdrom/Solaris_*/Tools/Boot/usr /usr
			    mount_return_code=$?
			    if [ $mount_return_code -ne 0 ]; then
				    echo "Unable to mount" \
				    "/cdrom/Solaris_*/Tools/Boot/usr"
				    exec /sbin/sh
			    fi
		    fi
		    
		    # By touching this file, we avoid failure during a possible
		    # disk0-based reboot (think Solaris Install CD) when
		    # mounting a possibly non-existent hsfs Solaris Install
		    # image.  See above comment dealing with .netmnt for
		    # more info.
		    echo $Installfs > /tmp/.netmnt
	    fi
	fi
}

#
# on x86, if this is being booted as a recovery image, go directly to a shell
#
check_recovery()
{
	prtconf -v /devices | grep install_media > /dev/null
	if [ $? != 0 ] ; then
		while [ true ] ; do
			/sbin/install-recovery
		done
	fi
}

#
# menu for jumpstart, itu and text install on x86
#
itu_jumpstart_menu()
{
	printf "\n\n"
	printf "    1.     Solaris Interactive (default)\n"
	printf "    2.     Custom JumpStart\n"
	printf "    3.     Solaris Interactive Text (Desktop session)\n"
	printf "    4.     Solaris Interactive Text (Console session)\n"
	printf "    5.     Apply driver updates\n"
	printf "    6.     Single user shell\n\n"
	printf "Enter the number of your choice.\n"

	# selection <timeout> <number of items>
	/sbin/selection 30 6
	x=$?

	case $x in
		1) printf "\nSolaris Interactive\n\n" ;;
		2) printf "\nCustom Jumpstart\n"
			INSTALL_BOOT="yes"
			cat < /dev/null > /tmp/.install_boot
			;;
		3) printf "\nSolaris Interactive Text (Desktop session)\n\n"
			cat < /dev/null > /tmp/.text_install
			;;
		4) printf "\nSolaris Interactive Text (Console session)\n\n"
			cat < /dev/null > /tmp/.nowin
			;;
		5) printf "\nApply driver updates\n\n" 
			/sbin/install-du
			;;
		6) printf "\nSingle user shell\n\n"
			/sbin/install-recovery
			;;
		*) printf "\nSolaris Interactive\n\n" ;;
	esac

}

# common (to all platforms) keyboard layout
#
init_keyboard()
{
	# Systems with no hardware keyboard ID will provide an eeprom value.
	#
	if test -x /usr/lib/set_keyboard_layout
	then
		/usr/lib/set_keyboard_layout
	fi

	# Load the keymap for the attached keyboard.
	#
	/usr/bin/loadkeys

	# Initialize the keyboard defaults
	#
	[ -h /dev/kbd -a -x /usr/bin/kbd ] && /usr/bin/kbd -i >/dev/null 2>&1
}

###################################################
###################################################
#           Main
#

#
# Mount tmpfs
#
if [ ! -f /tmp/.rcSmnt ]; then
	# mount tmpfs, for now it can't swap so we can't fill it too full
	/sbin/mount -F tmpfs swap /tmp
	if [ $? -ne 0 ]; then
		echo "tmpfs mount failed."
		/sbin/sh
	fi

	cat < /dev/null > /tmp/.rcSmnt
fi

#
# Check for no reboot flag
#
if [ "${RB_NOBOOTRC}" = "YES" ]; then
	RB_NOBOOTRC="no"
	export RB_NOBOOTRC
	/sbin/sh
fi

if [ -f /tmp/.rcSrun ]; then
	exit 0
fi

#
# Start the twirling dial
#
if [ -x /sbin/dial ]; then
	/sbin/dial &
	dial_pid=$!
fi

#    
# Unpack writeable initialized files into /tmp.
# NOTE: send output to /tmp (not /dev/null) to avoid nfs bug on remote
#       ro mounted fs
( cd /.tmp_proto; find . -print -depth | cpio -pdm /tmp 2>/tmp/cpio.out )

mkdir -p /tmp/root/var/sadm/system/logs

MEMSIZE=`/sbin/mem`
echo "Memory free after tmpfs initialization: ${MEMSIZE}" >> \
	${LOGS}/sysidtool.log

########
# Configured "/" writeable files may not be updated.
#
# create /etc/vfstab (/tmp/root/etc/vfstab)
echo "swap - /tmp tmpfs - no -" >> /etc/vfstab

#
# add the procfs mount line
#
echo "/proc - /proc proc - no -" >> /etc/vfstab

# configure devfs in /tmp/dev
#
find dev -depth -print | cpio -pdum /tmp >/dev/null 2>&1
ln -sf /devices /tmp/devices 
/sbin/mount -F lofs -O /tmp/dev /dev

_INIT_RECONFIG=set; export _INIT_RECONFIG
mkdir -p /tmp/etc
mkdir -p /tmp/etc/sysevent
/usr/lib/sysevent/syseventd -r /tmp

# devfsadm needs to be run to cause everthing to be linked. This
# causes devfsadmd to be started
/usr/lib/devfsadm/devfsadmd -r /tmp -p /tmp/root/etc/path_to_inst

# mount the wanbootfs filesystem (needed for https later on)
BOOTFS_DISK="/devices/ramdisk-bootfs:a"

if [ -b "$BOOTFS_DISK" ] ; then
    mount -r -F hsfs "$BOOTFS_DISK" /etc/netboot > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
	mount -r -F ufs "$BOOTFS_DISK" /etc/netboot > /dev/null 2>&1
	if [ $? -ne 0 ] ; then
	    echo "Cannot mount wanbootfs filesystem from $BOOTFS_DISK"
	    exec /bin/sh
	fi
    fi
fi

#
# read bootargs for the boot device
# if fails - ignore and assume regular install
# if ok - see if preinstall bootargs set
#	and if possible, copy the /devices from the stub

# read prom for default boot dev
# looking for "FD=..."
#
# Also, remember if an install option is specified. If so, skip
# the itu_jumpstart_menu.
#
install_menu=none

set -- ""
set -- `/sbin/getbootargs 2>/dev/null`
if [ $# -gt 0 ] ; then
	while [ $# -gt 0 ] ; do
		case $1 in
		#
		# Public flags
		#
		FD=*)
			
			# at end of script, save root dev in /tmp/.preinstall
			# this is an unambiguous indication of stub boot
			FJS_BOOT="yes"
			From=`(IFS="="; set -- $1; echo "$2 $3 $4 $5" )`
			break
			;;
		install)
			install_menu=install
			INSTALL_BOOT="yes"
			cat < /dev/null > /tmp/.install_boot
			shift
			;;
		text)
			install_menu=text
			cat < /dev/null > /tmp/.text_install
			shift
			;;
		dhcp)
			TRY_DHCP="yes"
			shift
			;;

		tape*|*:/*)
			echo "cj_location $1" >> /tmp/.cjfiles_method
			# providing a URL implies you want to do an install
			# boot, so do it.
			INSTALL_BOOT="yes"
			cat < /dev/null > /tmp/.install_boot
			shift
			;;
		ask)
			cat < /dev/null > /tmp/.cjfiles_ask
			shift
			;;
		nowin|w)
			install_menu=nowin
			cat < /dev/null > /tmp/.nowin
			shift
			;;
		#
		# Private flags
		#
		install_debug*)
			echo "$1" > /tmp/.install_debug
			shift
			;;
		cd)
			cat < /dev/null > /tmp/.netcdboot
			shift
			;;
		*)
			shift
			;;
		esac
	done
fi


#
# figure out root file system information (ie local or remote)
#
eval `/sbin/get_root -t Roottype -b Rootfs /`

echo "fd - /dev/fd fd - no -" >> /etc/vfstab
/sbin/mount -F fd /dev/fd

##########
# load keyboard mappings on x86
#
# Included from init.d/keymap

if [ ${PLATFORM} = ${I386} ]; then
	kbdtype=`prtconf -v /devices | sed -n '/kbd-type/{;n;p;}' \
                    | cut -f 2 -d \'`
	grep -v "setprop kbd-type" /boot/solaris/bootenv.rc > /tmp/bootenv$$
	echo "setprop kbd-type '$kbdtype'" >> /tmp/bootenv$$
	cat /tmp/bootenv$$ > /boot/solaris/bootenv.rc
	rm /tmp/bootenv$$

	init_keyboard

	# x86 interface for unbundled device drivers
	#
	pkill dial
	echo "${Rootfs} - / ${Roottype} - no ro" >> /etc/vfstab
	case $install_menu in
	install)
		printf "Custom JumpStart\n"
		;;
	text)
		printf "Solaris Interactive Text (Desktop session)\n"
		;;
	nowin)
		printf "Solaris Interactive Text (Console session)\n"
		;;
	none|*)
		check_recovery
		itu_jumpstart_menu
		;;
	esac

	#
	# We are done with devices, create disk to BIOS disk mapping.
	# This will be consumed late by bootadm in grub menu creation.
	# We do this early because Install/Upgrade will likely break
	# disk content comparison.
	#
	/boot/solaris/bin/create_diskmap

	# restart the twirling dial
	#
	if [ -x /sbin/dial ]; then
		/sbin/dial &
		dial_pid=$!
	fi
fi

#
# Configure network interfaces:
#	- software loopback interface
#	- hardware interfaces
# Complete the network configuration
#
/sbin/ifconfig lo0 plumb
/sbin/ifconfig lo0 127.0.0.1 up

#
# Configure all network interfaces
#
/sbin/ifconfig -a plumb > /dev/null 2>&1

#
# Get the complete list of network devices
# so that we can ifconfig them individually
#
for i in `ifconfig -a | awk '/^[a-z0-9]*:/{print $1}'`; do
	net_device_list="${i}${net_device_list}"
done

case ${Roottype} in
	ufs|hsfs)            # we are on a ufs or hsfs (local machine)
		# get the name of the device for the Solaris distribution
		Installfs=`basedevof ${Rootfs} ""`
		if [ "X${Installfs}" = "X${Rootfs}" ] ; then
		    # the "base" device of the device the system is
		    # booted from is the same as the root device -
		    # so it can't possibly be an install image
		    Installfs=""
		elif [ -b ${Installfs} ] ; then
			echo  $Installfs >> ${CDROOT}
			break
		else
			find_media
		fi

		# the slice 0 is type hsfs, if there is a slice 0.
		Installtype=hsfs
		;;
	nfs*)            # we are over the network (nfs, nfs2, nfs3)
		Roottype=nfs
		Network="yes"
		# set Installfs=... from config file from server
		Installtype=nfs
		;;
	*)              # fatal error - unknown "/" filesystem
		# we cannot use it as-is - note all the fs specific changes
		/sbin/dial $dial_pid
		while : ; do
			echo "FATAL ERROR: "/" file system type "\
			    "\"${Roottype}\" is unknown"
			echo "             Exiting to shell."
			/sbin/sh
		done
		;;
esac

# Export net boot configuration strategy. _INIT_NET_IF is set to the
# interface name of the netbooted interface if this is a net boot.
# _INIT_NET_STRATEGY is set to the network configuration strategy.
# First use netbootinfo and if that fails use the legacy netstrategy
_INIT_NET_STRATEGY=`${NETBOOTINFO} net-config-strategy 2>/dev/null`
if [ $? -eq 0 -a ${_INIT_NET_STRATEGY}X != "noneX" ]; then
	Roottype=`${NETBOOTINFO} rootfs-type`
	if [ $? -ne 0 ]; then
		Roottype=""
	fi

	_INIT_NET_IF=`${NETBOOTINFO} interface-name`
	if [ $? -ne 0 ]; then
		_INIT_NET_IF=""
	fi

	if [ "X${_INIT_NET_IF}" != "X" ] ; then
	    # we must have net booted
	    Network=yes
	fi
else
	_INIT_NET_STRATEGY=""
        set -- `/sbin/netstrategy`
        if [ $? -eq 0 ]; then
                if [ "$1" = "nfs" -o "$1" = "cachefs" ]; then
                        _INIT_NET_IF="$2"
                fi
                _INIT_NET_STRATEGY="$3"
                export _INIT_NET_IF _INIT_NET_STRATEGY 
        fi
fi

if [ "X${_INIT_NET_STRATEGY}" = "Xdhcp" ] ; then
	USING_DHCP=1
fi
export _INIT_NET_IF _INIT_NET_STRATEGY

if [ $USING_DHCP -ne 1 -a "X${TRY_DHCP}" = "Xyes" ] ; then
	# Try manually turning DHCP on
	for i in `ifconfig -a |grep "^[a-z0-9]*:" |sed -e "s/: .*$//g"` ; do
		if [ $i = "lo0" ] ; then
			continue
		fi

		echo "Trying DHCP on $i"
		/sbin/ifconfig $i dhcp primary start wait 120
		if [ $? -eq 0 ] ; then
			echo Success
			USING_DHCP=1
			break
		else
			# No luck, give up on this interface
			/sbin/ifconfig $i dhcp drop
		fi
	done
fi

if [ "X${_INIT_NET_STRATEGY}" = "Xmanual" ] ; then
	echo "Network interface was configured manually."
	manual_find_and_mount_cdrom
elif [ $USING_DHCP -eq 1 ] ; then
	if [ "${PLATFORM}" = "${SPARC}" ]; then
		echo "Using DHCP for network configuration information."
		dhcp_find_and_mount_cdrom
	fi
else
	echo "Using RPC Bootparams for network configuration information."
	bootparams_find_and_mount_cdrom
fi

if [ -f /etc/netboot/etc/inet/hosts ] ; then
	cat etc/netboot/etc/inet/hosts >> /etc/inet/hosts
fi

# Since sparc still uses the dvd stub miniroot which doesn't contain
# loadkeys, this can't be run when x86 runs it because /usr would not
# have been mounted yet.
#
if [ "${PLATFORM}" = "${SPARC}" ]; then
	init_keyboard
fi

# save the name of the root device in /tmp/.preinstall
if [ "${FJS_BOOT}" = "yes" -a "${From}" ]; then
	FromDisk=`echo ${From} | ( read d n junk ; echo $d )`
	if [ -b /devices/${FromDisk} ]; then
		lsline=`ls -l /dev/dsk | grep ${FromDisk}`
		if [ "${lsline}" ]; then
			shortname=`( set -- ${lsline} ;
				while [ $# -gt 3 ]; do
					shift;
				done
			if [ "$2" = "->" ]; then echo "$1";
			    else echo "" ; fi )`
			if [ -b /dev/dsk/${shortname} ]; then
				echo /dev/dsk/${shortname} > \
				/tmp/.preinstall
			fi
		fi
	fi
fi

##########
# x86 config cleanup logic
if [ -x /usr/sbin/install.d/atconfig ]; then
	/usr/sbin/install.d/atconfig
fi

###########################################################################
# now we exit rcS, and the rc files bring the system further
# on up (ie networking comes up...)
###########################################################################

pkill dial

cat < /dev/null > /tmp/.rcSrun

unset NO_AUTOSTART_NFS_DAEMONS

exit 0
