summaryrefslogtreecommitdiff
path: root/Core/CORE_DXE/GC.c
blob: c045bda4f467b63d7a3deea784ad5a4715ed655f (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2012, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/Core/CORE_DXE/GC.c 1     11/21/12 4:07a Wesleychen $
//
// $Revision: 1 $
//
// $Date: 11/21/12 4:07a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/Core/CORE_DXE/GC.c $
// 
// 1     11/21/12 4:07a Wesleychen
// Update rev.45 for EIP105534.
// 
// 45    11/14/12 5:57p Artems
// [TAG]  		EIP105534
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	system hangs during SCT execution
// [RootCause]  	Incorrect handling of FORM_FEED escape character in
// StringToImage function
// [Solution]  	Graphics console - added flag to ignore line breaks
// HiiFont - fixed multiline drawing logic
// [Files]  		Gc.c HiiFont.c HiiFontEx.c
// 
// 44    7/06/12 11:19a Artems
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Removed unnecessary traces
// [Files]  		Gc.c
// 
// 43    7/06/12 11:11a Artems
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Support for monitor native resolution
// [Files]  		GC.c, ConSplit.c, Out.c, Core_Dxe.sdl
// 
// 42    6/19/12 3:43p Artems
// [TAG]  		EIP N/A
// [Category]  	Bug Fix
// [Severity]  	Critical
// [Symptom]  	GC.c file gives build error
// [RootCause]  	incorrect pointer used
// [Solution]  	changed pointer
// [Files]  		gc.c
// 
// 41    6/19/12 10:36a Artems
// [TAG]  		EIP89430
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	Cursor position wasn't 0:0 after call to SetMode
// [RootCause]  	When console SetMode involved changing of screen
// resolution
// screen was cleared by graphics driver, however cursol position wasn't
// reset
// [Solution]  	Added code to resent cursor position when changing screen
// resolution
// [Files]  		Gc.c
// 
// 40    5/21/12 10:46a Artems
// [TAG]  		EIP N/A
// [Category]  	New Feature
// [Description]  	Added text mode for full screen in native resolution
// [Files]  		GC.c
// 
// 39    4/30/12 3:55p Artems
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Modified GraphicsConsole driver to output whole string
// instead of symbol-by-symbol output
// [Files]  		Gc.c, AmiDxeLib.h, EfiLib.c, UefiHiiUtils.c
// 
// 38    8/12/11 12:22p Artems
// EIP 64107: Added changes for module to be compliant with UEFI
// specification v 2.3.1
// 
// 37    8/12/11 11:54a Artems
// Bug fix: avoid using uninitialized data fields of GC_DATA structure
// 
// 36    4/12/10 3:25p Artems
// Fixed bug of drawing cursor when window position is undefined
// 
// 35    12/17/09 10:22a Artems
// EIP 29311 added automatic discovering of graphics mode for text mode 0
// 
// 34    12/03/09 3:13p Felixp
// TestString is implemented for UEFI 2.1 case.
// 
// 33    10/09/09 6:06p Felixp
// UEFI 2.1 - related changes (suppot Framework and UEFI HII).
// 
// 32    8/28/09 10:25a Felixp
// Component Name protocol implementation is upadted  to support both 
// ComponentName and ComponentName2 protocols
// (based on value of the EFI_SPECIFICATION_VERSION SDL token).
// 
// 31    7/10/09 6:26p Felixp
// 
// 30    7/08/09 5:01p Artems
// Added missing function headers
// 
// 29    5/22/09 7:27p Felixp
// 
// 27    10/19/07 9:19a Felixp
//  - Graphic Console driver is updated to support different graphical
// resolutions (used to be only 800 by 600).
//  - SDL tokens are added in CORE_DXE.sdl to customize list of text
// modes.
//  - SDL tokens are added in CORE_DXE.sdl to customize color values(EIP:
// 9854).
// 
// 26    4/13/07 2:45p Robert
// Updates for Coding Standard
// 
// 25    3/30/07 6:13p Felixp
// 
// 24    3/29/07 10:22a Felixp
// 
// 23    3/28/07 5:39p Artems
// Fixed errors reported by SCT test
// 
// 22    3/13/07 1:48p Artems
// Fixed bug in 100 x 31 mode
// 
// 21    3/13/07 10:42a Artems
// 
// 20    3/09/07 1:40p Artems
// Clear screen function changed to clear only affected part of screen,
// not 
// entire screen
// 
// 19    1/04/07 6:19p Artems
// 
// 18    12/20/06 10:38a Felixp
// Graphics Output Protocol support added
// 
// 17    8/24/06 10:11a Felixp
// x64 support: warning/error fixes
// 
// 16    8/04/06 4:07p Robert
// 
// 15    3/13/06 10:07a Felixp
// 
// 14    9/29/05 2:52p Robert
// Initialization of Global Variables
// 
// 13    7/22/05 11:29a Felixp
// enable cursor in reset function
// 
// 12    7/20/05 6:50p Felixp
// 
// 11    7/20/05 6:24p Robert
// Added check for trying to print a wide character at position 79.  This
// should produce a line feed instead
// 
// 10    7/20/05 5:31p Robert
// Data structure for temporary storage of cursor position was only big
// enough for narrow glyph not for wide glyph
// 
// 7     3/03/05 12:16p Felixp
// HII New protocol is default now
// 
// 6     2/25/05 11:27a Robert
// Cleared current p[osition buffer before clearing the screen and moving
// the cursor
// 
// 5     2/25/05 10:52a Robert
// Clear screen procedure would leave a block on the screen where the
// cursor was.
// We changed the order of the operation so that the cursor moved first
// and the screen was cleared afterward
// 
// 4     2/24/05 5:27p Felixp
// bug fix in DriverBindingStop (Cursor blinking timer was not getting
// stopped)
// 
// 3     2/07/05 4:04p Yakovlevs
// 
// 2     2/04/05 1:47p Felixp
// warning fixed
// 
// 1     1/28/05 12:45p Felixp
// 
// 3     1/06/05 11:22a Robert
// 
// 2     1/06/05 10:09a Robert
// Added Blinking Cursor support
// 
// 1     12/22/04 6:19p Admin
// 
// 1     12/22/04 6:18p Admin
// 
// 9     12/16/04 3:40p Felixp
// 
// 8     12/16/04 2:14p Robert
// Added TempBlt Buffer init for printing of default string.  was
// forgetting to re-init
// buffer pointer when doing a carriage return line feed
// 
// 7     12/16/04 11:39a Robert
// code optimization for color selection
// 
// 6     12/15/04 4:02p Robert
// added checks for memory allocation
// 
// 5     12/14/04 6:02p Robert
// added component name support
// 
// 4     12/14/04 5:28p Robert
// 
// 3     12/14/04 4:28p Robert
//
// 2     11/11/04 5:09p Robert
//
// 1     11/11/04 3:28p Robert
//
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:  GC.h
//
// Description:  Graphics console driver that produces the Simple Text Out
//		interface
//
//<AMI_FHDR_END>
//**********************************************************************

//-----------------------------------------------------------------------------

#include <AmiDxeLib.h>
#include <Protocol/UgaDraw.h>
#include <Protocol/GraphicsOutput.h>
#include <Protocol/HiiFont.h>
#include <Protocol/SimpleTextOut.h >
#include <Protocol/DevicePath.h>
#include <Protocol/DriverBinding.h>
#include <Protocol/ComponentName2.h>
#include <Protocol/EdidActive.h>
#include <Token.h>

//-----------------------------------------------------------------------------

#define	GLYPH_HEIGHT		19
#define	NARROW_GLYPH_WIDTH	8
#define WIDE_GLYPH_WIDTH	16

#define MODE_ZERO_MIN_HOR_RES NARROW_GLYPH_WIDTH * 80
#define MODE_ZERO_MIN_VER_RES GLYPH_HEIGHT * 25

#define CURSOR_THICKNESS	3
#define CURSOR_OFFSET   	15      //base line of simple narrow font

#define	NULL_CHAR			0x0000
#define	BACKSPACE			0x0008
#define	LINE_FEED			0x000A
#define	FORM_FEED			0x000C
#define	CARRIAGE_RETURN		0x000D

//-----------------------------------------------------------------------------
// Data Structures

#pragma pack(1)

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name: TEXT_MODE
//
// Description: 
// This structure represents text mode internal information structure
//
// Fields: Name          Type                    Description
//----------------------------------------------------------------------------
// ModeNum              INT32               Mode number
// Col                  INT32               Max number of columns
// Row                  INT32               Max number of rows
// VideoCol             UINT32              Horizontal screen resolution
// VideoRow             UINT32              Vertical screen resolution
// 
// Notes:  
//
// Referrals:
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _TEXT_MODE  {
	INT32	ModeNum;
	INT32	Col;
	INT32	Row;
	UINT32	VideoCol; // horizontal pixels
	UINT32	VideoRow; // vertical pixels
} TEXT_MODE;

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name: GC_TEXT_MODE
//
// Description: 
// This structure represents text mode extended internal information structure
//
// Fields: Name          Type                    Description
//----------------------------------------------------------------------------
// ModeNum              INT32               Mode number
// Col                  INT32               Max number of columns
// Row                  INT32               Max number of rows
// VideoCol             UINT32              Horizontal screen resolution
// VideoRow             UINT32              Vertical screen resolution
// Supported            BOOLEAN             Flag if this mode supported
// GraphicsMode         UINT32              Correspondent graphics mode
// 
// Notes:  
//
// Referrals:
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _GC_TEXT_MODE  {
	INT32	ModeNum;
	INT32	Col;
	INT32	Row;
	UINT32	VideoCol; // horizontal pixels
	UINT32	VideoRow; // vertical pixels
    BOOLEAN Supported;
    UINT32  GraphicsMode;
} GC_TEXT_MODE;

typedef struct _GC_DATA GC_DATA;

typedef VOID (* AGC_UPDATE_BLT_BUFFER ) (
	IN     GC_DATA 			             *This,
	IN     UINT32			             Width,
    IN     UINT32                        Height,
	IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer
	);

typedef VOID (* AGC_CLEAR_SCREEN) (
	IN OUT GC_DATA *This
	);

typedef VOID (* AGC_SCROLL_UP) (
	IN GC_DATA *This
	);

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name: GC_DATA
//
// Description: 
// This structure represents internal information structure for Graphics console
// driver
//
// Fields: Name          Type                               Description
//----------------------------------------------------------------------------
// SimpleTextOut        EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL     Protocol structure
// Signature            UINT32                              Unique signature
// GraphicsOutput       EFI_GRAPHICS_OUTPUT_PROTOCOL*       Pointer to Gop driver
// Hii                  EFI_HII_PROTOCOL*                   Pointer to HII driver
// SupportedModes       GC_TEXT_MODE*                       Pointer to supported modes array
// MaxRows              UINT32                              Max rows in current mode
// MaxColumns           UINT32                              Max columns in current mode
// DeltaX               UINT32                              Horizontal indent of text window on screen in pixels
// DeltaY               UINT32                              Vertical indent of text window on screen in  pixels
// Cursor               EFI_GRAPHICS_OUTPUT_BLT_PIXEL       Array for saving cursor image
// BlinkVisible         BOOLEAN                             Current state of cursor in blinking mode
// CursorEvent          EFI_EVENT                           Event generated to blink cursor
// OemUpdateBltBuffer   AGC_UPDATE_BLT_BUFFER               Custom porting hook
// OemClearScreen       AGC_CLEAR_SCREEN                    Custom porting hook
// OemScrollUp          AGC_SCROLL_UP                       Custom porting hook
// 
// Notes:  
//
// Referrals:
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

struct _GC_DATA{
	EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL	SimpleTextOut;
	UINT32				            Signature;			            //signature (must be 0x54445348 ('GRCS') )
    UINT32                          Version;
	EFI_GRAPHICS_OUTPUT_PROTOCOL	*GraphicsOutput;
	EFI_HII_FONT_PROTOCOL		    *HiiFont;
    GC_TEXT_MODE                    *SupportedModes;
    UINT32                          MaxRows;                        //max number of rows in current mode
    UINT32                          MaxColumns;                     //max number of columns in current mode
    UINT32                          DeltaX;                         //horizontal offset in pixels for current text mode
    UINT32                          DeltaY;                         //vertical offset in pixels for current text mode
    EFI_GRAPHICS_OUTPUT_BLT_PIXEL   Cursor[NARROW_GLYPH_WIDTH * 3]; //Save cursor image
	BOOLEAN				            BlinkVisible;                   //if true cursor is visible, otherwise - invisible
	EFI_EVENT			            CursorEvent;
	AGC_UPDATE_BLT_BUFFER		    OemUpdateBltBuffer;		        //pointer to custom hook
	AGC_CLEAR_SCREEN		        OemClearScreen;		            //pointer to custom hook
	AGC_SCROLL_UP			        OemScrollUp;			        //pointer to custom hook
};
#pragma pack()

//-----------------------------------------------------------------------------
// Function Prototypes

EFI_STATUS	DriverBindingSupported ( 
	IN EFI_DRIVER_BINDING_PROTOCOL    *This,
	IN EFI_HANDLE                     ControllerHandle,
	IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath);

EFI_STATUS	DriverBindingStart ( 
	IN EFI_DRIVER_BINDING_PROTOCOL    *This,
	IN EFI_HANDLE                     ControllerHandle,
	IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath);

EFI_STATUS	DriverBindingStop ( 
	IN EFI_DRIVER_BINDING_PROTOCOL	*This,
	IN EFI_HANDLE			        ControllerHandle,
	IN  UINTN			            NumberOfChildren,
	IN  EFI_HANDLE			        *ChildHandleBuffer);

//******************** Simple Text Output protocol functions prototypes ***********

EFI_STATUS GCReset(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, 
    IN BOOLEAN ExtendedVerification);

EFI_STATUS GCOutputString(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, 
    IN CHAR16 *String);

EFI_STATUS GCTestString(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, 
    IN CHAR16 *String);

EFI_STATUS GCQueryMode(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, 
    IN UINTN ModeNum, 
    OUT UINTN *Col, 
    OUT UINTN *Row);

EFI_STATUS GCSetMode(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, 
    IN UINTN ModeNum);

EFI_STATUS GCSetAttribute(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, 
    IN UINTN Attribute);

EFI_STATUS GCClearScreen(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This);

EFI_STATUS GCSetCursorPosition(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, 
    IN UINTN Column, 
    IN UINTN Row);

EFI_STATUS GCEnableCursor(
    IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, 
    IN BOOLEAN Visible);

//******************** Service functions prototypes ********************************

EFI_STATUS GetColorFromAttribute(
    IN UINT32 Attribute, 
    OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Foreground,
    OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Background);

EFI_STATUS GetGraphicsModeNumber (
    IN EFI_GRAPHICS_OUTPUT_PROTOCOL	*GraphicsOutput, 
    IN UINT32                       HorRes,
    IN UINT32                       VerRes,
	OUT UINT32                      *ModeNum,
    IN  BOOLEAN                     ExactMatch,
    OUT UINT32                      *ActualHorRes OPTIONAL,
    OUT UINT32                      *ActualVerRes OPTIONAL );

EFI_STATUS SetupGraphicsDevice(
    IN GC_DATA *GcData);

VOID EFIAPI BlinkCursorEvent ( IN EFI_EVENT Event, IN VOID *Context );

VOID DrawCursor(
    IN GC_DATA *GcData,
    IN BOOLEAN Visible);

VOID ScrollUp(
    IN GC_DATA *GcData);

VOID SaveCursorImage(
    IN GC_DATA *GcData);

VOID ShiftCursor(
    IN GC_DATA *GcData,
    IN UINT16 Step);

VOID AddChar(
    IN GC_DATA *GcData,
    IN CHAR16 Char,
    IN UINT16 Width
);

VOID FlushString(
    IN GC_DATA *GcData
);

//********************** Hooks prototypes ******************************************

VOID GcUpdateBltBuffer (
	IN     GC_DATA 			             *This,			    //pointer to internal structure
	IN     UINT32			             Width,	            //width of the buffer in pixels
    IN     UINT32                        Height,            //height of the buffer in pixels
	IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer	        //pointer to BLT buffer to update
	);

VOID GcInternalClearScreen (
	IN OUT GC_DATA *This
	);

//-----------------------------------------------------------------------------
// Globals
extern const TEXT_MODE TextModeArray[];
extern const MaxTextMode;
extern const EFI_GRAPHICS_OUTPUT_BLT_PIXEL ColorArray[];
static CHAR16 *TextBuffer = NULL;
static UINT32 TextBufferSize;
static UINT32 Position;
static UINT32 StringWidth;
static EFI_HANDLE GopHandle;

//-----------------------------------------------------------------------------
// Protocol implementation
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL	mGCProtocol	=
	{
	GCReset,
	GCOutputString,
	GCTestString,
	GCQueryMode,
	GCSetMode,
	GCSetAttribute,
	GCClearScreen,
	GCSetCursorPosition,
	GCEnableCursor,
	NULL
	};


//-----------------------------------------------------------------------------
// Driver Binding Protocol

EFI_DRIVER_BINDING_PROTOCOL gGraphicsConsoleDriverBindingProtocol = {
	DriverBindingSupported,
	DriverBindingStart,
	DriverBindingStop,
	0x10,
	NULL,
	NULL
	};


//-----------------------------------------------------------------------------
// Function Definitions
#ifdef EFI_DEBUG
#ifndef EFI_COMPONENT_NAME2_PROTOCOL_GUID //old Core
#ifndef LANGUAGE_CODE_ENGLISH
#define LANGUAGE_CODE_ENGLISH "eng"
#endif
static BOOLEAN LanguageCodesEqual(
    CONST CHAR8* LangCode1, CONST CHAR8* LangCode2
){
    return    LangCode1[0]==LangCode2[0] 
           && LangCode1[1]==LangCode2[1]
           && LangCode1[2]==LangCode2[2];
}
static EFI_GUID gEfiComponentName2ProtocolGuid = EFI_COMPONENT_NAME_PROTOCOL_GUID;
#endif
//Driver Name
static UINT16 *gDriverName=L"AMI Graphic Console Driver";

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Name:  GraphicsConsoleGetControllerName
//
// Description: 
//  EFI_COMPONENT_NAME_PROTOCOL GetControllerName function
//
// Input:       
//  IN EFI_COMPONENT_NAME_PROTOCOL* This - pointer to protocol instance
//  IN EFI_HANDLE Controller - controller handle
//  IN EFI_HANDLE ChildHandle - child handle
//  IN CHAR8* Language - pointer to language description
//  OUT CHAR16** ControllerName - pointer to store pointer to controller name
//
// Output:      
//      EFI_STATUS
//          EFI_SUCCESS - controller name returned
//          EFI_INVALID_PARAMETER - language undefined
//          EFI_UNSUPPORTED - given language not supported
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

static EFI_STATUS GraphicsConsoleGetControllerName (
		IN  EFI_COMPONENT_NAME2_PROTOCOL  *This,
		IN  EFI_HANDLE                   ControllerHandle,
 		IN  EFI_HANDLE                   ChildHandle        OPTIONAL,
  		IN  CHAR8                        *Language,
  		OUT CHAR16                       **ControllerName
)
{
	return EFI_UNSUPPORTED;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Name:   GraphicsConsoleGetDriverName
//
// Description: 
//  EFI_COMPONENT_NAME_PROTOCOL GetDriverName function
//
// Input:       
//  IN EFI_COMPONENT_NAME_PROTOCOL* This - pointer to protocol instance
//  IN CHAR8* Language - pointer to language description
//  OUT CHAR16** DriverName - pointer to store pointer to driver name
//
// Output:      
//  EFI_STATUS
//      EFI_SUCCESS - driver name returned
//      EFI_INVALID_PARAMETER - language undefined
//      EFI_UNSUPPORTED - given language not supported
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

static EFI_STATUS GraphicsConsoleGetDriverName(
    IN  EFI_COMPONENT_NAME2_PROTOCOL  *This,
	IN  CHAR8                        *Language,
	OUT CHAR16                       **DriverName
)
{
	//Supports only English
	if(!Language || !DriverName) 
        return EFI_INVALID_PARAMETER;

	if (!LanguageCodesEqual( Language, LANGUAGE_CODE_ENGLISH)) 
        return EFI_UNSUPPORTED;
	else 
        *DriverName=gDriverName;
	
	return EFI_SUCCESS;
}

//Component Name Protocol
static EFI_COMPONENT_NAME2_PROTOCOL gGraphicsConsole = {
  GraphicsConsoleGetDriverName,
  GraphicsConsoleGetControllerName,
  LANGUAGE_CODE_ENGLISH
};
#endif

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCEntryPoint
//
// Description:	
//  Installs gGraphicsConsoleDriverBindingProtocol protocol
//
// Input:
//	IN EFI_HANDLE ImageHandle - driver image handle
//	IN EFI_SYSTEM_TABLE *SystemTable - pointer to system table
//
// Output:
//	EFI_STATUS
//      EFI_SUCCESS - Driver binding protocol was installed
//
// Modified:
//
// Referrals: InitAmiLib InstallMultipleProtocolInterfaces
//
// Notes:
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS	GCEntryPoint (
	IN EFI_HANDLE           ImageHandle,
	IN EFI_SYSTEM_TABLE     *SystemTable
)
{
	EFI_STATUS	Status;

	InitAmiLib(ImageHandle, SystemTable);

	// initiaize the ImageHandle and DriverBindingHandle
	gGraphicsConsoleDriverBindingProtocol.DriverBindingHandle = NULL;
	gGraphicsConsoleDriverBindingProtocol.ImageHandle = ImageHandle;

	// Install driver binding protocol here
	Status = pBS->InstallMultipleProtocolInterfaces (
						&gGraphicsConsoleDriverBindingProtocol.DriverBindingHandle,
						&gEfiDriverBindingProtocolGuid, &gGraphicsConsoleDriverBindingProtocol,
#ifdef EFI_DEBUG
						&gEfiComponentName2ProtocolGuid, &gGraphicsConsole,
#endif
						NULL);

	return Status;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: DriverBindingSupported
//
// Description: 
//  Checks to see if this driver can be used
//
// Input:
//	IN EFI_DRIVER_BINDING_PROTOCOL *This - pointer to protocol instance
//	IN EFI_HANDLE Controller - handle of controller to install driver on
//	IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath - pointer to device path
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS - Driver supports the Device
//      EFI_NOT_SUPPORTED - Driver cannot support the Device 
//
// Notes:
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS	DriverBindingSupported (
	IN EFI_DRIVER_BINDING_PROTOCOL    *This,
	IN EFI_HANDLE                     ControllerHandle,
	IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
)
{
	EFI_STATUS                   Status;
	EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
	EFI_HII_FONT_PROTOCOL		 *HiiFont;

	Status = pBS->OpenProtocol(
                        ControllerHandle, 
                        &gEfiGraphicsOutputProtocolGuid,
    	                &GraphicsOutput, 
                        This->DriverBindingHandle,
		                ControllerHandle, 
                        EFI_OPEN_PROTOCOL_BY_DRIVER);
	if (EFI_ERROR(Status))
        return Status;
    else
		pBS->CloseProtocol( 
                        ControllerHandle, 
                        &gEfiGraphicsOutputProtocolGuid,
					    This->DriverBindingHandle, 
                        ControllerHandle);

	Status = pBS->OpenProtocol(
                        ControllerHandle, 
                        &gEfiDevicePathProtocolGuid,
    	                NULL, 
                        This->DriverBindingHandle,
		                ControllerHandle, 
                        EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
	if (EFI_ERROR(Status)) 
        return Status;

	Status = pBS->LocateProtocol ( &gEfiHiiFontProtocolGuid, NULL, &HiiFont);

	return Status;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: DriverBindingStart
//
// Description: 
//  This function grabs needed protocols and initializes the supported text modes
//
// Input:
//	IN EFI_DRIVER_BINDING_PROTOCOL *This - pointer to protocol instance
//	IN EFI_HANDLE Controller - handle of controller to install driver on
//	IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath - pointer to device path
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS - SimpleTextOut Protocol installed
//      EFI_NOT_SUPPORTED - SimpleTextOut Protocol not installed
//
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS	DriverBindingStart (
	IN EFI_DRIVER_BINDING_PROTOCOL *This,
	IN EFI_HANDLE                  ControllerHandle,
	IN EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath
)
{
	EFI_STATUS Status;
    INT32 StartMode;

#if (CURSOR_BLINK_ENABLE != 0)
	EFI_STATUS			EventStatus;
#endif
	GC_DATA				*GcData = NULL;

	// Create private data structure and fill with proper data
	Status = pBS->AllocatePool(EfiBootServicesData, sizeof(GC_DATA), &GcData);
	if (EFI_ERROR(Status))
        return Status;

	pBS->CopyMem( &(GcData->SimpleTextOut), 
                  &mGCProtocol, 
				  sizeof(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL) );

	Status = pBS->AllocatePool(EfiBootServicesData, 
                               sizeof(SIMPLE_TEXT_OUTPUT_MODE), 
                               &(GcData->SimpleTextOut.Mode));
    if(EFI_ERROR(Status))
    {
        pBS->FreePool(GcData);
        return Status;
    }

	Status = pBS->AllocatePool(EfiBootServicesData, 
                               sizeof(GC_TEXT_MODE) * MaxTextMode, 
                               &(GcData->SupportedModes));
    if(EFI_ERROR(Status))
    {
        pBS->FreePool(GcData->SimpleTextOut.Mode);
        pBS->FreePool(GcData);
        return Status;
    }

	Status = pBS->OpenProtocol( 
                            ControllerHandle, 
                            &gEfiGraphicsOutputProtocolGuid,
					        &(GcData->GraphicsOutput), 
                            This->DriverBindingHandle,
					        ControllerHandle, 
                            EFI_OPEN_PROTOCOL_BY_DRIVER );
	if (EFI_ERROR(Status))
    {
        pBS->FreePool(GcData->SupportedModes);
        pBS->FreePool(GcData->SimpleTextOut.Mode);
        pBS->FreePool(GcData);
		return Status;
    }

	// Find the Hii Protocol and attach it to the datastructure
	Status = pBS->LocateProtocol ( &gEfiHiiFontProtocolGuid, NULL, &(GcData->HiiFont));

	if (EFI_ERROR(Status))
	{
		pBS->CloseProtocol( 
                        ControllerHandle, 
                        &gEfiGraphicsOutputProtocolGuid,
					    This->DriverBindingHandle, 
                        ControllerHandle);
        pBS->FreePool(GcData->SupportedModes);
        pBS->FreePool(GcData->SimpleTextOut.Mode);
        pBS->FreePool(GcData);
		return EFI_UNSUPPORTED;
	}

    GopHandle = ControllerHandle;
    Status = SetupGraphicsDevice(GcData);
    if(EFI_ERROR(Status))
	{
		pBS->CloseProtocol( 
                        ControllerHandle, 
                        &gEfiGraphicsOutputProtocolGuid,
					    This->DriverBindingHandle, 
                        ControllerHandle);
        pBS->FreePool(GcData->SupportedModes);
        pBS->FreePool(GcData->SimpleTextOut.Mode);
        pBS->FreePool(GcData);
		return EFI_UNSUPPORTED;
	}	

    //initialize porting hooks and signature
    GcData->Signature = 0x54445348;
    GcData->Version = 1;
    GcData->OemUpdateBltBuffer = GcUpdateBltBuffer;
    GcData->OemClearScreen = GcInternalClearScreen;
    GcData->OemScrollUp = NULL;
    GcData->DeltaX = 0;
    GcData->DeltaY = 0;
    GcData->MaxColumns = 0;
    GcData->MaxRows = 0;

	// Default the cursor blink to the show cursor state
	GcData->BlinkVisible = TRUE;


   (GcData->SimpleTextOut.Mode)->CursorVisible = FALSE;    //since initial position of window is undefined we cannot draw cursor yet
    Status = GCSetAttribute(&(GcData->SimpleTextOut), EFI_BACKGROUND_BLACK | EFI_WHITE);  //set default attributes
    StartMode = ((GcData->SimpleTextOut.Mode)->MaxMode > 3 && START_IN_NATIVE_RESOLUTION) ? 3 : 0;
    Status = GCSetMode(&(GcData->SimpleTextOut), StartMode);
    Status = GCEnableCursor(&(GcData->SimpleTextOut), TRUE);    //enable cursor

	// install the simple text out protocol
	Status = pBS->InstallMultipleProtocolInterfaces ( 
                    &ControllerHandle,            
                    &gEfiSimpleTextOutProtocolGuid, 
                    &GcData->SimpleTextOut,
                    NULL);
	
	if (EFI_ERROR(Status))
	{
		// close protocols and free allocated memory
		pBS->CloseProtocol( 
                        ControllerHandle, 
                        &gEfiGraphicsOutputProtocolGuid,
					    This->DriverBindingHandle, 
                        ControllerHandle);

        pBS->FreePool(GcData->SupportedModes);
        pBS->FreePool(GcData->SimpleTextOut.Mode);
		pBS->FreePool(GcData);
        return EFI_UNSUPPORTED;
    }

#if (CURSOR_BLINK_ENABLE != 0)
	EventStatus = pBS->CreateEvent (
                    EVT_TIMER | EVT_NOTIFY_SIGNAL,
					TPL_NOTIFY,
					BlinkCursorEvent,
					&(GcData->SimpleTextOut),
					&(GcData->CursorEvent)
					);
	if (!EFI_ERROR(EventStatus))
	{
		pBS->SetTimer(GcData->CursorEvent, TimerPeriodic, 5000000);
	}
#endif
	
	return Status;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: DriverBindingStop
//
// Description: 
//  Uninstalls the driver from given ControllerHandle
//
// Input:
//  IN EFI_DRIVER_BINDING_PROTOCOL *This - pointer to protocol instance
//  IN EFI_HANDLE ControllerHandle - controller handle to uninstall driver from
//  IN UINTN NumberOfChildren - number of children supported by this driver
//  IN EFI_HANDLE *ChildHandleBuffer  - pointer to child handles buffer
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS	- driver uninstalled from controller
//      EFI_NOT_STARTED - driver was not started
//		
// Notes:
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS	DriverBindingStop ( 
	IN EFI_DRIVER_BINDING_PROTOCOL *This,
	IN EFI_HANDLE                  ControllerHandle,
	IN UINTN                       NumberOfChildren,
	IN EFI_HANDLE                  *ChildHandleBuffer
)
{
	EFI_STATUS						Status;
	GC_DATA							*GcData = NULL;
	EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL	*SimpleTextOut = NULL;

	Status = pBS->OpenProtocol (
                            ControllerHandle, 
                            &gEfiSimpleTextOutProtocolGuid, 
					        &SimpleTextOut, 
                            This->DriverBindingHandle, 
					        ControllerHandle, 
                            EFI_OPEN_PROTOCOL_GET_PROTOCOL);
	if (EFI_ERROR (Status))
		return EFI_NOT_STARTED;

	Status = pBS->CloseProtocol (
                            ControllerHandle, 
                            &gEfiSimpleTextOutProtocolGuid, 
					        This->DriverBindingHandle, 
                            ControllerHandle);

	GcData = (GC_DATA *) SimpleTextOut;	

	// uninstall the simple text out protocol
	Status = pBS->UninstallMultipleProtocolInterfaces ( 
                            ControllerHandle,            
                            &gEfiSimpleTextOutProtocolGuid, 
                            &GcData->SimpleTextOut,
                            NULL);
	if (EFI_ERROR (Status))
		return Status;

#if (CURSOR_BLINK_ENABLE != 0)
	pBS->SetTimer(GcData->CursorEvent, TimerCancel, 0);
	pBS->CloseEvent(GcData->CursorEvent);
#endif

	Status = pBS->CloseProtocol( 
                            ControllerHandle, 
                            &gEfiGraphicsOutputProtocolGuid,
						    This->DriverBindingHandle, 
                            ControllerHandle);

    pBS->FreePool(GcData->SupportedModes);
    pBS->FreePool(GcData->SimpleTextOut.Mode);
    pBS->FreePool(GcData);

	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCReset
//
// Description: 
//  Resets the text output device
//
// Input:
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//	IN BOOLEAN ExtendedVerification - indicates that the driver should preform more
//			exhausted verification of the device during reset
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS	- device reset properly
//	    EFI_DEVICE_ERROR - Device is not functioning properly
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS  GCReset(
	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
	IN BOOLEAN                         ExtendedVerification
)
{
	// Set mode clears the screen and sets the cursor back to (0,0) So before 
	//	we do that, set the Attribute colors to default
	This->SetAttribute(This, EFI_BACKGROUND_BLACK | EFI_WHITE);
//    This->SetMode(This, 0);
    This->ClearScreen(This);
	This->EnableCursor(This, TRUE);
	return	EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCOutputString
//
// Description: 
//  Writes a string to the output device and advances the cursor 
//	as necessary.
//
// Input:
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//	IN CHAR16 *String - pointer to string to be displayed to the screen
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS	- device reset properly
//	    EFI_DEVICE_ERROR - Device reported an Error while outputting a string
//	    EFI_UNSUPPORTED - The output device's mode is not currently in a defined state
//	    EFI_WARN_UNKNOWN_GLYPH - This warning code indicates that some of the 
//          characters in the unicode string could not be rendered and were skipped
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS GCOutputString(
	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
	IN CHAR16                          *String
)
{
	EFI_STATUS		      Status;
	GC_DATA			      *GcData;
    BOOLEAN               UnknownGlyph = FALSE;
    BOOLEAN               CursorVisible;
    EFI_FONT_DISPLAY_INFO FontInfo;
    EFI_IMAGE_OUTPUT      *Glyph = NULL;
    UINT16                GlyphWidth;
	
	// find private data structure	
	GcData = (GC_DATA *) This;	

    CursorVisible = (GcData->SimpleTextOut.Mode)->CursorVisible;
	if (CursorVisible)    
        This->EnableCursor(This, FALSE);
	
	// now loop through the string and display it to the output device
	while (*String != 0)
	{
		switch (*String)
		{
			case CARRIAGE_RETURN:
                FlushString(GcData);
                This->SetCursorPosition(
                                    This, 
                                    0, 
                                    (GcData->SimpleTextOut.Mode)->CursorRow);
				break;

			case LINE_FEED:
			case FORM_FEED:
                FlushString(GcData);
                if((GcData->SimpleTextOut.Mode)->CursorRow == (GcData->MaxRows - 1))
                {
                    ScrollUp(GcData);
                    //cursor position not changed, but image under it does - save new image
                    SaveCursorImage(GcData);
                }
                else
                    This->SetCursorPosition(
                                    This, 
                                    (GcData->SimpleTextOut.Mode)->CursorColumn, 
                                    (GcData->SimpleTextOut.Mode)->CursorRow + 1);
				break;
			
			case BACKSPACE:
                FlushString(GcData);
                if((GcData->SimpleTextOut.Mode)->CursorColumn != 0)
                    This->SetCursorPosition(
                                    This, 
                                    (GcData->SimpleTextOut.Mode)->CursorColumn - 1, 
                                    (GcData->SimpleTextOut.Mode)->CursorRow);
				break;
			
			default:
                Status = HiiLibGetGlyphWidth(*String, &GlyphWidth);
                if(Status == EFI_UNSUPPORTED) {
                    Status = GcData->HiiFont->GetGlyph(
                                                GcData->HiiFont,
                                                *String,
                                                &FontInfo,
                                                &Glyph,
                                                0);
                    if(EFI_ERROR(Status))
                        return Status;
                    GlyphWidth = Glyph->Width;
                }

                if(Status == EFI_WARN_UNKNOWN_GLYPH)
                    UnknownGlyph = TRUE;

                AddChar(GcData, *String, GlyphWidth / 8);

                if(Glyph != NULL) {
                    pBS->FreePool(Glyph->Image.Bitmap);
                    pBS->FreePool(Glyph);
                    Glyph = NULL; 
                }

				break; // end of default case
		}

		String++;
	}
    FlushString(GcData);
	if (CursorVisible)    
        This->EnableCursor(This, TRUE);

	return (UnknownGlyph) ? EFI_WARN_UNKNOWN_GLYPH : EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCTestString
//
// Description: 
//  Tests to see if the String has the glyphs that correspond to
//	each character in the string
//
// Input: 
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//	IN CHAR16 *String - pointer to a string that needs to be tested
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS - all characters can be drawn
//	    EFI_UNSUPPORTED - there are characters that cannot be drawn
//		
// Notes:
//	Uses the HII function TestString
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS  GCTestString(
	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
	IN CHAR16                          *String
)
{
	GC_DATA		*GcData;
    EFI_STATUS  Status;
    EFI_IMAGE_OUTPUT *Glyph = NULL;
	
	// find private data structure	
	GcData = (GC_DATA *) This;	
	
	// now loop through the string and display it to the output device
	while (*String != 0)
	{
		switch (*String)
		{
			case CARRIAGE_RETURN: case LINE_FEED: case BACKSPACE:
				break;
			default:
                Status = GcData->HiiFont->GetGlyph(
                                                GcData->HiiFont,
                                                *String,
                                                NULL,
                                                &Glyph,
                                                0);
                if(EFI_ERROR(Status))
                    return Status;

                pBS->FreePool(Glyph->Image.Bitmap);
                pBS->FreePool(Glyph);
                if(Status == EFI_WARN_UNKNOWN_GLYPH)
                    return EFI_UNSUPPORTED;
                Glyph = NULL; 
				break;
		}
		String++;
	}
	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCQueryMode
//
// Description: 
//  Returns information for an available text mode that the output
//	device supports
//
// Input: 
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//	IN UINTN ModeNum - The mode to return information on
//	OUT UINTN *Col - the number of columns supported
//	OUT UINTN *Row - the number of rows supported
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS	- device reset properly
//	    EFI_DEVICE_ERROR - Device reported an Error while outputting a string
//	    EFI_UNSUPPORTED - The ModeNumber is not supported
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS GCQueryMode(
	IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
	IN  UINTN                           ModeNum,
	OUT UINTN                           *Col,
	OUT UINTN                           *Row
)
{
	GC_DATA		*GcData;
    UINT32      i = 0;
	
	GcData = (GC_DATA *) This;	

    if(ModeNum >= (UINTN)(GcData->SimpleTextOut.Mode)->MaxMode)
        return EFI_UNSUPPORTED;

    while(GcData->SupportedModes[i].ModeNum != ModeNum)
        i++;

    if(!GcData->SupportedModes[i].Supported)
        return EFI_UNSUPPORTED;

	// if the mode is a valid mode, return the data from the array of
	//	for the height and width
	*Col = GcData->SupportedModes[i].Col;
	*Row = GcData->SupportedModes[i].Row;
	
	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCSetMode
//
// Description: 
//  Sets the text mode to the requested mode.  It checks to see if
//	it is a valid mode
//
// Input: 
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//	IN UINTN ModeNum - mode number to change to
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS - the new mode is valid and has been set
//	    EFI_UNSUPPORTED - the new mode is not valid
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GCSetMode(
	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
	IN UINTN                           ModeNum
)
{
	EFI_STATUS	Status;
	GC_DATA		*GcData;
    BOOLEAN     CursorVisible;
    UINT32      i = 0;
    UINT32      DeltaX, DeltaY;
    UINT32      SaveX, SaveY;

	GcData = (GC_DATA *) This;	

    if(ModeNum >= (UINTN)(GcData->SimpleTextOut.Mode)->MaxMode)
        return EFI_UNSUPPORTED;

    while(GcData->SupportedModes[i].ModeNum != ModeNum)
        i++;

    if(!GcData->SupportedModes[i].Supported)
        return EFI_UNSUPPORTED;

    SaveX = GcData->DeltaX;
    SaveY = GcData->DeltaY;

    DeltaX = (GcData->SupportedModes[i].VideoCol - 
              GcData->SupportedModes[i].Col * NARROW_GLYPH_WIDTH) / 2;
    DeltaY = (GcData->SupportedModes[i].VideoRow - 
              GcData->SupportedModes[i].Row * GLYPH_HEIGHT) / 2;

    //save cursor status and hide it if nesessary
    CursorVisible = (GcData->SimpleTextOut.Mode)->CursorVisible;
	if (CursorVisible)
		This->EnableCursor(This, FALSE);

    if(GcData->GraphicsOutput->Mode->Mode != GcData->SupportedModes[i].GraphicsMode)
    {
        Status = GcData->GraphicsOutput->SetMode(GcData->GraphicsOutput,
                                                 GcData->SupportedModes[i].GraphicsMode);
        if(EFI_ERROR(Status))
            return Status;

        if(GcData->OemClearScreen != GcInternalClearScreen) { //we have porting hook installed - call it
            This->ClearScreen(This);
        } else {
        /* the screen is cleared by graphics driver when changed graphics mode
           we just need to reset cursor position */
            This->Mode->CursorColumn = 0;
	        This->Mode->CursorRow = 0;
        }
    }
    else
        This->ClearScreen(This);    //call clear screen with old values

    if(GcData->DeltaX == SaveX && GcData->DeltaY == SaveY)
    {                               //initial position was not changed by porting hook inside C
                                    //ClearScreen function
        GcData->DeltaX = DeltaX;
        GcData->DeltaY = DeltaY;
    }

    GcData->MaxColumns = GcData->SupportedModes[i].Col;
    GcData->MaxRows = GcData->SupportedModes[i].Row;
    (GcData->SimpleTextOut.Mode)->Mode = GcData->SupportedModes[i].ModeNum;

	//  restore cursor at new position
	if (CursorVisible)
		This->EnableCursor(This, TRUE);

    if(TextBuffer == NULL || TextBufferSize < GcData->MaxColumns) {
        if(TextBuffer)
            pBS->FreePool(TextBuffer);

        Status = pBS->AllocatePool(EfiBootServicesData, (GcData->MaxColumns + 1) * sizeof(CHAR16), &TextBuffer);
        if(EFI_ERROR(Status))
            return Status;

        TextBufferSize = GcData->MaxColumns + 1;    //additional space for null-terminator
        Position = 0;
        StringWidth = 0;
    }

	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCSetAttribute
//
// Description: 
//  Sets the foreground color and background color for the screen
//
// Input: 
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//	IN UINTN Attribute - the attributes to set
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS - the attribute was changed successfully
//	    EFI_DEVICE_ERROR - The device had an error
//	    EFI_UNSUPPORTED - The attribute is not supported
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS  GCSetAttribute(
	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
	IN UINTN                           Attribute
)
{
	GC_DATA	*GcData;
	
	GcData = (GC_DATA *) This;
	
	(GcData->SimpleTextOut.Mode)->Attribute = (INT32) Attribute;

	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCClearScreen
//
// Description: 
//  Clears the text screen
//
// Input: 
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS - the screen was cleared
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS  GCClearScreen(
	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This
)
{
	GC_DATA			              *GcData;

	GcData = (GC_DATA *) This;

    GcData->OemClearScreen(GcData);

	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCSetCursorPosition
//
// Description: 
//  This function sets the position of the cursor
//
// Input: 
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//	IN UINTN Column - the new column
//	IN UINTN Row - The new row
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS - the cursor position was changed
//	    EFI_DEVICE_ERROR - The device had an error
//	    EFI_UNSUPPORTED - The device is not in a valid text mode or the 
//	                      cursor position is not valid
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GCSetCursorPosition(
	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
	IN UINTN                           Column,
	IN UINTN                           Row
)
{
	GC_DATA	*GcData;
    BOOLEAN CursorVisible;
	
	GcData = (GC_DATA *) This;
	
	// check for a valid text mode and check for a valid position 
	//	on the screen
	
	if ( ((UINT32)Column >= GcData->MaxColumns) || 
		 ((UINT32)Row >= GcData->MaxRows) )
		return EFI_UNSUPPORTED;
		
	
    //save cursor status and hide it if nesessary
    CursorVisible = (GcData->SimpleTextOut.Mode)->CursorVisible;
	if (CursorVisible)
		This->EnableCursor(This, FALSE);
	
	(GcData->SimpleTextOut.Mode)->CursorColumn = (INT32)Column;
	(GcData->SimpleTextOut.Mode)->CursorRow = (INT32)Row;

	
	//  restore cursor at new position
	if (CursorVisible)
		This->EnableCursor(This, TRUE);
	
	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GCEnableCursor
//
// Description: 
//  Makes cursor invisible or visible
//
// Input: 
//	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This - pointer to the protocol instance
//	IN BOOLEAN Visible - a boolean that if TRUE the cursor will be visible
//	if FALSE the cursor will be invisible
//
// Output:
//  EFI_STATUS
//	    EFI_SUCCESS - the cursor visibility was set correctly
//	    EFI_DEVICE_ERROR - The device had an error
//	    EFI_UNSUPPORTED - The device does not support visibilty control
//	                      for the cursor
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS  GCEnableCursor(
	IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This,
	IN BOOLEAN                         Visible
)
{
	GC_DATA	*GcData;
	
	GcData = (GC_DATA *) This;

	// check to see if the we are already set to the same cursor visibility mode
	if (Visible != (GcData->SimpleTextOut.Mode)->CursorVisible)
	{
		(GcData->SimpleTextOut.Mode)->CursorVisible = Visible;
//if we put cursor back we have to update image under it in order it contains older data
        if(Visible)
            SaveCursorImage(GcData);

		DrawCursor(GcData, Visible);
	}
	
	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GetColorFromAttribute
//
// Description: 
//  Turns color attributes into Pixel values
//
// Input: 
//	IN  UINT32 Attribute - The color to set
//	OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Foreground - foreground color
//	OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Background - background color
//
// Output:
//  EFI_STATUS
//      EFI_SUCCESS - valid colors returned
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS GetColorFromAttribute(
    IN  UINT32                         Attribute, 
    OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Foreground, 
    OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Background
)
{
	UINT8 IndexF;
	UINT8 IndexB;

	if ((Attribute & 0x80) != 0)
			return EFI_UNSUPPORTED;

    IndexF = (UINT8)(Attribute & 0x0f);
    IndexB = (UINT8)((Attribute >> 4) & 0x0f);

	*Foreground = ColorArray[IndexF];
	*Background = ColorArray[IndexB];

	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: DrawCursor
//
// Description: 
//  This function draws /hides the cursor in the current cursor position
//
// Input: 
//	IN GC_DATA * GcData - Private data structure for SimpleTextOut interface
//  IN BOOLEAN Visible - if TRUE - draws cursor, if FALSE - hides cursor
//
// Output:
//  VOID
//
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID DrawCursor(
    IN GC_DATA *GcData, 
    IN BOOLEAN Visible
)
{
    EFI_GRAPHICS_OUTPUT_BLT_PIXEL Fill;

    if(Visible)
    {
        Fill = ColorArray[((GcData->SimpleTextOut.Mode)->Attribute & 0x0f)];    //get foreground color
        GcData->GraphicsOutput->Blt(
            GcData->GraphicsOutput,
            &Fill,
            EfiBltVideoFill,
            0,
            0,                   
            GcData->DeltaX + (GcData->SimpleTextOut.Mode)->CursorColumn * NARROW_GLYPH_WIDTH,
            GcData->DeltaY + (GcData->SimpleTextOut.Mode)->CursorRow * GLYPH_HEIGHT + CURSOR_OFFSET,
            NARROW_GLYPH_WIDTH,
            CURSOR_THICKNESS,
            0); 
    }
    else
    {
        GcData->GraphicsOutput->Blt(
            GcData->GraphicsOutput,
            GcData->Cursor,
            EfiBltBufferToVideo,
            0,
            0,                   
            GcData->DeltaX + (GcData->SimpleTextOut.Mode)->CursorColumn * NARROW_GLYPH_WIDTH,
            GcData->DeltaY + (GcData->SimpleTextOut.Mode)->CursorRow * GLYPH_HEIGHT + CURSOR_OFFSET,
            NARROW_GLYPH_WIDTH,
            CURSOR_THICKNESS,
            NARROW_GLYPH_WIDTH * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL)); 
    }                                   
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: BlinkCursorEvent
//
// Description: 
//  This is the function that makes the cursor blink. A timer event 
//	is created that will cause this function to be called
//
// Input: 
//	IN EFI_EVENT Event - event that was triggered
//  IN VOID *Context - pointer to the event context
//
// Output:
//  VOID
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID EFIAPI BlinkCursorEvent(
	IN EFI_EVENT Event,
	IN VOID      *Context
)
{
	GC_DATA	*GcData;
	
	GcData = (GC_DATA *) Context;

	if (!(GcData->SimpleTextOut.Mode)->CursorVisible)
		return;

	if (GcData->BlinkVisible)
	{
		// remove the cursor from the screen
		DrawCursor(GcData, FALSE);
        GcData->BlinkVisible = FALSE;
	}
	else		
	{
		// put cursor back
		DrawCursor(GcData, TRUE);
        GcData->BlinkVisible = TRUE;
	}
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: GetGraphicsModeNumber
//
// Description: 
//  This function returns graphics mode number, correspondend with given
//  horizontal and vertical resolution
//
// Input: 
//	IN EFI_GRAPHICS_OUTPUT_PROTOCOL	*GraphicsOutput - pointer to Gop driver
//  IN UINT32 HorRes - required horizontal resolution
//  IN UINT32 VerRes - required vertical resolution
//  OUT UINT32 *ModeNum - returned graphics mode number
//      
// Output: 
//  EFI_STATUS
//      EFI_SUCCESS - correct mode number returned
//      EFI_NOT_FOUND - mode number not found for given resolution
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS GetGraphicsModeNumber (
    IN  EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput, 
    IN  UINT32                       HorRes,
    IN  UINT32                       VerRes,
	OUT UINT32                       *ModeNum,
    IN  BOOLEAN                      ExactMatch,
    OUT UINT32                       *ActualHorRes OPTIONAL,
    OUT UINT32                       *ActualVerRes OPTIONAL
)
{
	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION	Info;
	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION	*pInfo = &Info;
	EFI_STATUS				                Status;
	UINTN							        SizeOfInfo;
	UINT32							        i;

	for(i = 0; i < GraphicsOutput->Mode->MaxMode; i++) {
		Status = GraphicsOutput->QueryMode(GraphicsOutput, i, &SizeOfInfo, &pInfo);

		if (!EFI_ERROR(Status)) { 
            if (ExactMatch && pInfo->HorizontalResolution == HorRes && pInfo->VerticalResolution == VerRes ) {
			    *ModeNum = i;
                return Status;
            }
            if(!ExactMatch && pInfo->HorizontalResolution >= HorRes && pInfo->VerticalResolution >= VerRes ) {
                *ModeNum = i;
                *ActualHorRes = pInfo->HorizontalResolution;
                *ActualVerRes = pInfo->VerticalResolution;
                return Status;
            }
		}
	}
    return EFI_NOT_FOUND;
}

EFI_STATUS GetNativeResolution (
    IN  EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput,
	OUT UINT32                       *ModeNum,
    OUT UINT32                       *ActualHorRes,
    OUT UINT32                       *ActualVerRes
)
{
    EFI_STATUS Status;
    EFI_EDID_ACTIVE_PROTOCOL *EdidActive;
    UINT32 HorRes;
    UINT32 VerRes;

    Status = pBS->HandleProtocol(GopHandle, &gEfiEdidActiveProtocolGuid, &EdidActive);
    if(EFI_ERROR(Status) || EdidActive->SizeOfEdid == 0)
        return EFI_UNSUPPORTED;

    if(!((EdidActive->Edid)[24] & BIT01))
    /* native resolution not supported */
        return EFI_UNSUPPORTED;

    HorRes = ((((EdidActive->Edid)[0x36 + 4]) & 0xF0) << 4) + (EdidActive->Edid)[0x36 + 2];
    VerRes = ((((EdidActive->Edid)[0x36 + 7]) & 0xF0) << 4) + (EdidActive->Edid)[0x36 + 5];

    Status = GetGraphicsModeNumber(GraphicsOutput, HorRes, VerRes, ModeNum, TRUE, NULL, NULL);
    if(!EFI_ERROR(Status)) {
        *ActualHorRes = HorRes;
        *ActualVerRes = VerRes;
    }
    return Status;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: SetupGraphicsDevice
//
// Description: 
//  This function fills array of supported text modes
//
// Input: 
//	IN GC_DATA *Data - pointer to private protocol data structure
//      
// Output: 
//  EFI_STATUS
//      EFI_SUCCESS - function executed successfully
//      EFI_UNSUPPORTED - no supported modes found
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SetupGraphicsDevice(
    IN GC_DATA *GcData
)
{
    EFI_STATUS Status;
    INT32 i;
    UINT32 GraphicsModeNumber;
    INT32 MaxSupportedModes = 1;
    BOOLEAN DefaultModeNotSupported = FALSE;
    INT32 ModeZeroIndex;
    UINT32 HorRes;
    UINT32 VerRes;

    for(i = 0; i < MaxTextMode; i++) {
        HorRes = TextModeArray[i].VideoCol;
        VerRes = TextModeArray[i].VideoRow;
        if(HorRes == 0 && VerRes == 0) {
            Status = GetNativeResolution(GcData->GraphicsOutput, &GraphicsModeNumber, &HorRes, &VerRes);
        } else {
        /* get mode number with exact resolution */
            Status = GetGraphicsModeNumber(GcData->GraphicsOutput,
                                            HorRes,
                                            VerRes,
                                            &GraphicsModeNumber,
                                            TRUE, NULL, NULL);
        }

        if(!EFI_ERROR(Status)) {
            GcData->SupportedModes[i].Supported = TRUE;
            GcData->SupportedModes[i].GraphicsMode = GraphicsModeNumber;
            GcData->SupportedModes[i].ModeNum = TextModeArray[i].ModeNum;

            if(TextModeArray[i].Col == 0)
                GcData->SupportedModes[i].Col = HorRes / NARROW_GLYPH_WIDTH;
            else
                GcData->SupportedModes[i].Col = TextModeArray[i].Col;

            if(TextModeArray[i].Row == 0)
                GcData->SupportedModes[i].Row = VerRes / GLYPH_HEIGHT;
            else
                GcData->SupportedModes[i].Row = TextModeArray[i].Row;

            GcData->SupportedModes[i].VideoCol = HorRes;
            GcData->SupportedModes[i].VideoRow = VerRes;

            MaxSupportedModes = (TextModeArray[i].ModeNum >= MaxSupportedModes) ? 
                                 TextModeArray[i].ModeNum + 1 : MaxSupportedModes;
        } else {
            GcData->SupportedModes[i].Supported = FALSE;
            GcData->SupportedModes[i].ModeNum = TextModeArray[i].ModeNum;

            if(TextModeArray[i].ModeNum == 0) {
                ModeZeroIndex = i;
                DefaultModeNotSupported = TRUE;
            }
        }
    }

    if(DefaultModeNotSupported) {
        Status = GetGraphicsModeNumber(GcData->GraphicsOutput,
                                       MODE_ZERO_MIN_HOR_RES,
                                       MODE_ZERO_MIN_VER_RES,
                                       &GraphicsModeNumber,
                                       FALSE,
                                       &(GcData->SupportedModes[ModeZeroIndex].VideoCol),
                                       &(GcData->SupportedModes[ModeZeroIndex].VideoRow));
        if(EFI_ERROR(Status)) {
            (GcData->SimpleTextOut.Mode)->MaxMode = 0;
            return EFI_UNSUPPORTED;
        }

        GcData->SupportedModes[ModeZeroIndex].Supported = TRUE;
        GcData->SupportedModes[ModeZeroIndex].GraphicsMode = GraphicsModeNumber;
        GcData->SupportedModes[ModeZeroIndex].Row = 25;
        GcData->SupportedModes[ModeZeroIndex].Col = 80;
    }

    (GcData->SimpleTextOut.Mode)->MaxMode = MaxSupportedModes;
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: ScrollUp
//
// Description: 
//  This function scrolls screen one row up and clears bottom row
//
// Input:       
//  IN GC_DATA *GcData - pointer to private protocol data structure
//      
// Output:      
//  VOID
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID ScrollUp(
    IN GC_DATA *GcData
)
{
    EFI_GRAPHICS_OUTPUT_BLT_PIXEL Fill;

    GcData->GraphicsOutput->Blt(GcData->GraphicsOutput,
                                &Fill,
                                EfiBltVideoToVideo,
                                GcData->DeltaX,
                                GcData->DeltaY + GLYPH_HEIGHT,                   
                                GcData->DeltaX,
                                GcData->DeltaY,
                                GcData->MaxColumns * NARROW_GLYPH_WIDTH,
                                (GcData->MaxRows - 1) * GLYPH_HEIGHT,
                                0);
//clear bottom line
    Fill = ColorArray[(((GcData->SimpleTextOut.Mode)->Attribute >> 4) & 0xf)];
    GcData->GraphicsOutput->Blt(GcData->GraphicsOutput,
                                &Fill,
                                EfiBltVideoFill,
                                0,
                                0,                   
                                GcData->DeltaX,
                                GcData->DeltaY + (GcData->MaxRows - 1) * GLYPH_HEIGHT,
                                GcData->MaxColumns * NARROW_GLYPH_WIDTH,
                                GLYPH_HEIGHT,
                                0);
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name:    SaveCursorImage
//
// Description: 
//  This function saves image under cursor to restore, when cursor moves
//
// Input:       
//  IN GC_DATA *GcData - pointer to private protocol data structure
//      
// Output:      
//  VOID
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID SaveCursorImage(
    IN GC_DATA *GcData
)
{
    GcData->GraphicsOutput->Blt(
            GcData->GraphicsOutput,
            GcData->Cursor,
            EfiBltVideoToBltBuffer,
            GcData->DeltaX + (GcData->SimpleTextOut.Mode)->CursorColumn * NARROW_GLYPH_WIDTH,
            GcData->DeltaY + (GcData->SimpleTextOut.Mode)->CursorRow * GLYPH_HEIGHT + CURSOR_OFFSET,
            0,
            0,                   
            NARROW_GLYPH_WIDTH,
            CURSOR_THICKNESS,
            NARROW_GLYPH_WIDTH * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
}    

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name: ShiftCursor
//
// Description: 
//  This function shifts cursor right to number of columns defined in Step
//  If cursor reaches right edge of the screen it moves one line down, scrolling screen
//  if nesessary
//
// Input: 
//	IN GC_DATA *GcData - pointer to private protocol data structure
//  IN UINT16 Step - number of columns to shift cursor right
//      
// Output: 
//  VOID
//		
// Notes:
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID ShiftCursor(
    IN GC_DATA *GcData,
    IN UINT16  Step
)
{
    EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)GcData;

    if(((GcData->SimpleTextOut.Mode)->CursorColumn + Step) >= (INT32)GcData->MaxColumns)
    {
        if((GcData->SimpleTextOut.Mode)->CursorRow == GcData->MaxRows - 1)
        {
            ScrollUp(GcData);
            This->SetCursorPosition(
                              This, 
                              0, 
                              (GcData->SimpleTextOut.Mode)->CursorRow);
        }
        else
        {
            This->SetCursorPosition(
                              This, 
                              0, 
                              (GcData->SimpleTextOut.Mode)->CursorRow + 1);
        }
    }
    else
    {
        This->SetCursorPosition(
                              This, 
                              (GcData->SimpleTextOut.Mode)->CursorColumn + Step, 
                              (GcData->SimpleTextOut.Mode)->CursorRow);
    }
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name:    GcUpdateBltBuffer
//
// Description: 
//  This function is a porting hook to implement specific action on
//  Blt buffer before put it on screen
//
// Input:       
//  IN GC_DATA *GcData - pointer to internal structure
//  IN UINT32 Width - width of passed buffer in pixels
//  IN UINT32 Height - height of passed buffer in pixels
//  IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer - pointer to Blt buffer
//              to perform action upon
//      
// Output:
//  VOID
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID GcUpdateBltBuffer (
	IN     GC_DATA 			             *This,
	IN     UINT32			             Width,
    IN     UINT32                        Height,
	IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer
)
{
    return;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name:    GcInternalClearScreen
//
// Description: 
//  This function is a porting hook to implement specific action when
//  clear screen operation is needed
//
// Input:       
//  IN GC_DATA *This - pointer to private protocol data structure
//      
// Output:      
//  VOID
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID GcInternalClearScreen (
	IN OUT GC_DATA *This
)
{
    EFI_GRAPHICS_OUTPUT_BLT_PIXEL Fill;
    EFI_STATUS Status;
    UINT32 StartX;
    UINT32 StartY;
    UINT32 SizeX;
    UINT32 SizeY;

    if(This->MaxColumns == 0 || This->MaxRows == 0) { //this is the first invocation
        StartX = 0;
        StartY = 0;
        SizeX = This->GraphicsOutput->Mode->Info->HorizontalResolution;
        SizeY = This->GraphicsOutput->Mode->Info->VerticalResolution;
    } else {
        StartX = This->DeltaX;
        StartY = This->DeltaY;
        SizeX = This->MaxColumns * NARROW_GLYPH_WIDTH;
        SizeY = This->MaxRows * GLYPH_HEIGHT;
    }

    Fill = ColorArray[(((This->SimpleTextOut.Mode)->Attribute >> 4) & 0xf)];
    This->GraphicsOutput->Blt(This->GraphicsOutput,
                                &Fill,
                                EfiBltVideoFill,
                                0,
                                0,                   
                                StartX,
                                StartY,
                                SizeX,
                                SizeY,
                                0);

	Status = This->SimpleTextOut.SetCursorPosition(&(This->SimpleTextOut), 0, 0);
    if(EFI_ERROR(Status)) { //on first invocation this failed because MaxRows = MaxCols = 0
        (This->SimpleTextOut.Mode)->CursorColumn = 0;
	    (This->SimpleTextOut.Mode)->CursorRow = 0;
    }
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name:    AddChar
//
// Description: 
//  This function adds character to internal buffer of ready-to-print string
//
// Input:       
//  IN GC_DATA *This - pointer to private protocol data structure
//  IN CHAR16 Char - character to add
//  IN UINT16 Width - character width in system font symbols
//      
// Output:      
//  VOID
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID AddChar(
    IN GC_DATA *GcData,
    IN CHAR16 Char,
    IN UINT16 Width
)
{
    UINT32 CurrentWidth;

    CurrentWidth = (GcData->SimpleTextOut.Mode)->CursorColumn + StringWidth;
    if((CurrentWidth + Width) > GcData->MaxColumns) {
        FlushString(GcData);

        if(Width == 2)
            ShiftCursor(GcData, 1);
    }

    TextBuffer[Position] = Char;
    Position++;
    StringWidth += Width;
}

//<AMI_PHDR_START>
//-----------------------------------------------------------------------------
// Name:    FlushString
//
// Description: 
//  This function flushes string from internal buffer to screen
//
// Input:       
//  IN GC_DATA *This - pointer to private protocol data structure
//      
// Output:      
//  VOID
//
//-----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID FlushString(
    IN GC_DATA *GcData
)
{
    EFI_STATUS Status;
    EFI_FONT_DISPLAY_INFO FontInfo;
    EFI_IMAGE_OUTPUT      *Image = NULL;

    if(Position == 0)
        return;

//retreive colors
    GetColorFromAttribute(
                        (GcData->SimpleTextOut.Mode)->Attribute,
                        &FontInfo.ForegroundColor,
                        &FontInfo.BackgroundColor);
//use system font
    FontInfo.FontInfoMask =   EFI_FONT_INFO_SYS_FONT 
                            | EFI_FONT_INFO_SYS_SIZE 
                            | EFI_FONT_INFO_SYS_STYLE;

/* put NULL-terminator */
    TextBuffer[Position] = 0;

    Status = GcData->HiiFont->StringToImage(GcData->HiiFont,
                                    EFI_HII_IGNORE_LINE_BREAK,
                                    TextBuffer,
                                    &FontInfo,
                                    &Image,
                                    0, 0, NULL, NULL, NULL);

    GcData->OemUpdateBltBuffer(GcData, Image->Width, Image->Height, Image->Image.Bitmap);

    GcData->GraphicsOutput->Blt(
                    GcData->GraphicsOutput,
                    Image->Image.Bitmap,
                    EfiBltBufferToVideo,
                    0,
                    0,                   
                    GcData->DeltaX + (GcData->SimpleTextOut.Mode)->CursorColumn * NARROW_GLYPH_WIDTH,
                    GcData->DeltaY + (GcData->SimpleTextOut.Mode)->CursorRow * GLYPH_HEIGHT,
                    Image->Width,
                    Image->Height,
                    Image->Width * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));     

    ShiftCursor(GcData, (Image->Width / 8));

    pBS->FreePool(Image->Image.Bitmap);
    pBS->FreePool(Image);

    Position = 0;
    StringWidth = 0;
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2012, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************