File: ofpathname

package info (click to toggle)
powerpc-ibm-utils 1.2.22-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,684 kB
  • ctags: 1,134
  • sloc: ansic: 13,342; sh: 3,823; perl: 935; makefile: 121
file content (1478 lines) | stat: -rwxr-xr-x 34,829 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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
#! /bin/bash

# Copyright (c) 2004 International Business Machines
# Common Public License Version 1.0 (see COPYRIGHT)
#
# Author Nathan Fontenot <nfont@linux.vnet.ibm.com>

#
# ofpathname - This utility provides a mechanism for converting a logical
# device name to an open firmware device path, and vice versa.
#
# TODO: This script doesn't handle floppy drives and token ring devices,
#       perhaps they should be added in at some point.
#

OFPATHNAME="ofpathname"
VERSION="0.5"
FIND=/usr/bin/find
CAT=/bin/cat
PSERIES_PLATFORM=$(dirname $0)/pseries_platform

# Find out what platfrom we are running on.  Hopefully this
# list will get expanded with time.
PLATFORM=$(sed /proc/cpuinfo -ne "s/^machine\t*: \(.*\)/\1/p")
case $PLATFORM in
    EFIKA5K2\ *)	PLATFORM=efika ;;
esac

# Usage statemnet
usage()
{
    echo "Usage: $OFPATHNAME [OPTION] DEVICE"
    echo "Provide logical device names <==> Open Firmware Device Path Conversion"
    echo ""
    echo "Optional arguments."
    echo "  -l               Convert Open Firmware device pathname to"
    echo "                   logical device name."
    echo "  -a               Find matching Open Firmware device alias[es]."
    echo "  -q, --quiet      Do not report failures, exit quietly"
    echo "  -V, --version    Display version information and exit"
    echo "  -h, --help       Display this help information and exit"
    echo ""
}

show_version()
{
    echo "$OFPATHNAME: Version $VERSION"
    echo "Written by: Nathan Fontenot <nfont@linux.vnet.ibm.com>"
}

#
# err
# Common routine to print error messages for ofpathname.  Since most of the
# error messages can be generated in multiple places, we put all the text
# here to avoid errors in duplicating the messages.
#
# The first and only parameteris the error message number, all of which
# are defined below as ERR_*.
#
ERR_NO_OFPATH=1
ERR_NO_SYSFS=2
ERR_NO_SYSFS_DEVINFO=3
ERR_NOT_CONFIG=4
ERR_NO_LOGDEV=5
err()
{
    local emsg=$1

    if [[ -n $be_quiet ]]; then
        exit 1
    fi

    case $emsg in
        1)  echo "$OFPATHNAME: Could not retrieve Open Firmware device path"
            echo "            for logical device \"$DEVNAME_ARG\"." ;;

        2)  echo "$OFPATHNAME: sysfs (/sys) is needed and does not appear"
            echo "            to be mounted on this system." ;;

        3)  echo "$OFPATHNAME: Could not find sysfs information for logical"
            echo "            device \"$DEVNAME_ARG\"." ;;

        4)  echo "$OFPATHANME: Logical device \"$DEVNAME_ARG\" does not appear"
            echo "            to be configured." ;;

        5)  echo "$OFPATHNAME: Could not retrieve logical device name for"
            echo "            Open Firmware path \"$DEVNAME_ARG\"."
    esac

    exit 1
}

