File: ofpath

package info (click to toggle)
yaboot 1.3.16-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,508 kB
  • ctags: 3,009
  • sloc: ansic: 10,206; sh: 2,933; asm: 343; makefile: 241
file content (940 lines) | stat: -rwxr-xr-x 24,178 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
#! /bin/sh

###############################################################################
##
## ofpath: determine OpenFirmware path from unix device node
##
## Copyright (C) 2010, 2011 Milan Kupcevic
##
## Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
##
## Portions based on show_of_path.sh:
##
## Copyright (C) 2000 Olaf Hering <olh@suse.de>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##
###############################################################################

PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
PRG="${0##*/}"
VERSION=1.0.7+debian3
DEBUG=0
export LC_COLLATE=C

## --version output.
version()
{
echo \
"$PRG $VERSION
Written by Ethan Benson, portions rewritten by Milan Kupcevic
Portions based on show_of_path.sh written by Olaf Hering

Copyright (C) 2010, 2011 Milan Kupcevic
Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
Portions Copyright (C) 2000 Olaf Hering
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
}

## --help output.
usage()
{
echo \
"Usage: $PRG [OPTION]... FILE
Find OpenFirmware device path from unix device node.

      --debug                print boring junk only useful for debugging
  -h, --help                 display this help and exit
  -V, --version              output version information and exit"
}

## a small seq replacement, seq is not present on boot/rescue floppies.
smallseq()
{
    local v="$1"
    local n=1
    echo 1
    while [ "$v" -gt 1 ] ; do
	echo "$(($n + 1))"
	local n="$(($n + 1))"
	local v="$(($v - 1))"
    done
    return 0
}

## a kludge to replace wc -l, wc is not present on boot/rescue
## floppies. max file is 145 lines, 3 hosts * 16 devs each * 3 lines
## per device, + 1 "Attached Devices:" line.
linecount()
{
    if [ $# = 0 ] ; then
	local file="$(cat)"
	local v="$file"
    else
	local file="$(cat $1)"
	local v="$file"
    fi

    if [ -z "$file" ] ; then
	echo 0
	return 0
    fi

    ## use real wc if available
    if (command -v wc > /dev/null 2>&1) ; then
	if [ -x `command -v wc` ] ; then
	    lines="$(echo "$file" | wc -l)"
	    if [ $? = 0 ] ; then
		echo $lines
		unset lines
		return 0
	    fi
	fi
    fi

    while true ; do
	for i in `smallseq 145` ; do
	    local b="$(echo "$file" | tail -n $i)"
	    if [ "$v" = "$b" ] ; then
		echo "$i"
		break 2
	    fi
	done
    done
    return 0
}

## small tr replacment which handles a specific need of this script.
smalltr()
{
    case "$1" in
	a) echo 1 ;; b) echo 2 ;; c) echo 3 ;; d) echo 4 ;; e) echo 5 ;; f) echo 6 ;;
	g) echo 7 ;; h) echo 8 ;; i) echo 9 ;; j) echo 10 ;; k) echo 11 ;; l) echo 12 ;;
	m) echo 13 ;; n) echo 14 ;; o) echo 15 ;; p) echo 16 ;;
	1) echo a ;; 2) echo b ;; 3) echo c ;; 4) echo d ;; 5) echo e ;;
	6) echo f ;; 7) echo g ;; 8) echo h ;; 9) echo i ;; 10) echo j ;;
	11) echo k ;; 12) echo l ;; 13) echo m ;; 14) echo n ;; 15) echo o ;;
	16) echo p ;;
    esac
    return 0
}

## replacment for grep -l which is not supported by busybox grep.
## echo $(cat..) hack needed because busybox grep barfs with `line too
## long' when fed /proc files.  the for loop is needed since busybox
## grep seems to have somewhat broken regexp support.
## usage: lgrep filename regexp regexp ...
lgrep()
{
    local f="$1"
    shift
    for i in "$@" ; do
	echo "$(cat "$f")" | grep -q "$i" && echo "$f" && break
    done
    return 0
}

## if readlink is missing use a kludge
if (command -v readlink > /dev/null 2>&1) ; then
    true
