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