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