summaryrefslogtreecommitdiff
path: root/lib/ipmi_sdr.c
blob: 62aac08a9002d8898b10a4a09d2eb541fcf543c0 (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
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
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
/*
 * Copyright (c) 2012 Hewlett-Packard Development Company, L.P.
 *
 * Based on code from
 * Copyright (c) 2003 Sun Microsystems, Inc.  All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistribution of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
 * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL
 * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
 * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */
#define _BSD_SOURCE

#include <string.h>

#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>

#include <ipmitool/ipmi.h>
#include <ipmitool/log.h>
#include <ipmitool/ipmi_mc.h>
#include <ipmitool/ipmi_sdr.h>
#include <ipmitool/ipmi_sdradd.h>
#include <ipmitool/ipmi_sensor.h>
#include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi_sel.h>
#include <ipmitool/ipmi_entity.h>
#include <ipmitool/ipmi_constants.h>
#include <ipmitool/ipmi_strings.h>

#if HAVE_CONFIG_H
# include <config.h>
#endif

extern int verbose;
static int use_built_in;	/* Uses DeviceSDRs instead of SDRR */
static int sdr_max_read_len = 0;
static int sdr_extended = 0;
static long sdriana = 0;

static struct sdr_record_list *sdr_list_head = NULL;
static struct sdr_record_list *sdr_list_tail = NULL;
static struct ipmi_sdr_iterator *sdr_list_itr = NULL;

void printf_sdr_usage();

/* From src/plugins/ipmi_intf.c: */
uint16_t
ipmi_intf_get_max_response_data_size(struct ipmi_intf * intf);

/* ipmi_sdr_get_unit_string  -  return units for base/modifier
 *
 * @pct:	units are a percentage
 * @type:	unit type
 * @base:	base
 * @modifier:	modifier
 *
 * returns pointer to static string
 */
const char *
ipmi_sdr_get_unit_string(uint8_t pct, uint8_t type, uint8_t base, uint8_t modifier)
{
	static char unitstr[16];
	/*
	 * By default, if units are supposed to be percent, we will pre-pend
	 * the percent string  to the textual representation of the units.
	 */
	char *pctstr = pct ? "% " : "";
	memset(unitstr, 0, sizeof (unitstr));
	switch (type) {
	case 2:
		snprintf(unitstr, sizeof (unitstr), "%s%s * %s",
			 pctstr, unit_desc[base], unit_desc[modifier]);
		break;
	case 1:
		snprintf(unitstr, sizeof (unitstr), "%s%s/%s",
			 pctstr, unit_desc[base], unit_desc[modifier]);
		break;
	case 0:
	default:
		/*
		 * Display the text "percent" only when the Base unit is
		 * "unspecified" and the caller specified to print percent.
		 */
		if (base == 0 && pct) {
			snprintf(unitstr, sizeof(unitstr), "percent");
		} else {
			snprintf(unitstr, sizeof (unitstr), "%s%s", 
				pctstr, unit_desc[base]);
		}
		break;
	}
	return unitstr;
}

/* sdr_sensor_has_analog_reading  -  Determine if sensor has an analog reading
 *
 */
static int
sdr_sensor_has_analog_reading(struct ipmi_intf *intf,
			    struct sensor_reading *sr)
{
	/* Compact sensors can't return analog values so we false */
	if (!sr->full) {
		return 0;
	}
	/*
	 * Per the IPMI Specification:
	 *	Only Full Threshold sensors are identified as providing
	 *	analog readings.
	 *
	 * But... HP didn't interpret this as meaning that "Only Threshold
	 *        Sensors" can provide analog readings.  So, HP packed analog
	 *        readings into some of their non-Threshold Sensor.   There is
	 *	  nothing that explictly prohibits this in the spec, so if
	 *	  an Analog reading is available in a Non-Threshod sensor and
	 *	  there are units specified for identifying the reading then
	 *	  we do an analog conversion even though the sensor is
	 *	  non-Threshold.   To be safe, we provide this extension for
	 *	  HP.
	 *
	 */
	if ( UNITS_ARE_DISCRETE(&sr->full->cmn) ) {
		return 0;/* Sensor specified as not having Analog Units */
	}
	if ( !IS_THRESHOLD_SENSOR(&sr->full->cmn) ) {
		/* Non-Threshold Sensors are not defined as having analog */
		/* But.. We have one with defined with Analog Units */
		if ( (sr->full->cmn.unit.pct | sr->full->cmn.unit.modifier |
			 sr->full->cmn.unit.type.base |
			 sr->full->cmn.unit.type.modifier)) {
			 /* And it does have the necessary units specs */
			 if ( !(intf->manufacturer_id == IPMI_OEM_HP) ) {
				/* But to be safe we only do this for HP */
				return 0;
			 }
		} else {
			return 0;
		}
	}
	/*
	 * If sensor has linearization, then we should be able to update the
	 * reading factors and if we cannot fail the conversion.
	 */
	if (sr->full->linearization >= SDR_SENSOR_L_NONLINEAR &&
	    sr->full->linearization <= 0x7F) {
		if (ipmi_sensor_get_sensor_reading_factors(intf, sr->full, sr->s_reading) < 0){
			sr->s_reading_valid = 0;
			return 0;
		}
	}

	return 1;
}

/* sdr_convert_sensor_reading  -  convert raw sensor reading
 *
 * @sensor:	sensor record
 * @val:	raw sensor reading
 *
 * returns floating-point sensor reading
 */
double
sdr_convert_sensor_reading(struct sdr_record_full_sensor *sensor, uint8_t val)
{
	int m, b, k1, k2;
	double result;

	m = __TO_M(sensor->mtol);
	b = __TO_B(sensor->bacc);
	k1 = __TO_B_EXP(sensor->bacc);
	k2 = __TO_R_EXP(sensor->bacc);

	switch (sensor->cmn.unit.analog) {
	case 0:
		result = (double) (((m * val) +
				    (b * pow(10, k1))) * pow(10, k2));
		break;
	case 1:
		if (val & 0x80)
			val++;
		/* Deliberately fall through to case 2. */
	case 2:
		result = (double) (((m * (int8_t) val) +
				    (b * pow(10, k1))) * pow(10, k2));
		break;
	default:
		/* Oops! This isn't an analog sensor. */
		return 0.0;
	}

	switch (sensor->linearization & 0x7f) {
	case SDR_SENSOR_L_LN:
		result = log(result);
		break;
	case SDR_SENSOR_L_LOG10:
		result = log10(result);
		break;
	case SDR_SENSOR_L_LOG2:
		result = (double) (log(result) / log(2.0));
		break;
	case SDR_SENSOR_L_E:
		result = exp(result);
		break;
	case SDR_SENSOR_L_EXP10:
		result = pow(10.0, result);
		break;
	case SDR_SENSOR_L_EXP2:
		result = pow(2.0, result);
		break;
	case SDR_SENSOR_L_1_X:
		result = pow(result, -1.0);	/*1/x w/o exception */
		break;
	case SDR_SENSOR_L_SQR:
		result = pow(result, 2.0);
		break;
	case SDR_SENSOR_L_CUBE:
		result = pow(result, 3.0);
		break;
	case SDR_SENSOR_L_SQRT:
		result = sqrt(result);
		break;
	case SDR_SENSOR_L_CUBERT:
		result = cbrt(result);
		break;
	case SDR_SENSOR_L_LINEAR:
	default:
		break;
	}
	return result;
}
/* sdr_convert_sensor_hysterisis  -  convert raw sensor hysterisis
 *
 * Even though spec says histerisis should be computed using Mx+B
 * formula, B is irrelevant when doing raw comparison
 *
 * threshold rearm point is computed using threshold +/- hysterisis
 * with the full formula however B can't be applied in raw comparisons
 *
 * @sensor:	sensor record
 * @val:	raw sensor reading
 *
 * returns floating-point sensor reading
 */
double
sdr_convert_sensor_hysterisis(struct sdr_record_full_sensor *sensor, uint8_t val)
{
	int m, k2;
	double result;

	m = __TO_M(sensor->mtol);

	k2 = __TO_R_EXP(sensor->bacc);

	switch (sensor->cmn.unit.analog) {
	case 0:
		result = (double) (((m * val)) * pow(10, k2));
		break;
	case 1:
		if (val & 0x80)
			val++;
		/* Deliberately fall through to case 2. */
	case 2:
		result = (double) (((m * (int8_t) val) ) * pow(10, k2));
		break;
	default:
		/* Oops! This isn't an analog sensor. */
		return 0.0;
	}

	switch (sensor->linearization & 0x7f) {
	case SDR_SENSOR_L_LN:
		result = log(result);
		break;
	case SDR_SENSOR_L_LOG10:
		result = log10(result);
		break;
	case SDR_SENSOR_L_LOG2:
		result = (double) (log(result) / log(2.0));
		break;
	case SDR_SENSOR_L_E:
		result = exp(result);
		break;
	case SDR_SENSOR_L_EXP10:
		result = pow(10.0, result);
		break;
	case SDR_SENSOR_L_EXP2:
		result = pow(2.0, result);
		break;
	case SDR_SENSOR_L_1_X:
		result = pow(result, -1.0);	/*1/x w/o exception */
		break;
	case SDR_SENSOR_L_SQR:
		result = pow(result, 2.0);
		break;
	case SDR_SENSOR_L_CUBE:
		result = pow(result, 3.0);
		break;
	case SDR_SENSOR_L_SQRT:
		result = sqrt(result);
		break;
	case SDR_SENSOR_L_CUBERT:
		result = cbrt(result);
		break;
	case SDR_SENSOR_L_LINEAR:
	default:
		break;
	}
	return result;
}


/* sdr_convert_sensor_tolerance  -  convert raw sensor reading
 *
 * @sensor:	sensor record
 * @val:	raw sensor reading
 *
 * returns floating-point sensor tolerance(interpreted)
 */
double
sdr_convert_sensor_tolerance(struct sdr_record_full_sensor *sensor, uint8_t val)
{
	int m,   k2;
	double result;

	m = __TO_M(sensor->mtol);
	k2 = __TO_R_EXP(sensor->bacc);

	switch (sensor->cmn.unit.analog) {
	case 0:
                /* as suggested in section 30.4.1 of IPMI 1.5 spec */
		result = (double) ((((m * (double)val/2)) ) * pow(10, k2));
		break;
	case 1:
		if (val & 0x80)
			val++;
		/* Deliberately fall through to case 2. */
	case 2:
		result = (double) (((m * ((double)((int8_t) val)/2))) * pow(10, k2));
		break;
	default:
		/* Oops! This isn't an analog sensor. */
		return 0.0;
	}

	switch (sensor->linearization & 0x7f) {
	case SDR_SENSOR_L_LN:
		result = log(result);
		break;
	case SDR_SENSOR_L_LOG10:
		result = log10(result);
		break;
	case SDR_SENSOR_L_LOG2:
		result = (double) (log(result) / log(2.0));
		break;
	case SDR_SENSOR_L_E:
		result = exp(result);
		break;
	case SDR_SENSOR_L_EXP10:
		result = pow(10.0, result);
		break;
	case SDR_SENSOR_L_EXP2:
		result = pow(2.0, result);
		break;
	case SDR_SENSOR_L_1_X:
		result = pow(result, -1.0);	/*1/x w/o exception */
		break;
	case SDR_SENSOR_L_SQR:
		result = pow(result, 2.0);
		break;
	case SDR_SENSOR_L_CUBE:
		result = pow(result, 3.0);
		break;
	case SDR_SENSOR_L_SQRT:
		result = sqrt(result);
		break;
	case SDR_SENSOR_L_CUBERT:
		result = cbrt(result);
		break;
	case SDR_SENSOR_L_LINEAR:
	default:
		break;
	}
	return result;
}

/* sdr_convert_sensor_value_to_raw  -  convert sensor reading back to raw
 *
 * @sensor:	sensor record
 * @val:	converted sensor reading
 *
 * returns raw sensor reading
 */
uint8_t
sdr_convert_sensor_value_to_raw(struct sdr_record_full_sensor * sensor,
				double val)
{
	int m, b, k1, k2;
	double result;

	/* only works for analog sensors */
	if (UNITS_ARE_DISCRETE((&sensor->cmn)))
		return 0;

	m = __TO_M(sensor->mtol);
	b = __TO_B(sensor->bacc);
	k1 = __TO_B_EXP(sensor->bacc);
	k2 = __TO_R_EXP(sensor->bacc);

	/* don't divide by zero */
	if (m == 0)
		return 0;

	result = (((val / pow(10, k2)) - (b * pow(10, k1))) / m);

	if ((result - (int) result) >= .5)
		return (uint8_t) ceil(result);
	else
		return (uint8_t) result;
}

/* ipmi_sdr_get_sensor_thresholds  -  return thresholds for sensor
 *
 * @intf:	ipmi interface
 * @sensor:	sensor number
 * @target:	sensor owner ID
 * @lun:	sensor lun
 * @channel:	channel number
 *
 * returns pointer to ipmi response
 */
struct ipmi_rs *
ipmi_sdr_get_sensor_thresholds(struct ipmi_intf *intf, uint8_t sensor,
					uint8_t target, uint8_t lun, uint8_t channel)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	uint8_t  bridged_request = 0;
	uint32_t save_addr;
	uint32_t save_channel;

	if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
		bridged_request = 1;
		save_addr = intf->target_addr;
		intf->target_addr = target;
		save_channel = intf->target_channel;
		intf->target_channel = channel;
	}

	memset(&req, 0, sizeof (req));
	req.msg.netfn = IPMI_NETFN_SE;
	req.msg.lun = lun;
	req.msg.cmd = GET_SENSOR_THRESHOLDS;
	req.msg.data = &sensor;
	req.msg.data_len = sizeof (sensor);

	rsp = intf->sendrecv(intf, &req);
	if (bridged_request) {
		intf->target_addr = save_addr;
		intf->target_channel = save_channel;
	}
	return rsp;
}

/* ipmi_sdr_get_sensor_hysteresis  -  return hysteresis for sensor
 *
 * @intf:	ipmi interface
 * @sensor:	sensor number
 * @target:	sensor owner ID
 * @lun:	sensor lun
 * @channel:	channel number
 *
 * returns pointer to ipmi response
 */
struct ipmi_rs *
ipmi_sdr_get_sensor_hysteresis(struct ipmi_intf *intf, uint8_t sensor,
					uint8_t target, uint8_t lun, uint8_t channel)
{
	struct ipmi_rq req;
	uint8_t rqdata[2];
	struct ipmi_rs *rsp;
	uint8_t  bridged_request = 0;
	uint32_t save_addr;
	uint32_t save_channel;

	if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
		bridged_request = 1;
		save_addr = intf->target_addr;
		intf->target_addr = target;
		save_channel = intf->target_channel;
		intf->target_channel = channel;
	}

	rqdata[0] = sensor;
	rqdata[1] = 0xff;	/* reserved */

	memset(&req, 0, sizeof (req));
	req.msg.netfn = IPMI_NETFN_SE;
	req.msg.lun = lun;
	req.msg.cmd = GET_SENSOR_HYSTERESIS;
	req.msg.data = rqdata;
	req.msg.data_len = 2;

	rsp = intf->sendrecv(intf, &req);
	if (bridged_request) {
		intf->target_addr = save_addr;
		intf->target_channel = save_channel;
	}
	return rsp;
}

/* ipmi_sdr_get_sensor_reading  -  retrieve a raw sensor reading
 *
 * @intf:	ipmi interface
 * @sensor:	sensor id
 *
 * returns ipmi response structure
 */
struct ipmi_rs *
ipmi_sdr_get_sensor_reading(struct ipmi_intf *intf, uint8_t sensor)
{
	struct ipmi_rq req;

	memset(&req, 0, sizeof (req));
	req.msg.netfn = IPMI_NETFN_SE;
	req.msg.cmd = GET_SENSOR_READING;
	req.msg.data = &sensor;
	req.msg.data_len = 1;

	return intf->sendrecv(intf, &req);
}


/* ipmi_sdr_get_sensor_reading_ipmb  -  retrieve a raw sensor reading from ipmb
 *
 * @intf:	ipmi interface
 * @sensor:	sensor id
 * @target:	IPMB target address
 * @lun:	sensor lun
 * @channel:	channel number
 *
 * returns ipmi response structure
 */
struct ipmi_rs *
ipmi_sdr_get_sensor_reading_ipmb(struct ipmi_intf *intf, uint8_t sensor,
				 uint8_t target, uint8_t lun, uint8_t channel)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	uint8_t  bridged_request = 0;
	uint32_t save_addr;
	uint32_t save_channel;

	if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
		lprintf(LOG_DEBUG,
			"Bridge to Sensor "
			"Intf my/%#x tgt/%#x:%#x Sdr tgt/%#x:%#x\n",
			intf->my_addr, intf->target_addr, intf->target_channel,
			target, channel);
		bridged_request = 1;
		save_addr = intf->target_addr;
		intf->target_addr = target;
		save_channel = intf->target_channel;
		intf->target_channel = channel;
	}
	memset(&req, 0, sizeof (req));
	req.msg.netfn = IPMI_NETFN_SE;
	req.msg.lun = lun;
	req.msg.cmd = GET_SENSOR_READING;
	req.msg.data = &sensor;
	req.msg.data_len = 1;

	rsp = intf->sendrecv(intf, &req);
	if (bridged_request) {
		intf->target_addr    = save_addr;
		intf->target_channel = save_channel;
	}
	return rsp;
}

/* ipmi_sdr_get_sensor_event_status  -  retrieve sensor event status
 *
 * @intf:	ipmi interface
 * @sensor:	sensor id
 * @target:	sensor owner ID
 * @lun:	sensor lun
 * @channel:	channel number
 *
 * returns ipmi response structure
 */
