summaryrefslogtreecommitdiffstats
path: root/src/usr/isteps/nvdimm/bpm_update.C
blob: 3ffdb595ba7c5542da56039188c9e07580720e4e (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/isteps/nvdimm/bpm_update.C $                          */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2019                             */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

#include "nvdimm.H"
#include "bpm_update.H"
#include "nvdimm_update.H"

#include <isteps/nvdimm/nvdimm.H>
#include <errl/hberrltypes.H>
#include <errl/errlmanager.H>
#include <endian.h>
#include <sys/time.h>
#include <hbotcompid.H>
#include <trace/interface.H>
#include <initservice/istepdispatcherif.H>
#include <isteps/nvdimm/bpmreasoncodes.H>

#include <hwas/common/hwasCallout.H>

#include <targeting/common/targetservice.H>
#include <attributeenums.H>

namespace NVDIMM
{
namespace BPM
{

trace_desc_t* g_trac_bpm = nullptr;
TRAC_INIT(&g_trac_bpm, BPM_COMP_NAME, 4*KILOBYTE);

// For debug traces
#define TRACUCOMP(args...)
//#define TRACUCOMP(args...) TRACFCOMP(args)
#define TRACUBIN(args...)
//#define TRACUBIN(args...) TRACUBIN(args)

// These constants are kept out of the header file since they aren't relevant
// outside of this file.
const uint16_t BPM_ADDRESS_ZERO = 0;
const uint16_t BPM_CONFIG_START_ADDRESS = 0x1800;
// There are two potential start addresses for the firmware section.
// They are:
const uint16_t MAIN_PROGRAM_ADDRESS     = 0x8000;
const uint16_t MAIN_PROGRAM_ADDRESS_ALT = 0xA000;

// In order to disable write protection on the BPM to perform updates a sequence
// of characters must be written. The hex represenation of those characters are
// defined by this constant. The sequence is SMOD
const uint8_t BPM_PASSWORD[] = {0x53, 0x4D, 0x4F, 0x44};
const size_t BPM_PASSWORD_LENGTH = 4;

// These are the segment codes used to dump out a particular config data segment
// on the BPM.
const uint16_t DEFAULT_REG_PAGE     = 0x905E;
const uint16_t SEGMENT_A_CODE       = 0x9A5E;
const uint16_t SEGMENT_B_CODE       = 0x9B5E;
const uint16_t SEGMENT_C_CODE       = 0x9C5E;
const uint16_t SEGMENT_D_CODE       = 0x9D5E;

// Starting addresses relative to address 0x1800.
// Segments appear in reverse order on BPM.
// Each segment is SEGMENT_SIZE long.
const size_t SEGMENT_D_START_ADDR = 0x000;
const size_t SEGMENT_C_START_ADDR = 0x080;
const size_t SEGMENT_B_START_ADDR = 0x100;
const size_t SEGMENT_A_START_ADDR = 0x180;

const std::map<uint16_t, size_t> segmentMap
{
    {SEGMENT_A_CODE, SEGMENT_A_START_ADDR},
    {SEGMENT_B_CODE, SEGMENT_B_START_ADDR},
    {SEGMENT_C_CODE, SEGMENT_C_START_ADDR},
    {SEGMENT_D_CODE, SEGMENT_D_START_ADDR},
};

const uint8_t MAX_RETRY = 3;

/**
 * @brief A helper function used in assert statements to verify the correct
 *        BSP commands were passed into the correct function arguments.
 *
 * @param[in]   i_command    The command that will verified to be a BSP command.
 *
 * @return      bool         true if i_command is a BSP command.
 *                           false if it's not a BSP command.
 */
bool isBspCommand(const uint8_t i_command)
{
    bool result = ((i_command == BPM_PASSTHROUGH) || (i_command == BPM_LOCAL))
                  ? true : false;

    return result;
}

/**
 * @brief A helper function used in assert statements to verify the correct
 *        BCL commands were passed into the correct function arguments.
 *
 * @param[in]   i_command    The command that will verified to be a BCL command.
 *
 * @return      bool         true if i_command is a BCL command.
 *                           false if it's not a BCL command.
 */
bool isBclCommand(const uint8_t i_command)
{
    bool result = false;
    switch(i_command)
    {
        case BCL_ENTER_BSL_MODE:
        case BCL_IS_BSL_MODE:
        case BCL_WRITE_REG:
        case BCL_START_UPDATE:
        case BCL_END_UPDATE:
        case BCL_IS_UPDATE_IN_PROGRESS:
        {
            result = true;
            break;
        }
        default:
        {
            result = false;
            break;
        }
    }

    return result;
}

/**
 * @brief A helper function used in assert statements to verify the correct
 *        BSL commands were passed into the correct function arguments.
 *
 * @param[in]   i_command    The command that will verified to be a BSL command.
 *
 * @return      bool         true if i_command is a BSL command.
 *                           false if it's not a BSL command.
 */
bool isBslCommand(const uint8_t i_command)
{
    bool result = false;
    switch(i_command)
    {
        case BSL_RX_DATA_BLOCK:
        case BSL_RX_PASSWORD:
        case BSL_ERASE_SEGMENT:
        case BSL_TOGGLE_INFO:
        case BSL_ERASE_BLOCK:
        case BSL_MASS_ERASE:
        case BSL_CRC_CHECK:
        case BSL_LOAD_PC:
        case BSL_TX_DATA_BLOCK:
        case BSL_TX_BSL_VERSION:
        case BSL_TX_BUFFER_SIZE:
        case BSL_RX_DATA_BLOCK_FAST:
        case BSL_RESET_DEVICE:
        case BSL_VERIFY_BLOCK:
        {
            result = true;
            break;
        }
        default:
        {
            result = false;
            break;
        }
    }

    return result;
}

/**
 * @brief Helper function to pull out the BPM address offset in the given
 *        payload.
 *
 * @param[in]   i_payload       The payload from which to extract the address
 *                              offset.
 */
uint16_t getPayloadAddressBE(payload_t i_payload)
{
    // Get the payload address and convert back to big endian.
    uint16_t payloadAddress = (i_payload[PAYLOAD_ADDRESS_START_INDEX])
                            | (i_payload[PAYLOAD_ADDRESS_START_INDEX + 1] << 8);
    return payloadAddress;
}

/**
 *  @brief  Helper function to extract the Segement ID from the segment code.
 *
 *  @param[in]  i_segmentCode       The Segment code to pull the segment ID from
 *
 *  @return     uint8_t             The Segment ID (A, B, C, D) as a hex value.
 *                                  For example 0xA, 0xB, etc.
 */
uint8_t getSegmentIdentifier(uint16_t i_segmentCode)
{
    uint8_t segmentId = (i_segmentCode >> 8) & 0xF;
    return segmentId;
}

/**
 *  @brief Helper function to sleep for longer durations in 5 second increments.
 *
 *  @param[in]      i_sleepInSeconds    How many seconds to sleep.
 */
void longSleep(uint8_t const i_sleepInSeconds)
{
    int iterations = i_sleepInSeconds / 5;
    do
    {
        // Send progress code.
        INITSERVICE::sendProgressCode();

        // Sleep for 5 seconds
        nanosleep(5, 0);

        --iterations;
    } while (iterations > 0);
}

void runBpmUpdates(bpmList_t           * const i_16gb_BPMs,
                   bpmList_t           * const i_32gb_BPMs,
                   BpmFirmwareLidImage * const i_16gb_fwImage,
                   BpmFirmwareLidImage * const i_32gb_fwImage,
                   BpmConfigLidImage   * const i_16gb_configImage,
                   BpmConfigLidImage   * const i_32gb_configImage)
{

    assert(   (i_16gb_BPMs == nullptr)
           ||  i_16gb_BPMs->empty()
           || ((i_16gb_fwImage != nullptr) && (i_16gb_configImage != nullptr)),
           "BPM::runBpmUpdates(): Update images for 16gb BPMs was nullptr and "
           "there are 16gb BPMs in the system to may require updates.");
    assert(   (i_32gb_BPMs == nullptr)
           ||  i_32gb_BPMs->empty()
           || ((i_32gb_fwImage != nullptr) && (i_32gb_configImage != nullptr)),
           "BPM::runBpmUpdates(): Update images for 32gb BPMs was nullptr and "
           "there are 32gb BPMs in the system to may require updates.");

    errlHndl_t errl = nullptr;

    do {

        if (   (i_16gb_BPMs != nullptr)
            && (i_16gb_fwImage != nullptr)
            && (i_16gb_configImage != nullptr))
        {
            TRACFCOMP(g_trac_bpm,
                     "Check/update %d BPMs on 16GB_TYPE NVDIMMs",
                     i_16gb_BPMs->size());

            for(auto& bpm : *i_16gb_BPMs)
            {
                errl = bpm.runUpdate(*i_16gb_fwImage, *i_16gb_configImage);
                if (errl != nullptr)
                {
                    uint32_t nvdimmHuid = TARGETING::get_huid(bpm.getNvdimm());
                    if (bpm.attemptAnotherUpdate())
                    {
                        TRACFCOMP(g_trac_bpm, ERR_MRK
                                 "An error occurred during a 16GB_TYPE BPM "
                                 "update for NVDIMM 0x%.8X. "
                                 "Try again.",
                                 nvdimmHuid);

                        delete errl;
                        errl = bpm.runUpdate(*i_16gb_fwImage,
                                             *i_16gb_configImage);
                        if (errl != nullptr)
                        {
                            TRACFCOMP(g_trac_bpm, ERR_MRK
                                     "Another error occurred while attempting "
                                     "to update the same 16GB_TYPE BPM for "
                                     "NVDIMM 0x%.8X. Commit and move onto the "
                                     "next BPM",
                                     nvdimmHuid);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        TRACFCOMP(g_trac_bpm, ERR_MRK
                                 "An error occurred during a 16GB_TYPE BPM "
                                 "update for NVDIMM 0x%.8X. "
                                 "Commit and move onto the next BPM",
                                 nvdimmHuid);
                    }
                    ERRORLOG::errlCommit(errl, BPM_COMP_ID);
                }
            }
        }

        if (  (i_32gb_BPMs != nullptr)
           && (i_32gb_fwImage != nullptr)
           && (i_32gb_configImage != nullptr))
        {
            TRACFCOMP(g_trac_bpm,
                     "Check/update %d BPMs on 32GB_TYPE NVDIMMs",
                     i_32gb_BPMs->size());

            for(auto& bpm : *i_32gb_BPMs)
            {
                errl = bpm.runUpdate(*i_32gb_fwImage, *i_32gb_configImage);
                if (errl != nullptr)
                {
                    uint32_t nvdimmHuid = TARGETING::get_huid(bpm.getNvdimm());
                    if (bpm.attemptAnotherUpdate())
                    {
                        TRACFCOMP(g_trac_bpm, ERR_MRK
                                 "An error occurred during a 32GB_TYPE BPM "
                                 "update for NVDIMM 0x%.8X. "
                                 "Try again.",
                                 nvdimmHuid);

                        delete errl;
                        errl = bpm.runUpdate(*i_32gb_fwImage,
                                             *i_32gb_configImage);
                        if (errl != nullptr)
                        {
                            TRACFCOMP(g_trac_bpm, ERR_MRK
                                     "Another error occurred while attempting "
                                     "to update the same 32GB_TYPE BPM for "
                                     "NVDIMM 0x%.8X. Commit and move onto the "
                                     "next BPM",
                                     nvdimmHuid);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        TRACFCOMP(g_trac_bpm, ERR_MRK
                                 "An error occurred during a 32GB_TYPE BPM "
                                 "update for NVDIMM 0x%.8X. "
                                 "Commit and move onto the next BPM",
                                 nvdimmHuid);
                    }
                    ERRORLOG::errlCommit(errl, BPM_COMP_ID);
                }
            }
        }
    } while(0);
}

// =============================================================================
//                      BpmFirmwareLidImage Class Functions
// =============================================================================

BpmFirmwareLidImage::BpmFirmwareLidImage(void * const i_lidImageAddr,
                                         size_t i_size)
    : iv_lidImage(i_lidImageAddr), iv_lidImageSize(i_size)
{
    assert(i_lidImageAddr != nullptr,
          "BPM::BpmFirmwareLidImage(): Provided LID image must not be nullptr");
}

uint16_t BpmFirmwareLidImage::getVersion() const
{
    uint16_t version = INVALID_VERSION;

    if (iv_lidImageSize >= sizeof(firmware_image_header_t))
    {
        const firmware_image_header_t * header =
            reinterpret_cast<const firmware_image_header_t*>(iv_lidImage);

        version = TWO_UINT8_TO_UINT16(header->iv_versionMajor,
                                      header->iv_versionMinor);
    }

    return version;
}

uint16_t BpmFirmwareLidImage::getNumberOfBlocks() const
{
    uint16_t numberOfBlocks = 0;

    if (iv_lidImageSize >= sizeof(firmware_image_header_t))
    {
        const firmware_image_header_t * header =
            reinterpret_cast<const firmware_image_header_t*>(iv_lidImage);

        numberOfBlocks = header->iv_numberOfBlocks;
    }

    return numberOfBlocks;
}

void const * BpmFirmwareLidImage::getFirstBlock() const
{
    void * block = nullptr;

    if (getNumberOfBlocks() > 0)
    {
        block = reinterpret_cast<uint8_t* const>(iv_lidImage)
                + sizeof(firmware_image_header_t);
    }

    return block;
}

// =============================================================================
//                      BpmConfigLidImage Class Functions
// =============================================================================

BpmConfigLidImage::BpmConfigLidImage(void * const i_lidImageAddr,
                                     size_t       i_size)
    : iv_lidImage(i_lidImageAddr), iv_lidImageSize(i_size)
{
    assert(i_lidImageAddr != nullptr,
          "BPM::BpmConfigLidImage(): Provided LID image must not be nullptr");
}

uint16_t BpmConfigLidImage::getVersion() const
{
    uint16_t version = INVALID_VERSION;

    if (iv_lidImageSize >= sizeof(config_image_header_t))
    {
        const config_image_header_t * header =
            reinterpret_cast<const config_image_header_t*>(iv_lidImage);

        version = TWO_UINT8_TO_UINT16(header->iv_versionMajor,
                                      header->iv_versionMinor);
    }

    return version;
}

uint16_t BpmConfigLidImage::getNumberOfFragments() const
{
    uint16_t numberOfFragments = 0;

    if (iv_lidImageSize >= sizeof(config_image_header_t))
    {
        const config_image_header_t * header =
            reinterpret_cast<const config_image_header_t*>(iv_lidImage);

        numberOfFragments = header->iv_numberOfFragments;
    }

    return numberOfFragments;
}

void const * BpmConfigLidImage::getFirstFragment() const
{
    void * fragment = nullptr;

    if (getNumberOfFragments() > 0)
    {
        fragment = reinterpret_cast<uint8_t* const>(iv_lidImage)
                 + sizeof(config_image_header_t);
    }

    return fragment;
}

// =============================================================================
//                           Bpm Class Functions
// =============================================================================

Bpm::Bpm(const TARGETING::TargetHandle_t i_nvdimm)
    : iv_nvdimm(i_nvdimm),
      iv_bslVersion(0),
      iv_firmwareStartAddress(0),
      iv_attemptAnotherUpdate(false),
      iv_segmentDMerged(false),
      iv_segmentBMerged(false),
      iv_updateAttempted(false)
{
    assert((i_nvdimm != nullptr) && (isNVDIMM(i_nvdimm)),
          "BPM::Bpm(): An nvdimm target must be given.");

    memset(&iv_segmentD, 0, SEGMENT_SIZE);
    memset(&iv_segmentB, 0, SEGMENT_SIZE);

}

bool Bpm::attemptAnotherUpdate()
{
    return iv_attemptAnotherUpdate;
}

bool Bpm::hasAttemptedUpdate()
{
    return iv_updateAttempted;
}

void Bpm::setAttemptAnotherUpdate()
{

    if (iv_updateAttempted)
    {
        // Since iv_updateAttempted is true that means that this function was
        // called on a subsequent update attempt, meaning we should no longer
        // attempt updates if the current attempt fails.
        iv_attemptAnotherUpdate = false;
    }
    else
    {
        // Since iv_updateAttempted is false that means that this function was
        // called on the first update attempt because by default
        // iv_updateAttempted is false and is only set to true as the last part
        // of the update procedure.
        iv_attemptAnotherUpdate = true;
    }

}

const TARGETING::TargetHandle_t Bpm::getNvdimm()
{
    return iv_nvdimm;
}

errlHndl_t Bpm::readBslVersion()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::readBslVersion()");
    errlHndl_t errl = nullptr;

    do {
        // Enter Update mode
        errl = enterUpdateMode();
        if (errl != nullptr)
        {
            break;
        }

        // Verify in Update mode
        errl = inUpdateMode();
        if (errl != nullptr)
        {
            break;
        }

        // Enter Bootstrap Loader (BSL) mode
        errl = enterBootstrapLoaderMode();
        if (errl != nullptr)
        {
            break;
        }

        // Unlock the device. This is a BSL command so we must already be in
        // BSL mode to execute it.
        errl = unlockDevice();
        if (errl != nullptr)
        {
            break;
        }

        // Command to get the version is a BSL command, so it has to be sent as
        // a payload.
        payload_t payload;
        errl = setupPayload(payload, BSL_TX_BSL_VERSION, BPM_ADDRESS_ZERO);
        if (errl != nullptr)
        {
            break;
        }

        // Issue the BSL command
        errl = issueCommand(BPM_PASSTHROUGH,
                            payload,
                            WRITE,
                            NO_DELAY_EXTERNAL_RESPONSE);
        if (errl != nullptr)
        {
            break;
        }

        // Get the result from the BPM.
        errl = getResponse(&iv_bslVersion, sizeof(uint8_t));
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::readBslVersion(): "
                     "Failed to determine BSL Version.");
            break;
        }

        TRACFCOMP(g_trac_bpm, "Bpm::readBslVersion(): BSL Version is 0x%X",
                  iv_bslVersion);
    } while(0);

    // Reset the device. This will exit BSL mode.
    errlHndl_t exitErrl = resetDevice();
    if (exitErrl != nullptr)
    {
        handleMultipleErrors(errl, exitErrl);
    }

    // Exit update mode
    exitErrl = exitUpdateMode();
    if (exitErrl != nullptr)
    {
        handleMultipleErrors(errl, exitErrl);
    }

    return errl;
}

errlHndl_t Bpm::getFwVersion(uint16_t & o_fwVersion) const
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::getFwVersion()");
    errlHndl_t errl = nullptr;

    do {
        uint8_t bpmMajor = 0, bpmMinor = 0;
        errl = nvdimmReadReg(iv_nvdimm,
                             ES_FWREV1,
                             bpmMajor);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::getFwVersion(): "
                      "Failed to read BPM major version byte");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        errl = nvdimmReadReg(iv_nvdimm,
                             ES_FWREV0,
                             bpmMinor);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::getFwVersion(): "
                      "Failed to read BPM minor version byte");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        o_fwVersion = TWO_UINT8_TO_UINT16(bpmMajor, bpmMinor);

    } while(0);

    return errl;
}

