3 ###############################################################################
5 ## ofpath: determine OpenFirmware path from unix device node
6 ## Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
8 ## Portions based on show_of_path.sh:
10 ## Copyright (C) 2000 Olaf Hering <olh@suse.de>
12 ## This program is free software; you can redistribute it and/or
13 ## modify it under the terms of the GNU General Public License
14 ## as published by the Free Software Foundation; either version 2
15 ## of the License, or (at your option) any later version.
17 ## This program is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## GNU General Public License for more details.
22 ## You should have received a copy of the GNU General Public License
23 ## along with this program; if not, write to the Free Software
24 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ###############################################################################
28 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
39 Written by Ethan Benson
40 Portions based on show_of_path.sh written by Olaf Hering
42 Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
43 Portions Copyright (C) 2000 Olaf Hering
44 This is free software; see the source for copying conditions. There is NO
45 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
52 "Usage: $PRG [OPTION]... FILE
53 Find OpenFirmware device path from unix device node.
55 --debug print boring junk only useful for debugging
56 -h, --help display this help and exit
57 -V, --version output version information and exit"
60 ## a small seq replacement, seq is not present on boot/rescue floppies.
66 while [ "$v" -gt 1 ] ; do
74 ## a kludge to replace wc -l, wc is not present on boot/rescue
75 ## floppies. max file is 145 lines, 3 hosts * 16 devs each * 3 lines
76 ## per device, + 1 "Attached Devices:" line.
83 local file="$(cat $1)"
87 if [ -z "$file" ] ; then
92 ## use real wc if available
93 if (command -v wc > /dev/null 2>&1) ; then
94 if [ -x `command -v wc` ] ; then
95 lines="$(echo "$file" | wc -l)"
105 for i in `smallseq 145` ; do
106 local b="$(echo "$file" | tail -n $i)"
107 if [ "$v" = "$b" ] ; then
116 ## small tr replacment which handles a specific need of this script.
120 a) echo 1 ;; b) echo 2 ;; c) echo 3 ;; d) echo 4 ;; e) echo 5 ;; f) echo 6 ;;
121 g) echo 7 ;; h) echo 8 ;; i) echo 9 ;; j) echo 10 ;; k) echo 11 ;; l) echo 12 ;;
122 m) echo 13 ;; n) echo 14 ;; o) echo 15 ;; p) echo 16 ;;
123 1) echo a ;; 2) echo b ;; 3) echo c ;; 4) echo d ;; 5) echo e ;;
124 6) echo f ;; 7) echo g ;; 8) echo h ;; 9) echo i ;; 10) echo j ;;
125 11) echo k ;; 12) echo l ;; 13) echo m ;; 14) echo n ;; 15) echo o ;;
131 ## replacment for grep -l which is not supported by busybox grep.
132 ## echo $(cat..) hack needed because busybox grep barfs with `line too
133 ## long' when fed /proc files. the for loop is needed since busybox
134 ## grep seems to have somewhat broken regexp support.
135 ## usage: lgrep filename regexp regexp ...
141 echo "$(cat "$f")" | grep -q "$i" && echo "$f" && break
146 ## if readlink is missing use a kludge
147 if (command -v readlink > /dev/null 2>&1) ; then
152 local SYMTARGET="$(v=`ls -l "$1" 2>/dev/null` ; echo ${v##*> })"
153 if [ -n "$SYMTARGET" ] ; then
162 ## a function to print relevant scsi host path when there is more then
163 ## one. this function also takes care of stripping off the trailing
184 ## this finds information we need on both newworld and oldworld macs.
185 ## mainly what scsi host a disk is attached to.
188 ## see if system has scsi at all
189 if [ ! -f /proc/scsi/scsi ] ; then
190 local kver="$(uname -r)"
193 if [ -d /sys/bus/scsi/devices -a \
194 -n "$(ls /sys/bus/scsi/devices 2>/dev/null)" ] ; then
195 echo 1>&2 "$PRG: /proc/scsi/scsi does not exist"
196 echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_SCSI_PROC_FS=y"
201 echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
205 ## first we have to figure out the SCSI ID, have to do that
206 ## anyway [to] find the attached scsi disks = "Direct-Access" and
207 ## stop at sda=1 sdb=2 or whatever count in 3 lines steps
209 ## get last letter of device node, ie sda -> a
210 SUBNODE=${DEVNODE##*sd}
212 ## turn SUBNODE above into a number starting at 1, ie a -> 1
213 SUBDEV="$(smalltr $SUBNODE)"
214 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SUBNODE=$SUBNODE SUBDEV=$SUBDEV"
218 ## copy scsi file into a variable removing "Attached Devices"
219 ## which is the first line. this avoids a lot of
220 ## [incmopatible] crap later, and improves readability.
222 ## find number of lines once and recycle that number, to save
223 ## some time (linecount is a bit slow). subtract one line
224 ## to scrap Attached Devices:
226 SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
228 if [ "$SUBDEV" -gt "$(cat /proc/scsi/scsi | grep Direct-Access | linecount)" ] ; then
229 echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
233 PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
235 for i in $(smallseq $(($SCSILINES / 3))) ; do
237 ## put every scsi device into one single line
238 DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
239 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVINFO=$DEVINFO"
241 ## cut the type field, expect "Direct-Access" later.
242 DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
243 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVTYPE=$DEVTYPE"
245 ## get the device LUN.
246 DEVLUN="$(v=$(echo ${DEVINFO##*Lun: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
247 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVLUN=$DEVLUN"
249 ## get the device id.
250 DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
251 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVID=$DEVID"
253 ## get the device bus.
254 DEVBUS="$(v=$(echo ${DEVINFO##*Channel: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
255 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVBUS=$DEVBUS"
257 ## get the scsi host id.
258 DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
259 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVHOST=$DEVHOST"
261 if [ "$DEVTYPE" = "Direct-Access" ] || [ "$DEVTYPE" = "Direct-Access-RBC" ] ; then
262 DEVCOUNT="$(($DEVCOUNT + 1))"
263 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVCOUNT=$DEVCOUNT"
264 if [ "$SUBDEV" = "$DEVCOUNT" ] ; then
269 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVICE_HOST=$DEVICE_HOST"
275 ## figure out what the scsi driver is, it is /proc/scsi/dirname.
276 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVICE_HOST=$DEVICE_HOST"
277 SCSI_DRIVER="$(x=`ls /proc/scsi/*/$DEVICE_HOST 2>/dev/null | cat` ; y=`echo ${x##*proc/scsi/}` ; echo ${y%%/*})"
278 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SCSI_DRIVER=$SCSI_DRIVER"
280 ## figure out which host we found.
281 SCSI_HOSTNUMBER="$(v=`ls /proc/scsi/$SCSI_DRIVER/* 2>/dev/null | cat | grep -n "$DEVICE_HOST\>"` ; echo ${v%%:*})"
282 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SCSI_HOSTNUMBER=$SCSI_HOSTNUMBER"
295 while [[ $PWD != "/" ]]; do
296 ls $ATTR > /dev/null 2>&1
297 if [[ $? -eq 0 ]]; then
307 echo 1>&2 "$PRG: Cannot find $RET for $DEVICE"
314 local DEVICE="$DEVICE_HOST:$DEVICE_BUS:$DEVICE_ID:$DEVICE_LUN"
315 local START="/sys/bus/scsi/devices/$DEVICE"
317 read_attr $ATTR $START
323 local DEVICE="$DEVICE_HOST:$DEVICE_BUS:$DEVICE_ID:$DEVICE_LUN"
324 local START="/sys/bus/scsi/devices/$DEVICE/.."
326 read_attr $ATTR $START
334 B=$((0x$DEVICE_ID << 8))
335 C=$((0x$DEVICE_BUS << 5))
340 vdiskno=$((0x8000 | $B | $C | $D ))
343 vdisk=$(printf "%x" $vdisk)
344 local extrazeroes="000000000000"
345 echo $vdisk$extrazeroes
353 A=$(( ($lunint >> 8) & 0xff ))
354 B=$(($lunint & 0xff))
355 C=$(( ($lunint >> 24) & 0xff ))
356 D=$(( ($lunint >> 16) & 0xff ))
358 local lunstr=$(printf "%02x%02x%02x%02x00000000" $A $B $C $D)
359 lunstr=$(echo $lunstr | sed 's/^[0]*//')
365 local L=$(echo "$DEVICE_LUN"|gawk '{$1=tolower($1);print}')
368 local fc_lun=$(int_to_scsilun $L)
376 for f in `find -H $start_dir -maxdepth 2 -name port_name`; do
381 # strip the leading 0x
388 local DEVSPEC=$(read_parent_attr devspec)
389 local COMPAT=$(cat "/proc/device-tree$DEVSPEC/compatible")
390 local DEVICE_PATH="/sys/bus/scsi/devices/$DEVICE_HOST:$DEVICE_BUS:$DEVICE_ID:$DEVICE_LUN"
394 local vdisk=$(get_vdisk_lun)
395 if [[ $PARTITION = "" ]]; then
396 echo "$DEVSPEC/disk@$vdisk"
398 echo "$DEVSPEC/disk@$vdisk:$PARTITION"
403 local vfc_lun=$(get_fc_scsilun)
404 local wwpn=$(get_fc_wwpn "$DEVICE_PATH/../../fc_remote_ports*")
405 if [[ $DEVICE_LUN != "0" ]]; then
406 local vfc_lun=$(get_fc_scsilun)
407 echo "$DEVSPEC/disk@$wwpn,$vfc_lun"
409 echo "$DEVSPEC/disk@$wwpn"
417 if [[ -d "/proc/device-tree$DEVSPEC/sas" ]]; then
418 local vendor_id=$(read_parent_attr vendor)
421 if [[ $vendor_id = "0x1000" ]]; then
422 sas_addr=$(read_dev_attr sas_address)
423 sas_addr=${sas_addr##0x}
424 if [[ $DEVICE_LUN != "0" ]]; then
425 local LUN=$(int_to_scsilun $DEVICE_LUN)
426 echo "$DEVSPEC/sas/disk@$sas_addr,$LUN"
428 echo "$DEVSPEC/sas/disk@$sas_addr"
434 if [[ -e /sys/class/scsi_host/host$DEVICE_HOST/fw_type ]]; then
435 fwtype=$(cat /sys/class/scsi_host/host$DEVICE_HOST/fw_type)
438 if [[ $fwtype = "1" ]]; then
439 sas_addr=$(read_dev_attr device_id)
440 sas_addr=${sas_addr##0x}
441 if [[ $DEVICE_LUN != "0" ]]; then
442 local LUN=$(int_to_scsilun $DEVICE_LUN)
443 echo "$DEVSPEC/sas/disk@$sas_addr,$LUN"
445 echo "$DEVSPEC/sas/disk@$sas_addr"
450 B=$(echo "$DEVICE_BUS"|gawk '{$1=tolower($1);print}')
452 T=$(echo "$DEVICE_ID"|gawk '{$1=tolower($1);print}')
454 L=$(echo "$DEVICE_LUN"|gawk '{$1=tolower($1);print}')
457 sas_addr=$(( ($B << 16) | ($T << 8) | $L ))
458 if [[ $DEVICE_LUN != "0" ]]; then
459 printf "%s/sas/disk@%x,%x\n" $DEVSPEC $sas_addr $DEVICE_LUN
461 printf "%s/sas/disk@%x\n" $DEVSPEC $sas_addr
467 local fc=${DEVSPEC%@*}
470 if [[ -e /proc/device-tree$DEVSPEC/device_type ]]; then
471 local devtype=$(cat /proc/device-tree$DEVSPEC/device_type);
472 if [[ $devtype = "fcp" || $devtype = "scsi-fcp" ]]; then
477 if [[ $fc = "fibre-channel" ]]; then
478 local wwpn=$(get_fc_wwpn "$DEVICE_PATH/../../fc_remote_ports*")
479 local ofpath=$DEVSPEC
481 if [[ ! -e /proc/device-tree$DEVSPEC/disk ]]; then
482 for dir in `find /proc/device-tree$DEVSPEC -type d`; do
483 if [[ -e $dir/disk ]]; then
484 ofpath=${dir##/proc/device-tree}
490 ofpath=$(printf "%s/disk@%s" $ofpath $wwpn)
492 if [[ $DEVICE_LUN != "0" ]]; then
493 local fc_lun=$(get_fc_scsilun $DEVICE_LUN)
494 ofpath=$(printf "%s,%s" $ofpath $fc_lun)
501 echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
505 ## generic function that can find OF device paths for scsi devices,
506 ## must be run after scsiinfo().
509 case "$SCSI_DRIVER" in
511 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
512 lgrep "$i" "^ADPT" "^pci900[45]" "^pciclass,01000" ; done)"
513 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
514 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
517 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
518 lgrep "$i" "^Symbios" "^pci1000" "^pciclass,01000" ; done)"
519 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
520 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
523 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
524 lgrep "$i" "mesh" ; done)"
525 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
526 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
529 #Not all G5 device trees have a compatible "k2-sata" node
530 #per channel use parent
531 HOST_LIST="$(for i in `find /proc/device-tree -name compatible ` ; do
532 lgrep "$i" "k2-s-ata" ; done | sort)"
533 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
535 while [ "$DEVICE_PATH" = "" ] ; do
536 SCSI_HOSTNUMBER=`expr $SCSI_HOSTNUMBER - 1`
537 let "K2_DEVICE_ID += 1"
538 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
540 echo "${DEVICE_PATH##*device-tree}/k2-sata@$K2_DEVICE_ID/disk@0:$PARTITION"
543 HOST_LIST="$(for i in `find /proc/device-tree -name name | grep usb` ; do
544 lgrep "$i" "disk" ; done)"
545 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
546 echo "${DEVICE_PATH##*device-tree}:$PARTITION"
549 # sbp-2 driver may not have a dir in /proc/scsi
550 HOST_LIST="$(for i in `find /proc/device-tree -name name` ; do
551 lgrep "$i" "sbp-2" ; done)"
552 if [ "$HOST_LIST" = "" ] ; then
555 if [ "$SCSI_HOSTNUMBER" = "" ] ; then
558 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
559 echo "${DEVICE_PATH##*device-tree}/disk@0:$PARTITION"
563 echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
572 if [ ! -L "/proc/ide/$DEVNODE" ] ; then
573 echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
577 local IDEBUS="$(v=`readlink /proc/ide/$DEVNODE` ; echo ${v%%/*} )"
578 if [ -z "$IDEBUS" ] ; then
579 echo 1>&2 "$PRG: BUG: IDEBUS == NULL"
583 case "$(uname -r)" in
584 2.5.*|2.6.0*|2.6.1|2.6.1-*|2.6.2|2.6.2-*)
585 echo 1>&2 "$PRG: Linux kernel `uname -r` is not supported"
589 if ! (grep -q '.* .* sysfs ' /proc/mounts 2> /dev/null) ; then
590 echo 1>&2 "$PRG: sysfs must be mounted for ofpath to support this system"
593 local SYS="$(m=`grep '.* .* sysfs ' /proc/mounts | head -n 1` ; echo `d=${m#* };echo ${d%% *}`)"
594 if [ -z "$SYS" -o ! -d "$SYS" ] ; then
595 echo 1>&2 "$PRG: Unable to determine sysfs mountpoint"
598 local OF1275IDE="${SYS}/block/${DEVNODE}/device/../../devspec"
601 local OF1275IDE="/proc/ide/$IDEBUS/devspec"
605 if [ ! -f "$OF1275IDE" ] ; then
606 case "$(cat /proc/device-tree/model)" in
607 PowerMac3*|PowerMac4*|PowerMac5*|PowerMac6*|PowerMac7*|RackMac*)
608 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
609 if [ -z "$CDROM" ] ; then
610 echo 1>&2 "$PRG: WARNING: Your kernel is too old for proper support, device may be innaccurate."
611 echo "ultra2:$PARTITION"
617 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
618 if [ -z "$CDROM" ] ; then
619 if [ "$DEVNODE" = hda ] ; then
622 echo "ultra1:$PARTITION"
630 local DEVSPEC="$(cat $OF1275IDE)"
631 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVSPEC=$DEVSPEC"
632 if [ -z "$DEVSPEC" ] ; then
633 echo 1>&2 "$PRG: KERNEL BUG: $OF1275IDE exists, but is empty"
637 if [ ! -f "/proc/ide/${IDEBUS}/channel" ] ; then
638 echo 1>&2 "$PRG: KERNEL BUG: /proc/ide/${IDEBUS}/channel does not exist"
642 case "$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)" in
644 local MASTER="/disk@0"
645 local SLAVE="/disk@1"
648 local MASTER="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@0"
649 local SLAVE="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@1"
651 scsi) ## some lame controllers pretend they are scsi, hopefully all kludges are created equal.
652 local MASTER="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 0))"
653 local SLAVE="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 1))"
656 local MASTER="/disk@$(cat /proc/ide/${IDEBUS}/channel),0"
657 local SLAVE="/disk@$(cat /proc/ide/${IDEBUS}/channel),1"
660 echo 1>&2 "$PRG: Unsupported IDE device type: \"$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)\""
666 hda|hdc|hde|hdg|hdi|hdk|hdm|hdo)
667 echo "${DEVSPEC}${MASTER}:$PARTITION"
670 hdb|hdd|hdf|hdh|hdj|hdl|hdn|hdp)
671 echo "${DEVSPEC}${SLAVE}:$PARTITION"
675 echo 1>&2 "$PRG: /dev/$DEVNODE is not supported"
682 ## figure out the OpenFirmware device path for newworld macs.
683 ## sd* scsi disks , hd* ide disks.
688 ## use common scsiinfo function to get info we need.
691 ## now we have the data for /@$DEVID:$PARTITION
692 ## find the actual OF path.
693 scsi_ofpath || return 1
696 ide_ofpath || return 1
699 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
708 ## for some reason 2.4 kernels put OF aliases in aliases@0/ instead of plain aliases/
709 if [ -d "/proc/device-tree/aliases" ] ; then
710 local ALIASES="aliases"
711 elif [ -d "/proc/device-tree/aliases@0" ] ; then
712 local ALIASES="aliases@0"
714 echo 1>&2 "$PRG: Cannot find OpenFirmware aliases directory in /proc/device-tree/"
718 local MODEL="$(cat /proc/device-tree/compatible)"
719 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: Oldworld subarch: $MODEL"
722 AAPL,7300*|AAPL,7500*|AAPL,8500*|AAPL,9500*|AAPL,\?\?\?\?*)
726 case "$SCSI_DRIVER" in
728 echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITION
731 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
734 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
740 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
749 case "$SCSI_DRIVER" in
751 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
754 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
760 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@0:$PARTITION
763 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@1:$PARTITION
766 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
774 case "$SCSI_DRIVER" in
776 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
779 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
785 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@0:$PARTITION
788 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@1:$PARTITION
791 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
795 AAPL,Gossamer*|AAPL,PowerMac\ G3*)
799 case "$SCSI_DRIVER" in
801 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
804 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
810 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@0:$PARTITION
813 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@1:$PARTITION
816 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@0:$PARTITION
819 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@1:$PARTITION
822 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
827 if [ -f /proc/device-tree/$ALIASES/ata0 ] ; then
832 if [ -f /proc/device-tree/$ALIASES/ata1 ] ; then
840 case "$SCSI_DRIVER" in
842 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
845 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
851 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@0:$PARTITION
854 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@1:$PARTITION
857 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@0:$PARTITION
860 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@1:$PARTITION
863 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
872 case "$SCSI_DRIVER" in
874 echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITON
877 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
880 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
886 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@0:$PARTITION
889 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@1:$PARTITION
892 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@0:$PARTITION
895 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@1:$PARTITION
898 echo $(cat /proc/device-tree/$ALIASES/ata2):$PARTITION
901 echo $(cat /proc/device-tree/$ALIASES/ata3):$PARTITION
904 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
910 echo 1>&2 "$PRG: This machine is not supported: $MODEL"
919 read_attr devspec /sys/class/net/$DEVICE/device
924 local hfnum=${DEVICE##hf}
927 if [[ $hfnum = "0" || $hfnum = "2" ]]; then
928 hfpath=$(find /proc/device-tree -name hfi-ethernet* | sort | head -n 1)
929 elif [[ $hfnum = "1" || $hfnum = "3" ]]; then
930 hfpath=$(find /proc/device-tree -name hfi-ethernet* | sort | tail -n 1)
932 echo 1>&2 "$PRG: Unsupported device: $DEVICE"
936 hfpath=${hfpath##/proc/device-tree}
940 ## find OpenFirmware device path for IBM CHRP hardware (scsi only)
946 ## use common scsiinfo function to get info we need.
949 ## now we have the data for /@$DEVID:$PARTITION
950 ## find the actual OF path.
951 scsi_ofpath || return 1
954 eth_ofpath || return 1
957 hfi_ofpath || return 1
960 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
967 ## If we get lame devfs name, we need to make it foad
971 /dev/ide/*|/dev/scsi/*|/dev/discs/*)
980 ## convert devfs names into normal short ones, written by Tom Rini.
983 ## get partition number, if any
984 local PARTNUM="${1##*[a-z]}"
985 ## Find the bus type.
986 local TYPE="$(v=${1#/dev/} ; echo ${v%/host*})"
987 ## Find the host number.
988 local HOST="$(v=${1#/dev/*/host} ; echo ${v%/bus*})"
989 ## Find the bus number.
990 local BUS="$(v=${1#/dev/*/bus} ; echo ${v%/tar*})"
992 local TARGET="$(v=${1#/dev/*/target} ; echo ${v%/lun*})"
1018 echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
1021 local DEV="${DEV}${PARTNUM}"
1026 local LUN="$(v=${1#/dev/*/lun} ; echo ${v%/*})"
1028 ## In this case, we need to figure out what number our device is
1031 ## copy scsi file into a variable removing "Attached Devices"
1032 ## which is the first line. this avoids a lot of
1033 ## [incmopatible] crap later, and improves readability.
1035 ## find number of lines once and recycle that number, to save
1036 ## some time (linecount is a bit slow). subtract one line
1037 ## to scrap Attached Devices:
1039 local SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
1040 local PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
1042 for i in $(smallseq $(($SCSILINES / 3))) ; do
1044 ## put every scsi device into one single line
1045 local DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
1046 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVINFO=$DEVINFO"
1048 ## cut the type field, expect "Direct-Access" later.
1049 local DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
1050 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVTYPE=$DEVTYPE"
1052 if [ "$DEVTYPE" = "Direct-Access" ] || [ "$DEVTYPE" = "Direct-Access-RBC" ] ; then
1053 ## Lets find out some more information
1054 ## get the device id.
1055 local DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
1056 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVID=$DEVID"
1058 ## get the device lun.
1059 local DEVLUN="$(v=$(echo ${DEVINFO##*Lun: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
1060 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVLUN=$DEVLUN"
1062 ## get the device channel.
1063 local DEVCHAN="$(v=$(echo ${DEVINFO##*Channel: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
1064 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCHAN=$DEVCHAN"
1066 ## get the scsi host id.
1067 local DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
1068 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVHOST=$DEVHOST"
1070 local DEVCOUNT="$(($DEVCOUNT + 1))"
1071 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCOUNT=$DEVCOUNT"
1072 if [ "$DEVHOST" = "$HOST" -a "$DEVCHAN" = "$BUS" -a \
1073 "$DEVID" = "$TARGET" -a "$DEVLUN" = "$LUN" ] ; then
1074 local DEV="sd$(smalltr $DEVCOUNT)${PARTNUM}"
1080 echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
1084 echo 1>&2 "$PRG: Unknown bus $TYPE"
1088 ## we should never get here
1092 ## make sure that find, head and tail can be found. otherwise the
1093 ## script will silently give bogus paths. these are the only /usr/*
1094 ## utilities this script depends on.
1097 if command -v find > /dev/null 2>&1 ; then
1098 [ -x `command -v find` ] || FAIL=1 ; else FAIL=1 ; fi
1099 if command -v head > /dev/null 2>&1 ; then
1100 [ -x `command -v head` ] || FAIL=1 ; else FAIL=1 ; fi
1101 if command -v tail > /dev/null 2>&1 ; then
1102 [ -x `command -v tail` ] || FAIL=1 ; else FAIL=1 ; fi
1104 if [ "$FAIL" = 1 ] ; then
1105 echo 1>&2 "$PRG: \`find', \`head', or \`tail' could not be found, aborting."
1112 ## parse command line switches.
1113 if [ $# != 0 ] ; then
1129 echo 1>&2 "$PRG: unrecognized option \`$1'"
1130 echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
1134 echo 1>&2 "$PRG: You must specify a filename"
1135 echo 1>&2 "Try \`$PRG --help' for more information."
1145 echo 1>&2 "$PRG: You must specify a /dev device"
1146 echo 1>&2 "Try \`$PRG --help' for more information."
1150 ## check that we are running on a GNU/Linux system, OSX/BSD does not
1151 ## have the same /proc stuff
1152 if [ `uname -s` != Linux ] ; then
1153 echo 1>&2 "$PRG: This utility will only work with GNU/Linux"
1157 ## check for ppc, i think uname -m is safe for this...
1158 if [ `uname -m` != ppc -a `uname -m` != ppc64 ] ; then
1159 echo 1>&2 "$PRG: This utility will only work on PowerPC hardware"
1163 ## ofpath cannot live without procfs
1164 if [ ! -f /proc/uptime ] ; then
1165 echo 1>&2 "$PRG: This utility requires the /proc filesystem"
1169 ## check for retarded devfs names and tell them to foad.
1170 if ckdevfs "$device" ; then
1171 device="$(fixdevfs $device)" || exit 1
1174 ## check for newworld mac. use cat hack due to /proc wierdness.
1175 if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = NewWorld ] ; then
1177 elif [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = OldWorld ] ; then
1179 elif (cat /proc/cpuinfo 2>/dev/null | grep ^motherboard | grep -q AAPL) ; then
1181 elif (cat /proc/cpuinfo 2> /dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
1183 elif (cat /proc/cpuinfo 2>/dev/null | grep ^machine | grep -q 'CHRP Pegasos') ; then
1186 echo 1>&2 "$PRG: This machine is not yet supported"
1190 ## make sure /proc/device-tree exists
1191 if [ ! -d /proc/device-tree ] ; then
1192 echo 1>&2 "$PRG: /proc/device-tree does not exist"
1193 echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_PROC_DEVICETREE=y"
1197 ## make sure we have what we need.
1198 checkutils || exit 1
1200 ## get the base device node and scrap /dev/ ie /dev/hda2 -> hda
1201 DEVICE="${device##*/}"
1202 DEVNODE="${DEVICE%%[0-9]*}"
1203 PARTITION="${DEVICE##*[a-z]}"
1205 ## use appropriate search for right sub arch.
1207 # Pegasos OF seems to be NewWorld-ish enough to cope with this.