2 This module implements TCG EFI Protocol.
4 Caution: This module requires additional review when modified.
5 This driver will have external input - TcgDxePassThroughToTpm
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
9 TcgDxePassThroughToTpm() will receive untrusted input and do basic validation.
11 Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>
12 This program and the accompanying materials
13 are licensed and made available under the terms and conditions of the BSD License
14 which accompanies this distribution. The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 #include <IndustryStandard/Tpm12.h>
24 #include <IndustryStandard/Acpi.h>
25 #include <IndustryStandard/PeImage.h>
26 #include <IndustryStandard/SmBios.h>
27 #include <IndustryStandard/TcpaAcpi.h>
29 #include <Guid/GlobalVariable.h>
30 #include <Guid/SmBios.h>
31 #include <Guid/HobList.h>
32 #include <Guid/TcgEventHob.h>
33 #include <Guid/EventGroup.h>
34 #include <Guid/EventExitBootServiceFailed.h>
35 #include <Guid/TpmInstance.h>
37 #include <Protocol/DevicePath.h>
38 #include <Protocol/TcgService.h>
39 #include <Protocol/AcpiTable.h>
40 #include <Protocol/MpService.h>
42 #include <Library/DebugLib.h>
43 #include <Library/BaseMemoryLib.h>
44 #include <Library/UefiRuntimeServicesTableLib.h>
45 #include <Library/UefiDriverEntryPoint.h>
46 #include <Library/HobLib.h>
47 #include <Library/UefiBootServicesTableLib.h>
48 #include <Library/BaseLib.h>
49 #include <Library/MemoryAllocationLib.h>
50 #include <Library/PrintLib.h>
51 #include <Library/TpmCommLib.h>
52 #include <Library/PcdLib.h>
53 #include <Library/UefiLib.h>
57 #define EFI_TCG_LOG_AREA_SIZE 0x10000
59 #define TCG_DXE_DATA_FROM_THIS(this) \
60 BASE_CR (this, TCG_DXE_DATA, TcgProtocol)
62 typedef struct _TCG_DXE_DATA
{
63 EFI_TCG_PROTOCOL TcgProtocol
;
64 TCG_EFI_BOOT_SERVICE_CAPABILITY BsCap
;
65 EFI_TCG_CLIENT_ACPI_TABLE
*TcgClientAcpiTable
;
66 EFI_TCG_SERVER_ACPI_TABLE
*TcgServerAcpiTable
;
69 TIS_TPM_HANDLE TpmHandle
;
74 EFI_TCG_CLIENT_ACPI_TABLE mTcgClientAcpiTemplate
= {
76 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE
,
77 sizeof (mTcgClientAcpiTemplate
),
80 // Compiler initializes the remaining bytes to 0
81 // These fields should be filled in in production
84 0, // 0 for PC Client Platform Class
85 0, // Log Area Max Length
86 (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1) // Log Area Start Address
90 // The following EFI_TCG_SERVER_ACPI_TABLE default setting is just one example,
91 // the TPM device connectes to LPC, and also defined the ACPI _UID as 0xFF,
92 // this _UID can be changed and should match with the _UID setting of the TPM
95 EFI_TCG_SERVER_ACPI_TABLE mTcgServerAcpiTemplate
= {
97 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE
,
98 sizeof (mTcgServerAcpiTemplate
),
101 // Compiler initializes the remaining bytes to 0
102 // These fields should be filled in in production
105 1, // 1 for Server Platform Class
107 0, // Log Area Max Length
108 (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1), // Log Area Start Address
109 0x0100, // TCG Specification revision 1.0
111 0, // Interrupt Flags
113 {0}, // Reserved 3 bytes
114 0, // Global System Interrupt
116 EFI_ACPI_3_0_SYSTEM_MEMORY
,
120 TPM_BASE_ADDRESS
// Base Address
123 {0}, // Configuration Address
124 0xFF, // ACPI _UID value of the device, can be changed for different platforms
125 0, // ACPI _UID value of the device, can be changed for different platforms
126 0, // ACPI _UID value of the device, can be changed for different platforms
127 0 // ACPI _UID value of the device, can be changed for different platforms
130 UINTN mBootAttempts
= 0;
131 CHAR16 mBootVarName
[] = L
"BootOrder";
134 Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function
135 Caller is responsible to free LocationBuf.
137 @param[out] LocationBuf Returns Processor Location Buffer.
138 @param[out] Num Returns processor number.
140 @retval EFI_SUCCESS Operation completed successfully.
141 @retval EFI_UNSUPPORTED MpService protocol not found.
145 GetProcessorsCpuLocation (
146 OUT EFI_CPU_PHYSICAL_LOCATION
**LocationBuf
,
151 EFI_MP_SERVICES_PROTOCOL
*MpProtocol
;
153 UINTN EnabledProcessorNum
;
154 EFI_PROCESSOR_INFORMATION ProcessorInfo
;
155 EFI_CPU_PHYSICAL_LOCATION
*ProcessorLocBuf
;
158 Status
= gBS
->LocateProtocol (&gEfiMpServiceProtocolGuid
, NULL
, (VOID
**) &MpProtocol
);
159 if (EFI_ERROR (Status
)) {
161 // MP protocol is not installed
163 return EFI_UNSUPPORTED
;
166 Status
= MpProtocol
->GetNumberOfProcessors(
171 if (EFI_ERROR(Status
)){
175 Status
= gBS
->AllocatePool(
177 sizeof(EFI_CPU_PHYSICAL_LOCATION
) * ProcessorNum
,
178 (VOID
**) &ProcessorLocBuf
180 if (EFI_ERROR(Status
)){
185 // Get each processor Location info
187 for (Index
= 0; Index
< ProcessorNum
; Index
++) {
188 Status
= MpProtocol
->GetProcessorInfo(
193 if (EFI_ERROR(Status
)){
194 FreePool(ProcessorLocBuf
);
199 // Get all Processor Location info & measure
202 &ProcessorLocBuf
[Index
],
203 &ProcessorInfo
.Location
,
204 sizeof(EFI_CPU_PHYSICAL_LOCATION
)
208 *LocationBuf
= ProcessorLocBuf
;
215 This service provides EFI protocol capability information, state information
216 about the TPM, and Event Log state information.
218 @param[in] This Indicates the calling context
219 @param[out] ProtocolCapability The callee allocates memory for a TCG_BOOT_SERVICE_CAPABILITY
220 structure and fills in the fields with the EFI protocol
221 capability information and the current TPM state information.
222 @param[out] TCGFeatureFlags This is a pointer to the feature flags. No feature
223 flags are currently defined so this parameter
224 MUST be set to 0. However, in the future,
225 feature flags may be defined that, for example,
226 enable hash algorithm agility.
227 @param[out] EventLogLocation This is a pointer to the address of the event log in memory.
228 @param[out] EventLogLastEntry If the Event Log contains more than one entry,
229 this is a pointer to the address of the start of
230 the last entry in the event log in memory.
232 @retval EFI_SUCCESS Operation completed successfully.
233 @retval EFI_INVALID_PARAMETER ProtocolCapability does not match TCG capability.
239 IN EFI_TCG_PROTOCOL
*This
,
240 OUT TCG_EFI_BOOT_SERVICE_CAPABILITY
*ProtocolCapability
,
241 OUT UINT32
*TCGFeatureFlags
,
242 OUT EFI_PHYSICAL_ADDRESS
*EventLogLocation
,
243 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
246 TCG_DXE_DATA
*TcgData
;
248 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
250 if (ProtocolCapability
!= NULL
) {
251 *ProtocolCapability
= TcgData
->BsCap
;
254 if (TCGFeatureFlags
!= NULL
) {
255 *TCGFeatureFlags
= 0;
258 if (EventLogLocation
!= NULL
) {
259 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
260 *EventLogLocation
= TcgData
->TcgClientAcpiTable
->Lasa
;
262 *EventLogLocation
= TcgData
->TcgServerAcpiTable
->Lasa
;
266 if (EventLogLastEntry
!= NULL
) {
267 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
268 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)0;
270 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)TcgData
->LastEvent
;
278 This service abstracts the capability to do a hash operation on a data buffer.
280 @param[in] This Indicates the calling context
281 @param[in] HashData Pointer to the data buffer to be hashed
282 @param[in] HashDataLen Length of the data buffer to be hashed
283 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
284 @param[in, out] HashedDataLen Resultant length of the hashed data
285 @param[in, out] HashedDataResult Resultant buffer of the hashed data
287 @retval EFI_SUCCESS Operation completed successfully.
288 @retval EFI_INVALID_PARAMETER HashDataLen is NULL.
289 @retval EFI_INVALID_PARAMETER HashDataLenResult is NULL.
290 @retval EFI_OUT_OF_RESOURCES Cannot allocate buffer of size *HashedDataLen.
291 @retval EFI_UNSUPPORTED AlgorithmId not supported.
292 @retval EFI_BUFFER_TOO_SMALL *HashedDataLen < sizeof (TCG_DIGEST).
298 IN EFI_TCG_PROTOCOL
*This
,
300 IN UINT64 HashDataLen
,
301 IN TCG_ALGORITHM_ID AlgorithmId
,
302 IN OUT UINT64
*HashedDataLen
,
303 IN OUT UINT8
**HashedDataResult
306 if (HashedDataLen
== NULL
|| HashedDataResult
== NULL
) {
307 return EFI_INVALID_PARAMETER
;
310 switch (AlgorithmId
) {
312 if (*HashedDataLen
== 0) {
313 *HashedDataLen
= sizeof (TPM_DIGEST
);
314 *HashedDataResult
= AllocatePool ((UINTN
) *HashedDataLen
);
315 if (*HashedDataResult
== NULL
) {
316 return EFI_OUT_OF_RESOURCES
;
320 if (*HashedDataLen
< sizeof (TPM_DIGEST
)) {
321 *HashedDataLen
= sizeof (TPM_DIGEST
);
322 return EFI_BUFFER_TOO_SMALL
;
324 *HashedDataLen
= sizeof (TPM_DIGEST
);
326 if (*HashedDataResult
== NULL
) {
327 *HashedDataResult
= AllocatePool ((UINTN
) *HashedDataLen
);
330 return TpmCommHashAll (
333 (TPM_DIGEST
*)*HashedDataResult
336 return EFI_UNSUPPORTED
;
341 Add a new entry to the Event Log.
343 @param[in] TcgData TCG_DXE_DATA structure.
344 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
345 @param[in] NewEventData Pointer to the new event data.
347 @retval EFI_SUCCESS The new event log entry was added.
348 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
354 IN TCG_DXE_DATA
*TcgData
,
355 IN TCG_PCR_EVENT_HDR
*NewEventHdr
,
356 IN UINT8
*NewEventData
359 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
360 TcgData
->LastEvent
= (UINT8
*)(UINTN
)TcgData
->TcgClientAcpiTable
->Lasa
;
361 return TpmCommLogEvent (
363 &TcgData
->EventLogSize
,
364 (UINTN
)TcgData
->TcgClientAcpiTable
->Laml
,
369 TcgData
->LastEvent
= (UINT8
*)(UINTN
)TcgData
->TcgServerAcpiTable
->Lasa
;
370 return TpmCommLogEvent (
372 &TcgData
->EventLogSize
,
373 (UINTN
)TcgData
->TcgServerAcpiTable
->Laml
,
381 This service abstracts the capability to add an entry to the Event Log.
383 @param[in] This Indicates the calling context
384 @param[in] TCGLogData Pointer to the start of the data buffer containing
385 the TCG_PCR_EVENT data structure. All fields in
386 this structure are properly filled by the caller.
387 @param[in, out] EventNumber The event number of the event just logged
388 @param[in] Flags Indicate additional flags. Only one flag has been
389 defined at this time, which is 0x01 and means the
390 extend operation should not be performed. All
391 other bits are reserved.
393 @retval EFI_SUCCESS Operation completed successfully.
394 @retval EFI_OUT_OF_RESOURCES Insufficient memory in the event log to complete this action.
400 IN EFI_TCG_PROTOCOL
*This
,
401 IN TCG_PCR_EVENT
*TCGLogData
,
402 IN OUT UINT32
*EventNumber
,
406 TCG_DXE_DATA
*TcgData
;
408 if (TCGLogData
== NULL
){
409 return EFI_INVALID_PARAMETER
;
412 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
414 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
415 return EFI_DEVICE_ERROR
;
417 return TcgDxeLogEventI (
419 (TCG_PCR_EVENT_HDR
*)TCGLogData
,
425 This service is a proxy for commands to the TPM.
427 @param[in] This Indicates the calling context
428 @param[in] TpmInputParameterBlockSize Size of the TPM input parameter block
429 @param[in] TpmInputParameterBlock Pointer to the TPM input parameter block
430 @param[in] TpmOutputParameterBlockSize Size of the TPM output parameter block
431 @param[in] TpmOutputParameterBlock Pointer to the TPM output parameter block
433 @retval EFI_SUCCESS Operation completed successfully.
434 @retval EFI_INVALID_PARAMETER Invalid ordinal.
435 @retval EFI_UNSUPPORTED Current Task Priority Level >= EFI_TPL_CALLBACK.
436 @retval EFI_TIMEOUT The TIS timed-out.
441 TcgDxePassThroughToTpm (
442 IN EFI_TCG_PROTOCOL
*This
,
443 IN UINT32 TpmInputParameterBlockSize
,
444 IN UINT8
*TpmInputParameterBlock
,
445 IN UINT32 TpmOutputParameterBlockSize
,
446 IN UINT8
*TpmOutputParameterBlock
449 TCG_DXE_DATA
*TcgData
;
451 if (TpmInputParameterBlock
== NULL
||
452 TpmOutputParameterBlock
== NULL
||
453 TpmInputParameterBlockSize
== 0 ||
454 TpmOutputParameterBlockSize
== 0) {
455 return EFI_INVALID_PARAMETER
;
458 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
460 return TisPcExecute (
463 TpmInputParameterBlock
,
464 (UINTN
) TpmInputParameterBlockSize
,
465 TpmOutputParameterBlock
,
466 (UINTN
) TpmOutputParameterBlockSize
471 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
472 and add an entry to the Event Log.
474 @param[in] TcgData TCG_DXE_DATA structure.
475 @param[in] HashData Physical address of the start of the data buffer
476 to be hashed, extended, and logged.
477 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
478 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
479 @param[in] NewEventData Pointer to the new event data.
481 @retval EFI_SUCCESS Operation completed successfully.
482 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
483 @retval EFI_DEVICE_ERROR The command was unsuccessful.
488 TcgDxeHashLogExtendEventI (
489 IN TCG_DXE_DATA
*TcgData
,
491 IN UINT64 HashDataLen
,
492 IN OUT TCG_PCR_EVENT_HDR
*NewEventHdr
,
493 IN UINT8
*NewEventData
498 if (HashData
== NULL
&& HashDataLen
> 0) {
499 return EFI_INVALID_PARAMETER
;
502 if (HashDataLen
> 0 || HashData
!= NULL
) {
503 Status
= TpmCommHashAll (
508 ASSERT_EFI_ERROR (Status
);
511 Status
= TpmCommExtend (
513 &NewEventHdr
->Digest
,
514 NewEventHdr
->PCRIndex
,
517 if (!EFI_ERROR (Status
)) {
518 Status
= TcgDxeLogEventI (TcgData
, NewEventHdr
, NewEventData
);
525 This service abstracts the capability to do a hash operation on a data buffer,
526 extend a specific TPM PCR with the hash result, and add an entry to the Event Log
528 @param[in] This Indicates the calling context
529 @param[in] HashData Physical address of the start of the data buffer
530 to be hashed, extended, and logged.
531 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
532 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
533 @param[in, out] TCGLogData The physical address of the start of the data
534 buffer containing the TCG_PCR_EVENT data structure.
535 @param[in, out] EventNumber The event number of the event just logged.
536 @param[out] EventLogLastEntry Physical address of the first byte of the entry
537 just placed in the Event Log. If the Event Log was
538 empty when this function was called then this physical
539 address will be the same as the physical address of
540 the start of the Event Log.
542 @retval EFI_SUCCESS Operation completed successfully.
543 @retval EFI_UNSUPPORTED AlgorithmId != TPM_ALG_SHA.
544 @retval EFI_UNSUPPORTED Current TPL >= EFI_TPL_CALLBACK.
545 @retval EFI_DEVICE_ERROR The command was unsuccessful.
550 TcgDxeHashLogExtendEvent (
551 IN EFI_TCG_PROTOCOL
*This
,
552 IN EFI_PHYSICAL_ADDRESS HashData
,
553 IN UINT64 HashDataLen
,
554 IN TPM_ALGORITHM_ID AlgorithmId
,
555 IN OUT TCG_PCR_EVENT
*TCGLogData
,
556 IN OUT UINT32
*EventNumber
,
557 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
560 TCG_DXE_DATA
*TcgData
;
563 if (TCGLogData
== NULL
|| EventLogLastEntry
== NULL
){
564 return EFI_INVALID_PARAMETER
;
567 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
569 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
570 return EFI_DEVICE_ERROR
;
573 if (AlgorithmId
!= TPM_ALG_SHA
) {
574 return EFI_UNSUPPORTED
;
577 Status
= TcgDxeHashLogExtendEventI (
579 (UINT8
*) (UINTN
) HashData
,
581 (TCG_PCR_EVENT_HDR
*)TCGLogData
,
585 if (!EFI_ERROR(Status
)){
586 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) TcgData
->LastEvent
;
592 TCG_DXE_DATA mTcgDxeData
= {
597 TcgDxePassThroughToTpm
,
598 TcgDxeHashLogExtendEvent
601 sizeof (mTcgDxeData
.BsCap
),
608 &mTcgClientAcpiTemplate
,
609 &mTcgServerAcpiTemplate
,
616 Initialize the Event Log and log events passed from the PEI phase.
618 @retval EFI_SUCCESS Operation completed successfully.
619 @retval EFI_OUT_OF_RESOURCES Out of memory.
629 TCG_PCR_EVENT
*TcgEvent
;
630 EFI_PEI_HOB_POINTERS GuidHob
;
631 EFI_PHYSICAL_ADDRESS Lasa
;
633 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
634 Lasa
= mTcgClientAcpiTemplate
.Lasa
;
636 Status
= gBS
->AllocatePages (
639 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE
),
642 if (EFI_ERROR (Status
)) {
645 mTcgClientAcpiTemplate
.Lasa
= Lasa
;
647 // To initialize them as 0xFF is recommended
648 // because the OS can know the last entry for that.
650 SetMem ((VOID
*)(UINTN
)mTcgClientAcpiTemplate
.Lasa
, EFI_TCG_LOG_AREA_SIZE
, 0xFF);
651 mTcgClientAcpiTemplate
.Laml
= EFI_TCG_LOG_AREA_SIZE
;
654 Lasa
= mTcgServerAcpiTemplate
.Lasa
;
656 Status
= gBS
->AllocatePages (
659 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE
),
662 if (EFI_ERROR (Status
)) {
665 mTcgServerAcpiTemplate
.Lasa
= Lasa
;
667 // To initialize them as 0xFF is recommended
668 // because the OS can know the last entry for that.
670 SetMem ((VOID
*)(UINTN
)mTcgServerAcpiTemplate
.Lasa
, EFI_TCG_LOG_AREA_SIZE
, 0xFF);
671 mTcgServerAcpiTemplate
.Laml
= EFI_TCG_LOG_AREA_SIZE
;
674 GuidHob
.Raw
= GetHobList ();
675 while (!EFI_ERROR (Status
) &&
676 (GuidHob
.Raw
= GetNextGuidHob (&gTcgEventEntryHobGuid
, GuidHob
.Raw
)) != NULL
) {
677 TcgEvent
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
678 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
679 Status
= TcgDxeLogEventI (
681 (TCG_PCR_EVENT_HDR
*)TcgEvent
,
690 Measure and log an action string, and extend the measurement result into PCR[5].
692 @param[in] String A specific string that indicates an Action event.
694 @retval EFI_SUCCESS Operation completed successfully.
695 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
704 TCG_PCR_EVENT_HDR TcgEvent
;
706 TcgEvent
.PCRIndex
= 5;
707 TcgEvent
.EventType
= EV_EFI_ACTION
;
708 TcgEvent
.EventSize
= (UINT32
)AsciiStrLen (String
);
709 return TcgDxeHashLogExtendEventI (
719 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
721 @retval EFI_SUCCESS Operation completed successfully.
722 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
727 MeasureHandoffTables (
732 SMBIOS_TABLE_ENTRY_POINT
*SmbiosTable
;
733 TCG_PCR_EVENT_HDR TcgEvent
;
734 EFI_HANDOFF_TABLE_POINTERS HandoffTables
;
736 EFI_CPU_PHYSICAL_LOCATION
*ProcessorLocBuf
;
739 // Measure SMBIOS with EV_EFI_HANDOFF_TABLES to PCR[1]
741 Status
= EfiGetSystemConfigurationTable (
742 &gEfiSmbiosTableGuid
,
743 (VOID
**) &SmbiosTable
746 if (!EFI_ERROR (Status
)) {
747 ASSERT (SmbiosTable
!= NULL
);
749 TcgEvent
.PCRIndex
= 1;
750 TcgEvent
.EventType
= EV_EFI_HANDOFF_TABLES
;
751 TcgEvent
.EventSize
= sizeof (HandoffTables
);
753 HandoffTables
.NumberOfTables
= 1;
754 HandoffTables
.TableEntry
[0].VendorGuid
= gEfiSmbiosTableGuid
;
755 HandoffTables
.TableEntry
[0].VendorTable
= SmbiosTable
;
757 DEBUG ((DEBUG_INFO
, "The Smbios Table starts at: 0x%x\n", SmbiosTable
->TableAddress
));
758 DEBUG ((DEBUG_INFO
, "The Smbios Table size: 0x%x\n", SmbiosTable
->TableLength
));
760 Status
= TcgDxeHashLogExtendEventI (
762 (UINT8
*)(UINTN
)SmbiosTable
->TableAddress
,
763 SmbiosTable
->TableLength
,
765 (UINT8
*)&HandoffTables
769 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_SERVER
) {
772 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
774 Status
= GetProcessorsCpuLocation(&ProcessorLocBuf
, &ProcessorNum
);
776 if (!EFI_ERROR(Status
)){
777 TcgEvent
.PCRIndex
= 1;
778 TcgEvent
.EventType
= EV_TABLE_OF_DEVICES
;
779 TcgEvent
.EventSize
= sizeof (HandoffTables
);
781 HandoffTables
.NumberOfTables
= 1;
782 HandoffTables
.TableEntry
[0].VendorGuid
= gEfiMpServiceProtocolGuid
;
783 HandoffTables
.TableEntry
[0].VendorTable
= ProcessorLocBuf
;
785 Status
= TcgDxeHashLogExtendEventI (
787 (UINT8
*)(UINTN
)ProcessorLocBuf
,
788 sizeof(EFI_CPU_PHYSICAL_LOCATION
) * ProcessorNum
,
790 (UINT8
*)&HandoffTables
793 FreePool(ProcessorLocBuf
);
801 Measure and log Separator event, and extend the measurement result into a specific PCR.
803 @param[in] PCRIndex PCR index.
805 @retval EFI_SUCCESS Operation completed successfully.
806 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
811 MeasureSeparatorEvent (
812 IN TPM_PCRINDEX PCRIndex
815 TCG_PCR_EVENT_HDR TcgEvent
;
819 TcgEvent
.PCRIndex
= PCRIndex
;
820 TcgEvent
.EventType
= EV_SEPARATOR
;
821 TcgEvent
.EventSize
= (UINT32
)sizeof (EventData
);
822 return TcgDxeHashLogExtendEventI (
832 Read an EFI Variable.
834 This function allocates a buffer to return the contents of the variable. The caller is
835 responsible for freeing the buffer.
837 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
838 @param[in] VendorGuid A unique identifier for the vendor.
839 @param[out] VarSize The size of the variable data.
841 @return A pointer to the buffer to return the contents of the variable.Otherwise NULL.
848 IN EFI_GUID
*VendorGuid
,
856 Status
= gRT
->GetVariable (
863 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
867 VarData
= AllocatePool (*VarSize
);
868 if (VarData
!= NULL
) {
869 Status
= gRT
->GetVariable (
876 if (EFI_ERROR (Status
)) {
886 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
888 @param[in] PCRIndex PCR Index.
889 @param[in] EventType Event type.
890 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
891 @param[in] VendorGuid A unique identifier for the vendor.
892 @param[in] VarData The content of the variable data.
893 @param[in] VarSize The size of the variable data.
895 @retval EFI_SUCCESS Operation completed successfully.
896 @retval EFI_OUT_OF_RESOURCES Out of memory.
897 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
903 IN TPM_PCRINDEX PCRIndex
,
904 IN TCG_EVENTTYPE EventType
,
906 IN EFI_GUID
*VendorGuid
,
912 TCG_PCR_EVENT_HDR TcgEvent
;
914 EFI_VARIABLE_DATA
*VarLog
;
916 VarNameLength
= StrLen (VarName
);
917 TcgEvent
.PCRIndex
= PCRIndex
;
918 TcgEvent
.EventType
= EventType
;
919 TcgEvent
.EventSize
= (UINT32
)(sizeof (*VarLog
) + VarNameLength
* sizeof (*VarName
) + VarSize
920 - sizeof (VarLog
->UnicodeName
) - sizeof (VarLog
->VariableData
));
922 VarLog
= (EFI_VARIABLE_DATA
*)AllocatePool (TcgEvent
.EventSize
);
923 if (VarLog
== NULL
) {
924 return EFI_OUT_OF_RESOURCES
;
927 VarLog
->VariableName
= *VendorGuid
;
928 VarLog
->UnicodeNameLength
= VarNameLength
;
929 VarLog
->VariableDataLength
= VarSize
;
933 VarNameLength
* sizeof (*VarName
)
936 (CHAR16
*)VarLog
->UnicodeName
+ VarNameLength
,
941 Status
= TcgDxeHashLogExtendEventI (
953 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
955 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
956 @param[in] VendorGuid A unique identifier for the vendor.
957 @param[out] VarSize The size of the variable data.
958 @param[out] VarData Pointer to the content of the variable.
960 @retval EFI_SUCCESS Operation completed successfully.
961 @retval EFI_OUT_OF_RESOURCES Out of memory.
962 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
967 ReadAndMeasureBootVariable (
969 IN EFI_GUID
*VendorGuid
,
976 *VarData
= ReadVariable (VarName
, VendorGuid
, VarSize
);
977 if (*VarData
== NULL
) {
978 return EFI_NOT_FOUND
;
981 Status
= MeasureVariable (
983 EV_EFI_VARIABLE_BOOT
,
993 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
995 The EFI boot variables are BootOrder and Boot#### variables.
997 @retval EFI_SUCCESS Operation completed successfully.
998 @retval EFI_OUT_OF_RESOURCES Out of memory.
999 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1004 MeasureAllBootVariables (
1015 Status
= ReadAndMeasureBootVariable (
1017 &gEfiGlobalVariableGuid
,
1019 (VOID
**) &BootOrder
1021 if (Status
== EFI_NOT_FOUND
) {
1024 ASSERT (BootOrder
!= NULL
);
1026 if (EFI_ERROR (Status
)) {
1027 FreePool (BootOrder
);
1031 BootCount
/= sizeof (*BootOrder
);
1032 for (Index
= 0; Index
< BootCount
; Index
++) {
1033 UnicodeSPrint (mBootVarName
, sizeof (mBootVarName
), L
"Boot%04x", BootOrder
[Index
]);
1034 Status
= ReadAndMeasureBootVariable (
1036 &gEfiGlobalVariableGuid
,
1040 if (!EFI_ERROR (Status
)) {
1041 FreePool (BootVarData
);
1045 FreePool (BootOrder
);
1050 Ready to Boot Event notification handler.
1052 Sequence of OS boot events is measured in this event notification handler.
1054 @param[in] Event Event whose notification function is being invoked
1055 @param[in] Context Pointer to the notification function's context
1066 TPM_PCRINDEX PcrIndex
;
1068 if (mBootAttempts
== 0) {
1071 // Measure handoff tables.
1073 Status
= MeasureHandoffTables ();
1074 if (EFI_ERROR (Status
)) {
1075 DEBUG ((EFI_D_ERROR
, "HOBs not Measured. Error!\n"));
1079 // Measure BootOrder & Boot#### variables.
1081 Status
= MeasureAllBootVariables ();
1082 if (EFI_ERROR (Status
)) {
1083 DEBUG ((EFI_D_ERROR
, "Boot Variables not Measured. Error!\n"));
1087 // 1. This is the first boot attempt.
1089 Status
= TcgMeasureAction (
1090 EFI_CALLING_EFI_APPLICATION
1092 ASSERT_EFI_ERROR (Status
);
1095 // 2. Draw a line between pre-boot env and entering post-boot env.
1097 for (PcrIndex
= 0; PcrIndex
< 8; PcrIndex
++) {
1098 Status
= MeasureSeparatorEvent (PcrIndex
);
1099 ASSERT_EFI_ERROR (Status
);
1103 // 3. Measure GPT. It would be done in SAP driver.
1107 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
1111 // 5. Read & Measure variable. BootOrder already measured.
1115 // 6. Not first attempt, meaning a return from last attempt
1117 Status
= TcgMeasureAction (
1118 EFI_RETURNING_FROM_EFI_APPLICATOIN
1120 ASSERT_EFI_ERROR (Status
);
1123 DEBUG ((EFI_D_INFO
, "TPM TcgDxe Measure Data when ReadyToBoot\n"));
1125 // Increase boot attempt counter.
1131 Install TCG ACPI Table when ACPI Table Protocol is available.
1133 A system's firmware uses an ACPI table to identify the system's TCG capabilities
1134 to the Post-Boot environment. The information in this ACPI table is not guaranteed
1135 to be valid until the Host Platform transitions from pre-boot state to post-boot state.
1137 @param[in] Event Event whose notification function is being invoked
1138 @param[in] Context Pointer to the notification function's context
1149 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
1153 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**)&AcpiTable
);
1154 if (EFI_ERROR (Status
)) {
1158 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
1159 CopyMem (mTcgClientAcpiTemplate
.Header
.OemId
, PcdGetPtr (PcdAcpiDefaultOemId
), sizeof (mTcgClientAcpiTemplate
.Header
.OemId
));
1160 OemTableId
= PcdGet64 (PcdAcpiDefaultOemTableId
);
1161 CopyMem (&mTcgClientAcpiTemplate
.Header
.OemTableId
, &OemTableId
, sizeof (UINT64
));
1162 mTcgClientAcpiTemplate
.Header
.OemRevision
= PcdGet32 (PcdAcpiDefaultOemRevision
);
1163 mTcgClientAcpiTemplate
.Header
.CreatorId
= PcdGet32 (PcdAcpiDefaultCreatorId
);
1164 mTcgClientAcpiTemplate
.Header
.CreatorRevision
= PcdGet32 (PcdAcpiDefaultCreatorRevision
);
1166 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1167 // service of the ACPI table protocol to install it.
1169 Checksum
= CalculateCheckSum8 ((UINT8
*)&mTcgClientAcpiTemplate
, sizeof (mTcgClientAcpiTemplate
));
1170 mTcgClientAcpiTemplate
.Header
.Checksum
= Checksum
;
1172 Status
= AcpiTable
->InstallAcpiTable (
1174 &mTcgClientAcpiTemplate
,
1175 sizeof (mTcgClientAcpiTemplate
),
1179 CopyMem (mTcgServerAcpiTemplate
.Header
.OemId
, PcdGetPtr (PcdAcpiDefaultOemId
), sizeof (mTcgServerAcpiTemplate
.Header
.OemId
));
1180 OemTableId
= PcdGet64 (PcdAcpiDefaultOemTableId
);
1181 CopyMem (&mTcgServerAcpiTemplate
.Header
.OemTableId
, &OemTableId
, sizeof (UINT64
));
1182 mTcgServerAcpiTemplate
.Header
.OemRevision
= PcdGet32 (PcdAcpiDefaultOemRevision
);
1183 mTcgServerAcpiTemplate
.Header
.CreatorId
= PcdGet32 (PcdAcpiDefaultCreatorId
);
1184 mTcgServerAcpiTemplate
.Header
.CreatorRevision
= PcdGet32 (PcdAcpiDefaultCreatorRevision
);
1186 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1187 // service of the ACPI table protocol to install it.
1189 Checksum
= CalculateCheckSum8 ((UINT8
*)&mTcgServerAcpiTemplate
, sizeof (mTcgServerAcpiTemplate
));
1190 mTcgServerAcpiTemplate
.Header
.Checksum
= Checksum
;
1192 Status
= AcpiTable
->InstallAcpiTable (
1194 &mTcgServerAcpiTemplate
,
1195 sizeof (mTcgServerAcpiTemplate
),
1199 ASSERT_EFI_ERROR (Status
);
1203 Exit Boot Services Event notification handler.
1205 Measure invocation and success of ExitBootServices.
1207 @param[in] Event Event whose notification function is being invoked
1208 @param[in] Context Pointer to the notification function's context
1213 OnExitBootServices (
1221 // Measure invocation of ExitBootServices,
1223 Status
= TcgMeasureAction (
1224 EFI_EXIT_BOOT_SERVICES_INVOCATION
1226 ASSERT_EFI_ERROR (Status
);
1229 // Measure success of ExitBootServices
1231 Status
= TcgMeasureAction (
1232 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
1234 ASSERT_EFI_ERROR (Status
);
1238 Exit Boot Services Failed Event notification handler.
1240 Measure Failure of ExitBootServices.
1242 @param[in] Event Event whose notification function is being invoked
1243 @param[in] Context Pointer to the notification function's context
1248 OnExitBootServicesFailed (
1256 // Measure Failure of ExitBootServices,
1258 Status
= TcgMeasureAction (
1259 EFI_EXIT_BOOT_SERVICES_FAILED
1261 ASSERT_EFI_ERROR (Status
);
1266 Get TPM Deactivated state.
1268 @param[out] TPMDeactivatedFlag Returns TPM Deactivated state.
1270 @retval EFI_SUCCESS Operation completed successfully.
1271 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1276 OUT BOOLEAN
*TPMDeactivatedFlag
1280 TPM_STCLEAR_FLAGS VFlags
;
1282 Status
= TpmCommGetFlags (
1283 mTcgDxeData
.TpmHandle
,
1284 TPM_CAP_FLAG_VOLATILE
,
1288 if (!EFI_ERROR (Status
)) {
1289 *TPMDeactivatedFlag
= VFlags
.deactivated
;
1296 The driver's entry point.
1298 It publishes EFI TCG Protocol.
1300 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1301 @param[in] SystemTable A pointer to the EFI System Table.
1303 @retval EFI_SUCCESS The entry point is executed successfully.
1304 @retval other Some error occurs when executing this entry point.
1310 IN EFI_HANDLE ImageHandle
,
1311 IN EFI_SYSTEM_TABLE
*SystemTable
1318 if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &gEfiTpmDeviceInstanceTpm12Guid
)){
1319 DEBUG ((EFI_D_ERROR
, "No TPM12 instance required!\n"));
1320 return EFI_UNSUPPORTED
;
1323 mTcgDxeData
.TpmHandle
= (TIS_TPM_HANDLE
)(UINTN
)TPM_BASE_ADDRESS
;
1324 Status
= TisPcRequestUseTpm (mTcgDxeData
.TpmHandle
);
1325 if (EFI_ERROR (Status
)) {
1326 DEBUG ((EFI_D_ERROR
, "TPM not detected!\n"));
1330 Status
= GetTpmStatus (&mTcgDxeData
.BsCap
.TPMDeactivatedFlag
);
1331 if (EFI_ERROR (Status
)) {
1334 "Line %d in file " __FILE__
":\n "
1335 "DriverEntry: TPM not working properly\n",
1341 Status
= gBS
->InstallProtocolInterface (
1343 &gEfiTcgProtocolGuid
,
1344 EFI_NATIVE_INTERFACE
,
1345 &mTcgDxeData
.TcgProtocol
1347 if (!EFI_ERROR (Status
) && !mTcgDxeData
.BsCap
.TPMDeactivatedFlag
) {
1349 // Setup the log area and copy event log from hob list to it
1351 Status
= SetupEventLog ();
1352 ASSERT_EFI_ERROR (Status
);
1355 // Measure handoff tables, Boot#### variables etc.
1357 Status
= EfiCreateEventReadyToBootEx (
1364 Status
= gBS
->CreateEventEx (
1369 &gEfiEventExitBootServicesGuid
,
1374 // Measure Exit Boot Service failed
1376 Status
= gBS
->CreateEventEx (
1379 OnExitBootServicesFailed
,
1381 &gEventExitBootServicesFailedGuid
,
1387 // Install ACPI Table
1389 EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid
, TPL_CALLBACK
, InstallAcpiTable
, NULL
, &Registration
);