errlHndl_t Bpm::issueCommand(const uint8_t i_bspCommand,
                             const uint8_t i_command,
                             const uint8_t i_opType,
                             const int     i_msDelay)
{
    assert(isBspCommand(i_bspCommand),
           "i_bspCommand must be a valid BSP command");
    assert(isBclCommand(i_command),
           "i_command must be a valid BCL command");
    // i_opType gets set in the BPM_CMD_STATUS register where it is only given
    // two bits. So any value above 3 is not valid.
    assert(i_opType <= 3, "i_opType can only range between 0 and 3");

    errlHndl_t errl = nullptr;

    // i_command must be sent in BPM_REG_PAYLOAD_START, but it doesn't need to
    // be formatted into a typical payload since the command isn't a BSL
    // command. So, just create a payload_t, push_back the command, and let the
    // issueCommand function that takes a payload_t parameter handle the rest.
    payload_t payloadCommand;
    payloadCommand.push_back(i_command);

    errl = issueCommand(i_bspCommand, payloadCommand, i_opType, i_msDelay);

    return errl;
}

errlHndl_t Bpm::issueCommand(const uint8_t i_command,
                             payload_t     i_payload,
                             const uint8_t i_opType,
                             const int     i_msDelay)
{
    assert(isBspCommand(i_command),
           "i_bspCommand must be a valid BSP command");

    // i_opType gets set in the BPM_CMD_STATUS register where it is only given
    // two bits. So any value above 3 is not valid.
    assert(i_opType <= 3, "i_opType can only range between 0 and 3");

    errlHndl_t errl = nullptr;

    do {

        // Check the full payload size to make sure it's not too large. Add the
        // size of the SYNC_BYTE that was dropped during payload creation to
        // verify that the full payload sent by the NVDIMM won't exceed the max
        // size the BPM is able to receive.
        if ((i_payload.size() + SYNC_BYTE_SIZE) > MAX_PAYLOAD_SIZE)
        {
            uint8_t payloadSize = i_payload.size() + SYNC_BYTE_SIZE;
            uint8_t payloadHeaderDataSize =
                i_payload[PAYLOAD_HEADER_DATA_LENGTH_INDEX];
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::issueCommand(): "
                     "payload size %d exceeds max payload size of %d",
                     payloadSize, MAX_PAYLOAD_SIZE);
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_ISSUE_COMMAND
            * @reasoncode       BPM_RC::BPM_INVALID_PAYLOAD_SIZE
            * @userdata1[00:31] Full Payload Size, including SYNC_BYTE
            * @userdata1[32:63] MAX_PAYLOAD_SIZE
            * @userdata2[00:31] Payload Header + Data size
            * @userdata2[32:63] NVDIMM Target HUID associated with this BPM
            * @devdesc          The maximum payload size to be sent to the BPM
            *                   was exceeded.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                           BPM_RC::BPM_ISSUE_COMMAND,
                                           BPM_RC::BPM_INVALID_PAYLOAD_SIZE,
                                           TWO_UINT16_TO_UINT32(payloadSize,
                                                    MAX_PAYLOAD_SIZE),
                                           TWO_UINT32_TO_UINT64(
                                               payloadHeaderDataSize,
                                               TARGETING::get_huid(iv_nvdimm))
                                           );
            errl->addProcedureCallout(HWAS::EPUB_PRC_HB_CODE,
                                      HWAS::SRCI_PRIORITY_HIGH);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

        // Load the payload
        int i = 0;
        for (const auto& byte : i_payload)
        {
            errl = nvdimmWriteReg(iv_nvdimm,
                                (BPM_REG_PAYLOAD_START + (i * sizeof(uint8_t))),
                                  byte);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::issueCommand(): "
                          "Failed to write payload to BPM_REG_PAYLOAD_START");
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }

            ++i;
        }
        if (errl != nullptr)
        {
            break;
        }

        // Clear the error status register
        errl = nvdimmWriteReg(iv_nvdimm,
                              BPM_REG_ERR_STATUS,
                              0x00);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::issueCommand(): "
                      "Failed to clear error status register");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Set the payload length. This is the actual length of the payload
        // excluding the size of the SYNC_BYTE that was dropped during payload
        // creation which is already missing from size().
        uint8_t data = i_payload.size();
        errl = nvdimmWriteReg(iv_nvdimm,
                              BPM_PAYLOAD_LENGTH,
                              data);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::issueCommand(): "
                      "Failed to set payload length");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Setup the command status register
        command_status_register_t commandStatus;
        commandStatus.bits.Bsp_Cmd_In_Progress = 1;
        commandStatus.bits.Operator_Type = i_opType;
        errl = nvdimmWriteReg(iv_nvdimm,
                              BPM_CMD_STATUS,
                              commandStatus.value);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::issueCommand(): "
                      "Failed to setup the command status register");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Setup command type. The basically executes the command
        errl = nvdimmWriteReg(iv_nvdimm,
                              BPM_REG_CMD,
                              i_command);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::issueCommand(): "
                      "Failed to set the command type. "
                      "The command was not issued to the BPM");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        errl = waitForCommandStatusBitReset(commandStatus);
        if (errl != nullptr)
        {
            break;
        }

        // If a delay was given then wait for the delay and check the response.
        // Otherwise, do not wait and do not check the response. For a list of
        // commands and delays, see bpm_update.H for more info.
        if (i_msDelay > 0)
        {
            // Wait the given time in ms. Default 1ms for most commands.
            nanosleep(0, i_msDelay * NS_PER_MSEC);

            // Check the response from the BPM. A non-zero response value
            // indicates failure. So, assume a failure and check for success.
            uint8_t data = 0xFF;
            errl = getResponse(&data, sizeof(uint8_t));
            if (errl != nullptr)
            {
                break;
            }

            // If the data read from the response is a non-zero value then the
            // issued command failed.
            if (data != 0)
            {
                /*@
                * @errortype
                * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
                * @moduleid         BPM_RC::BPM_ISSUE_COMMAND
                * @reasoncode       BPM_RC::BPM_BAD_RESPONSE
                * @userdata1        The command that failed to execute.
                *                   See bpm_update.H for list of commands.
                * @userdata2        NVDIMM Target HUID associated with this BPM
                * @devdesc          The command sent to the BPM failed.
                * @custdesc         A problem occurred during IPL of the system.
                */
                errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                         BPM_RC::BPM_ISSUE_COMMAND,
                                         BPM_RC::BPM_BAD_RESPONSE,
                                         i_payload[PAYLOAD_COMMAND_INDEX],
                                         TARGETING::get_huid(iv_nvdimm));
                errl->addPartCallout(iv_nvdimm,
                                     HWAS::BPM_PART_TYPE,
                                     HWAS::SRCI_PRIORITY_HIGH);
                errl->collectTrace(BPM_COMP_NAME);
                nvdimmAddPage4Regs(iv_nvdimm,errl);
                nvdimmAddVendorLog(iv_nvdimm, errl);
                break;
            }
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::runUpdate(BpmFirmwareLidImage i_fwImage,
                          BpmConfigLidImage i_configImage)
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::runUpdate(): "
              "Running BPM Update for NVDIMM 0x%.8X",
               TARGETING::get_huid(iv_nvdimm));

    errlHndl_t errl = nullptr;
    // Assume an update is necessary for the BPM and determine if it isn't.
    bool shouldPerformUpdate = true;

    // Get the sys target to check for attribute overrides.
    TARGETING::Target* sys = nullptr;
    TARGETING::targetService().getTopLevelTarget(sys);

    auto updateOverride =
        sys->getAttr<TARGETING::ATTR_BPM_UPDATE_OVERRIDE>();
    uint16_t firmwareOverrideFlag = (updateOverride & 0xFF00);
    uint16_t configOverrideFlag = (updateOverride & 0x00FF);

    do {

        // First check if there is a BPM connected
        errl = verifyGoodBpmState();
        if (errl != nullptr)
        {
            // Either there isn't a BPM connected to this NVDIMM or it's not
            // functional. Don't bother with updates.
            shouldPerformUpdate = false;
            iv_attemptAnotherUpdate = false;
            break;
        }

        // Check the version on the BPM against the version in the image.
        uint16_t bpmFwVersion = INVALID_VERSION;
        errl = getFwVersion(bpmFwVersion);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runUpdate(): "
                     "Could not determine firmware version on BPM "
                     "Skipping update.");
            break;
        }

        TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                  "Firmware version on the BPM 0x%.4X, "
                  "Firmware version of image 0x%.4X.",
                  bpmFwVersion, i_fwImage.getVersion());

        if (i_fwImage.getVersion() == bpmFwVersion)
        {
            shouldPerformUpdate = false;
            if (updateOverride == TARGETING::BPM_UPDATE_BEHAVIOR_DEFAULT_ALL)
            {
                TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                         "Firmware version on the BPM matches the version in "
                         "the image. Skipping update.");
                break;
            }
        }

        if (updateOverride == TARGETING::BPM_UPDATE_BEHAVIOR_SKIP_ALL)
        {
            TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                     "ATTR_BPM_UPDATE_OVERRIDE set to SKIP_ALL. "
                     "Skipping update.");
            break;
        }

        // Depending on the BSL version a CRC check may be necessary
        errl = readBslVersion();
        if (errl != nullptr)
        {
            break;
        }

        // If the BSL version read from the BPM isn't a supported version then
        // don't perform the updates as the update flow may have changed between
        // BSL versions.
        if (iv_bslVersion != BSL_VERSION_1_4)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::runUpdate(): "
                      "Unsupported BSL Version 0x%.2X detected on BPM. "
                      "Cancelling Update.");

            break;
        }

        if ((shouldPerformUpdate
                || (firmwareOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_FORCE_FW))
           && !(firmwareOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_SKIP_FW))
        {
            if (firmwareOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_FORCE_FW)
            {
                TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                         "ATTR_BPM_UPDATE_OVERRIDE set to force firmware "
                         "portion of BPM updates. Running Firmware Update...");
            }

            errl = runFirmwareUpdates(i_fwImage);
            if (errl != nullptr)
            {
                break;
            }
        }
        else
        {
            if (firmwareOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_SKIP_FW)
            {
                TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                         "ATTR_BPM_UPDATE_OVERRIDE set to skip firmware "
                         "portion of BPM updates. Skipping Firmware Update...");
            }
            else
            {
                TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                         "Firmware Data on BPM already up-to-date. "
                         "Skipping Firmware Update...");
            }
        }

        if ((shouldPerformUpdate
                || (configOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_FORCE_CONFIG))
           && !(configOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_SKIP_CONFIG))
        {
            if (configOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_FORCE_CONFIG)
            {
                TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                         "ATTR_BPM_UPDATE_OVERRIDE set to force config "
                         "portion of BPM updates. Running Config Update...");
            }
            errl = runConfigUpdates(i_configImage);
            if (errl != nullptr)
            {
                break;
            }
        }
        else
        {
            if (configOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_SKIP_CONFIG)
            {
                TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                         "ATTR_BPM_UPDATE_OVERRIDE set to skip config "
                         "portion of BPM updates. Skipping Config Update...");
            }
            else
            {
                TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                         "Configuration Data on BPM already up-to-date. "
                         "Skipping Config Update...");
            }
        }

    } while(0);

    if ((shouldPerformUpdate
        || (configOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_FORCE_CONFIG)
        || (firmwareOverrideFlag == TARGETING::BPM_UPDATE_BEHAVIOR_FORCE_FW))
        && (updateOverride != TARGETING::BPM_UPDATE_BEHAVIOR_SKIP_ALL))
    {
        // Reset controller and unlock encryption if necessary
        errlHndl_t exitErrl = nvdimmResetController(iv_nvdimm);
        if (exitErrl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runUpdate() "
                     "Couldn't reset NVDIMM controller.");
            handleMultipleErrors(errl, exitErrl);
        }

        // If the update was successful then we must wait for 15 seconds before
        // polling the status of the BPM since it has to finish updating its
        // firmware and resetting.
        TRACFCOMP(g_trac_bpm, "Bpm::runUpdate(): "
                  "Wait for the BPM to finish update and reset procedure, "
                  "sleep for 15 seconds");
        longSleep(15);

        // Poll SCAP_STATUS register for BPM state before we check final
        // firmware version.
        exitErrl = verifyGoodBpmState();
        if (exitErrl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runUpdate(): "
                     "Could not verify that BPM was present and enabled!");
            handleMultipleErrors(errl, exitErrl);
        }

        uint16_t bpmFwVersion = INVALID_VERSION;
        exitErrl = getFwVersion(bpmFwVersion);
        if (exitErrl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runUpdate(): "
                     "Could not determine firmware version on the BPM");
            handleMultipleErrors(errl, exitErrl);
        }

        TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                  "Firmware version on the BPM 0x%.4X, "
                  "Firmware version of image 0x%.4X.",
                  bpmFwVersion, i_fwImage.getVersion());

        if (i_fwImage.getVersion() == bpmFwVersion)
        {
            TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::runUpdate(): "
                     "Firmware version on the BPM matches the version in the "
                     "image. Firmware Update Successful.");
            iv_attemptAnotherUpdate = false;
        }
        else
        {
            // Attempt another update if one hasn't already been attempted.
            setAttemptAnotherUpdate();
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runUpdate(): "
                     "Version on BPM didn't match image. %s ",
                     iv_attemptAnotherUpdate ?
                     "Attempt another update..."
                     : "Attempts to update the BPM have failed.");
            if (iv_attemptAnotherUpdate == false)
            {
                /*@
                * @errortype
                * @severity         ERRORLOG::ERRL_SEV_UNRECOVERABLE
                * @moduleid         BPM_RC::BPM_RUN_FW_UPDATES
                * @reasoncode       BPM_RC::BPM_VERSION_MISMATCH
                * @userdata1[00:31] Version on the BPM
                * @userdata1[32:63] Version of the flash image
                * @userdata2        NVDIMM Target HUID associated with this BPM
                * @devdesc          The version on the BPM didn't match the
                *                   version in the flash image.
                * @custdesc         A problem occurred during IPL of the system.
                */
                exitErrl = new ERRORLOG::ErrlEntry(
                                           ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                           BPM_RC::BPM_RUN_FW_UPDATES,
                                           BPM_RC::BPM_VERSION_MISMATCH,
                                           TWO_UINT32_TO_UINT64(bpmFwVersion,
                                               i_fwImage.getVersion()),
                                           TARGETING::get_huid(iv_nvdimm));
                exitErrl->collectTrace(BPM_COMP_NAME);
                handleMultipleErrors(errl, exitErrl);
            }
        }

        TRACFCOMP(g_trac_bpm, EXIT_MRK"Bpm::runUpdate(): "
                  "Concluding BPM Update for NVDIMM 0x%.8X %s",
                  TARGETING::get_huid(iv_nvdimm),
                  (errl != nullptr) ? "with errors" : "without errors");
    }

    // An update has been attempted at least once. Set member variable to true
    // to dictate future update attempts. This variable should only be set at
    // the end of the update procedure in order to properly control future
    // update attempts.
    iv_updateAttempted = true;

    if (errl == nullptr)
    {
        /*@
        * @errortype
        * @severity         ERRORLOG::ERRL_SEV_INFORMATIONAL
        * @moduleid         BPM_RC::BPM_RUN_UPDATE
        * @reasoncode       BPM_RC::BPM_UPDATE_SUCCESSFUL
        * @userdata1        NVDIMM Target HUID associated with this BPM
        * @devdesc          BPM Update finished without errors.
        * @custdesc         Informational log associated with DIMM updates.
        */
        errlHndl_t infoErrl = new ERRORLOG::ErrlEntry(
                                            ERRORLOG::ERRL_SEV_INFORMATIONAL,
                                            BPM_RC::BPM_RUN_UPDATE,
                                            BPM_RC::BPM_UPDATE_SUCCESSFUL,
                                            TARGETING::get_huid(iv_nvdimm));
        infoErrl->collectTrace(BPM_COMP_NAME);
        ERRORLOG::errlCommit(infoErrl, BPM_COMP_ID);
    }

    return errl;
}

