summaryrefslogtreecommitdiff
path: root/Board/EM/TCG2/Common/AmiTcgPlatformPeiLib.c
blob: b7a3ee13923010920553f785b45ae4d852fdd7ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**        5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093         **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//*************************************************************************
// $Header: /Alaska/SOURCE/Modules/TCG2/Common/AmiTcgPlatform/AmiTcgPlatformPei/AmiTcgPlatformPeiLib.c 3     6/14/14 12:32a Fredericko $
//
// $Revision: 3 $
//
// $Date: 6/14/14 12:32a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/TCG2/Common/AmiTcgPlatform/AmiTcgPlatformPei/AmiTcgPlatformPeiLib.c $
// 
// 3     6/14/14 12:32a Fredericko
// Fix locking of Physical Presense
// 
// 2     6/09/14 4:51p Fredericko
// Changes for SetVariable vulnerability during Runtime
// 
// 1     4/21/14 2:17p Fredericko
// 
// 1     10/08/13 12:04p Fredericko
// Initial Check-In for Tpm-Next module
// 
// 1     7/10/13 5:54p Fredericko
// [TAG]  		EIP120969
// [Category]  	New Feature
// [Description]  	TCG (TPM20)
// 
// 19    3/31/13 7:40p Fredericko
// [TAG]  		EIP118211
// [Category]  	Improvement
// [Description]  	Implement ability to skip Physical presence lock in
// manufacturing mode
// [Files]  		TcgPei.c,  AmiTcgPlatformDxe.c, AmiTcgNvflagSample.c
// 
// [TAG]  		EIP
// [Category]  	Improvement
// [Description]  	Timing policy changes for slower TPMs (ability to skip
// setting of physical presence in PEI to DXE)
// [Files]  	AmiTcgPlatformDxe.c, AmiTcgNvflagSample.c,
// AmiTcgPlatformPei.c
// 
// 18    3/06/13 3:23p Fredericko
// [TAG]  		EIP112717
// [Category]  	Improvement
// [Description]  	Change code to use PCR from token definition file
// instead of using numbers
// [Files]  		xTcgDxe.c
// AmiTcgPlatformPeiLib.c
// AmiTcgPlatformDxe.c
// 
// 17    11/05/12 11:12a Fredericko
// Continue Selftest Vendor ID Token added
// 
// 16    4/27/12 6:16p Fredericko
// 1. Remove unused functions.
// 
// 15    3/19/12 6:47p Fredericko
// Changes for Tcg Performance Metrics Improvement.
// Files Changed: Tcg.sdl, TcgMisc.h, TcgDxe.c, TcgCommon.c, TcgCommon.h, 
// AmiTcgPlatformPeiLib.c, AmiTcgPlatformDxe.c, TcgDxe.dxs
// 
// 14    2/03/12 5:52p Fredericko
// [TAG]  		EIP81665
// [Category]  	Improvement
// [Description]  	Support for MOR feature improvement. Removed unneed
// functions.
// EIP: 80813: System will assert in AmiTcgPlatformPei.lib if PeiRamboot
// module is not included in the project
// [Files]  		Tcg.sdl, AmiTcgPlatformDxe.c, Tcgdxe.c, Tcglegacy.c
// 
// 13    12/30/11 4:58p Fredericko
// [TAG]  		EIP78141
// [Category]  	New Feature
// [Description]  	Added hooks to override generic TPM platform hash
// functions.
// [Files]  		1. AmiTcgPlatform.sdl
// 2. AmiTcgPlatformPei.h
// 3. AmiTcgPlatformPeiLib.c
// 4. AmiTcgPlatformPeiAfterMem.c
// 5. AmiTcgPlatformDxe.c
// 6. AmiTcgPlatformDxe.h
// 
// 12    12/18/11 10:27p Fredericko
// Changes to support TcgplatformPeiPolicy in relation to O.S. requests.
// 
// 11    12/12/11 3:52p Fredericko
// [TAG]  		EIP76865
// [Category]  	Improvement
// [Description]  	Dual Support for TCM and TPM. System could hang in TXT
// if txt is enabled in setup
// [Files]  		AmiTcgPlatfompeilib.c, AmiTcgPlatformPpi.cif,
// AmiTcgPlatformPpi.h, AmiTcgPlatformProtocol.cif,
// AmiTcgPlatformProtocol.h,
// EMpTcmPei.c, TcgDxe.cif, TcgPei.cif, TcgPeiAfterMem.cif,
// TcgPeiAfterMem.mak, TcgTcmPeiAfterMem.c, xTcgDxe.c, xTcgPei.c, 
// xTcgPeiAfterMem.c 
// 
// 10    9/27/11 10:28p Fredericko
// [TAG]  		EIP67286
// [Category]  	Improvement
// [Description]  	changes for Tcg Setup policy
// [Files]  		Tcg.sdl
// TcgPei.cif
// TcgPei.mak
// xtcgPei.c
// xTcgPeiAfterMem.c
// TcgPeiAfterMem.mak
// TcgDxe.cif
// TcgDxe.mak
// xTcgDxe.c
// AmiTcgPlatformPeilib.c
// AmiTcgPlatformDxelib.c
// 
// 9     9/01/11 2:25p Fredericko
// [TAG]  		EIP66113
// [Category]  	Improvement
// [Description]  	Support LTsx on server platforms where startup commands
// are sent by the ACM binaries.
// [Files]  		AmiTcgPlatformPeiBoardBeforeMem.c
// AmiTcgPlatformPeiLib.c
// AmiTcgPlatformPei.h
// 
// 8     8/26/11 3:00p Fredericko
// 
// 7     8/09/11 6:30p Fredericko
// [TAG]  		EIP66468
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	1. Changes for Tcg Ppi 1.2 support. 
// [Files]  		1 TcgSmm.h
// 2.TcgSmm.c
// 3.Tcg_ppi1_2.asl
// 4. AmiTcgNvflagsSample.c
// 5. AmiTcgPlatformPeiLib.c
// 6. AmiTcgPlatformDxe.sdl
// 7. AmiTcgPlatformDxe.c
// 
// 6     7/25/11 3:23a Fredericko
// [TAG]  		EIP65177
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	TCG Ppi Sec ver 1.2 update
// 
// 5     4/05/11 8:08p Fredericko
// Changes for the measurement of FVMAIN in the case of the trusted
// cryptographic module
// 
// 4     4/04/11 2:17p Fredericko
// Measurement of Dxe FVol commented back into code
// 
// 3     3/29/11 9:20p Fredericko
// Handle TPM startup and selftest errors as fatal errors. Don't continue
// with any TPM initialization.
// 
// 2     3/29/11 2:20p Fredericko
// [TAG]        EIP 54642
// [Category] Improvement
// [Description] 1. Checkin Files related to TCG function override 
// 2. Include TCM and TPM auto detection
// [Files] Affects all TCG files
//
// 
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:	AmiTcgPlatformPeilib.c
//
// Description:	Function file that contains library files for AmiTcgPlatformPei
//
//<AMI_FHDR_END>
//*************************************************************************
#include <Efi.h>
#include <Pei.h>
#include <TcgCommon.h>
#include <Tpm20Includes\Tpm20Pei.h>
#include <AmiPeiLib.h>
#include <TcgMisc.h>
#include "PPI\TcgService\TcgTcmService.h"
#include "PPI\TcgService\TcgService.h"
#include "PPI\TpmDevice\TpmDevice.h"
#include "PPI\CpuIo.h"
#include "PPI\LoadFile.h"
#include <Ppi\ReadOnlyVariable.h>
#include <ppi\ReadOnlyVariable2.h>
#include "AmiTcgPlatformPei.h"
#include "TcgPlatformSetupPeiPolicy.h"
#include <ppi\AmiTcgPlatformPpi.h>
#include <Token.h>


