]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Tcg2Config/Tcg2ConfigImpl.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Config / Tcg2ConfigImpl.c
1 /** @file
2 HII Config Access protocol implementation of TCG2 configuration module.
3 NOTE: This module is only for reference only, each platform should have its own setup page.
4
5 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2018 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "Tcg2ConfigImpl.h"
12 #include <Library/PcdLib.h>
13 #include <Library/Tpm2CommandLib.h>
14 #include <Library/Tpm2DeviceLib.h>
15 #include <Library/IoLib.h>
16
17 #include <Guid/TpmInstance.h>
18
19 #include <IndustryStandard/TpmPtp.h>
20
21 #define EFI_TCG2_EVENT_LOG_FORMAT_ALL (EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 | EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
22
23 TPM_INSTANCE_ID mTpmInstanceId[TPM_DEVICE_MAX + 1] = TPM_INSTANCE_ID_LIST;
24
25 TCG2_CONFIG_PRIVATE_DATA *mTcg2ConfigPrivateDate;
26 TCG2_CONFIG_PRIVATE_DATA mTcg2ConfigPrivateDateTemplate = {
27 TCG2_CONFIG_PRIVATE_DATA_SIGNATURE,
28 {
29 Tcg2ExtractConfig,
30 Tcg2RouteConfig,
31 Tcg2Callback
32 }
33 };
34
35 HII_VENDOR_DEVICE_PATH mTcg2HiiVendorDevicePath = {
36 {
37 {
38 HARDWARE_DEVICE_PATH,
39 HW_VENDOR_DP,
40 {
41 (UINT8)(sizeof (VENDOR_DEVICE_PATH)),
42 (UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
43 }
44 },
45 TCG2_CONFIG_FORM_SET_GUID
46 },
47 {
48 END_DEVICE_PATH_TYPE,
49 END_ENTIRE_DEVICE_PATH_SUBTYPE,
50 {
51 (UINT8)(END_DEVICE_PATH_LENGTH),
52 (UINT8)((END_DEVICE_PATH_LENGTH) >> 8)
53 }
54 }
55 };
56
57 UINT8 mCurrentPpRequest;
58
59 /**
60 Return if PTP CRB is supported.
61
62 @param[in] Register Pointer to PTP register.
63
64 @retval TRUE PTP CRB is supported.
65 @retval FALSE PTP CRB is unsupported.
66 **/
67 BOOLEAN
68 IsPtpCrbSupported (
69 IN VOID *Register
70 )
71 {
72 PTP_CRB_INTERFACE_IDENTIFIER InterfaceId;
73
74 //
75 // Check interface id
76 //
77 InterfaceId.Uint32 = MmioRead32 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->InterfaceId);
78
79 if (((InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_CRB) ||
80 (InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO)) &&
81 (InterfaceId.Bits.CapCRB != 0))
82 {
83 return TRUE;
84 }
85
86 return FALSE;
87 }
88
89 /**
90 Return if PTP FIFO is supported.
91
92 @param[in] Register Pointer to PTP register.
93
94 @retval TRUE PTP FIFO is supported.
95 @retval FALSE PTP FIFO is unsupported.
96 **/
97 BOOLEAN
98 IsPtpFifoSupported (
99 IN VOID *Register
100 )
101 {
102 PTP_CRB_INTERFACE_IDENTIFIER InterfaceId;
103
104 //
105 // Check interface id
106 //
107 InterfaceId.Uint32 = MmioRead32 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->InterfaceId);
108
109 if (((InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_CRB) ||
110 (InterfaceId.Bits.InterfaceType == PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO)) &&
111 (InterfaceId.Bits.CapFIFO != 0))
112 {
113 return TRUE;
114 }
115
116 return FALSE;
117 }
118
119 /**
120 Set PTP interface type.
121 Do not update PcdActiveTpmInterfaceType here because interface change only happens on next _TPM_INIT
122
123 @param[in] Register Pointer to PTP register.
124 @param[in] PtpInterface PTP interface type.
125
126 @retval EFI_SUCCESS PTP interface type is set.
127 @retval EFI_INVALID_PARAMETER PTP interface type is invalid.
128 @retval EFI_UNSUPPORTED PTP interface type is unsupported.
129 @retval EFI_WRITE_PROTECTED PTP interface is locked.
130 **/
131 EFI_STATUS
132 SetPtpInterface (
133 IN VOID *Register,
134 IN UINT8 PtpInterface
135 )
136 {
137 TPM2_PTP_INTERFACE_TYPE PtpInterfaceCurrent;
138 PTP_CRB_INTERFACE_IDENTIFIER InterfaceId;
139
140 PtpInterfaceCurrent = PcdGet8 (PcdActiveTpmInterfaceType);
141 if ((PtpInterfaceCurrent != Tpm2PtpInterfaceFifo) &&
142 (PtpInterfaceCurrent != Tpm2PtpInterfaceCrb))
143 {
144 return EFI_UNSUPPORTED;
145 }
146
147 InterfaceId.Uint32 = MmioRead32 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->InterfaceId);
148 if (InterfaceId.Bits.IntfSelLock != 0) {
149 return EFI_WRITE_PROTECTED;
150 }
151
152 switch (PtpInterface) {
153 case Tpm2PtpInterfaceFifo:
154 if (InterfaceId.Bits.CapFIFO == 0) {
155 return EFI_UNSUPPORTED;
156 }
157
158 InterfaceId.Bits.InterfaceSelector = PTP_INTERFACE_IDENTIFIER_INTERFACE_SELECTOR_FIFO;
159 MmioWrite32 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->InterfaceId, InterfaceId.Uint32);
160 return EFI_SUCCESS;
161 case Tpm2PtpInterfaceCrb:
162 if (InterfaceId.Bits.CapCRB == 0) {
163 return EFI_UNSUPPORTED;
164 }
165
166 InterfaceId.Bits.InterfaceSelector = PTP_INTERFACE_IDENTIFIER_INTERFACE_SELECTOR_CRB;
167 MmioWrite32 ((UINTN)&((PTP_CRB_REGISTERS *)Register)->InterfaceId, InterfaceId.Uint32);
168 return EFI_SUCCESS;
169 default:
170 return EFI_INVALID_PARAMETER;
171 }
172 }
173
174 /**
175 This function allows a caller to extract the current configuration for one
176 or more named elements from the target driver.
177
178 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
179 @param[in] Request A null-terminated Unicode string in
180 <ConfigRequest> format.
181 @param[out] Progress On return, points to a character in the Request
182 string. Points to the string's null terminator if
183 request was successful. Points to the most recent
184 '&' before the first failing name/value pair (or
185 the beginning of the string if the failure is in
186 the first name/value pair) if the request was not
187 successful.
188 @param[out] Results A null-terminated Unicode string in
189 <ConfigAltResp> format which has all values filled
190 in for the names in the Request string. String to
191 be allocated by the called function.
192
193 @retval EFI_SUCCESS The Results is filled with the requested values.
194 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
195 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
196 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
197 driver.
198
199 **/
200 EFI_STATUS
201 EFIAPI
202 Tcg2ExtractConfig (
203 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
204 IN CONST EFI_STRING Request,
205 OUT EFI_STRING *Progress,
206 OUT EFI_STRING *Results
207 )
208 {
209 if ((Progress == NULL) || (Results == NULL)) {
210 return EFI_INVALID_PARAMETER;
211 }
212
213 *Progress = Request;
214 return EFI_NOT_FOUND;
215 }
216
217 /**
218 Save TPM request to variable space.
219
220 @param[in] PpRequest Physical Presence request command.
221
222 @retval EFI_SUCCESS The operation is finished successfully.
223 @retval Others Other errors as indicated.
224
225 **/
226 EFI_STATUS
227 SaveTcg2PpRequest (
228 IN UINT8 PpRequest
229 )
230 {
231 UINT32 ReturnCode;
232 EFI_STATUS Status;
233
234 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (PpRequest, 0);
235 if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS) {
236 mCurrentPpRequest = PpRequest;
237 Status = EFI_SUCCESS;
238 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE) {
239 Status = EFI_OUT_OF_RESOURCES;
240 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED) {
241 Status = EFI_UNSUPPORTED;
242 } else {
243 Status = EFI_DEVICE_ERROR;
244 }
245
246 return Status;
247 }
248
249 /**
250 Save TPM request to variable space.
251
252 @param[in] PpRequestParameter Physical Presence request parameter.
253
254 @retval EFI_SUCCESS The operation is finished successfully.
255 @retval Others Other errors as indicated.
256
257 **/
258 EFI_STATUS
259 SaveTcg2PpRequestParameter (
260 IN UINT32 PpRequestParameter
261 )
262 {
263 UINT32 ReturnCode;
264 EFI_STATUS Status;
265
266 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (mCurrentPpRequest, PpRequestParameter);
267 if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS) {
268 Status = EFI_SUCCESS;
269 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE) {
270 Status = EFI_OUT_OF_RESOURCES;
271 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED) {
272 Status = EFI_UNSUPPORTED;
273 } else {
274 Status = EFI_DEVICE_ERROR;
275 }
276
277 return Status;
278 }
279
280 /**
281 Save Tcg2 PCR Banks request request to variable space.
282
283 @param[in] PCRBankIndex PCR Bank Index.
284 @param[in] Enable Enable or disable this PCR Bank.
285
286 @retval EFI_SUCCESS The operation is finished successfully.
287 @retval Others Other errors as indicated.
288
289 **/
290 EFI_STATUS
291 SaveTcg2PCRBanksRequest (
292 IN UINTN PCRBankIndex,
293 IN BOOLEAN Enable
294 )
295 {
296 UINT32 ReturnCode;
297 EFI_STATUS Status;
298
299 if (Enable) {
300 mTcg2ConfigPrivateDate->PCRBanksDesired |= (0x1 << PCRBankIndex);
301 } else {
302 mTcg2ConfigPrivateDate->PCRBanksDesired &= ~(0x1 << PCRBankIndex);
303 }
304
305 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_SET_PCR_BANKS, mTcg2ConfigPrivateDate->PCRBanksDesired);
306 if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS) {
307 Status = EFI_SUCCESS;
308 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE) {
309 Status = EFI_OUT_OF_RESOURCES;
310 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED) {
311 Status = EFI_UNSUPPORTED;
312 } else {
313 Status = EFI_DEVICE_ERROR;
314 }
315
316 return Status;
317 }
318
319 /**
320 This function processes the results of changes in configuration.
321
322 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
323 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>
324 format.
325 @param[out] Progress A pointer to a string filled in with the offset of
326 the most recent '&' before the first failing
327 name/value pair (or the beginning of the string if
328 the failure is in the first name/value pair) or
329 the terminating NULL if all was successful.
330
331 @retval EFI_SUCCESS The Results is processed successfully.
332 @retval EFI_INVALID_PARAMETER Configuration is NULL.
333 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
334 driver.
335
336 **/
337 EFI_STATUS
338 EFIAPI
339 Tcg2RouteConfig (
340 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
341 IN CONST EFI_STRING Configuration,
342 OUT EFI_STRING *Progress
343 )
344 {
345 if ((Configuration == NULL) || (Progress == NULL)) {
346 return EFI_INVALID_PARAMETER;
347 }
348
349 *Progress = Configuration;
350
351 return EFI_NOT_FOUND;
352 }
353
354 /**
355 Get HID string of TPM2 ACPI device object
356
357 @param[in] Hid Points to HID String Buffer.
358 @param[in] Size HID String size in bytes. Must >= TPM_HID_ACPI_SIZE
359
360 @return HID String get status.
361
362 **/
363 EFI_STATUS
364 GetTpm2HID (
365 CHAR8 *Hid,
366 UINTN Size
367 )
368 {
369 EFI_STATUS Status;
370 UINT32 ManufacturerID;
371 UINT32 FirmwareVersion1;
372 UINT32 FirmwareVersion2;
373 BOOLEAN PnpHID;
374
375 PnpHID = TRUE;
376
377 ZeroMem (Hid, Size);
378
379 //
380 // Get Manufacturer ID
381 //
382 Status = Tpm2GetCapabilityManufactureID (&ManufacturerID);
383 if (!EFI_ERROR (Status)) {
384 DEBUG ((DEBUG_INFO, "TPM_PT_MANUFACTURER 0x%08x\n", ManufacturerID));
385 //
386 // ManufacturerID defined in TCG Vendor ID Registry
387 // may tailed with 0x00 or 0x20
388 //
389 if (((ManufacturerID >> 24) == 0x00) || ((ManufacturerID >> 24) == 0x20)) {
390 //
391 // HID containing PNP ID "NNN####"
392 // NNN is uppercase letter for Vendor ID specified by manufacturer
393 //
394 CopyMem (Hid, &ManufacturerID, 3);
395 } else {
396 //
397 // HID containing ACP ID "NNNN####"
398 // NNNN is uppercase letter for Vendor ID specified by manufacturer
399 //
400 CopyMem (Hid, &ManufacturerID, 4);
401 PnpHID = FALSE;
402 }
403 } else {
404 DEBUG ((DEBUG_ERROR, "Get TPM_PT_MANUFACTURER failed %x!\n", Status));
405 ASSERT (FALSE);
406 return Status;
407 }
408
409 Status = Tpm2GetCapabilityFirmwareVersion (&FirmwareVersion1, &FirmwareVersion2);
410 if (!EFI_ERROR (Status)) {
411 DEBUG ((DEBUG_INFO, "TPM_PT_FIRMWARE_VERSION_1 0x%x\n", FirmwareVersion1));
412 DEBUG ((DEBUG_INFO, "TPM_PT_FIRMWARE_VERSION_2 0x%x\n", FirmwareVersion2));
413 //
414 // #### is Firmware Version 1
415 //
416 if (PnpHID) {
417 AsciiSPrint (Hid + 3, TPM_HID_PNP_SIZE - 3, "%02d%02d", ((FirmwareVersion1 & 0xFFFF0000) >> 16), (FirmwareVersion1 & 0x0000FFFF));
418 } else {
419 AsciiSPrint (Hid + 4, TPM_HID_ACPI_SIZE - 4, "%02d%02d", ((FirmwareVersion1 & 0xFFFF0000) >> 16), (FirmwareVersion1 & 0x0000FFFF));
420 }
421 } else {
422 DEBUG ((DEBUG_ERROR, "Get TPM_PT_FIRMWARE_VERSION_X failed %x!\n", Status));
423 ASSERT (FALSE);
424 return Status;
425 }
426
427 return EFI_SUCCESS;
428 }
429
430 /**
431 This function processes the results of changes in configuration
432 for TCG2 version information.
433
434 @param[in] Action Specifies the type of action taken by the browser.
435 ASSERT if the Action is not EFI_BROWSER_ACTION_SUBMITTED.
436 @param[in] QuestionId A unique value which is sent to the original
437 exporting driver so that it can identify the type
438 of data to expect.
439 @param[in] Type The type of value for the question.
440 @param[in] Value A pointer to the data being sent to the original
441 exporting driver.
442
443 @retval EFI_SUCCESS The callback successfully handled the action.
444
445 **/
446 EFI_STATUS
447 Tcg2VersionInfoCallback (
448 IN EFI_BROWSER_ACTION Action,
449 IN EFI_QUESTION_ID QuestionId,
450 IN UINT8 Type,
451 IN EFI_IFR_TYPE_VALUE *Value
452 )
453 {
454 EFI_INPUT_KEY Key;
455 UINT64 PcdTcg2PpiVersion;
456 UINT8 PcdTpm2AcpiTableRev;
457
458 ASSERT (Action == EFI_BROWSER_ACTION_SUBMITTED);
459
460 if (QuestionId == KEY_TCG2_PPI_VERSION) {
461 //
462 // Get the PCD value after EFI_BROWSER_ACTION_SUBMITTED,
463 // the SetVariable to TCG2_VERSION_NAME should have been done.
464 // If the PCD value is not equal to the value set to variable,
465 // the PCD is not DynamicHii type and does not map to the setup option.
466 //
467 PcdTcg2PpiVersion = 0;
468 CopyMem (
469 &PcdTcg2PpiVersion,
470 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer),
471 AsciiStrSize ((CHAR8 *)PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer))
472 );
473 if (PcdTcg2PpiVersion != Value->u64) {
474 CreatePopUp (
475 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
476 &Key,
477 L"WARNING: PcdTcgPhysicalPresenceInterfaceVer is not DynamicHii type and does not map to this option!",
478 L"The version configuring by this setup option will not work!",
479 NULL
480 );
481 }
482 } else if (QuestionId == KEY_TPM2_ACPI_REVISION) {
483 //
484 // Get the PCD value after EFI_BROWSER_ACTION_SUBMITTED,
485 // the SetVariable to TCG2_VERSION_NAME should have been done.
486 // If the PCD value is not equal to the value set to variable,
487 // the PCD is not DynamicHii type and does not map to the setup option.
488 //
489 PcdTpm2AcpiTableRev = PcdGet8 (PcdTpm2AcpiTableRev);
490
491 if (PcdTpm2AcpiTableRev != Value->u8) {
492 CreatePopUp (
493 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
494 &Key,
495 L"WARNING: PcdTpm2AcpiTableRev is not DynamicHii type and does not map to this option!",
496 L"The Revision configuring by this setup option will not work!",
497 NULL
498 );
499 }
500 }
501
502 return EFI_SUCCESS;
503 }
504
505 /**
506 This function processes the results of changes in configuration.
507
508 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
509 @param[in] Action Specifies the type of action taken by the browser.
510 @param[in] QuestionId A unique value which is sent to the original
511 exporting driver so that it can identify the type
512 of data to expect.
513 @param[in] Type The type of value for the question.
514 @param[in] Value A pointer to the data being sent to the original
515 exporting driver.
516 @param[out] ActionRequest On return, points to the action requested by the
517 callback function.
518
519 @retval EFI_SUCCESS The callback successfully handled the action.
520 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
521 variable and its data.
522 @retval EFI_DEVICE_ERROR The variable could not be saved.
523 @retval EFI_UNSUPPORTED The specified Action is not supported by the
524 callback.
525
526 **/
527 EFI_STATUS
528 EFIAPI
529 Tcg2Callback (
530 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
531 IN EFI_BROWSER_ACTION Action,
532 IN EFI_QUESTION_ID QuestionId,
533 IN UINT8 Type,
534 IN EFI_IFR_TYPE_VALUE *Value,
535 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
536 )
537 {
538 EFI_STATUS Status;
539 EFI_INPUT_KEY Key;
540 CHAR8 HidStr[16];
541 CHAR16 UnHidStr[16];
542 TCG2_CONFIG_PRIVATE_DATA *Private;
543
544 if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) {
545 return EFI_INVALID_PARAMETER;
546 }
547
548 Private = TCG2_CONFIG_PRIVATE_DATA_FROM_THIS (This);
549
550 if (Action == EFI_BROWSER_ACTION_FORM_OPEN) {
551 //
552 // Update TPM2 HID info
553 //
554 if (QuestionId == KEY_TPM_DEVICE) {
555 Status = GetTpm2HID (HidStr, 16);
556
557 if (EFI_ERROR (Status)) {
558 //
559 // Fail to get TPM2 HID
560 //
561 HiiSetString (Private->HiiHandle, STRING_TOKEN (STR_TPM2_ACPI_HID_CONTENT), L"Unknown", NULL);
562 } else {
563 AsciiStrToUnicodeStrS (HidStr, UnHidStr, 16);
564 HiiSetString (Private->HiiHandle, STRING_TOKEN (STR_TPM2_ACPI_HID_CONTENT), UnHidStr, NULL);
565 }
566 }
567
568 return EFI_SUCCESS;
569 }
570
571 if (Action == EFI_BROWSER_ACTION_CHANGING) {
572 if (QuestionId == KEY_TPM_DEVICE_INTERFACE) {
573 Status = SetPtpInterface ((VOID *)(UINTN)PcdGet64 (PcdTpmBaseAddress), Value->u8);
574 if (EFI_ERROR (Status)) {
575 CreatePopUp (
576 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
577 &Key,
578 L"Error: Fail to set PTP interface!",
579 NULL
580 );
581 return EFI_DEVICE_ERROR;
582 }
583 }
584 }
585
586 if (Action == EFI_BROWSER_ACTION_CHANGED) {
587 if (QuestionId == KEY_TPM_DEVICE) {
588 return EFI_SUCCESS;
589 }
590
591 if (QuestionId == KEY_TPM2_OPERATION) {
592 return SaveTcg2PpRequest (Value->u8);
593 }
594
595 if (QuestionId == KEY_TPM2_OPERATION_PARAMETER) {
596 return SaveTcg2PpRequestParameter (Value->u32);
597 }
598
599 if ((QuestionId >= KEY_TPM2_PCR_BANKS_REQUEST_0) && (QuestionId <= KEY_TPM2_PCR_BANKS_REQUEST_4)) {
600 return SaveTcg2PCRBanksRequest (QuestionId - KEY_TPM2_PCR_BANKS_REQUEST_0, Value->b);
601 }
602 }
603
604 if (Action == EFI_BROWSER_ACTION_SUBMITTED) {
605 if ((QuestionId == KEY_TCG2_PPI_VERSION) || (QuestionId == KEY_TPM2_ACPI_REVISION)) {
606 return Tcg2VersionInfoCallback (Action, QuestionId, Type, Value);
607 }
608 }
609
610 return EFI_UNSUPPORTED;
611 }
612
613 /**
614 Append Buffer With TpmAlgHash.
615
616 @param[in] Buffer Buffer to be appended.
617 @param[in] BufferSize Size of buffer.
618 @param[in] TpmAlgHash TpmAlgHash.
619
620 **/
621 VOID
622 AppendBufferWithTpmAlgHash (
623 IN UINT16 *Buffer,
624 IN UINTN BufferSize,
625 IN UINT32 TpmAlgHash
626 )
627 {
628 switch (TpmAlgHash) {
629 case TPM_ALG_SHA1:
630 if (Buffer[0] != 0) {
631 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
632 }
633
634 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA1");
635 break;
636 case TPM_ALG_SHA256:
637 if (Buffer[0] != 0) {
638 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
639 }
640
641 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA256");
642 break;
643 case TPM_ALG_SHA384:
644 if (Buffer[0] != 0) {
645 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
646 }
647
648 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA384");
649 break;
650 case TPM_ALG_SHA512:
651 if (Buffer[0] != 0) {
652 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
653 }
654
655 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA512");
656 break;
657 case TPM_ALG_SM3_256:
658 if (Buffer[0] != 0) {
659 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
660 }
661
662 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SM3_256");
663 break;
664 }
665 }
666
667 /**
668 Fill Buffer With BootHashAlg.
669
670 @param[in] Buffer Buffer to be filled.
671 @param[in] BufferSize Size of buffer.
672 @param[in] BootHashAlg BootHashAlg.
673
674 **/
675 VOID
676 FillBufferWithBootHashAlg (
677 IN UINT16 *Buffer,
678 IN UINTN BufferSize,
679 IN UINT32 BootHashAlg
680 )
681 {
682 Buffer[0] = 0;
683 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA1) != 0) {
684 if (Buffer[0] != 0) {
685 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
686 }
687
688 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA1");
689 }
690
691 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA256) != 0) {
692 if (Buffer[0] != 0) {
693 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
694 }
695
696 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA256");
697 }
698
699 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA384) != 0) {
700 if (Buffer[0] != 0) {
701 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
702 }
703
704 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA384");
705 }
706
707 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA512) != 0) {
708 if (Buffer[0] != 0) {
709 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
710 }
711
712 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA512");
713 }
714
715 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SM3_256) != 0) {
716 if (Buffer[0] != 0) {
717 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
718 }
719
720 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SM3_256");
721 }
722 }
723
724 /**
725 Set ConfigInfo according to TpmAlgHash.
726
727 @param[in,out] Tcg2ConfigInfo TCG2 config info.
728 @param[in] TpmAlgHash TpmAlgHash.
729
730 **/
731 VOID
732 SetConfigInfo (
733 IN OUT TCG2_CONFIGURATION_INFO *Tcg2ConfigInfo,
734 IN UINT32 TpmAlgHash
735 )
736 {
737 switch (TpmAlgHash) {
738 case TPM_ALG_SHA1:
739 Tcg2ConfigInfo->Sha1Supported = TRUE;
740 break;
741 case TPM_ALG_SHA256:
742 Tcg2ConfigInfo->Sha256Supported = TRUE;
743 break;
744 case TPM_ALG_SHA384:
745 Tcg2ConfigInfo->Sha384Supported = TRUE;
746 break;
747 case TPM_ALG_SHA512:
748 Tcg2ConfigInfo->Sha512Supported = TRUE;
749 break;
750 case TPM_ALG_SM3_256:
751 Tcg2ConfigInfo->Sm3Supported = TRUE;
752 break;
753 }
754 }
755
756 /**
757 Fill Buffer With TCG2EventLogFormat.
758
759 @param[in] Buffer Buffer to be filled.
760 @param[in] BufferSize Size of buffer.
761 @param[in] TCG2EventLogFormat TCG2EventLogFormat.
762
763 **/
764 VOID
765 FillBufferWithTCG2EventLogFormat (
766 IN UINT16 *Buffer,
767 IN UINTN BufferSize,
768 IN UINT32 TCG2EventLogFormat
769 )
770 {
771 Buffer[0] = 0;
772 if ((TCG2EventLogFormat & EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2) != 0) {
773 if (Buffer[0] != 0) {
774 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
775 }
776
777 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"TCG_1_2");
778 }
779
780 if ((TCG2EventLogFormat & EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) != 0) {
781 if (Buffer[0] != 0) {
782 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
783 }
784
785 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"TCG_2");
786 }
787
788 if ((TCG2EventLogFormat & (~EFI_TCG2_EVENT_LOG_FORMAT_ALL)) != 0) {
789 if (Buffer[0] != 0) {
790 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
791 }
792
793 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"UNKNOWN");
794 }
795 }
796
797 /**
798 This function publish the TCG2 configuration Form for TPM device.
799
800 @param[in, out] PrivateData Points to TCG2 configuration private data.
801
802 @retval EFI_SUCCESS HII Form is installed for this network device.
803 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
804 @retval Others Other errors as indicated.
805
806 **/
807 EFI_STATUS
808 InstallTcg2ConfigForm (
809 IN OUT TCG2_CONFIG_PRIVATE_DATA *PrivateData
810 )
811 {
812 EFI_STATUS Status;
813 EFI_HII_HANDLE HiiHandle;
814 EFI_HANDLE DriverHandle;
815 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
816 UINTN Index;
817 TPML_PCR_SELECTION Pcrs;
818 CHAR16 TempBuffer[1024];
819 TCG2_CONFIGURATION_INFO Tcg2ConfigInfo;
820 TPM2_PTP_INTERFACE_TYPE TpmDeviceInterfaceDetected;
821 BOOLEAN IsCmdImp = FALSE;
822
823 DriverHandle = NULL;
824 ConfigAccess = &PrivateData->ConfigAccess;
825 Status = gBS->InstallMultipleProtocolInterfaces (
826 &DriverHandle,
827 &gEfiDevicePathProtocolGuid,
828 &mTcg2HiiVendorDevicePath,
829 &gEfiHiiConfigAccessProtocolGuid,
830 ConfigAccess,
831 NULL
832 );
833 if (EFI_ERROR (Status)) {
834 return Status;
835 }
836
837 PrivateData->DriverHandle = DriverHandle;
838
839 //
840 // Publish the HII package list
841 //
842 HiiHandle = HiiAddPackages (
843 &gTcg2ConfigFormSetGuid,
844 DriverHandle,
845 Tcg2ConfigDxeStrings,
846 Tcg2ConfigBin,
847 NULL
848 );
849 if (HiiHandle == NULL) {
850 gBS->UninstallMultipleProtocolInterfaces (
851 DriverHandle,
852 &gEfiDevicePathProtocolGuid,
853 &mTcg2HiiVendorDevicePath,
854 &gEfiHiiConfigAccessProtocolGuid,
855 ConfigAccess,
856 NULL
857 );
858
859 return EFI_OUT_OF_RESOURCES;
860 }
861
862 PrivateData->HiiHandle = HiiHandle;
863
864 //
865 // Update static data
866 //
867 switch (PrivateData->TpmDeviceDetected) {
868 case TPM_DEVICE_NULL:
869 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"Not Found", NULL);
870 break;
871 case TPM_DEVICE_1_2:
872 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"TPM 1.2", NULL);
873 break;
874 case TPM_DEVICE_2_0_DTPM:
875 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"TPM 2.0", NULL);
876 break;
877 default:
878 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"Unknown", NULL);
879 break;
880 }
881
882 ZeroMem (&Tcg2ConfigInfo, sizeof (Tcg2ConfigInfo));
883 Status = Tpm2GetCapabilityPcrs (&Pcrs);
884 if (EFI_ERROR (Status)) {
885 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_ACTIVE_HASH_ALGO_CONTENT), L"[Unknown]", NULL);
886 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_SUPPORTED_HASH_ALGO_CONTENT), L"[Unknown]", NULL);
887 } else {
888 TempBuffer[0] = 0;
889 for (Index = 0; Index < Pcrs.count; Index++) {
890 if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
891 AppendBufferWithTpmAlgHash (TempBuffer, sizeof (TempBuffer), Pcrs.pcrSelections[Index].hash);
892 }
893 }
894
895 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_ACTIVE_HASH_ALGO_CONTENT), TempBuffer, NULL);
896
897 TempBuffer[0] = 0;
898 for (Index = 0; Index < Pcrs.count; Index++) {
899 AppendBufferWithTpmAlgHash (TempBuffer, sizeof (TempBuffer), Pcrs.pcrSelections[Index].hash);
900 SetConfigInfo (&Tcg2ConfigInfo, Pcrs.pcrSelections[Index].hash);
901 }
902
903 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_SUPPORTED_HASH_ALGO_CONTENT), TempBuffer, NULL);
904 }
905
906 Status = Tpm2GetCapabilityIsCommandImplemented (TPM_CC_ChangeEPS, &IsCmdImp);
907 if (EFI_ERROR (Status)) {
908 DEBUG ((DEBUG_ERROR, "Tpm2GetCapabilityIsCmdImpl fails %r\n", Status));
909 }
910
911 Tcg2ConfigInfo.ChangeEPSSupported = IsCmdImp;
912
913 FillBufferWithBootHashAlg (TempBuffer, sizeof (TempBuffer), PcdGet32 (PcdTcg2HashAlgorithmBitmap));
914 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_BIOS_HASH_ALGO_CONTENT), TempBuffer, NULL);
915
916 //
917 // Tcg2 Capability
918 //
919 FillBufferWithTCG2EventLogFormat (TempBuffer, sizeof (TempBuffer), PrivateData->ProtocolCapability.SupportedEventLogs);
920 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_SUPPORTED_EVENT_LOG_FORMAT_CONTENT), TempBuffer, NULL);
921
922 FillBufferWithBootHashAlg (TempBuffer, sizeof (TempBuffer), PrivateData->ProtocolCapability.HashAlgorithmBitmap);
923 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_HASH_ALGO_BITMAP_CONTENT), TempBuffer, NULL);
924
925 UnicodeSPrint (TempBuffer, sizeof (TempBuffer), L"%d", PrivateData->ProtocolCapability.NumberOfPCRBanks);
926 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_NUMBER_OF_PCR_BANKS_CONTENT), TempBuffer, NULL);
927
928 FillBufferWithBootHashAlg (TempBuffer, sizeof (TempBuffer), PrivateData->ProtocolCapability.ActivePcrBanks);
929 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_ACTIVE_PCR_BANKS_CONTENT), TempBuffer, NULL);
930
931 //
932 // Update TPM device interface type
933 //
934 if (PrivateData->TpmDeviceDetected == TPM_DEVICE_2_0_DTPM) {
935 TpmDeviceInterfaceDetected = PcdGet8 (PcdActiveTpmInterfaceType);
936 switch (TpmDeviceInterfaceDetected) {
937 case Tpm2PtpInterfaceTis:
938 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"TIS", NULL);
939 break;
940 case Tpm2PtpInterfaceFifo:
941 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"PTP FIFO", NULL);
942 break;
943 case Tpm2PtpInterfaceCrb:
944 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"PTP CRB", NULL);
945 break;
946 default:
947 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"Unknown", NULL);
948 break;
949 }
950
951 Tcg2ConfigInfo.TpmDeviceInterfaceAttempt = TpmDeviceInterfaceDetected;
952 switch (TpmDeviceInterfaceDetected) {
953 case Tpm2PtpInterfaceTis:
954 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = FALSE;
955 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = FALSE;
956 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), L"TIS", NULL);
957 break;
958 case Tpm2PtpInterfaceFifo:
959 case Tpm2PtpInterfaceCrb:
960 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = IsPtpFifoSupported ((VOID *)(UINTN)PcdGet64 (PcdTpmBaseAddress));
961 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = IsPtpCrbSupported ((VOID *)(UINTN)PcdGet64 (PcdTpmBaseAddress));
962 TempBuffer[0] = 0;
963 if (Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported) {
964 if (TempBuffer[0] != 0) {
965 StrCatS (TempBuffer, sizeof (TempBuffer) / sizeof (CHAR16), L", ");
966 }
967
968 StrCatS (TempBuffer, sizeof (TempBuffer) / sizeof (CHAR16), L"PTP FIFO");
969 }
970
971 if (Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported) {
972 if (TempBuffer[0] != 0) {
973 StrCatS (TempBuffer, sizeof (TempBuffer) / sizeof (CHAR16), L", ");
974 }
975
976 StrCatS (TempBuffer, sizeof (TempBuffer) / sizeof (CHAR16), L"PTP CRB");
977 }
978
979 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), TempBuffer, NULL);
980 break;
981 default:
982 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = FALSE;
983 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = FALSE;
984 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), L"Unknown", NULL);
985 break;
986 }
987 }
988
989 //
990 // Set ConfigInfo, to control the check box.
991 //
992 Status = gRT->SetVariable (
993 TCG2_STORAGE_INFO_NAME,
994 &gTcg2ConfigFormSetGuid,
995 EFI_VARIABLE_BOOTSERVICE_ACCESS,
996 sizeof (Tcg2ConfigInfo),
997 &Tcg2ConfigInfo
998 );
999 if (EFI_ERROR (Status)) {
1000 DEBUG ((DEBUG_ERROR, "Tcg2ConfigDriver: Fail to set TCG2_STORAGE_INFO_NAME\n"));
1001 }
1002
1003 return EFI_SUCCESS;
1004 }
1005
1006 /**
1007 This function removes TCG2 configuration Form.
1008
1009 @param[in, out] PrivateData Points to TCG2 configuration private data.
1010
1011 **/
1012 VOID
1013 UninstallTcg2ConfigForm (
1014 IN OUT TCG2_CONFIG_PRIVATE_DATA *PrivateData
1015 )
1016 {
1017 //
1018 // Uninstall HII package list
1019 //
1020 if (PrivateData->HiiHandle != NULL) {
1021 HiiRemovePackages (PrivateData->HiiHandle);
1022 PrivateData->HiiHandle = NULL;
1023 }
1024
1025 //
1026 // Uninstall HII Config Access Protocol
1027 //
1028 if (PrivateData->DriverHandle != NULL) {
1029 gBS->UninstallMultipleProtocolInterfaces (
1030 PrivateData->DriverHandle,
1031 &gEfiDevicePathProtocolGuid,
1032 &mTcg2HiiVendorDevicePath,
1033 &gEfiHiiConfigAccessProtocolGuid,
1034 &PrivateData->ConfigAccess,
1035 NULL
1036 );
1037 PrivateData->DriverHandle = NULL;
1038 }
1039
1040 FreePool (PrivateData);
1041 }