2 This module implements Tcg2 Protocol.
4 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<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
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.
17 #include <IndustryStandard/Acpi.h>
18 #include <IndustryStandard/PeImage.h>
19 #include <IndustryStandard/TcpaAcpi.h>
21 #include <Guid/GlobalVariable.h>
22 #include <Guid/HobList.h>
23 #include <Guid/TcgEventHob.h>
24 #include <Guid/EventGroup.h>
25 #include <Guid/EventExitBootServiceFailed.h>
26 #include <Guid/ImageAuthentication.h>
27 #include <Guid/TpmInstance.h>
29 #include <Protocol/DevicePath.h>
30 #include <Protocol/MpService.h>
31 #include <Protocol/VariableWrite.h>
32 #include <Protocol/Tcg2Protocol.h>
33 #include <Protocol/TrEEProtocol.h>
35 #include <Library/DebugLib.h>
36 #include <Library/BaseMemoryLib.h>
37 #include <Library/UefiRuntimeServicesTableLib.h>
38 #include <Library/UefiDriverEntryPoint.h>
39 #include <Library/HobLib.h>
40 #include <Library/UefiBootServicesTableLib.h>
41 #include <Library/BaseLib.h>
42 #include <Library/MemoryAllocationLib.h>
43 #include <Library/PrintLib.h>
44 #include <Library/Tpm2CommandLib.h>
45 #include <Library/PcdLib.h>
46 #include <Library/UefiLib.h>
47 #include <Library/Tpm2DeviceLib.h>
48 #include <Library/HashLib.h>
49 #include <Library/PerformanceLib.h>
50 #include <Library/ReportStatusCodeLib.h>
51 #include <Library/Tcg2PhysicalPresenceLib.h>
53 #define PERF_ID_TCG2_DXE 0x3120
60 #define TCG2_DEFAULT_MAX_COMMAND_SIZE 0x1000
61 #define TCG2_DEFAULT_MAX_RESPONSE_SIZE 0x1000
65 EFI_TCG2_EVENT_LOG_FORMAT LogFormat
;
66 } TCG2_EVENT_INFO_STRUCT
;
68 TCG2_EVENT_INFO_STRUCT mTcg2EventInfo
[] = {
69 {&gTcgEventEntryHobGuid
, EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
},
70 {&gTcgEvent2EntryHobGuid
, EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
},
73 #define TCG_EVENT_LOG_AREA_COUNT_MAX 2
76 EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat
;
77 EFI_PHYSICAL_ADDRESS Lasa
;
81 BOOLEAN EventLogStarted
;
82 BOOLEAN EventLogTruncated
;
83 } TCG_EVENT_LOG_AREA_STRUCT
;
85 typedef struct _TCG_DXE_DATA
{
86 EFI_TCG2_BOOT_SERVICE_CAPABILITY BsCap
;
87 TCG_EVENT_LOG_AREA_STRUCT EventLogAreaStruct
[TCG_EVENT_LOG_AREA_COUNT_MAX
];
88 BOOLEAN GetEventLogCalled
[TCG_EVENT_LOG_AREA_COUNT_MAX
];
89 TCG_EVENT_LOG_AREA_STRUCT FinalEventLogAreaStruct
[TCG_EVENT_LOG_AREA_COUNT_MAX
];
90 EFI_TCG2_FINAL_EVENTS_TABLE
*FinalEventsTable
[TCG_EVENT_LOG_AREA_COUNT_MAX
];
93 TCG_DXE_DATA mTcgDxeData
= {
95 sizeof (EFI_TCG2_BOOT_SERVICE_CAPABILITY
), // Size
96 { 1, 1 }, // StructureVersion
97 { 1, 1 }, // ProtocolVersion
98 EFI_TCG2_BOOT_HASH_ALG_SHA1
, // HashAlgorithmBitmap
99 EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
, // SupportedEventLogs
100 TRUE
, // TPMPresentFlag
101 TCG2_DEFAULT_MAX_COMMAND_SIZE
, // MaxCommandSize
102 TCG2_DEFAULT_MAX_RESPONSE_SIZE
, // MaxResponseSize
104 0, // NumberOfPCRBanks
109 UINTN mBootAttempts
= 0;
110 CHAR16 mBootVarName
[] = L
"BootOrder";
112 VARIABLE_TYPE mVariableType
[] = {
113 {EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
},
114 {EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
},
115 {EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
},
116 {EFI_IMAGE_SECURITY_DATABASE
, &gEfiImageSecurityDatabaseGuid
},
117 {EFI_IMAGE_SECURITY_DATABASE1
, &gEfiImageSecurityDatabaseGuid
},
120 EFI_HANDLE mImageHandle
;
123 Measure PE image into TPM log based on the authenticode image hashing in
124 PE/COFF Specification 8.0 Appendix A.
126 Caution: This function may receive untrusted input.
127 PE/COFF image is external input, so this function will validate its data structure
128 within this image buffer before use.
130 Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().
132 @param[in] PCRIndex TPM PCR index
133 @param[in] ImageAddress Start address of image buffer.
134 @param[in] ImageSize Image size
135 @param[out] DigestList Digeest list of this image.
137 @retval EFI_SUCCESS Successfully measure image.
138 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
139 @retval other error value
142 MeasurePeImageAndExtend (
144 IN EFI_PHYSICAL_ADDRESS ImageAddress
,
146 OUT TPML_DIGEST_VALUES
*DigestList
151 This function dump raw data.
154 @param Size raw data size
164 for (Index
= 0; Index
< Size
; Index
++) {
165 DEBUG ((EFI_D_INFO
, "%02x", (UINTN
)Data
[Index
]));
171 This function dump raw data with colume format.
174 @param Size raw data size
187 #define COLUME_SIZE (16 * 2)
189 Count
= Size
/ COLUME_SIZE
;
190 Left
= Size
% COLUME_SIZE
;
191 for (Index
= 0; Index
< Count
; Index
++) {
192 DEBUG ((EFI_D_INFO
, "%04x: ", Index
* COLUME_SIZE
));
193 InternalDumpData (Data
+ Index
* COLUME_SIZE
, COLUME_SIZE
);
194 DEBUG ((EFI_D_INFO
, "\n"));
198 DEBUG ((EFI_D_INFO
, "%04x: ", Index
* COLUME_SIZE
));
199 InternalDumpData (Data
+ Index
* COLUME_SIZE
, Left
);
200 DEBUG ((EFI_D_INFO
, "\n"));
205 Check if buffer is all zero.
207 @param[in] Buffer Buffer to be checked.
208 @param[in] BufferSize Size of buffer to be checked.
210 @retval TRUE Buffer is all zero.
211 @retval FALSE Buffer is not all zero.
223 for (Index
= 0; Index
< BufferSize
; Index
++) {
224 if (BufferData
[Index
] != 0) {
232 Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function
233 Caller is responsible to free LocationBuf.
235 @param[out] LocationBuf Returns Processor Location Buffer.
236 @param[out] Num Returns processor number.
238 @retval EFI_SUCCESS Operation completed successfully.
239 @retval EFI_UNSUPPORTED MpService protocol not found.
243 GetProcessorsCpuLocation (
244 OUT EFI_CPU_PHYSICAL_LOCATION
**LocationBuf
,
249 EFI_MP_SERVICES_PROTOCOL
*MpProtocol
;
251 UINTN EnabledProcessorNum
;
252 EFI_PROCESSOR_INFORMATION ProcessorInfo
;
253 EFI_CPU_PHYSICAL_LOCATION
*ProcessorLocBuf
;
256 Status
= gBS
->LocateProtocol (&gEfiMpServiceProtocolGuid
, NULL
, (VOID
**) &MpProtocol
);
257 if (EFI_ERROR (Status
)) {
259 // MP protocol is not installed
261 return EFI_UNSUPPORTED
;
264 Status
= MpProtocol
->GetNumberOfProcessors(
269 if (EFI_ERROR(Status
)){
273 Status
= gBS
->AllocatePool(
275 sizeof(EFI_CPU_PHYSICAL_LOCATION
) * ProcessorNum
,
276 (VOID
**) &ProcessorLocBuf
278 if (EFI_ERROR(Status
)){
283 // Get each processor Location info
285 for (Index
= 0; Index
< ProcessorNum
; Index
++) {
286 Status
= MpProtocol
->GetProcessorInfo(
291 if (EFI_ERROR(Status
)){
292 FreePool(ProcessorLocBuf
);
297 // Get all Processor Location info & measure
300 &ProcessorLocBuf
[Index
],
301 &ProcessorInfo
.Location
,
302 sizeof(EFI_CPU_PHYSICAL_LOCATION
)
306 *LocationBuf
= ProcessorLocBuf
;
313 The EFI_TCG2_PROTOCOL GetCapability function call provides protocol
314 capability information and state information.
316 @param[in] This Indicates the calling context
317 @param[in, out] ProtocolCapability The caller allocates memory for a EFI_TCG2_BOOT_SERVICE_CAPABILITY
318 structure and sets the size field to the size of the structure allocated.
319 The callee fills in the fields with the EFI protocol capability information
320 and the current EFI TCG2 state information up to the number of fields which
321 fit within the size of the structure passed in.
323 @retval EFI_SUCCESS Operation completed successfully.
324 @retval EFI_DEVICE_ERROR The command was unsuccessful.
325 The ProtocolCapability variable will not be populated.
326 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
327 The ProtocolCapability variable will not be populated.
328 @retval EFI_BUFFER_TOO_SMALL The ProtocolCapability variable is too small to hold the full response.
329 It will be partially populated (required Size field will be set).
334 IN EFI_TCG2_PROTOCOL
*This
,
335 IN OUT EFI_TCG2_BOOT_SERVICE_CAPABILITY
*ProtocolCapability
338 DEBUG ((DEBUG_VERBOSE
, "Tcg2GetCapability ...\n"));
340 if ((This
== NULL
) || (ProtocolCapability
== NULL
)) {
341 return EFI_INVALID_PARAMETER
;
344 DEBUG ((DEBUG_VERBOSE
, "Size - 0x%x\n", ProtocolCapability
->Size
));
345 DEBUG ((DEBUG_VERBOSE
, " 1.1 - 0x%x, 1.0 - 0x%x\n", sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY
), sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0
)));
347 if (ProtocolCapability
->Size
< mTcgDxeData
.BsCap
.Size
) {
349 // Handle the case that firmware support 1.1 but OS only support 1.0.
351 if ((mTcgDxeData
.BsCap
.ProtocolVersion
.Major
> 0x01) ||
352 ((mTcgDxeData
.BsCap
.ProtocolVersion
.Major
== 0x01) && ((mTcgDxeData
.BsCap
.ProtocolVersion
.Minor
> 0x00)))) {
353 if (ProtocolCapability
->Size
>= sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0
)) {
354 CopyMem (ProtocolCapability
, &mTcgDxeData
.BsCap
, sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0
));
355 ProtocolCapability
->Size
= sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0
);
356 ProtocolCapability
->StructureVersion
.Major
= 1;
357 ProtocolCapability
->StructureVersion
.Minor
= 0;
358 ProtocolCapability
->ProtocolVersion
.Major
= 1;
359 ProtocolCapability
->ProtocolVersion
.Minor
= 0;
360 DEBUG ((EFI_D_ERROR
, "TreeGetCapability (Compatible) - %r\n", EFI_SUCCESS
));
364 ProtocolCapability
->Size
= mTcgDxeData
.BsCap
.Size
;
365 return EFI_BUFFER_TOO_SMALL
;
368 CopyMem (ProtocolCapability
, &mTcgDxeData
.BsCap
, mTcgDxeData
.BsCap
.Size
);
369 DEBUG ((DEBUG_VERBOSE
, "Tcg2GetCapability - %r\n", EFI_SUCCESS
));
374 This function dump PCR event.
376 @param[in] EventHdr TCG PCR event structure.
380 IN TCG_PCR_EVENT_HDR
*EventHdr
385 DEBUG ((EFI_D_INFO
, " Event:\n"));
386 DEBUG ((EFI_D_INFO
, " PCRIndex - %d\n", EventHdr
->PCRIndex
));
387 DEBUG ((EFI_D_INFO
, " EventType - 0x%08x\n", EventHdr
->EventType
));
388 DEBUG ((EFI_D_INFO
, " Digest - "));
389 for (Index
= 0; Index
< sizeof(TCG_DIGEST
); Index
++) {
390 DEBUG ((EFI_D_INFO
, "%02x ", EventHdr
->Digest
.digest
[Index
]));
392 DEBUG ((EFI_D_INFO
, "\n"));
393 DEBUG ((EFI_D_INFO
, " EventSize - 0x%08x\n", EventHdr
->EventSize
));
394 InternalDumpHex ((UINT8
*)(EventHdr
+ 1), EventHdr
->EventSize
);
398 This function dump TCG_EfiSpecIDEventStruct.
400 @param[in] TcgEfiSpecIdEventStruct A pointer to TCG_EfiSpecIDEventStruct.
403 DumpTcgEfiSpecIdEventStruct (
404 IN TCG_EfiSpecIDEventStruct
*TcgEfiSpecIdEventStruct
407 TCG_EfiSpecIdEventAlgorithmSize
*DigestSize
;
409 UINT8
*VendorInfoSize
;
411 UINT32 NumberOfAlgorithms
;
413 DEBUG ((EFI_D_INFO
, " TCG_EfiSpecIDEventStruct:\n"));
414 DEBUG ((EFI_D_INFO
, " signature - '"));
415 for (Index
= 0; Index
< sizeof(TcgEfiSpecIdEventStruct
->signature
); Index
++) {
416 DEBUG ((EFI_D_INFO
, "%c", TcgEfiSpecIdEventStruct
->signature
[Index
]));
418 DEBUG ((EFI_D_INFO
, "'\n"));
419 DEBUG ((EFI_D_INFO
, " platformClass - 0x%08x\n", TcgEfiSpecIdEventStruct
->platformClass
));
420 DEBUG ((EFI_D_INFO
, " specVersion - %d.%d%d\n", TcgEfiSpecIdEventStruct
->specVersionMajor
, TcgEfiSpecIdEventStruct
->specVersionMinor
, TcgEfiSpecIdEventStruct
->specErrata
));
421 DEBUG ((EFI_D_INFO
, " uintnSize - 0x%02x\n", TcgEfiSpecIdEventStruct
->uintnSize
));
423 CopyMem (&NumberOfAlgorithms
, TcgEfiSpecIdEventStruct
+ 1, sizeof(NumberOfAlgorithms
));
424 DEBUG ((EFI_D_INFO
, " NumberOfAlgorithms - 0x%08x\n", NumberOfAlgorithms
));
426 DigestSize
= (TCG_EfiSpecIdEventAlgorithmSize
*)((UINT8
*)TcgEfiSpecIdEventStruct
+ sizeof(*TcgEfiSpecIdEventStruct
) + sizeof(NumberOfAlgorithms
));
427 for (Index
= 0; Index
< NumberOfAlgorithms
; Index
++) {
428 DEBUG ((EFI_D_INFO
, " digest(%d)\n", Index
));
429 DEBUG ((EFI_D_INFO
, " algorithmId - 0x%04x\n", DigestSize
[Index
].algorithmId
));
430 DEBUG ((EFI_D_INFO
, " digestSize - 0x%04x\n", DigestSize
[Index
].digestSize
));
432 VendorInfoSize
= (UINT8
*)&DigestSize
[NumberOfAlgorithms
];
433 DEBUG ((EFI_D_INFO
, " VendorInfoSize - 0x%02x\n", *VendorInfoSize
));
434 VendorInfo
= VendorInfoSize
+ 1;
435 DEBUG ((EFI_D_INFO
, " VendorInfo - "));
436 for (Index
= 0; Index
< *VendorInfoSize
; Index
++) {
437 DEBUG ((EFI_D_INFO
, "%02x ", VendorInfo
[Index
]));
439 DEBUG ((EFI_D_INFO
, "\n"));
443 This function get size of TCG_EfiSpecIDEventStruct.
445 @param[in] TcgEfiSpecIdEventStruct A pointer to TCG_EfiSpecIDEventStruct.
448 GetTcgEfiSpecIdEventStructSize (
449 IN TCG_EfiSpecIDEventStruct
*TcgEfiSpecIdEventStruct
452 TCG_EfiSpecIdEventAlgorithmSize
*DigestSize
;
453 UINT8
*VendorInfoSize
;
454 UINT32 NumberOfAlgorithms
;
456 CopyMem (&NumberOfAlgorithms
, TcgEfiSpecIdEventStruct
+ 1, sizeof(NumberOfAlgorithms
));
458 DigestSize
= (TCG_EfiSpecIdEventAlgorithmSize
*)((UINT8
*)TcgEfiSpecIdEventStruct
+ sizeof(*TcgEfiSpecIdEventStruct
) + sizeof(NumberOfAlgorithms
));
459 VendorInfoSize
= (UINT8
*)&DigestSize
[NumberOfAlgorithms
];
460 return sizeof(TCG_EfiSpecIDEventStruct
) + sizeof(UINT32
) + (NumberOfAlgorithms
* sizeof(TCG_EfiSpecIdEventAlgorithmSize
)) + sizeof(UINT8
) + (*VendorInfoSize
);
464 This function dump PCR event 2.
466 @param[in] TcgPcrEvent2 TCG PCR event 2 structure.
470 IN TCG_PCR_EVENT2
*TcgPcrEvent2
476 TPMI_ALG_HASH HashAlgo
;
482 DEBUG ((EFI_D_INFO
, " Event:\n"));
483 DEBUG ((EFI_D_INFO
, " PCRIndex - %d\n", TcgPcrEvent2
->PCRIndex
));
484 DEBUG ((EFI_D_INFO
, " EventType - 0x%08x\n", TcgPcrEvent2
->EventType
));
486 DEBUG ((EFI_D_INFO
, " DigestCount: 0x%08x\n", TcgPcrEvent2
->Digest
.count
));
488 DigestCount
= TcgPcrEvent2
->Digest
.count
;
489 HashAlgo
= TcgPcrEvent2
->Digest
.digests
[0].hashAlg
;
490 DigestBuffer
= (UINT8
*)&TcgPcrEvent2
->Digest
.digests
[0].digest
;
491 for (DigestIndex
= 0; DigestIndex
< DigestCount
; DigestIndex
++) {
492 DEBUG ((EFI_D_INFO
, " HashAlgo : 0x%04x\n", HashAlgo
));
493 DEBUG ((EFI_D_INFO
, " Digest(%d): ", DigestIndex
));
494 DigestSize
= GetHashSizeFromAlgo (HashAlgo
);
495 for (Index
= 0; Index
< DigestSize
; Index
++) {
496 DEBUG ((EFI_D_INFO
, "%02x ", DigestBuffer
[Index
]));
498 DEBUG ((EFI_D_INFO
, "\n"));
502 CopyMem (&HashAlgo
, DigestBuffer
+ DigestSize
, sizeof(TPMI_ALG_HASH
));
503 DigestBuffer
= DigestBuffer
+ DigestSize
+ sizeof(TPMI_ALG_HASH
);
505 DEBUG ((EFI_D_INFO
, "\n"));
506 DigestBuffer
= DigestBuffer
- sizeof(TPMI_ALG_HASH
);
508 CopyMem (&EventSize
, DigestBuffer
, sizeof(TcgPcrEvent2
->EventSize
));
509 DEBUG ((EFI_D_INFO
, " EventSize - 0x%08x\n", EventSize
));
510 EventBuffer
= DigestBuffer
+ sizeof(TcgPcrEvent2
->EventSize
);
511 InternalDumpHex (EventBuffer
, EventSize
);
515 This function returns size of TCG PCR event 2.
517 @param[in] TcgPcrEvent2 TCG PCR event 2 structure.
519 @return size of TCG PCR event 2.
523 IN TCG_PCR_EVENT2
*TcgPcrEvent2
528 TPMI_ALG_HASH HashAlgo
;
534 DigestCount
= TcgPcrEvent2
->Digest
.count
;
535 HashAlgo
= TcgPcrEvent2
->Digest
.digests
[0].hashAlg
;
536 DigestBuffer
= (UINT8
*)&TcgPcrEvent2
->Digest
.digests
[0].digest
;
537 for (DigestIndex
= 0; DigestIndex
< DigestCount
; DigestIndex
++) {
538 DigestSize
= GetHashSizeFromAlgo (HashAlgo
);
542 CopyMem (&HashAlgo
, DigestBuffer
+ DigestSize
, sizeof(TPMI_ALG_HASH
));
543 DigestBuffer
= DigestBuffer
+ DigestSize
+ sizeof(TPMI_ALG_HASH
);
545 DigestBuffer
= DigestBuffer
- sizeof(TPMI_ALG_HASH
);
547 CopyMem (&EventSize
, DigestBuffer
, sizeof(TcgPcrEvent2
->EventSize
));
548 EventBuffer
= DigestBuffer
+ sizeof(TcgPcrEvent2
->EventSize
);
550 return (UINTN
)EventBuffer
+ EventSize
- (UINTN
)TcgPcrEvent2
;
554 This function dump event log.
556 @param[in] EventLogFormat The type of the event log for which the information is requested.
557 @param[in] EventLogLocation A pointer to the memory address of the event log.
558 @param[in] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
559 address of the start of the last entry in the event log in memory.
560 @param[in] FinalEventsTable A pointer to the memory address of the final event table.
564 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat
,
565 IN EFI_PHYSICAL_ADDRESS EventLogLocation
,
566 IN EFI_PHYSICAL_ADDRESS EventLogLastEntry
,
567 IN EFI_TCG2_FINAL_EVENTS_TABLE
*FinalEventsTable
570 TCG_PCR_EVENT_HDR
*EventHdr
;
571 TCG_PCR_EVENT2
*TcgPcrEvent2
;
572 TCG_EfiSpecIDEventStruct
*TcgEfiSpecIdEventStruct
;
573 UINTN NumberOfEvents
;
575 DEBUG ((EFI_D_INFO
, "EventLogFormat: (0x%x)\n", EventLogFormat
));
577 switch (EventLogFormat
) {
578 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
:
579 EventHdr
= (TCG_PCR_EVENT_HDR
*)(UINTN
)EventLogLocation
;
580 while ((UINTN
)EventHdr
<= EventLogLastEntry
) {
581 DumpEvent (EventHdr
);
582 EventHdr
= (TCG_PCR_EVENT_HDR
*)((UINTN
)EventHdr
+ sizeof(TCG_PCR_EVENT_HDR
) + EventHdr
->EventSize
);
584 if (FinalEventsTable
== NULL
) {
585 DEBUG ((EFI_D_INFO
, "FinalEventsTable: NOT FOUND\n"));
587 DEBUG ((EFI_D_INFO
, "FinalEventsTable: (0x%x)\n", FinalEventsTable
));
588 DEBUG ((EFI_D_INFO
, " Version: (0x%x)\n", FinalEventsTable
->Version
));
589 DEBUG ((EFI_D_INFO
, " NumberOfEvents: (0x%x)\n", FinalEventsTable
->NumberOfEvents
));
591 EventHdr
= (TCG_PCR_EVENT_HDR
*)(UINTN
)(FinalEventsTable
+ 1);
592 for (NumberOfEvents
= 0; NumberOfEvents
< FinalEventsTable
->NumberOfEvents
; NumberOfEvents
++) {
593 DumpEvent (EventHdr
);
594 EventHdr
= (TCG_PCR_EVENT_HDR
*)((UINTN
)EventHdr
+ sizeof(TCG_PCR_EVENT_HDR
) + EventHdr
->EventSize
);
598 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
:
602 EventHdr
= (TCG_PCR_EVENT_HDR
*)(UINTN
)EventLogLocation
;
603 DumpEvent (EventHdr
);
605 TcgEfiSpecIdEventStruct
= (TCG_EfiSpecIDEventStruct
*)(EventHdr
+ 1);
606 DumpTcgEfiSpecIdEventStruct (TcgEfiSpecIdEventStruct
);
608 TcgPcrEvent2
= (TCG_PCR_EVENT2
*)((UINTN
)TcgEfiSpecIdEventStruct
+ GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct
));
609 while ((UINTN
)TcgPcrEvent2
<= EventLogLastEntry
) {
610 DumpEvent2 (TcgPcrEvent2
);
611 TcgPcrEvent2
= (TCG_PCR_EVENT2
*)((UINTN
)TcgPcrEvent2
+ GetPcrEvent2Size (TcgPcrEvent2
));
614 if (FinalEventsTable
== NULL
) {
615 DEBUG ((EFI_D_INFO
, "FinalEventsTable: NOT FOUND\n"));
617 DEBUG ((EFI_D_INFO
, "FinalEventsTable: (0x%x)\n", FinalEventsTable
));
618 DEBUG ((EFI_D_INFO
, " Version: (0x%x)\n", FinalEventsTable
->Version
));
619 DEBUG ((EFI_D_INFO
, " NumberOfEvents: (0x%x)\n", FinalEventsTable
->NumberOfEvents
));
621 TcgPcrEvent2
= (TCG_PCR_EVENT2
*)(UINTN
)(FinalEventsTable
+ 1);
622 for (NumberOfEvents
= 0; NumberOfEvents
< FinalEventsTable
->NumberOfEvents
; NumberOfEvents
++) {
623 DumpEvent2 (TcgPcrEvent2
);
624 TcgPcrEvent2
= (TCG_PCR_EVENT2
*)((UINTN
)TcgPcrEvent2
+ GetPcrEvent2Size (TcgPcrEvent2
));
634 The EFI_TCG2_PROTOCOL Get Event Log function call allows a caller to
635 retrieve the address of a given event log and its last entry.
637 @param[in] This Indicates the calling context
638 @param[in] EventLogFormat The type of the event log for which the information is requested.
639 @param[out] EventLogLocation A pointer to the memory address of the event log.
640 @param[out] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
641 address of the start of the last entry in the event log in memory.
642 @param[out] EventLogTruncated If the Event Log is missing at least one entry because an event would
643 have exceeded the area allocated for events, this value is set to TRUE.
644 Otherwise, the value will be FALSE and the Event Log will be complete.
646 @retval EFI_SUCCESS Operation completed successfully.
647 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect
648 (e.g. asking for an event log whose format is not supported).
653 IN EFI_TCG2_PROTOCOL
*This
,
654 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat
,
655 OUT EFI_PHYSICAL_ADDRESS
*EventLogLocation
,
656 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
,
657 OUT BOOLEAN
*EventLogTruncated
662 DEBUG ((EFI_D_INFO
, "Tcg2GetEventLog ... (0x%x)\n", EventLogFormat
));
665 return EFI_INVALID_PARAMETER
;
668 for (Index
= 0; Index
< sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0]); Index
++) {
669 if (EventLogFormat
== mTcg2EventInfo
[Index
].LogFormat
) {
674 if (Index
== sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0])) {
675 return EFI_INVALID_PARAMETER
;
678 if ((mTcg2EventInfo
[Index
].LogFormat
& mTcgDxeData
.BsCap
.SupportedEventLogs
) == 0) {
679 return EFI_INVALID_PARAMETER
;
682 if (!mTcgDxeData
.BsCap
.TPMPresentFlag
) {
683 if (EventLogLocation
!= NULL
) {
684 *EventLogLocation
= 0;
686 if (EventLogLastEntry
!= NULL
) {
687 *EventLogLastEntry
= 0;
689 if (EventLogTruncated
!= NULL
) {
690 *EventLogTruncated
= FALSE
;
695 if (EventLogLocation
!= NULL
) {
696 *EventLogLocation
= mTcgDxeData
.EventLogAreaStruct
[Index
].Lasa
;
697 DEBUG ((EFI_D_INFO
, "Tcg2GetEventLog (EventLogLocation - %x)\n", *EventLogLocation
));
700 if (EventLogLastEntry
!= NULL
) {
701 if (!mTcgDxeData
.EventLogAreaStruct
[Index
].EventLogStarted
) {
702 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)0;
704 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)mTcgDxeData
.EventLogAreaStruct
[Index
].LastEvent
;
706 DEBUG ((EFI_D_INFO
, "Tcg2GetEventLog (EventLogLastEntry - %x)\n", *EventLogLastEntry
));
709 if (EventLogTruncated
!= NULL
) {
710 *EventLogTruncated
= mTcgDxeData
.EventLogAreaStruct
[Index
].EventLogTruncated
;
711 DEBUG ((EFI_D_INFO
, "Tcg2GetEventLog (EventLogTruncated - %x)\n", *EventLogTruncated
));
714 DEBUG ((EFI_D_INFO
, "Tcg2GetEventLog - %r\n", EFI_SUCCESS
));
716 // Dump Event Log for debug purpose
717 if ((EventLogLocation
!= NULL
) && (EventLogLastEntry
!= NULL
)) {
718 DumpEventLog (EventLogFormat
, *EventLogLocation
, *EventLogLastEntry
, mTcgDxeData
.FinalEventsTable
[Index
]);
722 // All events generated after the invocation of EFI_TCG2_GET_EVENT_LOG SHALL be stored
723 // in an instance of an EFI_CONFIGURATION_TABLE named by the VendorGuid of EFI_TCG2_FINAL_EVENTS_TABLE_GUID.
725 mTcgDxeData
.GetEventLogCalled
[Index
] = TRUE
;
731 Add a new entry to the Event Log.
733 @param[in, out] EventLogPtr Pointer to the Event Log data.
734 @param[in, out] LogSize Size of the Event Log.
735 @param[in] MaxSize Maximum size of the Event Log.
736 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
737 @param[in] NewEventHdrSize New event header size.
738 @param[in] NewEventData Pointer to the new event data.
739 @param[in] NewEventSize New event data size.
741 @retval EFI_SUCCESS The new event log entry was added.
742 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
747 IN OUT UINT8
**EventLogPtr
,
748 IN OUT UINTN
*LogSize
,
750 IN VOID
*NewEventHdr
,
751 IN UINT32 NewEventHdrSize
,
752 IN UINT8
*NewEventData
,
753 IN UINT32 NewEventSize
758 if (NewEventSize
> MAX_ADDRESS
- NewEventHdrSize
) {
759 return EFI_OUT_OF_RESOURCES
;
762 NewLogSize
= NewEventHdrSize
+ NewEventSize
;
764 if (NewLogSize
> MAX_ADDRESS
- *LogSize
) {
765 return EFI_OUT_OF_RESOURCES
;
768 if (NewLogSize
+ *LogSize
> MaxSize
) {
769 DEBUG ((EFI_D_INFO
, " MaxSize - 0x%x\n", MaxSize
));
770 DEBUG ((EFI_D_INFO
, " NewLogSize - 0x%x\n", NewLogSize
));
771 DEBUG ((EFI_D_INFO
, " LogSize - 0x%x\n", *LogSize
));
772 DEBUG ((EFI_D_INFO
, "TcgCommLogEvent - %r\n", EFI_OUT_OF_RESOURCES
));
773 return EFI_OUT_OF_RESOURCES
;
776 *EventLogPtr
+= *LogSize
;
777 *LogSize
+= NewLogSize
;
778 CopyMem (*EventLogPtr
, NewEventHdr
, NewEventHdrSize
);
780 *EventLogPtr
+ NewEventHdrSize
,
788 Add a new entry to the Event Log.
790 @param[in] EventLogFormat The type of the event log for which the information is requested.
791 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
792 @param[in] NewEventHdrSize New event header size.
793 @param[in] NewEventData Pointer to the new event data.
794 @param[in] NewEventSize New event data size.
796 @retval EFI_SUCCESS The new event log entry was added.
797 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
802 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat
,
803 IN VOID
*NewEventHdr
,
804 IN UINT32 NewEventHdrSize
,
805 IN UINT8
*NewEventData
,
806 IN UINT32 NewEventSize
811 TCG_EVENT_LOG_AREA_STRUCT
*EventLogAreaStruct
;
813 for (Index
= 0; Index
< sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0]); Index
++) {
814 if (EventLogFormat
== mTcg2EventInfo
[Index
].LogFormat
) {
819 if (Index
== sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0])) {
820 return EFI_INVALID_PARAMETER
;
824 // Record to normal event log
826 EventLogAreaStruct
= &mTcgDxeData
.EventLogAreaStruct
[Index
];
828 if (EventLogAreaStruct
->EventLogTruncated
) {
829 return EFI_VOLUME_FULL
;
832 EventLogAreaStruct
->LastEvent
= (UINT8
*)(UINTN
)EventLogAreaStruct
->Lasa
;
833 Status
= TcgCommLogEvent (
834 &EventLogAreaStruct
->LastEvent
,
835 &EventLogAreaStruct
->EventLogSize
,
836 (UINTN
)EventLogAreaStruct
->Laml
,
843 if (Status
== EFI_OUT_OF_RESOURCES
) {
844 EventLogAreaStruct
->EventLogTruncated
= TRUE
;
845 return EFI_VOLUME_FULL
;
846 } else if (Status
== EFI_SUCCESS
) {
847 EventLogAreaStruct
->EventLogStarted
= TRUE
;
851 // If GetEventLog is called, record to FinalEventsTable, too.
853 if (mTcgDxeData
.GetEventLogCalled
[Index
]) {
854 if (mTcgDxeData
.FinalEventsTable
[Index
] == NULL
) {
856 // no need for FinalEventsTable
860 EventLogAreaStruct
= &mTcgDxeData
.FinalEventLogAreaStruct
[Index
];
862 if (EventLogAreaStruct
->EventLogTruncated
) {
863 return EFI_VOLUME_FULL
;
866 EventLogAreaStruct
->LastEvent
= (UINT8
*)(UINTN
)EventLogAreaStruct
->Lasa
;
867 Status
= TcgCommLogEvent (
868 &EventLogAreaStruct
->LastEvent
,
869 &EventLogAreaStruct
->EventLogSize
,
870 (UINTN
)EventLogAreaStruct
->Laml
,
876 if (Status
== EFI_OUT_OF_RESOURCES
) {
877 EventLogAreaStruct
->EventLogTruncated
= TRUE
;
878 return EFI_VOLUME_FULL
;
879 } else if (Status
== EFI_SUCCESS
) {
880 EventLogAreaStruct
->EventLogStarted
= TRUE
;
882 // Increase the NumberOfEvents in FinalEventsTable
884 (mTcgDxeData
.FinalEventsTable
[Index
])->NumberOfEvents
++;
885 DEBUG ((EFI_D_INFO
, "FinalEventsTable->NumberOfEvents - 0x%x\n", (mTcgDxeData
.FinalEventsTable
[Index
])->NumberOfEvents
));
886 DEBUG ((EFI_D_INFO
, " Size - 0x%x\n", (UINTN
)EventLogAreaStruct
->LastEvent
- (UINTN
)mTcgDxeData
.FinalEventsTable
[Index
]));
894 This function get digest from digest list.
896 @param HashAlg digest algorithm
897 @param DigestList digest list
900 @retval EFI_SUCCESS Sha1Digest is found and returned.
901 @retval EFI_NOT_FOUND Sha1Digest is not found.
904 Tpm2GetDigestFromDigestList (
905 IN TPMI_ALG_HASH HashAlg
,
906 IN TPML_DIGEST_VALUES
*DigestList
,
913 DigestSize
= GetHashSizeFromAlgo (HashAlg
);
914 for (Index
= 0; Index
< DigestList
->count
; Index
++) {
915 if (DigestList
->digests
[Index
].hashAlg
== HashAlg
) {
918 &DigestList
->digests
[Index
].digest
,
925 return EFI_NOT_FOUND
;
929 Get TPML_DIGEST_VALUES data size.
931 @param[in] DigestList TPML_DIGEST_VALUES data.
933 @return TPML_DIGEST_VALUES data size.
937 IN TPML_DIGEST_VALUES
*DigestList
944 TotalSize
= sizeof(DigestList
->count
);
945 for (Index
= 0; Index
< DigestList
->count
; Index
++) {
946 DigestSize
= GetHashSizeFromAlgo (DigestList
->digests
[Index
].hashAlg
);
947 TotalSize
+= sizeof(DigestList
->digests
[Index
].hashAlg
) + DigestSize
;
954 Get TPML_DIGEST_VALUES compact binary buffer size.
956 @param[in] DigestListBin TPML_DIGEST_VALUES compact binary buffer.
958 @return TPML_DIGEST_VALUES compact binary buffer size.
961 GetDigestListBinSize (
962 IN VOID
*DigestListBin
969 TPMI_ALG_HASH HashAlg
;
971 Count
= ReadUnaligned32 (DigestListBin
);
972 TotalSize
= sizeof(Count
);
973 DigestListBin
= (UINT8
*)DigestListBin
+ sizeof(Count
);
974 for (Index
= 0; Index
< Count
; Index
++) {
975 HashAlg
= ReadUnaligned16 (DigestListBin
);
976 TotalSize
+= sizeof(HashAlg
);
977 DigestListBin
= (UINT8
*)DigestListBin
+ sizeof(HashAlg
);
979 DigestSize
= GetHashSizeFromAlgo (HashAlg
);
980 TotalSize
+= DigestSize
;
981 DigestListBin
= (UINT8
*)DigestListBin
+ DigestSize
;
988 Return if hash alg is supported in TPM PCR bank.
990 @param HashAlg Hash algorithm to be checked.
992 @retval TRUE Hash algorithm is supported.
993 @retval FALSE Hash algorithm is not supported.
996 IsHashAlgSupportedInPcrBank (
997 IN TPMI_ALG_HASH HashAlg
1002 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA1
) != 0) {
1006 case TPM_ALG_SHA256
:
1007 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA256
) != 0) {
1011 case TPM_ALG_SHA384
:
1012 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA384
) != 0) {
1016 case TPM_ALG_SHA512
:
1017 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA512
) != 0) {
1021 case TPM_ALG_SM3_256
:
1022 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SM3_256
) != 0) {
1032 Copy TPML_DIGEST_VALUES into a buffer
1034 @param[in,out] Buffer Buffer to hold TPML_DIGEST_VALUES.
1035 @param[in] DigestList TPML_DIGEST_VALUES to be copied.
1037 @return The end of buffer to hold TPML_DIGEST_VALUES.
1040 CopyDigestListToBuffer (
1041 IN OUT VOID
*Buffer
,
1042 IN TPML_DIGEST_VALUES
*DigestList
1048 CopyMem (Buffer
, &DigestList
->count
, sizeof(DigestList
->count
));
1049 Buffer
= (UINT8
*)Buffer
+ sizeof(DigestList
->count
);
1050 for (Index
= 0; Index
< DigestList
->count
; Index
++) {
1051 if (!IsHashAlgSupportedInPcrBank (DigestList
->digests
[Index
].hashAlg
)) {
1052 DEBUG ((EFI_D_ERROR
, "WARNING: TPM2 Event log has HashAlg unsupported by PCR bank (0x%x)\n", DigestList
->digests
[Index
].hashAlg
));
1055 CopyMem (Buffer
, &DigestList
->digests
[Index
].hashAlg
, sizeof(DigestList
->digests
[Index
].hashAlg
));
1056 Buffer
= (UINT8
*)Buffer
+ sizeof(DigestList
->digests
[Index
].hashAlg
);
1057 DigestSize
= GetHashSizeFromAlgo (DigestList
->digests
[Index
].hashAlg
);
1058 CopyMem (Buffer
, &DigestList
->digests
[Index
].digest
, DigestSize
);
1059 Buffer
= (UINT8
*)Buffer
+ DigestSize
;
1066 Add a new entry to the Event Log.
1068 @param[in] DigestList A list of digest.
1069 @param[in,out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
1070 @param[in] NewEventData Pointer to the new event data.
1072 @retval EFI_SUCCESS The new event log entry was added.
1073 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
1076 TcgDxeLogHashEvent (
1077 IN TPML_DIGEST_VALUES
*DigestList
,
1078 IN OUT TCG_PCR_EVENT_HDR
*NewEventHdr
,
1079 IN UINT8
*NewEventData
1085 EFI_STATUS RetStatus
;
1086 TCG_PCR_EVENT2 TcgPcrEvent2
;
1087 UINT8
*DigestBuffer
;
1089 DEBUG ((EFI_D_INFO
, "SupportedEventLogs - 0x%08x\n", mTcgDxeData
.BsCap
.SupportedEventLogs
));
1091 RetStatus
= EFI_SUCCESS
;
1092 for (Index
= 0; Index
< sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0]); Index
++) {
1093 if ((mTcgDxeData
.BsCap
.SupportedEventLogs
& mTcg2EventInfo
[Index
].LogFormat
) != 0) {
1094 DEBUG ((EFI_D_INFO
, " LogFormat - 0x%08x\n", mTcg2EventInfo
[Index
].LogFormat
));
1095 switch (mTcg2EventInfo
[Index
].LogFormat
) {
1096 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
:
1097 Status
= Tpm2GetDigestFromDigestList (TPM_ALG_SHA1
, DigestList
, &NewEventHdr
->Digest
);
1098 if (!EFI_ERROR (Status
)) {
1100 // Enter critical region
1102 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
1103 Status
= TcgDxeLogEvent (
1104 mTcg2EventInfo
[Index
].LogFormat
,
1106 sizeof(TCG_PCR_EVENT_HDR
),
1108 NewEventHdr
->EventSize
1110 if (Status
!= EFI_SUCCESS
) {
1113 gBS
->RestoreTPL (OldTpl
);
1115 // Exit critical region
1119 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
:
1120 ZeroMem (&TcgPcrEvent2
, sizeof(TcgPcrEvent2
));
1121 TcgPcrEvent2
.PCRIndex
= NewEventHdr
->PCRIndex
;
1122 TcgPcrEvent2
.EventType
= NewEventHdr
->EventType
;
1123 DigestBuffer
= (UINT8
*)&TcgPcrEvent2
.Digest
;
1124 DigestBuffer
= CopyDigestListToBuffer (DigestBuffer
, DigestList
);
1125 CopyMem (DigestBuffer
, &NewEventHdr
->EventSize
, sizeof(NewEventHdr
->EventSize
));
1126 DigestBuffer
= DigestBuffer
+ sizeof(NewEventHdr
->EventSize
);
1129 // Enter critical region
1131 OldTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
1132 Status
= TcgDxeLogEvent (
1133 mTcg2EventInfo
[Index
].LogFormat
,
1135 sizeof(TcgPcrEvent2
.PCRIndex
) + sizeof(TcgPcrEvent2
.EventType
) + GetDigestListSize (DigestList
) + sizeof(TcgPcrEvent2
.EventSize
),
1137 NewEventHdr
->EventSize
1139 if (Status
!= EFI_SUCCESS
) {
1142 gBS
->RestoreTPL (OldTpl
);
1144 // Exit critical region
1155 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
1156 and add an entry to the Event Log.
1158 @param[in] Flags Bitmap providing additional information.
1159 @param[in] HashData Physical address of the start of the data buffer
1160 to be hashed, extended, and logged.
1161 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
1162 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
1163 @param[in] NewEventData Pointer to the new event data.
1165 @retval EFI_SUCCESS Operation completed successfully.
1166 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
1167 @retval EFI_DEVICE_ERROR The command was unsuccessful.
1171 TcgDxeHashLogExtendEvent (
1174 IN UINT64 HashDataLen
,
1175 IN OUT TCG_PCR_EVENT_HDR
*NewEventHdr
,
1176 IN UINT8
*NewEventData
1180 TPML_DIGEST_VALUES DigestList
;
1182 if (!mTcgDxeData
.BsCap
.TPMPresentFlag
) {
1183 return EFI_DEVICE_ERROR
;
1186 Status
= HashAndExtend (
1187 NewEventHdr
->PCRIndex
,
1192 if (!EFI_ERROR (Status
)) {
1193 if ((Flags
& EFI_TCG2_EXTEND_ONLY
) == 0) {
1194 Status
= TcgDxeLogHashEvent (&DigestList
, NewEventHdr
, NewEventData
);
1198 if (Status
== EFI_DEVICE_ERROR
) {
1199 DEBUG ((EFI_D_ERROR
, "TcgDxeHashLogExtendEvent - %r. Disable TPM.\n", Status
));
1200 mTcgDxeData
.BsCap
.TPMPresentFlag
= FALSE
;
1201 REPORT_STATUS_CODE (
1202 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1203 (PcdGet32 (PcdStatusCodeSubClassTpmDevice
) | EFI_P_EC_INTERFACE_ERROR
)
1211 The EFI_TCG2_PROTOCOL HashLogExtendEvent function call provides callers with
1212 an opportunity to extend and optionally log events without requiring
1213 knowledge of actual TPM commands.
1214 The extend operation will occur even if this function cannot create an event
1215 log entry (e.g. due to the event log being full).
1217 @param[in] This Indicates the calling context
1218 @param[in] Flags Bitmap providing additional information.
1219 @param[in] DataToHash Physical address of the start of the data buffer to be hashed.
1220 @param[in] DataToHashLen The length in bytes of the buffer referenced by DataToHash.
1221 @param[in] Event Pointer to data buffer containing information about the event.
1223 @retval EFI_SUCCESS Operation completed successfully.
1224 @retval EFI_DEVICE_ERROR The command was unsuccessful.
1225 @retval EFI_VOLUME_FULL The extend operation occurred, but the event could not be written to one or more event logs.
1226 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1227 @retval EFI_UNSUPPORTED The PE/COFF image type is not supported.
1231 Tcg2HashLogExtendEvent (
1232 IN EFI_TCG2_PROTOCOL
*This
,
1234 IN EFI_PHYSICAL_ADDRESS DataToHash
,
1235 IN UINT64 DataToHashLen
,
1236 IN EFI_TCG2_EVENT
*Event
1240 TCG_PCR_EVENT_HDR NewEventHdr
;
1241 TPML_DIGEST_VALUES DigestList
;
1243 DEBUG ((DEBUG_VERBOSE
, "Tcg2HashLogExtendEvent ...\n"));
1245 if ((This
== NULL
) || (DataToHash
== 0) || (Event
== NULL
)) {
1246 return EFI_INVALID_PARAMETER
;
1249 if (!mTcgDxeData
.BsCap
.TPMPresentFlag
) {
1250 return EFI_DEVICE_ERROR
;
1253 if (Event
->Size
< Event
->Header
.HeaderSize
+ sizeof(UINT32
)) {
1254 return EFI_INVALID_PARAMETER
;
1257 if (Event
->Header
.PCRIndex
> MAX_PCR_INDEX
) {
1258 return EFI_INVALID_PARAMETER
;
1261 NewEventHdr
.PCRIndex
= Event
->Header
.PCRIndex
;
1262 NewEventHdr
.EventType
= Event
->Header
.EventType
;
1263 NewEventHdr
.EventSize
= Event
->Size
- sizeof(UINT32
) - Event
->Header
.HeaderSize
;
1264 if ((Flags
& PE_COFF_IMAGE
) != 0) {
1265 Status
= MeasurePeImageAndExtend (
1266 NewEventHdr
.PCRIndex
,
1268 (UINTN
)DataToHashLen
,
1271 if (!EFI_ERROR (Status
)) {
1272 if ((Flags
& EFI_TCG2_EXTEND_ONLY
) == 0) {
1273 Status
= TcgDxeLogHashEvent (&DigestList
, &NewEventHdr
, Event
->Event
);
1276 if (Status
== EFI_DEVICE_ERROR
) {
1277 DEBUG ((EFI_D_ERROR
, "MeasurePeImageAndExtend - %r. Disable TPM.\n", Status
));
1278 mTcgDxeData
.BsCap
.TPMPresentFlag
= FALSE
;
1279 REPORT_STATUS_CODE (
1280 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
1281 (PcdGet32 (PcdStatusCodeSubClassTpmDevice
) | EFI_P_EC_INTERFACE_ERROR
)
1285 Status
= TcgDxeHashLogExtendEvent (
1287 (UINT8
*) (UINTN
) DataToHash
,
1293 DEBUG ((DEBUG_VERBOSE
, "Tcg2HashLogExtendEvent - %r\n", Status
));
1298 This service enables the sending of commands to the TPM.
1300 @param[in] This Indicates the calling context
1301 @param[in] InputParameterBlockSize Size of the TPM input parameter block.
1302 @param[in] InputParameterBlock Pointer to the TPM input parameter block.
1303 @param[in] OutputParameterBlockSize Size of the TPM output parameter block.
1304 @param[in] OutputParameterBlock Pointer to the TPM output parameter block.
1306 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
1307 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
1308 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1309 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
1314 IN EFI_TCG2_PROTOCOL
*This
,
1315 IN UINT32 InputParameterBlockSize
,
1316 IN UINT8
*InputParameterBlock
,
1317 IN UINT32 OutputParameterBlockSize
,
1318 IN UINT8
*OutputParameterBlock
1323 DEBUG ((EFI_D_INFO
, "Tcg2SubmitCommand ...\n"));
1325 if ((This
== NULL
) ||
1326 (InputParameterBlockSize
== 0) || (InputParameterBlock
== NULL
) ||
1327 (OutputParameterBlockSize
== 0) || (OutputParameterBlock
== NULL
)) {
1328 return EFI_INVALID_PARAMETER
;
1331 if (!mTcgDxeData
.BsCap
.TPMPresentFlag
) {
1332 return EFI_DEVICE_ERROR
;
1335 if (InputParameterBlockSize
> mTcgDxeData
.BsCap
.MaxCommandSize
) {
1336 return EFI_INVALID_PARAMETER
;
1338 if (OutputParameterBlockSize
> mTcgDxeData
.BsCap
.MaxResponseSize
) {
1339 return EFI_INVALID_PARAMETER
;
1342 Status
= Tpm2SubmitCommand (
1343 InputParameterBlockSize
,
1344 InputParameterBlock
,
1345 &OutputParameterBlockSize
,
1346 OutputParameterBlock
1348 DEBUG ((EFI_D_INFO
, "Tcg2SubmitCommand - %r\n", Status
));
1353 This service returns the currently active PCR banks.
1355 @param[in] This Indicates the calling context
1356 @param[out] ActivePcrBanks Pointer to the variable receiving the bitmap of currently active PCR banks.
1358 @retval EFI_SUCCESS The bitmap of active PCR banks was stored in the ActivePcrBanks parameter.
1359 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1363 Tcg2GetActivePCRBanks (
1364 IN EFI_TCG2_PROTOCOL
*This
,
1365 OUT UINT32
*ActivePcrBanks
1368 if (ActivePcrBanks
== NULL
) {
1369 return EFI_INVALID_PARAMETER
;
1371 *ActivePcrBanks
= mTcgDxeData
.BsCap
.ActivePcrBanks
;
1376 This service sets the currently active PCR banks.
1378 @param[in] This Indicates the calling context
1379 @param[in] ActivePcrBanks Bitmap of the requested active PCR banks. At least one bit SHALL be set.
1381 @retval EFI_SUCCESS The bitmap in ActivePcrBank parameter is already active.
1382 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1386 Tcg2SetActivePCRBanks (
1387 IN EFI_TCG2_PROTOCOL
*This
,
1388 IN UINT32 ActivePcrBanks
1394 DEBUG ((EFI_D_INFO
, "Tcg2SetActivePCRBanks ... (0x%x)\n", ActivePcrBanks
));
1396 if (ActivePcrBanks
== 0) {
1397 return EFI_INVALID_PARAMETER
;
1399 if ((ActivePcrBanks
& (~mTcgDxeData
.BsCap
.HashAlgorithmBitmap
)) != 0) {
1400 return EFI_INVALID_PARAMETER
;
1402 if (ActivePcrBanks
== mTcgDxeData
.BsCap
.ActivePcrBanks
) {
1404 // Need clear previous SET_PCR_BANKS setting
1406 ReturnCode
= Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_NO_ACTION
, 0);
1408 ReturnCode
= Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_SET_PCR_BANKS
, ActivePcrBanks
);
1411 if (ReturnCode
== TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS
) {
1412 Status
= EFI_SUCCESS
;
1413 } else if (ReturnCode
== TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE
) {
1414 Status
= EFI_OUT_OF_RESOURCES
;
1415 } else if (ReturnCode
== TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED
) {
1416 Status
= EFI_UNSUPPORTED
;
1418 Status
= EFI_DEVICE_ERROR
;
1421 DEBUG ((EFI_D_INFO
, "Tcg2SetActivePCRBanks - %r\n", Status
));
1427 This service retrieves the result of a previous invocation of SetActivePcrBanks.
1429 @param[in] This Indicates the calling context
1430 @param[out] OperationPresent Non-zero value to indicate a SetActivePcrBank operation was invoked during the last boot.
1431 @param[out] Response The response from the SetActivePcrBank request.
1433 @retval EFI_SUCCESS The result value could be returned.
1434 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1438 Tcg2GetResultOfSetActivePcrBanks (
1439 IN EFI_TCG2_PROTOCOL
*This
,
1440 OUT UINT32
*OperationPresent
,
1441 OUT UINT32
*Response
1446 if ((OperationPresent
== NULL
) || (Response
== NULL
)) {
1447 return EFI_INVALID_PARAMETER
;
1450 ReturnCode
= Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction (OperationPresent
, Response
);
1451 if (ReturnCode
== TCG_PP_RETURN_TPM_OPERATION_RESPONSE_SUCCESS
) {
1454 return EFI_UNSUPPORTED
;
1458 EFI_TCG2_PROTOCOL mTcg2Protocol
= {
1461 Tcg2HashLogExtendEvent
,
1463 Tcg2GetActivePCRBanks
,
1464 Tcg2SetActivePCRBanks
,
1465 Tcg2GetResultOfSetActivePcrBanks
,
1469 Initialize the Event Log and log events passed from the PEI phase.
1471 @retval EFI_SUCCESS Operation completed successfully.
1472 @retval EFI_OUT_OF_RESOURCES Out of memory.
1482 EFI_PEI_HOB_POINTERS GuidHob
;
1483 EFI_PHYSICAL_ADDRESS Lasa
;
1485 UINT32 DigestListBinSize
;
1487 TCG_EfiSpecIDEventStruct
*TcgEfiSpecIdEventStruct
;
1488 UINT8 TempBuf
[sizeof(TCG_EfiSpecIDEventStruct
) + sizeof(UINT32
) + (HASH_COUNT
* sizeof(TCG_EfiSpecIdEventAlgorithmSize
)) + sizeof(UINT8
)];
1489 TCG_PCR_EVENT_HDR FirstPcrEvent
;
1490 TCG_EfiSpecIdEventAlgorithmSize
*DigestSize
;
1491 TCG_EfiSpecIdEventAlgorithmSize
*TempDigestSize
;
1492 UINT8
*VendorInfoSize
;
1493 UINT32 NumberOfAlgorithms
;
1495 DEBUG ((EFI_D_INFO
, "SetupEventLog\n"));
1498 // 1. Create Log Area
1500 for (Index
= 0; Index
< sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0]); Index
++) {
1501 if ((mTcgDxeData
.BsCap
.SupportedEventLogs
& mTcg2EventInfo
[Index
].LogFormat
) != 0) {
1502 mTcgDxeData
.EventLogAreaStruct
[Index
].EventLogFormat
= mTcg2EventInfo
[Index
].LogFormat
;
1503 Lasa
= (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1);
1504 Status
= gBS
->AllocatePages (
1506 EfiBootServicesData
,
1507 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen
)),
1510 if (EFI_ERROR (Status
)) {
1513 mTcgDxeData
.EventLogAreaStruct
[Index
].Lasa
= Lasa
;
1514 mTcgDxeData
.EventLogAreaStruct
[Index
].Laml
= PcdGet32 (PcdTcgLogAreaMinLen
);
1516 // To initialize them as 0xFF is recommended
1517 // because the OS can know the last entry for that.
1519 SetMem ((VOID
*)(UINTN
)Lasa
, PcdGet32 (PcdTcgLogAreaMinLen
), 0xFF);
1521 // Create first entry for Log Header Entry Data
1523 if (mTcg2EventInfo
[Index
].LogFormat
!= EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
) {
1525 // TcgEfiSpecIdEventStruct
1527 TcgEfiSpecIdEventStruct
= (TCG_EfiSpecIDEventStruct
*)TempBuf
;
1528 CopyMem (TcgEfiSpecIdEventStruct
->signature
, TCG_EfiSpecIDEventStruct_SIGNATURE_03
, sizeof(TcgEfiSpecIdEventStruct
->signature
));
1529 TcgEfiSpecIdEventStruct
->platformClass
= PcdGet8 (PcdTpmPlatformClass
);
1530 TcgEfiSpecIdEventStruct
->specVersionMajor
= TCG_EfiSpecIDEventStruct_SPEC_VERSION_MAJOR_TPM2
;
1531 TcgEfiSpecIdEventStruct
->specVersionMinor
= TCG_EfiSpecIDEventStruct_SPEC_VERSION_MINOR_TPM2
;
1532 TcgEfiSpecIdEventStruct
->specErrata
= TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2
;
1533 TcgEfiSpecIdEventStruct
->uintnSize
= sizeof(UINTN
)/sizeof(UINT32
);
1534 NumberOfAlgorithms
= 0;
1535 DigestSize
= (TCG_EfiSpecIdEventAlgorithmSize
*)((UINT8
*)TcgEfiSpecIdEventStruct
+ sizeof(*TcgEfiSpecIdEventStruct
) + sizeof(NumberOfAlgorithms
));
1536 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA1
) != 0) {
1537 TempDigestSize
= DigestSize
;
1538 TempDigestSize
+= NumberOfAlgorithms
;
1539 TempDigestSize
->algorithmId
= TPM_ALG_SHA1
;
1540 TempDigestSize
->digestSize
= SHA1_DIGEST_SIZE
;
1541 NumberOfAlgorithms
++;
1543 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA256
) != 0) {
1544 TempDigestSize
= DigestSize
;
1545 TempDigestSize
+= NumberOfAlgorithms
;
1546 TempDigestSize
->algorithmId
= TPM_ALG_SHA256
;
1547 TempDigestSize
->digestSize
= SHA256_DIGEST_SIZE
;
1548 NumberOfAlgorithms
++;
1550 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA384
) != 0) {
1551 TempDigestSize
= DigestSize
;
1552 TempDigestSize
+= NumberOfAlgorithms
;
1553 TempDigestSize
->algorithmId
= TPM_ALG_SHA384
;
1554 TempDigestSize
->digestSize
= SHA384_DIGEST_SIZE
;
1555 NumberOfAlgorithms
++;
1557 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA512
) != 0) {
1558 TempDigestSize
= DigestSize
;
1559 TempDigestSize
+= NumberOfAlgorithms
;
1560 TempDigestSize
->algorithmId
= TPM_ALG_SHA512
;
1561 TempDigestSize
->digestSize
= SHA512_DIGEST_SIZE
;
1562 NumberOfAlgorithms
++;
1564 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SM3_256
) != 0) {
1565 TempDigestSize
= DigestSize
;
1566 TempDigestSize
+= NumberOfAlgorithms
;
1567 TempDigestSize
->algorithmId
= TPM_ALG_SM3_256
;
1568 TempDigestSize
->digestSize
= SM3_256_DIGEST_SIZE
;
1569 NumberOfAlgorithms
++;
1571 CopyMem (TcgEfiSpecIdEventStruct
+ 1, &NumberOfAlgorithms
, sizeof(NumberOfAlgorithms
));
1572 TempDigestSize
= DigestSize
;
1573 TempDigestSize
+= NumberOfAlgorithms
;
1574 VendorInfoSize
= (UINT8
*)TempDigestSize
;
1575 *VendorInfoSize
= 0;
1580 FirstPcrEvent
.PCRIndex
= 0;
1581 FirstPcrEvent
.EventType
= EV_NO_ACTION
;
1582 ZeroMem (&FirstPcrEvent
.Digest
, sizeof(FirstPcrEvent
.Digest
));
1583 FirstPcrEvent
.EventSize
= (UINT32
)GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct
);
1588 Status
= TcgDxeLogEvent (
1589 mTcg2EventInfo
[Index
].LogFormat
,
1591 sizeof(FirstPcrEvent
),
1592 (UINT8
*)TcgEfiSpecIdEventStruct
,
1593 FirstPcrEvent
.EventSize
1600 // 2. Create Final Log Area
1602 for (Index
= 0; Index
< sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0]); Index
++) {
1603 if ((mTcgDxeData
.BsCap
.SupportedEventLogs
& mTcg2EventInfo
[Index
].LogFormat
) != 0) {
1604 if (mTcg2EventInfo
[Index
].LogFormat
== EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
) {
1605 Lasa
= (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1);
1606 Status
= gBS
->AllocatePages (
1609 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcg2FinalLogAreaLen
)),
1612 if (EFI_ERROR (Status
)) {
1615 SetMem ((VOID
*)(UINTN
)Lasa
, PcdGet32 (PcdTcg2FinalLogAreaLen
), 0xFF);
1620 mTcgDxeData
.FinalEventsTable
[Index
] = (VOID
*)(UINTN
)Lasa
;
1621 (mTcgDxeData
.FinalEventsTable
[Index
])->Version
= EFI_TCG2_FINAL_EVENTS_TABLE_VERSION
;
1622 (mTcgDxeData
.FinalEventsTable
[Index
])->NumberOfEvents
= 0;
1624 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].EventLogFormat
= mTcg2EventInfo
[Index
].LogFormat
;
1625 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].Lasa
= Lasa
+ sizeof(EFI_TCG2_FINAL_EVENTS_TABLE
);
1626 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].Laml
= PcdGet32 (PcdTcg2FinalLogAreaLen
) - sizeof(EFI_TCG2_FINAL_EVENTS_TABLE
);
1627 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].EventLogSize
= 0;
1628 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].LastEvent
= (VOID
*)(UINTN
)mTcgDxeData
.FinalEventLogAreaStruct
[Index
].Lasa
;
1629 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].EventLogStarted
= FALSE
;
1630 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].EventLogTruncated
= FALSE
;
1633 // Install to configuration table for EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
1635 Status
= gBS
->InstallConfigurationTable (&gEfiTcg2FinalEventsTableGuid
, (VOID
*)mTcgDxeData
.FinalEventsTable
[Index
]);
1636 if (EFI_ERROR (Status
)) {
1641 // No need to handle EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
1643 mTcgDxeData
.FinalEventsTable
[Index
] = NULL
;
1644 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].EventLogFormat
= mTcg2EventInfo
[Index
].LogFormat
;
1645 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].Lasa
= 0;
1646 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].Laml
= 0;
1647 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].EventLogSize
= 0;
1648 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].LastEvent
= 0;
1649 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].EventLogStarted
= FALSE
;
1650 mTcgDxeData
.FinalEventLogAreaStruct
[Index
].EventLogTruncated
= FALSE
;
1656 // 3. Sync data from PEI to DXE
1658 Status
= EFI_SUCCESS
;
1659 for (Index
= 0; Index
< sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0]); Index
++) {
1660 if ((mTcgDxeData
.BsCap
.SupportedEventLogs
& mTcg2EventInfo
[Index
].LogFormat
) != 0) {
1661 GuidHob
.Raw
= GetHobList ();
1662 Status
= EFI_SUCCESS
;
1663 while (!EFI_ERROR (Status
) &&
1664 (GuidHob
.Raw
= GetNextGuidHob (mTcg2EventInfo
[Index
].EventGuid
, GuidHob
.Raw
)) != NULL
) {
1665 TcgEvent
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
1666 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
1667 switch (mTcg2EventInfo
[Index
].LogFormat
) {
1668 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
:
1669 Status
= TcgDxeLogEvent (
1670 mTcg2EventInfo
[Index
].LogFormat
,
1672 sizeof(TCG_PCR_EVENT_HDR
),
1673 ((TCG_PCR_EVENT
*)TcgEvent
)->Event
,
1674 ((TCG_PCR_EVENT_HDR
*)TcgEvent
)->EventSize
1677 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
:
1678 DigestListBinSize
= GetDigestListBinSize ((UINT8
*)TcgEvent
+ sizeof(TCG_PCRINDEX
) + sizeof(TCG_EVENTTYPE
));
1679 CopyMem (&EventSize
, (UINT8
*)TcgEvent
+ sizeof(TCG_PCRINDEX
) + sizeof(TCG_EVENTTYPE
) + DigestListBinSize
, sizeof(UINT32
));
1680 Status
= TcgDxeLogEvent (
1681 mTcg2EventInfo
[Index
].LogFormat
,
1683 sizeof(TCG_PCRINDEX
) + sizeof(TCG_EVENTTYPE
) + DigestListBinSize
+ sizeof(UINT32
),
1684 (UINT8
*)TcgEvent
+ sizeof(TCG_PCRINDEX
) + sizeof(TCG_EVENTTYPE
) + DigestListBinSize
+ sizeof(UINT32
),
1697 Measure and log an action string, and extend the measurement result into PCR[5].
1699 @param[in] String A specific string that indicates an Action event.
1701 @retval EFI_SUCCESS Operation completed successfully.
1702 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1710 TCG_PCR_EVENT_HDR TcgEvent
;
1712 TcgEvent
.PCRIndex
= 5;
1713 TcgEvent
.EventType
= EV_EFI_ACTION
;
1714 TcgEvent
.EventSize
= (UINT32
)AsciiStrLen (String
);
1715 return TcgDxeHashLogExtendEvent (
1725 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
1727 @retval EFI_SUCCESS Operation completed successfully.
1728 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1732 MeasureHandoffTables (
1737 TCG_PCR_EVENT_HDR TcgEvent
;
1738 EFI_HANDOFF_TABLE_POINTERS HandoffTables
;
1740 EFI_CPU_PHYSICAL_LOCATION
*ProcessorLocBuf
;
1742 ProcessorLocBuf
= NULL
;
1743 Status
= EFI_SUCCESS
;
1745 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_SERVER
) {
1748 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
1750 Status
= GetProcessorsCpuLocation(&ProcessorLocBuf
, &ProcessorNum
);
1752 if (!EFI_ERROR(Status
)){
1753 TcgEvent
.PCRIndex
= 1;
1754 TcgEvent
.EventType
= EV_TABLE_OF_DEVICES
;
1755 TcgEvent
.EventSize
= sizeof (HandoffTables
);
1757 HandoffTables
.NumberOfTables
= 1;
1758 HandoffTables
.TableEntry
[0].VendorGuid
= gEfiMpServiceProtocolGuid
;
1759 HandoffTables
.TableEntry
[0].VendorTable
= ProcessorLocBuf
;
1761 Status
= TcgDxeHashLogExtendEvent (
1763 (UINT8
*)(UINTN
)ProcessorLocBuf
,
1764 sizeof(EFI_CPU_PHYSICAL_LOCATION
) * ProcessorNum
,
1766 (UINT8
*)&HandoffTables
1769 FreePool(ProcessorLocBuf
);
1777 Measure and log Separator event, and extend the measurement result into a specific PCR.
1779 @param[in] PCRIndex PCR index.
1781 @retval EFI_SUCCESS Operation completed successfully.
1782 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1786 MeasureSeparatorEvent (
1787 IN TPM_PCRINDEX PCRIndex
1790 TCG_PCR_EVENT_HDR TcgEvent
;
1793 DEBUG ((EFI_D_INFO
, "MeasureSeparatorEvent Pcr - %x\n", PCRIndex
));
1796 TcgEvent
.PCRIndex
= PCRIndex
;
1797 TcgEvent
.EventType
= EV_SEPARATOR
;
1798 TcgEvent
.EventSize
= (UINT32
)sizeof (EventData
);
1799 return TcgDxeHashLogExtendEvent (
1801 (UINT8
*)&EventData
,
1809 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1811 @param[in] PCRIndex PCR Index.
1812 @param[in] EventType Event type.
1813 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1814 @param[in] VendorGuid A unique identifier for the vendor.
1815 @param[in] VarData The content of the variable data.
1816 @param[in] VarSize The size of the variable data.
1818 @retval EFI_SUCCESS Operation completed successfully.
1819 @retval EFI_OUT_OF_RESOURCES Out of memory.
1820 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1825 IN TPM_PCRINDEX PCRIndex
,
1826 IN TCG_EVENTTYPE EventType
,
1828 IN EFI_GUID
*VendorGuid
,
1834 TCG_PCR_EVENT_HDR TcgEvent
;
1835 UINTN VarNameLength
;
1836 EFI_VARIABLE_DATA_TREE
*VarLog
;
1838 DEBUG ((EFI_D_INFO
, "Tcg2Dxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN
)PCRIndex
, (UINTN
)EventType
));
1839 DEBUG ((EFI_D_INFO
, "VariableName - %s, VendorGuid - %g)\n", VarName
, VendorGuid
));
1841 VarNameLength
= StrLen (VarName
);
1842 TcgEvent
.PCRIndex
= PCRIndex
;
1843 TcgEvent
.EventType
= EventType
;
1845 TcgEvent
.EventSize
= (UINT32
)(sizeof (*VarLog
) + VarNameLength
* sizeof (*VarName
) + VarSize
1846 - sizeof (VarLog
->UnicodeName
) - sizeof (VarLog
->VariableData
));
1848 VarLog
= (EFI_VARIABLE_DATA_TREE
*)AllocatePool (TcgEvent
.EventSize
);
1849 if (VarLog
== NULL
) {
1850 return EFI_OUT_OF_RESOURCES
;
1853 VarLog
->VariableName
= *VendorGuid
;
1854 VarLog
->UnicodeNameLength
= VarNameLength
;
1855 VarLog
->VariableDataLength
= VarSize
;
1857 VarLog
->UnicodeName
,
1859 VarNameLength
* sizeof (*VarName
)
1861 if (VarSize
!= 0 && VarData
!= NULL
) {
1863 (CHAR16
*)VarLog
->UnicodeName
+ VarNameLength
,
1869 if (EventType
== EV_EFI_VARIABLE_DRIVER_CONFIG
) {
1871 // Digest is the event data (EFI_VARIABLE_DATA)
1873 Status
= TcgDxeHashLogExtendEvent (
1881 Status
= TcgDxeHashLogExtendEvent (
1894 Read then Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1896 @param[in] PCRIndex PCR Index.
1897 @param[in] EventType Event type.
1898 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1899 @param[in] VendorGuid A unique identifier for the vendor.
1900 @param[out] VarSize The size of the variable data.
1901 @param[out] VarData Pointer to the content of the variable.
1903 @retval EFI_SUCCESS Operation completed successfully.
1904 @retval EFI_OUT_OF_RESOURCES Out of memory.
1905 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1909 ReadAndMeasureVariable (
1910 IN TPM_PCRINDEX PCRIndex
,
1911 IN TCG_EVENTTYPE EventType
,
1913 IN EFI_GUID
*VendorGuid
,
1920 Status
= GetVariable2 (VarName
, VendorGuid
, VarData
, VarSize
);
1921 if (EventType
== EV_EFI_VARIABLE_DRIVER_CONFIG
) {
1922 if (EFI_ERROR (Status
)) {
1924 // It is valid case, so we need handle it.
1931 // if status error, VarData is freed and set NULL by GetVariable2
1933 if (EFI_ERROR (Status
)) {
1934 return EFI_NOT_FOUND
;
1938 Status
= MeasureVariable (
1950 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
1952 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1953 @param[in] VendorGuid A unique identifier for the vendor.
1954 @param[out] VarSize The size of the variable data.
1955 @param[out] VarData Pointer to the content of the variable.
1957 @retval EFI_SUCCESS Operation completed successfully.
1958 @retval EFI_OUT_OF_RESOURCES Out of memory.
1959 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1963 ReadAndMeasureBootVariable (
1965 IN EFI_GUID
*VendorGuid
,
1970 return ReadAndMeasureVariable (
1972 EV_EFI_VARIABLE_BOOT
,
1981 Read then Measure and log an EFI Secure variable, and extend the measurement result into PCR[7].
1983 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1984 @param[in] VendorGuid A unique identifier for the vendor.
1985 @param[out] VarSize The size of the variable data.
1986 @param[out] VarData Pointer to the content of the variable.
1988 @retval EFI_SUCCESS Operation completed successfully.
1989 @retval EFI_OUT_OF_RESOURCES Out of memory.
1990 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1994 ReadAndMeasureSecureVariable (
1996 IN EFI_GUID
*VendorGuid
,
2001 return ReadAndMeasureVariable (
2003 EV_EFI_VARIABLE_DRIVER_CONFIG
,
2012 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
2014 The EFI boot variables are BootOrder and Boot#### variables.
2016 @retval EFI_SUCCESS Operation completed successfully.
2017 @retval EFI_OUT_OF_RESOURCES Out of memory.
2018 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
2022 MeasureAllBootVariables (
2033 Status
= ReadAndMeasureBootVariable (
2035 &gEfiGlobalVariableGuid
,
2037 (VOID
**) &BootOrder
2039 if (Status
== EFI_NOT_FOUND
|| BootOrder
== NULL
) {
2043 if (EFI_ERROR (Status
)) {
2045 // BootOrder can't be NULL if status is not EFI_NOT_FOUND
2047 FreePool (BootOrder
);
2051 BootCount
/= sizeof (*BootOrder
);
2052 for (Index
= 0; Index
< BootCount
; Index
++) {
2053 UnicodeSPrint (mBootVarName
, sizeof (mBootVarName
), L
"Boot%04x", BootOrder
[Index
]);
2054 Status
= ReadAndMeasureBootVariable (
2056 &gEfiGlobalVariableGuid
,
2060 if (!EFI_ERROR (Status
)) {
2061 FreePool (BootVarData
);
2065 FreePool (BootOrder
);
2070 Measure and log all EFI Secure variables, and extend the measurement result into a specific PCR.
2072 The EFI boot variables are BootOrder and Boot#### variables.
2074 @retval EFI_SUCCESS Operation completed successfully.
2075 @retval EFI_OUT_OF_RESOURCES Out of memory.
2076 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
2080 MeasureAllSecureVariables (
2089 Status
= EFI_NOT_FOUND
;
2090 for (Index
= 0; Index
< sizeof(mVariableType
)/sizeof(mVariableType
[0]); Index
++) {
2091 Status
= ReadAndMeasureSecureVariable (
2092 mVariableType
[Index
].VariableName
,
2093 mVariableType
[Index
].VendorGuid
,
2097 if (!EFI_ERROR (Status
)) {
2108 Measure and log launch of FirmwareDebugger, and extend the measurement result into a specific PCR.
2110 @retval EFI_SUCCESS Operation completed successfully.
2111 @retval EFI_OUT_OF_RESOURCES Out of memory.
2112 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
2116 MeasureLaunchOfFirmwareDebugger (
2120 TCG_PCR_EVENT_HDR TcgEvent
;
2122 TcgEvent
.PCRIndex
= 7;
2123 TcgEvent
.EventType
= EV_EFI_ACTION
;
2124 TcgEvent
.EventSize
= sizeof(FIRMWARE_DEBUGGER_EVENT_STRING
) - 1;
2125 return TcgDxeHashLogExtendEvent (
2127 (UINT8
*)FIRMWARE_DEBUGGER_EVENT_STRING
,
2128 sizeof(FIRMWARE_DEBUGGER_EVENT_STRING
) - 1,
2130 (UINT8
*)FIRMWARE_DEBUGGER_EVENT_STRING
2135 Measure and log all Secure Boot Policy, and extend the measurement result into a specific PCR.
2137 Platform firmware adhering to the policy must therefore measure the following values into PCR[7]: (in order listed)
2138 - The contents of the SecureBoot variable
2139 - The contents of the PK variable
2140 - The contents of the KEK variable
2141 - The contents of the EFI_IMAGE_SECURITY_DATABASE variable
2142 - The contents of the EFI_IMAGE_SECURITY_DATABASE1 variable
2144 - Entries in the EFI_IMAGE_SECURITY_DATABASE that are used to validate EFI Drivers or EFI Boot Applications in the boot path
2146 NOTE: Because of the above, UEFI variables PK, KEK, EFI_IMAGE_SECURITY_DATABASE,
2147 EFI_IMAGE_SECURITY_DATABASE1 and SecureBoot SHALL NOT be measured into PCR[3].
2149 @param[in] Event Event whose notification function is being invoked
2150 @param[in] Context Pointer to the notification function's context
2154 MeasureSecureBootPolicy (
2162 Status
= gBS
->LocateProtocol (&gEfiVariableWriteArchProtocolGuid
, NULL
, (VOID
**)&Protocol
);
2163 if (EFI_ERROR (Status
)) {
2167 if (PcdGetBool (PcdFirmwareDebuggerInitialized
)) {
2168 Status
= MeasureLaunchOfFirmwareDebugger ();
2169 DEBUG ((EFI_D_INFO
, "MeasureLaunchOfFirmwareDebugger - %r\n", Status
));
2172 Status
= MeasureAllSecureVariables ();
2173 DEBUG ((EFI_D_INFO
, "MeasureAllSecureVariables - %r\n", Status
));
2176 // We need measure Separator(7) here, because this event must be between SecureBootPolicy (Configure)
2177 // and ImageVerification (Authority)
2178 // There might be a case that we need measure UEFI image from DriverOrder, besides BootOrder. So
2179 // the Authority measurement happen before ReadToBoot event.
2181 Status
= MeasureSeparatorEvent (7);
2182 DEBUG ((EFI_D_INFO
, "MeasureSeparatorEvent - %r\n", Status
));
2187 Ready to Boot Event notification handler.
2189 Sequence of OS boot events is measured in this event notification handler.
2191 @param[in] Event Event whose notification function is being invoked
2192 @param[in] Context Pointer to the notification function's context
2203 TPM_PCRINDEX PcrIndex
;
2205 PERF_START_EX (mImageHandle
, "EventRec", "Tcg2Dxe", 0, PERF_ID_TCG2_DXE
);
2206 if (mBootAttempts
== 0) {
2209 // Measure handoff tables.
2211 Status
= MeasureHandoffTables ();
2212 if (EFI_ERROR (Status
)) {
2213 DEBUG ((EFI_D_ERROR
, "HOBs not Measured. Error!\n"));
2217 // Measure BootOrder & Boot#### variables.
2219 Status
= MeasureAllBootVariables ();
2220 if (EFI_ERROR (Status
)) {
2221 DEBUG ((EFI_D_ERROR
, "Boot Variables not Measured. Error!\n"));
2225 // 1. This is the first boot attempt.
2227 Status
= TcgMeasureAction (
2228 EFI_CALLING_EFI_APPLICATION
2230 if (EFI_ERROR (Status
)) {
2231 DEBUG ((EFI_D_ERROR
, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION
));
2235 // 2. Draw a line between pre-boot env and entering post-boot env.
2236 // PCR[7] is already done.
2238 for (PcrIndex
= 0; PcrIndex
< 7; PcrIndex
++) {
2239 Status
= MeasureSeparatorEvent (PcrIndex
);
2240 if (EFI_ERROR (Status
)) {
2241 DEBUG ((EFI_D_ERROR
, "Seperator Event not Measured. Error!\n"));
2246 // 3. Measure GPT. It would be done in SAP driver.
2250 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
2254 // 5. Read & Measure variable. BootOrder already measured.
2258 // 6. Not first attempt, meaning a return from last attempt
2260 Status
= TcgMeasureAction (
2261 EFI_RETURNING_FROM_EFI_APPLICATOIN
2263 if (EFI_ERROR (Status
)) {
2264 DEBUG ((EFI_D_ERROR
, "%a not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN
));
2268 DEBUG ((EFI_D_INFO
, "TPM2 Tcg2Dxe Measure Data when ReadyToBoot\n"));
2270 // Increase boot attempt counter.
2273 PERF_END_EX (mImageHandle
, "EventRec", "Tcg2Dxe", 0, PERF_ID_TCG2_DXE
+ 1);
2277 Exit Boot Services Event notification handler.
2279 Measure invocation and success of ExitBootServices.
2281 @param[in] Event Event whose notification function is being invoked
2282 @param[in] Context Pointer to the notification function's context
2287 OnExitBootServices (
2295 // Measure invocation of ExitBootServices,
2297 Status
= TcgMeasureAction (
2298 EFI_EXIT_BOOT_SERVICES_INVOCATION
2300 if (EFI_ERROR (Status
)) {
2301 DEBUG ((EFI_D_ERROR
, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION
));
2305 // Measure success of ExitBootServices
2307 Status
= TcgMeasureAction (
2308 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
2310 if (EFI_ERROR (Status
)) {
2311 DEBUG ((EFI_D_ERROR
, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED
));
2316 Exit Boot Services Failed Event notification handler.
2318 Measure Failure of ExitBootServices.
2320 @param[in] Event Event whose notification function is being invoked
2321 @param[in] Context Pointer to the notification function's context
2326 OnExitBootServicesFailed (
2334 // Measure Failure of ExitBootServices,
2336 Status
= TcgMeasureAction (
2337 EFI_EXIT_BOOT_SERVICES_FAILED
2339 if (EFI_ERROR (Status
)) {
2340 DEBUG ((EFI_D_ERROR
, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED
));
2346 The function install Tcg2 protocol.
2348 @retval EFI_SUCCESS Tcg2 protocol is installed.
2349 @retval other Some error occurs.
2360 Status
= gBS
->InstallMultipleProtocolInterfaces (
2362 &gEfiTcg2ProtocolGuid
,
2370 The driver's entry point. It publishes EFI Tcg2 Protocol.
2372 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2373 @param[in] SystemTable A pointer to the EFI System Table.
2375 @retval EFI_SUCCESS The entry point is executed successfully.
2376 @retval other Some error occurs when executing this entry point.
2381 IN EFI_HANDLE ImageHandle
,
2382 IN EFI_SYSTEM_TABLE
*SystemTable
2388 UINT32 MaxCommandSize
;
2389 UINT32 MaxResponseSize
;
2390 TPML_PCR_SELECTION Pcrs
;
2392 EFI_TCG2_EVENT_ALGORITHM_BITMAP TpmHashAlgorithmBitmap
;
2393 UINT32 ActivePCRBanks
;
2394 UINT32 NumberOfPCRBanks
;
2396 mImageHandle
= ImageHandle
;
2398 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &gEfiTpmDeviceInstanceNoneGuid
) ||
2399 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &gEfiTpmDeviceInstanceTpm12Guid
)){
2400 DEBUG ((EFI_D_ERROR
, "No TPM2 instance required!\n"));
2401 return EFI_UNSUPPORTED
;
2404 if (GetFirstGuidHob (&gTpmErrorHobGuid
) != NULL
) {
2405 DEBUG ((EFI_D_ERROR
, "TPM2 error!\n"));
2406 return EFI_DEVICE_ERROR
;
2409 Status
= Tpm2RequestUseTpm ();
2410 if (EFI_ERROR (Status
)) {
2411 DEBUG ((EFI_D_ERROR
, "TPM2 not detected!\n"));
2418 ASSERT (TCG_EVENT_LOG_AREA_COUNT_MAX
== sizeof(mTcg2EventInfo
)/sizeof(mTcg2EventInfo
[0]));
2420 mTcgDxeData
.BsCap
.Size
= sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY
);
2421 mTcgDxeData
.BsCap
.ProtocolVersion
.Major
= 1;
2422 mTcgDxeData
.BsCap
.ProtocolVersion
.Minor
= 1;
2423 mTcgDxeData
.BsCap
.StructureVersion
.Major
= 1;
2424 mTcgDxeData
.BsCap
.StructureVersion
.Minor
= 1;
2426 DEBUG ((EFI_D_INFO
, "Tcg2.ProtocolVersion - %02x.%02x\n", mTcgDxeData
.BsCap
.ProtocolVersion
.Major
, mTcgDxeData
.BsCap
.ProtocolVersion
.Minor
));
2427 DEBUG ((EFI_D_INFO
, "Tcg2.StructureVersion - %02x.%02x\n", mTcgDxeData
.BsCap
.StructureVersion
.Major
, mTcgDxeData
.BsCap
.StructureVersion
.Minor
));
2429 Status
= Tpm2GetCapabilityManufactureID (&mTcgDxeData
.BsCap
.ManufacturerID
);
2430 if (EFI_ERROR (Status
)) {
2431 DEBUG ((EFI_D_ERROR
, "Tpm2GetCapabilityManufactureID fail!\n"));
2433 DEBUG ((EFI_D_INFO
, "Tpm2GetCapabilityManufactureID - %08x\n", mTcgDxeData
.BsCap
.ManufacturerID
));
2437 UINT32 FirmwareVersion1
;
2438 UINT32 FirmwareVersion2
;
2440 Status
= Tpm2GetCapabilityFirmwareVersion (&FirmwareVersion1
, &FirmwareVersion2
);
2441 if (EFI_ERROR (Status
)) {
2442 DEBUG ((EFI_D_ERROR
, "Tpm2GetCapabilityFirmwareVersion fail!\n"));
2444 DEBUG ((EFI_D_INFO
, "Tpm2GetCapabilityFirmwareVersion - %08x %08x\n", FirmwareVersion1
, FirmwareVersion2
));
2448 Status
= Tpm2GetCapabilityMaxCommandResponseSize (&MaxCommandSize
, &MaxResponseSize
);
2449 if (EFI_ERROR (Status
)) {
2450 DEBUG ((EFI_D_ERROR
, "Tpm2GetCapabilityMaxCommandResponseSize fail!\n"));
2452 mTcgDxeData
.BsCap
.MaxCommandSize
= (UINT16
)MaxCommandSize
;
2453 mTcgDxeData
.BsCap
.MaxResponseSize
= (UINT16
)MaxResponseSize
;
2454 DEBUG ((EFI_D_INFO
, "Tpm2GetCapabilityMaxCommandResponseSize - %08x, %08x\n", MaxCommandSize
, MaxResponseSize
));
2458 // Get supported PCR and current Active PCRs
2460 Status
= Tpm2GetCapabilityPcrs (&Pcrs
);
2461 if (EFI_ERROR (Status
)) {
2462 DEBUG ((EFI_D_ERROR
, "Tpm2GetCapabilityPcrs fail!\n"));
2463 TpmHashAlgorithmBitmap
= EFI_TCG2_BOOT_HASH_ALG_SHA1
;
2464 ActivePCRBanks
= EFI_TCG2_BOOT_HASH_ALG_SHA1
;
2466 DEBUG ((EFI_D_INFO
, "Tpm2GetCapabilityPcrs Count - %08x\n", Pcrs
.count
));
2467 TpmHashAlgorithmBitmap
= 0;
2469 for (Index
= 0; Index
< Pcrs
.count
; Index
++) {
2470 DEBUG ((EFI_D_INFO
, "hash - %x\n", Pcrs
.pcrSelections
[Index
].hash
));
2471 switch (Pcrs
.pcrSelections
[Index
].hash
) {
2473 TpmHashAlgorithmBitmap
|= EFI_TCG2_BOOT_HASH_ALG_SHA1
;
2474 if (!IsZeroBuffer (Pcrs
.pcrSelections
[Index
].pcrSelect
, Pcrs
.pcrSelections
[Index
].sizeofSelect
)) {
2475 ActivePCRBanks
|= EFI_TCG2_BOOT_HASH_ALG_SHA1
;
2478 case TPM_ALG_SHA256
:
2479 TpmHashAlgorithmBitmap
|= EFI_TCG2_BOOT_HASH_ALG_SHA256
;
2480 if (!IsZeroBuffer (Pcrs
.pcrSelections
[Index
].pcrSelect
, Pcrs
.pcrSelections
[Index
].sizeofSelect
)) {
2481 ActivePCRBanks
|= EFI_TCG2_BOOT_HASH_ALG_SHA256
;
2484 case TPM_ALG_SHA384
:
2485 TpmHashAlgorithmBitmap
|= EFI_TCG2_BOOT_HASH_ALG_SHA384
;
2486 if (!IsZeroBuffer (Pcrs
.pcrSelections
[Index
].pcrSelect
, Pcrs
.pcrSelections
[Index
].sizeofSelect
)) {
2487 ActivePCRBanks
|= EFI_TCG2_BOOT_HASH_ALG_SHA384
;
2490 case TPM_ALG_SHA512
:
2491 TpmHashAlgorithmBitmap
|= EFI_TCG2_BOOT_HASH_ALG_SHA512
;
2492 if (!IsZeroBuffer (Pcrs
.pcrSelections
[Index
].pcrSelect
, Pcrs
.pcrSelections
[Index
].sizeofSelect
)) {
2493 ActivePCRBanks
|= EFI_TCG2_BOOT_HASH_ALG_SHA512
;
2496 case TPM_ALG_SM3_256
:
2497 TpmHashAlgorithmBitmap
|= EFI_TCG2_BOOT_HASH_ALG_SM3_256
;
2498 if (!IsZeroBuffer (Pcrs
.pcrSelections
[Index
].pcrSelect
, Pcrs
.pcrSelections
[Index
].sizeofSelect
)) {
2499 ActivePCRBanks
|= EFI_TCG2_BOOT_HASH_ALG_SM3_256
;
2505 mTcgDxeData
.BsCap
.HashAlgorithmBitmap
= TpmHashAlgorithmBitmap
& PcdGet32 (PcdTcg2HashAlgorithmBitmap
);
2506 mTcgDxeData
.BsCap
.ActivePcrBanks
= ActivePCRBanks
& PcdGet32 (PcdTcg2HashAlgorithmBitmap
);
2509 // Need calculate NumberOfPCRBanks here, because HashAlgorithmBitmap might be removed by PCD.
2511 NumberOfPCRBanks
= 0;
2512 for (Index
= 0; Index
< 32; Index
++) {
2513 if ((mTcgDxeData
.BsCap
.HashAlgorithmBitmap
& (1u << Index
)) != 0) {
2518 if (PcdGet32 (PcdTcg2NumberOfPCRBanks
) == 0) {
2519 mTcgDxeData
.BsCap
.NumberOfPCRBanks
= NumberOfPCRBanks
;
2521 mTcgDxeData
.BsCap
.NumberOfPCRBanks
= PcdGet32 (PcdTcg2NumberOfPCRBanks
);
2522 if (PcdGet32 (PcdTcg2NumberOfPCRBanks
) > NumberOfPCRBanks
) {
2523 DEBUG ((EFI_D_ERROR
, "ERROR: PcdTcg2NumberOfPCRBanks(0x%x) > NumberOfPCRBanks(0x%x)\n", PcdGet32 (PcdTcg2NumberOfPCRBanks
), NumberOfPCRBanks
));
2524 mTcgDxeData
.BsCap
.NumberOfPCRBanks
= NumberOfPCRBanks
;
2528 mTcgDxeData
.BsCap
.SupportedEventLogs
= EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
| EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
;
2529 if ((mTcgDxeData
.BsCap
.ActivePcrBanks
& EFI_TCG2_BOOT_HASH_ALG_SHA1
) == 0) {
2531 // No need to expose TCG1.2 event log if SHA1 bank does not exist.
2533 mTcgDxeData
.BsCap
.SupportedEventLogs
&= ~EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
;
2536 DEBUG ((EFI_D_INFO
, "Tcg2.SupportedEventLogs - 0x%08x\n", mTcgDxeData
.BsCap
.SupportedEventLogs
));
2537 DEBUG ((EFI_D_INFO
, "Tcg2.HashAlgorithmBitmap - 0x%08x\n", mTcgDxeData
.BsCap
.HashAlgorithmBitmap
));
2538 DEBUG ((EFI_D_INFO
, "Tcg2.NumberOfPCRBanks - 0x%08x\n", mTcgDxeData
.BsCap
.NumberOfPCRBanks
));
2539 DEBUG ((EFI_D_INFO
, "Tcg2.ActivePcrBanks - 0x%08x\n", mTcgDxeData
.BsCap
.ActivePcrBanks
));
2541 if (mTcgDxeData
.BsCap
.TPMPresentFlag
) {
2543 // Setup the log area and copy event log from hob list to it
2545 Status
= SetupEventLog ();
2546 ASSERT_EFI_ERROR (Status
);
2549 // Measure handoff tables, Boot#### variables etc.
2551 Status
= EfiCreateEventReadyToBootEx (
2558 Status
= gBS
->CreateEventEx (
2563 &gEfiEventExitBootServicesGuid
,
2568 // Measure Exit Boot Service failed
2570 Status
= gBS
->CreateEventEx (
2573 OnExitBootServicesFailed
,
2575 &gEventExitBootServicesFailedGuid
,
2580 // Create event callback, because we need access variable on SecureBootPolicyVariable
2581 // We should use VariableWriteArch instead of VariableArch, because Variable driver
2582 // may update SecureBoot value based on last setting.
2584 EfiCreateProtocolNotifyEvent (&gEfiVariableWriteArchProtocolGuid
, TPL_CALLBACK
, MeasureSecureBootPolicy
, NULL
, &Registration
);
2588 // Install Tcg2Protocol
2590 Status
= InstallTcg2 ();
2591 DEBUG ((EFI_D_INFO
, "InstallTcg2 - %r\n", Status
));