else
    readlink()
    {
        local SYMTARGET="$(v=`ls -l "$1" 2>/dev/null` ; echo ${v##*> })"
        if [ -n "$SYMTARGET" ] ; then
            echo "$SYMTARGET"
            return 0
        else
            return 1
        fi
    }
fi

## a function to print relevant scsi host path when there is more then
## one.  this function also takes care of stripping off the trailing
## /compatible.
printhost()
{
    case "$1" in
	1)
	echo "${2%/*}"
	;;
	2)
	echo "${3%/*}"
	;;
	3)
	echo "${4%/*}"
	;;
	4)
	echo "${5%/*}"
	;;
    esac
    return 0
}

# read OpenFirmware device path from its corresponding devspec
find_of_path()
{
  [ -z "$1" ] && return
  [ -f "$1/devspec" ] && {
    OF_PATH="`cat $1/devspec`"
    SYS_PATH="$1"
    return
  }
  find_of_path "${1%/*}"
}

## this finds information we need on both newworld and oldworld macs.
## mainly what scsi host a disk is attached to.
scsiinfo()
{
  SYS_DEVICE="`readlink -f /sys/block/$DEVNODE/device`"

  [ -e "$SYS_DEVICE" ] && {
 
    SCSI_DEVICE=${SYS_DEVICE##*/}
    HOST_ID=${SCSI_DEVICE%%:*}
    BUS_ID=`T=${SCSI_DEVICE#*:}; echo ${T%%:*}`
    DEVICE_ID=`T=${SCSI_DEVICE#*:*:}; echo ${T%%:*}`
    LUN_ID=${SCSI_DEVICE##*:}

    OF_PATH=;find_of_path "$SYS_DEVICE"

    SCSI_DRIVER=`cat /sys/class/scsi_host/host$HOST_ID/proc_name 2>/dev/null`

    SCSI_NAME=`cat /proc/device-tree$OF_PATH/name 2>/dev/null`

    IEEE1394_ID=`cat /sys/block/$DEVNODE/device/ieee1394_id 2>/dev/null`
    IEEE1394_ID=${IEEE1394_ID%%:*}

    PLUG_ID=$(ls -dv $SYS_PATH/host* 2>/dev/null | grep -n "/host$HOST_ID$")
    PLUG_ID=$((${PLUG_ID%%:*}-1))

    PART=${PARTITION:+:$PARTITION}

    [ "$LUN_ID" != "0" ] && LUNX=`printf ",%x" $LUN_ID`

    [ "$DEBUG" = "1" ] && {
      echo SYS_DEVICE=$SYS_DEVICE
      echo SCSI_DEVICE=$SCSI_DEVICE
      echo HOST_ID=$HOST_ID
      echo BUS_ID=$BUS_ID
      echo DEVICE_ID=$DEVICE_ID
      echo LUN_ID=$LUN_ID
      echo PLUG_ID=$PLUG_ID
      echo IEEE1394_ID=$IEEE1394_ID
      echo SCSI_NAME=$SCSI_NAME
      echo SCSI_DRIVER=$SCSI_DRIVER
      echo SYS_PATH=$SYS_PATH
      echo OF_PATH=$OF_PATH
      echo PART=$PART
      echo LUNX=$LUNX
    }
 
    SCSI_DRIVER=${SCSI_DRIVER:?}
 
    [ -z "$OF_PATH" ] && {
      echo 1>&2 "Cannot find $DEVNODE among OpenFirmware registered devices"
 
      exit 1
    }
 
    return 0
  }
 
  echo 1>&2 "Link /sys/block/$DEVNODE/device does not exist"
 
  exit 1
}

## add OF device node to the host OF path,
## must be run after scsiinfo().
scsi_ofpath()
{
  if [ -d "/proc/device-tree$OF_PATH/sas" ] ; then

    SAS_ID=$(( ($BUS_ID << 16) | ($DEVICE_ID << 8) | $LUN_ID))
    printf "%s/sas/disk@%x%s\n" $OF_PATH $SAS_ID $LUNX$PART

  elif [ -d "/proc/device-tree$OF_PATH/scsi@`printf %x $BUS_ID`" ] ; then

    printf "%s/scsi@%x/@%x%s\n" $OF_PATH $BUS_ID $DEVICE_ID $LUNX$PART

  else

    case "$SCSI_DRIVER-$SCSI_NAME" in
      aic7xxx-*|sym53c8xx-*|mesh-*|*-scsi)
        printf "%s/@%x%s\n" $OF_PATH $DEVICE_ID $LUNX$PART
        ;;
      sata_sil-*|sata_vsc-*)
        printf "%s/@%x%s\n" $OF_PATH $(($PLUG_ID * 2)) $PART
        ;;
      sata_svw-*|*-pci-ata)
        printf "%s/@%x/@%x%s\n" $OF_PATH $PLUG_ID $DEVICE_ID $PART
        ;;
      pata_macio-*)
        printf "%s/@%x%s\n" $OF_PATH $DEVICE_ID $PART
        ;;
      sbp2-*)
        IEEE1394_ID=${IEEE1394_ID:?}
        printf "%s/node@%s/sbp-2/@%x%s\n" $OF_PATH $IEEE1394_ID $DEVICE_ID $PART
        ;;
      ipr-obsidian)
        printf "%s/@%x,%x%s\n" $OF_PATH $BUS_ID $DEVICE_ID $LUNX$PART
        ;;
      *)
        echo 1>&2 "${PRG}: ${SCSI_NAME} (${SCSI_DRIVER}) is not supported"
        return 1
        ;;
    esac

  fi

  return 0
}

