]> git.ozlabs.org Git - yaboot.git/blob - ybin/ofpath
Fix ofpath for `scsi' ide controllers
[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.5-UNRELEASED_UNSUPPORTED
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)
301             HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
302                         lgrep "$i" "k2-s-ata" ; done)"
303             DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
304             echo "${DEVICE_PATH##*device-tree}/k2-sata@$DEVICE_ID/disk@0:$PARTITION"
305             ;;
306         *)
307             echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
308             return 1
309             ;;
310     esac
311     return 0
312 }
313
314 ide_ofpath()
315 {
316     if [ ! -L "/proc/ide/$DEVNODE" ] ; then
317         echo 2>&1 "$PRG: /dev/$DEVNODE: Device not configured"
318         return 1
319     fi
320
321     local IDEBUS="$(v=`readlink /proc/ide/$DEVNODE` ; echo ${v%%/*} )"
322     if [ -z "$IDEBUS" ] ; then
323         echo 1>&2 "$PRG: BUG: IDEBUS == NULL"
324         return 1
325     fi
326     local OF1275IDE="/proc/ide/$IDEBUS/devspec"
327
328     if [ ! -f "$OF1275IDE" ] ; then
329         case "$(cat /proc/device-tree/model)" in
330             "PowerMac3,6")
331                 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
332                 if [ -z "$CDROM" ] ; then
333                     echo 1>&2 "$PRG: WARNING: Your kernel is too old for proper support, device may be innaccurate."
334                     echo "ultra2:$PARTITION"
335                 else
336                     echo "cd:$PARTITION"
337                 fi
338                 ;;
339             *)
340                 local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
341                 if [ -z "$CDROM" ] ; then
342                     if [ "$DEVNODE" = hda ] ; then
343                         echo "hd:$PARTITION"
344                     else
345                         echo "ultra1:$PARTITION"
346                     fi
347                 else
348                     echo "cd:$PARTITION"
349                 fi
350                 ;;
351         esac
352     else
353         local DEVSPEC="$(cat $OF1275IDE)"
354         [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVSPEC=$DEVSPEC"
355         if [ -z "$DEVSPEC" ] ; then
356             echo 1>&2 "$PRG: KERNEL BUG: $OF1275IDE exists, but is empty"
357             return 1
358         fi
359
360         if [ ! -f "/proc/ide/${IDEBUS}/channel" ] ; then
361             echo 1>&2 "$PRG: KERNEL BUG: /proc/ide/${IDEBUS}/channel does not exist"
362             return 1
363         fi
364
365         case "$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)" in
366             ide|ata)
367                 local MASTER="/disk@0"
368                 local SLAVE="/disk@1"
369                 ;;
370             pci-ide|pci-ata)
371                 local MASTER="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@0"
372                 local SLAVE="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@1"
373                 ;;
374             scsi) ## some lame controllers pretend they are scsi, hopefully all kludges are created equal.
375                 local MASTER="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 0))"
376                 local SLAVE="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 1))"
377                 ;;
378             *)
379                 echo 2>&1 "$PRG: Unsupported IDE device type: \"$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)\""
380                 return 1
381                 ;;
382         esac
383
384         case "$DEVNODE" in
385             hda|hdc|hde|hdg|hdi|hdk|hdm|hdo)
386                 echo "${DEVSPEC}${MASTER}:$PARTITION"
387                 return 0
388                 ;;
389             hdb|hdd|hdf|hdh|hdj|hdl|hdn|hdp)
390                 echo "${DEVSPEC}${SLAVE}:$PARTITION"
391                 return 0
392                 ;;
393             *)
394                 echo 1>&2 "$PRG: /dev/$DEVNODE is not supported"
395                 return 1
396                 ;;
397         esac
398     fi
399 }
400
401 ## figure out the OpenFirmware device path for newworld macs.
402 ## sd* scsi disks , hd* ide disks.
403 newworld()
404 {
405     case "$DEVNODE" in
406         sd*)
407             if ls -l /proc/device-tree | grep -q ^lr ; then
408                 true
409             else
410                 echo 1>&2 "$PRG: /proc/device-tree is broken.  Do not use BootX to boot, use yaboot."
411                 echo 1>&2 "$PRG: The yaboot HOWTO can be found here: http://www.alaska.net/~erbenson/doc"
412                 return 1
413             fi
414
415             ## use common scsiinfo function to get info we need.
416             scsiinfo || return 1
417
418             ## now we have the data for /@$DEVID:$PARTITION
419             ## find the actual OF path. 
420             scsi_ofpath || return 1
421             ;;
422         hd*)
423             ide_ofpath || return 1
424             ;;
425         *)
426             echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
427             return 1
428             ;;
429     esac
430     return 0
431 }
432
433 oldworld()
434 {
435     ## for some reason 2.4 kernels put OF aliases in aliases@0/ instead of plain aliases/
436     if [ -d "/proc/device-tree/aliases" ] ; then
437         local ALIASES="aliases"
438     elif [ -d "/proc/device-tree/aliases@0" ] ; then
439         local ALIASES="aliases@0"
440     else
441         echo 1>&2 "$PRG: Cannot find OpenFirmware aliases directory in /proc/device-tree/"
442         return 1
443     fi
444
445     local MODEL="$(cat /proc/device-tree/compatible)"
446     [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: Oldworld subarch: $MODEL"
447
448     case "$MODEL" in
449         AAPL,7300*|AAPL,7500*|AAPL,8500*|AAPL,9500*|AAPL,\?\?\?\?*)
450             case "$DEVNODE" in
451                 sd*)
452                 scsiinfo || return 1
453                 case "$SCSI_DRIVER" in
454                     mesh)
455                     echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITION
456                     ;;
457                     53c94)
458                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
459                     ;;
460                     *)
461                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
462                     return 1
463                     ;;
464                 esac
465                 ;;
466                 *)
467                 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
468                 return 1
469                 ;;
470             esac
471             ;;
472         AAPL,e407*)
473             case "$DEVNODE" in
474                 sd*)
475                 scsiinfo || return 1
476                 case "$SCSI_DRIVER" in
477                     mesh)
478                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
479                     ;;
480                     *)
481                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
482                     return 1
483                     ;;
484                 esac
485                 ;;
486                 hda*)
487                 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@0:$PARTITION
488                 ;;
489                 hdb*)
490                 echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@1:$PARTITION
491                 ;;
492                 hd*)
493                 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
494                 ;;
495             esac
496             ;;
497         AAPL,e826*)
498             case "$DEVNODE" in
499                 sd*)
500                 scsiinfo || return 1
501                 case "$SCSI_DRIVER" in
502                     mesh)
503                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
504                     ;;
505                     *)
506                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
507                     return 1
508                     ;;
509                 esac
510                 ;;
511                 hda*)
512                 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@0:$PARTITION
513                 ;;
514                 hdb*)
515                 echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@1:$PARTITION
516                 ;;
517                 hd*)
518                 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
519                 ;;
520             esac
521             ;;
522         AAPL,Gossamer*|AAPL,PowerMac\ G3*)
523             case "$DEVNODE" in
524                 sd*)
525                 scsiinfo || return 1
526                 case "$SCSI_DRIVER" in
527                     mesh)
528                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
529                     ;;
530                     *)
531                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
532                     return 1
533                     ;;
534                 esac
535                 ;;
536                 hda*)
537                 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@0:$PARTITION
538                 ;;
539                 hdb*)
540                 echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@1:$PARTITION
541                 ;;
542                 hdc*)
543                 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@0:$PARTITION
544                 ;;
545                 hdd*)
546                 echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@1:$PARTITION
547                 ;;
548                 hd*)
549                 echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
550                 ;;
551             esac
552             ;;
553         AAPL,PowerBook1998*)
554             if [ -f  /proc/device-tree/$ALIASES/ata0 ] ; then
555                 local ATA0=ata0
556             else
557                 local ATA0=ide0
558             fi
559             if [ -f  /proc/device-tree/$ALIASES/ata1 ] ; then
560                 local ATA1=ata1
561             else
562                 local ATA1=bay-ata1
563             fi
564             case "$DEVNODE" in
565                 sd*)
566                 scsiinfo || return 1
567                 case "$SCSI_DRIVER" in
568                     mesh)
569                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
570                     ;;
571                     *)
572                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
573                     return 1
574                     ;;
575                 esac
576                 ;;
577                 hda*)
578                 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@0:$PARTITION
579                 ;;
580                 hdb*)
581                 echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@1:$PARTITION
582                 ;;
583                 hdc*)
584                 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@0:$PARTITION
585                 ;;
586                 hdd*)
587                 echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@1:$PARTITION
588                 ;;
589                 *)
590                 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
591                 return 1
592                 ;;
593             esac
594             ;;
595         AAPL,3400/2400*)
596             case "$DEVNODE" in
597                 sd*)
598                 scsiinfo || return 1
599                 case "$SCSI_DRIVER" in
600                     mesh)
601                     echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITON
602                     ;;
603                     53c94)
604                     echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
605                     ;;
606                     *)
607                     echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
608                     return 1
609                     ;;
610                 esac
611                 ;;
612                 hda*)
613                 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@0:$PARTITION
614                 ;;
615                 hdb*)
616                 echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@1:$PARTITION
617                 ;;
618                 hdc*)
619                 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@0:$PARTITION
620                 ;;
621                 hdd*)
622                 echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@1:$PARTITION
623                 ;;
624                 hde*)
625                 echo $(cat /proc/device-tree/$ALIASES/ata2):$PARTITION
626                 ;;
627                 hdf*)
628                 echo $(cat /proc/device-tree/$ALIASES/ata3):$PARTITION
629                 ;;
630                 *)
631                 echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
632                 return 1
633                 ;;
634             esac
635             ;;
636         *)
637             echo 1>&2 "$PRG: This machine is not supported: $MODEL"
638             return 1
639             ;;
640     esac
641     return 0
642 }
643
644 ## find OpenFirmware device path for IBM CHRP hardware (scsi only)
645 chrp()
646 {
647     case "$DEVNODE" in
648         sd*)
649             if ls -l /proc/device-tree | grep -q ^lr ; then
650                 true
651             else
652                 echo 1>&2 "$PRG: /proc/device-tree is broken."
653                 return 1
654             fi
655
656             ## use common scsiinfo function to get info we need.
657             scsiinfo || return 1
658
659             ## now we have the data for /@$DEVID:$PARTITION
660             ## find the actual OF path. 
661             scsi_ofpath || return 1
662             ;;
663         *)
664             echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
665             return 1
666             ;;
667     esac
668     return 0
669 }
670
671 ## If we get lame devfs name, we need to make it foad
672 ckdevfs()
673 {
674     case "$1" in
675         /dev/ide/*|/dev/scsi/*|/dev/discs/*)
676         return 0
677         ;;
678         *)
679         return 1
680         ;;
681     esac
682 }
683
684 ## convert devfs names into normal short ones, written by Tom Rini.
685 fixdevfs()
686 {
687     ## get partition number, if any
688     local PARTNUM="${1##*[a-z]}"
689     ## Find the bus type.
690     local TYPE="$(v=${1#/dev/} ; echo ${v%/host*})"
691     ## Find the host number.
692     local HOST="$(v=${1#/dev/*/host} ; echo ${v%/bus*})"
693     ## Find the bus number.
694     local BUS="$(v=${1#/dev/*/bus} ; echo ${v%/tar*})"
695     ## Find the target.
696     local TARGET="$(v=${1#/dev/*/target} ; echo ${v%/lun*})"
697
698     case "$TYPE" in
699         ide)
700         case "$HOST" in
701             0)
702             case "$TARGET" in
703                 0)
704                 local DEV=hda
705                 ;;
706                 1)
707                 local DEV=hdb
708                 ;;
709             esac
710             ;;
711             1)
712             case "$TARGET" in
713                 0)
714                 local DEV=hdc
715                 ;;
716                 1)
717                 local DEV=hdd
718                 ;;
719             esac
720             ;;
721             *)
722                 echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
723                 return 1
724         esac
725         local DEV="${DEV}${PARTNUM}"
726         echo "/dev/$DEV"
727         return 0
728         ;;
729         scsi)
730         local LUN="$(v=${1#/dev/*/lun} ; echo ${v%/*})"
731
732         ## In this case, we need to figure out what number our device is
733         local DEVCOUNT=0
734
735         ## copy scsi file into a variable removing "Attached Devices"
736         ## which is the first line. this avoids a lot of
737         ## [incmopatible] crap later, and improves readability.
738
739         ## find number of lines once and recycle that number, to save
740         ## some time (linecount is a bit slow). subtract one line
741         ## to scrap Attached Devices:
742
743         local SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
744         local PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
745
746         for i in $(smallseq $(($SCSILINES / 3))) ; do
747
748             ## put every scsi device into one single line
749             local DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
750             [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVINFO=$DEVINFO"
751
752             ## cut the type field, expect "Direct-Access" later.
753             local DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
754             [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVTYPE=$DEVTYPE"
755
756             if [ "$DEVTYPE" = "Direct-Access" ] ; then
757                 ## Lets find out some more information
758                 ## get the device id.
759                 local DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
760                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVID=$DEVID"
761
762                 ## get the device lun.
763                 local DEVLUN="$(v=$(echo ${DEVINFO##*Lun: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
764                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVLUN=$DEVLUN"
765
766                 ## get the device channel.
767                 local DEVCHAN="$(v=$(echo ${DEVINFO##*Channel: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
768                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCHAN=$DEVCHAN"
769
770                 ## get the scsi host id.
771                 local DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
772                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVHOST=$DEVHOST"
773
774                 local DEVCOUNT="$(($DEVCOUNT + 1))"
775                 [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCOUNT=$DEVCOUNT"
776                 if [ "$DEVHOST" = "$HOST" -a "$DEVCHAN" = "$BUS" -a \
777                     "$DEVID" = "$TARGET" -a "$DEVLUN" = "$LUN" ] ; then
778                     local DEV="sd$(smalltr $DEVCOUNT)${PARTNUM}"
779                     echo "/dev/$DEV"
780                     return 0
781                 fi
782             fi
783         done
784         echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
785         return 1
786         ;;
787         *)
788         echo 1>&2 "$PRG: Unknown bus $TYPE"
789         return 1
790         ;;
791     esac
792     ## we should never get here
793     return 1
794 }
795
796 ## make sure that find, head and tail can be found.  otherwise the
797 ## script will silently give bogus paths.  these are the only /usr/*
798 ## utilities this script depends on.
799 checkutils()
800 {
801     if command -v find > /dev/null 2>&1 ; then
802         [ -x `command -v find` ] || FAIL=1 ; else FAIL=1 ; fi
803     if command -v head > /dev/null 2>&1 ; then
804         [ -x `command -v head` ] || FAIL=1 ; else FAIL=1 ; fi
805     if command -v tail > /dev/null 2>&1 ; then
806         [ -x `command -v tail` ] || FAIL=1 ; else FAIL=1 ; fi
807
808     if [ "$FAIL" = 1 ] ; then
809         echo 1>&2 "$PRG: \`find', \`head', or \`tail' could not be found, aborting."
810         return 1
811     else
812         return 0
813     fi
814 }
815
816 ## parse command line switches.
817 if [ $# != 0 ] ; then
818     while true ; do
819         case "$1" in
820             -V|--version)
821                 version
822                 exit 0
823                 ;;
824             -h|--help)
825                 usage
826                 exit 0
827                 ;;
828             --debug)
829                 DEBUG=1
830                 shift
831                 ;;
832             -*)
833                 echo 1>&2 "$PRG: unrecognized option \`$1'"
834                 echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
835                 exit 1
836                 ;;
837             "")
838                 echo 1>&2 "$PRG: You must specify a filename"
839                 echo 1>&2 "Try \`$PRG --help' for more information."
840                 exit 1
841                 ;;
842             *)
843                 device="$1"
844                 break
845                 ;;
846         esac
847     done
848 else
849     echo 1>&2 "$PRG: You must specify a /dev device"
850     echo 1>&2 "Try \`$PRG --help' for more information."
851     exit 1
852 fi
853
854 ## check that FILE is a block device and exists.
855 if [ ! -e "$device" ] ; then
856     echo 1>&2 "$PRG: $device: No such file or directory"
857     exit 1
858 elif [ ! -b "$device" ] ; then
859     echo 1>&2 "$PRG: $device is not a block device"
860     exit 1
861 fi
862
863 ## check that we are running on a GNU/Linux system, OSX/BSD does not
864 ## have the same /proc stuff
865 if [ `uname -s` != Linux ] ; then
866     echo 1>&2 "$PRG: This utility will only work with GNU/Linux"
867     exit 1
868 fi
869
870 ## check for ppc, i think uname -m is safe for this...
871 if [ `uname -m` != ppc ] ; then
872     echo 1>&2 "$PRG: This utility will only work on PowerPC hardware"
873     exit 1
874 fi
875
876 ## ofpath cannot live without procfs
877 if [ ! -f /proc/uptime ] ; then
878     echo 1>&2 "$PRG: This utility requires the /proc filesystem"
879     exit 1
880 fi
881
882 ## check for retarded devfs names and tell them to foad.
883 if ckdevfs "$device" ; then
884     device="$(fixdevfs $device)" || exit 1
885 fi
886
887 ## check for newworld mac. use cat hack due to /proc wierdness.
888 if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = NewWorld ] ; then
889     SUBARCH=NewWorld
890 elif [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = OldWorld ] ; then
891     SUBARCH=OldWorld
892 elif (cat /proc/cpuinfo 2>/dev/null | grep ^motherboard | grep -q AAPL) ; then
893     SUBARCH=OldWorld
894 elif (cat /proc/cpuinfo 2> /dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
895     SUBARCH=CHRP
896 else
897     echo 1>&2 "$PRG: This machine is not yet supported"
898     exit 1
899 fi
900
901 ## make sure /proc/device-tree exists
902 if [ ! -d /proc/device-tree ] ; then
903     echo 1>&2 "$PRG: /proc/device-tree does not exist"
904     echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_PROC_DEVICETREE=y"
905     exit 1
906 fi
907
908 ## make sure we have what we need.
909 checkutils || exit 1
910
911 ## get the base device node and scrap /dev/ ie /dev/hda2 -> hda
912 DEVICE="${device##*/}"
913 DEVNODE="${DEVICE%%[0-9]*}"
914 PARTITION="${DEVICE##*[a-z]}"
915
916 ## use appropriate search for right sub arch.
917 case "$SUBARCH" in
918     NewWorld)
919         newworld || exit 1
920         ;;
921     OldWorld)
922         oldworld || exit 1
923         ;;
924     CHRP)
925         chrp || exit 1
926         ;;
927 esac
928
929 exit 0