errlHndl_t Bpm::inUpdateMode()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::inUpdateMode()");
    errlHndl_t errl = nullptr;

    do {

        errl = issueCommand(BPM_LOCAL,
                            BCL_IS_UPDATE_IN_PROGRESS,
                            READ,
                            NO_DELAY_NO_RESPONSE);
        if (errl != nullptr)
        {
            break;
        }

        uint8_t isUpdateInProgress = 0;
        errl = nvdimmReadReg(iv_nvdimm,
                             BPM_REG_ERR_STATUS,
                             isUpdateInProgress);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::inUpdateMode(): "
                      "Failed to read error status register");
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

        if (!isUpdateInProgress)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::inUpdateMode(): "
                     "Failed to enter update mode");
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_IN_UPDATE_MODE
            * @reasoncode       BPM_RC::BPM_UPDATE_MODE_VERIFICATION_FAIL
            * @userdata1        NVDIMM Target HUID associated with this BPM
            * @devdesc          Failed to verify update mode was entered using
            *                   the BSL interface.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                     BPM_RC::BPM_IN_UPDATE_MODE,
                                     BPM_RC::BPM_UPDATE_MODE_VERIFICATION_FAIL,
                                     TARGETING::get_huid(iv_nvdimm));
            errl->addPartCallout(iv_nvdimm,
                                 HWAS::BPM_PART_TYPE,
                                 HWAS::SRCI_PRIORITY_HIGH);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::enterUpdateMode()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::enterUpdateMode()");
    errlHndl_t errl = nullptr;

    do {

        // Disable write protection on the BPM. Otherwise, we can't write the
        // magic values that enable the nvdimm-bpm interface.
        errl = disableWriteProtection();
        if (errl != nullptr)
        {
            break;
        }

        // Write the magic values to enable nvdimm-bpm interface
        errl = writeToMagicRegisters(UPDATE_MODE_MAGIC_VALUES);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::enterUpdateMode(): "
                      "Failed to write magic numbers that enable "
                      "update mode");
            break;
        }

        TRACFCOMP(g_trac_bpm, "Bpm::enterUpdateMode(): "
                 "Issuing BPM_LOCAL BCL_START_UPDATE command.");

        errl = issueCommand(BPM_LOCAL,
                            BCL_START_UPDATE,
                            WRITE,
                            NO_DELAY_NO_RESPONSE);
        if (errl != nullptr)
        {
            break;
        }

        nanosleep(2,0);

        /*@
        * @errortype
        * @severity         ERRORLOG::ERRL_SEV_INFORMATIONAL
        * @moduleid         BPM_RC::BPM_START_UPDATE
        * @reasoncode       BPM_RC::BPM_ENTER_UPDATE_MODE
        * @userdata1        NVDIMM Target HUID associated with this BPM
        * @devdesc          BPM has entered update mode.
        * @custdesc         Informational log associated with DIMM updates.
        */
        errlHndl_t infoErrl = new ERRORLOG::ErrlEntry(
                                            ERRORLOG::ERRL_SEV_INFORMATIONAL,
                                            BPM_RC::BPM_START_UPDATE,
                                            BPM_RC::BPM_ENTER_UPDATE_MODE,
                                            TARGETING::get_huid(iv_nvdimm));
        infoErrl->addPartCallout(iv_nvdimm,
                             HWAS::BPM_PART_TYPE,
                             HWAS::SRCI_PRIORITY_HIGH);
        infoErrl->collectTrace(BPM_COMP_NAME);
        nvdimmAddVendorLog(iv_nvdimm, infoErrl);
        nvdimmAddPage4Regs(iv_nvdimm, infoErrl);
        ERRORLOG::errlCommit(infoErrl, BPM_COMP_ID);

    } while(0);

    return errl;
}

errlHndl_t Bpm::exitUpdateMode()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::exitUpdateMode()");
    errlHndl_t errl = nullptr;

    do {

        errl = writeToMagicRegisters(UPDATE_MODE_MAGIC_VALUES);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::exitUpdateMode(): "
                      "Failed to write the update magic values to "
                      " be able to send BPM_LOCAL commands.");
            break;
        }

        errl = issueCommand(BPM_LOCAL,
                            BCL_IS_UPDATE_IN_PROGRESS,
                            READ,
                            NO_DELAY_NO_RESPONSE);
        if (errl != nullptr)
        {
            break;
        }

        uint8_t isUpdateInProgress = 0;
        errl = nvdimmReadReg(iv_nvdimm,
                             BPM_REG_ERR_STATUS,
                             isUpdateInProgress);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::exitUpdateMode(): "
                      "Failed to read BPM_REG_ERR_STATUS register to determine "
                      "if BPM is in update mode.");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Sending the exit update command when the BPM isn't in update mode can
        // cause unpredicatable behavior and errors.
        if (isUpdateInProgress)
        {
            errl = issueCommand(BPM_LOCAL,
                                BCL_END_UPDATE,
                                WRITE,
                                NO_DELAY_NO_RESPONSE);
            if (errl != nullptr)
            {
                break;
            }
        }
        else
        {
            TRACFCOMP(g_trac_bpm, "Bpm::exitUpdateMode(): "
                      "Not in update mode. "
                      "Exit update command will not be sent.");
        }

        // Write back the production magic values
        errl = writeToMagicRegisters(PRODUCTION_MAGIC_VALUES);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::exitUpdateMode(): "
                      "Failed to write the production magic values to "
                      "disable update mode.");
            break;
        }

        /*@
        * @errortype
        * @severity         ERRORLOG::ERRL_SEV_INFORMATIONAL
        * @moduleid         BPM_RC::BPM_END_UPDATE
        * @reasoncode       BPM_RC::BPM_EXIT_UPDATE_MODE
        * @userdata1        NVDIMM Target HUID associated with this BPM
        * @devdesc          BPM has exited update mode.
        * @custdesc         Informational log associated with DIMM updates.
        */
        errlHndl_t infoErrl = new ERRORLOG::ErrlEntry(
                                            ERRORLOG::ERRL_SEV_INFORMATIONAL,
                                            BPM_RC::BPM_END_UPDATE,
                                            BPM_RC::BPM_EXIT_UPDATE_MODE,
                                            TARGETING::get_huid(iv_nvdimm));
        infoErrl->addPartCallout(iv_nvdimm,
                             HWAS::BPM_PART_TYPE,
                             HWAS::SRCI_PRIORITY_HIGH);
        infoErrl->collectTrace(BPM_COMP_NAME);
        nvdimmAddVendorLog(iv_nvdimm, infoErrl);
        nvdimmAddPage4Regs(iv_nvdimm, infoErrl);
        ERRORLOG::errlCommit(infoErrl, BPM_COMP_ID);

    } while(0);

    return errl;
}

errlHndl_t Bpm::updateFirmware(BpmFirmwareLidImage i_image)
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::updateFirmware()");
    errlHndl_t errl = nullptr;

    // The reset vector address is near the end of the firmware section.
    // We must do a special operation on it when it shows up during the update.
    const uint16_t RESET_VECTOR_ADDRESS     = 0xFFFE;

    bool mainAddressEncountered = false;

    // Get the number of blocks in the image
    const uint16_t NUMBER_OF_BLOCKS = i_image.getNumberOfBlocks();

    char const * data =
        reinterpret_cast<char const *>(i_image.getFirstBlock());

    firmware_image_block_t const * block =
        reinterpret_cast<firmware_image_block_t const *>
            (data);

    for(size_t i = 0; i < NUMBER_OF_BLOCKS; ++i)
    {
        // This is done once at the main program address.
        if ( ((block->iv_addressOffset == MAIN_PROGRAM_ADDRESS)
           || (block->iv_addressOffset == MAIN_PROGRAM_ADDRESS_ALT))
           && !mainAddressEncountered)
        {
            // Only execute this once.
            mainAddressEncountered = true;

            // Save the firmware start address for later. This will be needed
            // for the final CRC check when the update is completed.
            iv_firmwareStartAddress = block->iv_addressOffset;

            payload_t payload;
            errl = setupPayload(payload,
                                BSL_MASS_ERASE,
                                iv_firmwareStartAddress);
            if (errl != nullptr)
            {
                break;
            }

            errl = issueCommand(BPM_PASSTHROUGH,
                                payload,
                                WRITE,
                                ERASE_FIRMWARE_DELAY);
            if (errl != nullptr)
            {
                break;
            }

            TRACFCOMP(g_trac_bpm, "Bpm::updateFirmware(): "
                      "Performing BSL_MASS_ERASE on BPM, sleep for 5 seconds.");
            longSleep(5);

            TRACFCOMP(g_trac_bpm, "Bpm::updateFirmware(): "
                     "Begin writing flash image to BPM "
                     "with a starting address of 0x%.4X",
                     iv_firmwareStartAddress);

        }

        if (block->iv_addressOffset % 0x400 == 0)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::updateFirmware(): "
                     "Writing to address offset 0x%.4X. "
                     "Firmware blocks written: %d; Remaining: %d",
                     block->iv_addressOffset,
                     i, (NUMBER_OF_BLOCKS - i));
        }

        // Construct the payload for this block in the image
        payload_t payload;
        errl = setupPayload(payload, block, BSL_RX_DATA_BLOCK);
        if (errl != nullptr)
        {
            break;
        }

        if (block->iv_addressOffset == RESET_VECTOR_ADDRESS)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::updateFirmware(): "
                     "Encountered RESET_VECTOR_ADDRESS 0x%.4X. "
                     "Attempt to write RESET_VECTOR to BPM up to %d times.",
                     RESET_VECTOR_ADDRESS,
                     MAX_RETRY);
            // Attempting to BSL_VERIFY_BLOCK on the reset vector data will
            // fail. To verify that this data is written correctly we will check
            // the response packet sent by the BPM.
            const uint8_t RESET_VECTOR_RECEIVE_SUCCESS = 0x80;
            uint8_t retry = 1;
            do
            {
                // Issue the write command to the BPM.
                // The RESET_VECTOR is special in that its response is checked
                // externally.
                errl = issueCommand(BPM_PASSTHROUGH,
                                    payload,
                                    WRITE,
                                    NO_DELAY_EXTERNAL_RESPONSE);
                if (errl != nullptr)
                {
                    break;
                }

                // Get the response packet and verify that the status is
                // RESET_VECTOR_RECEIVE_SUCCESS.
                //
                // Any status besides RESET_VECTOR_RECEIVE_SUCCESS is considered
                // a fail. So, assume a failure and check.
                uint8_t status = 0xFF;
                errl = getResponse(&status,
                                   sizeof(uint8_t));
                if (errl != nullptr)
                {
                    break;
                }

                if (status != RESET_VECTOR_RECEIVE_SUCCESS)
                {
                    TRACFCOMP(g_trac_bpm, "Bpm::updateFirmware(): "
                             "status %d from BPM was not "
                             "RESET_VECTOR_RECEIVE_SUCCESS value of %d. "
                             "Retrying...",
                             status,
                             RESET_VECTOR_RECEIVE_SUCCESS);

                    if (++retry > MAX_RETRY)
                    {
                        TRACFCOMP(g_trac_bpm, "Bpm::updateFirmware(): "
                                  "Never received RESET_VECTOR_RECEIVE_SUCCESS "
                                  "status from BPM in three attempts. "
                                  "Aborting Update");
                        /*@
                        * @errortype
                        * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
                        * @moduleid         BPM_RC::BPM_UPDATE_FIRMWARE
                        * @reasoncode       BPM_RC::BPM_RESET_VECTOR_NEVER_RECEIVED
                        * @userdata1        NVDIMM Target HUID associated with this BPM
                        * @devdesc          RESET_VECTOR_RECEIVE_SUCCESS status was not
                        *                   received in three attempts.
                        * @custdesc         A problem occurred during IPL of the system.
                        */
                        errl = new ERRORLOG::ErrlEntry(
                                        ERRORLOG::ERRL_SEV_PREDICTIVE,
                                        BPM_RC::BPM_UPDATE_FIRMWARE,
                                        BPM_RC::BPM_RESET_VECTOR_NEVER_RECEIVED,
                                        TARGETING::get_huid(iv_nvdimm));
                        errl->addPartCallout(iv_nvdimm,
                                             HWAS::BPM_PART_TYPE,
                                             HWAS::SRCI_PRIORITY_HIGH);
                        errl->collectTrace(BPM_COMP_NAME);
                        nvdimmAddPage4Regs(iv_nvdimm,errl);
                        nvdimmAddVendorLog(iv_nvdimm, errl);

                        // Change the state of iv_attemptAnotherUpdate to signal
                        // if another update attempt should occur.
                        setAttemptAnotherUpdate();

                        break;
                    }
                }
                else
                {
                    // RESET_VECTOR was written and received successfully.
                    // Exit retry loop.
                    break;
                }

                // Sleep for 0.001 second before attempting again.
                nanosleep(0, 1 * NS_PER_MSEC);

            } while(retry <= MAX_RETRY);
            if (errl != nullptr)
            {
                break;
            }
        }
        else
        {
            // Attempt to write the data using a retry loop. This will also
            // verify that the data was correctly written to the BPM.
            errl = blockWrite(payload);
            if (errl != nullptr)
            {
                break;
            }
        }

        // Move to the next block
        // iv_blocksize doesn't include the sizeof itself. So, add another byte
        // for it.
        data += block->iv_blockSize + sizeof(uint8_t);
        block = reinterpret_cast<firmware_image_block_t const *>(data);
    }

    TRACFCOMP(g_trac_bpm, EXIT_MRK"Bpm::updateFirmware(): "
             "Firmware flash image write and verification completed "
             "%s",
             (errl == nullptr) ? "without errors" : "with errors");

    return errl;
}