struct ipmi_rs *
ipmi_sdr_get_sensor_event_status(struct ipmi_intf *intf, uint8_t sensor,
				 uint8_t target, uint8_t lun, uint8_t channel)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	uint8_t  bridged_request = 0;
	uint32_t save_addr;
	uint32_t save_channel;

	if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
		bridged_request = 1;
		save_addr = intf->target_addr;
		intf->target_addr = target;
		save_channel = intf->target_channel;
		intf->target_channel = channel;
	}
	memset(&req, 0, sizeof (req));
	req.msg.netfn = IPMI_NETFN_SE;
	req.msg.lun = lun;
	req.msg.cmd = GET_SENSOR_EVENT_STATUS;
	req.msg.data = &sensor;
	req.msg.data_len = 1;

	rsp = intf->sendrecv(intf, &req);
	if (bridged_request) {
		intf->target_addr = save_addr;
		intf->target_channel = save_channel;
	}
	return rsp;
}

/* ipmi_sdr_get_sensor_event_enable  -  retrieve sensor event enables
 *
 * @intf:	ipmi interface
 * @sensor:	sensor id
 * @target:	sensor owner ID
 * @lun:	sensor lun
 * @channel:	channel number
 *
 * returns ipmi response structure
 */
struct ipmi_rs *
ipmi_sdr_get_sensor_event_enable(struct ipmi_intf *intf, uint8_t sensor,
				 uint8_t target, uint8_t lun, uint8_t channel)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	uint8_t  bridged_request = 0;
	uint32_t save_addr;
	uint32_t save_channel;

	if ( BRIDGE_TO_SENSOR(intf, target, channel) ) {
		bridged_request = 1;
		save_addr = intf->target_addr;
		intf->target_addr = target;
		save_channel = intf->target_channel;
		intf->target_channel = channel;
	}

	memset(&req, 0, sizeof (req));
	req.msg.netfn = IPMI_NETFN_SE;
	req.msg.lun = lun;
	req.msg.cmd = GET_SENSOR_EVENT_ENABLE;
	req.msg.data = &sensor;
	req.msg.data_len = 1;

	rsp = intf->sendrecv(intf, &req);
	if (bridged_request) {
		intf->target_addr = save_addr;
		intf->target_channel = save_channel;
	}
	return rsp;
}

/* ipmi_sdr_get_thresh_status  -  threshold status indicator
 *
 * @rsp:		response from Get Sensor Reading comand
 * @validread:	validity of the status field argument
 * @invalidstr:	string to return if status field is not valid
 *
 * returns
 *   cr = critical
 *   nc = non-critical
 *   nr = non-recoverable
 *   ok = ok
 *   ns = not specified
 */
const char *
ipmi_sdr_get_thresh_status(struct sensor_reading *sr, const char *invalidstr)
{
	uint8_t stat;
	if (!sr->s_reading_valid) {
	    return invalidstr;
	}
	stat = sr->s_data2;
	if (stat & SDR_SENSOR_STAT_LO_NR) {
		if (verbose)
			return "Lower Non-Recoverable";
		else if (sdr_extended)
			return "lnr";
		else
			return "nr";
	} else if (stat & SDR_SENSOR_STAT_HI_NR) {
		if (verbose)
			return "Upper Non-Recoverable";
		else if (sdr_extended)
			return "unr";
		else
			return "nr";
	} else if (stat & SDR_SENSOR_STAT_LO_CR) {
		if (verbose)
			return "Lower Critical";
		else if (sdr_extended)
			return "lcr";
		else
			return "cr";
	} else if (stat & SDR_SENSOR_STAT_HI_CR) {
		if (verbose)
			return "Upper Critical";
		else if (sdr_extended)
			return "ucr";
		else
			return "cr";
	} else if (stat & SDR_SENSOR_STAT_LO_NC) {
		if (verbose)
			return "Lower Non-Critical";
		else if (sdr_extended)
			return "lnc";
		else
			return "nc";
	} else if (stat & SDR_SENSOR_STAT_HI_NC) {
		if (verbose)
			return "Upper Non-Critical";
		else if (sdr_extended)
			return "unc";
		else
			return "nc";
	}
	return "ok";
}

/* ipmi_sdr_get_header  -  retreive SDR record header
 *
 * @intf:	ipmi interface
 * @itr:	sdr iterator
 *
 * returns pointer to static sensor retrieval struct
 * returns NULL on error
 */
static struct sdr_get_rs *
ipmi_sdr_get_header(struct ipmi_intf *intf, struct ipmi_sdr_iterator *itr)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	struct sdr_get_rq sdr_rq;
	static struct sdr_get_rs sdr_rs;
	int try = 0;

	memset(&sdr_rq, 0, sizeof (sdr_rq));
	sdr_rq.reserve_id = itr->reservation;
	sdr_rq.id = itr->next;
	sdr_rq.offset = 0;
	sdr_rq.length = 5;	/* only get the header */

	memset(&req, 0, sizeof (req));
	if (itr->use_built_in == 0) {
		req.msg.netfn = IPMI_NETFN_STORAGE;
		req.msg.cmd = GET_SDR;
	} else {
		req.msg.netfn = IPMI_NETFN_SE;
		req.msg.cmd = GET_DEVICE_SDR;
	}
	req.msg.data = (uint8_t *) & sdr_rq;
	req.msg.data_len = sizeof (sdr_rq);

	for (try = 0; try < 5; try++) {
		sdr_rq.reserve_id = itr->reservation;
		rsp = intf->sendrecv(intf, &req);
		if (rsp == NULL) {
			lprintf(LOG_ERR, "Get SDR %04x command failed",
				itr->next);
			continue;
		} else if (rsp->ccode == 0xc5) {
			/* lost reservation */
			lprintf(LOG_DEBUG, "SDR reservation %04x cancelled. "
				"Sleeping a bit and retrying...",
				itr->reservation);

			sleep(rand() & 3);

			if (ipmi_sdr_get_reservation(intf, itr->use_built_in,
                                      &(itr->reservation)) < 0) {
				lprintf(LOG_ERR,
					"Unable to renew SDR reservation");
				return NULL;
			}
		} else if (rsp->ccode > 0) {
			lprintf(LOG_ERR, "Get SDR %04x command failed: %s",
				itr->next, val2str(rsp->ccode,
						   completion_code_vals));
			continue;
		} else {
			break;
		}
	}

   if (try == 5)
      return NULL;

	if (!rsp)
		return NULL;

	lprintf(LOG_DEBUG, "SDR record ID   : 0x%04x", itr->next);

	memcpy(&sdr_rs, rsp->data, sizeof (sdr_rs));

	if (sdr_rs.length == 0) {
		lprintf(LOG_ERR, "SDR record id 0x%04x: invalid length %d",
			itr->next, sdr_rs.length);
		return NULL;
	}

	/* achu (chu11 at llnl dot gov): - Some boards are stupid and
	 * return a record id from the Get SDR Record command
	 * different than the record id passed in.  If we find this
	 * situation, we cheat and put the original record id back in.
	 * Otherwise, a later Get SDR Record command will fail with
	 * completion code CBh = "Requested Sensor, data, or record
	 * not present". Exception is if 'Record ID' is specified as 0000h.
	 * For further information see IPMI v2.0 Spec, Section 33.12
	 */
	if ((itr->next != 0x0000) &&
		(sdr_rs.id != itr->next)) {
		lprintf(LOG_DEBUG, "SDR record id mismatch: 0x%04x", sdr_rs.id);
		sdr_rs.id = itr->next;
	}

	lprintf(LOG_DEBUG, "SDR record type : 0x%02x", sdr_rs.type);
	lprintf(LOG_DEBUG, "SDR record next : 0x%04x", sdr_rs.next);
	lprintf(LOG_DEBUG, "SDR record bytes: %d", sdr_rs.length);

	return &sdr_rs;
}

/* ipmi_sdr_get_next_header  -  retreive next SDR header
 *
 * @intf:	ipmi interface
 * @itr:	sdr iterator
 *
 * returns pointer to sensor retrieval struct
 * returns NULL on error
 */
struct sdr_get_rs *
ipmi_sdr_get_next_header(struct ipmi_intf *intf, struct ipmi_sdr_iterator *itr)
{
	struct sdr_get_rs *header;

	if (itr->next == 0xffff)
		return NULL;

	header = ipmi_sdr_get_header(intf, itr);
	if (header == NULL)
		return NULL;

	itr->next = header->next;

	return header;
}

/*
 * This macro is used to print nominal, normal and threshold settings,
 * but it is not compatible with PRINT_NORMAL/PRINT_THRESH since it does
 * not have the sensor.init.thresholds setting qualifier as is done in
 * PRINT_THRESH.   This means CSV output can be different than non CSV
 * output if sensor.init.thresholds is ever zero
 */
/* helper macro for printing CSV output for Full SDR Threshold reading */
#define SENSOR_PRINT_CSV(FULLSENS, FLAG, READ)				\
	if ((FLAG)) {							\
		if (UNITS_ARE_DISCRETE((&FULLSENS->cmn)))		\
			printf("0x%02X,", READ); 			\
		else							\
			printf("%.3f,",	sdr_convert_sensor_reading(	\
			       (FULLSENS), READ));			\
	} else {							\
		printf(",");						\
	}

/* helper macro for printing analog values for Full SDR Threshold readings */
#define SENSOR_PRINT_NORMAL(FULLSENS, NAME, READ)			\
	if ((FULLSENS)->analog_flag.READ != 0) {			\
		printf(" %-21s : ", NAME);				\
		if (UNITS_ARE_DISCRETE((&FULLSENS->cmn)))		\
			printf("0x%02X\n",				\
			       (FULLSENS)->READ);			\
		else							\
			printf("%.3f\n", sdr_convert_sensor_reading(	\
				         (FULLSENS), (FULLSENS)->READ));\
	}

/* helper macro for printing Full SDR sensor Thresholds */
#define SENSOR_PRINT_THRESH(FULLSENS, NAME, READ, FLAG)			\
	if ((FULLSENS)->cmn.sensor.init.thresholds &&			\
	    (FULLSENS)->cmn.mask.type.threshold.read.FLAG != 0) {	\
		printf(" %-21s : ", NAME);				\
		if (UNITS_ARE_DISCRETE((&FULLSENS->cmn)))		\
			printf("0x%02X\n",				\
			       (FULLSENS)->threshold.READ);		\
		else                                        		\
			printf("%.3f\n", sdr_convert_sensor_reading(	\
			     (FULLSENS), (FULLSENS)->threshold.READ));	\
	}

int
ipmi_sdr_print_sensor_event_status(struct ipmi_intf *intf,
				   uint8_t sensor_num,
				   uint8_t sensor_type,
				   uint8_t event_type, int numeric_fmt,
				   uint8_t target, uint8_t lun, uint8_t channel)
{
	struct ipmi_rs *rsp;
	int i;
	const struct valstr assert_cond_1[] = {
		{0x80, "unc+"},
		{0x40, "unc-"},
		{0x20, "lnr+"},
		{0x10, "lnr-"},
		{0x08, "lcr+"},
		{0x04, "lcr-"},
		{0x02, "lnc+"},
		{0x01, "lnc-"},
		{0x00, NULL},
	};
	const struct valstr assert_cond_2[] = {
		{0x08, "unr+"},
		{0x04, "unr-"},
		{0x02, "ucr+"},
		{0x01, "ucr-"},
		{0x00, NULL},
	};

	rsp = ipmi_sdr_get_sensor_event_status(intf, sensor_num,
						target, lun, channel);

	if (rsp == NULL) {
		lprintf(LOG_DEBUG,
			"Error reading event status for sensor #%02x",
			sensor_num);
		return -1;
	}
	if (rsp->ccode > 0) {
		lprintf(LOG_DEBUG,
			"Error reading event status for sensor #%02x: %s",
			sensor_num, val2str(rsp->ccode, completion_code_vals));
		return -1;
	}
	/* There is an assumption here that data_len >= 1 */
	if (IS_READING_UNAVAILABLE(rsp->data[0])) {
		printf(" Event Status          : Unavailable\n");
		return 0;
	}
	if (IS_SCANNING_DISABLED(rsp->data[0])) {
		//printf(" Event Status          : Scanning Disabled\n");
		//return 0;
	}
	if (IS_EVENT_MSG_DISABLED(rsp->data[0])) {
		printf(" Event Status          : Event Messages Disabled\n");
		//return 0;
	}

	switch (numeric_fmt) {
	case DISCRETE_SENSOR:
		if (rsp->data_len == 2) {
			ipmi_sdr_print_discrete_state(intf, "Assertion Events",
						      sensor_type, event_type,
						      rsp->data[1], 0);
		} else if (rsp->data_len > 2) {
			ipmi_sdr_print_discrete_state(intf, "Assertion Events",
						      sensor_type, event_type,
						      rsp->data[1],
						      rsp->data[2]);
		}
		if (rsp->data_len == 4) {
			ipmi_sdr_print_discrete_state(intf, "Deassertion Events",
						      sensor_type, event_type,
						      rsp->data[3], 0);
		} else if (rsp->data_len > 4) {
			ipmi_sdr_print_discrete_state(intf, "Deassertion Events",
						      sensor_type, event_type,
						      rsp->data[3],
						      rsp->data[4]);
		}
		break;

	case ANALOG_SENSOR:
		printf(" Assertion Events      : ");
		for (i = 0; i < 8; i++) {
			if (rsp->data[1] & (1 << i))
				printf("%s ", val2str(1 << i, assert_cond_1));
		}
		if (rsp->data_len > 2) {
			for (i = 0; i < 4; i++) {
				if (rsp->data[2] & (1 << i))
					printf("%s ",
					       val2str(1 << i, assert_cond_2));
			}
			printf("\n");
			if ((rsp->data_len == 4 && rsp->data[3] != 0) ||
			    (rsp->data_len > 4
			     && (rsp->data[3] != 0 && rsp->data[4] != 0))) {
				printf(" Deassertion Events    : ");
				for (i = 0; i < 8; i++) {
					if (rsp->data[3] & (1 << i))
						printf("%s ",
						       val2str(1 << i,
							       assert_cond_1));
				}
				if (rsp->data_len > 4) {
					for (i = 0; i < 4; i++) {
						if (rsp->data[4] & (1 << i))
							printf("%s ",
							       val2str(1 << i,
								       assert_cond_2));
					}
				}
				printf("\n");
			}
		} else {
			printf("\n");
		}
		break;

	default:
		break;
	}

	return 0;
}

static int
ipmi_sdr_print_sensor_mask(struct ipmi_intf *intf,
			struct sdr_record_mask *mask,
			uint8_t sensor_type,
			uint8_t event_type, int numeric_fmt)
{
	/* iceblink - don't print some event status fields - CVS rev1.53 */
	return 0;

	switch (numeric_fmt) {
	case DISCRETE_SENSOR:
		ipmi_sdr_print_discrete_state(intf, "Assert Event Mask", sensor_type,
					      event_type,
					      mask->type.discrete.
					      assert_event & 0xff,
					      (mask->type.discrete.
					       assert_event & 0xff00) >> 8);
		ipmi_sdr_print_discrete_state(intf, "Deassert Event Mask",
					      sensor_type, event_type,
					      mask->type.discrete.
					      deassert_event & 0xff,
					      (mask->type.discrete.
					       deassert_event & 0xff00) >> 8);
		break;

	case ANALOG_SENSOR:
		printf(" Assert Event Mask     : ");
		if (mask->type.threshold.assert_lnr_high)
			printf("lnr+ ");
		if (mask->type.threshold.assert_lnr_low)
			printf("lnr- ");
		if (mask->type.threshold.assert_lcr_high)
			printf("lcr+ ");
		if (mask->type.threshold.assert_lcr_low)
			printf("lcr- ");
		if (mask->type.threshold.assert_lnc_high)
			printf("lnc+ ");
		if (mask->type.threshold.assert_lnc_low)
			printf("lnc- ");
		if (mask->type.threshold.assert_unc_high)
			printf("unc+ ");
		if (mask->type.threshold.assert_unc_low)
			printf("unc- ");
		if (mask->type.threshold.assert_ucr_high)
			printf("ucr+ ");
		if (mask->type.threshold.assert_ucr_low)
			printf("ucr- ");
		if (mask->type.threshold.assert_unr_high)
			printf("unr+ ");
		if (mask->type.threshold.assert_unr_low)
			printf("unr- ");
		printf("\n");

		printf(" Deassert Event Mask   : ");
		if (mask->type.threshold.deassert_lnr_high)
			printf("lnr+ ");
		if (mask->type.threshold.deassert_lnr_low)
			printf("lnr- ");
		if (mask->type.threshold.deassert_lcr_high)
			printf("lcr+ ");
		if (mask->type.threshold.deassert_lcr_low)
			printf("lcr- ");
		if (mask->type.threshold.deassert_lnc_high)
			printf("lnc+ ");
		if (mask->type.threshold.deassert_lnc_low)
			printf("lnc- ");
		if (mask->type.threshold.deassert_unc_high)
			printf("unc+ ");
		if (mask->type.threshold.deassert_unc_low)
			printf("unc- ");
		if (mask->type.threshold.deassert_ucr_high)
			printf("ucr+ ");
		if (mask->type.threshold.deassert_ucr_low)
			printf("ucr- ");
		if (mask->type.threshold.deassert_unr_high)
			printf("unr+ ");
		if (mask->type.threshold.deassert_unr_low)
			printf("unr- ");
		printf("\n");
		break;

	default:
		break;
	}

	return 0;
}