//*************************************************************************
//                      GLOBAL DEFINITIONS
//*************************************************************************
EFI_GUID gTcgPpiguid                    =  PEI_TCG_PPI_GUID;
EFI_GUID gTpmDevicePpiguid              =  PEI_TPM_PPI_GUID;
EFI_GUID                Descguid = AMI_TCG_PERM_FLAGS_GUID;
EFI_GUID                   Tpm20Hobguid = TPM20_HOB_GUID;


EFI_GUID   gTpmguidEndOfPei = EFI_PEI_END_OF_PEI_PHASE_PPI_GUID;


EFI_GUID gTcgReadOnlyVariablePpiGuid
    = EFI_TCG_PEI_READ_ONLY_VARIABLE_PPI_GUID;


EFI_GUID        TcgBoardEfiGlobalVariableGuid = TCG_EFI_GLOBAL_VARIABLE_GUID;

typedef struct
{
    TPM_1_2_CMD_HEADER hdr;
    UINT32             pcr;
    TCG_DIGEST         digest;  
} TPM_EXTEND_CMD;

typedef struct
{
    TPM_1_2_RET_HEADER rethdr;
    TCG_DIGEST         Outdigest;  
} TPM_EXTEND_RET;

//**********************************************************************
//                  Links
//**********************************************************************
extern MEASURE_CRTM_VERSION_PEI_FUNC_PTR  MEASURE_CRTM_VERSION_PEI_FUNCTION;
MEASURE_CRTM_VERSION_PEI_FUNC_PTR *MeasureCRTMVersionFuncPtr = MEASURE_CRTM_VERSION_PEI_FUNCTION;

extern MEASURE_TCG_PCCLIENT_ID_PEI_FUNC_PTR  MEASURE_TCG_PCCLIENT_ID_PEI_FUNCTION;
MEASURE_TCG_PCCLIENT_ID_PEI_FUNC_PTR *MeasureTCGPcClientSpecIDFuncPtr = MEASURE_TCG_PCCLIENT_ID_PEI_FUNCTION;

extern MEASURE_CORE_DXE_FW_VOL_PEI_FUNC_PTR  MEASURE_CORE_DXE_FW_VOL_PEI_FUNCTION;
MEASURE_CORE_DXE_FW_VOL_PEI_FUNC_PTR *MeasureDxeCoreFwVolFuncPtr = MEASURE_CORE_DXE_FW_VOL_PEI_FUNCTION;


//**********************************************************************
//                  TCG_Helper functions
//**********************************************************************
#pragma pack(1)
typedef struct _TCG_PEI_CALLBACK_CONTEXT
{
    PEI_TPM_PPI      *TpmDevice;
    EFI_PEI_SERVICES **PeiServices;
} TCG_PEI_CALLBACK_CONTEXT;
#pragma pack()

UINT8 Tpm20SupportType()
{
   return (TRUE);
}

static AMI_TPM20SUPPORTTYPE_PPI Tpm20SupportTypePpi = {
    Tpm20SupportType
};


static EFI_PEI_PPI_DESCRIPTOR mTpm20SupportList[] = {
    {
        EFI_PEI_PPI_DESCRIPTOR_PPI
        | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
        &Tpm20Hobguid,
        &Tpm20SupportTypePpi
    }
};


EFI_STATUS
__stdcall TcgCommonPassThrough(
    IN VOID                    *CallbackContext,
    IN UINT32                  NoInputBuffers,
    IN TPM_TRANSMIT_BUFFER     *InputBuffers,
    IN UINT32                  NoOutputBuffers,
    IN OUT TPM_TRANSMIT_BUFFER *OutputBuffers )
{
    TCG_PEI_CALLBACK_CONTEXT *Ctx;

    Ctx = (TCG_PEI_CALLBACK_CONTEXT*)CallbackContext;

    return Ctx->TpmDevice->Transmit(
               Ctx->TpmDevice,
               Ctx->PeiServices,
               NoInputBuffers,
               InputBuffers,
               NoOutputBuffers,
               OutputBuffers
               );
}

BOOLEAN IsMfgMode(
    IN EFI_PEI_SERVICES **PeiServices, 
    IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadVariablePpi
);


//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   TcgPeiBuildHobGuid
//
// Description: Internal abstracted function to create a Hob
//
// Input:       IN  EFI_PEI_SERVICES  **PeiServices,
//              IN  EFI_GUID          *Guid,
//              IN  UINTN             DataLength,
//              OUT VOID              **Hob
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS TcgPeiBuildHobGuid(
    IN EFI_PEI_SERVICES **PeiServices,
    IN EFI_GUID         *Guid,
    IN UINTN            DataLength,
    OUT VOID            **Hob )
{
    EFI_STATUS Status;

    Status = (*PeiServices)->CreateHob(
        PeiServices,
        EFI_HOB_TYPE_GUID_EXTENSION,
        (UINT16) ( sizeof (EFI_HOB_GUID_TYPE) + DataLength ),
        Hob
        );

    if ( EFI_ERROR( Status ))
    {
        return Status;
    }

    ((EFI_HOB_GUID_TYPE*)(*Hob))->Name = *Guid;

    return EFI_SUCCESS;
}



//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   FillDriverLocByFile
//
// Description: Helper function to locate a Driver by guid and fill in
//              Offset data about it. Mainly for MA Driver
//
//
// Input:       IN OUT UINT32* Offset,
//              IN     EFI_PEI_SERVICES  **ps,
//              IN     EFI_GUID *Driveguid,
//              IN OUT VOID **MAStart,
//              IN OUT UINTN *MASize
//
//
// Output:      VOID
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS FillDriverLocByFile(
    IN OUT UINT32       * Offset,
    IN EFI_PEI_SERVICES **ps,
    IN EFI_GUID         *Driveguid,
    IN OUT VOID         **MAStart,
    IN OUT UINTN        *MASize )
{
    UINT8                      * Temp;
    EFI_FIRMWARE_VOLUME_HEADER *pFV;
    EFI_FFS_FILE_HEADER        *ppFile;
    EFI_STATUS                 Status;
    UINTN                      Instance = 0;
    MPDRIVER_LEGHEADER         *Buffer;
    UINT32                     CodeSec = 0;

    while ( TRUE )
    {
        Status = (*ps)->FfsFindNextVolume( ps, Instance, &pFV );

        if ( EFI_ERROR( Status ))
        {
            return  Status; 
        }

        ppFile = NULL;
        //
        // Start new search in volume
        //
        while ( TRUE )
        {
            Status = (*ps)->FfsFindNextFile( ps,
                                             EFI_FV_FILETYPE_FREEFORM,
                                             pFV,
                                             &ppFile );

            if ( Status == EFI_NOT_FOUND )
            {
                break;
            }

            if ( CompareGuid( &(ppFile->Name), Driveguid ))
            {
                Temp = ((UINT8*) ppFile + sizeof (EFI_FFS_FILE_HEADER)
                       + sizeof(ppFile->Size));
                Buffer   = (MPDRIVER_LEGHEADER*)(Temp + 1);
                CodeSec  = Buffer->CodeP;
                *Offset  = (UINT32)Buffer + CodeSec;
                *MAStart = Buffer;
                *MASize  = (UINTN)Buffer->Size;
                return Status;
            }
        }
        Instance += 1;
    }
}