errlHndl_t Bpm::updateConfig()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::updateConfig()");
    errlHndl_t errl = nullptr;

    do {

        // Erase Segment D on the BPM via the BSL interface.
        errl = eraseSegment(SEGMENT_D_CODE);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::updateConfig(): "
                      "Failed to erase Segment D.");
            break;
        }

        // Write the updated Segment D buffer to the BPM via the BSL interface.
        errl = writeSegment(iv_segmentD, SEGMENT_D_CODE);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::updateConfig(): "
                      "Failed to write Segment D.");
            break;
        }


        // Erase Segment B on the BPM via the BSL interface.
        errl = eraseSegment(SEGMENT_B_CODE);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::updateConfig(): "
                      "Failed to erase Segment B.");
            break;
        }

        // Write the updated Segment B buffer to the BPM via the BSL interface.
        errl = writeSegment(iv_segmentB, SEGMENT_B_CODE);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::updateConfig(): "
                      "Failed to write Segment B.");
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::enterBootstrapLoaderMode()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::enterBootstrapLoaderMode()");
    errlHndl_t errl = nullptr;

    do {

        // Entering BSL mode depends on the state of the BPM and it may need
        // several retries in order to successfully enter BSL mode.
        int retry = 5;
        bool inBslMode = false;

        while (retry != 0)
        {

            errl = issueCommand(BPM_LOCAL,
                                BCL_IS_BSL_MODE,
                                WRITE,
                                NO_DELAY_NO_RESPONSE);
            if (errl != nullptr)
            {
                break;
            }

            uint8_t data = 0;
            errl = nvdimmReadReg(iv_nvdimm,
                                 BPM_REG_ERR_STATUS,
                                 data);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::enterBootstrapLoaderMode(): "
                          "Failed to read BPM_REG_ERR_STATUS to verify that "
                          "BSL mode was enabled.");
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }
            // data will be 1 if the BPM successfully entered BSL mode.
            if (data == 1)
            {
                inBslMode = true;
                TRACFCOMP(g_trac_bpm, "Bpm::enterBootstrapLoaderMode(): "
                          "BSL Mode entered, sleep for 5 seconds.");
                longSleep(5);
                break;
            }

            // Sleep for 0.001 second.
            nanosleep(0, 1 * NS_PER_MSEC);

            errl = issueCommand(BPM_LOCAL,
                                BCL_ENTER_BSL_MODE,
                                WRITE,
                                NO_DELAY_NO_RESPONSE);
            if (errl != nullptr)
            {
                break;
            }

            TRACUCOMP(g_trac_bpm, "Bpm::enterBootstrapLoaderMode(): "
                      "Unable to enter BSL Mode, retries remaining %d. "
                      "Sleep for 2 seconds before trying again.",
                      (retry - 1));
            nanosleep(2,0);
            --retry;

        }
        if (errl != nullptr)
        {
            break;
        }

        if (!inBslMode)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::enterBootstrapLoaderMode(): "
                     "Failed to enter BSL mode on the BPM");
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_ENTER_BSL_MODE
            * @reasoncode       BPM_RC::BPM_FAILED_TO_ENTER_BSL_MODE
            * @userdata1[0:63]  NVDIMM Target HUID associated with this BPM
            * @devdesc          Failed to enter BSL mode after several attempts.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                           BPM_RC::BPM_ENTER_BSL_MODE,
                                           BPM_RC::BPM_FAILED_TO_ENTER_BSL_MODE,
                                           TARGETING::get_huid(iv_nvdimm));
            errl->addPartCallout(iv_nvdimm,
                                 HWAS::BPM_PART_TYPE,
                                 HWAS::SRCI_PRIORITY_HIGH);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::setupPayload(payload_t       & o_payload,
                             const uint8_t     i_command,
                             const uint16_t    i_address,
                             const uint8_t     i_data[],
                             const size_t      i_length)
{
    // Enforce sane inputs
    assert((    (i_data == nullptr && i_length == 0)
             || (i_data != nullptr && i_length != 0)),
          "if i_length is non-zero then i_data must not be nullptr, otherwise i_data must be nullptr.");
    assert(isBslCommand(i_command),
           "i_command must be a valid BSL command");

    errlHndl_t errl = nullptr;

    // Calculate the block size.
    size_t blockSize = sizeof(uint16_t) + i_length;

    // Allocate memory for the block
    firmware_image_block_t* myBlock = reinterpret_cast<firmware_image_block_t*>(
        malloc(sizeof(firmware_image_block_t) + i_length));

    // Setup the block "header" info
    myBlock->iv_blockSize = blockSize;
    myBlock->iv_addressOffset = i_address;

    // Copy the data if any exists.
    if (i_data != nullptr)
    {
        memcpy(&myBlock->iv_data, i_data, i_length);
    }

    // Setup the return payload
    errl = setupPayload(o_payload, myBlock, i_command);

    // Block is no longer needed.
    free(myBlock);

    return errl;
}

errlHndl_t Bpm::setupPayload(payload_t                    & o_payload,
                             const firmware_image_block_t * i_block,
                             const uint8_t                  i_command)
{
    assert(i_block != nullptr, "i_block must not be nullptr.");
    assert(isBslCommand(i_command),
           "i_command must be a valid BSL command");

    errlHndl_t errl = nullptr;

    // The data size in the block is the total block size
    // minus the 2 bytes for the address offset.
    const uint8_t blockDataSize = i_block->iv_blockSize - sizeof(uint16_t);

    // The header plus payload data section size. This excludes the address
    // offset, extra bytes, and CRC bytes.
    const uint8_t headerDataSize = PAYLOAD_HEADER_SIZE + blockDataSize;

    do {

    if (blockDataSize > MAX_PAYLOAD_DATA_SIZE)
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK
                 "Bpm::setupPayload(): Block Data Size %d exceeds max payload "
                 "size of %d",
                 blockDataSize,
                 MAX_PAYLOAD_DATA_SIZE);
        /*@
        * @errortype
        * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
        * @moduleid         BPM_RC::BPM_SETUP_PAYLOAD
        * @reasoncode       BPM_RC::BPM_INVALID_PAYLOAD_DATA_SIZE
        * @userdata1[0:7]   Block Data Size
        * @userdata1[8:15]  MAX_PAYLOAD_DATA_SIZE
        * @userdata2[0:63]  NVDIMM Target HUID associated with this BPM
        * @devdesc          Failed to enter BSL mode after several attempts.
        * @custdesc         A problem occurred during IPL of the system.
        */
        errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                       BPM_RC::BPM_SETUP_PAYLOAD,
                                       BPM_RC::BPM_INVALID_PAYLOAD_DATA_SIZE,
                                       TWO_UINT8_TO_UINT16(blockDataSize,
                                           MAX_PAYLOAD_DATA_SIZE),
                                       TARGETING::get_huid(iv_nvdimm));
        errl->collectTrace(BPM_COMP_NAME);
        nvdimmAddPage4Regs(iv_nvdimm,errl);
        nvdimmAddVendorLog(iv_nvdimm, errl);
        break;
    }

    // Create the payload with the exact size needed.
    payload_t payload(MAX_PAYLOAD_OTHER_DATA_SIZE + blockDataSize);

    // Instead of using push_back, use a pointer to an element in the vector.
    // Since the size of the vector is declared and intialized to zero ahead of
    // time push_back will not work. Also, some of the data is larger than
    // uint8_t and so it's easier to just use memcpy for insertion.
    // NOTE: Because push_back isn't being used the size() of the vector doesn't
    //       change along with the data being added to the vector. This was
    //       corrected by explicitly setting the payload size in the constructor
    //       call above.
    uint8_t * payloadIterator = payload.data();

    // According to SMART, we must supply the header + data size twice.
    uint8_t header[PAYLOAD_HEADER_SIZE] = { SYNC_BYTE,
                                            i_command,
                                            headerDataSize,
                                            headerDataSize };

    memcpy(payloadIterator, &header, PAYLOAD_HEADER_SIZE);

    // Move past the header
    payloadIterator += PAYLOAD_HEADER_SIZE;

    // Write the address offset in little endian form.
    uint16_t addressLE = htole16(i_block->iv_addressOffset);
    uint8_t* addressOffset = reinterpret_cast<uint8_t*>(&addressLE);
    memcpy(payloadIterator, addressOffset, sizeof(uint16_t));

    // Move past the address
    payloadIterator += sizeof(uint16_t);

    // The extra bytes vary based on the given command.
    // These are the extra bytes for their corresponding bootstrap loader
    // commands. They are arranged in little endian form so that no byte
    // swapping is required.
    const uint8_t BSL_ERASE_SEGMENT_EXTRA_BYTES[] = {0x02, 0xA5};
    const uint8_t BSL_MASS_ERASE_EXTRA_BYTES[]    = {0x06, 0xA5};
    switch(i_command)
    {
        case BSL_ERASE_SEGMENT:
        {
            memcpy(payloadIterator,
                   &BSL_ERASE_SEGMENT_EXTRA_BYTES,
                   sizeof(uint16_t));

            break;
        }
        case BSL_MASS_ERASE:
        {
            memcpy(payloadIterator,
                   &BSL_MASS_ERASE_EXTRA_BYTES,
                   sizeof(uint16_t));
            break;
        }
        default:
        {
            // Give the size of the data section as a uint16_t in little
            // endian form.
            uint8_t dataLength[] = {blockDataSize, 0x0};
            memcpy(payloadIterator, &dataLength, sizeof(uint16_t));
            break;
        }
    }

    // Move past the payload's extra bytes.
    payloadIterator += sizeof(uint16_t);

    if (blockDataSize > 0)
    {
        // Copy the payload data from the LID image block to the payload's data
        // section.
        memcpy(payloadIterator, &i_block->iv_data, blockDataSize);

        // Move past the payload's data section.
        payloadIterator += blockDataSize;
    }

    // Calculate the CRC bytes
    // Pass in the size of the payload excluding the two reserved bytes
    // for the CRC.
    uint16_t crc = htole16(crc16_calc(payload.data(), payload.size()-2));

    // Write the CRC bytes
    uint8_t* crcBytes = reinterpret_cast<uint8_t*>(&crc);
    memcpy(payloadIterator, crcBytes, sizeof(uint16_t));

    // The sync byte is automatically sent by the NVDIMM to the BPM so
    // including it in the payload isn't necessary. It is only needed to
    // calculate the CRC bytes.
    payload.erase(payload.begin());
    // Force the returned payload to have the exact capacity and size of the
    // payload.
    o_payload.swap(payload);

    } while(0);

    return errl;
}

