]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Tcg2Config/Tcg2ConfigImpl.c
7a97f6bdc7e67735163e996cf7d48eb252cc2219
[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 - 2016, 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 This function processes the results of changes in configuration.
383
384 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
385 @param[in] Action Specifies the type of action taken by the browser.
386 @param[in] QuestionId A unique value which is sent to the original
387 exporting driver so that it can identify the type
388 of data to expect.
389 @param[in] Type The type of value for the question.
390 @param[in] Value A pointer to the data being sent to the original
391 exporting driver.
392 @param[out] ActionRequest On return, points to the action requested by the
393 callback function.
394
395 @retval EFI_SUCCESS The callback successfully handled the action.
396 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
397 variable and its data.
398 @retval EFI_DEVICE_ERROR The variable could not be saved.
399 @retval EFI_UNSUPPORTED The specified Action is not supported by the
400 callback.
401
402 **/
403 EFI_STATUS
404 EFIAPI
405 Tcg2Callback (
406 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,
407 IN EFI_BROWSER_ACTION Action,
408 IN EFI_QUESTION_ID QuestionId,
409 IN UINT8 Type,
410 IN EFI_IFR_TYPE_VALUE *Value,
411 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
412 )
413 {
414 EFI_INPUT_KEY Key;
415
416 if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) {
417 return EFI_INVALID_PARAMETER;
418 }
419
420 if (Action == EFI_BROWSER_ACTION_CHANGING) {
421 if (QuestionId == KEY_TPM_DEVICE_INTERFACE) {
422 EFI_STATUS Status;
423 Status = SetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress), Value->u8);
424 if (EFI_ERROR (Status)) {
425 CreatePopUp (
426 EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
427 &Key,
428 L"Error: Fail to set PTP interface!",
429 NULL
430 );
431 return EFI_DEVICE_ERROR;
432 }
433 }
434 }
435
436 if (Action == EFI_BROWSER_ACTION_CHANGED) {
437 if (QuestionId == KEY_TPM_DEVICE) {
438 return EFI_SUCCESS;
439 }
440 if (QuestionId == KEY_TPM2_OPERATION) {
441 return SaveTcg2PpRequest (Value->u8);
442 }
443 if (QuestionId == KEY_TPM2_OPERATION_PARAMETER) {
444 return SaveTcg2PpRequestParameter (Value->u32);
445 }
446 if ((QuestionId >= KEY_TPM2_PCR_BANKS_REQUEST_0) && (QuestionId <= KEY_TPM2_PCR_BANKS_REQUEST_4)) {
447 SaveTcg2PCRBanksRequest (QuestionId - KEY_TPM2_PCR_BANKS_REQUEST_0, Value->b);
448 }
449 }
450
451 return EFI_UNSUPPORTED;
452 }
453
454 /**
455 Append Buffer With TpmAlgHash.
456
457 @param[in] Buffer Buffer to be appended.
458 @param[in] BufferSize Size of buffer.
459 @param[in] TpmAlgHash TpmAlgHash.
460
461 **/
462 VOID
463 AppendBufferWithTpmAlgHash (
464 IN UINT16 *Buffer,
465 IN UINTN BufferSize,
466 IN UINT32 TpmAlgHash
467 )
468 {
469 switch (TpmAlgHash) {
470 case TPM_ALG_SHA1:
471 if (Buffer[0] != 0) {
472 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
473 }
474 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA1");
475 break;
476 case TPM_ALG_SHA256:
477 if (Buffer[0] != 0) {
478 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
479 }
480 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA256");
481 break;
482 case TPM_ALG_SHA384:
483 if (Buffer[0] != 0) {
484 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
485 }
486 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA384");
487 break;
488 case TPM_ALG_SHA512:
489 if (Buffer[0] != 0) {
490 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
491 }
492 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA512");
493 break;
494 case TPM_ALG_SM3_256:
495 if (Buffer[0] != 0) {
496 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
497 }
498 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SM3_256");
499 break;
500 }
501 }
502
503 /**
504 Fill Buffer With BootHashAlg.
505
506 @param[in] Buffer Buffer to be filled.
507 @param[in] BufferSize Size of buffer.
508 @param[in] BootHashAlg BootHashAlg.
509
510 **/
511 VOID
512 FillBufferWithBootHashAlg (
513 IN UINT16 *Buffer,
514 IN UINTN BufferSize,
515 IN UINT32 BootHashAlg
516 )
517 {
518 Buffer[0] = 0;
519 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA1) != 0) {
520 if (Buffer[0] != 0) {
521 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
522 }
523 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA1");
524 }
525 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA256) != 0) {
526 if (Buffer[0] != 0) {
527 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
528 }
529 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA256");
530 }
531 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA384) != 0) {
532 if (Buffer[0] != 0) {
533 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
534 }
535 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA384");
536 }
537 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SHA512) != 0) {
538 if (Buffer[0] != 0) {
539 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
540 }
541 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SHA512");
542 }
543 if ((BootHashAlg & EFI_TCG2_BOOT_HASH_ALG_SM3_256) != 0) {
544 if (Buffer[0] != 0) {
545 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
546 }
547 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"SM3_256");
548 }
549 }
550
551 /**
552 Set ConfigInfo according to TpmAlgHash.
553
554 @param[in,out] Tcg2ConfigInfo TCG2 config info.
555 @param[in] TpmAlgHash TpmAlgHash.
556
557 **/
558 VOID
559 SetConfigInfo (
560 IN OUT TCG2_CONFIGURATION_INFO *Tcg2ConfigInfo,
561 IN UINT32 TpmAlgHash
562 )
563 {
564 switch (TpmAlgHash) {
565 case TPM_ALG_SHA1:
566 Tcg2ConfigInfo->Sha1Supported = TRUE;
567 break;
568 case TPM_ALG_SHA256:
569 Tcg2ConfigInfo->Sha256Supported = TRUE;
570 break;
571 case TPM_ALG_SHA384:
572 Tcg2ConfigInfo->Sha384Supported = TRUE;
573 break;
574 case TPM_ALG_SHA512:
575 Tcg2ConfigInfo->Sha512Supported = TRUE;
576 break;
577 case TPM_ALG_SM3_256:
578 Tcg2ConfigInfo->Sm3Supported = TRUE;
579 break;
580 }
581 }
582
583 /**
584 Fill Buffer With TCG2EventLogFormat.
585
586 @param[in] Buffer Buffer to be filled.
587 @param[in] BufferSize Size of buffer.
588 @param[in] TCG2EventLogFormat TCG2EventLogFormat.
589
590 **/
591 VOID
592 FillBufferWithTCG2EventLogFormat (
593 IN UINT16 *Buffer,
594 IN UINTN BufferSize,
595 IN UINT32 TCG2EventLogFormat
596 )
597 {
598 Buffer[0] = 0;
599 if ((TCG2EventLogFormat & EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2) != 0) {
600 if (Buffer[0] != 0) {
601 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
602 }
603 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"TCG_1_2");
604 }
605 if ((TCG2EventLogFormat & EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) != 0) {
606 if (Buffer[0] != 0) {
607 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
608 }
609 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"TCG_2");
610 }
611 if ((TCG2EventLogFormat & (~EFI_TCG2_EVENT_LOG_FORMAT_ALL)) != 0) {
612 if (Buffer[0] != 0) {
613 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L", ");
614 }
615 StrCatS (Buffer, BufferSize / sizeof (CHAR16), L"UNKNOWN");
616 }
617 }
618
619 /**
620 Check if buffer is all zero.
621
622 @param[in] Buffer Buffer to be checked.
623 @param[in] BufferSize Size of buffer to be checked.
624
625 @retval TRUE Buffer is all zero.
626 @retval FALSE Buffer is not all zero.
627 **/
628 BOOLEAN
629 InternalIsZeroBuffer (
630 IN VOID *Buffer,
631 IN UINTN BufferSize
632 )
633 {
634 UINT8 *BufferData;
635 UINTN Index;
636
637 BufferData = Buffer;
638 for (Index = 0; Index < BufferSize; Index++) {
639 if (BufferData[Index] != 0) {
640 return FALSE;
641 }
642 }
643 return TRUE;
644 }
645
646 /**
647 This function publish the TCG2 configuration Form for TPM device.
648
649 @param[in, out] PrivateData Points to TCG2 configuration private data.
650
651 @retval EFI_SUCCESS HII Form is installed for this network device.
652 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
653 @retval Others Other errors as indicated.
654
655 **/
656 EFI_STATUS
657 InstallTcg2ConfigForm (
658 IN OUT TCG2_CONFIG_PRIVATE_DATA *PrivateData
659 )
660 {
661 EFI_STATUS Status;
662 EFI_HII_HANDLE HiiHandle;
663 EFI_HANDLE DriverHandle;
664 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
665 UINTN Index;
666 TPML_PCR_SELECTION Pcrs;
667 CHAR16 TempBuffer[1024];
668 TCG2_CONFIGURATION_INFO Tcg2ConfigInfo;
669 UINT8 TpmDeviceInterfaceDetected;
670
671 DriverHandle = NULL;
672 ConfigAccess = &PrivateData->ConfigAccess;
673 Status = gBS->InstallMultipleProtocolInterfaces (
674 &DriverHandle,
675 &gEfiDevicePathProtocolGuid,
676 &mTcg2HiiVendorDevicePath,
677 &gEfiHiiConfigAccessProtocolGuid,
678 ConfigAccess,
679 NULL
680 );
681 if (EFI_ERROR (Status)) {
682 return Status;
683 }
684
685 PrivateData->DriverHandle = DriverHandle;
686
687 //
688 // Publish the HII package list
689 //
690 HiiHandle = HiiAddPackages (
691 &gTcg2ConfigFormSetGuid,
692 DriverHandle,
693 Tcg2ConfigDxeStrings,
694 Tcg2ConfigBin,
695 NULL
696 );
697 if (HiiHandle == NULL) {
698 gBS->UninstallMultipleProtocolInterfaces (
699 DriverHandle,
700 &gEfiDevicePathProtocolGuid,
701 &mTcg2HiiVendorDevicePath,
702 &gEfiHiiConfigAccessProtocolGuid,
703 ConfigAccess,
704 NULL
705 );
706
707 return EFI_OUT_OF_RESOURCES;
708 }
709
710 PrivateData->HiiHandle = HiiHandle;
711
712 //
713 // Update static data
714 //
715 switch (PrivateData->TpmDeviceDetected) {
716 case TPM_DEVICE_NULL:
717 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"Not Found", NULL);
718 break;
719 case TPM_DEVICE_1_2:
720 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"TPM 1.2", NULL);
721 break;
722 case TPM_DEVICE_2_0_DTPM:
723 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"TPM 2.0", NULL);
724 break;
725 default:
726 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_STATE_CONTENT), L"Unknown", NULL);
727 break;
728 }
729
730 ZeroMem (&Tcg2ConfigInfo, sizeof(Tcg2ConfigInfo));
731 Status = Tpm2GetCapabilityPcrs (&Pcrs);
732 if (EFI_ERROR (Status)) {
733 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_ACTIVE_HASH_ALGO_CONTENT), L"[Unknown]", NULL);
734 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_SUPPORTED_HASH_ALGO_CONTENT), L"[Unknown]", NULL);
735 } else {
736 TempBuffer[0] = 0;
737 for (Index = 0; Index < Pcrs.count; Index++) {
738 if (!InternalIsZeroBuffer (Pcrs.pcrSelections[Index].pcrSelect, Pcrs.pcrSelections[Index].sizeofSelect)) {
739 AppendBufferWithTpmAlgHash (TempBuffer, sizeof(TempBuffer), Pcrs.pcrSelections[Index].hash);
740 }
741 }
742 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_ACTIVE_HASH_ALGO_CONTENT), TempBuffer, NULL);
743
744 TempBuffer[0] = 0;
745 for (Index = 0; Index < Pcrs.count; Index++) {
746 AppendBufferWithTpmAlgHash (TempBuffer, sizeof(TempBuffer), Pcrs.pcrSelections[Index].hash);
747 SetConfigInfo (&Tcg2ConfigInfo, Pcrs.pcrSelections[Index].hash);
748 }
749 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM2_SUPPORTED_HASH_ALGO_CONTENT), TempBuffer, NULL);
750 }
751
752 FillBufferWithBootHashAlg (TempBuffer, sizeof(TempBuffer), PcdGet32 (PcdTcg2HashAlgorithmBitmap));
753 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_BIOS_HASH_ALGO_CONTENT), TempBuffer, NULL);
754
755 //
756 // Tcg2 Capability
757 //
758 FillBufferWithTCG2EventLogFormat (TempBuffer, sizeof(TempBuffer), PrivateData->ProtocolCapability.SupportedEventLogs);
759 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_SUPPORTED_EVENT_LOG_FORMAT_CONTENT), TempBuffer, NULL);
760
761 FillBufferWithBootHashAlg (TempBuffer, sizeof(TempBuffer), PrivateData->ProtocolCapability.HashAlgorithmBitmap);
762 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_HASH_ALGO_BITMAP_CONTENT), TempBuffer, NULL);
763
764 UnicodeSPrint (TempBuffer, sizeof (TempBuffer), L"%d", PrivateData->ProtocolCapability.NumberOfPCRBanks);
765 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_NUMBER_OF_PCR_BANKS_CONTENT), TempBuffer, NULL);
766
767 FillBufferWithBootHashAlg (TempBuffer, sizeof(TempBuffer), PrivateData->ProtocolCapability.ActivePcrBanks);
768 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_ACTIVE_PCR_BANKS_CONTENT), TempBuffer, NULL);
769
770 //
771 // Update TPM device interface type
772 //
773 if (PrivateData->TpmDeviceDetected == TPM_DEVICE_2_0_DTPM) {
774 TpmDeviceInterfaceDetected = GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
775 switch (TpmDeviceInterfaceDetected) {
776 case TPM_DEVICE_INTERFACE_TIS:
777 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"TIS", NULL);
778 break;
779 case TPM_DEVICE_INTERFACE_PTP_FIFO:
780 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"PTP FIFO", NULL);
781 break;
782 case TPM_DEVICE_INTERFACE_PTP_CRB:
783 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"PTP CRB", NULL);
784 break;
785 default:
786 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_STATE_CONTENT), L"Unknown", NULL);
787 break;
788 }
789
790 Tcg2ConfigInfo.TpmDeviceInterfaceAttempt = TpmDeviceInterfaceDetected;
791 switch (TpmDeviceInterfaceDetected) {
792 case TPM_DEVICE_INTERFACE_TIS:
793 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = FALSE;
794 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = FALSE;
795 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), L"TIS", NULL);
796 break;
797 case TPM_DEVICE_INTERFACE_PTP_FIFO:
798 case TPM_DEVICE_INTERFACE_PTP_CRB:
799 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = IsPtpFifoSupported((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
800 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = IsPtpCrbSupported((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
801 TempBuffer[0] = 0;
802 if (Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported) {
803 if (TempBuffer[0] != 0) {
804 StrCatS (TempBuffer, sizeof(TempBuffer) / sizeof (CHAR16), L", ");
805 }
806 StrCatS (TempBuffer, sizeof(TempBuffer) / sizeof (CHAR16), L"PTP FIFO");
807 }
808 if (Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported) {
809 if (TempBuffer[0] != 0) {
810 StrCatS (TempBuffer, sizeof(TempBuffer) / sizeof (CHAR16), L", ");
811 }
812 StrCatS (TempBuffer, sizeof(TempBuffer) / sizeof (CHAR16), L"PTP CRB");
813 }
814 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), TempBuffer, NULL);
815 break;
816 default:
817 Tcg2ConfigInfo.TpmDeviceInterfacePtpFifoSupported = FALSE;
818 Tcg2ConfigInfo.TpmDeviceInterfacePtpCrbSupported = FALSE;
819 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TCG2_DEVICE_INTERFACE_CAPABILITY_CONTENT), L"Unknown", NULL);
820 break;
821 }
822 }
823
824 //
825 // Set ConfigInfo, to control the check box.
826 //
827 Status = gRT->SetVariable (
828 TCG2_STORAGE_INFO_NAME,
829 &gTcg2ConfigFormSetGuid,
830 EFI_VARIABLE_BOOTSERVICE_ACCESS,
831 sizeof(Tcg2ConfigInfo),
832 &Tcg2ConfigInfo
833 );
834 if (EFI_ERROR (Status)) {
835 DEBUG ((EFI_D_ERROR, "Tcg2ConfigDriver: Fail to set TCG2_STORAGE_INFO_NAME\n"));
836 }
837 return EFI_SUCCESS;
838 }
839
840 /**
841 This function removes TCG2 configuration Form.
842
843 @param[in, out] PrivateData Points to TCG2 configuration private data.
844
845 **/
846 VOID
847 UninstallTcg2ConfigForm (
848 IN OUT TCG2_CONFIG_PRIVATE_DATA *PrivateData
849 )
850 {
851 //
852 // Uninstall HII package list
853 //
854 if (PrivateData->HiiHandle != NULL) {
855 HiiRemovePackages (PrivateData->HiiHandle);
856 PrivateData->HiiHandle = NULL;
857 }
858
859 //
860 // Uninstall HII Config Access Protocol
861 //
862 if (PrivateData->DriverHandle != NULL) {
863 gBS->UninstallMultipleProtocolInterfaces (
864 PrivateData->DriverHandle,
865 &gEfiDevicePathProtocolGuid,
866 &mTcg2HiiVendorDevicePath,
867 &gEfiHiiConfigAccessProtocolGuid,
868 &PrivateData->ConfigAccess,
869 NULL
870 );
871 PrivateData->DriverHandle = NULL;
872 }
873
874 FreePool (PrivateData);
875 }