]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/TrEEConfig/TpmDetection.c
Fix execution status & DEBUG message level mismatch. EFI_D_ERROR is used only...
[mirror_edk2.git] / SecurityPkg / Tcg / TrEEConfig / TpmDetection.c
CommitLineData
c1d93242
JY
1/** @file\r
2 TPM1.2/dTPM2.0 auto detection.\r
3\r
2bc36371 4Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>\r
c1d93242
JY
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15\r
16#include <PiPei.h>\r
5437d457 17#include <Ppi/ReadOnlyVariable2.h>\r
c1d93242
JY
18\r
19#include <Library/BaseLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/IoLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/PeiServicesLib.h>\r
24#include <Library/PcdLib.h>\r
25#include <Library/Tpm12DeviceLib.h>\r
26#include <Library/Tpm12CommandLib.h>\r
27#include <IndustryStandard/Tpm12.h>\r
28\r
29#include "TrEEConfigNvData.h"\r
30\r
31/**\r
32 This routine return if dTPM (1.2 or 2.0) present.\r
33\r
34 @retval TRUE dTPM present\r
35 @retval FALSE dTPM not present\r
36**/\r
37BOOLEAN\r
38IsDtpmPresent (\r
39 VOID\r
40 )\r
41{\r
42 UINT8 RegRead;\r
43 \r
44 RegRead = MmioRead8 ((UINTN)PcdGet64 (PcdTpmBaseAddress));\r
45 if (RegRead == 0xFF) {\r
46 DEBUG ((EFI_D_ERROR, "DetectTpmDevice: Dtpm not present\n"));\r
47 return FALSE;\r
48 } else {\r
33985e3b 49 DEBUG ((EFI_D_INFO, "DetectTpmDevice: Dtpm present\n"));\r
c1d93242
JY
50 return TRUE;\r
51 }\r
52}\r
53\r
54/**\r
55 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.\r
56\r
57 @param SetupTpmDevice TpmDevice configuration in setup driver\r
58\r
59 @return TpmDevice configuration\r
60**/\r
61UINT8\r
62DetectTpmDevice (\r
63 IN UINT8 SetupTpmDevice\r
64 )\r
65{\r
66 EFI_STATUS Status;\r
67 EFI_BOOT_MODE BootMode;\r
5437d457
JY
68 TREE_DEVICE_DETECTION TrEEDeviceDetection;\r
69 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;\r
70 UINTN Size;\r
71\r
c1d93242
JY
72 Status = PeiServicesGetBootMode (&BootMode);\r
73 ASSERT_EFI_ERROR (Status);\r
74\r
75 //\r
5437d457 76 // In S3, we rely on normal boot Detection, because we save to ReadOnly Variable in normal boot.\r
c1d93242
JY
77 //\r
78 if (BootMode == BOOT_ON_S3_RESUME) {\r
33985e3b 79 DEBUG ((EFI_D_INFO, "DetectTpmDevice: S3 mode\n"));\r
c1d93242 80\r
5437d457
JY
81 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);\r
82 ASSERT_EFI_ERROR (Status);\r
83\r
84 Size = sizeof(TREE_DEVICE_DETECTION);\r
85 ZeroMem (&TrEEDeviceDetection, sizeof(TrEEDeviceDetection));\r
86 Status = VariablePpi->GetVariable (\r
87 VariablePpi,\r
88 TREE_DEVICE_DETECTION_NAME,\r
89 &gTrEEConfigFormSetGuid,\r
90 NULL,\r
91 &Size,\r
92 &TrEEDeviceDetection\r
93 );\r
94 if (!EFI_ERROR (Status) &&\r
95 (TrEEDeviceDetection.TpmDeviceDetected >= TPM_DEVICE_MIN) &&\r
96 (TrEEDeviceDetection.TpmDeviceDetected <= TPM_DEVICE_MAX)) {\r
97 DEBUG ((EFI_D_ERROR, "TpmDevice from DeviceDetection: %x\n", TrEEDeviceDetection.TpmDeviceDetected));\r
98 return TrEEDeviceDetection.TpmDeviceDetected;\r
99 }\r
c1d93242
JY
100 }\r
101\r
33985e3b 102 DEBUG ((EFI_D_INFO, "DetectTpmDevice:\n"));\r
5437d457 103 if (!IsDtpmPresent ()) {\r
c1d93242
JY
104 // dTPM not available\r
105 return TPM_DEVICE_NULL;\r
106 }\r
107\r
108 // dTPM available and not disabled by setup\r
109 // We need check if it is TPM1.2 or TPM2.0\r
110 // So try TPM1.2 command at first\r
111\r
112 Status = Tpm12RequestUseTpm ();\r
113 if (EFI_ERROR (Status)) {\r
114 return TPM_DEVICE_2_0_DTPM;\r
115 }\r
116\r
5437d457
JY
117 if (BootMode == BOOT_ON_S3_RESUME) {\r
118 Status = Tpm12Startup (TPM_ST_STATE);\r
119 } else {\r
120 Status = Tpm12Startup (TPM_ST_CLEAR);\r
121 }\r
c1d93242
JY
122 if (EFI_ERROR (Status)) {\r
123 return TPM_DEVICE_2_0_DTPM;\r
124 }\r
125\r
126 // NO initialization needed again.\r
127 PcdSet8 (PcdTpmInitializationPolicy, 0);\r
128 return TPM_DEVICE_1_2;\r
129}\r