ide_ofpath()
{
    if [ ! -L "/proc/ide/$DEVNODE" ] ; then
	echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
	return 1
    fi

    local IDEBUS="$(v=`readlink /proc/ide/$DEVNODE` ; echo ${v%%/*} )"
    if [ -z "$IDEBUS" ] ; then
	echo 1>&2 "$PRG: BUG: IDEBUS == NULL"
	return 1
    fi

    case "$(uname -r)" in
	2.5.*|2.6.0*|2.6.1|2.6.1-*|2.6.2|2.6.2-*)
	    echo 1>&2 "$PRG: Linux kernel `uname -r` is not supported"
	    return 1
	    ;;
	2.6.*|2.7.*)
	    if ! (grep -q '.* .* sysfs ' /proc/mounts 2> /dev/null) ; then
		echo 1>&2 "$PRG: sysfs must be mounted for ofpath to support this system"
		return 1
	    fi
	    local SYS="$(m=`grep '.* .* sysfs ' /proc/mounts | head -n 1` ; echo `d=${m#* };echo ${d%% *}`)"
	    if [ -z "$SYS" -o ! -d "$SYS" ] ; then
		echo 1>&2 "$PRG: Unable to determine sysfs mountpoint"
		return 1
	    fi
	    local OF1275IDE="${SYS}/block/${DEVNODE}/device/../../devspec"
	    ;;
	*)
	    local OF1275IDE="/proc/ide/$IDEBUS/devspec"
	    ;;
    esac

    if [ ! -f "$OF1275IDE" ] ; then
	case "$(cat /proc/device-tree/model)" in
	    PowerMac3*|PowerMac4*|PowerMac5*|PowerMac6*|PowerMac7*|RackMac*)
		local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
		if [ -z "$CDROM" ] ; then
		    echo 1>&2 "$PRG: WARNING: Your kernel is too old for proper support, device may be innaccurate."
		    echo "ultra2:$PARTITION"
		else
		    echo "cd:$PARTITION"
		fi
		;;
	    *)
		local CDROM="$(grep "^drive name:" /proc/sys/dev/cdrom/info 2> /dev/null | grep $DEVNODE)"
		if [ -z "$CDROM" ] ; then
		    if [ "$DEVNODE" = hda ] ; then
			echo "hd:$PARTITION"
		    else
			echo "ultra1:$PARTITION"
		    fi
		else
		    echo "cd:$PARTITION"
		fi
		;;
	esac
    else
	local DEVSPEC="$(cat $OF1275IDE)"
	[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVSPEC=$DEVSPEC"
	if [ -z "$DEVSPEC" ] ; then
	    echo 1>&2 "$PRG: KERNEL BUG: $OF1275IDE exists, but is empty"
	    return 1
	fi

	if [ ! -f "/proc/ide/${IDEBUS}/channel" ] ; then
	    echo 1>&2 "$PRG: KERNEL BUG: /proc/ide/${IDEBUS}/channel does not exist"
	    return 1
	fi

	case "$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)" in
	    ide|ata)
		local MASTER="/disk@0"
		local SLAVE="/disk@1"
		;;
	    pci-ide|pci-ata)
		local MASTER="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@0"
		local SLAVE="/@$(cat /proc/ide/${IDEBUS}/channel)/disk@1"
		;;
	    scsi) ## some lame controllers pretend they are scsi, hopefully all kludges are created equal.
		local MASTER="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 0))"
		local SLAVE="/@$(($(cat /proc/ide/${IDEBUS}/channel) * 2 + 1))"
		;;
	    spi)
		local MASTER="/disk@$(cat /proc/ide/${IDEBUS}/channel),0"
		local SLAVE="/disk@$(cat /proc/ide/${IDEBUS}/channel),1"
		;;
	    *)
		echo 1>&2 "$PRG: Unsupported IDE device type: \"$(cat /proc/device-tree${DEVSPEC}/device_type 2> /dev/null)\""
		return 1
		;;
	esac

	case "$DEVNODE" in
	    hda|hdc|hde|hdg|hdi|hdk|hdm|hdo)
		echo "${DEVSPEC}${MASTER}${PARTITION:+:${PARTITION}}"
		return 0
		;;
	    hdb|hdd|hdf|hdh|hdj|hdl|hdn|hdp)
		echo "${DEVSPEC}${SLAVE}${PARTITION:+:${PARTITION}}"
		return 0
		;;
	    *)
		echo 1>&2 "$PRG: /dev/$DEVNODE is not supported"
		return 1
		;;
	esac
    fi
}

