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