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

//**********************************************************************
// $Header: /Alaska/SOURCE/Core/Library/StdLibC.c 22    8/15/12 5:41p Artems $
//
// $Revision: 22 $
//
// $Date: 8/15/12 5:41p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/Library/StdLibC.c $
// 
// 22    8/15/12 5:41p Artems
// [TAG]  		EIP N/A
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	Added correct error codes for display purposes
// [Files]  		StdLib.c
// 
// 21    11/17/11 6:12p Artems
// Fixed bug: PI errors don't display properly
// 
// 20    11/11/11 3:37p Artems
// Added UEFI 2.3.1 error codes
// 
// 19    2/05/11 3:11p Artems
// Added PI 1.0-1.1 error and warning messages
// 
// 18    8/07/09 4:45p Felixp
// Minor bug fix in StrtolEx function.
// The code of Sprintf_s_va_list and Swprintf_s_va_list functions is
// optimized.
// 
// 17    7/31/09 4:24p Felixp
// Minor improvements in Sprintf_s_va_list and Swprintf_s_va_list
// functions.
// 
// 16    7/30/09 4:23p Vyacheslava
// Minor bug fix. EIP#24453: Synopsis: CPU exception when printing long
// debug messages.
// 
// 15    7/29/09 4:04p Vyacheslava
// Buffer overflow protection has been added for Sprintf.
// 
// 14    7/28/09 4:52p Vyacheslava
//  Bug fix. EIP#24453: Synopsis: CPU exception when printing long
// debug messages.
// 
// 13    7/10/09 4:35p Felixp
// 
// 12    7/10/09 3:49p Felixp
// Function headers added
// 
// 11    5/04/09 3:21p Felixp
// Bug fix in Strcpy and Wcscpy functions.  
// The functions were returning pointer to the next character after the
// terminator of the destination string. 
// The code is updated to return pointer to the beginning of the
// destination string.
// 
// 10    8/24/06 9:26a Felixp
// Preliminary x64 support (work in progress):
// 1. Processor architecture specific functions moved to a processor
// library
// 2. Makefile removed (AmiLib files are build by the AmiPeiLib and
// AmeDxeLib makefile)
// 3. Tokens.c added
// 
// 9     5/19/06 10:25p Felixp
// Strcpy, Wcscpy changed to return pointer to the end of the string
// 
// 8     4/30/06 9:40p Felixp
// 
// 7     7/11/05 4:52p Felixp
// Sprintf and Swprinf: added support for %c (character printing)
// 
// 6     4/22/05 4:47p Felixp
// 
// 5     4/21/05 8:01p Felixp
// Spintf and Swprinf changed to reflect value changes for AMI Status Code
// (see the corresponding change in EFI.h
// 
// 4     4/02/05 2:25p Felixp
// Swprintf function added
// 
// 3     3/25/05 5:25p Felixp
// Strtol modified to support both ASCII and Unicode strings
// 
// 2     1/18/05 3:22p Felixp
// PrintDebugMessage renamed to Trace
// 
// 1     12/23/04 9:41a Felixp
// 
// 14    12/15/04 9:06p Felixp
// LegacyBoot application added
// Minor build process improvements
// 
// 13    12/15/04 3:52p Markw
// Added Strstr.
// 
// 12    11/24/04 9:36a Felixp
// int64 suppot added to Itoa and Itow
// 
// 11    11/10/04 5:14p Felixp
// 1. Bug fixes in Itoa and Itow
// 2. const qualifiers removed
// 
// 10    10/22/04 7:42p Felixp
// 
// 9     9/21/04 10:47a Robert
// Sprintf_va_list needed to add a carrage return character when it found
// a new line character
// 
// 8     4/04/04 2:58p Felixp
// 
// 7     3/30/04 3:42p Markw
// Added sprintf status code support.
// 
// 6     3/30/04 9:51a Markw
// Added Sprintf declaration before Sprintf_va_list.
// 
// 5     3/29/04 5:26p Markw
// Added Guid printing through sprintf, and renamed some functions. Split
// sprintf into the variable parameter list and non-varialble parameter
// list.
// 
// 4     3/26/04 5:59p Markw
// Replaced a broken printf with a working sprintf. Does not support of
// floating points.
// 
// 3     3/17/04 12:01p Markw
// Changed unsigned (where it should be size_t) to UINTN.
// 
// 2     2/26/04 3:23p Felixp
// 
// 1     1/22/04 3:04p Markw
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:	StdLibC.c
//
// Description:
//  StdLibC.c contains a number of standard C library functions, including
// but not limited to string and cctype library functions.
//
//<AMI_FHDR_END>
//*************************************************************************
#include <AmiLib.h>

char EfiErrorStatus[][25] = 
{
	"EFI_LOAD_ERROR",
	"EFI_INVALID_PARAMETER",
	"EFI_UNSUPPORTED",
	"EFI_BAD_BUFFER_SIZE",
	"EFI_BUFFER_TOO_SMALL",
	"EFI_NOT_READY",
	"EFI_DEVICE_ERROR",
	"EFI_WRITE_PROTECTED",
	"EFI_OUT_OF_RESOURCES",
	"EFI_VOLUME_CORRUPTED",
	"EFI_VOLUME_FULL",
	"EFI_NO_MEDIA",
	"EFI_MEDIA_CHANGED",
	"EFI_NOT_FOUND",
	"EFI_ACCESS_DENIED",
	"EFI_NO_RESPONSE",
	"EFI_NO_MAPPING",
	"EFI_TIMEOUT",
	"EFI_NOT_STARTED",
	"EFI_ALREADY_STARTED",
	"EFI_ABORTED",
	"EFI_ICMP_ERROR",
	"EFI_TFTP_ERROR",
	"EFI_PROTOCOL_ERROR",
	"EFI_INCOMPATIBLE_VERSION",
	"EFI_SECURITY_VIOLATION",
	"EFI_CRC_ERROR"
#if EFI_SPECIFICATION_VERSION<0x20000
	,"EFI_NOT_AVAILABLE_YET",
	"EFI_UNLOAD_IMAGE"
#else
    ,"EFI_END_OF_MEDIA",
    "EFI_UNDEFINED_29",
    "EFI_UNDEFINED_30",
    "EFI_END_OF_FILE",
    "EFI_INVALID_LANGUAGE",
    "EFI_COMPROMISED_DATA"
#endif
};

