summaryrefslogtreecommitdiffstats
path: root/src/tools/occtoolp9
blob: 535ebc030f31a9705f30dc15e1d370eb89590c28 (plain)
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
#!/bin/bash

blue=""
red=""
green=""
bold=""
normal=""
if [ $TERM == "xterm-color" ] || [ $TERM == "xterm" ]; then
    blue="\033[34;1m";
    red="\033[31;1m";
    green="\033[32;1m";
    bold="\033[1m";
    normal="\033[0m";
fi
binfile="binary.bin"
inputfile=""
G_powerArch="p9";
cmd_sram_addr="fffbe000"
doorbell_addr="6d035"
rsp_sram_addr="fffbf000"
poll_version="20"
occ_cmd=""
occ_cmd_data=""
action=""


interface="cronus"
if [ -z "$BMCIP" ]; then
    if [ -n "$BMC_IP_ADDRESS" ]; then
        BMCIP=$BMC_IP_ADDRESS
        interface="bmc"
    fi
else
    interface="bmc"
fi

sudo=""
if [ -e /usr/sbin/opal-prd ]; then
    if [ "$USER" != "root" ]; then
        sudo="sudo"
    fi
    interface="opal"
fi

function convertToBinary
{
    infile=$1
    outfile=$2
    hexStringBuffer=""

    if [ -n "$outfile" ]; then
        if [ $verbose -ne 0 ]; then
            echo "convertToBinary: Reading ${infile}..."
        fi
        let skipped_bytes=0
        while IFS= read -r line
        do
            if [ ${#line} -ge 2 ]; then
                if [[ "${line}" =~ "~[" ]]; then
                    # Strip off header data
                    #~[0x0000] 07000002 09801003 14010000 00000000    *................*
                    #echo "LINE : $line"
                    strippedLine=`echo $line | sed 's/.*~\[//'`
                    #echo "LINE2: $strippedLine"
                    # strip off offset and only keep hex data
                    strippedLine=`echo ${strippedLine:8:36} | sed 's#\s##g'`
                    #echo "LINE3: $strippedLine"
                    #strippedLine=`echo ${strippedLine:10:36} | sed 's#\s##g'`
                    if [ $skipped_bytes == 0 ]; then
                        # Skip response header
                        let rsp_length=0x${strippedLine:6:4}
                        #echo "Rsp Length: $rsp_length"
                        strippedLine="${strippedLine:10}";
                        let skipped_bytes=5
                    fi
                else
                    strippedLine=`echo $line | sed 's#\s##g'`
                fi
                #if [ $verbose -ne 0 ]; then
                #    echo "READ: $line / $strippedLine"
                #fi
                hexStringBuffer="${hexStringBuffer}$strippedLine"
            fi
        done < $infile

        if [ -e "$outfile" ]; then
            rm $outfile
        fi
        for ((i = 0; i < ${#hexStringBuffer}; i+=2)); do
            byte="${hexStringBuffer:$i:2}"
            echo -n -e "\x$byte" >> $outfile
        done
        if [ -e "$outfile" ]; then
            echo "Converted $infile to binary: $outfile";
        fi
    else
        echo "ERROR: No output file name specified for convertToBinary"
    fi
} # end convertToBinary()

function usage
{
    echo "Usage: occtoolp9 <options> <command>"
    echo "       OCC Debug Tool (via opal-prd or CRONUS)"
    echo ""
    echo "       commands:"
    echo "           -h = help"
    echo "           -p = Send POLL command to the OCC"
    echo "           -X XX XXXXXX... = Send XX command to the OCC with specified cmd data"
    echo "              -X 53 0500000001LLLLTTTT = list first 50 OCC sensors (LLLL=Location,TTTT=Type)"
    echo "              -X 53 0600003d = get sensor details for GUID 0x003d"
    echo "           -SL = List all OCC sensors"
    echo "           -SL0 = List all OCC sensors (including sensors with 0 readings)"
    echo "           -trace  Collect OCC Trace hex trace data"
    echo ""
    echo "         OPAL-PRD Only Commands:"
    echo "           -I = HTMGT info"
    echo ""
    echo "         CRONUS Only Commands:"
    echo "           -P = Send POLL command to the OCC and purge any elog, if found"
    echo "           -CMDRSP = Dump OCC Command/Response buffers from SRAM"
    echo ""
    echo "       options:"
#    echo "           -b <BMC-ip-address> = send poll via IPMI/BMC"
#    echo "              address is optional if BMCIP is set"
    echo "           -o # = send command to OCC# (default 0)"
    echo "           -v = verbose (dump raw data)"
#    echo "           -f <filename> = parse response from specified filename"
#    echo "           -A = force IPMI userid of admin (vs ADMIN)"
#    echo "           -SM = use ADMIN/ADMIN (SuperMicro default)"
#    echo "           -pw BMC_PW = specified BMC password"
    echo "           -pt = parse OCC response w/o seq/cmd/rsp/len/csum (from opal-prd passthru or syslog)"
#    echo "           -oldbmc = send IPMI cmds without OCC instance number (prior to Aug 2015)"
    echo "           -nocolor = skip highlighting"
    echo "           -ch # = which cronus channel to use (default is 3)"
    echo ""
#    echo "       TODO:"
#    echo "           -OF OCC FFDC"
#    echo "           -S type=TT,loc=LL = List all OCC sensors"
#    echo "              Sensor Types: 0xFFFF=All, 0x1=Generic, 0x2=Current, 0x4=Voltage, 0x8=Temperature,"
#    echo "                     0x10=Utilization, 0x20=Time, 0x40=Frequency, 0x80=Power, 0x200=Performance"
#    echo "              Sensor Locations: 0xFFFF=All, 0x1=System, 0x2=Processor, 0x4=Partition, 0x8=Memory,"
#    echo "                     0x10=VRM, 0x20=OCC 0x40=Core"
    exit 0
}

bmc="";
let verbose=0
quiet="-quiet"
let purge=0
# Default OCC/Processor
let occ=0
let bmcFound=0
number=$RANDOM
let "number %= 126"
let number=$number+1
sequence=$(printf "%02x" $number)
let seqMismatch=0
let channel=3
let non_zero_sensors=1
let skip_header=0

if [ -n "$bmcId" ]; then
    bmcId="$bmcId"
elif [ -n "$adminId" ]; then
    bmcId="$adminId"
else
    bmcId="ADMIN"
fi

if [ -n "$bmcPw" ]; then
    bmcPw="$bmcPw"
else
    bmcPw="admin"
fi
let sendOccInstance=1

let passThru=0
while [ -n "$(echo $1 | grep '-')" ]; do
    case $1 in

        #### COMMANDS:

    -h ) usage; exit 0;;

    -p ) occ_cmd="00"
         occ_cmd_data=$poll_version
         ;;

    -SL )
        action="SensorList"
        ;;

    -SL0 )
        action="SensorList"
        let non_zero_sensors=0
        ;;

    -trace )
        action="trace"
        ;;

    -X )
        if [ -n "$2" ] && ( [ ${#2} -eq 2 ] || [ ${#2} -eq 4 ] ); then
             if [ "${2:0:2}" == "0x" ]; then
                 occ_cmd="${2:2:2}"
             else
                 occ_cmd="$2"
             fi
             if [ -n "$3" ]; then
                 let odd_length="${#3} & 1"
                 if [ $odd_length -eq 0 ]; then
                     if [ "${3:0:2}" == "0x" ]; then
                         occ_cmd_data="${3:2}"
                     else
                         occ_cmd_data="$3"
                     fi
                 else
                     echo -e "${red}ERROR: OCC command data must be even number of HEX digits${normal}"
                     exit 12
                 fi
             fi
         else
             echo -e "${red}ERROR: -X requires 2 byte command (in HEX)${normal}"
             exit 12
         fi
         ;;

        #### OPAL-PRD ONLY COMMANDS/OPTIONS:

    -I )
        action="TMGTInfo"
        ;;

        #### CRONUS ONLY COMMANDS/OPTIONS:

    -ch )
        if [ -n "$2" ]; then
            let channel=$2
        else
            echo -e "${red}ERROR: -ch requires channel number${normal}"
            exit 13
        fi
        ;;

    -CMDRSP )
        if [ "$interface" == "cronus" ]; then
            action="CmdRspBuffer"
        else
            echo "ERROR: -CMDRSP only supported via CRONUS"
            exit 1
        fi
        ;;

    -P ) occ_cmd="00"
        occ_cmd_data=$poll_version
        let purge=1
        ;;

        ### OPTIONS:

    -A ) bmcId="admin"
        ;;

    -b )
        if [ -z "$2" ] || [ "`echo $2 | cut -c1`" == "-" ]; then
            if [ "$BMCIP" != "" ]; then
                bmc=$BMCIP
                interface="bmc"
            else
                echo -e "${red}ERROR: -b requires BMCIP${normal}"
                exit 12
            fi
        else
            bmc=$2
            interface="bmc"
            shift
        fi
        if [ $verbose -ne 0 ]; then
            echo "BMC: $bmc";
        fi
        ;;

    -f )
        if [ -n "$2" ] && [ -e "$2" ]; then
            inputfile="$2"
        else
            echo -e "${red}ERROR: -f requires a input filename${normal}"
            exit -2;
        fi;;

    -nocolor ) blue=""; red=""; green=""; bold=""; normal=""
        ;;

    -o )
        if [ -z "$2" ] || [ "`echo $2 | cut -c1`" == "-" ]; then
            echo -e "${red}ERROR: -o requires OCC instance number${normal}"
            exit 13
        else
            let occ=$2
            shift
        fi;;

    -oldbmc ) let sendOccInstance=0
        ;;

    -pt ) let passThru=1
        ;;

    -pw )
        if [ -n "$2" ]; then
            bmcPw="$2"
        else
            echo -e "${red}ERROR: -pw requires BMC password${normal}"
            exit 13
        fi
        ;;

    -SM ) bmcPw="ADMIN"
        ;;

    -v ) let verbose=1; quiet=""
        ;;

    * ) echo -e "${red}ERROR: unknown option $1${normal}"; exit 1
        ;;

    esac
    shift
