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.
119 local i a d t val out mod cur
124 d=$(printf "%d\n" \'${val:0:1})
126 if (( $d > 57 )) ; then # is a letter
127 for ((i=0; i < ${#val}; i++)) ; do
128 d=$(printf "%d\n" \'${val:$i:1})
138 while ((t != 0)) ; do
141 if (($mod == 0)) ; then
146 cur=$(echo $mod | gawk '{printf "%c", $1}')
157 ## replacment for grep -l which is not supported by busybox grep.
158 ## echo $(cat..) hack needed because busybox grep barfs with `line too
159 ## long' when fed /proc files. the for loop is needed since busybox
160 ## grep seems to have somewhat broken regexp support.
161 ## usage: lgrep filename regexp regexp ...
167 echo "$(cat "$f")" | grep -q "$i" && echo "$f" && break
172 ## if readlink is missing use a kludge
173 if (command -v readlink > /dev/null 2>&1) ; then
178 local SYMTARGET="$(v=`ls -l "$1" 2>/dev/null` ; echo ${v##*> })"
179 if [ -n "$SYMTARGET" ] ; then
188 ## a function to print relevant scsi host path when there is more then
189 ## one. this function also takes care of stripping off the trailing
210 ## this finds information we need on both newworld and oldworld macs.
211 ## mainly what scsi host a disk is attached to.
214 ## see if system has scsi at all
215 if [ ! -f /proc/scsi/scsi ] ; then
216 local kver="$(uname -r)"
219 if [ -d /sys/bus/scsi/devices -a \
220 -n "$(ls /sys/bus/scsi/devices 2>/dev/null)" ] ; then
221 echo 1>&2 "$PRG: /proc/scsi/scsi does not exist"
222 echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_SCSI_PROC_FS=y"
227 echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
231 ## first we have to figure out the SCSI ID, have to do that
232 ## anyway [to] find the attached scsi disks = "Direct-Access" and
233 ## stop at sda=1 sdb=2 or whatever count in 3 lines steps
235 ## get last letter of device node, ie sda -> a
236 SUBNODE=${DEVNODE##*sd}
238 ## turn SUBNODE above into a number starting at 1, ie a -> 1
239 SUBDEV="$(smalltr $SUBNODE)"
240 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SUBNODE=$SUBNODE SUBDEV=$SUBDEV"
244 ## copy scsi file into a variable removing "Attached Devices"
245 ## which is the first line. this avoids a lot of
246 ## [incmopatible] crap later, and improves readability.
248 ## find number of lines once and recycle that number, to save
249 ## some time (linecount is a bit slow). subtract one line
250 ## to scrap Attached Devices:
252 SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
254 if [ "$SUBDEV" -gt "$(cat /proc/scsi/scsi | grep Direct-Access | linecount)" ] ; then
255 echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
259 PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
261 for i in $(smallseq $(($SCSILINES / 3))) ; do
263 ## put every scsi device into one single line
264 DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
265 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVINFO=$DEVINFO"
267 ## cut the type field, expect "Direct-Access" later.
268 DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
269 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVTYPE=$DEVTYPE"
271 ## get the device LUN.
272 DEVLUN="$(v=$(echo ${DEVINFO##*Lun: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
273 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVLUN=$DEVLUN"
275 ## get the device id.
276 DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
277 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVID=$DEVID"
279 ## get the device bus.
280 DEVBUS="$(v=$(echo ${DEVINFO##*Channel: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
281 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVBUS=$DEVBUS"
283 ## get the scsi host id.
284 DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
285 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVHOST=$DEVHOST"
287 if [ "$DEVTYPE" = "Direct-Access" ] || [ "$DEVTYPE" = "Direct-Access-RBC" ] ; then
288 DEVCOUNT="$(($DEVCOUNT + 1))"
289 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVCOUNT=$DEVCOUNT"
290 if [ "$SUBDEV" = "$DEVCOUNT" ] ; then
295 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVICE_HOST=$DEVICE_HOST"
301 ## figure out what the scsi driver is, it is /proc/scsi/dirname.
302 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVICE_HOST=$DEVICE_HOST"
303 SCSI_DRIVER="$(x=`ls /proc/scsi/*/$DEVICE_HOST 2>/dev/null | cat` ; y=`echo ${x##*proc/scsi/}` ; echo ${y%%/*})"
304 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SCSI_DRIVER=$SCSI_DRIVER"
306 ## figure out which host we found.
307 SCSI_HOSTNUMBER="$(v=`ls /proc/scsi/$SCSI_DRIVER/* 2>/dev/null | cat | grep -n "$DEVICE_HOST\>"` ; echo ${v%%:*})"
308 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SCSI_HOSTNUMBER=$SCSI_HOSTNUMBER"
321 while [[ $PWD != "/" ]]; do
322 ls $ATTR > /dev/null 2>&1
323 if [[ $? -eq 0 ]]; then
333 echo 1>&2 "$PRG: Cannot find $RET for $DEVICE"
340 local DEVICE="$DEVICE_HOST:$DEVICE_BUS:$DEVICE_ID:$DEVICE_LUN"
341 local START="/sys/bus/scsi/devices/$DEVICE"
343 read_attr $ATTR $START
349 local DEVICE="$DEVICE_HOST:$DEVICE_BUS:$DEVICE_ID:$DEVICE_LUN"
350 local START="/sys/bus/scsi/devices/$DEVICE/.."
352 read_attr $ATTR $START
360 B=$((0x$DEVICE_ID << 8))
361 C=$((0x$DEVICE_BUS << 5))
366 vdiskno=$((0x8000 | $B | $C | $D ))
369 vdisk=$(printf "%x" $vdisk)
370 local extrazeroes="000000000000"
371 echo $vdisk$extrazeroes
379 A=$(( ($lunint >> 8) & 0xff ))
380 B=$(($lunint & 0xff))
381 C=$(( ($lunint >> 24) & 0xff ))
382 D=$(( ($lunint >> 16) & 0xff ))
384 local lunstr=$(printf "%02x%02x%02x%02x00000000" $A $B $C $D)
385 lunstr=$(echo $lunstr | sed 's/^[0]*//')
391 local L=$(echo "$DEVICE_LUN"|gawk '{$1=tolower($1);print}')
394 local fc_lun=$(int_to_scsilun $L)
402 for f in `find -H $start_dir -maxdepth 2 -name port_name`; do
407 # strip the leading 0x
414 local DEVSPEC=$(read_parent_attr devspec)
415 local COMPAT=$(cat "/proc/device-tree$DEVSPEC/compatible")
416 local DEVICE_PATH="/sys/bus/scsi/devices/$DEVICE_HOST:$DEVICE_BUS:$DEVICE_ID:$DEVICE_LUN"
420 local vdisk=$(get_vdisk_lun)
421 if [[ $PARTITION = "" ]]; then
422 echo "$DEVSPEC/disk@$vdisk"
424 echo "$DEVSPEC/disk@$vdisk:$PARTITION"
429 local vfc_lun=$(get_fc_scsilun)
430 local wwpn=$(get_fc_wwpn "$DEVICE_PATH/../../fc_remote_ports*")
431 if [[ $DEVICE_LUN != "0" ]]; then
432 local vfc_lun=$(get_fc_scsilun)
433 echo "$DEVSPEC/disk@$wwpn,$vfc_lun"
435 echo "$DEVSPEC/disk@$wwpn"
443 if [[ -d "/proc/device-tree$DEVSPEC/sas" ]]; then
444 local vendor_id=$(read_parent_attr vendor)
447 if [[ $vendor_id = "0x1000" ]]; then
448 sas_addr=$(read_dev_attr sas_address)
449 sas_addr=${sas_addr##0x}
450 if [[ $DEVICE_LUN != "0" ]]; then
451 local LUN=$(int_to_scsilun $DEVICE_LUN)
452 echo "$DEVSPEC/sas/disk@$sas_addr,$LUN"
454 echo "$DEVSPEC/sas/disk@$sas_addr"
460 if [[ -e /sys/class/scsi_host/host$DEVICE_HOST/fw_type ]]; then
461 fwtype=$(cat /sys/class/scsi_host/host$DEVICE_HOST/fw_type)
464 if [[ $fwtype = "1" ]]; then
465 sas_addr=$(read_dev_attr device_id)
466 sas_addr=${sas_addr##0x}
467 if [[ $DEVICE_LUN != "0" ]]; then
468 local LUN=$(int_to_scsilun $DEVICE_LUN)
469 echo "$DEVSPEC/sas/disk@$sas_addr,$LUN"
471 echo "$DEVSPEC/sas/disk@$sas_addr"
476 B=$(echo "$DEVICE_BUS"|gawk '{$1=tolower($1);print}')
478 T=$(echo "$DEVICE_ID"|gawk '{$1=tolower($1);print}')
480 L=$(echo "$DEVICE_LUN"|gawk '{$1=tolower($1);print}')
483 sas_addr=$(( ($B << 16) | ($T << 8) | $L ))
484 if [[ $DEVICE_LUN != "0" ]]; then
485 printf "%s/sas/disk@%x,%x\n" $DEVSPEC $sas_addr $DEVICE_LUN
487 printf "%s/sas/disk@%x\n" $DEVSPEC $sas_addr
493 local fc=${DEVSPEC%@*}
496 if [[ -e /proc/device-tree$DEVSPEC/device_type ]]; then
497 local devtype=$(cat /proc/device-tree$DEVSPEC/device_type);
498 if [[ $devtype = "fcp" || $devtype = "scsi-fcp" ]]; then
503 if [[ $fc = "fibre-channel" ]]; then
504 local wwpn=$(get_fc_wwpn "$DEVICE_PATH/../../fc_remote_ports*")
505 local ofpath=$DEVSPEC
507 if [[ ! -e /proc/device-tree$DEVSPEC/disk ]]; then
508 for dir in `find /proc/device-tree$DEVSPEC -type d`; do
509 if [[ -e $dir/disk ]]; then
510 ofpath=${dir##/proc/device-tree}
516 ofpath=$(printf "%s/disk@%s" $ofpath $wwpn)
518 if [[ $DEVICE_LUN != "0" ]]; then
519 local fc_lun=$(get_fc_scsilun $DEVICE_LUN)
520 ofpath=$(printf "%s,%s" $ofpath $fc_lun)
527 echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
531 ## generic function that can find OF device paths for scsi devices,
532 ## must be run after scsiinfo().
535 case "$SCSI_DRIVER" in
537 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
538 lgrep "$i" "^ADPT" "^pci900[45]" "^pciclass,01000" ; done)"
539 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
540 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
543 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
544 lgrep "$i" "^Symbios" "^pci1000" "^pciclass,01000" ; done)"
545 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
546 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
549 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
550 lgrep "$i" "mesh" ; done)"
551 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
552 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
555 #Not all G5 device trees have a compatible "k2-sata" node
556 #per channel use parent
557 HOST_LIST="$(for i in `find /proc/device-tree -name compatible ` ; do
558 lgrep "$i" "k2-s-ata" ; done | sort)"
559 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
561 while [ "$DEVICE_PATH" = "" ] ; do
562 SCSI_HOSTNUMBER=`expr $SCSI_HOSTNUMBER - 1`
563 let "K2_DEVICE_ID += 1"
564 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
566 echo "${DEVICE_PATH##*device-tree}/k2-sata@$K2_DEVICE_ID/disk@0:$PARTITION"
569 HOST_LIST="$(for i in `find /proc/device-tree -name name | grep usb` ; do
570 lgrep "$i" "disk" ; done)"
571 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
572 echo "${DEVICE_PATH##*device-tree}:$PARTITION"
575 # sbp-2 driver may not have a dir in /proc/scsi
576 HOST_LIST="$(for i in `find /proc/device-tree -name name` ; do
577 lgrep "$i" "sbp-2" ; done)"
578 if [ "$HOST_LIST" = "" ] ; then
581 if [ "$SCSI_HOSTNUMBER" = "" ] ; then
584 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
585 echo "${DEVICE_PATH##*device-tree}/disk@0:$PARTITION"
589 echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
598 if [ ! -L "/proc/ide/$DEVNODE" ] ; then
599 echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
603 local IDEBUS="$(v=`readlink /proc/ide/$DEVNODE` ; echo ${v%%/*} )"
604 if [ -z "$IDEBUS" ] ; then
605 echo 1>&2 "$PRG: BUG: IDEBUS == NULL"
609 case "$(uname -r)" in
610 2.5.*|2.6.0*|2.6.1|2.6.1-*|2.6.2|2.6.2-*)
611 echo 1>&2 "$PRG: Linux kernel `uname -r` is not supported"
615 if ! (grep -q '.* .* sysfs ' /proc/mounts 2> /dev/null) ; then
616 echo 1>&2 "$PRG: sysfs must be mounted for ofpath to support this system"
619 local SYS="$(m=`grep '.* .* sysfs ' /proc/mounts | head -n 1` ; echo `d=${m#* };echo ${d%% *}`)"
620 if [ -z "$SYS" -o ! -d "$SYS" ] ; then
621 echo 1>&2 "$PRG: Unable to determine sysfs mountpoint"
624 local OF1275IDE="${SYS}/block/${DEVNODE}/device/../../devspec"
627 local OF1275IDE="/proc/ide/$IDEBUS/devspec"
631 if [ ! -f "$OF1275IDE" ] ; then
632 case "$(cat /proc/device-tree/model)" in
633 PowerMac3*|PowerMac4*|PowerMac5*|PowerMac6*|PowerMac7*|RackMac*)
634 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
635 if [ -z "$CDROM" ] ; then
636 echo 1>&2 "$PRG: WARNING: Your kernel is too old for proper support, device may be innaccurate."
637 echo "ultra2:$PARTITION"
643 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
644 if [ -z "$CDROM" ] ; then
645 if [ "$DEVNODE" = hda ] ; then
648 echo "ultra1:$PARTITION"
656 local DEVSPEC="$(cat $OF1275IDE)"
657 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVSPEC=$DEVSPEC"
658 if [ -z "$DEVSPEC" ] ; then
659 echo 1>&2 "$PRG: KERNEL BUG: $OF1275IDE exists, but is empty"
663 if [ ! -f "/proc/ide/${IDEBUS}/channel" ] ; then
664 echo 1>&2 "$PRG: KERNEL BUG: /proc/ide/${IDEBUS}/channel does not exist"
668 case "$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)" in
670 local MASTER="/disk@0"
671 local SLAVE="/disk@1"
674 local MASTER="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@0"
675 local SLAVE="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@1"
677 scsi) ## some lame controllers pretend they are scsi, hopefully all kludges are created equal.
678 local MASTER="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 0))"
679 local SLAVE="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 1))"
682 local MASTER="/disk@$(cat /proc/ide/${IDEBUS}/channel),0"
683 local SLAVE="/disk@$(cat /proc/ide/${IDEBUS}/channel),1"
686 echo 1>&2 "$PRG: Unsupported IDE device type: \"$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)\""
692 hda|hdc|hde|hdg|hdi|hdk|hdm|hdo)
693 echo "${DEVSPEC}${MASTER}:$PARTITION"
696 hdb|hdd|hdf|hdh|hdj|hdl|hdn|hdp)
697 echo "${DEVSPEC}${SLAVE}:$PARTITION"
701 echo 1>&2 "$PRG: /dev/$DEVNODE is not supported"
708 ## figure out the OpenFirmware device path for newworld macs.
709 ## sd* scsi disks , hd* ide disks.
714 ## use common scsiinfo function to get info we need.
717 ## now we have the data for /@$DEVID:$PARTITION
718 ## find the actual OF path.
719 scsi_ofpath || return 1
722 ide_ofpath || return 1
725 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
734 ## for some reason 2.4 kernels put OF aliases in aliases@0/ instead of plain aliases/
735 if [ -d "/proc/device-tree/aliases" ] ; then
736 local ALIASES="aliases"
737 elif [ -d "/proc/device-tree/aliases@0" ] ; then
738 local ALIASES="aliases@0"
740 echo 1>&2 "$PRG: Cannot find OpenFirmware aliases directory in /proc/device-tree/"
744 local MODEL="$(cat /proc/device-tree/compatible)"
745 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: Oldworld subarch: $MODEL"
748 AAPL,7300*|AAPL,7500*|AAPL,8500*|AAPL,9500*|AAPL,\?\?\?\?*)
752 case "$SCSI_DRIVER" in
754 echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITION
757 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
760 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
766 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
775 case "$SCSI_DRIVER" in
777 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
780 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
786 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@0:$PARTITION
789 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@1:$PARTITION
792 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
800 case "$SCSI_DRIVER" in
802 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
805 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
811 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@0:$PARTITION
814 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@1:$PARTITION
817 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
821 AAPL,Gossamer*|AAPL,PowerMac\ G3*)
825 case "$SCSI_DRIVER" in
827 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
830 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
836 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@0:$PARTITION
839 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@1:$PARTITION
842 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@0:$PARTITION
845 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@1:$PARTITION
848 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
853 if [ -f /proc/device-tree/$ALIASES/ata0 ] ; then
858 if [ -f /proc/device-tree/$ALIASES/ata1 ] ; then
866 case "$SCSI_DRIVER" in
868 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
871 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
877 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@0:$PARTITION
880 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@1:$PARTITION
883 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@0:$PARTITION
886 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@1:$PARTITION
889 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
898 case "$SCSI_DRIVER" in
900 echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITON
903 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
906 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
912 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@0:$PARTITION
915 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@1:$PARTITION
918 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@0:$PARTITION
921 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@1:$PARTITION
924 echo $(cat /proc/device-tree/$ALIASES/ata2):$PARTITION
927 echo $(cat /proc/device-tree/$ALIASES/ata3):$PARTITION
930 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
936 echo 1>&2 "$PRG: This machine is not supported: $MODEL"
945 read_attr devspec /sys/class/net/$DEVICE/device
950 local hfnum=${DEVICE##hf}
953 if [[ $hfnum = "0" || $hfnum = "2" ]]; then
954 hfpath=$(find /proc/device-tree -name hfi-ethernet* | sort | head -n 1)
955 elif [[ $hfnum = "1" || $hfnum = "3" ]]; then
956 hfpath=$(find /proc/device-tree -name hfi-ethernet* | sort | tail -n 1)
958 echo 1>&2 "$PRG: Unsupported device: $DEVICE"
962 hfpath=${hfpath##/proc/device-tree}
966 ## find OpenFirmware device path for IBM CHRP hardware (scsi only)
972 ## use common scsiinfo function to get info we need.
975 ## now we have the data for /@$DEVID:$PARTITION
976 ## find the actual OF path.
977 scsi_ofpath || return 1
980 eth_ofpath || return 1
983 hfi_ofpath || return 1
986 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
993 ## If we get lame devfs name, we need to make it foad
997 /dev/ide/*|/dev/scsi/*|/dev/discs/*)
1006 ## convert devfs names into normal short ones, written by Tom Rini.
1009 ## get partition number, if any
1010 local PARTNUM="${1##*[a-z]}"
1011 ## Find the bus type.
1012 local TYPE="$(v=${1#/dev/} ; echo ${v%/host*})"
1013 ## Find the host number.
1014 local HOST="$(v=${1#/dev/*/host} ; echo ${v%/bus*})"
1015 ## Find the bus number.
1016 local BUS="$(v=${1#/dev/*/bus} ; echo ${v%/tar*})"
1018 local TARGET="$(v=${1#/dev/*/target} ; echo ${v%/lun*})"
1044 echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
1047 local DEV="${DEV}${PARTNUM}"
1052 local LUN="$(v=${1#/dev/*/lun} ; echo ${v%/*})"
1054 ## In this case, we need to figure out what number our device is
1057 ## copy scsi file into a variable removing "Attached Devices"
1058 ## which is the first line. this avoids a lot of
1059 ## [incmopatible] crap later, and improves readability.
1061 ## find number of lines once and recycle that number, to save
1062 ## some time (linecount is a bit slow). subtract one line
1063 ## to scrap Attached Devices:
1065 local SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
1066 local PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
1068 for i in $(smallseq $(($SCSILINES / 3))) ; do
1070 ## put every scsi device into one single line
1071 local DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
1072 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVINFO=$DEVINFO"
1074 ## cut the type field, expect "Direct-Access" later.
1075 local DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
1076 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVTYPE=$DEVTYPE"
1078 if [ "$DEVTYPE" = "Direct-Access" ] || [ "$DEVTYPE" = "Direct-Access-RBC" ] ; then
1079 ## Lets find out some more information
1080 ## get the device id.
1081 local DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
1082 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVID=$DEVID"
1084 ## get the device lun.
1085 local DEVLUN="$(v=$(echo ${DEVINFO##*Lun: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
1086 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVLUN=$DEVLUN"
1088 ## get the device channel.
1089 local DEVCHAN="$(v=$(echo ${DEVINFO##*Channel: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
1090 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCHAN=$DEVCHAN"
1092 ## get the scsi host id.
1093 local DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
1094 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVHOST=$DEVHOST"
1096 local DEVCOUNT="$(($DEVCOUNT + 1))"
1097 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCOUNT=$DEVCOUNT"
1098 if [ "$DEVHOST" = "$HOST" -a "$DEVCHAN" = "$BUS" -a \
1099 "$DEVID" = "$TARGET" -a "$DEVLUN" = "$LUN" ] ; then
1100 local DEV="sd$(smalltr $DEVCOUNT)${PARTNUM}"
1106 echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
1110 echo 1>&2 "$PRG: Unknown bus $TYPE"
1114 ## we should never get here
1118 ## make sure that find, head and tail can be found. otherwise the
1119 ## script will silently give bogus paths. these are the only /usr/*
1120 ## utilities this script depends on.
1123 if command -v find > /dev/null 2>&1 ; then
1124 [ -x `command -v find` ] || FAIL=1 ; else FAIL=1 ; fi
1125 if command -v head > /dev/null 2>&1 ; then
1126 [ -x `command -v head` ] || FAIL=1 ; else FAIL=1 ; fi
1127 if command -v tail > /dev/null 2>&1 ; then
1128 [ -x `command -v tail` ] || FAIL=1 ; else FAIL=1 ; fi
1130 if [ "$FAIL" = 1 ] ; then
1131 echo 1>&2 "$PRG: \`find', \`head', or \`tail' could not be found, aborting."
1138 ## parse command line switches.
1139 if [ $# != 0 ] ; then
1155 echo 1>&2 "$PRG: unrecognized option \`$1'"
1156 echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
1160 echo 1>&2 "$PRG: You must specify a filename"
1161 echo 1>&2 "Try \`$PRG --help' for more information."
1171 echo 1>&2 "$PRG: You must specify a /dev device"
1172 echo 1>&2 "Try \`$PRG --help' for more information."
1176 ## check that we are running on a GNU/Linux system, OSX/BSD does not
1177 ## have the same /proc stuff
1178 if [ `uname -s` != Linux ] ; then
1179 echo 1>&2 "$PRG: This utility will only work with GNU/Linux"
1183 ## check for ppc, i think uname -m is safe for this...
1184 if [ `uname -m` != ppc -a `uname -m` != ppc64 ] ; then
1185 echo 1>&2 "$PRG: This utility will only work on PowerPC hardware"
1189 ## ofpath cannot live without procfs
1190 if [ ! -f /proc/uptime ] ; then
1191 echo 1>&2 "$PRG: This utility requires the /proc filesystem"
1195 ## check for retarded devfs names and tell them to foad.
1196 if ckdevfs "$device" ; then
1197 device="$(fixdevfs $device)" || exit 1
1200 ## check for newworld mac. use cat hack due to /proc wierdness.
1201 if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = NewWorld ] ; then
1203 elif [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = OldWorld ] ; then
1205 elif (cat /proc/cpuinfo 2>/dev/null | grep ^motherboard | grep -q AAPL) ; then
1207 elif (cat /proc/cpuinfo 2> /dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
1209 elif (cat /proc/cpuinfo 2>/dev/null | grep ^machine | grep -q 'CHRP Pegasos') ; then
1212 echo 1>&2 "$PRG: This machine is not yet supported"
1216 ## make sure /proc/device-tree exists
1217 if [ ! -d /proc/device-tree ] ; then
1218 echo 1>&2 "$PRG: /proc/device-tree does not exist"
1219 echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_PROC_DEVICETREE=y"
1223 ## make sure we have what we need.
1224 checkutils || exit 1
1226 ## get the base device node and scrap /dev/ ie /dev/hda2 -> hda
1227 DEVICE="${device##*/}"
1228 DEVNODE="${DEVICE%%[0-9]*}"
1229 PARTITION="${DEVICE##*[a-z]}"
1231 ## use appropriate search for right sub arch.
1233 # Pegasos OF seems to be NewWorld-ish enough to cope with this.