summaryrefslogtreecommitdiffstats
path: root/EdkCompatibilityPkg/Sample/Tools/Source/GenFvImage/GenFvImageLib.c
blob: 3c591841eddb023c5f1c0d12de30803163f64fde (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
/*++

Copyright (c) 2004 - 2007, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

  GenFvImageLib.c

Abstract:

  This file contains functions required to generate a Firmware Volume.

--*/

//
// Include files
//
#include "GenFvImageLib.h"
#include "GenFvImageLibInternal.h"
#include <string.h>
#include EFI_GUID_DEFINITION (PeiPeCoffLoader)
#include "EfiFirmwareFileSystem.h"
#include "EfiWorkingBlockHeader.h"
#include "EfiVariable.h"
#include <io.h>
#include <assert.h>
#include "CommonLib.h"
#include "FvLib.h"
#include "EfiImage.h"
#include "crc32.h"
#include "EfiUtilityMsgs.h"
#include EFI_GUID_DEFINITION (FirmwareFileSystem)
#include EFI_GUID_DEFINITION (FirmwareFileSystem2)

//
// Define the PE/COFF loader
//
extern EFI_PEI_PE_COFF_LOADER_PROTOCOL  mPeCoffLoader;

//
// Local function prototypes
//
EFI_STATUS
GetPe32Info (
  IN UINT8                  *Pe32,
  OUT UINT32                *EntryPoint,
  OUT UINT32                *BaseOfCode,
  OUT UINT16                *MachineType
  );

//
// Local function implementations.
//
#if (PI_SPECIFICATION_VERSION < 0x00010000)
EFI_GUID  FfsGuid = EFI_FIRMWARE_FILE_SYSTEM_GUID;
#else
EFI_GUID  FfsGuid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
#endif

EFI_GUID  DefaultFvPadFileNameGuid = { 0x78f54d4, 0xcc22, 0x4048, 0x9e, 0x94, 0x87, 0x9c, 0x21, 0x4d, 0x56, 0x2f };

//
// This data array will be located at the base of the Firmware Volume Header (FVH)
// in the boot block.  It must not exceed 14 bytes of code.  The last 2 bytes
// will be used to keep the FVH checksum consistent.
// This code will be run in response to a starutp IPI for HT-enabled systems.
//
#define SIZEOF_STARTUP_DATA_ARRAY 0x10

UINT8                                   m128kRecoveryStartupApDataArray[SIZEOF_STARTUP_DATA_ARRAY] = {
  //
  // EA D0 FF 00 F0               ; far jmp F000:FFD0
  // 0, 0, 0, 0, 0, 0, 0, 0, 0,   ; Reserved bytes
  // 0, 0                         ; Checksum Padding
  //
  0xEA,
  0xD0,
  0xFF,
  0x0,
  0xF0,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00
};

UINT8                                   m64kRecoveryStartupApDataArray[SIZEOF_STARTUP_DATA_ARRAY] = {
  //
  // EB CE                               ; jmp short ($-0x30)
  // ; (from offset 0x0 to offset 0xFFD0)
  // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
  // 0, 0                                ; Checksum Padding
  //
  0xEB,
  0xCE,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00
};

EFI_STATUS
ParseFvInf (
  IN MEMORY_FILE  *InfFile,
  IN FV_INFO      *FvInfo
  )
/*++

Routine Description:

  This function parses a FV.INF file and copies info into a FV_INFO structure.

Arguments:

  InfFile         Memory file image.
  FvInfo          Information read from INF file.

Returns:

  EFI_SUCCESS       INF file information successfully retrieved.
  EFI_ABORTED       INF file has an invalid format.
  EFI_NOT_FOUND     A required string was not found in the INF file.
--*/
{
  CHAR8       Value[_MAX_PATH];
  UINT64      Value64;
  UINTN       Index;
  EFI_STATUS  Status;

  //
  // Initialize FV info
  //
  memset (FvInfo, 0, sizeof (FV_INFO));

  //
  // Read the FV base address
  //
  Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_BASE_ADDRESS_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Get the base address
    //
    Status = AsciiStringToUint64 (Value, FALSE, &Value64);
    if (EFI_ERROR (Status)) {
      Error (NULL, 0, 0, EFI_FV_BASE_ADDRESS_STRING, "invalid value");
      return EFI_ABORTED;
    }

    FvInfo->BaseAddress = Value64;
  } else {
    Error (NULL, 0, 0, EFI_FV_BASE_ADDRESS_STRING, "could not find value");
    return EFI_ABORTED;
  }
  //
  // Read the FV Guid
  //
  Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_GUID_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Get the guid value
    //
    Status = StringToGuid (Value, &FvInfo->FvGuid);
    if (EFI_ERROR (Status)) {
      memcpy (&FvInfo->FvGuid, &FfsGuid, sizeof (EFI_GUID));
    }
  } else {
    memcpy (&FvInfo->FvGuid, &FfsGuid, sizeof (EFI_GUID));
  }
  //
  // Read the FV file name
  //
  Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_FILE_NAME_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // copy the file name
    //
    strcpy (FvInfo->FvName, Value);
  } else {
    Error (NULL, 0, 0, EFI_FV_FILE_NAME_STRING, "value not specified");
    return EFI_ABORTED;
  }
  //
  // Read the Sym file name
  //
  Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_SYM_FILE_NAME_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // copy the file name
    //
    strcpy (FvInfo->SymName, Value);
  } else {
    //
    // Symbols not required, so init to NULL.
    //
    strcpy (FvInfo->SymName, "");
  }
  //
  // Read the read disabled capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_READ_DISABLED_CAP_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the read disabled flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_READ_DISABLED_CAP;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_READ_DISABLED_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_READ_DISABLED_CAP_STRING, "value not specified");
    return Status;
  }
  //
  // Read the read enabled capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_READ_ENABLED_CAP_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the read disabled flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_READ_ENABLED_CAP;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_READ_ENABLED_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_READ_ENABLED_CAP_STRING, "value not specified");
    return Status;
  }
  //
  // Read the read status attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_READ_STATUS_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the read disabled flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_READ_STATUS;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_READ_STATUS_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_READ_STATUS_STRING, "value not specified");
    return Status;
  }
  //
  // Read the write disabled capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_WRITE_DISABLED_CAP_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the write disabled flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_WRITE_DISABLED_CAP;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_WRITE_DISABLED_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_WRITE_DISABLED_CAP_STRING, "value not specified");
    return Status;
  }
  //
  // Read the write enabled capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_WRITE_ENABLED_CAP_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the write disabled flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_WRITE_ENABLED_CAP;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_WRITE_ENABLED_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_WRITE_ENABLED_CAP_STRING, "value not specified");
    return Status;
  }
  //
  // Read the write status attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_WRITE_STATUS_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the write disabled flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_WRITE_STATUS;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_WRITE_STATUS_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_WRITE_STATUS_STRING, "value not specified");
    return Status;
  }
  //
  // Read the lock capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_LOCK_CAP_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the attribute flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_LOCK_CAP;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_LOCK_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_LOCK_CAP_STRING, "value not specified");
    return Status;
  }
  //
  // Read the lock status attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_LOCK_STATUS_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the attribute flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_LOCK_STATUS;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_LOCK_STATUS_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_LOCK_STATUS_STRING, "value not specified");
    return Status;
  }
  //
  // Read the sticky write attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_STICKY_WRITE_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the attribute flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_STICKY_WRITE;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_STICKY_WRITE_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_STICKY_WRITE_STRING, "value not specified");
    return Status;
  }
  //
  // Read the memory mapped attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_MEMORY_MAPPED_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the attribute flag
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_MEMORY_MAPPED;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_MEMORY_MAPPED_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_MEMORY_MAPPED_STRING, "value not specified");
    return Status;
  }
  //
  // Read the erase polarity attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ERASE_POLARITY_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update the attribute flag
    //
    if (strcmp (Value, ONE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ERASE_POLARITY;
    } else if (strcmp (Value, ZERO_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ERASE_POLARITY_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ERASE_POLARITY_STRING, "value not specified");
    return Status;
  }

#if (PI_SPECIFICATION_VERSION >= 0x00010000)        
  //
  // Read the read lock capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_READ_LOCK_CAP_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_READ_LOCK_CAP;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_READ_LOCK_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_READ_LOCK_CAP_STRING, "value not specified");
    return Status;
  }

  //
  // Read the read lock status attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_READ_LOCK_STATUS_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_READ_LOCK_STATUS;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_READ_LOCK_STATUS_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_READ_LOCK_STATUS_STRING, "value not specified");
    return Status;
  }

  //
  // Read the write lock capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_WRITE_LOCK_CAP_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_WRITE_LOCK_CAP;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_WRITE_LOCK_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_WRITE_LOCK_CAP_STRING, "value not specified");
    return Status;
  }

  //
  // Read the write lock status attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_WRITE_LOCK_STATUS_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_WRITE_LOCK_STATUS;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_WRITE_LOCK_STATUS_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_WRITE_LOCK_STATUS_STRING, "value not specified");
    return Status;
  }
#endif