errlHndl_t Bpm::unlockDevice()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::unlockDevice()");
    errlHndl_t errl = nullptr;

    do {

        // This is a BSL command, so it must be formatted into a payload.
        payload_t payload;

        // This command must send the password in order to unlock the device.
        errl = setupPayload(payload,
                            BSL_RX_PASSWORD,
                            BPM_ADDRESS_ZERO,
                            BPM_PASSWORD,
                            BPM_PASSWORD_LENGTH);
        if (errl != nullptr)
        {
            break;
        }

        errl = issueCommand(BPM_PASSTHROUGH, payload, WRITE);
        if (errl != nullptr)
        {
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::resetDevice()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::resetDevice()");
    errlHndl_t errl = nullptr;

    do {

        // Verify we are in BSL mode by checking SCAP_STATUS because if we aren't
        // then we don't need to do anything.
        scap_status_register_t status;
        errl = nvdimmReadReg(iv_nvdimm,
                             SCAP_STATUS,
                             status.full);
        if (errl != nullptr)
        {
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        if (status.bit.Bpm_Bsl_Mode)
        {
            // This is a BSL command, so it must be formatted into a payload.
            payload_t payload;
            errl = setupPayload(payload, BSL_RESET_DEVICE, BPM_ADDRESS_ZERO);
            if (errl != nullptr)
            {
                break;
            }

            // Despite this being a BSL command we cannot check the response
            // because the BPM will either be offline and cannot respond or
            // the command will have completed and we won't be in BSL mode
            // anymore and therefor shouldn't check the response.
            errl = issueCommand(BPM_PASSTHROUGH,
                                payload,
                                WRITE,
                                NO_DELAY_NO_RESPONSE);
            if (errl != nullptr)
            {
                break;
            }

            // If we wait less than 15 seconds for the reset to occur it is
            // possible that BPM won't be ready for more commands via the NVDIMM
            TRACFCOMP(g_trac_bpm, "Bpm::resetDevice(): "
                      "Resetting BPM for NVDIMM 0x%.8X, sleep for 15 seconds.",
                      TARGETING::get_huid(iv_nvdimm));
            longSleep(15);
        }
        else
        {
            TRACFCOMP(g_trac_bpm, "Bpm::resetDevice(): "
                      "Not in BSL Mode. Don't send the reset command.");
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::readViaScapRegister(uint8_t const i_reg, uint8_t & io_data)
{
    TRACUCOMP(g_trac_bpm, ENTER_MRK"Bpm::readViaScapRegister()");
    errlHndl_t errl = nullptr;

    do {

        // Wait for the SCAP_STATUS Busy bit to be zero.
        errl = waitForBusyBit();
        if (errl != nullptr)
        {
            break;
        }

        // Write to SCAP register which register we're attempting to access on
        // the BPM
        errl = nvdimmWriteReg(iv_nvdimm,
                              SCAP_REG,
                              i_reg);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::readViaScapRegister(): "
                      "Failed to set SCAP_REG to register 0x%.2X",
                      i_reg);
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Wait for the SCAP_STATUS Busy bit to be zero.
        errl = waitForBusyBit();
        if (errl != nullptr)
        {
            break;
        }

        // Read out the data from the requested register
        errl = nvdimmReadReg(iv_nvdimm,
                             SCAP_DATA,
                             io_data);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "BPM::readViaScapRegister(): "
                     "Failed to read data from SCAP_DATA for register 0x%.2X.",
                     i_reg);
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::writeViaScapRegister(uint8_t const i_reg, uint8_t const i_data)
{
    TRACUCOMP(g_trac_bpm, ENTER_MRK"Bpm::writeViaScapRegister()");
    errlHndl_t errl = nullptr;

    do {

        // The SCAP_REG and SCAP_DATA registers require a few retries to get the
        // values to stick. This loop sets SCAP_REG to i_reg
        uint8_t retry = 0;
        uint8_t data = 0;
        do {

            // Wait for the SCAP_STATUS Busy bit to be zero.
            errl = waitForBusyBit();
            if (errl != nullptr)
            {
                break;
            }

            // Write to SCAP register which register we're attempting to access
            // on the BPM
            errl = nvdimmWriteReg(iv_nvdimm,
                                  SCAP_REG,
                                  i_reg);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::writeViaScapRegister(): "
                          "Failed to set SCAP_REG to register 0x%.2X",
                          i_reg);
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }

            // Wait for the SCAP_STATUS Busy bit to be zero.
            errl = waitForBusyBit();
            if (errl != nullptr)
            {
                break;
            }

            // Wait 100ms
            nanosleep(0, 100 * NS_PER_MSEC);

            errl = nvdimmReadReg(iv_nvdimm,
                                 SCAP_REG,
                                 data);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "BPM::writeViaScapRegister(): "
                         "Failed to read from SCAP_REG to verify that "
                         "requested register 0x%.2X was written successfully.",
                         i_reg);
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }

            if (data == i_reg)
            {
                TRACUCOMP(g_trac_bpm, "Bpm::writeViaScapRegister(): "
                         "REG 0x%X was successfully written to SCAP_REG 0x434. "
                         "Stop retries.",
                         i_reg);
                break;
            }

        } while(++retry < MAX_RETRY);
        if (errl != nullptr)
        {
            break;
        }
        if ((retry >= MAX_RETRY) && (data != i_reg))
        {
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_WRITE_VIA_SCAP
            * @reasoncode       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT_REG
            * @userdata1[0:31]  The register that we were attempting to write to
            *                   SCAP_REG.
            * @userdata1[32:63] The data that was found in the register on the
            *                   final attempt.
            * @userdata2        NVDIMM Target HUID associated with this BPM
            * @devdesc          The command sent to the BPM failed.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                           BPM_RC::BPM_WRITE_VIA_SCAP,
                                           BPM_RC::BPM_EXCEEDED_RETRY_LIMIT_REG,
                                           TWO_UINT32_TO_UINT64(i_reg,
                                               data),
                                           TARGETING::get_huid(iv_nvdimm));
            errl->addPartCallout(iv_nvdimm,
                                 HWAS::BPM_PART_TYPE,
                                 HWAS::SRCI_PRIORITY_HIGH);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

        // The SCAP_REG and SCAP_DATA registers require a few retries to get the
        // values to stick. This loop sets SCAP_DATA to i_data
        retry = 0;
        data = 0;
        do {

            // Wait for the SCAP_STATUS Busy bit to be zero.
            errl = waitForBusyBit();
            if (errl != nullptr)
            {
                break;
            }

            // Write the data to the register we're attempting to access
            // on the BPM.
            errl = nvdimmWriteReg(iv_nvdimm,
                                  SCAP_DATA,
                                  i_data);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, ERR_MRK"BPM::writeViaScapRegister(): "
                         "Failed to write data 0x%.2X to SCAP_DATA for "
                         "register 0x%.2X.",
                         i_data,
                         i_reg);
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }

            // Wait for the SCAP_STATUS Busy bit to be zero.
            errl = waitForBusyBit();
            if (errl != nullptr)
            {
                break;
            }

            // Wait 100ms
            nanosleep(0, 100 * NS_PER_MSEC);

            errl = nvdimmReadReg(iv_nvdimm,
                                 SCAP_DATA,
                                 data);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, ERR_MRK"BPM::writeViaScapRegister(): "
                         "Failed to read from SCAP_DATA to verify "
                         "that requested data 0x%.2X was written successfully.",
                         i_data);
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }

            if (data == i_data)
            {
                TRACUCOMP(g_trac_bpm, "Bpm::writeViaScapRegister(): "
                         "DATA 0x%X was successfully written to SCAP_DATA 0x435."
                         " Stop retries.",
                         i_data);
                break;
            }

        } while(++retry < MAX_RETRY);
        if (errl != nullptr)
        {
            break;
        }
        if ((retry >= MAX_RETRY) && (data != i_data))
        {
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_WRITE_VIA_SCAP
            * @reasoncode       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT_DATA
            * @userdata1[0:31]  The data that we were attempting to write to
            *                   SCAP_DATA.
            * @userdata1[32:63] The data that was found in the register on the
            *                   final attempt.
            * @userdata2        NVDIMM Target HUID associated with this BPM
            * @devdesc          The command sent to the BPM failed.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                          BPM_RC::BPM_WRITE_VIA_SCAP,
                                          BPM_RC::BPM_EXCEEDED_RETRY_LIMIT_DATA,
                                          TWO_UINT32_TO_UINT64(i_data,
                                              data),
                                          TARGETING::get_huid(iv_nvdimm));
            errl->addPartCallout(iv_nvdimm,
                                 HWAS::BPM_PART_TYPE,
                                 HWAS::SRCI_PRIORITY_HIGH);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::disableWriteProtection()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::disableWriteProtection()");
    errlHndl_t errl = nullptr;

    do {

        // The following write sequence to the I2C_REG_PROTECT register
        // indirectly removes write protection from registers 0x40-0x7F on
        // page 4.
        for ( size_t i = 0; i < BPM_PASSWORD_LENGTH; ++i)
        {
            errl = nvdimmWriteReg(iv_nvdimm,
                                  I2C_REG_PROTECT,
                                  BPM_PASSWORD[i]);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::disableWriteProtection(): "
                          "Failed to write the unlock sequence to "
                          "I2C_REG_PROTECT");
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }
        }
        if (errl != nullptr)
        {
            break;
        }

        nanosleep(0, 100 * NS_PER_MSEC);

        // Make sure protection was removed
        uint8_t data = 0;
        errl = nvdimmReadReg(iv_nvdimm,
                             I2C_REG_PROTECT,
                             data);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::disableWriteProtection(): "
                      "Failed to verify that write protection was removed");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }
        const uint8_t WRITE_PROTECT_DISABLED = 0x80;
        if (!(data & WRITE_PROTECT_DISABLED))
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::disableWriteProtection(): "
                      "Failed to disable write protection. I2C_REG_PROTECT");
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_DISABLE_WRITE_PROTECTION
            * @reasoncode       BPM_RC::BPM_DISABLE_WRITE_PROTECTION_FAILED
            * @userdata1        NVDIMM Target HUID associated with this BPM
            * @devdesc          Failed to enter BSL mode after several attempts.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                 BPM_RC::BPM_DISABLE_WRITE_PROTECTION,
                                 BPM_RC::BPM_DISABLE_WRITE_PROTECTION_FAILED,
                                 TARGETING::get_huid(iv_nvdimm));
            errl->addPartCallout(iv_nvdimm,
                                 HWAS::BPM_PART_TYPE,
                                 HWAS::SRCI_PRIORITY_HIGH);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::switchBpmPage(uint16_t const i_segmentCode)
{

    errlHndl_t errl = nullptr;

    do {
        const uint8_t SPECIAL_CONTROL_COMMAND1 = 0x3E;
        const uint8_t SPECIAL_CONTROL_COMMAND2 = 0x3F;
        // Next, switch to the desired BPM segment by writing the segment code
        // to the BPM's Special Control Command registers.
        //
        // Since the SCAP_DATA register can only hold 1 byte at a time we must
        // do this in two steps.
        // According to SMART, the segment code must be written in the following
        // form to those registers:
        // Register 0x3E gets LO(i_segmentCode) byte
        // Register 0x3F gets HI(i_segmentCode) byte
        // Example: 0x9D5E is the segment code for Segment D. It must be written
        //          as follows
        // 0x3E, 0x5E
        // 0x3F, 0x9D
        const uint8_t loSegCode = i_segmentCode & 0xFF;
        const uint8_t hiSegCode = (i_segmentCode >> 8) & 0xFF;

        TRACUCOMP(g_trac_bpm, "Bpm::switchBpmPage(): "
                 "Writing 0x%.2X to SPECIAL_CONTROL_COMMAND1 and "
                 "0x%.2X to SPECIAL_CONTROL_COMMAND2",
                 loSegCode,
                 hiSegCode);

        // First, clear the SPECIAL_CONTROL_COMMAND2 register so that we can
        // write the full sequence without issue.
        errl = writeViaScapRegister(SPECIAL_CONTROL_COMMAND2, 0x00);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::switchBpmPage(): "
                     "Writing 0x%.2X to SPECIAL_CONTROL_COMMAND2 "
                     "FAILED. BPM page will not have switched properly!!",
                     hiSegCode);
            break;
        }

        // Write the LO segment code.
        errl = writeViaScapRegister(SPECIAL_CONTROL_COMMAND1, loSegCode);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::switchBpmPage(): "
                     "Writing 0x%.2X to SPECIAL_CONTROL_COMMAND1 "
                     "FAILED. BPM page will not have switched properly!!",
                     loSegCode);
            break;
        }

        // Write the HI segment code.
        errl = writeViaScapRegister(SPECIAL_CONTROL_COMMAND2, hiSegCode);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::switchBpmPage(): "
                     "Writing 0x%.2X to SPECIAL_CONTROL_COMMAND2 "
                     "FAILED. BPM page will not have switched properly!!",
                     hiSegCode);
            break;
        }


        // Request to open segment page is sent.
        // Wait a few seconds for the operation to complete.
        nanosleep(2,0);

    } while(0);

    return errl;
}

errlHndl_t Bpm::writeToMagicRegisters(
        uint8_t const (&i_magicValues)[NUM_MAGIC_REGISTERS])
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::writeToMagicRegisters() 0x%.2X 0x%.2X",
             i_magicValues[0],
             i_magicValues[1]);
    errlHndl_t errl = nullptr;

    do {
        const uint16_t magic_registers[NUM_MAGIC_REGISTERS] =
            {BPM_MAGIC_REG1, BPM_MAGIC_REG2};

        for (size_t i = 0; i < NUM_MAGIC_REGISTERS; ++i)
        {
            errl = nvdimmWriteReg(iv_nvdimm,
                                  magic_registers[i],
                                  i_magicValues[i]);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::writeToMagicRegisters(): "
                          "Failed to write the magic values to the magic "
                          "registers");
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }
        }
        if (errl != nullptr)
        {
            break;
        }

        // Verify the magic values were written
        uint8_t magic_data[NUM_MAGIC_REGISTERS] = {0};
        for (size_t i = 0; i < NUM_MAGIC_REGISTERS; ++i)
        {
            errl = nvdimmReadReg(iv_nvdimm,
                                 magic_registers[i],
                                 magic_data[i]);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::writeToMagicRegisters(): "
                          "Failed to read back magic values to verify that "
                          "they were written.");
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }
        }
        if (errl != nullptr)
        {
            break;
        }

        // If either of the magic values stored in magic_data don't match the
        // corresponding expected values in magic_values then an error occurred.
        if (  (magic_data[0] != i_magicValues[0])
           || (magic_data[1] != i_magicValues[1]))
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::writeToMagicRegisters(): "
                     "Magic values read from BPM didn't match expected values "
                     "BPM_MAGIC_REG1 Expected 0x%.2X Actual 0x%.2X "
                     "BPM_MAGIC_REG2 Expected 0x%.2X Actual 0x%.2X",
                     i_magicValues[0], magic_data[0],
                     i_magicValues[1], magic_data[1]);

            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_WRITE_MAGIC_REG
            * @reasoncode       BPM_RC::BPM_WRITE_TO_MAGIC_REG_FAILED
            * @userdata1[0:7]   BPM_MAGIC_REG1 expected value
            * @userdata1[8:15]  BPM_MAGIC_REG1 actual value
            * @userdata1[16:23] BPM_MAGIC_REG2 expected value
            * @userdata1[24:31] BPM_MAGIC_REG2 actual value
            * @userdata2[0:63]  NVDIMM Target HUID associated with this BPM
            * @devdesc          Failed to write values to the magic registers on
            *                   the BPM.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                     BPM_RC::BPM_WRITE_MAGIC_REG,
                                     BPM_RC::BPM_WRITE_TO_MAGIC_REG_FAILED,
                                     TWO_UINT16_TO_UINT32(
                                        TWO_UINT8_TO_UINT16(i_magicValues[0],
                                            magic_data[0]),
                                        TWO_UINT8_TO_UINT16(i_magicValues[1],
                                            magic_data[1])),
                                     TARGETING::get_huid(iv_nvdimm));
            errl->addPartCallout(iv_nvdimm,
                                 HWAS::BPM_PART_TYPE,
                                 HWAS::SRCI_PRIORITY_HIGH);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

        TRACUCOMP(g_trac_bpm, "Bpm::writeToMagicRegisters(): "
                 "Magic values successfully written to BPM "
                 "BPM_MAGIC_REG1 0x%.2X "
                 "BPM_MAGIC_REG2 0x%.2X ",
                 magic_data[0],
                 magic_data[1]);

    } while(0);

    return errl;
}

