2 The module entry point for Tcg2 configuration module.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "Tcg2ConfigImpl.h"
11 extern TPM_INSTANCE_ID mTpmInstanceId
[TPM_DEVICE_MAX
+ 1];
14 Update default PCR banks data.
16 @param[in] HiiPackage HII Package.
17 @param[in] HiiPackageSize HII Package size.
18 @param[in] PCRBanks PCR Banks data.
22 UpdateDefaultPCRBanks (
24 IN UINTN HiiPackageSize
,
28 EFI_HII_PACKAGE_HEADER
*HiiPackageHeader
;
29 EFI_IFR_OP_HEADER
*IfrOpCodeHeader
;
30 EFI_IFR_CHECKBOX
*IfrCheckBox
;
31 EFI_IFR_DEFAULT
*IfrDefault
;
33 HiiPackageHeader
= (EFI_HII_PACKAGE_HEADER
*)HiiPackage
;
35 switch (HiiPackageHeader
->Type
) {
36 case EFI_HII_PACKAGE_FORMS
:
37 IfrOpCodeHeader
= (EFI_IFR_OP_HEADER
*)(HiiPackageHeader
+ 1);
38 while ((UINTN
)IfrOpCodeHeader
< (UINTN
)HiiPackageHeader
+ HiiPackageHeader
->Length
) {
39 switch (IfrOpCodeHeader
->OpCode
) {
40 case EFI_IFR_CHECKBOX_OP
:
41 IfrCheckBox
= (EFI_IFR_CHECKBOX
*)IfrOpCodeHeader
;
42 if ((IfrCheckBox
->Question
.QuestionId
>= KEY_TPM2_PCR_BANKS_REQUEST_0
) && (IfrCheckBox
->Question
.QuestionId
<= KEY_TPM2_PCR_BANKS_REQUEST_4
)) {
43 IfrDefault
= (EFI_IFR_DEFAULT
*)(IfrCheckBox
+ 1);
44 ASSERT (IfrDefault
->Header
.OpCode
== EFI_IFR_DEFAULT_OP
);
45 ASSERT (IfrDefault
->Type
== EFI_IFR_TYPE_BOOLEAN
);
46 IfrDefault
->Value
.b
= (BOOLEAN
)((PCRBanks
>> (IfrCheckBox
->Question
.QuestionId
- KEY_TPM2_PCR_BANKS_REQUEST_0
)) & 0x1);
50 IfrOpCodeHeader
= (EFI_IFR_OP_HEADER
*)((UINTN
)IfrOpCodeHeader
+ IfrOpCodeHeader
->Length
);
58 Initialize TCG2 version information.
60 This function will initialize efi varstore configuration data for
61 TCG2_VERSION_NAME variable, check the value of related PCD with
62 the variable value and set string for the version state content
63 according to the PCD value.
65 @param[in] PrivateData Points to TCG2 configuration private data.
69 InitializeTcg2VersionInfo (
70 IN TCG2_CONFIG_PRIVATE_DATA
*PrivateData
74 EFI_STRING ConfigRequestHdr
;
76 TCG2_VERSION Tcg2Version
;
78 UINT64 PcdTcg2PpiVersion
;
79 UINT8 PcdTpm2AcpiTableRev
;
82 // Get the PCD value before initializing efi varstore configuration data.
84 PcdTcg2PpiVersion
= 0;
87 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
),
88 AsciiStrSize ((CHAR8
*) PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
))
91 PcdTpm2AcpiTableRev
= PcdGet8 (PcdTpm2AcpiTableRev
);
94 // Initialize efi varstore configuration data.
96 ZeroMem (&Tcg2Version
, sizeof (Tcg2Version
));
97 ConfigRequestHdr
= HiiConstructConfigHdr (
98 &gTcg2ConfigFormSetGuid
,
100 PrivateData
->DriverHandle
102 ASSERT (ConfigRequestHdr
!= NULL
);
103 DataSize
= sizeof (Tcg2Version
);
104 Status
= gRT
->GetVariable (
106 &gTcg2ConfigFormSetGuid
,
111 if (!EFI_ERROR (Status
)) {
113 // EFI variable does exist and validate current setting.
115 ActionFlag
= HiiValidateSettings (ConfigRequestHdr
);
118 // Current configuration is invalid, reset to defaults.
120 ActionFlag
= HiiSetToDefaults (ConfigRequestHdr
, EFI_HII_DEFAULT_CLASS_STANDARD
);
123 // Get the default values from variable.
125 DataSize
= sizeof (Tcg2Version
);
126 Status
= gRT
->GetVariable (
128 &gTcg2ConfigFormSetGuid
,
133 ASSERT_EFI_ERROR (Status
);
137 // EFI variable doesn't exist or variable size is not expected.
141 // Store zero data Buffer Storage to EFI variable.
143 Status
= gRT
->SetVariable (
145 &gTcg2ConfigFormSetGuid
,
146 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
147 sizeof (Tcg2Version
),
150 if (EFI_ERROR (Status
)) {
151 DEBUG ((DEBUG_ERROR
, "Tcg2ConfigDriver: Fail to set TCG2_VERSION_NAME\n"));
155 // Build this variable based on default values stored in IFR.
157 ActionFlag
= HiiSetToDefaults (ConfigRequestHdr
, EFI_HII_DEFAULT_CLASS_STANDARD
);
160 // Get the default values from variable.
162 DataSize
= sizeof (Tcg2Version
);
163 Status
= gRT
->GetVariable (
165 &gTcg2ConfigFormSetGuid
,
170 ASSERT_EFI_ERROR (Status
);
171 if (PcdTcg2PpiVersion
!= Tcg2Version
.PpiVersion
) {
172 DEBUG ((DEBUG_WARN
, "WARNING: PcdTcgPhysicalPresenceInterfaceVer default value is not same with the default value in VFR\n"));
173 DEBUG ((DEBUG_WARN
, "WARNING: The default value in VFR has be chosen\n"));
175 if (PcdTpm2AcpiTableRev
!= Tcg2Version
.Tpm2AcpiTableRev
) {
176 DEBUG ((DEBUG_WARN
, "WARNING: PcdTpm2AcpiTableRev default value is not same with the default value in VFR\n"));
177 DEBUG ((DEBUG_WARN
, "WARNING: The default value in VFR has be chosen\n"));
181 FreePool (ConfigRequestHdr
);
184 // Get the PCD value again.
185 // If the PCD value is not equal to the value in variable,
186 // the PCD is not DynamicHii type and does not map to the setup option.
188 PcdTcg2PpiVersion
= 0;
191 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
),
192 AsciiStrSize ((CHAR8
*) PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
))
194 if (PcdTcg2PpiVersion
!= Tcg2Version
.PpiVersion
) {
195 DEBUG ((DEBUG_WARN
, "WARNING: PcdTcgPhysicalPresenceInterfaceVer is not DynamicHii type and does not map to TCG2_VERSION.PpiVersion\n"));
196 DEBUG ((DEBUG_WARN
, "WARNING: The TCG2 PPI version configuring from setup page will not work\n"));
199 switch (PcdTcg2PpiVersion
) {
200 case TCG2_PPI_VERSION_1_2
:
201 HiiSetString (PrivateData
->HiiHandle
, STRING_TOKEN (STR_TCG2_PPI_VERSION_STATE_CONTENT
), L
"1.2", NULL
);
203 case TCG2_PPI_VERSION_1_3
:
204 HiiSetString (PrivateData
->HiiHandle
, STRING_TOKEN (STR_TCG2_PPI_VERSION_STATE_CONTENT
), L
"1.3", NULL
);
212 // Get the PcdTpm2AcpiTableRev value again.
213 // If the PCD value is not equal to the value in variable,
214 // the PCD is not DynamicHii type and does not map to TCG2_VERSION Variable.
216 PcdTpm2AcpiTableRev
= PcdGet8 (PcdTpm2AcpiTableRev
);
217 if (PcdTpm2AcpiTableRev
!= Tcg2Version
.Tpm2AcpiTableRev
) {
218 DEBUG ((DEBUG_WARN
, "WARNING: PcdTpm2AcpiTableRev is not DynamicHii type and does not map to TCG2_VERSION.Tpm2AcpiTableRev\n"));
219 DEBUG ((DEBUG_WARN
, "WARNING: The Tpm2 ACPI Revision configuring from setup page will not work\n"));
222 switch (PcdTpm2AcpiTableRev
) {
223 case EFI_TPM2_ACPI_TABLE_REVISION_3
:
224 HiiSetString (PrivateData
->HiiHandle
, STRING_TOKEN (STR_TPM2_ACPI_REVISION_STATE_CONTENT
), L
"Rev 3", NULL
);
226 case EFI_TPM2_ACPI_TABLE_REVISION_4
:
227 HiiSetString (PrivateData
->HiiHandle
, STRING_TOKEN (STR_TPM2_ACPI_REVISION_STATE_CONTENT
), L
"Rev 4", NULL
);
236 The entry point for Tcg2 configuration driver.
238 @param[in] ImageHandle The image handle of the driver.
239 @param[in] SystemTable The system table.
241 @retval EFI_ALREADY_STARTED The driver already exists in system.
242 @retval EFI_OUT_OF_RESOURCES Fail to execute entry point due to lack of resources.
243 @retval EFI_SUCCES All the related protocols are installed on the driver.
244 @retval Others Fail to install protocols as indicated.
249 Tcg2ConfigDriverEntryPoint (
250 IN EFI_HANDLE ImageHandle
,
251 IN EFI_SYSTEM_TABLE
*SystemTable
255 TCG2_CONFIG_PRIVATE_DATA
*PrivateData
;
256 TCG2_CONFIGURATION Tcg2Configuration
;
257 TCG2_DEVICE_DETECTION Tcg2DeviceDetection
;
260 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLockProtocol
;
261 UINT32 CurrentActivePCRBanks
;
263 Status
= gBS
->OpenProtocol (
269 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
271 if (!EFI_ERROR (Status
)) {
272 return EFI_ALREADY_STARTED
;
276 // Create a private data structure.
278 PrivateData
= AllocateCopyPool (sizeof (TCG2_CONFIG_PRIVATE_DATA
), &mTcg2ConfigPrivateDateTemplate
);
279 ASSERT (PrivateData
!= NULL
);
280 mTcg2ConfigPrivateDate
= PrivateData
;
282 // Install private GUID.
284 Status
= gBS
->InstallMultipleProtocolInterfaces (
290 ASSERT_EFI_ERROR (Status
);
292 Status
= gBS
->LocateProtocol (&gEfiTcg2ProtocolGuid
, NULL
, (VOID
**) &PrivateData
->Tcg2Protocol
);
293 ASSERT_EFI_ERROR (Status
);
295 PrivateData
->ProtocolCapability
.Size
= sizeof(PrivateData
->ProtocolCapability
);
296 Status
= PrivateData
->Tcg2Protocol
->GetCapability (
297 PrivateData
->Tcg2Protocol
,
298 &PrivateData
->ProtocolCapability
300 ASSERT_EFI_ERROR (Status
);
302 DataSize
= sizeof(Tcg2Configuration
);
303 Status
= gRT
->GetVariable (
305 &gTcg2ConfigFormSetGuid
,
310 if (EFI_ERROR (Status
)) {
312 // Variable not ready, set default value
314 Tcg2Configuration
.TpmDevice
= TPM_DEVICE_DEFAULT
;
320 if ((Tcg2Configuration
.TpmDevice
> TPM_DEVICE_MAX
) || (Tcg2Configuration
.TpmDevice
< TPM_DEVICE_MIN
)) {
321 Tcg2Configuration
.TpmDevice
= TPM_DEVICE_DEFAULT
;
325 // Set value for Tcg2CurrentActivePCRBanks
326 // Search Tcg2ConfigBin[] and update default value there
328 Status
= PrivateData
->Tcg2Protocol
->GetActivePcrBanks (PrivateData
->Tcg2Protocol
, &CurrentActivePCRBanks
);
329 ASSERT_EFI_ERROR (Status
);
330 PrivateData
->PCRBanksDesired
= CurrentActivePCRBanks
;
331 UpdateDefaultPCRBanks (Tcg2ConfigBin
+ sizeof(UINT32
), ReadUnaligned32((UINT32
*)Tcg2ConfigBin
) - sizeof(UINT32
), CurrentActivePCRBanks
);
334 // Sync data from PCD to variable, so that we do not need detect again in S3 phase.
336 Tcg2DeviceDetection
.TpmDeviceDetected
= TPM_DEVICE_NULL
;
337 for (Index
= 0; Index
< sizeof(mTpmInstanceId
)/sizeof(mTpmInstanceId
[0]); Index
++) {
338 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &mTpmInstanceId
[Index
].TpmInstanceGuid
)) {
339 Tcg2DeviceDetection
.TpmDeviceDetected
= mTpmInstanceId
[Index
].TpmDevice
;
344 PrivateData
->TpmDeviceDetected
= Tcg2DeviceDetection
.TpmDeviceDetected
;
345 Tcg2Configuration
.TpmDevice
= Tcg2DeviceDetection
.TpmDeviceDetected
;
348 // Save to variable so platform driver can get it.
350 Status
= gRT
->SetVariable (
351 TCG2_DEVICE_DETECTION_NAME
,
352 &gTcg2ConfigFormSetGuid
,
353 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
354 sizeof(Tcg2DeviceDetection
),
357 if (EFI_ERROR (Status
)) {
358 DEBUG ((EFI_D_ERROR
, "Tcg2ConfigDriver: Fail to set TCG2_DEVICE_DETECTION_NAME\n"));
359 Status
= gRT
->SetVariable (
360 TCG2_DEVICE_DETECTION_NAME
,
361 &gTcg2ConfigFormSetGuid
,
362 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
366 ASSERT_EFI_ERROR (Status
);
370 // Save to variable so platform driver can get it.
372 Status
= gRT
->SetVariable (
374 &gTcg2ConfigFormSetGuid
,
375 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
376 sizeof(Tcg2Configuration
),
379 if (EFI_ERROR (Status
)) {
380 DEBUG ((EFI_D_ERROR
, "Tcg2ConfigDriver: Fail to set TCG2_STORAGE_NAME\n"));
384 // We should lock Tcg2DeviceDetection, because it contains information needed at S3.
386 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**)&VariableLockProtocol
);
387 if (!EFI_ERROR (Status
)) {
388 Status
= VariableLockProtocol
->RequestToLock (
389 VariableLockProtocol
,
390 TCG2_DEVICE_DETECTION_NAME
,
391 &gTcg2ConfigFormSetGuid
393 ASSERT_EFI_ERROR (Status
);
397 // Install Tcg2 configuration form
399 Status
= InstallTcg2ConfigForm (PrivateData
);
400 if (EFI_ERROR (Status
)) {
404 InitializeTcg2VersionInfo (PrivateData
);
409 if (PrivateData
!= NULL
) {
410 UninstallTcg2ConfigForm (PrivateData
);
417 Unload the Tcg2 configuration form.
419 @param[in] ImageHandle The driver's image handle.
421 @retval EFI_SUCCESS The Tcg2 configuration form is unloaded.
422 @retval Others Failed to unload the form.
427 Tcg2ConfigDriverUnload (
428 IN EFI_HANDLE ImageHandle
432 TCG2_CONFIG_PRIVATE_DATA
*PrivateData
;
434 Status
= gBS
->HandleProtocol (
437 (VOID
**) &PrivateData
439 if (EFI_ERROR (Status
)) {
443 ASSERT (PrivateData
->Signature
== TCG2_CONFIG_PRIVATE_DATA_SIGNATURE
);
445 gBS
->UninstallMultipleProtocolInterfaces (
452 UninstallTcg2ConfigForm (PrivateData
);