summaryrefslogtreecommitdiffstats
path: root/EdkCompatibilityPkg/Sample/Tools/Source/HiiPack/HiiPack.c
blob: 70b88279d2e1a53344f81015ea9065776467d75c (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
/*++

Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
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:

  HiiPack.c  

Abstract:

  Process HII export and pack files and create HII export files,
  dumps, or variable defaults packs.

--*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "Tiano.h"
#include "EfiUtilityMsgs.h"
#include "ParseInf.h"
#include "EfiInternalFormRepresentation.h"
#include "HiiPack.h"
#include "Hii.h"
#include "IfrParse.h"
#include "StringParse.h"

#define UTILITY_VERSION "v1.0"
#define UTILITY_NAME    "HiiPack"
#define MAX_PATH        260

//
// We may have to create an empty IFR formset to provide a GUID for an HII
// export pack. Create a structure definition to make it easier.
//
#pragma pack(1)

typedef struct {
  EFI_HII_IFR_PACK      PackHeader;
  EFI_IFR_FORM_SET      Formset;
  EFI_IFR_END_FORM_SET  EndFormset;
} EMPTY_FORMSET_PACK;

#pragma pack()
//
// We'll store lists of file names from the command line in
// a linked list of these
//
typedef struct _FILE_NAME_LIST {
  struct _FILE_NAME_LIST  *Next;
  UINT8                   FileName[MAX_PATH];
  int                     Tag;  // used for whatever
} FILE_NAME_LIST;

//
// When merging HII export packs, we save HII data table headers in a linked
// list of these.
//
typedef struct _DATA_TABLE_HEADER_LIST {
  struct _DATA_TABLE_HEADER_LIST  *Next;
  EFI_HII_DATA_TABLE              DataTableHeader;
} DATA_TABLE_HEADER_LIST;
//
// Create some defines for the different operation modes supported by this utility
//
#define MODE_CREATE_HII_EXPORT  1
#define MODE_MERGE_HII_EXPORTS  2
#define MODE_EMIT_DEFAULTS      3
#define MODE_DUMP_HII_EXPORT    4
//
// Here's all our globals.
//
static struct {
  FILE_NAME_LIST  *PackFileNames;           // Input HII pack file names
  FILE_NAME_LIST  *HiiExportFileNames;      // Input files when merging
  CHAR8           OutputFileName[MAX_PATH]; // Output dump file
  BOOLEAN         MfgFlag;                  // From -mfg command line arg
  BOOLEAN         NoEmptyVarPacks;          // From -noemptyvarpacks command line arg
  BOOLEAN         NoVarPacks;               // From -novarpacks command line arg
  EFI_GUID        Guid;                     // Guid specified on command line
  BOOLEAN         GuidSpecified;
  BOOLEAN         DumpStrings;              // In dump mode, dump string data
  int             Verbose;
  int             Mode;                     // Mode this utility is operating in
} mGlobals;

static
void
Usage (
  VOID
  );

static
STATUS
ProcessArgs (
  int   Argc,
  char  *Argv[]
  );

static
STATUS
DumpHiiExportFile (
  char    *HiiExportFileName,
  char    *OutputFileName
  );

static
void
DumpString (
  FILE    *OutFptr,
  int     StringIndex,
  CHAR16  *Str,
  int     Indent
  );

static
void
DumpStringPack (
  FILE                  *OutFptr,
  EFI_HII_STRING_PACK   *Pack,
  int                   BaseOffset,
  int                   Indent
  );

static
void
DumpVariablePacks (
  FILE                  *OutFptr,
  EFI_HII_VARIABLE_PACK *Pack,
  int                   NumPacks,
  int                   BaseOffset,
  int                   Indent
  );

static
void
TestDumpHiiPack (
  FILE    *OutFptr,
  char    *BufferStart,
  int     BufferSize
  );

static
void
DumpRawBytes (
  FILE                  *OutFptr,
  char                  *Buffer,
  int                   Count,
  int                   BaseOffset,
  int                   Indent
  );

static
void
DumpIfrPack (
  FILE                  *OutFptr,
  EFI_HII_IFR_PACK      *Pack,
  int                   BaseOffset,
  int                   Indent
  );

static
void
FreeGlobals (
  VOID
  );

static
STATUS
AddStringPack (
  EFI_HII_STRING_PACK   *PackHeader
  );

static
STATUS
ProcessHiiExportFile (
  char    *FileName,
  int     MfgDefaults
  );

static
STATUS
ProcessIfrFiles (
  FILE_NAME_LIST *FileName
  );

static
STATUS
EmitDefaults (
  FILE_NAME_LIST *HiiExportFiles,
  int            MfgDefaults,
  int            NoEmptyVarPacks
  );

static
STATUS
MergeHiiExports (
  FILE_NAME_LIST *HiiExportFiles,
  char           *OutputFileName,
  int            MfgDefaults,
  int            NoEmptyVarPacks
  );

void
GuidToString (
  EFI_GUID   *Guid,
  char       *Str
  );

static
CHAR16  *
AsciiToWchar (
  CHAR8 *Str
  );

static
STATUS
CreateHiiExport (
  char              *OutputFileName,
  EFI_GUID          *DummyFormsetGuid,
  FILE_NAME_LIST    *PackFiles,
  int               MfgDefaults
  );

int
main (
  int   Argc,
  char  *Argv[]
  )
/*++

Routine Description:

  Call the routine to parse the command-line options, then process the file.
  
Arguments:

  Standard C main() argc and argv.

Returns:

  0       if successful
  nonzero otherwise
  
--*/
// GC_TODO:    Argc - add argument and description to function comment
// GC_TODO:    ] - add argument and description to function comment
{
  STATUS  Status;
  //
  // Set the utility name for error reporting purposes
  //
  SetUtilityName (UTILITY_NAME);
  //
  // Process the command-line arguments
  //
  Status = ProcessArgs (Argc, Argv);
  if (Status != STATUS_SUCCESS) {
    return Status;
  }
  //
  // Switch based on whether we're dumping, merging, etc.
  //
  if (mGlobals.Mode == MODE_DUMP_HII_EXPORT) {
    if (mGlobals.Verbose) {
      fprintf (stdout, "Dumping HII export file %s => %s\n", mGlobals.HiiExportFileNames, mGlobals.OutputFileName);
    }

    DumpHiiExportFile (mGlobals.HiiExportFileNames->FileName, mGlobals.OutputFileName);
  } else if (mGlobals.Mode == MODE_CREATE_HII_EXPORT) {
    CreateHiiExport (mGlobals.OutputFileName, &mGlobals.Guid, mGlobals.PackFileNames, mGlobals.MfgFlag);
  } else if (mGlobals.Mode == MODE_MERGE_HII_EXPORTS) {
    MergeHiiExports (mGlobals.HiiExportFileNames, mGlobals.OutputFileName, mGlobals.MfgFlag, mGlobals.NoEmptyVarPacks);
  } else if (mGlobals.Mode == MODE_EMIT_DEFAULTS) {
    EmitDefaults (mGlobals.HiiExportFileNames, mGlobals.MfgFlag, mGlobals.NoEmptyVarPacks);
  }
  //
  //
  FreeGlobals ();
  IfrParseEnd ();
  StringEnd ();
  return GetUtilityStatus ();
}

/******************************************************************************/
static
STATUS
MergeHiiExports (
  FILE_NAME_LIST *HiiExportFiles,
  char           *OutputFileName,
  int            MfgDefaults,
  int            NoEmptyVarPacks
  )
/*++

Routine Description:

  Given a linked list of input HII export pack files, read in the contents
  of each and create a single HII export pack that contains the contents
  of all the input files.
  
Arguments:

  HiiExportFiles    - pointer to linked list of input HII export pack file names
  OutputFileName    - name of output (merged) HII export file
  MfgDefaults       - non-zero to emit manufacturing defaults in output file
  NoEmptyVarPacks   - non-zero to not emit 0-length variable packs to the output file

Returns:

  STATUS_SUCCESS    - if successful
  STATUS_ERROR      - otherwise
  
--*/
{
  EFI_HII_HANDLE          HiiHandle;
  FILE                    *OutFptr;
  FILE                    *InFptr;
  STATUS                  Status;
  CHAR8                   *Buffer;
  int                     FileSize;
  int                     DataTableIndex;
  int                     Count;
  int                     NumDataTables;
  EFI_HII_EXPORT_TABLE    *HiiExportTableHeader;
  EFI_HII_EXPORT_TABLE    TempHiiExportTableHeader;
  EFI_HII_DATA_TABLE      *DataTableHeader;
  EFI_HII_STRING_PACK     *StringPack;
  EFI_HII_VARIABLE_PACK   *VarPack;
  EFI_HII_IFR_PACK        *IfrPack;
  EFI_GUID                HiiExportRevisionGuid = EFI_HII_PROTOCOL_GUID;
  EFI_GUID                PackageGuid;
  EFI_GUID                FormsetGuid;
  long                    DataTableHeaderOffset;
  DATA_TABLE_HEADER_LIST  *DataTableList;
  DATA_TABLE_HEADER_LIST  *LastDataTable;
  DATA_TABLE_HEADER_LIST  *TempDataTable;
  //
  // Init locals
  //
  HiiHandle     = FIRST_HII_PACK_HANDLE;
  Buffer        = NULL;
  InFptr        = NULL;
  OutFptr       = NULL;
  Status        = STATUS_ERROR;
  DataTableList = NULL;
  LastDataTable = NULL;
  //
  // Initialize our IFR parser and string routines
  //
  IfrParseInit ();
  StringInit ();
  //
  // Process each input HII export file
  //
  NumDataTables = 0;
  while (HiiExportFiles != NULL) {
    if (mGlobals.Verbose) {
      fprintf (stdout, "Processing file %s\n", HiiExportFiles->FileName);
    }
    //
    // Read in the entire file contents
    //
    if ((InFptr = fopen (HiiExportFiles->FileName, "rb")) == NULL) {
      Error (NULL, 0, 0, HiiExportFiles->FileName, "failed to open HII export file for reading");
      goto Done;
    }

    fseek (InFptr, 0, SEEK_END);
    FileSize = (int) ftell (InFptr);
    fseek (InFptr, 0, SEEK_SET);
    Buffer = (CHAR8 *) malloc (FileSize);
    if (Buffer == NULL) {
      Error (NULL, 0, 0, "memory allocation failure", NULL);
      goto Done;
    }

    if (fread (Buffer, FileSize, 1, InFptr) != 1) {
      Error (NULL, 0, 0, HiiExportFiles->FileName, "failed to read file contents");
      goto Done;
    }

    fclose (InFptr);
    InFptr                = NULL;
    HiiExportTableHeader  = (EFI_HII_EXPORT_TABLE *) Buffer;
    //
    // Walk all the data tables
    //
    DataTableHeader = (EFI_HII_DATA_TABLE *) (HiiExportTableHeader + 1);
    for (DataTableIndex = 0; DataTableIndex < (int) HiiExportTableHeader->NumberOfHiiDataTables; DataTableIndex++) {
      NumDataTables++;
      //
      // Make sure we're still pointing into our buffer
      //
      if (((char *) DataTableHeader < Buffer) || ((char *) DataTableHeader > Buffer + FileSize)) {
        Error (NULL, 0, 0, "bad data table size in input file", NULL);
        goto Done;
      }
      //
      // Save a copy of the data table header
      //
      TempDataTable = (DATA_TABLE_HEADER_LIST *) malloc (sizeof (DATA_TABLE_HEADER_LIST));
      if (TempDataTable == NULL) {
        Error (NULL, 0, 0, "memory allocation failure", NULL);
        goto Done;
      }

      memset ((void *) TempDataTable, 0, sizeof (DATA_TABLE_HEADER_LIST));
      memcpy (&TempDataTable->DataTableHeader, DataTableHeader, sizeof (EFI_HII_DATA_TABLE));
      if (DataTableList == NULL) {
        DataTableList = TempDataTable;
      } else {
        LastDataTable->Next = TempDataTable;
      }

      LastDataTable = TempDataTable;
      //
      // If there is an IFR pack, parse it
      //
      if (DataTableHeader->IfrDataOffset != 0) {
        if (IfrParsePack (
            HiiHandle,
            (EFI_HII_IFR_PACK *) ((char *) DataTableHeader + DataTableHeader->IfrDataOffset),
            &DataTableHeader->PackageGuid
            ) != STATUS_SUCCESS
            ) {
          goto Done;
        }
      }
      //
      // If there is string data, save it
      //
      if (DataTableHeader->StringDataOffset != 0) {
        Status = StringParsePack (
                  HiiHandle,
                  (EFI_HII_STRING_PACK *) ((char *) DataTableHeader + DataTableHeader->StringDataOffset),
                  NULL,
                  &DataTableHeader->PackageGuid
                  );
        if (Status != STATUS_SUCCESS) {
          goto Done;
        }
      }
      //
      // If there is device path data, process it
      //
      if (DataTableHeader->DevicePathOffset != 0) {
        Error (NULL, 0, 0, "application error", "%s contains unsupported device path data", HiiExportFiles->FileName);
        goto Done;
      }
      //
      // Next data pack
      //
      DataTableHeader = (EFI_HII_DATA_TABLE *) ((char *) DataTableHeader + DataTableHeader->DataTableSize);
      HiiHandle++;
    }

    free (Buffer);
    Buffer = NULL;
    //
    // Next input file
    //
    HiiExportFiles = HiiExportFiles->Next;
  }
  //
  // Now create defaults
  //
  if (IfrSetDefaults (MfgDefaults) != STATUS_SUCCESS) {
    goto Done;
  }
  //
  // Create and write the output HII export header
  //
  if ((OutFptr = fopen (OutputFileName, "wb")) == NULL) {
    Error (NULL, 0, 0, OutputFileName, "failed to open output file for writing");
    goto Done;
  }

  memset ((void *) &TempHiiExportTableHeader, 0, sizeof (EFI_HII_EXPORT_TABLE));
  TempHiiExportTableHeader.NumberOfHiiDataTables = HiiHandle - FIRST_HII_PACK_HANDLE;
  memcpy (&TempHiiExportTableHeader.Revision, &HiiExportRevisionGuid, sizeof (EFI_GUID));
  if (fwrite ((void *) &TempHiiExportTableHeader, sizeof (EFI_HII_EXPORT_TABLE), 1, OutFptr) != 1) {
    Error (NULL, 0, 0, OutputFileName, "failed to write HII export table header to output file");
    goto Done;
  }
  //
  // Now go back through all the handles and create new data packs for each, writing out
  // the contents as we go.
  //
  HiiHandle = FIRST_HII_PACK_HANDLE;
  for (TempDataTable = DataTableList; TempDataTable != NULL; TempDataTable = TempDataTable->Next) {
    //
    // Write a data table header to the output file. We'll rewind the file and
    // write an updated one when we're done with this data set
    //
    DataTableHeaderOffset                         = ftell (OutFptr);
    TempDataTable->DataTableHeader.HiiHandle      = HiiHandle;
    TempDataTable->DataTableHeader.DataTableSize  = sizeof (EFI_HII_DATA_TABLE);
    //
    // We may change the number of variable data when merging export files, so init to 0
    //
    TempDataTable->DataTableHeader.NumberOfVariableData = 0;
    if (fwrite ((void *) &TempDataTable->DataTableHeader, sizeof (EFI_HII_DATA_TABLE), 1, OutFptr) != 1) {
      Error (NULL, 0, 0, OutputFileName, "failed to write HII data table header to output file");
      goto Done;
    }
    //
    // Get the string pack if any
    //
    Status = StringGetPack (HiiHandle, &StringPack, &FileSize, &Count, &FormsetGuid, &PackageGuid);
    if (Status == STATUS_SUCCESS) {
      TempDataTable->DataTableHeader.StringDataOffset = TempDataTable->DataTableHeader.DataTableSize;
      TempDataTable->DataTableHeader.DataTableSize += FileSize;
      //
      // TempDataTable->DataTableHeader.NumberOfLanguages should be unchanged
      //
      if (fwrite ((void *) StringPack, FileSize, 1, OutFptr) != 1) {
        Error (NULL, 0, 0, "failed to write string pack to output file", NULL);
        goto Done;
      }
    }
    //
    // Get the IFR pack
    //
    Status = IfrGetIfrPack (HiiHandle, &IfrPack, &FormsetGuid);
    if (Status == STATUS_SUCCESS) {
      //
      // Write the IFR pack, followed by the variable packs
      //
      TempDataTable->DataTableHeader.IfrDataOffset = TempDataTable->DataTableHeader.DataTableSize;
      TempDataTable->DataTableHeader.DataTableSize += IfrPack->Header.Length;
      if (fwrite ((void *) IfrPack, IfrPack->Header.Length, 1, OutFptr) != 1) {
        Error (NULL, 0, 0, "failed to write IFR pack to output file", NULL);
        goto Done;
      }
      //
      // If this is just a formset stub, then don't write the variable packs
      //
      if (IfrPack->Header.Length != sizeof (EMPTY_FORMSET_PACK)) {
        //
        // Go through all the variable packs and see if they're referenced by this IFR
        //
        Count = 0;
        do {
          Status = IfrGetVarPack (Count, &VarPack);
          if (Status == STATUS_SUCCESS) {
            //
            // Check for variable data length of 0
            //
            if ((NoEmptyVarPacks == 0) ||
                ((VarPack->Header.Length - sizeof (EFI_HII_VARIABLE_PACK) - VarPack->VariableNameLength) != 0)
                ) {
              //
              // See if it's referenced by this IFR
              //
              if (IfrReferencesVarPack (HiiHandle, VarPack) == STATUS_SUCCESS) {
                if (TempDataTable->DataTableHeader.VariableDataOffset == 0) {
                  TempDataTable->DataTableHeader.VariableDataOffset = TempDataTable->DataTableHeader.DataTableSize;
                }

                TempDataTable->DataTableHeader.DataTableSize += VarPack->Header.Length;
                TempDataTable->DataTableHeader.NumberOfVariableData++;
                if (fwrite ((void *) VarPack, VarPack->Header.Length, 1, OutFptr) != 1) {
                  Error (NULL, 0, 0, "failed to write variable pack to output file", NULL);
                  goto Done;
                }

              }
            }
          }

          Count++;
        } while (Status == STATUS_SUCCESS);
      }

      Status = STATUS_SUCCESS;
    }
    //
    // Get the device path pack
    //
    //
    // Rewind the file and write the updated data table header.
    //
    fseek (OutFptr, DataTableHeaderOffset, SEEK_SET);
    if (fwrite ((void *) &TempDataTable->DataTableHeader, sizeof (EFI_HII_DATA_TABLE), 1, OutFptr) != 1) {
      Error (NULL, 0, 0, OutputFileName, "failed to write HII data table header to output file");
      goto Done;
    }

    fseek (OutFptr, 0, SEEK_END);
    HiiHandle++;
  }

  Status = STATUS_SUCCESS;
Done:
  IfrParseEnd ();
  StringEnd ();
  if (Buffer != NULL) {
    free (Buffer);
  }

  if (InFptr != NULL) {
    fclose (InFptr);
  }

  if (OutFptr != NULL) {
    fclose (OutFptr);
  }

  while (DataTableList != NULL) {
    TempDataTable = DataTableList->Next;
    free (DataTableList);
    DataTableList = TempDataTable;
  }

  return Status;
}

/******************************************************************************/
static
STATUS
CreateHiiExport (
  char              *OutputFileName,
  EFI_GUID          *DummyFormsetGuid,
  FILE_NAME_LIST    *PackFiles,
  int               MfgDefaults
  )
/*++

Routine Description:

  Given a linked list of HII pack file names, walk the list to
  process them and create a single HII export file.
  
Arguments:

  OutputFileName    - name of output HII export file to create
  DummyFormsetGuid  - IFR formsets contain a GUID which is used in many 
                      places while processing data tables. If we were not
                      given an IFR pack, then we'll create a stub IFR
                      pack using this GUID as the formset GUID.
  PackFiles         - linked list of HII pack files to process
  MfgDefaults       - when creating variable packs (via IFR pack processing),
                      use manufacturing defaults rather than standard defaults

Returns:

  STATUS_SUCCESS    - if successful
  STATUS_ERROR      - otherwise
  
--*/
{
  STATUS                      Status;
  EMPTY_FORMSET_PACK          EmptyFormset;
  EFI_HII_DATA_TABLE          DataTableHeader;
  EFI_HII_EXPORT_TABLE        ExportTableHeader;
  long                        DataTableHeaderOffset;
  long                        FileSize;
  FILE                        *OutFptr;
  FILE                        *InFptr;
  FILE_NAME_LIST              *TempFile;
  EFI_GUID                    HiiExportRevisionGuid = EFI_HII_PROTOCOL_GUID;
  EFI_GUID                    TempGuid;
  EFI_GUID                    PackageGuid;
  char                        *Buffer;
  EFI_HII_VARIABLE_PACK       *VarPack;
  EFI_HII_IFR_PACK            *IfrPack;
  EFI_HII_STRING_PACK_HEADER  *StringPack;
  EFI_HII_STRING_PACK_HEADER  TerminatorStringPack;
  int                         NumIfr;
  int                         NumStrings;
  int                         Index;
  int                         VarPackIndex;
  //
  // If no input HII pack files, then why are we here? Should have been caught when
  // args were processed though.
  //
  if (PackFiles == NULL) {
    Error (NULL, 0, 0, "no input pack files specified", NULL);
    return STATUS_ERROR;
  }

  InFptr  = NULL;
  Status  = STATUS_ERROR;
  Buffer  = NULL;
  //
  // Open the output file for writing
  //
  if ((OutFptr = fopen (OutputFileName, "wb")) == NULL) {
    Error (NULL, 0, 0, OutputFileName, "failed to open output file for writing");
    goto Done;
  }
  //
  // Figure out how many data tables we are going to need. We'll create one
  // data table if no more than one IFR, or we'll create one data table per IFR,
  // and then one for strings if multiple IFR
  //
  NumIfr      = 0;
  NumStrings  = 0;
  for (TempFile = PackFiles; TempFile != NULL; TempFile = TempFile->Next) {
    if (TempFile->Tag == EFI_HII_IFR) {
      NumIfr++;
    } else if (TempFile->Tag == EFI_HII_STRING) {
      NumStrings++;
    }
  }
  //
  // Three possibilities:
  //  1) No IFR, so create one data table that contains only strings and an empty formset
  //  2) Only 1 IFR, so create an export table with one data table that contains the IFR
  //     and all the strings
  //  3) Multiple IFR, so create a data table for each IFR and another data table with
  //     all the strings.
  //
  // Initialize the export table header and write it out
  //
  memset ((void *) &ExportTableHeader, 0, sizeof (EFI_HII_EXPORT_TABLE));
  if (NumIfr < 2) {
    ExportTableHeader.NumberOfHiiDataTables = 1;
  } else {
    //
    // One data table per IFR, plus one for strings (if any).
    //
    ExportTableHeader.NumberOfHiiDataTables = (UINT16) NumIfr;
    if (NumStrings != 0) {
      ExportTableHeader.NumberOfHiiDataTables++;
    }
  }
  //
  // Init the GUID in the HII export table header
  //
  memcpy (&ExportTableHeader.Revision, &HiiExportRevisionGuid, sizeof (EFI_GUID));
  if (fwrite ((void *) &ExportTableHeader, sizeof (EFI_HII_EXPORT_TABLE), 1, OutFptr) != 1) {
    Error (NULL, 0, 0, OutputFileName, "failed to write HII export table header to output file");
    goto Done;
  }
  //
  // *****************************************************************************************
  //
  //  CASE 1 - No IFR => one data table that contains only strings and an empty formset.
  //           No variable data.
  //
  //  CASE 2 - Only 1 IFR => create an export table with one data table that contains the IFR
  //           and all the strings plus variable data
  //
  //  CASE 3 - Multiple IFR => create a data table for each IFR and another data table with
  //           all the strings. Each IFR data table has variable data if applicable.
  //
  // *****************************************************************************************
  //
  // If the user did not give us an IFR file, then we'll have to create an empty formset
  // and emit it to the output file. In this case, we need a formset GUID on the command
  // line.
  //
  if ((NumIfr == 0) && (mGlobals.GuidSpecified == 0)) {
    //
    //    Warning (NULL, 0, 0, "using NULL GUID for empty formset", "specify -g GUID on the command line if desired");
    //
    memset ((void *) &PackageGuid, 0, sizeof (EFI_GUID));
  } else if (mGlobals.GuidSpecified) {
    //
    // Use it for the package GUID
    //
    memcpy (&PackageGuid, &mGlobals.Guid, sizeof (EFI_GUID));
  }
  //
  // Init the data table header.
  // Write out the blank data table header. Save the offset so we can
  // write an updated version at the end of processing.
  //
  memset ((void *) &DataTableHeader, 0, sizeof (EFI_HII_DATA_TABLE));
  DataTableHeaderOffset     = ftell (OutFptr);
  DataTableHeader.HiiHandle = FIRST_HII_PACK_HANDLE;
  if (mGlobals.Verbose) {
    fprintf (stdout, "writing data table (first time) to offset 0x%X\n", ftell (OutFptr));
  }

  if (fwrite ((void *) &DataTableHeader, sizeof (EFI_HII_DATA_TABLE), 1, OutFptr) != 1) {
    Error (NULL, 0, 0, "failed to write Data Table Header to output file", NULL);
    goto Done;
  }
  //
  // Set the data table size, then write out all the string packs
  //
  DataTableHeader.DataTableSize = sizeof (EFI_HII_DATA_TABLE);
  //
  // Write out the string files to a single data record
  //
  for (TempFile = PackFiles; TempFile != NULL; TempFile = TempFile->Next) {
    //
    // Continue to next file if it's not a string pack file
    //
    if (TempFile->Tag != EFI_HII_STRING) {
      continue;
    }
    //
    // Set the offset in the header if this is the first string pack
    //
    if (DataTableHeader.StringDataOffset == 0) {
      DataTableHeader.StringDataOffset = DataTableHeader.DataTableSize;
    }

    if ((InFptr = fopen (TempFile->FileName, "rb")) == NULL) {
      Error (NULL, 0, 0, TempFile->FileName, "failed to open input string pack file for reading");
      goto Done;
    }
    //
    // Get the file size, then read it into a buffer
    //
    fseek (InFptr, 0, SEEK_END);
    FileSize = ftell (InFptr);
    fseek (InFptr, 0, SEEK_SET);
    Buffer = (char *) malloc (FileSize);
    if (Buffer == NULL) {
      Error (NULL, 0, 0, TempFile->FileName, "memory allocation failure reading in file contents");
      goto Done;
    }

    if (fread (Buffer, FileSize, 1, InFptr) != 1) {
      Error (NULL, 0, 0, TempFile->FileName, "failed to read file contents");
      goto Done;
    }

    fclose (InFptr);
    InFptr = NULL;
    //
    // Verify that it's actually a string pack
    //
    StringPack = (EFI_HII_STRING_PACK_HEADER *) Buffer;
    while ((char *) StringPack < Buffer + FileSize) {
      if (StringPack->Header.Type != EFI_HII_STRING) {
        Error (NULL, 0, 0, TempFile->FileName, "file does not consist entirely of string packs");
        goto Done;
      }

      if (StringPack->Header.Length == 0) {
        break;
      }

      DataTableHeader.NumberOfLanguages++;
      DataTableHeader.DataTableSize += StringPack->Header.Length;
      //
      // Write the string pack to the output file
      //
      if (mGlobals.Verbose) {
        fprintf (stdout, "writing string pack to offset 0x%X\n", ftell (OutFptr));
      }

      if (fwrite (StringPack, StringPack->Header.Length, 1, OutFptr) != 1) {
        Error (NULL, 0, 0, TempFile->FileName, "failed to write string pack to output file");
        goto Done;
      }
      //
      // Sanity check that adding the length advances us (no wrap)
      //
      if ((char *) StringPack + StringPack->Header.Length <= (char *) StringPack) {
        Error (NULL, 0, 0, TempFile->FileName, "invalid pack size in file");
        goto Done;
      }

      StringPack = (EFI_HII_STRING_PACK_HEADER *) ((char *) StringPack + StringPack->Header.Length);
    }
    //
    // Free up buffer, go to next input string pack file
    //
    free (Buffer);
    Buffer = NULL;
  }
  //
  // Write a null-terminator string pack if we had any string packs at all
  //
  if (DataTableHeader.StringDataOffset != 0) {
    memset (&TerminatorStringPack, 0, sizeof (EFI_HII_STRING_PACK_HEADER));
    TerminatorStringPack.Header.Length  = 0;
    TerminatorStringPack.Header.Type    = EFI_HII_STRING;
    if (mGlobals.Verbose) {
      fprintf (stdout, "writing terminator string pack to offset 0x%X\n", ftell (OutFptr));
    }

    if (fwrite (&TerminatorStringPack, sizeof (EFI_HII_STRING_PACK_HEADER), 1, OutFptr) != 1) {
      Error (NULL, 0, 0, "failed to write string pack terminator to output file", NULL);
      goto Done;
    }

    DataTableHeader.DataTableSize += sizeof (EFI_HII_STRING_PACK_HEADER);
  }
  //
  // Parse all the IFR packs, then get the GUID from the first
  // one so we can use it for the package GUID if necessary.
  //
  memcpy (&PackageGuid, &mGlobals.Guid, sizeof (EFI_GUID));
  if (NumIfr != 0) {
    IfrParseInit ();
    if (ProcessIfrFiles (PackFiles) != STATUS_SUCCESS) {
      goto Done;
    }
    //
    // Set variable defaults in all variable packs
    //
    IfrSetDefaults (MfgDefaults);
    //
    // Get the GUID from the first IFR pack if the user did not specify a GUID on
    // the command line.
    //
    if (mGlobals.GuidSpecified == 0) {
      if (IfrGetIfrPack (FIRST_HII_PACK_HANDLE, &IfrPack, &PackageGuid) != STATUS_SUCCESS) {
        Error (NULL, 0, 0, "internal application error", "failed to retrieve IFR pack after parsing");
        goto Done;
      }
    }
  }
  //
  // Set the package GUID in the data table header.
  //
  memcpy (&DataTableHeader.PackageGuid, &PackageGuid, sizeof (EFI_GUID));
  //
  // If no IFR, then create and write the empty formset. Otherwise
  // parse the IFR and emit it and the variable data for it.
  //
  if (NumIfr == 0) {
    memset ((void *) &EmptyFormset, 0, sizeof (EMPTY_FORMSET_PACK));
    EmptyFormset.PackHeader.Header.Type   = EFI_HII_IFR;
    EmptyFormset.PackHeader.Header.Length = sizeof (EMPTY_FORMSET_PACK);
    //
    // Formset Opcode
    //
    EmptyFormset.Formset.Header.OpCode  = EFI_IFR_FORM_SET_OP;
    EmptyFormset.Formset.Header.Length  = (UINT8) sizeof (EFI_IFR_FORM_SET);
    memcpy (&EmptyFormset.Formset.Guid, &PackageGuid, sizeof (EFI_GUID));
    //
    // EndFormset Opcode
    //
    EmptyFormset.EndFormset.Header.OpCode = EFI_IFR_END_FORM_SET_OP;
    EmptyFormset.EndFormset.Header.Length = (UINT8) sizeof (EFI_IFR_END_FORM_SET);
    DataTableHeader.IfrDataOffset         = DataTableHeader.DataTableSize;
    if (mGlobals.Verbose) {
      fprintf (stdout, "writing stub IFR formset to to offset 0x%X\n", ftell (OutFptr));
    }

    if (fwrite (&EmptyFormset, sizeof (EMPTY_FORMSET_PACK), 1, OutFptr) != 1) {
      Error (NULL, 0, 0, OutputFileName, "failed to write formset stub to output file");
      goto Done;
    }

    DataTableHeader.DataTableSize += sizeof (EMPTY_FORMSET_PACK);
    //
    // Go back and re-write the data table header, reposition to the end, then return.
    //
    fseek (OutFptr, DataTableHeaderOffset, SEEK_SET);
    if (mGlobals.Verbose) {
      fprintf (stdout, "writing data table (second time) to offset 0x%X\n", ftell (OutFptr));
    }

    if (fwrite ((void *) &DataTableHeader, sizeof (EFI_HII_DATA_TABLE), 1, OutFptr) != 1) {
      Error (NULL, 0, 0, "failed to write Data Table Header to output file", NULL);
      goto Done;
    }

    fseek (OutFptr, 0, SEEK_END);
    if (mGlobals.Verbose) {
      fprintf (
        stdout,
        "final file offset=0x%X DataTableHeader.DataTableSize=0x%X\n",
        ftell (OutFptr),
        DataTableHeader.DataTableSize
        );
    }
  } else if (NumIfr == 1) {
    //
    // They gave us one input IFR file. We parsed it above, so get each one
    // and emit the IFR and each variable pack it references.
    // Update the data pack header for the IFR pack, then write the IFR pack data
    //
    DataTableHeader.IfrDataOffset = DataTableHeader.DataTableSize;
    if (IfrGetIfrPack (FIRST_HII_PACK_HANDLE, &IfrPack, &TempGuid) != STATUS_SUCCESS) {
      Error (NULL, 0, 0, "internal application error", "failed to retrieve IFR pack after parsing");
      goto Done;
    }

    if (mGlobals.Verbose) {
      fprintf (stdout, "writing IFR pack to 0x%X\n", ftell (OutFptr));
    }

    if (fwrite (IfrPack, IfrPack->Header.Length, 1, OutFptr) != 1) {
      Error (NULL, 0, 0, OutputFileName, "failed to write IFR pack to output file");
      goto Done;
    }

    DataTableHeader.DataTableSize += IfrPack->Header.Length;
    //
    // Now go through all the variable packs discovered during IFR processing
    // and write them to the output file
    //
    if (mGlobals.NoVarPacks == 0) {
      Index = 0;
      do {
        Status = IfrGetVarPack (Index, &VarPack);
        if (Status == STATUS_SUCCESS) {
          //
          // If this is the first variable pack, then update the "offset
          // to variable data" in the data table header
          //
          if (Index == 0) {
            DataTableHeader.VariableDataOffset = DataTableHeader.DataTableSize;
          }

          DataTableHeader.DataTableSize += VarPack->Header.Length;
          DataTableHeader.NumberOfVariableData++;
          if (fwrite ((void *) VarPack, VarPack->Header.Length, 1, OutFptr) != 1) {
            Error (NULL, 0, 0, OutputFileName, "failed to write variable pack to output file");
            goto Done;
          }

          Index++;
        }
      } while (Status == STATUS_SUCCESS);
    }
    //
    // Reposition in the output file and write the updated data table header
    //
    fseek (OutFptr, DataTableHeaderOffset, SEEK_SET);
    if (fwrite ((void *) &DataTableHeader, sizeof (EFI_HII_DATA_TABLE), 1, OutFptr) != 1) {
      Error (NULL, 0, 0, "failed to write Data Table Header to output file", NULL);
      goto Done;
    }

    fseek (OutFptr, 0, SEEK_END);
  } else {
    //
    // Multiple IFR input files. Close out the current data table (strings)
    // if applicable. Then retrieve each parsed IFR pack and create a data pack
    // that contains the IFR (one per data set) and the variable packs that
    // the given IFR form references.
    //
    if (NumStrings != 0) {
      fseek (OutFptr, DataTableHeaderOffset, SEEK_SET);
      if (fwrite ((void *) &DataTableHeader, sizeof (EFI_HII_DATA_TABLE), 1, OutFptr) != 1) {
        Error (NULL, 0, 0, "failed to write Data Table Header to output file", NULL);
        goto Done;
      }

      fseek (OutFptr, 0, SEEK_END);
    } else {
      //
      // No strings, so back up over the data table header we wrote because we assumed
      // at least one string pack.
      //
      fseek (OutFptr, DataTableHeaderOffset, SEEK_SET);
    }
    //
    // Now go through all the IFR packs and write them out, along with variable
    // data referenced by each. Note that multiple IFR forms can refer to the
    // same variables, so the same variable data could be duplicated in multiple
    // data packs.
    //
    Index = FIRST_HII_PACK_HANDLE;
    while (IfrGetIfrPack (Index, &IfrPack, &TempGuid) == STATUS_SUCCESS) {
      //
      // Initialize the data table header
      //
      memset (&DataTableHeader, 0, sizeof (EFI_HII_DATA_TABLE));
      memcpy (&DataTableHeader.PackageGuid, &PackageGuid, sizeof (EFI_GUID));
      //
      // If we didn't have strings, then the HiiHandle should be just Index,
      // rather than Index+1. But since the HiiHandle is not required to start
      // with 1, we'll let it be Index+1.
      //
      DataTableHeader.HiiHandle     = (EFI_HII_HANDLE) (Index + 1);
      DataTableHeader.DataTableSize = sizeof (EFI_HII_DATA_TABLE);
      //
      // Save the file offset of the data table header so we can write an updated
      // version later.
      //
      DataTableHeaderOffset = ftell (OutFptr);
      if (mGlobals.Verbose) {
        fprintf (stdout, "writing data table header to 0x%X\n", ftell (OutFptr));
      }

      if (fwrite ((void *) &DataTableHeader, sizeof (EFI_HII_DATA_TABLE), 1, OutFptr) != 1) {
        Error (NULL, 0, 0, "failed to write Data Table Header to output file", NULL);
        goto Done;
      }

      DataTableHeader.IfrDataOffset = DataTableHeader.DataTableSize;
      if (fwrite (IfrPack, IfrPack->Header.Length, 1, OutFptr) != 1) {
        Error (NULL, 0, 0, OutputFileName, "failed to write IFR pack to output file");
        goto Done;
      }

      DataTableHeader.DataTableSize += IfrPack->Header.Length;
      //
      // Go through all the variable packs and see if this IFR references each. If the
      // IFR does reference it, then add the variable pack to the output.
      //
      if (mGlobals.NoVarPacks == 0) {
        VarPackIndex = 0;
        while (IfrGetVarPack (VarPackIndex, &VarPack) == STATUS_SUCCESS) {
          //
          // See if the IFR references this variable pack
          //
          if (IfrReferencesVarPack (Index, VarPack) == STATUS_SUCCESS) {
            //
            // If this is the first variable pack, then set the offset in
            // the data table header.
            //
            if (DataTableHeader.VariableDataOffset == 0) {
              DataTableHeader.VariableDataOffset = DataTableHeader.DataTableSize;
            }
            //
            // Write the variable pack
            //
            if (fwrite (VarPack, VarPack->Header.Length, 1, OutFptr) != 1) {
              Error (NULL, 0, 0, OutputFileName, "failed to write variable pack to output file");
              goto Done;
            }

            DataTableHeader.NumberOfVariableData++;
            DataTableHeader.DataTableSize += VarPack->Header.Length;
          }

          VarPackIndex++;
        }
      }
      //
      // Write the updated data table header
      //
      fseek (OutFptr, DataTableHeaderOffset, SEEK_SET);
      if (fwrite ((void *) &DataTableHeader, sizeof (EFI_HII_DATA_TABLE), 1, OutFptr) != 1) {
        Error (NULL, 0, 0, "failed to write Data Table Header to output file", NULL);
        goto Done;
      }

      fseek (OutFptr, 0, SEEK_END);
      //
      // Next IFR pack
      //
      Index++;
    }
  }

  Status = STATUS_SUCCESS;
Done:
  IfrParseEnd ();
  StringEnd ();
  if (Buffer != NULL) {
    free (Buffer);
  }

  if (InFptr != NULL) {
    fclose (InFptr);
  }

  if (OutFptr != NULL) {
    fclose (OutFptr);
  }

  return Status;
}

/******************************************************************************/
static
STATUS
ProcessIfrFiles (
  FILE_NAME_LIST  *FileName
  )
/*++

Routine Description:

  Given a linked list of pack file names, read in each IFR pack file
  and process the contents.
  
Arguments:

  FileName    - pointer to linked list of input pack file names

Returns:

  STATUS_SUCCESS    - if successful
  STATUS_ERROR      - otherwise
  
--*/
{
  FILE                *InFptr;
  char                *Buffer;
  long                BufferSize;
  STATUS              Status;
  STATUS              IfrStatus;
  int                 Handle;
  EFI_GUID            FormsetGuid;
  EFI_HII_PACK_HEADER *PackHeader;
  //
  // Process each input IFR file
  //
  Status  = STATUS_ERROR;
  Handle  = 1;
  InFptr  = NULL;
  Buffer  = NULL;
  while (FileName != NULL) {
    //
    // Only process IFR pack files
    //
    if (FileName->Tag != EFI_HII_IFR) {
      FileName = FileName->Next;
      continue;
    }
    //
    // Open the input file, then read the contents
    //
    if ((InFptr = fopen (FileName->FileName, "rb")) == NULL) {
      Error (NULL, 0, 0, FileName->FileName, "failed to open input IFR file");
      goto Done;
    }

    fseek (InFptr, 0, SEEK_END);
    BufferSize = ftell (InFptr);
    fseek (InFptr, 0, SEEK_SET);
    Buffer = (char *) malloc (BufferSize);
    if (Buffer == NULL) {
      Error (NULL, 0, 0, "memory allocation failure", NULL);
      goto Done;
    }

    if (fread (Buffer, BufferSize, 1, InFptr) != 1) {
      Error (NULL, 0, 0, FileName->FileName, "failed to read file contents");
      goto Done;
    }

    fclose (InFptr);
    InFptr = NULL;
    //
    // Check the buffer contents -- better be an IFR pack
    //
    if (BufferSize < sizeof (EFI_HII_PACK_HEADER)) {
      Error (NULL, 0, 0, FileName->FileName, "file is not large enough to contain an IFR pack");
      goto Done;
    }

    PackHeader = (EFI_HII_PACK_HEADER *) Buffer;
    if (PackHeader->Type != EFI_HII_IFR) {
      Error (NULL, 0, 0, FileName->FileName, "file does not appear to be an IFR pack");
      goto Done;
    }
    //
    // Process the contents
    //
    memset ((void *) &FormsetGuid, 0, sizeof (EFI_GUID));
    IfrStatus = IfrParsePack (Handle, (EFI_HII_IFR_PACK *) PackHeader, &FormsetGuid);
    if (IfrStatus != STATUS_SUCCESS) {
      goto Done;
    }

    Handle++;
    free (Buffer);
    Buffer    = NULL;
    FileName  = FileName->Next;
  }

  Status = STATUS_SUCCESS;
Done:
  if (InFptr != NULL) {
    fclose (InFptr);
  }

  if (Buffer != NULL) {
    free (Buffer);
  }

  return Status;
}

static
STATUS
EmitDefaults (
  FILE_NAME_LIST  *HiiExportFiles,
  int             MfgDefaults,
  int             NoEmptyVarPacks
  )
/*++

Routine Description:

  Given a linked list of HII export files, read in each file,
  process the contents, and then emit variable packs.
  
Arguments:

  HiiExportFiles  - linked list of HII export files to process
  MfgDefaults     - emit manufacturing defaults
  NoEmptyVarPacks - don't emit variable packs if they are 0-length

Returns:

  STATUS_SUCCESS    - if successful
  STATUS_ERROR      - otherwise
  
--*/
{
  int                   HiiHandle;
  FILE                  *OutFptr;
  FILE                  *InFptr;
  EFI_HII_VARIABLE_PACK *VarPack;
  CHAR8                 OutFileName[MAX_PATH];
  CHAR8                 GuidString[100];
  STATUS                Status;
  CHAR8                 *Buffer;
  int                   FileSize;
  int                   DataTableIndex;
  EFI_HII_EXPORT_TABLE  *HiiExportTableHeader;
  EFI_HII_DATA_TABLE    *DataTableHeader;
  //
  // Init locals
  //
  HiiHandle = FIRST_HII_PACK_HANDLE;
  Buffer    = NULL;
  InFptr    = NULL;
  OutFptr   = NULL;
  Status    = STATUS_ERROR;
  //
  // Initialize our IFR parser
  //
  IfrParseInit ();
  //
  // Process each input HII export file
  //
  while (HiiExportFiles != NULL) {
    if (mGlobals.Verbose) {
      fprintf (stdout, "Processing file %s\n", HiiExportFiles->FileName);
    }
    //
    // Read in the entire file contents
    //
    if ((InFptr = fopen (HiiExportFiles->FileName, "rb")) == NULL) {
      Error (NULL, 0, 0, HiiExportFiles->FileName, "failed to open HII export file for reading");
      goto Done;
    }

    fseek (InFptr, 0, SEEK_END);
    FileSize = (int) ftell (InFptr);
    fseek (InFptr, 0, SEEK_SET);
    Buffer = (CHAR8 *) malloc (FileSize);
    if (Buffer == NULL) {
      Error (NULL, 0, 0, "memory allocation failure", NULL);
      goto Done;
    }

    if (fread (Buffer, FileSize, 1, InFptr) != 1) {
      Error (NULL, 0, 0, HiiExportFiles->FileName, "failed to read file contents");
      goto Done;
    }

    fclose (InFptr);
    InFptr                = NULL;
    HiiExportTableHeader  = (EFI_HII_EXPORT_TABLE *) Buffer;
    //
    // Walk all the data tables
    //
    DataTableHeader = (EFI_HII_DATA_TABLE *) (HiiExportTableHeader + 1);
    for (DataTableIndex = 0; DataTableIndex < (int) HiiExportTableHeader->NumberOfHiiDataTables; DataTableIndex++) {
      //
      // Make sure we're still pointing into our buffer
      //
      if (((char *) DataTableHeader < Buffer) || ((char *) DataTableHeader > Buffer + FileSize)) {
        Error (NULL, 0, 0, "bad data table size in input file", NULL);
        goto Done;
      }
      //
      // If there is an IFR pack, parse it
      //
      HiiHandle++;
      if (DataTableHeader->IfrDataOffset != 0) {
        if (IfrParsePack (
            HiiHandle,
            (EFI_HII_IFR_PACK *) ((char *) DataTableHeader + DataTableHeader->IfrDataOffset),
            &DataTableHeader->PackageGuid
            ) != STATUS_SUCCESS
            ) {
          goto Done;
        }
      }
      //
      // Next data pack
      //
      DataTableHeader = (EFI_HII_DATA_TABLE *) ((char *) DataTableHeader + DataTableHeader->DataTableSize);
    }

    free (Buffer);
    Buffer = NULL;
    //
    // Next input file
    //
    HiiExportFiles = HiiExportFiles->Next;
  }
  //
  // Now create defaults
  //
  if (IfrSetDefaults (MfgDefaults) != STATUS_SUCCESS) {
    goto Done;
  }
  //
  // Now retrieve each variable pack and write it out to a GUID-VarName.hpk file
  //
  HiiHandle = 0;
  do {
    Status = IfrGetVarPack (HiiHandle, &VarPack);
    if (Status == STATUS_SUCCESS) {
      //
      // Check for variable data length of 0
      //
      if ((NoEmptyVarPacks == 0) ||
          ((VarPack->Header.Length - sizeof (EFI_HII_VARIABLE_PACK) - VarPack->VariableNameLength) != 0)
          ) {
        //
        // Open the output file and write the variable pack
        //
        GuidToString (&VarPack->VariableGuid, GuidString);
        if (MfgDefaults) {
          sprintf (
            OutFileName,
            "%s-%S-MfgDefaults%s",
            GuidString,
            (CHAR16 *) (VarPack + 1),
            DEFAULT_HII_PACK_FILENAME_EXTENSION
            );
        } else {
          sprintf (
            OutFileName,
            "%s-%S-Defaults%s",
            GuidString,
            (CHAR16 *) (VarPack + 1),
            DEFAULT_HII_PACK_FILENAME_EXTENSION
            );
        }

        if (mGlobals.Verbose) {
          fprintf (
            stdout,
            "Creating %svariable defaults pack file %s\n",
            MfgDefaults ? "manufacturing " : "",
            OutFileName
            );
        }

        if ((OutFptr = fopen (OutFileName, "wb")) == NULL) {
          Error (NULL, 0, 0, OutFileName, "failed to open output file for writing", NULL);
          goto Done;
        }

        if (fwrite ((void *) VarPack, VarPack->Header.Length, 1, OutFptr) != 1) {
          Error (NULL, 0, 0, OutFileName, "failed to write defaults to output file");
          goto Done;
        }

        fclose (OutFptr);
        OutFptr = NULL;
      } else {
        //
        // Print a message that we skipped one if in verbose mode
        //
        if (mGlobals.Verbose) {
          GuidToString (&VarPack->VariableGuid, GuidString);
          if (MfgDefaults) {
            sprintf (
              OutFileName,
              "%s-%S-MfgDefaults%s",
              GuidString,
              (CHAR16 *) (VarPack + 1),
              DEFAULT_HII_PACK_FILENAME_EXTENSION
              );
          } else {
            sprintf (
              OutFileName,
              "%s-%S-Defaults%s",
              GuidString,
              (CHAR16 *) (VarPack + 1),
              DEFAULT_HII_PACK_FILENAME_EXTENSION
              );
          }

          fprintf (
            stdout,
            "Skipping 0-length %svariable defaults pack file %s\n",
            MfgDefaults ? "manufacturing " : "",
            OutFileName
            );
        }
      }
    }

    HiiHandle++;
  } while (Status == STATUS_SUCCESS);
  Status = STATUS_SUCCESS;
Done:
  IfrParseEnd ();
  if (Buffer != NULL) {
    free (Buffer);
  }

  if (InFptr != NULL) {
    fclose (InFptr);
  }

  if (OutFptr != NULL) {
    fclose (OutFptr);
  }

  return Status;
}

static
void
FreeGlobals (
  VOID
  )
/*++

Routine Description:

  Free up an memory we allocated so we can exit cleanly
  
Arguments:

Returns: NA

--*/
{
  FILE_NAME_LIST  *Next;
  //
  // Free up input pack file names
  //
  while (mGlobals.PackFileNames != NULL) {
    Next = mGlobals.PackFileNames->Next;
    free (mGlobals.PackFileNames);
    mGlobals.PackFileNames = Next;
  }
  //
  // Free up input HII export file names
  //
  while (mGlobals.HiiExportFileNames != NULL) {
    Next = mGlobals.HiiExportFileNames->Next;
    free (mGlobals.HiiExportFileNames);
    mGlobals.HiiExportFileNames = Next;
  }
}

static
STATUS
DumpHiiExportFile (
  char    *HiiExportFileName,
  char    *OutputFileName
  )
/*++

Routine Description:

  Dump the contents of an HII export file for debug purposes
  
Arguments:

  HiiExportFileName - name of input HII export file
  OutputFileName    - name of output file to dump contents

Returns: 
  STATUS_SUCCESS  - no problems
  STATUS_ERROR    - problems encountered processing the file

--*/
{
  FILE                *InFptr;

  FILE                *OutFptr;
  char                *Buffer;
  char                *BufferStart;
  char                *BufferEnd;
  int                 BufferSize;
  STATUS              Status;
  char                GuidString[100];
  int                 Counter;
  int                 NumberOfTables;
  EFI_HII_DATA_TABLE  *DataTableHeader;
  EFI_GUID              HiiExportRevisionGuid = EFI_HII_PROTOCOL_GUID;
  //
  // Init locals
  //
  InFptr      = NULL;
  OutFptr     = NULL;
  BufferStart = NULL;
  Status      = STATUS_ERROR;
  //
  // Open the input file
  //
  if ((InFptr = fopen (HiiExportFileName, "rb")) == NULL) {
    Error (NULL, 0, 0, HiiExportFileName, "failed to open input HII export file for reading");
    goto Done;
  }
  //
  // Open the output file
  //
  if ((OutFptr = fopen (OutputFileName, "w")) == NULL) {
    Error (NULL, 0, 0, OutputFileName, "failed to open output file for writing");
    goto Done;
  }
  //
  // Get the file size, then allocate a buffer and read in the file contents.
  //
  fseek (InFptr, 0, SEEK_END);
  BufferSize = (int) ftell (InFptr);
  fseek (InFptr, 0, SEEK_SET);
  BufferStart = (char *) malloc (BufferSize);
  if (BufferStart == NULL) {
    Error (NULL, 0, 0, "memory allocation failure", NULL);
    goto Done;
  }

  if (fread (BufferStart, BufferSize, 1, InFptr) != 1) {
    Error (NULL, 0, 0, HiiExportFileName, "error reading file contents");
    goto Done;
  }

  fclose (InFptr);
  InFptr = NULL;
  //
  // Crude check of the input data -- check the size and GUID
  //
  if (BufferSize < sizeof (EFI_HII_EXPORT_TABLE)) {
    Error (NULL, 0, 0, HiiExportFileName, "files not large enough to contain an HII export table header");
    goto Done;
  }

  if (memcmp (&((EFI_HII_EXPORT_TABLE *) BufferStart)->Revision, &HiiExportRevisionGuid, sizeof (EFI_GUID)) != 0) {
    Error (NULL, 0, 0, HiiExportFileName, "invalid HII export revision GUID -- is this an HII export file?");
    //
    // See if it's a HII pack file
    //
    TestDumpHiiPack (OutFptr, BufferStart, BufferSize);
    goto Done;
  }
  //
  // Now walk the export data
  //
  Buffer    = BufferStart;
  BufferEnd = BufferStart + BufferSize;
  //
  // Dump the header
  //
  fprintf (OutFptr, "HII dump of file %s\n\n", HiiExportFileName);
  NumberOfTables = ((EFI_HII_EXPORT_TABLE *) Buffer)->NumberOfHiiDataTables;
  fprintf (OutFptr, "Number of data tables:  %d\n", NumberOfTables);
  GuidToString (&((EFI_HII_EXPORT_TABLE *) Buffer)->Revision, GuidString);
  fprintf (OutFptr, "HII export revision:    %s\n", GuidString);
  //
  // Now walk the data tables
  //
  Buffer += sizeof (EFI_HII_EXPORT_TABLE);
  for (Counter = 0; Counter < NumberOfTables; Counter++) {
    DataTableHeader = (EFI_HII_DATA_TABLE *) Buffer;
    fprintf (OutFptr, "----------------------------------------------------------\n");
    fprintf (OutFptr, "  DataTable at offset 0x%08X\n", (int) Buffer - (int) BufferStart);
    fprintf (OutFptr, "    HII Handle:                            0x%08X\n", DataTableHeader->HiiHandle);
    GuidToString (&DataTableHeader->PackageGuid, GuidString);
    fprintf (OutFptr, "    Package GUID:                          %s\n", GuidString);
    fprintf (OutFptr, "    Data table size:                       0x%08X\n", DataTableHeader->DataTableSize);
    fprintf (OutFptr, "    IFR data offset:                       0x%08X\n", DataTableHeader->IfrDataOffset);
    fprintf (OutFptr, "    String data offset:                    0x%08X\n", DataTableHeader->StringDataOffset);
    fprintf (OutFptr, "    Variable data offset:                  0x%08X\n", DataTableHeader->VariableDataOffset);
    fprintf (OutFptr, "    Device path offset:                    0x%08X\n", DataTableHeader->DevicePathOffset);
    fprintf (OutFptr, "    Number of variable data:               0x%08X\n", DataTableHeader->NumberOfVariableData);
    fprintf (OutFptr, "    Number of languages:                   0x%08X\n", DataTableHeader->NumberOfLanguages);
    //
    // Dump strings
    //
    if (DataTableHeader->StringDataOffset != 0) {
      DumpStringPack (
        OutFptr,
        (EFI_HII_STRING_PACK *) ((char *) DataTableHeader + DataTableHeader->StringDataOffset),
        DataTableHeader->StringDataOffset,
        6
        );
    }
    //
    // Dump IFR
    //
    if (DataTableHeader->IfrDataOffset != 0) {
      DumpIfrPack (
        OutFptr,
        (EFI_HII_IFR_PACK *) ((char *) DataTableHeader + DataTableHeader->IfrDataOffset),
        DataTableHeader->IfrDataOffset,
        6
        );
    }
    //
    // Dump variables
    //
    if (DataTableHeader->VariableDataOffset != 0) {
      DumpVariablePacks (
        OutFptr,
        (EFI_HII_VARIABLE_PACK *) ((char *) DataTableHeader + DataTableHeader->VariableDataOffset),
        DataTableHeader->NumberOfVariableData,
        DataTableHeader->VariableDataOffset,
        6
        );
    }
    //
    // Dump device path
    //
    //
    // Check position before advancing
    //
    if ((Buffer + DataTableHeader->DataTableSize < Buffer) || (Buffer + DataTableHeader->DataTableSize > BufferEnd)) {
      Error (NULL, 0, 0, HiiExportFileName, "bad data table size at offset 0x%X", (int) Buffer - (int) BufferStart);
      goto Done;
    }

    Buffer += DataTableHeader->DataTableSize;
  }

  Status = STATUS_SUCCESS;
Done:
  if (OutFptr != NULL) {
    fclose (OutFptr);
  }

  if (InFptr != NULL) {
    fclose (InFptr);
  }

  if (BufferStart != NULL) {
    free (BufferStart);
  }

  return Status;
}

static
void
DumpIfrPack (
  FILE                  *OutFptr,
  EFI_HII_IFR_PACK      *Pack,
  int                   BaseOffset,
  int                   Indent
  )
/*++

Routine Description:

  Dump the contents of an IFR pack for debug purposes
  
Arguments:

  OutFptr         - file pointer to which to dump the output
  Pack            - pointer to IFR pack to dump
  BaseOffset      - offset from which Pack starts in its parent data table
  Indent          - indent this many spaces when printing text to OutFptr

Returns: 
  NA

--*/
{
  EFI_IFR_FORM_SET  *IfrFormSet;
  char              GuidString[100];
  if (Pack->Header.Type != EFI_HII_IFR) {
    Error (NULL, 0, 0, "found non-IFR pack type at IFR data offset", NULL);
    return ;
  }

  fprintf (OutFptr, "%*cIFR pack at offset      0x%08X\n", Indent, ' ', BaseOffset);
  fprintf (OutFptr, "%*c  Length                0x%08X\n", Indent, ' ', Pack->Header.Length);
  //
  // Get the GUID from the formset
  //
  IfrFormSet = (EFI_IFR_FORM_SET *) (Pack + 1);
  GuidToString (&IfrFormSet->Guid, GuidString);
  fprintf (OutFptr, "%*c  Variable GUID         %s\n", Indent, ' ', GuidString);
  //
  // Print the IFR formset size, with a note indicating if it's a min (likely stub)
  // formset
  //
  if (Pack->Header.Length == sizeof (EMPTY_FORMSET_PACK)) {
    fprintf (
      OutFptr,
      "%*c  IFR formset size      0x%08X (empty formset)\n",
      Indent,
      ' ',
      Pack->Header.Length - sizeof (EFI_HII_IFR_PACK)
      );
  } else {
    fprintf (
      OutFptr,
      "%*c  IFR formset size      0x%08X\n",
      Indent,
      ' ',
      Pack->Header.Length - sizeof (EFI_HII_IFR_PACK)
      );
  }
  //
  // Dump raw bytes -- not much use
  //
}

static
void
DumpVariablePacks (
  FILE                  *OutFptr,
  EFI_HII_VARIABLE_PACK *Pack,
  int                   NumPacks,
  int                   BaseOffset,
  int                   Indent
  )
/*++

Routine Description:

  Dump the contents of an IFR pack for debug purposes
  
Arguments:

  OutFptr         - file pointer to which to dump the output
  Pack            - pointer to variable pack to dump
  NumPacks        - number of packs in Pack[] array
  BaseOffset      - offset from which Pack starts in its parent data table
  Indent          - indent this many spaces when printing text to OutFptr

Returns: 
  NA

--*/
{
  int   Count;

  int   Len;
  char  GuidString[100];

  for (Count = 0; Count < NumPacks; Count++) {
    if (Pack->Header.Type != EFI_HII_VARIABLE) {
      Error (NULL, 0, 0, "found non-variable pack type in variable pack array", NULL);
      return ;
    }

    fprintf (OutFptr, "%*cVariable pack at offset 0x%08X\n", Indent, ' ', BaseOffset);
    fprintf (OutFptr, "%*c  Length                0x%08X\n", Indent, ' ', Pack->Header.Length);
    GuidToString (&Pack->VariableGuid, GuidString);
    fprintf (OutFptr, "%*c  Variable GUID         %s\n", Indent, ' ', GuidString);
    fprintf (OutFptr, "%*c  Variable Name         %S\n", Indent, ' ', (CHAR16 *) (Pack + 1));
    Len = sizeof (EFI_HII_VARIABLE_PACK) + Pack->VariableNameLength;
    fprintf (OutFptr, "%*c  Variable Size         0x%08X\n", Indent, ' ', Pack->Header.Length - Len);
    //
    // Dump raw bytes
    //
    DumpRawBytes (OutFptr, (char *) Pack + Len, Pack->Header.Length - Len, Len, Indent + 2);
    BaseOffset += Pack->Header.Length;
    Pack = (EFI_HII_VARIABLE_PACK *) ((char *) Pack + Pack->Header.Length);
  }
}

static
void
DumpStringPack (
  FILE                  *OutFptr,
  EFI_HII_STRING_PACK   *Pack,
  int                   BaseOffset,
  int                   Indent
  )
/*++

Routine Description:

  Dump the contents of a string pack array for debug purposes
  
Arguments:

  OutFptr         - file pointer to which to dump the output
  Pack            - pointer to string pack array to dump
  BaseOffset      - offset from which Pack starts in its parent data table
  Indent          - indent this many spaces when printing text to OutFptr

Returns: 
  NA

--*/
{
  int     Count;
  int     *IndexPtr;
  CHAR16  *WCPtr;
  //
  // String pack array is terminated with a zero-length string pack
  //
  while (Pack->Header.Length > 0) {
    if (Pack->Header.Type != EFI_HII_STRING) {
      Error (NULL, 0, 0, "found non-string pack type in string pack array", NULL);
      return ;
    }

    fprintf (OutFptr, "%*cString pack at offset   0x%08X\n", Indent, ' ', BaseOffset);
    fprintf (OutFptr, "%*c  Length                0x%08X\n", Indent, ' ', Pack->Header.Length);
    fprintf (
      OutFptr,
      "%*c  Language              %S\n",
      Indent,
      ' ',
      (CHAR16 *) ((char *) Pack + Pack->LanguageNameString)
      );
    fprintf (
      OutFptr,
      "%*c  Printable Language    %S\n",
      Indent,
      ' ',
      (CHAR16 *) ((char *) Pack + Pack->PrintableLanguageName)
      );
    fprintf (OutFptr, "%*c  Number of strings     0x%08X\n", Indent, ' ', Pack->NumStringPointers);
    fprintf (OutFptr, "%*c  Attributes            0x%08X\n", Indent, ' ', Pack->Attributes);
    IndexPtr = (int *) (Pack + 1);
    //
    // Dump string data
    //
    if (mGlobals.DumpStrings) {
      for (Count = 0; Count < (int) Pack->NumStringPointers; Count++) {
        fprintf (OutFptr, "%*c    String 0x%04X: ", Indent, ' ', Count);
        //
        // Print raw hex bytes
        //
        for (WCPtr = (CHAR16 *) ((char *) Pack +*IndexPtr); *WCPtr != 0; WCPtr++) {
          fprintf (OutFptr, "%02X ", (unsigned int) *WCPtr);
        }

        fprintf (OutFptr, "00\n");
        IndexPtr++;
      }
    }

    BaseOffset += Pack->Header.Length;
    Pack = (EFI_HII_STRING_PACK *) ((char *) Pack + Pack->Header.Length);
  }
}

static
void
TestDumpHiiPack (
  FILE    *OutFptr,
  char    *Buffer,
  int     BufferSize
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  OutFptr     - GC_TODO: add argument description
  Buffer      - GC_TODO: add argument description
  BufferSize  - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  EFI_HII_PACK_HEADER *PackHeader;

  PackHeader = (EFI_HII_PACK_HEADER *) Buffer;
  //
  // Check size match
  //
  if (PackHeader->Length != (unsigned int) BufferSize) {
    return ;
  }
  //
  // Check type
  //
  switch (PackHeader->Type) {
  case EFI_HII_STRING:
    fprintf (stdout, "Dumping as string pack\n");
    DumpStringPack (OutFptr, (EFI_HII_STRING_PACK *) Buffer, 0, 2);
    break;

  case EFI_HII_IFR:
    fprintf (stdout, "Dumping as IFR pack\n");
    DumpIfrPack (OutFptr, (EFI_HII_IFR_PACK *) Buffer, 0, 2);
    break;

  case EFI_HII_VARIABLE:
    fprintf (stdout, "Dumping as IFR pack\n");
    DumpVariablePacks (OutFptr, (EFI_HII_VARIABLE_PACK *) Buffer, 1, 0, 2);
    break;
  }
}

static
void
DumpRawBytes (
  FILE                  *OutFptr,
  char                  *Buffer,
  int                   Count,
  int                   BaseOffset,
  int                   Indent
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  OutFptr     - GC_TODO: add argument description
  Buffer      - GC_TODO: add argument description
  Count       - GC_TODO: add argument description
  BaseOffset  - GC_TODO: add argument description
  Indent      - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  int Counter;

  for (Counter = 0; Counter < Count; Counter++) {
    if ((Counter & 0xF) == 0) {
      if (Counter != 0) {
        fprintf (OutFptr, "\n%*c%08X ", Indent, ' ', Counter);
      } else {
        fprintf (OutFptr, "\n%*c%08X ", Indent, ' ', Counter);
      }
    }

    fprintf (OutFptr, "%02X ", (unsigned int) (unsigned char) *Buffer);
    Buffer++;
  }

  fprintf (OutFptr, "\n");
}

void
GuidToString (
  EFI_GUID   *Guid,
  char       *Str
  )
/*++

Routine Description:

  Given a pointer to a GUID, sprint the value into a string
  
Arguments:

  Guid   - pointer to input GUID
  Str    - pointer to outgoing printed GUID value

Returns:
  NA
  
--*/
{
  sprintf (
    Str,
    "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
    Guid->Data1,
    Guid->Data2,
    Guid->Data3,
    Guid->Data4[0],
    Guid->Data4[1],
    Guid->Data4[2],
    Guid->Data4[3],
    Guid->Data4[4],
    Guid->Data4[5],
    Guid->Data4[6],
    Guid->Data4[7]
    );
}

int
FindFilesCallback (
  char *FoundFileName
  )
/*++

Routine Description:

  Callback function used to get files matching a file mask. This
  function is called when the command-line arguments to this utility
  are parsed and the user specified "-s Path FileMask" to process
  all HII export files in Path and its subdirectories that match
  FileMask.
  
Arguments:

  FoundFileName - name of file found.

Returns:
  non-zero    - caller should halt processing
  zero        - no problems while processing FoundFileName
  
--*/
{
  FILE_NAME_LIST  *FileName;

  FILE_NAME_LIST  *TempFileName;

  FileName = (FILE_NAME_LIST *) malloc (sizeof (FILE_NAME_LIST));
  if (FileName == NULL) {
    Error (NULL, 0, 0, "memory allocation failure", NULL);
    return STATUS_ERROR;
  }

  memset ((void *) FileName, 0, sizeof (FILE_NAME_LIST));
  strcpy (FileName->FileName, FoundFileName);
  if (mGlobals.HiiExportFileNames == NULL) {
    mGlobals.HiiExportFileNames = FileName;
  } else {
    //
    // Add to the end of the list
    //
    for (TempFileName = mGlobals.HiiExportFileNames; TempFileName->Next != NULL; TempFileName = TempFileName->Next)
      ;
    TempFileName->Next = FileName;
  }

  return 0;
}

static
STATUS
ProcessArgs (
  int   Argc,
  char  *Argv[]
  )
/*++

Routine Description:

  Process the command line arguments
  
Arguments:

  As per standard C main()

Returns:

  STATUS_SUCCESS    - if successful
  STATUS_ERROR      - otherwise
  
--*/
// GC_TODO:    Argc - add argument and description to function comment
// GC_TODO:    ] - add argument and description to function comment
{
  FILE_NAME_LIST      *FileName;

  FILE_NAME_LIST      *TempFileName;
  FILE                *InFptr;
  EFI_HII_PACK_HEADER PackHeader;

  memset ((void *) &mGlobals, 0, sizeof (mGlobals));
  //
  // Skip program name
  //
  Argc--;
  Argv++;

  if (Argc == 0) {
    Usage ();
    return STATUS_ERROR;
  }
  //
  // First arg must be one of create, merge, defaults, or dump
  //
  if (_stricmp (Argv[0], "create") == 0) {
    mGlobals.Mode = MODE_CREATE_HII_EXPORT;
  } else if (_stricmp (Argv[0], "merge") == 0) {
    mGlobals.Mode = MODE_MERGE_HII_EXPORTS;
  } else if (_stricmp (Argv[0], "defaults") == 0) {
    mGlobals.Mode = MODE_EMIT_DEFAULTS;
  } else if (_stricmp (Argv[0], "dump") == 0) {
    mGlobals.Mode = MODE_DUMP_HII_EXPORT;
  } else if (strcmp (Argv[0], "-?") == 0) {
    Usage ();
    return STATUS_ERROR;
  } else {
    Error (NULL, 0, 0, Argv[0], "unrecognized mode");
    return STATUS_ERROR;
  }

  Argv++;
  Argc--;
  //
  // Process until no more args.
  //
  while (Argc > 0) {
    if (_stricmp (Argv[0], "-o") == 0) {
      //
      // -o option to specify the output file
      //
      if ((Argc <= 1) || (Argv[1][0] == '-')) {
        Error (UTILITY_NAME, 0, 0, Argv[0], "missing output file name");
        return STATUS_ERROR;
      }

      if (mGlobals.OutputFileName[0] == 0) {
        mGlobals.OutputFileName[MAX_PATH - 1] = 0;
        strncpy (mGlobals.OutputFileName, Argv[1], MAX_PATH - 1);
      } else {
        Error (UTILITY_NAME, 0, 0, Argv[1], "-o option already specified with '%s'", mGlobals.OutputFileName);
        return STATUS_ERROR;
      }

      Argv++;
      Argc--;
    } else if (_stricmp (Argv[0], "-mfg") == 0) {
      mGlobals.MfgFlag = 1;
    } else if (_stricmp (Argv[0], "-g") == 0) {
      //
      // -g option to specify the guid
      //
      if ((Argc <= 1) || (Argv[1][0] == '-')) {
        Error (UTILITY_NAME, 0, 0, Argv[0], "missing GUID");
        return STATUS_ERROR;
      }

      StringToGuid (Argv[1], &mGlobals.Guid);
      mGlobals.GuidSpecified = 1;
      Argv++;
      Argc--;
    } else if (_stricmp (Argv[0], "-v") == 0) {
      mGlobals.Verbose = 1;
    } else if (_stricmp (Argv[0], "-p") == 0) {
      //
      // -p option to specify an input pack file. Only valid for 'create' mode
      //
      if (mGlobals.Mode != MODE_CREATE_HII_EXPORT) {
        Error (NULL, 0, 0, Argv[0], "option only valid in 'create' mode");
        return STATUS_ERROR;
      }

      if ((Argc <= 1) || (Argv[1][0] == '-')) {
        Error (UTILITY_NAME, 0, 0, Argv[0], "missing pack file name");
        return STATUS_ERROR;
      }
      //
      // Consume arguments until next -arg or end
      //
      do {
        Argv++;
        Argc--;
        //
        // Open the file, read the pack header, and figure out what type of
        // HII pack it is.
        //
        if ((InFptr = fopen (Argv[0], "rb")) == NULL) {
          Error (NULL, 0, 0, Argv[0], "failed to open input HII pack file for reading");
          return STATUS_ERROR;
        }

        if (fread (&PackHeader, sizeof (EFI_HII_PACK_HEADER), 1, InFptr) != 1) {
          Error (NULL, 0, 0, Argv[0], "failed to read pack header from input HII pack file");
          fclose (InFptr);
          return STATUS_ERROR;
        }

        fclose (InFptr);
        if ((PackHeader.Type != EFI_HII_STRING) &&
            (PackHeader.Type != EFI_HII_IFR) &&
            (PackHeader.Type != EFI_HII_VARIABLE)
            ) {
          Error (NULL, 0, 0, Argv[0], "unsupported HII pack type 0x%X", (unsigned int) PackHeader.Type);
          return STATUS_ERROR;
        }
        //
        // Add this file name to our list of pack files
        //
        FileName = (FILE_NAME_LIST *) malloc (sizeof (FILE_NAME_LIST));
        if (FileName == NULL) {
          Error (NULL, 0, 0, "memory allocation failure", NULL);
          return STATUS_ERROR;
        }

        memset ((void *) FileName, 0, sizeof (FILE_NAME_LIST));
        FileName->Tag = (int) PackHeader.Type;
        strcpy (FileName->FileName, Argv[0]);
        if (mGlobals.PackFileNames == NULL) {
          mGlobals.PackFileNames = FileName;
        } else {
          //
          // Add to the end of the list
          //
          for (TempFileName = mGlobals.PackFileNames; TempFileName->Next != NULL; TempFileName = TempFileName->Next)
            ;
          TempFileName->Next = FileName;
        }
      } while ((Argc > 1) && (Argv[1][0] != '-'));
    } else if (_stricmp (Argv[0], "-noemptyvarpacks") == 0) {
      mGlobals.NoEmptyVarPacks = 1;
    } else if (_stricmp (Argv[0], "-novarpacks") == 0) {
      mGlobals.NoVarPacks = 1;
    } else if (_stricmp (Argv[0], "-x") == 0) {
      //
      // -x option to specify an input HII export file name. Not valid for 'create' mode
      //
      if (mGlobals.Mode == MODE_CREATE_HII_EXPORT) {
        Error (NULL, 0, 0, Argv[0], "option is not valid in 'create' mode");
        return STATUS_ERROR;
      }

      if ((Argc <= 1) || (Argv[1][0] == '-')) {
        Error (UTILITY_NAME, 0, 0, Argv[0], "missing HII export input file name");
        return STATUS_ERROR;
      }
      //
      // Consume arguments until next -arg or end
      //
      do {
        Argv++;
        Argc--;
        //
        // Add this file name to our list of export files
        //
        FileName = (FILE_NAME_LIST *) malloc (sizeof (FILE_NAME_LIST));
        if (FileName == NULL) {
          Error (NULL, 0, 0, "memory allocation failure", NULL);
          return STATUS_ERROR;
        }

        memset ((void *) FileName, 0, sizeof (FILE_NAME_LIST));
        strcpy (FileName->FileName, Argv[0]);
        if (mGlobals.HiiExportFileNames == NULL) {
          mGlobals.HiiExportFileNames = FileName;
        } else {
          //
          // Add to the end of the list
          //
          for (TempFileName = mGlobals.HiiExportFileNames;
               TempFileName->Next != NULL;
               TempFileName = TempFileName->Next
              )
            ;
          TempFileName->Next = FileName;
        }
      } while ((Argc > 1) && (Argv[1][0] != '-'));
    } else if (_stricmp (Argv[0], "-dumpstrings") == 0) {
      mGlobals.DumpStrings = 1;
    } else if (_stricmp (Argv[0], "-s") == 0) {
      //
      // -s option to specify input HII export files using a path and file mask.
      // Only valid in merge mode
      //
      if (mGlobals.Mode != MODE_MERGE_HII_EXPORTS) {
        Error (NULL, 0, 0, Argv[0], "option only valid in 'merge' mode");
        return STATUS_ERROR;
      }

      if ((Argc <= 1) || (Argv[1][0] == '-')) {
        Error (UTILITY_NAME, 0, 0, Argv[0], "missing root directory name");
        return STATUS_ERROR;
      }

      if ((Argc <= 2) || (Argv[2][0] == '-')) {
        Error (UTILITY_NAME, 0, 0, Argv[0], "missing file mask");
        return STATUS_ERROR;
      }
      //
      // Call our function to process the directory and file mask. If
      // the directory does not start with c:\, then prepend cwd to it.
      //
      if (FindFiles (Argv[1], Argv[2], FindFilesCallback)) {
        Error (NULL, 0, 0, "failed to process matching files", "%s\\%s", Argv[1], Argv[2]);
        return STATUS_ERROR;
      }

      Argv += 2;
      Argc -= 2;
    } else if (_stricmp (Argv[0], "-p") == 0) {
      //
      // -p option to specify an input pack file. Only valid for 'create' mode
      //
      if (mGlobals.Mode != MODE_CREATE_HII_EXPORT) {
        Error (NULL, 0, 0, Argv[0], "option only valid in 'create' mode");
        return STATUS_ERROR;
      }

      if ((Argc <= 1) || (Argv[1][0] == '-')) {
        Error (UTILITY_NAME, 0, 0, Argv[0], "missing pack file name");
        return STATUS_ERROR;
      }
      //
      // Consume arguments until next -arg or end
      //
      do {
        Argv++;
        Argc--;
        //
        // Open the file, read the pack header, and figure out what type of
        // HII pack it is.
        //
        if ((InFptr = fopen (Argv[0], "rb")) == NULL) {
          Error (NULL, 0, 0, Argv[0], "failed to open input HII pack file for reading");
          return STATUS_ERROR;
        }

        if (fread (&PackHeader, sizeof (EFI_HII_PACK_HEADER), 1, InFptr) != 1) {
          Error (NULL, 0, 0, Argv[0], "failed to read pack header from input HII pack file");
          fclose (InFptr);
          return STATUS_ERROR;
        }

        fclose (InFptr);
        if ((PackHeader.Type != EFI_HII_STRING) &&
            (PackHeader.Type != EFI_HII_IFR) &&
            (PackHeader.Type != EFI_HII_VARIABLE)
            ) {
          Error (NULL, 0, 0, Argv[0], "unsupported HII pack type 0x%X", (unsigned int) PackHeader.Type);
          return STATUS_ERROR;
        }
        //
        // Add this file name to our list of pack files
        //
        FileName = (FILE_NAME_LIST *) malloc (sizeof (FILE_NAME_LIST));
        if (FileName == NULL) {
          Error (NULL, 0, 0, "memory allocation failure", NULL);
          return STATUS_ERROR;
        }

        memset ((void *) FileName, 0, sizeof (FILE_NAME_LIST));
        FileName->Tag = (int) PackHeader.Type;
        strcpy (FileName->FileName, Argv[0]);
        if (mGlobals.PackFileNames == NULL) {
          mGlobals.PackFileNames = FileName;
        } else {
          //
          // Add to the end of the list
          //
          for (TempFileName = mGlobals.PackFileNames; TempFileName->Next != NULL; TempFileName = TempFileName->Next)
            ;
          TempFileName->Next = FileName;
        }
      } while ((Argc > 1) && (Argv[1][0] != '-'));
    } else {
      Error (NULL, 0, 0, Argv[0], "unrecognized option");
      return STATUS_ERROR;
    }

    Argv++;
    Argc--;
  }
  //
  // All modes except 'defaults' requires an output file name
  //
  if (mGlobals.Mode != MODE_EMIT_DEFAULTS) {
    if (mGlobals.OutputFileName[0] == 0) {
      Error (NULL, 0, 0, "must specify '-o OutputFileName'", NULL);
      return STATUS_ERROR;
    }
    //
    // If merging, then you have to specify at least one HII export files.
    // We support specifying only one file in case you want to take an export file
    // and emit a copy with different (for example, manufacturing) defaults.
    //
    if (mGlobals.Mode == MODE_MERGE_HII_EXPORTS) {
      if (mGlobals.HiiExportFileNames == NULL) {
        Error (NULL, 0, 0, "must specify at least one HII export file in 'merge' mode", NULL);
        return STATUS_ERROR;
      }
    } else if (mGlobals.Mode == MODE_CREATE_HII_EXPORT) {
      //
      // Must have specified at least one HII pack file
      //
      if (mGlobals.PackFileNames == NULL) {
        Error (NULL, 0, 0, "must specify at least one input HII pack file in 'create' mode", NULL);
        return STATUS_ERROR;
      }
    }
  } else {
    //
    // Must have specified an input HII export file name
    //
    if (mGlobals.HiiExportFileNames == NULL) {
      Error (NULL, 0, 0, "must specify at least one '-x HiiExportFileName'", NULL);
      return STATUS_ERROR;
    }
  }

  return STATUS_SUCCESS;
}

static
void
Usage (
  VOID
  )
/*++

Routine Description:

  Print usage information for this utility.
  
Arguments:

  None.

Returns:

  Nothing.
  
--*/
{
  int          Index;
  const char   *Str[] = {
    UTILITY_NAME" "UTILITY_VERSION" - Create/Dump HII Database Files Utility",
    "  Copyright (C), 2004 - 2008 Intel Corporation",
#if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )
    "  Built from "UTILITY_BUILD", project of "UTILITY_VENDOR,
#endif
    "",
    "Usage:",
    "  "UTILITY_NAME " [MODE] [OPTION]",
    "Modes:",
    "  create     create an HII export file from one or more HII pack files",
    "  merge      merge two or more HII export files into one HII export file",
    "  defaults   emit variable defaults from an input HII export file",
    "  dump       ASCII dump the contents of an HII export file",
    "Options for all modes:",
    "  -o FileName write output to FileName",
    "  -mfg        use manufacturing defaults from IFR rather than standard defaults",
    "  -g GUID     use GUID for a package GUID in the data tables where applicable",
    "  -v          verbose operation",
    "Options for 'create' mode:",
    "  -p PackFileName(s)  include contents of HII pack file PackFileName",
    "                      in the output file",
    "  -novarpacks         don't emit variable packs to the output file",
    "Options for 'merge' mode:",
    "  -x HiiExportFileName(s)  include contents of HII export file",
    "                           HiiExportFileName in the output file",
    "  -s Path FileMask         include all matching HII export files in Path",
    "                           and its subdirectories in the output file.",
    "                           If Path does not begin with the form C:\\, then",
    "                           it is assumed to be relative to the current working",
    "                           directory. FileMask may contain wildcard characters.",
    "Options for 'defaults' mode:",
    "  -x HiiExportFileName     emit defaults from all variables referenced",
    "                           in input file HiiExportFileName",
    "  -noemptyvarpacks         don't emit variable packs for 0-length variables",
    "Options for 'dump' mode:",
    "  -x HiiExportFileName     dump contents of input file HiiExportFileName",
    "  -dumpstrings             dump string data",
    NULL
  };
  for (Index = 0; Str[Index] != NULL; Index++) {
    fprintf (stdout, "%s\n", Str[Index]);
  }
}