char PiErrorStatus[][25] = 
{
    "EFI_INTERRUPT_PENDING",
    "EFI_REQUEST_UNLOAD_IMAGE",
    "EFI_NOT_AVAILABLE_YET"
};

char AmiErrorStatus[][25] = 
{
	"EFI_DBE_EOF",
	"EFI_DBE_BOF"
};

char EfiWarningStatus[][26] =
{
	"EFI_WARN_UNKNOWN_GLYPH",
	"EFI_WARN_DELETE_FAILURE",
	"EFI_WARN_WRITE_FAILURE",
	"EFI_WARN_BUFFER_TOO_SMALL"
};

char PiWarningStatus[][35] =
{
	"EFI_WARN_INTERRUPT_SOURCE_PENDING",
	"EFI_WARN_INTERRUPT_SOURCE_QUIESCED"
};

#if EFI_SPECIFICATION_VERSION<0x20000
#define NUM_OF_EFI_ERRORS	29
#else
#define NUM_OF_EFI_ERRORS	30
#endif
#define NUM_OF_EFI_WARNINGS	4
#define NUM_OF_AMI_ERRORS 2
#define NUM_OF_PI_ERRORS 3
#define NUM_OF_PI_WARNINGS 2

char EfiUnknown[] = "Status Code";	//If unknown value.

CHAR16 * wcsupr(CHAR16 *str);

#define true  TRUE
#define false FALSE