## figure out the OpenFirmware device path for newworld macs.
## sd* scsi disks , hd* ide disks.
newworld()
{
    case "$DEVNODE" in
	sd*)
	    ## use common scsiinfo function to get info we need.
	    scsiinfo || return 1

	    ## now we have the data for /@$DEVID:$PARTITION
	    ## find the actual OF path.
	    scsi_ofpath || return 1
	    ;;
	hd*)
	    ide_ofpath || return 1
	    ;;
	*)
	    echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
	    return 1
	    ;;
    esac
    return 0
}

oldworld()
{
    ## for some reason 2.4 kernels put OF aliases in aliases@0/ instead of plain aliases/
    if [ -d "/proc/device-tree/aliases" ] ; then
	local ALIASES="aliases"
    elif [ -d "/proc/device-tree/aliases@0" ] ; then
	local ALIASES="aliases@0"
    else
	echo 1>&2 "$PRG: Cannot find OpenFirmware aliases directory in /proc/device-tree/"
	return 1
    fi

    local MODEL="$(cat /proc/device-tree/compatible)"
    [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: Oldworld subarch: $MODEL"

    case "$MODEL" in
	AAPL,7300*|AAPL,7500*|AAPL,8500*|AAPL,9500*|AAPL,\?\?\?\?*)
	    case "$DEVNODE" in
		sd*)
		scsiinfo || return 1
		case "$SCSI_DRIVER" in
		    mesh)
		    echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITION
		    ;;
		    53c94)
		    echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
		    ;;
		    *)
		    echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
		    return 1
		    ;;
		esac
		;;
		*)
		echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
		return 1
		;;
	    esac
	    ;;
	AAPL,e407*)
	    case "$DEVNODE" in
		sd*)
		scsiinfo || return 1
		case "$SCSI_DRIVER" in
		    mesh)
		    echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
		    ;;
		    *)
		    echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
		    return 1
		    ;;
		esac
		;;
		hda*)
		echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@0:$PARTITION
		;;
		hdb*)
		echo $(cat /proc/device-tree/$ALIASES/ata)/ATA-Disk\@1:$PARTITION
		;;
		hd*)
		echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
		;;
	    esac
	    ;;
	AAPL,e826*)
	    case "$DEVNODE" in
		sd*)
		scsiinfo || return 1
		case "$SCSI_DRIVER" in
		    mesh)
		    echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
		    ;;
		    *)
		    echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
		    return 1
		    ;;
		esac
		;;
		hda*)
		echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@0:$PARTITION
		;;
		hdb*)
		echo $(cat /proc/device-tree/$ALIASES/ata)/ata-disk\@1:$PARTITION
		;;
		hd*)
		echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
		;;
	    esac
	    ;;
	AAPL,Gossamer*|AAPL,PowerMac\ G3*)
	    case "$DEVNODE" in
		sd*)
		scsiinfo || return 1
		case "$SCSI_DRIVER" in
		    mesh)
		    echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITION
		    ;;
		    *)
		    echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
		    return 1
		    ;;
		esac
		;;
		hda*)
		echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@0:$PARTITION
		;;
		hdb*)
		echo $(cat /proc/device-tree/$ALIASES/ide0)/ata-disk\@1:$PARTITION
		;;
		hdc*)
		echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@0:$PARTITION
		;;
		hdd*)
		echo $(cat /proc/device-tree/$ALIASES/ide1)/ata-disk\@1:$PARTITION
		;;
		hd*)
		echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
		;;
	    esac
	    ;;
	AAPL,PowerBook1998*)
	    if [ -f  /proc/device-tree/$ALIASES/ata0 ] ; then
		local ATA0=ata0
	    else
		local ATA0=ide0
	    fi
	    if [ -f  /proc/device-tree/$ALIASES/ata1 ] ; then
		local ATA1=ata1
	    else
		local ATA1=bay-ata1
	    fi
	    case "$DEVNODE" in
		sd*)
		scsiinfo || return 1
		case "$SCSI_DRIVER" in
		    mesh)
		    echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
		    ;;
		    *)
		    echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
		    return 1
		    ;;
		esac
		;;
		hda*)
		echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@0:$PARTITION
		;;
		hdb*)
		echo $(cat /proc/device-tree/$ALIASES/$ATA0)/ata-disk\@1:$PARTITION
		;;
		hdc*)
		echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@0:$PARTITION
		;;
		hdd*)
		echo $(cat /proc/device-tree/$ALIASES/$ATA1)/atapi-disk\@1:$PARTITION
		;;
		*)
		echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
		return 1
		;;
	    esac
	    ;;
	AAPL,3400/2400*)
	    case "$DEVNODE" in
		sd*)
		scsiinfo || return 1
		case "$SCSI_DRIVER" in
		    mesh)
		    echo $(cat /proc/device-tree/$ALIASES/scsi-int)/sd\@$DEVICE_ID:$PARTITON
		    ;;
		    53c94)
		    echo $(cat /proc/device-tree/$ALIASES/scsi)/sd\@$DEVICE_ID:$PARTITON
		    ;;
		    *)
		    echo 1>&2 "$PRG: Driver $SCSI_DRIVER is not supported"
		    return 1
		    ;;
		esac
		;;
		hda*)
		echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@0:$PARTITION
		;;
		hdb*)
		echo $(cat /proc/device-tree/$ALIASES/ata0)/ata-disk\@1:$PARTITION
		;;
		hdc*)
		echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@0:$PARTITION
		;;
		hdd*)
		echo $(cat /proc/device-tree/$ALIASES/ata1)/atapi-disk\@1:$PARTITION
		;;
		hde*)
		echo $(cat /proc/device-tree/$ALIASES/ata2):$PARTITION
		;;
		hdf*)
		echo $(cat /proc/device-tree/$ALIASES/ata3):$PARTITION
		;;
		*)
		echo 1>&2 "$PRG: Unsupported device: /dev/$DEVNODE"
		return 1
		;;
	    esac
	    ;;
	*)
	    echo 1>&2 "$PRG: This machine is not supported: $MODEL"
	    return 1
	    ;;
    esac
    return 0
}