//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   FillDriverLoc
//
// Description: Minor function to fill in MPDriver Offsets for TPM
//              Device PPI
//
//
// Input:       IN  OUT  UINT32* Offset,
//              IN       EFI_PEI_SERVICES  **ps,
//              IN       EFI_GUID *Driveguid
//
// Output:      VOID
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
VOID FillDriverLoc(
    IN OUT UINT32       * Offset,
    IN EFI_PEI_SERVICES **ps,
    IN EFI_GUID         *Driveguid )
{
    EFI_HOB_GUID_TYPE *DrvHob;
    UINT8             * Temp;

    (*ps)->GetHobList( ps, &DrvHob );
    while ( !EFI_ERROR( FindNextHobByType( EFI_HOB_TYPE_GUID_EXTENSION, &DrvHob )))
    {
        if ((DrvHob->Header.HobType == EFI_HOB_TYPE_GUID_EXTENSION)
            && (guidcmp( &DrvHob->Name, Driveguid )) == 0 )
        {
            Temp    = (UINT8*)++DrvHob;
            *Offset = *(UINT32*) Temp;
            break;
        }
    }
}






//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   LocateTcgPpi
//
// Description: Locates and initializes TCG Ppi
//
//
// Input:       IN      EFI_FFS_FILE_HEADER       *FfsHeader
//              IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS LocateTcgPpi(
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_TPM_PPI      **gTpmDevicePpi,
    IN PEI_TCG_PPI      **gTcgPpi
)
{
    EFI_STATUS      Status;

    Status = (*PeiServices)->LocatePpi(
                            PeiServices,
                            &gTpmDevicePpiguid,
                            0, NULL,
                            gTpmDevicePpi);     


    if(EFI_ERROR(Status)){
        PEI_TRACE((-1, PeiServices, "gTpmDevicePpiguid NOT found %r \n", Status)); 
        return Status;
    }

     Status =  (*PeiServices)->LocatePpi(
                            PeiServices,
                            &gTcgPpiguid,
                            0, NULL,
                            gTcgPpi);

    if(EFI_ERROR(Status)){
        PEI_TRACE((-1, PeiServices, "gTcgPpiguid NOT found %r \n", Status)); 
        return Status;
    }

    return Status;
}




//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   LocateTcgPpi
//
// Description: Locates and initializes TCG Ppi
//
//
// Input:       IN      EFI_FFS_FILE_HEADER       *FfsHeader
//              IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS LocateTcmPpi(
    IN EFI_PEI_SERVICES **PeiServices,
    IN PEI_TPM_PPI      **gTpmDevicePpi,
    IN PEI_TCM_PPI      **gTcmPpi
)
{
    EFI_STATUS      Status;

    Status = (*PeiServices)->LocatePpi(
                            PeiServices,
                            &gTpmDevicePpiguid,
                            0, NULL,
                            gTpmDevicePpi);     


    if(EFI_ERROR(Status))return Status;

     Status =  (*PeiServices)->LocatePpi(
                            PeiServices,
                            &gTcgPpiguid,
                            0, NULL,
                            gTcmPpi);

    return Status;
}


EFI_STATUS Tpm20PeiSendStartup(IN EFI_PEI_SERVICES **PeiServices)
{
    EFI_STATUS          Status = EFI_SUCCESS;
    TPM2_Startup_Cmd    StartupCmd;
    TPM2_Common_RespHdr StartupReponse;
    UINT32              ReturnSize = 0;
    EFI_BOOT_MODE       BootMode;
    PEI_TPM_PPI        *TpmPpi = NULL;         
    PEI_TCG_PPI        *TcgPpi = NULL;


    Status = LocateTcgPpi(PeiServices,&TpmPpi, &TcgPpi);
    if(EFI_ERROR(Status))return EFI_NOT_FOUND;

    Status = TpmPpi->Init(TpmPpi, PeiServices );
    if ( EFI_ERROR( Status ))
    {
        return Status;
    }

    StartupCmd.tag = (TPMI_ST_COMMAND_TAG)TPM_H2NS(TPM_ST_NO_SESSIONS);
    StartupCmd.CommandSize = TPM_H2NL((sizeof(TPM2_Startup_Cmd)));
    StartupCmd.CommandCode = TPM_H2NL(TPM_CC_Startup);

    Status = (*PeiServices)->GetBootMode (PeiServices, &BootMode);
    if(EFI_ERROR(Status))return Status;    

    if(BootMode == BOOT_ON_S3_RESUME){
         StartupCmd.StartupType = TPM_H2NS(TPM_SU_STATE);
    }else{
         StartupCmd.StartupType = TPM_H2NS(TPM_SU_CLEAR);
    }

    ReturnSize = (UINT32)sizeof(StartupReponse);

    MemSet((UINT8 *)&StartupReponse,(UINTN)sizeof(StartupReponse), 0);

    Status = TcgPpi->TCGPassThroughToTpm(TcgPpi,
                                      PeiServices,
                                      sizeof(TPM2_Startup_Cmd),
                                      (UINT8*)&StartupCmd,
                                      ReturnSize,
                                      (UINT8*)&StartupReponse);

    if((StartupReponse.ResponseCode) != TPM_RC_SUCCESS){
          Status = EFI_DEVICE_ERROR;
    }

    return Status;
}


EFI_STATUS Tpm20PeiSelfTest(IN EFI_PEI_SERVICES **PeiServices)
{
    EFI_STATUS          Status = EFI_SUCCESS;
    TPM2_SelfTest       SelfTestCmd;
    TPM2_Common_RespHdr   SelfTestReponse;
    UINT32                ReturnSize = 0;
    PEI_TPM_PPI        *TpmPpi = NULL;         
    PEI_TCG_PPI        *TcgPpi = NULL;

    Status = LocateTcgPpi(PeiServices,&TpmPpi, &TcgPpi);
    if(EFI_ERROR(Status))return EFI_NOT_FOUND;

    Status = TpmPpi->Init(TpmPpi, PeiServices );
    if ( EFI_ERROR( Status ))
    {
        return Status;
    }

    SelfTestCmd.tag = (TPMI_ST_COMMAND_TAG)TPM_H2NS(TPM_ST_NO_SESSIONS);
    SelfTestCmd.CommandSize = TPM_H2NL((sizeof(TPM2_SelfTest)));
    SelfTestCmd.CommandCode = TPM_H2NL(TPM_CC_SelfTest);
    SelfTestCmd.SelfTestType = 0;

    ReturnSize = (UINT32)sizeof(SelfTestReponse);
      
    MemSet((UINT8 *)&SelfTestReponse,(UINTN)sizeof(SelfTestReponse), 0);

    Status = TcgPpi->TCGPassThroughToTpm(TcgPpi,
                                        PeiServices,
                                        sizeof(SelfTestCmd),
                                        (UINT8*)&SelfTestCmd,
                                        ReturnSize,
                                        (UINT8*)&SelfTestReponse);


    Status = TpmPpi->Close(TpmPpi, PeiServices );
    if ( EFI_ERROR( Status ))
    {
        return Status;
    }

    return Status;
}