//*************************************************************************
//<AMI_GHDR_START>
//
// Name: Stdlib_Functions
//
// Description:
//  C standard general utilities library functions, typically found in
// stdlib.h, that are defined in the AMI library.
//
// Fields: Header Function Description
// ------------------------------------------------------------------
// AmiLib    Atoi     Convert 8-bit ASCII string to INT32.
// AmiLib    StrtolEx Convert 8-bit ASCII string to INT64.
// AmiLib    ItoaEx   Convert INT64 to 8-bit ASCII string.
// AmiLib    ItowEx   Convert INT64 to 16-bit Unicode string.
// AmiDxeLib Malloc   Allocate memory from EfiBootServicesData.
// AmiDxeLib MallocZ  Allocate memory from EfiBootServicesData that has been cleared with zeros.
//
// Notes:
//  Header details which header file contains the function prototype for
// the above functions; append .h to the given name.  None indicates that
// no header file contains a function prototype.
//  
//<AMI_GHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_GHDR_START>
//
// Name: Ctype_Functions
//
// Description:
//  C character handling functions, typically found in ctype.h, that are
// defined in the AMI library.
//
// Fields: Header Function Description
// ------------------------------------------------------------------
// None    Toupper     Convert lowercase 8-bit ASCII character to uppercase.
//
// Notes:
//  Header details which header file contains the function prototype for
// the above functions; append .h to the given name.  None indicates that
// no header file contains a function prototype.
//  
//<AMI_GHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_GHDR_START>
//
// Name: String_Functions
//
// Description:
//  String related functions that are defined in the AMI library.
//
// Fields: Header Function Description
// ------------------------------------------------------------------
// AmiLib  Strlen      Find length of null-terminated 8-bit ASCII string.
// AmiLib  Wcslen      Find length of null-terminated 16-bit Unicode string.
// AmiLib  Strcpy      Copy an 8-bit ASCII string into another string.
// AmiLib  Wcscpy      Copy a 16-bit Unicode string into another string.
// AmiLib  Strcmp      Compare two 8-bit ASCII strings to each other.
// AmiLib  Wcscmp      Compare two 16-bit Unicode strings to each other.
// AmiLib  Strupr      Convert an 8-bit ASCII string into all uppercase characters.
// AmiLib  Wcsupr      Convert a 16-bit Unicode string to all uppercase characters.
// AmiLib  Strstr      Find an occurrence of an 8-bit ASCII string inside another string.
// AmiLib  Sprintf     Print a formatted 8-bit ASCII string to a buffer.
// AmiLib  Swprintf    Print a formatted 16-bit Unicode string to a buffer.
// AmiLib  Sprintf_s   Print a formatted 8-bit ASCII string to a buffer of predetermined size.
// AmiLib  Swprintf_s  Print a formatted 16-bit Unicode string to a buffer of predetermined size.
// AmiLib  ItoaEx      Convert INT64 to 8-bit ASCII string.
// AmiLib  ItowEx      Convert INT64 to 16-bit Unicode string.
//
// Notes:
//  Header details which header file contains the function prototype for
// the above functions; append .h to the given name.  None indicates that
// no header file contains a function prototype.
// 
//<AMI_GHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_GHDR_START>
//
// Name: Cstring_Functions
//
// Description:
//  C string handling functions, typically found in string.h, that are
// defined in the AMI library.
//
// Fields: Header Function Description
// ------------------------------------------------------------------
// AmiLib  Strlen      Find length of null-terminated 8-bit ASCII string.
// AmiLib  Wcslen      Find length of null-terminated 16-bit Unicode string.
// AmiLib  Strcpy      Copy an 8-bit ASCII string into another string.
// AmiLib  Wcscpy      Copy a 16-bit Unicode string into another string.
// AmiLIb  MemCpy      Copy a buffer into another buffer.
// AmiLib  Strcmp      Compare two 8-bit ASCII strings to each other.
// AmiLib  Wcscmp      Compare two 16-bit Unicode strings to each other.
// AmiLib  MemCmp      Compare two buffers for equality.
// AmiLib  Strupr      Convert an 8-bit ASCII string into all uppercase characters.
// AmiLib  Wcsupr      Convert a 16-bit Unicode string to all uppercase characters.
// AmiLib  Strstr      Find an occurrence of an 8-bit ASCII string inside another string.
// AmiLib  MemSet      Fills a buffer with a user provided value. 
//
// Notes:
//  Header details which header file contains the function prototype for
// the above functions; append .h to the given name.  None indicates that
// no header file contains a function prototype.
// 
//<AMI_GHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_GHDR_START>
//
// Name: Stdio_Functions
//
// Description:
//  C standard I/O functions, typically found in stdio.h, that are
// defined in the AMI library.
//
// Fields: Header Function Description
// ------------------------------------------------------------------
// AmiLib  Sprintf     Print a formatted 8-bit ASCII string to a buffer.
// AmiLib  Swprintf    Print a formatted 16-bit Unicode string to a buffer.
// AmiLib  Sprintf_s   Print a formatted 8-bit ASCII string to a buffer of predetermined size.
// AmiLib  Swprintf_s  Print a formatted 16-bit Unicode string to a buffer of predetermined size.
//
// Notes:
//  Header details which header file contains the function prototype for
// the above functions; append .h to the given name.  None indicates that
// no header file contains a function prototype.
// 
//<AMI_GHDR_END>
//*************************************************************************

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Toupper
//
// Description:
//  INT32 Toupper(IN INT32 c) converts a lowercase ASCII character, c, to an
// uppercase ASCII character and returns its uppercase ASCII value.  If c is
// not a valid lowercase ASCII character, returns c.
//
// Input:
//  IN INT32 c
// ASCII value to be converted from lowercase to uppercase.
//
// Output:
//  INT32 value of c, if c is not a valid lowercase ASCII character.
//  Otherwise, INT32 ASCII value of c as an uppercase letter.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
int Toupper(int c) {
	return (c>='a' && c<='z') ? c-'a'+'A' : c;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Strlen
//
// Description:
//  UINTN Strlen(IN CHAR8 *string) takes a null-terminated CHAR8 string and
// returns its UINTN length (not including the null-terminator).
//
// Input:
//  IN CHAR8 *string
// Pointer to a null-terminated CHAR8 string.
//
// Output:
//  UINTN length of the string (not including the null-terminator).
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
UINTN Strlen(char *string) {
	UINTN length=0;
	while(*string++) length++;
	return length;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Wcslen
//
// Description:
//  UINTN Wcslen(IN CHAR16 *string) takes a null-terminated CHAR16 string and
// returns its UINTN length (not including the null-terminator).
//
// Input:
//  IN CHAR16 *string
// Pointer to a null-terminated CHAR16 string.
//
// Output:
//  UINTN length of the string (not including the null-terminator).
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
UINTN Wcslen(CHAR16 *string) {
	UINTN length=0;
	while(*string++) length++;
	return length;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Strcpy
//
// Description:
//  CHAR8* Strcpy(IN OUT CHAR8 *string1, IN CHAR8 *string2) copies the
// contents of a CHAR8 string, string2, into another CHAR8 string, string1.
//
// Input:
//  IN OUT CHAR8 *string1
// Pointer to a CHAR8 string buffer to receive the contents of string2.
// String size must be greater than or equal to string2.  User is
// responsible for allocating the necessary memory resources.
//
//  IN CHAR8 *string2
// Pointer to a null-terminated CHAR8 string to be copied.
//
// Output:
//  CHAR8* string pointer to the last character added to string1.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
char* Strcpy(char *string1,char *string2){
    char *dest = string1;
	while(*string1++=*string2++);
	return dest;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Wcscpy
//
// Description:
//  CHAR16* Wcscpy(IN OUT CHAR16 *string1, IN CHAR16 *string2) copies the
// contents of a CHAR16 string, string2, into another CHAR16 string, string1.
//
// Input:
//  IN OUT CHAR16 *string1
// Pointer to a CHAR16 string buffer to receive the contents of string2.
// String size must be greater than or equal to string2.  User is
// responsible for allocating the necessary memory resources.
//
//  IN CHAR16 *string2
// Pointer to a null-terminated CHAR16 string to be copied.
//
// Output:
//  CHAR16* string pointer to the last character added to string1.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
CHAR16* Wcscpy(CHAR16 *string1, CHAR16* string2){
    CHAR16 *dest = string1;
	while(*string1++=*string2++);
	return dest;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Strcmp
//
// Description:
//  INT32 Strcmp(IN CHAR8 *string1, IN CHAR8 *string2) compares two CHAR8
// strings, string1 and string2, and returns 0 if they are identical,
// non-zero if they are not identical.
//
// Input:
//  IN CHAR8 *string1
// Pointer to a CHAR8 null-terminated string.
//
//  IN CHAR8 *string2
// Pointer to a CHAR8 null-terminated string.
//
// Output:
//  INT32 non-zero value if both string1 and string2 are not identical.
//  Otherwise, returns 0 if both string1 and string 2 are identical.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
int Strcmp( char *string1, char *string2 ) {
	while(*string1 && *string1==*string2) {*string1++;*string2++;}
	return *string1 - *string2;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Wcscmp
//
// Description:
//  INT32 Wcscmp(IN CHAR16 *string1, IN CHAR16 *string2) compares two CHAR16
// strings, string1 and string2, and returns 0 if they are identical,
// non-zero if they are not identical.
//
// Input:
//  IN CHAR16 *string1
//      Pointer to a CHAR16 null-terminated string.
//
//  IN CHAR16 *string2
//      Pointer to a CHAR16 null-terminated string.
//
// Output:
//  INT32 non-zero value if both string1 and string2 are not identical.
//  Otherwise, returns 0 if both string1 and string 2 are identical.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
int Wcscmp( CHAR16 *string1, CHAR16 *string2 ) {
	while(*string1 && *string1==*string2) {*string1++;*string2++;}
	return *string1 - *string2;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Atoi
//
// Description:
//  INT32 Atoi(IN CHAR8 *string) converts an ASCII string that represents an
// integer into an actual INT32 integer value and returns the result.
//
// Input:
//  IN CHAR8 *string
// Pointer to a CHAR8 string that represents an integer number.
//
// Output:
//  INT32 value that the string represented.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
int Atoi(char *string) {
	int value=0;
	while (*string>='0' && *string<='9') {
		value *=10;
		value += *string-'0';
		string++;
	}
	return value;
}

//*************************************************************************
//
// Name: ItoaHelper
//
// Description:
//  CHAR8* ItoaHelper(IN INT64 value, IN OUT CHAR8 *string, IN INT32 radix,
// IN BOOLEAN is_int64) is a helper function for ItoaEx and ItowEx which 
// converts an INT64 value into a CHAR8 ASCII representation in reverse
// order.
//
// Input:
//  IN INT64 value
// Value to be converted.
//
//  IN OUT CHAR8 *string
// Pointer to a CHAR8 string buffer that will hold the result of the reverse
// conversion.  User is responsible for allocating the necessary memory
// resources.
//
//  IN INT32 radix
// Radix of the conversion.
//
//  IN BOOLEAN is_int64
// TRUE if value is a 64-bit integer; FALSE if it's 32-bit.
//
// Output:
//  CHAR8* string pointer to the last character added to the input string, 
// which now contains an ASCII string that is the reverse of what value 
// represents.
//
// Modified:
//
// Referrals:
//  Div64
// 
// Notes:	
// 
//*************************************************************************
char * ItoaHelper(INT64 value, char *string,int radix, BOOLEAN is_int64) {
	UINTN digit;
	UINT64 v = (value>=0) 
			   ? (UINT64) value
			   : (radix==10) 
				 ? (UINT64)-value 
				 : (is_int64) 
				   ? (UINT64)value 
				   : (UINT32)value;
	if (v)
		while (v) {
			v = Div64(v,radix,&digit);
			if (digit<0xa) *string=(char)(digit+'0');
			else *string=(char)(digit-0xa+'a');
			string++;
		}
	else *string++='0';
	if (radix==10 && value<0) *string++='-';
	*string--=0;
	return string;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: ItoaEx
//
// Description:
//  CHAR8* ItoaEx(IN INT64 value, IN OUT CHAR8 *string, IN INT32 radix, 
// IN BOOLEAN is_int64) converts an INT64 value into a CHAR8 ASCII 
// representation and returns a pointer to the ASCII string.
//
// Input:
//  IN INT64 value
// Value that needs to be converted into an ASCII representation.
//
//  IN OUT CHAR8 *string
// Pointer to a CHAR8 string buffer which will contain the result of the
// conversion.  User is responsible for allocating the necessary memory
// resources.
//
//  IN INT32 radix
// Radix of the conversion.  For example, 10 for decimal, 16 for hexadecimal,
// etc.
//
//  IN BOOLEAN is_int64
// TRUE if value is a 64-bit integer; FALSE if it's 32-bit.
//
// Output:
//  CHAR8* string pointer, string, that contains the ASCII representation of
// value in the user requested radix.
//
// Modified:
//
// Referrals:
//  ItoaHelper
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
char * ItoaEx(INT64 value, char *string,int radix, BOOLEAN is_int64) {
	char *strp=string;
	char *str2p=ItoaHelper(value,strp,radix,is_int64);
	//reverse string
	while(strp<str2p) {
		char temp=*strp;
		*strp=*str2p;
		*str2p=temp;
		strp++;str2p--;
	}
	return string;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: ItowEx
//
// Description:
//  CHAR16* ItowEx(IN INT64 value, IN OUT CHAR16 *string, IN INT32 radix, 
// IN BOOLEAN is_int64) converts an INT64 value into a 16-bit Unicode 
// representation and returns a CHAR16 pointer to the string.
//
// Input:
//  IN INT64 value
// Value that needs to be converted into an ASCII representation.
//
//  IN OUT CHAR16 *string
// Pointer to a CHAR8 string buffer which will contain the result of the
// conversion.
//
//  IN INT32 radix
// Radix of the conversion.  For example, 10 for decimal, 16 for hexadecimal,
// etc.
//
//  IN BOOLEAN is_int64
// TRUE if value is a 64-bit integer; FALSE if it's 32-bit.
//
// Output:
//  CHAR16* string pointer, string, that contains the 16-bit Unicode
// representation of value in the user requested radix.
//
// Modified:
//
// Referrals:
//  ItoaHelper
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
CHAR16 * ItowEx(INT64 value, CHAR16 *string,int radix, BOOLEAN is_int64) {
	char s[0x100];
	CHAR16 *str = string;
	char *send=ItoaHelper(value,(char*)s,radix,is_int64);
	//convert to unicode
	while(send>=s) {
		*str++ = *send--;
	}
	*str=0;
	return string;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: StrtolEx
//
// Description:
//  INT64 StrtolEx(IN CHAR8 *nptr, OUT CHAR8 **endptr, IN INT32 base,
// IN INT32 increment) converts a CHAR8 string representation of a number
// into an INT64 representation.
//
// Input:
//  IN CHAR8 *nptr
// CHAR8 string to be converted into an INT64.
//
//  OUT CHAR8 **endptr
// CHAR8 string pointer to the final character read from nptr.
//
//  IN INT32 base
// The base of the string being passed in.  If 0, string will be parsed by
// defaults; preceding "0x" treated as hexadecimal, preceding "0" treated as
// octal, everything else treated as decimal.
//
//  IN INT32 increment
// The number of characters in between each number digit.  For example,
// "14131" would be 1; "1 4 1 3 1" would be 2.
//
// Output:
//  INT64 representation of the number in the ASCII nptr string using the
// provided base and increment amount.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
long StrtolEx(char *nptr,char **endptr,int base, int increment) {
	unsigned char overflow=false;
	char sign=1;
	char digit;
	long value=0;

	while(*nptr == ' ' || *nptr == '\t') nptr+=increment;
	if (*nptr == '\0') {*endptr=(char*)nptr;return 0;}

	if (*nptr == '-') {sign = -1;nptr+=increment;}
	if (*nptr == '+') nptr+=increment;

	if (!base) {
		base=10;
		if (*nptr == '0') {
			base=8;
			nptr+=increment;
			if ((*nptr&0xdf)=='X') {	//Check for 'x' or 'X'
				base=16;
				nptr+=increment;
			}
		}
	}
	while(true) {
		if (*nptr >= '0' && *nptr <='9') digit=*nptr-'0';
		else if ((*nptr&0xdf)>='A'&& (*nptr&0xdf)<='Z') digit = (*nptr&0xdf) - 'A' + 0xa;
		else break;
		if (digit >= base) break;
		value = value * base + digit;
		if (sign==1) {
			if ((unsigned) value >= (unsigned) 0x80000000) overflow=true;
		} else if ((unsigned) value > (unsigned) 0x80000000) overflow=true;
		nptr+=increment;
	}
	*endptr=(char*)nptr;
	if (overflow) {
		value=0x7fffffff;
		if (sign==-1) value++;
	}
	return value*sign;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Strupr
//
// Description:
//  CHAR8* Strupr(IN CHAR8 *str) converts a CHAR8 string to all uppercase
// characters.
//
// Input:
//  IN CHAR8 *str
// CHAR8 string to be converted to all uppercase.
//
// Output:
//  CHAR8* string pointer to the start of the modified string.
//
// Modified:
//
// Referrals:
// 
// Notes:	
//  Original string will be modified!
// 
//<AMI_PHDR_END>
//*************************************************************************
char * Strupr(char *str) {
	char *strptr=str;
	while (*strptr) {	//End of string?
		if (*strptr >=0x61 && *strptr<=0x7a) *strptr-=0x20;	//If char is lower case, convert to upper.
		strptr++;		//Next char
	}
	return str;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Wcsupr
//
// Description:
//  CHAR16* Wcsupr(IN CHAR16 *str) converts a CHAR16 string to all uppercase
// characters.
//
// Input:
//  IN CHAR16 *str
// CHAR16 string to be converted to all uppercase.
//
// Output:
//  CHAR16* string pointer to the start of the modified string.
//
// Modified:
//
// Referrals:
// 
// Notes:	
//  Original string will be modified!
// 
//<AMI_PHDR_END>
//*************************************************************************
CHAR16 * Wcsupr(CHAR16 *str) {
	CHAR16 *strptr=str;
	while (*strptr) {	//End of string?
		if (*strptr >=0x61 && *strptr<=0x7a) *strptr-=0x20;	//If char is lower case, convert to upper.
		strptr++;		//Next char
	}
	return str;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Strstr
//
// Description:
//  CHAR8* Strstr(IN CONST CHAR8 *string, IN CONST CHAR8 *strSearch) locates
// strSearch within string, and returns a pointer to the start of the located
// string.
//
// Input:
//  IN CONST CHAR8 *string
// The string to be searched through.
//
//  IN CONST CHAR8 *strSearch 
// The string to locate/find.
//
// Output:
//  CHAR8* string pointer to the start of the located string if strSearch is 
// located.  If not found, returns a pointer to the end of the input string.
// If strSearch is an empty null-terminated string, returns the start of
// string.
//
// Modified:
//
// Referrals:
// 
// Notes:	
// 
//<AMI_PHDR_END>
//*************************************************************************
char *Strstr(
   const char *string,
   const char *strSearch 
)
{
  char *StartPos = (char *)string;

  if (!*strSearch) return StartPos;

  while (*StartPos)
  {
    char *p = StartPos;
    char *q = (char *) strSearch;

    while (*q && *p == *q) {++p;++q;}
    if (!*q) return StartPos;
    ++StartPos;
  }
  return StartPos;
}

//*************************************************************************
//
// Name: Sprintf_va_list
//
// Description:
//  UINTN Sprintf_va_list(OUT CHAR8 *buffer, IN CHAR8 *format,
// IN va_list arg) produces a null-terminated ASCII string in the output
// buffer.  The ASCII string is produced by parsing the format string
// specified by format.  Arguments are pulled from the variable argument
// list, specified by arg, based on the contents of the format string.  The
// number of ASCII characters in the produced output buffer is returned, not
// including the null-terminator.
//
// Input:
//  buffer - Pointer to a null-terminated output ASCII string buffer.  
//  User is responsible for allocating the necessary memory resources!
//
//  BufferSize - Size of the buffer in bytes.
//
//  format - Pointer to a null-terminated format ASCII string.
//
//  arg - Marker for the variable argument list.
//
// Output:
//  UINTN - number of ASCII characters in the produced output buffer, not
//  including the null-terminator.
//
// Referrals:
//  va_arg
//  Strtol
//  Sprintf
//  Strupr
//  I64toa
//  Itoa
//  Strlen
// 
// Notes: Refer to Sprintf function for format string syntax.
//
//*************************************************************************
UINTN Sprintf_va_list( char *buffer, char *format, va_list arg )
{
    return Sprintf_s_va_list( buffer, 0, format, arg );
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Sprintf
//
// Description:
//  UINTN Sprintf(OUT CHAR8 *Buffer, IN CHAR8 *Format, IN ...) produces a
// null-terminated ASCII string in the output Buffer.  The ASCII string is
// produced by parsing the format string specified by Format.  Arguments are 
// pulled from the variable argument list based on the contents of the format
// string.  The number of ASCII characters in the produced output buffer is
// returned, not including the null-terminator. See notes for format string 
// information.
//
// Input:
//  OUT CHAR8 *Buffer
// Pointer to a null-terminated output ASCII string buffer.  User is
// responsible for allocating the necessary memory resources!
//
//  IN CHAR8 *Format
// Pointer to a null-terminated format ASCII string.
//
//  IN ...
// Variable argument list which provides the data/variables used within the
// format string.
//
// Output:
//  UINTN number of ASCII characters in the produced output buffer, not
// including the null-terminator.
//
// Modified:
//
// Referrals:
//  va_start
//  Sprintf_va_list
//  va_end
// 
// Notes:
//  Objects inside the format string have the following syntax.
//   %[flags][width]type
//
//  *** [flags] ***
//  
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
// . Flag  .  Description
// .       . 
// . -     .  The field is left justified.  If flag is not specified, then 
// .       . the field is right justified.
// .       . 
// . space .  Prefix a space character to a number.  Only valid for types X, 
// .       . x, and d.
// .       .  
// . +     .  Prefix a plus character to a number.  Only valid for types X,
// .       . x, and d.  If both space and `+' are specified, then space is
// .       . ignored.
// .       .
// . 0     .  Pad with `0' characters to the left of a number.  Only valid
// .       . for types X, x, and d.
// .       .
// . L, l  .  The number being printed is a UINT64.  Only valid for types X,
// .       . x, and d.  If this flag is not specified, then the number being
// .       . printed is an int.
// .       .
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
//  
//  NOTE
//   All invalid [flags] are ignored.
//
// *** [width] ***
//
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
// . Width  .  Description
// .        .
// . *      .  The width of the field is specified by a UINTN argument in the
// .        . argument list.
// .        .
// . Number .  The number specified as a decimal value represents the width of
// .        . the field.
// .        .
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
//
// NOTE
//  If [width] is not specified, then a field width of 0 is assumed.
//
// *** type ***
// 
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
// . Type  .  Description
// .       .
// . %     .  Print a `%'.
// .       .
// . c     .  The argument is an ASCII character. 
// .       .
// . x     .  The argument is a hexadecimal number.  The characters used are 
// .       . 0..9 and a..f.  If the flag `l' is not specified, then the
// .       . argument is assumed to be an int.
// .       .
// . X     .  The argument is a hexadecimal number.  The characters used are 
// .       . 0..9 and A..F.  If the flag `l' is not specified, then the
// .       . argument is assumed to be an int.
// .       .
// . d     .  The argument is a decimal number.  If the flag `l' is not
// .       . specified, then the argument is assumed to be an int.
// .       .
// . i     .  The same as `d'.
// .       .
// . s     .  The argument is a pointer to null-terminated ASCII string.
// .       .
// . a     .  The same as `s'.
// .       .
// . S     .  The argument is a pointer to a null-terminated Unicode string.
// .       .
// . g     .  The argument is a pointer to a GUID structure.  The GUID is
// .       . printed in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
// .       .
// . G     .  The argument is a pointer to a GUID structure.  The GUID is
// .       . printed in the format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
// .       .
// . r     .  The argument is an EFI_STATUS value.  This value is converted
// .       . to a string.
// .       .
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
//
// NOTE
//  All invalid type characters are copied into the result string.
//
//<AMI_PHDR_END>
//*************************************************************************
UINTN Sprintf( char *Buffer, char *Format, ...)
{
    va_list	ArgList = va_start(ArgList,Format);
    UINTN Ret = Sprintf_va_list( Buffer, Format, ArgList );
    va_end(ArgList);
    return Ret;
}

char* GetStatusCodeString(EFI_STATUS Status){
    static char* EfiSuccess="EFI_SUCCESS";

    if (Status == 0) return EfiSuccess;
    if ((Status & EFI_ERROR_BIT) == 0 ){//warning
        if ((Status & PI_WARNING_BIT) == PI_WARNING_BIT ){//PI warning
            if (Status >= NUM_OF_PI_WARNINGS) return NULL;
            return PiWarningStatus[Status];
        }else{//UEFI warning
            if (Status > NUM_OF_EFI_WARNINGS) return NULL;
            return EfiWarningStatus[Status-1];
        }
    }else{//error
        UINTN Index = Status & ~(EFI_ERROR_BIT | PI_ERROR_BIT | OEM_ERROR_BIT);
        if ((Status & PI_ERROR_BIT) == PI_ERROR_BIT ){//PI error
            if (Index >= NUM_OF_PI_ERRORS) return NULL;
            return PiErrorStatus[Index];
        }else if ((Status & OEM_ERROR_BIT) == OEM_ERROR_BIT ){//OEM error
            if (Index > NUM_OF_AMI_ERRORS) return NULL;
            return AmiErrorStatus[Index-1];
        }else{//UEFI error
            if (Index > NUM_OF_EFI_ERRORS) return NULL;
            return EfiErrorStatus[Index-1];
        }

    }     
}

//*************************************************************************
//
// Name: Sprintf_s_va_list
//
// Description:
//  UINTN Sprintf_s_va_list(OUT CHAR8 *Buffer, IN UINTN BufferSize,
// IN CHAR8 *Format, IN va_list Marker) produces a null-terminated ASCII
// string in the output Buffer of size BufferSize.  The ASCII string is
// produced by parsing the format string specified by Format.  Arguments are
// pulled from the variable argument list, Marker, based on the contents of
// the format string.  The number of ASCII characters in the produced output
// buffer is returned, not including the null-terminator.
//
// Input:
//  OUT CHAR8 *Buffer
// Pointer to a null-terminated output ASCII string buffer.  User is
// responsible for allocating the necessary memory resources!
//
//  IN UINTN BufferSize
// The size, in bytes, of the output Buffer.
//
//  IN CHAR8 *Format
// Pointer to a null-terminated format ASCII string.
//
//  IN va_list Marker
// Marker for the variable argument list.
//
// Output:
//  UINTN number of ASCII characters in the produced output buffer, not
// including the null-terminator.
//
// Modified:
//
// Referrals:
//  Sprintf_va_list
//  MemCpy
// 
// Notes:	
//  This is a helper function for Sprintf_s.  Refer to Sprintf function for
// format string syntax.
// 
//*************************************************************************
UINTN Sprintf_s_va_list(char *Buffer, UINTN BufferSize, char *Format, va_list Marker)
{
	char filler;
	int width;
	char numbuffer[32];
	UINTN strlength;
	char *strp = Format;
	char *buffp = Buffer;
	char *_strp, *str2p;
	int	radix;
    BOOLEAN its_int64;
    UINTN n;

    if (Buffer==NULL || Format==NULL) return -1;
    //If BuuferSize is 0, no size check required
    while(BufferSize!=1 && *strp) {
        if (*strp != '%'){
            *buffp++ = *strp++;
            BufferSize--;
            continue;
        }
        strp++; //skip %
        if (*strp=='%') {
            strp++;
            *buffp++ = '%';
            BufferSize--;
            continue;
        }
        
        filler = ' ';
    
        //Get flags
        if (*strp == '0') { 
            filler = '0'; 
            *strp++;
        }

        //Get Width
        if (*strp == '*') { //width is supplied by argument.
            strp++;
            width = va_arg(Marker,int);
        } else {
            width = Strtol(strp,&_strp,10);	//If no width, then 0 returned.
            strp=_strp;
        }
    
        //Get type.
        if (*strp == 's' || *strp == 'a'/*to be compatible with Intel Print library*/) { //string
            char *str = va_arg(Marker,char *);
            while (*str) {
                if ( --BufferSize == 0 ) {
                    *buffp = 0; // null terminator
                    return buffp - Buffer;
                }
                *buffp++ = *str++;
            }
            ++strp;
            continue;
        }
        
        if (*strp == 'S') { // unicode string
            CHAR16 *str = va_arg(Marker,CHAR16 *);
            while (*str) {
                if ( --BufferSize == 0 ) {
                    *buffp = 0; // null terminator
                    return buffp - Buffer;
                }
                *buffp++ = (char)*str++;
            }
            ++strp;
            continue;
        }
        
        if (*strp == 'c') { //character
            *buffp++ = va_arg(Marker,char);
            ++strp;
            continue;
        }
        
        if ((*strp & 0xdf) == 'G') {     //'G' or 'g'
            EFI_GUID *guid = va_arg(Marker,EFI_GUID*);
            CHAR8 *origp = buffp;

            n = Sprintf_s( 
                buffp,
                BufferSize,
                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
                guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
                guid->Data4[5], guid->Data4[6], guid->Data4[7]
            );
            buffp += n;
            if (*strp == 'G') 
                Strupr(origp);
            BufferSize -= n;
            ++strp;
            continue;
        }
        
        if (*strp == 'r') {
            EFI_STATUS Status = va_arg(Marker,EFI_STATUS);
            char *StatusCodeString = GetStatusCodeString(Status);

            if (StatusCodeString==NULL)
                n = Sprintf_s( buffp, BufferSize, "%s(%X)", EfiUnknown, Status );
            else
                n = Sprintf_s( buffp, BufferSize, "%s", StatusCodeString );                

            buffp += n;
            BufferSize -= n;
            ++strp;
            continue;
        }
        
        if (*strp == 'l') { 
            strp++; 
            its_int64 = TRUE;
        } else 
            its_int64 = FALSE;
    
        if (*strp == 'd' || *strp == 'i') 
            radix = 10;
        else if ( (*strp & 0xdf) == 'X' ) 
            radix = 16; //'X' or 'x'
        else 
            continue;	//invalid *strp
        
        if (its_int64) 
            I64toa( va_arg(Marker,INT64), numbuffer, radix );
        else 
            Itoa( va_arg(Marker,int), numbuffer, radix );
        if (*strp == 'X') 
            Strupr(numbuffer);
        
        strlength = Strlen(numbuffer);
        while ( strlength++ < (unsigned)width ) {
            if ( --BufferSize == 0 ) {
                *buffp = 0; // null terminator
                return buffp - Buffer;
            }
            *buffp++ = filler;
        }
        
        str2p = numbuffer;
        while (*str2p) {
            if ( --BufferSize == 0 ) {
                *buffp = 0; // null terminator
                return buffp - Buffer;
            }
            *buffp++ = *str2p++;
        }
        
        strp++;
    }
    
    *buffp = 0;
    return buffp - Buffer;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Sprintf_s
//
// Description:
//  UINTN Sprintf_s(OUT CHAR8 *Buffer, IN UINTN BufferSize, IN CHAR8 *Format,
// IN ...) produces a null-terminated ASCII string in the output Buffer of
// size BufferSize.  The ASCII string is produced by parsing the format
// string specified by Format.  Arguments are pulled from the variable
// argument list based on the contents of the format string.  The number of
// ASCII characters in the produced output buffer is returned, not including
// the null-terminator.
//
// Input:
//  OUT CHAR8 *Buffer
// Pointer to a null-terminated output ASCII string buffer.  User is
// responsible for allocating the necessary memory resources!
//
//  IN UINTN BufferSize
// The size, in bytes, of the output Buffer.
//
//  IN CHAR8 *Format
// Pointer to a null-terminated format ASCII string.
//
//  IN ...
// Variable argument list of data/variables used within the format string.
//
// Output:
//  UINTN number of ASCII characters in the produced output buffer, not
// including the null-terminator.
//
// Modified:
//
// Referrals:
//  va_start
//  Sprintf_s_va_list
//  va_end
// 
// Notes:	
//  Refer to Sprintf function for format string syntax.
// 
//<AMI_PHDR_END>
//*************************************************************************
UINTN Sprintf_s( char *Buffer, UINTN BufferSize, char *Format, ... )
{
	va_list	ArgList = va_start(ArgList, Format);
	UINTN Ret = Sprintf_s_va_list( Buffer, BufferSize, Format, ArgList );
	va_end(ArgList);
	return Ret;
}

//*************************************************************************
//
// Name: Swprintf_va_list
//
// Description:
//  UINTN Swprintf_va_list(OUT CHAR16 *buffer, IN CHAR16 *format,
// IN va_list arg) produces a null-terminated Unicode string in the output
// buffer.  The Unicode string is produced by parsing the format string
// specified by format.  Arguments are pulled from the variable argument list
// specified by arg based on the contents of the format string.  The number 
// of Unicode characters in the produced output buffer is returned, not
// including the null-terminator.
//
// Input:
//  OUT CHAR16 *buffer
// Pointer to a null-terminated output Unicode string buffer.  User is
// responsible for allocating the necessary memory resources!
//
//  IN CHAR16 *format
// Pointer to a null-terminated format Unicode string.
//
//  IN va_list arg
// Marker for the variable argument list.
//
// Output:
//  UINTN number of Unicode characters in the produced output buffer, not
// including the null-terminator.
//
// Modified:
//
// Referrals:
//  va_arg
//  Wcstol
//  Swprintf
//  Wcsupr
//  I64tow
//  Itow
//  Wcslen
// 
// Notes:	
//  Refer to Sprintf function for format string syntax.
// 
//*************************************************************************
UINTN Swprintf_va_list(CHAR16 *buffer, CHAR16 *format, va_list arg)
{
    return Swprintf_s_va_list( buffer, 0, format, arg );
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Swprintf
//
// Description:
//  UINTN Swprintf(OUT CHAR16 *Buffer, IN CHAR16 *Format, IN ...) produces a
// null-terminated Unicode string in the output Buffer. The Unicode string is
// produced by parsing the format string specified by Format.  Arguments are
// pulled from the variable argument list based on the contents of the format
// string.  The number of Unicode characters in the produced output buffer is
// returned, not including the null-terminator.  
//
// Input:
//  OUT CHAR16 *Buffer
// Pointer to a null-terminated output Unicode string buffer.  User is
// responsible for allocating the necssary memory resources!
//
//  IN CHAR16 *Format
// Pointer to a null-terminated format Unicode string.
//
//  IN ...
// Variable arguement list of data/variables used within the format string.
//
// Output:
//  UINTN number of Unicode characters in the produced output buffer, not
// including the null-terminator.
//
// Modified:
//
// Referrals:
//  va_start
//  Swprintf_va_list
//  va_end
// 
// Notes:	
//  See Sprintf function for format string details.
//
//<AMI_PHDR_END>
//*************************************************************************
UINTN Swprintf(CHAR16 *Buffer, CHAR16 *Format, ...)
{
	va_list	ArgList = va_start(ArgList,Format);
	UINTN Ret = Swprintf_va_list(Buffer,Format,ArgList);
	va_end(ArgList);
	return Ret;
}

//*************************************************************************
//
// Name: Swprintf_s_va_list
//
// Description:
//  UINTN Swprintf_s_va_list(OUT CHAR16 *Buffer, IN UINTN BufferSize,
// IN CHAR16 *Format, IN va_list Marker) produces a null-terminated Unicode
// string in the output Buffer of size BufferSize.  The Unicode string is
// produced by parsing the format string specified by Format.  Arguments are
// pulled from the variable argument list based on the contents of the format
// string.  The number of Unicode characters in the produced output buffer is 
// returned, not including the null-terminator.
//
// Input:
//  OUT CHAR16 *Buffer
// Pointer to a null-terminated output Unicode string buffer.  User is
// responsible for allocating the necessary memory resources!
//
//  IN UINTN BufferSize
// The size, in bytes, of the output Buffer.
//
//  IN CHAR16 *Format
// Pointer to a null-terminated format Unicode string.
//
//  IN va_list Marker
// Marker for the variable argument list.
//
// Output:
//  UINTN number of ASCII characters in the produced output buffer, not
// including the null-terminator.
//
// Modified:
//
// Referrals:
//  Swprintf_va_list
//  MemCpy
// 
// Notes:	
//  This is a helper function for Swprintf_s.  Refer to Sprintf function for
// format string syntax.
// 
//*************************************************************************
UINTN Swprintf_s_va_list(CHAR16 *Buffer, UINTN BufferSize, CHAR16 *Format, va_list Marker)
{
    CHAR16 filler;
    int width;
    CHAR16 numbuffer[32];
    UINTN strlength;
    CHAR16 *strp = Format;
    CHAR16 *buffp = Buffer;
    CHAR16 *_strp, *str2p;
    int radix;
    BOOLEAN its_int64;
    UINTN n;

    if (Buffer==NULL || Format==NULL) return -1;
    //If BuuferSize is 0, no size check required
    while(BufferSize!=1 && *strp) {
        if (*strp != '%'){
            *buffp++ = *strp++;
            BufferSize--;
            continue;
        }
        strp++; //skip %
        if (*strp=='%') {
            strp++;
            *buffp++ = '%';
            BufferSize--;
            continue;
        }
        
        filler = ' ';
    
        //Get flags
        if (*strp == '0') { 
            filler = '0'; 
            *strp++;
        }

        //Get Width
        if (*strp == '*') { //width is supplied by argument.
            strp++;
            width = va_arg(Marker, int);
        } else {
            width = Wcstol( strp, &_strp, 10 );    //If no width, then 0 returned.
            strp=_strp;
        }
    
        //Get type.
        if (*strp == 's') { // unicode string
            CHAR16 *str = va_arg(Marker, CHAR16*);
            while (*str) {
                if ( --BufferSize == 0 ) {
                    *buffp = 0; // null terminator
                    return buffp - Buffer;
                }
                *buffp++ = *str++;
            }
            ++strp;
            continue;
        }

        if ( *strp == 'S' || *strp == 'a' /*to be compatible with Intel Print library*/ ) { //string
            char *str = va_arg(Marker, char *);
            while (*str) {
                if ( --BufferSize == 0 ) {
                    *buffp = 0; // null terminator
                    return buffp - Buffer;
                }
                *buffp++ = *str++;
            }
            ++strp;
            continue;
        }

        if (*strp == 'c') { //character
            *buffp++ = va_arg(Marker, CHAR16);
            ++strp;
            continue;
        }

        if ((*strp & 0xdf) == 'G') {     //'G' or 'g'
            EFI_GUID *guid = va_arg(Marker, EFI_GUID*);
            CHAR16 *origp = buffp;
            n = Swprintf_s(
                buffp, 
                BufferSize,
                L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
                guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
                guid->Data4[5], guid->Data4[6], guid->Data4[7]
            );
            buffp += n;
            if (*strp == 'G') 
                Wcsupr(origp);
            BufferSize -= n;
            ++strp;
            continue;
        }

        if (*strp == 'r') {
            EFI_STATUS Status = va_arg(Marker, EFI_STATUS);
            char *StatusCodeString = GetStatusCodeString(Status);

            if (StatusCodeString==NULL)
                n = Swprintf_s( buffp, BufferSize, L"%S(%X)", EfiUnknown, Status );
            else
                n = Swprintf_s( buffp, BufferSize, L"%S", StatusCodeString );                

            buffp += n;
            BufferSize -= n;
            ++strp;
            continue;
        }

        if (*strp == 'l') { 
            strp++; 
            its_int64 = TRUE;
        } else 
            its_int64 = FALSE;
    
        if (*strp == 'd' || *strp == 'i') 
            radix = 10;
        else if ((*strp & 0xdf) == 'X') 
            radix = 16;  //'X' or 'x'
        else 
            continue;    //invalid *strp

        if (its_int64) 
            I64tow( va_arg(Marker,INT64), numbuffer, radix );
        else 
            Itow( va_arg(Marker,int), numbuffer, radix );
        if (*strp == 'X') 
            Wcsupr(numbuffer);

        strlength = Wcslen(numbuffer);
        while ( strlength++ < (unsigned)width ) {
            if ( --BufferSize == 0 ) {
                *buffp = 0; // null terminator
                return buffp - Buffer;
            }
            *buffp++ = filler;
        }

        str2p = numbuffer;
        while (*str2p) {
            if ( --BufferSize == 0 ) {
                *buffp = 0; // null terminator
                return buffp - Buffer;
            }
            *buffp++ = *str2p++;
        }
            
        strp++;
    }
    
    *buffp = 0;
    return buffp - Buffer;
}

//*************************************************************************
//<AMI_PHDR_START>
//
// Name: Swprintf_s
//
// Description:
//  UINTN Swprintf_s(OUT CHAR16 *Buffer, IN UINTN BufferSize,
// IN CHAR16 *Format, IN ...) produces a null-terminated Unicode string in
// the output Buffer of size BufferSize.  The Unicode string is produced by
// parsing the format string specified by Format.  Arguments are pulled from
// the variable argument list based on the contents of the format string.
// The number of Unicode characters in the produced output buffer is
// returned, not including the null-terminator.
//
// Input:
//  OUT CHAR16 *Buffer
// Pointer to a null-terminated output Unicode string buffer.  User is
// responsible for allocating the necessary memory resources!
//
//  IN UINTN BufferSize
// The size, in bytes, of the output Buffer.
//
//  IN CHAR16 *Format
// Pointer to a null-terminated format Unicode string.
//
//  IN ...
// Variable argument list of data/variables used within the format string.
//
// Output:
//  UINTN number of Unicode characters in the produced output buffer, not
// including the null-terminator.
//
// Modified:
//
// Referrals:
//  va_start
//  Swprintf_s_va_list
//  va_end
// 
// Notes:	
//  Refer to Sprintf function for format string syntax.
// 
//<AMI_PHDR_END>
//*************************************************************************
UINTN Swprintf_s(CHAR16 *Buffer, UINTN BufferSize, CHAR16 *Format, ...)
{
	va_list	ArgList = va_start(ArgList,Format);
	UINTN Ret = Swprintf_s_va_list(Buffer,BufferSize,Format,ArgList);
	va_end(ArgList);
	return Ret;
}
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************