Fix ofpath SATA support and sysfs detection
[yaboot.git] / ybin / ofpath
index 25dd1ce3df1b34ab0d9e8650cc06c23eb75edba1..4c4ceaa20b537e3b707b659485e238af3447d828 100755 (executable)
@@ -3,7 +3,7 @@
 ###############################################################################
 ##
 ## ofpath: determine OpenFirmware path from unix device node
-## Copyright (C) 2000, 2001, 2002 Ethan Benson
+## Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
 ##
 ## Portions based on show_of_path.sh:
 ##
@@ -27,7 +27,7 @@
 
 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
 PRG="${0##*/}"
-VERSION=1.0.3-DEVEL_DO_NOT_DISTRIBUTE-1
+VERSION=1.0.6-pre2
 DEBUG=0
 export LC_COLLATE=C
 
@@ -39,7 +39,7 @@ echo \
 Written by Ethan Benson
 Portions based on show_of_path.sh written by Olaf Hering
 
-Copyright (C) 2000, 2001, 2002 Ethan Benson
+Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
 Portions Copyright (C) 2000 Olaf Hering
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
@@ -149,7 +149,7 @@ if (command -v readlink > /dev/null 2>&1) ; then
 else
     readlink()
     {
-        local SYMTARGET="$(v=`ls -l "$2" 2>/dev/null` ; echo ${v##*> })"
+        local SYMTARGET="$(v=`ls -l "$1" 2>/dev/null` ; echo ${v##*> })"
         if [ -n "$SYMTARGET" ] ; then
             echo "$SYMTARGET"
             return 0
@@ -187,6 +187,17 @@ scsiinfo()
 {
     ## see if system has scsi at all
     if [ ! -f /proc/scsi/scsi ] ; then
+       local kver="$(uname -r)"
+       case "$kver" in
+           2.5.*|2.6.*)
+               if [ -d /sys/bus/scsi/devices -a \
+                   -n "$(ls /sys/bus/scsi/devices 2>/dev/null)" ] ; then
+                   echo 1>&2 "$PRG: /proc/scsi/scsi does not exist"
+                   echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_SCSI_PROC_FS=y"
+                   return 1
+               fi
+               ;;
+       esac
        echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
        return 1
     fi
@@ -286,6 +297,12 @@ scsi_ofpath()
            DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
            echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
            ;;
+       ata_k2|sata_svw)
+           HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
+                       lgrep "$i" "k2-s-ata" ; done)"
+           DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
+           echo "${DEVICE_PATH##*device-tree}/k2-sata@$DEVICE_ID/disk@0:$PARTITION"
+           ;;
        *)
            echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
            return 1
@@ -296,23 +313,51 @@ scsi_ofpath()
 
 ide_ofpath()
 {
-    local OF1275IDE=/proc/ide/pmac
+    if [ ! -L "/proc/ide/$DEVNODE" ] ; then
+       echo 2>&1 "$PRG: /dev/$DEVNODE: Device not configured"
+       return 1
+    fi
+
+    local IDEBUS="$(v=`readlink /proc/ide/$DEVNODE` ; echo ${v%%/*} )"
+    if [ -z "$IDEBUS" ] ; then
+       echo 1>&2 "$PRG: BUG: IDEBUS == NULL"
+       return 1
+    fi
+
+    case "$(uname -r)" in
+       2.5.*|2.6.0*|2.6.1|2.6.1-*|2.6.2|2.6.2-*)
+           echo "$PRG: Linux kernel `uname -r` is not supported"
+           return 1
+           ;;
+       2.6.*|2.7.*)
+           if ! (grep -q '.* .* sysfs ' /proc/mounts 2> /dev/null) ; then
+               echo 1>&2 "$PRG: sysfs must be mounted for ofpath to support this system"
+               return 1
+           fi
+           local SYS="$(m=`grep '.* .* sysfs ' /proc/mounts | head -n 1` ; echo `d=${m#* };echo ${d%% *}`)"
+           if [ -z "$SYS" -o ! -d "$SYS" ] ; then
+               echo 2>&1 "$PRG: Unable to determine sysfs mountpoint"
+               return 1
+           fi
+           local OF1275IDE="${SYS}/block/${DEVNODE}/device/../../devspec"
+           ;;
+       *)
+           local OF1275IDE="/proc/ide/$IDEBUS/devspec"
+           ;;
+    esac
 
     if [ ! -f "$OF1275IDE" ] ; then
        case "$(cat /proc/device-tree/model)" in