int
ipmi_sdr_print_sensor_event_enable(struct ipmi_intf *intf,
				   uint8_t sensor_num,
				   uint8_t sensor_type,
				   uint8_t event_type, int numeric_fmt,
				   uint8_t target, uint8_t lun, uint8_t channel)
{
	struct ipmi_rs *rsp;
	int i;
	const struct valstr assert_cond_1[] = {
		{0x80, "unc+"},
		{0x40, "unc-"},
		{0x20, "lnr+"},
		{0x10, "lnr-"},
		{0x08, "lcr+"},
		{0x04, "lcr-"},
		{0x02, "lnc+"},
		{0x01, "lnc-"},
		{0x00, NULL},
	};
	const struct valstr assert_cond_2[] = {
		{0x08, "unr+"},
		{0x04, "unr-"},
		{0x02, "ucr+"},
		{0x01, "ucr-"},
		{0x00, NULL},
	};

	rsp = ipmi_sdr_get_sensor_event_enable(intf, sensor_num,
						target, lun, channel);

	if (rsp == NULL) {
		lprintf(LOG_DEBUG,
			"Error reading event enable for sensor #%02x",
			sensor_num);
		return -1;
	}
	if (rsp->ccode > 0) {
		lprintf(LOG_DEBUG,
			"Error reading event enable for sensor #%02x: %s",
			sensor_num, val2str(rsp->ccode, completion_code_vals));
		return -1;
	}

	if (IS_SCANNING_DISABLED(rsp->data[0])) {
		//printf(" Event Enable          : Scanning Disabled\n");
		//return 0;
	}
	if (IS_EVENT_MSG_DISABLED(rsp->data[0])) {
		printf(" Event Enable          : Event Messages Disabled\n");
		//return 0;
	}

	switch (numeric_fmt) {
	case DISCRETE_SENSOR:
		/* discrete */
		if (rsp->data_len == 2) {
			ipmi_sdr_print_discrete_state(intf, "Assertions Enabled",
						      sensor_type, event_type,
						      rsp->data[1], 0);
		} else if (rsp->data_len > 2) {
			ipmi_sdr_print_discrete_state(intf, "Assertions Enabled",
						      sensor_type, event_type,
						      rsp->data[1],
						      rsp->data[2]);
		}
		if (rsp->data_len == 4) {
			ipmi_sdr_print_discrete_state(intf, "Deassertions Enabled",
						      sensor_type, event_type,
						      rsp->data[3], 0);
		} else if (rsp->data_len > 4) {
			ipmi_sdr_print_discrete_state(intf, "Deassertions Enabled",
						      sensor_type, event_type,
						      rsp->data[3],
						      rsp->data[4]);
		}
		break;

	case ANALOG_SENSOR:
		/* analog */
		printf(" Assertions Enabled    : ");
		for (i = 0; i < 8; i++) {
			if (rsp->data[1] & (1 << i))
				printf("%s ", val2str(1 << i, assert_cond_1));
		}
		if (rsp->data_len > 2) {
			for (i = 0; i < 4; i++) {
				if (rsp->data[2] & (1 << i))
					printf("%s ",
					       val2str(1 << i, assert_cond_2));
			}
			printf("\n");
			if ((rsp->data_len == 4 && rsp->data[3] != 0) ||
			    (rsp->data_len > 4
			     && (rsp->data[3] != 0 || rsp->data[4] != 0))) {
				printf(" Deassertions Enabled  : ");
				for (i = 0; i < 8; i++) {
					if (rsp->data[3] & (1 << i))
						printf("%s ",
						       val2str(1 << i,
							       assert_cond_1));
				}
				if (rsp->data_len > 4) {
					for (i = 0; i < 4; i++) {
						if (rsp->data[4] & (1 << i))
							printf("%s ",
							       val2str(1 << i,
								       assert_cond_2));
					}
				}
				printf("\n");
			}
		} else {
			printf("\n");
		}
		break;

	default:
		break;
	}

	return 0;
}

/* ipmi_sdr_print_sensor_hysteresis  -  print hysteresis for Discrete & Analog
 *
 * @sensor:		Common Sensor Record SDR pointer
 * @full:		Full Sensor Record SDR pointer (if applicable)
 * @hysteresis_value:	Actual hysteresis value
 * @hvstr:		hysteresis value Identifier String
 *
 * returns void
 */
void
ipmi_sdr_print_sensor_hysteresis(struct sdr_record_common_sensor *sensor,
		 struct sdr_record_full_sensor   *full,
		 uint8_t hysteresis_value,
		 const char *hvstr)
{
	/*
	 * compact can have pos/neg hysteresis, but they cannot be analog!
	 * We use not full in addition to our discrete units check just in
	 * case a compact sensor is incorrectly identified as analog.
	 */
	if (!full || UNITS_ARE_DISCRETE(sensor)) {
		if ( hysteresis_value == 0x00 || hysteresis_value == 0xff ) {
			printf(" %s   : Unspecified\n", hvstr);
		} else {
			printf(" %s   : 0x%02X\n", hvstr, hysteresis_value);
		}
		return;
	}
	/* A Full analog sensor */
	double creading = sdr_convert_sensor_hysterisis(full, hysteresis_value);
	if ( hysteresis_value == 0x00 || hysteresis_value == 0xff ||
	     creading == 0.0 ) {
		printf(" %s   : Unspecified\n", hvstr);
	} else {
		printf(" %s   : %.3f\n", hvstr, creading);
	}
}

/* print_sensor_min_max  -  print Discrete & Analog Minimum/Maximum Sensor Range
 *
 * @full:		Full Sensor Record SDR pointer
 *
 * returns void
 */
static void
print_sensor_min_max(struct sdr_record_full_sensor *full)
{
	if (!full) { /* No min/max for compact SDR record */
	    return;
	}

	double creading = 0.0;
	uint8_t is_analog = !UNITS_ARE_DISCRETE(&full->cmn);
	if (is_analog)
		creading = sdr_convert_sensor_reading(full, full->sensor_min);
	if ((full->cmn.unit.analog == 0 && full->sensor_min == 0x00) ||
	    (full->cmn.unit.analog == 1 && full->sensor_min == 0xff) ||
	    (full->cmn.unit.analog == 2 && full->sensor_min == 0x80) ||
	    (is_analog && (creading == 0.0)))
		printf(" Minimum sensor range  : Unspecified\n");
	else {
		if (is_analog)
			printf(" Minimum sensor range  : %.3f\n", creading);
		else
			printf(" Minimum sensor range  : 0x%02X\n", full->sensor_min);

	}
	if (is_analog)
		creading = sdr_convert_sensor_reading(full, full->sensor_max);
	if ((full->cmn.unit.analog == 0 && full->sensor_max == 0xff) ||
	    (full->cmn.unit.analog == 1 && full->sensor_max == 0x00) ||
	    (full->cmn.unit.analog == 2 && full->sensor_max == 0x7f) ||
	    (is_analog && (creading == 0.0)))
		printf(" Maximum sensor range  : Unspecified\n");
	else {
		if (is_analog)
			printf(" Maximum sensor range  : %.3f\n", creading);
		else
			printf(" Maximum sensor range  : 0x%02X\n", full->sensor_max);
	}
}

/* print_csv_discrete  -  print csv formatted discrete sensor
 *
 * @sensor:		common sensor structure
 * @sr:			sensor reading
 *
 * returns void
 */
static void
print_csv_discrete(struct ipmi_intf *intf,
		struct sdr_record_common_sensor *sensor,
		const struct sensor_reading *sr)
{
	if (!sr->s_reading_valid  || sr->s_reading_unavailable) {
		printf("%02Xh,ns,%d.%d,No Reading",
		       sensor->keys.sensor_num,
		       sensor->entity.id,
		       sensor->entity.instance);
		return;
	}

	if (sr->s_has_analog_value) {	/* Sensor has an analog value */
		printf("%s,%s,", sr->s_a_str, sr->s_a_units);
	} else {	/* Sensor has a discrete value */
		printf("%02Xh,", sensor->keys.sensor_num);
	}
	printf("ok,%d.%d,",
	       sensor->entity.id,
	       sensor->entity.instance);
	ipmi_sdr_print_discrete_state_mini(intf, NULL, ", ",
		sensor->sensor.type,
		sensor->event_type,
		sr->s_data2,
		sr->s_data3);
}

/* ipmi_sdr_read_sensor_value  -  read sensor value
 *
 * @intf		Interface pointer
 * @sensor		Common sensor component pointer
 * @sdr_record_type	Type of sdr sensor record
 * @precision		decimal precision for analog format conversion
 *
 * returns a pointer to sensor value reading data structure
 */
struct sensor_reading *
ipmi_sdr_read_sensor_value(struct ipmi_intf *intf,
		 struct sdr_record_common_sensor *sensor,
		 uint8_t sdr_record_type, int precision)
{
	static struct sensor_reading sr;

	if (sensor == NULL)
		return NULL;

	/* Initialize to reading valid value of zero */
	memset(&sr, 0, sizeof(sr));

	switch (sdr_record_type) {
		unsigned int idlen;
		case (SDR_RECORD_TYPE_FULL_SENSOR):
			sr.full = (struct sdr_record_full_sensor *)sensor;
			idlen = sr.full->id_code & 0x1f;
			idlen = idlen < sizeof(sr.s_id) ?
						idlen : sizeof(sr.s_id) - 1;
			memcpy(sr.s_id, sr.full->id_string, idlen);
			break;
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sr.compact = (struct sdr_record_compact_sensor *)sensor;
			idlen = sr.compact->id_code & 0x1f;
			idlen = idlen < sizeof(sr.s_id) ?
						idlen : sizeof(sr.s_id) - 1;
			memcpy(sr.s_id, sr.compact->id_string, idlen);
			break;
		default:
			return NULL;
	}

	/*
	 * Get current reading via IPMI interface
	 */
	struct ipmi_rs *rsp;
	rsp = ipmi_sdr_get_sensor_reading_ipmb(intf,
					       sensor->keys.sensor_num,
					       sensor->keys.owner_id,
					       sensor->keys.lun,
					       sensor->keys.channel);
	sr.s_a_val   = 0.0;	/* init analog value to a floating point 0 */
	sr.s_a_str[0] = '\0';	/* no converted analog value string */
	sr.s_a_units = "";	/* no converted analog units units */


	if (rsp == NULL) {
		lprintf(LOG_DEBUG, "Error reading sensor %s (#%02x)",
			sr.s_id, sensor->keys.sensor_num);
		return &sr;
	}

	if (rsp->ccode) {
		if ( !((sr.full    && rsp->ccode == 0xcb) ||
		       (sr.compact && rsp->ccode == 0xcd)) ) {
			lprintf(LOG_DEBUG,
				"Error reading sensor %s (#%02x): %s", sr.s_id,
				sensor->keys.sensor_num,
				val2str(rsp->ccode, completion_code_vals));
		}
		return &sr;
	}

	if (rsp->data_len < 2) {
		/*
		 * We must be returned both a value (data[0]), and the validity
		 * of the value (data[1]), in order to correctly interpret
		 * the reading.    If we don't have both of these we can't have
		 * a valid sensor reading.
		 */
		lprintf(LOG_DEBUG, "Error reading sensor %s invalid len %d",
			sr.s_id, rsp->data_len);
		return &sr;
	}


	if (IS_READING_UNAVAILABLE(rsp->data[1]))
		sr.s_reading_unavailable = 1;

	if (IS_SCANNING_DISABLED(rsp->data[1])) {
		sr.s_scanning_disabled = 1;
		lprintf(LOG_DEBUG, "Sensor %s (#%02x) scanning disabled",
			sr.s_id, sensor->keys.sensor_num);
		return &sr;
	}
	if ( !sr.s_reading_unavailable ) {
		sr.s_reading_valid = 1;
		sr.s_reading = rsp->data[0];
	}
	if (rsp->data_len > 2)
		sr.s_data2   = rsp->data[2];
	if (rsp->data_len > 3)
		sr.s_data3   = rsp->data[3];
	if (sdr_sensor_has_analog_reading(intf, &sr)) {
		sr.s_has_analog_value = 1;
		if (sr.s_reading_valid) {
			sr.s_a_val = sdr_convert_sensor_reading(sr.full, sr.s_reading);
		}
		/* determine units string with possible modifiers */
		sr.s_a_units = ipmi_sdr_get_unit_string(sr.full->cmn.unit.pct,
					   sr.full->cmn.unit.modifier,
					   sr.full->cmn.unit.type.base,
					   sr.full->cmn.unit.type.modifier);
		snprintf(sr.s_a_str, sizeof(sr.s_a_str), "%.*f",
			(sr.s_a_val == (int) sr.s_a_val) ? 0 :
			precision, sr.s_a_val);
	}
	return &sr;
}

