summaryrefslogtreecommitdiff
path: root/Core/CORE_DXE/FrameworkHii/HIIDB.c
blob: b4bfbfc3a22665f56b6d38b923aba08d1c42af75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2005, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**             6145-F Northbelt Pkwy, Norcross, GA 30071            **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Core/CORE_DXE/FrameworkHii/HIIDB.c 5     2/10/10 5:08p Felixp $
//
// $Revision: 5 $
//
// $Date: 2/10/10 5:08p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/CORE_DXE/FrameworkHii/HIIDB.c $
// 
// 5     2/10/10 5:08p Felixp
// Bug fix in HiiExtGetFormInfo function.
// 
// 4     2/10/10 3:43p Felixp
// x64 compiler warnings are fixed.
// 
// 3     12/29/09 2:38p Yakovlevs
// Fixed issue with hanging in TSE big number of setup questions.
// Changed size of Length fields in  some HII internal structures from
// UIN16 to UINTN. 
// Code clenup (removed not used variables changes to compile with W4)
// 
// 2     12/04/09 2:16p Yakovlevs
// 1.Fixed potential NULL pointer use in function NewFontPack()
// 2.Fixed inconsistancy in VarStore management (requires updated HiiDb.h)
// 
// 1     10/09/09 6:10p Felixp
// 
// 45    6/03/09 10:58a Yakovlevs
// if Hii->RemovePack is called reset global pointers to NULL since they
// might pointing at data that was freed.
// 
// 44    10/01/08 1:01p Yakovlevs
// fixed bug in HiiExtGetFormLabels() function.
// 
// 43    1/09/08 10:49a Yakovlevs
// 
// 42    9/05/07 9:18p Felixp
// 
// 41    9/05/07 5:59p Yakovlevs
// Bug fixes in UpdateForm and RemovePack routines.
// 
// 2     6/28/07 5:16p Yakovlevs
// Fixed FormUpdate Protocol Function, and some other minor bug fixes.
// 
// 39    5/23/07 4:32p Pavell
// Reindexing formsets
// 
// 38    3/18/07 3:10p Felixp
// GetSecondaryLanguages fix: return EFI_NOT_FOUND when no languages found
// 
// 37    3/12/07 6:06p Yakovlevs
// Fixed NewKbPack function.
// 
// 36    3/12/07 1:46p Yakovlevs
// Handled a situation when there are no Keyboard Layout data in HII
// Database.
// 
// 35    3/07/07 8:21p Yakovlevs
// Added GetKeyboardLayout function implamentation.
// 
// 34    1/23/07 12:23p Yakovlevs
// 
// 33    12/21/06 1:57p Felixp
// 
// 32    12/20/06 10:37a Felixp
// 
// 31    12/18/06 5:42p Pavell
// Added a fix for variable size determination
// 30    11/15/06 5:55p Yakovlevs
// 
// 29    11/02/06 6:45p Yakovlevs
// Fixed HiiTestString NON_SPACING attribute handling
// 
// 27    10/31/06 11:15a Yakovlevs
// HiiTestString fixed to skip none spacing characters.
// 
// 25    10/30/06 6:09p Yakovlevs
// HII Spec v 0.92 updates
// 
// 24    9/27/06 7:44p Felixp
// Improvement in GetHiiString: 
// If string is not found for the current system language language, try
// with the package primary language.
// 
// 23    9/26/06 9:03a Felixp
// Multilanguage Support. Do not use hard-coded glyphs. Load font file
// instead.
// 
// 22    9/18/06 6:23p Felixp
// Bug fix in HiiGetString: buffer overrun when Raw is FALSE
// 
// 21    8/24/06 10:11a Felixp
// x64 support: warning/error fixes
// 
// 20    8/04/06 5:18p Felixp
// Bug fix in NewFontPack function (wide glyph handling).
// 
// 19    7/24/06 11:46a Yakovlevs
// Hii->ExportDatabase support added.
// 
// 18    3/13/06 5:30p Felixp
// 
// 17    10/03/05 2:47p Markw
// Fixed uninitialized variables.
// 
// 16    7/20/05 6:43p Felixp
// GlyphToBlt modifed to support wide characters
// 
// 15    7/19/05 8:17p Felixp
// NewString modified to support update for all languages when three
// spaces Language provided
// 
// 13    7/13/05 9:46p Felixp
// 
// 12    6/24/05 2:03p Yakovlevs
// 
// 11    6/23/05 5:43p Yakovlevs
// Fixed "Missing String" message
// 
// 10    5/27/05 12:16p Felixp
// bug fix in HiiGetString
// 
// 9     2/28/05 6:21p Yakovlevs
// Fixed DateTime and Object Conditions issue;
// 
// 8     3/17/05 1:57p Felixp
// implementation of HiiNewPackNew updated
// 
// 7     3/04/05 12:52p Felixp
// 
// 6     3/04/05 9:23a Mandal
// 

//**********************************************************************
//<AMI_FHDR_START>
//
// Name:	<This File Name>
//
// Description:	
//
//<AMI_FHDR_END>
//**********************************************************************
//
// Module Name: HIIDB.C
//
// Abstract: This file contains implementation of HII Database Protocol.
//
#include <HiiDb.h>
#include <Protocol/DevicePath.h>
#include <Protocol/HiiUtilities.h>
extern UINT8	*UsStdNarrowGlyphData;
extern HII_UTILITIES_PROTOCOL HiiUtilitiesProtocol;

//==================================================================================
// Some globals we need 
//==================================================================================

//------------------------------------------
//Prototypes
EFI_STATUS InitPrivateData(EFI_HANDLE MyImgHandle);
EFI_STATUS CreateLabelObjects(HII_LABEL *Label);
//EFI_STATUS HiiNewPack (IN EFI_HII_PROTOCOL		*This,
//					IN EFI_HII_PACK_LIST	*Package,
//					OUT EFI_HII_HANDLE		*Handle);

//------------------------------------------
//GUIDs
EFI_GUID gEfiHiiProtocolGuidOld	= EFI_HII_OLD_PROTOCOL_GUID;
EFI_GUID gEfiHiiProtocolGuidNew	= EFI_HII_NEW_PROTOCOL_GUID;
static EFI_GUID gEfiExtHiiProtocolGuid	= EFI_HII_EXT_PROTOCOL_GUID;
//------------------------------------------
//Data Vars 

//Define String Database Key fields
#define STR_KEY			0
#define STR_KEY_CNT		1
//it is going to be combined key 
//Token->size16;LangIdx->size32;Handle->size16; total = 8 byte->UINT64
//Token->Least Segnificant Field Handle Most Significant Field
//the resulting Index for StringToken->23, LangIndex->2, Hii Handle=1 will be 
//	   Handle|LangIdx |Token
//     0x0001|00000002|0023 -> easy to sort easy to find
static DBE_OFFSET_KEY_CONTEXT gStrOffsetInfo = {EFI_FIELD_OFFSET(HII_STR ,Token), 8 };
static DBE_KEY_FIELD	gStrKey=OFFSET_KEY(gStrOffsetInfo);

//Any Language Printable string
static UINT16	gAnyLang[]=L"Any Language";
//HII DATABASE global var
static	HII_DB			gHiiDb;

///////////////////////////////////////////////////////////////////
//in order to save some time when LocateHandle routine been called 
//I have decide to save Current HandleInfo structure pionter 
//here rather than locate it every time 
static HII_HANDLE	*mCurHnd=NULL;
static HII_LANG		*mCurLang=NULL;
static UINTN		mCurHid=0, mCurLid=0;

///////////////////////////////////////////////////////////////////
//in order to save some time when HiiGetLine routine been called 
//I have decide to save Current StringInfo structure pionter 
//here rather than locate it every time HiiGetLine been called 
static HII_STR		*mCurStr=NULL;

///////////////////////////////////////////////////////////////////
// save the varstore context based on the formset
static UINT16  CurrentVarId = 0;

//==================================================================================
// Entry Point
//==================================================================================
//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:	HiiDbEntryPoint()
//
// Description: Initialize HII Database (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
//
// Input:	EFI_HANDLE			ImageHandle		This Driver Image Handle.
//			EFI_SYSTEM_TABLE 	*SystemTable	Pointer to the EFI System Table
//
// Output:  EFI_STATUS	  	EFI_SUCCESS - Driver Loadded.
//  						Other       - Driver Error
//
//<AMI_PHDR_END>
//**********************************************************************

EFI_STATUS HiiDbEntryPoint(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable){
	EFI_STATUS				Status=0;
	UINTN					hCnt;
	EFI_HANDLE				*hBuf;
//-----------------------------------
	InitAmiLib(ImageHandle, SystemTable);

	//Only one HII Driver has to be in the system
	Status=pBS->LocateHandleBuffer(ByProtocol, &gEfiHiiProtocolGuidOld, NULL, &hCnt, &hBuf);

  	//If there was no error, report that we has been already started
  	if (!EFI_ERROR(Status)) {
		if (!hBuf)pBS->FreePool(hBuf); //Don't forget to free the memory
		return EFI_ALREADY_STARTED;
	}

	Status=InitPrivateData(ImageHandle);
	if(EFI_ERROR(Status)) return Status;

	Status = pBS->InstallMultipleProtocolInterfaces(&gHiiDb.HiiHandle,
					&gEfiHiiProtocolGuidOld,&gHiiDb.HiiProtocolOld,
					&gEfiHiiProtocolGuidNew,&gHiiDb.HiiProtocolNew,
					&gEfiExtHiiProtocolGuid,&gHiiDb.HiiExtProtocol,
                    &gHiiUtilitiesProtocolGuid, &HiiUtilitiesProtocol,
					NULL);
 	return Status;
}

///////////////////////////////////////////////////////////////////
// Common Worker Functions
///////////////////////////////////////////////////////////////////
//*************************************************************************
//<AMI_PHDR_START>
//
// Name: GetDefaultLang
//
// Description:
//  EFI_STATUS GetDefaultLang(OUT CHAR16 *DefaultLang) returns the default 
// system language in the string specified by DefaultLang.
//
// Input:
//  OUT CHAR16 *DefaultLang
// Pointer to the null-terminated Unicode string.
//
// Output:
//  EFI_NOT_FOUND, if the default language could not be found.
//  Otherwise, EFI_SUCCESS.
//
// Modified:
//
// Referrals:
// 
// Notes:	
//          
//<AMI_PHDR_END>
//*************************************************************************
//Get System Default Language
EFI_STATUS GetDefaultLang(CHAR16 *DefaultLang){
	EFI_GUID 	EfiGlobalVariableGuid = EFI_GLOBAL_VARIABLE;		
	EFI_STATUS	Status=0;
	UINT8       al[3];
	UINTN		i=sizeof(al);
//-----------------------
    Status=pRS->GetVariable(L"Lang",&EfiGlobalVariableGuid,NULL,&i,al);
    if (EFI_ERROR(Status)) return Status;
	
	for(i=0; i<sizeof(al) ;i++) DefaultLang[i]=al[i];
	DefaultLang[sizeof(al)]=0;
	return Status;
}

//-----------------------------------------------------------------
//This Function will look in Item List and try to find mutch for Item of Size.
//if it finds it will return Pointer to the matching Item and update Index if not - NULL
EFI_STATUS LocateItem(T_ITEM_LIST *ItemLst, VOID *Item, UINTN Size, OUT UINTN *Index){
	UINTN		i;
//-----------------	
	for(i=0; i<ItemLst->ItemCount; i++){
		if(!MemCmp(ItemLst->Items[i],Item,Size)) {
			if(Index)*Index=i;
			return EFI_SUCCESS;
		}
	}
	return EFI_NOT_FOUND;
}

///////////////////////////////////////////////////////////////////
//Will return Lenth of the screen representation of Str with respect 
//of wide narrow and non spacing glyphs it using
//Len excluding '0' char (\n)
UINT16 GetScrLen(CHAR16 *Str, UINT16 Len){
	UINTN		i;
	UINT16		u,sl=0;
	HII_N_FONT	*fnt;
//--------------------------------------
	for(i=0;i<Len;i++){
		u=Str[i];
		if(u<0x20 || u>=0xfff0)continue;
		fnt=gHiiDb.FontDb[u];
		if(fnt){
			if(fnt->Attr & GLYPH_NON_SPACING) continue;
			if(fnt->Attr & GLYPH_WIDE) sl+=2;
			else sl++;
		} else sl++; //we will print empty rect for non existent glyph
	}
	return sl;
}

///////////////////////////////////////////////////////////////////
//Will allocate Mem for the length of the string and Copy "Str" there
UINT16* StrAlloc(UINT16 *Str,UINT16 *StrLen OPTIONAL, UINT16 *ScrLen OPTIONAL){
	UINT16				len;
	UINT16				*str;
//-------------------------------------
	len=(UINT16)Wcslen(Str);//terminator not included
	str=(UINT16*)Malloc((len+1)*sizeof(UINT16));
	if(!str)return NULL;
	Wcscpy(str,Str);
	if(StrLen)*StrLen=len+1;
	if(ScrLen)*ScrLen=GetScrLen(Str,len);
	return str;
}

///////////////////////////////////////////////////////////////////
//This will Change Language Attributes
//VOID SetLangAttr(UINTN Index, UINT32 Attr){
//	HII_LANG	*lang;
//--------------------
//	lang=(HII_LANG*)gHiiDb.LangDb.ResDsc[Index];
//	lang->Attr=Attr;
//}

///////////////////////////////////////////////////////////////////
//this will find and return HII_LANG "Index", 
//if "Language" is stored In Hii Handle Database 
HII_LANG* LocateLang(UINT16 *Language, UINTN *Index){
	EFI_STATUS	Status;
	UINTN		idx;
//------------------------
	if( !mCurLang || MemCmp(&mCurLang->sName[0],Language,sizeof(mCurLang->sName)) )
	{
		Status=LocateItem(&gHiiDb.LangDb,Language,sizeof(UINT16)*4,&idx);
		if(EFI_ERROR(Status)){
			mCurLang=NULL; //flush Language Cache
			mCurLid=0;	  //make index to generate exception if used
		} else {
			mCurLang=gHiiDb.LangDb.Items[idx];
			mCurLid=idx;
		}
	}
	if(mCurLang && Index)*Index=mCurLid;
	return mCurLang;
}

HII_HANDLE *LocateHandleGuid(EFI_GUID *Guid, UINTN	*Index){
	HII_HANDLE		*phnd=NULL;	
	UINTN			i;	
//-------------------------------
	for(i=0; i<gHiiDb.HandleDb.ItemCount; i++){
		phnd=gHiiDb.HandleDb.Items[i];
		if(!MemCmp(&phnd->Guid,Guid,sizeof(EFI_GUID)))break;
		else phnd=NULL;
	}
	if(phnd && Index)*Index=i;
	return phnd;
}

///////////////////////////////////////////////////////////////////
//This function will Locate another Handle with the same Guid as 
//passed Handle. 
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//This function is Obsolete but I need it for compliance with Intel's
//Setup because they using same guid more than once per Ifr and Strings pare.
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
VOID LocateSameGuidHandle(UINTN CurIdx, UINTN *IdxBuffer, UINTN *IdxCount){
	HII_HANDLE	*phnd=NULL,*chnd=gHiiDb.HandleDb.Items[CurIdx];
	UINTN		i, c=0;
//----------------------------
	for(i=0; i<gHiiDb.HandleDb.ItemCount; i++){
		if(i==CurIdx) continue;
		phnd=gHiiDb.HandleDb.Items[i];
		if(!MemCmp(&phnd->Guid,&chnd->Guid,sizeof(EFI_GUID))){
			IdxBuffer[c]=i;
			c++;
		}else phnd=NULL;
	}
	*IdxCount=c;
}

///////////////////////////////////////////////////////////////////
//if on passed handle in any string function there are no 
//strings associated with we need to find another handle with 
//the same guid having strings on it
HII_HANDLE	*LocateStrHandleGuid(UINTN CurIdx, UINTN *Index){
	HII_HANDLE	*phnd=NULL,*chnd=gHiiDb.HandleDb.Items[CurIdx];
	UINTN		i;
//----------------------------
	for(i=0; i<gHiiDb.HandleDb.ItemCount; i++){
		if(i==CurIdx) continue;
		phnd=gHiiDb.HandleDb.Items[i];
		if(!MemCmp(&phnd->Guid,&chnd->Guid,sizeof(EFI_GUID)) && phnd->HasStr){
			if(Index)*Index=i;
			break;
		}else phnd=NULL;
	}
	return phnd;
}

///////////////////////////////////////////////////////////////////
//this will find and return HII_HANDLE "Index", 
//if "Handle" is stored In Hii Handle Database 
static HII_HANDLE* LocateHandle(EFI_HII_HANDLE Handle, UINTN	*Index){
	EFI_STATUS	Status;
	UINTN		idx;
//------------------------
	if((!mCurHnd) || (mCurHnd->Handle!=Handle)){
		Status=LocateItem(&gHiiDb.HandleDb,&Handle,sizeof(EFI_HII_HANDLE),&idx);	
		if(EFI_ERROR(Status)){
			mCurHnd=NULL; //flush Handle Cache
			mCurHid=-1;	  //make index to generate exception if used
		} else {
			mCurHnd=gHiiDb.HandleDb.Items[idx];
			mCurHid=idx;
		}
	}
	if(mCurHnd && Index)*Index=mCurHid;
	return mCurHnd;
}

