]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Tcg2Config/TpmDetection.c
SecurityPkg: Apply uncrustify changes
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Config / TpmDetection.c
CommitLineData
1abfa4ce
JY
1/** @file\r
2 TPM1.2/dTPM2.0 auto detection.\r
3\r
b3548d32 4Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
289b714b 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
1abfa4ce
JY
6\r
7**/\r
8\r
1abfa4ce
JY
9#include <PiPei.h>\r
10#include <Ppi/ReadOnlyVariable2.h>\r
11\r
12#include <Library/BaseLib.h>\r
13#include <Library/BaseMemoryLib.h>\r
1abfa4ce
JY
14#include <Library/DebugLib.h>\r
15#include <Library/PeiServicesLib.h>\r
16#include <Library/PcdLib.h>\r
17#include <Library/Tpm12DeviceLib.h>\r
18#include <Library/Tpm12CommandLib.h>\r
19#include <IndustryStandard/Tpm12.h>\r
20\r
21#include "Tcg2ConfigNvData.h"\r
26fc074a 22#include "Tcg2Internal.h"\r
1abfa4ce 23\r
1abfa4ce
JY
24/**\r
25 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.\r
26\r
27 @param SetupTpmDevice TpmDevice configuration in setup driver\r
28\r
29 @return TpmDevice configuration\r
30**/\r
31UINT8\r
32DetectTpmDevice (\r
c411b485 33 IN UINT8 SetupTpmDevice\r
1abfa4ce
JY
34 )\r
35{\r
c411b485
MK
36 EFI_STATUS Status;\r
37 EFI_BOOT_MODE BootMode;\r
38 TCG2_DEVICE_DETECTION Tcg2DeviceDetection;\r
39 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;\r
40 UINTN Size;\r
1abfa4ce
JY
41\r
42 Status = PeiServicesGetBootMode (&BootMode);\r
43 ASSERT_EFI_ERROR (Status);\r
44\r
45 //\r
46 // In S3, we rely on normal boot Detection, because we save to ReadOnly Variable in normal boot.\r
47 //\r
48 if (BootMode == BOOT_ON_S3_RESUME) {\r
e905fbb0 49 DEBUG ((DEBUG_INFO, "DetectTpmDevice: S3 mode\n"));\r
1abfa4ce 50\r
c411b485 51 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **)&VariablePpi);\r
1abfa4ce
JY
52 ASSERT_EFI_ERROR (Status);\r
53\r
c411b485
MK
54 Size = sizeof (TCG2_DEVICE_DETECTION);\r
55 ZeroMem (&Tcg2DeviceDetection, sizeof (Tcg2DeviceDetection));\r
1abfa4ce
JY
56 Status = VariablePpi->GetVariable (\r
57 VariablePpi,\r
58 TCG2_DEVICE_DETECTION_NAME,\r
59 &gTcg2ConfigFormSetGuid,\r
60 NULL,\r
61 &Size,\r
62 &Tcg2DeviceDetection\r
63 );\r
64 if (!EFI_ERROR (Status) &&\r
65 (Tcg2DeviceDetection.TpmDeviceDetected >= TPM_DEVICE_MIN) &&\r
c411b485
MK
66 (Tcg2DeviceDetection.TpmDeviceDetected <= TPM_DEVICE_MAX))\r
67 {\r
e905fbb0 68 DEBUG ((DEBUG_ERROR, "TpmDevice from DeviceDetection: %x\n", Tcg2DeviceDetection.TpmDeviceDetected));\r
1abfa4ce
JY
69 return Tcg2DeviceDetection.TpmDeviceDetected;\r
70 }\r
71 }\r
72\r
e905fbb0 73 DEBUG ((DEBUG_INFO, "DetectTpmDevice:\n"));\r
1abfa4ce
JY
74\r
75 // dTPM available and not disabled by setup\r
76 // We need check if it is TPM1.2 or TPM2.0\r
77 // So try TPM1.2 command at first\r
78\r
79 Status = Tpm12RequestUseTpm ();\r
80 if (EFI_ERROR (Status)) {\r
6d2eec02
MK
81 //\r
82 // dTPM not available\r
83 //\r
84 return TPM_DEVICE_NULL;\r
1abfa4ce
JY
85 }\r
86\r
87 if (BootMode == BOOT_ON_S3_RESUME) {\r
88 Status = Tpm12Startup (TPM_ST_STATE);\r
89 } else {\r
90 Status = Tpm12Startup (TPM_ST_CLEAR);\r
91 }\r
c411b485 92\r
1abfa4ce
JY
93 if (EFI_ERROR (Status)) {\r
94 return TPM_DEVICE_2_0_DTPM;\r
95 }\r
96\r
97 // NO initialization needed again.\r
fe3ca12d
SZ
98 Status = PcdSet8S (PcdTpmInitializationPolicy, 0);\r
99 ASSERT_EFI_ERROR (Status);\r
1abfa4ce
JY
100 return TPM_DEVICE_1_2;\r
101}\r