#!/bin/sh
#
# SILO can only boot from ext[23] or ufs filesystem, located on a disk 
# with a sun disklabel. Also, if the old PROM version is used (major 
# version <= 2), the complete partition where kernel is installed must
# fit within 1GB from the beginning of the disk.
#
# -- Jurij Smakov <jurij@wooyd.org>  Thu, 30 Dec 2004 01:40:34 -0500

. /lib/partman/lib/base.sh

check_sparc() {
	arch=$(archdetect 2>/dev/null) || exit 0
	cpu="${arch%%/*}"
	[ "$cpu" = "sparc" ] || exit 0
}
  
prom_major_version() {
	[ -f /proc/cpuinfo ] || exit 0
	prom="$(grep '^prom[[:space:]]*:' /proc/cpuinfo)"
	prom_version="${prom##*: }"
	prom_version="${prom_version##OBP }"
	prom_major="${prom_version%%.*}"
	echo "prom_major=$prom_major"
}

boot_part_params() {
	result=''
	startdir="$(pwd)"
	for dev in $DEVICES/*; do
		[ -d "$dev" ] || continue
		cd $dev
		partitions=''
		open_dialog PARTITIONS
		while { read_line num id size type fs path name; [ "$id" ]; }; do
			[ -f "$id/mountpoint" ] || continue
			mp=$(cat $id/mountpoint)
			[ "$mp" = /boot ] && \
				result="boot_name=$mp boot_fs=$fs boot_last=${id##*-} boot_dev=$dev"
			[ "$mp" = / ] && [ -z "$result" ] && \
				result="boot_name=$mp boot_fs=$fs boot_last=${id##*-} boot_dev=$dev"
		done
		close_dialog
	done
	cd "$startdir"
	echo "$result"
}

silo_limit_check() {
	if ! longint_le "$boot_last" 1073741824 ; then
		db_subst silo-installer/silo_too_far SILO_PART "$boot_name"
		db_subst silo-installer/silo_too_far SILO_OFF "$(longint2human $boot_last)"
		db_input critical silo-installer/silo_too_far || true
		db_go || exit 1
	fi
}

silo_boot_fs_check() {
	case "$boot_fs" in
	    ext2|ext3|ufs)
		;;
	    *)
		db_subst silo-installer/silo_bad_fs SILO_PART "$boot_name"
		db_subst silo-installer/silo_bad_fs SILO_TYPE "$boot_fs"
		db_input critical silo-installer/silo_bad_fs || true
		db_go || exit 1
		;;
	esac
}

silo_label_type_check() {
	if [ "$boot_dev" ]; then
		cd "$boot_dev"
		open_dialog GET_LABEL_TYPE
		read_line label_type
		close_dialog
		# parted-server tells us that label_type is loop when opening a raid
		# device. We don't fail for that now.
		if [ "$label_type" != "sun" ] && [ "$label_type" != "loop" ]; then
			db_subst silo-installer/silo_bad_disk SILO_PART "$boot_name"
			db_subst silo-installer/silo_bad_disk SILO_DISK "$label_type"
			db_input critical silo-installer/silo_bad_disk || true
			db_go || exit 1
		fi
	fi
}

check_sparc
eval "$(boot_part_params)"
silo_label_type_check
silo_boot_fs_check
eval "$(prom_major_version)"
[ -n "$prom_major" ] || exit 0
[ "$prom_major" -le 2 ] || exit 0
silo_limit_check