///////////////////////////////////////////////////////////////////
//this will find and return HII_HANDLE "Index", 
//if "Handle" is stored in Handle Database 
//or Append Handle Database with this Handle if not and return a new "Index" 
EFI_STATUS LocateHandleAdd(EFI_HII_HANDLE Handle, EFI_GUID *GuidId, UINTN *Index){
	EFI_STATUS		Status=0;
	HII_HANDLE		*phnd;
//----------------
	phnd=LocateHandle(Handle,Index);
	if(!phnd){
		phnd=MallocZ(sizeof(HII_HANDLE));
		if(!phnd)return EFI_OUT_OF_RESOURCES;
		pBS->CopyMem(&phnd->Guid,GuidId,sizeof(EFI_GUID));
		phnd->Handle=Handle;
		Status=AppendItemLst(&gHiiDb.HandleDb,phnd);
		if(EFI_ERROR(Status)) return Status;
		*Index=gHiiDb.HandleDb.ItemCount-1;
	}
	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
//this will find and return HII_LANG "Index", 
//if "Language" is stored in Handle Database 
//or Append Handle Database with this Handle if not and return a new "Index" 
EFI_STATUS LocateLangAdd(UINT16 *Language, UINT16 *PrintLanguage, UINTN *Index){
	EFI_STATUS	Status;
	HII_LANG	*lang;
//------------------
	//Check if we have the Language installed already
	lang=LocateLang(Language,Index);	
	if(!lang){ 
		lang=MallocZ(sizeof(HII_LANG));
		if(!lang)return EFI_OUT_OF_RESOURCES;
		pBS->CopyMem(&lang->sName,Language,sizeof(UINT16)*4);
		if(PrintLanguage){
			lang->lName=StrAlloc(PrintLanguage,NULL,NULL);
			if(!lang->lName) return EFI_OUT_OF_RESOURCES;
		}
		Status=AppendItemLst(&gHiiDb.LangDb,lang);
		if(EFI_ERROR(Status)) return Status;
		if(Index)*Index=gHiiDb.LangDb.ItemCount-1;
	}
	return EFI_SUCCESS;
}


///////////////////////////////////////////////////////////////////
//This will check if Lang Index is present in Secondary Lang List 
//on this Handle
///////////////////////////////////////////////////////////////////
EFI_STATUS LocateSecLangAdd(HII_HANDLE *Hnd, UINTN	*SecLangIdx, BOOLEAN AddIt){
	UINTN		i;
//------------------------------
	for(i=0; i<Hnd->SecLang.ItemCount; i++){
		if(*SecLangIdx==(UINTN)(Hnd->SecLang.Items[i])) return EFI_SUCCESS;
	}
	if(AddIt)return AppendItemLst(&Hnd->SecLang,(VOID*)(*SecLangIdx));
	else return EFI_NOT_FOUND;
}


///////////////////////////////////////////////////////////////////
//=================================================================
//HII Database Protocol Functions 
//=================================================================
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
// Font related 
///////////////////////////////////////////////////////////////////
EFI_STATUS NewFontPack(IN EFI_HII_FONT_PACK		*Package)
{
	UINTN				i;
	UINT16				u;
	EFI_NARROW_GLYPH	*ng;
	EFI_WIDE_GLYPH		*wg;
//--------------------------

    //Set Pointer to the beginning of Package Data.
    ng=(EFI_NARROW_GLYPH*)(Package+1);

	if(Package->NumberOfNarrowGlyphs){
		HII_N_FONT			*fnt;
	//---------------------------
		//Allocate Space for Number of Narrow Glyphs
		fnt=(HII_N_FONT*)Malloc(sizeof(HII_N_FONT)*Package->NumberOfNarrowGlyphs);
		if(!fnt)return EFI_OUT_OF_RESOURCES;

		for(i=0; i<Package->NumberOfNarrowGlyphs; i++){
			u=ng[i].UnicodeWeight;
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
		//we will populate only empty spots in FontD or oposite
//made SDL token for that 
			if(!gHiiDb.FontDb[u]){
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
				//fnt=(HII_FONT*)Malloc(sizeof(HII_FONT));
				pBS->CopyMem((VOID*)&fnt[i], &ng[i].Attributes,sizeof(HII_N_FONT));
				gHiiDb.FontDb[u]=&fnt[i];
			}
		}
	}


	if(Package->NumberOfWideGlyphs){
		UINTN		j,k;
		HII_W_FONT	*fnt;
	//---------------
		//Allocate Space for Number of Wide Glyphs
		wg=(EFI_WIDE_GLYPH*)(ng+Package->NumberOfNarrowGlyphs);
		fnt=(HII_W_FONT*)Malloc((sizeof(HII_W_FONT))*Package->NumberOfWideGlyphs);
		if(!fnt)return EFI_OUT_OF_RESOURCES;
		
		for(i=0; i<Package->NumberOfWideGlyphs; i++){
			u=wg[i].UnicodeWeight;
			if(!gHiiDb.FontDb[u]){
				fnt[i].Attr=wg[i].Attributes;
				for(j=0,k=0; j<NG_SIZE;j++, k+=2){
					fnt[i].GlData[k]=wg[i].GlyphCol1[j];
					fnt[i].GlData[k+1]=wg[i].GlyphCol2[j];
				}
				gHiiDb.FontDb[u]=(HII_N_FONT*)&fnt[i];
			}
		}
	}
	return EFI_SUCCESS;
}



///////////////////////////////////////////////////////////////////
EFI_STATUS HiiTestString(IN EFI_HII_PROTOCOL	*This, 
						 IN CHAR16				*StringToTest,
						 IN OUT UINT32			*FirstMissing,
						 OUT UINT32				*GlyphBufferSize)
{
	EFI_STATUS		Status=0;
	UINT32			i;
	UINT32			bs=0;
	HII_N_FONT		*fnt;
//------------------------------
	if(!This) return EFI_INVALID_PARAMETER;
	if ((!FirstMissing)||(!StringToTest)||(!GlyphBufferSize)) return EFI_INVALID_PARAMETER;

	i=*FirstMissing;
	while (StringToTest[i]!=0){
		fnt=gHiiDb.FontDb[ StringToTest[i] ];
		if(fnt){
			if(fnt->Attr& GLYPH_NON_SPACING ){
				i++;
				continue;
			}
			if(fnt->Attr&GLYPH_WIDE) bs+=WG_SIZE;		
			else bs+=NG_SIZE;		
			i++;
		} else {
			Status=EFI_NOT_FOUND;
			break;
		}
	}
	*FirstMissing=i;
	*GlyphBufferSize=bs;
	return Status;
}

static UINT8	gGE[NG_SIZE]=
	{0x00,0x00,0x00,0x7E,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x7E,0x00,0x00,0x00};// \
//     0x00,0x00,0x00,0x7E,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x7E,0x00,0x00,0x00};

static EFI_WIDE_GLYPH	gGB;
///////////////////////////////////////////////////////////////////
EFI_STATUS HiiGetGlyph(IN EFI_HII_PROTOCOL	*This,
					   IN CHAR16			*Source,
					   IN OUT UINT16		*Index,
					   OUT UINT8			**GlyphBuffer,
					   OUT UINT16			*BitWidth,
					   IN OUT UINT32		*InternalStatus)
{
	UINTN				i, bs=NG_SIZE;
	HII_N_FONT			*fnt;//=gHiiDb.FontDb[Source[j]];
//	HII_GL_COL			*gc1=NULL, *gc2=NULL;
//	BOOLEAN				w=FALSE;
//------------------------------------------
	if(!This) return EFI_INVALID_PARAMETER;

	if((!GlyphBuffer)||(!Index)||(!Source)||(!BitWidth)) return EFI_INVALID_PARAMETER;
	fnt=gHiiDb.FontDb[Source[*Index]];

	if(!fnt) {
		if(InternalStatus)*InternalStatus=(UINT32)-1;
		//If after calling TestString somebody will call us with invalid Unicode Char
		pBS->CopyMem(&gGB.GlyphCol1[0],&gGE, NG_SIZE); 
		*BitWidth=GLYPH_W;
		*GlyphBuffer=(UINT8*)&gGB;
		(*Index)++;
		return EFI_NOT_FOUND;
	} 

	//first char must not be the non-spacing character 
	if(fnt->Attr&GLYPH_NON_SPACING) return EFI_INVALID_PARAMETER;

	if(fnt->Attr&GLYPH_WIDE){
		*BitWidth=GLYPH_W*2;
		bs=WG_SIZE;
	} else *BitWidth=GLYPH_W;
	
	pBS->SetMem(&gGB,sizeof(EFI_WIDE_GLYPH),0);
	do{
		fnt=gHiiDb.FontDb[Source[*Index]];
		for(i=0;i<bs;i++) gGB.GlyphCol1[i]|=fnt->GlData[i]; 
		(*Index)++;
	} while(fnt->Attr&GLYPH_NON_SPACING);
	*GlyphBuffer=(UINT8*)&gGB;
	if(InternalStatus)*InternalStatus=0;
		
	return EFI_SUCCESS;
}


///////////////////////////////////////////////////////////////////
//!!!! this function suppose to take care of thing with mixed size gpyphs 
EFI_STATUS HiiGlyphToBlt(IN EFI_HII_PROTOCOL	*This,
						 IN UINT8				*GlyphBuffer,
						 IN EFI_UGA_PIXEL		Foreground,
						 IN EFI_UGA_PIXEL		Background,
						 IN UINTN				Count,//Number of NARROW Glyps left in BltBuffer
						 //or count from the end of the buffer in terms of NARROW glyphs
						 IN UINTN				Width,
						 IN UINTN				Height,
						 IN OUT EFI_UGA_PIXEL	*BltBuffer)
{
//	UINTN	x,y;//,i,o;
//	UINT8	k = Width > GLYPH_W ? 2 : 1;
//------------------------------------
//	if(!This) return EFI_INVALID_PARAMETER;
//	if( !(Width==GLYPH_W || Width==(GLYPH_W*2)) || Height!=GLYPH_H )return EFI_INVALID_PARAMETER;
/*	for (y=0,o=0; y<Height; y++){
		for(i=0; i<Width/GLYPH_W; i++){
			for (x=0; x<GLYPH_W; x++){
				if ( GlyphBuffer[y*k+i] & (0x80 >> x)) BltBuffer[y*GLYPH_W*Count+x+o]=Foreground;
				else BltBuffer[y*GLYPH_W*Count+x+o]=Background;
			}
			o=GLYPH_W;
		}
	}

	return EFI_SUCCESS;
  UINTN                     X;
  UINTN                     Y;
*/
	UINT8 Mask;
	UINTN Index, Line, Part;
	UINTN TotalParts = Width/GLYPH_W;
	EFI_NARROW_GLYPH *Glyph = (EFI_NARROW_GLYPH *)GlyphBuffer;
	if(!This) return EFI_INVALID_PARAMETER;
	if( !(Width==GLYPH_W || Width==GLYPH_W*2) || Height!=GLYPH_H || Count<TotalParts)return EFI_INVALID_PARAMETER;
	for (Line = 0, Index=0; Line < Height; Line++, Index+=GLYPH_W * (Count-TotalParts)) {
		for(Part=0; Part<TotalParts; Part++){
			for (Mask = 0x80; Mask > 0; Mask>>=1, Index++) {
				if (Glyph->GlyphCol1[Line*TotalParts+Part] & Mask)
					BltBuffer[Index] = Foreground;
				else
					BltBuffer[Index] = Background;
			}
		}
	}
/*///
	BltBuffer[Index] = (Glyph->GlyphCol1[y] & (1 << x)) ? Foreground : Background;
	if (Width > GLYPH_W)
		BltBuffer[Index+GLYPH_W] = (Glyph->GlyphCol1[GLYPH_H+y] & (1 << x)) ? Foreground : Background;
///
	EFI_NARROW_GLYPH *Glyph = (EFI_NARROW_GLYPH *)GlyphBuffer;
	for (y = 0; y < Height; y++) {
		for (x = 0; x < GLYPH_W; x++) {
			UINTN Index = y * GLYPH_W * Count + (GLYPH_W - x - 1);
			for(i=0; i<Width/GLYPH_W; i++)
				if ((Glyph->GlyphCol1[i*GLYPH_H+y] & (1 << x)) != 0)
					BltBuffer[Index+GLYPH_W*i] = Foreground;
				else
					BltBuffer[Index+GLYPH_W*i] = Background;
		}
	}*/
  return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
// String related
///////////////////////////////////////////////////////////////////
HII_STR	*LocateString(HII_HANDLE *PHnd, UINTN LangIdx, STRING_REF Token/*, UINTN HandleIdx OPTIONAL*/){
	UINTN			li=LangIdx, j;
	HII_STR			sd = {0,0,0,0,NULL};
	INT8			v;
	VOID			**cs;
//-----------------
	//if CurrentString must be refreshed	
	if(!mCurStr || mCurStr->LangIdx!=LangIdx || mCurStr->Token!=Token || mCurStr->Handle!=PHnd->Handle)
	{

		//---------------------
		do {
			sd.Handle=PHnd->Handle;
			sd.LangIdx=(UINT32)li;
			sd.Token=Token;

			DbeLocateKey(&gHiiDb.StringDb,STR_KEY,(VOID*)&sd,(VOID**)&cs,&v,&j);

			if(v)mCurStr=NULL;
			else {
				mCurStr=(HII_STR*)(*cs);
				break;
			}

			//if we were looking for any language and failed
			//just get Primary Package Language
			if(!li)li=PHnd->PriLang;
			else break;
		} while(v);
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//this patch was made to keep compatibility with Intel's implemntation
//of BDS - DevicerManagerVfrBin and FrontPageVfrBin uses the same Guid
//and Same String Pack expecting valid strings on from one StringPack
//when passing different GUIDs?????????
/*		if(!mCurStr){
			HII_HANDLE	*phnd;
			UINTN		i,sgib[10]={0,0,0,0,0,0,0,0,0,0};//same GUID index Buffer
		//----------------------------------
			LocateSameGuidHandle(HandleIdx,&sgib[0],&c);
			for(i=0; i<c;i++){
				li=LangIdx;
				phnd=gHiiDb.HandleDb.Items[sgib[i]];
				do {
					sd.Handle=phnd->Handle;
					sd.LangIdx=(UINT32)li;
					sd.Token=Token;

					DbeLocateKey(&gHiiDb.StringDb,STR_KEY,(VOID*)&sd,(VOID**)&cs,&v,&j);

					if(v)mCurStr=NULL;
					else {
						mCurStr=(HII_STR*)(*cs);
						break;
					}
					//if we were looking for any language and faild
					//just get Primary Package Language
					if(!li)li=PHnd->PriLang;
					else break;
				} while(v);
				if(mCurStr)break;
			}
		}
*/
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
	}
	return mCurStr;
}

///////////////////////////////////////////////////////////////////
EFI_STATUS	ReplaceString(HII_STR *StrData, UINT16 *Replacement)
{
	UINT16	*str;
	UINT16	len,slen;
//-----------------------------
	str=StrAlloc(Replacement, &len, &slen);
	if(!str)return EFI_OUT_OF_RESOURCES;
	
	if(StrData->NewStr.StrPtr)pBS->FreePool(StrData->NewStr.StrPtr);

	StrData->NewStr.StrLen=len;
	StrData->NewStr.ScrLen=slen;
	StrData->NewStr.StrPtr=str;
	
	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
EFI_STATUS	AddString(UINTN	LangIdx, HII_HANDLE *PHandle, UINT16 *String, BOOLEAN Initial, STRING_REF *Token)
{
	HII_STR				*str;
//-------------------------------------
	str=MallocZ(sizeof(HII_STR));
	if(!str) return EFI_OUT_OF_RESOURCES;

	if(Initial){
		str->String.StrPtr=StrAlloc(String,&str->String.StrLen,&str->String.ScrLen);
		if(!(str->String.StrPtr))return EFI_OUT_OF_RESOURCES;
	} else {
		str->NewStr.StrPtr=StrAlloc(String,&str->NewStr.StrLen, &str->NewStr.ScrLen);
		if(!(str->NewStr.StrPtr))return EFI_OUT_OF_RESOURCES;
	}
	
	str->LangIdx=(UINT32)LangIdx;
	//if Token is provided, use it. Otherwise assign new one.
	if (Token && *Token) str->Token=*Token;
	else
	{
		str->Token=PHandle->NextStrToken;
		PHandle->NextStrToken++;
		if (Token) *Token = str->Token;
	}
	str->Handle=PHandle->Handle;

	return DbeInsert(&gHiiDb.StringDb,(VOID*)str);
}

EFI_STATUS UpdateString(IN UINTN				li,
                        IN HII_HANDLE			*phnd,
                        IN STRING_REF			*Reference,
						IN CHAR16				*NewString)
{
	EFI_STATUS Status;
	//Check if String with this Token#  already present in String Db
	if((*Reference)==0){
		Status=AddString(li,phnd,NewString,FALSE,Reference);
		if(EFI_ERROR(Status)) return Status;
	} else {
		HII_STR			*str;	
		if (*Reference >= phnd->NextStrToken) return EFI_INVALID_PARAMETER;
		str=LocateString(phnd,li,*Reference);
		//if str is NULL, it means that string with this token has been created 
		//for a different language 
		Status= (str) ? ReplaceString(str,NewString) : AddString(li,phnd,NewString,FALSE,Reference);
	}
	return Status;

}
///////////////////////////////////////////////////////////////////
EFI_STATUS HiiNewString(IN EFI_HII_PROTOCOL		*This,
						IN CHAR16				*Language,
                        IN EFI_HII_HANDLE		Handle,
                        IN STRING_REF			*Reference,
						IN CHAR16				*NewString)
{
	UINTN			li=0, hi=0;
	HII_LANG		*lang;
	HII_HANDLE		*phnd;
	EFI_STATUS		Status=0;
	UINT16			dl[4]={0x20,0x20,0x20,0}, *pdl;//Any Lang
//-------------------------------
	if(!This || !Reference || !NewString) return EFI_INVALID_PARAMETER;
	//first Check if the Handle is valid. 
	phnd=LocateHandle(Handle,&hi);
	if(!phnd) return EFI_INVALID_PARAMETER;

	if(!phnd->HasStr){
		phnd=LocateStrHandleGuid(hi,&hi);
		if(!phnd) return EFI_INVALID_PARAMETER;
	}
	
	if(!Language){
		pdl=&dl[0];
		if(EFI_ERROR( GetDefaultLang(pdl))){
			pdl=(UINT16*)gHiiDb.LangDb.Items[phnd->PriLang];
		}
	} else pdl=Language;

	//dl has a language string "xxx"
	lang=LocateLang(pdl,&li);

	if (!li)
	{//language index 0 referes to language "   ", which means we have to update
	 //string for all languages
		//update string for primary language
		Status = UpdateString(phnd->PriLang,phnd,Reference,NewString);
		//update string for all secondary languages
		for(li=0; !EFI_ERROR(Status) && li<phnd->SecLang.ItemCount; li++)
			Status = UpdateString((UINTN)phnd->SecLang.Items[li],phnd,Reference,NewString);
		return Status;
	}

	if(!lang) return EFI_INVALID_PARAMETER;

	//If language is valid but not mutch Pack PriLang
	if(phnd->PriLang!=li){
		Status=LocateSecLangAdd(phnd,&li,FALSE);
		if(EFI_ERROR(Status))li=phnd->PriLang;
		//lang=(HII_LANG*)gHiiDb.LangDb.Items[li];
	} 
	return UpdateString(li,phnd,Reference,NewString);
}

///////////////////////////////////////////////////////////////////
EFI_STATUS GetLanguageFromPack(EFI_HII_STRING_PACK *Pack, UINTN *LangIndex){
	UINT16		*sn, *ln;		
	UINT8		*ofs=(UINT8*)Pack;//(Pack+1);
//-------------------------
	sn=(UINT16*)(ofs+Pack->LanguageNameString);
	ln=(UINT16*)(ofs+Pack->PrintableLanguageName);
	return LocateLangAdd(sn,ln,LangIndex);	
}

///////////////////////////////////////////////////////////////////
BOOLEAN IsLastStrPack(EFI_HII_STRING_PACK *Pack){
	if(Pack->Header.Type==EFI_HII_STRING && 
		Pack->Header.Length == 0 && Pack->Attributes == 0 &&
		Pack->LanguageNameString == 0 && Pack->NumStringPointers == 0 &&
		Pack->PrintableLanguageName == 0 ) return TRUE;
	return FALSE;
}

///////////////////////////////////////////////////////////////////
EFI_STATUS HiiGetPrimaryLanguage(IN EFI_HII_PROTOCOL	*This,
								 IN EFI_HII_HANDLE		Handle,
								 OUT EFI_STRING *LanguageString)
{
	HII_HANDLE		*phnd;
	HII_LANG		*lang;
	UINTN			hi;
	UINT16			*ls,*bp;
//----------------------
	if(!This || !LanguageString) return EFI_INVALID_PARAMETER;

	phnd=LocateHandle(Handle,&hi);
	if(!phnd) return EFI_INVALID_PARAMETER;
	if(!phnd->HasStr){
		phnd=LocateStrHandleGuid(hi,&hi);
		if(!phnd) return EFI_INVALID_PARAMETER;
	}
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//???????????????????????????????????????
	//if for some reson Somebody submit just IFR vith no Strings
	//there are no way to determine language so I just put there 0 -Any Language
	//but Intel's code expect me to return something like "eng" 
	//so I will return Default System Lang.
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
	ls=Malloc(sizeof(UINT16)*3*(phnd->SecLang.ItemCount+1)+1); //4 characters 
	if(!ls) return EFI_OUT_OF_RESOURCES;

	bp=ls;
	lang=gHiiDb.LangDb.Items[phnd->PriLang];
	pBS->CopyMem(ls,&lang->sName,sizeof(UINT16)*3);
	bp+=3;

	for(hi=0;hi<phnd->SecLang.ItemCount; hi++){
		lang=gHiiDb.LangDb.Items[(UINTN)phnd->SecLang.Items[hi]];
		pBS->CopyMem(bp,&lang->sName,sizeof(UINT16)*3);
		bp+=3;
	}
	*bp=0;
	*LanguageString=ls;

	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
//
EFI_STATUS HiiGetSecondaryLanguages(IN EFI_HII_PROTOCOL		*This,
									IN EFI_HII_HANDLE		Handle,
                                    IN CHAR16				*PrimaryLanguage,
									OUT EFI_STRING			*LanguageString)
{
	UINTN			id,i;
	HII_HANDLE		*phnd;
	HII_LANG		*lang;
	UINT16			*ls;
//----------------------
	if(!This) return EFI_INVALID_PARAMETER;

	phnd=LocateHandle(Handle,NULL);
	if(!phnd) return EFI_INVALID_PARAMETER;
	
	//Some how caller managed to scrue up with parameters.
	lang=LocateLang(PrimaryLanguage,&id);//Lang provided does not mutch with what we've recorded 
	if( !lang || phnd->PriLang!=id ) return EFI_INVALID_PARAMETER;
	
	if(phnd->SecLang.ItemCount>0){
		UINT16	*l;
	//----------------
		ls=Malloc(sizeof(UINT16)*3*phnd->SecLang.ItemCount+1);
		if(!ls) return EFI_OUT_OF_RESOURCES;
		l=ls;
		for(i=0; i<phnd->SecLang.ItemCount; i++){
			//we are not returning Any Language str "   "
			//And Any Lang String has index 0 upon initialization
			if((UINTN)(phnd->SecLang.Items[i])){
				lang=(HII_LANG*)gHiiDb.LangDb.Items[(UINTN)(phnd->SecLang.Items[i])];
				pBS->CopyMem(l,&lang->sName[0],sizeof(UINT16)*3);//4 unicode chars;
				l+=3; 
			}
		}//resulting string will be consist of N*3xUINT16+TERMINATOR 
		*l=0x0000; //put terminator at the end of set of sec lang
		*LanguageString=ls;
	}else { *LanguageString=NULL; return EFI_NOT_FOUND;}
	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
//This is a worker function to avoid dubliceted code in HiiGetString,
//HiiGetLine and ExtHiiGetStrInfo
HII_STR *GetHiiString(EFI_HII_HANDLE Handle, UINT16 *Lang, STRING_REF	Token){
	UINTN			li,hi;
	UINT16			dl[4]={0x20,0x20,0x20,0}, *pdl;
	HII_HANDLE		*phnd;
	HII_STR			*str;
//------------------------------
	phnd=LocateHandle(Handle,&hi);
	if(!phnd) return NULL;

	if(!phnd->HasStr){
		phnd=LocateStrHandleGuid(hi,&hi);
		if(!phnd) return NULL;
	}

	if(!Lang){
		pdl=&dl[0];
		if(EFI_ERROR(GetDefaultLang(pdl)))pdl=(UINT16*)gHiiDb.LangDb.Items[phnd->PriLang];
	} else pdl=Lang;

	//dl has a language string it "   " or "xxx"
	//lang=LocateLang(pdl,&li);
	if(!LocateLang(pdl,&li)) return NULL;
	str=LocateString(phnd,li,Token);

	// If String is not found and language is not specified,
	// let's try with the primary package language.
	if (!str && !Lang) str=LocateString(phnd,0,Token);

	return str;
}


///////////////////////////////////////////////////////////////////
EFI_STATUS HiiGetStringNew(IN EFI_HII_PROTOCOL		*This,
						IN EFI_HII_HANDLE		Handle,
						IN STRING_REF			Token,
						IN BOOLEAN				Raw,
						IN CHAR16				*LanguageString,
						IN OUT UINTN			*BufferLength, //in units of UINT16
						OUT EFI_STRING			StringBuffer)
{
	HII_STR			*str;
	UINTN			j,i;
	UINT16			*ps, sl; 
//--------------------------
	
	if( !This || !BufferLength || *BufferLength && !StringBuffer ) return EFI_INVALID_PARAMETER;

	str=GetHiiString(Handle,LanguageString,Token);
	if(!str) return EFI_INVALID_PARAMETER;

	if(str->NewStr.StrPtr){
		ps=str->NewStr.StrPtr;
		sl=str->NewStr.StrLen;
	} else {
		ps=str->String.StrPtr;
		sl=str->String.StrLen;
	}
	//str->StrLen in UINT16 units
	if(*BufferLength<sl*sizeof(UINT16)) {
		*BufferLength=sl*sizeof(UINT16);
		return EFI_BUFFER_TOO_SMALL;
	}

	if(Raw){
		pBS->CopyMem(&StringBuffer[0],ps,sl*sizeof(UINT16));
		*BufferLength=sl*sizeof(UINT16);
	} else {
		for(i=0,j=0;i<(UINTN)(sl-1);i++){
			//replace <CR> with <Space>
			//if(ps[i]==0x0013) 
			//	StringBuffer[j]=0x0020;	
			//else {
				//skip all Special and Control Characters. 
				if((ps[i]>=0xFFF0 && ps[i]<=0xFFFF))
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//It is amazing how Intel's engineers not in compliance with 
//their own Specs. 
//"If (Raw) TRUE, the string is returned unedited in the internal storage format described
//above. If FALSE, the string returned is edited by replacing !!<cr>!! with <space> and by
//removing special characters such as the ??<wide>?? prefix."
//					||(ps[i]>=0x0000 && ps[i]<=0x001F)) 
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
				   continue;
				//Copy the rest of chars
				else StringBuffer[j]=ps[i];
			//}
			j++;
		}
		StringBuffer[j]=0;//Terminate String!!
		*BufferLength=(UINT16)((j+1)*sizeof(UINT16));
	}
	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
EFI_STATUS HiiGetStringOld(IN EFI_HII_PROTOCOL		*This,
						IN EFI_HII_HANDLE		Handle,
						IN STRING_REF			Token,
						IN BOOLEAN				Raw,
						IN CHAR16				*LanguageString,
						IN OUT UINT16			*BufferLength, //in units of UINT16
						OUT EFI_STRING			StringBuffer)
{
	EFI_STATUS	Status;
	UINTN		bl;
//-----------------
	if(BufferLength==NULL) return EFI_INVALID_PARAMETER;

	bl=(UINTN)(*BufferLength);
	Status=HiiGetStringNew(This,Handle,Token,Raw,LanguageString,&bl,StringBuffer);
	(*BufferLength)=(UINT16)bl;

	if((Status==EFI_BUFFER_TOO_SMALL) && (bl > 0xFFFF)) Status=EFI_INVALID_PARAMETER;
	
	return Status;
}


///////////////////////////////////////////////////////////////////
EFI_STATUS HiiGetLine(IN EFI_HII_PROTOCOL		*This,
					  IN EFI_HII_HANDLE			Handle,
					  IN STRING_REF				Token,
					  IN OUT UINT16				*Index,
					  IN UINT16					LineWidth,
					  IN CHAR16					*LanguageString,
					  IN OUT UINT16				*BufferLength,
					  OUT EFI_STRING			StringBuffer)
{
	HII_STR			*str;
	UINTN			j,i;
	UINT16			*ps, sl; 
	BOOLEAN			cr=FALSE;
//--------------------------
	
	if( !This || !BufferLength || !StringBuffer ) return EFI_INVALID_PARAMETER;
	
	str=GetHiiString(Handle,LanguageString,Token);
	if(!str) return EFI_INVALID_PARAMETER;

	if(str->NewStr.StrPtr){
		ps=str->NewStr.StrPtr;
		sl=str->NewStr.StrLen;
	} else {
		ps=str->String.StrPtr;
		sl=str->String.StrLen;
	}

	//I don't know should I remove a special chars or not 
	// So I will for now 
	for(j=0,i=*Index; (i<sl || i<LineWidth); i++ ){
		//replace <CR> with <Space>
		if(ps[i]==0x0013){
			if(cr) { //if <CR> hit second time in a row - terminate string
				StringBuffer[j]=0x0000;	
				*Index=(UINT16)i;
				break;
			} else { //else replece it with space an set "cr" flag
				StringBuffer[j]=0x0020;
				cr=TRUE;
			}
		}else{
			cr=FALSE; //if we here - reset "cr" flarg 
			//skip all Special and Control Characters. 
			if((ps[i]>=0xFFF0 && ps[i]<=0xFFFF)||(ps[i]<=0x001F)) 
				continue; //will not increase "j" the buffer index
			//Copy the rest of chars into the Buffer
			else 
				StringBuffer[j]=ps[i];
		}
		j++;
	}
	StringBuffer[j]=0;//Terminate String!!
	*BufferLength=(UINT16)((j+1)*sizeof(UINT16));
	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
EFI_STATUS HiiResetStrings(IN EFI_HII_PROTOCOL			*This,
						   IN EFI_HII_HANDLE			Handle)
{
	HII_STR			*str;
	UINTN			i,hi;//,j,gbi[10],gc=0;
	HII_HANDLE		*phnd;
//------------------------

	if(!This) return EFI_INVALID_PARAMETER;
	phnd=LocateHandle(Handle,&hi);
	if(!phnd) return EFI_INVALID_PARAMETER;
	
	if(!phnd->HasStr){
		phnd=LocateStrHandleGuid(hi,&hi);
		if(!phnd) return EFI_INVALID_PARAMETER;
	}
	//LocateSameGuidHandle(hi, &gbi[0], &gc);

	//for(j=0; j<=gc; j++){
	for(i=0; i<gHiiDb.StringDb.RecordCount;i++){
		//if(!phnd->HasStr) break;
		DbeGoToIndex(&gHiiDb.StringDb,STR_KEY,i,&str);
		if(str->Handle==phnd->Handle){
			if(str->NewStr.StrPtr){
				pBS->FreePool(str->NewStr.StrPtr);
				str->NewStr.StrPtr=NULL;
				str->NewStr.StrLen=0;
				str->NewStr.ScrLen=0;
				//Cleanup Dtabase from RECORDS which was addes
				//after initial Package
				if(!str->String.StrPtr){
					DbeDelete(&gHiiDb.StringDb, str, TRUE);
					i--;//we ned to step on the same data index 
					continue;
					//since we have delited current one
					//phnd->NextStrToken--;
				}
				
			}
		}
		if(str->Handle>phnd->Handle) break;
	}
	phnd->NextStrToken=phnd->StrCount;

	//	if(j<gc)phnd=gHiiDb.HandleDb.Items[gbi[j]];	
	//}
	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
EFI_STATUS NewStringPack(IN EFI_HII_STRING_PACK	*Package,
						 UINTN					HandleId)
{
	UINTN				i,lid;
	UINT8				*ofs;
	UINT16				*str;
	RELOFST				*ps;//string pointer
	EFI_HII_STRING_PACK	*cp=Package;
	EFI_STATUS			Status=0;
	BOOLEAN				f=TRUE;
	HII_HANDLE		*phnd=(HII_HANDLE*)gHiiDb.HandleDb.Items[HandleId];
//--------------------------------
	while(!IsLastStrPack(cp)){
		ps=(RELOFST*)(cp+1);
		ofs=(UINT8*)cp;
		//Check if Language exists already
		Status=GetLanguageFromPack(cp,&lid);
		if(EFI_ERROR(Status)) return Status;
		if(f){//if this is first string pack make this lang Primary
			phnd->PriLang=(UINT32)lid;
			f=FALSE;
		} else {
			//All other languages if any will be Secondary languages
			Status=LocateSecLangAdd(phnd,&lid,TRUE);
			if(EFI_ERROR(Status)) return Status;
		}

		for(i=0; i<Package->NumStringPointers; i++){
			phnd->NextStrToken=(STRING_REF)i;
			if(!ps[i]) continue;
			str=(UINT16*)(ofs+ps[i]);
			Status=AddString(lid,phnd,str,TRUE,NULL);
			if(EFI_ERROR(Status)) return Status;
		}
		

		cp=(EFI_HII_STRING_PACK*)((UINT8*)cp+cp->Header.Length);
	}
	phnd->StrCount=(STRING_REF)Package->NumStringPointers;
	phnd->HasStr=TRUE;
	return Status;
}


///////////////////////////////////////////////////////////////////
// IFR Releated Functions 
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//this one will calculate length in bytes for the buffer designed 
//to receive a copy of form instance
//UINT16	GetFormLen(HII_FORM *Form){
//	HII_LABEL	*lbl;
//	UINTN		i;
//	UINT16		len=Form->BufferLength;
//------------------------
//	for(i=0; i<Form->Labels.ItemCount; i++){
//		lbl=Form->Labels.Items[i];
//		if(lbl->Erased) len-=lbl->OrgDataLength;
//		len+=lbl->UpdDataLength;
//	}
//	return len;
//}

///////////////////////////////////////////////////////////////////
//this one will calculate length in bytes for the buffer designed 
//to receive a copy of FormSet instance
//UINT16	GetFormSetLen(HII_FORMSET *FormSet){
//	UINT16		len=0;
//	UINTN		i;
//	HII_FORM	*frm;
//------------------
//	for(i=0; i<FormSet->Forms.ItemCount; i++){
//		frm=FormSet->Forms.Items[i];
//		len+=GetFormLen(frm);
//	}
//	return len+FormSet->FormSetData->Header.Length+FormSet->EndFormSet->Header.Length;
//}

UINTN	GetDataLen(	UINT8 *OpCode, UINTN *Count, BOOLEAN Remove){
	UINTN				i;
	UINTN				len=0;
	UINT8				*p=OpCode;
//------------------------
	for(i=0; i<*Count; i++){
//		if( !Remove && 
//			( (*p)==EFI_IFR_END_FORM_OP || (*p)==EFI_IFR_LABEL_OP )
//		){ 
//			i=i;
//		}
		if( ( Remove && ( (*p)==EFI_IFR_END_FORM_OP || (*p)==EFI_IFR_LABEL_OP )
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
	//when adding data I expect that caller knows what he is doing and 
	//passes me correct Data Count. Guess what? This is not TRUE in Intel's Setup.
	//Intel uses Zeroed buffer to fill with desired Opcodes, set Data count to 0xFF and here we go.
	//In this case if in buffer we will found junk which satisfy conditions below
	//we can seriousely scrue up!!!!!
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
			  || ((*p)==0 || (*p)>EFI_IFR_VARSTORE_SELECT_PAIR_OP ))){ 
				  *Count=i; 
				  break;
		}
		len = len + ((EFI_IFR_OP_HEADER*)p)->Length;
		p=OpCode+len;
	}
	return len;
}


///////////////////////////////////////////////////////////////////
//this will copy form from HII DB private storage to decignated Buffer
VOID CopyForm(UINT8* Buffer, HII_FORM *Form, UINTN *BytesCnt){
	UINTN		i,cnt=0; 
	HII_LABEL	*lbl=NULL;
	UINT8		*bp=Buffer;
//----------------

	//Now First Label has data starting from opcode next to FORM_OP  
	//and lbl->Label of very first label in the list actualy FORM_OP
	//so don't need following code

	//if(Form->Labels.ItemCount)lbl=(HII_LABEL*)Form->Labels.Items[0];
	//1. copy form till first label (including LABEL OPCODE)
	//if(lbl)//Total Bufferlength including EOF opcode 
	//	cnt=(UINT8*)(lbl->Label)-(UINT8*)Form->FormData;  
	//else 
	//	cnt=Form->BufferLength-Form->EndForm->Header.Length;

	//pBS->CopyMem(bp,Form->FormData,For);
	//bp+=cnt; //Advance buffer pointer;
	//(*BytesCnt)=cnt;
	(*BytesCnt)=0;
	//Keep circling trough Labels copying corresponded data 
	for(i=0;i<Form->Labels.ItemCount;i++){
		lbl=(HII_LABEL*)Form->Labels.Items[i];
		//copy Label OpCode
		pBS->CopyMem(bp,lbl->Label,lbl->Label->Header.Length);
		bp+=lbl->Label->Header.Length;
		(*BytesCnt)+=lbl->Label->Header.Length;

		//if Label was updated copy Update data first...
		if(lbl->LabelData){
			pBS->CopyMem(bp,lbl->LabelData,lbl->DataLength);
			bp+=lbl->DataLength;
			(*BytesCnt)+=lbl->DataLength;
		}
	}
	//Copy EOF opcode
	cnt=Form->EndForm->Header.Length;
	pBS->CopyMem(bp,Form->EndForm,cnt);
	(*BytesCnt)+=cnt;
	return;
}

///////////////////////////////////////////////////////////////////
//will copy entire formset data excluding Pcakage Header
VOID CopyFormSet(UINT8* Buffer, HII_FORMSET *FormSet, UINTN *BytesCnt){
	UINTN		i; 
	UINT8		*bp=Buffer,*pp;
	UINTN		cnt;
	HII_FORM	*frm;
//----------------
	bp=Buffer;
	//Copy Formset OpCode...
	pBS->CopyMem(bp,FormSet->FormSetData,FormSet->FormSetData->Header.Length);
	bp+=FormSet->FormSetData->Header.Length;
	*BytesCnt=FormSet->FormSetData->Header.Length;
	//Copy any information that we might have from end of FormSet Opcode 
	//to the befinning of FOrm OpCode VARSTORE for example.
	if(FormSet->Forms.ItemCount){
		pp=(UINT8*)FormSet->FormSetData+FormSet->FormSetData->Header.Length;
		frm=FormSet->Forms.Items[0];
		cnt=(UINTN)((UINT8*)frm->FormData-pp);
		pBS->CopyMem(bp,pp,cnt);
		bp+=cnt;
		*BytesCnt+=cnt;
	}

	for(i=0; i<FormSet->Forms.ItemCount; i++){
		frm=FormSet->Forms.Items[i];
		CopyForm(bp,frm,&cnt);
		bp+=cnt;
		*BytesCnt+=cnt;
	}
	//copy END_OF_FORM_SET OP here
	cnt=FormSet->EndFormSet->Header.Length;
	pBS->CopyMem(bp,&FormSet->EndFormSet->Header.OpCode,cnt);
	*BytesCnt+=cnt;
	return;
}



///////////////////////////////////////////////////////////////////
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
// Cache Form and Formset 
HII_FORM	*LocateForm(HII_FORMSET *FormSet, EFI_FORM_ID FormId){
	EFI_STATUS			Status=0;
	UINTN				fid;
//----------------------------------------------
	Status=LocateItem(&FormSet->Forms,&FormId,sizeof(EFI_FORM_ID),&fid);
	if(EFI_ERROR(Status)) return NULL;

	return (HII_FORM*)FormSet->Forms.Items[fid]; 
}

///////////////////////////////////////////////////////////////////
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
// Cache Form and Formset 
HII_FORMSET *LocateFormSet(UINTN HandleIdx, UINTN* FormSetIdx){
	EFI_STATUS			Status=0;
	UINTN				fsid;
//--------------------------
	Status=LocateItem(&gHiiDb.IfrDb,&((UINT32)HandleIdx),sizeof(UINT32),&fsid);
	if(EFI_ERROR(Status)) return NULL; 
	if(FormSetIdx!=NULL)*FormSetIdx=fsid;
	return (HII_FORMSET*)gHiiDb.IfrDb.Items[fsid];
}


HII_LABEL *LocateFormLabel(HII_FORM *Form, EFI_FORM_LABEL LabelId, UINTN *LabelIdx){
	EFI_STATUS			Status=0;
	UINTN				lid;
//----------------------------------------------
	Status=LocateItem(&Form->Labels,&LabelId, sizeof(EFI_FORM_LABEL),&lid);
	if(EFI_ERROR(Status)) return NULL;
		
	*LabelIdx=lid;
	return (HII_LABEL*)Form->Labels.Items[lid]; 
}

HII_LABEL *LocateFormSetLabel(HII_FORMSET *FormSet, EFI_FORM_LABEL LabelId, UINTN *LabelIdx){
	EFI_STATUS			Status=0;
	UINTN				lid;
//----------------------------------------------
	Status=LocateItem(&FormSet->Labels,&LabelId, sizeof(EFI_FORM_LABEL),&lid);
	if(EFI_ERROR(Status)) return NULL;
		
	if(LabelIdx)*LabelIdx=lid;
	return (HII_LABEL*)FormSet->Labels.Items[lid]; 
}


///////////////////////////////////////////////////////////////////
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//This MUST be here to make Shell working 
static UINT8 gDummyVfr[] = {
	//Header 
	0x2c, 0x00, 0x00, 0x00, //Actual Length of Data Buffer
	0x03, 0x00,				//Type;
	// start of IFR data
	/*O*/ 0x0E, //Opcode=EFI_IFR_FORM_SET_OP
	/*L*/ 0x24, //Length
	//Offset=8
	/*G*/ 0xBC, 0x30, 0x0C, 0x9E, 0x06, 0x3F, 0xA6, 0x4B, 0x82, 0x88, 0x09, 0x17, 0x9B, 0x85, 0x5D, 0xBE, //GUID
	/*S*/ 0x02, 0x00, //FormSet Title
	/*S*/ 0x00, 0x00, //Help
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//Callback
	0x00, 0x00, //Class
	0x00, 0x00, //SubClass
	0x00, 0x00, //NvDataSize
	/*O*/ 0x0D, //OpCOde=EFI_IFR_END_FORM_SET_OP
	/*L*/ 0x02, 
};
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
EFI_STATUS HiiGetFormsNew(IN EFI_HII_PROTOCOL		*This,
					   IN EFI_HII_HANDLE		Handle,
					   IN EFI_FORM_ID			FormId,
					   IN OUT UINTN             *BufferLength,
					   OUT UINT8				*Buffer)
{
	UINTN				hid;
	HII_FORMSET			*fs;
	HII_FORM			*frm;
	HII_HANDLE			*phnd;
	UINTN				len;
//--------------------------------------
	if(This==NULL && BufferLength==NULL) return EFI_INVALID_PARAMETER;
	//phnd=LocateHandle(Handle, &hid);
	//TODO: Felix: by the HII Spec. we should return EFI_INVALID_PARAMETER
	//However, Intel Device Manager expects EFI_NOT_FOUND
	//(it hangs if it is something else)
	phnd=LocateHandle(Handle, &hid);
	if(!phnd) return EFI_NOT_FOUND;

	fs=LocateFormSet(hid, NULL);
	if(!fs){
		if(!FormId){
		//Technically I must Return NOT_FOUND ERROR but...
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//this patch was made to keep Shell working. Shell and HII coexistance is 
//based on assumption that "It must be some IFR pack associated with String pack"
//If this statement FALSE Shell will hang in INFINITE LOOP!!!!
////////////////////////////////////////////////////////////////////////
//this comments I took from Intel's HII Database Driver
//	"If someone is passing in a string only, create a dummy IfrPack with a Guid 
//	to enable future searching of this data."
////////////////////////////////////////////////////////////////////////
			if(*BufferLength< sizeof(gDummyVfr)) {
				*BufferLength=sizeof(gDummyVfr);
				return EFI_BUFFER_TOO_SMALL;
			}
			*BufferLength=sizeof(gDummyVfr);
			pBS->CopyMem(Buffer,&gDummyVfr[0],sizeof(gDummyVfr));
			//copy handle guid here Shell expect it to mutch with some standart guids
			pBS->CopyMem((VOID*)(Buffer+8),(VOID*)&(phnd->Guid),sizeof(EFI_GUID));
			return EFI_SUCCESS;
		} else return EFI_NOT_FOUND;
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
	}

	if(!FormId){//we must return whole FormSet
		//STUPID SHELL expect me to return formset with EFI_HII_PACK_HEADER
		// why? I don't know!
		len=fs->BufferLength+sizeof(EFI_HII_PACK_HEADER);
	} else {
		frm=LocateForm(fs,FormId);
		if(!frm) return EFI_NOT_FOUND;
		len=frm->BufferLength;
	}

	if(*BufferLength<len){
		*BufferLength=len;
		return EFI_BUFFER_TOO_SMALL;
	}
	*BufferLength=len;

	//If "FormId" we must return whole FormSet
	if(!FormId){
		//STUPID SHELL expect me to return formset with EFI_HII_PACK_HEADER
		//Create it
		*((UINT32*)Buffer)=(UINT32)len;
		*((UINT16*)(Buffer+4))=3;//IFR Type
		CopyFormSet(Buffer+6,fs,&hid);
	}
	else 
		CopyForm(Buffer,frm,&hid);

	return	EFI_SUCCESS; 
}

///////////////////////////////////////////////////////////////////
EFI_STATUS HiiGetFormsOld(IN EFI_HII_PROTOCOL		*This,
					   IN EFI_HII_HANDLE		Handle,
					   IN EFI_FORM_ID			FormId,
					   IN OUT UINT16            *BufferLength,
					   OUT UINT8				*Buffer)
{
	EFI_STATUS			Status=0;
	UINTN				bl;
//--------------------------------
	if(BufferLength==NULL) return EFI_INVALID_PARAMETER;

	bl=(UINTN)(*BufferLength);
	Status=HiiGetFormsNew(This,Handle,FormId,&bl,Buffer);
	(*BufferLength)=(UINT16)bl;

	if((Status==EFI_BUFFER_TOO_SMALL) && (bl > 0xFFFF)) Status=EFI_INVALID_PARAMETER;
	return Status;
}


///////////////////////////////////////////////////////////////////
//Will free all memory blocks associated with Label->Objects structures
VOID DeleteLabelObjects(HII_LABEL *Label){
	UINTN		i;
	HII_OBJECT	*obj;
//------------------
	for(i=0;i<Label->Objects.ItemCount; i++){
		obj=Label->Objects.Items[i];
		ClearItemLst(&obj->Conditions,FALSE);
		ClearItemLst(&obj->Options,FALSE);
	}
	ClearItemLst(&Label->Objects,TRUE);
}



///////////////////////////////////////////////////////////////////
EFI_STATUS HiiUpdateForm(IN EFI_HII_PROTOCOL		*This,
						 IN EFI_HII_HANDLE			Handle,
						 IN EFI_FORM_LABEL			Label,
						 IN BOOLEAN					AddData,
						 IN EFI_HII_UPDATE_DATA		*Data)
{
	EFI_STATUS			Status=0;
	UINTN				hid,lid;
	HII_FORMSET			*fs;
	HII_FORM			*frm;
	HII_LABEL			*lbl;
	UINTN				len,cnt;
	UINT8				*ub;
	EFI_IFR_OP_HEADER	*op;
//-------------------------
	if(!This || !Data ) return EFI_INVALID_PARAMETER;

	if(!LocateHandle(Handle, &hid)) return EFI_INVALID_PARAMETER;
	
	//Find Formset
	fs=LocateFormSet(hid, NULL);
	if(!fs) return EFI_INVALID_PARAMETER;
	
	//Find Label
	//FormUpdate= TRUE means just change Form Title!!!
	if(Data->FormUpdate){ 
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//In Intel's HII implementation Data->FormValue field is missing
		//Find Form using UPDATE Data
//		frm=LocateForm(fs,Data->FormValue);
//		if(!frm) return EFI_NOT_FOUND;
		//Update Form Title
//		frm->FormData->FormTitle=Data->FormTitle;
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
	}

	//
	if(Data->DataCount){
		//Find Label
		lbl=LocateFormSetLabel(fs, Label, &lid);
		if(!lbl) return EFI_NOT_FOUND;	
		frm=lbl->OwnerForm;
		cnt=Data->DataCount;
		if(AddData){
			op=(EFI_IFR_OP_HEADER*)&Data->Data;
			if(!op->OpCode || !op->Length) return EFI_INVALID_PARAMETER;
			//Calculate size of UPDATE data
			len=GetDataLen((UINT8*)op,&cnt,FALSE);
			ub=Malloc(len+lbl->DataLength);
			if(!ub) return EFI_OUT_OF_RESOURCES;
		
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
			//Copy Update Data in Private Buffer
			pBS->CopyMem(ub,op,len);	
			//Copy Old Data in same Private Buffer
			if(lbl->LabelData)pBS->CopyMem((VOID*)(ub+len),lbl->LabelData,lbl->DataLength);
//for coimpatibility with Intel's setup I've change order in which the chanks of opcodes
// been copied in a new label buffer - New data will be added after the old data
			//if(lbl->LabelData)pBS->CopyMem(ub,lbl->LabelData,lbl->DataLength);
			//pBS->CopyMem((VOID*)(ub+lbl->DataLength),op,len);	
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO

			//Update Buffer parameters in every structure affected by update
			lbl->DataLength = lbl->DataLength + len;
			lbl->DataCount = lbl->DataCount + Data->DataCount;
			if(lbl->Updated && lbl->LabelData)pBS->FreePool(lbl->LabelData);
			lbl->LabelData=(EFI_IFR_OP_HEADER*)ub;
			fs->BufferLength = fs->BufferLength + len;
			frm->BufferLength = frm->BufferLength + len;
			//Delete Old Data
			DeleteLabelObjects(lbl);
		}else{
			//we will not erase an empty Label!
			if(lbl->LabelData){
				op=lbl->LabelData;
				if(Data->DataCount>=lbl->DataCount){
					cnt=lbl->DataCount;
					len=lbl->DataLength;
				} else len=GetDataLen((UINT8*)op,&cnt,TRUE);
				
				//if after erasing no data has left, no need to allocate new buffer 
				if((INTN)(lbl->DataLength-len)>0){
					ub=Malloc(lbl->DataLength-len);	
					if(!ub) 
						return EFI_OUT_OF_RESOURCES;
					//Copy rest of Old Data in Private Buffer
					pBS->CopyMem(ub,(UINT8*)lbl->LabelData+len,lbl->DataLength-len);
				} else ub=NULL;
										
				//Update Buffer parameters in every structure affected by update
				lbl->DataLength = lbl->DataLength - len;
				lbl->DataCount = lbl->DataCount - cnt;
				if(lbl->Updated && lbl->LabelData)pBS->FreePool(lbl->LabelData);
				lbl->LabelData=(EFI_IFR_OP_HEADER*)ub;
				fs->BufferLength = fs->BufferLength - len;
				frm->BufferLength = frm->BufferLength - len;
				DeleteLabelObjects(lbl);
			}
		}
		//Populate pointers on Label Objects from Label Dtat Buffer
		if(lbl->LabelData!=NULL)Status=CreateLabelObjects(lbl);
		if(EFI_ERROR(Status))return Status;
		lbl->Updated=TRUE;

		if(lbl->UpdateCallBack)	lbl->UpdateCallBack(Label, Handle,AddData);			
	}

	if(Data->FormSetUpdate){
		fs->FormSetData->CallbackHandle=Data->FormCallbackHandle;
	}
	
	return EFI_SUCCESS;
}

EFI_STATUS InitDefStorage(HII_FORMSET *FormSet){
	EFI_STATUS			Status;
	HII_STORE			*st;
	static CHAR8		*ss="Setup";//{'S','e','t','u','p',0};
	CHAR8				*pn;//pointer to var name;
//------------------------------------------------
	st=MallocZ(sizeof(HII_STORE));
	if(!st)return EFI_OUT_OF_RESOURCES;

	Status=AppendItemLst(&FormSet->Storages,st);

	st->VarStore=MallocZ(sizeof(EFI_IFR_VARSTORE)+6);
	if(!st->VarStore)return EFI_OUT_OF_RESOURCES;
	
	//Init Default VarStore Structure VarId is Always 0x0000
	st->VarStore->Header.Length=sizeof(EFI_IFR_VARSTORE)+6;
	st->VarStore->Header.OpCode=EFI_IFR_VARSTORE_OP;
	st->VarStore->Size=FormSet->FormSetData->NvDataSize;

	//Copy formset GUID here 
	pBS->CopyMem(&st->VarStore->Guid,&FormSet->FormSetData->Guid,sizeof(EFI_GUID));
	
	pn=(CHAR8*)st->VarStore;
	pn+=sizeof(EFI_IFR_VARSTORE);
	pBS->CopyMem(pn,ss,6);

	return EFI_SUCCESS;
}

//Selects Var Store Object
HII_STORE *LocateStorage(HII_FORMSET *FormSet, UINT16 VarId, UINTN *StoreId OPTIONAL){
	UINTN		i;
	HII_STORE	*st;
//---------------------
	for(i=0; i<FormSet->Storages.ItemCount; i++){
		st=(HII_STORE*)FormSet->Storages.Items[i];
		if(st->VarStore->VarId==VarId) {
			if(StoreId)*StoreId=i;
			return st;
		}
	}
	return NULL;
}


EFI_STATUS CreateLabelObjects(HII_LABEL *Label)
{
	EFI_IFR_OP_HEADER	*op;
	UINTN				i,j=0, k;
	HII_OBJECT			*obj=NULL;
	HII_STORE			*st1=NULL, *st2=NULL;
	static T_ITEM_LIST	clst={0,0,NULL},olst={0,0,NULL};	//temp conditions and objects list list 
    static T_ITEM_LIST  supprstores = {0, 0, NULL};
	EFI_STATUS			Status;
	BOOLEAN				cf=FALSE;	//conditions flag
    BOOLEAN             Suppress = FALSE;
//-------------------------------------------
	op=Label->LabelData;

	obj=NULL;
	//st1=(HII_STORE*)Label->OwnerForm->Owner->Storages.Items[0];
    st1 = LocateStorage(Label->OwnerForm->Owner, CurrentVarId, NULL);
    if(!st1) { st1=(HII_STORE*)Label->OwnerForm->Owner->Storages.Items[0]; CurrentVarId = 0; }
	st2=NULL;
	for(i=0;i<Label->DataCount;i++){
		//Select Default Storage so Store1
		switch(op->OpCode){
			//Storage Select Opcodes
			case EFI_IFR_VARSTORE_SELECT_OP:
			{
				EFI_IFR_VARSTORE_SELECT	*vs=(EFI_IFR_VARSTORE_SELECT*)op;
				HII_STORE				*st=LocateStorage(Label->OwnerForm->Owner,vs->VarId,NULL);
                // preserve the varstore selection - to be reset only by the next varstore select
                // or a new formset
			//------------------------
				if(st) { st1=st; CurrentVarId = vs->VarId; }
			}	
			break;
			case EFI_IFR_VARSTORE_SELECT_PAIR_OP:
			{
				EFI_IFR_VARSTORE_SELECT_PAIR	*v2=(EFI_IFR_VARSTORE_SELECT_PAIR*)op;
				HII_STORE				*s1=LocateStorage(Label->OwnerForm->Owner,v2->VarId,NULL);
				HII_STORE				*s2=LocateStorage(Label->OwnerForm->Owner,v2->SecondaryVarId,NULL);
				//------------------------
				if(s1) { st1=s1; CurrentVarId = v2->VarId; }
				if(s2) st2=s2; 
			}
			break;
			//Condition OpCodes
			case EFI_IFR_GRAYOUT_IF_OP:
			case EFI_IFR_SUPPRESS_IF_OP:
			case EFI_IFR_INCONSISTENT_IF_OP:
				//if we found some logical expression and local ovject list(olst) has some objects 
				//we must add this objects with no conditions to the Label->Objects list
				//and clear local object list to start fill it with objects who has conditions.
				if(!cf){
					for(j=0;j<olst.ItemCount;j++){
						obj=olst.Items[j];
						Status=AppendItemLst(&Label->Objects,obj);
						ASSERT(!EFI_ERROR(Status));
						if(EFI_ERROR(Status)) return Status;
					}
					ClearItemLst(&olst,FALSE);
					obj=NULL;
				}
				cf=TRUE;
                if( op->OpCode == EFI_IFR_SUPPRESS_IF_OP) Suppress = TRUE;
                else Suppress = FALSE;
			///////////////////////////////////////////////////////////////////////
			//Do not add break instruction  here it is not present for a reason!!!!
			///////////////////////////////////////////////////////////////////////
			case EFI_IFR_AND_OP:
			case EFI_IFR_OR_OP:
			case EFI_IFR_NOT_OP:
			case EFI_IFR_EQ_ID_VAL_OP: 
			case EFI_IFR_EQ_ID_ID_OP:
			case EFI_IFR_EQ_ID_LIST_OP:	
			case EFI_IFR_EQ_VAR_VAL_OP:
				//Add Opcode to the temp Condition list;
				Status=AppendItemLst(&clst,op);
				ASSERT(!EFI_ERROR(Status));
				if(EFI_ERROR(Status)) return Status;//out of resources

                if(op->OpCode == EFI_IFR_EQ_ID_VAL_OP && Suppress)
                {
                    Status = AppendItemLst(&supprstores, st1);
                    ASSERT(!EFI_ERROR(Status));
                }
			break;
			//end of conditions OpCode
			case EFI_IFR_END_IF_OP:
				//this is definetely end of object presentation,
				//but some times it might be no object to add conditions info to
				if(!olst.ItemCount){
					for(j=0; j<clst.ItemCount;j++) {
						Status=AppendItemLst(&Label->Conditions,clst.Items[j]);
						ASSERT(!EFI_ERROR(Status));
						if(EFI_ERROR(Status)) return Status;
					}
				}
				for(j=0;j<olst.ItemCount;j++){
					obj=olst.Items[j];
					//I'm trying to allocate exactly as mutch memory as I need for Conditions.Items[] array.
					//If you initialize ItemList.InitialCount with some specific value>0, then next call to 
					//Append ItemLst function will allocate this number of entries for ItemList.Items[] Array.
					obj->Conditions.InitialCount=clst.ItemCount;
					Status=AppendItemLst(&obj->Conditions,clst.Items[0]);
					ASSERT(!EFI_ERROR(Status));
					if(EFI_ERROR(Status)) return Status;
					pBS->CopyMem(&obj->Conditions.Items[1],&clst.Items[1],sizeof(VOID*)*(clst.ItemCount-1));
					obj->Conditions.ItemCount=clst.ItemCount;
                    for(k = 0; k < supprstores.ItemCount; k++)
                    {
                        Status=AppendItemLst(&obj->SuppressStores,supprstores.Items[k]);
						ASSERT(!EFI_ERROR(Status));
						if(EFI_ERROR(Status)) return Status;
                    }
                    
					Status=AppendItemLst(&Label->Objects,obj);
					ASSERT(!EFI_ERROR(Status));
					if(EFI_ERROR(Status)) return Status;
				}
				ClearItemLst(&clst,FALSE); //don't need to free data - it's VFR pointers!
				ClearItemLst(&olst,FALSE); 
                ClearItemLst(&supprstores,FALSE);
				obj=NULL;
				cf=FALSE;
                Suppress = FALSE;
			break;
			//visual opcodes
			case EFI_IFR_SUBTITLE_OP:
			case EFI_IFR_TEXT_OP:
			case EFI_IFR_INVENTORY_OP:
			case EFI_IFR_GRAPHIC_OP:
			case EFI_IFR_CHECKBOX_OP:
			case EFI_IFR_NUMERIC_OP:
			case EFI_IFR_PASSWORD_OP:
			case EFI_IFR_REF_OP:
			case EFI_IFR_DATE_OP:
			case EFI_IFR_TIME_OP:
			case EFI_IFR_STRING_OP:
			case EFI_IFR_SAVE_DEFAULTS_OP:
			case EFI_IFR_RESTORE_DEFAULTS_OP:
			case EFI_IFR_HIDDEN_OP:
			//continiouse 
			case EFI_IFR_ONE_OF_OP:
			case EFI_IFR_ORDERED_LIST_OP:
				ASSERT(!obj); //obj pointer must be cleared when we enter there
				if(obj)return EFI_INVALID_PARAMETER; 
				obj=MallocZ(sizeof(HII_OBJECT));
				ASSERT(obj);//Allocation Problems!
				if(!obj) return EFI_OUT_OF_RESOURCES;
				obj->ObjectCode=op;
				obj->Store1=st1;
				obj->Store2=st2;
				Status=AppendItemLst(&olst,obj);	
				ASSERT(!EFI_ERROR(Status));
				if(EFI_ERROR(Status)) return Status;
				//in case of DATE or Time opcode advance by 2 opcodes
				if(op->OpCode==EFI_IFR_TIME_OP || op->OpCode==EFI_IFR_DATE_OP){
					op=(EFI_IFR_OP_HEADER*)((UINT8*)op+op->Length);
					op=(EFI_IFR_OP_HEADER*)((UINT8*)op+op->Length);
					i+=2;
				}
				obj=NULL;
			break;
			case EFI_IFR_ONE_OF_OPTION_OP:
				ASSERT(olst.ItemCount);
				if(!olst.ItemCount)	return EFI_INVALID_PARAMETER;
				obj=(HII_OBJECT*)olst.Items[olst.ItemCount-1];
				if(!obj) return EFI_INVALID_PARAMETER; //obj must be allocated!
				Status=AppendItemLst(&obj->Options,op);
				ASSERT(!EFI_ERROR(Status));
				if(EFI_ERROR(Status)) return Status;//out of resources
			break;
			case EFI_IFR_END_ONE_OF_OP:					
				obj=NULL;
			break;

		}//end switch;
		//Advance to next opcode
		op=(EFI_IFR_OP_HEADER*)((UINT8*)op+op->Length);
	}
	//If there were no any Condition Opcodes contents of the ObjectList(olst) was not copied to the Label.Objects
	for(j=0;j<olst.ItemCount;j++){
		obj=olst.Items[j];
		Status=AppendItemLst(&Label->Objects,obj);
		ASSERT(!EFI_ERROR(Status));
		if(EFI_ERROR(Status)) return Status;
	}
	ClearItemLst(&olst,FALSE); 
	return EFI_SUCCESS;
}

//This Routine is called to parse FormVfr and Create Objects which must be shown on the screen;
EFI_STATUS CreateFormObjects(HII_FORM *Form){
	UINTN		i;
	EFI_STATUS	Status = EFI_SUCCESS;
//---------------------------------
	
	for(i=0; i<Form->Labels.ItemCount; i++){
		Status=CreateLabelObjects((HII_LABEL*)Form->Labels.Items[i]);		
		ASSERT(!EFI_ERROR(Status));
		if(EFI_ERROR(Status))return Status;
	}
	return Status;
}

EFI_STATUS NewKbdPack(EFI_HII_KEYBOARD_PACK	*Package,
					  UINTN				HandleId)
{
	EFI_STATUS			Status;
	HII_KB_LAYOUT_DATA	*kbld;	
//--------------------
	//Get Some Memory to accomodate Kb Layout Buffer	
	kbld=Malloc(sizeof(EFI_KEY_DESCRIPTOR)*Package->DescriptorCount+sizeof(HII_KB_LAYOUT_DATA));
	ASSERT(kbld);
	if(kbld==NULL) return EFI_OUT_OF_RESOURCES;

	kbld->HandleIndex=HandleId; //just in case to find HII_HANDLE structure assotiated with this PACK
	kbld->KeyDscCount=Package->DescriptorCount;
	//We will store KB Layout Data right after the HII_KB_LAYOUT_DATA structure itself
	kbld->KeyDsc=(EFI_KEY_DESCRIPTOR*)(kbld+1);
	MemCpy((VOID*)kbld->KeyDsc, (VOID*)Package->Descriptor, sizeof(EFI_KEY_DESCRIPTOR)*kbld->KeyDscCount);	
	
	//Add saved KB Layout Data to the HII DB 
	Status=AppendItemLst((T_ITEM_LIST*)&gHiiDb.KeybdDb.KblInitCount,(VOID*)kbld); 
	ASSERT_EFI_ERROR(Status);

	//Set recently submitted KB Layout as the current one
	gHiiDb.KeybdDb.ActiveLayout=gHiiDb.KeybdDb.KblCount-1;

	return Status;
}

EFI_STATUS NewIfrPack(EFI_HII_IFR_PACK	*Package,
					  UINTN				HandleId)
{
	EFI_STATUS			Status;
	EFI_IFR_OP_HEADER	*op;
//	INT32				id=0;
	HII_FORM			*cf=NULL;
	HII_FORMSET			*cfs=NULL;
	HII_LABEL			*cl=NULL;
	HII_STORE			*st=NULL;
	HII_HANDLE			*phnd=(HII_HANDLE*)gHiiDb.HandleDb.Items[HandleId];
	UINT8				*p;
	BOOLEAN				la=FALSE;//label active flag - helps count OPCODES and Label.dLength
//---------------------------------------------
	//Copy Data to a new Storage =(EFI_IFR_OP_HEADER*)(Package+1);
	p=Malloc(Package->Header.Length);
	if(!p) return EFI_OUT_OF_RESOURCES;
	pBS->CopyMem(p,Package,Package->Header.Length);

	op=(EFI_IFR_OP_HEADER*)(p+sizeof(EFI_HII_PACK_HEADER));
	//Parse IFR and crete Working set of data 
	//Every IFR Pack must start from FORM_SET_OP
	while(/*(op->OpCode!=EFI_IFR_END_FORM_SET_OP)||*/((UINTN)op<((UINTN)p)+Package->Header.Length)){
		switch (op->OpCode){
			//Take care of Forms Sets
			case EFI_IFR_FORM_SET_OP:
                CurrentVarId = 0;
				cfs=MallocZ(sizeof(HII_FORMSET));
				if(!cfs) return EFI_OUT_OF_RESOURCES;
				//cfs->BufferLength=0; it is 0 
				cfs->FormSetData=(EFI_IFR_FORM_SET*)op;
				cfs->HandleId=(UINT32)HandleId;
				cfs->Handle=phnd->Handle;
				//Setup Initial Storage var 
				Status=InitDefStorage(cfs);
				if(EFI_ERROR(Status)) return Status;
			break;
			case EFI_IFR_VARSTORE_OP:
				if(!cfs) return EFI_INVALID_PARAMETER; 
				st=MallocZ(sizeof(HII_STORE));
				if(!st) return EFI_OUT_OF_RESOURCES;
				Status=AppendItemLst(&cfs->Storages,st);
				st->VarStore=(EFI_IFR_VARSTORE*)op;
			break;
			case EFI_IFR_END_FORM_SET_OP:
				if(!cfs)return EFI_INVALID_PARAMETER; //found EFI_IFR_END_FORM_SET_OP without EFI_IFR_FORM_SET_OP
				//cfs->BufferLength=(UINT16)((UINT8*)op+op->Length-(UINT8*)cfs->FormSetData);
                CurrentVarId = 0;
				cfs->EndFormSet=(EFI_IFR_END_FORM_SET*)op;
				cfs->HandleId=(UINT32)HandleId;
				cfs->BufferLength = cfs->BufferLength + op->Length;
				Status=AppendItemLst(&gHiiDb.IfrDb,(VOID*)cfs);
				if(EFI_ERROR(Status)) return Status;
				cfs=NULL;
			break;
			//Take care of Forms and Labels
			case EFI_IFR_FORM_OP:
			case EFI_IFR_LABEL_OP:
			{	
				EFI_IFR_OP_HEADER	*nxt;
			//----------------------------
				if(op->OpCode==EFI_IFR_FORM_OP){
					//it must be some FORM_SET OpCode before we met FORM OpCode
					if(!cfs) return EFI_INVALID_PARAMETER; 
					cf=MallocZ(sizeof(HII_FORM));
					if(!cf) return EFI_OUT_OF_RESOURCES;
					//cf->BufferLength=0; 
					cf->FormData=(EFI_IFR_FORM*)op;
					cf->FormId=cf->FormData->FormId;
					cf->Owner=cfs;
				}
				if(!cf && !cfs )return EFI_INVALID_PARAMETER; //Form mus be present
				cl=MallocZ(sizeof(HII_LABEL));
				if(!cl) return EFI_OUT_OF_RESOURCES;

				cl->OwnerForm=cf;
				cl->Label=(EFI_IFR_LABEL*)op;
				//it must be a valid opcode next...
				nxt=(EFI_IFR_OP_HEADER*)((UINT8*)op+op->Length);
				if(nxt->OpCode!=EFI_IFR_END_FORM_OP && nxt->OpCode!=EFI_IFR_LABEL_OP)cl->LabelData=nxt;
			
				if(op->OpCode==EFI_IFR_FORM_OP){
					cl->LabelId=0xFACE; //Fake LabelID
				} else {
					cl->LabelId=cl->Label->LabelId;
					Status=AppendItemLst(&cfs->Labels,cl);
					if(EFI_ERROR(Status)) return Status;
				}
	
				Status=AppendItemLst(&cf->Labels,cl);
				if(EFI_ERROR(Status)) return Status;

				la=TRUE;
			}
			break;
			case EFI_IFR_END_FORM_OP:
				if(!cf)return EFI_INVALID_PARAMETER; //found EFI_IFR_END_FORM_OP without EFI_IFR_FORM_OP
				//cf->BufferLength=(UINT16)((UINT8*)op+op->Length-(UINT8*)cf->FormData);
				cf->EndForm=(EFI_IFR_END_FORM*)op;
				cf->BufferLength = cf->BufferLength + op->Length;
				Status=AppendItemLst(&cfs->Forms,cf);
				if(EFI_ERROR(Status)) return Status;
				Status=CreateFormObjects(cf);
				cf=NULL;
				la=FALSE;
			break;
			//Labels Has Left
			/*case EFI_IFR_LABEL_OP:{
				EFI_IFR_OP_HEADER	*nxt;
			//---------------------------
				if(!cf && !cfs )return EFI_INVALID_PARAMETER; //Form mus be present
				cl=MallocZ(sizeof(HII_LABEL));
				if(!cl) return EFI_OUT_OF_RESOURCES;

				cl->Label=(EFI_IFR_LABEL*)op;
				cl->LabelId=cl->Label->LabelId;
				cl->OwnerForm=cf;
				nxt=(EFI_IFR_OP_HEADER*)(cl->Label+1);
				//it must be a valid opcode next...
				if(nxt->OpCode!=EFI_IFR_END_FORM_OP && nxt->OpCode!=EFI_IFR_LABEL_OP)cl->LabelData=nxt;

				Status=AppendItemLst(&cf->Labels,cl);
				if(EFI_ERROR(Status)) return Status;

				Status=AppendItemLst(&cfs->Labels,cl);
				if(EFI_ERROR(Status)) return Status;
				la=TRUE;
				break;
				}
			*/
			default :
				if(la){
					cl->DataCount++;
					cl->DataLength = cl->DataLength + op->Length;
				}
		} //Switch
		if(cfs)cfs->BufferLength = cfs->BufferLength + op->Length;
		if(cf)cf->BufferLength = cf->BufferLength + op->Length;
		op=(EFI_IFR_OP_HEADER*)((UINT8*)op+op->Length);
	}
	phnd->Ifr=p;
	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
// Common Interface functions
///////////////////////////////////////////////////////////////////
EFI_STATUS HiiNewPackNew(IN EFI_HII_PROTOCOL	*This,
						 IN  EFI_HII_PACKAGES    *Packages,
						 OUT EFI_HII_HANDLE      *Handle)
{
	UINTN				i;
	EFI_HII_PACK_HEADER	*hdr, **hdrptr;
	EFI_STATUS			Status=0;
	UINTN				c=0,hid=0;
	HII_HANDLE			*phnd=NULL;
	BOOLEAN 			sp=0,vp=0,hp=0;
//-----------------------------------------
	if(!This) return EFI_INVALID_PARAMETER;
		
	hdrptr=(EFI_HII_PACK_HEADER**)(Packages+1);
	for(i=0; i<Packages->NumberOfPackages; i++){
		hdr=hdrptr[i];
		switch(hdr->Type){
			case EFI_HII_FONT:
				Status=NewFontPack((EFI_HII_FONT_PACK*)hdr); 
				if(EFI_ERROR(Status)) return Status;
			break;
			case EFI_HII_STRING:
				if(sp)return EFI_INVALID_PARAMETER;
				phnd=LocateHandleGuid(Packages->GuidId,&hid);
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//this is wrong !!! 
		//here we are trying to check if somebody using passed GuidId registered Ifr with us 
		//it should not be like this but Intel's Setup Browser uses same GuidId sor 2 different 
		//Ifr instances. To maintain compatibility I'll try to patch code...
				if(!phnd || phnd->HasStr){
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
					Status=LocateHandleAdd(gHiiDb.NextHiiHandle,Packages->GuidId,&hid);
					if(Status) return Status;		
					else c++;
					phnd=gHiiDb.HandleDb.Items[hid];
				}
				Status=NewStringPack((EFI_HII_STRING_PACK*)hdr, hid); 
				if(EFI_ERROR(Status)) return Status;
				sp=TRUE;
			break;
			case EFI_HII_IFR:
				if(vp)return EFI_INVALID_PARAMETER;
				phnd=LocateHandleGuid(Packages->GuidId,&hid);
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//this is wrong !!!//same goes gor IFR 
				if(!phnd || phnd->Ifr){
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
					Status=LocateHandleAdd(gHiiDb.NextHiiHandle,Packages->GuidId,&hid);
					if(Status) return Status;		
					else c++;
					phnd=gHiiDb.HandleDb.Items[hid];
				}
		
				Status=NewIfrPack((EFI_HII_IFR_PACK*)hdr, hid); 
				if(EFI_ERROR(Status)) return Status;
				vp=TRUE;
			break;
			case EFI_HII_KEYBOARD:
				Status=NewKbdPack((EFI_HII_KEYBOARD_PACK*)hdr, hid);
			break;
			case EFI_HII_HANDLES:
				if(hp)return EFI_INVALID_PARAMETER;
//TODO			
				hp=TRUE;	
			break;
			case EFI_HII_VARIABLE:
//TODO			
			break;
			case EFI_HII_DEVICE_PATH:
//TODO
			break;
			default: return EFI_INVALID_PARAMETER;
		}//switch
	}
	if(c)gHiiDb.NextHiiHandle++;

	if(phnd)*Handle=phnd->Handle;
	else *Handle=0;

	return Status; 
}

EFI_STATUS HiiNewPackOld (IN EFI_HII_PROTOCOL	*This,
					IN EFI_HII_PACK_LIST	*Package,
					OUT EFI_HII_HANDLE		*Handle)
{
	EFI_STATUS			Status=0;
	UINTN				c=0,hid;
	HII_HANDLE			*phnd=NULL;
//----------------------------------
	if(!This) return EFI_INVALID_PARAMETER;

	//Check if we have this GUID ib GuidDb already 
	//For Font and Keyboadr Data HAndle is allways = 0  	
	if(Package->FontPack) Status=NewFontPack(Package->FontPack); 
	if(EFI_ERROR(Status)) return Status;

	if(Package->StringPack){
		phnd=LocateHandleGuid(Package->GuidId,&hid);
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//this is wrong !!! 
		//here we are trying to check if somebody using passed GuidId registered Ifr with us 
		//it should not be like this but Intel's Setup Browser uses same GuidId sor 2 different 
		//Ifr instances. To maintain compatibility I'll try to patch code...
		if(!phnd || phnd->HasStr){
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//		if(!phnd){
			Status=LocateHandleAdd(gHiiDb.NextHiiHandle,Package->GuidId,&hid);
			if(Status) return Status;		
			else c++;
			phnd=gHiiDb.HandleDb.Items[hid];
		}

		//if(phnd->HasStr) return EFI_INVALID_PARAMETER;
		Status=NewStringPack(Package->StringPack, hid); 
		if(EFI_ERROR(Status)) return Status;
	}

	if(Package->IfrPack){
		phnd=LocateHandleGuid(Package->GuidId,&hid);
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//this is wrong !!! 
		//here we are trying to check if somebody using passed GuidId registered Ifr with us 
		//it should not be like this but Intel's Setup Browser uses same GuidId sor 2 different 
		//Ifr instances. To maintain compatibility I'll try to patch code...
		if(!phnd || phnd->Ifr){
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//		if(!phnd){
			Status=LocateHandleAdd(gHiiDb.NextHiiHandle,Package->GuidId,&hid);
			if(Status) return Status;		
			else c++;
			phnd=gHiiDb.HandleDb.Items[hid];
		}
		
		//if(phnd->HasIfr) return EFI_INVALID_PARAMETER;
		Status=NewIfrPack(Package->IfrPack, hid); 
		if(EFI_ERROR(Status)) return Status;
	}
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//nothing is done for keyboard layout functions 
//this is empty spot in spec!
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
	if(c)gHiiDb.NextHiiHandle++;

	if(phnd)*Handle=phnd->Handle;
	else *Handle=0;

	return Status; 
}

///////////////////////////////////////////////////////////////////
//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO//TODO
//this function even not in spec yet so UNSUPPORT it!!!
// here is an explanation I've got from Initial's Source 
//  "This function allows to extract the NV Image 
//  that represents the default storage image"
//Still Unclear what to do with it
EFI_STATUS HiiGetDefaultImageOld(IN EFI_HII_PROTOCOL		*This,
							  IN EFI_HII_HANDLE			Handle,
							  IN UINTN					DefaultMask,
							  IN OUT UINT16             *BufferLength,
							  IN OUT UINT8              *Buffer)
{
	HII_HANDLE			*phnd;
	HII_FORMSET			*fs;
	EFI_IFR_OP_HEADER	*op;
	UINTN				hi,fsi,i,t=0,cv=0;
	UINT8				*fsb;//formset buffer
	BOOLEAN				ol=FALSE; 
	EFI_STATUS			Status;
//---------------------------------
	if(!This) return EFI_INVALID_PARAMETER;
	
	phnd=LocateHandle(Handle,&hi);
	if(!phnd || !phnd->Ifr) return EFI_INVALID_PARAMETER;
	
	fs=LocateFormSet(hi,&fsi);
	if(!fs) return EFI_INVALID_PARAMETER;

	if(*BufferLength<fs->FormSetData->NvDataSize){
		*BufferLength=fs->FormSetData->NvDataSize;
		return EFI_BUFFER_TOO_SMALL;
	}
	*BufferLength=fs->FormSetData->NvDataSize;
	i=*BufferLength;

	if (DefaultMask & EFI_IFR_FLAG_DEFAULT) 
		Status=pRS->GetVariable(L"SetupDefaultOverride",&phnd->Guid,NULL,&i,Buffer);
	else {
		if (DefaultMask & EFI_IFR_FLAG_MANUFACTURING) 
            Status=pRS->GetVariable(L"SetupManufacturingOverride",&phnd->Guid,NULL,&i,Buffer);
		else {
			DefaultMask = DefaultMask | EFI_IFR_FLAG_DEFAULT;
			Status=pRS->GetVariable(L"Setup",&phnd->Guid,NULL,&i,Buffer);
		}
	}
	
	fsb=Malloc(fs->BufferLength);
	if(!fsb) return EFI_OUT_OF_RESOURCES;
	CopyFormSet(fsb,fs,&i);

	op=(EFI_IFR_OP_HEADER*)fsb;
	while(op->OpCode!=EFI_IFR_END_FORM_SET_OP){
		switch(op->OpCode){
			case EFI_IFR_ORDERED_LIST_OP:
				ol=TRUE;
				i=((EFI_IFR_ONE_OF*)op)->QuestionId;
				t=((EFI_IFR_ONE_OF*)op)->Width;
				break;
			case EFI_IFR_ONE_OF_OP:
				ol=FALSE;
				i=((EFI_IFR_ONE_OF*)op)->QuestionId;
				t=((EFI_IFR_ONE_OF*)op)->Width;
				break;
			case EFI_IFR_ONE_OF_OPTION_OP:{
				EFI_IFR_ONE_OF_OPTION	*ifro=(EFI_IFR_ONE_OF_OPTION*)op;
			//-----------------
				if(!cv){ 
					if(ol){
						//pBS->CopyMem(&Buffer[i],&((EFI_IFR_ONE_OF_OPTION*)op)->Value, 1);
						pBS->CopyMem(&Buffer[i],&ifro->Value, 1);	
						i++;
					} else {
						//if (((EFI_IFR_ONE_OF_OPTION*)op)->Flags & DefaultMask) 
						//	pBS->CopyMem(&Buffer[i],&((EFI_IFR_ONE_OF_OPTION*)op)->Value,t);
						if (ifro->Flags & DefaultMask) 
							pBS->CopyMem(&Buffer[i],&ifro->Value,t);
            		}
				} 
			} break;
			case EFI_IFR_END_OP:
				t=0;
				break;
			case EFI_IFR_CHECKBOX_OP:{
				EFI_IFR_CHECK_BOX *ifrc=(EFI_IFR_CHECK_BOX*)op;
			//--------------------
				if(!cv){
					//if(((EFI_IFR_CHECK_BOX*)op)->Flags & DefaultMask)
					//	Buffer[((EFI_IFR_CHECK_BOX*)op)->QuestionId] = 1;
					//else
					//	Buffer[((EFI_IFR_CHECK_BOX*)op)->QuestionId] = 0;
					if(ifrc->Flags & DefaultMask)
						Buffer[ifrc->QuestionId] = 1;
					else
						Buffer[ifrc->QuestionId] = 0;
				}
			} break;
			case EFI_IFR_NUMERIC_OP:{
				EFI_IFR_NUMERIC *ifrn=(EFI_IFR_NUMERIC*)op;
			//--------------------------
		    	//if (!cv)pBS->CopyMem(&Buffer[((EFI_IFR_NUMERIC*)op)->QuestionId],
				//		&((EFI_IFR_NUMERIC*)op)->Default,((EFI_IFR_NUMERIC*)op)->Width);
		        if (!cv){
					pBS->CopyMem(&Buffer[ifrn->QuestionId],	&ifrn->Default,ifrn->Width);
				}
			} break;
			
			case EFI_IFR_VARSTORE_SELECT_OP:
			case EFI_IFR_VARSTORE_SELECT_PAIR_OP:
				cv=((EFI_IFR_VARSTORE_SELECT*)op)->VarId;
			break;
		}
		op=(EFI_IFR_OP_HEADER*)((UINT8*)op+op->Length);
	}
	pBS->FreePool(fsb);
	return EFI_SUCCESS;
}
///////////////////////////////////////////////////////////////////
EFI_STATUS HiiFindHandles(IN EFI_HII_PROTOCOL	*This,
						  //spec not says in what units assume, for now, it is in EFI_HII_HANDLE
						  IN OUT UINT16			*HandleBufferLength,
						  OUT EFI_HII_HANDLE	*Handle)
{
	UINTN				i;
	HII_HANDLE			*hnd;
//-------------------------------
	if(!This) return EFI_INVALID_PARAMETER;

	if(*HandleBufferLength<gHiiDb.HandleDb.ItemCount*sizeof(EFI_HII_HANDLE)){
		*HandleBufferLength = (UINT16)gHiiDb.HandleDb.ItemCount*sizeof(EFI_HII_HANDLE);
 		return EFI_BUFFER_TOO_SMALL;
	}
	*HandleBufferLength = (UINT16)gHiiDb.HandleDb.ItemCount*sizeof(EFI_HII_HANDLE);
	for (i=0; i<gHiiDb.HandleDb.ItemCount; i++){
		hnd=(HII_HANDLE*)gHiiDb.HandleDb.Items[i];
		Handle[i]=hnd->Handle;
	}
	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
// Will remove all stuff associated with form set on particular handle
// including all Update Data and Free all memory used to store private data  
//	This function assumes "HandleIdx" is valid.
VOID RemoveFormSet(UINTN HandleIdx){
	HII_FORMSET		*fst;
	HII_FORM		*frm;
	HII_LABEL		*lbl;
	UINTN			i,j,fsi;
//--------------------------------
	while(TRUE) { //it might be more then one formset with the same Handle
		fst=LocateFormSet(HandleIdx, &fsi);
		if(!fst) return; //Not a single form set was registred with this Handle

		for(i=0;i<fst->Forms.ItemCount;i++){
			frm=(HII_FORM*)fst->Forms.Items[i];
			for(j=0;j<frm->Labels.ItemCount;j++){
				lbl=frm->Labels.Items[j];
				DeleteLabelObjects(lbl);
				if(lbl->LabelData && lbl->Updated) pBS->FreePool(lbl->LabelData);
			}
			ClearItemLst(&frm->Labels,TRUE);
		}
		ClearItemLst(&fst->Labels,FALSE);
		ClearItemLst(&fst->Forms,TRUE);
		DeleteItemLst(&gHiiDb.IfrDb,fsi,TRUE);
	}
}

///////////////////////////////////////////////////////////////////
// Will remove all String records associated with "HandleIdx" for 
// all languages.  This function assumes "HandleIdx" is valid.
VOID RemoveStrings(UINTN HandleIdx){
	HII_HANDLE		*hnd=(HII_HANDLE*)gHiiDb.HandleDb.Items[HandleIdx];
	HII_STR			*str;
	INTN			i;
//-----------------------
	for(i=0; (UINTN)i<gHiiDb.StringDb.RecordCount; i++){
		DbeGoToIndex(&gHiiDb.StringDb,STR_KEY,(UINTN)i,&str);
		if(str->Handle==hnd->Handle){
			if(str->String.StrPtr)pBS->FreePool(str->String.StrPtr);
			if(str->NewStr.StrPtr)pBS->FreePool(str->NewStr.StrPtr);
			DbeDelete(&gHiiDb.StringDb,str,TRUE);
			i--;
			continue;
		}
		if(str->Handle>hnd->Handle) break;
	}
}


///////////////////////////////////////////////////////////////////
EFI_STATUS HiiRemovePack(IN EFI_HII_PROTOCOL		*This,
						 IN EFI_HII_HANDLE			Handle)

{
	UINTN				hid;
	HII_HANDLE			*phnd;
	HII_FORMSET			*fs;
	UINTN				i;
//--------------------
	if(!This) return EFI_INVALID_PARAMETER;
	
	//Here we will try to remove Pack from DB
	phnd=LocateHandle(Handle,&hid);
	if(!phnd) return EFI_INVALID_PARAMETER;

	//So we found requested Handle, now remove all stuff associated with it.
	//Remove IFRs
	if(phnd->Ifr){ 
		RemoveFormSet(hid); 
		pBS->FreePool(phnd->Ifr);
	}
	//Remove Strings
	if(phnd->HasStr)RemoveStrings(hid);

	//Free Items[] buffer if it was allocated
	//if no Secondary Languages installed for this HII_HANDLE nothing has to be done
	if(phnd->SecLang.ItemCount)ClearItemLst(&phnd->SecLang,FALSE);

	//reindexing remaining formsets to take care of
	//removed HII_HANDLE record from HiiDB.HandleDb[]
	for(i=0; i<gHiiDb.IfrDb.ItemCount; i++) {
		fs=gHiiDb.IfrDb.Items[i];
		if(fs->HandleId>hid) fs->HandleId--;
	}

	DeleteItemLst(&gHiiDb.HandleDb, hid, TRUE);

    //The current saved handle might be pointing on the one been removed.
    //so reset them to initial values.
    mCurHnd=NULL;
    mCurLang=NULL;
    mCurHid=0;
    mCurLid=0;
    mCurStr=NULL;

	return EFI_SUCCESS;
}

///////////////////////////////////////////////////////////////////
//HelperFunction to calculate size needed to dump the database
//excluding EFI_HII_DATA_TABLE header 
UINTN	GetHandleExpBuffSize(HII_HANDLE *HiiHandle, UINTN HndIndex){
	UINTN			sz=0, i, li=(UINTN)-1;	
	HII_FORMSET		*fs=NULL;
	HII_STR			*str=NULL;
	HII_STORE		*st=NULL;
//-------------------------------------	

	//This is an order of data structures as provided in Intel's implementation	
	//DevPath Data stuff Dummy so far just reserve space for DP_PACK and END_OF_DP
	sz+=sizeof(EFI_HII_DEVICE_PATH_PACK)+sizeof(EFI_DEVICE_PATH_PROTOCOL);

	//Find Formset we are working with
	fs=LocateFormSet(HndIndex, NULL);

	if(!fs) sz+= sizeof(gDummyVfr); //no formset no VAR data just Dummy Formset
	else {	
		//Variable data	
		for(i=0; i<fs->Storages.ItemCount; i++){
		//--------------------
			st=fs->Storages.Items[i];
			if(st->VarStore->Size){
				UINT8	*p;
				UINTN	ind = 0, vsize = 0;
				UINT16 VarName[256] = {0};
			//---------------------
				sz+=sizeof(EFI_HII_VARIABLE_PACK);
				//sz+=st->VarStore->Size;
				p=(UINT8*)(st->VarStore+1);
				while(*p){
					p++;
					vsize+=sizeof(UINT16); 
				}
				vsize+=sizeof(UINT16);//NULL terminator
				sz+=vsize;
				
				p=(UINT8*)(st->VarStore+1);
				while(*p && ind < sizeof(VarName)){
					VarName[ind] = (CHAR16)*p;
					p++; ind++;
				}
				vsize=0;
				pRS->GetVariable(VarName,&st->VarStore->Guid,NULL,&vsize,NULL);
				if(vsize)
					sz+=vsize;
				else
					sz += st->VarStore->Size;
			}	
		}
		if(fs->Storages.ItemCount > 0)
			sz+=sizeof(EFI_HII_VARIABLE_PACK); // !! added dummy VARIABLE PACK

		//formset data size
		sz+=fs->BufferLength+sizeof(EFI_HII_PACK_HEADER);// !! changed to +=
	}	

	//string data size 
	if(HiiHandle->HasStr){
		for(i=0; i<gHiiDb.StringDb.RecordCount; i++){
			DbeGoToIndex(&gHiiDb.StringDb,STR_KEY,(UINTN)i,&str);
			if(str->Handle==HiiHandle->Handle){
				if((li==-1)||(li!=str->LangIdx)){
					li=str->LangIdx;
					sz+=sizeof(EFI_HII_STRING_PACK); //new pack for new language
				}
				//the StrLen stored in UNICODECHAR units
				if(str->NewStr.StrPtr)sz+=str->NewStr.StrLen*sizeof(CHAR16)+sizeof(RELOFST);
				else sz+=str->String.StrLen*sizeof(CHAR16)+sizeof(RELOFST); 
			}
			if((str->Handle>HiiHandle->Handle)||(i==gHiiDb.StringDb.RecordCount-1)){ 
				sz+=sizeof(EFI_HII_STRING_PACK);//terminator pack
				break;
			}
		}
	}
	return sz;
}


UINT32 CountStrOfLang(HII_HANDLE *HiiHandle, UINTN StartIdx, UINTN LangIdx ){
	UINTN	i;
	UINT32	cnt=0;
	HII_STR	*str;
//----------------------
	for (i=StartIdx; i<gHiiDb.StringDb.RecordCount;i++){
		DbeGoToIndex(&gHiiDb.StringDb,STR_KEY,i,&str);
		if(str->Handle==HiiHandle->Handle){
			if(str->LangIdx==LangIdx)cnt++;
			else break;
		} else break;
	}
	return cnt;
}


VOID ExportHandleData(IN HII_HANDLE *HiiHandle, IN UINTN HndIndex, 
					  IN EFI_HII_DATA_TABLE *DataTbl, IN OUT UINT8 **BufferPointer)
{
	UINTN			sz=0, i;	
	HII_FORMSET		*fs;
	HII_STR			*str;
	HII_STORE		*st;
	UINT8			*p1=*BufferPointer, *p2;
	EFI_STATUS		Status;
//-------------------------------------	

	//This is an order of data structures as provided in Intel's implementation	
	//DevPath Data stuff Dummy so far END_OF_DP
	DataTbl->DevicePathOffset=(UINT32)(p1-(UINT8*)DataTbl);

	((EFI_HII_DEVICE_PATH_PACK*)p1)->Header.Type=EFI_HII_DEVICE_PATH;

	//points to the space after EFI_HII_DEVICE_PATH_PACK structure
	p2=(p1+sizeof(EFI_HII_DEVICE_PATH_PACK));

	((EFI_DEVICE_PATH_PROTOCOL*)p2)->Type=0xFF;
	((EFI_DEVICE_PATH_PROTOCOL*)p2)->SubType=0xFF;
//	((EFI_DEVICE_PATH_PROTOCOL*)p2)->Length=0;
	((EFI_DEVICE_PATH_PROTOCOL*)p2)->Length[0]=0;
	((EFI_DEVICE_PATH_PROTOCOL*)p2)->Length[1]=0;
	((EFI_HII_DEVICE_PATH_PACK*)p1)->Header.Length=(UINT32)(p2-p1+sizeof(EFI_DEVICE_PATH_PROTOCOL));

	p1=p2+sizeof(EFI_DEVICE_PATH_PROTOCOL);

	//Find Formset we are working with
	fs=LocateFormSet(HndIndex, NULL);
	if(!fs) { 
		//No formset no VAR data
		DataTbl->VariableDataOffset=0;
		
		//Copy a Dummy formset to be compatible with Intel
		DataTbl->IfrDataOffset=(UINT32)(p1-(UINT8*)DataTbl);

		pBS->CopyMem(p1,&gDummyVfr[0],sizeof(gDummyVfr));
		//copy handle guid here Shell expect it to mutch with some standart guids
		pBS->CopyMem((VOID*)(p1+8),(VOID*)&HiiHandle->Guid,sizeof(EFI_GUID));
		//Adjust the pointer
		p1+=sizeof(gDummyVfr);
	} else {	
		//Variable data	
		DataTbl->VariableDataOffset=(UINT32)(p1-(UINT8*)DataTbl);
		for(i=0; i<fs->Storages.ItemCount; i++){
		//--------------------
			st=fs->Storages.Items[i];
			if(st->VarStore->Size){
				UINT8	*p;
			//---------------------
				p2=p1+sizeof(EFI_HII_VARIABLE_PACK);
				((EFI_HII_VARIABLE_PACK*)p1)->Header.Type=EFI_HII_VARIABLE;
				//pBS->CopyMem((VOID*)&(((EFI_HII_VARIABLE_PACK*)p1)->VariableGuid), 
				//	&st->VarStore.Guid, sizeof(EFI_GUID));

				((EFI_HII_VARIABLE_PACK*)p1)->VariableGuid=st->VarStore->Guid;
				((EFI_HII_VARIABLE_PACK*)p1)->VariableId=st->VarStore->VarId;

				p=(UINT8*)(st->VarStore+1);
				while(*p){
					*p2=*p;
					p++;
					p2+=sizeof(UINT16);
					//per spec VariableLength goes in Bytes
					((EFI_HII_VARIABLE_PACK*)p1)->VariableNameLength+=sizeof(UINT16);
				}	
				p2+=sizeof(UINT16); //Null terminaterd
				((EFI_HII_VARIABLE_PACK*)p1)->VariableNameLength+=sizeof(UINT16);
				//Copy contents of the variable at present p2
				p=p1+sizeof(EFI_HII_VARIABLE_PACK);
				sz=st->VarStore->Size;
				Status=pRS->GetVariable((UINT16*)p,&st->VarStore->Guid,NULL,&sz,p2);
							
				DataTbl->NumberOfVariableData++;
				//don't forget to update size of Var Data in Pack Header
				((EFI_HII_VARIABLE_PACK*)p1)->Header.Length=(UINT32)(p2+sz-p1);

				p1=p2+sz;
			}		
		}
		if(!DataTbl->NumberOfVariableData)DataTbl->VariableDataOffset=0;	
		else {  // !! Add zero-terminating EFI HII variable
				((EFI_HII_VARIABLE_PACK*)p1)->Header.Type=EFI_HII_VARIABLE;
				((EFI_HII_VARIABLE_PACK*)p1)->Header.Length = sizeof(EFI_HII_VARIABLE_PACK);
				p1 += sizeof(EFI_HII_VARIABLE_PACK);
		}	
		//Formset goes data here
		DataTbl->IfrDataOffset=(UINT32)(p1-(UINT8*)DataTbl);
		p2=p1+sizeof(EFI_HII_PACK_HEADER);
		((EFI_HII_PACK_HEADER*)p1)->Length=(UINT32)fs->BufferLength+sizeof(EFI_HII_PACK_HEADER);
		((EFI_HII_PACK_HEADER*)p1)->Type=EFI_HII_IFR;
		
		i=0;
		CopyFormSet(p2,fs,&i);
		p1=p2+i;
	}	
	//If HIIHandle Data don't have Strings make p2==p1 since we are returning p2 value
	p2=p1;
	//string data goes here 
	if(HiiHandle->HasStr){
		UINT8		*p=p1;
		UINT32		li=(UINTN)-1;
	//-----------------
		DataTbl->StringDataOffset=(UINT32)(p1-(UINT8*)DataTbl);
		for(i=0; i<gHiiDb.StringDb.RecordCount; i++){
			DbeGoToIndex(&gHiiDb.StringDb,STR_KEY,(UINTN)i,&str);
			if(str->Handle==HiiHandle->Handle){
		
				if((li==-1)||(li!=str->LangIdx)){
					//p1 points on the EFI_HII_STRING_PACK
					p1=p;
					((EFI_HII_STRING_PACK*)p1)->Header.Type=EFI_HII_STRING;
					DataTbl->NumberOfLanguages++;
					//p2 points on the RELOFS array
					p2=p1+sizeof(EFI_HII_STRING_PACK);
					
					li=str->LangIdx;
					
					((EFI_HII_STRING_PACK*)p1)->NumStringPointers=CountStrOfLang(HiiHandle,i,li);
					//p points on the UnicodeString;
					p=p2+((EFI_HII_STRING_PACK*)p1)->NumStringPointers*sizeof(UINT32);
					((EFI_HII_STRING_PACK*)p1)->LanguageNameString=(UINT32)((UINTN)p-(UINTN)p1);
					((EFI_HII_STRING_PACK*)p1)->PrintableLanguageName=((EFI_HII_STRING_PACK*)p1)->LanguageNameString
						+4*sizeof(CHAR16);
					((EFI_HII_STRING_PACK*)p1)->Header.Length=sizeof(EFI_HII_STRING_PACK)+
						((EFI_HII_STRING_PACK*)p1)->NumStringPointers*sizeof(UINT32);

				}
				//update offset table 
				*((UINT32*)p2)=(UINT32)((UINTN)p-(UINTN)p1);
				//the StrLen stored in UNICODECHAR units including NULL CHAR16
				if(str->NewStr.StrPtr){
					pBS->CopyMem(p, str->NewStr.StrPtr, str->NewStr.StrLen*sizeof(CHAR16));
					p+=str->NewStr.StrLen*sizeof(CHAR16);
					((EFI_HII_STRING_PACK*)p1)->Header.Length+=str->NewStr.StrLen*sizeof(CHAR16);
				} else {
					pBS->CopyMem(p, str->String.StrPtr, str->String.StrLen*sizeof(CHAR16));
					p+=str->String.StrLen*sizeof(CHAR16);
					((EFI_HII_STRING_PACK*)p1)->Header.Length+=str->String.StrLen*sizeof(CHAR16);
				}
				p2+=sizeof(UINT32);
			}
			//don't forget to put terminator pack at the very end of the string data
			if((str->Handle > HiiHandle->Handle) || (i==gHiiDb.StringDb.RecordCount-1)){ 
				//Create a terminator pack
				p1=p;
				((EFI_HII_STRING_PACK*)p1)->Header.Type=EFI_HII_STRING;
				p2=p1+sizeof(EFI_HII_STRING_PACK);
				break;
			}
		}
	}

	*BufferPointer=p2;

}



///////////////////////////////////////////////////////////////////
EFI_STATUS HiiExport(IN EFI_HII_PROTOCOL		*This,
					 IN EFI_HII_HANDLE			Handle,
					 IN OUT UINTN				*BufferSize,
					 OUT VOID					*Buffer )
{
	UINTN					sz, i;
	HII_HANDLE				*phnd=NULL;
	EFI_HII_EXPORT_TABLE	*expt=NULL;
	EFI_HII_DATA_TABLE		*cdt=NULL;
	UINT8					*bp=NULL;	
//----------------
	if(!This) return EFI_INVALID_PARAMETER;
	
	sz=sizeof(EFI_HII_EXPORT_TABLE);

	//Calculate buffer Size 
	for(i=0; i<gHiiDb.HandleDb.ItemCount; i++){
		phnd=(HII_HANDLE*)gHiiDb.HandleDb.Items[i];
		if(Handle && Handle!=phnd->Handle) continue;
		sz+=GetHandleExpBuffSize(phnd,i) + sizeof(EFI_HII_DATA_TABLE);
	}
	//Check the buffer size provided
	if(sz > *BufferSize){
		*BufferSize=sz;
		return EFI_BUFFER_TOO_SMALL; 
	}	
	pBS->SetMem(Buffer, sz, 0);
	//Export Table
	expt=(EFI_HII_EXPORT_TABLE*)Buffer;
	if((UINTN)This == (UINTN)&gHiiDb.HiiProtocolOld) expt->Revision=gEfiHiiProtocolGuidOld;
	else expt->Revision=gEfiHiiProtocolGuidNew;

	bp=(UINT8*)(expt+1);

	//Start dumping the data
	for(i=0 ; i<gHiiDb.HandleDb.ItemCount; i++){
		
		phnd=(HII_HANDLE*)gHiiDb.HandleDb.Items[i];
		if(Handle && Handle!=phnd->Handle) continue;
		//CurrentDataTable
		cdt=(EFI_HII_DATA_TABLE*)bp;
		bp+=sizeof(EFI_HII_DATA_TABLE);

		sz=GetHandleExpBuffSize(phnd,i);

		cdt->HiiHandle=phnd->Handle;
		cdt->PackageGuid=phnd->Guid;
		cdt->DataTableSize=(UINT32)(sz+sizeof(EFI_HII_DATA_TABLE));

		ExportHandleData(phnd, i, cdt,  &bp);
//debug+ just to test how bp gets incremented
		ASSERT((UINTN)cdt+sizeof(EFI_HII_DATA_TABLE)+sz==(UINTN)bp);
//debug-
		expt->NumberOfHiiDataTables++;
	}
		
	return EFI_SUCCESS;
}

EFI_STATUS GetVarStoreData(HII_FORMSET *FormSet, HII_STORE *VarData, UINT8* Buffer, UINTN DefaultMask){
	HII_LABEL			*lbl;
	HII_OBJECT			*obj;
    HII_FORM            *frm;
	EFI_IFR_OP_HEADER	*op;
	UINTN				i,x,j,k,offs,len;
//------------------
	
	for(i=0; i<FormSet->Forms.ItemCount; i++){
        frm=FormSet->Forms.Items[i];
        for(x=0; x< frm->Labels.ItemCount; x++){
    		//iterate trough the Labels to get each HII_OBJECT 
		    lbl=frm->Labels.Items[x];	
    		for(j=0; j<lbl->Objects.ItemCount; j++){
			    //iterate trough the Objects to see if 
			    obj=lbl->Objects.Items[j];
			
    			if( VarData->VarStore->VarId!=obj->Store1->VarStore->VarId ||
			      MemCmp(&VarData->VarStore->Guid, &obj->Store1->VarStore->Guid, sizeof(EFI_GUID))
				) continue;
			
    			//we focused at the Object which has the same VarStore Properies as passed "VarData"
			    //So parse the object to see if it has any default flags set 
    			op=obj->ObjectCode;
    			switch(op->OpCode){
			
			    	//ONE_OF and ORDERED_LIST has almost the same encoding 
    				case EFI_IFR_ORDERED_LIST_OP:
				    case EFI_IFR_ONE_OF_OP:
    					{
				    		EFI_IFR_ONE_OF_OPTION *opt;
    					//-----------------------
				    		offs=((EFI_IFR_ONE_OF*)op)->QuestionId;
    						len=((EFI_IFR_ONE_OF*)op)->Width;

//DEBUG CODE to check if update runs out of bound
						if(offs>=VarData->VarStore->Size) {
							ASSERT_EFI_ERROR(EFI_BUFFER_TOO_SMALL);
							return EFI_BUFFER_TOO_SMALL;
						}
//DEBUG CODE to check if update runs out of bound
					
    						for(k=0; k<obj->Options.ItemCount; k++){
				    			opt=(EFI_IFR_ONE_OF_OPTION*)obj->Options.Items[k];
    							//for ORDERED_LIST it would not be any Defaults 
				    			if(op->OpCode==EFI_IFR_ORDERED_LIST_OP){
								    pBS->CopyMem(&Buffer[offs],&opt->Value, 1);
    								offs++;									

//DEBUG CODE to check if update runs out of bound
    								if(offs>=VarData->VarStore->Size) return EFI_BUFFER_TOO_SMALL;
//DEBUG CODE to check if update runs out of bound

    							} else {
    								UINTN f=(UINTN)opt->Flags;
				    			//------------------	
								    if (f & DefaultMask) {
    									pBS->CopyMem(&Buffer[offs],&opt->Value,len);
				    					break;
								    } 
    							} 
				    		}
					    }break;
				
    				case EFI_IFR_CHECKBOX_OP:
				    	{
    						EFI_IFR_CHECK_BOX *cb=(EFI_IFR_CHECK_BOX*)obj->ObjectCode;
				    		UINTN f=(UINTN)cb->Flags;
					    //--------------------
    						offs=cb->QuestionId;

//DEBUG CODE to check if update runs out of bound
    						if(offs>=VarData->VarStore->Size) return EFI_BUFFER_TOO_SMALL;
//DEBUG CODE to check if update runs out of bound

    						if(f & DefaultMask) Buffer[offs] = 1;
				    		else Buffer[offs] = 0;
										
    					} break;
					
    				case EFI_IFR_NUMERIC_OP:
				    	{
    						EFI_IFR_NUMERIC *num =(EFI_IFR_NUMERIC*)obj->ObjectCode;
				    	//--------------------------
    						offs=num->QuestionId;
				    		len=num->Width;

//DEBUG CODE to check if update runs out of bound
    						if(offs>=VarData->VarStore->Size) return EFI_BUFFER_TOO_SMALL;
//DEBUG CODE to check if update runs out of bound

    						pBS->CopyMem(&Buffer[offs],&num->Default,len);
				    	} break;
			
			    } //switch(obj->ObjectCode->Opcode)
			
    		} //for(j) loop
        }//for(x) loop
	} //for(i) loop
	return EFI_SUCCESS;
}



EFI_STATUS HiiGetDefaultImageNew(IN EFI_HII_PROTOCOL		*This,
								 IN EFI_HII_HANDLE			Handle,
								 IN UINTN					DefaultMask,
								OUT EFI_HII_VARIABLE_PACK_LIST **VariablePackList)
{
	EFI_STATUS					Status=EFI_NOT_FOUND;
	UINTN						i, j, k, hidx, vnl=0, tbsz=0;
	HII_HANDLE					*phnd;
	EFI_HII_VARIABLE_PACK_LIST	*vpl, *fvpl=NULL, *pvpl=NULL;
	HII_FORMSET					*fs;
	HII_STORE					*st;
	UINT8						*pdb, *pnb;
//----------------------------------
	
	//Check Parameters	
	if(This==NULL || VariablePackList==NULL || !Handle ) return EFI_INVALID_PARAMETER;

	//Find the handle
	phnd=LocateHandle(Handle, &hidx);
	if(phnd==NULL) return EFI_NOT_FOUND;

	//If no VarStores associated with passed Handle will return NULL
	*VariablePackList=NULL;	

	//If Handle Exists then find a FormSet associated with this Handle
	fs=LocateFormSet(hidx, NULL);
	//No formset found => no IFR data
	if(fs==NULL) return EFI_NOT_FOUND;

	//Collect the Variable data	
	for(i=0, hidx=0; i<fs->Storages.ItemCount; i++){
		st=fs->Storages.Items[i];
		if(!st->VarStore->Size) continue;
		
		Status=EFI_SUCCESS;
		hidx++;
		//get the variable name length in bytes(ASCII)
		vnl=st->VarStore->Header.Length-sizeof(EFI_IFR_VARSTORE);
		//determine how much memory we need to accomodate 
		//EFI_VARIABLE_PACK_LIST + EFI_VARIABLE_PACK + VarName + VarData
		vpl=MallocZ(sizeof(EFI_HII_VARIABLE_PACK_LIST)+ //EFI_VARIABLE_PACK_LIST
					sizeof(EFI_HII_VARIABLE_PACK)+		//EFI_VARIABLE_PACK
					(vnl*sizeof(UINT16))+ 				//EFI_VARIABLE_PACK has VarName in UINT16
					st->VarStore->Size);				//Size of actual NV Var Data	

		tbsz+=(sizeof(EFI_HII_VARIABLE_PACK_LIST)+		//EFI_VARIABLE_PACK_LIST
					sizeof(EFI_HII_VARIABLE_PACK)+		//EFI_VARIABLE_PACK
					(vnl*sizeof(UINT16))+ 				//EFI_VARIABLE_PACK has VarName in UINT16
					st->VarStore->Size);				//Size of actual NV Var Data	

		if(!vpl) return EFI_OUT_OF_RESOURCES;

		//determine where in VPL buffer Actual VAriable data suppose to start
		pdb=(UINT8*)vpl+sizeof(EFI_HII_VARIABLE_PACK_LIST)+sizeof(EFI_HII_VARIABLE_PACK)+(vnl*sizeof(UINT16));
		
		GetVarStoreData(fs,st,pdb,DefaultMask);
		
		//if this is the first structure in the list remember it we must return it's address 
		if(fvpl==NULL)fvpl=vpl;
		//if PreviouseVariablePackList ptr was initialized update pointer to next VPL with current VPL
		if(pvpl!=NULL)pvpl->NextVariablePack=vpl;
		//reinitialize PreviouseVariablePackList ptr with current VPL pointer
		pvpl=vpl;

		//Update context of the VariablePack pointer to the next data byte afer itself(per .92 spec)
		vpl->VariablePack = (EFI_HII_VARIABLE_PACK*)(vpl+1);

		//Now fill out fields of HII_VARIABLE_PACK structure
		vpl->VariablePack->Header.Type=EFI_HII_VARIABLE;
		vpl->VariablePack->Header.Length=(UINT32)(sizeof(EFI_HII_VARIABLE_PACK)+(vnl*sizeof(UINT16))+st->VarStore->Size);
		vpl->VariablePack->VariableId=st->VarStore->VarId;
		vpl->VariablePack->VariableNameLength=(UINT32)(vnl*sizeof(UINT16)); // per spec length in bytes.
		pBS->CopyMem(&vpl->VariablePack->VariableGuid, &st->VarStore->Guid, sizeof(EFI_GUID));
		//now copy Variable name "pdb" points at the befinning of VarName buffer 
		pdb=(UINT8*)vpl+sizeof(EFI_HII_VARIABLE_PACK_LIST)+sizeof(EFI_HII_VARIABLE_PACK);
		pnb=(UINT8*)(st->VarStore+1);
		//since in VARIABLE_PACKAGE VAR NAME is UNICODE simple MemCpy wouldn't do.
		for(j=0,k=0; j<vnl; j++, k+=2) {
			pdb[k]=pnb[j];
		}
	}
	
	//Per Spec we have to dump all data in single buffer.
	//Update the IN/OUT parameter
	if(tbsz) {
		*VariablePackList=Malloc(tbsz);
		if(*VariablePackList==NULL) return EFI_OUT_OF_RESOURCES;
	} else *VariablePackList=NULL;
	vpl=fvpl;
	pdb=(UINT8*)(*VariablePackList);
	while (vpl){
		//copy Var_PACK_LIST + VARPACK to the Common Buffer
		pBS->CopyMem(pdb,vpl,sizeof(EFI_HII_VARIABLE_PACK_LIST)+vpl->VariablePack->Header.Length);
		pvpl=(EFI_HII_VARIABLE_PACK_LIST*)pdb;
		pvpl->VariablePack=(EFI_HII_VARIABLE_PACK*)(pdb+sizeof(EFI_HII_VARIABLE_PACK_LIST));

		pdb+=(sizeof(EFI_HII_VARIABLE_PACK_LIST)+vpl->VariablePack->Header.Length);
		if(vpl->NextVariablePack!=NULL)pvpl->NextVariablePack=(EFI_HII_VARIABLE_PACK_LIST*)pdb;

		pvpl=vpl;
		vpl=vpl->NextVariablePack;
		
		pBS->FreePool(pvpl);
	}
		
	return Status;
}


///////////////////////////////////////////////////////////////////
//Keyboard
EFI_STATUS HiiGetKeyboardLayout(IN EFI_HII_PROTOCOL		*This,
								OUT UINT16				*DescriptorCount,
								OUT EFI_KEY_DESCRIPTOR	*Descriptor)
{
	HII_KB_LAYOUT_DATA	*kbld;
//-------------------------------
	if(!This) return EFI_INVALID_PARAMETER;

	if(!gHiiDb.KeybdDb.KblCount) return EFI_NOT_FOUND;

	kbld=gHiiDb.KeybdDb.KbLayout[gHiiDb.KeybdDb.ActiveLayout];
	
	//we got small buffer
	if(*DescriptorCount<kbld->KeyDscCount) {
		*DescriptorCount=kbld->KeyDscCount;		
		return EFI_BUFFER_TOO_SMALL;
	}
	
	MemCpy((VOID*)Descriptor, (VOID*)kbld->KeyDsc, sizeof(EFI_KEY_DESCRIPTOR)*kbld->KeyDscCount);	

	return EFI_SUCCESS;
}


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//EXTENDED HII INTERFACE 
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
EFI_STATUS HiiExtGetStringInfo(
							IN	EXT_HII_PROTOCOL	*This,
							IN	EFI_HII_HANDLE		Handle,
							IN	STRING_REF			Token,
							IN	CHAR16				*Lang,
							OUT EXT_STR_INFO		**ExtStrInfo)
{
	HII_STR		*str;
//-----------------------------------------
	if(!This || !ExtStrInfo ) return EFI_INVALID_PARAMETER;

	str=GetHiiString(Handle,Lang,Token);
	if(!str) return EFI_NOT_FOUND;

	if(str->NewStr.StrPtr)*ExtStrInfo=&str->NewStr;
	else *ExtStrInfo=&str->String;

	return EFI_SUCCESS;
}


EFI_STATUS HiiExtGetFormInfo(IN  EXT_HII_PROTOCOL	*This,
							 IN  EFI_HII_HANDLE		Handle,
							 IN  EFI_FORM_ID		FormId,
							 OUT HII_FORM			**ExtFormInfo)
{
	HII_FORM		*frm;
	UINTN			hi;
	HII_HANDLE		*phnd;
	HII_FORMSET		*fs;
//-----------------------------------------
	if(!This || !ExtFormInfo ) return EFI_INVALID_PARAMETER;

	phnd=LocateHandle(Handle,&hi);
	ASSERT(phnd);
	if(!phnd)return EFI_NOT_FOUND;

	fs=LocateFormSet(hi,NULL);
	ASSERT(fs);
	if(!fs)return EFI_NOT_FOUND;

	frm=LocateForm(fs,FormId);
	ASSERT(frm);
	if(!frm)return EFI_NOT_FOUND;
	*ExtFormInfo=frm;

	return EFI_SUCCESS;
}


EFI_STATUS HiiExtRegisterLabelUpdateCallback(IN  EXT_HII_PROTOCOL		*This,
									   IN  EFI_HII_HANDLE				Handle,
									   IN  EFI_FORM_LABEL				LabelId,
									   IN  HII_LABEL_UPDATE_CALLBACK	CallBack)
{
	UINTN		hi;
	HII_LABEL	*lbl;
	HII_FORMSET *fs;
//----------------------------------
	if(!This || !CallBack ) return EFI_INVALID_PARAMETER;

	if(!LocateHandle(Handle, &hi)) return EFI_INVALID_PARAMETER;
	
	//Find Formset
	fs=LocateFormSet(hi, NULL);
	if(!fs) return EFI_INVALID_PARAMETER;

	lbl=LocateFormSetLabel(fs,LabelId,NULL);
	if(!lbl) return EFI_NOT_FOUND;

	lbl->UpdateCallBack=CallBack;

	return EFI_SUCCESS;
}

EFI_STATUS HiiExtUnregisterLabelUpdateCallback(IN  EXT_HII_PROTOCOL		*This,
									   IN  EFI_HII_HANDLE				Handle,
									   IN  EFI_FORM_LABEL				LabelId)
{
	UINTN		hi;
	HII_LABEL	*lbl;
	HII_FORMSET *fs;
//----------------------------------
	if(!This) return EFI_INVALID_PARAMETER;

	if(!LocateHandle(Handle, &hi)) return EFI_INVALID_PARAMETER;
	
	//Find Formset
	fs=LocateFormSet(hi, NULL);
	if(!fs) return EFI_INVALID_PARAMETER;

	lbl=LocateFormSetLabel(fs,LabelId,NULL);
	if(!lbl) return EFI_NOT_FOUND;

	lbl->UpdateCallBack=NULL;

	return EFI_SUCCESS;
}

EFI_STATUS	HiiExtGetFormSetInfo(IN  EXT_HII_PROTOCOL	*This,
								IN  UINT16				ClassMask,		//if ==0xFFFF all Classes
								IN	UINT16				SubClass,		//if ==0xFFFF all SubClasses
								OUT T_ITEM_LIST		*FormSetList)
{
	UINTN			i;
	HII_FORMSET		*fs;
//------------------------------
	if(!This || !FormSetList) return EFI_INVALID_PARAMETER;
	
	//Make sure the list is empty
	ClearItemLst(FormSetList,FALSE);
	for(i=0; i<gHiiDb.IfrDb.ItemCount;i++){
		fs=gHiiDb.IfrDb.Items[i];
		if(SubClass!=0xFFFF && SubClass!=fs->FormSetData->SubClass) continue;
		if((fs->FormSetData->Class & ClassMask)==fs->FormSetData->Class) AppendItemLst(FormSetList,fs);
	}
	return EFI_SUCCESS;
}

EFI_STATUS HiiExtGetFormLabels(IN  EXT_HII_PROTOCOL		*This,
							   IN  EFI_HII_HANDLE		Handle,
							   IN  EFI_FORM_ID			FormID,
							   OUT T_ITEM_LIST			*LabelList)
{
	UINTN			i;
	HII_FORMSET		*fs;
	HII_FORM		*frm;
	HII_LABEL		*lbl;
	EFI_STATUS		Status;
//-----------------------------------------
	if(!This || !LabelList ) return EFI_INVALID_PARAMETER;

	if(!LocateHandle(Handle,&i)) return EFI_INVALID_PARAMETER;;

	fs=LocateFormSet(i,NULL);
	ASSERT(fs);
	if(!fs)return EFI_NOT_FOUND;

	frm=LocateForm(fs,FormID);
	ASSERT(frm);
	if(!frm)return EFI_NOT_FOUND;

	ClearItemLst(LabelList,FALSE);
	for(i=1;i<frm->Labels.ItemCount;i++){
		lbl=frm->Labels.Items[i];
		Status=AppendItemLst(LabelList,lbl);
		ASSERT(!EFI_ERROR(Status));
		if(EFI_ERROR(Status))return Status;
	}
	return EFI_SUCCESS;
}


///////////////////////////////////////////////////////////////////
//Init All Private Data
EFI_STATUS InitPrivateData(EFI_HANDLE MyImgHandle){
	EFI_STATUS			Status=0;
	//HII_LANG			*al;//any lang
//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST
	EFI_HII_PACK_LIST	*pl=Malloc(sizeof(EFI_HII_PACK_LIST));
	// {BB2F3C9D-C7A1-4283-8AE2-4F4362990E2E}
	static EFI_GUID		pguid={ 0xbb2f3c9d, 0xc7a1, 0x4283, 0x8a, 0xe2, 0x4f, 0x43, 0x62, 0x99, 0x0e, 0x2e };
	EFI_HII_HANDLE		fph;//,sth;
//	UINT16				bl;
//	UINT8				*fsb;
//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST
//-----------------------------
	pBS->SetMem(&gHiiDb, sizeof(HII_DB),0 );
	
	gHiiDb.ImageHandle=MyImgHandle;
	gHiiDb.NextHiiHandle=1; //Zero Handle is Invalid it must be 1 based

	//Create Any Langguage Entry - empty string L"   " 
	Status=LocateLangAdd(L"   ",L"Any Language",NULL);
	if(EFI_ERROR(Status)) return Status;

	//Create and Initialize String Database
	gHiiDb.StringDb.KeyCount=STR_KEY_CNT;
	gHiiDb.StringDb.InitialCount=STRING_DB_MAX_COUNT;
	gHiiDb.StringDb.KeyField=&gStrKey;
	gHiiDb.StringDb.MemoryType=EfiBootServicesData;
	gHiiDb.StringDb.RecordCount=0;
	gHiiDb.StringDb.IndexArray=MallocZ(sizeof(VOID*)*gHiiDb.StringDb.InitialCount*gHiiDb.StringDb.KeyCount);
	if(!gHiiDb.StringDb.IndexArray) return EFI_OUT_OF_RESOURCES;

	//Init Protocol Instance here
	gHiiDb.HiiProtocolOld.NewPack=HiiNewPackOld;
	gHiiDb.HiiProtocolNew.NewPack=HiiNewPackNew;

	gHiiDb.HiiProtocolOld.RemovePack=HiiRemovePack;
	gHiiDb.HiiProtocolNew.RemovePack=HiiRemovePack;

	gHiiDb.HiiProtocolOld.FindHandles=HiiFindHandles;
	gHiiDb.HiiProtocolNew.FindHandles=HiiFindHandles;

	gHiiDb.HiiProtocolOld.GetDefaultImage=HiiGetDefaultImageOld;
	gHiiDb.HiiProtocolNew.GetDefaultImage=HiiGetDefaultImageNew;

	gHiiDb.HiiProtocolOld.TestString=HiiTestString;
	gHiiDb.HiiProtocolNew.TestString=HiiTestString;

	gHiiDb.HiiProtocolOld.GetGlyph=HiiGetGlyph;
	gHiiDb.HiiProtocolNew.GetGlyph=HiiGetGlyph;

	gHiiDb.HiiProtocolOld.GlyphToBlt=HiiGlyphToBlt;
	gHiiDb.HiiProtocolNew.GlyphToBlt=HiiGlyphToBlt;

	gHiiDb.HiiProtocolOld.NewString=HiiNewString;
	gHiiDb.HiiProtocolNew.NewString=HiiNewString;

	gHiiDb.HiiProtocolOld.GetPrimaryLanguages=HiiGetPrimaryLanguage;
	gHiiDb.HiiProtocolNew.GetPrimaryLanguages=HiiGetPrimaryLanguage;

	gHiiDb.HiiProtocolOld.GetSecondaryLanguages=HiiGetSecondaryLanguages;
	gHiiDb.HiiProtocolNew.GetSecondaryLanguages=HiiGetSecondaryLanguages;

	gHiiDb.HiiProtocolOld.GetString=HiiGetStringOld;
	gHiiDb.HiiProtocolNew.GetString=HiiGetStringNew;

	gHiiDb.HiiProtocolOld.GetLine=HiiGetLine;
	gHiiDb.HiiProtocolNew.GetLine=HiiGetLine;

	gHiiDb.HiiProtocolOld.GetForms=HiiGetFormsOld;
	gHiiDb.HiiProtocolNew.GetForms=HiiGetFormsNew;

	gHiiDb.HiiProtocolOld.UpdateForm=HiiUpdateForm;
	gHiiDb.HiiProtocolNew.UpdateForm=HiiUpdateForm;

	gHiiDb.HiiProtocolOld.GetKeyboardLayout=HiiGetKeyboardLayout;
	gHiiDb.HiiProtocolNew.GetKeyboardLayout=HiiGetKeyboardLayout;

	gHiiDb.HiiProtocolOld.ResetStrings=HiiResetStrings; 
	gHiiDb.HiiProtocolNew.ResetStrings=HiiResetStrings; 

	gHiiDb.HiiProtocolOld.ExportDatabase=HiiExport;
	gHiiDb.HiiProtocolNew.ExportDatabase=HiiExport;

	//Now do Extended Hii Interface
	gHiiDb.HiiExtProtocol.ExtGetStringInfo=HiiExtGetStringInfo;
	gHiiDb.HiiExtProtocol.ExtGetFormInfo=HiiExtGetFormInfo;
	gHiiDb.HiiExtProtocol.ExtGetFormsetInfo=HiiExtGetFormSetInfo;
	gHiiDb.HiiExtProtocol.ExtRegLblCallBack=HiiExtRegisterLabelUpdateCallback;
	gHiiDb.HiiExtProtocol.ExtUnregLblCallBack=HiiExtUnregisterLabelUpdateCallback;
	gHiiDb.HiiExtProtocol.ExtGetFormLabels=HiiExtGetFormLabels;		
	
	//------------------------------------
	//init Font Data 
	pl->FontPack=(EFI_HII_FONT_PACK*)&UsStdNarrowGlyphData;
	pl->GuidId=&pguid;
	pl->IfrPack=NULL;//(EFI_HII_IFR_PACK*)&FrontPageVfrBin;
	pl->KeyboardPack=NULL;
	pl->StringPack=NULL;//(EFI_HII_STRING_PACK*)&FrontPageStrings;
///// Load Font Pack
{
#define FONT_FFS_FILE_GUID { 0xdac2b117, 0xb5fb, 0x4964, { 0xa3, 0x12, 0xd, 0xcc, 0x77, 0x6, 0x1b, 0x9b } }
static EFI_GUID guidFontFile = FONT_FFS_FILE_GUID;
	EFI_STATUS Status;
	EFI_FIRMWARE_VOLUME_PROTOCOL *pFV;
	UINTN DataSize;
	EFI_GUID *pSectionGuid = NULL;
	UINT32 Authentication;
	EFI_HANDLE *pHandle;
	UINTN Number,i;
	Status = pBS->LocateHandleBuffer(ByProtocol,&guidFV, NULL, &Number, &pHandle);
	for(i=0;i<Number; i++)
	{
		Status=pBS->HandleProtocol(pHandle[i], &guidFV, &pFV);
		if (EFI_ERROR(Status)) continue;
		pSectionGuid=NULL;
		DataSize=0;
		Status=pFV->ReadSection (
			pFV,&guidFontFile,
			EFI_SECTION_FREEFORM_SUBTYPE_GUID,0, &pSectionGuid, &DataSize,
			&Authentication
	 	);
		if (!EFI_ERROR(Status))
		{
			pl->FontPack=(EFI_HII_FONT_PACK*)((UINT32*)(pSectionGuid+1)+1);
			break;
		}
	}
	Status=HiiNewPackOld((EFI_HII_PROTOCOL*)&gHiiDb.HiiProtocolOld,pl,&fph);
	pBS->FreePool(pSectionGuid);
}
////////////////////
	if(EFI_ERROR(Status)) return Status;

//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST
/*	pl->FontPack=NULL;
	pl->IfrPack=NULL;	
	pl->KeyboardPack=NULL;
	pl->StringPack=(EFI_HII_STRING_PACK*)&SetupStrings;

	Status=HiiNewPack(&gHiiDb.HiiProtocol,pl,&sth);
	if(EFI_ERROR(Status)) return Status;

	fsb=NULL;
	bl=0;
	Status=HiiGetForms(&gHiiDb.HiiProtocol,fph,0,&bl,fsb);
	if(Status==EFI_BUFFER_TOO_SMALL);
	else if(EFI_ERROR(Status)) return Status;
	fsb=Malloc(bl);
	if(!fsb) return EFI_OUT_OF_RESOURCES;
	Status=HiiGetForms(&gHiiDb.HiiProtocol,fph,0,&bl,fsb);

	//Now, I'll try to ges some strings from FrontPageDb
	{
		UINT16				mystr[81];
		EFI_STRING			plan, slan;
		EFI_IFR_FORM_SET	*fs=(EFI_IFR_FORM_SET*)fsb;
		UINT32				gb,i=0;
	//-----------------------------------
		
		Status=HiiGetPrimaryLanguage(&gHiiDb.HiiProtocol,fph,&plan);
		//it might be more than 1 Sec Lang.
		Status=HiiGetSecondaryLanguages(&gHiiDb.HiiProtocol,fph,plan,&slan);
		

		bl=80;

		Status=HiiGetString(&gHiiDb.HiiProtocol,fph,fs->FormSetTitle,TRUE,slan,&bl,&mystr[0]);

		Status=HiiGetString(&gHiiDb.HiiProtocol,fph,fs->FormSetTitle,TRUE,plan,&bl,&mystr[0]);

		Status=HiiGetString(&gHiiDb.HiiProtocol,fph,fs->Help,TRUE,slan,&bl,&mystr[0]);

		Status=HiiGetString(&gHiiDb.HiiProtocol,fph,fs->Help,TRUE,plan,&bl,&mystr[0]);

		Status=HiiTestString(&gHiiDb.HiiProtocol,&mystr[0],&i,&gb);

	}

*/
//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST//TEST
	pBS->FreePool(pl);
//
//	return 	pBS->InstallMultipleProtocolInterfaces(	gHiiDb.ImageHandle,
//				&gEfiHiiProtocolGuid,&gHiiDb.HiiProtocol,NULL,NULL);
	return	EFI_SUCCESS;
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2005, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**             6145-F Northbelt Pkwy, Norcross, GA 30071            **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************