/* ipmi_sdr_print_sensor_fc  -  print full & compact SDR records
 *
 * @intf:		ipmi interface
 * @sensor:		common sensor structure
 * @sdr_record_type:	type of sdr record, either full or compact
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_sensor_fc(struct ipmi_intf *intf,
			   struct sdr_record_common_sensor    *sensor,
			   uint8_t sdr_record_type)
{
	char sval[16];
	unsigned int i = 0;
	uint8_t target, lun, channel;
	struct sensor_reading *sr;


	sr = ipmi_sdr_read_sensor_value(intf, sensor, sdr_record_type, 2);

	if (sr == NULL)
		return -1;

	target = sensor->keys.owner_id;
	lun = sensor->keys.lun;
	channel = sensor->keys.channel;

	/*
	 * CSV OUTPUT
	 */

	if (csv_output) {
		/*
		 * print sensor name, reading, unit, state
		 */
		printf("%s,", sr->s_id);
		if (!IS_THRESHOLD_SENSOR(sensor)) {
			/* Discrete/Non-Threshold */
			print_csv_discrete(intf, sensor, sr);
			printf("\n");
		}
		else {
			/* Threshold Analog & Discrete*/
			if (sr->s_reading_valid) {
				if (sr->s_has_analog_value) {
					/* Analog/Threshold */
					printf("%.*f,", (sr->s_a_val ==
					(int) sr->s_a_val) ? 0 : 3,
					sr->s_a_val);
					printf("%s,%s", sr->s_a_units,
					       ipmi_sdr_get_thresh_status(sr, "ns"));
				} else { /* Discrete/Threshold */
					print_csv_discrete(intf, sensor, sr);
				}
			} else {
				printf(",,ns");
			}

			if (verbose) {
				printf(",%d.%d,%s,%s,",
					sensor->entity.id, sensor->entity.instance,
					val2str(sensor->entity.id, entity_id_vals),
					ipmi_get_sensor_type(intf, sensor->sensor.type));

				if (sr->full) {
					SENSOR_PRINT_CSV(sr->full, sr->full->analog_flag.nominal_read,
						sr->full->nominal_read);
					SENSOR_PRINT_CSV(sr->full, sr->full->analog_flag.normal_min,
						sr->full->normal_min);
					SENSOR_PRINT_CSV(sr->full, sr->full->analog_flag.normal_max,
						     sr->full->normal_max);
					SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.unr,
						sr->full->threshold.upper.non_recover);
					SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.ucr,
						sr->full->threshold.upper.critical);
					SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.unc,
						sr->full->threshold.upper.non_critical);
					SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.lnr,
						sr->full->threshold.lower.non_recover);
					SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.lcr,
						sr->full->threshold.lower.critical);
					SENSOR_PRINT_CSV(sr->full, sensor->mask.type.threshold.read.lnc,
						sr->full->threshold.lower.non_critical);

					if (UNITS_ARE_DISCRETE(sensor)) {
						printf("0x%02X,0x%02X", sr->full->sensor_min, sr->full->sensor_max);
					}
					else {
						printf("%.3f,%.3f",
						       sdr_convert_sensor_reading(sr->full,
									      sr->full->sensor_min),
						       sdr_convert_sensor_reading(sr->full,
									      sr->full->sensor_max));
					}
				} else {
					printf(",,,,,,,,,,");
				}
			}
			printf("\n");
		}

		return 0;	/* done */
	}

	/*
	 * NORMAL OUTPUT
	 */

	if (verbose == 0 && sdr_extended == 0) {
		/*
		 * print sensor name, reading, state
		 */
		printf("%-16s | ", sr->s_id);

		memset(sval, 0, sizeof (sval));

		if (sr->s_reading_valid) {
			if( sr->s_has_analog_value ) {
				snprintf(sval, sizeof (sval), "%s %s",
						      sr->s_a_str,
						      sr->s_a_units);
			} else /* Discrete */
				snprintf(sval, sizeof(sval),
					"0x%02x", sr->s_reading);
		}
		else if (sr->s_scanning_disabled)
			snprintf(sval, sizeof (sval), sr->full ? "disabled"   : "Not Readable");
		else
			snprintf(sval, sizeof (sval), sr->full ? "no reading" : "Not Readable");

		printf("%s", sval);

		for (i = strlen(sval); i <= sizeof (sval); i++)
			printf(" ");
		printf(" | ");

		if (IS_THRESHOLD_SENSOR(sensor)) {
			printf("%s", ipmi_sdr_get_thresh_status(sr, "ns"));
		}
		else {
			printf("%s", sr->s_reading_valid ? "ok" : "ns");
		}

		printf("\n");

		return 0;	/* done */
	} else if (verbose == 0 && sdr_extended == 1) {
		/*
		 * print sensor name, number, state, entity, reading
		 */
		printf("%-16s | %02Xh | ",
		       sr->s_id, sensor->keys.sensor_num);

		if (IS_THRESHOLD_SENSOR(sensor)) {
			/* Threshold Analog & Discrete */
			printf("%-3s | %2d.%1d | ",
			   ipmi_sdr_get_thresh_status(sr, "ns"),
		           sensor->entity.id, sensor->entity.instance);
		}
		else {
			/* Non Threshold Analog & Discrete */
			printf("%-3s | %2d.%1d | ",
			       (sr->s_reading_valid ? "ok" : "ns"),
			       sensor->entity.id, sensor->entity.instance);
		}

		memset(sval, 0, sizeof (sval));

		if (sr->s_reading_valid) {
			if (IS_THRESHOLD_SENSOR(sensor) &&
				sr->s_has_analog_value ) {
				/* Threshold Analog */
					snprintf(sval, sizeof (sval), "%s %s",
						      sr->s_a_str,
						      sr->s_a_units);
			} else {
				/* Analog & Discrete & Threshold/Discrete */
				char *header = NULL;
				if (sr->s_has_analog_value) { /* Sensor has an analog value */
					printf("%s %s", sr->s_a_str, sr->s_a_units);
					header = ", ";
				}
				ipmi_sdr_print_discrete_state_mini(intf, header, ", ",
								   sensor->sensor.type,
								   sensor->event_type,
								   sr->s_data2,
								   sr->s_data3);
			}
		}
		else if (sr->s_scanning_disabled)
			snprintf(sval, sizeof (sval), "Disabled");
		else
			snprintf(sval, sizeof (sval), "No Reading");

		printf("%s\n", sval);
		return 0;	/* done */
	}
	/*
	 * VERBOSE OUTPUT
	 */

	printf("Sensor ID              : %s (0x%x)\n",
	       sr->s_id, sensor->keys.sensor_num);
	printf(" Entity ID             : %d.%d (%s)\n",
	       sensor->entity.id, sensor->entity.instance,
	       val2str(sensor->entity.id, entity_id_vals));

	if (!IS_THRESHOLD_SENSOR(sensor)) {
		/* Discrete */
		printf(" Sensor Type (Discrete): %s (0x%02x)\n",
				ipmi_get_sensor_type(intf, sensor->sensor.type),
				sensor->sensor.type);
		lprintf(LOG_DEBUG, " Event Type Code       : 0x%02x",
			sensor->event_type);

		printf(" Sensor Reading        : ");
		if (sr->s_reading_valid) {
			if (sr->s_has_analog_value) { /* Sensor has an analog value */
				printf("%s %s\n", sr->s_a_str, sr->s_a_units);
			} else {
				printf("%xh\n", sr->s_reading);
			}
		}
		else if (sr->s_scanning_disabled)
			printf("Disabled\n");
		else {
			/* Used to be 'Not Reading' */
			printf("No Reading\n");
		}

		printf(" Event Message Control : ");
		switch (sensor->sensor.capabilities.event_msg) {
		case 0:
			printf("Per-threshold\n");
			break;
		case 1:
			printf("Entire Sensor Only\n");
			break;
		case 2:
			printf("Global Disable Only\n");
			break;
		case 3:
			printf("No Events From Sensor\n");
			break;
		}

		ipmi_sdr_print_discrete_state(intf, "States Asserted",
					      sensor->sensor.type,
					      sensor->event_type,
					      sr->s_data2,
					      sr->s_data3);
		ipmi_sdr_print_sensor_mask(intf, &sensor->mask, sensor->sensor.type,
					   sensor->event_type, DISCRETE_SENSOR);
		ipmi_sdr_print_sensor_event_status(intf,
						   sensor->keys.sensor_num,
						   sensor->sensor.type,
						   sensor->event_type,
						   DISCRETE_SENSOR,
						   target,
						   lun, channel);
		ipmi_sdr_print_sensor_event_enable(intf,
						   sensor->keys.sensor_num,
						   sensor->sensor.type,
						   sensor->event_type,
						   DISCRETE_SENSOR,
						   target,
						   lun, channel);
		printf(" OEM                   : %X\n",
					sr->full ? sr->full->oem : sr->compact->oem);
		printf("\n");

		return 0;	/* done */
	}
	printf(" Sensor Type (Threshold)  : %s (0x%02x)\n",
		ipmi_get_sensor_type(intf, sensor->sensor.type),
		sensor->sensor.type);

	printf(" Sensor Reading        : ");
	if (sr->s_reading_valid) {
		if (sr->full) {
			uint16_t raw_tol = __TO_TOL(sr->full->mtol);
			if (UNITS_ARE_DISCRETE(sensor)) {
				printf("0x%02X (+/- 0x%02X) %s\n",
				sr->s_reading, raw_tol, sr->s_a_units);
			}
			else {
				double tol = sdr_convert_sensor_tolerance(sr->full, raw_tol);
				printf("%.*f (+/- %.*f) %s\n",
				       (sr->s_a_val == (int) sr->s_a_val) ? 0 : 3,
				       sr->s_a_val, (tol == (int) tol) ? 0 :
				       3, tol, sr->s_a_units);
			}
		} else {
			printf("0x%02X %s\n", sr->s_reading, sr->s_a_units);
		}
	} else if (sr->s_scanning_disabled)
		printf("Disabled\n");
	else
		printf("No Reading\n");

	printf(" Status                : %s\n",
	       ipmi_sdr_get_thresh_status(sr, "Not Available"));

	if(sr->full) {
		SENSOR_PRINT_NORMAL(sr->full, "Nominal Reading", nominal_read);
		SENSOR_PRINT_NORMAL(sr->full, "Normal Minimum", normal_min);
		SENSOR_PRINT_NORMAL(sr->full, "Normal Maximum", normal_max);

		SENSOR_PRINT_THRESH(sr->full, "Upper non-recoverable", upper.non_recover, unr);
		SENSOR_PRINT_THRESH(sr->full, "Upper critical", upper.critical, ucr);
		SENSOR_PRINT_THRESH(sr->full, "Upper non-critical", upper.non_critical, unc);
		SENSOR_PRINT_THRESH(sr->full, "Lower non-recoverable", lower.non_recover, lnr);
		SENSOR_PRINT_THRESH(sr->full, "Lower critical", lower.critical, lcr);
		SENSOR_PRINT_THRESH(sr->full, "Lower non-critical", lower.non_critical, lnc);
	}
	ipmi_sdr_print_sensor_hysteresis(sensor, sr->full,
		sr->full ? sr->full->threshold.hysteresis.positive :
		sr->compact->threshold.hysteresis.positive, "Positive Hysteresis");

	ipmi_sdr_print_sensor_hysteresis(sensor, sr->full,
		sr->full ? sr->full->threshold.hysteresis.negative :
		sr->compact->threshold.hysteresis.negative, "Negative Hysteresis");

	print_sensor_min_max(sr->full);

	printf(" Event Message Control : ");
	switch (sensor->sensor.capabilities.event_msg) {
	case 0:
		printf("Per-threshold\n");
		break;
	case 1:
		printf("Entire Sensor Only\n");
		break;
	case 2:
		printf("Global Disable Only\n");
		break;
	case 3:
		printf("No Events From Sensor\n");
		break;
	}

	printf(" Readable Thresholds   : ");
	switch (sensor->sensor.capabilities.threshold) {
	case 0:
		printf("No Thresholds\n");
		break;
	case 1:		/* readable according to mask */
	case 2:		/* readable and settable according to mask */
		if (sensor->mask.type.threshold.read.lnr)
			printf("lnr ");
		if (sensor->mask.type.threshold.read.lcr)
			printf("lcr ");
		if (sensor->mask.type.threshold.read.lnc)
			printf("lnc ");
		if (sensor->mask.type.threshold.read.unc)
			printf("unc ");
		if (sensor->mask.type.threshold.read.ucr)
			printf("ucr ");
		if (sensor->mask.type.threshold.read.unr)
			printf("unr ");
		printf("\n");
		break;
	case 3:
		printf("Thresholds Fixed\n");
		break;
	}

	printf(" Settable Thresholds   : ");
	switch (sensor->sensor.capabilities.threshold) {
	case 0:
		printf("No Thresholds\n");
		break;
	case 1:		/* readable according to mask */
	case 2:		/* readable and settable according to mask */
		if (sensor->mask.type.threshold.set.lnr)
			printf("lnr ");
		if (sensor->mask.type.threshold.set.lcr)
			printf("lcr ");
		if (sensor->mask.type.threshold.set.lnc)
			printf("lnc ");
		if (sensor->mask.type.threshold.set.unc)
			printf("unc ");
		if (sensor->mask.type.threshold.set.ucr)
			printf("ucr ");
		if (sensor->mask.type.threshold.set.unr)
			printf("unr ");
		printf("\n");
		break;
	case 3:
		printf("Thresholds Fixed\n");
		break;
	}

	if (sensor->mask.type.threshold.status_lnr ||
	    sensor->mask.type.threshold.status_lcr ||
	    sensor->mask.type.threshold.status_lnc ||
	    sensor->mask.type.threshold.status_unc ||
	    sensor->mask.type.threshold.status_ucr ||
	    sensor->mask.type.threshold.status_unr) {
		printf(" Threshold Read Mask   : ");
		if (sensor->mask.type.threshold.status_lnr)
			printf("lnr ");
		if (sensor->mask.type.threshold.status_lcr)
			printf("lcr ");
		if (sensor->mask.type.threshold.status_lnc)
			printf("lnc ");
		if (sensor->mask.type.threshold.status_unc)
			printf("unc ");
		if (sensor->mask.type.threshold.status_ucr)
			printf("ucr ");
		if (sensor->mask.type.threshold.status_unr)
			printf("unr ");
		printf("\n");
	}

	ipmi_sdr_print_sensor_mask(intf, &sensor->mask,
				   sensor->sensor.type,
				   sensor->event_type, ANALOG_SENSOR);
	ipmi_sdr_print_sensor_event_status(intf,
					   sensor->keys.sensor_num,
					   sensor->sensor.type,
					   sensor->event_type, ANALOG_SENSOR,
					   target,
					   lun, channel);

	ipmi_sdr_print_sensor_event_enable(intf,
					   sensor->keys.sensor_num,
					   sensor->sensor.type,
					   sensor->event_type, ANALOG_SENSOR,
					   target,
					   lun, channel);

	printf("\n");
	return 0;
}

static inline int
get_offset(uint8_t x)
{
	int i;
	for (i = 0; i < 8; i++)
		if (x >> i == 1)
			return i;
	return 0;
}

/* ipmi_sdr_print_discrete_state_mini  -  print list of asserted states
 *                                        for a discrete sensor
 *
 * @header	: header string if necessary
 * @separator	: field separator string
 * @sensor_type	: sensor type code
 * @event_type	: event type code
 * @state	: mask of asserted states
 *
 * no meaningful return value
 */
void
ipmi_sdr_print_discrete_state_mini(struct ipmi_intf *intf,
				   const char *header, const char *separator,
				   uint8_t sensor_type, uint8_t event_type,
				   uint8_t state1, uint8_t state2)
{
	const struct ipmi_event_sensor_types *evt;
	int pre = 0, c = 0;

	if (state1 == 0 && (state2 & 0x7f) == 0)
		return;

	if (header)
		printf("%s", header);

	for (evt = ipmi_get_first_event_sensor_type(intf, sensor_type, event_type);
			evt != NULL; evt = ipmi_get_next_event_sensor_type(evt)) {
		if (evt->data != 0xFF) {
			continue;
		}

		if (evt->offset > 7) {
			if ((1 << (evt->offset - 8)) & (state2 & 0x7f)) {
				if (pre++ != 0) {
					printf("%s", separator);
				}
				if (evt->desc) {
					printf("%s", evt->desc);
				}
			}
		} else {
			if ((1 << evt->offset) & state1) {
				if (pre++ != 0) {
					printf("%s", separator);
				}
				if (evt->desc) {
					printf("%s", evt->desc);
				}
			}
		}
		c++;
	}
}

/* ipmi_sdr_print_discrete_state  -  print list of asserted states
 *                                   for a discrete sensor
 *
 * @desc        : description for this line
 * @sensor_type	: sensor type code
 * @event_type	: event type code
 * @state	: mask of asserted states
 *
 * no meaningful return value
 */
void
ipmi_sdr_print_discrete_state(struct ipmi_intf *intf, const char *desc,
			      uint8_t sensor_type, uint8_t event_type,
			      uint8_t state1, uint8_t state2)
{
	const struct ipmi_event_sensor_types *evt;
	int pre = 0, c = 0;

	if (state1 == 0 && (state2 & 0x7f) == 0)
		return;

	for (evt = ipmi_get_first_event_sensor_type(intf, sensor_type, event_type);
			evt != NULL; evt = ipmi_get_next_event_sensor_type(evt)) {
		if (evt->data != 0xFF) {
			continue;
		}

		if (pre == 0) {
			printf(" %-21s : %s\n", desc, ipmi_get_sensor_type(intf, sensor_type));
			pre = 1;
		}

		if (evt->offset > 7) {
			if ((1 << (evt->offset - 8)) & (state2 & 0x7f)) {
				if (evt->desc) {
					printf("                         "
					       "[%s]\n",
					       evt->desc);
				} else {
					printf("                         "
					       "[no description]\n");
				}
			}
		} else {
			if ((1 << evt->offset) & state1) {
				if (evt->desc) {
					printf("                         "
					       "[%s]\n",
					       evt->desc);
				} else {
					printf("                         "
					       "[no description]\n");
				}
			}
		}
		c++;
	}
}


/* ipmi_sdr_print_sensor_eventonly  -  print SDR event only record
 *
 * @intf:	ipmi interface
 * @sensor:	event only sdr record
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_sensor_eventonly(struct ipmi_intf *intf,
				struct sdr_record_eventonly_sensor *sensor)
{
	char desc[17];

	if (sensor == NULL)
		return -1;

	memset(desc, 0, sizeof (desc));
	snprintf(desc, sizeof(desc), "%.*s", (sensor->id_code & 0x1f) + 1, sensor->id_string);

	if (verbose) {
		printf("Sensor ID              : %s (0x%x)\n",
		       sensor->id_code ? desc : "", sensor->keys.sensor_num);
		printf("Entity ID              : %d.%d (%s)\n",
		       sensor->entity.id, sensor->entity.instance,
		       val2str(sensor->entity.id, entity_id_vals));
		printf("Sensor Type            : %s (0x%02x)\n",
			ipmi_get_sensor_type(intf, sensor->sensor_type),
			sensor->sensor_type);
		lprintf(LOG_DEBUG, "Event Type Code        : 0x%02x",
			sensor->event_type);
		printf("\n");
	} else {
		if (csv_output)
			printf("%s,%02Xh,ns,%d.%d,Event-Only\n",
			       sensor->id_code ? desc : "",
			       sensor->keys.sensor_num,
			       sensor->entity.id, sensor->entity.instance);
		else if (sdr_extended)
			printf("%-16s | %02Xh | ns  | %2d.%1d | Event-Only\n",
			       sensor->id_code ? desc : "",
			       sensor->keys.sensor_num,
			       sensor->entity.id, sensor->entity.instance);
		else
			printf("%-16s | Event-Only        | ns\n",
			       sensor->id_code ? desc : "");
	}

	return 0;
}

/* ipmi_sdr_print_sensor_mc_locator  -  print SDR MC locator record
 *
 * @intf:	ipmi interface
 * @mc:		mc locator sdr record
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_sensor_mc_locator(struct ipmi_intf *intf,
				 struct sdr_record_mc_locator *mc)
{
	char desc[17];

	if (mc == NULL)
		return -1;

	memset(desc, 0, sizeof (desc));
	snprintf(desc, sizeof(desc), "%.*s", (mc->id_code & 0x1f) + 1, mc->id_string);

	if (verbose == 0) {
		if (csv_output)
			printf("%s,00h,ok,%d.%d\n",
			       mc->id_code ? desc : "",
			       mc->entity.id, mc->entity.instance);
		else if (sdr_extended) {
			printf("%-16s | 00h | ok  | %2d.%1d | ",
			       mc->id_code ? desc : "",
			       mc->entity.id, mc->entity.instance);

			printf("%s MC @ %02Xh\n",
			       (mc->
				pwr_state_notif & 0x1) ? "Static" : "Dynamic",
			       mc->dev_slave_addr);
		} else {
			printf("%-16s | %s MC @ %02Xh %s | ok\n",
			       mc->id_code ? desc : "",
			       (mc->
				pwr_state_notif & 0x1) ? "Static" : "Dynamic",
			       mc->dev_slave_addr,
			       (mc->pwr_state_notif & 0x1) ? " " : "");
		}

		return 0;	/* done */
	}

	printf("Device ID              : %s\n", mc->id_string);
	printf("Entity ID              : %d.%d (%s)\n",
	       mc->entity.id, mc->entity.instance,
	       val2str(mc->entity.id, entity_id_vals));

	printf("Device Slave Address   : %02Xh\n", mc->dev_slave_addr);
	printf("Channel Number         : %01Xh\n", mc->channel_num);

	printf("ACPI System P/S Notif  : %sRequired\n",
	       (mc->pwr_state_notif & 0x4) ? "" : "Not ");
	printf("ACPI Device P/S Notif  : %sRequired\n",
	       (mc->pwr_state_notif & 0x2) ? "" : "Not ");
	printf("Controller Presence    : %s\n",
	       (mc->pwr_state_notif & 0x1) ? "Static" : "Dynamic");
	printf("Logs Init Agent Errors : %s\n",
	       (mc->global_init & 0x8) ? "Yes" : "No");

	printf("Event Message Gen      : ");
	if (!(mc->global_init & 0x3))
		printf("Enable\n");
	else if ((mc->global_init & 0x3) == 0x1)
		printf("Disable\n");
	else if ((mc->global_init & 0x3) == 0x2)
		printf("Do Not Init Controller\n");
	else
		printf("Reserved\n");

	printf("Device Capabilities\n");
	printf(" Chassis Device        : %s\n",
	       (mc->dev_support & 0x80) ? "Yes" : "No");
	printf(" Bridge                : %s\n",
	       (mc->dev_support & 0x40) ? "Yes" : "No");
	printf(" IPMB Event Generator  : %s\n",
	       (mc->dev_support & 0x20) ? "Yes" : "No");
	printf(" IPMB Event Receiver   : %s\n",
	       (mc->dev_support & 0x10) ? "Yes" : "No");
	printf(" FRU Inventory Device  : %s\n",
	       (mc->dev_support & 0x08) ? "Yes" : "No");
	printf(" SEL Device            : %s\n",
	       (mc->dev_support & 0x04) ? "Yes" : "No");
	printf(" SDR Repository        : %s\n",
	       (mc->dev_support & 0x02) ? "Yes" : "No");
	printf(" Sensor Device         : %s\n",
	       (mc->dev_support & 0x01) ? "Yes" : "No");

	printf("\n");

	return 0;
}