errlHndl_t Bpm::dumpSegment(uint16_t const i_segmentCode,
                            uint8_t  (&o_buffer)[SEGMENT_SIZE])
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::dumpSegment(): Segment %X",
              getSegmentIdentifier(i_segmentCode));
    assert(i_segmentCode == SEGMENT_B_CODE, "Bpm::dumpSegment(): Only Segment B is supported.");

    errlHndl_t errl = nullptr;

    do {

        errl = disableWriteProtection();
        if (errl != nullptr)
        {
            break;
        }

        // We cannot be in BSL mode when dumping the config segments. Verify we
        // aren't in BSL mode by checking SCAP_STATUS
        scap_status_register_t status;
        errl = nvdimmReadReg(iv_nvdimm,
                             SCAP_STATUS,
                             status.full);
        if (errl != nullptr)
        {
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        if (status.bit.Bpm_Bsl_Mode)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::dumpSegment(): "
                     "BSL Mode is enabled. Attempting to exit BSL mode.");

            // Try to exit BSL mode. This function will exit BSL.
            errl = resetDevice();
            if (errl != nullptr)
            {
                break;
            }

            // Exit update mode if on and write back production magic values.
            errl = exitUpdateMode();
            if (errl != nullptr)
            {
                break;
            }

            errl = nvdimmReadReg(iv_nvdimm,
                                 SCAP_STATUS,
                                 status.full);
            if (errl != nullptr)
            {
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }
            if (status.bit.Bpm_Bsl_Mode)
            {
                TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::dumpSegment(): "
                         "Couldn't dump Segment %X. BSL Mode is enabled.",
                         getSegmentIdentifier(i_segmentCode));

                /*@
                * @errortype
                * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
                * @moduleid         BPM_RC::BPM_DUMP_SEGMENT
                * @reasoncode       BPM_RC::BPM_BSL_MODE_ENABLED
                * @userdata1[0:63]  NVDIMM Target HUID associated with this BPM
                * @devdesc          Couldn't dump segment data because BSL mode
                *                   was enabled.
                * @custdesc         A problem occurred during IPL of the system.
                */
                errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                               BPM_RC::BPM_DUMP_SEGMENT,
                                               BPM_RC::BPM_BSL_MODE_ENABLED,
                                               TARGETING::get_huid(iv_nvdimm));
                errl->addPartCallout(iv_nvdimm,
                                     HWAS::BPM_PART_TYPE,
                                     HWAS::SRCI_PRIORITY_HIGH);
                errl->collectTrace(BPM_COMP_NAME);
                nvdimmAddPage4Regs(iv_nvdimm,errl);
                nvdimmAddVendorLog(iv_nvdimm, errl);

                break;
            }
        }

        // First the NVDIMM MAGIC registers BPM_MAGIC_REG1 and BPM_MAGIC_REG2
        // must be programmed to 0xBA and 0xAB respectively.
        const uint8_t magic_values[NUM_MAGIC_REGISTERS] = {0xBA, 0xAB};
        errl = writeToMagicRegisters(magic_values);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::dumpSegment(): "
                      "Failed to write magic numbers that enable "
                      "reading of segment data.");
            break;
        }

        uint8_t retry = 1;
        // Attempt to switch to the correct page and dump data twice.
        do {
            // Set buffer to be all zeroes.
            memset(&o_buffer, 0, SEGMENT_SIZE);

            // Open this segments page on the BPM.
            errl = switchBpmPage(i_segmentCode);
            if (errl != nullptr)
            {
                break;
            }

            TRACFCOMP(g_trac_bpm, "Bpm::dumpSegment(): "
                     "Dumping Segment %X to buffer.",
                     getSegmentIdentifier(i_segmentCode));

            // Dump the segment data
            bool wrongPage = false;
            for (uint8_t reg = 0; reg < SEGMENT_SIZE; ++reg)
            {
                errl = readViaScapRegister(reg, o_buffer[reg]);
                if (errl != nullptr)
                {
                    break;
                }

                // We can determine if the page switch succeeded based on the
                // first three bytes from regs 0x10-0x12. If Segment B was
                // opened, then 0x10-0x1F is serial number for the BPM.
                // SMART guarantees the first three bytes to be as follows:
                if ((reg == 0x10) && (o_buffer[reg] != 0x53))
                {
                    TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::dumpSegment "
                             "data 0x%.2X at offset 0x%.2x wasn't expected "
                             "value 0x53",
                             o_buffer[reg], reg);
                    wrongPage = true;
                }
                if ((reg == 0x11) && (o_buffer[reg] != 0x46))
                {
                    TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::dumpSegment "
                             "data 0x%.2X at offset 0x%.2x wasn't expected "
                             "value 0x46",
                             o_buffer[reg], reg);
                    wrongPage = true;
                }
                if ((reg == 0x12) && (o_buffer[reg] != 0x52))
                {
                    TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::dumpSegment "
                             "data 0x%.2X at offset 0x%.2x wasn't expected "
                             "value 0x52",
                             o_buffer[reg], reg);
                    wrongPage = true;
                }

                if (wrongPage && (reg == 0x20))
                {
                    break;
                }

            }

            TRACUBIN(g_trac_bpm, "Segment BIN DUMP", o_buffer, SEGMENT_SIZE);

            if ((errl != nullptr) || (wrongPage == false))
            {
                break;
            }

            // Close this segments page on the BPM before making another
            // attempt.
            errl = switchBpmPage(DEFAULT_REG_PAGE);
            if (errl != nullptr)
            {
                break;
            }

        } while(++retry < MAX_RETRY);
        if (errl != nullptr)
        {
            break;
        }
        if (retry >= MAX_RETRY)
        {
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_DUMP_SEGMENT
            * @reasoncode       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT
            * @userdata1        The segment code for the page that failed to
            *                   open.
            * @userdata2        NVDIMM Target HUID associated with this BPM
            * @devdesc          Failed to open the segment page in the given
            *                   amount of retries.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                           BPM_RC::BPM_DUMP_SEGMENT,
                                           BPM_RC::BPM_EXCEEDED_RETRY_LIMIT,
                                           i_segmentCode,
                                           TARGETING::get_huid(iv_nvdimm));
            errl->addPartCallout(iv_nvdimm,
                                 HWAS::BPM_PART_TYPE,
                                 HWAS::SRCI_PRIORITY_HIGH);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            setAttemptAnotherUpdate();
            break;
        }

    } while(0);

        TRACFCOMP(g_trac_bpm, "Bpm::dumpSegment(): "
                 "Closing Segment %X's page.",
                 getSegmentIdentifier(i_segmentCode));

        // Close the Segment page by switching back to the default page.
        errlHndl_t closeSegmentErrl = switchBpmPage(DEFAULT_REG_PAGE);
        if (closeSegmentErrl != nullptr)
        {
            handleMultipleErrors(errl, closeSegmentErrl);
        }

        // Write back the production magic values.
        errlHndl_t magicErrl = writeToMagicRegisters(PRODUCTION_MAGIC_VALUES);
        if (magicErrl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::dumpSegment(): "
                      "Failed to write update mode magic numbers.");
            handleMultipleErrors(errl, magicErrl);
        }

    return errl;
}

errlHndl_t Bpm::mergeSegment(BpmConfigLidImage const i_configImage,
                             uint16_t const i_segmentCode,
                             uint8_t (&o_buffer)[SEGMENT_SIZE])
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::mergeSegment(): Segment %X",
              getSegmentIdentifier(i_segmentCode));
    errlHndl_t errl = nullptr;

    size_t segmentStartOffset = 0;
    auto it = segmentMap.find(i_segmentCode);
    if (it != segmentMap.end())
    {
        segmentStartOffset = it->second;
    }
    else
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::mergeSegment(): "
                 "Couldn't find start offset for Segment %X",
                 getSegmentIdentifier(i_segmentCode));
        assert(false, "Add the missing Segment %X Start Offset to the offset map", getSegmentIdentifier(i_segmentCode));
    }

    TRACFCOMP(g_trac_bpm, "Bpm::mergeSegment(): "
             "Segment %X Start offset: 0x%X",
             getSegmentIdentifier(i_segmentCode),
             segmentStartOffset);

    do {

        const size_t NUMBER_OF_FRAGMENTS = i_configImage.getNumberOfFragments();
        char const * data = reinterpret_cast<char const *>(
                i_configImage.getFirstFragment());

        config_image_fragment_t const * fragment =
            reinterpret_cast<config_image_fragment_t const *>(data);

        TRACUCOMP(g_trac_bpm, "mergeSegment(): "
                 "NUMBER_OF_FRAGMENTS = 0x%.4X", NUMBER_OF_FRAGMENTS);

        for(size_t i = 0; i < NUMBER_OF_FRAGMENTS; ++i)
        {
            // The fragment offsets are given as offsets within the
            // configuration segment data. So, if the fragment offset is less
            // than the starting offset of this segment then the fragment is not
            // relevant to this segment.
            if (fragment->iv_offset < segmentStartOffset)
            {
                // This fragment is not for the segment we are dealing with.
                TRACUCOMP(g_trac_bpm, "mergeSegment(): "
                         "Fragment with offset 0x%.4X not related to "
                         "Segment %X, skipping",
                         fragment->iv_offset,
                         getSegmentIdentifier(i_segmentCode));

                // Move to the next fragment
                data += sizeof(config_image_fragment_t)
                      + fragment->iv_fragmentSize;
                fragment =
                    reinterpret_cast<config_image_fragment_t const *>(data);
                continue;
            }
            // Each segment is 128 bytes in size. So, if the offset given for
            // the fragment is greater than the upper boundry then no more
            // fragments exist for this segment.
            if (fragment->iv_offset >= segmentStartOffset + SEGMENT_SIZE)
            {
                // This fragment and all other fragments afterward are not for
                // this segment.
                TRACUCOMP(g_trac_bpm, "mergeSegment(): "
                         "Fragment with offset 0x%.4X greater than/equal to "
                         "Segment %X ending offset, skipping",
                         fragment->iv_offset,
                         getSegmentIdentifier(i_segmentCode));
                break;
            }

            // The fragment offset may be out of bounds for the buffer so
            // scale it down to be within the buffer size.
            size_t offset = fragment->iv_offset % SEGMENT_SIZE;

            // Overwrite the BPM segment data at the offset specified by the
            // fragment.
            memcpy(&o_buffer[offset],
                   &(fragment->iv_data),
                   fragment->iv_fragmentSize);

            // Move to the next fragment
            data += sizeof(config_image_fragment_t) + fragment->iv_fragmentSize;
            fragment = reinterpret_cast<config_image_fragment_t const *>(data);
        }

        TRACUBIN(g_trac_bpm, "Merged Segment", o_buffer, SEGMENT_SIZE);

    } while(0);

    return errl;
}

errlHndl_t Bpm::eraseSegment(uint16_t i_segmentCode)
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::eraseSegment(): Segment %X",
              getSegmentIdentifier(i_segmentCode));
    errlHndl_t errl = nullptr;

    do {

        payload_t payload;

        size_t segmentStartOffset = 0;
        auto it = segmentMap.find(i_segmentCode);
        if (it != segmentMap.end())
        {
            segmentStartOffset = it->second;
        }
        else
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::mergeSegment(): "
                     "Couldn't find start offset for Segment %X",
                     getSegmentIdentifier(i_segmentCode));
            assert(false, "Add the missing Segment %X Start Offset to the offset map", getSegmentIdentifier(i_segmentCode));
        }
        errl = setupPayload(payload,
                            BSL_ERASE_SEGMENT,
                            BPM_CONFIG_START_ADDRESS + segmentStartOffset);
        if (errl != nullptr)
        {
            break;
        }

        errl = issueCommand(BPM_PASSTHROUGH,
                            payload,
                            WRITE,
                            ERASE_SEGMENT_DELAY);
        if (errl != nullptr)
        {
            break;
        }

        // Wait 1 second for the operation to complete.
        TRACFCOMP(g_trac_bpm, "Bpm::eraseSegment(): "
                  "Erasing Segment %X. "
                  "Waiting 1 second for operation to complete.",
                  getSegmentIdentifier(i_segmentCode));
        nanosleep(1,0);

    } while(0);

    TRACFCOMP(g_trac_bpm, EXIT_MRK"Bpm::eraseSegment(): "
             "Segment %X erase operation completed "
             "%s",
             getSegmentIdentifier(i_segmentCode),
             (errl == nullptr) ? "without errors" : "with errors");

    return errl;
}

errlHndl_t Bpm::writeSegment(uint8_t const (&i_buffer)[SEGMENT_SIZE],
                             uint16_t const i_segmentCode)
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::writeSegment(): Segment %X",
              getSegmentIdentifier(i_segmentCode));
    errlHndl_t errl = nullptr;

    do {

        auto it = segmentMap.find(i_segmentCode);
        size_t segmentStartOffset = 0;
        if (it != segmentMap.end())
        {
            segmentStartOffset = it->second;
        }

        // To update the given segment, we have to send over the data as
        // payloads. Since the max size of a payload's data is 16 bytes, there
        // will be 8 payloads sent to update a given segment because each
        // segment is 128 bytes.
        for (size_t offset = 0;
             offset < SEGMENT_SIZE;
             offset += MAX_PAYLOAD_DATA_SIZE)
        {
            // Construct a payload for the data at this offset up to the
            // MAX_PAYLOAD_DATA_SIZE.
            payload_t payload;
            // Each segment is 128 bytes and the segment start addresses
            // are their relative position to BPM_CONFIG_START_ADDRESS. To
            // arrive at the correct address offset for this data we must
            // calculate the addressOffset in the following way.
            uint16_t addressOffset = BPM_CONFIG_START_ADDRESS
                                   + segmentStartOffset
                                   + offset;
            errl = setupPayload(payload,
                                BSL_RX_DATA_BLOCK,
                                addressOffset,
                                &i_buffer[offset],
                                MAX_PAYLOAD_DATA_SIZE);
            if (errl != nullptr)
            {
                break;
            }

            if (addressOffset % 0x20 == 0)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::writeSegment(): "
                         "Writing to address offset 0x%.4X. "
                         "Config bytes written: 0x%X; Remaining: 0x%X",
                         addressOffset,
                         offset, (SEGMENT_SIZE - offset));
            }

            // Attempt to write the payload using a retry loop.
            errl = blockWrite(payload);
            if (errl != nullptr)
            {
                break;
            }
        }
        if (errl != nullptr)
        {
            break;
        }

    } while(0);

    TRACFCOMP(g_trac_bpm, EXIT_MRK"Bpm::writeSegment(): "
             "Segment %X write and verification completed "
             "%s",
             getSegmentIdentifier(i_segmentCode),
             (errl == nullptr) ? "without errors" : "with errors");

    return errl;
}

