]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Tcg2Config/Tcg2ConfigDriver.c
SecurityPkg: Clean up source files
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Config / Tcg2ConfigDriver.c
1 /** @file
2 The module entry point for Tcg2 configuration module.
3
4 Copyright (c) 2015 - 2018, 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
9
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.
12
13 **/
14
15 #include "Tcg2ConfigImpl.h"
16
17 extern TPM_INSTANCE_ID mTpmInstanceId[TPM_DEVICE_MAX + 1];
18
19 /**
20 Update default PCR banks data.
21
22 @param[in] HiiPackage HII Package.
23 @param[in] HiiPackageSize HII Package size.
24 @param[in] PCRBanks PCR Banks data.
25
26 **/
27 VOID
28 UpdateDefaultPCRBanks (
29 IN VOID *HiiPackage,
30 IN UINTN HiiPackageSize,
31 IN UINT32 PCRBanks
32 )
33 {
34 EFI_HII_PACKAGE_HEADER *HiiPackageHeader;
35 EFI_IFR_OP_HEADER *IfrOpCodeHeader;
36 EFI_IFR_CHECKBOX *IfrCheckBox;
37 EFI_IFR_DEFAULT *IfrDefault;
38
39 HiiPackageHeader = (EFI_HII_PACKAGE_HEADER *)HiiPackage;
40
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);
53 }
54 break;
55 }
56 IfrOpCodeHeader = (EFI_IFR_OP_HEADER *)((UINTN)IfrOpCodeHeader + IfrOpCodeHeader->Length);
57 }
58 break;
59 }
60 return ;
61 }
62
63 /**
64 Initialize TCG2 version information.
65
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.
70
71 @param[in] PrivateData Points to TCG2 configuration private data.
72
73 **/
74 VOID
75 InitializeTcg2VersionInfo (
76 IN TCG2_CONFIG_PRIVATE_DATA *PrivateData
77 )
78 {
79 EFI_STATUS Status;
80 EFI_STRING ConfigRequestHdr;
81 BOOLEAN ActionFlag;
82 TCG2_VERSION Tcg2Version;
83 UINTN DataSize;
84 UINT64 PcdTcg2PpiVersion;
85 UINT8 PcdTpm2AcpiTableRev;
86
87 //
88 // Get the PCD value before initializing efi varstore configuration data.
89 //
90 PcdTcg2PpiVersion = 0;
91 CopyMem (
92 &PcdTcg2PpiVersion,
93 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer),
94 AsciiStrSize ((CHAR8 *) PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer))
95 );
96
97 PcdTpm2AcpiTableRev = PcdGet8 (PcdTpm2AcpiTableRev);
98
99 //
100 // Initialize efi varstore configuration data.
101 //
102 ZeroMem (&Tcg2Version, sizeof (Tcg2Version));
103 ConfigRequestHdr = HiiConstructConfigHdr (
104 &gTcg2ConfigFormSetGuid,
105 TCG2_VERSION_NAME,
106 PrivateData->DriverHandle
107 );
108 ASSERT (ConfigRequestHdr != NULL);
109 DataSize = sizeof (Tcg2Version);
110 Status = gRT->GetVariable (
111 TCG2_VERSION_NAME,
112 &gTcg2ConfigFormSetGuid,
113 NULL,
114 &DataSize,
115 &Tcg2Version
116 );
117 if (!EFI_ERROR (Status)) {
118 //
119 // EFI variable does exist and validate current setting.
120 //
121 ActionFlag = HiiValidateSettings (ConfigRequestHdr);
122 if (!ActionFlag) {
123 //
124 // Current configuration is invalid, reset to defaults.
125 //
126 ActionFlag = HiiSetToDefaults (ConfigRequestHdr, EFI_HII_DEFAULT_CLASS_STANDARD);
127 ASSERT (ActionFlag);
128 //
129 // Get the default values from variable.
130 //
131 DataSize = sizeof (Tcg2Version);
132 Status = gRT->GetVariable (
133 TCG2_VERSION_NAME,
134 &gTcg2ConfigFormSetGuid,
135 NULL,
136 &DataSize,
137 &Tcg2Version
138 );
139 ASSERT_EFI_ERROR (Status);
140 }
141 } else {
142 //
143 // EFI variable doesn't exist or variable size is not expected.
144 //
145
146 //
147 // Store zero data Buffer Storage to EFI variable.
148 //
149 Status = gRT->SetVariable (
150 TCG2_VERSION_NAME,
151 &gTcg2ConfigFormSetGuid,
152 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
153 sizeof (Tcg2Version),
154 &Tcg2Version
155 );
156 if (EFI_ERROR (Status)) {
157 DEBUG ((DEBUG_ERROR, "Tcg2ConfigDriver: Fail to set TCG2_VERSION_NAME\n"));
158 return;
159 } else {
160 //
161 // Build this variable based on default values stored in IFR.
162 //
163 ActionFlag = HiiSetToDefaults (ConfigRequestHdr, EFI_HII_DEFAULT_CLASS_STANDARD);
164 ASSERT (ActionFlag);
165 //
166 // Get the default values from variable.
167 //
168 DataSize = sizeof (Tcg2Version);
169 Status = gRT->GetVariable (
170 TCG2_VERSION_NAME,
171 &gTcg2ConfigFormSetGuid,
172 NULL,
173 &DataSize,
174 &Tcg2Version
175 );
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"));
180 }
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"));
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
241 /**
242 The entry point for Tcg2 configuration driver.
243
244 @param[in] ImageHandle The image handle of the driver.
245 @param[in] SystemTable The system table.
246
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.
251
252 **/
253 EFI_STATUS
254 EFIAPI
255 Tcg2ConfigDriverEntryPoint (
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 ((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,
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 ((EFI_D_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
414 ErrorExit:
415 if (PrivateData != NULL) {
416 UninstallTcg2ConfigForm (PrivateData);
417 }
418
419 return Status;
420 }
421
422 /**
423 Unload the Tcg2 configuration form.
424
425 @param[in] ImageHandle The driver's image handle.
426
427 @retval EFI_SUCCESS The Tcg2 configuration form is unloaded.
428 @retval Others Failed to unload the form.
429
430 **/
431 EFI_STATUS
432 EFIAPI
433 Tcg2ConfigDriverUnload (
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 }