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);
52 IfrOpCodeHeader
= (EFI_IFR_OP_HEADER
*)((UINTN
)IfrOpCodeHeader
+ IfrOpCodeHeader
->Length
);
62 Initialize TCG2 version information.
64 This function will initialize efi varstore configuration data for
65 TCG2_VERSION_NAME variable, check the value of related PCD with
66 the variable value and set string for the version state content
67 according to the PCD value.
69 @param[in] PrivateData Points to TCG2 configuration private data.
73 InitializeTcg2VersionInfo (
74 IN TCG2_CONFIG_PRIVATE_DATA
*PrivateData
78 EFI_STRING ConfigRequestHdr
;
80 TCG2_VERSION Tcg2Version
;
82 UINT64 PcdTcg2PpiVersion
;
83 UINT8 PcdTpm2AcpiTableRev
;
86 // Get the PCD value before initializing efi varstore configuration data.
88 PcdTcg2PpiVersion
= 0;
91 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
),
92 AsciiStrSize ((CHAR8
*)PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
))
95 PcdTpm2AcpiTableRev
= PcdGet8 (PcdTpm2AcpiTableRev
);
98 // Initialize efi varstore configuration data.
100 ZeroMem (&Tcg2Version
, sizeof (Tcg2Version
));
101 ConfigRequestHdr
= HiiConstructConfigHdr (
102 &gTcg2ConfigFormSetGuid
,
104 PrivateData
->DriverHandle
106 ASSERT (ConfigRequestHdr
!= NULL
);
107 DataSize
= sizeof (Tcg2Version
);
108 Status
= gRT
->GetVariable (
110 &gTcg2ConfigFormSetGuid
,
115 if (!EFI_ERROR (Status
)) {
117 // EFI variable does exist and validate current setting.
119 ActionFlag
= HiiValidateSettings (ConfigRequestHdr
);
122 // Current configuration is invalid, reset to defaults.
124 ActionFlag
= HiiSetToDefaults (ConfigRequestHdr
, EFI_HII_DEFAULT_CLASS_STANDARD
);
127 // Get the default values from variable.
129 DataSize
= sizeof (Tcg2Version
);
130 Status
= gRT
->GetVariable (
132 &gTcg2ConfigFormSetGuid
,
137 ASSERT_EFI_ERROR (Status
);
141 // EFI variable doesn't exist or variable size is not expected.
145 // Store zero data Buffer Storage to EFI variable.
147 Status
= gRT
->SetVariable (
149 &gTcg2ConfigFormSetGuid
,
150 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
151 sizeof (Tcg2Version
),
154 if (EFI_ERROR (Status
)) {
155 DEBUG ((DEBUG_ERROR
, "Tcg2ConfigDriver: Fail to set TCG2_VERSION_NAME\n"));
159 // Build this variable based on default values stored in IFR.
161 ActionFlag
= HiiSetToDefaults (ConfigRequestHdr
, EFI_HII_DEFAULT_CLASS_STANDARD
);
164 // Get the default values from variable.
166 DataSize
= sizeof (Tcg2Version
);
167 Status
= gRT
->GetVariable (
169 &gTcg2ConfigFormSetGuid
,
174 ASSERT_EFI_ERROR (Status
);
175 if (PcdTcg2PpiVersion
!= Tcg2Version
.PpiVersion
) {
176 DEBUG ((DEBUG_WARN
, "WARNING: PcdTcgPhysicalPresenceInterfaceVer 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"));
180 if (PcdTpm2AcpiTableRev
!= Tcg2Version
.Tpm2AcpiTableRev
) {
181 DEBUG ((DEBUG_WARN
, "WARNING: PcdTpm2AcpiTableRev default value is not same with the default value in VFR\n"));
182 DEBUG ((DEBUG_WARN
, "WARNING: The default value in VFR has be chosen\n"));
187 FreePool (ConfigRequestHdr
);
190 // Get the PCD value again.
191 // If the PCD value is not equal to the value in variable,
192 // the PCD is not DynamicHii type and does not map to the setup option.
194 PcdTcg2PpiVersion
= 0;
197 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
),
198 AsciiStrSize ((CHAR8
*)PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
))
200 if (PcdTcg2PpiVersion
!= Tcg2Version
.PpiVersion
) {
201 DEBUG ((DEBUG_WARN
, "WARNING: PcdTcgPhysicalPresenceInterfaceVer is not DynamicHii type and does not map to TCG2_VERSION.PpiVersion\n"));
202 DEBUG ((DEBUG_WARN
, "WARNING: The TCG2 PPI version configuring from setup page will not work\n"));
205 switch (PcdTcg2PpiVersion
) {
206 case TCG2_PPI_VERSION_1_2
:
207 HiiSetString (PrivateData
->HiiHandle
, STRING_TOKEN (STR_TCG2_PPI_VERSION_STATE_CONTENT
), L
"1.2", NULL
);
209 case TCG2_PPI_VERSION_1_3
:
210 HiiSetString (PrivateData
->HiiHandle
, STRING_TOKEN (STR_TCG2_PPI_VERSION_STATE_CONTENT
), L
"1.3", NULL
);
218 // Get the PcdTpm2AcpiTableRev value again.
219 // If the PCD value is not equal to the value in variable,
220 // the PCD is not DynamicHii type and does not map to TCG2_VERSION Variable.
222 PcdTpm2AcpiTableRev
= PcdGet8 (PcdTpm2AcpiTableRev
);
223 if (PcdTpm2AcpiTableRev
!= Tcg2Version
.Tpm2AcpiTableRev
) {
224 DEBUG ((DEBUG_WARN
, "WARNING: PcdTpm2AcpiTableRev is not DynamicHii type and does not map to TCG2_VERSION.Tpm2AcpiTableRev\n"));
225 DEBUG ((DEBUG_WARN
, "WARNING: The Tpm2 ACPI Revision configuring from setup page will not work\n"));
228 switch (PcdTpm2AcpiTableRev
) {
229 case EFI_TPM2_ACPI_TABLE_REVISION_3
:
230 HiiSetString (PrivateData
->HiiHandle
, STRING_TOKEN (STR_TPM2_ACPI_REVISION_STATE_CONTENT
), L
"Rev 3", NULL
);
232 case EFI_TPM2_ACPI_TABLE_REVISION_4
:
233 HiiSetString (PrivateData
->HiiHandle
, STRING_TOKEN (STR_TPM2_ACPI_REVISION_STATE_CONTENT
), L
"Rev 4", NULL
);
242 The entry point for Tcg2 configuration driver.
244 @param[in] ImageHandle The image handle of the driver.
245 @param[in] SystemTable The system table.
247 @retval EFI_ALREADY_STARTED The driver already exists in system.
248 @retval EFI_OUT_OF_RESOURCES Fail to execute entry point due to lack of resources.
249 @retval EFI_SUCCESS All the related protocols are installed on the driver.
250 @retval Others Fail to install protocols as indicated.
255 Tcg2ConfigDriverEntryPoint (
256 IN EFI_HANDLE ImageHandle
,
257 IN EFI_SYSTEM_TABLE
*SystemTable
261 TCG2_CONFIG_PRIVATE_DATA
*PrivateData
;
262 TCG2_CONFIGURATION Tcg2Configuration
;
263 TCG2_DEVICE_DETECTION Tcg2DeviceDetection
;
266 EDKII_VARIABLE_LOCK_PROTOCOL
*VariableLockProtocol
;
267 UINT32 CurrentActivePCRBanks
;
269 Status
= gBS
->OpenProtocol (
275 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
277 if (!EFI_ERROR (Status
)) {
278 return EFI_ALREADY_STARTED
;
282 // Create a private data structure.
284 PrivateData
= AllocateCopyPool (sizeof (TCG2_CONFIG_PRIVATE_DATA
), &mTcg2ConfigPrivateDateTemplate
);
285 ASSERT (PrivateData
!= NULL
);
286 mTcg2ConfigPrivateDate
= PrivateData
;
288 // Install private GUID.
290 Status
= gBS
->InstallMultipleProtocolInterfaces (
296 ASSERT_EFI_ERROR (Status
);
298 Status
= gBS
->LocateProtocol (&gEfiTcg2ProtocolGuid
, NULL
, (VOID
**)&PrivateData
->Tcg2Protocol
);
299 ASSERT_EFI_ERROR (Status
);
301 PrivateData
->ProtocolCapability
.Size
= sizeof (PrivateData
->ProtocolCapability
);
302 Status
= PrivateData
->Tcg2Protocol
->GetCapability (
303 PrivateData
->Tcg2Protocol
,
304 &PrivateData
->ProtocolCapability
306 ASSERT_EFI_ERROR (Status
);
308 DataSize
= sizeof (Tcg2Configuration
);
309 Status
= gRT
->GetVariable (
311 &gTcg2ConfigFormSetGuid
,
316 if (EFI_ERROR (Status
)) {
318 // Variable not ready, set default value
320 Tcg2Configuration
.TpmDevice
= TPM_DEVICE_DEFAULT
;
326 if ((Tcg2Configuration
.TpmDevice
> TPM_DEVICE_MAX
) || (Tcg2Configuration
.TpmDevice
< TPM_DEVICE_MIN
)) {
327 Tcg2Configuration
.TpmDevice
= TPM_DEVICE_DEFAULT
;
331 // Set value for Tcg2CurrentActivePCRBanks
332 // Search Tcg2ConfigBin[] and update default value there
334 Status
= PrivateData
->Tcg2Protocol
->GetActivePcrBanks (PrivateData
->Tcg2Protocol
, &CurrentActivePCRBanks
);
335 ASSERT_EFI_ERROR (Status
);
336 PrivateData
->PCRBanksDesired
= CurrentActivePCRBanks
;
337 UpdateDefaultPCRBanks (Tcg2ConfigBin
+ sizeof (UINT32
), ReadUnaligned32 ((UINT32
*)Tcg2ConfigBin
) - sizeof (UINT32
), CurrentActivePCRBanks
);
340 // Sync data from PCD to variable, so that we do not need detect again in S3 phase.
342 Tcg2DeviceDetection
.TpmDeviceDetected
= TPM_DEVICE_NULL
;
343 for (Index
= 0; Index
< sizeof (mTpmInstanceId
)/sizeof (mTpmInstanceId
[0]); Index
++) {
344 if (CompareGuid (PcdGetPtr (PcdTpmInstanceGuid
), &mTpmInstanceId
[Index
].TpmInstanceGuid
)) {
345 Tcg2DeviceDetection
.TpmDeviceDetected
= mTpmInstanceId
[Index
].TpmDevice
;
350 PrivateData
->TpmDeviceDetected
= Tcg2DeviceDetection
.TpmDeviceDetected
;
351 Tcg2Configuration
.TpmDevice
= Tcg2DeviceDetection
.TpmDeviceDetected
;
354 // Save to variable so platform driver can get it.
356 Status
= gRT
->SetVariable (
357 TCG2_DEVICE_DETECTION_NAME
,
358 &gTcg2ConfigFormSetGuid
,
359 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
360 sizeof (Tcg2DeviceDetection
),
363 if (EFI_ERROR (Status
)) {
364 DEBUG ((DEBUG_ERROR
, "Tcg2ConfigDriver: Fail to set TCG2_DEVICE_DETECTION_NAME\n"));
365 Status
= gRT
->SetVariable (
366 TCG2_DEVICE_DETECTION_NAME
,
367 &gTcg2ConfigFormSetGuid
,
368 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
372 ASSERT_EFI_ERROR (Status
);
376 // Save to variable so platform driver can get it.
378 Status
= gRT
->SetVariable (
380 &gTcg2ConfigFormSetGuid
,
381 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
382 sizeof (Tcg2Configuration
),
385 if (EFI_ERROR (Status
)) {
386 DEBUG ((DEBUG_ERROR
, "Tcg2ConfigDriver: Fail to set TCG2_STORAGE_NAME\n"));
390 // We should lock Tcg2DeviceDetection, because it contains information needed at S3.
392 Status
= gBS
->LocateProtocol (&gEdkiiVariableLockProtocolGuid
, NULL
, (VOID
**)&VariableLockProtocol
);
393 if (!EFI_ERROR (Status
)) {
394 Status
= VariableLockProtocol
->RequestToLock (
395 VariableLockProtocol
,
396 TCG2_DEVICE_DETECTION_NAME
,
397 &gTcg2ConfigFormSetGuid
399 ASSERT_EFI_ERROR (Status
);
403 // Install Tcg2 configuration form
405 Status
= InstallTcg2ConfigForm (PrivateData
);
406 if (EFI_ERROR (Status
)) {
410 InitializeTcg2VersionInfo (PrivateData
);
415 if (PrivateData
!= NULL
) {
416 UninstallTcg2ConfigForm (PrivateData
);
423 Unload the Tcg2 configuration form.
425 @param[in] ImageHandle The driver's image handle.
427 @retval EFI_SUCCESS The Tcg2 configuration form is unloaded.
428 @retval Others Failed to unload the form.
433 Tcg2ConfigDriverUnload (
434 IN EFI_HANDLE ImageHandle
438 TCG2_CONFIG_PRIVATE_DATA
*PrivateData
;
440 Status
= gBS
->HandleProtocol (
443 (VOID
**)&PrivateData
445 if (EFI_ERROR (Status
)) {
449 ASSERT (PrivateData
->Signature
== TCG2_CONFIG_PRIVATE_DATA_SIGNATURE
);
451 gBS
->UninstallMultipleProtocolInterfaces (
458 UninstallTcg2ConfigForm (PrivateData
);