//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   TcgPeiTpmStartup
//
// Description: Sends Initial TPM Startup Command
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//              IN      EFI_BOOT_MODE                BootMode
//
// Output:      EFI STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS
 TcgPeiTpmStartup(
    IN EFI_PEI_SERVICES **PeiServices,
    IN EFI_BOOT_MODE    BootMode )
{
    EFI_STATUS               Status;
    TPM_1_2_CMD_STARTUP      cmdStartup;
    TPM_1_2_RET_HEADER       retHeader;
    TPM_STARTUP_TYPE         TpmSt;
    PEI_TPM_PPI              *TpmPpi = NULL;         
    PEI_TCG_PPI              *TcgPpi = NULL;
    TCG_PLATFORM_SETUP_INTERFACE   *TcgPeiPolicy = NULL;
    EFI_GUID                       gTcgPeiPolicyGuid =\
                                      TCG_PLATFORM_SETUP_PEI_POLICY_GUID;
    TCG_CONFIGURATION              ConfigFlags;
    EFI_HOB_GUID_TYPE          *ptrTpm20Hob;
    UINT8                      Tpm20Device = FALSE;

    TpmSt = TPM_ST_CLEAR;

    Status = (*PeiServices)->LocatePpi(
                PeiServices,
                &gTcgPeiPolicyGuid,
                0, NULL,
                &TcgPeiPolicy);

    if(EFI_ERROR(Status) || TcgPeiPolicy == NULL )return Status;

    Status = TcgPeiPolicy->getTcgPeiPolicy(PeiServices, &ConfigFlags);

    if(EFI_ERROR(Status))return Status;

    if(ConfigFlags.DisallowTpm == 1)
    {
        BootMode = BOOT_IN_RECOVERY_MODE;  //deactivate the TPM
    }

    if ( BootMode == BOOT_ON_S3_RESUME )
    {
        TpmSt = TPM_ST_STATE;
    }

#if (TCG_CONVENTIONAL_BIOS_6_1)

    if ( BootMode == BOOT_IN_RECOVERY_MODE )
    {
        TpmSt = TPM_ST_DEACTIVATED;
    }
#endif

    Status = LocateTcgPpi(PeiServices,&TpmPpi, &TcgPpi);
    if(EFI_ERROR(Status))return EFI_NOT_FOUND;

    Status = TpmPpi->Init(TpmPpi, PeiServices );
    if ( EFI_ERROR( Status ))
    {
        return Status;
    }
    
    cmdStartup.Header.Tag       = TPM_H2NS( TPM_TAG_RQU_COMMAND );
    cmdStartup.Header.ParamSize = TPM_H2NL( sizeof (cmdStartup));
    cmdStartup.Header.Ordinal   = TPM_H2NL( TPM_ORD_Startup );
    cmdStartup.StartupType      = TPM_H2NS( TpmSt );

    Status = TcgPpi->TCGPassThroughToTpm(
                  TcgPpi,
                  PeiServices,
                  sizeof (cmdStartup),
                  (UINT8*)&cmdStartup,
                  sizeof (retHeader),
                  (UINT8*)&retHeader);

    PEI_TRACE((-1, PeiServices,
               "Tcg Startup Command Return Code: size: %x; retCode:%x; tag:%x; bytes %08x\n",
               TPM_H2NL(retHeader.ParamSize ),
               TPM_H2NL(retHeader.RetCode ),
               (UINT32)
               TPM_H2NS(retHeader.Tag )));

    if ( retHeader.ParamSize == 0 )
    {
       return EFI_DEVICE_ERROR;

    }else if(retHeader.RetCode != 0){
        //up till this point we do not know if it is a 1.2 device or
        //a 2.0 device possibly a TPM 20 device send StartupCmd for TPM 20
        Status = Tpm20PeiSendStartup(PeiServices);
        if(!EFI_ERROR(Status)){
           //TPM 2.0 device using TIS interface create TPM20 hob
            Status = TcgPeiBuildHobGuid( PeiServices,
                                    &Tpm20Hobguid,
                                    sizeof (UINT8),
                                    &ptrTpm20Hob );
            
            ASSERT_PEI_ERROR( PeiServices, Status );
            ptrTpm20Hob++;
            Tpm20Device = TRUE;
            (*PeiServices)->CopyMem( ptrTpm20Hob, &Tpm20Device, sizeof (Tpm20Device));
            
             Status = (*PeiServices)->InstallPpi( PeiServices, &mTpm20SupportList[0] );
             if ( EFI_ERROR( Status ))
             {
                return EFI_UNLOAD_IMAGE;
             }
        }
    }
    Status = TpmPpi->Close(TpmPpi, PeiServices );
    if (EFI_ERROR( Status )){
         return Status;
    }

    return (Status | TPM_H2NL( retHeader.RetCode ));
}




//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   TcmPeiStartup
//
// Description: Sends Initial TPM Startup Command
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//              IN      EFI_BOOT_MODE                BootMode
//
// Output:      EFI STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
static
EFI_STATUS
__stdcall TcmPeiStartup(
    IN EFI_PEI_SERVICES **PeiServices,
    IN EFI_BOOT_MODE    BootMode )
{
    EFI_STATUS               Status;
    TPM_1_2_CMD_STARTUP      cmdStartup;
    TPM_1_2_RET_HEADER       retHeader;
    TPM_STARTUP_TYPE         TpmSt;
    PEI_TPM_PPI              *TpmPpi = NULL;         
    PEI_TCM_PPI              *TcgPpi = NULL;

    TpmSt = TPM_ST_CLEAR;

    if ( BootMode == BOOT_ON_S3_RESUME )
    {
        TpmSt = TPM_ST_STATE;
    }

#if (TCG_CONVENTIONAL_BIOS_6_1)

    if ( BootMode == BOOT_IN_RECOVERY_MODE )
    {
        TpmSt = TPM_ST_DEACTIVATED;
    }
#endif

    Status = LocateTcmPpi(PeiServices,&TpmPpi, &TcgPpi);
    if(EFI_ERROR(Status))return EFI_NOT_FOUND;

    Status = TpmPpi->Init(TpmPpi, PeiServices );
    if ( EFI_ERROR( Status ))
    {
        return Status;
    }


    cmdStartup.Header.Tag       = TPM_H2NS( TPM_TAG_RQU_COMMAND );
    cmdStartup.Header.ParamSize = TPM_H2NL( sizeof (cmdStartup));
    cmdStartup.Header.Ordinal   = TPM_H2NL( TCM_ORD_Startup );
    
    cmdStartup.StartupType      = TcgCommonH2NS( TpmSt );

    Status = TcgPpi->TCMPassThroughToTcm(
                  TcgPpi,
                  PeiServices,
                  sizeof (cmdStartup),
                  (UINT8*)&cmdStartup,
                  sizeof (retHeader),
                  (UINT8*)&retHeader);


    Status = TpmPpi->Close(TpmPpi, PeiServices );
    if (EFI_ERROR( Status )){
         return Status;
    }

    PEI_TRACE((-1, PeiServices,
               "Tcg Startup Command Return Code: size: %x; retCode:%x; tag:%x; bytes %08x\n",
               TPM_H2NL(retHeader.ParamSize ),
               TPM_H2NL(retHeader.RetCode ),
               (UINT32)
               TPM_H2NS(retHeader.Tag )));

    if ( retHeader.ParamSize == 0 )
    {
        return EFI_DEVICE_ERROR;
    }
    return (Status | TPM_H2NL( retHeader.RetCode ));
}

