TianoCore EDK2 master
Loading...
Searching...
No Matches
Tcg2ConfigDriver.c
Go to the documentation of this file.
1
9#include "Tcg2ConfigImpl.h"
10
11extern TPM_INSTANCE_ID mTpmInstanceId[TPM_DEVICE_MAX + 1];
12
21VOID
23 IN VOID *HiiPackage,
24 IN UINTN HiiPackageSize,
25 IN UINT32 PCRBanks
26 )
27{
28 EFI_HII_PACKAGE_HEADER *HiiPackageHeader;
29 EFI_IFR_OP_HEADER *IfrOpCodeHeader;
30 EFI_IFR_CHECKBOX *IfrCheckBox;
31 EFI_IFR_DEFAULT *IfrDefault;
32
33 HiiPackageHeader = (EFI_HII_PACKAGE_HEADER *)HiiPackage;
34
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);
47 }
48
49 break;
50 }
51
52 IfrOpCodeHeader = (EFI_IFR_OP_HEADER *)((UINTN)IfrOpCodeHeader + IfrOpCodeHeader->Length);
53 }
54
55 break;
56 }
57
58 return;
59}
60
72VOID
74 IN TCG2_CONFIG_PRIVATE_DATA *PrivateData
75 )
76{
77 EFI_STATUS Status;
78 EFI_STRING ConfigRequestHdr;
79 BOOLEAN ActionFlag;
80 TCG2_VERSION Tcg2Version;
81 UINTN DataSize;
82 UINT64 PcdTcg2PpiVersion;
83 UINT8 PcdTpm2AcpiTableRev;
84
85 //
86 // Get the PCD value before initializing efi varstore configuration data.
87 //
88 PcdTcg2PpiVersion = 0;
89 CopyMem (
90 &PcdTcg2PpiVersion,
91 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer),
92 AsciiStrSize ((CHAR8 *)PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer))
93 );
94
95 PcdTpm2AcpiTableRev = PcdGet8 (PcdTpm2AcpiTableRev);
96
97 //
98 // Initialize efi varstore configuration data.
99 //
100 ZeroMem (&Tcg2Version, sizeof (Tcg2Version));
101 ConfigRequestHdr = HiiConstructConfigHdr (
102 &gTcg2ConfigFormSetGuid,
103 TCG2_VERSION_NAME,
104 PrivateData->DriverHandle
105 );
106 ASSERT (ConfigRequestHdr != NULL);
107 DataSize = sizeof (Tcg2Version);
108 Status = gRT->GetVariable (
109 TCG2_VERSION_NAME,
110 &gTcg2ConfigFormSetGuid,
111 NULL,
112 &DataSize,
113 &Tcg2Version
114 );
115 if (!EFI_ERROR (Status)) {
116 //
117 // EFI variable does exist and validate current setting.
118 //
119 ActionFlag = HiiValidateSettings (ConfigRequestHdr);
120 if (!ActionFlag) {
121 //
122 // Current configuration is invalid, reset to defaults.
123 //
124 ActionFlag = HiiSetToDefaults (ConfigRequestHdr, EFI_HII_DEFAULT_CLASS_STANDARD);
125 ASSERT (ActionFlag);
126 //
127 // Get the default values from variable.
128 //
129 DataSize = sizeof (Tcg2Version);
130 Status = gRT->GetVariable (
131 TCG2_VERSION_NAME,
132 &gTcg2ConfigFormSetGuid,
133 NULL,
134 &DataSize,
135 &Tcg2Version
136 );
137 ASSERT_EFI_ERROR (Status);
138 }
139 } else {
140 //
141 // EFI variable doesn't exist or variable size is not expected.
142 //
143
144 //
145 // Store zero data Buffer Storage to EFI variable.
146 //
147 Status = gRT->SetVariable (
148 TCG2_VERSION_NAME,
149 &gTcg2ConfigFormSetGuid,
150 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
151 sizeof (Tcg2Version),
152 &Tcg2Version
153 );
154 if (EFI_ERROR (Status)) {
155 DEBUG ((DEBUG_ERROR, "Tcg2ConfigDriver: Fail to set TCG2_VERSION_NAME\n"));
156 return;
157 } else {
158 //
159 // Build this variable based on default values stored in IFR.
160 //
161 ActionFlag = HiiSetToDefaults (ConfigRequestHdr, EFI_HII_DEFAULT_CLASS_STANDARD);
162 ASSERT (ActionFlag);
163 //
164 // Get the default values from variable.
165 //
166 DataSize = sizeof (Tcg2Version);
167 Status = gRT->GetVariable (
168 TCG2_VERSION_NAME,
169 &gTcg2ConfigFormSetGuid,
170 NULL,
171 &DataSize,
172 &Tcg2Version
173 );
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"));
178 }
179
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"));
183 }
184 }
185 }
186
187 FreePool (ConfigRequestHdr);
188
189 //
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.
193 //
194 PcdTcg2PpiVersion = 0;
195 CopyMem (
196 &PcdTcg2PpiVersion,
197 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer),
198 AsciiStrSize ((CHAR8 *)PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer))
199 );
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"));
203 }
204
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);
208 break;
209 case TCG2_PPI_VERSION_1_3:
210 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_PPI_VERSION_STATE_CONTENT), L"1.3", NULL);
211 break;
212 default:
213 ASSERT (FALSE);
214 break;
215 }
216
217 //
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.
221 //
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"));
226 }
227
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);
231 break;
232 case EFI_TPM2_ACPI_TABLE_REVISION_4:
233 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_ACPI_REVISION_STATE_CONTENT), L"Rev 4", NULL);
234 break;
235 default:
236 ASSERT (FALSE);
237 break;
238 }
239}
240
254EFIAPI
256 IN EFI_HANDLE ImageHandle,
257 IN EFI_SYSTEM_TABLE *SystemTable
258 )
259{
260 EFI_STATUS Status;
261 TCG2_CONFIG_PRIVATE_DATA *PrivateData;
262 TCG2_CONFIGURATION Tcg2Configuration;
263 TCG2_DEVICE_DETECTION Tcg2DeviceDetection;
264 UINTN Index;
265 UINTN DataSize;
266 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLockProtocol;
267 UINT32 CurrentActivePCRBanks;
268
269 Status = gBS->OpenProtocol (
270 ImageHandle,
271 &gEfiCallerIdGuid,
272 NULL,
273 ImageHandle,
274 ImageHandle,
275 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
276 );
277 if (!EFI_ERROR (Status)) {
278 return EFI_ALREADY_STARTED;
279 }
280
281 //
282 // Create a private data structure.
283 //
284 PrivateData = AllocateCopyPool (sizeof (TCG2_CONFIG_PRIVATE_DATA), &mTcg2ConfigPrivateDateTemplate);
285 ASSERT (PrivateData != NULL);
286 mTcg2ConfigPrivateDate = PrivateData;
287 //
288 // Install private GUID.
289 //
290 Status = gBS->InstallMultipleProtocolInterfaces (
291 &ImageHandle,
292 &gEfiCallerIdGuid,
293 PrivateData,
294 NULL
295 );
296 ASSERT_EFI_ERROR (Status);
297
298 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **)&PrivateData->Tcg2Protocol);
299 ASSERT_EFI_ERROR (Status);
300
301 PrivateData->ProtocolCapability.Size = sizeof (PrivateData->ProtocolCapability);
302 Status = PrivateData->Tcg2Protocol->GetCapability (
303 PrivateData->Tcg2Protocol,
304 &PrivateData->ProtocolCapability
305 );
306 ASSERT_EFI_ERROR (Status);
307
308 DataSize = sizeof (Tcg2Configuration);
309 Status = gRT->GetVariable (
310 TCG2_STORAGE_NAME,
311 &gTcg2ConfigFormSetGuid,
312 NULL,
313 &DataSize,
314 &Tcg2Configuration
315 );
316 if (EFI_ERROR (Status)) {
317 //
318 // Variable not ready, set default value
319 //
320 Tcg2Configuration.TpmDevice = TPM_DEVICE_DEFAULT;
321 }
322
323 //
324 // Validation
325 //
326 if ((Tcg2Configuration.TpmDevice > TPM_DEVICE_MAX) || (Tcg2Configuration.TpmDevice < TPM_DEVICE_MIN)) {
327 Tcg2Configuration.TpmDevice = TPM_DEVICE_DEFAULT;
328 }
329
330 //
331 // Set value for Tcg2CurrentActivePCRBanks
332 // Search Tcg2ConfigBin[] and update default value there
333 //
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);
338
339 //
340 // Sync data from PCD to variable, so that we do not need detect again in S3 phase.
341 //
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;
346 break;
347 }
348 }
349
350 PrivateData->TpmDeviceDetected = Tcg2DeviceDetection.TpmDeviceDetected;
351 Tcg2Configuration.TpmDevice = Tcg2DeviceDetection.TpmDeviceDetected;
352
353 //
354 // Save to variable so platform driver can get it.
355 //
356 Status = gRT->SetVariable (
357 TCG2_DEVICE_DETECTION_NAME,
358 &gTcg2ConfigFormSetGuid,
359 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
360 sizeof (Tcg2DeviceDetection),
361 &Tcg2DeviceDetection
362 );
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,
369 0,
370 NULL
371 );
372 ASSERT_EFI_ERROR (Status);
373 }
374
375 //
376 // Save to variable so platform driver can get it.
377 //
378 Status = gRT->SetVariable (
379 TCG2_STORAGE_NAME,
380 &gTcg2ConfigFormSetGuid,
381 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
382 sizeof (Tcg2Configuration),
383 &Tcg2Configuration
384 );
385 if (EFI_ERROR (Status)) {
386 DEBUG ((DEBUG_ERROR, "Tcg2ConfigDriver: Fail to set TCG2_STORAGE_NAME\n"));
387 }
388
389 //
390 // We should lock Tcg2DeviceDetection, because it contains information needed at S3.
391 //
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
398 );
399 ASSERT_EFI_ERROR (Status);
400 }
401
402 //
403 // Install Tcg2 configuration form
404 //
405 Status = InstallTcg2ConfigForm (PrivateData);
406 if (EFI_ERROR (Status)) {
407 goto ErrorExit;
408 }
409
410 InitializeTcg2VersionInfo (PrivateData);
411
412 return EFI_SUCCESS;
413
414ErrorExit:
415 if (PrivateData != NULL) {
416 UninstallTcg2ConfigForm (PrivateData);
417 }
418
419 return Status;
420}
421
432EFIAPI
434 IN EFI_HANDLE ImageHandle
435 )
436{
437 EFI_STATUS Status;
438 TCG2_CONFIG_PRIVATE_DATA *PrivateData;
439
440 Status = gBS->HandleProtocol (
441 ImageHandle,
442 &gEfiCallerIdGuid,
443 (VOID **)&PrivateData
444 );
445 if (EFI_ERROR (Status)) {
446 return Status;
447 }
448
449 ASSERT (PrivateData->Signature == TCG2_CONFIG_PRIVATE_DATA_SIGNATURE);
450
451 gBS->UninstallMultipleProtocolInterfaces (
452 ImageHandle,
453 &gEfiCallerIdGuid,
454 PrivateData,
455 NULL
456 );
457
458 UninstallTcg2ConfigForm (PrivateData);
459
460 return EFI_SUCCESS;
461}
UINT64 UINTN
UINTN EFIAPI AsciiStrSize(IN CONST CHAR8 *String)
Definition: String.c:681
UINT32 EFIAPI ReadUnaligned32(IN CONST UINT32 *Buffer)
Definition: Unaligned.c:145
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
EFI_STRING EFIAPI HiiConstructConfigHdr(IN CONST EFI_GUID *Guid OPTIONAL, IN CONST CHAR16 *Name OPTIONAL, IN EFI_HANDLE DriverHandle)
Definition: HiiLib.c:723
EFI_STRING_ID EFIAPI HiiSetString(IN EFI_HII_HANDLE HiiHandle, IN EFI_STRING_ID StringId OPTIONAL, IN CONST EFI_STRING String, IN CONST CHAR8 *SupportedLanguages OPTIONAL)
Definition: HiiString.c:52
BOOLEAN EFIAPI HiiValidateSettings(IN CONST EFI_STRING Request OPTIONAL)
Definition: HiiLib.c:2689
BOOLEAN EFIAPI HiiSetToDefaults(IN CONST EFI_STRING Request OPTIONAL, IN UINT16 DefaultId)
Definition: HiiLib.c:2714
EFI_RUNTIME_SERVICES * gRT
#define NULL
Definition: Base.h:319
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet8(TokenName)
Definition: PcdLib.h:336
#define PcdGetPtr(TokenName)
Definition: PcdLib.h:388
VOID UpdateDefaultPCRBanks(IN VOID *HiiPackage, IN UINTN HiiPackageSize, IN UINT32 PCRBanks)
EFI_STATUS EFIAPI Tcg2ConfigDriverUnload(IN EFI_HANDLE ImageHandle)
VOID InitializeTcg2VersionInfo(IN TCG2_CONFIG_PRIVATE_DATA *PrivateData)
EFI_STATUS EFIAPI Tcg2ConfigDriverEntryPoint(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
VOID UninstallTcg2ConfigForm(IN OUT TCG2_CONFIG_PRIVATE_DATA *PrivateData)
EFI_STATUS InstallTcg2ConfigForm(IN OUT TCG2_CONFIG_PRIVATE_DATA *PrivateData)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
#define STRING_TOKEN(t)
#define EFI_VARIABLE_NON_VOLATILE