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