//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   ContinueTPMSelfTest
//
// Description: Executes ContinueSelfTest operation on TPM. Certain TPM
//              operation require this operation to be execute before.
//
// Input:       IN  EFI_PEI_SERVICES **ps
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS ContinueTPMSelfTest(
    IN EFI_PEI_SERVICES **ps )
{
    TPM_1_2_CMD_HEADER  cmd;
    TPM_1_2_RET_HEADER  result;
    EFI_STATUS          Status;
    PEI_TPM_PPI         *TpmPpi = NULL;         
    PEI_TCG_PPI         *TcgPpi = NULL;
    AMI_TPM20SUPPORTTYPE_PPI *Tpm20SupportType = NULL;

    Status = (*ps)->LocatePpi(
                            ps,
                            &Tpm20Hobguid,
                            0, NULL,
                            &Tpm20SupportType);     


    if(!EFI_ERROR(Status) && Tpm20SupportType!= NULL){
        return (Tpm20PeiSelfTest(ps));
    }


#if defined DONT_SEND_SELFTEST_TILL_READY_TO_BOOT && DONT_SEND_SELFTEST_TILL_READY_TO_BOOT == 1
    if(*(UINT16 *)(UINTN)(PORT_TPM_IOMEMBASE + 0xF00) == SELF_TEST_VID)
    {
        return EFI_SUCCESS;
    }
#endif

    Status = LocateTcgPpi(ps,&TpmPpi, &TcgPpi);
    if(EFI_ERROR(Status))return EFI_NOT_FOUND;

    Status = TpmPpi->Init(TpmPpi, ps );
    if ( EFI_ERROR( Status )){
         return Status;
    }

    cmd.Tag       = TPM_H2NS( TPM_TAG_RQU_COMMAND );
    cmd.ParamSize = TPM_H2NL( sizeof (cmd));
    cmd.Ordinal   = TPM_H2NL( TPM_ORD_ContinueSelfTest );

    Status =  TcgPpi->TCGPassThroughToTpm(
                    TcgPpi,
                    ps,
                    sizeof (cmd),
                    (UINT8*)&cmd,
                    sizeof (result),
                    (UINT8*)&result );

    PEI_TRACE((-1, ps, "TCG Pei: Self Test : status=%x; RetCode=%x\n", Status,
               TPM_H2NL( result.RetCode )));

    Status = TpmPpi->Close(TpmPpi, ps );

    if ( EFI_ERROR( Status ))
    {
        return Status;
    }
    else if ( result.RetCode != 0 )
    {
        return EFI_DEVICE_ERROR;
    }

    return EFI_SUCCESS;
}



//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   ContinueTCMSelfTest
//
// Description: Executes ContinueSelfTest operation on TPM. Certain TPM
//              operation require this operation to be execute before.
//
// Input:       IN  EFI_PEI_SERVICES **ps
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************

EFI_STATUS ContinueTCMSelfTest(
    IN EFI_PEI_SERVICES **ps )
{
    TPM_1_2_CMD_HEADER       cmd;
    TPM_1_2_RET_HEADER       result;
    EFI_STATUS               Status;
    PEI_TPM_PPI         *TpmPpi = NULL;         
    PEI_TCG_PPI         *TcgPpi = NULL;


    Status = LocateTcgPpi(ps,&TpmPpi, &TcgPpi);
    if(EFI_ERROR(Status))return EFI_NOT_FOUND;

    Status = TpmPpi->Init(TpmPpi, ps );
    if ( EFI_ERROR( Status )){
         return Status;
    }

    cmd.Tag       = TPM_H2NS( TPM_TAG_RQU_COMMAND );
    cmd.ParamSize = TPM_H2NL( sizeof (cmd));
    cmd.Ordinal   = TPM_H2NL(TCM_ORD_ContinueSelfTest );

    Status =  TcgPpi->TCGPassThroughToTpm(
                    TcgPpi,
                    ps,
                    sizeof (cmd),
                    (UINT8*)&cmd,
                    sizeof (result),
                    (UINT8*)&result );

    PEI_TRACE((-1, ps, "TCG Pei: Self Test : status=%x; RetCode=%x\n", Status,
               TPM_H2NL( result.RetCode )));

    Status = TpmPpi->Close(TpmPpi, ps );

    if ( EFI_ERROR( Status ))
    {
        return Status;
    }

    if ( result.RetCode != 0 )
    {
        return EFI_DEVICE_ERROR;
    }

    return EFI_SUCCESS;
}



//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   SendStartupandSelftest
//
// Description: This function performs TPM MA initialization
//
//
// Input:       IN      EFI_FFS_FILE_HEADER       *FfsHeader
//              IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS SendStartupandSelftest(
    IN EFI_PEI_SERVICES **PeiServices,
    IN EFI_BOOT_MODE    BootMode
)
{
    EFI_STATUS      Status;
    EFI_GUID        SkipTpmStartupGuid = AMI_SKIP_TPM_STARTUP_GUID;
    BOOLEAN         SkipTpmStartup = FALSE;
    AMI_TCG_PEI_FUNCTION_OVERRIDE_PPI *PpiOverride;
    TCG_PLATFORM_SETUP_INTERFACE   *TcgPeiPolicy = NULL;
    EFI_GUID                        gTcgPeiPolicyGuid =\
                                      TCG_PLATFORM_SETUP_PEI_POLICY_GUID;
    TCG_CONFIGURATION              ConfigFlags;

    Status = (*PeiServices)->LocatePpi(
                    PeiServices,
                    &SkipTpmStartupGuid,
                    0, NULL,
                    &PpiOverride);

    if(!EFI_ERROR(Status)) {
        SkipTpmStartup = TRUE;
    }

    
    Status = (*PeiServices)->LocatePpi(
                PeiServices,
                &gTcgPeiPolicyGuid,
                0, NULL,
                &TcgPeiPolicy);

    if(EFI_ERROR(Status) || TcgPeiPolicy == NULL )return Status;

    Status = TcgPeiPolicy->getTcgPeiPolicy(PeiServices, &ConfigFlags);

    if(EFI_ERROR(Status))return Status;

    if(ConfigFlags.DisallowTpm == 1)
    {
        BootMode = BOOT_IN_RECOVERY_MODE;  //deactivate the TPM
        Status = TcgPeiTpmStartup( PeiServices, BootMode );
        return Status;
    }
    
    if(!AutoSupportType()){
       if (!SkipTpmStartup) {
	        Status = TcgPeiTpmStartup( PeiServices, BootMode );
 	        if(Status){  
  	          //sartup command failed
   	         return EFI_DEVICE_ERROR;
    		}
        }
                       
        Status = ContinueTPMSelfTest( PeiServices );
         if(Status){  
            //Selftest command failed
            return EFI_DEVICE_ERROR;
        }
    }else{
         Status = TcmPeiStartup( PeiServices, BootMode );
         if(Status){  
            //sartup command failed
            return EFI_DEVICE_ERROR;
        }

        Status = ContinueTCMSelfTest( PeiServices );
        if(Status){  
            //selftest command failed
            return EFI_DEVICE_ERROR;
        }
    }
    return Status;
}