errlHndl_t Bpm::preprocessSegments(BpmConfigLidImage const i_configImage)
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::preprocessSegments()");
    errlHndl_t errl = nullptr;

    do {

        if (iv_attemptAnotherUpdate && iv_segmentDMerged && iv_segmentBMerged)
        {
            // The segment data has already been merged with the flash image
            // data. Doing it again has the potential to fail depending on where
            // the last update attempt failed.
            TRACFCOMP(g_trac_bpm, "Bpm::preprocessSegments(): "
                      "Segment data was merged in a previous update attempt, "
                      "skipping preprocessing and using existing data.");
            break;
        }

        // Merge the fragments for D with the data from the BPM. For D, this
        // will just populate the empty segment with the data from the flash
        // image.
        if (!iv_segmentDMerged)
        {
            errl = mergeSegment(i_configImage, SEGMENT_D_CODE, iv_segmentD);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::preprocessSegments(): "
                          "Failed to merge Segment D.");
                break;
            }
            iv_segmentDMerged = true;
        }
        else
        {
            TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::preprocessSegments(): "
                     "Segment %X has been merged already. Skipping merge...",
                     getSegmentIdentifier(SEGMENT_D_CODE));
        }

        // Merge the fragments for B with the data from the BPM.
        if (!iv_segmentBMerged)
        {
            // Dump the segment into a buffer. This is only necessary for
            // segment B as segment D comes straight from the flash image file.
            errl = dumpSegment(SEGMENT_B_CODE, iv_segmentB);
            if (errl != nullptr)
            {
                break;
            }

            errl = mergeSegment(i_configImage, SEGMENT_B_CODE, iv_segmentB);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::preprocessSegments(): "
                          "Failed to merge Segment B.");
                break;
            }
            iv_segmentBMerged = true;
        }
        else
        {
            TRACFCOMP(g_trac_bpm, INFO_MRK"Bpm::preprocessSegments(): "
                     "Segment %X has been merged already. Skipping merge...",
                     getSegmentIdentifier(SEGMENT_B_CODE));
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::getResponse(uint8_t * const o_responseData,
                            uint8_t   const i_responseSize)
{
    TRACUCOMP(g_trac_bpm, ENTER_MRK"Bpm::getResponse()");

    errlHndl_t errl = nullptr;
    memset(o_responseData, 0xFF, i_responseSize);

    do {

        // Get the result from the BPM.
        // First clear the error status register
        errl = nvdimmWriteReg(iv_nvdimm,
                              BPM_REG_ERR_STATUS,
                              0x00);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::getResponse(): "
                      "Failed to clear error status register");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Set the payload length
        // The 4 header bytes plus 2 CRC bytes make up the other data size in
        // the response payload.
        const uint8_t RESPONSE_PAYLOAD_OTHER_DATA_SIZE = 6;
        uint8_t responsePayloadSize = RESPONSE_PAYLOAD_OTHER_DATA_SIZE
                                    + i_responseSize;

        errl = nvdimmWriteReg(iv_nvdimm,
                              BPM_PAYLOAD_LENGTH,
                              responsePayloadSize);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::getResponse(): "
                      "Failed to set payload length");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Setup the command status register
        command_status_register_t commandStatus;
        commandStatus.bits.Bsp_Cmd_In_Progress = 1;
        commandStatus.bits.Operator_Type = READ;
        errl = nvdimmWriteReg(iv_nvdimm,
                              BPM_CMD_STATUS,
                              commandStatus.value);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::getResponse(): "
                      "Failed to setup command status register");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Setup command type.
        errl = nvdimmWriteReg(iv_nvdimm,
                              BPM_REG_CMD,
                              BPM_PASSTHROUGH);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::getResponse(): "
                      "Failed to setup command type.");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        errl = waitForCommandStatusBitReset(commandStatus);
        if (errl != nullptr)
        {
            break;
        }

        // Read out the response payload.
        payload_t responsePayload;

        for (size_t i = 0; i < responsePayloadSize; ++i)
        {
            uint8_t data = 0;
            errl = nvdimmReadReg(iv_nvdimm,
                                (BPM_REG_PAYLOAD_START + (i * sizeof(uint8_t))),
                                data);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::getResponse(): "
                          "Failed to read response payload");
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }

            responsePayload.push_back(data);
        }
        if (errl != nullptr)
        {
            break;
        }

        // Verify the data from the response was good.
        uint8_t* responseIterator = responsePayload.data();
        uint16_t responseCrc = *(reinterpret_cast<uint16_t *>
                (&responseIterator[PAYLOAD_HEADER_SIZE + i_responseSize]));
        // The BPM is going to give the response CRC in LE. So convert it to BE.
        responseCrc = le16toh(responseCrc);
        uint16_t expectedCrc = crc16_calc(responseIterator,
                                          PAYLOAD_HEADER_SIZE + i_responseSize);
        if (responseCrc != expectedCrc)
        {
            memset(o_responseData, 0xFF, i_responseSize);
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::getResponse(): "
                      "Response CRC verification failed. "
                      "Received invalid data from BPM.");
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_GET_RESPONSE
            * @reasoncode       BPM_RC::BPM_RESPONSE_CRC_MISMATCH
            * @userdata1[00:31] Expected Response CRC (in Big Endian)
            * @userdata1[32:63] Actual Response CRC   (in Big Endian)
            * @userdata2        NVDIMM Target HUID associated with this BPM
            * @devdesc          The response CRC calculated by the BPM didn't
            *                   match the CRC calculated by hostboot.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                           BPM_RC::BPM_GET_RESPONSE,
                                           BPM_RC::BPM_RESPONSE_CRC_MISMATCH,
                                           TWO_UINT32_TO_UINT64(expectedCrc,
                                               responseCrc),
                                           TARGETING::get_huid(iv_nvdimm));
            errl->addPartCallout(iv_nvdimm,
                                 HWAS::BPM_PART_TYPE,
                                 HWAS::SRCI_PRIORITY_HIGH);
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

        // Write the data to the output buffer
        for (size_t i = 0; i < i_responseSize; ++i)
        {
            // Only copy the response data from the payload to the output buffer
            o_responseData[i] = responsePayload[i + PAYLOAD_HEADER_SIZE];
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::verifyBlockWrite(payload_t  i_payload,
                            uint8_t    i_dataLength,
                            uint8_t  & o_status)
{
    errlHndl_t errl = nullptr;
    // Assume a bad status.
    o_status = 0xFF;

    do {

        // Pull the address to verify out of the payload. It was inserted in
        // little endian form so it needs to be converted back to big endian
        // because setupPayload expects an address in big endian.
        uint16_t address = getPayloadAddressBE(i_payload);

        // The data section of the payload is organized in the following way:
        // 2 bytes: uint16_t size of data to verify in little endian format
        // 2 bytes: CRC of the data to be verified on the BPM in little endian.
        const size_t VERIFY_BLOCK_PAYLOAD_DATA_SIZE = 4;
        uint8_t data[VERIFY_BLOCK_PAYLOAD_DATA_SIZE] = {0};

        // Since the data length is stored as uint16_t but the length we deal
        // with is uint8_t we can easily convert this to little endian by
        // storing our uint8_t data length in the first index of the array and
        // leaving the next index 0.
        data[0] = i_dataLength;

        // Calculate the uint16_t CRC for the data that was written to the BPM.
        // The BPM will compare its calculated CRC with this one to verify if
        // the block was written correctly.
        uint16_t crc = htole16(crc16_calc(&i_payload[PAYLOAD_DATA_START_INDEX],
                                  i_dataLength));

        memcpy(&data[2], &crc, sizeof(uint16_t));

        payload_t verifyPayload;
        errl = setupPayload(verifyPayload,
                            BSL_VERIFY_BLOCK,
                            address,
                            data,
                            VERIFY_BLOCK_PAYLOAD_DATA_SIZE);
        if (errl != nullptr)
        {
            break;
        }

        // Issue the command to the BPM.
        errl = issueCommand(BPM_PASSTHROUGH,
                            verifyPayload,
                            WRITE,
                            NO_DELAY_EXTERNAL_RESPONSE);
        if (errl != nullptr)
        {
            break;
        }

        errl = getResponse(&o_status, sizeof(uint8_t));
        if (errl != nullptr)
        {
            break;
        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::blockWrite(payload_t i_payload)
{
    assert(i_payload[PAYLOAD_COMMAND_INDEX] == BSL_RX_DATA_BLOCK,
          "Bpm::blockWrite(): "
          "Can only write BSL_RX_DATA_BLOCK commands");

    errlHndl_t errl = nullptr;
    uint8_t retry = 0;

    // Get the payload address for trace output.
    uint16_t payloadAddress = getPayloadAddressBE(i_payload);

    // Any status from verifyBlockWrite that is non-zero is considered a
    // fail. So, assume a fail and check.
    uint8_t wasVerified = 0xFF;
    do {


        // Since the write command has its response packet checked within the
        // issueCommand() function we must attempt to retry the write if we get
        // a bad response from the BPM.
        errl = blockWriteRetry(i_payload);
        if (errl != nullptr)
        {
            break;
        }

        // Sleep for 0.001 second
        nanosleep(0, 1 * NS_PER_MSEC);

        uint8_t dataLength = i_payload[PAYLOAD_HEADER_DATA_LENGTH_INDEX]
                           - PAYLOAD_HEADER_SIZE;
        errl = verifyBlockWrite(i_payload,
                                dataLength,
                                wasVerified);
        if (  (errl != nullptr)
           && (errl->reasonCode() == BPM_RC::BPM_RESPONSE_CRC_MISMATCH)
           && ((retry + 1) < MAX_RETRY))
        {
            // Delete the retryable error and continue
            TRACFCOMP(g_trac_bpm, "Bpm::blockWrite(): "
                     "Encountered a retryable error. Delete and continue.");
            delete errl;
            errl = nullptr;
        }
        else if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::blockWrite(): "
                     "BSL_VERIFY_BLOCK failed for address 0x%.4X. "
                     "A non-retryable error occurred on attempt %d/%d",
                     payloadAddress,
                     (retry + 1),
                     MAX_RETRY);
            // A non-retryable error occurred. Break from retry loop.
            break;
        }

        if (wasVerified != 0)
        {
            TRACUCOMP(g_trac_bpm, "Bpm::blockWrite(): "
                     "BSL_VERIFY_BLOCK failed for address 0x%.4X. "
                     "Attempt %d/%d",
                     payloadAddress,
                     (retry + 1),
                     MAX_RETRY);
        }
        else
        {
            // Write verified successfully, stop retries.
            break;
        }

    } while (++retry < MAX_RETRY);
    if ((errl == nullptr) && (retry >= MAX_RETRY) && (wasVerified != 0))
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::blockWrite(): "
                 "Failed to write payload data to BPM after %d retries.",
                 MAX_RETRY);
        /*@
        * @errortype
        * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
        * @moduleid         BPM_RC::BPM_BLOCK_WRITE
        * @reasoncode       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT
        * @userdata1[0:63]  NVDIMM Target HUID associated with this BPM
        * @devdesc          The block of data to be written to the BPM
        *                   failed to write successfully in the given number
        *                   of retries.
        * @custdesc         A problem occurred during IPL of the system.
        */
        errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                       BPM_RC::BPM_BLOCK_WRITE,
                                       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT,
                                       TARGETING::get_huid(iv_nvdimm));
        errl->addPartCallout(iv_nvdimm,
                             HWAS::BPM_PART_TYPE,
                             HWAS::SRCI_PRIORITY_HIGH);
        errl->collectTrace(BPM_COMP_NAME);
        nvdimmAddVendorLog(iv_nvdimm, errl);

    }

    if (errl != nullptr)
    {
        // Change the state of iv_attemptAnotherUpdate. This will signal
        // another update attempt or cease further attempts.
        setAttemptAnotherUpdate();
    }

    return errl;
}

errlHndl_t Bpm::blockWriteRetry(payload_t i_payload)
{
    assert(i_payload[PAYLOAD_COMMAND_INDEX] == BSL_RX_DATA_BLOCK,
          "Bpm::blockWriteRetry(): "
          "Can only retry BSL_RX_DATA_BLOCK commands");

    errlHndl_t errl = nullptr;
    uint8_t retry = 0;

    // Get the payload address for trace output.
    uint16_t payloadAddress = getPayloadAddressBE(i_payload);

    do {

        // Send the payload data over as a pass-through command. The response
        // will be checked internally.
        errl = issueCommand(BPM_PASSTHROUGH, i_payload, WRITE);
        if (errl == nullptr)
        {
            // Command was a success. Stop retries.
            break;
        }

        if (  (errl != nullptr)
           && (errl->reasonCode() == BPM_RC::BPM_RESPONSE_CRC_MISMATCH)
           && ((retry + 1) < MAX_RETRY))
        {
            // Delete the retryable error and continue
            TRACFCOMP(g_trac_bpm, "Bpm::blockWriteRetry(): "
                     "Encountered a retryable error. Delete and continue.");
            delete errl;
            errl = nullptr;
        }
        else if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::blockWriteRetry(): "
                     "BSL_RX_DATA_BLOCK failed for address 0x%.4X. "
                     "A non-retryable error occurred on attempt %d/%d",
                     payloadAddress,
                     (retry + 1),
                     MAX_RETRY);
            // A non-retryable error occurred. Break from retry loop.
            break;
        }

    } while (++retry < MAX_RETRY);
    if ((errl == nullptr) && (retry >= MAX_RETRY))
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::blockWriteRetry(): "
                 "Failed to write payload data to BPM after %d retries.",
                 MAX_RETRY);
        /*@
        * @errortype
        * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
        * @moduleid         BPM_RC::BPM_RETRY_BLOCK_WRITE
        * @reasoncode       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT
        * @userdata1[0:63]  NVDIMM Target HUID associated with this BPM
        * @devdesc          The block of data to be written to the BPM
        *                   failed to write successfully in the given number
        *                   of retries.
        * @custdesc         A problem occurred during IPL of the system.
        */
        errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                       BPM_RC::BPM_RETRY_BLOCK_WRITE,
                                       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT,
                                       TARGETING::get_huid(iv_nvdimm));
        errl->addPartCallout(iv_nvdimm,
                             HWAS::BPM_PART_TYPE,
                             HWAS::SRCI_PRIORITY_HIGH);
        errl->collectTrace(BPM_COMP_NAME);
        nvdimmAddPage4Regs(iv_nvdimm,errl);
        nvdimmAddVendorLog(iv_nvdimm, errl);

    }

    if (errl != nullptr)
    {
        // Change the state of iv_attemptAnotherUpdate. This will signal
        // another update attempt or cease further attempts.
        setAttemptAnotherUpdate();
    }

    return errl;
}

errlHndl_t Bpm::waitForCommandStatusBitReset(
    command_status_register_t i_commandStatus)
{
    errlHndl_t errl = nullptr;

    do {
        // Wait until the COMMAND_IN_PROGRESS bit is reset
        errl = nvdimmReadReg(iv_nvdimm,
                             BPM_CMD_STATUS,
                             i_commandStatus.value);
        if (errl != nullptr)
        {
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        // Give the BPM 20 seconds to complete any given command before we time
        // out and cancel the update procedure.
        int retry = 20 * MS_PER_SEC;

        while (i_commandStatus.bits.Bsp_Cmd_In_Progress)
        {
            nanosleep(0, 1 * NS_PER_MSEC);
            errl = nvdimmReadReg(iv_nvdimm,
                                 BPM_CMD_STATUS,
                                 i_commandStatus.value);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::waitForCommandStatusBitReset(): "
                          "Failed to read BPM_CMD_STATUS register");
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }

            if (--retry <= 0)
            {
                TRACFCOMP(g_trac_bpm, ERR_MRK
                         "BPM::waitForCommandStatusBitReset(): "
                         "BSP_CMD_IN_PROGRESS bit has not reset in allotted "
                         "number of retries. Cancel update procedure");
                /*@
                * @errortype
                * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
                * @moduleid         BPM_RC::BPM_WAIT_FOR_CMD_BIT_RESET
                * @reasoncode       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT
                * @userdata1[0:63]  NVDIMM Target HUID associated with this BPM
                * @devdesc          The command status bit failed to reset in
                *                   the given number of retries.
                * @custdesc         A problem occurred during IPL of the system.
                */
                errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                           BPM_RC::BPM_WAIT_FOR_CMD_BIT_RESET,
                                           BPM_RC::BPM_EXCEEDED_RETRY_LIMIT,
                                           TARGETING::get_huid(iv_nvdimm));
                errl->addPartCallout(iv_nvdimm,
                                     HWAS::BPM_PART_TYPE,
                                     HWAS::SRCI_PRIORITY_HIGH);
                errl->collectTrace(BPM_COMP_NAME);
                nvdimmAddPage4Regs(iv_nvdimm,errl);
                nvdimmAddVendorLog(iv_nvdimm, errl);
                break;
            }

        }
        if (errl != nullptr)
        {
            break;
        }

        // Check for error
        if (i_commandStatus.bits.Error_Flag)
        {
            uint8_t error = 0;
            errl = nvdimmReadReg(iv_nvdimm,
                                 BPM_REG_ERR_STATUS,
                                 error);
            if (errl != nullptr)
            {
                TRACFCOMP(g_trac_bpm, "Bpm::waitForCommandStatusBitReset(): "
                          "Failed to read BPM_REG_ERR_STATUS");
                errl->collectTrace(BPM_COMP_NAME);
                break;
            }

            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::waitForCommandStatusBitReset(): "
                      "BPM_CMD_STATUS Error Flag is set");
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_WAIT_FOR_CMD_BIT_RESET
            * @reasoncode       BPM_RC::BPM_CMD_STATUS_ERROR_BIT_SET
            * @userdata1[0:7]   Error status code returned by BPM
            * @userdata2[0:63]  NVDIMM Target HUID associated with this BPM
            * @devdesc          The command status register returned an error.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                       BPM_RC::BPM_WAIT_FOR_CMD_BIT_RESET,
                                       BPM_RC::BPM_CMD_STATUS_ERROR_BIT_SET,
                                       error,
                                       TARGETING::get_huid(iv_nvdimm));
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;

        }

    } while(0);

    return errl;
}