## find OpenFirmware device path for IBM CHRP hardware (scsi only)
chrp()
{
    case "$DEVNODE" in
	sd*)

	    ## use common scsiinfo function to get info we need.
	    scsiinfo || return 1

	    ## now we have the data for /@$DEVID:$PARTITION
	    ## find the actual OF path.
	    scsi_ofpath || return 1
	    ;;
	*)
	    echo 1>&2 "$PRG: Device: /dev/$DEVNODE is not supported"
	    return 1
	    ;;
    esac
    return 0
}

## If we get lame devfs name, we need to make it foad
ckdevfs()
{
    case "$1" in
	/dev/ide/*|/dev/scsi/*|/dev/discs/*)
	return 0
	;;
	*)
	return 1
	;;
    esac
}

## convert devfs names into normal short ones, written by Tom Rini.
fixdevfs()
{
    ## get partition number, if any
    local PARTNUM="${1##*[a-z]}"
    ## Find the bus type.
    local TYPE="$(v=${1#/dev/} ; echo ${v%/host*})"
    ## Find the host number.
    local HOST="$(v=${1#/dev/*/host} ; echo ${v%/bus*})"
    ## Find the bus number.
    local BUS="$(v=${1#/dev/*/bus} ; echo ${v%/tar*})"
    ## Find the target.
    local TARGET="$(v=${1#/dev/*/target} ; echo ${v%/lun*})"

    case "$TYPE" in
	ide)
	case "$HOST" in
	    0)
	    case "$TARGET" in
		0)
		local DEV=hda
		;;
		1)
		local DEV=hdb
		;;
	    esac
	    ;;
	    1)
	    case "$TARGET" in
		0)
	        local DEV=hdc
	        ;;
		1)
		local DEV=hdd
		;;
	    esac
	    ;;
	    *)
		echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
		return 1
	esac
	local DEV="${DEV}${PARTNUM}"
	echo "/dev/$DEV"
	return 0
	;;
	scsi)
	local LUN="$(v=${1#/dev/*/lun} ; echo ${v%/*})"

	## In this case, we need to figure out what number our device is
	local DEVCOUNT=0

	## copy scsi file into a variable removing "Attached Devices"
	## which is the first line. this avoids a lot of
	## [incmopatible] crap later, and improves readability.

	## find number of lines once and recycle that number, to save
	## some time (linecount is a bit slow). subtract one line
	## to scrap Attached Devices:

	local SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
	local PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"

	for i in $(smallseq $(($SCSILINES / 3))) ; do

	    ## put every scsi device into one single line
	    local DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
	    [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVINFO=$DEVINFO"

	    ## cut the type field, expect "Direct-Access" later.
	    local DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
	    [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVTYPE=$DEVTYPE"

	    if [ "$DEVTYPE" = "Direct-Access" ] || [ "$DEVTYPE" = "Direct-Access-RBC" ] ; then
		## Lets find out some more information
		## get the device id.
		local DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
		[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVID=$DEVID"

		## get the device lun.
		local DEVLUN="$(v=$(echo ${DEVINFO##*Lun: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
		[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVLUN=$DEVLUN"

		## get the device channel.
		local DEVCHAN="$(v=$(echo ${DEVINFO##*Channel: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
		[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCHAN=$DEVCHAN"

		## get the scsi host id.
		local DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
		[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVHOST=$DEVHOST"

		local DEVCOUNT="$(($DEVCOUNT + 1))"
		[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: fixdevfs: DEVCOUNT=$DEVCOUNT"
		if [ "$DEVHOST" = "$HOST" -a "$DEVCHAN" = "$BUS" -a \
		    "$DEVID" = "$TARGET" -a "$DEVLUN" = "$LUN" ] ; then
		    local DEV="sd$(smalltr $DEVCOUNT)${PARTNUM}"
		    echo "/dev/$DEV"
		    return 0
		fi
	    fi
	done
	echo 1>&2 "$PRG: $1: Unable to translate this device, try again without devfs."
	return 1
	;;
	*)
	echo 1>&2 "$PRG: Unknown bus $TYPE"
	return 1
	;;
    esac
    ## we should never get here
    return 1
}

## make sure that find, head and tail can be found.  otherwise the
## script will silently give bogus paths.  these are the only /usr/*
## utilities this script depends on.
checkutils()
{
    if command -v find > /dev/null 2>&1 ; then
        [ -x `command -v find` ] || FAIL=1 ; else FAIL=1 ; fi
    if command -v head > /dev/null 2>&1 ; then
        [ -x `command -v head` ] || FAIL=1 ; else FAIL=1 ; fi
    if command -v tail > /dev/null 2>&1 ; then
        [ -x `command -v tail` ] || FAIL=1 ; else FAIL=1 ; fi

    if [ "$FAIL" = 1 ] ; then
	echo 1>&2 "$PRG: \`find', \`head', or \`tail' could not be found, aborting."
	return 1
    else
	return 0
    fi
}

## parse command line switches.
if [ $# != 0 ] ; then
    while true ; do
	case "$1" in
	    -V|--version)
		version
		exit 0
		;;
	    -h|--help)
		usage
		exit 0
		;;
	    --debug)
		DEBUG=1
		shift
		;;
	    -*)
		echo 1>&2 "$PRG: unrecognized option \`$1'"
		echo 1>&2 "$PRG: Try \`$PRG --help' for more information."
		exit 1
		;;
	    "")
		echo 1>&2 "$PRG: You must specify a filename"
		echo 1>&2 "Try \`$PRG --help' for more information."
		exit 1
		;;
	    *)
		device="$1"
		break
		;;
	esac
    done
