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 echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
471 ## generic function that can find OF device paths for scsi devices,
472 ## must be run after scsiinfo().
475 case "$SCSI_DRIVER" in
477 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
478 lgrep "$i" "^ADPT" "^pci900[45]" "^pciclass,01000" ; done)"
479 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
480 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
483 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
484 lgrep "$i" "^Symbios" "^pci1000" "^pciclass,01000" ; done)"
485 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
486 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
489 HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
490 lgrep "$i" "mesh" ; done)"
491 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
492 echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
495 #Not all G5 device trees have a compatible "k2-sata" node
496 #per channel use parent
497 HOST_LIST="$(for i in `find /proc/device-tree -name compatible ` ; do
498 lgrep "$i" "k2-s-ata" ; done | sort)"
499 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
501 while [ "$DEVICE_PATH" = "" ] ; do
502 SCSI_HOSTNUMBER=`expr $SCSI_HOSTNUMBER - 1`
503 let "K2_DEVICE_ID += 1"
504 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
506 echo "${DEVICE_PATH##*device-tree}/k2-sata@$K2_DEVICE_ID/disk@0:$PARTITION"
509 HOST_LIST="$(for i in `find /proc/device-tree -name name | grep usb` ; do
510 lgrep "$i" "disk" ; done)"
511 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
512 echo "${DEVICE_PATH##*device-tree}:$PARTITION"
515 # sbp-2 driver may not have a dir in /proc/scsi
516 HOST_LIST="$(for i in `find /proc/device-tree -name name` ; do
517 lgrep "$i" "sbp-2" ; done)"
518 if [ "$HOST_LIST" = "" ] ; then
521 if [ "$SCSI_HOSTNUMBER" = "" ] ; then
524 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
525 echo "${DEVICE_PATH##*device-tree}/disk@0:$PARTITION"
529 echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
538 if [ ! -L "/proc/ide/$DEVNODE" ] ; then
539 echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
543 local IDEBUS="$(v=`readlink /proc/ide/$DEVNODE` ; echo ${v%%/*} )"
544 if [ -z "$IDEBUS" ] ; then
545 echo 1>&2 "$PRG: BUG: IDEBUS == NULL"
549 case "$(uname -r)" in
550 2.5.*|2.6.0*|2.6.1|2.6.1-*|2.6.2|2.6.2-*)
551 echo 1>&2 "$PRG: Linux kernel `uname -r` is not supported"
555 if ! (grep -q '.* .* sysfs ' /proc/mounts 2> /dev/null) ; then
556 echo 1>&2 "$PRG: sysfs must be mounted for ofpath to support this system"
559 local SYS="$(m=`grep '.* .* sysfs ' /proc/mounts | head -n 1` ; echo `d=${m#* };echo ${d%% *}`)"
560 if [ -z "$SYS" -o ! -d "$SYS" ] ; then
561 echo 1>&2 "$PRG: Unable to determine sysfs mountpoint"
564 local OF1275IDE="${SYS}/block/${DEVNODE}/device/../../devspec"
567 local OF1275IDE="/proc/ide/$IDEBUS/devspec"
571 if [ ! -f "$OF1275IDE" ] ; then
572 case "$(cat /proc/device-tree/model)" in
573 PowerMac3*|PowerMac4*|PowerMac5*|PowerMac6*|PowerMac7*|RackMac*)
574 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
575 if [ -z "$CDROM" ] ; then
576 echo 1>&2 "$PRG: WARNING: Your kernel is too old for proper support, device may be innaccurate."
577 echo "ultra2:$PARTITION"
583 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
584 if [ -z "$CDROM" ] ; then
585 if [ "$DEVNODE" = hda ] ; then
588 echo "ultra1:$PARTITION"
596 local DEVSPEC="$(cat $OF1275IDE)"
597 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVSPEC=$DEVSPEC"
598 if [ -z "$DEVSPEC" ] ; then
599 echo 1>&2 "$PRG: KERNEL BUG: $OF1275IDE exists, but is empty"
603 if [ ! -f "/proc/ide/${IDEBUS}/channel" ] ; then
604 echo 1>&2 "$PRG: KERNEL BUG: /proc/ide/${IDEBUS}/channel does not exist"
608 case "$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)" in
610 local MASTER="/disk@0"
611 local SLAVE="/disk@1"
614 local MASTER="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@0"
615 local SLAVE="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@1"
617 scsi) ## some lame controllers pretend they are scsi, hopefully all kludges are created equal.
618 local MASTER="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 0))"
619 local SLAVE="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 1))"
622 local MASTER="/disk@$(cat /proc/ide/${IDEBUS}/channel),0"
623 local SLAVE="/disk@$(cat /proc/ide/${IDEBUS}/channel),1"
626 echo 1>&2 "$PRG: Unsupported IDE device type: \"$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)\""
632 hda|hdc|hde|hdg|hdi|hdk|hdm|hdo)
633 echo "${DEVSPEC}${MASTER}:$PARTITION"
636 hdb|hdd|hdf|hdh|hdj|hdl|hdn|hdp)
637 echo "${DEVSPEC}${SLAVE}:$PARTITION"
641 echo 1>&2 "$PRG: /dev/$DEVNODE is not supported"
648 ## figure out the OpenFirmware device path for newworld macs.
649 ## sd* scsi disks , hd* ide disks.
654 ## use common scsiinfo function to get info we need.
657 ## now we have the data for /@$DEVID:$PARTITION
658 ## find the actual OF path.
659 scsi_ofpath || return 1
662 ide_ofpath || return 1
665 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
674 ## for some reason 2.4 kernels put OF aliases in aliases@0/ instead of plain aliases/
675 if [ -d "/proc/device-tree/aliases" ] ; then
676 local ALIASES="aliases"
677 elif [ -d "/proc/device-tree/aliases@0" ] ; then
678 local ALIASES="aliases@0"
680 echo 1>&2 "$PRG: Cannot find OpenFirmware aliases directory in /proc/device-tree/"
684 local MODEL="$(cat /proc/device-tree/compatible)"
685 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: Oldworld subarch: $MODEL"
688 AAPL,7300*|AAPL,7500*|AAPL,8500*|AAPL,9500*|AAPL,\?\?\?\?*)
692 case "$SCSI_DRIVER" in
694 echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITION
697 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
700 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
706 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
715 case "$SCSI_DRIVER" in
717 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
720 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
726 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@0:$PARTITION
729 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@1:$PARTITION
732 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
740 case "$SCSI_DRIVER" in
742 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
745 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
751 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@0:$PARTITION
754 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@1:$PARTITION
757 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
761 AAPL,Gossamer*|AAPL,PowerMac\ G3*)
765 case "$SCSI_DRIVER" in
767 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
770 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
776 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@0:$PARTITION
779 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@1:$PARTITION
782 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@0:$PARTITION
785 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@1:$PARTITION
788 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
793 if [ -f /proc/device-tree/$ALIASES/ata0 ] ; then
798 if [ -f /proc/device-tree/$ALIASES/ata1 ] ; then
806 case "$SCSI_DRIVER" in
808 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
811 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
817 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@0:$PARTITION
820 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@1:$PARTITION
823 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@0:$PARTITION
826 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@1:$PARTITION
829 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
838 case "$SCSI_DRIVER" in
840 echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITON
843 echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
846 echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
852 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@0:$PARTITION
855 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@1:$PARTITION
858 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@0:$PARTITION
861 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@1:$PARTITION
864 echo $(cat /proc/device-tree/$ALIASES/ata2):$PARTITION
867 echo $(cat /proc/device-tree/$ALIASES/ata3):$PARTITION
870 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
876 echo 1>&2 "$PRG: This machine is not supported: $MODEL"
883 ## find OpenFirmware device path for IBM CHRP hardware (scsi only)
889 ## use common scsiinfo function to get info we need.
892 ## now we have the data for /@$DEVID:$PARTITION
893 ## find the actual OF path.
894 scsi_ofpath || return 1
897 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
904 ## If we get lame devfs name, we need to make it foad
908 /dev/ide/*|/dev/scsi/*|/dev/discs/*)
917 ## convert devfs names into normal short ones, written by Tom Rini.
920 ## get partition number, if any
921 local PARTNUM="${1##*[a-z]}"
922 ## Find the bus type.
923 local TYPE="$(v=${1#/dev/} ; echo ${v%/host*})"
924 ## Find the host number.
925 local HOST="$(v=${1#/dev/*/host} ; echo ${v%/bus*})"
926 ## Find the bus number.
927 local BUS="$(v=${1#/dev/*/bus} ; echo ${v%/tar*})"
929 local TARGET="$(v=${1#/dev/*/target} ; echo ${v%/lun*})"
955 echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
958 local DEV="${DEV}${PARTNUM}"
963 local LUN="$(v=${1#/dev/*/lun} ; echo ${v%/*})"
965 ## In this case, we need to figure out what number our device is
968 ## copy scsi file into a variable removing "Attached Devices"
969 ## which is the first line. this avoids a lot of
970 ## [incmopatible] crap later, and improves readability.
972 ## find number of lines once and recycle that number, to save
973 ## some time (linecount is a bit slow). subtract one line
974 ## to scrap Attached Devices:
976 local SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
977 local PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
979 for i in $(smallseq $(($SCSILINES / 3))) ; do
981 ## put every scsi device into one single line
982 local DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
983 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVINFO=$DEVINFO"
985 ## cut the type field, expect "Direct-Access" later.
986 local DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
987 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVTYPE=$DEVTYPE"
989 if [ "$DEVTYPE" = "Direct-Access" ] || [ "$DEVTYPE" = "Direct-Access-RBC" ] ; then
990 ## Lets find out some more information
991 ## get the device id.
992 local DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
993 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVID=$DEVID"
995 ## get the device lun.
996 local DEVLUN="$(v=$(echo ${DEVINFO##*Lun: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
997 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVLUN=$DEVLUN"
999 ## get the device channel.
1000 local DEVCHAN="$(v=$(echo ${DEVINFO##*Channel: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
1001 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCHAN=$DEVCHAN"
1003 ## get the scsi host id.
1004 local DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
1005 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVHOST=$DEVHOST"
1007 local DEVCOUNT="$(($DEVCOUNT + 1))"
1008 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCOUNT=$DEVCOUNT"
1009 if [ "$DEVHOST" = "$HOST" -a "$DEVCHAN" = "$BUS" -a \
1010 "$DEVID" = "$TARGET" -a "$DEVLUN" = "$LUN" ] ; then
1011 local DEV="sd$(smalltr $DEVCOUNT)${PARTNUM}"
1017 echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
1021 echo 1>&2 "$PRG: Unknown bus $TYPE"
1025 ## we should never get here
1029 ## make sure that find, head and tail can be found. otherwise the
1030 ## script will silently give bogus paths. these are the only /usr/*
1031 ## utilities this script depends on.
1034 if command -v find > /dev/null 2>&1 ; then
1035 [ -x `command -v find` ] || FAIL=1 ; else FAIL=1 ; fi
1036 if command -v head > /dev/null 2>&1 ; then
1037 [ -x `command -v head` ] || FAIL=1 ; else FAIL=1 ; fi
1038 if command -v tail > /dev/null 2>&1 ; then
1039 [ -x `command -v tail` ] || FAIL=1 ; else FAIL=1 ; fi
1041 if [ "$FAIL" = 1 ] ; then
1042 echo 1>&2 "$PRG: \`find', \`head', or \`tail' could not be found, aborting."
1049 ## parse command line switches.
1050 if [ $# != 0 ] ; then
1066 echo 1>&2 "$PRG: unrecognized option \`$1'"
1067 echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
1071 echo 1>&2 "$PRG: You must specify a filename"
1072 echo 1>&2 "Try \`$PRG --help' for more information."
1082 echo 1>&2 "$PRG: You must specify a /dev device"
1083 echo 1>&2 "Try \`$PRG --help' for more information."
1087 ## check that FILE is a block device and exists.
1088 if [ ! -e "$device" ] ; then
1089 echo 1>&2 "$PRG: $device: No such file or directory"
1091 elif [ ! -b "$device" ] ; then
1092 echo 1>&2 "$PRG: $device is not a block device"
1096 ## check that we are running on a GNU/Linux system, OSX/BSD does not
1097 ## have the same /proc stuff
1098 if [ `uname -s` != Linux ] ; then
1099 echo 1>&2 "$PRG: This utility will only work with GNU/Linux"
1103 ## check for ppc, i think uname -m is safe for this...
1104 if [ `uname -m` != ppc -a `uname -m` != ppc64 ] ; then
1105 echo 1>&2 "$PRG: This utility will only work on PowerPC hardware"
1109 ## ofpath cannot live without procfs
1110 if [ ! -f /proc/uptime ] ; then
1111 echo 1>&2 "$PRG: This utility requires the /proc filesystem"
1115 ## check for retarded devfs names and tell them to foad.
1116 if ckdevfs "$device" ; then
1117 device="$(fixdevfs $device)" || exit 1
1120 ## check for newworld mac. use cat hack due to /proc wierdness.
1121 if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = NewWorld ] ; then
1123 elif [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = OldWorld ] ; then
1125 elif (cat /proc/cpuinfo 2>/dev/null | grep ^motherboard | grep -q AAPL) ; then
1127 elif (cat /proc/cpuinfo 2> /dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
1129 elif (cat /proc/cpuinfo 2>/dev/null | grep ^machine | grep -q 'CHRP Pegasos') ; then
1132 echo 1>&2 "$PRG: This machine is not yet supported"
1136 ## make sure /proc/device-tree exists
1137 if [ ! -d /proc/device-tree ] ; then
1138 echo 1>&2 "$PRG: /proc/device-tree does not exist"
1139 echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_PROC_DEVICETREE=y"
1143 ## make sure we have what we need.
1144 checkutils || exit 1
1146 ## get the base device node and scrap /dev/ ie /dev/hda2 -> hda
1147 DEVICE="${device##*/}"
1148 DEVNODE="${DEVICE%%[0-9]*}"
1149 PARTITION="${DEVICE##*[a-z]}"
1151 ## use appropriate search for right sub arch.
1153 # Pegasos OF seems to be NewWorld-ish enough to cope with this.