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