//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   TCGPEI_GETCAP
//
// Description: Executes TPM operation to read capabilities
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      TPM_Capabilities_PermanentFlag
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
TPM_Capabilities_PermanentFlag INTTCGPEI_GETCAP(
    IN EFI_PEI_SERVICES **PeiServices )
{
    TPM_Capabilities_PermanentFlag  *cap = NULL;
    EFI_STATUS                      Status;
    TPM_GetCapabilities_Input       cmdGetCap;
    UINT8                           result[0x100];
    PEI_TPM_PPI                     *TpmPpi = NULL;         
    PEI_TCG_PPI                     *TcgPpi = NULL;
    EFI_GUID TcgPpiguid                    =  PEI_TCG_PPI_GUID;
    EFI_GUID TpmDevicePpiguid              =  PEI_TPM_PPI_GUID;

    cmdGetCap.Tag         = TPM_H2NS( TPM_TAG_RQU_COMMAND );
    cmdGetCap.ParamSize   = TPM_H2NL( sizeof (cmdGetCap));
    cmdGetCap.CommandCode = TPM_H2NL( TPM_ORD_GetCapability );
    cmdGetCap.caparea     = TPM_H2NL( TPM_CAP_FLAG );
    cmdGetCap.subCapSize  = TPM_H2NL( 4 ); // subCap is always 32bit long
    cmdGetCap.subCap      = TPM_H2NL( TPM_CAP_FLAG_PERMANENT );

    Status = (*PeiServices)->LocatePpi(
                            PeiServices,
                            &TpmDevicePpiguid,
                            0, NULL,
                            &TpmPpi);     

    ASSERT_PEI_ERROR( PeiServices, Status );

    Status = TpmPpi->Init(TpmPpi, PeiServices);
   
    if(Status){
        MemSet(result,sizeof(TPM_Capabilities_PermanentFlag), 0);
        cap = (TPM_Capabilities_PermanentFlag*)result;
        return *cap;
    }

    Status =  (*PeiServices)->LocatePpi(
                            PeiServices,
                            &TcgPpiguid,
                            0, NULL,
                            &TcgPpi);

    ASSERT_PEI_ERROR( PeiServices, Status );


    Status = TcgPpi->TCGPassThroughToTpm(
                TcgPpi,
                PeiServices,
                sizeof(cmdGetCap),
                (UINT8*)&cmdGetCap,
                0x100,
                result );

    cap = (TPM_Capabilities_PermanentFlag*)result;

    PEI_TRACE((-1, PeiServices,
               "GetCapability: %r; size: %x; retCode:%x; tag:%x; bytes %08x\n",
               Status,TPM_H2NL( cap->ParamSize ), TPM_H2NL(cap->RetCode ),
               (UINT32)TPM_H2NS(cap->tag ), TPM_H2NL( *(UINT32*)&cap->disabled )));

 
    Status = TpmPpi->Close(TpmPpi, PeiServices);

    return *cap;
}



//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   TCGPEI_GETCAP
//
// Description: Executes TPM operation to read capabilities
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      TPM_Capabilities_PermanentFlag
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
TPM_Capabilities_PermanentFlag NuvotonProprietaryGetFlags(
    IN EFI_PEI_SERVICES **PeiServices )
{
    TPM_Capabilities_PermanentFlag  cap;
    EFI_STATUS                      Status;
    TPM_RQU_COMMAND_HDR             cmdGetTpmStatus;
    UINT8                           result[0x100];
    PEI_TPM_PPI                     *TpmPpi = NULL;         
    PEI_TCG_PPI                     *TcgPpi = NULL;
    EFI_GUID TcgPpiguid                    =  PEI_TCG_PPI_GUID;
    EFI_GUID TpmDevicePpiguid              =  PEI_TPM_PPI_GUID;

    cmdGetTpmStatus.tag         = TPM_H2NS( TPM_TAG_RQU_COMMAND );
    cmdGetTpmStatus.paramSize   = TPM_H2NL( sizeof (TPM_RQU_COMMAND_HDR));
    cmdGetTpmStatus.ordinal     = TPM_H2NL( NTC_ORD_GET_TPM_STATUS );

    Status = (*PeiServices)->LocatePpi(
                            PeiServices,
                            &TpmDevicePpiguid,
                            0, NULL,
                            &TpmPpi);     

    ASSERT_PEI_ERROR( PeiServices, Status );

    Status = TpmPpi->Init(TpmPpi, PeiServices);

    MemSet(&cap,sizeof(TPM_Capabilities_PermanentFlag), 0);           

    if(Status){
        return cap;
    }

    Status =  (*PeiServices)->LocatePpi(
                            PeiServices,
                            &TcgPpiguid,
                            0, NULL,
                            &TcgPpi);

    ASSERT_PEI_ERROR( PeiServices, Status );


    Status = TcgPpi->TCGPassThroughToTpm(
                TcgPpi,
                PeiServices,
                sizeof(cmdGetTpmStatus),
                (UINT8*)&cmdGetTpmStatus,
                0x100,
                result );
          
    if(((NUVOTON_SPECIFIC_FLAGS *)result)->RetCode == 0)
    {
        if(((NUVOTON_SPECIFIC_FLAGS *)result)->isdisabled){
            cap.disabled = 1; 
        }

        if(((NUVOTON_SPECIFIC_FLAGS *)result)->isdeactivated){
            cap.deactivated = 1; 
        }

        if(((NUVOTON_SPECIFIC_FLAGS *)result)->isOwnerSet){
            cap.ownership = 1; 
        }
    }else{
            cap.RetCode = ((NUVOTON_SPECIFIC_FLAGS *)result)->RetCode;
    }

 
    Status = TpmPpi->Close(TpmPpi, PeiServices);

    return cap;
}






//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   TCGPEI_GETCAP
//
// Description: Executes TPM operation to read capabilities
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      TPM_Capabilities_PermanentFlag
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
TPM_Capabilities_PermanentFlag TCGPEI_GETCAP(
    IN EFI_PEI_SERVICES **PeiServices )
{
    TPM_PERM_FLAGS                 *CapPpi;
    EFI_GUID                       guid = AMI_TCG_PERM_FLAGS_GUID;
    EFI_STATUS                     Status;
    TPM_Capabilities_PermanentFlag Cap;
    EFI_PEI_PPI_DESCRIPTOR  *FlagsPpiDesc;
    TPM_PERM_FLAGS          *FlagsPpi;

    Status = (*PeiServices)->LocatePpi(
                        PeiServices,
                        &guid,
                        0,
                        NULL,
                        &CapPpi);
    
    
    if (EFI_ERROR(Status)){
        //
        // Allocate descriptor and PPI structures
        Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (EFI_PEI_PPI_DESCRIPTOR), &FlagsPpiDesc);
        ASSERT_PEI_ERROR (PeiServices, Status);
        (*PeiServices)->SetMem ((VOID*) FlagsPpiDesc, sizeof (EFI_PEI_PPI_DESCRIPTOR), 0);

        Status = (*PeiServices)->AllocatePool (PeiServices, sizeof (TPM_PERM_FLAGS), &FlagsPpi);
        ASSERT_PEI_ERROR (PeiServices, Status);
        (*PeiServices)->SetMem ((VOID*) FlagsPpi, sizeof (TPM_PERM_FLAGS), 0);

        Cap = INTTCGPEI_GETCAP( PeiServices );
        (*PeiServices)->CopyMem(&FlagsPpi->Capabilities, &Cap, sizeof(TPM_Capabilities_PermanentFlag));
    
        FlagsPpiDesc->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
        FlagsPpiDesc->Ppi = FlagsPpi;

        FlagsPpiDesc->Guid = &Descguid;
        Status = (**PeiServices).InstallPpi (PeiServices, FlagsPpiDesc);
        ASSERT_PEI_ERROR (PeiServices, Status);

        return Cap;
    }
    else{
     return CapPpi->Capabilities;
    }
}


