]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Tcg2Config/Tcg2ConfigPeim.c
Add TPM2 support defined in trusted computing group.
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Config / Tcg2ConfigPeim.c
CommitLineData
1abfa4ce
JY
1/** @file\r
2 The module entry point for Tcg2 configuration module.\r
3\r
4Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
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
17\r
18#include <Guid/TpmInstance.h>\r
19\r
20#include <Library/BaseLib.h>\r
21#include <Library/BaseMemoryLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/MemoryAllocationLib.h>\r
24#include <Library/PeiServicesLib.h>\r
25#include <Library/PcdLib.h>\r
26\r
27#include <Ppi/ReadOnlyVariable2.h>\r
28#include <Ppi/TpmInitialized.h>\r
29#include <Protocol/Tcg2Protocol.h>\r
30\r
31#include "Tcg2ConfigNvData.h"\r
32\r
33TPM_INSTANCE_ID mTpmInstanceId[] = TPM_INSTANCE_ID_LIST;\r
34\r
35CONST EFI_PEI_PPI_DESCRIPTOR gTpmSelectedPpi = {\r
36 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
37 &gEfiTpmDeviceSelectedGuid,\r
38 NULL\r
39};\r
40\r
41EFI_PEI_PPI_DESCRIPTOR mTpmInitializationDonePpiList = {\r
42 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
43 &gPeiTpmInitializationDonePpiGuid,\r
44 NULL\r
45};\r
46\r
47/**\r
48 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.\r
49\r
50 @param SetupTpmDevice TpmDevice configuration in setup driver\r
51\r
52 @return TpmDevice configuration\r
53**/\r
54UINT8\r
55DetectTpmDevice (\r
56 IN UINT8 SetupTpmDevice\r
57 );\r
58\r
59/**\r
60 The entry point for Tcg2 configuration driver.\r
61\r
62 @param FileHandle Handle of the file being invoked.\r
63 @param PeiServices Describes the list of possible PEI Services.\r
64\r
65 @retval EFI_SUCCES Convert variable to PCD successfully.\r
66 @retval Others Fail to convert variable to PCD.\r
67**/\r
68EFI_STATUS\r
69EFIAPI\r
70Tcg2ConfigPeimEntryPoint (\r
71 IN EFI_PEI_FILE_HANDLE FileHandle,\r
72 IN CONST EFI_PEI_SERVICES **PeiServices\r
73 )\r
74{\r
75 UINTN Size;\r
76 EFI_STATUS Status;\r
77 EFI_STATUS Status2;\r
78 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;\r
79 TCG2_CONFIGURATION Tcg2Configuration;\r
80 UINTN Index;\r
81 UINT8 TpmDevice;\r
82\r
83 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);\r
84 ASSERT_EFI_ERROR (Status);\r
85\r
86 Size = sizeof(Tcg2Configuration);\r
87 Status = VariablePpi->GetVariable (\r
88 VariablePpi,\r
89 TCG2_STORAGE_NAME,\r
90 &gTcg2ConfigFormSetGuid,\r
91 NULL,\r
92 &Size,\r
93 &Tcg2Configuration\r
94 );\r
95 if (EFI_ERROR (Status)) {\r
96 //\r
97 // Variable not ready, set default value\r
98 //\r
99 Tcg2Configuration.TpmDevice = TPM_DEVICE_DEFAULT;\r
100 }\r
101\r
102 //\r
103 // Validation\r
104 //\r
105 if ((Tcg2Configuration.TpmDevice > TPM_DEVICE_MAX) || (Tcg2Configuration.TpmDevice < TPM_DEVICE_MIN)) {\r
106 Tcg2Configuration.TpmDevice = TPM_DEVICE_DEFAULT;\r
107 }\r
108\r
109 //\r
110 // Although we have SetupVariable info, we still need detect TPM device manually.\r
111 //\r
112 DEBUG ((EFI_D_INFO, "Tcg2Configuration.TpmDevice from Setup: %x\n", Tcg2Configuration.TpmDevice));\r
113\r
114 if (PcdGetBool (PcdTpmAutoDetection)) {\r
115 TpmDevice = DetectTpmDevice (Tcg2Configuration.TpmDevice);\r
116 DEBUG ((EFI_D_INFO, "TpmDevice final: %x\n", TpmDevice));\r
117 if (TpmDevice != TPM_DEVICE_NULL) {\r
118 Tcg2Configuration.TpmDevice = TpmDevice;\r
119 }\r
120 } else {\r
121 TpmDevice = Tcg2Configuration.TpmDevice;\r
122 }\r
123\r
124 //\r
125 // Convert variable to PCD.\r
126 // This is work-around because there is no gurantee DynamicHiiPcd can return correct value in DXE phase.\r
127 // Using DynamicPcd instead.\r
128 //\r
129 // NOTE: Tcg2Configuration variable contains the desired TpmDevice type,\r
130 // while PcdTpmInstanceGuid PCD contains the real detected TpmDevice type\r
131 //\r
132 for (Index = 0; Index < sizeof(mTpmInstanceId)/sizeof(mTpmInstanceId[0]); Index++) {\r
133 if (TpmDevice == mTpmInstanceId[Index].TpmDevice) {\r
134 Size = sizeof(mTpmInstanceId[Index].TpmInstanceGuid);\r
135 PcdSetPtr (PcdTpmInstanceGuid, &Size, &mTpmInstanceId[Index].TpmInstanceGuid);\r
136 DEBUG ((EFI_D_INFO, "TpmDevice PCD: %g\n", &mTpmInstanceId[Index].TpmInstanceGuid));\r
137 break;\r
138 }\r
139 }\r
140\r
141 //\r
142 // Selection done\r
143 //\r
144 Status = PeiServicesInstallPpi (&gTpmSelectedPpi);\r
145 ASSERT_EFI_ERROR (Status);\r
146\r
147 //\r
148 // Even if no TPM is selected or detected, we still need intall TpmInitializationDonePpi.\r
149 // Because TcgPei or Tcg2Pei will not run, but we still need a way to notify other driver.\r
150 // Other driver can know TPM initialization state by TpmInitializedPpi.\r
151 //\r
152 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid)) {\r
153 Status2 = PeiServicesInstallPpi (&mTpmInitializationDonePpiList);\r
154 ASSERT_EFI_ERROR (Status2);\r
155 }\r
156\r
157 return Status;\r
158}\r