# is_hbtl
# return true if the link is in HBTL (Host:Bus:Target ID:LUN) format
is_hbtl()
{
        local ln_name=$1;
        local tmp="${ln_name//[^:]}"
        if [[ ${#tmp} = 3 ]]; then
              echo 1
        else
              echo 0
	fi
}
#
# get_link
# return the directory path that a link points to.
# The only parameter is the link name.
#
get_link()
{
    local ln_name=$1;
    echo `ls -l $ln_name 2>/dev/null | awk -F"->" '{print $2}'`
}

#
# get_hbtl
# Given a path that ends in an HBTL (Host:Bus:Target ID:LUN), break it apart
# into its constituent parts in the global vars HOST, BUS, TARGET and LUN
#
# #1 path ending in HBTL
#
get_hbtl()
{
    local hbtl

    HBTL=${1##*/}
    hbtl=$HBTL

    HOST=${hbtl%%:*}
    hbtl=${hbtl#*:}
    BUS=${hbtl%%:*}
    BUS=`echo "ibase=10;obase=16; $BUS" | bc | tr "[:upper:]" "[:lower:]"`
    hbtl=${hbtl#*:}
    ID=${hbtl%%:*}
    ID=`echo "ibase=10;obase=16; $ID" | bc | tr "[:upper:]" "[:lower:]"`
    LUN=${hbtl#*:}
    LUN=`echo "ibase=10;obase=16; $LUN" | bc | tr "[:upper:]" "[:lower:]"`
}

#
# get_vdisk_no
# Given a path that ends in an HBTL, convert the HBTL values into a
# virtual disk number (not sure what the real terminology is for it).
# To do the conversion, the HBTL (A:B:C:D) is split apart and
# calculated as;
#     no = (0x8000 | B << 8 | C << 5 | D) * 1000000000000
#
# $1 path ending in HBTL
#
get_vdisk_no()
{
    get_hbtl $1

    local B C D
    typeset -i B C D

    B=$((0x$ID << 8))
    C=$((0x$BUS << 5))
    D=$((0x$LUN))

    local vdiskno vdisk
    typeset -i vdiskno
    vdiskno=$((0x8000 | $B | $C | $D ))
    vdisk=${vdiskno##-}

    vdisk=`echo \`bc << END
ibase=10
obase=16
$vdisk
END\``

    local extrazeroes="000000000000"
    echo $vdisk$extrazeroes
}

#
# get_usb_vdisk_no
# Given a path that ends in an HBTL, convert the HBTL values into a
# virtual disk number (not sure what the real terminology is for it).
# To do the conversion, the HBTL (A:B:C:D) is split apart and
# calculated as;
#     no = (0x1000000 | (usb_port << 16) | D);
#
# $1 path ending in HBTL
#
get_usb_vdisk_no()
{
        get_hbtl $1
        local usb_port=$2
        local B

        B=$((0x$usb_port << 16))

        local vdiskno vdisk
        vdiskno=$((0x1000000 | $B | $LUN ))
        vdisk=${vdiskno##-}

vdisk=$(bc << END
ibase=10
obase=16
$vdisk
END
)
        local extrazeroes="00000000"
        echo $vdisk$extrazeroes
}

#
# get_usb_storage_no
# Get usb device storage (port) number which is captured in
# devpath file
#
# $1 starting directory to look for devpath file
get_usb_storage_no()
{
    for dir in `$FIND /sys -name $1`; do
        # Move up until we find one with a devpath link
        goto_dir $dir "devpath" 0
        if [ $? -eq 0 ]; then
            break;
        fi
    done;

    if [ -f $PWD/devpath ]; then
	echo `$CAT $PWD/devpath`
    else
        err $ERR_NOT_CONFIG
    fi

}

#
# goto_dir
# This looks for a given file in a given directory or any parents of the
# given directory.
#
# $1 starting directory
# $2 file to search for
# $3 on_exit behavior on error
goto_dir()
{
    local start_dir=$1
    local fname=$2
    local found=0
    local on_exit=1

    if [[ $# -eq 3 ]]; then
	on_exit=$3
    fi

    cd $start_dir
    while [[ $PWD != "/" ]]; do
	ls $fname >/dev/null 2>&1
        if [[ $? -eq 0 ]]; then
            found=1
	    break
        fi
        cd ..
    done

    if [[ $found -eq 0 ]]; then
	if [[ $on_exit -eq 1 ]]; then
	    err $ERR_NO_SYSFS_DEVINFO
	else
	    return 1
	fi
    fi
}

#
# find_dir
# This looks for a given file in a given directory or any parents of the
# given directory.  This differs from goto_dir in that we don't actually try
# to cd to the directories, we just return the directory name if found.
#
# $1 starting directory
# $2 file to search for
# $3 on_exit behavior on error
find_dir()
{
    local dir=$1
    local fname=$2

    while [[ -n $dir ]]; do
	/bin/ls $dir/$fname >/dev/null 2>&1
	if [[ $? -eq 0 ]]; then
	    echo $dir
	    return
	fi

	dir=${dir%/*}
    done
}

#
# is_pata_dev
# Check to see if this is a PATA device
#
is_pata_dev()
{
    local this_dir=$PWD
    local sysfs_dir
    local udev_path
    local udevinfo="/usr/bin/udevinfo"
    local udevadm="/sbin/udevadm"

    if [[ -a $udevadm ]]; then
        udev_path=`$udevadm info --query=path --name=$DEVNAME`
    elif [[ -a $udevinfo ]]; then
        udev_path=`$udevinfo -q path -n $DEVNAME`
    else
        echo "no"
        return
    fi

    if [[ -z $udev_path ]]; then
        echo "no"
    else
	sysfs_dir=`get_link -f /sys/$udev_path/device`
	if [[ ! -d $sysfs_dir ]]; then
	    echo "no"
	else
	    goto_dir $sysfs_dir devspec
	    DEVTYPE=$(cat /proc/device-tree/$(cat $PWD/devspec)/device_type)

	    if [[ $DEVTYPE = "ata" ]]; then
	        echo "yes"
	    else
	        echo "no"
	    fi
	fi
    fi

    cd $this_dir
}

#
# print_aliases
# Print the aliases from /proc/device-tree/aliases for the specified device
#
print_aliases()
{
    dev=$1
    local found=0

    shopt -s nullglob
    for i in /proc/device-tree/aliases/*; do
        if sed -e "s/\x00$//g" $i | grep -qx "$dev" ; then
	    echo ${i##*/}
	    found=1
	fi
    done

    if [[ $found = "0" ]]; then
        echo "No aliases found."
	exit 1
    else
	exit 0
    fi
}

get_slave()
{
	cd /sys/class/*/$1
	while [[ -n "`ls slaves 2> /dev/null`" ]]; do cd slaves/*; done
	$FIND /dev -name "`basename $PWD`"
}

#
# is_net_interface
# Check to see if this is a network interface
#
is_net_interface()
{
    local res

    res=`$FIND /sys/class/net -name $1`
    if [[ ${#res} = 0 ]]; then
	echo "no"
    else
	echo "yes"
    fi
}

#
# logical_to_ofpathname
# Conversion for logical device name to an Open Firmware device path
#
logical_to_ofpathname()
{
    local is_cdrom

    # follow any links to the real device name
    while [[ -L $DEVNAME ]]; do
        DEVNAME=`get_link $DEVNAME`
    done

    while [[ -L /dev/$DEVNAME ]]; do
        DEVNAME=`get_link /dev/$DEVNAME`
    done

    DEVICE=${DEVNAME##*/}
    DEVNODE=${DEVICE%%[0-9]*}

    # try to determine if this is a cdrom device
    if [[ ${DEVNAME_ARG##*/} = cdrom ]]; then
        is_cdrom=yes
    elif [[ `get_link /dev/cdrom` = /dev/$DEVICE ]]; then
        is_cdrom=yes
    else
        is_cdrom=no
    fi

    case $DEVICE in
        eth*)       l2of_ethernet ;;
        hf*)        l2of_hfi ;;
        sd* | sr*)  # PATA devices appear as sd*, but should be converted
		    # using the ide logic
		    is_pata=$(is_pata_dev $DEVNAME)
		    if [[ $is_pata = "yes" ]]; then
		        l2of_ide
		    else
			l2of_scsi
		    fi ;;
        hd*)        l2of_ide ;;
	vd*)	    l2of_vd ;;
        fd*)        echo "no fd support yet" ;;
        dm-*)
                    DEVNAME=`get_slave $DEVICE`
                    logical_to_ofpathname
                    exit
                    ;;
	*)         # check if the device is a network interface
                   is_net=$(is_net_interface $DEVICE)
                   if [[ $is_net = "yes" ]]; then
                      l2of_ethernet
                   fi ;;
    esac

    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi

    if [[ $is_cdrom = yes ]]; then
        OF_PATH=$OFPATH:1\\ppc\\bootinfo.txt
    fi

    if [[ $do_alias = "1" ]]; then
        print_aliases $OF_PATH
    else
        echo $OF_PATH
    fi
}

#
# l2of_ide
# Conversion routine for logical => OF path of ide devices
#
l2of_ide()
{
    cd /sys/block/$DEVICE
    local link=`get_link "device"`
    if [[ -z $link ]]; then
        err $ERR_NO_SYSFS_DEVINFO
    fi
    cd $link

    # get the device number
    local devdir=${PWD##/*/}
    local channelno=${devdir%%\.*}
    local devno=${devdir##*\.}

    goto_dir $PWD "devspec"

    OF_PATH=`$CAT $PWD/devspec`
    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi

    # PCI ATA controller nodes (found on some Macs) have one child per IDE
    # channel.
    case `$CAT "/proc/device-tree/$OF_PATH/device_type"` in
        pci-ata | pci-ide)  OF_PATH=$OF_PATH/@$channelno ;;
    esac

    # On Efika, obtained devno "0:0:0:0" doesn't match actual device.
    # Note: according to vendor, "0,0" means primary master.  Secondary
    # channel is not present, and primary slave is rare enough that we
    # can reasonably ignore it.
    if [ "$PLATFORM" = "efika" ] ; then
        devno=0,0
    fi

    OF_PATH=$OF_PATH/disk@$devno
}

#
# l2of_vd
# Conversion routine for logical => OF path of virtio block devices
#
l2of_vd()
{
    local found=0

    # There may be many instances of DEVICE under /sys
    for dir in `$FIND /sys -name $DEVICE`; do
	# Move up until we find one with a device link
	goto_dir $dir "device" 0
	if [ $? -eq 0 ]; then
	    found=1;
	    break;
	fi
    done;

    if [ $found -eq 0 ]; then
        err $ERR_NOT_CONFIG
    fi

    local link=`get_link "device"`
    if [[ -z $link ]]; then
        err $ERR_NO_SYSFS_DEVINFO
    fi
    cd $link

    goto_dir $PWD "devspec"

    OF_PATH=`$CAT $PWD/devspec`
    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi
}

#
# l2of_ethernet
# Conversion routine for logical => OF path of ethernet devices
#
l2of_ethernet()
{
    for syspath in `$FIND /sys -name $DEVICE 2> /dev/null`; do
        if [[ -e $syspath/device/devspec ]]; then
            OF_PATH=`$CAT $syspath/device/devspec`
            break
        fi
    done

    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi
}

#
# l2of_hfi
# Conversion routine for logical => OF path of HFI devices
#
l2of_hfi()
{
    local hfnum=${DEVICE##hf}
    local hfpath

    if [[ $hfnum = "0" || $hfnum = "2" ]]; then
        hfpath=`$FIND /proc/device-tree -name hfi-ethernet* | sort | head -n 1`
    elif [[ $hfnum = "1" || $hfnum = "3" ]]; then
        hfpath=`$FIND /proc/device-tree -name hfi-ethernet* | sort | tail -n 1`
    else
        err $ERR_NO_OFPATH
    fi

    OF_PATH=${hfpath##/proc/device-tree}
}

#
# int_to_scsilun
# Conversion routine for SCSI HBTL LUN => SCSI LUN name
#
int_to_scsilun()
{
    local lunint=$1
    local A B C D

    A=$(( ($lunint >> 8) & 0xff ))
    B=$(($lunint & 0xff))
    C=$(( ($lunint >> 24) & 0xff ))
    D=$(( ($lunint >> 16) & 0xff ))

    local lunstr=$(printf "%02x%02x%02x%02x00000000" $A $B $C $D)
    lunstr=`echo $lunstr | sed 's/^[0]*//'`
    echo "$lunstr"
}

#
# scsilun_to_int
# Conversion routine for SCSI LUN name => SCSI HBTL LUN
#
scsilun_to_int()
{
    local lunstr=$1
    local A B C D L

    L=${lunstr/00000000}
    L=`echo $L | tr "[a-z]" "[A-Z]"`
    L=`echo "ibase=16;obase=A; $L" | bc`

    A=$(( ($L >> 8) & 0xff ))
    B=$(($L & 0xff))
    C=$(( ($L >> 24) & 0xff ))
    D=$(( ($L >> 16) & 0xff ))

    L=$(( (($A << 24) | ($B << 16) | ($C << 8) | $D) ))
    echo "$L"
}

get_fc_scsilun()
{
    local lun=$1
    local L
    L=`echo $lun | tr "[a-z]" "[A-Z]"`
    L=`echo "ibase=16;obase=A; $L" | bc`

    local fc_lun=`int_to_scsilun $L`
    echo "$fc_lun"
}

get_fc_wwpn()
{
    local start_dir=$1

    for f in `$FIND -H $start_dir -maxdepth 2 -name port_name`; do
        local wwpn=`$CAT $f`
	break
    done

    # strip the leading 0x
    wwpn=${wwpn:2}

    echo "$wwpn"
}

#
# l2of_scsi
# Converion routine for logical => OF path of scsi devices
#
l2of_scsi()
{
    local found=0
    local devtype

    # There may be many instances of DEVICE under /sys
    for dir in `$FIND /sys -name $DEVICE`; do
	# Move up until we find one with a device link
	goto_dir $dir "device" 0
	if [ $? -eq 0 ]; then
	    found=1;
	    break;
	fi
    done;

    if [ $found -eq 0 ]; then
        err $ERR_NOT_CONFIG
    fi

    # follow the 'device' link
    local link=`get_link "device"`
    if [[ -z $link ]]; then
	# device may not be a link
	link=device
    fi

    get_hbtl $link
    cd $link

    # save the name of the current directory, we may need it later...
    local device_dir=${PWD##/*/}
    local device_path=$PWD

    # move up directories until we find one with devspec information
    goto_dir $PWD "devspec"

    OF_PATH=`$CAT $PWD/devspec`
    if [[ -z $OF_PATH ]]; then
        err $ERR_NO_OFPATH
    fi

    local vdev=${OF_PATH%/*}
    local fc=${OF_PATH%@*}
    fc=${fc##/*/}

    if [[ -e /proc/device-tree$OF_PATH/device_type ]]; then
        devtype=`$CAT /proc/device-tree$OF_PATH/device_type`;
        if [[ $devtype = "fcp" || $devtype = "scsi-fcp" ]]; then
                fc="fibre-channel";
        fi
    fi

    if [[ $fc = "usb" ]]; then
	 local hub_no storage_no disk_no

        storage_no=`get_usb_storage_no $DEVICE`
        if [[ $storage_no = *.* ]]; then
                hub_no=${storage_no%%.*}
                storage_no=${storage_no##*.}
        fi

        disk_no=`get_usb_vdisk_no $device_dir $storage_no`

        if [[ -z $hub_no ]]; then
                OF_PATH=$OF_PATH/storage\@$storage_no/disk\@$disk_no
        else
                OF_PATH=$OF_PATH/hub\@$hub_no/storage\@$storage_no/disk\@$disk_no
        fi

   elif [[ $fc = "fibre-channel" ]]; then
	local wwpn=`get_fc_wwpn "$device_path/../../fc_remote_ports*"`

        if [[ ! -e /proc/device-tree$OF_PATH/disk ]]; then
                for dir in `$FIND /proc/device-tree$OF_PATH -type d`; do
                        if [[ -e $dir/disk ]]; then
                                OF_PATH=${dir##/proc/device-tree}
                                break;
                        fi
                done
        fi

        OF_PATH=$(printf "%s/disk@%s" $OF_PATH $wwpn)

        if [[ $LUN != "0" ]]; then
                local fc_lun=`get_fc_scsilun $LUN`
                OF_PATH=$(printf "%s,%s" $OF_PATH $fc_lun)
        fi
    elif [[ $vdev = "/vdevice" ]]; then
        # get the v-device data
        local tmp=${OF_PATH//\/vdevice/}
        local vdevtype=${tmp%@*}

        if [[ $vdevtype = "/vfc-client" ]]; then
                local vfc_lun=`get_fc_scsilun $LUN`
		local wwpn=`get_fc_wwpn "$device_path/../../fc_remote_ports*"`
                OF_PATH=$(printf "%s/disk@%s,%s" $OF_PATH $wwpn $vfc_lun)
        else
                local i vdiskno
                cd host*
                cd target*
                vdiskno=`get_vdisk_no $device_dir`

                OF_PATH=$OF_PATH/disk\@$vdiskno
        fi
    elif [[ -d /proc/device-tree$OF_PATH/sas ]]; then
        local vendor_id sas_id

	vendor_id=`od -tx /proc/device-tree/$OF_PATH/vendor-id`
	vendor_id=`echo $vendor_id | cut -d " " -f 2`
	if [[ $vendor_id = "00001000" ]]; then
	    local dev_id
	    goto_dir $device_path "sas_end_device*"
	    dev_id=${PWD##*-}
	    sas_id=`cat /sys/class/sas_device/end_device-$dev_id/sas_address`
	    sas_id=${sas_id##0x}

	    OF_PATH=$(printf "%s/sas/disk@%s" $OF_PATH $sas_id)
	    if [[ $LUN != "0" ]]; then
                local LUNSTR=`int_to_scsilun $LUN`
                OF_PATH=$(printf "%s,%s" $OF_PATH $LUNSTR)
	    fi
	else
            local B T L
            local fwtype="0"

            if [[ -e /sys/class/scsi_host/host$HOST/fw_type ]]; then
                fwtype=`$CAT /sys/class/scsi_host/host$HOST/fw_type`
            fi

            if [[ $fwtype = "1" ]]; then
                goto_dir $device_path "device_id"
                sas_id=`$CAT $PWD/device_id`
                sas_id=${sas_id##0x}

	        OF_PATH=$(printf "%s/sas/disk@%s" $OF_PATH $sas_id)
                if [[ $LUN != "0" ]]; then
                    local LUNSTR=`int_to_scsilun $LUN`
                    OF_PATH=$(printf "%s,%s" $OF_PATH $LUNSTR)
                fi
            else
                B=`echo $BUS | tr "[a-z]" "[A-Z]"`
                B=`echo "ibase=16;obase=A; $B" | bc`
                T=`echo $ID | tr "[a-z]" "[A-Z]"`
                T=`echo "ibase=16;obase=A; $T" | bc`
                L=`echo $LUN | tr "[a-z]" "[A-Z]"`
                L=`echo "ibase=16;obase=A; $L" | bc`

                sas_id=$(( ($B << 16) | ($T << 8) | $L ))

	        OF_PATH=$(printf "%s/sas/disk@%x" $OF_PATH $sas_id)
                if [[ $LUN != "0" ]]; then
                    OF_PATH=$(printf "%s,%x" $OF_PATH $LUN)
                fi
            fi
	fi
    else
        # make sure the "scsi" information is on the end of the path
        local scsi_name=${OF_PATH##/*/}
        scsi_name=${scsi_name%%@*}
        if [[ $scsi_name != "scsi" ]]; then
            scsi_name="scsi@$BUS"
            OF_PATH=$OF_PATH/$scsi_name
        fi

        OF_PATH=$OF_PATH/sd@$ID,$LUN
    fi
}

#
# ofpathname_to_logical
# Conversion for Open Firmware device paths to logical device names
#
ofpathname_to_logical()
{
    DEVPATH=${DEVNAME%/*}
    DEVICE=${DEVNAME##/*/}
    DEVTYPE=${DEVICE%\@*}
    SAS=${DEVPATH##/*/}
    FC=${SAS%%\@*}

    if [[ $do_alias = "1" ]]; then
        print_aliases $OF_PATH
    fi

    if [[ $DEVTYPE = "disk" && $DEVICE != ${DEVICE%:*} ]]; then
        DEVTYPE=${DEVICE%:*}
    fi

    if [[ $DEVTYPE = "disk" && $SAS = "sas" ]]; then
        DEVTYPE="sas"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "vfc-client" ]]; then
        DEVTYPE="vfc"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "fibre-channel" ]]; then
        DEVTYPE="fc"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "QLGC,qlc" ]]; then
        DEVTYPE="fc"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "fp" ]]; then
        DEVTYPE="fc"
    fi

    if [[ $DEVTYPE = "disk" && $FC = "storage" ]]; then
        local devpath=$DEVPATH
        DEVPATH=${devpath%/*}
        DEVTYPE="usb"

        if [[ $DEVNAME = *hub* ]]; then
            devpath=$DEVPATH
            DEVPATH=${devpath%/*}
        fi
    fi

    # Remove any possible cdrom data from DEVICE
    if [[ ${DEVICE##*,} = "\ppc\bootinfo.txt" ||
          ${DEVICE##*,} = \ppc\bootinfo.txt ]]; then
        DEVICE=${DEVICE%,*}
    fi

    # Remove any possible yaboot suffix from DEVICE
    if [[ ${DEVICE##*,} = "yaboot" ]]; then
	DEVICE=${DEVICE%,*}
    fi

    case $DEVTYPE in
        sd* | scsi*   )  of2l_scsi ;;
        sas           )  of2l_sas ;;
        vfc           )  of2l_vfc ;;
        fc            )  of2l_fc ;;
        v-scsi | disk )  of2l_vscsi
                         if [[ -z $LOGICAL_DEVNAME && $DEVTYPE = disk ]]; then
                             of2l_ide
                          fi ;;
        eth* | l-lan  )  of2l_ethernet ;;
        hfi-ethernet* )  of2l_hfi ;;
        disk*         )  of2l_ide ;;
        usb           )  of2l_usb ;;
    esac

    if [[ -z $LOGICAL_DEVNAME ]]; then
        err $ERR_NO_LOGDEV
    fi

    # See if this device is the cdrom
    if [[ `get_link "/dev/cdrom"` = $LOGICAL_DEVNAME ]]; then
        LOGICAL_DEVNAME="cdrom"
    fi

    echo $LOGICAL_DEVNAME
}

#
# of2l_ide
# Conversion routine for OF path => logical name for ide devices
#
of2l_ide()
{
    local dir

    for dir in `$FIND /sys/block -name 'hd*'`; do
        # get devno
        local devno=${DEVICE##*@}
        devno=${devno%%:*}

        cd $dir
        local link=`get_link "device"`
        if [[ -n $link ]]; then
            cd $link

            # see if this is the correct device
            local this_devno=${PWD##*\.}
            if [[ $devno -eq $this_devno ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec = $DEVPATH ]]; then
                    LOGICAL_DEVNAME="${dir##*/}"
                    break
                fi
            fi
        fi
    done
}

#
# of2l_ethernet
# Conversion routine for OF path => logical names of ethernet devices
#
of2l_ethernet()
{
    local dir

    # strip off ip info if present
    local devname=${DEVNAME%%:*}
    local netdir="/sys/class/net"

    for dir in `ls $netdir`; do
	local devdir=`find_dir $netdir/$dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            err $ERR_NO_SYSFS_DEVINFO
        fi

        cd $link
        local devspec=`$CAT ./devspec 2>/dev/null`

        if [[ $devspec = $devname ]]; then
            LOGICAL_DEVNAME="${dir##*/}"
            return
        fi
    done
}

#
# of2l_hfi
# Conversion routine for OF path => logical names of HFI devices
#
of2l_hfi()
{
    # strip off ip info if present
    local devname=${DEVNAME%%:*}
    local hfpath

    hfpath=`$FIND /proc/device-tree -name hfi-ethernet* | sort | head -n 1`
    hfpath=${hfpath##/proc/device-tree}

    if [[ $hfpath = $devname ]] ; then
        LOGICAL_DEVNAME="hf0"
    else
        hfpath=`$FIND /proc/device-tree -name hfi-ethernet* | sort | tail -n 1`
        hfpath=${hfpath##/proc/device-tree}

        if [[ $hfpath = $devname ]] ; then
            LOGICAL_DEVNAME="hf1"
        else
            err $ERR_NO_LOGDEV
        fi
    fi
}

#
# of2l_usb
# Conversion routine for OF path => logical names of usb devices
#
of2l_usb()
{
    DEV_HBTL_NO=${DEVICE##*\@}

    local dir
    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
        local devdir=`find_dir $dir device`
        if [[ -z $devdir ]]; then
            continue
        fi

        cd $devdir

        local link=`get_link "device"`
        local vdisk_no

        if [[ -n $link ]]; then
           local port_no storage_no target

           target=${link##*/}
           port_no=`get_usb_storage_no $target`
           storage_no=${port_no##*.}

           vdisk_no=`get_usb_vdisk_no $target $storage_no`

           cd $link
           if [[ $vdisk_no = $DEV_HBTL_NO ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec = $DEVPATH ]]; then
                    LOGICAL_DEVNAME=${dir##/*/}
                    return
                fi
            fi
        fi
    done
}

#
# of2l_vscsi
# Conversion routine for OF path => logical names of virtual scsi devices
#
of2l_vscsi()
{
    DEV_HBTL_NO=${DEVICE##*\@}

    local dir
    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -n $link ]]; then
            local vdiskno=`get_vdisk_no $link`

            cd $link
            if [[ $vdiskno = $DEV_HBTL_NO ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec = $DEVPATH ]]; then
                    LOGICAL_DEVNAME=${dir##/*/}
                    return
                fi
            fi
	fi
    done
}

#
# of2l_scsi
# Conversion routine for OF path => logical names of scsi devices
#
of2l_scsi()
{
    DEV_TARGET=${DEVICE##*\@}
    DEV_TARGET=${DEV_TARGET%%,*}
    DEV_LUN=${DEVICE##*,}

    # At this point DEV_LUN may be in the form X:Y, we're only interested
    # in the X component.
    DEV_LUN=${DEV_LUN%%:*}

    local dir
    for dir in `$FIND /sys/block -name '[sv][dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            err $ERR_NO_SYSFS_DEVINFO
        fi

        local hbtl=`is_hbtl $link`
        # Do not call get_hbtl for virtio block devices
        if [[ $hbtl = 1 ]]; then
               get_hbtl $link
	fi
        cd $link

        # save the name of the current directory, we may need it later...
        local device_dir=${PWD##/*/}

        if [[ !$hbtl || ($ID = $DEV_TARGET && $LUN = $DEV_LUN) ]]; then
            goto_dir $PWD "devspec"
            local devspec=`$CAT ./devspec 2>/dev/null`

            # Handle virtio block devices
            if [[ !$hbtl && $devspec = $DEVNAME ]]; then
                LOGICAL_DEVNAME="${dir##*/}"
                return
            fi

            if [[ $devspec = $DEVPATH ]]; then
                LOGICAL_DEVNAME="${dir##*/}"
                return
            fi

            local scsi_name=${devspec##/*/}
            scsi_name=${scsi_name%%@*}

            if [[ $scsi_name != "scsi" ]]; then
                scsi_name="scsi@$BUS"
                devspec=$devspec/$scsi_name

                if [[ $devspec = $DEVPATH ]]; then
                    LOGICAL_DEVNAME="${dir##*/}"
                    return
                fi
            fi
        fi
    done
}

#
# of2l_sas
# Conversion routine for OF path => logical names of sas devices
#
of2l_sas()
{
    local matchtype dir

    DEV_NAME=${DEVICE##*\@}
    DEV_ID=${DEV_NAME%%,*}

    LUN=${DEV_NAME/$DEV_ID}
    LUN=${LUN/,/}

    if [[ ${#LUN} = 0 ]]; then
        LUN="0"
    fi

    lunint=`scsilun_to_int $LUN`

    PCI_ID=${DEVPATH%%/sas*}
    vendor_id=`od -tx /proc/device-tree/$PCI_ID/vendor-id`
    vendor_id=`echo $vendor_id | cut -d " " -f 2`

    if [[ $vendor_id = "00001000" ]]; then
        matchtype="libsas"
    else
        matchtype="ipr32"
    fi

    for dir in `$FIND /sys/class/scsi_host -maxdepth 1 -name 'host*'`; do
        cd $dir
        local link=`get_link "device"`

        cd $link
        cd ..

        local devspec=`$CAT ./devspec 2>/dev/null`
        if [[ $devspec = $PCI_ID ]]; then
                # check for ipr64
                if [[ -e $dir/fw_type ]]; then
                        local fwtype=`$CAT $dir/fw_type`
                        if [[ $fwtype = "1" ]]; then
                                matchtype="ipr64"
                                break
                        fi
                fi

                break
        fi
    done

    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            err $ERR_NO_SYSFS_DEVINFO
        fi

        get_hbtl $link
        cd $link

        # save the name of the current directory, we may need it later...
        local device_dir=$PWD

        local B T L
        B=`echo $BUS | tr "[a-z]" "[A-Z]"`
        B=`echo "ibase=16;obase=A; $B" | bc`
        T=`echo $ID | tr "[a-z]" "[A-Z]"`
        T=`echo "ibase=16;obase=A; $T" | bc`
        L=`echo $LUN | tr "[a-z]" "[A-Z]"`
        L=`echo "ibase=16;obase=A; $L" | bc`

        if [[ $matchtype = "ipr32" ]]; then
            local sas_id=$((($B << 16) | ($T << 8) | $L))

            sas_id=`echo "ibase=A;obase=16; $sas_id" | bc`
            sas_id=`echo $sas_id | tr "[A-Z]" "[a-z]"`

            if [[ $sas_id = $DEV_ID ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec/sas = $DEVPATH ]]; then
                    LOGICAL_DEVNAME="${dir##*/}"
                    return
                fi
            fi
        elif [[ $matchtype = "ipr64" ]]; then
            if [[ -e $devdir/device/device_id ]]; then
                local deviceid=`$CAT $devdir/device/device_id`
                deviceid=${deviceid##0x}

                if [[ $deviceid != $DEV_ID ]]; then
                    continue
                fi

                if [[ $L = $lunint ]]; then
                    goto_dir $PWD "devspec"
                    local devspec=`$CAT ./devspec 2>/dev/null`

                    if [[ $devspec/sas = $DEVPATH ]]; then
                        LOGICAL_DEVNAME="${dir##*/}"
                        return
                    fi
                fi
            fi
        elif [[ $matchtype = "libsas" ]]; then
            local dev_id
	    goto_dir $device_dir "sas_end_device*" 0
	    dev_id=${PWD##*-}

            if [[ ! -e /sys/class/sas_device/end_device-$dev_id/sas_address ]]; then
                continue
            fi

	    sas_id=`cat /sys/class/sas_device/end_device-$dev_id/sas_address`
	    sas_id=${sas_id##0x}

            if [[ $sas_id != $DEV_ID ]]; then
                continue
            fi

            if [[ $L = $lunint ]]; then
                goto_dir $PWD "devspec"
                local devspec=`$CAT ./devspec 2>/dev/null`

                if [[ $devspec/sas = $DEVPATH ]]; then
                    LOGICAL_DEVNAME="${dir##*/}"
                    return
                fi
            fi
        else
                err $ERR_NO_LOGDEV
        fi

    done
}

#
# of2l_vfc
# Conversion routine for OF path => logical names of vFC devices
#
of2l_vfc()
{
    DEV_ID=${DEVICE##*\@}
    OF_WWPN=${DEV_ID%%,*}
    OF_LUN=${DEV_ID##$OF_WWPN}
    OF_LUN=${OF_LUN#,}
    OF_LUN=`echo $OF_LUN | sed 's/^[0]*//'`

    local dir
    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            continue
        fi

        get_hbtl $link
        cd $link
        link=$PWD

        local device_dir=${PWD##/*/}
        goto_dir $PWD "devspec"
        OF_PATH=`$CAT $PWD/devspec`
        if [[ -z $OF_PATH ]]; then
                err $ERR_NO_LOGDEV
        fi

        local vdev=${OF_PATH%/*}
        local tmp=${OF_PATH//\/vdevice/}
        local vdevtype=${tmp%@*}

        if [[ $vdevtype != "/vfc-client" ]]; then
                continue
        fi

        local vfc_lun=`get_fc_scsilun $LUN`
	local wwpn=`get_fc_wwpn "$link/../../fc_remote_ports*"`

        if [[ $vfc_lun = $OF_LUN && $wwpn = $OF_WWPN ]]; then
            LOGICAL_DEVNAME="${dir##*/}"
            return
        fi
    done
}

#
# of2l_fc
# Conversion routine for OF path => logical names of FC devices
#
of2l_fc()
{
    DEV_ID=${DEVICE##*\@}
    OF_WWPN=${DEV_ID%%,*}
    OF_LUN=${DEV_ID/$OF_WWPN}
    OF_LUN=${OF_LUN/,/}

    if [[ ${#OF_LUN} = 0 ]]; then
        OF_LUN="0"
    fi

    lunint=`scsilun_to_int $OF_LUN`

    local dir
    for dir in `$FIND /sys/block -name 's[dr]*'`; do
        # go up to find directory with 'device' link
	local devdir=`find_dir $dir device`
	if [[ -z $devdir ]]; then
	    continue
	fi

	cd $devdir

        local link=`get_link "device"`
        if [[ -z $link ]]; then
            continue
        fi

        get_hbtl $link
        cd $link
        link=$PWD

	# find device_path, not all dirs will have a fc_remote_ports
	goto_dir $PWD "fc_remote_ports*" 0
	if [[ $? -eq 1 ]]; then
	    continue
	fi
	device_path=$PWD

	cd $link
        local device_dir=${PWD##/*/}
        goto_dir $PWD "devspec"
        OF_PATH=`$CAT devspec`
        if [[ -z $OF_PATH ]]; then
                err $ERR_NO_LOGDEV
        fi

	local wwpn=`get_fc_wwpn "$device_path/fc_remote_ports*"`

        if [[ $wwpn = $OF_WWPN ]]; then
                local L
                L=`echo $LUN | tr "[a-z]" "[A-Z]"`
                L=`echo "ibase=16;obase=A; $L" | bc`

                if [[ $L = $lunint ]]; then
                        LOGICAL_DEVNAME="${dir##*/}"
                        return
                fi
        fi
    done
}

#
# Main
#
. $PSERIES_PLATFORM
if [[ $platform = $PLATFORM_POWERKVM_HOST ]]; then
	echo "$OFPATHNAME: is not supported on the $platform_name platform"
	exit 1
fi

if [[ "$#" -eq 0 ]]; then
    usage
    exit 0
fi

# default: convert logical => OFpath
do_of2l=0

# default: do not do alias lookups
do_alias=0

getopt -o "l:Vqh" -l "help,version,quiet" $@ > /dev/null 2>&1
while [[ -n $1 ]]; do
    case "$1" in
	-a)		do_alias=1 ;;

        -l)             do_of2l=1
                        DEVNAME_ARG=$2
                        shift ;;

	-V | --version) show_version
                        exit 0 ;;

        -q | --quiet)   be_quiet=1 ;;

        -h | --help)    usage
                        exit 0 ;;
	*)              DEVNAME_ARG=$1 ;;
    esac

    shift
done

DEVNAME=$DEVNAME_ARG

# double check device name
if [[ -z $DEVNAME ]]; then
    usage
    exit 1
fi

# We need sysfs
if [[ ! -d "/sys" ]]; then
    err $ERR_NO_SYSFS
    exit 1
fi


if [[ $do_of2l = "0" ]]; then
    # logical devname => OF pathname
    logical_to_ofpathname
else
    # OF pathnmae => logical devname
    ofpathname_to_logical
fi

exit 0