2 The module entry point for Tcg2 configuration module.
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "Tcg2ConfigImpl.h"
17 extern TPM_INSTANCE_ID mTpmInstanceId
[TPM_DEVICE_MAX
+ 1];
20 Update default PCR banks data.
22 @param[in] HiiPackage HII Package.
23 @param[in] HiiPackageSize HII Package size.
24 @param[in] PCRBanks PCR Banks data.
28 UpdateDefaultPCRBanks (
30 IN UINTN HiiPackageSize
,
34 EFI_HII_PACKAGE_HEADER
*HiiPackageHeader
;
35 EFI_IFR_OP_HEADER
*IfrOpCodeHeader
;
36 EFI_IFR_CHECKBOX
*IfrCheckBox
;
37 EFI_IFR_DEFAULT
*IfrDefault
;
39 HiiPackageHeader
= (EFI_HII_PACKAGE_HEADER
*)HiiPackage
;
41 switch (HiiPackageHeader
->Type
) {
42 case EFI_HII_PACKAGE_FORMS
:
43 IfrOpCodeHeader
= (EFI_IFR_OP_HEADER
*)(HiiPackageHeader
+ 1);
44 while ((UINTN
)IfrOpCodeHeader
< (UINTN
)HiiPackageHeader
+ HiiPackageHeader
->Length
) {
45 switch (IfrOpCodeHeader
->OpCode
) {
46 case EFI_IFR_CHECKBOX_OP
:
47 IfrCheckBox
= (EFI_IFR_CHECKBOX
*)IfrOpCodeHeader
;
48 if ((IfrCheckBox
->Question
.QuestionId
>= KEY_TPM2_PCR_BANKS_REQUEST_0
) && (IfrCheckBox
->Question
.QuestionId
<= KEY_TPM2_PCR_BANKS_REQUEST_4
)) {
49 IfrDefault
= (EFI_IFR_DEFAULT
*)(IfrCheckBox
+ 1);
50 ASSERT (IfrDefault
->Header
.OpCode
== EFI_IFR_DEFAULT_OP
);
51 ASSERT (IfrDefault
->Type
== EFI_IFR_TYPE_BOOLEAN
);
52 IfrDefault
->Value
.b
= (BOOLEAN
)((PCRBanks
>> (IfrCheckBox
->Question
.QuestionId
- KEY_TPM2_PCR_BANKS_REQUEST_0
)) & 0x1);
56 IfrOpCodeHeader
= (EFI_IFR_OP_HEADER
*)((UINTN
)IfrOpCodeHeader
+ IfrOpCodeHeader
->Length
);
64 Initialize TCG2 version information.
66 This function will initialize efi varstore configuration data for
67 TCG2_VERSION_NAME variable, check the value of related PCD with
68 the variable value and set string for the version state content
69 according to the PCD value.
71 @param[in] PrivateData Points to TCG2 configuration private data.
75 InitializeTcg2VersionInfo (
76 IN TCG2_CONFIG_PRIVATE_DATA
*PrivateData
80 EFI_STRING ConfigRequestHdr
;
82 TCG2_VERSION Tcg2Version
;
84 UINT64 PcdTcg2PpiVersion
;
85 UINT8 PcdTpm2AcpiTableRev
;
88 // Get the PCD value before initializing efi varstore configuration data.
90 PcdTcg2PpiVersion
= 0;
93 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
),
94 AsciiStrSize ((CHAR8
*) PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer
))
97 PcdTpm2AcpiTableRev
= PcdGet8 (PcdTpm2AcpiTableRev
);
100 // Initialize efi varstore configuration data.
102 ZeroMem (&Tcg2Version
, sizeof (Tcg2Version
));
103 ConfigRequestHdr
= HiiConstructConfigHdr (
104 &gTcg2ConfigFormSetGuid
,
106 PrivateData
->DriverHandle
108 ASSERT (ConfigRequestHdr
!= NULL
);
109 DataSize
= sizeof (Tcg2Version
);
110 Status
= gRT
->GetVariable (
112 &gTcg2ConfigFormSetGuid
,
117 if (!EFI_ERROR (Status
)) {
119 // EFI variable does exist and validate current setting.
121 ActionFlag
= HiiValidateSettings (ConfigRequestHdr
);
124 // Current configuration is invalid, reset to defaults.
126 ActionFlag
= HiiSetToDefaults (ConfigRequestHdr
, EFI_HII_DEFAULT_CLASS_STANDARD
);
129 // Get the default values from variable.
131 DataSize
= sizeof (Tcg2Version
);
132 Status
= gRT
->GetVariable (
134 &gTcg2ConfigFormSetGuid
,
139 ASSERT_EFI_ERROR (Status
);
143 // EFI variable doesn't exist or variable size is not expected.
147 // Store zero data Buffer Storage to EFI variable.
149 Status
= gRT
->SetVariable (
151 &gTcg2ConfigFormSetGuid
,
152 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
153 sizeof (Tcg2Version
),
156 if (EFI_ERROR (Status
)) {
157 DEBUG ((DEBUG_ERROR
, "Tcg2ConfigDriver: Fail to set TCG2_VERSION_NAME\n"));
161 // Build this variable based on default values stored in IFR.
163 ActionFlag
= HiiSetToDefaults (ConfigRequestHdr
, EFI_HII_DEFAULT_CLASS_STANDARD
);
166 // Get the default values from variable.
168 DataSize
= sizeof (Tcg2Version
);
169 Status
= gRT
->GetVariable (
171 &gTcg2ConfigFormSetGuid
,
176 ASSERT_EFI_ERROR (Status
);
177 if (PcdTcg2PpiVersion
!= Tcg2Version
.PpiVersion
) {
178 DEBUG ((DEBUG_WARN
, "WARNING: PcdTcgPhysicalPresenceInterfaceVer default value is not same with the default value in VFR\n"));
179 DEBUG ((DEBUG_WARN
, "WARNING: The default value in VFR has be chosen\n"));
181 if (PcdTpm2AcpiTableRev
!= Tcg2Version
.Tpm2AcpiTableRev
) {
182 DEBUG ((DEBUG_WARN
, "WARNING: PcdTpm2AcpiTableRev default value is not same with the default value in VFR\n"));
183 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_SUCCES 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 ((EFI_D_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 ((EFI_D_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
);