done

#echo "OCC${occ}, BMC: ${bmc}, purge=$purge, verbose=$verbose"



# Force Call Home: ipmitool -H 9.3.29.129 -I lanplus -U admin -P admin raw 0x3a 0x0d 0x40 0x24
# For Error w/Reset (pmc failure): putscom pu 1010842 1000000000180000 -p0

function sendOccCmdBmc
{
    let bmcOccInstance=$occ+1
    outName="/tmp/occ_cmd.txt"

    if [ -z "$inputfile" ]; then

        if [ -e "${outName}" ]; then
            if [ $verbose -ne 0 ]; then
                echo "--> removing ${outName}"
            fi
            rm ${outName}
        fi
        if [ $sendOccInstance -ne 0 ]; then
            occInstance=$(printf "0x%02x" $bmcOccInstance)
        else
            occInstance=""
        fi
        #if [ $verbose -ne 0 ]; then
        echo -e "${bold}==> ipmitool -H $bmc -I lanplus -U $bmcId -P $bmcPw raw 0x3a 0x0d ${occInstance} 0x00 0x10${normal}"
        #fi
        ipmitool -H $bmc -I lanplus -U $bmcId -P $bmcPw raw 0x3a 0x0d ${occInstance} 0x00 0x10 > ${outName}
        let rc=$?

    else
        outName="$inputfile"
        echo "grep -q 'opal-prd: HBRT: HTMGT:' $inputfile"
        grep -q 'opal-prd: HBRT: HTMGT:' $inputfile
        if [ $? -eq 0 ]; then
            echo "Converting $inputfile"
            # Convert opal-prd poll rsp to "XX XX XX XX" format
            cat $inputfile  | sed 's/.*HBRT: HTMGT:.*\] //' | sed 's/    .*//' | sed 's/ //g' | sed 's/\(..\)/\1 /g' > ${inputfile}.filtered
        else
            cat $inputfile | sed 's/ //g' | sed 's/\(..\)/\1 /g' > ${inputfile}.filtered
        fi
        if [ -s "${inputfile}.filtered" ]; then
            outName="${inputfile}.filtered"
        fi
        echo -e "${bold}==> Reading data from ${outName}${normal}"
        let rc=0;
    fi

    if [ $verbose -ne 0 ]; then
        echo "--> cat ${outName}"
        cat ${outName}
        #let occRspLength=`wc -w ${outName} | awk '{print $1}'`
        #echo "<-- $occRspLength bytes"
    fi

    if [ $rc -ne 0 ]; then
        echo -e "${red}ERROR: failed sending POLL to OCC via IPMI/BMC (rc=$rc)${normal}"
        exit 8
    fi
    if [ -e "${outName}" ]; then
        options=""
        if [ $verbose -eq 0 ]; then
            options="-q"
        else
            echo -e "${bold}==> /afs/rch/usr2/cjcain/bin/asm2bin.new -s:1 $options ${outName}${normal}"
        fi
        /afs/rch/usr2/cjcain/bin/asm2bin.new -s:1 $options  ${outName}
        if [ -e "${outName}.bin" ]; then
            binfile="${outName}.bin"
        else
            echo -e "${red}ERROR: ${outName}.bin not found...${normal}"
            exit 7
        fi
    else
        echo -e "${red}ERROR: ${outName} not found...${normal}"
        exit 9
    fi
} # end sendOccCmdBmc()

