summaryrefslogtreecommitdiff
path: root/EDK/MiniSetup/BootOnly/EDKhelper.c
blob: b0aabd9397f2ea3fc6c449e33beb44dff0ad1ec2 (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
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
//**                                                             **//
//**         (C)Copyright 2010, American Megatrends, Inc.        **//
//**                                                             **//
//**                     All Rights Reserved.                    **//
//**                                                             **//
//**   5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093  **//
//**                                                             **//
//**                     Phone (770)-246-8600                    **//
//**                                                             **//
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
// $Archive: /Alaska/SOURCE/Modules/AMITSE2_0/AMITSE/BootOnly/EDKhelper.c $
//
// $Author: Arunsb $
//
// $Revision: 11 $
//
// $Date: 2/11/14 8:14p $
//
//*****************************************************************//
//*****************************************************************//
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/AMITSE2_0/AMITSE/BootOnly/EDKhelper.c $
// 
// 11    2/11/14 8:14p Arunsb
// [TAG]	EIP125719
// [Category]	Improvement
// [Description]	The EfiCreateEventReadyToBoot is conflicted under
// UefiLib.h and EDKhelper.h.
// [Files]	boot.c, EDKhelper.c and EDKHelper.h
// 
// 10    5/22/13 10:26a Arunsb
// [TAG]  		EIP122907
// [Category]  	Improvement
// [Description]  	Functions added for name value support
// 
// 9     10/18/12 5:59a Arunsb
// Updated for 2.16.1235 QA submission
// 
// 11    10/10/12 12:36p Arunsb
// Synched the source for v2.16.1232, backup with Aptio
// 
// 8     5/29/12 3:58a Arunsb
// [TAG]  		EIP91109
// [Category]  	Improvement
// [Description]  	Sync the Aptio IV source for AptioV
// 
// 7     6/17/10 2:59p Madhans
// Dynamic parsing support in TSE.
// 
// 6     4/23/10 6:22p Madhans
// To resolve Build issues with EDK support on.
// 
// 5     2/19/10 1:01p Madhans
// Updated for TSE 2.01. Refer Changelog.log for File change history.
// 
// 7     2/19/10 8:14a Mallikarjunanv
// updated year in copyright message
// 
// 6     1/09/10 5:49a Mallikarjunanv
// Updated TSE2.01 Release sources with coding standards
// 
// 5     7/31/09 6:27p Presannar
// Removed Redefinition of gEfiOEMBadgingProtocolGuid, EfiCopyMem
// 
// 4     6/24/09 6:09p Madhans
// Made TSE_USE_EDK_LIBRARY=OFF to not to refer EDK module.
// 
// 3     6/23/09 6:56p Blaines
// Coding standard update, 
// Remove spaces from file header to allow proper chm function list
// creation.
// 
// 2     6/12/09 7:41p Presannar
// Initial implementation of coding standards
// 
// 1     6/04/09 8:05p Madhans
// 
// 2     5/07/09 10:35a Madhans
// Changes after Bin module
// 
// 3     5/06/09 8:09p Mallikarjunanv
// updated for if no edk lib support
// 
// 2     4/28/09 9:39p Madhans
// Tse 2.0 Code complete Checkin.
// 
// 1     3/31/09 3:33p Madhans
// To build with/without EDK libs.
//
//*****************************************************************//
//*****************************************************************//

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:		EdkHelper.c
//
// Description:	This file contains code from EDK library.
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

/*++

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

Module Name:

  xxxxxx.c

Abstract:

  xxxxxxxxxxxx

--*/

#include "minisetup.h"

#if !TSE_USE_EDK_LIBRARY

extern EFI_SYSTEM_TABLE 	*pST;
extern EFI_BOOT_SERVICES 	*pBS;
extern EFI_RUNTIME_SERVICES 	*pRS;

#if !TSE_APTIO_5_SUPPORT
EFI_SYSTEM_TABLE 	*gST=NULL;
EFI_BOOT_SERVICES 	*gBS=NULL;
EFI_RUNTIME_SERVICES 	*gRT=NULL;
EFI_GUID  gEfiGlobalVariableGuid = EFI_GLOBAL_VARIABLE_GUID;
EFI_GUID gEfiEventReadyToBootGuid = EFI_EVENT_GROUP_READY_TO_BOOT;
#endif

#define EFI_SHELL_FILE_GUID  \
  { 0xc57ad6b7, 0x0515, 0x40a8, 0x9d, 0x21, 0x55, 0x16, 0x52, 0x85, 0x4e, 0x37 }
EFI_GUID gEfiShellFileGuid = EFI_SHELL_FILE_GUID;

/*
#define EFI_GLOBAL_VARIABLE_GUID \
  { \
    0x8BE4DF61, 0x93CA, 0x11d2, 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C \
  }
*/

#define EFI_PRINT_PROTOCOL_GUID  \
   { 0xdf2d868e, 0x32fc, 0x4cf0, 0x8e, 0x6b, 0xff, 0xd9, 0x5d, 0x13, 0x43, 0xd0 }

EFI_GUID  gEfiPrintProtocolGuid = EFI_PRINT_PROTOCOL_GUID;

#ifndef EFI_OEM_BADGING_PROTOCOL_GUID
#define EFI_OEM_BADGING_PROTOCOL_GUID \
  { 0x170e13c0, 0xbf1b, 0x4218, 0x87, 0x1d, 0x2a, 0xbd, 0xc6, 0xf8, 0x87, 0xbc }
#endif
//EFI_GUID  gEfiOEMBadgingProtocolGuid = EFI_OEM_BADGING_PROTOCOL_GUID;
extern EFI_GUID  gEfiOEMBadgingProtocolGuid;

/*
#define EFI_EVENT_GROUP_READY_TO_BOOT \
  { 0x7ce88fb3, 0x4bd7, 0x4679, { 0x87, 0xa8, 0xa8, 0xd8, 0xde, 0xe5, 0x0d, 0x2b } }
*/
VOID InitAmiLib(
	IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable
);

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiInitializeDriverLib
//
// Description:	To avoid including EDK libs.
//
// Input:		ImageHandle and Systemtable
//
// Output:		Status
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
EfiInitializeDriverLib (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  )
{
	InitAmiLib(ImageHandle,SystemTable);
	gST = pST;
	gBS = pBS;
	gRT = pRS;
	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiLibAllocateZeroPool
//
// Description:	To avoid including EDK libs.
//
// Input:		size
//
// Output:		pointer
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void* MallocZ(UINTN Size);
VOID *
EfiLibAllocateZeroPool (
  IN  UINTN   AllocationSize
  )
{
	return MallocZ(AllocationSize);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiLibAllocatePool
//
// Description:	To avoid including EDK libs.
//
// Input:		size
//
// Output:		pointer
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void* Malloc(UINTN Size);
VOID *
EfiLibAllocatePool (
  IN  UINTN   AllocationSize
  )
{
	return Malloc(AllocationSize);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiCompareGuid
//
// Description:	To avoid including EDK libs.
//
// Input:		IN EFI_GUID *Guid1,  IN EFI_GUID *Guid2
//
// Output:		BOOLEAN
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
EfiCompareGuid (
  IN EFI_GUID *Guid1,
  IN EFI_GUID *Guid2
  )
{
	return (BOOLEAN)(0==MemCmp((UINT8*)Guid1,(UINT8*)Guid2,sizeof(EFI_GUID)));
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiCompareMem
//
// Description:	To avoid including EDK libs.
//
// Input:		IN VOID *MemOne, IN VOID *MemTwo,
//
// Output:		INTN
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
INTN
EfiCompareMem (
  IN VOID     *MemOne,
  IN VOID     *MemTwo,
  IN UINTN    Length
  )
{
	return MemCmp(MemOne,MemTwo,Length);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	Wcslen
//
// Description:	To avoid including EDK libs.
//
// Input:		CHAR16 *string
//
// Output:		UINTN
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINTN Wcslen(CHAR16 *string);
UINTN
EfiStrLen (
  IN CHAR16   *String
  )
{
	return Wcslen(String);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	Wcscpy
//
// Description:	To avoid including EDK libs.
//
// Input:		CHAR16 *string1, CHAR16* string2
//
// Output:		CHAR16*
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
CHAR16* Wcscpy(CHAR16 *string1, CHAR16* string2);
VOID
EfiStrCpy (
  IN CHAR16   *Destination,
  IN CHAR16   *Source
  )
{
	Wcscpy(Destination,Source);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	Wcscmp
//
// Description:	To avoid including EDK libs.
//
// Input:		CHAR16 *string1, CHAR16 *string2
//
// Output:		int
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
int Wcscmp( CHAR16 *string1, CHAR16 *string2 );
INTN
EfiStrCmp (
  IN CHAR16   *String,
  IN CHAR16   *String2
  )
{
	return Wcscmp( String, String2 );
} 

int Strcmp( char *string1, char *string2 );
INTN
EfiAsciiStrCmp (
  IN CHAR8   *String,
  IN CHAR8   *String2
  )
{
	return Strcmp( String, String2 );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	DPAdd
//
// Description:	To avoid including EDK libs.
//
// Input:		EFI_DEVICE_PATH_PROTOCOL *pDp1, EFI_DEVICE_PATH_PROTOCOL *pDp2
//
// Output:		VOID*
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID* DPAdd(EFI_DEVICE_PATH_PROTOCOL *pDp1, EFI_DEVICE_PATH_PROTOCOL *pDp2);

EFI_DEVICE_PATH_PROTOCOL *
EfiAppendDevicePath (
  IN EFI_DEVICE_PATH_PROTOCOL  *Src1,
  IN EFI_DEVICE_PATH_PROTOCOL  *Src2
  )
{
	return (EFI_DEVICE_PATH_PROTOCOL *)DPAdd(Src1,Src2);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	DPAddNode
//
// Description:	To avoid including EDK libs.
//
// Input:		EFI_DEVICE_PATH_PROTOCOL *pDp1, EFI_DEVICE_PATH_PROTOCOL *pDp2
//
// Output:		VOID*
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID* DPAddNode(EFI_DEVICE_PATH_PROTOCOL *pDp1, EFI_DEVICE_PATH_PROTOCOL *pDp2);

EFI_DEVICE_PATH_PROTOCOL        *
EfiAppendDevicePathNode (
  IN EFI_DEVICE_PATH_PROTOCOL  *Src1,
  IN EFI_DEVICE_PATH_PROTOCOL  *Src2
  )
{
	return (EFI_DEVICE_PATH_PROTOCOL *)DPAddNode(Src1, Src2);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiDevicePathFromHandle
//
// Description:	To avoid including EDK libs.
//
// Input:		IN EFI_HANDLE  Handle
//
// Output:		EFI_DEVICE_PATH_PROTOCOL *
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_DEVICE_PATH_PROTOCOL *
EfiDevicePathFromHandle (
  IN EFI_HANDLE  Handle
  )
/*++

Routine Description:

  Get the device path protocol interface installed on a specified handle.

Arguments:

  Handle  - a specified handle

Returns:

  The device path protocol interface installed on that handle.

--*/
{
  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;

  DevicePath = NULL;
  gBS->HandleProtocol (
        Handle,
        &gEfiDevicePathProtocolGuid,
        (VOID *) &DevicePath
        );
  return DevicePath;
}

VOID
EfiStrCat (
  IN CHAR16   *Destination,
  IN CHAR16   *Source
  )
/*++

Routine Description:
  Concatinate Source on the end of Destination

Arguments:
  Destination - String to added to the end of.
  Source      - String to concatinate.

Returns:
  NONE

--*/
{   
  EfiStrCpy (Destination + EfiStrLen (Destination), Source);
}

INTN
EfiStrnCmp (
  IN CHAR16   *String,
  IN CHAR16   *String2,
  IN UINTN    Length
  )
/*++

Routine Description:
  This function compares the Unicode string String to the Unicode
  string String2 for len characters.  If the first len characters
  of String is identical to the first len characters of String2,
  then 0 is returned.  If substring of String sorts lexicographically
  after String2, the function returns a number greater than 0. If
  substring of String sorts lexicographically before String2, the
  function returns a number less than 0.

Arguments:
  String  - Compare to String2
  String2 - Compare to String
  Length  - Number of Unicode characters to compare

Returns:
  0     - The substring of String and String2 is identical.
  > 0   - The substring of String sorts lexicographically after String2
  < 0   - The substring of String sorts lexicographically before String2

--*/
{
  while (*String && Length != 0) {
    if (*String != *String2) {
      break;
    }
    String  += 1;
    String2 += 1;
    Length  -= 1;
  }
  return Length > 0 ? *String - *String2 : 0;
}


UINTN
EfiStrSize (
  IN CHAR16   *String
  )
/*++

Routine Description:
  Return the number bytes in the Unicode String. This is not the same as
  the length of the string in characters. The string size includes the NULL

Arguments:
  String - String to process

Returns:
  Number of bytes in String

--*/
{
  return ((EfiStrLen (String) + 1) * sizeof (CHAR16));
}


#define AMI_SIZE_OF_FILEPATH_DEVICE_PATH STRUCT_OFFSET(FILEPATH_DEVICE_PATH,PathName)

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiFileDevicePath
//
// Description:	To avoid including EDK libs.
//
// Input:		IN EFI_HANDLE Device  OPTIONAL, IN CHAR16 *FileName
//
// Output:		EFI_DEVICE_PATH_PROTOCOL *
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_DEVICE_PATH_PROTOCOL *
EfiFileDevicePath (
  IN EFI_HANDLE               Device  OPTIONAL,
  IN CHAR16                   *FileName
  )
/*++

Routine Description:

  This function allocates a device path for a file and appends it to an existiong
  device path.

Arguments:
  Device     - A pointer to a device handle.

  FileName   - A pointer to a Null-terminated Unicodestring.

Returns:
  A device path contain the file name.

--*/
{
  UINTN                     Size;
  FILEPATH_DEVICE_PATH      *FilePath;
  EFI_DEVICE_PATH_PROTOCOL  *Eop;
  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;

  for (Size = 0; FileName[Size] != 0; Size++)
    ;
  Size        = (Size + 1) * 2;

  FilePath    = EfiLibAllocateZeroPool (Size + AMI_SIZE_OF_FILEPATH_DEVICE_PATH + sizeof (EFI_DEVICE_PATH_PROTOCOL));

  DevicePath  = NULL;

  if (FilePath != NULL) {

    //
    // Build a file path
    //
    FilePath->Header.Type     = MEDIA_DEVICE_PATH;
    FilePath->Header.SubType  = MEDIA_FILEPATH_DP;
    SetDevicePathNodeLength (&FilePath->Header, Size + AMI_SIZE_OF_FILEPATH_DEVICE_PATH);
    EfiCopyMem (FilePath->PathName, FileName, Size);
    Eop = NextDevicePathNode (&FilePath->Header);
    SetDevicePathEndNode (Eop);

    //
    // Append file path to device's device path
    //

    DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) FilePath;
    if (Device != NULL) {
      DevicePath = EfiAppendDevicePath (
                    EfiDevicePathFromHandle (Device),
                    DevicePath
                    );

      gBS->FreePool (FilePath);
    }
  }

  return DevicePath;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiInitializeFwVolDevicepathNode
//
// Description:	To avoid including EDK libs.
//
// Input:		IN  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvDevicePathNode,
//				IN EFI_GUID *NameGuid
//
// Output:		VOID EFIAPI
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
EFIAPI
EfiInitializeFwVolDevicepathNode (
  IN  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH     *FvDevicePathNode,
  IN EFI_GUID                               *NameGuid
  )
/*++

Routine Description:

  Initialize a Firmware Volume (FV) Media Device Path node.
  
  Tiano extended the EFI 1.10 device path nodes. Tiano does not own this enum
  so as we move to UEFI 2.0 support we must use a mechanism that conforms with
  the UEFI 2.0 specification to define the FV device path. An UEFI GUIDed 
  device path is defined for PIWG extensions of device path. If the code 
  is compiled to conform with the UEFI 2.0 specification use the new device path
  else use the old form for backwards compatability.

Arguments:

  FvDevicePathNode  - Pointer to a FV device path node to initialize
  NameGuid          - FV file name to use in FvDevicePathNode

Returns:

  None

--*/
{
//*** AMI PORTING BEGIN ***//
//NEW PIWG Specific Device Path defined here is not in compliance with PI DXE CIS 1.0
//Let's disable it
//See also corresponding change in TianoSpecDevicePath.h
#if 1
//*** AMI PORTING END *****//
  //
  // Use old Device Path that conflicts with UEFI
  //
  FvDevicePathNode->Header.Type     = MEDIA_DEVICE_PATH;
  FvDevicePathNode->Header.SubType  = MEDIA_FV_FILEPATH_DP;
  SetDevicePathNodeLength (&FvDevicePathNode->Header, sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH));
  
#else
  //
  // Use the new Device path that does not conflict with the UEFI
  //
  FvDevicePathNode->Piwg.Header.Type     = MEDIA_DEVICE_PATH;
  FvDevicePathNode->Piwg.Header.SubType  = MEDIA_VENDOR_DP;
  SetDevicePathNodeLength (&FvDevicePathNode->Piwg.Header, sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH));

  //
  // Add the GUID for generic PIWG device paths
  //
  EfiCopyMem (&FvDevicePathNode->Piwg.PiwgSpecificDevicePath, &gEfiFrameworkDevicePathGuid, sizeof(EFI_GUID));

  //
  // Add in the FW Vol File Path PIWG defined inforation
  //
  FvDevicePathNode->Piwg.Type = PIWG_MEDIA_FW_VOL_FILEPATH_DEVICE_PATH_TYPE;

#endif
  EfiCopyMem (&((AMITSE_MEDIA_FW_VOL_FILEPATH_DEVICE_PATH*)FvDevicePathNode)->FvFileName, NameGuid, sizeof(EFI_GUID));
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiLibNamedEventSignal
//
// Description:	To avoid including EDK libs.
//
// Input:		IN EFI_GUID            *Name
//
// Output:		STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
EfiLibNamedEventSignal (
  IN EFI_GUID            *Name
  )
/*++

Routine Description:
  Signals a named event. All registered listeners will run.
  The listeners should register using EfiLibNamedEventListen() function.

  NOTE: For now, the named listening/signalling is implemented
  on a protocol interface being installed and uninstalled.
  In the future, this maybe implemented based on a dedicated mechanism.

Arguments:
  Name - Name to perform the signaling on. The name is a GUID.

Returns:
  EFI_SUCCESS if successfull.

--*/
{
  EFI_STATUS  Status;
  EFI_HANDLE  Handle;

  Handle = NULL;
  Status = gBS->InstallProtocolInterface (
                  &Handle,
                  Name,
                  EFI_NATIVE_INTERFACE,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  Status = gBS->UninstallProtocolInterface (
                  Handle,
                  Name,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	LibReportStatusCode
//
// Description:	To avoid including EDK libs.
//
// Input:		IN EFI_STATUS_CODE_TYPE Type, IN EFI_STATUS_CODE_VALUE Value,
//				IN UINT32 Instance, IN EFI_GUID *CallerId OPTIONAL,
//				IN EFI_STATUS_CODE_DATA *Data OPTIONAL
//
// Output:		STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS LibReportStatusCode(
	IN EFI_STATUS_CODE_TYPE Type, IN EFI_STATUS_CODE_VALUE Value,
	IN UINT32 Instance, IN EFI_GUID *CallerId OPTIONAL,
	IN EFI_STATUS_CODE_DATA *Data OPTIONAL
);

EFI_STATUS
EfiLibReportStatusCode (
  IN EFI_STATUS_CODE_TYPE     Type,
  IN EFI_STATUS_CODE_VALUE    Value,
  IN UINT32                   Instance,
  IN EFI_GUID                 *CallerId OPTIONAL,
  IN EFI_STATUS_CODE_DATA     *Data     OPTIONAL  
  )
{
	return LibReportStatusCode(Type,Value,Instance,CallerId,Data);
}


VOID
EFIAPI
EventNotifySignalAllNullEvent (
  IN EFI_EVENT                Event,
  IN VOID                     *Context
  )
{
  //
  // This null event is a size efficent way to enusre that 
  // EFI_EVENT_NOTIFY_SIGNAL_ALL is error checked correctly.
  // EFI_EVENT_NOTIFY_SIGNAL_ALL is now mapped into 
  // CreateEventEx() and this function is used to make the
  // old error checking in CreateEvent() for Tiano extensions
  // function.
  //
  return;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	TseEfiCreateEventReadyToBoot
//
// Description:	To avoid including EDK libs.
//
// Input:		IN EFI_TPL                      NotifyTpl,
//				IN EFI_EVENT_NOTIFY             NotifyFunction,
//				IN VOID                         *NotifyContext,
//				OUT EFI_EVENT                   *ReadyToBootEvent
//
// Output:		EFI_STATUS
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
EFIAPI
TseEfiCreateEventReadyToBoot (
  IN EFI_TPL                      NotifyTpl,
  IN EFI_EVENT_NOTIFY             NotifyFunction,
  IN VOID                         *NotifyContext,
  OUT EFI_EVENT                   *ReadyToBootEvent
  )
/*++

Routine Description:
  Create a Read to Boot Event.  
  
  Tiano extended the CreateEvent Type enum to add a ready to boot event type. 
  This was bad as Tiano did not own the enum. In UEFI 2.0 CreateEventEx was
  added and now it's possible to not voilate the UEFI specification and use 
  the ready to boot event class defined in UEFI 2.0. This library supports
  the R8.5/EFI 1.10 form and R8.6/UEFI 2.0 form and allows common code to 
  work both ways.

Arguments:
  @param LegacyBootEvent  Returns the EFI event returned from gBS->CreateEvent(Ex)

Return:
  EFI_SUCCESS   - Event was created.
  Other         - Event was not created.

--*/
{
  EFI_STATUS        Status;
  UINT32            EventType;
  EFI_EVENT_NOTIFY  WorkerNotifyFunction;

#if (EFI_SPECIFICATION_VERSION < 0x00020000)

  if (NotifyFunction == NULL) {
    EventType = EFI_EVENT_SIGNAL_READY_TO_BOOT | EFI_EVENT_NOTIFY_SIGNAL_ALL;
  } else {
    EventType = EFI_EVENT_SIGNAL_READY_TO_BOOT;
  }
  WorkerNotifyFunction = NotifyFunction;

  //
  // prior to UEFI 2.0 use Tiano extension to EFI
  //
  Status = gBS->CreateEvent (
                  EventType,
                  NotifyTpl,
                  WorkerNotifyFunction,
                  NotifyContext,
                  ReadyToBootEvent
                  );
#else

  EventType = EFI_EVENT_NOTIFY_SIGNAL;
  if (NotifyFunction == NULL) {
    //
    // CreatEventEx will check NotifyFunction is NULL or not
    //
    WorkerNotifyFunction = EventNotifySignalAllNullEvent;
  } else {
    WorkerNotifyFunction = NotifyFunction;
  }

  //
  // For UEFI 2.0 and the future use an Event Group
  //
  Status = gBS->CreateEventEx (
                  EventType,
                  NotifyTpl,
                  WorkerNotifyFunction,
                  NotifyContext,
                  &gEfiEventReadyToBootGuid,
                  ReadyToBootEvent
                  );
#endif
  return Status;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	IsHexDigit
//
// Description:	To avoid including EDK libs.
//
// Input:	
//
// Output:	
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN
IsHexDigit (
  OUT UINT8      *Digit,
  IN  CHAR16      Char
  )
/*++

  Routine Description:
    Determines if a Unicode character is a hexadecimal digit.
    The test is case insensitive.

  Arguments:
    Digit - Pointer to byte that receives the value of the hex character.
    Char  - Unicode character to test.

  Returns:
    TRUE  - If the character is a hexadecimal digit.
    FALSE - Otherwise.

--*/
{
  if ((Char >= L'0') && (Char <= L'9')) {
    *Digit = (UINT8) (Char - L'0');
    return TRUE;
  }

  if ((Char >= L'A') && (Char <= L'F')) {
    *Digit = (UINT8) (Char - L'A' + 0x0A);
    return TRUE;
  }

  if ((Char >= L'a') && (Char <= L'f')) {
    *Digit = (UINT8) (Char - L'a' + 0x0A);
    return TRUE;
  }

  return FALSE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	HexStringToBuf
//
// Description:	To avoid including EDK libs.
//
// Input:	
//
// Output:	
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
HexStringToBuf (
  IN OUT UINT8                     *Buf,   
  IN OUT UINTN                    *Len,
  IN     CHAR16                    *Str,
  OUT    UINTN                     *ConvertedStrLen  OPTIONAL
  )
/*++

  Routine Description:
    Converts Unicode string to binary buffer.
    The conversion may be partial.
    The first character in the string that is not hex digit stops the conversion.
    At a minimum, any blob of data could be represented as a hex string.

  Arguments:
    Buf    - Pointer to buffer that receives the data.
    Len    - Length in bytes of the buffer to hold converted data.
                If routine return with EFI_SUCCESS, containing length of converted data.
                If routine return with EFI_BUFFER_TOO_SMALL, containg length of buffer desired.
    Str    - String to be converted from.
    ConvertedStrLen - Length of the Hex String consumed.

  Returns:
    EFI_SUCCESS: Routine Success.
    EFI_BUFFER_TOO_SMALL: The buffer is too small to hold converted data.
    EFI_

--*/
{
  UINTN       HexCnt;
  UINTN       Idx;
  UINTN       BufferLength;
  UINT8       Digit;
  UINT8       Byte;

  //
  // Find out how many hex characters the string has.
  //
  for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, Str[Idx]); Idx++, HexCnt++);

  if (HexCnt == 0) {
    *Len = 0;
    return EFI_SUCCESS;
  }
  //
  // Two Unicode characters make up 1 buffer byte. Round up.
  //
  BufferLength = (HexCnt + 1) / 2; 

  //
  // Test if  buffer is passed enough.
  //
  if (BufferLength > (*Len)) {
    *Len = BufferLength;
    return EFI_BUFFER_TOO_SMALL;
  }

  *Len = BufferLength;

  for (Idx = 0; Idx < HexCnt; Idx++) {

    IsHexDigit (&Digit, Str[HexCnt - 1 - Idx]);

    //
    // For odd charaters, write the lower nibble for each buffer byte,
    // and for even characters, the upper nibble.
    //
    if ((Idx & 1) == 0) {
      Byte = Digit;
    } else {
      Byte = Buf[Idx / 2];
      Byte &= 0x0F;
      Byte |= Digit << 4;
    }

    Buf[Idx / 2] = Byte;
  }

  if (ConvertedStrLen != NULL) {
    *ConvertedStrLen = HexCnt;
  }

  return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------------------------------------------------------------------------------
// Procedure:	NameValueHexStringToBuf
//
// Description:	Converts name value string to buffer
//
// Input:	: 	Buf    - Pointer to buffer that receives the data.
//			Len    - Length in bytes of the buffer to hold converted data.
//            		If routine return with EFI_SUCCESS, containing length of converted data.
//            		If routine return with EFI_BUFFER_TOO_SMALL, containg length of buffer desired.
//			Str    - String to be converted from.
//			ConvertedStrLen - Length of the Hex String consumed.
//
// Output:	EFI_SUCCESS: Routine Success.
//			EFI_BUFFER_TOO_SMALL: The buffer is too small to hold converted data.
//
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT8 HexToNibbleChar (IN UINT8 Nibble);
EFI_STATUS NameValueHexStringToBuf (
  IN OUT UINT8                     *Buf,   
  IN OUT UINTN                    *Len,
  IN     CHAR16                    *Str,
  OUT    UINTN                     *ConvertedStrLen  OPTIONAL
  )
{
  UINTN       HexCnt;
  UINTN       Idx, ByteIndex = 0;
  UINTN       BufferLength;
  UINT8       Digit;
  UINT8       Byte;
  UINT64	  Ext8byte = 0;
  UINT32	  Ext4byte = 0;
  UINT16      Word;

  //
  // Find out how many hex characters the string has.
  //
  for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, Str[Idx]); Idx++, HexCnt++);

  if (HexCnt == 0) {
    *Len = 0;
    return EFI_SUCCESS;
  }
  //
  // Two Unicode characters make up 1 buffer byte. Round up.
  //
  BufferLength = (HexCnt + 1) / 2; 

  //
  // Test if  buffer is passed enough.
  //
  if (BufferLength > (*Len)) {
    *Len = BufferLength;
    return EFI_BUFFER_TOO_SMALL;
  }

  *Len = BufferLength;

  for (Idx = 0; Idx < HexCnt; Idx += 4)
  {
	  //00 31 00 36 00 30 00 00
	  Ext4byte = *((UINT32 *)&Str[Idx+2]);			//00 31 00 36

	  Word = ((UINT16)(Ext4byte & 0x0000FFFF));		//00 36
	  Byte = ((UINT8)(Word & 0x00FF));				//36
	  Buf [ByteIndex] = HexToNibbleChar (Byte);				//6
	  Buf [ByteIndex] <<= 0x4;						//60
	  
	  Word = ((UINT16)(Ext4byte >> 16));			//00 31
	  Byte = ((UINT8)(Word & 0x00FF));				//31
	  Buf [ByteIndex] |= HexToNibbleChar (Byte);				//1
	  
	  ByteIndex ++;

	  Ext4byte = *((UINT32 *)&Str[Idx]);	//00 30 00 30
	  Word = ((UINT16)(Ext4byte & 0x0000FFFF));	//00 30
	  Byte = ((UINT8)(Word & 0x00FF));				//30
	  Buf [ByteIndex] = HexToNibbleChar (Byte);			//0
	  Buf [ByteIndex] <<= 0x4;						//60

	  Word = ((UINT16)(Ext4byte >> 16));	//00 30
	  Byte = ((UINT8)(Word & 0x00FF));			//30
	  Buf [ByteIndex] |= HexToNibbleChar (Byte);			//0
	  
	  ByteIndex ++;
  }

  if (ConvertedStrLen != NULL) {
    *ConvertedStrLen = HexCnt;
  }

  return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	Mul64
//
// Description:	To avoid including EDK libs.
//
// Input:	
//
// Output:	
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT64 Mul64(
    IN UINT64   Value64,
    IN UINTN	Value32
    );
#if !TSE_APTIO_5_SUPPORT
UINT64
MultU64x32 (
  IN  UINT64  Multiplicand,
  IN  UINTN   Multiplier
  )
{
	return Mul64(Multiplicand,Multiplier);
}
#endif
UINTN DPLength(EFI_DEVICE_PATH_PROTOCOL *pDp);

UINTN
EfiDevicePathSize (
  IN EFI_DEVICE_PATH_PROTOCOL  *DevicePath
  )
{
	return DPLength(DevicePath);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	NibbleToHexChar
//
// Description:	Converts the low nibble of a byte  to hex unicode character.
//
// Input:	Nibble - lower nibble of a byte.
//
// Output:	Hex unicode character.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
CHAR16
NibbleToHexChar (
  IN UINT8      Nibble
  )
{
  Nibble &= 0x0F;
  if (Nibble <= 0x9) {
    return (CHAR16)(Nibble + L'0');
  }

  return (CHAR16)(Nibble - 0xA + L'A');
}


//<AMI_PHDR_START>
//------------------------------------------------------------------------------------
// Procedure:	HexToNibbleChar
//
// Description:	Converts the hex value to character.
//
// Input:	Nibble - lower nibble of a byte.
//
// Output:	Hex unicode character.
//
//--------------------------------------------------------------------------------------
//<AMI_PHDR_END>
UINT8
HexToNibbleChar (
  IN UINT8      Nibble
  )
{
  if (Nibble - L'0' <= 0x9) {
    return (CHAR16)(Nibble - L'0');
  }

  return (CHAR16)(Nibble + 0xA - L'A');
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BufToHexString
//
// Description:	Converts binary buffer to Unicode string.
//					At a minimum, any blob of data could be represented as a hex string.
//
// Input:	Str - Pointer to the string.
//				    HexStringBufferLength - Length in bytes of buffer to hold the hex string. Includes tailing '\0' character.
//                                        If routine return with EFI_SUCCESS, containing length of hex string buffer.
//                                        If routine return with EFI_BUFFER_TOO_SMALL, containg length of hex string buffer desired.
//				    Buf - Buffer to be converted from.
//				    Len - Length in bytes of the buffer to be converted.
//
// Output:	EFI_SUCCESS: Routine success.
//				    EFI_BUFFER_TOO_SMALL: The hex string buffer is too small.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
BufToHexString (
  IN OUT CHAR16                    *Str,
  IN OUT UINTN                     *HexStringBufferLength,
  IN     UINT8                     *Buf,
  IN     UINTN                      Len
  )
{
  UINTN       Idx;
  UINT8       Byte;
  UINTN       StrLen;

  //
  // Make sure string is either passed or allocate enough.
  // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.
  // Plus the Unicode termination character.
  //
  StrLen = Len * 2;
  if (StrLen > ((*HexStringBufferLength) - 1)) {
    *HexStringBufferLength = StrLen + 1;
    return EFI_BUFFER_TOO_SMALL;
  }

  *HexStringBufferLength = StrLen + 1;
  //
  // Ends the string.
  //
  Str[StrLen] = L'\0'; 

  for (Idx = 0; Idx < Len; Idx++) {

    Byte = Buf[Idx];
    Str[StrLen - 1 - Idx * 2] = NibbleToHexChar (Byte);
    Str[StrLen - 2 - Idx * 2] = NibbleToHexChar ((UINT8)(Byte >> 4));
  }

  return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	NameValueBufToHexString
//
// Description:	Converts binary buffer to Unicode string.
//					At a minimum, any blob of data could be represented as a hex string.
//					Used only for name value implementaion
//
// Input:	Str - Pointer to the string.
//				    HexStringBufferLength - Length in bytes of buffer to hold the hex string. Includes tailing '\0' character.
//                                        If routine return with EFI_SUCCESS, containing length of hex string buffer.
//                                        If routine return with EFI_BUFFER_TOO_SMALL, containg length of hex string buffer desired.
//				    Buf - Buffer to be converted from.
//				    Len - Length in bytes of the buffer to be converted.
//
// Output:	EFI_SUCCESS: Routine success.
//				    EFI_BUFFER_TOO_SMALL: The hex string buffer is too small.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
NameValueBufToHexString (
  IN OUT CHAR16          **Str,
  IN OUT UINTN             *HexStringBufferLength,
  IN     UINT8                     *Buf,
  IN     UINTN                    Len
  )
{
  UINT8       Byte;

  //
  // Make sure string is either passed or allocate enough.
  // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.
  // Plus the Unicode termination character.
  //

  if (Len <= sizeof (UINT64))			//For upto 8 bytes we have to take care of endian allignment
  {
	  UINTN       Idx;
	  UINTN       StrLen;
	  CHAR16 	*tempStr = (CHAR16 *)NULL;

	  //
	  // Make sure string is either passed or allocate enough.
	  // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.
	  // Plus the Unicode termination character.
	  //
	  StrLen = Len * 2;
	  *Str = EfiLibAllocateZeroPool ((StrLen + 1) * sizeof (UINT16) );
	  if (NULL == *Str)
	  {
		  return EFI_OUT_OF_RESOURCES;
	  }
	  *HexStringBufferLength = (StrLen + 1) * sizeof (UINT16);			//UefiVarSetNvram needs full size
	  //
	  // Ends the string.
	  //
	  tempStr = *Str;
	  tempStr[StrLen] = L'\0'; 

	  for (Idx = 0; Idx < Len; Idx++) 
	  {
	    Byte = Buf[Idx];
	    tempStr[StrLen - 1 - Idx * 2] = NibbleToHexChar (Byte);
	    tempStr[StrLen - 2 - Idx * 2] = NibbleToHexChar ((UINT8)(Byte >> 4));
	  }
  }
  else
  {
	  CHAR16 *tempBuf = (CHAR16 *)NULL, *tempStr = (CHAR16 *)NULL;
	  UINT32 iIndex = 0, StringHexLength = 0;
	  CHAR16 Word;

	  tempBuf = EfiLibAllocateZeroPool (Len + sizeof (CHAR16));		//If full string is given then NULL wont be there so crashing so added one NULL
	  if (NULL == tempBuf)											//character at end	
	  {
		  return EFI_OUT_OF_RESOURCES;
	  }
	  MemCpy (tempBuf, Buf, Len);
	  //Finding length to allocate
	  while (tempBuf [iIndex])
	  {
		  StringHexLength ++;
		  iIndex ++;
	  }
	  iIndex = 0;
	 *Str = EfiLibAllocateZeroPool ((StringHexLength  *  sizeof (CHAR16) * 4) + sizeof (CHAR16));
	 if (NULL == *Str )
  	 {
		 return EFI_OUT_OF_RESOURCES;
	 }
	 tempStr = *Str;
	  while (tempBuf [iIndex])
	  {
		  Word = tempBuf [iIndex];
		  Byte =  ((UINT8)(Word >> 8));
		  tempStr [iIndex * 4] = NibbleToHexChar ((UINT8)(Byte >> 4));
		  tempStr [iIndex * 4 + 1] = NibbleToHexChar (Byte);
		  Byte = Word & 0x00FF;
		  tempStr [iIndex * 4 + 2] = NibbleToHexChar ((UINT8)(Byte >> 4));
		  tempStr [iIndex * 4 + 3] = NibbleToHexChar (Byte);
		  iIndex ++;
	  }
	  *HexStringBufferLength =  (StringHexLength  *  sizeof (CHAR16) * 4) + sizeof (CHAR16);
	  tempStr [StringHexLength * 4] = L'\0';
	  MemFreePointer ((VOID **)&tempBuf);
  }
  return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	ToLower
//
// Description:	Converts the unicode character of the string from uppercase to lowercase.
//
// Input:	Str        -  String to be converted
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
ToLower (
  IN OUT CHAR16    *Str
  )
{
  CHAR16      *Ptr;
  
  for (Ptr = Str; *Ptr != L'\0'; Ptr++) {
    if (*Ptr >= L'A' && *Ptr <= L'Z') {
      *Ptr = (CHAR16) (*Ptr - L'A' + L'a');
    }
  }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	SwapBuffer
//
// Description:	Swap bytes in the buffer.
//
// Input:	Buffer     -  Binary buffer.
//					BufferSize -  Size of the buffer in bytes.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
SwapBuffer (
  IN OUT UINT8     *Buffer,
  IN UINTN         BufferSize
  )
{
  UINTN  Index;
  UINT8  Temp;
  UINTN  SwapCount;

  SwapCount = BufferSize / 2;
  for (Index = 0; Index < SwapCount; Index++) {
    Temp = Buffer[Index];
    Buffer[Index] = Buffer[BufferSize - 1 - Index];
    Buffer[BufferSize - 1 - Index] = Temp;
  }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	UnicodeToConfigString
//
// Description:	Convert Unicode string to binary representation Config string, e.g.
//					"ABCD" => "0041004200430044". Config string appears in <ConfigHdr> (i.e.
//					"&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
//
// Input:	ConfigString  - Binary representation of Unicode String, <string> := (<HexCh>4)+
//					StrBufferLen  - On input: Length in bytes of buffer to hold the Unicode string.
//       		    Includes tailing '\0' character.
//					On output:
//						If return EFI_SUCCESS, containing length of Unicode string buffer.
//						If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
//					UnicodeString - Original Unicode string.
//
// Output:	EFI_SUCCESS          - Routine success.
//					EFI_BUFFER_TOO_SMALL - The string buffer is too small.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
UnicodeToConfigString (
  IN OUT CHAR16                *ConfigString,
  IN OUT UINTN                 *StrBufferLen,
  IN CHAR16                    *UnicodeString
  )
{
  UINTN       Index;
  UINTN       Len;
  UINTN       BufferSize;
  CHAR16      *String;

  Len = EfiStrLen (UnicodeString);
  BufferSize = (Len * 4 + 1) * sizeof (CHAR16);

  if (*StrBufferLen < BufferSize) {
    *StrBufferLen = BufferSize;
    return EFI_BUFFER_TOO_SMALL;
  }

  *StrBufferLen = BufferSize;
  String        = ConfigString;

  for (Index = 0; Index < Len; Index++) {
    BufToHexString (ConfigString, &BufferSize, (UINT8 *) UnicodeString, 2);

    ConfigString += 4;
    UnicodeString += 1;
  }

  //
  // Add tailing '\0' character
  //
  *ConfigString = L'\0';

  //
  // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
  //
  ToLower (String);  
  return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiLibAllocateCopyPool
//
// Description:	Allocate BootServicesData pool and use a buffer provided by 
//					caller to fill it.
//
// Input:	AllocationSize  - The size to allocate
//					Buffer          - Buffer that will be filled into the buffer allocated
//
// Output:	Pointer of the buffer allocated.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID *
EfiLibAllocateCopyPool (
  IN  UINTN   AllocationSize,
  IN  VOID    *Buffer
  )
{
  VOID  *Memory;

  Memory = NULL;
  gBS->AllocatePool (EfiBootServicesData, AllocationSize, &Memory);
  if (Memory != NULL) {
    gBS->CopyMem (Memory, Buffer, AllocationSize);
  }

  return Memory;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BufferToHexString
//
// Description:	Converts binary buffer to Unicode string in reversed byte order from BufToHexString().
//
// Input:	Str        -  String for output
//					Buffer     -  Binary buffer.
//					BufferSize -  Size of the buffer in bytes.
//
// Output:	EFI_SUCCESS  -  The function completed successfully.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
BufferToHexString (
  IN OUT CHAR16    *Str,
  IN UINT8         *Buffer,
  IN UINTN         BufferSize
  )
{
  EFI_STATUS  Status;
  UINT8       *NewBuffer;
  UINTN       StrBufferLen;

  NewBuffer = EfiLibAllocateCopyPool (BufferSize, Buffer);
  SwapBuffer (NewBuffer, BufferSize);

  StrBufferLen = BufferSize * 2 + 1;
  Status = BufToHexString (Str, &StrBufferLen, NewBuffer, BufferSize);

  gBS->FreePool (NewBuffer);
  //
  // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
  //
  ToLower (Str);
  
  return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	EfiStrStr
//
// Description:	Find a substring.
//
// Input:	String      - Null-terminated string to search.
//					StrCharSet  - Null-terminated string to search for.
//
// Output:	The address of the first occurrence of the matching substring if successful, 
//					or NULL otherwise.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
CHAR16*
 EfiStrStr (
   IN  CHAR16  *String,
   IN  CHAR16  *StrCharSet
   )
 {
   CHAR16 *Src;
   CHAR16 *Sub;
   
   Src = String;
   Sub = StrCharSet;
   
   while ((*String != L'\0') && (*StrCharSet != L'\0')) {
     if (*String++ != *StrCharSet++) {
       String = ++Src;
       StrCharSet = Sub;
     }
   }
   if (*StrCharSet == L'\0') {
     return Src;
   } else {
     return NULL;
   }
 }

UINT64 Shr64(
	IN UINT64 Value,
	IN UINT8 Shift
	);

UINT64 Shl64(
	IN UINT64 Value,
	IN UINT8 Shift
	);
#if !TSE_APTIO_5_SUPPORT
UINT64
LShiftU64 (
  IN UINT64   Operand,
  IN UINTN    Count
  )
{
 UINT8 Shift;

  while(Count)
  {
	Shift = (UINT8)((Count>256)?256:Count);
  	Operand = Shl64(Operand,Shift);
	Count -= Shift;
  }

  return Operand;
}

UINT64
RShiftU64 (
  IN UINT64   Operand,
  IN UINTN    Count
  )
{
 UINT8 Shift;

  while(Count)
  {
	Shift = (UINT8)((Count>256)?256:Count);
  	Operand = Shr64(Operand,Shift);
	Count -= Shift;
  }

  return Operand;
}
#endif
UINT64 Div64 (
	IN UINT64	Dividend,
	IN UINTN	Divisor,	//Can only be 31 bits.
  	OUT UINTN	*Remainder OPTIONAL
  	);

UINT64
AmiTseDivU64x32 (
  IN  UINT64  Dividend,
  IN  UINTN   Divisor,
  OUT UINTN   *Remainder OPTIONAL
  )
{
	return Div64(Dividend,Divisor,Remainder);
}

VOID
EfiDebugAssert (
    IN CHAR8    *FileName,
    IN INTN     LineNumber,
    IN CHAR8    *Description
    )
{
}

VOID
EfiDebugPrint (
IN  UINTN ErrorLevel,
IN  CHAR8 *Format,
...
)
{
}

#define EFI_FORM_BROWSER_PROTOCOL_GUID \
  { \
    0xe5a1333e, 0xe1b4, 0x4d55, 0xce, 0xeb, 0x35, 0xc3, 0xef, 0x13, 0x34, 0x43 \
  }
EFI_GUID  gEfiFormBrowserProtocolGuid = EFI_FORM_BROWSER_PROTOCOL_GUID;

#else //!TSE_USE_EDK_LIBRARY

// To support building when TSE_USE_EDK_LIBRARY = 1 
#ifdef TSE_FOR_APTIO_4_50
const UINTN FlashEmpty = (UINTN)(-FLASH_ERASE_POLARITY);
const UINT32 FlashEmptyNext = (FLASH_ERASE_POLARITY ? 0xffffff : 0);

VOID MemCpy(VOID* pDestination, VOID* pSource, UINTN Count)
{
	gBS->CopyMem (pDestination, pSource, Count);
}
#endif

#endif //#if !TSE_USE_EDK_LIBRARY

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**     5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093     **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************