else
    echo 1>&2 "$PRG: You must specify a /dev device"
    echo 1>&2 "Try \`$PRG --help' for more information."
    exit 1
fi

## check that FILE is a block device and exists.
if [ ! -e "$device" ] ; then
    echo 1>&2 "$PRG: $device: No such file or directory"
    exit 1
elif [ ! -b "$device" ] ; then
    echo 1>&2 "$PRG: $device is not a block device"
    exit 1
fi

## check that we are running on a GNU/Linux system, OSX/BSD does not
## have the same /proc stuff
if [ `uname -s` != Linux ] ; then
    echo 1>&2 "$PRG: This utility will only work with GNU/Linux"
    exit 1
fi

## check for ppc, i think uname -m is safe for this...
if [ `uname -m` != ppc -a `uname -m` != ppc64 ] ; then
    echo 1>&2 "$PRG: This utility will only work on PowerPC hardware"
    exit 1
fi

## ofpath cannot live without procfs
if [ ! -f /proc/uptime ] ; then
    echo 1>&2 "$PRG: This utility requires the /proc filesystem"
    exit 1
fi

## check for retarded devfs names and tell them to foad.
if ckdevfs "$device" ; then
    device="$(fixdevfs $device)" || exit 1
fi

## check for newworld mac. use cat hack due to /proc wierdness.
if [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = NewWorld ] ; then
    SUBARCH=NewWorld