#if (PI_SPECIFICATION_VERSION < 0x00010000)     
  //
  // Read the alignment capabilities attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_CAP_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_CAP;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_CAP_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_CAP_STRING, "value not specified");
    return Status;
  }

  //
  // Read the word alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_2_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_2;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_2_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_2_STRING, "value not specified");
    return Status;
  }

  
  //
  // Read the dword alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_4_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_4;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_4_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_4_STRING, "value not specified");
    return Status;
  }
  //
  // Read the word alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_8_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_8;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_8_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_8_STRING, "value not specified");
    return Status;
  }
  //
  // Read the qword alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_16_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_16;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_16_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_16_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 32 byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_32_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_32;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_32_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_32_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 64 byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_64_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_64;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_64_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_64_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 128 byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_128_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_128;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_128_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_128_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 256 byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_256_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_256;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_256_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_256_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 512 byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_512_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_512;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_512_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_512_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 1K byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_1K_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_1K;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_1K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_1K_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 2K byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_2K_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_2K;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_2K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_2K_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 4K byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_4K_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_4K;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_4K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_4K_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 8K byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_8K_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_8K;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_8K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_8K_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 16K byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_16K_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_16K;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_16K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_16K_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 32K byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_32K_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_32K;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_32K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_32K_STRING, "value not specified");
    return Status;
  }
  //
  // Read the 64K byte alignment capability attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB_ALIGNMENT_64K_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, TRUE_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB_ALIGNMENT_64K;
    } else if (strcmp (Value, FALSE_STRING) != 0) {
      Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_64K_STRING, "expected %s | %s", TRUE_STRING, FALSE_STRING);
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB_ALIGNMENT_64K_STRING, "value not specified");
    return Status;
  }

  if (!(FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_CAP) &&
      (
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_2) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_4) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_8) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_16) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_32) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_64) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_128) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_256) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_512) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_1K) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_2K) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_4K) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_8K) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_16K) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_32K) ||
        (FvInfo->FvAttributes & EFI_FVB_ALIGNMENT_64K)
      )
     ){
    Error (
      NULL,
      0,
      0,
      "illegal combination of alignment attributes",
      "if %s is not %s, no individual alignments can be %s",
      EFI_FVB_ALIGNMENT_CAP_STRING,
      TRUE_STRING,
      TRUE_STRING
      );
    return EFI_ABORTED;
  }
#else
  //
  // Read the PI1.0 FVB2 Alignment Capabilities Attribute
  //
  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FVB2_ALIGNMENT_STRING, 0, Value);

  if (Status == EFI_SUCCESS) {
    //
    // Update attribute
    //
    if (strcmp (Value, EFI_FVB2_ALIGNMENT_1_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_1;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_2_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_2;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_4_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_4;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_8_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_8;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_16_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_16;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_32_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_32;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_64_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_64;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_128_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_128;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_256_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_256;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_512_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_512;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_1K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_1K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_2K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_2K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_4K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_4K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_8K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_8K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_16K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_16K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_32K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_32K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_64K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_64K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_128K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_128K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_256K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_256K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_512K_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMNET_512K;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_1M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_1M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_2M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_2M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_4M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_4M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_8M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_8M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_16M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_16M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_32M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_32M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_64M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_64M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_128M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_128M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_256M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_256M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_512M_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_512M;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_1G_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_1G;
    } else if (strcmp (Value, EFI_FVB2_ALIGNMENT_2G_STRING) == 0) {
      FvInfo->FvAttributes |= EFI_FVB2_ALIGNMENT_2G;
    } else {
      Error (NULL, 0, 0, EFI_FVB2_ALIGNMENT_STRING, "value not correct!");
      return EFI_ABORTED;
    }
  } else {
    Error (NULL, 0, 0, EFI_FVB2_ALIGNMENT_STRING, "value not specified");
    return Status;
  }

#endif  
  //
  // Read block maps
  //
  for (Index = 0; Index < MAX_NUMBER_OF_FV_BLOCKS; Index++) {
    //
    // Read the number of blocks
    //
    Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_NUM_BLOCKS_STRING, Index, Value);

    if (Status == EFI_SUCCESS) {
      //
      // Update the number of blocks
      //
      Status = AsciiStringToUint64 (Value, FALSE, &Value64);
      if (EFI_ERROR (Status)) {
        Error (NULL, 0, 0, Value, "invalid value for %s", EFI_NUM_BLOCKS_STRING);
        return EFI_ABORTED;
      }

      FvInfo->FvBlocks[Index].NumBlocks = (UINT32) Value64;
    } else {
      //
      // If there is no number of blocks, but there is a size, then we have a mismatched pair
      // and should return an error.
      //
      Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_BLOCK_SIZE_STRING, Index, Value);
      if (!EFI_ERROR (Status)) {
        Error (NULL, 0, 0, "must specify both", "%s and %s", EFI_NUM_BLOCKS_STRING, EFI_BLOCK_SIZE_STRING);
        return EFI_ABORTED;
      } else {
        //
        // We are done
        //
        break;
      }
    }
    //
    // Read the size of blocks
    //
    Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_BLOCK_SIZE_STRING, Index, Value);

    if (Status == EFI_SUCCESS) {
      //
      // Update the number of blocks
      //
      Status = AsciiStringToUint64 (Value, FALSE, &Value64);
      if (EFI_ERROR (Status)) {
        Error (NULL, 0, 0, Value, "invalid value specified for %s", EFI_BLOCK_SIZE_STRING);
        return EFI_ABORTED;
      }

      FvInfo->FvBlocks[Index].BlockLength = (UINT32) Value64;
    } else {
      //
      // There is a number of blocks, but there is no size, so we have a mismatched pair
      // and should return an error.
      //
      Error (NULL, 0, 0, "must specify both", "%s and %s", EFI_NUM_BLOCKS_STRING, EFI_BLOCK_SIZE_STRING);
      return EFI_ABORTED;
    }
  }
  //
  // Read files
  //
  for (Index = 0; Index < MAX_NUMBER_OF_FILES_IN_FV; Index++) {
    //
    // Read the number of blocks
    //
    Status = FindToken (InfFile, FILES_SECTION_STRING, EFI_FILE_NAME_STRING, Index, Value);

    if (Status == EFI_SUCCESS) {
      //
      // Add the file
      //
      strcpy (FvInfo->FvFiles[Index], Value);
    } else {
      break;
    }
  }

  if (FindSection (InfFile, COMPONENT_SECTION_STRING)) {
    Index = 0;
    //
    // Read component FV_VARIABLE
    //
    Status = FindToken (InfFile, COMPONENT_SECTION_STRING, EFI_NV_VARIABLE_STRING, 0, Value);

    if (Status == EFI_SUCCESS) {
      //
      // Add the component
      //
      strcpy (FvInfo->FvComponents[Index].ComponentName, EFI_NV_VARIABLE_STRING);
      Status = AsciiStringToUint64 (Value, FALSE, &Value64);
      if (EFI_ERROR (Status)) {
        printf ("ERROR: %s is not a valid integer.\n", EFI_NV_VARIABLE_STRING);
        return EFI_ABORTED;
      }

      FvInfo->FvComponents[Index].Size = (UINTN) Value64;
    } else {
      printf ("WARNING: Could not read %s.\n", EFI_NV_VARIABLE_STRING);
    }

    Index++;
    //
    // Read component FV_EVENT_LOG
    //
    Status = FindToken (InfFile, COMPONENT_SECTION_STRING, EFI_NV_EVENT_LOG_STRING, 0, Value);

    if (Status == EFI_SUCCESS) {
      //
      // Add the component
      //
      strcpy (FvInfo->FvComponents[Index].ComponentName, EFI_NV_EVENT_LOG_STRING);
      Status = AsciiStringToUint64 (Value, FALSE, &Value64);
      if (EFI_ERROR (Status)) {
        printf ("ERROR: %s is not a valid integer.\n", EFI_NV_EVENT_LOG_STRING);
        return EFI_ABORTED;
      }

      FvInfo->FvComponents[Index].Size = (UINTN) Value64;
    } else {
      printf ("WARNING: Could not read %s.\n", EFI_NV_EVENT_LOG_STRING);
    }

    Index++;
    //
    // Read component FV_FTW_WORKING
    //
    Status = FindToken (InfFile, COMPONENT_SECTION_STRING, EFI_NV_FTW_WORKING_STRING, 0, Value);

    if (Status == EFI_SUCCESS) {
      //
      // Add the component
      //
      strcpy (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_WORKING_STRING);
      Status = AsciiStringToUint64 (Value, FALSE, &Value64);
      if (EFI_ERROR (Status)) {
        printf ("ERROR: %s is not a valid integer.\n", EFI_NV_FTW_WORKING_STRING);
        return EFI_ABORTED;
      }

      FvInfo->FvComponents[Index].Size = (UINTN) Value64;
    } else {
      printf ("WARNING: Could not read %s.\n", EFI_NV_FTW_WORKING_STRING);
    }

    Index++;
    //
    // Read component FV_FTW_SPARE
    //
    Status = FindToken (InfFile, COMPONENT_SECTION_STRING, EFI_NV_FTW_SPARE_STRING, 0, Value);

    if (Status == EFI_SUCCESS) {
      //
      // Add the component
      //
      strcpy (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_SPARE_STRING);
      Status = AsciiStringToUint64 (Value, FALSE, &Value64);
      if (EFI_ERROR (Status)) {
        printf ("ERROR: %s is not a valid integer.\n", EFI_NV_FTW_SPARE_STRING);
        return EFI_ABORTED;
      }

      FvInfo->FvComponents[Index].Size = (UINTN) Value64;
    } else {
      printf ("WARNING: Could not read %s.\n", EFI_NV_FTW_SPARE_STRING);
    }
  }
  //
  // Compute size for easy access later
  //
  FvInfo->Size = 0;
  for (Index = 0; FvInfo->FvBlocks[Index].NumBlocks; Index++) {
    FvInfo->Size += FvInfo->FvBlocks[Index].NumBlocks * FvInfo->FvBlocks[Index].BlockLength;
  }

  return EFI_SUCCESS;
}

VOID
UpdateFfsFileState (
  IN EFI_FFS_FILE_HEADER          *FfsFile,
  IN EFI_FIRMWARE_VOLUME_HEADER   *FvHeader
  )
/*++

Routine Description:

  This function changes the FFS file attributes based on the erase polarity
  of the FV.

Arguments:

  FfsFile   File header.
  FvHeader  FV header.

Returns:

  None

--*/
{
  if (FvHeader->Attributes & EFI_FVB_ERASE_POLARITY) {
    FfsFile->State = (UINT8)~(FfsFile->State);
  }
}