function sendOccCmd
{
    cmd=$1
    data=$2
    let dataLength=${#data}/2
    dataLenStr=$(printf "%04x" $dataLength)
    # Checksum Calculation
    let csum=0x"$sequence"+0x"$cmd";
    let csum="$csum + ( $dataLength >> 8 ) + ( $dataLength & 0xFF )"
    let index=0
    while [ $index -lt ${#data} ]; do
        echo "adding $csum+${data:$index:2}"
        let csum=$csum+0x${data:$index:2}
        let index=$index+2
    done
    csumStr=$(printf "%04x" $csum)

    # First, store the command in command buffer in SRAM
    if [ $verbose -ne 0 ]; then
        echo -e "${bold}==> putsram $cmd_sram_addr ${sequence}${cmd}${dataLenStr}${data}${csumStr} -p${occ} -ch $channel $quiet  (write $cmd command to OCC SRAM)${normal}"
    fi
    putsram $cmd_sram_addr ${sequence}${cmd}${dataLenStr}${data}${csumStr} -p${occ} -ch $channel $quiet
    let rc=$?
    if [ $rc -ne 0 ]; then
        echo -e "${red}ERROR: failed writing $cmd command to OCC SRAM (rc=$rc)${normal}"
        exit 1;
    fi

    # Second, send a doorbell to OCC
    if [ $verbose -ne 0 ]; then
        echo -e "${bold}==> putscom pu $doorbell_addr 0101000000000000 -p${occ} $quiet  (send doorbell to OCC)${normal}"
    fi
    putscom pu $doorbell_addr 0101000000000000 -p${occ} $quiet
    let rc=$?
    if [ $rc -ne 0 ]; then
        echo -e "${red}ERROR: failed sendingn doorbell to OCC (rc=$rc)${normal}"
    fi

    # Third, collect response from response buffer in SRAM
    if [ $verbose -ne 0 ]; then
        echo -e "${bold}==> getsram $rsp_sram_addr 4096 -p${occ} -ch $channel $quiet  (read $cmd response from SRAM)${normal}"
    fi
    if [ -e "${binfile}" ]; then
        rm ${binfile}
    fi
    getsram $rsp_sram_addr 4096 -p${occ} -ch $channel $quiet -fb ${binfile}
    let rc=$?
    if [ $rc -ne 0 ]; then
        echo -e "${red}ERROR: failed reading $cmd response from SRAM (rc=$rc)${normal}"
        exit 2;
    fi

    ### Response Header
    let offset=0
    let rspSeq=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`
    let expSeq=0x"${sequence}"
    if [ $rspSeq -eq $expSeq ]; then
        printf "Rsp Sequence: 0x%02X\n" $rspSeq;
    else
        echo -e "${red}"
        printf "Rsp Sequence: 0x%02X  MISMATCH - expected 0x%02X\n" $rspSeq $sequence;
        echo -e "${normal}"
        let seqMismatch=1
    fi
    let offset=$offset+1
    let rspCmd=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`
    printf "     Command: 0x%02X\n" $rspCmd;
    let offset=$offset+1
    let rspStatus=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`
    printf "      Status: 0x%02X\n" $rspStatus;
    let offset=$offset+1
    let rspLength=0x`hexdump -s $offset -n 2 -e '2/1 "%02X"' ${binfile}`
    printf "      Length: 0x%04X\n" $rspLength;
    let offset=$offset+2
    echo   "        Data:"
    hexdump -C -s $offset ${binfile}

} # end sendOccCmd()


function send_occ_cmd
{
    #if [ -z "$bmc" ] && [ -z "$inputfile" ] && [ -n "$occ_cmd" ]; then
    # Clean up rsp file if still exists
    if [ -e "${binfile}" ]; then
        rm -f ${binfile}
    fi

    if [ "$interface" == "cronus" ]; then
        echo -e "${blue}Sending 0x$occ_cmd command to OCC${occ} (via cronus)...${normal}"
        # First, store the command in command buffer in SRAM
        let cmd_data_length=${#occ_cmd_data}/2
        let csum=0x"$sequence"+0x$occ_cmd+$cmd_data_length; # seq# + cmd + length
        #TODO: Checksums for lengths > 255 bytes!!
        if [ $cmd_data_length -gt 0 ]; then
            for ((i = 0; i < ${#occ_cmd_data}; i+=2)); do
                let byte=0x${occ_cmd_data:$i:2}
                let csum=$csum+$byte
            done
        fi
        cmdString=`printf "%s%s%04X${occ_cmd_data}%04X" $sequence $occ_cmd $cmd_data_length $csum`
        if [ $verbose -ne 0 ]; then
            echo -e "${bold}==> putsram $cmd_sram_addr ${cmdString} -p${occ} -ch $channel $quiet  (write $occ_cmd command to OCC SRAM)${normal}"
        fi
        putsram $cmd_sram_addr ${cmdString} -p${occ} -ch $channel $quiet
        let rc=$?
        if [ $rc -ne 0 ]; then
            echo -e "${red}ERROR: failed writing $occ_cmd command to OCC SRAM (rc=$rc)${normal}"

            echo -e "${bold}==> getsram $rsp_sram_addr 128 -all -ch $channel $quiet  (read OCC rsp buffer from SRAM)${normal}"
            getsram $rsp_sram_addr -ch 3 128 -all

            exit 1;
        fi

        # Second, send a doorbell to OCC
        if [ $verbose -ne 0 ]; then
            echo -e "${bold}==> putscom pu $doorbell_addr 0101000000000000 -p${occ} $quiet  (send doorbell to OCC)${normal}"
        fi
        putscom pu $doorbell_addr 0101000000000000 -p${occ} $quiet
        let rc=$?
        if [ $rc -ne 0 ]; then
            echo -e "${red}ERROR: failed sending doorbell to OCC (rc=$rc)${normal}"
        fi

        # Third, collect response from response buffer in SRAa
        if [ $verbose -ne 0 ]; then
            echo -e "${bold}==> getsram $rsp_sram_addr 1024 -p${occ} -ch $channel $quiet  (read POLL response from SRAM)${normal}"
        fi
        getsram $rsp_sram_addr 1024 -p${occ} -ch $channel $quiet -fb ${binfile}
        let rc=$?
        if [ $rc -ne 0 ]; then
            echo -e "${red}ERROR: failed reading POLL response from SRAM (rc=$rc)${normal}"
            exit 2;
        fi
    elif [ "$interface" == "opal" ]; then
        echo -e "${blue}Sending 0x$occ_cmd command to OCC${occ} (via opal-prd)...${normal}"
        # First, store the command in command buffer in SRAM
        let cmd_data_length=${#occ_cmd_data}/2
        let csum=0x"$sequence"+0x$occ_cmd+$cmd_data_length; # seq# + cmd + length

        opal_occ_cmd_data=`printf "0x%02X 0x%s" $occ $occ_cmd`
        if [ $cmd_data_length -gt 0 ]; then
            for ((i = 0; i < ${#occ_cmd_data}; i+=2)); do
                let byte=0x${occ_cmd_data:$i:2}
                opal_occ_cmd_data=`printf "%s 0x%02X" "$opal_occ_cmd_data" $byte`
            done
        fi

        if [ $verbose -ne 0 ]; then
            echo -e "${bold}==> $sudo opal-prd --expert-mode htmgt-passthru 0x03 $opal_occ_cmd_data${normal}"
        fi
        rsp_file="/tmp/occtoolp9_rsp.txt"
        $sudo opal-prd --expert-mode htmgt-passthru 0x03 $opal_occ_cmd_data | sed 's/\s//g' > $rsp_file
        # get status of first cmd in piped cmdline
        let rc=${PIPESTATUS[0]}
        #let rc=$?
        if [ $rc -ne 0 ]; then
            echo -e "${red}ERROR: opal-prd passthrough failed sending $occ_cmd command to OCC$occ (rc=$rc)${normal}"
            if [ -e /var/log/syslog ]; then
                echo "==> tail /var/log/syslog"
                $sudo tail /var/log/syslog
            fi
            exit 30
        else
            #echo "Parsing poll data:"
            #./occ_poll -pt -f /tmp/htmgt.bin
            echo "Converting $rsp_file to $binfile..."
            convertToBinary $rsp_file $binfile
        fi
    else
        if [ -z "$inputfile" ]; then
            echo -e "${blue}Polling OCC${occ} (via BMC)...${normal}"
            sendOccCmdBmc
        fi
    fi
} # end send_occ_cmd()


function parse_rsp_poll
{
    # Response Data
    let rstatus="0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`"
    flags="";
    let bitset="$rstatus & 0x80";
    if [ $bitset -ne 0 ]; then flags=" Master"; fi
    let bitset="$rstatus & 0x40";
    if [ $bitset -ne 0 ]; then flags="$flags FIRMaster"; fi
    let bitset="$rstatus & 0x10";
    if [ $bitset -ne 0 ]; then flags="$flags OCCPmcrOwner"; fi
    let bitset="$rstatus & 0x08";
    if [ $bitset -ne 0 ]; then flags="$flags AttnEnabled"; fi
    let bitset="$rstatus & 0x02";
    if [ $bitset -ne 0 ]; then flags="$flags ObsReady"; fi
    let bitset="$rstatus & 0x01";
    if [ $bitset -ne 0 ]; then flags="$flags ActReady"; fi
    if [ -n "$flags" ]; then flags="($flags)"; fi
    printf "       Status: 0x%02X $flags\n" $rstatus
    let offset=$offset+1
    let xstatus="0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`"
    flags=""
    let bitset="$xstatus & 0x80";
    if [ $bitset -ne 0 ]; then flags=" DVS-OT"; fi
    let bitset="$xstatus & 0x40";
    if [ $bitset -ne 0 ]; then flags="$flags DVS-power"; fi
    let bitset="$xstatus & 0x20";
    if [ $bitset -ne 0 ]; then flags="$flags MemThrottle-OT"; fi
    let bitset="$xstatus & 0x10";
    if [ $bitset -ne 0 ]; then flags="$flags QckPwrDrop"; fi
    if [ -n "$flags" ]; then
        flags="($flags )";
        echo -en "$bold";
    fi
    printf "    ExtStatus: 0x%02X $flags\n" $xstatus
    echo -en "$normal";
    let offset=$offset+1
    hexdump -s $offset -n 1 -e '"         OCCs: 0x%02X\n"' ${binfile}
    let offset=$offset+1
    hexdump -s $offset -n 1 -e '"      CfgNeed: 0x%02X\n"' ${binfile}
    let offset=$offset+1
    let state="0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`"
    flags="UNKNOWN";
    if [ $state -eq 1 ]; then flags="STANDBY";
    elif [ $state -eq 2 ]; then flags="OBSERVATION";
    elif [ $state -eq 3 ]; then flags="ACTIVE";
    elif [ $state -eq 4 ]; then
        flags="SAFE";
        echo -en "$red";
    elif [ $state -eq 5 ]; then flags="CHARACTERIZATION";
    fi
    printf "        State: 0x%02X ($flags)\n" $state
    echo -en "$normal";
    let offset=$offset+1
    hexdump -s $offset -n 2 -e '"     reserved: 0x" 2/1 "%02X" "\n"' ${binfile}
    let offset=$offset+2
    let elogId=0x`hexdump -s $offset -n 1 -e '1/1 "%02X"' ${binfile}`
    if [ $elogId -ne 0 ]; then
        echo -en "$red";
    fi
    printf "       ElogID: 0x%02X\n" $elogId
    let offset=$offset+1
    let elogAddr="0x`hexdump -s $offset -n 4 -e '4/1 "%02X"' ${binfile}`"
    printf "     ElogAddr: 0x%08X\n" $elogAddr
    let offset=$offset+4
    let elogLen="0x`hexdump -s $offset -n 2 -e '2/1 "%02X"' ${binfile}`"
    printf "     Elog Len: 0x%04X\n" $elogLen
    let offset=$offset+2
    echo -en "$normal";
    hexdump -s $offset -n 2 -e '" WOF Disabled: 0x" 2/1 "%02X" "\n"' ${binfile}
    let offset=$offset+2
    if [ $occRspLength -gt 17 ]; then
        hexdump -s $offset -n 16 -e '"   Code Level: " 16 "%_p" "\n"' ${binfile}
        let offset=$offset+16
        hexdump -s $offset -n 6 -e '"   Sensor Tag: " 6 "%_p" "\n"' ${binfile}
        let offset=$offset+6

        if [ $occRspLength -gt 39 ]; then
            #let offset=43
            let numSensorBlocks="0x`hexdump -s $offset -n 1 -e '4/1 "%02X"' ${binfile}`"
            echo "Sensor Blocks: $numSensorBlocks"
            let offset=$offset+1
            let sensorVers="0x`hexdump -s $offset -n 1 -e '4/1 "%02X"' ${binfile}`"
            echo "  Sensor Vers: $sensorVers"
            let offset=$offset+1

            if [ $verbose -ne 0 ]; then
                echo "Raw Sensor Data:"
                hexdump -C -s $offset ${binfile}
            fi

            while [ $numSensorBlocks -gt 0 ]; do
                sensor="`hexdump -s $offset -n 4 -e '4/1 "%_u"' ${binfile}`"
                let offset=$offset+5
                let sensorFormat="0x`hexdump -s $offset -n 1 -e '4/1 "%02X"' ${binfile}`"
                let offset=$offset+1
                let sensorLength="0x`hexdump -s $offset -n 1 -e '4/1 "%02X"' ${binfile}`"
                let offset=$offset+1
                let numSensors="0x`hexdump -s $offset -n 1 -e '4/1 "%02X"' ${binfile}`"
                let offset=$offset+1

                # TEMP: degrees C, FREQ: MHz, POWR: 4Byte tag, 4Byte accumulator, 2Byte current reading
                echo -e  "       ${bold}Sensor: $sensor - format:$sensorFormat / $numSensors sensors ($sensorLength bytes/sensor)${normal}"
                indent="       ";

                if [ $G_powerArch == "p9" ]; then
                    if [ "$sensor" == "TEMP" ]; then
                        echo "$indent  SSSSSSSS FF TT  (SSSS = Sensor ID, FF is FRU type, TT is temp in C)";
                    elif [ "$sensor" == "FREQ" ]; then
                        echo "$indent  SSSSSSSS FFFF  (SSSS = Sensor ID, FFFF is freq in MHz)";
                    elif [ "$sensor" == "POWR" ]; then
                        if [ $sensorFormat -ne $((0xA0)) ]; then
                            echo "$indent  SSSSSSSS FF CH rrrr TTTTTTTT AAAAAAAAAAAAAAAA CCCC  (SS=Sensor ID, FF=Function ID,"
                            echo "               CH=APSS Channel, TT=Update Tag, AA=Accumulator, CC=current reading (W)"
                        else
                            echo "$indent          SSSSSSSS UUUU CCCC TTTTTTTT AAAAAAAAAAAAAAAA  (SS=Sensor ID,"
                            echo "                UU=Update Time (us), CC=current reading (W), TT=Update Tag, AA=Accumulator)"
                        fi
                    elif [ "$sensor" == "EXTN" ]; then
                        echo "$indent  NNNNNNNN FF 00 DDDDDDDDDDDD (NNNN = Name/Sensor ID, FF is flags, DDDD is value)";
                    fi
                else
                    #p8
                    if [ "$sensor" == "TEMP" ]; then
                        echo "$indent  SSSS TTTT  (SSSS = Sensor ID, TTTT is temp in C)";
                    elif [ "$sensor" == "FREQ" ]; then
                        echo "$indent  SSSS FFFF  (SSSS = Sensor ID, FFFF is freq in MHz)";
                    elif [ "$sensor" == "POWR" ]; then
                        echo "$indent  SSSS TTTTTTTT AAAAAAAA CCCC  (SS = Sensor ID, TT = Update Tag";
                        echo "                                AA = Accumulator, CC = current reading in Watts)";
                    fi
                fi
                while [ $numSensors -gt 0 ]; do
                    if [ $G_powerArch == "p9" ]; then
                        #hexdump -s $offset -n $sensorLength -e '"  " 16/1 "%02X" "\n"' ${binfile}
                        if [ "$sensor" == "TEMP" ]; then
                            hexdump -s $offset -n $sensorLength -e '"         " 4/1 "%02X" " " 1/1 "%02X" " " 1/1 "%02X""\n"' ${binfile}
                            #hexdump -s $offset -n $sensorLength -e '"=0x" 2/1 "%02X" " "1/2 "%d" "\n"' ${binfile}
                        elif [ "$sensor" == "FREQ" ]; then
                            hexdump -s $offset -n $sensorLength -e '"         " 4/1 "%02X" " " 2/1 "%02X" "\n"' ${binfile}
                        elif [ "$sensor" == "POWR" ]; then
                            if [ $sensorFormat -ne $((0xA0)) ]; then
                                hexdump -s $offset -n $sensorLength -e '"         " 4/1 "%02X" " " 1/1 "%02X" " " 1/1 "%02X" " " 2/1 "%02X" " " 4/1 "%02X" " " 8/1 "%02X" " " 2/1 "%02X" "\n"' ${binfile}
                            else
                                # APSS-less
                                let reading_offset=$offset+6
                                let power="0x`hexdump -s $reading_offset -n 2 -e '2/1 "%02X"' ${binfile}`";
                                hexdump -s $offset -n 20       -e '"         System: " 4/1 "%02X" " " 2/1 "%02X" " " 2/1 "%02X" " " 4/1 "%02X" " " 8/1 "%02X" " ('$power' W)\n"' ${binfile}
                                let power_offset=$offset+20
                                let reading_offset=$power_offset+6
                                let power="0x`hexdump -s $reading_offset -n 2 -e '2/1 "%02X"' ${binfile}`";
                                hexdump -s $power_offset -n 20 -e '"      Processor: " 4/1 "%02X" " " 2/1 "%02X" " " 2/1 "%02X" " " 4/1 "%02X" " " 8/1 "%02X" " ('$power' W)\n"' ${binfile}
                                let power_offset=$power_offset+20
                                let power="0x`hexdump -s $power_offset -n 2 -e '2/1 "%02X"' ${binfile}`";
                                hexdump -s $power_offset -n 14 -e '"            Vdd:               " 2/1 "%02X" " " 4/1 "%02X" " " 8/1 "%02X" " ('$power' W)\n"' ${binfile}
                                let power_offset=$power_offset+14
                                let power="0x`hexdump -s $power_offset -n 2 -e '2/1 "%02X"' ${binfile}`";
                                hexdump -s $power_offset -n 14 -e '"            Vdn:               " 2/1 "%02X" " " 4/1 "%02X" " " 8/1 "%02X" " ('$power' W)\n"' ${binfile}
                            fi
                        elif [ "$sensor" == "CAPS" ]; then
                            let capoffset=$offset
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent  Current Power Cap: %6d Watts\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent      Current Power: %6d Watts (output power)\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent        N Power Cap: %6d Watts (cap without redundant power)\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent      Max Power Cap: %6d Watts\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent Hard Min Power Cap: %6d Watts\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            if [ $sensorFormat -gt 2 ]; then
                                printf "$indent Soft Min Power Cap: %6d Watts\n" $pcap;
                                let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                                let capoffset+=2
                            fi
                            printf "$indent     User Power Cap: %6d Watts\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 1 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=1
                            printf "$indent User Power Limit Source: %d\n" $pcap;
                        elif [ "$sensor" == "EXTN" ]; then
                            hexdump -s $offset -n $sensorLength -e '"         " 4/1 "%02X" " " 1/1 "%02X" " " 1/1 "%02X" " " 6/1 "%02X""\n"' ${binfile}
                        else
                            echo -e "${red}ERROR: Unknown sensor eye catcher: $sensor${normal}";
                        fi
                    else
                        #p8
                        #hexdump -s $offset -n $sensorLength -e '"  " 16/1 "%02X" "\n"' ${binfile}
                        if [ "$sensor" == "TEMP" ]; then
                            hexdump -s $offset -n $sensorLength -e '"         " 2/1 "%02X" " " 2/1 "%02X" "\n"' ${binfile}
                            #hexdump -s $offset -n $sensorLength -e '"=0x" 2/1 "%02X" " "1/2 "%d" "\n"' ${binfile}
                        elif [ "$sensor" == "FREQ" ]; then
                            hexdump -s $offset -n $sensorLength -e '"         " 2/1 "%02X" " " 2/1 "%02X" "\n"' ${binfile}
                        elif [ "$sensor" == "POWR" ]; then
                            hexdump -s $offset -n $sensorLength -e '"         " 2/1 "%02X" " " 4/1 "%02X" " " 4/1 "%02X" " " 2/1 "%02X" "\n"' ${binfile}
                        elif [ "$sensor" == "CAPS" ]; then
                            let capoffset=$offset
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent  Current Power Cap: %6d Watts\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent      Current Power: %6d Watts (output power)\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent        N Power Cap: %6d Watts (cap without redundant power)\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent      Max Power Cap: %6d Watts\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent      Min Power Cap: %6d Watts\n" $pcap;
                            let pcap="0x`hexdump -s $capoffset -n 2 -e '4/1 "%02X"' ${binfile}`";
                            let capoffset+=2
                            printf "$indent     User Power Cap: %6d Watts\n" $pcap;
                        else
                            echo -e "${red}ERROR: Unknown sensor eye catcher: $sensor${normal}";
                        fi
                    fi
                    let offset=$offset+$sensorLength
                    let numSensors=$numSensors-1
                done
                let numSensorBlocks=$numSensorBlocks-1
            done # end sensors
        else
            echo "...rsp truncated (only read $occRspLength bytes)"
        fi
    else
        echo "...rsp truncated (only read $occRspLength bytes)"
    fi
} # end parse_rsp_poll()


function parse_rsp_mfg
{
    # Response Data
    let subcmd="0x${occ_cmd_data:0:2}"

    if [ $subcmd -eq 5 ]; then # LIST SENSORS
        printf "  MFG Sub Cmd: 0x%02X  (List Sensors)\n" $subcmd

        let more_sensors="0x`hexdump -s $offset -n 1 -e '4/1 "%02X"' ${binfile}`"
        if [ $skip_header -eq 0 ] || [ $verbose -ne 0 ]; then
            printf "   truncated?: %d  (1 = list was truncated due to space)\n" $more_sensors
        fi
        let offset=$offset+1
        let count="0x`hexdump -s $offset -n 1 -e '4/1 "%02X"' ${binfile}`"
        printf "  Num Sensors: %d\n" $count
        let offset=$offset+1
        let index=0
        while [ $index -lt $count ];
        do
            let index=$index+1
            let guid="0x`hexdump -s $offset -n 2 -e '4/1 "%02X"' ${binfile}`"
            #printf "         GUID[%2d]: 0x%04X\n" $index $guid
            let offset=$offset+2
            name="`hexdump -s $offset -n 16 -v -e '"%_p"' ${binfile}`"
            #printf "  Sensor Name[%2d]: %s\n" $index $name
            let offset=$offset+16
            let sample="0x`hexdump -s $offset -n 2 -e '4/1 "%02X"' ${binfile}`"
            #printf "Latest Sample[%2d]: %6d $units (0x%04X)\n" $index $sample $sample
            printf "    [%2d] GUID: 0x%04X / %s  Sample: %6d $units (0x%04X)\n" $index $guid $name $sample $sample
            let offset=$offset+2
            let last_sensor=$guid
        done
    elif [ $subcmd -eq 6 ]; then # GET SENSOR INFO
        printf "  MFG Sub Cmd: 0x%02X  (Get Sensor Info)\n" $subcmd

        let unit_offset=$offset+29
        units="`hexdump -s $unit_offset -n  4 -v -e '"%_p"' ${binfile} | sed 's/\\.//g'`"

        let guid="0x`hexdump -s $offset -n 2 -e '4/1 "%02X"' ${binfile}`"
        printf "         GUID: 0x%04X\n" $guid
        let offset=$offset+2
        let sample="0x`hexdump -s $offset -n 2 -e '4/1 "%02X"' ${binfile}`"
        printf "Latest Sample: %6d $units (0x%04X)\n" $sample $sample
        let offset=$offset+2
        let sensStatus="0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`"
        printf "       Status: 0x%02X\n" $sensStatus
        let offset=$offset+1
        let sample="0x`hexdump -s $offset -n 4 -e '4/1 "%02X"' ${binfile}`"
        printf "  Accumulator: %d  (0x%08X)\n" $sample $sample
        let offset=$offset+4
        let sample="0x`hexdump -s $offset -n 2 -e '4/1 "%02X"' ${binfile}`"
        printf "   Min Sample: %6d $units (0x%04X)\n" $sample $sample
        let offset=$offset+2
        let sample="0x`hexdump -s $offset -n 2 -e '4/1 "%02X"' ${binfile}`"
        printf "   Max Sample: %6d $units (0x%04X)\n" $sample $sample
        let offset=$offset+2
        name="`hexdump -s $offset -n 16 -v -e '"%_p"' ${binfile}`"
        echo   "  Sensor Name: $name"
        let offset=$offset+16
        #units="`hexdump -s $offset -n  4 -v -e '"%_p"' ${binfile}`"
        echo "        Units: $units"
        let offset=$offset+4
        let sample="0x`hexdump -s $offset -n 4 -e '4/1 "%02X"' ${binfile}`"
        printf "  Update Freq: 0x%08X\n" $sample
        let offset=$offset+4
        let sample="0x`hexdump -s $offset -n 4 -e '4/1 "%02X"' ${binfile}`"
        printf " Scale Factor: 0x%08X\n" $sample
        let offset=$offset+4
        let sample="0x`hexdump -s $offset -n 2 -e '4/1 "%02X"' ${binfile}`"
        printf " Sen Location: 0x%04X\n" $sample
        let offset=$offset+2
        let sample="0x`hexdump -s $offset -n 2 -e '4/1 "%02X"' ${binfile}`"
        printf "  Sensor Type: 0x%04X\n" $sample
        let offset=$offset+2
    else
        printf "  MFG Sub Cmd: 0x%02X\n" $subcmd
        let remaining_bytes=$end_of_rsp_data-$offset
        #hexdump -C    -s $offset ${binfile}
        hexdump -C -s $offset -n $remaining_bytes ${binfile}
        let offset=$offset+$remaining_bytes
    fi

    let remaining_bytes=$end_of_rsp_data-$offset
    if [ $remaining_bytes -gt 0 ]; then
        echo "Remaining data:"
        hexdump -C -s $offset -n $remaining_bytes ${binfile}
    fi

} # end parse_rsp_mfg()


function handle_occ_rsp
{
    if [ -e "$binfile" ]; then
        let occRspLength=`wc -c ${binfile} | awk '{print $1}'`
    else
        let occRspLength=0
    fi
    if [ "$interface" == "cronus" ]; then
        if [ $skip_header -eq 0 ] || [ $verbose -ne 0 ]; then
            echo ""
            if [ "$occ_cmd" == "00" ]; then
                echo -e "${blue}POLL Response:${normal}"
            else
                echo -e "${blue}$occ_cmd Command Response:${normal}"
            fi

            if [ $verbose -ne 0 ]; then
                #echo "==> wc -c ${binfile} | awk '{print $1}'"
                echo "... $occRspLength bytes"
            fi
        fi

        if [ $verbose -ne 0 ]; then
            echo "--> $binfile is $occRspLength bytes long"
            echo "Raw Rsp Header: (32 bytes)"
            hexdump -C -n 32 ${binfile}
        fi
    fi

    ### Response Header
    let offset=0
    let rspStatus=0
    if [ "$interface" == "opal" ]; then
        let offset=0
        let end_of_rsp_data=$occRspLength
        let length=$end_of_rsp_data
    elif [ -z $bmc ] && [ -z "$inputfile" ]; then
        let expSeq=0x"${sequence}"
        let rspSeq=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`
        if [ $rspSeq -eq $expSeq ]; then
            if [ $skip_header -eq 0 ] || [ $verbose -ne 0 ]; then
                printf " Rsp Sequence: 0x%02X\n" $rspSeq;
            fi
        else
            echo -e -n "${red}"
            printf " Rsp Sequence: 0x%02X  MISMATCH - expected 0x%02X\n" $rspSeq $expSeq;
            echo -e -n "${normal}"
            let seqMismatch=1
        fi
        let offset=$offset+1
        if [ $skip_header -eq 0 ] || [ $verbose -ne 0 ]; then
            hexdump -s $offset -n 1 -e '"      Command: 0x%02X\n"' ${binfile}
        fi
        let offset=$offset+1
        if [ $skip_header -eq 0 ] || [ $verbose -ne 0 ]; then
            hexdump -s $offset -n 1 -e '"   RSP Status: 0x%02X\n"' ${binfile}
        fi
        let rspStatus=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`;
        let offset=$offset+1

        let length="0x`hexdump -s $offset -n 2 -e '2/1 "%02X"' ${binfile}`"
        let offset=$offset+2
        let end_of_rsp_data=$offset+$length
    elif [ -n "$inputfile" ]; then
        if [ $passThru -eq 0 ]; then
            # USED TO PARSE POLL RESPONSES FROM syslog file (must be formated as just hex with spaces between each byte)
            let rspSeq=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`
            printf " Rsp Sequence: 0x%02X\n" $rspSeq;
            let offset=$offset+1
            hexdump -s $offset -n 1 -e '"      Command: 0x%02X\n"' ${binfile}
            let offset=$offset+1
            hexdump -s $offset -n 1 -e '"   RSP Status: 0x%02X\n"' ${binfile}
            let rspStatus=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`;
            let offset=$offset+1
            let length="0x`hexdump -s $offset -n 2 -e '2/1 "%02X"' ${binfile}`"
            printf "       Length: 0x%04X\n" $length
            let offset=$offset+2
        else
            let offset=0
            let end_of_rsp_data=$occRspLength
        fi
    elif [ $sendOccInstance -ne 0 ]; then
        let compCode=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`
        compCodeString="(Success)"
        if [ $compCode -ne 0 ]; then
            compCodeString="(Failure)"
        fi
        printf "AMI Comp Code: 0x%02X $compCodeString\n" $compCode
        let offset=$offset+1
        printf "AMI Rsp Length: %d bytes\n" $occRspLength
    else
        hexdump -s $offset -n 1 -e '"   RSP Status: 0x%02X\n"' ${binfile}
        let rspStatus=0x`hexdump -s $offset -n 1 -e '"%02X"' ${binfile}`;
        let offset=$offset+1
    fi

    if [ $skip_header -eq 0 ] || [ $verbose -ne 0 ]; then
        printf "  Data Length: 0x%04X\n" $length
    fi

    # Check for exception
    let key="$rspStatus & 0xF0"
    if [ $key -eq 224 ]; then
        printf "${red}OCC EXCEPTION STATUS FOUND: 0x%02X ${normal}\n" $rspStatus;
        echo "Exception Data:"
        hexdump -s $offset -n $length -e '"         " 2/1 "%02X" " " 2/1 "%02X" "\n"' ${binfile}
        echo ""
        let seqMismatch=0
    fi

    let elogId=0
    if [ "$occ_cmd" == "00" ]; then # POLL COMMAND
        parse_rsp_poll
        if [ $elogId -ne 0 ]; then
            process_elog
        fi
    elif [ "$occ_cmd" == "53" ]; then # MFG COMMAND
        parse_rsp_mfg
    else
        # Dump response as hex data
        echo "Response Data:"
        let remaining_bytes=$end_of_rsp_data-$offset
        if [ $verbose -ne 0 ]; then
            # -v to display all repeated data (like remaining 00s)
            hexdump -C -v -s $offset -n $remaining_bytes ${binfile}
        else
            hexdump -C    -s $offset -n $remaining_bytes ${binfile}
        fi
    fi
} # end handle_occ_rsp()


function process_elog
{
    if [ -z $bmc ] && [ -z "$inputfile" ]; then
        echo ""
        printf "${bold}==> Retrieving elog from POLL (id 0x%02X, addr 0x%08X, len 0x%04X)${normal}\n" $elogId $elogAddr $elogLen;
        if [ $verbose -ne 0 ]; then
            printf "${bold}==> getsram %08X %d -p${occ} -ch $channel $quiet  (read ELOG data from SRAM)${normal}" $elogAddr $elogLen
        fi
        sramAddr=`printf "%08X" $elogAddr`
        echo "getsram $sramAddr $elogLen -p${occ} -ch $channel $quiet -fb elog.bin"
        getsram $sramAddr $elogLen -p${occ} -ch $channel $quiet -fb elog.bin
        let rc=$?
        if [ $rc -ne 0 ]; then
            echo -e "${red}ERROR: failed reading Elog data from SRAM (rc=$rc)${normal}"
            exit  3;
        fi
        if [ $verbose -ne 0 ]; then
            echo "Raw Elog:"
            hexdump -C -s 0 elog.bin
        else
            echo ""
        fi
        echo ""

        let offset=4
        hexdump -s $offset -n 1  -e '" Return Code: 0x2A%02X\n"' elog.bin
        let offset=$offset+1
        let sev="0x`hexdump -s $offset -n 1 -e '"%02X"' elog.bin`"
        flags="Unknown"
        if [ $sev -eq 0 ]; then flags="Informational";
        elif [ $sev -eq 1 ]; then flags="Recoverable";
        elif [ $sev -eq 2 ]; then flags="Unrecoverable"; fi
        printf "    Severity: 0x%02X ($flags)\n" $sev
        let offset=$offset+1
        flags=""
        let actions="0x`hexdump -s $offset -n 1 -e '"%02X"' elog.bin`"
        let bitset="$actions & 0x80";
        if [ $bitset -ne 0 ]; then flags="ResetRqd"; fi
        let bitset="$actions & 0x40";
        if [ $bitset -ne 0 ]; then flags="$flags SafeModeRqd"; fi
        if [ -n "$flags" ]; then flags="($flags)"; fi
        printf "     Actions: 0x%02X $flags\n" $actions
        let offset=$offset+5
        hexdump -s $offset -n 1  -e '" MaxCallouts: 0x%02X\n"' elog.bin
        let offset=$offset+1
        echo "    Callouts: tt cccccccccccccccc pp rrrr (type, callout, priority, rsvd)"
        hexdump -v -s $offset -n 72 -e '"              " 1/1 "%02X" " " 8/1 "%02X" " " 1/1 "%02X" " " 2/1 "%02X" "\n"' elog.bin
        let offset=$offset+72
        if [ $verbose -ne 0 ] || [ $purge -ne 0 ]; then
            echo "Remaining Elog Data:"
            #    hexdump -C -s $offset -n 64 -e '"%06.6_ax:  " 4/1 "%02X" " " 4/1 "%02X" " " 4/1 "%02X" " " 4/1 "%02X" "\n"' elog.bin
            hexdump -C -s $offset elog.bin
            #    hexdump -C -s $offset -n 64 -x elog.bin
        else
            echo "    (add -v parm to display full elog data OR -p to dump then purge elog)"
        fi

        rm elog.bin

        if [ $purge -ne 0 ]; then
            printf "${bold}==> Sending CLEAR ELOG command (id 0x%02X)${normal}\n" $elogId;
            if [ -z $bmc ] && [ -z "$inputfile" ]; then
                # First, store the clear elog command in command buffer in SRAM
                let csum=0xCC+0x12+0x01+$elogId
                clearElogCmd=`printf "cc120001%02X%04X" $elogId $csum`
                if [ $verbose -ne 0 ]; then
                    echo -e "${bold}==> putsram $cmd_sram_addr $clearElogCmd -p${occ} -ch $channel $quiet  (write CLEAR_ELOG command to OCC SRAM)${normal}"
                fi
                putsram $cmd_sram_addr $clearElogCmd -p${occ} -ch $channel $quiet
                let rc=$?
                if [ $rc -ne 0 ]; then
                    echo -e "${red}ERROR: failed writing CLEAR_ELOG command to OCC SRAM (rc=$rc)${normal}"
                    exit 1;
                fi

                # Second, send a doorbell to OCC
                if [ $verbose -ne 0 ]; then
                    echo -e "${bold}==> putscom pu $doorbell_addr 0101000000000000 -p${occ} $quiet  (send doorbell to OCC)${normal}"
                fi
                putscom pu $doorbell_addr 0101000000000000 -p${occ} $quiet
                let rc=$?
                if [ $rc -ne 0 ]; then
                    echo -e "${red}ERROR: failed sendingn doorbell to OCC (rc=$rc)${normal}"
                fi

                # Third, collect response from response buffer in SRAa
                if [ $verbose -ne 0 ]; then
                    echo -e "${bold}==> getsram $rsp_sram_addr 16 -p${occ} -ch $channel $quiet  (read CLEAR_ELOG response from SRAM)${normal}"
                fi
                getsram $rsp_sram_addr 16 -p${occ} -ch $channel $quiet
                let rc=$?
                if [ $rc -ne 0 ]; then
                    echo -e "${red}ERROR: failed reading CLEAR_ELOG response from SRAM (rc=$rc)${normal}"
                    exit 2;
                fi
            else
                if [ $verbose -ne 0 ]; then
                    echo -e "${bold}==> ipmitool -H $bmc -I lanplus -U $bmcId -P $bmcPw raw 0x3a 0x0d 0x12 $elogId${normal}"
                fi
                ipmitool -H $bmc -I lanplus -U $bmcId -P $bmcPw raw 0x3a 0x0d 0x12 $elogId
                let rc=$?
                if [ $rc -ne 0 ]; then
                    echo -e "${red}ERROR: failed sending CLEAR_ELOG $elogId to OCC via IPMI/BMC (rc=$rc)${normal}"
                    exit 8
                fi
            fi
        else
            printf "${bold}==> Ignoring elog from POLL (id 0x%02X, addr 0x%08X, len 0x%04X)${normal}\n" $elogId $elogAddr $elogLen;
        fi
    else
        printf "${red}Note: Unable to read elog data via IPMI, use cronus (id 0x%02X, addr 0x%08X, len 0x%04X)${normal}\n" $elogId $elogAddr $elogLen;
    fi
} # end process_elog()


function tmgt_info
{
    ### Dump HTMGT and OCC states and status
    echo "==> opal-prd --expert-mode htmgt-passthru 0x01"
    $sudo opal-prd --expert-mode htmgt-passthru 0x01 | sed 's/\s//g' > /tmp/htmgt.bin
    # get status of first cmd in piped cmdline
    let rc=${PIPESTATUS[0]}
    let inSafe=0
    let numOccs=0
    if [ $rc -eq 0 ]; then
        let lineno=0
        while read line; do
            if [ ${#line} -gt 2 ]; then
                let lineno=$lineno+1
                if [ ${lineno} -eq 1 ]; then
                    let numOccs=0x${line:0:2}
                    let master=0x${line:2:2}
                    let state=0x${line:4:2}
                    let targState=0x${line:6:2}
                    let count=0x${line:8:2}
                    let pstype=0x${line:10:2}
                    # 1 byte reserved
                    let safe=0x${line:14:2}
                    safeRc=${line:20:4}
                    let safeInst=0x${line:24:8}
                    if [ $state -eq 1 ]; then
                        stateText="STANDBY"
                    elif [ $state -eq 2 ]; then
                        stateText="OBSERV "
                    elif [ $state -eq 3 ]; then
                        stateText="ACTIVE "
                    elif [ $state -eq 4 ]; then
                        stateText="SAFE   "
                    elif [ $state -eq 5 ]; then
                        stateText="CHARACT"
                    elif [ $state -eq $((0x85)) ]; then
                        stateText="RESET  "
                    elif [ $state -eq $((0x87)) ]; then
                        stateText="TRANSIT"
                    elif [ $state -eq $((0x88)) ]; then
                        stateText="LOADING"
                    elif [ $state -eq $((0x89)) ]; then
                        stateText=""
                    else
                        stateText="UNDEFIN"
                    fi
                    if [ $safe -ne 0 ]; then
                        let inSafe=1
                        echo "HTMGT State: $stateText($(printf "0x%02X" $state)), $numOccs OCC (master:OCC$master), resetCount:$count,  (SAFE MODE: rc=0x${safeRc} / OCC$safeInst)"
                    else
                        echo "HTMGT State: $stateText($(printf "0x%02X" $state)), $numOccs OCC (master:OCC$master), resetCount:$count"
                    fi
                else
                    let inst=0x${line:0:2}
                    let state=0x${line:2:2}
                    let role=0x${line:4:2} #
                    let capab=0x${line:6:2} #
                    let occComm=0x${line:8:2}
                    # 3 bytes reserved
                    let failed=0x${line:16:2}
                    let needReset=0x${line:18:2}
                    let resetReason=0x${line:20:2} #
                    let count=0x${line:22:2}
                    pollRsp=${line:24:8}
                    type="Slave "
                    if [ $master -eq $inst ]; then
                        type="Master"
                    fi
                    if [ $state -eq 1 ]; then
                        stateText="STANDBY"
                    elif [ $state -eq 2 ]; then
                        stateText="OBSERV "
                    elif [ $state -eq 3 ]; then
                        stateText="ACTIVE "
                    elif [ $state -eq 4 ]; then
                        stateText="SAFE   "
                    elif [ $state -eq 5 ]; then
                        stateText="CHARACT"
                    elif [ $state -eq $((0x85)) ]; then
                        stateText="RESET  "
                    elif [ $state -eq $((0x87)) ]; then
                        stateText="TRANSIT"
                    elif [ $state -eq $((0x88)) ]; then
                        stateText="LOADING"
                    elif [ $state -eq $((0x89)) ]; then
                        stateText=""
                    else
                        stateText="UNDEFIN"
                    fi
                    flags=""
                    if [ $occComm -eq 0 ]; then
                        flags="${flags}NOCOMM "
                    elif [ $failed -ne 0 ]; then
                        flags="${flags}FAILED "
                    elif [ $needReset -ne 0 ]; then
                        flags="${flags}NEEDSRESET "
                    fi
                    echo "OCC${inst}: $type $stateText($(printf "0x%02X" $state)) resetCount:$count  $flags pollRsp:0x${pollRsp}..."
                fi
            fi
        done < /tmp/htmgt.bin
    else
        echo "ERROR: opal-prd occ passthru command failed with rc=$rc"
    fi

    #01000903 00000000 00000000 00000000
    #00030101 01000000 00000000 c3000100

    if [ $rc -eq 0 ] && [ $inSafe -ne 0 ]; then
        let rc=4
    fi
} # end tmgt_info()


function occ_ffdc
{
    echo ""
    let occ=0
    if [ -n "$2" ]; then
        let occ=$2
        let numOccs=1
    fi

    while [ $numOccs -gt 0 ]; do
        echo "`hostname` OCC$occ FFDC Data:  (`date`)"
        echo "==> opal-prd --expert-mode htmgt-passthru 0x03 $occ 0x42 0x00"
        $sudo opal-prd --expert-mode htmgt-passthru 0x03 $occ 0x42 0x00
        let rc=${PIPESTATUS[0]}
        echo ""
        let occ=$occ+1
        let numOccs=$numOccs-1
    done
} # end occ_ffdc()


function occ_trace
{
    echo ""
    let occ=0
    let numOccs=1
    if [ -n "$2" ]; then
        let occ=$2
    fi

    while [ $numOccs -gt 0 ]; do
        echo "`hostname` OCC$occ Trace:  (`date`)"
        occ_cmd="40"
        occ_cmd_data="03010045525200" # ERR traces
        send_occ_cmd
        handle_occ_rsp
        occ_cmd_data="030100494D5000" # IMP traces
        send_occ_cmd
        handle_occ_rsp
        occ_cmd_data="030100494E4600" # INF traces
        send_occ_cmd
        handle_occ_rsp
        let occ=$occ+1
        let numOccs=$numOccs-1
    done
} # end occ_trace()


if [ $passThru -eq 1 ] && [ -z "$inputfile" ]; then
    # No input file specified, check clipboard for hex data to parse
    inputfile="/tmp/occtool.temp.txt"
    echo "Extracting copy buffer into ${inputfile}..."
    xselection PRIMARY > $inputfile
fi

if [ $passThru -eq 1 ] && [ -n "$inputfile" ]; then
    convertToBinary $inputfile $binfile
fi

#sendOccCmd 00 10
#exit

if [ -n "$action" ]; then
    if [ "$action" == "SensorList" ]; then
        let more_sensors=1
        let first_sensor=0
        let skip_header=1
        while [ $more_sensors -ne 0 ]; do
            occ_cmd="53"
            occ_cmd_data=`printf "0500%04X%02XFFFFFFFF" $first_sensor $non_zero_sensors` # all sensors, all locations, all types
            #echo "==> $occ_cmd $occ_cmd_data"
            send_occ_cmd
            handle_occ_rsp # will update more_sensors and last_sensor
            let first_sensor=$last_sensor+1
        done;
    elif [ "$action" == "OCCFFDC" ]; then
        occ_ffdc
    elif [ "$action" == "TMGTInfo" ]; then

        if [ "$interface" == "opal" ]; then
            tmgt_info
        else
            echo -e "${red}ERROR: Command only supported with opal-prd${normal} (not $interface)"
        fi
    elif [ "$action" == "trace" ]; then
        occ_trace
    elif [ "$action" == "CmdRspBuffer" ]; then
        let buffer_size=128
        echo "OCC Command Buffers from SRAM ($cmd_sram_addr):"
        if [ $verbose -ne 0 ]; then
            echo -e "${bold}==> getsram $cmd_sram_addr $buffer_size -pall -ch $channel $quiet  (read $cmd response from SRAM)${normal}"
        fi
        if [ -e "${binfile}" ]; then
            rm ${binfile}
        fi
        getsram $cmd_sram_addr $buffer_size -pall -ch $channel $quiet
        let rc=$?
        if [ $rc -ne 0 ]; then
            echo -e "${red}ERROR: failed reading response buffer from SRAM (rc=$rc)${normal}"
            exit 2;
        fi
        echo "OCC Response Buffers from SRAM ($rsp_sram_addr):"
        if [ $verbose -ne 0 ]; then
            echo -e "${bold}==> getsram $rsp_sram_addr $buffer_size -pall -ch $channel $quiet  (read $cmd response from SRAM)${normal}"
        fi
        if [ -e "${binfile}" ]; then
            rm ${binfile}
        fi
        getsram $rsp_sram_addr $buffer_size -pall -ch $channel $quiet
        let rc=$?
        if [ $rc -ne 0 ]; then
            echo -e "${red}ERROR: failed reading response buffer from SRAM (rc=$rc)${normal}"
            exit 2;
        fi
    fi
else
    if [ $passThru -eq 1 ]; then
        occ_cmd="00"
        handle_occ_rsp
    elif [ -n "$occ_cmd" ]; then
        # Send user specified command
        send_occ_cmd
        handle_occ_rsp
    else
        usage
        exit 0
    fi
fi

#cleanup
if [ -e "${binfile}" ]; then
    rm ${binfile}
fi

if [ $seqMismatch -ne 0 ]; then
    echo ""
    echo -e "${red}Warning: sequence number mismatch!${normal}  (Response data is stale!)"
    exit 9
fi


OpenPOWER on IntegriCloud