/* ipmi_sdr_print_sensor_generic_locator  -  print generic device locator record
 *
 * @intf:	ipmi interface
 * @gen:	generic device locator sdr record
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_sensor_generic_locator(struct ipmi_intf *intf,
				      struct sdr_record_generic_locator *dev)
{
	char desc[17];

	memset(desc, 0, sizeof (desc));
	snprintf(desc, sizeof(desc), "%.*s", (dev->id_code & 0x1f) + 1, dev->id_string);

	if (!verbose) {
		if (csv_output)
			printf("%s,00h,ns,%d.%d\n",
			       dev->id_code ? desc : "",
			       dev->entity.id, dev->entity.instance);
		else if (sdr_extended)
			printf
			    ("%-16s | 00h | ns  | %2d.%1d | Generic Device @%02Xh:%02Xh.%1d\n",
			     dev->id_code ? desc : "", dev->entity.id,
			     dev->entity.instance, dev->dev_access_addr,
			     dev->dev_slave_addr, dev->oem);
		else
			printf("%-16s | Generic @%02X:%02X.%-2d | ok\n",
			       dev->id_code ? desc : "",
			       dev->dev_access_addr,
			       dev->dev_slave_addr, dev->oem);

		return 0;
	}

	printf("Device ID              : %s\n", dev->id_string);
	printf("Entity ID              : %d.%d (%s)\n",
	       dev->entity.id, dev->entity.instance,
	       val2str(dev->entity.id, entity_id_vals));

	printf("Device Access Address  : %02Xh\n", dev->dev_access_addr);
	printf("Device Slave Address   : %02Xh\n", dev->dev_slave_addr);
	printf("Address Span           : %02Xh\n", dev->addr_span);
	printf("Channel Number         : %01Xh\n", dev->channel_num);
	printf("LUN.Bus                : %01Xh.%01Xh\n", dev->lun, dev->bus);
	printf("Device Type.Modifier   : %01Xh.%01Xh (%s)\n",
	       dev->dev_type, dev->dev_type_modifier,
	       val2str(dev->dev_type << 8 | dev->dev_type_modifier,
		       entity_device_type_vals));
	printf("OEM                    : %02Xh\n", dev->oem);
	printf("\n");

	return 0;
}

/* ipmi_sdr_print_sensor_fru_locator  -  print FRU locator record
 *
 * @intf:	ipmi interface
 * @fru:	fru locator sdr record
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_sensor_fru_locator(struct ipmi_intf *intf,
				  struct sdr_record_fru_locator *fru)
{
	char desc[17];

	memset(desc, 0, sizeof (desc));
	snprintf(desc, sizeof(desc), "%.*s", (fru->id_code & 0x1f) + 1, fru->id_string);

	if (!verbose) {
		if (csv_output)
			printf("%s,00h,ns,%d.%d\n",
			       fru->id_code ? desc : "",
			       fru->entity.id, fru->entity.instance);
		else if (sdr_extended)
			printf("%-16s | 00h | ns  | %2d.%1d | %s FRU @%02Xh\n",
			       fru->id_code ? desc : "",
			       fru->entity.id, fru->entity.instance,
			       (fru->logical) ? "Logical" : "Physical",
			       fru->device_id);
		else
			printf("%-16s | %s FRU @%02Xh %02x.%x | ok\n",
			       fru->id_code ? desc : "",
			       (fru->logical) ? "Log" : "Phy",
			       fru->device_id,
			       fru->entity.id, fru->entity.instance);

		return 0;
	}

	printf("Device ID              : %s\n", fru->id_string);
	printf("Entity ID              : %d.%d (%s)\n",
	       fru->entity.id, fru->entity.instance,
	       val2str(fru->entity.id, entity_id_vals));

	printf("Device Access Address  : %02Xh\n", fru->dev_slave_addr);
	printf("%s: %02Xh\n",
	       fru->logical ? "Logical FRU Device     " :
	       "Slave Address          ", fru->device_id);
	printf("Channel Number         : %01Xh\n", fru->channel_num);
	printf("LUN.Bus                : %01Xh.%01Xh\n", fru->lun, fru->bus);
	printf("Device Type.Modifier   : %01Xh.%01Xh (%s)\n",
	       fru->dev_type, fru->dev_type_modifier,
	       val2str(fru->dev_type << 8 | fru->dev_type_modifier,
		       entity_device_type_vals));
	printf("OEM                    : %02Xh\n", fru->oem);
	printf("\n");

	return 0;
}

/* ipmi_sdr_print_sensor_entity_assoc  -  print SDR entity association record
 *
 * @intf:	ipmi interface
 * @mc:		entity association sdr record
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_sensor_entity_assoc(struct ipmi_intf *intf,
				   struct sdr_record_entity_assoc *assoc)
{
	return 0;
}

/* ipmi_sdr_print_sensor_oem_intel  -  print Intel OEM sensors
 *
 * @intf:	ipmi interface
 * @oem:	oem sdr record
 *
 * returns 0 on success
 * returns -1 on error
 */
static int
ipmi_sdr_print_sensor_oem_intel(struct ipmi_intf *intf,
				struct sdr_record_oem *oem)
{
	switch (oem->data[3]) {	/* record sub-type */
	case 0x02:		/* Power Unit Map */
		if (verbose) {
			printf
			    ("Sensor ID              : Power Unit Redundancy (0x%x)\n",
			     oem->data[4]);
			printf
			    ("Sensor Type            : Intel OEM - Power Unit Map\n");
			printf("Redundant Supplies     : %d", oem->data[6]);
			if (oem->data[5])
				printf(" (flags %xh)", oem->data[5]);
			printf("\n");
		}

		switch (oem->data_len) {
		case 7:	/* SR1300, non-redundant */
			if (verbose)
				printf("Power Redundancy       : No\n");
			else if (csv_output)
				printf("Power Redundancy,Not Available,nr\n");
			else
				printf
				    ("Power Redundancy | Not Available     | nr\n");
			break;
		case 8:	/* SR2300, redundant, PS1 & PS2 present */
			if (verbose) {
				printf("Power Redundancy       : No\n");
				printf("Power Supply 2 Sensor  : %x\n",
				       oem->data[8]);
			} else if (csv_output) {
				printf("Power Redundancy,PS@%02xh,nr\n",
				       oem->data[8]);
			} else {
				printf
				    ("Power Redundancy | PS@%02xh            | nr\n",
				     oem->data[8]);
			}
			break;
		case 9:	/* SR2300, non-redundant, PSx present */
			if (verbose) {
				printf("Power Redundancy       : Yes\n");
				printf("Power Supply Sensor    : %x\n",
				       oem->data[7]);
				printf("Power Supply Sensor    : %x\n",
				       oem->data[8]);
			} else if (csv_output) {
				printf
				    ("Power Redundancy,PS@%02xh + PS@%02xh,ok\n",
				     oem->data[7], oem->data[8]);
			} else {
				printf
				    ("Power Redundancy | PS@%02xh + PS@%02xh   | ok\n",
				     oem->data[7], oem->data[8]);
			}
			break;
		}
		if (verbose)
			printf("\n");
		break;
	case 0x03:		/* Fan Speed Control */
		break;
	case 0x06:		/* System Information */
		break;
	case 0x07:		/* Ambient Temperature Fan Speed Control */
		break;
	default:
		lprintf(LOG_DEBUG, "Unknown Intel OEM SDR Record type %02x",
			oem->data[3]);
	}

	return 0;
}

/* ipmi_sdr_print_sensor_oem  -  print OEM sensors
 *
 * This function is generally only filled out by decoding what
 * a particular BMC might stuff into its OEM records.  The
 * records are keyed off manufacturer ID and record subtypes.
 *
 * @intf:	ipmi interface
 * @oem:	oem sdr record
 *
 * returns 0 on success
 * returns -1 on error
 */
static int
ipmi_sdr_print_sensor_oem(struct ipmi_intf *intf, struct sdr_record_oem *oem)
{
	int rc = 0;

	if (oem == NULL)
		return -1;
	if (oem->data_len == 0 || oem->data == NULL)
		return -1;

	if (verbose > 2)
		printbuf(oem->data, oem->data_len, "OEM Record");

	/* intel manufacturer id */
	if (oem->data[0] == 0x57 &&
	    oem->data[1] == 0x01 && oem->data[2] == 0x00) {
		rc = ipmi_sdr_print_sensor_oem_intel(intf, oem);
	}

	return rc;
}

/* ipmi_sdr_print_name_from_rawentry  -  Print SDR name  from raw data
 *
 * @intf:	ipmi interface
 * @type:	sensor type
 * @raw:	raw sensor data
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_name_from_rawentry(struct ipmi_intf *intf, uint16_t id,
                                  uint8_t type, uint8_t *raw)
{
   union {
      struct sdr_record_full_sensor *full;
      struct sdr_record_compact_sensor *compact;
      struct sdr_record_eventonly_sensor *eventonly;
      struct sdr_record_generic_locator *genloc;
      struct sdr_record_fru_locator *fruloc;
      struct sdr_record_mc_locator *mcloc;
      struct sdr_record_entity_assoc *entassoc;
      struct sdr_record_oem *oem;
   } record;

   int rc =0;
   char desc[17];
   const char *id_string;
   uint8_t id_code;
   memset(desc, ' ', sizeof (desc));

   switch ( type) {
      case SDR_RECORD_TYPE_FULL_SENSOR:
      record.full = (struct sdr_record_full_sensor *) raw;
      id_code = record.full->id_code;
      id_string = record.full->id_string;
      break;

      case SDR_RECORD_TYPE_COMPACT_SENSOR:
      record.compact = (struct sdr_record_compact_sensor *) raw	;
      id_code = record.compact->id_code;
      id_string = record.compact->id_string;
      break;

      case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
      record.eventonly  = (struct sdr_record_eventonly_sensor *) raw ;
      id_code = record.eventonly->id_code;
      id_string = record.eventonly->id_string;
      break;

      case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
      record.mcloc  = (struct sdr_record_mc_locator *) raw ;
      id_code = record.mcloc->id_code;
      id_string = record.mcloc->id_string;
      break;

      default:
      rc = -1;
   }
   if (!rc) {
       snprintf(desc, sizeof(desc), "%.*s", (id_code & 0x1f) + 1, id_string);
   }

   lprintf(LOG_INFO, "ID: 0x%04x , NAME: %-16s", id, desc);
   return rc;
}

/* ipmi_sdr_print_rawentry  -  Print SDR entry from raw data
 *
 * @intf:	ipmi interface
 * @type:	sensor type
 * @raw:	raw sensor data
 * @len:	length of raw sensor data
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_rawentry(struct ipmi_intf *intf, uint8_t type,
			uint8_t * raw, int len)
{
	int rc = 0;

	switch (type) {
	case SDR_RECORD_TYPE_FULL_SENSOR:
	case SDR_RECORD_TYPE_COMPACT_SENSOR:
		rc = ipmi_sdr_print_sensor_fc(intf,
					(struct sdr_record_common_sensor *) raw,
					type);
		break;
	case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
		rc = ipmi_sdr_print_sensor_eventonly(intf,
						     (struct
						      sdr_record_eventonly_sensor
						      *) raw);
		break;
	case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
		rc = ipmi_sdr_print_sensor_generic_locator(intf,
							   (struct
							    sdr_record_generic_locator
							    *) raw);
		break;
	case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
		rc = ipmi_sdr_print_sensor_fru_locator(intf,
						       (struct
							sdr_record_fru_locator
							*) raw);
		break;
	case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
		rc = ipmi_sdr_print_sensor_mc_locator(intf,
						      (struct
						       sdr_record_mc_locator *)
						      raw);
		break;
	case SDR_RECORD_TYPE_ENTITY_ASSOC:
		rc = ipmi_sdr_print_sensor_entity_assoc(intf,
							(struct
							 sdr_record_entity_assoc
							 *) raw);
		break;
	case SDR_RECORD_TYPE_OEM:{
			struct sdr_record_oem oem;
			oem.data = raw;
			oem.data_len = len;
			rc = ipmi_sdr_print_sensor_oem(intf,
						       (struct sdr_record_oem *)
						       &oem);
			break;
		}
	case SDR_RECORD_TYPE_DEVICE_ENTITY_ASSOC:
	case SDR_RECORD_TYPE_MC_CONFIRMATION:
	case SDR_RECORD_TYPE_BMC_MSG_CHANNEL_INFO:
		/* not implemented */
		break;
	}

	return rc;
}

/* ipmi_sdr_print_listentry  -  Print SDR entry from list
 *
 * @intf:	ipmi interface
 * @entry:	sdr record list entry
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_listentry(struct ipmi_intf *intf, struct sdr_record_list *entry)
{
	int rc = 0;

	switch (entry->type) {
	case SDR_RECORD_TYPE_FULL_SENSOR:
	case SDR_RECORD_TYPE_COMPACT_SENSOR:
		rc = ipmi_sdr_print_sensor_fc(intf, entry->record.common, entry->type);
		break;
	case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
		rc = ipmi_sdr_print_sensor_eventonly(intf,
						     entry->record.eventonly);
		break;
	case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
		rc = ipmi_sdr_print_sensor_generic_locator(intf,
							   entry->record.
							   genloc);
		break;
	case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
		rc = ipmi_sdr_print_sensor_fru_locator(intf,
						       entry->record.fruloc);
		break;
	case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
		rc = ipmi_sdr_print_sensor_mc_locator(intf,
						      entry->record.mcloc);
		break;
	case SDR_RECORD_TYPE_ENTITY_ASSOC:
		rc = ipmi_sdr_print_sensor_entity_assoc(intf,
							entry->record.entassoc);
		break;
	case SDR_RECORD_TYPE_OEM:
		rc = ipmi_sdr_print_sensor_oem(intf, entry->record.oem);
		break;
	case SDR_RECORD_TYPE_DEVICE_ENTITY_ASSOC:
	case SDR_RECORD_TYPE_MC_CONFIRMATION:
	case SDR_RECORD_TYPE_BMC_MSG_CHANNEL_INFO:
		/* not implemented yet */
		break;
	}

	return rc;
}

/* ipmi_sdr_print_sdr  -  iterate through SDR printing records
 *
 * intf:	ipmi interface
 * type:	record type to print
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_sdr(struct ipmi_intf *intf, uint8_t type)
{
	struct sdr_get_rs *header;
	struct sdr_record_list *e;
	int rc = 0;

	lprintf(LOG_DEBUG, "Querying SDR for sensor list");

	if (sdr_list_itr == NULL) {
		sdr_list_itr = ipmi_sdr_start(intf, 0);
		if (sdr_list_itr == NULL) {
			lprintf(LOG_ERR, "Unable to open SDR for reading");
			return -1;
		}
	}

	for (e = sdr_list_head; e != NULL; e = e->next) {
		if (type != e->type && type != 0xff && type != 0xfe)
			continue;
		if (type == 0xfe &&
		    e->type != SDR_RECORD_TYPE_FULL_SENSOR &&
		    e->type != SDR_RECORD_TYPE_COMPACT_SENSOR)
			continue;
		if (ipmi_sdr_print_listentry(intf, e) < 0)
			rc = -1;
	}

	while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
		uint8_t *rec;
		struct sdr_record_list *sdrr;

		rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
		if (rec == NULL) {
			lprintf(LOG_ERR, "ipmitool: ipmi_sdr_get_record() failed");
			rc = -1;
			continue;
		}

		sdrr = malloc(sizeof (struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			if (rec != NULL) {
				free(rec);
				rec = NULL;
			}
			break;
		}
		memset(sdrr, 0, sizeof (struct sdr_record_list));
		sdrr->id = header->id;
		sdrr->type = header->type;

		switch (header->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sdrr->record.common =
			    (struct sdr_record_common_sensor *) rec;
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			sdrr->record.eventonly =
			    (struct sdr_record_eventonly_sensor *) rec;
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			sdrr->record.genloc =
			    (struct sdr_record_generic_locator *) rec;
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			sdrr->record.fruloc =
			    (struct sdr_record_fru_locator *) rec;
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			sdrr->record.mcloc =
			    (struct sdr_record_mc_locator *) rec;
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			sdrr->record.entassoc =
			    (struct sdr_record_entity_assoc *) rec;
			break;
		default:
			free(rec);
			rec = NULL;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

                lprintf(LOG_DEBUG, "SDR record ID   : 0x%04x", sdrr->id);

		if (type == header->type || type == 0xff ||
		    (type == 0xfe &&
		     (header->type == SDR_RECORD_TYPE_FULL_SENSOR ||
		      header->type == SDR_RECORD_TYPE_COMPACT_SENSOR))) {
			if (ipmi_sdr_print_rawentry(intf, header->type,
						    rec, header->length) < 0)
				rc = -1;
		}

		/* add to global record liset */
		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;
	}

	return rc;
}

/* ipmi_sdr_get_reservation  -  Obtain SDR reservation ID
 *
 * @intf:	ipmi interface
 * @reserve_id:	pointer to short int for storing the id
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_get_reservation(struct ipmi_intf *intf, int use_builtin,
                         uint16_t * reserve_id)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;

	/* obtain reservation ID */
	memset(&req, 0, sizeof (req));

	if (use_builtin == 0) {
		req.msg.netfn = IPMI_NETFN_STORAGE;
	} else {
		req.msg.netfn = IPMI_NETFN_SE;
	}

	req.msg.cmd = GET_SDR_RESERVE_REPO;
	rsp = intf->sendrecv(intf, &req);

	/* be slient for errors, they are handled by calling function */
	if (rsp == NULL)
		return -1;
	if (rsp->ccode > 0)
		return -1;

	*reserve_id = ((struct sdr_reserve_repo_rs *) &(rsp->data))->reserve_id;
	lprintf(LOG_DEBUG, "SDR reservation ID %04x", *reserve_id);

	return 0;
}

