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 - 2012, 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>
28 #include <Guid/GlobalVariable.h>
29 #include <Guid/SmBios.h>
30 #include <Guid/HobList.h>
31 #include <Guid/TcgEventHob.h>
32 #include <Guid/EventGroup.h>
33 #include <Guid/EventExitBootServiceFailed.h>
34 #include <Protocol/DevicePath.h>
35 #include <Protocol/TcgService.h>
36 #include <Protocol/AcpiTable.h>
38 #include <Library/DebugLib.h>
39 #include <Library/BaseMemoryLib.h>
40 #include <Library/UefiRuntimeServicesTableLib.h>
41 #include <Library/UefiDriverEntryPoint.h>
42 #include <Library/HobLib.h>
43 #include <Library/UefiBootServicesTableLib.h>
44 #include <Library/BaseLib.h>
45 #include <Library/MemoryAllocationLib.h>
46 #include <Library/PrintLib.h>
47 #include <Library/TpmCommLib.h>
48 #include <Library/PcdLib.h>
49 #include <Library/UefiLib.h>
53 #define EFI_TCG_LOG_AREA_SIZE 0x10000
57 typedef struct _EFI_TCG_CLIENT_ACPI_TABLE
{
58 EFI_ACPI_DESCRIPTION_HEADER Header
;
61 EFI_PHYSICAL_ADDRESS Lasa
;
62 } EFI_TCG_CLIENT_ACPI_TABLE
;
64 typedef struct _EFI_TCG_SERVER_ACPI_TABLE
{
65 EFI_ACPI_DESCRIPTION_HEADER Header
;
69 EFI_PHYSICAL_ADDRESS Lasa
;
76 EFI_ACPI_3_0_GENERIC_ADDRESS_STRUCTURE BaseAddress
;
78 EFI_ACPI_3_0_GENERIC_ADDRESS_STRUCTURE ConfigAddress
;
83 } EFI_TCG_SERVER_ACPI_TABLE
;
87 #define TCG_DXE_DATA_FROM_THIS(this) \
88 BASE_CR (this, TCG_DXE_DATA, TcgProtocol)
90 typedef struct _TCG_DXE_DATA
{
91 EFI_TCG_PROTOCOL TcgProtocol
;
92 TCG_EFI_BOOT_SERVICE_CAPABILITY BsCap
;
93 EFI_TCG_CLIENT_ACPI_TABLE
*TcgClientAcpiTable
;
94 EFI_TCG_SERVER_ACPI_TABLE
*TcgServerAcpiTable
;
97 TIS_TPM_HANDLE TpmHandle
;
102 EFI_TCG_CLIENT_ACPI_TABLE mTcgClientAcpiTemplate
= {
104 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE
,
105 sizeof (mTcgClientAcpiTemplate
),
108 // Compiler initializes the remaining bytes to 0
109 // These fields should be filled in in production
112 0, // 0 for PC Client Platform Class
113 0, // Log Area Max Length
114 (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1) // Log Area Start Address
118 // The following EFI_TCG_SERVER_ACPI_TABLE default setting is just one example,
119 // the TPM device connectes to LPC, and also defined the ACPI _UID as 0xFF,
120 // this _UID can be changed and should match with the _UID setting of the TPM
121 // ACPI device object
123 EFI_TCG_SERVER_ACPI_TABLE mTcgServerAcpiTemplate
= {
125 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE
,
126 sizeof (mTcgServerAcpiTemplate
),
129 // Compiler initializes the remaining bytes to 0
130 // These fields should be filled in in production
133 1, // 1 for Server Platform Class
135 0, // Log Area Max Length
136 (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1), // Log Area Start Address
137 0x0100, // TCG Specification revision 1.0
139 0, // Interrupt Flags
141 {0}, // Reserved 3 bytes
142 0, // Global System Interrupt
144 EFI_ACPI_3_0_SYSTEM_MEMORY
,
148 TPM_BASE_ADDRESS
// Base Address
151 {0}, // Configuration Address
152 0xFF, // ACPI _UID value of the device, can be changed for different platforms
153 0, // ACPI _UID value of the device, can be changed for different platforms
154 0, // ACPI _UID value of the device, can be changed for different platforms
155 0 // ACPI _UID value of the device, can be changed for different platforms
158 UINTN mBootAttempts
= 0;
159 CHAR16 mBootVarName
[] = L
"BootOrder";
162 This service provides EFI protocol capability information, state information
163 about the TPM, and Event Log state information.
165 @param[in] This Indicates the calling context
166 @param[out] ProtocolCapability The callee allocates memory for a TCG_BOOT_SERVICE_CAPABILITY
167 structure and fills in the fields with the EFI protocol
168 capability information and the current TPM state information.
169 @param[out] TCGFeatureFlags This is a pointer to the feature flags. No feature
170 flags are currently defined so this parameter
171 MUST be set to 0. However, in the future,
172 feature flags may be defined that, for example,
173 enable hash algorithm agility.
174 @param[out] EventLogLocation This is a pointer to the address of the event log in memory.
175 @param[out] EventLogLastEntry If the Event Log contains more than one entry,
176 this is a pointer to the address of the start of
177 the last entry in the event log in memory.
179 @retval EFI_SUCCESS Operation completed successfully.
180 @retval EFI_INVALID_PARAMETER ProtocolCapability does not match TCG capability.
186 IN EFI_TCG_PROTOCOL
*This
,
187 OUT TCG_EFI_BOOT_SERVICE_CAPABILITY
*ProtocolCapability
,
188 OUT UINT32
*TCGFeatureFlags
,
189 OUT EFI_PHYSICAL_ADDRESS
*EventLogLocation
,
190 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
193 TCG_DXE_DATA
*TcgData
;
195 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
197 if (ProtocolCapability
!= NULL
) {
198 *ProtocolCapability
= TcgData
->BsCap
;
201 if (TCGFeatureFlags
!= NULL
) {
202 *TCGFeatureFlags
= 0;
205 if (EventLogLocation
!= NULL
) {
206 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
207 *EventLogLocation
= TcgData
->TcgClientAcpiTable
->Lasa
;
209 *EventLogLocation
= TcgData
->TcgServerAcpiTable
->Lasa
;
213 if (EventLogLastEntry
!= NULL
) {
214 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
215 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)0;
217 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)TcgData
->LastEvent
;
225 This service abstracts the capability to do a hash operation on a data buffer.
227 @param[in] This Indicates the calling context
228 @param[in] HashData Pointer to the data buffer to be hashed
229 @param[in] HashDataLen Length of the data buffer to be hashed
230 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
231 @param[in, out] HashedDataLen Resultant length of the hashed data
232 @param[in, out] HashedDataResult Resultant buffer of the hashed data
234 @retval EFI_SUCCESS Operation completed successfully.
235 @retval EFI_INVALID_PARAMETER HashDataLen is NULL.
236 @retval EFI_INVALID_PARAMETER HashDataLenResult is NULL.
237 @retval EFI_OUT_OF_RESOURCES Cannot allocate buffer of size *HashedDataLen.
238 @retval EFI_UNSUPPORTED AlgorithmId not supported.
239 @retval EFI_BUFFER_TOO_SMALL *HashedDataLen < sizeof (TCG_DIGEST).
245 IN EFI_TCG_PROTOCOL
*This
,
247 IN UINT64 HashDataLen
,
248 IN TCG_ALGORITHM_ID AlgorithmId
,
249 IN OUT UINT64
*HashedDataLen
,
250 IN OUT UINT8
**HashedDataResult
253 if (HashedDataLen
== NULL
|| HashedDataResult
== NULL
) {
254 return EFI_INVALID_PARAMETER
;
257 switch (AlgorithmId
) {
259 if (*HashedDataLen
== 0) {
260 *HashedDataLen
= sizeof (TPM_DIGEST
);
261 *HashedDataResult
= AllocatePool ((UINTN
) *HashedDataLen
);
262 if (*HashedDataResult
== NULL
) {
263 return EFI_OUT_OF_RESOURCES
;
267 if (*HashedDataLen
< sizeof (TPM_DIGEST
)) {
268 *HashedDataLen
= sizeof (TPM_DIGEST
);
269 return EFI_BUFFER_TOO_SMALL
;
271 *HashedDataLen
= sizeof (TPM_DIGEST
);
273 if (*HashedDataResult
== NULL
) {
274 *HashedDataResult
= AllocatePool ((UINTN
) *HashedDataLen
);
277 return TpmCommHashAll (
280 (TPM_DIGEST
*)*HashedDataResult
283 return EFI_UNSUPPORTED
;
288 Add a new entry to the Event Log.
290 @param[in] TcgData TCG_DXE_DATA structure.
291 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
292 @param[in] NewEventData Pointer to the new event data.
294 @retval EFI_SUCCESS The new event log entry was added.
295 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
301 IN TCG_DXE_DATA
*TcgData
,
302 IN TCG_PCR_EVENT_HDR
*NewEventHdr
,
303 IN UINT8
*NewEventData
306 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
307 TcgData
->LastEvent
= (UINT8
*)(UINTN
)TcgData
->TcgClientAcpiTable
->Lasa
;
308 return TpmCommLogEvent (
310 &TcgData
->EventLogSize
,
311 (UINTN
)TcgData
->TcgClientAcpiTable
->Laml
,
316 TcgData
->LastEvent
= (UINT8
*)(UINTN
)TcgData
->TcgServerAcpiTable
->Lasa
;
317 return TpmCommLogEvent (
319 &TcgData
->EventLogSize
,
320 (UINTN
)TcgData
->TcgServerAcpiTable
->Laml
,
328 This service abstracts the capability to add an entry to the Event Log.
330 @param[in] This Indicates the calling context
331 @param[in] TCGLogData Pointer to the start of the data buffer containing
332 the TCG_PCR_EVENT data structure. All fields in
333 this structure are properly filled by the caller.
334 @param[in, out] EventNumber The event number of the event just logged
335 @param[in] Flags Indicate additional flags. Only one flag has been
336 defined at this time, which is 0x01 and means the
337 extend operation should not be performed. All
338 other bits are reserved.
340 @retval EFI_SUCCESS Operation completed successfully.
341 @retval EFI_OUT_OF_RESOURCES Insufficient memory in the event log to complete this action.
347 IN EFI_TCG_PROTOCOL
*This
,
348 IN TCG_PCR_EVENT
*TCGLogData
,
349 IN OUT UINT32
*EventNumber
,
353 TCG_DXE_DATA
*TcgData
;
355 if (TCGLogData
== NULL
){
356 return EFI_INVALID_PARAMETER
;
359 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
361 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
362 return EFI_DEVICE_ERROR
;
364 return TcgDxeLogEventI (
366 (TCG_PCR_EVENT_HDR
*)TCGLogData
,
372 This service is a proxy for commands to the TPM.
374 @param[in] This Indicates the calling context
375 @param[in] TpmInputParameterBlockSize Size of the TPM input parameter block
376 @param[in] TpmInputParameterBlock Pointer to the TPM input parameter block
377 @param[in] TpmOutputParameterBlockSize Size of the TPM output parameter block
378 @param[in] TpmOutputParameterBlock Pointer to the TPM output parameter block
380 @retval EFI_SUCCESS Operation completed successfully.
381 @retval EFI_INVALID_PARAMETER Invalid ordinal.
382 @retval EFI_UNSUPPORTED Current Task Priority Level >= EFI_TPL_CALLBACK.
383 @retval EFI_TIMEOUT The TIS timed-out.
388 TcgDxePassThroughToTpm (
389 IN EFI_TCG_PROTOCOL
*This
,
390 IN UINT32 TpmInputParameterBlockSize
,
391 IN UINT8
*TpmInputParameterBlock
,
392 IN UINT32 TpmOutputParameterBlockSize
,
393 IN UINT8
*TpmOutputParameterBlock
396 TCG_DXE_DATA
*TcgData
;
398 if (TpmInputParameterBlock
== NULL
||
399 TpmOutputParameterBlock
== NULL
||
400 TpmInputParameterBlockSize
== 0 ||
401 TpmOutputParameterBlockSize
== 0) {
402 return EFI_INVALID_PARAMETER
;
405 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
407 return TisPcExecute (
410 TpmInputParameterBlock
,
411 (UINTN
) TpmInputParameterBlockSize
,
412 TpmOutputParameterBlock
,
413 (UINTN
) TpmOutputParameterBlockSize
418 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
419 and add an entry to the Event Log.
421 @param[in] TcgData TCG_DXE_DATA structure.
422 @param[in] HashData Physical address of the start of the data buffer
423 to be hashed, extended, and logged.
424 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
425 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
426 @param[in] NewEventData Pointer to the new event data.
428 @retval EFI_SUCCESS Operation completed successfully.
429 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
430 @retval EFI_DEVICE_ERROR The command was unsuccessful.
435 TcgDxeHashLogExtendEventI (
436 IN TCG_DXE_DATA
*TcgData
,
438 IN UINT64 HashDataLen
,
439 IN OUT TCG_PCR_EVENT_HDR
*NewEventHdr
,
440 IN UINT8
*NewEventData
445 if (HashData
== NULL
&& HashDataLen
> 0) {
446 return EFI_INVALID_PARAMETER
;
449 if (HashDataLen
> 0 || HashData
!= NULL
) {
450 Status
= TpmCommHashAll (
455 ASSERT_EFI_ERROR (Status
);
458 Status
= TpmCommExtend (
460 &NewEventHdr
->Digest
,
461 NewEventHdr
->PCRIndex
,
464 if (!EFI_ERROR (Status
)) {
465 Status
= TcgDxeLogEventI (TcgData
, NewEventHdr
, NewEventData
);
472 This service abstracts the capability to do a hash operation on a data buffer,
473 extend a specific TPM PCR with the hash result, and add an entry to the Event Log
475 @param[in] This Indicates the calling context
476 @param[in] HashData Physical address of the start of the data buffer
477 to be hashed, extended, and logged.
478 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
479 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
480 @param[in, out] TCGLogData The physical address of the start of the data
481 buffer containing the TCG_PCR_EVENT data structure.
482 @param[in, out] EventNumber The event number of the event just logged.
483 @param[out] EventLogLastEntry Physical address of the first byte of the entry
484 just placed in the Event Log. If the Event Log was
485 empty when this function was called then this physical
486 address will be the same as the physical address of
487 the start of the Event Log.
489 @retval EFI_SUCCESS Operation completed successfully.
490 @retval EFI_UNSUPPORTED AlgorithmId != TPM_ALG_SHA.
491 @retval EFI_UNSUPPORTED Current TPL >= EFI_TPL_CALLBACK.
492 @retval EFI_DEVICE_ERROR The command was unsuccessful.
497 TcgDxeHashLogExtendEvent (
498 IN EFI_TCG_PROTOCOL
*This
,
499 IN EFI_PHYSICAL_ADDRESS HashData
,
500 IN UINT64 HashDataLen
,
501 IN TPM_ALGORITHM_ID AlgorithmId
,
502 IN OUT TCG_PCR_EVENT
*TCGLogData
,
503 IN OUT UINT32
*EventNumber
,
504 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
507 TCG_DXE_DATA
*TcgData
;
509 if (TCGLogData
== NULL
|| EventLogLastEntry
== NULL
){
510 return EFI_INVALID_PARAMETER
;
513 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
515 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
516 return EFI_DEVICE_ERROR
;
519 if (AlgorithmId
!= TPM_ALG_SHA
) {
520 return EFI_UNSUPPORTED
;
523 return TcgDxeHashLogExtendEventI (
525 (UINT8
*) (UINTN
) HashData
,
527 (TCG_PCR_EVENT_HDR
*)TCGLogData
,
532 TCG_DXE_DATA mTcgDxeData
= {
537 TcgDxePassThroughToTpm
,
538 TcgDxeHashLogExtendEvent
541 sizeof (mTcgDxeData
.BsCap
),
548 &mTcgClientAcpiTemplate
,
549 &mTcgServerAcpiTemplate
,
556 Initialize the Event Log and log events passed from the PEI phase.
558 @retval EFI_SUCCESS Operation completed successfully.
559 @retval EFI_OUT_OF_RESOURCES Out of memory.
569 TCG_PCR_EVENT
*TcgEvent
;
570 EFI_PEI_HOB_POINTERS GuidHob
;
571 EFI_PHYSICAL_ADDRESS Lasa
;
573 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
574 Lasa
= mTcgClientAcpiTemplate
.Lasa
;
576 Status
= gBS
->AllocatePages (
579 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE
),
582 if (EFI_ERROR (Status
)) {
585 mTcgClientAcpiTemplate
.Lasa
= Lasa
;
587 // To initialize them as 0xFF is recommended
588 // because the OS can know the last entry for that.
590 SetMem ((VOID
*)(UINTN
)mTcgClientAcpiTemplate
.Lasa
, EFI_TCG_LOG_AREA_SIZE
, 0xFF);
591 mTcgClientAcpiTemplate
.Laml
= EFI_TCG_LOG_AREA_SIZE
;
594 Lasa
= mTcgServerAcpiTemplate
.Lasa
;
596 Status
= gBS
->AllocatePages (
599 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE
),
602 if (EFI_ERROR (Status
)) {
605 mTcgServerAcpiTemplate
.Lasa
= Lasa
;
607 // To initialize them as 0xFF is recommended
608 // because the OS can know the last entry for that.
610 SetMem ((VOID
*)(UINTN
)mTcgServerAcpiTemplate
.Lasa
, EFI_TCG_LOG_AREA_SIZE
, 0xFF);
611 mTcgServerAcpiTemplate
.Laml
= EFI_TCG_LOG_AREA_SIZE
;
614 GuidHob
.Raw
= GetHobList ();
615 while (!EFI_ERROR (Status
) &&
616 (GuidHob
.Raw
= GetNextGuidHob (&gTcgEventEntryHobGuid
, GuidHob
.Raw
)) != NULL
) {
617 TcgEvent
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
618 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
619 Status
= TcgDxeLogEventI (
621 (TCG_PCR_EVENT_HDR
*)TcgEvent
,
630 Measure and log an action string, and extend the measurement result into PCR[5].
632 @param[in] String A specific string that indicates an Action event.
634 @retval EFI_SUCCESS Operation completed successfully.
635 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
644 TCG_PCR_EVENT_HDR TcgEvent
;
646 TcgEvent
.PCRIndex
= 5;
647 TcgEvent
.EventType
= EV_EFI_ACTION
;
648 TcgEvent
.EventSize
= (UINT32
)AsciiStrLen (String
);
649 return TcgDxeHashLogExtendEventI (
659 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
661 @retval EFI_SUCCESS Operation completed successfully.
662 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
667 MeasureHandoffTables (
672 SMBIOS_TABLE_ENTRY_POINT
*SmbiosTable
;
673 TCG_PCR_EVENT_HDR TcgEvent
;
674 EFI_HANDOFF_TABLE_POINTERS HandoffTables
;
676 Status
= EfiGetSystemConfigurationTable (
677 &gEfiSmbiosTableGuid
,
678 (VOID
**) &SmbiosTable
681 if (!EFI_ERROR (Status
)) {
682 ASSERT (SmbiosTable
!= NULL
);
684 TcgEvent
.PCRIndex
= 1;
685 TcgEvent
.EventType
= EV_EFI_HANDOFF_TABLES
;
686 TcgEvent
.EventSize
= sizeof (HandoffTables
);
688 HandoffTables
.NumberOfTables
= 1;
689 HandoffTables
.TableEntry
[0].VendorGuid
= gEfiSmbiosTableGuid
;
690 HandoffTables
.TableEntry
[0].VendorTable
= SmbiosTable
;
692 DEBUG ((DEBUG_INFO
, "The Smbios Table starts at: 0x%x\n", SmbiosTable
->TableAddress
));
693 DEBUG ((DEBUG_INFO
, "The Smbios Table size: 0x%x\n", SmbiosTable
->TableLength
));
695 Status
= TcgDxeHashLogExtendEventI (
697 (UINT8
*)(UINTN
)SmbiosTable
->TableAddress
,
698 SmbiosTable
->TableLength
,
700 (UINT8
*)&HandoffTables
708 Measure and log Separator event, and extend the measurement result into a specific PCR.
710 @param[in] PCRIndex PCR index.
712 @retval EFI_SUCCESS Operation completed successfully.
713 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
718 MeasureSeparatorEvent (
719 IN TPM_PCRINDEX PCRIndex
722 TCG_PCR_EVENT_HDR TcgEvent
;
726 TcgEvent
.PCRIndex
= PCRIndex
;
727 TcgEvent
.EventType
= EV_SEPARATOR
;
728 TcgEvent
.EventSize
= (UINT32
)sizeof (EventData
);
729 return TcgDxeHashLogExtendEventI (
739 Read an EFI Variable.
741 This function allocates a buffer to return the contents of the variable. The caller is
742 responsible for freeing the buffer.
744 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
745 @param[in] VendorGuid A unique identifier for the vendor.
746 @param[out] VarSize The size of the variable data.
748 @return A pointer to the buffer to return the contents of the variable.Otherwise NULL.
755 IN EFI_GUID
*VendorGuid
,
763 Status
= gRT
->GetVariable (
770 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
774 VarData
= AllocatePool (*VarSize
);
775 if (VarData
!= NULL
) {
776 Status
= gRT
->GetVariable (
783 if (EFI_ERROR (Status
)) {
793 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
795 @param[in] PCRIndex PCR Index.
796 @param[in] EventType Event type.
797 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
798 @param[in] VendorGuid A unique identifier for the vendor.
799 @param[in] VarData The content of the variable data.
800 @param[in] VarSize The size of the variable data.
802 @retval EFI_SUCCESS Operation completed successfully.
803 @retval EFI_OUT_OF_RESOURCES Out of memory.
804 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
810 IN TPM_PCRINDEX PCRIndex
,
811 IN TCG_EVENTTYPE EventType
,
813 IN EFI_GUID
*VendorGuid
,
819 TCG_PCR_EVENT_HDR TcgEvent
;
821 EFI_VARIABLE_DATA
*VarLog
;
823 VarNameLength
= StrLen (VarName
);
824 TcgEvent
.PCRIndex
= PCRIndex
;
825 TcgEvent
.EventType
= EventType
;
826 TcgEvent
.EventSize
= (UINT32
)(sizeof (*VarLog
) + VarNameLength
* sizeof (*VarName
) + VarSize
827 - sizeof (VarLog
->UnicodeName
) - sizeof (VarLog
->VariableData
));
829 VarLog
= (EFI_VARIABLE_DATA
*)AllocatePool (TcgEvent
.EventSize
);
830 if (VarLog
== NULL
) {
831 return EFI_OUT_OF_RESOURCES
;
834 VarLog
->VariableName
= *VendorGuid
;
835 VarLog
->UnicodeNameLength
= VarNameLength
;
836 VarLog
->VariableDataLength
= VarSize
;
840 VarNameLength
* sizeof (*VarName
)
843 (CHAR16
*)VarLog
->UnicodeName
+ VarNameLength
,
848 Status
= TcgDxeHashLogExtendEventI (
860 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
862 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
863 @param[in] VendorGuid A unique identifier for the vendor.
864 @param[out] VarSize The size of the variable data.
865 @param[out] VarData Pointer to the content of the variable.
867 @retval EFI_SUCCESS Operation completed successfully.
868 @retval EFI_OUT_OF_RESOURCES Out of memory.
869 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
874 ReadAndMeasureBootVariable (
876 IN EFI_GUID
*VendorGuid
,
883 *VarData
= ReadVariable (VarName
, VendorGuid
, VarSize
);
884 if (*VarData
== NULL
) {
885 return EFI_NOT_FOUND
;
888 Status
= MeasureVariable (
890 EV_EFI_VARIABLE_BOOT
,
900 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
902 The EFI boot variables are BootOrder and Boot#### variables.
904 @retval EFI_SUCCESS Operation completed successfully.
905 @retval EFI_OUT_OF_RESOURCES Out of memory.
906 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
911 MeasureAllBootVariables (
922 Status
= ReadAndMeasureBootVariable (
924 &gEfiGlobalVariableGuid
,
928 if (Status
== EFI_NOT_FOUND
) {
931 ASSERT (BootOrder
!= NULL
);
933 if (EFI_ERROR (Status
)) {
934 FreePool (BootOrder
);
938 BootCount
/= sizeof (*BootOrder
);
939 for (Index
= 0; Index
< BootCount
; Index
++) {
940 UnicodeSPrint (mBootVarName
, sizeof (mBootVarName
), L
"Boot%04x", BootOrder
[Index
]);
941 Status
= ReadAndMeasureBootVariable (
943 &gEfiGlobalVariableGuid
,
947 if (!EFI_ERROR (Status
)) {
948 FreePool (BootVarData
);
952 FreePool (BootOrder
);
957 Ready to Boot Event notification handler.
959 Sequence of OS boot events is measured in this event notification handler.
961 @param[in] Event Event whose notification function is being invoked
962 @param[in] Context Pointer to the notification function's context
973 TPM_PCRINDEX PcrIndex
;
975 if (mBootAttempts
== 0) {
978 // Measure handoff tables.
980 Status
= MeasureHandoffTables ();
981 if (EFI_ERROR (Status
)) {
982 DEBUG ((EFI_D_ERROR
, "HOBs not Measured. Error!\n"));
986 // Measure BootOrder & Boot#### variables.
988 Status
= MeasureAllBootVariables ();
989 if (EFI_ERROR (Status
)) {
990 DEBUG ((EFI_D_ERROR
, "Boot Variables not Measured. Error!\n"));
994 // 1. This is the first boot attempt.
996 Status
= TcgMeasureAction (
997 EFI_CALLING_EFI_APPLICATION
999 ASSERT_EFI_ERROR (Status
);
1002 // 2. Draw a line between pre-boot env and entering post-boot env.
1004 for (PcrIndex
= 0; PcrIndex
< 8; PcrIndex
++) {
1005 Status
= MeasureSeparatorEvent (PcrIndex
);
1006 ASSERT_EFI_ERROR (Status
);
1010 // 3. Measure GPT. It would be done in SAP driver.
1014 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
1018 // 5. Read & Measure variable. BootOrder already measured.
1022 // 6. Not first attempt, meaning a return from last attempt
1024 Status
= TcgMeasureAction (
1025 EFI_RETURNING_FROM_EFI_APPLICATOIN
1027 ASSERT_EFI_ERROR (Status
);
1030 DEBUG ((EFI_D_INFO
, "TPM TcgDxe Measure Data when ReadyToBoot\n"));
1032 // Increase boot attempt counter.
1038 Install TCG ACPI Table when ACPI Table Protocol is available.
1040 A system's firmware uses an ACPI table to identify the system's TCG capabilities
1041 to the Post-Boot environment. The information in this ACPI table is not guaranteed
1042 to be valid until the Host Platform transitions from pre-boot state to post-boot state.
1044 @param[in] Event Event whose notification function is being invoked
1045 @param[in] Context Pointer to the notification function's context
1056 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
1059 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**)&AcpiTable
);
1060 if (EFI_ERROR (Status
)) {
1064 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
1067 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1068 // service of the ACPI table protocol to install it.
1070 Checksum
= CalculateCheckSum8 ((UINT8
*)&mTcgClientAcpiTemplate
, sizeof (mTcgClientAcpiTemplate
));
1071 mTcgClientAcpiTemplate
.Header
.Checksum
= Checksum
;
1073 Status
= AcpiTable
->InstallAcpiTable (
1075 &mTcgClientAcpiTemplate
,
1076 sizeof (mTcgClientAcpiTemplate
),
1082 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1083 // service of the ACPI table protocol to install it.
1085 Checksum
= CalculateCheckSum8 ((UINT8
*)&mTcgServerAcpiTemplate
, sizeof (mTcgServerAcpiTemplate
));
1086 mTcgServerAcpiTemplate
.Header
.Checksum
= Checksum
;
1088 Status
= AcpiTable
->InstallAcpiTable (
1090 &mTcgServerAcpiTemplate
,
1091 sizeof (mTcgServerAcpiTemplate
),
1095 ASSERT_EFI_ERROR (Status
);
1099 Exit Boot Services Event notification handler.
1101 Measure invocation and success of ExitBootServices.
1103 @param[in] Event Event whose notification function is being invoked
1104 @param[in] Context Pointer to the notification function's context
1109 OnExitBootServices (
1117 // Measure invocation of ExitBootServices,
1119 Status
= TcgMeasureAction (
1120 EFI_EXIT_BOOT_SERVICES_INVOCATION
1122 ASSERT_EFI_ERROR (Status
);
1125 // Measure success of ExitBootServices
1127 Status
= TcgMeasureAction (
1128 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
1130 ASSERT_EFI_ERROR (Status
);
1134 Exit Boot Services Failed Event notification handler.
1136 Measure Failure of ExitBootServices.
1138 @param[in] Event Event whose notification function is being invoked
1139 @param[in] Context Pointer to the notification function's context
1144 OnExitBootServicesFailed (
1152 // Measure Failure of ExitBootServices,
1154 Status
= TcgMeasureAction (
1155 EFI_EXIT_BOOT_SERVICES_FAILED
1157 ASSERT_EFI_ERROR (Status
);
1162 Get TPM Deactivated state.
1164 @param[out] TPMDeactivatedFlag Returns TPM Deactivated state.
1166 @retval EFI_SUCCESS Operation completed successfully.
1167 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1172 OUT BOOLEAN
*TPMDeactivatedFlag
1176 TPM_STCLEAR_FLAGS VFlags
;
1178 Status
= TpmCommGetFlags (
1179 mTcgDxeData
.TpmHandle
,
1180 TPM_CAP_FLAG_VOLATILE
,
1184 if (!EFI_ERROR (Status
)) {
1185 *TPMDeactivatedFlag
= VFlags
.deactivated
;
1192 The driver's entry point.
1194 It publishes EFI TCG Protocol.
1196 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1197 @param[in] SystemTable A pointer to the EFI System Table.
1199 @retval EFI_SUCCESS The entry point is executed successfully.
1200 @retval other Some error occurs when executing this entry point.
1206 IN EFI_HANDLE ImageHandle
,
1207 IN EFI_SYSTEM_TABLE
*SystemTable
1214 mTcgDxeData
.TpmHandle
= (TIS_TPM_HANDLE
)(UINTN
)TPM_BASE_ADDRESS
;
1215 Status
= TisPcRequestUseTpm (mTcgDxeData
.TpmHandle
);
1216 if (EFI_ERROR (Status
)) {
1217 DEBUG ((EFI_D_ERROR
, "TPM not detected!\n"));
1221 Status
= GetTpmStatus (&mTcgDxeData
.BsCap
.TPMDeactivatedFlag
);
1222 if (EFI_ERROR (Status
)) {
1225 "Line %d in file " __FILE__
":\n "
1226 "DriverEntry: TPM not working properly\n",
1232 Status
= gBS
->InstallProtocolInterface (
1234 &gEfiTcgProtocolGuid
,
1235 EFI_NATIVE_INTERFACE
,
1236 &mTcgDxeData
.TcgProtocol
1238 if (!EFI_ERROR (Status
) && !mTcgDxeData
.BsCap
.TPMDeactivatedFlag
) {
1240 // Setup the log area and copy event log from hob list to it
1242 Status
= SetupEventLog ();
1243 ASSERT_EFI_ERROR (Status
);
1246 // Measure handoff tables, Boot#### variables etc.
1248 Status
= EfiCreateEventReadyToBootEx (
1255 Status
= gBS
->CreateEventEx (
1260 &gEfiEventExitBootServicesGuid
,
1265 // Measure Exit Boot Service failed
1267 Status
= gBS
->CreateEventEx (
1270 OnExitBootServicesFailed
,
1272 &gEventExitBootServicesFailedGuid
,
1278 // Install ACPI Table
1280 EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid
, TPL_CALLBACK
, InstallAcpiTable
, NULL
, &Registration
);