EFI_STATUS
ReadFfsAlignment (
  IN EFI_FFS_FILE_HEADER    *FfsFile,
  IN OUT UINT32             *Alignment
  )
/*++

Routine Description:

  This function determines the alignment of the FFS input file from the file
  attributes.

Arguments:

  FfsFile       FFS file to parse
  Alignment     The minimum required alignment of the FFS file, in bytes

Returns:

  EFI_SUCCESS              The function completed successfully.
  EFI_INVALID_PARAMETER    One of the input parameters was invalid.
  EFI_ABORTED              An error occurred.

--*/
{
  //
  // Verify input parameters.
  //
  if (FfsFile == NULL || Alignment == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  switch ((FfsFile->Attributes >> 3) & 0x07) {

  case 0:
    //
    // 1 byte alignment
    //
    *Alignment = (1 << 0);
    break;

  case 1:
    //
    // 16 byte alignment
    //
    *Alignment = (1 << 4);
    break;

  case 2:
    //
    // 128 byte alignment
    //
    *Alignment = (1 << 7);
    break;

  case 3:
    //
    // 512 byte alignment
    //
    *Alignment = (1 << 9);
    break;

  case 4:
    //
    // 1K byte alignment
    //
    *Alignment = (1 << 10);
    break;

  case 5:
    //
    // 4K byte alignment
    //
    *Alignment = (1 << 12);
    break;

  case 6:
    //
    // 32K byte alignment
    //
    *Alignment = (1 << 15);
    break;

  case 7:
    //
    // 64K byte alignment
    //
    *Alignment = (1 << 16);
    break;

  default:
    Error (NULL, 0, 0, "nvalid file attribute calculated, this is most likely a utility error", NULL);
    return EFI_ABORTED;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
AddPadFile (
  IN OUT MEMORY_FILE  *FvImage,
  IN UINT32           DataAlignment
  )
/*++

Routine Description:

  This function adds a pad file to the FV image if it required to align the
  data of the next file.

Arguments:

  FvImage         The memory image of the FV to add it to.  The current offset
                  must be valid.
  DataAlignment   The data alignment of the next FFS file.

Returns:

  EFI_SUCCESS              The function completed successfully.
  EFI_INVALID_PARAMETER    One of the input parameters was invalid.
  EFI_OUT_OF_RESOURCES     Insufficient resources exist in the FV to complete
                           the pad file add.

--*/
{
  EFI_FFS_FILE_HEADER *PadFile;
  UUID                PadFileGuid;
  UINTN               PadFileSize;

  //
  // Verify input parameters.
  //
  if (FvImage == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Basic assumption is we start from an 8 byte aligned address
  // and our file header is a multiple of 8 bytes
  //
  assert ((UINTN) FvImage->CurrentFilePointer % 8 == 0);
  assert (sizeof (EFI_FFS_FILE_HEADER) % 8 == 0);

  //
  // Check if a pad file is necessary
  //
  if (((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + sizeof (EFI_FFS_FILE_HEADER)) % DataAlignment == 0) {
    return EFI_SUCCESS;
  }
  //
  // Write pad file header
  //
  PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;

  //
  // Verify that we have enough space for the file header
  //
  if ((UINTN) (PadFile + sizeof (EFI_FFS_FILE_HEADER)) >= (UINTN) FvImage->Eof) {
    return EFI_OUT_OF_RESOURCES;
  }

  UuidCreate (&PadFileGuid);
  memset (PadFile, 0, sizeof (EFI_FFS_FILE_HEADER));
  memcpy (&PadFile->Name, &PadFileGuid, sizeof (EFI_GUID));
  PadFile->Type       = EFI_FV_FILETYPE_FFS_PAD;
  PadFile->Attributes = 0;

  //
  // Calculate the pad file size
  //
  //
  // This is the earliest possible valid offset (current plus pad file header
  // plus the next file header)
  //
  PadFileSize = (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + (sizeof (EFI_FFS_FILE_HEADER) * 2);

  //
  // Add whatever it takes to get to the next aligned address
  //
  while ((PadFileSize % DataAlignment) != 0) {
    PadFileSize++;
  }
  //
  // Subtract the next file header size
  //
  PadFileSize -= sizeof (EFI_FFS_FILE_HEADER);

  //
  // Subtract the starting offset to get size
  //
  PadFileSize -= (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage;

  //
  // Write pad file size (calculated size minus next file header size)
  //
  PadFile->Size[0]  = (UINT8) (PadFileSize & 0xFF);
  PadFile->Size[1]  = (UINT8) ((PadFileSize >> 8) & 0xFF);
  PadFile->Size[2]  = (UINT8) ((PadFileSize >> 16) & 0xFF);

  //
  // Fill in checksums and state, they must be 0 for checksumming.
  //
  PadFile->IntegrityCheck.Checksum.Header = 0;
  PadFile->IntegrityCheck.Checksum.File   = 0;
  PadFile->State                          = 0;
  PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));
  if (PadFile->Attributes & FFS_ATTRIB_CHECKSUM) {
    PadFile->IntegrityCheck.Checksum.File = CalculateChecksum8 ((UINT8 *) PadFile, PadFileSize);
  } else {
    PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
  }

  PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;
  UpdateFfsFileState (
    (EFI_FFS_FILE_HEADER *) PadFile,
    (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
    );

  //
  // Verify that we have enough space (including the padding
  //
  if ((UINTN) (PadFile + sizeof (EFI_FFS_FILE_HEADER)) >= (UINTN) FvImage->Eof) {
    return EFI_OUT_OF_RESOURCES;
  }
  //
  // Update the current FV pointer
  //
  FvImage->CurrentFilePointer += PadFileSize;

  return EFI_SUCCESS;
}

BOOLEAN
IsVtfFile (
  IN EFI_FFS_FILE_HEADER    *FileBuffer
  )
/*++

Routine Description:

  This function checks the header to validate if it is a VTF file

Arguments:

  FileBuffer     Buffer in which content of a file has been read.

Returns:

  TRUE    If this is a VTF file
  FALSE   If this is not a VTF file

--*/
{
  EFI_GUID  VtfGuid = EFI_FFS_VOLUME_TOP_FILE_GUID;
  if (!memcmp (&FileBuffer->Name, &VtfGuid, sizeof (EFI_GUID))) {
    return TRUE;
  } else {
    return FALSE;
  }
}

EFI_STATUS
FfsRebaseImageRead (
  IN     VOID    *FileHandle,
  IN     UINTN   FileOffset,
  IN OUT UINT32  *ReadSize,
  OUT    VOID    *Buffer
  )
/*++

Routine Description:

  Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file

Arguments:

  FileHandle - The handle to the PE/COFF file

  FileOffset - The offset, in bytes, into the file to read

  ReadSize   - The number of bytes to read from the file starting at FileOffset

  Buffer     - A pointer to the buffer to read the data into.

Returns:

  EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset

--*/
{
  CHAR8   *Destination8;
  CHAR8   *Source8;
  UINT32  Length;

  Destination8  = Buffer;
  Source8       = (CHAR8 *) ((UINTN) FileHandle + FileOffset);
  Length        = *ReadSize;
  while (Length--) {
    *(Destination8++) = *(Source8++);
  }

  return EFI_SUCCESS;
}

EFI_STATUS
RebaseFfsFile (
  IN OUT EFI_FFS_FILE_HEADER    *FfsFile,
  IN EFI_PHYSICAL_ADDRESS       BaseAddress
  )
/*++

Routine Description:

  This function determines if a file is XIP and should be rebased.  It will
  rebase any PE32 sections found in the file using the base address.

Arguments:

  FfsFile           A pointer to Ffs file image.
  BaseAddress       The base address to use for rebasing the file image.

Returns:

  EFI_SUCCESS             The image was properly rebased.
  EFI_INVALID_PARAMETER   An input parameter is invalid.
  EFI_ABORTED             An error occurred while rebasing the input file image.
  EFI_OUT_OF_RESOURCES    Could not allocate a required resource.

--*/
{
  EFI_STATUS                            Status;
  EFI_PEI_PE_COFF_LOADER_IMAGE_CONTEXT  ImageContext;
  UINTN                                 MemoryImagePointer;
  UINTN                                 MemoryImagePointerAligned;

  EFI_PHYSICAL_ADDRESS                  ImageAddress;
  UINT64                                ImageSize;
  EFI_PHYSICAL_ADDRESS                  EntryPoint;

  UINT32                                Pe32FileSize;
  UINT32                                NewPe32BaseAddress;

  UINTN                                 Index;
  EFI_FILE_SECTION_POINTER              CurrentPe32Section;
  UINT8                                 FileGuidString[80];

  //
  // Verify input parameters
  //
  if (FfsFile == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Convert the GUID to a string so we can at least report which file
  // if we find an error.
  //
  PrintGuidToBuffer (&FfsFile->Name, FileGuidString, sizeof (FileGuidString), TRUE);

  //
  // Do some nominal checks on the file, then check for XIP.
  //
  Status = VerifyFfsFile (FfsFile);
  if (EFI_ERROR (Status)) {
    Error (NULL, 0, 0, "invalid FFS file", FileGuidString);
    return EFI_INVALID_PARAMETER;
  }

  if (FfsFile->Type != EFI_FV_FILETYPE_SECURITY_CORE &&
      FfsFile->Type != EFI_FV_FILETYPE_PEI_CORE &&
      FfsFile->Type != EFI_FV_FILETYPE_PEIM
      ) {
    //
    // File is not XIP, so don't rebase
    //
    return EFI_SUCCESS;
  }
  //
  // Rebase each PE32 section
  //
  for (Index = 1;; Index++) {
    Status = GetSectionByType (FfsFile, EFI_SECTION_PE32, Index, &CurrentPe32Section);
    if (EFI_ERROR (Status)) {
      break;
    }
    //
    // Calculate the PE32 base address, the FFS file base plus the offset of the PE32 section
    //
    NewPe32BaseAddress = ((UINT32) BaseAddress) + ((UINTN) CurrentPe32Section.Pe32Section - (UINTN) FfsFile);

    //
    // Initialize context
    //
    memset (&ImageContext, 0, sizeof (ImageContext));
    ImageContext.Handle     = (VOID *) ((UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION));
    ImageContext.ImageRead  = (EFI_PEI_PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;

    Status                  = mPeCoffLoader.GetImageInfo (&mPeCoffLoader, &ImageContext);

    if (EFI_ERROR (Status)) {
      Error (NULL, 0, 0, "GetImageInfo() failed", FileGuidString);
      return Status;
    }
    //
    // Allocate a buffer for the image to be loaded into.
    //
    Pe32FileSize              = GetLength (CurrentPe32Section.Pe32Section->CommonHeader.Size);
    MemoryImagePointer        = (UINTN) (malloc (Pe32FileSize + 0x1000));
    MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFF) & (-1 << 12);
    if (MemoryImagePointerAligned == 0) {
      Error (NULL, 0, 0, "memory allocation failure", NULL);
      return EFI_OUT_OF_RESOURCES;
    }

    //
    // bugbug
    //
    ImageContext.ImageAddress = MemoryImagePointerAligned;
    Status = mPeCoffLoader.LoadImage (&mPeCoffLoader, &ImageContext);
    if (EFI_ERROR (Status)) {
      Error (NULL, 0, 0, "LoadImage() failure", FileGuidString);
      free ((VOID *) MemoryImagePointer);
      return Status;
    }

    Status = mPeCoffLoader.RelocateImage (&mPeCoffLoader, &ImageContext);
    if (EFI_ERROR (Status)) {
      Error (NULL, 0, 0, "RelocateImage() failure", FileGuidString);
      free ((VOID *) MemoryImagePointer);
      return Status;
    }

    ImageAddress  = ImageContext.ImageAddress;
    ImageSize     = ImageContext.ImageSize;
    EntryPoint    = ImageContext.EntryPoint;

    if (ImageSize > Pe32FileSize) {
      Error (
        NULL,
        0,
        0,
        "rebased PE32 is larger than original PE32 image",
        "0x%X > 0x%X on file %s",
        ImageSize,
        Pe32FileSize,
        FileGuidString
        );
      free ((VOID *) MemoryImagePointer);
      return EFI_ABORTED;
    }

    memcpy (CurrentPe32Section.Pe32Section, (VOID *) MemoryImagePointerAligned, Pe32FileSize);

    free ((VOID *) MemoryImagePointer);
  }
  //
  // the above for loop will always exit with EFI_NOT_FOUND if it completes
  // normally.  If Index == 1 at exit, then no PE32 sections were found.  If it
  // exits with any other error code, then something broke...
  //
  if (Status != EFI_NOT_FOUND) {
    Error (NULL, 0, 0, "failed to parse PE32 section", FileGuidString);
    return Status;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
AddSymFile (
  IN UINT64               BaseAddress,
  IN EFI_FFS_FILE_HEADER  *FfsFile,
  IN OUT MEMORY_FILE      *SymImage,
  IN CHAR8                *SourceFileName
  )
/*++

Routine Description:

  This function adds the SYM tokens in the source file to the destination file.
  The SYM tokens are updated to reflect the base address.

Arguments:

  BaseAddress     The base address for the new SYM tokens.
  FfsFile         Pointer to the beginning of the FFS file in question.
  SymImage        The memory file to update with symbol information.
  SourceFileName  The source file.

Returns:

  EFI_SUCCESS              The function completed successfully.
  EFI_INVALID_PARAMETER    One of the input parameters was invalid.
  EFI_ABORTED              An error occurred.

--*/
{
  FILE                      *SourceFile;

  CHAR8                     Buffer[_MAX_PATH];
  CHAR8                     Type[_MAX_PATH];
  CHAR8                     Address[_MAX_PATH];
  CHAR8                     Section[_MAX_PATH];
  CHAR8                     Token[_MAX_PATH];
  CHAR8                     SymFileName[_MAX_PATH];
  CHAR8                     CodeModuleName[_MAX_PATH];
  CHAR8                     *Ptr;

  UINT64                    TokenAddress;

  EFI_STATUS                Status;
  EFI_FILE_SECTION_POINTER  Pe32Section;
  UINT32                    EntryPoint;
  UINT32                    BaseOfCode;
  UINT16                    MachineType;

  //
  // Verify input parameters.
  //
  if (BaseAddress == 0 || FfsFile == NULL || SymImage == NULL || SourceFileName == NULL) {
    Error (NULL, 0, 0, "invalid parameter passed to AddSymFile()", NULL);
    return EFI_INVALID_PARAMETER;
  }
  //
  // Check if we want to add this file
  //
  //
  // Get the file name
  //
  strcpy (Buffer, SourceFileName);

  //
  // Copy the file name for the path of the sym file and truncate the name portion.
  //
  strcpy (SymFileName, Buffer);
  Ptr = strrchr (SymFileName, '\\');
  assert (Ptr);
  Ptr[0] = 0;

  //
  // Find the file extension and make it lower case
  //
  Ptr = strrchr (SymFileName, '.');
  if (Ptr != NULL) {
    _strlwr (Ptr);
  }
  //
  // Check if it is PEI file
  //
  if (strstr (Buffer, ".pei") != NULL) {
    //
    // Find the human readable portion
    //
    if (!strtok (Buffer, "-") ||
        !strtok (NULL, "-") ||
        !strtok (NULL, "-") ||
        !strtok (NULL, "-") ||
        !strtok (NULL, "-") ||
        !strcpy (Buffer, strtok (NULL, "."))
          ) {
      Error (NULL, 0, 0, "failed to find human readable portion of the file name in AddSymFile()", NULL);
      return EFI_ABORTED;
    }
    //
    // Save code module name
    //
    strcpy (CodeModuleName, Buffer);

    //
    // Add the symbol file name and extension to the file path.
    //
    strcat (Buffer, ".sym");
    strcat (SymFileName, "\\");
    strcat (SymFileName, Buffer);
  } else {
    //
    // Only handle PEIM files.
    //
    return EFI_SUCCESS;
  }
  //
  // Find PE32 section
  //
  Status = GetSectionByType (FfsFile, EFI_SECTION_PE32, 1, &Pe32Section);

  //
  // BUGBUG: Assume if no PE32 section it is PIC and hardcode base address
  //
  if (Status == EFI_NOT_FOUND) {
    Status = GetSectionByType (FfsFile, EFI_SECTION_TE, 1, &Pe32Section);
  }

  if (Status == EFI_SUCCESS) {
    Status = GetPe32Info (
               (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),
               &EntryPoint,
               &BaseOfCode,
               &MachineType
               );
  } else {
    if (Status == EFI_NOT_FOUND) {
      BaseOfCode = 0x60;
      Status     = EFI_SUCCESS;
    } else {
      Error (NULL, 0, 0, "could not parse a PE32 section from the PEI file", NULL);
      return Status;
    }
  }

  if (EFI_ERROR (Status)) {
    Error (NULL, 0, 0, "GetPe32Info() could not get PE32 entry point for PEI file", NULL);
    return Status;
  }

  //
  // Open the source file
  //
  SourceFile = fopen (SymFileName, "r");
  if (SourceFile == NULL) {
    //
    // SYM files are not required.
    //
    return EFI_SUCCESS;
  }
  //
  // Read the first line
  //
  if (fgets (Buffer, _MAX_PATH, SourceFile) == NULL) {
    Buffer[0] = 0;
  }
  //
  // Make sure it matches the expected sym format
  //
  if (strcmp (Buffer, "TEXTSYM format | V1.0\n")) {
    fclose (SourceFile);
    Error (NULL, 0, 0, "AddSymFile() found unexpected sym format in input file", NULL);
    return EFI_ABORTED;
  }
  //
  // Read in the file
  //
  while (feof (SourceFile) == 0) {
    //
    // Read a line
    //
    if (fscanf (
          SourceFile,
          "%s | %s | %s | %s\n",
          Type,
          Address,
          Section,
          Token
          ) == 4) {
      //
      // If the token starts with "??" ignore it
      //
      if (Token[0] == '?' && Token[1] == '?') {
        continue;
      }
      //
      // Get the token address
      //
      AsciiStringToUint64 (Address, TRUE, &TokenAddress);

      //
      // Add the base address
      //
      TokenAddress += BaseAddress;

      //
      // If PE32 or TE section then find the start of code.  For PIC it is hardcoded.
      //
      if (Pe32Section.Pe32Section) {
        //
        // Add the offset of the PE32 section
        //
        TokenAddress += (UINTN) Pe32Section.Pe32Section - (UINTN) FfsFile;

        //
        // Add the size of the PE32 section header
        //
        TokenAddress += sizeof (EFI_PE32_SECTION);
      } else {
        //
        // BUGBUG: Don't know why this is 0x28 bytes.
        //
        TokenAddress += 0x28;
      }
      //
      // Add the beginning of the code
      //
      TokenAddress += BaseOfCode;

      sprintf (
        Buffer,
        "%s | %016I64X | %s | _%s%s\n",
        Type,
        TokenAddress,
        Section,
        CodeModuleName,
        Token
        );
      memcpy (SymImage->CurrentFilePointer, Buffer, strlen (Buffer) + 1);
      SymImage->CurrentFilePointer = (UINT8 *) (((UINTN) SymImage->CurrentFilePointer) + strlen (Buffer) + 1);
    }
  }

  fclose (SourceFile);
  return EFI_SUCCESS;
}

EFI_STATUS
AddFile (
  IN OUT MEMORY_FILE          *FvImage,
  IN FV_INFO                  *FvInfo,
  IN UINTN                    Index,
  IN OUT EFI_FFS_FILE_HEADER  **VtfFileImage,
  IN OUT MEMORY_FILE          *SymImage
  )
/*++

Routine Description:

  This function adds a file to the FV image.  The file will pad to the
  appropriate alignment if required.

Arguments:

  FvImage       The memory image of the FV to add it to.  The current offset
                must be valid.
  FvInfo        Pointer to information about the FV.
  Index         The file in the FvInfo file list to add.
  VtfFileImage  A pointer to the VTF file within the FvImage.  If this is equal
                to the end of the FvImage then no VTF previously found.
  SymImage      The memory image of the Sym file to update if symbols are present.
                The current offset must be valid.

Returns:

  EFI_SUCCESS              The function completed successfully.
  EFI_INVALID_PARAMETER    One of the input parameters was invalid.
  EFI_ABORTED              An error occurred.
  EFI_OUT_OF_RESOURCES     Insufficient resources exist to complete the add.

--*/
{
  FILE                  *NewFile;
  UINTN                 FileSize;
  UINT8                 *FileBuffer;
  UINTN                 NumBytesRead;
  UINT32                CurrentFileAlignment;
  EFI_STATUS            Status;
  EFI_PHYSICAL_ADDRESS  CurrentFileBaseAddress;
  UINT8                 VtfHeaderChecksum;
  UINT8                 VtfFileChecksum;
  UINT8                 FileState;
  UINT32                TailSize;
#if (PI_SPECIFICATION_VERSION < 0x00010000)
  EFI_FFS_FILE_TAIL     TailValue;
#endif
  //
  // Verify input parameters.
  //
  if (FvImage == NULL || FvInfo == NULL || FvInfo->FvFiles[Index][0] == 0 || VtfFileImage == NULL || SymImage == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Read the file to add
  //
  NewFile = fopen (FvInfo->FvFiles[Index], "rb");

  if (NewFile == NULL) {
    Error (NULL, 0, 0, FvInfo->FvFiles[Index], "failed to open file for reading");
    return EFI_ABORTED;
  }
  //
  // Get the file size
  //
  FileSize = _filelength (_fileno (NewFile));

  //
  // Read the file into a buffer
  //
  FileBuffer = malloc (FileSize);
  if (FileBuffer == NULL) {
    Error (NULL, 0, 0, "memory allocation failure", NULL);
    return EFI_OUT_OF_RESOURCES;
  }

  NumBytesRead = fread (FileBuffer, sizeof (UINT8), FileSize, NewFile);

  //
  // Done with the file, from this point on we will just use the buffer read.
  //
  fclose (NewFile);

  //
  // Verify read successful
  //
  if (NumBytesRead != sizeof (UINT8) * FileSize) {
    free (FileBuffer);
    Error (NULL, 0, 0, FvInfo->FvFiles[Index], "failed to read input file contents");
    return EFI_ABORTED;
  }
  //
  // Verify space exists to add the file
  //
  if (FileSize > (UINTN) ((UINTN) *VtfFileImage - (UINTN) FvImage->CurrentFilePointer)) {
    Error (NULL, 0, 0, FvInfo->FvFiles[Index], "insufficient space remains to add the file");
    return EFI_OUT_OF_RESOURCES;
  }
  //
  // Update the file state based on polarity of the FV.
  //
  UpdateFfsFileState (
    (EFI_FFS_FILE_HEADER *) FileBuffer,
    (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
    );

  //
  // If we have a VTF file, add it at the top.
  //
  if (IsVtfFile ((EFI_FFS_FILE_HEADER *) FileBuffer)) {
    if ((UINTN) *VtfFileImage == (UINTN) FvImage->Eof) {
      //
      // No previous VTF, add this one.
      //
      *VtfFileImage = (EFI_FFS_FILE_HEADER *) (UINTN) ((UINTN) FvImage->FileImage + FvInfo->Size - FileSize);
      //
      // Sanity check. The file MUST align appropriately
      //
      if ((((UINTN) *VtfFileImage) & 0x07) != 0) {
        Error (NULL, 0, 0, "VTF file does not align on 8-byte boundary", NULL);
      }
      //
      // copy VTF File Header
      //
      memcpy (*VtfFileImage, FileBuffer, sizeof (EFI_FFS_FILE_HEADER));

      //
      // Copy VTF body
      //
      memcpy (
        (UINT8 *) *VtfFileImage + sizeof (EFI_FFS_FILE_HEADER),
        FileBuffer + sizeof (EFI_FFS_FILE_HEADER),
        FileSize - sizeof (EFI_FFS_FILE_HEADER)
        );

      //
      // re-calculate the VTF File Header
      //
      FileState = (*VtfFileImage)->State;
      (*VtfFileImage)->State = 0;
      *(UINT32 *) ((*VtfFileImage)->Size) = FileSize;
      (*VtfFileImage)->IntegrityCheck.Checksum.Header = 0;
      (*VtfFileImage)->IntegrityCheck.Checksum.File = 0;

      VtfHeaderChecksum = CalculateChecksum8 ((UINT8 *) *VtfFileImage, sizeof (EFI_FFS_FILE_HEADER));
      (*VtfFileImage)->IntegrityCheck.Checksum.Header = VtfHeaderChecksum;
      //
      // Determine if it has a tail
      //
      if ((*VtfFileImage)->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
        TailSize = sizeof (EFI_FFS_FILE_TAIL);
      } else {
        TailSize = 0;
      }

      if ((*VtfFileImage)->Attributes & FFS_ATTRIB_CHECKSUM) {
        VtfFileChecksum = CalculateChecksum8 ((UINT8 *) *VtfFileImage, FileSize - TailSize);
        (*VtfFileImage)->IntegrityCheck.Checksum.File = VtfFileChecksum;
      } else {
        (*VtfFileImage)->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
      }
    #if (PI_SPECIFICATION_VERSION < 0x00010000)
      //
      // If it has a file tail, update it
      //
      if ((*VtfFileImage)->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
        TailValue = (EFI_FFS_FILE_TAIL) (~((*VtfFileImage)->IntegrityCheck.TailReference));
        *(EFI_FFS_FILE_TAIL *) (((UINTN) (*VtfFileImage) + GetLength ((*VtfFileImage)->Size) - sizeof (EFI_FFS_FILE_TAIL))) = TailValue;
      }
    #endif  
      (*VtfFileImage)->State = FileState;
      free (FileBuffer);
      return EFI_SUCCESS;
    } else {
      //
      // Already found a VTF file.
      //
      Error (NULL, 0, 0, "multiple VTF files are illegal in a single FV", NULL);
      free (FileBuffer);
      return EFI_ABORTED;
    }
  }
  //
  // Check if alignment is required
  //
  Status = ReadFfsAlignment ((EFI_FFS_FILE_HEADER *) FileBuffer, &CurrentFileAlignment);
  if (EFI_ERROR (Status)) {
    printf ("ERROR: Could not determine alignment of file %s.\n", FvInfo->FvFiles[Index]);
    free (FileBuffer);
    return EFI_ABORTED;
  }
  //
  // Add pad file if necessary
  //
  Status = AddPadFile (FvImage, CurrentFileAlignment);
  if (EFI_ERROR (Status)) {
    printf ("ERROR: Could not align the file data properly.\n");
    free (FileBuffer);
    return EFI_ABORTED;
  }
  //
  // Add file
  //
  if ((FvImage->CurrentFilePointer + FileSize) < FvImage->Eof) {
    //
    // Copy the file
    //
    memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);

    //
    // If the file is XIP, rebase
    //
    CurrentFileBaseAddress = FvInfo->BaseAddress + ((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage);
    //
    //    Status = RebaseFfsFile ((EFI_FFS_FILE_HEADER*) FvImage->CurrentFilePointer, CurrentFileBaseAddress);
    //    if (EFI_ERROR(Status)) {
    //      printf ("ERROR: Could not rebase the file %s.\n", FvInfo->FvFiles[Index]);
    //      return EFI_ABORTED;
    //    }
    //
    // Update Symbol file
    //
    Status = AddSymFile (
              CurrentFileBaseAddress,
              (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer,
              SymImage,
              FvInfo->FvFiles[Index]
              );
    assert (!EFI_ERROR (Status));

    //
    // Update the current pointer in the FV image
    //
    FvImage->CurrentFilePointer += FileSize;
  } else {
    printf ("ERROR: The firmware volume is out of space, could not add file %s.\n", FvInfo->FvFiles[Index]);
    return EFI_ABORTED;
  }
  //
  // Make next file start at QWord Boundry
  //
  while (((UINTN) FvImage->CurrentFilePointer & 0x07) != 0) {
    FvImage->CurrentFilePointer++;
  }
  //
  // Free allocated memory.
  //
  free (FileBuffer);

  return EFI_SUCCESS;
}

EFI_STATUS
AddVariableBlock (
  IN UINT8                    *FvImage,
  IN UINTN                    Size,
  IN FV_INFO                  *FvInfo
  )
{
  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
  VARIABLE_STORE_HEADER       *VarStoreHeader;
  //
  // Variable block should exclude FvHeader. Since the length of
  // FvHeader depends on the block map, which is variable length,
  // we could only decide the actual variable block length here.
  //
  FvHeader                  = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;
  FvImage                   = FvImage + FvHeader->HeaderLength;

  VarStoreHeader            = (VARIABLE_STORE_HEADER *) FvImage;

  VarStoreHeader->Signature = VARIABLE_STORE_SIGNATURE;
  VarStoreHeader->Size      = Size - FvHeader->HeaderLength;
  VarStoreHeader->Format    = VARIABLE_STORE_FORMATTED;
  VarStoreHeader->State     = VARIABLE_STORE_HEALTHY;
  VarStoreHeader->Reserved  = 0;
  VarStoreHeader->Reserved1 = 0;

  return EFI_SUCCESS;
}

EFI_STATUS
AddEventLogBlock (
  IN UINT8                    *FvImage,
  IN UINTN                    Size,
  IN FV_INFO                  *FvInfo
  )
{
  return EFI_SUCCESS;
}

EFI_STATUS
AddFTWWorkingBlock (
  IN UINT8                    *FvImage,
  IN UINTN                    Size,
  IN FV_INFO                  *FvInfo
  )
{
  EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *FTWHeader;
  UINT32                                  Crc32;

  Crc32     = 0;
  FTWHeader = (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER *) FvImage;
  memcpy (&FTWHeader->Signature, &(FvInfo->FvGuid), sizeof (EFI_GUID));
  FTWHeader->WriteQueueSize = Size - sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER);
  CalculateCrc32 (FvImage, sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER), &Crc32);
  FTWHeader->Crc = Crc32;
  if (FvInfo->FvAttributes & EFI_FVB_ERASE_POLARITY) {
    FTWHeader->WorkingBlockValid    = 0;
    FTWHeader->WorkingBlockInvalid  = 1;
  } else {
    FTWHeader->WorkingBlockValid    = 1;
    FTWHeader->WorkingBlockInvalid  = 0;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
AddFTWSpareBlock (
  IN UINT8                    *FvImage,
  IN UINTN                    Size,
  IN FV_INFO                  *FvInfo
  )
{
  return EFI_SUCCESS;
}

EFI_STATUS
GenNonFFSFv (
  IN UINT8                    *FvImage,
  IN FV_INFO                  *FvInfo
  )
/*++

Routine Description:

  This function generate the non FFS FV image, such as the working block
  and spare block. How each component of the FV is built is component
  specific.

Arguments:

  FvImage       The memory image of the FV to add it to.  The current offset
                must be valid.
  FvInfo        Pointer to information about the FV.

Returns:

  EFI_SUCCESS              The function completed successfully.
  EFI_INVALID_PARAMETER    One of the input parameters was invalid.
  EFI_ABORTED              An error occurred.
  EFI_OUT_OF_RESOURCES     Insufficient resources exist to complete the add.

--*/
{
  UINTN                       Index;
  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
  UINT64                      TotalSize;

  FvHeader  = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;
  TotalSize = 0;

  for (Index = 0; FvInfo->FvComponents[Index].Size != 0; Index++) {
    if (_stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_VARIABLE_STRING) == 0) {
      AddVariableBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);
    } else if (_stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_EVENT_LOG_STRING) == 0) {
      AddEventLogBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);
    } else if (_stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_WORKING_STRING) == 0) {
      AddFTWWorkingBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);
    } else if (_stricmp (FvInfo->FvComponents[Index].ComponentName, EFI_NV_FTW_SPARE_STRING) == 0) {
      AddFTWSpareBlock (FvImage, FvInfo->FvComponents[Index].Size, FvInfo);
    } else {
      printf ("Error. Unknown Non-FFS block %s \n", FvInfo->FvComponents[Index].ComponentName);
      return EFI_ABORTED;
    }

    FvImage   = FvImage + FvInfo->FvComponents[Index].Size;
    TotalSize = TotalSize + FvInfo->FvComponents[Index].Size;
  }
  //
  // Index and TotalSize is zero mean there's no component, so this is an empty fv
  //
  if ((Index != 0 || TotalSize != 0) && TotalSize != FvInfo->Size) {
    printf ("Error. Component size does not sum up to FV size.\n");
    return EFI_ABORTED;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
PadFvImage (
  IN MEMORY_FILE          *FvImage,
  IN EFI_FFS_FILE_HEADER  *VtfFileImage
  )
/*++

Routine Description:

  This function places a pad file between the last file in the FV and the VTF
  file if the VTF file exists.

Arguments:

  FvImage       Memory file for the FV memory image
  VtfFileImage  The address of the VTF file.  If this is the end of the FV
                image, no VTF exists and no pad file is needed.

Returns:

  EFI_SUCCESS             Completed successfully.
  EFI_INVALID_PARAMETER   One of the input parameters was NULL.

--*/
{
  EFI_FFS_FILE_HEADER *PadFile;
  UINTN               FileSize;

  //
  // If there is no VTF or the VTF naturally follows the previous file without a
  // pad file, then there's nothing to do
  //
  if ((UINTN) VtfFileImage == (UINTN) FvImage->Eof || (void *) FvImage->CurrentFilePointer == (void *) VtfFileImage) {
    return EFI_SUCCESS;
  }
  //
  // Pad file starts at beginning of free space
  //
  PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;

  //
  // write header
  //
  memset (PadFile, 0, sizeof (EFI_FFS_FILE_HEADER));
  memcpy (&PadFile->Name, &DefaultFvPadFileNameGuid, sizeof (EFI_GUID));
  PadFile->Type       = EFI_FV_FILETYPE_FFS_PAD;
  PadFile->Attributes = 0;

  //
  // FileSize includes the EFI_FFS_FILE_HEADER
  //
  FileSize          = (UINTN) VtfFileImage - (UINTN) FvImage->CurrentFilePointer;
  PadFile->Size[0]  = (UINT8) (FileSize & 0x000000FF);
  PadFile->Size[1]  = (UINT8) ((FileSize & 0x0000FF00) >> 8);
  PadFile->Size[2]  = (UINT8) ((FileSize & 0x00FF0000) >> 16);

  //
  // Fill in checksums and state, must be zero during checksum calculation.
  //
  PadFile->IntegrityCheck.Checksum.Header = 0;
  PadFile->IntegrityCheck.Checksum.File   = 0;
  PadFile->State                          = 0;
  PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));
  if (PadFile->Attributes & FFS_ATTRIB_CHECKSUM) {
    PadFile->IntegrityCheck.Checksum.File = CalculateChecksum8 ((UINT8 *) PadFile, FileSize);
  } else {
    PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
  }

  PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;

  UpdateFfsFileState (
    (EFI_FFS_FILE_HEADER *) PadFile,
    (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
    );
  //
  // Update the current FV pointer
  //
  FvImage->CurrentFilePointer = FvImage->Eof;

  return EFI_SUCCESS;
}

EFI_STATUS
UpdateResetVector (
  IN MEMORY_FILE            *FvImage,
  IN FV_INFO                *FvInfo,
  IN EFI_FFS_FILE_HEADER    *VtfFile
  )
/*++

Routine Description:

  This parses the FV looking for the PEI core and then plugs the address into
  the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
  complete an IA32 Bootstrap FV.

Arguments:

  FvImage       Memory file for the FV memory image
  FvInfo        Information read from INF file.
  VtfFile       Pointer to the VTF file in the FV image.

Returns:

  EFI_SUCCESS             Function Completed successfully.
  EFI_ABORTED             Error encountered.
  EFI_INVALID_PARAMETER   A required parameter was NULL.
  EFI_NOT_FOUND           PEI Core file not found.

--*/
{
  EFI_FFS_FILE_HEADER       *PeiCoreFile;
  EFI_FFS_FILE_HEADER       *SecCoreFile;
  EFI_STATUS                Status;
  EFI_FILE_SECTION_POINTER  Pe32Section;
  UINT32                    EntryPoint;
  UINT32                    BaseOfCode;
  UINT16                    MachineType;
  EFI_PHYSICAL_ADDRESS      PeiCorePhysicalAddress;
  EFI_PHYSICAL_ADDRESS      SecCorePhysicalAddress;
  EFI_PHYSICAL_ADDRESS      *SecCoreEntryAddressPtr;
  UINT32                    *Ia32ResetAddressPtr;
  UINT8                     *BytePointer;
  UINT8                     *BytePointer2;
  UINT16                    *WordPointer;
  UINT16                    CheckSum;
  UINTN                     Index;
  EFI_FFS_FILE_STATE        SavedState;
  UINT32                    TailSize;
  UINT64                    FitAddress;
  FIT_TABLE                 *FitTablePtr;
#if (PI_SPECIFICATION_VERSION < 0x00010000)
  EFI_FFS_FILE_TAIL         TailValue;
#endif
  //
  // Verify input parameters
  //
  if (FvImage == NULL || FvInfo == NULL || VtfFile == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Initialize FV library
  //
  InitializeFvLib (FvImage->FileImage, (UINTN) FvImage->Eof - (UINTN) FvImage->FileImage);

  //
  // Verify VTF file
  //
  Status = VerifyFfsFile (VtfFile);
  if (EFI_ERROR (Status)) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Find the PEI Core
  //
  Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);
  if (EFI_ERROR (Status) || PeiCoreFile == NULL) {
    Error (NULL, 0, 0, "could not find the PEI core in the FV", NULL);
    return EFI_ABORTED;
  }
  //
  // PEI Core found, now find PE32 or TE section
  //
  Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
  if (Status == EFI_NOT_FOUND) {
    Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);
  }

  if (EFI_ERROR (Status)) {
    Error (NULL, 0, 0, "could not find PE32 or TE section in PEI core file", NULL);
    return EFI_ABORTED;
  }

  Status = GetPe32Info (
            (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),
            &EntryPoint,
            &BaseOfCode,
            &MachineType
            );

  if (EFI_ERROR (Status)) {
    Error (NULL, 0, 0, "could not get PE32 entry point for PEI core", NULL);
    return EFI_ABORTED;
  }
  //
  // Physical address is FV base + offset of PE32 + offset of the entry point
  //
  PeiCorePhysicalAddress = FvInfo->BaseAddress;
  PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;
  PeiCorePhysicalAddress += EntryPoint;

  if (MachineType == EFI_IMAGE_MACHINE_IA64) {
    //
    // Update PEI_CORE address
    //
    //
    // Set the uncached attribute bit in the physical address
    //
    PeiCorePhysicalAddress |= 0x8000000000000000;

    //
    // Check if address is aligned on a 16 byte boundary
    //
    if (PeiCorePhysicalAddress & 0xF) {
      printf (
        "ERROR: PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %Xh.\n",
        PeiCorePhysicalAddress
        );
      return EFI_ABORTED;
    }
    //
    // First Get the FIT table address
    //
    FitAddress  = (*(UINT64 *) (FvImage->Eof - IPF_FIT_ADDRESS_OFFSET)) & 0xFFFFFFFF;

    FitTablePtr = (FIT_TABLE *) (FvImage->FileImage + (FitAddress - FvInfo->BaseAddress));

    Status      = UpdatePeiCoreEntryInFit (FitTablePtr, PeiCorePhysicalAddress);

    if (!EFI_ERROR (Status)) {
      UpdateFitCheckSum (FitTablePtr);
    }
    //
    // Find the Sec Core
    //
    Status = GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE, 1, &SecCoreFile);
    if (EFI_ERROR (Status) || SecCoreFile == NULL) {
      Error (NULL, 0, 0, "could not find the Sec core in the FV", NULL);
      return EFI_ABORTED;
    }
    //
    // Sec Core found, now find PE32 section
    //
    Status = GetSectionByType (SecCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
    if (EFI_ERROR (Status)) {
      Error (NULL, 0, 0, "could not find PE32 section in SEC core file", NULL);
      return EFI_ABORTED;
    }

    Status = GetPe32Info (
              (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),
              &EntryPoint,
              &BaseOfCode,
              &MachineType
              );
    if (EFI_ERROR (Status)) {
      Error (NULL, 0, 0, "could not get PE32 entry point for SEC core", NULL);
      return EFI_ABORTED;
    }
    //
    // Physical address is FV base + offset of PE32 + offset of the entry point
    //
    SecCorePhysicalAddress = FvInfo->BaseAddress;
    SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;
    SecCorePhysicalAddress += EntryPoint;

    //
    // Update SEC_CORE address
    //
    //
    // Set the uncached attribute bit in the physical address
    //
    SecCorePhysicalAddress |= 0x8000000000000000;

    //
    // Update the address
    //
    SecCoreEntryAddressPtr  = (EFI_PHYSICAL_ADDRESS *) ((UINTN) FvImage->Eof - IPF_SALE_ENTRY_ADDRESS_OFFSET);
    *SecCoreEntryAddressPtr = SecCorePhysicalAddress;

    //
    // Check if address is aligned on a 16 byte boundary
    //
    if (SecCorePhysicalAddress & 0xF) {
      printf (
        "ERROR: SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %Xh.\n",
        SecCorePhysicalAddress
        );
      return EFI_ABORTED;
    }
  } else if ((MachineType == EFI_IMAGE_MACHINE_IA32) || 
             (MachineType == EFI_IMAGE_MACHINE_X64)) {
    //
    // Get the location to update
    //
    Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - IA32_PEI_CORE_ENTRY_OFFSET);

    //
    // Write lower 32 bits of physical address
    //
    *Ia32ResetAddressPtr = (UINT32) PeiCorePhysicalAddress;

    //
    // Update the BFV base address
    //
    Ia32ResetAddressPtr   = (UINT32 *) ((UINTN) FvImage->Eof - 4);
    *Ia32ResetAddressPtr  = (UINT32) (FvInfo->BaseAddress);

    CheckSum              = 0x0000;

    //
    // Update the Startup AP in the FVH header block ZeroVector region.
    //
    BytePointer   = (UINT8 *) ((UINTN) FvImage->FileImage);
    BytePointer2  = (FvInfo->Size == 0x10000) ? m64kRecoveryStartupApDataArray : m128kRecoveryStartupApDataArray;
    for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY; Index++) {
      *BytePointer++ = *BytePointer2++;
    }
    //
    // Calculate the checksum
    //
    WordPointer = (UINT16 *) ((UINTN) FvImage->FileImage);
    for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY / 2; Index++) {
      CheckSum = (UINT16) (CheckSum + ((UINT16) *WordPointer));
      WordPointer++;
    }
    //
    // Update the checksum field
    //
    BytePointer = (UINT8 *) ((UINTN) FvImage->FileImage);
    BytePointer += (SIZEOF_STARTUP_DATA_ARRAY - 2);
    WordPointer   = (UINT16 *) BytePointer;
    *WordPointer  = (UINT16) (0x10000 - (UINT32) CheckSum);
  } else {
    Error (NULL, 0, 0, "invalid machine type in PEI core", "machine type=0x%X", (UINT32) MachineType);
    return EFI_ABORTED;
  }
  //
  // Determine if it has an FFS file tail.
  //
  if (VtfFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
    TailSize = sizeof (EFI_FFS_FILE_TAIL);
  } else {
    TailSize = 0;
  }
  //
  // Now update file checksum
  //
  SavedState  = VtfFile->State;
  VtfFile->IntegrityCheck.Checksum.File = 0;
  VtfFile->State                        = 0;
  if (VtfFile->Attributes & FFS_ATTRIB_CHECKSUM) {
    VtfFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
                                              (UINT8 *) VtfFile,
                                              GetLength (VtfFile->Size) - TailSize
                                              );
  } else {
    VtfFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
  }

  VtfFile->State = SavedState;

#if (PI_SPECIFICATION_VERSION < 0x00010000)
  //
  // Update tail if present
  //
  if (VtfFile->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
    TailValue = (EFI_FFS_FILE_TAIL) (~(VtfFile->IntegrityCheck.TailReference));
    *(EFI_FFS_FILE_TAIL *) (((UINTN) (VtfFile) + GetLength (VtfFile->Size) - sizeof (EFI_FFS_FILE_TAIL))) = TailValue;
  }
#endif
  return EFI_SUCCESS;
}

EFI_STATUS
GetPe32Info (
  IN UINT8                  *Pe32,
  OUT UINT32                *EntryPoint,
  OUT UINT32                *BaseOfCode,
  OUT UINT16                *MachineType
  )
/*++

Routine Description:

  Retrieves the PE32 entry point offset and machine type from PE image or TE image.
  See EfiImage.h for machine types.  The entry point offset is from the beginning
  of the PE32 buffer passed in.

Arguments:

  Pe32          Beginning of the PE32.
  EntryPoint    Offset from the beginning of the PE32 to the image entry point.
  BaseOfCode    Base address of code.
  MachineType   Magic number for the machine type.

Returns:

  EFI_SUCCESS             Function completed successfully.
  EFI_ABORTED             Error encountered.
  EFI_INVALID_PARAMETER   A required parameter was NULL.
  EFI_UNSUPPORTED         The operation is unsupported.

--*/
{
  EFI_IMAGE_DOS_HEADER  *DosHeader;
  EFI_IMAGE_NT_HEADERS  *NtHeader;
  EFI_TE_IMAGE_HEADER   *TeHeader;

  //
  // Verify input parameters
  //
  if (Pe32 == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // First check whether it is one TE Image.
  //
  TeHeader = (EFI_TE_IMAGE_HEADER *) Pe32;
  if (TeHeader->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
    //
    // By TeImage Header to get output
    //
    *EntryPoint  = TeHeader->AddressOfEntryPoint + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;
    *BaseOfCode  = TeHeader->BaseOfCode + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;
    *MachineType = TeHeader->Machine;
  } else {
    //
    // Then check whether
    // is the DOS header
    //
    DosHeader = (EFI_IMAGE_DOS_HEADER *) Pe32;

    //
    // Verify DOS header is expected
    //
    if (DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
      printf ("ERROR: Unknown magic number in the DOS header, 0x%04X.\n", DosHeader->e_magic);
      return EFI_UNSUPPORTED;
    }
    //
    // Immediately following is the NT header.
    //
    NtHeader = (EFI_IMAGE_NT_HEADERS *) ((UINTN) Pe32 + DosHeader->e_lfanew);
    
    //
    // Verify NT header is expected
    //
    if (NtHeader->Signature != EFI_IMAGE_NT_SIGNATURE) {
      printf ("ERROR: Unrecognized image signature 0x%08X.\n", NtHeader->Signature);
      return EFI_UNSUPPORTED;
    }
    //
    // Get output
    //
    *EntryPoint   = NtHeader->OptionalHeader.AddressOfEntryPoint;
    *BaseOfCode   = NtHeader->OptionalHeader.BaseOfCode;
    *MachineType  = NtHeader->FileHeader.Machine;
  }

  //
  // Verify machine type is supported
  //
  if (*MachineType != EFI_IMAGE_MACHINE_IA32 && 
      *MachineType != EFI_IMAGE_MACHINE_IA64 &&
      *MachineType != EFI_IMAGE_MACHINE_X64) {
    printf ("ERROR: Unrecognized machine type in the PE32 file.\n");
    return EFI_UNSUPPORTED;
  }

  return EFI_SUCCESS;
}
//
// Exposed function implementations (prototypes are defined in GenFvImageLib.h)
//
EFI_STATUS
GenerateFvImage (
  IN CHAR8    *InfFileImage,
  IN UINTN    InfFileSize,
  OUT UINT8   **FvImage,
  OUT UINTN   *FvImageSize,
  OUT CHAR8   **FvFileName,
  OUT UINT8   **SymImage,
  OUT UINTN   *SymImageSize,
  OUT CHAR8   **SymFileName
  )
/*++

Routine Description:

  This is the main function which will be called from application.

Arguments:

  InfFileImage  Buffer containing the INF file contents.
  InfFileSize   Size of the contents of the InfFileImage buffer.
  FvImage       Pointer to the FV image created.
  FvImageSize   Size of the FV image created and pointed to by FvImage.
  FvFileName    Requested name for the FV file.
  SymImage      Pointer to the Sym image created.
  SymImageSize  Size of the Sym image created and pointed to by SymImage.
  SymFileName   Requested name for the Sym file.

Returns:

  EFI_SUCCESS             Function completed successfully.
  EFI_OUT_OF_RESOURCES    Could not allocate required resources.
  EFI_ABORTED             Error encountered.
  EFI_INVALID_PARAMETER   A required parameter was NULL.

--*/
{
  EFI_STATUS                  Status;
  MEMORY_FILE                 InfMemoryFile;
  MEMORY_FILE                 FvImageMemoryFile;
  MEMORY_FILE                 SymImageMemoryFile;
  FV_INFO                     FvInfo;
  UINTN                       Index;
  EFI_FIRMWARE_VOLUME_HEADER  *FvHeader;
  EFI_FFS_FILE_HEADER         *VtfFileImage;

  //
  // Check for invalid parameter
  //
  if (InfFileImage == NULL || FvImage == NULL || FvImageSize == NULL || FvFileName == NULL) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Initialize file structures
  //
  InfMemoryFile.FileImage           = InfFileImage;
  InfMemoryFile.CurrentFilePointer  = InfFileImage;
  InfMemoryFile.Eof                 = InfFileImage + InfFileSize;

  //
  // Parse the FV inf file for header information
  //
  Status = ParseFvInf (&InfMemoryFile, &FvInfo);
  if (EFI_ERROR (Status)) {
    printf ("ERROR: Could not parse the input INF file.\n");
    return EFI_ABORTED;
  }
  //
  // Update the file name return values
  //
  strcpy (*FvFileName, FvInfo.FvName);
  strcpy (*SymFileName, FvInfo.SymName);

  //
  // Calculate the FV size
  //
  *FvImageSize = FvInfo.Size;

  //
  // Allocate the FV
  //
  *FvImage = malloc (*FvImageSize);
  if (*FvImage == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  //
  // Allocate space for symbol file storage
  //
  *SymImage = malloc (SYMBOL_FILE_SIZE);
  if (*SymImage == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  //
  // Initialize the FV to the erase polarity
  //
  if (FvInfo.FvAttributes & EFI_FVB_ERASE_POLARITY) {
    memset (*FvImage, -1, *FvImageSize);
  } else {
    memset (*FvImage, 0, *FvImageSize);
  }
  //
  // Initialize FV header
  //
  FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) *FvImage;

  //
  // Initialize the zero vector to all zeros.
  //
  memset (FvHeader->ZeroVector, 0, 16);

  //
  // Copy the FFS GUID
  //
  memcpy (&FvHeader->FileSystemGuid, &FvInfo.FvGuid, sizeof (EFI_GUID));

  FvHeader->FvLength    = *FvImageSize;
  FvHeader->Signature   = EFI_FVH_SIGNATURE;
  FvHeader->Attributes  = FvInfo.FvAttributes;
#if (PI_SPECIFICATION_VERSION < 0x00010000)
  FvHeader->Revision    = EFI_FVH_REVISION;
  FvHeader->Reserved[0] = 0;
  FvHeader->Reserved[1] = 0;
  FvHeader->Reserved[2] = 0;
#else
  FvHeader->Revision    = EFI_FVH_PI_REVISION;
  FvHeader->ExtHeaderOffset = 0;
  FvHeader->Reserved[0] = 0;
#endif
  //
  // Copy firmware block map
  //
  for (Index = 0; FvInfo.FvBlocks[Index].NumBlocks != 0; Index++) {
    FvHeader->FvBlockMap[Index].NumBlocks   = FvInfo.FvBlocks[Index].NumBlocks;
    FvHeader->FvBlockMap[Index].BlockLength = FvInfo.FvBlocks[Index].BlockLength;
  }
  //
  // Add block map terminator
  //
  FvHeader->FvBlockMap[Index].NumBlocks   = 0;
  FvHeader->FvBlockMap[Index].BlockLength = 0;

  //
  // Complete the header
  //
  FvHeader->HeaderLength  = (UINT16) (((UINTN) &(FvHeader->FvBlockMap[Index + 1])) - (UINTN) *FvImage);
  FvHeader->Checksum      = 0;
  FvHeader->Checksum      = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));

  //
  // If there is no FFS file, find and generate each components of the FV
  //
  if (FvInfo.FvFiles[0][0] == 0) {
    Status = GenNonFFSFv (*FvImage, &FvInfo);
    if (EFI_ERROR (Status)) {
      printf ("ERROR: Could not generate NonFFS FV.\n");
      free (*FvImage);
      return EFI_ABORTED;
    }

    return EFI_SUCCESS;
  }
  //
  // Initialize our "file" view of the buffer
  //
  FvImageMemoryFile.FileImage           = *FvImage;
  FvImageMemoryFile.CurrentFilePointer  = *FvImage + FvHeader->HeaderLength;
  FvImageMemoryFile.Eof                 = *FvImage +*FvImageSize;

  //
  // Initialize our "file" view of the symbol file.
  //
  SymImageMemoryFile.FileImage          = *SymImage;
  SymImageMemoryFile.CurrentFilePointer = *SymImage;
  SymImageMemoryFile.Eof                = *FvImage + SYMBOL_FILE_SIZE;

  //
  // Initialize the FV library.
  //
  InitializeFvLib (FvImageMemoryFile.FileImage, FvInfo.Size);

  //
  // Files start on 8 byte alignments, so move to the next 8 byte aligned
  // address.  For now, just assert if it isn't.  Currently FV header is
  // always a multiple of 8 bytes.
  // BUGBUG: Handle this better
  //
  assert ((((UINTN) FvImageMemoryFile.CurrentFilePointer) % 8) == 0);

  //
  // Initialize the VTF file address.
  //
  VtfFileImage = (EFI_FFS_FILE_HEADER *) FvImageMemoryFile.Eof;

  //
  // Add files to FV
  //
  for (Index = 0; FvInfo.FvFiles[Index][0] != 0; Index++) {
    //
    // Add the file
    //
    Status = AddFile (&FvImageMemoryFile, &FvInfo, Index, &VtfFileImage, &SymImageMemoryFile);

    //
    // Exit if error detected while adding the file
    //
    if (EFI_ERROR (Status)) {
      printf ("ERROR: Could not add file %s.\n", FvInfo.FvFiles[Index]);
      free (*FvImage);
      return EFI_ABORTED;
    }
  }
  //
  // If there is a VTF file, some special actions need to occur.
  //
  if ((UINTN) VtfFileImage != (UINTN) FvImageMemoryFile.Eof) {
    //
    // Pad from the end of the last file to the beginning of the VTF file.
    //
    Status = PadFvImage (&FvImageMemoryFile, VtfFileImage);
    if (EFI_ERROR (Status)) {
      printf ("ERROR: Could not create the pad file between the last file and the VTF file.\n");
      free (*FvImage);
      return EFI_ABORTED;
    }
    //
    // Update reset vector (SALE_ENTRY for IPF)
    // Now for IA32 and IA64 platform, the fv which has bsf file must have the 
    // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the   
    // reset vector. If the PEI Core is found, the VTF file will probably get  
    // corrupted by updating the entry point.                                  
    //
    if ((FvInfo.BaseAddress + FvInfo.Size) == FV_IMAGES_TOP_ADDRESS) {       
      Status = UpdateResetVector (&FvImageMemoryFile, &FvInfo, VtfFileImage);
      if (EFI_ERROR(Status)) {                                               
        printf ("ERROR: Could not update the reset vector.\n");              
        free (*FvImage);                                                     
        return EFI_ABORTED;                                                  
      }                                                                      
    }
  } 
  //
  // Determine final Sym file size
  //
  *SymImageSize = SymImageMemoryFile.CurrentFilePointer - SymImageMemoryFile.FileImage;

  return EFI_SUCCESS;
}

EFI_STATUS
UpdatePeiCoreEntryInFit (
  IN FIT_TABLE     *FitTablePtr,
  IN UINT64        PeiCorePhysicalAddress
  )
/*++

Routine Description:

  This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
  Sec to Pei Core

Arguments:

  FitTablePtr             - The pointer of FIT_TABLE.
  PeiCorePhysicalAddress  - The address of Pei Core entry.

Returns:

  EFI_SUCCESS             - The PEI_CORE FIT entry was updated successfully.
  EFI_NOT_FOUND           - Not found the PEI_CORE FIT entry.

--*/
{
  FIT_TABLE *TmpFitPtr;
  UINTN     Index;
  UINTN     NumFitComponents;

  TmpFitPtr         = FitTablePtr;
  NumFitComponents  = TmpFitPtr->CompSize;

  for (Index = 0; Index < NumFitComponents; Index++) {
    if ((TmpFitPtr->CvAndType & FIT_TYPE_MASK) == COMP_TYPE_FIT_PEICORE) {
      TmpFitPtr->CompAddress = PeiCorePhysicalAddress;
      return EFI_SUCCESS;
    }

    TmpFitPtr++;
  }

  return EFI_NOT_FOUND;
}

VOID
UpdateFitCheckSum (
  IN FIT_TABLE   *FitTablePtr
  )
/*++

Routine Description:

  This function is used to update the checksum for FIT.


Arguments:

  FitTablePtr             - The pointer of FIT_TABLE.

Returns:

  None.

--*/
{
  if ((FitTablePtr->CvAndType & CHECKSUM_BIT_MASK) >> 7) {
    FitTablePtr->CheckSum = 0;
    FitTablePtr->CheckSum = CalculateChecksum8 ((UINT8 *) FitTablePtr, FitTablePtr->CompSize * 16);
  }
}