/* ipmi_sdr_start  -  setup sdr iterator
 *
 * @intf:	ipmi interface
 *
 * returns sdr iterator structure pointer
 * returns NULL on error
 */
struct ipmi_sdr_iterator *
ipmi_sdr_start(struct ipmi_intf *intf, int use_builtin)
{
	struct ipmi_sdr_iterator *itr;
	struct ipmi_rs *rsp;
	struct ipmi_rq req;

	struct ipm_devid_rsp *devid;

	itr = malloc(sizeof (struct ipmi_sdr_iterator));
	if (itr == NULL) {
		lprintf(LOG_ERR, "ipmitool: malloc failure");
		return NULL;
	}

	/* check SDRR capability */
	memset(&req, 0, sizeof (req));
	req.msg.netfn = IPMI_NETFN_APP;
	req.msg.cmd = BMC_GET_DEVICE_ID;
	req.msg.data_len = 0;

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "Get Device ID command failed");
		free(itr);
		itr = NULL;
		return NULL;
	}
	if (rsp->ccode > 0) {
		lprintf(LOG_ERR, "Get Device ID command failed: %#x %s",
			rsp->ccode, val2str(rsp->ccode, completion_code_vals));
		free(itr);
		itr = NULL;
		return NULL;
	}
	devid = (struct ipm_devid_rsp *) rsp->data;

   sdriana =  (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id);

	if (!use_builtin && (devid->device_revision & IPM_DEV_DEVICE_ID_SDR_MASK)) {
		if ((devid->adtl_device_support & 0x02) == 0) {
			if ((devid->adtl_device_support & 0x01)) {
				lprintf(LOG_DEBUG, "Using Device SDRs\n");
				use_built_in = 1;
			} else {
				lprintf(LOG_ERR, "Error obtaining SDR info");
				free(itr);
				itr = NULL;
				return NULL;
			}
		} else {
			lprintf(LOG_DEBUG, "Using SDR from Repository \n");
		}
	}
	itr->use_built_in = use_builtin ? 1 : use_built_in;
   /***********************/
	if (itr->use_built_in == 0) {
		struct sdr_repo_info_rs sdr_info;
		/* get sdr repository info */
		memset(&req, 0, sizeof (req));
		req.msg.netfn = IPMI_NETFN_STORAGE;
		req.msg.cmd = GET_SDR_REPO_INFO;

		rsp = intf->sendrecv(intf, &req);
		if (rsp == NULL) {
			lprintf(LOG_ERR, "Error obtaining SDR info");
			free(itr);
			itr = NULL;
			return NULL;
		}
		if (rsp->ccode > 0) {
			lprintf(LOG_ERR, "Error obtaining SDR info: %s",
				val2str(rsp->ccode, completion_code_vals));
			free(itr);
			itr = NULL;
			return NULL;
		}

		memcpy(&sdr_info, rsp->data, sizeof (sdr_info));
		/* IPMIv1.0 == 0x01
		   * IPMIv1.5 == 0x51
		   * IPMIv2.0 == 0x02
		 */
		if ((sdr_info.version != 0x51) &&
		    (sdr_info.version != 0x01) &&
		    (sdr_info.version != 0x02)) {
			lprintf(LOG_WARN, "WARNING: Unknown SDR repository "
				"version 0x%02x", sdr_info.version);
		}

		itr->total = sdr_info.count;
		itr->next = 0;

		lprintf(LOG_DEBUG, "SDR free space: %d", sdr_info.free);
		lprintf(LOG_DEBUG, "SDR records   : %d", sdr_info.count);

		/* Build SDRR if there is no record in repository */
		if( sdr_info.count == 0 ) {
		   lprintf(LOG_DEBUG, "Rebuilding SDRR...");

		   if( ipmi_sdr_add_from_sensors( intf, 0 ) != 0 ) {
		      lprintf(LOG_ERR, "Could not build SDRR!");
		      free(itr);
					itr = NULL;
		      return NULL;
		   }
		}
	} else {
		struct sdr_device_info_rs sdr_info;
		/* get device sdr info */
		memset(&req, 0, sizeof (req));
		req.msg.netfn = IPMI_NETFN_SE;
		req.msg.cmd = GET_DEVICE_SDR_INFO;

		rsp = intf->sendrecv(intf, &req);
		if (!rsp || !rsp->data_len || rsp->ccode) {
			printf("Err in cmd get sensor sdr info\n");
			free(itr);
			itr = NULL;
			return NULL;
		}
		memcpy(&sdr_info, rsp->data, sizeof (sdr_info));

		itr->total = sdr_info.count;
		itr->next = 0;
		lprintf(LOG_DEBUG, "SDR records   : %d", sdr_info.count);
	}

	if (ipmi_sdr_get_reservation(intf, itr->use_built_in,
                                &(itr->reservation)) < 0) {
		lprintf(LOG_ERR, "Unable to obtain SDR reservation");
		free(itr);
		itr = NULL;
		return NULL;
	}

	return itr;
}

/* ipmi_sdr_get_record  -  return RAW SDR record
 *
 * @intf:	ipmi interface
 * @header:	SDR header
 * @itr:	SDR iterator
 *
 * returns raw SDR data
 * returns NULL on error
 */
uint8_t *
ipmi_sdr_get_record(struct ipmi_intf * intf, struct sdr_get_rs * header,
		    struct ipmi_sdr_iterator * itr)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	struct sdr_get_rq sdr_rq;
	uint8_t *data;
	int i = 0, len = header->length;

	if (len < 1)
		return NULL;

	data = malloc(len + 1);
	if (data == NULL) {
		lprintf(LOG_ERR, "ipmitool: malloc failure");
		return NULL;
	}
	memset(data, 0, len + 1);

	memset(&sdr_rq, 0, sizeof (sdr_rq));
	sdr_rq.reserve_id = itr->reservation;
	sdr_rq.id = header->id;
	sdr_rq.offset = 0;

	memset(&req, 0, sizeof (req));
	if (itr->use_built_in == 0) {
		req.msg.netfn = IPMI_NETFN_STORAGE;
		req.msg.cmd = GET_SDR;
	} else {
		req.msg.netfn = IPMI_NETFN_SE;
		req.msg.cmd = GET_DEVICE_SDR;
	}
	req.msg.data = (uint8_t *) & sdr_rq;
	req.msg.data_len = sizeof (sdr_rq);

	/* check if max length is null */
	if ( sdr_max_read_len == 0 ) {
		/* get maximum response size */
		sdr_max_read_len = ipmi_intf_get_max_response_data_size(intf) - 2;

		/* cap the number of bytes to read */
		if (sdr_max_read_len > 0xFE) {
			sdr_max_read_len = 0xFE;
		}
	}

	/* read SDR record with partial reads
	 * because a full read usually exceeds the maximum
	 * transport buffer size.  (completion code 0xca)
	 */
	while (i < len) {
		sdr_rq.length = (len - i < sdr_max_read_len) ?
		    len - i : sdr_max_read_len;
		sdr_rq.offset = i + 5;	/* 5 header bytes */

		lprintf(LOG_DEBUG, "Getting %d bytes from SDR at offset %d",
			sdr_rq.length, sdr_rq.offset);

		rsp = intf->sendrecv(intf, &req);
		if (rsp == NULL) {
		    sdr_max_read_len = sdr_rq.length - 1;
		    if (sdr_max_read_len > 0) {
			/* no response may happen if requests are bridged
			   and too many bytes are requested */
			continue;
		    } else {
			free(data);
			data = NULL;
			return NULL;
		    }
		}

		switch (rsp->ccode) {
		case 0xca:
			/* read too many bytes at once */
			sdr_max_read_len = sdr_rq.length - 1;
			continue;
		case 0xc5:
			/* lost reservation */
			lprintf(LOG_DEBUG, "SDR reservation cancelled. "
				"Sleeping a bit and retrying...");

			sleep(rand() & 3);

			if (ipmi_sdr_get_reservation(intf, itr->use_built_in,
                                      &(itr->reservation)) < 0) {
				free(data);
				data = NULL;
				return NULL;
			}
			sdr_rq.reserve_id = itr->reservation;
			continue;
		}

		/* special completion codes handled above */
		if (rsp->ccode > 0 || rsp->data_len == 0) {
			free(data);
			data = NULL;
			return NULL;
		}

		memcpy(data + i, rsp->data + 2, sdr_rq.length);
		i += sdr_max_read_len;
	}

	return data;
}

/* ipmi_sdr_end  -  cleanup SDR iterator
 *
 * @intf:	ipmi interface
 * @itr:	SDR iterator
 *
 * no meaningful return code
 */
void
ipmi_sdr_end(struct ipmi_intf *intf, struct ipmi_sdr_iterator *itr)
{
	if (itr) {
		free(itr);
		itr = NULL;
	}
}

/* __sdr_list_add  -  helper function to add SDR record to list
 *
 * @head:	list head
 * @entry:	new entry to add to end of list
 *
 * returns 0 on success
 * returns -1 on error
 */
static int
__sdr_list_add(struct sdr_record_list *head, struct sdr_record_list *entry)
{
	struct sdr_record_list *e;
	struct sdr_record_list *new;

	if (head == NULL)
		return -1;

	new = malloc(sizeof (struct sdr_record_list));
	if (new == NULL) {
		lprintf(LOG_ERR, "ipmitool: malloc failure");
		return -1;
	}
	memcpy(new, entry, sizeof (struct sdr_record_list));

	e = head;
	while (e->next)
		e = e->next;
	e->next = new;
	new->next = NULL;

	return 0;
}

/* __sdr_list_empty  -  low-level handler to clean up record list
 *
 * @head:	list head to clean
 *
 * no meaningful return code
 */
static void
__sdr_list_empty(struct sdr_record_list *head)
{
	struct sdr_record_list *e, *f;
	for (e = head; e != NULL; e = f) {
		f = e->next;
		free(e);
		e = NULL;
	}
	head = NULL;
}

/* ipmi_sdr_list_empty  -  clean global SDR list
 *
 * @intf:	ipmi interface
 *
 * no meaningful return code
 */
void
ipmi_sdr_list_empty(struct ipmi_intf *intf)
{
	struct sdr_record_list *list, *next;

	ipmi_sdr_end(intf, sdr_list_itr);

	for (list = sdr_list_head; list != NULL; list = next) {
		switch (list->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			if (list->record.common) {
				free(list->record.common);
				list->record.common = NULL;
			}
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			if (list->record.eventonly) {
				free(list->record.eventonly);
				list->record.eventonly = NULL;
			}
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			if (list->record.genloc) {
				free(list->record.genloc);
				list->record.genloc = NULL;
			}
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			if (list->record.fruloc) {
				free(list->record.fruloc);
				list->record.fruloc = NULL;
			}
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			if (list->record.mcloc) {
				free(list->record.mcloc);
				list->record.mcloc = NULL;
			}
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			if (list->record.entassoc) {
				free(list->record.entassoc);
				list->record.entassoc = NULL;
			}
			break;
		}
		next = list->next;
		free(list);
		list = NULL;
	}

	sdr_list_head = NULL;
	sdr_list_tail = NULL;
	sdr_list_itr = NULL;
}

/* ipmi_sdr_find_sdr_bynumtype  -  lookup SDR entry by number/type
 *
 * @intf:	ipmi interface
 * @gen_id:	sensor owner ID/LUN - SEL generator ID
 * @num:	sensor number to search for
 * @type:	sensor type to search for
 *
 * returns pointer to SDR list
 * returns NULL on error
 */