//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   TCGPEI_GETCAP
//
// Description: Executes TPM operation to read capabilities
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      TPM_Capabilities_PermanentFlag
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
TCM_Capabilities_PermanentFlag TCMPEI_GETCAP(
    IN EFI_PEI_SERVICES **PeiServices )
{
    TCM_Capabilities_PermanentFlag * cap = NULL;
    EFI_STATUS                     Status;
    TPM_GetCapabilities_Input      cmdGetCap;
    UINT8                          result[0x100];
    PEI_TPM_PPI                    *TpmPpi = NULL;         
    PEI_TCM_PPI                    *TcgPpi = NULL;


    cmdGetCap.Tag         = TPM_H2NS( TPM_TAG_RQU_COMMAND );
    cmdGetCap.ParamSize   = TPM_H2NL( sizeof (cmdGetCap));
    cmdGetCap.CommandCode = TPM_H2NL( TCM_ORD_GetCapability );

    cmdGetCap.caparea     = TPM_H2NL( TPM_CAP_FLAG );
    cmdGetCap.subCapSize  = TPM_H2NL( 4 ); // subCap is always 32bit long
    cmdGetCap.subCap      = TPM_H2NL( TPM_CAP_FLAG_PERMANENT );

    Status = LocateTcmPpi(PeiServices, &TpmPpi, &TcgPpi);
    ASSERT_PEI_ERROR( PeiServices, Status );

    Status = TpmPpi->Init(TpmPpi, PeiServices );
    ASSERT_PEI_ERROR( PeiServices, Status );

    Status = TcgPpi->TCMPassThroughToTcm(
                TcgPpi,
                PeiServices,
                sizeof(cmdGetCap),
                (UINT8*)&cmdGetCap,
                0x100,
                result );

    cap = (TCM_Capabilities_PermanentFlag*)result;

    PEI_TRACE((-1, PeiServices,
               "GetCapability: %r; size: %x; retCode:%x; tag:%x; bytes %08x\n",
               Status,TPM_H2NL( cap->ParamSize ), TPM_H2NL(cap->RetCode ),
               (UINT32)TPM_H2NS(cap->tag ), TPM_H2NL( *(UINT32*)&cap->disabled )));

    Status = TpmPpi->Close(TpmPpi, PeiServices );
    ASSERT_PEI_ERROR( PeiServices, Status );

    return *cap;
}






//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   MeasureCRTMVersion
//
// Description: Measures EFI CRTM Version
//              Demo Version[546BFB1E1D0C4055A4AD4EF4BF17B83A]
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS
 MeasureCRTMVersion(
    IN EFI_PEI_SERVICES **PeiServices )
{
    EFI_TCG_PCR_EVENT TcgEvent;
    UINT32            EventNum;
    UINTN             Len         = sizeof(EFI_GUID);
    EFI_GUID          CrtmVersion = CRTM_GUID;
    PEI_TPM_PPI       *TpmPpi = NULL;         
    PEI_TCG_PPI       *TcgPpi = NULL;
    EFI_STATUS        Status;

    TcgEvent.Header.PCRIndex   = PCRi_CRTM_AND_POST_BIOS;  
    TcgEvent.Header.EventType     = EV_S_CRTM_VERSION;
    TcgEvent.Header.EventDataSize = Len;

    Status = LocateTcgPpi(PeiServices,&TpmPpi, &TcgPpi);
    if(EFI_ERROR(Status))return EFI_NOT_FOUND;

    MemCpy(
        &TcgEvent.Event.SCrtmVersion,
        &CrtmVersion,
        Len
        );

    return TcgPpi->TCGHashLogExtendEvent(
               TcgPpi,
               PeiServices,
               (UINT8*)&TcgEvent.Event,
               TcgEvent.Header.EventDataSize,
               (TCG_PCR_EVENT*)&TcgEvent,
               &EventNum
               );
}




//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   MeasureTcmCRTMVersion
//
// Description: Measures EFI CRTM Version
//              Demo Version[546BFB1E1D0C4055A4AD4EF4BF17B83A]
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS
 MeasureTcmCRTMVersion(
    IN EFI_PEI_SERVICES **PeiServices )
{
    EFI_TCM_PCR_EVENT TcmEvent;
    UINT32            EventNum;
    UINTN             Len         = sizeof(EFI_GUID);
    EFI_GUID          CrtmVersion = CRTM_GUID;
    PEI_TPM_PPI       *TpmPpi = NULL;         
    PEI_TCM_PPI       *TcgPpi = NULL;
    EFI_STATUS        Status;

    TcmEvent.Header.PCRIndex      = PCRi_CRTM_AND_POST_BIOS;
    TcmEvent.Header.EventType     = EV_S_CRTM_VERSION;
    TcmEvent.Header.EventDataSize = Len;

    MemCpy(&TcmEvent.Event.SCrtmVersion,
    &CrtmVersion, Len);

    Status = LocateTcmPpi(PeiServices, &TpmPpi, &TcgPpi);
    ASSERT_PEI_ERROR( PeiServices, Status );

    return TcgPpi->TCMHashLogExtendEvent(TcgPpi,
               PeiServices,
               (UINT8*)&TcmEvent.Event,
               TcmEvent.Header.EventDataSize,
               (TCM_PCR_EVENT*)&TcmEvent,
               &EventNum);
}



//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   MeasureTCGPcClientSpecID
//
// Description: Includes a measurement of the TcgSpecID into PCR[0]
//
//
// Input:       IN  PEI_TCG_PPI* tcg,
//              IN  EFI_PEI_SERVICES      **ps
//
// Output:
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS MeasureTCGPcClientSpecID(
    IN EFI_PEI_SERVICES **ps, 
    IN PEI_TCG_PPI      *tcg )
{
    TCG_PCR_EVENT                 ev;
    TCG_PCClientSpecIDEventStruct TcgInfo;
    TCG_VendorInfoStruct          TcgVenInfo;
    UINT32                        n;
    EFI_STATUS                    status;
    UINT8                         i = 0, times = 0;

    PEI_TRACE((-1, ps, "TCG Pei: TCG_PcClientSpecID\n"));

    MemCpy(
        TcgVenInfo.TCGBIOSVENDOR,
        TCG_BIOS_VENDOR,
        sizeof(TcgVenInfo.TCGBIOSVENDOR));

    MemCpy(
        TcgVenInfo.TCGOEMID,
        TCG_OEM_ID,
        sizeof(TcgVenInfo.TCGOEMID));

    //fill in pc-client spec id
    TcgInfo.PlatformClass     = TCG_PLATFORM_CLASS;
    TcgInfo.BIOSTypeInterface = TCG_BIOS_TYPE_INTERFACE;
    TcgInfo.BIOSTypeMapping   = TCG_BIOS_TYPE_MAPPING;
    TcgInfo.SpecVersionMajor  = TCG_SPEC_VERSION_MAJOR;
    TcgInfo.SpecVersionMinor  = TCG_SPEC_VERSION_MINOR;
    TcgInfo.SpecErrata        = TCG_SPEC_ERRATA;
    TcgInfo.Reserved          = 0x00;
    TcgInfo.VendorInfo        = &TcgVenInfo;
    TcgInfo.VendorInfoSize    = sizeof(TcgInfo.VendorInfo );


    ev.PCRIndex   = PCRi_CRTM_AND_POST_BIOS;
    ev.EventType  = EV_SPECIFICATION_IDENTIFIER;
    ev.EventSize  = sizeof (TcgInfo);
    ev.Event[0] = *(UINT8*)&TcgInfo;

M_TRY_AGAIN:
    status = tcg->TCGHashLogExtendEvent(tcg,
                                        ps,
                                        (UINT8*)&TcgInfo,
                                        sizeof(TcgInfo),
                                        &ev,
                                        &n );

     if ( EFI_ERROR( status ))
        {
            if ( status == EFI_NOT_READY )
                {
                    i = 0;
                    do
                    {
                        i++;
                    } while ( i < 200 );

                    if ( times > 0x2 )
                    {
                        PEI_TRACE((-1, ps,"xTcgPei::Could not measure PC_CLIENTSPECID \n"));
                        goto M_TRY_DONE;
                    }
                    times++;
                    goto M_TRY_AGAIN;
                }
        }
M_TRY_DONE:
    return status;
}








