]> git.ozlabs.org Git - yaboot.git/blob - ybin/ofpath
Fix for not all device trees having k2-sata@N/compatible (from gentoo)
[yaboot.git] / ybin / ofpath
1 #! /bin/sh
2
3 ###############################################################################
4 ##
5 ## ofpath: determine OpenFirmware path from unix device node
6 ## Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
7 ##
8 ## Portions based on show_of_path.sh:
9 ##
10 ## Copyright (C) 2000 Olaf Hering <olh@suse.de>
11 ##
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.
16 ##
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.
21 ##
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.
25 ##
26 ###############################################################################
27
28 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
29 PRG="${0##*/}"
30 VERSION=1.0.7
31 DEBUG=0
32 export LC_COLLATE=C
33
34 ## --version output.
35 version()
36 {
37 echo \
38 "$PRG $VERSION
39 Written by Ethan Benson
40 Portions based on show_of_path.sh written by Olaf Hering
41
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."
46 }
47
48 ## --help output.
49 usage()
50 {
51 echo \
52 "Usage: $PRG [OPTION]... FILE
53 Find OpenFirmware device path from unix device node.
54
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"
58 }
59
60 ## a small seq replacement, seq is not present on boot/rescue floppies.
61 smallseq()
62 {
63     local v="$1"
64     local n=1
65     echo 1
66     while [ "$v" -gt 1 ] ; do
67         echo "$(($n + 1))"
68         local n="$(($n + 1))"
69         local v="$(($v - 1))"
70     done
71     return 0
72 }
73
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.
77 linecount()
78 {
79     if [ $# = 0 ] ; then
80         local file="$(cat)"
81         local v="$file"
82     else
83         local file="$(cat $1)"
84         local v="$file"
85     fi
86
87     if [ -z "$file" ] ; then
88         echo 0
89         return 0
90     fi
91
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)"
96             if [ $? = 0 ] ; then
97                 echo $lines
98                 unset lines
99                 return 0
100             fi
101         fi
102     fi
103
104     while true ; do
105         for i in `smallseq 145` ; do
106             local b="$(echo "$file" | tail -n $i)"
107             if [ "$v" = "$b" ] ; then
108                 echo "$i"
109                 break 2
110             fi
111         done
112     done
113     return 0
114 }
115
116 ## small tr replacment which handles a specific need of this script.
117 smalltr()
118 {
119     case "$1" in
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 ;;
126         16) echo p ;;
127     esac
128     return 0
129 }
130
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 ...
136 lgrep()
137 {
138     local f="$1"
139     shift
140     for i in "$@" ; do
141         echo "$(cat "$f")" | grep -q "$i" && echo "$f" && break
142     done
143     return 0
144 }
145
146 ## if readlink is missing use a kludge
147 if (command -v readlink > /dev/null 2>&1) ; then
148     true
149 else
150     readlink()
151     {
152         local SYMTARGET="$(v=`ls -l "$1" 2>/dev/null` ; echo ${v##*> })"
153         if [ -n "$SYMTARGET" ] ; then
154             echo "$SYMTARGET"
155             return 0
156         else
157             return 1
158         fi
159     }
160 fi
161
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
164 ## /compatible.
165 printhost()
166 {
167     case "$1" in
168         1)
169         echo "${2%/*}"
170         ;;
171         2)
172         echo "${3%/*}"
173         ;;
174         3)
175         echo "${4%/*}"
176         ;;
177         4)
178         echo "${5%/*}"
179         ;;
180     esac
181     return 0
182 }
183
184 ## this finds information we need on both newworld and oldworld macs.
185 ## mainly what scsi host a disk is attached to.
186 scsiinfo()
187 {
188     ## see if system has scsi at all
189     if [ ! -f /proc/scsi/scsi ] ; then
190         local kver="$(uname -r)"
191         case "$kver" in
192             2.5.*|2.6.*)
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"
197                     return 1
198                 fi
199                 ;;
200         esac
201         echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
202         return 1
203     fi
204
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
208
209     ## get last letter of device node, ie sda -> a
210     SUBNODE=${DEVNODE##*sd}
211
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"
215
216     DEVCOUNT=0
217
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.
221
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:
225
226     SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
227
228     if [ "$SUBDEV" -gt "$(cat /proc/scsi/scsi | grep Direct-Access | linecount)" ] ; then
229         echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
230         return 1
231     fi
232
233     PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
234
235     for i in $(smallseq $(($SCSILINES / 3))) ; do
236
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"
240
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"
244
245         ## get the device id.
246         DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
247         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVID=$DEVID"
248
249         ## get the scsi host id.
250         DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
251         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVHOST=$DEVHOST"
252
253         if [ "$DEVTYPE" = "Direct-Access" ] ; then
254             DEVCOUNT="$(($DEVCOUNT + 1))"
255             [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVCOUNT=$DEVCOUNT"
256             if [ "$SUBDEV" = "$DEVCOUNT" ] ; then
257                 DEVICE_HOST=$DEVHOST
258                 DEVICE_ID=$DEVID
259                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVICE_HOST=$DEVICE_HOST"
260                 break
261             fi
262         fi
263     done
264
265     ## figure out what the scsi driver is, it is /proc/scsi/dirname.
266     [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVICE_HOST=$DEVICE_HOST"
267     SCSI_DRIVER="$(x=`ls /proc/scsi/*/$DEVICE_HOST 2>/dev/null | cat` ; y=`echo ${x##*proc/scsi/}` ; echo ${y%%/*})"
268     [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SCSI_DRIVER=$SCSI_DRIVER"
269
270     ## figure out which host we found.
271     SCSI_HOSTNUMBER="$(v=`ls /proc/scsi/$SCSI_DRIVER/* 2>/dev/null | cat | grep -n "$DEVICE_HOST\>"` ; echo ${v%%:*})"
272     [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SCSI_HOSTNUMBER=$SCSI_HOSTNUMBER"
273
274     return 0
275 }
276
277 ## generic function that can find OF device paths for scsi devices,
278 ## must be run after scsiinfo().
279 scsi_ofpath()
280 {
281     case "$SCSI_DRIVER" in
282         aic7xxx)
283             HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
284                         lgrep "$i" "^ADPT" "^pci900[45]" "^pciclass,01000" ; done)"
285             DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
286             echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
287             ;;
288         sym53c8xx)
289             HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
290                         lgrep "$i" "^Symbios" "^pci1000" "^pciclass,01000" ; done)"
291             DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
292             echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
293             ;;
294         mesh)
295             HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
296                         lgrep "$i" "mesh" ; done)"
297             DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
298             echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
299             ;;
300         ata_k2|sata_svw)
301             #Not all G5 device trees have a compatible "k2-sata" node 
302             #per channel use parent
303             HOST_LIST="$(for i in `find /proc/device-tree -name compatible ` ; do
304                         lgrep "$i" "k2-s-ata" ; done | sort)"
305             DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
306             K2_DEVICE_ID=0
307             while [ "$DEVICE_PATH" = "" ] ; do
308                 SCSI_HOSTNUMBER=`expr $SCSI_HOSTNUMBER - 1`
309                 let "K2_DEVICE_ID += 1"
310                 DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
311             done
312             echo "${DEVICE_PATH##*device-tree}/k2-sata@$K2_DEVICE_ID/disk@0:$PARTITION"
313             ;;
314         sbp2)
315             HOST_LIST="$(for i in `find /proc/device-tree -name name` ; do
316                         lgrep "$i" "sbp-2" ; done)"
317             DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
318             echo "${DEVICE_PATH##*device-tree}/disk@0:$PARTITION"
319             ;;
320         *)
321             echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
322             return 1
323             ;;
324     esac
325     return 0
326 }
327
328 ide_ofpath()
329 {
330     if [ ! -L "/proc/ide/$DEVNODE" ] ; then
331         echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
332         return 1
333     fi
334
335     local IDEBUS="$(v=`readlink /proc/ide/$DEVNODE` ; echo ${v%%/*} )"
336     if [ -z "$IDEBUS" ] ; then
337         echo 1>&2 "$PRG: BUG: IDEBUS == NULL"
338         return 1
339     fi
340
341     case "$(uname -r)" in
342         2.5.*|2.6.0*|2.6.1|2.6.1-*|2.6.2|2.6.2-*)
343             echo 1>&2 "$PRG: Linux kernel `uname -r` is not supported"
344             return 1
345             ;;
346         2.6.*|2.7.*)
347             if ! (grep -q '.* .* sysfs ' /proc/mounts 2> /dev/null) ; then
348                 echo 1>&2 "$PRG: sysfs must be mounted for ofpath to support this system"
349                 return 1
350             fi
351             local SYS="$(m=`grep '.* .* sysfs ' /proc/mounts | head -n 1` ; echo `d=${m#* };echo ${d%% *}`)"
352             if [ -z "$SYS" -o ! -d "$SYS" ] ; then
353                 echo 1>&2 "$PRG: Unable to determine sysfs mountpoint"
354                 return 1
355             fi
356             local OF1275IDE="${SYS}/block/${DEVNODE}/device/../../devspec"
357             ;;
358         *)
359             local OF1275IDE="/proc/ide/$IDEBUS/devspec"
360             ;;
361     esac
362
363     if [ ! -f "$OF1275IDE" ] ; then
364         case "$(cat /proc/device-tree/model)" in
365             PowerMac3*|PowerMac4*|PowerMac5*|PowerMac6*|PowerMac7*|RackMac*)
366                 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
367                 if [ -z "$CDROM" ] ; then
368                     echo 1>&2 "$PRG: WARNING: Your kernel is too old for proper support, device may be innaccurate."
369                     echo "ultra2:$PARTITION"
370                 else
371                     echo "cd:$PARTITION"
372                 fi
373                 ;;
374             *)
375                 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
376                 if [ -z "$CDROM" ] ; then
377                     if [ "$DEVNODE" = hda ] ; then
378                         echo "hd:$PARTITION"
379                     else
380                         echo "ultra1:$PARTITION"
381                     fi
382                 else
383                     echo "cd:$PARTITION"
384                 fi
385                 ;;
386         esac
387     else
388         local DEVSPEC="$(cat $OF1275IDE)"
389         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVSPEC=$DEVSPEC"
390         if [ -z "$DEVSPEC" ] ; then
391             echo 1>&2 "$PRG: KERNEL BUG: $OF1275IDE exists, but is empty"
392             return 1
393         fi
394
395         if [ ! -f "/proc/ide/${IDEBUS}/channel" ] ; then
396             echo 1>&2 "$PRG: KERNEL BUG: /proc/ide/${IDEBUS}/channel does not exist"
397             return 1
398         fi
399
400         case "$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)" in
401             ide|ata)
402                 local MASTER="/disk@0"
403                 local SLAVE="/disk@1"
404                 ;;
405             pci-ide|pci-ata)
406                 local MASTER="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@0"
407                 local SLAVE="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@1"
408                 ;;
409             scsi) ## some lame controllers pretend they are scsi, hopefully all kludges are created equal.
410                 local MASTER="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 0))"
411                 local SLAVE="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 1))"
412                 ;;
413             spi)
414                 local MASTER="/disk@$(cat /proc/ide/${IDEBUS}/channel),0"
415                 local SLAVE="/disk@$(cat /proc/ide/${IDEBUS}/channel),1"
416                 ;;
417             *)
418                 echo 1>&2 "$PRG: Unsupported IDE device type: \"$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)\""
419                 return 1
420                 ;;
421         esac
422
423         case "$DEVNODE" in
424             hda|hdc|hde|hdg|hdi|hdk|hdm|hdo)
425                 echo "${DEVSPEC}${MASTER}:$PARTITION"
426                 return 0
427                 ;;
428             hdb|hdd|hdf|hdh|hdj|hdl|hdn|hdp)
429                 echo "${DEVSPEC}${SLAVE}:$PARTITION"
430                 return 0
431                 ;;
432             *)
433                 echo 1>&2 "$PRG: /dev/$DEVNODE is not supported"
434                 return 1
435                 ;;
436         esac
437     fi
438 }
439
440 ## figure out the OpenFirmware device path for newworld macs.
441 ## sd* scsi disks , hd* ide disks.
442 newworld()
443 {
444     case "$DEVNODE" in
445         sd*)
446             ## use common scsiinfo function to get info we need.
447             scsiinfo || return 1
448
449             ## now we have the data for /@$DEVID:$PARTITION
450             ## find the actual OF path.
451             scsi_ofpath || return 1
452             ;;
453         hd*)
454             ide_ofpath || return 1
455             ;;
456         *)
457             echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
458             return 1
459             ;;
460     esac
461     return 0
462 }
463
464 oldworld()
465 {
466     ## for some reason 2.4 kernels put OF aliases in aliases@0/ instead of plain aliases/
467     if [ -d "/proc/device-tree/aliases" ] ; then
468         local ALIASES="aliases"
469     elif [ -d "/proc/device-tree/aliases@0" ] ; then
470         local ALIASES="aliases@0"
471     else
472         echo 1>&2 "$PRG: Cannot find OpenFirmware aliases directory in /proc/device-tree/"
473         return 1
474     fi
475
476     local MODEL="$(cat /proc/device-tree/compatible)"
477     [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: Oldworld subarch: $MODEL"
478
479     case "$MODEL" in
480         AAPL,7300*|AAPL,7500*|AAPL,8500*|AAPL,9500*|AAPL,\?\?\?\?*)
481             case "$DEVNODE" in
482                 sd*)
483                 scsiinfo || return 1
484                 case "$SCSI_DRIVER" in
485                     mesh)
486                     echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITION
487                     ;;
488                     53c94)
489                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
490                     ;;
491                     *)
492                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
493                     return 1
494                     ;;
495                 esac
496                 ;;
497                 *)
498                 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
499                 return 1
500                 ;;
501             esac
502             ;;
503         AAPL,e407*)
504             case "$DEVNODE" in
505                 sd*)
506                 scsiinfo || return 1
507                 case "$SCSI_DRIVER" in
508                     mesh)
509                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
510                     ;;
511                     *)
512                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
513                     return 1
514                     ;;
515                 esac
516                 ;;
517                 hda*)
518                 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@0:$PARTITION
519                 ;;
520                 hdb*)
521                 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@1:$PARTITION
522                 ;;
523                 hd*)
524                 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
525                 ;;
526             esac
527             ;;
528         AAPL,e826*)
529             case "$DEVNODE" in
530                 sd*)
531                 scsiinfo || return 1
532                 case "$SCSI_DRIVER" in
533                     mesh)
534                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
535                     ;;
536                     *)
537                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
538                     return 1
539                     ;;
540                 esac
541                 ;;
542                 hda*)
543                 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@0:$PARTITION
544                 ;;
545                 hdb*)
546                 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@1:$PARTITION
547                 ;;
548                 hd*)
549                 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
550                 ;;
551             esac
552             ;;
553         AAPL,Gossamer*|AAPL,PowerMac\ G3*)
554             case "$DEVNODE" in
555                 sd*)
556                 scsiinfo || return 1
557                 case "$SCSI_DRIVER" in
558                     mesh)
559                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
560                     ;;
561                     *)
562                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
563                     return 1
564                     ;;
565                 esac
566                 ;;
567                 hda*)
568                 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@0:$PARTITION
569                 ;;
570                 hdb*)
571                 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@1:$PARTITION
572                 ;;
573                 hdc*)
574                 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@0:$PARTITION
575                 ;;
576                 hdd*)
577                 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@1:$PARTITION
578                 ;;
579                 hd*)
580                 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
581                 ;;
582             esac
583             ;;
584         AAPL,PowerBook1998*)
585             if [ -f  /proc/device-tree/$ALIASES/ata0 ] ; then
586                 local ATA0=ata0
587             else
588                 local ATA0=ide0
589             fi
590             if [ -f  /proc/device-tree/$ALIASES/ata1 ] ; then
591                 local ATA1=ata1
592             else
593                 local ATA1=bay-ata1
594             fi
595             case "$DEVNODE" in
596                 sd*)
597                 scsiinfo || return 1
598                 case "$SCSI_DRIVER" in
599                     mesh)
600                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
601                     ;;
602                     *)
603                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
604                     return 1
605                     ;;
606                 esac
607                 ;;
608                 hda*)
609                 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@0:$PARTITION
610                 ;;
611                 hdb*)
612                 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@1:$PARTITION
613                 ;;
614                 hdc*)
615                 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@0:$PARTITION
616                 ;;
617                 hdd*)
618                 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@1:$PARTITION
619                 ;;
620                 *)
621                 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
622                 return 1
623                 ;;
624             esac
625             ;;
626         AAPL,3400/2400*)
627             case "$DEVNODE" in
628                 sd*)
629                 scsiinfo || return 1
630                 case "$SCSI_DRIVER" in
631                     mesh)
632                     echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITON
633                     ;;
634                     53c94)
635                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
636                     ;;
637                     *)
638                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
639                     return 1
640                     ;;
641                 esac
642                 ;;
643                 hda*)
644                 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@0:$PARTITION
645                 ;;
646                 hdb*)
647                 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@1:$PARTITION
648                 ;;
649                 hdc*)
650                 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@0:$PARTITION
651                 ;;
652                 hdd*)
653                 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@1:$PARTITION
654                 ;;
655                 hde*)
656                 echo $(cat /proc/device-tree/$ALIASES/ata2):$PARTITION
657                 ;;
658                 hdf*)
659                 echo $(cat /proc/device-tree/$ALIASES/ata3):$PARTITION
660                 ;;
661                 *)
662                 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
663                 return 1
664                 ;;
665             esac
666             ;;
667         *)
668             echo 1>&2 "$PRG: This machine is not supported: $MODEL"
669             return 1
670             ;;
671     esac
672     return 0
673 }
674
675 ## find OpenFirmware device path for IBM CHRP hardware (scsi only)
676 chrp()
677 {
678     case "$DEVNODE" in
679         sd*)
680             if ls -l /proc/device-tree | grep -q ^d ; then
681                 true
682             else
683                 echo 1>&2 "$PRG: /proc/device-tree is broken."
684                 return 1
685             fi
686
687             ## use common scsiinfo function to get info we need.
688             scsiinfo || return 1
689
690             ## now we have the data for /@$DEVID:$PARTITION
691             ## find the actual OF path.
692             scsi_ofpath || return 1
693             ;;
694         *)
695             echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
696             return 1
697             ;;
698     esac
699     return 0
700 }
701
702 ## If we get lame devfs name, we need to make it foad
703 ckdevfs()
704 {
705     case "$1" in
706         /dev/ide/*|/dev/scsi/*|/dev/discs/*)
707         return 0
708         ;;
709         *)
710         return 1
711         ;;
712     esac
713 }
714
715 ## convert devfs names into normal short ones, written by Tom Rini.
716 fixdevfs()
717 {
718     ## get partition number, if any
719     local PARTNUM="${1##*[a-z]}"
720     ## Find the bus type.
721     local TYPE="$(v=${1#/dev/} ; echo ${v%/host*})"
722     ## Find the host number.
723     local HOST="$(v=${1#/dev/*/host} ; echo ${v%/bus*})"
724     ## Find the bus number.
725     local BUS="$(v=${1#/dev/*/bus} ; echo ${v%/tar*})"
726     ## Find the target.
727     local TARGET="$(v=${1#/dev/*/target} ; echo ${v%/lun*})"
728
729     case "$TYPE" in
730         ide)
731         case "$HOST" in
732             0)
733             case "$TARGET" in
734                 0)
735                 local DEV=hda
736                 ;;
737                 1)
738                 local DEV=hdb
739                 ;;
740             esac
741             ;;
742             1)
743             case "$TARGET" in
744                 0)
745                 local DEV=hdc
746                 ;;
747                 1)
748                 local DEV=hdd
749                 ;;
750             esac
751             ;;
752             *)
753                 echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
754                 return 1
755         esac
756         local DEV="${DEV}${PARTNUM}"
757         echo "/dev/$DEV"
758         return 0
759         ;;
760         scsi)
761         local LUN="$(v=${1#/dev/*/lun} ; echo ${v%/*})"
762
763         ## In this case, we need to figure out what number our device is
764         local DEVCOUNT=0
765
766         ## copy scsi file into a variable removing "Attached Devices"
767         ## which is the first line. this avoids a lot of
768         ## [incmopatible] crap later, and improves readability.
769
770         ## find number of lines once and recycle that number, to save
771         ## some time (linecount is a bit slow). subtract one line
772         ## to scrap Attached Devices:
773
774         local SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
775         local PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
776
777         for i in $(smallseq $(($SCSILINES / 3))) ; do
778
779             ## put every scsi device into one single line
780             local DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
781             [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVINFO=$DEVINFO"
782
783             ## cut the type field, expect "Direct-Access" later.
784             local DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
785             [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVTYPE=$DEVTYPE"
786
787             if [ "$DEVTYPE" = "Direct-Access" ] ; then
788                 ## Lets find out some more information
789                 ## get the device id.
790                 local DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
791                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVID=$DEVID"
792
793                 ## get the device lun.
794                 local DEVLUN="$(v=$(echo ${DEVINFO##*Lun: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
795                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVLUN=$DEVLUN"
796
797                 ## get the device channel.
798                 local DEVCHAN="$(v=$(echo ${DEVINFO##*Channel: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
799                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCHAN=$DEVCHAN"
800
801                 ## get the scsi host id.
802                 local DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
803                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVHOST=$DEVHOST"
804
805                 local DEVCOUNT="$(($DEVCOUNT + 1))"
806                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCOUNT=$DEVCOUNT"
807                 if [ "$DEVHOST" = "$HOST" -a "$DEVCHAN" = "$BUS" -a \
808                     "$DEVID" = "$TARGET" -a "$DEVLUN" = "$LUN" ] ; then
809                     local DEV="sd$(smalltr $DEVCOUNT)${PARTNUM}"
810                     echo "/dev/$DEV"
811                     return 0
812                 fi
813             fi
814         done
815         echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
816         return 1
817         ;;
818         *)
819         echo 1>&2 "$PRG: Unknown bus $TYPE"
820         return 1
821         ;;
822     esac
823     ## we should never get here
824     return 1
825 }
826
827 ## make sure that find, head and tail can be found.  otherwise the
828 ## script will silently give bogus paths.  these are the only /usr/*
829 ## utilities this script depends on.
830 checkutils()
831 {
832     if command -v find > /dev/null 2>&1 ; then
833         [ -x `command -v find` ] || FAIL=1 ; else FAIL=1 ; fi
834     if command -v head > /dev/null 2>&1 ; then
835         [ -x `command -v head` ] || FAIL=1 ; else FAIL=1 ; fi
836     if command -v tail > /dev/null 2>&1 ; then
837         [ -x `command -v tail` ] || FAIL=1 ; else FAIL=1 ; fi
838
839     if [ "$FAIL" = 1 ] ; then
840         echo 1>&2 "$PRG: \`find', \`head', or \`tail' could not be found, aborting."
841         return 1
842     else
843         return 0
844     fi
845 }
846
847 ## parse command line switches.
848 if [ $# != 0 ] ; then
849     while true ; do
850         case "$1" in
851             -V|--version)
852                 version
853                 exit 0
854                 ;;
855             -h|--help)
856                 usage
857                 exit 0
858                 ;;
859             --debug)
860                 DEBUG=1
861                 shift
862                 ;;
863             -*)
864                 echo 1>&2 "$PRG: unrecognized option \`$1'"
865                 echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
866                 exit 1
867                 ;;
868             "")
869                 echo 1>&2 "$PRG: You must specify a filename"
870                 echo 1>&2 "Try \`$PRG --help' for more information."
871                 exit 1
872                 ;;
873             *)
874                 device="$1"
875                 break
876                 ;;
877         esac
878     done
879 else
880     echo 1>&2 "$PRG: You must specify a /dev device"
881     echo 1>&2 "Try \`$PRG --help' for more information."
882     exit 1
883 fi
884
885 ## check that FILE is a block device and exists.
886 if [ ! -e "$device" ] ; then
887     echo 1>&2 "$PRG: $device: No such file or directory"
888     exit 1
889 elif [ ! -b "$device" ] ; then
890     echo 1>&2 "$PRG: $device is not a block device"
891     exit 1
892 fi
893
894 ## check that we are running on a GNU/Linux system, OSX/BSD does not
895 ## have the same /proc stuff
896 if [ `uname -s` != Linux ] ; then
897     echo 1>&2 "$PRG: This utility will only work with GNU/Linux"
898     exit 1
899 fi
900
901 ## check for ppc, i think uname -m is safe for this...
902 if [ `uname -m` != ppc -a `uname -m` != ppc64 ] ; then
903     echo 1>&2 "$PRG: This utility will only work on PowerPC hardware"
904     exit 1
905 fi
906
907 ## ofpath cannot live without procfs
908 if [ ! -f /proc/uptime ] ; then
909     echo 1>&2 "$PRG: This utility requires the /proc filesystem"
910     exit 1
911 fi
912
913 ## check for retarded devfs names and tell them to foad.
914 if ckdevfs "$device" ; then
915     device="$(fixdevfs $device)" || exit 1
916 fi
917
918 ## check for newworld mac. use cat hack due to /proc wierdness.
919 if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = NewWorld ] ; then
920     SUBARCH=NewWorld
921 elif [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = OldWorld ] ; then
922     SUBARCH=OldWorld
923 elif (cat /proc/cpuinfo 2>/dev/null | grep ^motherboard | grep -q AAPL) ; then
924     SUBARCH=OldWorld
925 elif (cat /proc/cpuinfo 2> /dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
926     SUBARCH=CHRP
927 elif (cat /proc/cpuinfo 2>/dev/null | grep ^machine | grep -q 'CHRP Pegasos') ; then
928     SUBARCH=Pegasos
929 else
930     echo 1>&2 "$PRG: This machine is not yet supported"
931     exit 1
932 fi
933
934 ## make sure /proc/device-tree exists
935 if [ ! -d /proc/device-tree ] ; then
936     echo 1>&2 "$PRG: /proc/device-tree does not exist"
937     echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_PROC_DEVICETREE=y"
938     exit 1
939 fi
940
941 ## make sure we have what we need.
942 checkutils || exit 1
943
944 ## get the base device node and scrap /dev/ ie /dev/hda2 -> hda
945 DEVICE="${device##*/}"
946 DEVNODE="${DEVICE%%[0-9]*}"
947 PARTITION="${DEVICE##*[a-z]}"
948
949 ## use appropriate search for right sub arch.
950 case "$SUBARCH" in
951     # Pegasos OF seems to be NewWorld-ish enough to cope with this.
952     NewWorld|Pegasos)
953         newworld || exit 1
954         ;;
955     OldWorld)
956         oldworld || exit 1
957         ;;
958     CHRP)
959         chrp || exit 1
960         ;;
961 esac
962
963 exit 0