struct sdr_record_list *
ipmi_sdr_find_sdr_bynumtype(struct ipmi_intf *intf, uint16_t gen_id, uint8_t num, uint8_t type)
{
	struct sdr_get_rs *header;
	struct sdr_record_list *e;
	int found = 0;

	if (sdr_list_itr == NULL) {
		sdr_list_itr = ipmi_sdr_start(intf, 0);
		if (sdr_list_itr == NULL) {
			lprintf(LOG_ERR, "Unable to open SDR for reading");
			return NULL;
		}
	}

	/* check what we've already read */
	for (e = sdr_list_head; e != NULL; e = e->next) {
		switch (e->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			if (e->record.common->keys.sensor_num == num &&
			    e->record.common->keys.owner_id == (gen_id & 0x00ff) &&
			    e->record.common->sensor.type == type)
				return e;
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			if (e->record.eventonly->keys.sensor_num == num &&
			    e->record.eventonly->keys.owner_id == (gen_id & 0x00ff) &&
			    e->record.eventonly->sensor_type == type)
				return e;
			break;
		}
	}

	/* now keep looking */
	while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
		uint8_t *rec;
		struct sdr_record_list *sdrr;

		sdrr = malloc(sizeof (struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			break;
		}
		memset(sdrr, 0, sizeof (struct sdr_record_list));
		sdrr->id = header->id;
		sdrr->type = header->type;

		rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
		if (rec == NULL) {
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		switch (header->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sdrr->record.common =
			    (struct sdr_record_common_sensor *) rec;
			if (sdrr->record.common->keys.sensor_num == num
			    && sdrr->record.common->keys.owner_id == (gen_id & 0x00ff)
			    && sdrr->record.common->sensor.type == type)
				found = 1;
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			sdrr->record.eventonly =
			    (struct sdr_record_eventonly_sensor *) rec;
			if (sdrr->record.eventonly->keys.sensor_num == num
			    && sdrr->record.eventonly->keys.owner_id == (gen_id & 0x00ff)
			    && sdrr->record.eventonly->sensor_type == type)
				found = 1;
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			sdrr->record.genloc =
			    (struct sdr_record_generic_locator *) rec;
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			sdrr->record.fruloc =
			    (struct sdr_record_fru_locator *) rec;
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			sdrr->record.mcloc =
			    (struct sdr_record_mc_locator *) rec;
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			sdrr->record.entassoc =
			    (struct sdr_record_entity_assoc *) rec;
			break;
		default:
			free(rec);
			rec = NULL;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		/* put in the global record list */
		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;

		if (found)
			return sdrr;
	}

	return NULL;
}

/* ipmi_sdr_find_sdr_bysensortype  -  lookup SDR entry by sensor type
 *
 * @intf:	ipmi interface
 * @type:	sensor type to search for
 *
 * returns pointer to SDR list
 * returns NULL on error
 */
struct sdr_record_list *
ipmi_sdr_find_sdr_bysensortype(struct ipmi_intf *intf, uint8_t type)
{
	struct sdr_record_list *head;
	struct sdr_get_rs *header;
	struct sdr_record_list *e;

	if (sdr_list_itr == NULL) {
		sdr_list_itr = ipmi_sdr_start(intf, 0);
		if (sdr_list_itr == NULL) {
			lprintf(LOG_ERR, "Unable to open SDR for reading");
			return NULL;
		}
	}

	/* check what we've already read */
	head = malloc(sizeof (struct sdr_record_list));
	if (head == NULL) {
		lprintf(LOG_ERR, "ipmitool: malloc failure");
		return NULL;
	}
	memset(head, 0, sizeof (struct sdr_record_list));

	for (e = sdr_list_head; e != NULL; e = e->next) {
		switch (e->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			if (e->record.common->sensor.type == type)
				__sdr_list_add(head, e);
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			if (e->record.eventonly->sensor_type == type)
				__sdr_list_add(head, e);
			break;
		}
	}

	/* now keep looking */
	while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
		uint8_t *rec;
		struct sdr_record_list *sdrr;

		sdrr = malloc(sizeof (struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			break;
		}
		memset(sdrr, 0, sizeof (struct sdr_record_list));
		sdrr->id = header->id;
		sdrr->type = header->type;

		rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
		if (rec == NULL) {
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		switch (header->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sdrr->record.common =
			    (struct sdr_record_common_sensor *) rec;
			if (sdrr->record.common->sensor.type == type)
				__sdr_list_add(head, sdrr);
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			sdrr->record.eventonly =
			    (struct sdr_record_eventonly_sensor *) rec;
			if (sdrr->record.eventonly->sensor_type == type)
				__sdr_list_add(head, sdrr);
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			sdrr->record.genloc =
			    (struct sdr_record_generic_locator *) rec;
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			sdrr->record.fruloc =
			    (struct sdr_record_fru_locator *) rec;
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			sdrr->record.mcloc =
			    (struct sdr_record_mc_locator *) rec;
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			sdrr->record.entassoc =
			    (struct sdr_record_entity_assoc *) rec;
			break;
		default:
			free(rec);
			rec = NULL;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		/* put in the global record list */
		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;
	}

	return head;
}

/* ipmi_sdr_find_sdr_byentity  -  lookup SDR entry by entity association
 *
 * @intf:	ipmi interface
 * @entity:	entity id/instance to search for
 *
 * returns pointer to SDR list
 * returns NULL on error
 */
struct sdr_record_list *
ipmi_sdr_find_sdr_byentity(struct ipmi_intf *intf, struct entity_id *entity)
{
	struct sdr_get_rs *header;
	struct sdr_record_list *e;
	struct sdr_record_list *head;

	if (sdr_list_itr == NULL) {
		sdr_list_itr = ipmi_sdr_start(intf, 0);
		if (sdr_list_itr == NULL) {
			lprintf(LOG_ERR, "Unable to open SDR for reading");
			return NULL;
		}
	}

	head = malloc(sizeof (struct sdr_record_list));
	if (head == NULL) {
		lprintf(LOG_ERR, "ipmitool: malloc failure");
		return NULL;
	}
	memset(head, 0, sizeof (struct sdr_record_list));

	/* check what we've already read */
	for (e = sdr_list_head; e != NULL; e = e->next) {
		switch (e->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			if (e->record.common->entity.id == entity->id &&
			    (entity->instance == 0x7f ||
			     e->record.common->entity.instance ==
			     entity->instance))
				__sdr_list_add(head, e);
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			if (e->record.eventonly->entity.id == entity->id &&
			    (entity->instance == 0x7f ||
			     e->record.eventonly->entity.instance ==
			     entity->instance))
				__sdr_list_add(head, e);
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			if (e->record.genloc->entity.id == entity->id &&
			    (entity->instance == 0x7f ||
			     e->record.genloc->entity.instance ==
			     entity->instance))
				__sdr_list_add(head, e);
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			if (e->record.fruloc->entity.id == entity->id &&
			    (entity->instance == 0x7f ||
			     e->record.fruloc->entity.instance ==
			     entity->instance))
				__sdr_list_add(head, e);
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			if (e->record.mcloc->entity.id == entity->id &&
			    (entity->instance == 0x7f ||
			     e->record.mcloc->entity.instance ==
			     entity->instance))
				__sdr_list_add(head, e);
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			if (e->record.entassoc->entity.id == entity->id &&
			    (entity->instance == 0x7f ||
			     e->record.entassoc->entity.instance ==
			     entity->instance))
				__sdr_list_add(head, e);
			break;
		}
	}

	/* now keep looking */
	while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
		uint8_t *rec;
		struct sdr_record_list *sdrr;

		sdrr = malloc(sizeof (struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			break;
		}
		memset(sdrr, 0, sizeof (struct sdr_record_list));
		sdrr->id = header->id;
		sdrr->type = header->type;

		rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
		if (rec == NULL) {
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		switch (header->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sdrr->record.common =
			    (struct sdr_record_common_sensor *) rec;
			if (sdrr->record.common->entity.id == entity->id
			    && (entity->instance == 0x7f
				|| sdrr->record.common->entity.instance ==
				entity->instance))
				__sdr_list_add(head, sdrr);
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			sdrr->record.eventonly =
			    (struct sdr_record_eventonly_sensor *) rec;
			if (sdrr->record.eventonly->entity.id == entity->id
			    && (entity->instance == 0x7f
				|| sdrr->record.eventonly->entity.instance ==
				entity->instance))
				__sdr_list_add(head, sdrr);
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			sdrr->record.genloc =
			    (struct sdr_record_generic_locator *) rec;
			if (sdrr->record.genloc->entity.id == entity->id
			    && (entity->instance == 0x7f
				|| sdrr->record.genloc->entity.instance ==
				entity->instance))
				__sdr_list_add(head, sdrr);
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			sdrr->record.fruloc =
			    (struct sdr_record_fru_locator *) rec;
			if (sdrr->record.fruloc->entity.id == entity->id
			    && (entity->instance == 0x7f
				|| sdrr->record.fruloc->entity.instance ==
				entity->instance))
				__sdr_list_add(head, sdrr);
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			sdrr->record.mcloc =
			    (struct sdr_record_mc_locator *) rec;
			if (sdrr->record.mcloc->entity.id == entity->id
			    && (entity->instance == 0x7f
				|| sdrr->record.mcloc->entity.instance ==
				entity->instance))
				__sdr_list_add(head, sdrr);
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			sdrr->record.entassoc =
			    (struct sdr_record_entity_assoc *) rec;
			if (sdrr->record.entassoc->entity.id == entity->id
			    && (entity->instance == 0x7f
				|| sdrr->record.entassoc->entity.instance ==
				entity->instance))
				__sdr_list_add(head, sdrr);
			break;
		default:
			free(rec);
			rec = NULL;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		/* add to global record list */
		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;
	}

	return head;
}

/* ipmi_sdr_find_sdr_bytype  -  lookup SDR entries by type
 *
 * @intf:	ipmi interface
 * @type:	type of sensor record to search for
 *
 * returns pointer to SDR list with all matching entities
 * returns NULL on error
 */
struct sdr_record_list *
ipmi_sdr_find_sdr_bytype(struct ipmi_intf *intf, uint8_t type)
{
	struct sdr_get_rs *header;
	struct sdr_record_list *e;
	struct sdr_record_list *head;

	if (sdr_list_itr == NULL) {
		sdr_list_itr = ipmi_sdr_start(intf, 0);
		if (sdr_list_itr == NULL) {
			lprintf(LOG_ERR, "Unable to open SDR for reading");
			return NULL;
		}
	}

	head = malloc(sizeof (struct sdr_record_list));
	if (head == NULL) {
		lprintf(LOG_ERR, "ipmitool: malloc failure");
		return NULL;
	}
	memset(head, 0, sizeof (struct sdr_record_list));

	/* check what we've already read */
	for (e = sdr_list_head; e != NULL; e = e->next)
		if (e->type == type)
			__sdr_list_add(head, e);

	/* now keep looking */
	while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
		uint8_t *rec;
		struct sdr_record_list *sdrr;

		sdrr = malloc(sizeof (struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			break;
		}
		memset(sdrr, 0, sizeof (struct sdr_record_list));
		sdrr->id = header->id;
		sdrr->type = header->type;

		rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
		if (rec == NULL) {
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		switch (header->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sdrr->record.common =
			    (struct sdr_record_common_sensor *) rec;
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			sdrr->record.eventonly =
			    (struct sdr_record_eventonly_sensor *) rec;
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			sdrr->record.genloc =
			    (struct sdr_record_generic_locator *) rec;
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			sdrr->record.fruloc =
			    (struct sdr_record_fru_locator *) rec;
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			sdrr->record.mcloc =
			    (struct sdr_record_mc_locator *) rec;
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			sdrr->record.entassoc =
			    (struct sdr_record_entity_assoc *) rec;
			break;
		default:
			free(rec);
			rec = NULL;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		if (header->type == type)
			__sdr_list_add(head, sdrr);

		/* add to global record list */
		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;
	}

	return head;
}

/* ipmi_sdr_find_sdr_byid  -  lookup SDR entry by ID string
 *
 * @intf:	ipmi interface
 * @id:		string to match for sensor name
 *
 * returns pointer to SDR list
 * returns NULL on error
 */
struct sdr_record_list *
ipmi_sdr_find_sdr_byid(struct ipmi_intf *intf, char *id)
{
	struct sdr_get_rs *header;
	struct sdr_record_list *e;
	int found = 0;
	int idlen;

	if (id == NULL)
		return NULL;

	idlen = strlen(id);

	if (sdr_list_itr == NULL) {
		sdr_list_itr = ipmi_sdr_start(intf, 0);
		if (sdr_list_itr == NULL) {
			lprintf(LOG_ERR, "Unable to open SDR for reading");
			return NULL;
		}
	}

	/* check what we've already read */
	for (e = sdr_list_head; e != NULL; e = e->next) {
		switch (e->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
			if (!strncmp((const char *)e->record.full->id_string,
				     (const char *)id,
				     __max(e->record.full->id_code & 0x1f, idlen)))
				return e;
			break;
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			if (!strncmp((const char *)e->record.compact->id_string,
				     (const char *)id,
				     __max(e->record.compact->id_code & 0x1f, idlen)))
				return e;
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			if (!strncmp((const char *)e->record.eventonly->id_string,
				     (const char *)id,
				     __max(e->record.eventonly->id_code & 0x1f, idlen)))
				return e;
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			if (!strncmp((const char *)e->record.genloc->id_string,
				     (const char *)id,
				     __max(e->record.genloc->id_code & 0x1f, idlen)))
				return e;
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			if (!strncmp((const char *)e->record.fruloc->id_string,
				     (const char *)id,
				     __max(e->record.fruloc->id_code & 0x1f, idlen)))
				return e;
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			if (!strncmp((const char *)e->record.mcloc->id_string,
				     (const char *)id,
				     __max(e->record.mcloc->id_code & 0x1f, idlen)))
				return e;
			break;
		}
	}

	/* now keep looking */
	while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
		uint8_t *rec;
		struct sdr_record_list *sdrr;

		sdrr = malloc(sizeof (struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			break;
		}
		memset(sdrr, 0, sizeof (struct sdr_record_list));
		sdrr->id = header->id;
		sdrr->type = header->type;

		rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
		if (rec == NULL) {
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		switch (header->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
			sdrr->record.full =
			    (struct sdr_record_full_sensor *) rec;
			if (!strncmp(
			    (const char *)sdrr->record.full->id_string,
			    (const char *)id,
			    __max(sdrr->record.full->id_code & 0x1f, idlen)))
				found = 1;
			break;
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sdrr->record.compact =
			    (struct sdr_record_compact_sensor *) rec;
			if (!strncmp(
			    (const char *)sdrr->record.compact->id_string,
			    (const char *)id,
			    __max(sdrr->record.compact->id_code & 0x1f,
				   idlen)))
				found = 1;
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			sdrr->record.eventonly =
			    (struct sdr_record_eventonly_sensor *) rec;
			if (!strncmp(
			    (const char *)sdrr->record.eventonly->id_string,
			    (const char *)id,
			    __max(sdrr->record.eventonly->id_code & 0x1f,
				   idlen)))
				found = 1;
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			sdrr->record.genloc =
			    (struct sdr_record_generic_locator *) rec;
			if (!strncmp(
			    (const char *)sdrr->record.genloc->id_string,
			    (const char *)id,
			    __max(sdrr->record.genloc->id_code & 0x1f, idlen)))
				found = 1;
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			sdrr->record.fruloc =
			    (struct sdr_record_fru_locator *) rec;
			if (!strncmp(
			    (const char *)sdrr->record.fruloc->id_string,
			    (const char *)id,
			    __max(sdrr->record.fruloc->id_code & 0x1f, idlen)))
				found = 1;
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			sdrr->record.mcloc =
			    (struct sdr_record_mc_locator *) rec;
			if (!strncmp(
			    (const char *)sdrr->record.mcloc->id_string,
			    (const char *)id,
			    __max(sdrr->record.mcloc->id_code & 0x1f, idlen)))
				found = 1;
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			sdrr->record.entassoc =
			    (struct sdr_record_entity_assoc *) rec;
			break;
		default:
			free(rec);
			rec = NULL;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		/* add to global record liset */
		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;

		if (found)
			return sdrr;
	}

	return NULL;
}

/* ipmi_sdr_list_cache_fromfile  -  generate SDR cache for fast lookup from local file
 *
 * @intf:	ipmi interface
 * @ifile:	input filename
 *
 * returns pointer to SDR list
 * returns NULL on error
 */
int
ipmi_sdr_list_cache_fromfile(struct ipmi_intf *intf, const char *ifile)
{
	FILE *fp;
	struct __sdr_header {
		uint16_t id;
		uint8_t version;
		uint8_t type;
		uint8_t length;
	} header;
	struct sdr_record_list *sdrr;
	uint8_t *rec;
	int ret = 0, count = 0, bc = 0;

	if (ifile == NULL) {
		lprintf(LOG_ERR, "No SDR cache filename given");
		return -1;
	}

	fp = ipmi_open_file_read(ifile);
	if (fp == NULL) {
		lprintf(LOG_ERR, "Unable to open SDR cache %s for reading",
			ifile);
		return -1;
	}

	while (feof(fp) == 0) {
		memset(&header, 0, sizeof(header));
		bc = fread(&header, 1, 5, fp);
		if (bc <= 0)
			break;

		if (bc != 5) {
			lprintf(LOG_ERR, "header read %d bytes, expected 5",
				bc);
			ret = -1;
			break;
		}

		if (header.length == 0)
			continue;

		if (header.version != 0x51 &&
		    header.version != 0x01 &&
		    header.version != 0x02) {
			lprintf(LOG_WARN, "invalid sdr header version %02x",
				header.version);
			ret = -1;
			break;
		}

		sdrr = malloc(sizeof (struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			ret = -1;
			break;
		}
		memset(sdrr, 0, sizeof (struct sdr_record_list));

		sdrr->id = header.id;
		sdrr->type = header.type;

		rec = malloc(header.length + 1);
		if (rec == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			ret = -1;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			break;
		}
		memset(rec, 0, header.length + 1);

		bc = fread(rec, 1, header.length, fp);
		if (bc != header.length) {
			lprintf(LOG_ERR,
				"record %04x read %d bytes, expected %d",
				header.id, bc, header.length);
			ret = -1;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			if (rec != NULL) {
				free(rec);
				rec = NULL;
			}
			break;
		}

		switch (header.type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sdrr->record.common =
			    (struct sdr_record_common_sensor *) rec;
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			sdrr->record.eventonly =
			    (struct sdr_record_eventonly_sensor *) rec;
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			sdrr->record.genloc =
			    (struct sdr_record_generic_locator *) rec;
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			sdrr->record.fruloc =
			    (struct sdr_record_fru_locator *) rec;
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			sdrr->record.mcloc =
			    (struct sdr_record_mc_locator *) rec;
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			sdrr->record.entassoc =
			    (struct sdr_record_entity_assoc *) rec;
			break;
		default:
			free(rec);
			rec = NULL;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		/* add to global record liset */
		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;

		count++;

		lprintf(LOG_DEBUG, "Read record %04x from file into cache",
			sdrr->id);
	}

	if (sdr_list_itr == NULL) {
		sdr_list_itr = malloc(sizeof (struct ipmi_sdr_iterator));
		if (sdr_list_itr != NULL) {
			sdr_list_itr->reservation = 0;
			sdr_list_itr->total = count;
			sdr_list_itr->next = 0xffff;
		}
	}

	fclose(fp);
	return ret;
}

/* ipmi_sdr_list_cache  -  generate SDR cache for fast lookup
 *
 * @intf:	ipmi interface
 *
 * returns pointer to SDR list
 * returns NULL on error
 */
int
ipmi_sdr_list_cache(struct ipmi_intf *intf)
{
	struct sdr_get_rs *header;

	if (sdr_list_itr == NULL) {
		sdr_list_itr = ipmi_sdr_start(intf, 0);
		if (sdr_list_itr == NULL) {
			lprintf(LOG_ERR, "Unable to open SDR for reading");
			return -1;
		}
	}

	while ((header = ipmi_sdr_get_next_header(intf, sdr_list_itr)) != NULL) {
		uint8_t *rec;
		struct sdr_record_list *sdrr;

		sdrr = malloc(sizeof (struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			break;
		}
		memset(sdrr, 0, sizeof (struct sdr_record_list));
		sdrr->id = header->id;
		sdrr->type = header->type;

		rec = ipmi_sdr_get_record(intf, header, sdr_list_itr);
		if (rec == NULL) {
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		switch (header->type) {
		case SDR_RECORD_TYPE_FULL_SENSOR:
		case SDR_RECORD_TYPE_COMPACT_SENSOR:
			sdrr->record.common =
			    (struct sdr_record_common_sensor *) rec;
			break;
		case SDR_RECORD_TYPE_EVENTONLY_SENSOR:
			sdrr->record.eventonly =
			    (struct sdr_record_eventonly_sensor *) rec;
			break;
		case SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR:
			sdrr->record.genloc =
			    (struct sdr_record_generic_locator *) rec;
			break;
		case SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR:
			sdrr->record.fruloc =
			    (struct sdr_record_fru_locator *) rec;
			break;
		case SDR_RECORD_TYPE_MC_DEVICE_LOCATOR:
			sdrr->record.mcloc =
			    (struct sdr_record_mc_locator *) rec;
			break;
		case SDR_RECORD_TYPE_ENTITY_ASSOC:
			sdrr->record.entassoc =
			    (struct sdr_record_entity_assoc *) rec;
			break;
		default:
			free(rec);
			rec = NULL;
			if (sdrr != NULL) {
				free(sdrr);
				sdrr = NULL;
			}
			continue;
		}

		/* add to global record liset */
		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;
	}

	return 0;
}

/*
 * ipmi_sdr_get_info
 *
 * Execute the GET SDR REPOSITORY INFO command, and populate the sdr_info
 * structure.
 * See section 33.9 of the IPMI v2 specification for details
 *
 * returns 0 on success
 *         -1 on transport error
 *         > 0 for other errors
 */
int
ipmi_sdr_get_info(struct ipmi_intf *intf,
		  struct get_sdr_repository_info_rsp *sdr_repository_info)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;

	memset(&req, 0, sizeof (req));

	req.msg.netfn = IPMI_NETFN_STORAGE;	// 0x0A
	req.msg.cmd = IPMI_GET_SDR_REPOSITORY_INFO;	// 0x20
	req.msg.data = 0;
	req.msg.data_len = 0;

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "Get SDR Repository Info command failed");
		return -1;
	}
	if (rsp->ccode > 0) {
		lprintf(LOG_ERR, "Get SDR Repository Info command failed: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	}

	memcpy(sdr_repository_info,
	       rsp->data,
	       __min(sizeof (struct get_sdr_repository_info_rsp),
		     rsp->data_len));

	return 0;
}

/* ipmi_sdr_timestamp  -  return string from timestamp value
 *
 * @stamp:	32bit timestamp
 *
 * returns pointer to static buffer
 */
static char *
ipmi_sdr_timestamp(uint32_t stamp)
{
	static char tbuf[40];
	time_t s = (time_t) stamp;
	memset(tbuf, 0, 40);
	if (stamp)
		strftime(tbuf, sizeof (tbuf), "%m/%d/%Y %H:%M:%S",
			 gmtime(&s));
	return tbuf;
}

/*
 * ipmi_sdr_print_info
 *
 * Display the return data of the GET SDR REPOSITORY INFO command
 * See section 33.9 of the IPMI v2 specification for details
 *
 * returns 0 on success
 *         -1 on error
 */
int
ipmi_sdr_print_info(struct ipmi_intf *intf)
{
	uint32_t timestamp;
	uint16_t free_space;

	struct get_sdr_repository_info_rsp sdr_repository_info;

	if (ipmi_sdr_get_info(intf, &sdr_repository_info) != 0)
		return -1;

	printf("SDR Version                         : 0x%x\n",
	       sdr_repository_info.sdr_version);
	printf("Record Count                        : %d\n",
	       (sdr_repository_info.record_count_msb << 8) |
	       sdr_repository_info.record_count_lsb);

	free_space =
	    (sdr_repository_info.free_space[1] << 8) |
	    sdr_repository_info.free_space[0];

	printf("Free Space                          : ");
	switch (free_space) {
	case 0x0000:
		printf("none (full)\n");
		break;
	case 0xFFFF:
		printf("unspecified\n");
		break;
	case 0xFFFE:
		printf("> 64Kb - 2 bytes\n");
		break;
	default:
		printf("%d bytes\n", free_space);
		break;
	}

	timestamp =
	    (sdr_repository_info.most_recent_addition_timestamp[3] << 24) |
	    (sdr_repository_info.most_recent_addition_timestamp[2] << 16) |
	    (sdr_repository_info.most_recent_addition_timestamp[1] << 8) |
	    sdr_repository_info.most_recent_addition_timestamp[0];
	printf("Most recent Addition                : %s\n",
	       ipmi_sdr_timestamp(timestamp));

	timestamp =
	    (sdr_repository_info.most_recent_erase_timestamp[3] << 24) |
	    (sdr_repository_info.most_recent_erase_timestamp[2] << 16) |
	    (sdr_repository_info.most_recent_erase_timestamp[1] << 8) |
	    sdr_repository_info.most_recent_erase_timestamp[0];
	printf("Most recent Erase                   : %s\n",
	       ipmi_sdr_timestamp(timestamp));

	printf("SDR overflow                        : %s\n",
	       (sdr_repository_info.overflow_flag ? "yes" : "no"));

	printf("SDR Repository Update Support       : ");
	switch (sdr_repository_info.modal_update_support) {
	case 0:
		printf("unspecified\n");
		break;
	case 1:
		printf("non-modal\n");
		break;
	case 2:
		printf("modal\n");
		break;
	case 3:
		printf("modal and non-modal\n");
		break;
	default:
		printf("error in response\n");
		break;
	}

	printf("Delete SDR supported                : %s\n",
	       sdr_repository_info.delete_sdr_supported ? "yes" : "no");
	printf("Partial Add SDR supported           : %s\n",
	       sdr_repository_info.partial_add_sdr_supported ? "yes" : "no");
	printf("Reserve SDR repository supported    : %s\n",
	       sdr_repository_info.
	       reserve_sdr_repository_supported ? "yes" : "no");
	printf("SDR Repository Alloc info supported : %s\n",
	       sdr_repository_info.
				 get_sdr_repository_allo_info_supported ? "yes" : "no");

	return 0;
}

/* ipmi_sdr_dump_bin  -  Write raw SDR to binary file
 *
 * used for post-processing by other utilities
 *
 * @intf:	ipmi interface
 * @ofile:	output filename
 *
 * returns 0 on success
 * returns -1 on error
 */
static int
ipmi_sdr_dump_bin(struct ipmi_intf *intf, const char *ofile)
{
	struct sdr_get_rs *header;
	struct ipmi_sdr_iterator *itr;
	struct sdr_record_list *sdrr;
	FILE *fp;
	int rc = 0;

	/* open connection to SDR */
	itr = ipmi_sdr_start(intf, 0);
	if (itr == NULL) {
		lprintf(LOG_ERR, "Unable to open SDR for reading");
		return -1;
	}

	printf("Dumping Sensor Data Repository to '%s'\n", ofile);

	/* generate list of records */
	while ((header = ipmi_sdr_get_next_header(intf, itr)) != NULL) {
		sdrr = malloc(sizeof(struct sdr_record_list));
		if (sdrr == NULL) {
			lprintf(LOG_ERR, "ipmitool: malloc failure");
			return -1;
		}
		memset(sdrr, 0, sizeof(struct sdr_record_list));

		lprintf(LOG_INFO, "Record ID %04x (%d bytes)",
			header->id, header->length);

		sdrr->id = header->id;
		sdrr->version = header->version;
		sdrr->type = header->type;
		sdrr->length = header->length;
		sdrr->raw = ipmi_sdr_get_record(intf, header, itr);

		if (sdrr->raw == NULL) {
		    lprintf(LOG_ERR, "ipmitool: cannot obtain SDR record %04x", header->id);
				if (sdrr != NULL) {
					free(sdrr);
					sdrr = NULL;
				}
		    return -1;
		}

		if (sdr_list_head == NULL)
			sdr_list_head = sdrr;
		else
			sdr_list_tail->next = sdrr;

		sdr_list_tail = sdrr;
	}

	ipmi_sdr_end(intf, itr);

	/* now write to file */
	fp = ipmi_open_file_write(ofile);
	if (fp == NULL)
		return -1;

	for (sdrr = sdr_list_head; sdrr != NULL; sdrr = sdrr->next) {
		int r;
		uint8_t h[5];

		/* build and write sdr header */
		h[0] = sdrr->id & 0xff;   // LS Byte first
		h[1] = (sdrr->id >> 8) & 0xff;
		h[2] = sdrr->version;
		h[3] = sdrr->type;
		h[4] = sdrr->length;

		r = fwrite(h, 1, 5, fp);
		if (r != 5) {
			lprintf(LOG_ERR, "Error writing header "
				"to output file %s", ofile);
			rc = -1;
			break;
		}

		/* write sdr entry */
		if (!sdrr->raw) {
			lprintf(LOG_ERR, "Error: raw data is null (length=%d)",
								sdrr->length);
			rc = -1;
			break;
		}
		r = fwrite(sdrr->raw, 1, sdrr->length, fp);
		if (r != sdrr->length) {
			lprintf(LOG_ERR, "Error writing %d record bytes "
				"to output file %s", sdrr->length, ofile);
			rc = -1;
			break;
		}
	}
	fclose(fp);

	return rc;
}

/* ipmi_sdr_print_type  -  print all sensors of specified type
 *
 * @intf:	ipmi interface
 * @type:	sensor type
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_type(struct ipmi_intf *intf, char *type)
{
	struct sdr_record_list *list, *entry;
	int rc = 0;
	int x;
	uint8_t sensor_type = 0;

	if (type == NULL ||
	    strncasecmp(type, "help", 4) == 0 ||
	    strncasecmp(type, "list", 4) == 0) {
		printf("Sensor Types:\n");
		for (x = 1; x < SENSOR_TYPE_MAX; x += 2) {
			printf("\t%-25s (0x%02x)   %-25s (0x%02x)\n",
				sensor_type_desc[x], x,
				sensor_type_desc[x + 1], x + 1);
		}
		return 0;
	}

	if (strncmp(type, "0x", 2) == 0) {
		/* begins with 0x so let it be entered as raw hex value */
		if (str2uchar(type, &sensor_type) != 0) {
			lprintf(LOG_ERR,
					"Given type of sensor \"%s\" is either invalid or out of range.",
					type);
			return (-1);
		}
	} else {
		for (x = 1; x < SENSOR_TYPE_MAX; x++) {
			if (strncasecmp(sensor_type_desc[x], type,
					__maxlen(type,
						 sensor_type_desc[x])) == 0) {
				sensor_type = x;
				break;
			}
		}
		if (sensor_type != x) {
			lprintf(LOG_ERR, "Sensor Type \"%s\" not found.",
				type);
			printf("Sensor Types:\n");
			for (x = 1; x < SENSOR_TYPE_MAX; x += 2) {
				printf("\t%-25s (0x%02x)   %-25s (0x%02x)\n",
					sensor_type_desc[x], x,
					sensor_type_desc[x + 1], x + 1);
			}
			return 0;
		}
	}

	list = ipmi_sdr_find_sdr_bysensortype(intf, sensor_type);

	for (entry = list; entry != NULL; entry = entry->next) {
		rc = ipmi_sdr_print_listentry(intf, entry);
	}

	__sdr_list_empty(list);

	return rc;
}

/* ipmi_sdr_print_entity  -  print entity's for an id/instance
 *
 * @intf:	ipmi interface
 * @entitystr:	entity id/instance string, i.e. "1.1"
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_print_entity(struct ipmi_intf *intf, char *entitystr)
{
	struct sdr_record_list *list, *entry;
	struct entity_id entity;
	unsigned id = 0;
	unsigned instance = 0;
	int rc = 0;

	if (entitystr == NULL ||
	    strncasecmp(entitystr, "help", 4) == 0 ||
	    strncasecmp(entitystr, "list", 4) == 0) {
		print_valstr_2col(entity_id_vals, "Entity IDs", -1);
		return 0;
	}

	if (sscanf(entitystr, "%u.%u", &id, &instance) != 2) {
		/* perhaps no instance was passed
		 * in which case we want all instances for this entity
		 * so set entity.instance = 0x7f to indicate this
		 */
		if (sscanf(entitystr, "%u", &id) != 1) {
			int i, j=0;

			/* now try string input */
			for (i = 0; entity_id_vals[i].str != NULL; i++) {
				if (strncasecmp(entitystr, entity_id_vals[i].str,
						__maxlen(entitystr, entity_id_vals[i].str)) == 0) {
					entity.id = entity_id_vals[i].val;
					entity.instance = 0x7f;
					j=1;
				}
			}
			if (j == 0) {
				lprintf(LOG_ERR, "Invalid entity: %s", entitystr);
				return -1;
			}
		} else {
			entity.id = id;
			entity.instance = 0x7f;
		}
	} else {
		entity.id = id;
		entity.instance = instance;
	}

	list = ipmi_sdr_find_sdr_byentity(intf, &entity);

	for (entry = list; entry != NULL; entry = entry->next) {
		rc = ipmi_sdr_print_listentry(intf, entry);
	}

	__sdr_list_empty(list);

	return rc;
}

/* ipmi_sdr_print_entry_byid  -  print sdr entries identified by sensor id
 *
 * @intf:	ipmi interface
 * @argc:	number of entries to print
 * @argv:	list of sensor ids
 *
 * returns 0 on success
 * returns -1 on error
 */
static int
ipmi_sdr_print_entry_byid(struct ipmi_intf *intf, int argc, char **argv)
{
	struct sdr_record_list *sdr;
	int rc = 0;
	int v, i;

	if (argc < 1) {
		lprintf(LOG_ERR, "No Sensor ID supplied");
		return -1;
	}

	v = verbose;
	verbose = 1;

	for (i = 0; i < argc; i++) {
		sdr = ipmi_sdr_find_sdr_byid(intf, argv[i]);
		if (sdr == NULL) {
			lprintf(LOG_ERR, "Unable to find sensor id '%s'",
				argv[i]);
		} else {
			if (ipmi_sdr_print_listentry(intf, sdr) < 0)
				rc = -1;
		}
	}

	verbose = v;

	return rc;
}

/* ipmi_sdr_main  -  top-level handler for SDR subsystem
 *
 * @intf:	ipmi interface
 * @argc:	number of arguments
 * @argv:	argument list
 *
 * returns 0 on success
 * returns -1 on error
 */
int
ipmi_sdr_main(struct ipmi_intf *intf, int argc, char **argv)
{
	int rc = 0;

	/* initialize random numbers used later */
	srand(time(NULL));

	if (argc == 0)
		return ipmi_sdr_print_sdr(intf, 0xfe);
	else if (strncmp(argv[0], "help", 4) == 0) {
		printf_sdr_usage();
	} else if (strncmp(argv[0], "list", 4) == 0
		   || strncmp(argv[0], "elist", 5) == 0) {

		if (strncmp(argv[0], "elist", 5) == 0)
			sdr_extended = 1;
		else
			sdr_extended = 0;

		if (argc <= 1)
			rc = ipmi_sdr_print_sdr(intf, 0xfe);
		else if (strncmp(argv[1], "all", 3) == 0)
			rc = ipmi_sdr_print_sdr(intf, 0xff);
		else if (strncmp(argv[1], "full", 4) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_FULL_SENSOR);
		else if (strncmp(argv[1], "compact", 7) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_COMPACT_SENSOR);
		else if (strncmp(argv[1], "event", 5) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_EVENTONLY_SENSOR);
		else if (strncmp(argv[1], "mcloc", 5) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_MC_DEVICE_LOCATOR);
		else if (strncmp(argv[1], "fru", 3) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR);
		else if (strncmp(argv[1], "generic", 7) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR);
		else if (strcmp(argv[1], "help") == 0) {
			lprintf(LOG_NOTICE,
				"usage: sdr %s [all|full|compact|event|mcloc|fru|generic]",
				argv[0]);
			return 0;
		}
		else {
			lprintf(LOG_ERR,
				"Invalid SDR %s command: %s",
				argv[0], argv[1]);
			lprintf(LOG_NOTICE,
				"usage: sdr %s [all|full|compact|event|mcloc|fru|generic]",
				argv[0]);
			return (-1);
		}
	} else if (strncmp(argv[0], "type", 4) == 0) {
		sdr_extended = 1;
		rc = ipmi_sdr_print_type(intf, argv[1]);
	} else if (strncmp(argv[0], "entity", 6) == 0) {
		sdr_extended = 1;
		rc = ipmi_sdr_print_entity(intf, argv[1]);
	} else if (strncmp(argv[0], "info", 4) == 0) {
		rc = ipmi_sdr_print_info(intf);
	} else if (strncmp(argv[0], "get", 3) == 0) {
		rc = ipmi_sdr_print_entry_byid(intf, argc - 1, &argv[1]);
	} else if (strncmp(argv[0], "dump", 4) == 0) {
		if (argc < 2) {
			lprintf(LOG_ERR, "Not enough parameters given.");
			lprintf(LOG_NOTICE, "usage: sdr dump <file>");
			return (-1);
		}
		rc = ipmi_sdr_dump_bin(intf, argv[1]);
	} else if (strncmp(argv[0], "fill", 4) == 0) {
		if (argc <= 1) {
			lprintf(LOG_ERR, "Not enough parameters given.");
			lprintf(LOG_NOTICE, "usage: sdr fill sensors");
			lprintf(LOG_NOTICE, "usage: sdr fill file <file>");
			lprintf(LOG_NOTICE, "usage: sdr fill range <range>");
			return (-1);
		} else if (strncmp(argv[1], "sensors", 7) == 0) {
			rc = ipmi_sdr_add_from_sensors(intf, 21);
		} else if (strncmp(argv[1], "nosat", 5) == 0) {
			rc = ipmi_sdr_add_from_sensors(intf, 0);
		} else if (strncmp(argv[1], "file", 4) == 0) {
			if (argc < 3) {
				lprintf(LOG_ERR,
					"Not enough parameters given.");
				lprintf(LOG_NOTICE,
					"usage: sdr fill file <file>");
				return (-1);
			}
			rc = ipmi_sdr_add_from_file(intf, argv[2]);
		} else if (strncmp(argv[1], "range", 4) == 0) {
			if (argc < 3) {
				lprintf(LOG_ERR,
					"Not enough parameters given.");
				lprintf(LOG_NOTICE,
					"usage: sdr fill range <range>");
				return (-1);
			}
			rc = ipmi_sdr_add_from_list(intf, argv[2]);
		} else {
		    lprintf(LOG_ERR,
			    "Invalid SDR %s command: %s",
			    argv[0], argv[1]);
		    lprintf(LOG_NOTICE,
			    "usage: sdr %s <sensors|nosat|file|range> [options]",
			    argv[0]);
		    return (-1);
		}
	} else {
		lprintf(LOG_ERR, "Invalid SDR command: %s", argv[0]);
		rc = -1;
	}

	return rc;
}

void
printf_sdr_usage()
{
	lprintf(LOG_NOTICE,
"usage: sdr <command> [options]");
	lprintf(LOG_NOTICE,
"               list | elist [option]");
	lprintf(LOG_NOTICE,
"                     all           All SDR Records");
	lprintf(LOG_NOTICE,
"                     full          Full Sensor Record");
	lprintf(LOG_NOTICE,
"                     compact       Compact Sensor Record");
	lprintf(LOG_NOTICE,
"                     event         Event-Only Sensor Record");
	lprintf(LOG_NOTICE,
"                     mcloc         Management Controller Locator Record");
	lprintf(LOG_NOTICE,
"                     fru           FRU Locator Record");
	lprintf(LOG_NOTICE,
"                     generic       Generic Device Locator Record\n");
	lprintf(LOG_NOTICE,
"               type [option]");
	lprintf(LOG_NOTICE,
"                     <Sensor_Type> Retrieve the state of specified sensor.");
	lprintf(LOG_NOTICE,
"                                   Sensor_Type can be specified either as");
	lprintf(LOG_NOTICE,
"                                   a string or a hex value.");
	lprintf(LOG_NOTICE,
"                     list          Get a list of available sensor types\n");
	lprintf(LOG_NOTICE,
"               get <Sensor_ID>");
	lprintf(LOG_NOTICE,
"                     Retrieve state of the first sensor matched by Sensor_ID\n");
	lprintf(LOG_NOTICE,
"               info");
	lprintf(LOG_NOTICE,
"                     Display information about the repository itself\n");
	lprintf(LOG_NOTICE,
"               entity <Entity_ID>[.<Instance_ID>]");
	lprintf(LOG_NOTICE,
"                     Display all sensors associated with an entity\n");
	lprintf(LOG_NOTICE,
"               dump <file>");
	lprintf(LOG_NOTICE,
"                     Dump raw SDR data to a file\n");
	lprintf(LOG_NOTICE,
"               fill <option>");
	lprintf(LOG_NOTICE,
"                     sensors       Creates the SDR repository for the current");
	lprintf(LOG_NOTICE,
"                                   configuration");
	lprintf(LOG_NOTICE,
"                     nosat         Creates the SDR repository for the current");
	lprintf(LOG_NOTICE,
"                                   configuration, without satellite scan");
	lprintf(LOG_NOTICE,
"                     file <file>   Load SDR repository from a file");
	lprintf(LOG_NOTICE,
"                     range <range> Load SDR repository from a provided list");
	lprintf(LOG_NOTICE,
"                                   or range. Use ',' for list or '-' for");
	lprintf(LOG_NOTICE,
"                                   range, eg. 0x28,0x32,0x40-0x44");
}