-           "PowerMac3,6")
+           PowerMac3*|PowerMac4*|PowerMac5*|PowerMac6*|PowerMac7*|X*)
                local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
                if [ -z "$CDROM" ] ; then
+                   echo 1>&2 "$PRG: WARNING: Your kernel is too old for proper support, device may be innaccurate."
                    echo "ultra2:$PARTITION"
                else
                    echo "cd:$PARTITION"
                fi
                ;;
            *)
-               if [ ! -L "/proc/ide/$DEVNODE" ] ; then
-                   echo "$PRG: /dev/$DEVNODE: Device not configured"
-                   return 1
-               fi
                local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
                if [ -z "$CDROM" ] ; then
                    if [ "$DEVNODE" = hda ] ; then
@@ -326,34 +371,44 @@ ide_ofpath()
                ;;
        esac
     else
-       if [ ! -L "/proc/ide/$DEVNODE" ] ; then
-           echo 2>&1 "$PRG: /dev/$DEVNODE: Device not configured"
+       local DEVSPEC="$(cat $OF1275IDE)"
+       [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVSPEC=$DEVSPEC"
+       if [ -z "$DEVSPEC" ] ; then
+           echo 1>&2 "$PRG: KERNEL BUG: $OF1275IDE exists, but is empty"
            return 1
        fi
 
-       local IDEBUS="$(v=`readlink /proc/ide/$DEVNODE` ; echo ${v%%/*} )"
-       [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: IDEBUS=$IDEBUS"
-       local NUMBUSSES="$(($(linecount $OF1275IDE) / 5))"
-       [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: NUMBUSSES=$NUMBUSSES"
-       if [ "$((${IDEBUS##ide} + 1))" -gt "$NUMBUSSES" ] ; then
-           echo 1>&2 "$PRG: /dev/$DEVNODE is not supported"
+       if [ ! -f "/proc/ide/${IDEBUS}/channel" ] ; then
+           echo 1>&2 "$PRG: KERNEL BUG: /proc/ide/${IDEBUS}/channel does not exist"
            return 1
        fi
-       local IDEDEV="$(head -n $((5 * $((${IDEBUS##ide} + 1)))) $OF1275IDE | tail -n 5)"
-       [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: IDEDEV=$IDEDEV"
-       if [ "$(echo "$IDEDEV" | head -n 1)" != "$IDEBUS" ] ; then
-           echo "$PRG: /dev/$DEVNODE: is not supported"
-           return 1
-       fi
-       local DEVSPEC="$(v=`echo "$IDEDEV" | grep devspec` ; echo ${v##*:})"
-       [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVSPEC=$DEVSPEC"
+
+       case "$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)" in
+           ide|ata)
+               local MASTER="/disk@0"
+               local SLAVE="/disk@1"
+               ;;
+           pci-ide|pci-ata)
+               local MASTER="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@0"
+               local SLAVE="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@1"
+               ;;
+           scsi) ## some lame controllers pretend they are scsi, hopefully all kludges are created equal.
+               local MASTER="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 0))"
+               local SLAVE="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 1))"
+               ;;
+           *)
+               echo 2>&1 "$PRG: Unsupported IDE device type: \"$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)\""
+               return 1
+               ;;
+       esac
+
        case "$DEVNODE" in
-           hda|hdc|hde|hdg)
-               echo "${DEVSPEC}/disk@0:$PARTITION"
+           hda|hdc|hde|hdg|hdi|hdk|hdm|hdo)
+               echo "${DEVSPEC}${MASTER}:$PARTITION"
                return 0
                ;;
-           hdb|hdd|hdf|hdh)
-               echo "${DEVSPEC}/disk@1:$PARTITION"
+           hdb|hdd|hdf|hdh|hdj|hdl|hdn|hdp)
+               echo "${DEVSPEC}${SLAVE}:$PARTITION"
                return 0
                ;;
            *)
@@ -834,7 +889,7 @@ if [ `uname -s` != Linux ] ; then
 fi
 
 ## check for ppc, i think uname -m is safe for this...
-if [ `uname -m` != ppc ] ; then
+if [ `uname -m` != ppc -a `uname -m` != ppc64 ] ; then
     echo 1>&2 "$PRG: This utility will only work on PowerPC hardware"
     exit 1
 fi