]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Tcg2Config/Tcg2ConfigImpl.c
SecurityPkg: Tcg2Config: TPM2 ACPI Table Rev Option
[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 UINT8 PcdTpm2AcpiTableRev;
485
486 ASSERT (Action == EFI_BROWSER_ACTION_SUBMITTED);
487
488 if (QuestionId == KEY_TCG2_PPI_VERSION) {
489 //
490 // Get the PCD value after EFI_BROWSER_ACTION_SUBMITTED,
491 // the SetVariable to TCG2_VERSION_NAME should have been done.
492 // If the PCD value is not equal to the value set to variable,
493 // the PCD is not DynamicHii type and does not map to the setup option.
494 //
495 PcdTcg2PpiVersion = 0;
496 CopyMem (
497 &PcdTcg2PpiVersion,
498 PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer),
499 AsciiStrSize ((CHAR8 *) PcdGetPtr (PcdTcgPhysicalPresenceInterfaceVer))
500 );
501 if (PcdTcg2PpiVersion != Value->u64) {
502 CreatePopUp (
503 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
504 &Key,
505 L"WARNING: PcdTcgPhysicalPresenceInterfaceVer is not DynamicHii type and does not map to this option!",
506 L"The version configuring by this setup option will not work!",
507 NULL
508 );
509 }
510 } else if (QuestionId == KEY_TPM2_ACPI_REVISION){
511 //
512 // Get the PCD value after EFI_BROWSER_ACTION_SUBMITTED,
513 // the SetVariable to TCG2_VERSION_NAME should have been done.
514 // If the PCD value is not equal to the value set to variable,
515 // the PCD is not DynamicHii type and does not map to the setup option.
516 //
517 PcdTpm2AcpiTableRev = PcdGet8 (PcdTpm2AcpiTableRev);
518
519 if (PcdTpm2AcpiTableRev != Value->u8) {
520 CreatePopUp (
521 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
522 &Key,
523 L"WARNING: PcdTpm2AcpiTableRev is not DynamicHii type and does not map to this option!",
524 L"The Revision configuring by this setup option will not work!",
525 NULL
526 );
527 }
528 }
529
530 return EFI_SUCCESS;
531 }
532
533 /**
534 This function processes the results of changes in configuration.
535
536 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
537 @param[in] Action Specifies the type of action taken by the browser.
538 @param[in] QuestionId A unique value which is sent to the original
539 exporting driver so that it can identify the type
540 of data to expect.
541 @param[in] Type The type of value for the question.
542 @param[in] Value A pointer to the data being sent to the original
543 exporting driver.
544 @param[out] ActionRequest On return, points to the action requested by the
545 callback function.
546
547 @retval EFI_SUCCESS The callback successfully handled the action.
548 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
549 variable and its data.
550 @retval EFI_DEVICE_ERROR The variable could not be saved.
551 @retval EFI_UNSUPPORTED The specified Action is not supported by the
552 callback.
553
554 **/
555 EFI_STATUS
556 EFIAPI
557 Tcg2Callback (
558 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
559 IN EFI_BROWSER_ACTION Action,
560 IN EFI_QUESTION_ID QuestionId,
561 IN UINT8 Type,
562 IN EFI_IFR_TYPE_VALUE *Value,
563 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
564 )
565 {
566 EFI_STATUS Status;
567 EFI_INPUT_KEY Key;
568 CHAR8 HidStr[16];
569 CHAR16 UnHidStr[16];
570 TCG2_CONFIG_PRIVATE_DATA *Private;
571
572 if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) {
573 return EFI_INVALID_PARAMETER;
574 }
575
576 Private = TCG2_CONFIG_PRIVATE_DATA_FROM_THIS (This);
577
578 if (Action == EFI_BROWSER_ACTION_FORM_OPEN) {
579 //
580 // Update TPM2 HID info
581 //
582 if (QuestionId == KEY_TPM_DEVICE) {
583 Status = GetTpm2HID(HidStr, 16);
584
585 if (EFI_ERROR(Status)) {
586 //
587 // Fail to get TPM2 HID
588 //
589 HiiSetString (Private->HiiHandle, STRING_TOKEN (STR_TPM2_ACPI_HID_CONTENT), L"Unknown", NULL);
590 } else {
591 AsciiStrToUnicodeStrS(HidStr, UnHidStr, 16);
592 HiiSetString (Private->HiiHandle, STRING_TOKEN (STR_TPM2_ACPI_HID_CONTENT), UnHidStr, NULL);
593 }
594 }
595 return EFI_SUCCESS;
596 }
597
598 if (Action == EFI_BROWSER_ACTION_CHANGING) {
599 if (QuestionId == KEY_TPM_DEVICE_INTERFACE) {
600 Status = SetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress), Value->u8);
601 if (EFI_ERROR (Status)) {
602 CreatePopUp (
603 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
604 &Key,
605 L"Error: Fail to set PTP interface!",
606 NULL
607 );
608 return EFI_DEVICE_ERROR;
609 }
610 }
611 }
612
613 if (Action == EFI_BROWSER_ACTION_CHANGED) {
614 if (QuestionId == KEY_TPM_DEVICE) {
615 return EFI_SUCCESS;
616 }
617 if (QuestionId == KEY_TPM2_OPERATION) {
618 return SaveTcg2PpRequest (Value->u8);
619 }
620 if (QuestionId == KEY_TPM2_OPERATION_PARAMETER) {
621 return SaveTcg2PpRequestParameter (Value->u32);
622 }
623 if ((QuestionId >= KEY_TPM2_PCR_BANKS_REQUEST_0) && (QuestionId <= KEY_TPM2_PCR_BANKS_REQUEST_4)) {
624 return SaveTcg2PCRBanksRequest (QuestionId - KEY_TPM2_PCR_BANKS_REQUEST_0, Value->b);
625 }
626 }
627
628 if (Action == EFI_BROWSER_ACTION_SUBMITTED) {
629 if (QuestionId == KEY_TCG2_PPI_VERSION || QuestionId == KEY_TPM2_ACPI_REVISION) {
630 return Tcg2VersionInfoCallback (Action, QuestionId, Type, Value);
631 }
632 }
633
634 return EFI_UNSUPPORTED;
635 }
636
637 /**
638 Append Buffer With TpmAlgHash.
639
640 @param[in] Buffer Buffer to be appended.
641 @param[in] BufferSize Size of buffer.
642 @param[in] TpmAlgHash TpmAlgHash.
643
644 **/
645 VOID
646 AppendBufferWithTpmAlgHash (
647 IN UINT16 *Buffer,
648 IN UINTN BufferSize,
649 IN UINT32 TpmAlgHash
650 )
651 {
652 switch (TpmAlgHash) {
653 case TPM_ALG_SHA1:
654 if (Buffer[0] != 0) {
655 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
656 }
657 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA1");
658 break;
659 case TPM_ALG_SHA256:
660 if (Buffer[0] != 0) {
661 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
662 }
663 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA256");
664 break;
665 case TPM_ALG_SHA384:
666 if (Buffer[0] != 0) {
667 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
668 }
669 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA384");
670 break;
671 case TPM_ALG_SHA512:
672 if (Buffer[0] != 0) {
673 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
674 }
675 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA512");
676 break;
677 case TPM_ALG_SM3_256:
678 if (Buffer[0] != 0) {
679 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
680 }
681 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SM3_256");
682 break;
683 }
684 }
685
686 /**
687 Fill Buffer With BootHashAlg.
688
689 @param[in] Buffer Buffer to be filled.
690 @param[in] BufferSize Size of buffer.
691 @param[in] BootHashAlg BootHashAlg.
692
693 **/
694 VOID
695 FillBufferWithBootHashAlg (
696 IN UINT16 *Buffer,
697 IN UINTN BufferSize,
698 IN UINT32 BootHashAlg
699 )
700 {
701 Buffer[0] = 0;
702 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA1) != 0) {
703 if (Buffer[0] != 0) {
704 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
705 }
706 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA1");
707 }
708 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA256) != 0) {
709 if (Buffer[0] != 0) {
710 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
711 }
712 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA256");
713 }
714 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA384) != 0) {
715 if (Buffer[0] != 0) {
716 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
717 }
718 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA384");
719 }
720 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA512) != 0) {
721 if (Buffer[0] != 0) {
722 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
723 }
724 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA512");
725 }
726 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SM3_256) != 0) {
727 if (Buffer[0] != 0) {
728 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
729 }
730 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SM3_256");
731 }
732 }
733
734 /**
735 Set ConfigInfo according to TpmAlgHash.
736
737 @param[in,out] Tcg2ConfigInfo TCG2 config info.
738 @param[in] TpmAlgHash TpmAlgHash.
739
740 **/
741 VOID
742 SetConfigInfo (
743 IN OUT TCG2_CONFIGURATION_INFO *Tcg2ConfigInfo,
744 IN UINT32 TpmAlgHash
745 )
746 {
747 switch (TpmAlgHash) {
748 case TPM_ALG_SHA1:
749 Tcg2ConfigInfo->Sha1Supported = TRUE;
750 break;
751 case TPM_ALG_SHA256:
752 Tcg2ConfigInfo->Sha256Supported = TRUE;
753 break;
754 case TPM_ALG_SHA384:
755 Tcg2ConfigInfo->Sha384Supported = TRUE;
756 break;
757 case TPM_ALG_SHA512:
758 Tcg2ConfigInfo->Sha512Supported = TRUE;
759 break;
760 case TPM_ALG_SM3_256:
761 Tcg2ConfigInfo->Sm3Supported = TRUE;
762 break;
763 }
764 }
765
766 /**
767 Fill Buffer With TCG2EventLogFormat.
768
769 @param[in] Buffer Buffer to be filled.
770 @param[in] BufferSize Size of buffer.
771 @param[in] TCG2EventLogFormat TCG2EventLogFormat.
772
773 **/
774 VOID
775 FillBufferWithTCG2EventLogFormat (
776 IN UINT16 *Buffer,
777 IN UINTN BufferSize,
778 IN UINT32 TCG2EventLogFormat
779 )
780 {
781 Buffer[0] = 0;
782 if ((TCG2EventLogFormat & EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2) != 0) {
783 if (Buffer[0] != 0) {
784 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
785 }
786 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"TCG_1_2");
787 }
788 if ((TCG2EventLogFormat & EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) != 0) {
789 if (Buffer[0] != 0) {
790 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
791 }
792 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"TCG_2");
793 }
794 if ((TCG2EventLogFormat & (~EFI_TCG2_EVENT_LOG_FORMAT_ALL)) != 0) {
795 if (Buffer[0] != 0) {
796 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
797 }
798 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"UNKNOWN");
799 }
800 }
801
802 /**
803 This function publish the TCG2 configuration Form for TPM device.
804
805 @param[in, out] PrivateData Points to TCG2 configuration private data.
806
807 @retval EFI_SUCCESS HII Form is installed for this network device.
808 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
809 @retval Others Other errors as indicated.
810
811 **/
812 EFI_STATUS
813 InstallTcg2ConfigForm (
814 IN OUT TCG2_CONFIG_PRIVATE_DATA *PrivateData
815 )
816 {
817 EFI_STATUS Status;
818 EFI_HII_HANDLE HiiHandle;
819 EFI_HANDLE DriverHandle;
820 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
821 UINTN Index;
822 TPML_PCR_SELECTION Pcrs;
823 CHAR16 TempBuffer[1024];
824 TCG2_CONFIGURATION_INFO Tcg2ConfigInfo;
825 UINT8 TpmDeviceInterfaceDetected;
826
827 DriverHandle = NULL;
828 ConfigAccess = &PrivateData->ConfigAccess;
829 Status = gBS->InstallMultipleProtocolInterfaces (
830 &DriverHandle,
831 &gEfiDevicePathProtocolGuid,
832 &mTcg2HiiVendorDevicePath,
833 &gEfiHiiConfigAccessProtocolGuid,
834 ConfigAccess,
835 NULL
836 );
837 if (EFI_ERROR (Status)) {
838 return Status;
839 }
840
841 PrivateData->DriverHandle = DriverHandle;
842
843 //
844 // Publish the HII package list
845 //
846 HiiHandle = HiiAddPackages (
847 &gTcg2ConfigFormSetGuid,
848 DriverHandle,
849 Tcg2ConfigDxeStrings,
850 Tcg2ConfigBin,
851 NULL
852 );
853 if (HiiHandle == NULL) {
854 gBS->UninstallMultipleProtocolInterfaces (
855 DriverHandle,
856 &gEfiDevicePathProtocolGuid,
857 &mTcg2HiiVendorDevicePath,
858 &gEfiHiiConfigAccessProtocolGuid,
859 ConfigAccess,
860 NULL
861 );
862
863 return EFI_OUT_OF_RESOURCES;
864 }
865
866 PrivateData->HiiHandle = HiiHandle;
867
868 //
869 // Update static data
870 //
871 switch (PrivateData->TpmDeviceDetected) {
872 case TPM_DEVICE_NULL:
873 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"Not Found", NULL);
874 break;
875 case TPM_DEVICE_1_2:
876 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"TPM 1.2", NULL);
877 break;
878 case TPM_DEVICE_2_0_DTPM:
879 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"TPM 2.0", NULL);
880 break;
881 default:
882 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"Unknown", NULL);
883 break;
884 }
885
886 ZeroMem (&Tcg2ConfigInfo, sizeof(Tcg2ConfigInfo));
887 Status = Tpm2GetCapabilityPcrs (&Pcrs);
888 if (EFI_ERROR (Status)) {
889 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_ACTIVE_HASH_ALGO_CONTENT), L"[Unknown]", NULL);
890 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_SUPPORTED_HASH_ALGO_CONTENT), L"[Unknown]", NULL);
891 } else {
892 TempBuffer[0] = 0;
893 for (Index = 0; Index < Pcrs.count; Index++) {
894 if (!IsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
895 AppendBufferWithTpmAlgHash (TempBuffer, sizeof(TempBuffer), Pcrs.pcrSelections[Index].hash);
896 }
897 }
898 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_ACTIVE_HASH_ALGO_CONTENT), TempBuffer, NULL);
899
900 TempBuffer[0] = 0;
901 for (Index = 0; Index < Pcrs.count; Index++) {
902 AppendBufferWithTpmAlgHash (TempBuffer, sizeof(TempBuffer), Pcrs.pcrSelections[Index].hash);
903 SetConfigInfo (&Tcg2ConfigInfo, Pcrs.pcrSelections[Index].hash);
904 }
905 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_SUPPORTED_HASH_ALGO_CONTENT), TempBuffer, NULL);
906 }
907
908 FillBufferWithBootHashAlg (TempBuffer, sizeof(TempBuffer), PcdGet32 (PcdTcg2HashAlgorithmBitmap));
909 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_BIOS_HASH_ALGO_CONTENT), TempBuffer, NULL);
910
911 //
912 // Tcg2 Capability
913 //
914 FillBufferWithTCG2EventLogFormat (TempBuffer, sizeof(TempBuffer), PrivateData->ProtocolCapability.SupportedEventLogs);
915 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_SUPPORTED_EVENT_LOG_FORMAT_CONTENT), TempBuffer, NULL);
916
917 FillBufferWithBootHashAlg (TempBuffer, sizeof(TempBuffer), PrivateData->ProtocolCapability.HashAlgorithmBitmap);
918 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_HASH_ALGO_BITMAP_CONTENT), TempBuffer, NULL);
919
920 UnicodeSPrint (TempBuffer, sizeof (TempBuffer), L"%d", PrivateData->ProtocolCapability.NumberOfPCRBanks);
921 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_NUMBER_OF_PCR_BANKS_CONTENT), TempBuffer, NULL);
922
923 FillBufferWithBootHashAlg (TempBuffer, sizeof(TempBuffer), PrivateData->ProtocolCapability.ActivePcrBanks);
924 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_ACTIVE_PCR_BANKS_CONTENT), TempBuffer, NULL);
925
926 //
927 // Update TPM device interface type
928 //
929 if (PrivateData->TpmDeviceDetected == TPM_DEVICE_2_0_DTPM) {
930 TpmDeviceInterfaceDetected = GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
931 switch (TpmDeviceInterfaceDetected) {
932 case TPM_DEVICE_INTERFACE_TIS:
933 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"TIS", NULL);
934 break;
935 case TPM_DEVICE_INTERFACE_PTP_FIFO:
936 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"PTP FIFO", NULL);
937 break;
938 case TPM_DEVICE_INTERFACE_PTP_CRB:
939 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"PTP CRB", NULL);
940 break;
941 default:
942 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"Unknown", NULL);
943 break;
944 }
945
946 Tcg2ConfigInfo.TpmDeviceInterfaceAttempt = TpmDeviceInterfaceDetected;
947 switch (TpmDeviceInterfaceDetected) {
948 case TPM_DEVICE_INTERFACE_TIS:
949 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = FALSE;
950 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = FALSE;
951 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), L"TIS", NULL);
952 break;
953 case TPM_DEVICE_INTERFACE_PTP_FIFO:
954 case TPM_DEVICE_INTERFACE_PTP_CRB:
955 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = IsPtpFifoSupported((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
956 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = IsPtpCrbSupported((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
957 TempBuffer[0] = 0;
958 if (Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported) {
959 if (TempBuffer[0] != 0) {
960 StrCatS (TempBuffer, sizeof(TempBuffer) / sizeof (CHAR16), L", ");
961 }
962 StrCatS (TempBuffer, sizeof(TempBuffer) / sizeof (CHAR16), L"PTP FIFO");
963 }
964 if (Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported) {
965 if (TempBuffer[0] != 0) {
966 StrCatS (TempBuffer, sizeof(TempBuffer) / sizeof (CHAR16), L", ");
967 }
968 StrCatS (TempBuffer, sizeof(TempBuffer) / sizeof (CHAR16), L"PTP CRB");
969 }
970 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), TempBuffer, NULL);
971 break;
972 default:
973 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = FALSE;
974 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = FALSE;
975 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), L"Unknown", NULL);
976 break;
977 }
978 }
979
980 //
981 // Set ConfigInfo, to control the check box.
982 //
983 Status = gRT->SetVariable (
984 TCG2_STORAGE_INFO_NAME,
985 &gTcg2ConfigFormSetGuid,
986 EFI_VARIABLE_BOOTSERVICE_ACCESS,
987 sizeof(Tcg2ConfigInfo),
988 &Tcg2ConfigInfo
989 );
990 if (EFI_ERROR (Status)) {
991 DEBUG ((EFI_D_ERROR, "Tcg2ConfigDriver: Fail to set TCG2_STORAGE_INFO_NAME\n"));
992 }
993
994 return EFI_SUCCESS;
995 }
996
997 /**
998 This function removes TCG2 configuration Form.
999
1000 @param[in, out] PrivateData Points to TCG2 configuration private data.
1001
1002 **/
1003 VOID
1004 UninstallTcg2ConfigForm (
1005 IN OUT TCG2_CONFIG_PRIVATE_DATA *PrivateData
1006 )
1007 {
1008 //
1009 // Uninstall HII package list
1010 //
1011 if (PrivateData->HiiHandle != NULL) {
1012 HiiRemovePackages (PrivateData->HiiHandle);
1013 PrivateData->HiiHandle = NULL;
1014 }
1015
1016 //
1017 // Uninstall HII Config Access Protocol
1018 //
1019 if (PrivateData->DriverHandle != NULL) {
1020 gBS->UninstallMultipleProtocolInterfaces (
1021 PrivateData->DriverHandle,
1022 &gEfiDevicePathProtocolGuid,
1023 &mTcg2HiiVendorDevicePath,
1024 &gEfiHiiConfigAccessProtocolGuid,
1025 &PrivateData->ConfigAccess,
1026 NULL
1027 );
1028 PrivateData->DriverHandle = NULL;
1029 }
1030
1031 FreePool (PrivateData);
1032 }