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 - 2015, 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/TcpaAcpi.h>
28 #include <Guid/GlobalVariable.h>
29 #include <Guid/HobList.h>
30 #include <Guid/TcgEventHob.h>
31 #include <Guid/EventGroup.h>
32 #include <Guid/EventExitBootServiceFailed.h>
33 #include <Guid/TpmInstance.h>
35 #include <Protocol/DevicePath.h>
36 #include <Protocol/TcgService.h>
37 #include <Protocol/AcpiTable.h>
38 #include <Protocol/MpService.h>
40 #include <Library/DebugLib.h>
41 #include <Library/BaseMemoryLib.h>
42 #include <Library/UefiRuntimeServicesTableLib.h>
43 #include <Library/UefiDriverEntryPoint.h>
44 #include <Library/HobLib.h>
45 #include <Library/UefiBootServicesTableLib.h>
46 #include <Library/BaseLib.h>
47 #include <Library/MemoryAllocationLib.h>
48 #include <Library/PrintLib.h>
49 #include <Library/TpmCommLib.h>
50 #include <Library/PcdLib.h>
51 #include <Library/UefiLib.h>
52 #include <Library/ReportStatusCodeLib.h>
56 #define EFI_TCG_LOG_AREA_SIZE 0x10000
58 #define TCG_DXE_DATA_FROM_THIS(this) \
59 BASE_CR (this, TCG_DXE_DATA, TcgProtocol)
61 typedef struct _TCG_DXE_DATA
{
62 EFI_TCG_PROTOCOL TcgProtocol
;
63 TCG_EFI_BOOT_SERVICE_CAPABILITY BsCap
;
64 EFI_TCG_CLIENT_ACPI_TABLE
*TcgClientAcpiTable
;
65 EFI_TCG_SERVER_ACPI_TABLE
*TcgServerAcpiTable
;
68 TIS_TPM_HANDLE TpmHandle
;
73 EFI_TCG_CLIENT_ACPI_TABLE mTcgClientAcpiTemplate
= {
75 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE
,
76 sizeof (mTcgClientAcpiTemplate
),
79 // Compiler initializes the remaining bytes to 0
80 // These fields should be filled in in production
83 0, // 0 for PC Client Platform Class
84 0, // Log Area Max Length
85 (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1) // Log Area Start Address
89 // The following EFI_TCG_SERVER_ACPI_TABLE default setting is just one example,
90 // the TPM device connectes to LPC, and also defined the ACPI _UID as 0xFF,
91 // this _UID can be changed and should match with the _UID setting of the TPM
94 EFI_TCG_SERVER_ACPI_TABLE mTcgServerAcpiTemplate
= {
96 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE
,
97 sizeof (mTcgServerAcpiTemplate
),
100 // Compiler initializes the remaining bytes to 0
101 // These fields should be filled in in production
104 1, // 1 for Server Platform Class
106 0, // Log Area Max Length
107 (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1), // Log Area Start Address
108 0x0100, // TCG Specification revision 1.0
110 0, // Interrupt Flags
112 {0}, // Reserved 3 bytes
113 0, // Global System Interrupt
115 EFI_ACPI_3_0_SYSTEM_MEMORY
,
119 TPM_BASE_ADDRESS
// Base Address
122 {0}, // Configuration Address
123 0xFF, // ACPI _UID value of the device, can be changed for different platforms
124 0, // 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
129 UINTN mBootAttempts
= 0;
130 CHAR16 mBootVarName
[] = L
"BootOrder";
133 Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function
134 Caller is responsible to free LocationBuf.
136 @param[out] LocationBuf Returns Processor Location Buffer.
137 @param[out] Num Returns processor number.
139 @retval EFI_SUCCESS Operation completed successfully.
140 @retval EFI_UNSUPPORTED MpService protocol not found.
144 GetProcessorsCpuLocation (
145 OUT EFI_CPU_PHYSICAL_LOCATION
**LocationBuf
,
150 EFI_MP_SERVICES_PROTOCOL
*MpProtocol
;
152 UINTN EnabledProcessorNum
;
153 EFI_PROCESSOR_INFORMATION ProcessorInfo
;
154 EFI_CPU_PHYSICAL_LOCATION
*ProcessorLocBuf
;
157 Status
= gBS
->LocateProtocol (&gEfiMpServiceProtocolGuid
, NULL
, (VOID
**) &MpProtocol
);
158 if (EFI_ERROR (Status
)) {
160 // MP protocol is not installed
162 return EFI_UNSUPPORTED
;
165 Status
= MpProtocol
->GetNumberOfProcessors(
170 if (EFI_ERROR(Status
)){
174 Status
= gBS
->AllocatePool(
176 sizeof(EFI_CPU_PHYSICAL_LOCATION
) * ProcessorNum
,
177 (VOID
**) &ProcessorLocBuf
179 if (EFI_ERROR(Status
)){
184 // Get each processor Location info
186 for (Index
= 0; Index
< ProcessorNum
; Index
++) {
187 Status
= MpProtocol
->GetProcessorInfo(
192 if (EFI_ERROR(Status
)){
193 FreePool(ProcessorLocBuf
);
198 // Get all Processor Location info & measure
201 &ProcessorLocBuf
[Index
],
202 &ProcessorInfo
.Location
,
203 sizeof(EFI_CPU_PHYSICAL_LOCATION
)
207 *LocationBuf
= ProcessorLocBuf
;
214 This service provides EFI protocol capability information, state information
215 about the TPM, and Event Log state information.
217 @param[in] This Indicates the calling context
218 @param[out] ProtocolCapability The callee allocates memory for a TCG_BOOT_SERVICE_CAPABILITY
219 structure and fills in the fields with the EFI protocol
220 capability information and the current TPM state information.
221 @param[out] TCGFeatureFlags This is a pointer to the feature flags. No feature
222 flags are currently defined so this parameter
223 MUST be set to 0. However, in the future,
224 feature flags may be defined that, for example,
225 enable hash algorithm agility.
226 @param[out] EventLogLocation This is a pointer to the address of the event log in memory.
227 @param[out] EventLogLastEntry If the Event Log contains more than one entry,
228 this is a pointer to the address of the start of
229 the last entry in the event log in memory.
231 @retval EFI_SUCCESS Operation completed successfully.
232 @retval EFI_INVALID_PARAMETER ProtocolCapability does not match TCG capability.
238 IN EFI_TCG_PROTOCOL
*This
,
239 OUT TCG_EFI_BOOT_SERVICE_CAPABILITY
*ProtocolCapability
,
240 OUT UINT32
*TCGFeatureFlags
,
241 OUT EFI_PHYSICAL_ADDRESS
*EventLogLocation
,
242 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
245 TCG_DXE_DATA
*TcgData
;
247 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
249 if (ProtocolCapability
!= NULL
) {
250 *ProtocolCapability
= TcgData
->BsCap
;
253 if (TCGFeatureFlags
!= NULL
) {
254 *TCGFeatureFlags
= 0;
257 if (EventLogLocation
!= NULL
) {
258 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
259 *EventLogLocation
= TcgData
->TcgClientAcpiTable
->Lasa
;
261 *EventLogLocation
= TcgData
->TcgServerAcpiTable
->Lasa
;
265 if (EventLogLastEntry
!= NULL
) {
266 if (TcgData
->BsCap
.TPMDeactivatedFlag
|| (!TcgData
->BsCap
.TPMPresentFlag
)) {
267 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)0;
269 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)TcgData
->LastEvent
;
277 This service abstracts the capability to do a hash operation on a data buffer.
279 @param[in] This Indicates the calling context
280 @param[in] HashData Pointer to the data buffer to be hashed
281 @param[in] HashDataLen Length of the data buffer to be hashed
282 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
283 @param[in, out] HashedDataLen Resultant length of the hashed data
284 @param[in, out] HashedDataResult Resultant buffer of the hashed data
286 @retval EFI_SUCCESS Operation completed successfully.
287 @retval EFI_INVALID_PARAMETER HashDataLen is NULL.
288 @retval EFI_INVALID_PARAMETER HashDataLenResult is NULL.
289 @retval EFI_OUT_OF_RESOURCES Cannot allocate buffer of size *HashedDataLen.
290 @retval EFI_UNSUPPORTED AlgorithmId not supported.
291 @retval EFI_BUFFER_TOO_SMALL *HashedDataLen < sizeof (TCG_DIGEST).
297 IN EFI_TCG_PROTOCOL
*This
,
299 IN UINT64 HashDataLen
,
300 IN TCG_ALGORITHM_ID AlgorithmId
,
301 IN OUT UINT64
*HashedDataLen
,
302 IN OUT UINT8
**HashedDataResult
305 if (HashedDataLen
== NULL
|| HashedDataResult
== NULL
) {
306 return EFI_INVALID_PARAMETER
;
309 switch (AlgorithmId
) {
311 if (*HashedDataLen
== 0) {
312 *HashedDataLen
= sizeof (TPM_DIGEST
);
313 *HashedDataResult
= AllocatePool ((UINTN
) *HashedDataLen
);
314 if (*HashedDataResult
== NULL
) {
315 return EFI_OUT_OF_RESOURCES
;
319 if (*HashedDataLen
< sizeof (TPM_DIGEST
)) {
320 *HashedDataLen
= sizeof (TPM_DIGEST
);
321 return EFI_BUFFER_TOO_SMALL
;
323 *HashedDataLen
= sizeof (TPM_DIGEST
);
325 if (*HashedDataResult
== NULL
) {
326 *HashedDataResult
= AllocatePool ((UINTN
) *HashedDataLen
);
329 return TpmCommHashAll (
332 (TPM_DIGEST
*)*HashedDataResult
335 return EFI_UNSUPPORTED
;
340 Add a new entry to the Event Log.
342 @param[in] TcgData TCG_DXE_DATA structure.
343 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
344 @param[in] NewEventData Pointer to the new event data.
346 @retval EFI_SUCCESS The new event log entry was added.
347 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
353 IN TCG_DXE_DATA
*TcgData
,
354 IN TCG_PCR_EVENT_HDR
*NewEventHdr
,
355 IN UINT8
*NewEventData
358 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
359 TcgData
->LastEvent
= (UINT8
*)(UINTN
)TcgData
->TcgClientAcpiTable
->Lasa
;
360 return TpmCommLogEvent (
362 &TcgData
->EventLogSize
,
363 (UINTN
)TcgData
->TcgClientAcpiTable
->Laml
,
368 TcgData
->LastEvent
= (UINT8
*)(UINTN
)TcgData
->TcgServerAcpiTable
->Lasa
;
369 return TpmCommLogEvent (
371 &TcgData
->EventLogSize
,
372 (UINTN
)TcgData
->TcgServerAcpiTable
->Laml
,
380 This service abstracts the capability to add an entry to the Event Log.
382 @param[in] This Indicates the calling context
383 @param[in] TCGLogData Pointer to the start of the data buffer containing
384 the TCG_PCR_EVENT data structure. All fields in
385 this structure are properly filled by the caller.
386 @param[in, out] EventNumber The event number of the event just logged
387 @param[in] Flags Indicate additional flags. Only one flag has been
388 defined at this time, which is 0x01 and means the
389 extend operation should not be performed. All
390 other bits are reserved.
392 @retval EFI_SUCCESS Operation completed successfully.
393 @retval EFI_OUT_OF_RESOURCES Insufficient memory in the event log to complete this action.
399 IN EFI_TCG_PROTOCOL
*This
,
400 IN TCG_PCR_EVENT
*TCGLogData
,
401 IN OUT UINT32
*EventNumber
,
405 TCG_DXE_DATA
*TcgData
;
407 if (TCGLogData
== NULL
){
408 return EFI_INVALID_PARAMETER
;
411 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
413 if (TcgData
->BsCap
.TPMDeactivatedFlag
|| (!TcgData
->BsCap
.TPMPresentFlag
)) {
414 return EFI_DEVICE_ERROR
;
416 return TcgDxeLogEventI (
418 (TCG_PCR_EVENT_HDR
*)TCGLogData
,
424 This service is a proxy for commands to the TPM.
426 @param[in] This Indicates the calling context
427 @param[in] TpmInputParameterBlockSize Size of the TPM input parameter block
428 @param[in] TpmInputParameterBlock Pointer to the TPM input parameter block
429 @param[in] TpmOutputParameterBlockSize Size of the TPM output parameter block
430 @param[in] TpmOutputParameterBlock Pointer to the TPM output parameter block
432 @retval EFI_SUCCESS Operation completed successfully.
433 @retval EFI_INVALID_PARAMETER Invalid ordinal.
434 @retval EFI_UNSUPPORTED Current Task Priority Level >= EFI_TPL_CALLBACK.
435 @retval EFI_TIMEOUT The TIS timed-out.
440 TcgDxePassThroughToTpm (
441 IN EFI_TCG_PROTOCOL
*This
,
442 IN UINT32 TpmInputParameterBlockSize
,
443 IN UINT8
*TpmInputParameterBlock
,
444 IN UINT32 TpmOutputParameterBlockSize
,
445 IN UINT8
*TpmOutputParameterBlock
448 TCG_DXE_DATA
*TcgData
;
450 if (TpmInputParameterBlock
== NULL
||
451 TpmOutputParameterBlock
== NULL
||
452 TpmInputParameterBlockSize
== 0 ||
453 TpmOutputParameterBlockSize
== 0) {
454 return EFI_INVALID_PARAMETER
;
457 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
459 return TisPcExecute (
462 TpmInputParameterBlock
,
463 (UINTN
) TpmInputParameterBlockSize
,
464 TpmOutputParameterBlock
,
465 (UINTN
) TpmOutputParameterBlockSize
470 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
471 and add an entry to the Event Log.
473 @param[in] TcgData TCG_DXE_DATA structure.
474 @param[in] HashData Physical address of the start of the data buffer
475 to be hashed, extended, and logged.
476 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
477 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
478 @param[in] NewEventData Pointer to the new event data.
480 @retval EFI_SUCCESS Operation completed successfully.
481 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
482 @retval EFI_DEVICE_ERROR The command was unsuccessful.
487 TcgDxeHashLogExtendEventI (
488 IN TCG_DXE_DATA
*TcgData
,
490 IN UINT64 HashDataLen
,
491 IN OUT TCG_PCR_EVENT_HDR
*NewEventHdr
,
492 IN UINT8
*NewEventData
497 if (!TcgData
->BsCap
.TPMPresentFlag
) {
498 return EFI_DEVICE_ERROR
;
501 if (HashDataLen
> 0 || HashData
!= NULL
) {
502 Status
= TpmCommHashAll (
507 if (EFI_ERROR(Status
)) {
508 DEBUG ((DEBUG_ERROR
, "TpmCommHashAll Failed. %x\n", Status
));
513 Status
= TpmCommExtend (
515 &NewEventHdr
->Digest
,
516 NewEventHdr
->PCRIndex
,
519 if (!EFI_ERROR (Status
)) {
520 Status
= TcgDxeLogEventI (TcgData
, NewEventHdr
, NewEventData
);
524 if ((Status
== EFI_DEVICE_ERROR
) || (Status
== EFI_TIMEOUT
)) {
525 DEBUG ((EFI_D_ERROR
, "TcgDxeHashLogExtendEventI - %r. Disable TPM.\n", Status
));
526 TcgData
->BsCap
.TPMPresentFlag
= FALSE
;
528 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
529 (PcdGet32 (PcdStatusCodeSubClassTpmDevice
) | EFI_P_EC_INTERFACE_ERROR
)
531 Status
= EFI_DEVICE_ERROR
;
538 This service abstracts the capability to do a hash operation on a data buffer,
539 extend a specific TPM PCR with the hash result, and add an entry to the Event Log
541 @param[in] This Indicates the calling context
542 @param[in] HashData Physical address of the start of the data buffer
543 to be hashed, extended, and logged.
544 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
545 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
546 @param[in, out] TCGLogData The physical address of the start of the data
547 buffer containing the TCG_PCR_EVENT data structure.
548 @param[in, out] EventNumber The event number of the event just logged.
549 @param[out] EventLogLastEntry Physical address of the first byte of the entry
550 just placed in the Event Log. If the Event Log was
551 empty when this function was called then this physical
552 address will be the same as the physical address of
553 the start of the Event Log.
555 @retval EFI_SUCCESS Operation completed successfully.
556 @retval EFI_UNSUPPORTED AlgorithmId != TPM_ALG_SHA.
557 @retval EFI_UNSUPPORTED Current TPL >= EFI_TPL_CALLBACK.
558 @retval EFI_DEVICE_ERROR The command was unsuccessful.
563 TcgDxeHashLogExtendEvent (
564 IN EFI_TCG_PROTOCOL
*This
,
565 IN EFI_PHYSICAL_ADDRESS HashData
,
566 IN UINT64 HashDataLen
,
567 IN TPM_ALGORITHM_ID AlgorithmId
,
568 IN OUT TCG_PCR_EVENT
*TCGLogData
,
569 IN OUT UINT32
*EventNumber
,
570 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
573 TCG_DXE_DATA
*TcgData
;
576 if (TCGLogData
== NULL
|| EventLogLastEntry
== NULL
){
577 return EFI_INVALID_PARAMETER
;
580 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
582 if (TcgData
->BsCap
.TPMDeactivatedFlag
|| (!TcgData
->BsCap
.TPMPresentFlag
)) {
583 return EFI_DEVICE_ERROR
;
586 if (AlgorithmId
!= TPM_ALG_SHA
) {
587 return EFI_UNSUPPORTED
;
590 if (HashData
== 0 && HashDataLen
> 0) {
591 return EFI_INVALID_PARAMETER
;
594 Status
= TcgDxeHashLogExtendEventI (
596 (UINT8
*) (UINTN
) HashData
,
598 (TCG_PCR_EVENT_HDR
*)TCGLogData
,
602 if (!EFI_ERROR(Status
)){
603 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) TcgData
->LastEvent
;
609 TCG_DXE_DATA mTcgDxeData
= {
614 TcgDxePassThroughToTpm
,
615 TcgDxeHashLogExtendEvent
618 sizeof (mTcgDxeData
.BsCap
),
625 &mTcgClientAcpiTemplate
,
626 &mTcgServerAcpiTemplate
,
633 Initialize the Event Log and log events passed from the PEI phase.
635 @retval EFI_SUCCESS Operation completed successfully.
636 @retval EFI_OUT_OF_RESOURCES Out of memory.
646 TCG_PCR_EVENT
*TcgEvent
;
647 EFI_PEI_HOB_POINTERS GuidHob
;
648 EFI_PHYSICAL_ADDRESS Lasa
;
650 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
651 Lasa
= mTcgClientAcpiTemplate
.Lasa
;
653 Status
= gBS
->AllocatePages (
656 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE
),
659 if (EFI_ERROR (Status
)) {
662 mTcgClientAcpiTemplate
.Lasa
= Lasa
;
664 // To initialize them as 0xFF is recommended
665 // because the OS can know the last entry for that.
667 SetMem ((VOID
*)(UINTN
)mTcgClientAcpiTemplate
.Lasa
, EFI_TCG_LOG_AREA_SIZE
, 0xFF);
668 mTcgClientAcpiTemplate
.Laml
= EFI_TCG_LOG_AREA_SIZE
;
671 Lasa
= mTcgServerAcpiTemplate
.Lasa
;
673 Status
= gBS
->AllocatePages (
676 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE
),
679 if (EFI_ERROR (Status
)) {
682 mTcgServerAcpiTemplate
.Lasa
= Lasa
;
684 // To initialize them as 0xFF is recommended
685 // because the OS can know the last entry for that.
687 SetMem ((VOID
*)(UINTN
)mTcgServerAcpiTemplate
.Lasa
, EFI_TCG_LOG_AREA_SIZE
, 0xFF);
688 mTcgServerAcpiTemplate
.Laml
= EFI_TCG_LOG_AREA_SIZE
;
691 GuidHob
.Raw
= GetHobList ();
692 while (!EFI_ERROR (Status
) &&
693 (GuidHob
.Raw
= GetNextGuidHob (&gTcgEventEntryHobGuid
, GuidHob
.Raw
)) != NULL
) {
694 TcgEvent
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
695 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
696 Status
= TcgDxeLogEventI (
698 (TCG_PCR_EVENT_HDR
*)TcgEvent
,
707 Measure and log an action string, and extend the measurement result into PCR[5].
709 @param[in] String A specific string that indicates an Action event.
711 @retval EFI_SUCCESS Operation completed successfully.
712 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
721 TCG_PCR_EVENT_HDR TcgEvent
;
723 TcgEvent
.PCRIndex
= 5;
724 TcgEvent
.EventType
= EV_EFI_ACTION
;
725 TcgEvent
.EventSize
= (UINT32
)AsciiStrLen (String
);
726 return TcgDxeHashLogExtendEventI (
736 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
738 @retval EFI_SUCCESS Operation completed successfully.
739 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
744 MeasureHandoffTables (
749 TCG_PCR_EVENT_HDR TcgEvent
;
750 EFI_HANDOFF_TABLE_POINTERS HandoffTables
;
752 EFI_CPU_PHYSICAL_LOCATION
*ProcessorLocBuf
;
754 ProcessorLocBuf
= NULL
;
755 Status
= EFI_SUCCESS
;
757 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_SERVER
) {
760 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
762 Status
= GetProcessorsCpuLocation(&ProcessorLocBuf
, &ProcessorNum
);
764 if (!EFI_ERROR(Status
)){
765 TcgEvent
.PCRIndex
= 1;
766 TcgEvent
.EventType
= EV_TABLE_OF_DEVICES
;
767 TcgEvent
.EventSize
= sizeof (HandoffTables
);
769 HandoffTables
.NumberOfTables
= 1;
770 HandoffTables
.TableEntry
[0].VendorGuid
= gEfiMpServiceProtocolGuid
;
771 HandoffTables
.TableEntry
[0].VendorTable
= ProcessorLocBuf
;
773 Status
= TcgDxeHashLogExtendEventI (
775 (UINT8
*)(UINTN
)ProcessorLocBuf
,
776 sizeof(EFI_CPU_PHYSICAL_LOCATION
) * ProcessorNum
,
778 (UINT8
*)&HandoffTables
781 FreePool(ProcessorLocBuf
);
789 Measure and log Separator event, and extend the measurement result into a specific PCR.
791 @param[in] PCRIndex PCR index.
793 @retval EFI_SUCCESS Operation completed successfully.
794 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
799 MeasureSeparatorEvent (
800 IN TPM_PCRINDEX PCRIndex
803 TCG_PCR_EVENT_HDR TcgEvent
;
807 TcgEvent
.PCRIndex
= PCRIndex
;
808 TcgEvent
.EventType
= EV_SEPARATOR
;
809 TcgEvent
.EventSize
= (UINT32
)sizeof (EventData
);
810 return TcgDxeHashLogExtendEventI (
820 Read an EFI Variable.
822 This function allocates a buffer to return the contents of the variable. The caller is
823 responsible for freeing the buffer.
825 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
826 @param[in] VendorGuid A unique identifier for the vendor.
827 @param[out] VarSize The size of the variable data.
829 @return A pointer to the buffer to return the contents of the variable.Otherwise NULL.
836 IN EFI_GUID
*VendorGuid
,
844 Status
= gRT
->GetVariable (
851 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
855 VarData
= AllocatePool (*VarSize
);
856 if (VarData
!= NULL
) {
857 Status
= gRT
->GetVariable (
864 if (EFI_ERROR (Status
)) {
874 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
876 @param[in] PCRIndex PCR Index.
877 @param[in] EventType Event type.
878 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
879 @param[in] VendorGuid A unique identifier for the vendor.
880 @param[in] VarData The content of the variable data.
881 @param[in] VarSize The size of the variable data.
883 @retval EFI_SUCCESS Operation completed successfully.
884 @retval EFI_OUT_OF_RESOURCES Out of memory.
885 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
891 IN TPM_PCRINDEX PCRIndex
,
892 IN TCG_EVENTTYPE EventType
,
894 IN EFI_GUID
*VendorGuid
,
900 TCG_PCR_EVENT_HDR TcgEvent
;
902 EFI_VARIABLE_DATA
*VarLog
;
904 VarNameLength
= StrLen (VarName
);
905 TcgEvent
.PCRIndex
= PCRIndex
;
906 TcgEvent
.EventType
= EventType
;
907 TcgEvent
.EventSize
= (UINT32
)(sizeof (*VarLog
) + VarNameLength
* sizeof (*VarName
) + VarSize
908 - sizeof (VarLog
->UnicodeName
) - sizeof (VarLog
->VariableData
));
910 VarLog
= (EFI_VARIABLE_DATA
*)AllocatePool (TcgEvent
.EventSize
);
911 if (VarLog
== NULL
) {
912 return EFI_OUT_OF_RESOURCES
;
915 VarLog
->VariableName
= *VendorGuid
;
916 VarLog
->UnicodeNameLength
= VarNameLength
;
917 VarLog
->VariableDataLength
= VarSize
;
921 VarNameLength
* sizeof (*VarName
)
924 (CHAR16
*)VarLog
->UnicodeName
+ VarNameLength
,
929 Status
= TcgDxeHashLogExtendEventI (
941 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
943 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
944 @param[in] VendorGuid A unique identifier for the vendor.
945 @param[out] VarSize The size of the variable data.
946 @param[out] VarData Pointer to the content of the variable.
948 @retval EFI_SUCCESS Operation completed successfully.
949 @retval EFI_OUT_OF_RESOURCES Out of memory.
950 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
955 ReadAndMeasureBootVariable (
957 IN EFI_GUID
*VendorGuid
,
964 *VarData
= ReadVariable (VarName
, VendorGuid
, VarSize
);
965 if (*VarData
== NULL
) {
966 return EFI_NOT_FOUND
;
969 Status
= MeasureVariable (
971 EV_EFI_VARIABLE_BOOT
,
981 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
983 The EFI boot variables are BootOrder and Boot#### variables.
985 @retval EFI_SUCCESS Operation completed successfully.
986 @retval EFI_OUT_OF_RESOURCES Out of memory.
987 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
992 MeasureAllBootVariables (
1003 Status
= ReadAndMeasureBootVariable (
1005 &gEfiGlobalVariableGuid
,
1007 (VOID
**) &BootOrder
1009 if (Status
== EFI_NOT_FOUND
|| BootOrder
== NULL
) {
1013 if (EFI_ERROR (Status
)) {
1015 // BootOrder can't be NULL if status is not EFI_NOT_FOUND
1017 FreePool (BootOrder
);
1021 BootCount
/= sizeof (*BootOrder
);
1022 for (Index
= 0; Index
< BootCount
; Index
++) {
1023 UnicodeSPrint (mBootVarName
, sizeof (mBootVarName
), L
"Boot%04x", BootOrder
[Index
]);
1024 Status
= ReadAndMeasureBootVariable (
1026 &gEfiGlobalVariableGuid
,
1030 if (!EFI_ERROR (Status
)) {
1031 FreePool (BootVarData
);
1035 FreePool (BootOrder
);
1040 Ready to Boot Event notification handler.
1042 Sequence of OS boot events is measured in this event notification handler.
1044 @param[in] Event Event whose notification function is being invoked
1045 @param[in] Context Pointer to the notification function's context
1056 TPM_PCRINDEX PcrIndex
;
1058 if (mBootAttempts
== 0) {
1061 // Measure handoff tables.
1063 Status
= MeasureHandoffTables ();
1064 if (EFI_ERROR (Status
)) {
1065 DEBUG ((EFI_D_ERROR
, "HOBs not Measured. Error!\n"));
1069 // Measure BootOrder & Boot#### variables.
1071 Status
= MeasureAllBootVariables ();
1072 if (EFI_ERROR (Status
)) {
1073 DEBUG ((EFI_D_ERROR
, "Boot Variables not Measured. Error!\n"));
1077 // 1. This is the first boot attempt.
1079 Status
= TcgMeasureAction (
1080 EFI_CALLING_EFI_APPLICATION
1082 if (EFI_ERROR (Status
)) {
1083 DEBUG ((EFI_D_ERROR
, "%s not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION
));
1087 // 2. Draw a line between pre-boot env and entering post-boot env.
1089 for (PcrIndex
= 0; PcrIndex
< 8; PcrIndex
++) {
1090 Status
= MeasureSeparatorEvent (PcrIndex
);
1091 if (EFI_ERROR (Status
)) {
1092 DEBUG ((EFI_D_ERROR
, "Seperator Event not Measured. Error!\n"));
1097 // 3. Measure GPT. It would be done in SAP driver.
1101 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
1105 // 5. Read & Measure variable. BootOrder already measured.
1109 // 6. Not first attempt, meaning a return from last attempt
1111 Status
= TcgMeasureAction (
1112 EFI_RETURNING_FROM_EFI_APPLICATOIN
1114 if (EFI_ERROR (Status
)) {
1115 DEBUG ((EFI_D_ERROR
, "%s not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN
));
1119 DEBUG ((EFI_D_INFO
, "TPM TcgDxe Measure Data when ReadyToBoot\n"));
1121 // Increase boot attempt counter.
1127 Install TCG ACPI Table when ACPI Table Protocol is available.
1129 A system's firmware uses an ACPI table to identify the system's TCG capabilities
1130 to the Post-Boot environment. The information in this ACPI table is not guaranteed
1131 to be valid until the Host Platform transitions from pre-boot state to post-boot state.
1133 @param[in] Event Event whose notification function is being invoked
1134 @param[in] Context Pointer to the notification function's context
1145 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
1149 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**)&AcpiTable
);
1150 if (EFI_ERROR (Status
)) {
1154 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
1155 CopyMem (mTcgClientAcpiTemplate
.Header
.OemId
, PcdGetPtr (PcdAcpiDefaultOemId
), sizeof (mTcgClientAcpiTemplate
.Header
.OemId
));
1156 OemTableId
= PcdGet64 (PcdAcpiDefaultOemTableId
);
1157 CopyMem (&mTcgClientAcpiTemplate
.Header
.OemTableId
, &OemTableId
, sizeof (UINT64
));
1158 mTcgClientAcpiTemplate
.Header
.OemRevision
= PcdGet32 (PcdAcpiDefaultOemRevision
);
1159 mTcgClientAcpiTemplate
.Header
.CreatorId
= PcdGet32 (PcdAcpiDefaultCreatorId
);
1160 mTcgClientAcpiTemplate
.Header
.CreatorRevision
= PcdGet32 (PcdAcpiDefaultCreatorRevision
);
1162 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1163 // service of the ACPI table protocol to install it.
1165 Checksum
= CalculateCheckSum8 ((UINT8
*)&mTcgClientAcpiTemplate
, sizeof (mTcgClientAcpiTemplate
));
1166 mTcgClientAcpiTemplate
.Header
.Checksum
= Checksum
;
1168 Status
= AcpiTable
->InstallAcpiTable (
1170 &mTcgClientAcpiTemplate
,
1171 sizeof (mTcgClientAcpiTemplate
),
1175 CopyMem (mTcgServerAcpiTemplate
.Header
.OemId
, PcdGetPtr (PcdAcpiDefaultOemId
), sizeof (mTcgServerAcpiTemplate
.Header
.OemId
));
1176 OemTableId
= PcdGet64 (PcdAcpiDefaultOemTableId
);
1177 CopyMem (&mTcgServerAcpiTemplate
.Header
.OemTableId
, &OemTableId
, sizeof (UINT64
));
1178 mTcgServerAcpiTemplate
.Header
.OemRevision
= PcdGet32 (PcdAcpiDefaultOemRevision
);
1179 mTcgServerAcpiTemplate
.Header
.CreatorId
= PcdGet32 (PcdAcpiDefaultCreatorId
);
1180 mTcgServerAcpiTemplate
.Header
.CreatorRevision
= PcdGet32 (PcdAcpiDefaultCreatorRevision
);
1182 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1183 // service of the ACPI table protocol to install it.
1185 Checksum
= CalculateCheckSum8 ((UINT8
*)&mTcgServerAcpiTemplate
, sizeof (mTcgServerAcpiTemplate
));
1186 mTcgServerAcpiTemplate
.Header
.Checksum
= Checksum
;
1188 Status
= AcpiTable
->InstallAcpiTable (
1190 &mTcgServerAcpiTemplate
,
1191 sizeof (mTcgServerAcpiTemplate
),
1196 if (EFI_ERROR (Status
)) {
1197 DEBUG((EFI_D_ERROR
, "Tcg Acpi Table installation failure"));
1202 Exit Boot Services Event notification handler.
1204 Measure invocation and success of ExitBootServices.
1206 @param[in] Event Event whose notification function is being invoked
1207 @param[in] Context Pointer to the notification function's context
1212 OnExitBootServices (
1220 // Measure invocation of ExitBootServices,
1222 Status
= TcgMeasureAction (
1223 EFI_EXIT_BOOT_SERVICES_INVOCATION
1225 if (EFI_ERROR (Status
)) {
1226 DEBUG ((EFI_D_ERROR
, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION
));
1230 // Measure success of ExitBootServices
1232 Status
= TcgMeasureAction (
1233 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
1235 if (EFI_ERROR (Status
)){
1236 DEBUG ((EFI_D_ERROR
, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED
));
1241 Exit Boot Services Failed Event notification handler.
1243 Measure Failure of ExitBootServices.
1245 @param[in] Event Event whose notification function is being invoked
1246 @param[in] Context Pointer to the notification function's context
1251 OnExitBootServicesFailed (
1259 // Measure Failure of ExitBootServices,
1261 Status
= TcgMeasureAction (
1262 EFI_EXIT_BOOT_SERVICES_FAILED
1264 if (EFI_ERROR (Status
)){
1265 DEBUG ((EFI_D_ERROR
, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED
));
1270 Get TPM Deactivated state.
1272 @param[out] TPMDeactivatedFlag Returns TPM Deactivated state.
1274 @retval EFI_SUCCESS Operation completed successfully.
1275 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1280 OUT BOOLEAN
*TPMDeactivatedFlag
1284 TPM_STCLEAR_FLAGS VFlags
;
1286 Status
= TpmCommGetFlags (
1287 mTcgDxeData
.TpmHandle
,
1288 TPM_CAP_FLAG_VOLATILE
,
1292 if (!EFI_ERROR (Status
)) {
1293 *TPMDeactivatedFlag
= VFlags
.deactivated
;
1300 The driver's entry point.
1302 It publishes EFI TCG Protocol.
1304 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1305 @param[in] SystemTable A pointer to the EFI System Table.
1307 @retval EFI_SUCCESS The entry point is executed successfully.
1308 @retval other Some error occurs when executing this entry point.
1314 IN EFI_HANDLE ImageHandle
,
1315 IN EFI_SYSTEM_TABLE
*SystemTable
1322 if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &gEfiTpmDeviceInstanceTpm12Guid
)){
1323 DEBUG ((EFI_D_ERROR
, "No TPM12 instance required!\n"));
1324 return EFI_UNSUPPORTED
;
1327 if (GetFirstGuidHob (&gTpmErrorHobGuid
) != NULL
) {
1328 DEBUG ((EFI_D_ERROR
, "TPM error!\n"));
1329 return EFI_DEVICE_ERROR
;
1332 mTcgDxeData
.TpmHandle
= (TIS_TPM_HANDLE
)(UINTN
)TPM_BASE_ADDRESS
;
1333 Status
= TisPcRequestUseTpm (mTcgDxeData
.TpmHandle
);
1334 if (EFI_ERROR (Status
)) {
1335 DEBUG ((EFI_D_ERROR
, "TPM not detected!\n"));
1339 Status
= GetTpmStatus (&mTcgDxeData
.BsCap
.TPMDeactivatedFlag
);
1340 if (EFI_ERROR (Status
)) {
1343 "Line %d in file " __FILE__
":\n "
1344 "DriverEntry: TPM not working properly\n",
1350 Status
= gBS
->InstallProtocolInterface (
1352 &gEfiTcgProtocolGuid
,
1353 EFI_NATIVE_INTERFACE
,
1354 &mTcgDxeData
.TcgProtocol
1356 if (!EFI_ERROR (Status
) && (!mTcgDxeData
.BsCap
.TPMDeactivatedFlag
) && mTcgDxeData
.BsCap
.TPMPresentFlag
) {
1358 // Setup the log area and copy event log from hob list to it
1360 Status
= SetupEventLog ();
1361 ASSERT_EFI_ERROR (Status
);
1364 // Measure handoff tables, Boot#### variables etc.
1366 Status
= EfiCreateEventReadyToBootEx (
1373 Status
= gBS
->CreateEventEx (
1378 &gEfiEventExitBootServicesGuid
,
1383 // Measure Exit Boot Service failed
1385 Status
= gBS
->CreateEventEx (
1388 OnExitBootServicesFailed
,
1390 &gEventExitBootServicesFailedGuid
,
1396 // Install ACPI Table
1398 EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid
, TPL_CALLBACK
, InstallAcpiTable
, NULL
, &Registration
);