errlHndl_t Bpm::verifyGoodBpmState()
{
    errlHndl_t errl = nullptr;
    int retry = 100;
    scap_status_register_t status;
    const uint8_t BPM_PRESENT_AND_ENABLED = 0x11;

    while (retry > 0)
    {

        errl = nvdimmReadReg(iv_nvdimm,
                             SCAP_STATUS,
                             status.full);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::verifyGoodBpmState(): "
                      "Failed to read SCAP_STATUS to determine "
                      "state of BPM.");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        if ((status.full & 0xFF) == BPM_PRESENT_AND_ENABLED)
        {
            // BPM is present and enabled. Stop retries.
            break;
        }

        --retry;
        nanosleep(0, 1 * NS_PER_MSEC);
    }
    if (retry <= 0)
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::verifyGoodBpmState(): "
                  "BPM failed to become present and enabled "
                  "in 100 retries.");
        /*@
        * @errortype
        * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
        * @moduleid         BPM_RC::BPM_VERIFY_GOOD_BPM_STATE
        * @reasoncode       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT
        * @userdata1        NVDIMM Target HUID associated with this BPM
        * @userdata2        SCAP_STATUS register contents. See nvdimm.H
        *                   for bits associated with this register.
        * @devdesc          The BPM did not become present and enabled
        *                   in given number of retries.
        * @custdesc         A problem occurred during IPL of the system.
        */
        errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                   BPM_RC::BPM_VERIFY_GOOD_BPM_STATE,
                                   BPM_RC::BPM_EXCEEDED_RETRY_LIMIT,
                                   TARGETING::get_huid(iv_nvdimm),
                                   status.full);
        errl->collectTrace(BPM_COMP_NAME);
        errl->addPartCallout(iv_nvdimm,
                             HWAS::BPM_PART_TYPE,
                             HWAS::SRCI_PRIORITY_HIGH);
        nvdimmAddPage4Regs(iv_nvdimm,errl);
        nvdimmAddVendorLog(iv_nvdimm, errl);
    }

    return errl;
}

errlHndl_t Bpm::waitForBusyBit()
{
    errlHndl_t errl = nullptr;
    int retry = 10;
    scap_status_register_t status;

    while (retry > 0)
    {

        errl = nvdimmReadReg(iv_nvdimm,
                             SCAP_STATUS,
                             status.full);
        if (errl != nullptr)
        {
            TRACFCOMP(g_trac_bpm, "Bpm::waitForBusyBit(): "
                      "Failed to read from SCAP_STATUS to determine "
                      "state of Busy bit.");
            errl->collectTrace(BPM_COMP_NAME);
            break;
        }

        if (!status.bit.Busy)
        {
            // SCAP Register is no longer busy. Stop retries.
            break;
        }

        if (retry <= 0)
        {
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::waitForBusyBit(): "
                      "SCAP_STATUS Busy bit failed to reset to 0 "
                      "in 10 retries.");
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_WAIT_FOR_BUSY_BIT_RESET
            * @reasoncode       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT
            * @userdata1[0:63]  NVDIMM Target HUID associated with this BPM
            * @devdesc          The SCAP status register busy bit failed to
            *                   reset in given number of retries.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                       BPM_RC::BPM_WAIT_FOR_BUSY_BIT_RESET,
                                       BPM_RC::BPM_EXCEEDED_RETRY_LIMIT,
                                       TARGETING::get_huid(iv_nvdimm));
            errl->collectTrace(BPM_COMP_NAME);
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

        --retry;
        nanosleep(0, 2 * NS_PER_MSEC);
    }

    return errl;
}

errlHndl_t Bpm::runConfigUpdates(BpmConfigLidImage i_configImage)
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::runConfigUpdates()");
    errlHndl_t errl = nullptr;

    do {

        // Before the entering BSL mode, we must do preprocessing prior to the
        // config part of the update. Segment B needs to be dumped from the
        // BPM into a buffer and then the config data from the image needs to be
        // inserted into it. To dump segment data, it is required to have
        // working firmware which will not be the case during BSL mode.
        errl = preprocessSegments(i_configImage);
        if (errl != nullptr)
        {
            break;
        }

        // Enter Update mode
        errl = enterUpdateMode();
        if (errl != nullptr)
        {
            break;
        }

        // Verify in Update mode
        errl = inUpdateMode();
        if (errl != nullptr)
        {
            break;
        }

        // Enter Bootstrap Loader (BSL) mode to perform firmware update
        errl = enterBootstrapLoaderMode();
        if (errl != nullptr)
        {
            break;
        }

        // Unlock the device. This is a BSL command so we must already be in
        // BSL mode to execute it.
        errl = unlockDevice();
        if (errl != nullptr)
        {
            break;
        }

        // Perform the configuration data segment updates.
        // As of BSL 1.4 this is done via the BSL interface instead of SCAP
        // registers.
        errl = updateConfig();
        if (errl != nullptr)
        {
            // We are returning with an error. Since the error is from the
            // config part of the updates it's best to erase the firmware on the
            // BPM so that updates will be attempted on it in the future.
            // Because there isn't a way to determine the validity of the config
            // section on the BPM we're completely reliant on what the firmware
            // version reports to decide if we need to update or not. If we see
            // that the firmware version matches the image but for some reason
            // the config data wasn't updated properly we could believe we
            // updated successfully when, in fact, we just left the BPM in a bad
            // state.
            if (  (iv_firmwareStartAddress == MAIN_PROGRAM_ADDRESS)
               || (iv_firmwareStartAddress == MAIN_PROGRAM_ADDRESS_ALT))
            {
                payload_t payload;
                errlHndl_t fwEraseErrl = setupPayload(payload,
                                                      BSL_MASS_ERASE,
                                                      iv_firmwareStartAddress);
                if (fwEraseErrl != nullptr)
                {
                    handleMultipleErrors(errl, fwEraseErrl);
                    break;
                }

                fwEraseErrl = issueCommand(BPM_PASSTHROUGH,
                                    payload,
                                    WRITE,
                                    ERASE_FIRMWARE_DELAY);
                if (fwEraseErrl != nullptr)
                {
                    handleMultipleErrors(errl, fwEraseErrl);
                    break;
                }

                TRACFCOMP(g_trac_bpm, "Bpm::updateFirmware(): "
                          "Performing BSL_MASS_ERASE on BPM to force full "
                          "update on any subsequent attempt. Sleep for 5 "
                          "seconds.");
                longSleep(5);
            }
            break;
        }

    } while(0);

    // Reset the device. This will exit BSL mode.
    errlHndl_t exitErrl = resetDevice();
    if (exitErrl != nullptr)
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runConfigUpdates(): "
                  "Failed to reset the device");
        handleMultipleErrors(errl, exitErrl);
    }

    // Exit update mode
    exitErrl = exitUpdateMode();
    if (exitErrl != nullptr)
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runConfigUpdates(): "
                  "Failed to exit update mode");
        handleMultipleErrors(errl, exitErrl);
    }


    return errl;
}

errlHndl_t Bpm::runFirmwareUpdates(BpmFirmwareLidImage i_image)
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::runFirmwareUpdates()");
    errlHndl_t errl = nullptr;

    do {

        // Enter Update mode
        errl = enterUpdateMode();
        if (errl != nullptr)
        {
            break;
        }

        // Verify in Update mode
        errl = inUpdateMode();
        if (errl != nullptr)
        {
            break;
        }

        // Enter Bootstrap Loader (BSL) mode to perform firmware update
        errl = enterBootstrapLoaderMode();
        if (errl != nullptr)
        {
            break;
        }

        // Unlock the device. This is a BSL command so we must already be in
        // BSL mode to execute it.
        errl = unlockDevice();
        if (errl != nullptr)
        {
            break;
        }

        // Run Firmware Update
        errl = updateFirmware(i_image);
        if (errl != nullptr)
        {
            break;
        }

        TRACFCOMP(g_trac_bpm, "Bpm::runFirmwareUpdates(): "
                 "Perform final CRC check on entire BPM flash to load "
                 "new firmware.");

        errl = checkFirmwareCrc();
        if (errl != nullptr)
        {
            setAttemptAnotherUpdate();
            TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm:: runFirmwareUpdates(): "
                     "Final CRC check failed. %s ",
                     (iv_attemptAnotherUpdate == false) ?
                     "Attempt another update..."
                     : "Attempts to update the BPM have failed. Firmware will not load.");
            break;
        }

    } while(0);

    // Reset the device. This will exit BSL mode.
    errlHndl_t exitErrl = resetDevice();
    if (exitErrl != nullptr)
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runFirmwareUpdates(): "
                  "Failed to reset the device");
        handleMultipleErrors(errl, exitErrl);
    }

    // Exit update mode
    exitErrl = exitUpdateMode();
    if (exitErrl != nullptr)
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::runFirmwareUpdates(): "
                  "Failed to exit update mode");
        handleMultipleErrors(errl, exitErrl);
    }

    return errl;
}

errlHndl_t Bpm::checkFirmwareCrc()
{
    TRACFCOMP(g_trac_bpm, ENTER_MRK"Bpm::checkFirmwareCrc()");
    errlHndl_t errl = nullptr;

    // The COMMAND_CRC_CHECK would return a 3 byte response in the following
    // format:
    //
    //  ========================================================================
    //          [Status Code]         [Computed_CRC_Lo] [Computed_CRC_Hi]
    //  ========================================================================
    //  BSL_LOCKED                          0x00             0x00
    //  PARAMETER_ERROR                     0x00             0x00
    //  MAIN_FW_NOT_SUPPORT_CRC_CHECK       0x00             0x00
    //  MEMORY_WRITE_CHECK_FAILED           CRC_Low          CRC_Hi
    //  WRITE_FORBIDDEN                     CRC_Low          CRC_Hi
    //  VERIFY_MISMATCH                     CRC_Low          CRC_Hi
    //  SUCCESSFUL_OPERATION                CRC_Low          CRC_Hi
    //
    //  For status codes BSL_LOCKED, PARAMETER_ERROR, and
    //  MAIN_FW_NOT_SUPPORT_CRC_CHECK the response CRC values are considered
    //  as DONT CARE.
    //
    //  For the remainder of the status codes the CRC values are the
    //  computed CRC of the image.
    //
    //  For SUCCESSFUL_OPERATION, the RESET_VECTOR was written.
    //  See bpm_update.H for more info on the status codes
    const uint8_t CRC_CHECK_RESPONSE_SIZE = 3;
    uint8_t responseData[CRC_CHECK_RESPONSE_SIZE] = {0};

    do {

         TRACFCOMP(g_trac_bpm, "Bpm::checkFirmwareCrc(): "
                  "Performing final CRC check.");
         payload_t crcPayload;
         errl = setupPayload(crcPayload,
                             BSL_CRC_CHECK,
                             iv_firmwareStartAddress);
         if (errl != nullptr)
         {
             break;
         }

         errl = issueCommand(BPM_PASSTHROUGH,
                             crcPayload,
                             WRITE,
                             NO_DELAY_EXTERNAL_RESPONSE);
         if (errl != nullptr)
         {
             break;
         }

        // Wait 10 seconds for the CRC check to complete.
        TRACFCOMP(g_trac_bpm, "Bpm::checkFirmwareCrc(): "
                 "Allow CRC check to complete on BPM by waiting 10 seconds.");
        longSleep(10);

        errl = getResponse(responseData, CRC_CHECK_RESPONSE_SIZE);
        if (errl != nullptr)
        {
            break;
        }

        TRACFCOMP(g_trac_bpm, "Bpm::checkFirmwareCrc(): "
                  "Response Packet CRC check status = 0x%X, CRC_Low = 0x%X, "
                  "CRC_Hi = 0x%X",
                  responseData[0],
                  responseData[1],
                  responseData[2]);

        if (responseData[0] != SUCCESSFUL_OPERATION)
        {
            /*@
            * @errortype
            * @severity         ERRORLOG::ERRL_SEV_PREDICTIVE
            * @moduleid         BPM_RC::BPM_CHECK_FIRMWARE_CRC
            * @reasoncode       BPM_RC::BPM_FIRMWARE_CRC_VERIFY_FAILURE
            * @userdata1[0:7]   CRC check response status code. See bpm_update.H
            * @userdata1[8:15]  CRC low byte
            * @userdata1[16:23] CRC high byte
            * @userdata2[0:63]  NVDIMM Target HUID associated with this BPM
            * @devdesc          The firmware CRC check failed. Cross check the
            *                   CRC check response status code for more details.
            * @custdesc         A problem occurred during IPL of the system.
            */
            errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_PREDICTIVE,
                                       BPM_RC::BPM_CHECK_FIRMWARE_CRC,
                                       BPM_RC::BPM_FIRMWARE_CRC_VERIFY_FAILURE,
                                       FOUR_UINT8_TO_UINT32(responseData[0],
                                                             responseData[1],
                                                             responseData[2],
                                                             0),
                                       TARGETING::get_huid(iv_nvdimm));
            nvdimmAddPage4Regs(iv_nvdimm,errl);
            nvdimmAddVendorLog(iv_nvdimm, errl);
            break;
        }

    } while(0);

    if (errl != nullptr)
    {
        TRACFCOMP(g_trac_bpm, ERR_MRK"Bpm::checkFirmwareCrc(): "
                  "Error occurred during BPM Firmware CRC check. "
                  "Firmware image will not load on BPM and update must be "
                  "attempted again.");
        errl->collectTrace(BPM_COMP_NAME);
    }

    return errl;
}

/**
 *  @brief Helper function to handle two potential errors that might occur in a
 *         function that only returns a single error log. If the return error is
 *         not nullptr then the second error will be linked to it and committed
 *         if this is the final update attempt. Otherwise, it will be deleted
 *         since the update procedure will occur again and may be successful.
 *         If the return error is nullptr then the return error will point to
 *         the second's error and the second error will point to nullptr.
 *
 *  @param[in/out]      io_returnErrl   A pointer to the error that would be
 *                                      returned by the function that called
 *                                      this one. If nullptr, then it will be
 *                                      set point to the secondary error and
 *                                      that error will become nullptr.
 *
 *  @param[in/out]     io_secondErrl    The secondary error that occurred which
 *                                      in addition to the usual returned error.
 */
void Bpm::handleMultipleErrors(errlHndl_t& io_returnErrl,
                               errlHndl_t& io_secondErrl)
{
    if (iv_updateAttempted && (io_returnErrl != nullptr))
    {
        io_secondErrl->plid(io_returnErrl->plid());
        TRACFCOMP(g_trac_bpm, "Committing second error eid=0x%X with plid of "
                 "returned error: 0x%X",
                 io_secondErrl->eid(),
                 io_returnErrl->plid());
        io_secondErrl->collectTrace(BPM_COMP_NAME);
        io_secondErrl->addPartCallout(iv_nvdimm,
                                      HWAS::BPM_PART_TYPE,
                                      HWAS::SRCI_PRIORITY_HIGH);
        ERRORLOG::errlCommit(io_secondErrl, BPM_COMP_ID);
    }
    else if (io_returnErrl == nullptr)
    {
        io_returnErrl = io_secondErrl;
        io_secondErrl = nullptr;
    }
    else
    {
        // Another update attempt will be made, delete this secondary error.
        delete io_secondErrl;
        io_secondErrl = nullptr;
    }
}

uint16_t Bpm::crc16_calc(const void* i_ptr, int i_size)
{
    uint16_t crc = 0xFFFF;
    const uint8_t* data = reinterpret_cast<const uint8_t*>(i_ptr);

    while (--i_size >= 0)
    {
        crc = crc ^ *(data++) << 8;
        for (size_t i = 0; i < 8; ++i)
        {
            if (crc & 0x8000)
            {
                crc = crc << 1 ^ 0x1021;
            }
            else
            {
                crc = crc << 1;
            }
        }
    }

    return (crc & 0xFFFF);
}

}; // End of BPM namespace
}; // End of NVDIMM namespace
OpenPOWER on IntegriCloud