//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   MeasureDxeCoreFwVol
//
// Description: Measures the firmware volume as a EV_POST_CODE event
//
//
// Input:       IN    PEI_TCG_PPI* tcg,
//              IN    EFI_PEI_SERVICES              **ps,
//              IN    EFI_FIRMWARE_VOLUME_HEADER    *fwv
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS MeasureDxeCoreFwVol(
    IN PEI_TCG_PPI                * tcg,
    IN EFI_PEI_SERVICES           **ps,
    IN EFI_FIRMWARE_VOLUME_HEADER *fwv )
{ 
    PEI_EFI_POST_CODE       ev;
    UINT32                  n;
    EFI_STATUS              status;
    void                    *Context = NULL;;


    PEI_TRACE((-1, ps, "TCG Pei: measure FwMain: at %x size %d\n", fwv,
               fwv->FvLength));
        
    ev.Header.PCRIndex      = PCRi_CRTM_AND_POST_BIOS;
    ev.Header.EventType     = EV_POST_CODE;
    ev.Header.EventDataSize = sizeof (EFI_TCG_EV_POST_CODE);
    ev.Event.PostCodeAddress = (EFI_PHYSICAL_ADDRESS)FV_MAIN_BASE;

#if PARTIALLY_MEASURE_FVMAIN == 1
    #if x64_BUILD
        ev.Event.PostCodeLength = (UINT64)TCG_FV_MAIN_SIZE;
    #else
        ev.Event.PostCodeLength = (UINTN)TCG_FV_MAIN_SIZE;
    #endif
#else
#if defined x64_BUILD &&  x64_BUILD == 1
        ev.Event.PostCodeLength = (UINT64)fwv->FvLength;
#else
        ev.Event.PostCodeLength = (UINTN)fwv->FvLength;
#endif
#endif
   status = tcg->TCGHashLogExtendEvent( tcg, ps, (UINT8*)fwv,
                                            (UINT32)ev.Event.PostCodeLength,
                                            (TCG_PCR_EVENT*)&ev, &n );

    return status;

}



//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   TcmMeasureDxeCoreFwVol
//
// Description: Measures the firmware volume as a EV_POST_CODE event
//
//
// Input:       IN    PEI_TCM_PPI* tcg,
//              IN    EFI_PEI_SERVICES              **ps,
//              IN    EFI_FIRMWARE_VOLUME_HEADER    *fwv
//
// Output:      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
EFI_STATUS TCMMeasureDxeCoreFwVol(
    IN PEI_TCM_PPI                * tcg,
    IN EFI_PEI_SERVICES           **ps,
    IN EFI_FIRMWARE_VOLUME_HEADER *fwv )
{
    TCM_PEI_EFI_POST_CODE   tcmev;
    UINT32            n;
    EFI_STATUS        status;
    TCG_DIGEST        TempDigest;
    void              *Context = NULL;;


    PEI_TRACE((-1, ps, "TCG Pei: measure FwMain: at %x size %d\n", fwv,
               fwv->FvLength));

    tcmev.Header.PCRIndex      = PCRi_CRTM_AND_POST_BIOS;
    tcmev.Header.EventType     = EV_POST_CODE;
    tcmev.Header.EventDataSize = sizeof (EFI_TCG_EV_POST_CODE );

    SHA1HashAll( Context, fwv, (UINTN)fwv->FvLength, &TempDigest );

    tcmev.Event.PostCodeAddress = (EFI_PHYSICAL_ADDRESS)&TempDigest;
#if x64_BUILD
    tcmev.Event.PostCodeLength = (UINT64)TPM_SHA1_160_HASH_LEN;
#else
    tcmev.Event.PostCodeLength = (UINTN)TPM_SHA1_160_HASH_LEN;
#endif

    status = tcg->TCMHashLogExtendEvent( tcg, ps, (UINT8*)&TempDigest,
                                         (UINT32)tcmev.Event.PostCodeLength,
                                         (TCM_PCR_EVENT*)&tcmev, &n );

    return status;
}






//**********************************************************************
//<AMI_PHDR_START>
//
// Procedure:   Lock_TPMPhysicalPresence
//
// Description: check whether to Lock TPM physical Presence
//
//
// Input:       IN      EFI_PEI_SERVICES          **PeiServices,
//
// Output:      BOOLEAN
//
// Modified:
//
// Referrals:
//
// Notes:
//<AMI_PHDR_END>
//**********************************************************************
BOOLEAN Lock_TPMPhysicalPresence(
    IN EFI_PEI_SERVICES **PeiServices )
{
    EFI_GUID                       gAmiTcgEfiOSVarguid
                                         = AMI_TCG_EFI_OS_VARIABLE_GUID;
    UINTN                          OSVarSize = sizeof(AMI_PPI_NV_VAR);
    UINT32                         Attribs = EFI_VARIABLE_NON_VOLATILE
                                    | EFI_VARIABLE_BOOTSERVICE_ACCESS;
    UINTN                          Size = sizeof(UINT8);
    UINTN                          INTSize = sizeof(UINT32);
    UINT32                         OwnerCap = 0;
    EFI_GUID                       peiTcgEfiGlobalVariableGuid \
                                            = TCG_EFI_GLOBAL_VARIABLE_GUID;
    TCG_PLATFORM_SETUP_INTERFACE   *TcgPeiPolicy = NULL;
    EFI_GUID                       gTcgPeiPolicyGuid =\
                                        TCG_PLATFORM_SETUP_PEI_POLICY_GUID;
#if MANUFACTURING_MODE_SUPPORT
    EFI_STATUS                        Status;
    EFI_PEI_READ_ONLY_VARIABLE2_PPI   *ReadVariable2Ppi;
    EFI_GUID                          TcgManufacturingModeGuid     = AMI_TCG_MANUFACTURING_MODE_HOB_GUID;
	EFI_HOB_GUID_TYPE                 *TcgManufacturingModeHob;
	BOOLEAN                           ManufacturingModeVar = FALSE;
#endif
   
#if MANUFACTURING_MODE_SUPPORT
	Status = (*PeiServices)->LocatePpi(PeiServices,
                    		&gEfiPeiReadOnlyVariable2PpiGuid,
                    		0,
                    		NULL,
                    		&ReadVariable2Ppi);

	ASSERT_PEI_ERROR(PeiServices, Status);
    if(!EFI_ERROR(Status)){
        if(IsMfgMode(PeiServices, ReadVariable2Ppi)){
            
            ManufacturingModeVar = TRUE;
            Status =  TcgPeiBuildHobGuid(PeiServices,
                                           &TcgManufacturingModeGuid,
                                           sizeof (BOOLEAN),
                                           &TcgManufacturingModeHob );

            TcgManufacturingModeHob++;
            (*PeiServices)->CopyMem( TcgManufacturingModeHob, &ManufacturingModeVar, sizeof (ManufacturingModeVar));

            return FALSE;
        }   
    }
#endif
    return FALSE;
}

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