elif [ "$(v=`cat /proc/cpuinfo 2>/dev/null | grep pmac-generation` ; echo ${v##*:[ ]})" = OldWorld ] ; then
    SUBARCH=OldWorld
elif (cat /proc/cpuinfo 2>/dev/null | grep ^motherboard | grep -q AAPL) ; then
    SUBARCH=OldWorld
elif (cat /proc/cpuinfo 2> /dev/null | grep ^machine | grep -q 'CHRP IBM') ; then
    SUBARCH=CHRP
elif (cat /proc/cpuinfo 2> /dev/null | grep ^platform | grep -q 'Maple') ; then
    SUBARCH=Maple
elif (cat /proc/cpuinfo 2>/dev/null | grep ^machine | grep -q 'CHRP Pegasos') ; then
    SUBARCH=Pegasos
else
    echo 1>&2 "$PRG: This machine is not yet supported"
    exit 1
fi

## make sure /proc/device-tree exists
if [ ! -d /proc/device-tree ] ; then
    echo 1>&2 "$PRG: /proc/device-tree does not exist"
    echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_PROC_DEVICETREE=y"
    exit 1
fi

## make sure we have what we need.
checkutils || exit 1

## get the base device node and scrap /dev/ ie /dev/hda2 -> hda
DEVICE="${device##*/}"
DEVNODE="${DEVICE%%[0-9]*}"
PARTITION="${DEVICE##*[a-z]}"

## use appropriate search for right sub arch.
case "$SUBARCH" in
    # Pegasos OF seems to be NewWorld-ish enough to cope with this.
    NewWorld|Pegasos)
	newworld || exit 1
	;;
    OldWorld)
	oldworld || exit 1
	;;
    CHRP|Maple)
	chrp || exit 1
	;;
esac

exit 0