2 This module implements TCG EFI Protocol.
4 Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <IndustryStandard/Tpm12.h>
17 #include <IndustryStandard/Acpi.h>
18 #include <IndustryStandard/PeImage.h>
19 #include <IndustryStandard/SmBios.h>
21 #include <Guid/GlobalVariable.h>
22 #include <Guid/SmBios.h>
23 #include <Guid/HobList.h>
24 #include <Guid/TcgEventHob.h>
25 #include <Guid/EventGroup.h>
26 #include <Protocol/DevicePath.h>
27 #include <Protocol/TcgService.h>
28 #include <Protocol/AcpiTable.h>
30 #include <Library/DebugLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/UefiRuntimeServicesTableLib.h>
33 #include <Library/UefiDriverEntryPoint.h>
34 #include <Library/HobLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/BaseLib.h>
37 #include <Library/MemoryAllocationLib.h>
38 #include <Library/PrintLib.h>
39 #include <Library/TpmCommLib.h>
40 #include <Library/PcdLib.h>
41 #include <Library/UefiLib.h>
45 #define EFI_TCG_LOG_AREA_SIZE 0x10000
49 typedef struct _EFI_TCG_CLIENT_ACPI_TABLE
{
50 EFI_ACPI_DESCRIPTION_HEADER Header
;
53 EFI_PHYSICAL_ADDRESS Lasa
;
54 } EFI_TCG_CLIENT_ACPI_TABLE
;
56 typedef struct _EFI_TCG_SERVER_ACPI_TABLE
{
57 EFI_ACPI_DESCRIPTION_HEADER Header
;
61 EFI_PHYSICAL_ADDRESS Lasa
;
68 EFI_ACPI_3_0_GENERIC_ADDRESS_STRUCTURE BaseAddress
;
70 EFI_ACPI_3_0_GENERIC_ADDRESS_STRUCTURE ConfigAddress
;
75 } EFI_TCG_SERVER_ACPI_TABLE
;
79 #define TCG_DXE_DATA_FROM_THIS(this) \
80 BASE_CR (this, TCG_DXE_DATA, TcgProtocol)
82 typedef struct _TCG_DXE_DATA
{
83 EFI_TCG_PROTOCOL TcgProtocol
;
84 TCG_EFI_BOOT_SERVICE_CAPABILITY BsCap
;
85 EFI_TCG_CLIENT_ACPI_TABLE
*TcgClientAcpiTable
;
86 EFI_TCG_SERVER_ACPI_TABLE
*TcgServerAcpiTable
;
89 TIS_TPM_HANDLE TpmHandle
;
94 EFI_TCG_CLIENT_ACPI_TABLE mTcgClientAcpiTemplate
= {
96 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE
,
97 sizeof (mTcgClientAcpiTemplate
),
100 // Compiler initializes the remaining bytes to 0
101 // These fields should be filled in in production
104 0, // 0 for PC Client Platform Class
105 0, // Log Area Max Length
106 (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1) // Log Area Start Address
110 // The following EFI_TCG_SERVER_ACPI_TABLE default setting is just one example,
111 // the TPM device connectes to LPC, and also defined the ACPI _UID as 0xFF,
112 // this _UID can be changed and should match with the _UID setting of the TPM
113 // ACPI device object
115 EFI_TCG_SERVER_ACPI_TABLE mTcgServerAcpiTemplate
= {
117 EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE
,
118 sizeof (mTcgServerAcpiTemplate
),
121 // Compiler initializes the remaining bytes to 0
122 // These fields should be filled in in production
125 1, // 1 for Server Platform Class
127 0, // Log Area Max Length
128 (EFI_PHYSICAL_ADDRESS
) (SIZE_4GB
- 1), // Log Area Start Address
129 0x0100, // TCG Specification revision 1.0
131 0, // Interrupt Flags
133 {0}, // Reserved 3 bytes
134 0, // Global System Interrupt
136 EFI_ACPI_3_0_SYSTEM_MEMORY
,
140 TPM_BASE_ADDRESS
// Base Address
143 {0}, // Configuration Address
144 0xFF, // ACPI _UID value of the device, can be changed for different platforms
145 0, // ACPI _UID value of the device, can be changed for different platforms
146 0, // ACPI _UID value of the device, can be changed for different platforms
147 0 // ACPI _UID value of the device, can be changed for different platforms
150 UINTN mBootAttempts
= 0;
151 CHAR16 mBootVarName
[] = L
"BootOrder";
154 This service provides EFI protocol capability information, state information
155 about the TPM, and Event Log state information.
157 @param[in] This Indicates the calling context
158 @param[out] ProtocolCapability The callee allocates memory for a TCG_BOOT_SERVICE_CAPABILITY
159 structure and fills in the fields with the EFI protocol
160 capability information and the current TPM state information.
161 @param[out] TCGFeatureFlags This is a pointer to the feature flags. No feature
162 flags are currently defined so this parameter
163 MUST be set to 0. However, in the future,
164 feature flags may be defined that, for example,
165 enable hash algorithm agility.
166 @param[out] EventLogLocation This is a pointer to the address of the event log in memory.
167 @param[out] EventLogLastEntry If the Event Log contains more than one entry,
168 this is a pointer to the address of the start of
169 the last entry in the event log in memory.
171 @retval EFI_SUCCESS Operation completed successfully.
172 @retval EFI_INVALID_PARAMETER ProtocolCapability does not match TCG capability.
178 IN EFI_TCG_PROTOCOL
*This
,
179 OUT TCG_EFI_BOOT_SERVICE_CAPABILITY
*ProtocolCapability
,
180 OUT UINT32
*TCGFeatureFlags
,
181 OUT EFI_PHYSICAL_ADDRESS
*EventLogLocation
,
182 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
185 TCG_DXE_DATA
*TcgData
;
187 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
189 if (ProtocolCapability
!= NULL
) {
190 *ProtocolCapability
= TcgData
->BsCap
;
193 if (TCGFeatureFlags
!= NULL
) {
194 *TCGFeatureFlags
= 0;
197 if (EventLogLocation
!= NULL
) {
198 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
199 *EventLogLocation
= TcgData
->TcgClientAcpiTable
->Lasa
;
201 *EventLogLocation
= TcgData
->TcgServerAcpiTable
->Lasa
;
205 if (EventLogLastEntry
!= NULL
) {
206 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
207 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)0;
209 *EventLogLastEntry
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)TcgData
->LastEvent
;
217 This service abstracts the capability to do a hash operation on a data buffer.
219 @param[in] This Indicates the calling context
220 @param[in] HashData Pointer to the data buffer to be hashed
221 @param[in] HashDataLen Length of the data buffer to be hashed
222 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
223 @param[in, out] HashedDataLen Resultant length of the hashed data
224 @param[in, out] HashedDataResult Resultant buffer of the hashed data
226 @retval EFI_SUCCESS Operation completed successfully.
227 @retval EFI_INVALID_PARAMETER HashDataLen is NULL.
228 @retval EFI_INVALID_PARAMETER HashDataLenResult is NULL.
229 @retval EFI_OUT_OF_RESOURCES Cannot allocate buffer of size *HashedDataLen.
230 @retval EFI_UNSUPPORTED AlgorithmId not supported.
231 @retval EFI_BUFFER_TOO_SMALL *HashedDataLen < sizeof (TCG_DIGEST).
237 IN EFI_TCG_PROTOCOL
*This
,
239 IN UINT64 HashDataLen
,
240 IN TCG_ALGORITHM_ID AlgorithmId
,
241 IN OUT UINT64
*HashedDataLen
,
242 IN OUT UINT8
**HashedDataResult
245 if (HashedDataLen
== NULL
|| HashedDataResult
== NULL
) {
246 return EFI_INVALID_PARAMETER
;
249 switch (AlgorithmId
) {
251 if (*HashedDataLen
== 0) {
252 *HashedDataLen
= sizeof (TPM_DIGEST
);
253 *HashedDataResult
= AllocatePool ((UINTN
) *HashedDataLen
);
254 if (*HashedDataResult
== NULL
) {
255 return EFI_OUT_OF_RESOURCES
;
259 if (*HashedDataLen
< sizeof (TPM_DIGEST
)) {
260 *HashedDataLen
= sizeof (TPM_DIGEST
);
261 return EFI_BUFFER_TOO_SMALL
;
263 *HashedDataLen
= sizeof (TPM_DIGEST
);
265 return TpmCommHashAll (
268 (TPM_DIGEST
*)*HashedDataResult
271 return EFI_UNSUPPORTED
;
276 Add a new entry to the Event Log.
278 @param[in] TcgData TCG_DXE_DATA structure.
279 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
280 @param[in] NewEventData Pointer to the new event data.
282 @retval EFI_SUCCESS The new event log entry was added.
283 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
289 IN TCG_DXE_DATA
*TcgData
,
290 IN TCG_PCR_EVENT_HDR
*NewEventHdr
,
291 IN UINT8
*NewEventData
294 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
295 TcgData
->LastEvent
= (UINT8
*)(UINTN
)TcgData
->TcgClientAcpiTable
->Lasa
;
296 return TpmCommLogEvent (
298 &TcgData
->EventLogSize
,
299 (UINTN
)TcgData
->TcgClientAcpiTable
->Laml
,
304 TcgData
->LastEvent
= (UINT8
*)(UINTN
)TcgData
->TcgServerAcpiTable
->Lasa
;
305 return TpmCommLogEvent (
307 &TcgData
->EventLogSize
,
308 (UINTN
)TcgData
->TcgServerAcpiTable
->Laml
,
316 This service abstracts the capability to add an entry to the Event Log.
318 @param[in] This Indicates the calling context
319 @param[in] TCGLogData Pointer to the start of the data buffer containing
320 the TCG_PCR_EVENT data structure. All fields in
321 this structure are properly filled by the caller.
322 @param[in, out] EventNumber The event number of the event just logged
323 @param[in] Flags Indicate additional flags. Only one flag has been
324 defined at this time, which is 0x01 and means the
325 extend operation should not be performed. All
326 other bits are reserved.
328 @retval EFI_SUCCESS Operation completed successfully.
329 @retval EFI_OUT_OF_RESOURCES Insufficient memory in the event log to complete this action.
335 IN EFI_TCG_PROTOCOL
*This
,
336 IN TCG_PCR_EVENT
*TCGLogData
,
337 IN OUT UINT32
*EventNumber
,
341 TCG_DXE_DATA
*TcgData
;
343 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
345 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
346 return EFI_DEVICE_ERROR
;
348 return TcgDxeLogEventI (
350 (TCG_PCR_EVENT_HDR
*)TCGLogData
,
356 This service is a proxy for commands to the TPM.
358 @param[in] This Indicates the calling context
359 @param[in] TpmInputParameterBlockSize Size of the TPM input parameter block
360 @param[in] TpmInputParameterBlock Pointer to the TPM input parameter block
361 @param[in] TpmOutputParameterBlockSize Size of the TPM output parameter block
362 @param[in] TpmOutputParameterBlock Pointer to the TPM output parameter block
364 @retval EFI_SUCCESS Operation completed successfully.
365 @retval EFI_INVALID_PARAMETER Invalid ordinal.
366 @retval EFI_UNSUPPORTED Current Task Priority Level >= EFI_TPL_CALLBACK.
367 @retval EFI_TIMEOUT The TIS timed-out.
372 TcgDxePassThroughToTpm (
373 IN EFI_TCG_PROTOCOL
*This
,
374 IN UINT32 TpmInputParameterBlockSize
,
375 IN UINT8
*TpmInputParameterBlock
,
376 IN UINT32 TpmOutputParameterBlockSize
,
377 IN UINT8
*TpmOutputParameterBlock
380 TCG_DXE_DATA
*TcgData
;
382 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
384 return TisPcExecute (
387 TpmInputParameterBlock
,
388 (UINTN
) TpmInputParameterBlockSize
,
389 TpmOutputParameterBlock
,
390 (UINTN
) TpmOutputParameterBlockSize
395 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
396 and add an entry to the Event Log.
398 @param[in] TcgData TCG_DXE_DATA structure.
399 @param[in] HashData Physical address of the start of the data buffer
400 to be hashed, extended, and logged.
401 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
402 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
403 @param[in] NewEventData Pointer to the new event data.
405 @retval EFI_SUCCESS Operation completed successfully.
406 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
407 @retval EFI_DEVICE_ERROR The command was unsuccessful.
412 TcgDxeHashLogExtendEventI (
413 IN TCG_DXE_DATA
*TcgData
,
415 IN UINT64 HashDataLen
,
416 IN OUT TCG_PCR_EVENT_HDR
*NewEventHdr
,
417 IN UINT8
*NewEventData
422 if (HashDataLen
> 0) {
423 Status
= TpmCommHashAll (
428 ASSERT_EFI_ERROR (Status
);
431 Status
= TpmCommExtend (
433 &NewEventHdr
->Digest
,
434 NewEventHdr
->PCRIndex
,
437 if (!EFI_ERROR (Status
)) {
438 Status
= TcgDxeLogEventI (TcgData
, NewEventHdr
, NewEventData
);
445 This service abstracts the capability to do a hash operation on a data buffer,
446 extend a specific TPM PCR with the hash result, and add an entry to the Event Log
448 @param[in] This Indicates the calling context
449 @param[in] HashData Physical address of the start of the data buffer
450 to be hashed, extended, and logged.
451 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
452 @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
453 @param[in, out] TCGLogData The physical address of the start of the data
454 buffer containing the TCG_PCR_EVENT data structure.
455 @param[in, out] EventNumber The event number of the event just logged.
456 @param[out] EventLogLastEntry Physical address of the first byte of the entry
457 just placed in the Event Log. If the Event Log was
458 empty when this function was called then this physical
459 address will be the same as the physical address of
460 the start of the Event Log.
462 @retval EFI_SUCCESS Operation completed successfully.
463 @retval EFI_UNSUPPORTED AlgorithmId != TPM_ALG_SHA.
464 @retval EFI_UNSUPPORTED Current TPL >= EFI_TPL_CALLBACK.
465 @retval EFI_DEVICE_ERROR The command was unsuccessful.
470 TcgDxeHashLogExtendEvent (
471 IN EFI_TCG_PROTOCOL
*This
,
472 IN EFI_PHYSICAL_ADDRESS HashData
,
473 IN UINT64 HashDataLen
,
474 IN TPM_ALGORITHM_ID AlgorithmId
,
475 IN OUT TCG_PCR_EVENT
*TCGLogData
,
476 IN OUT UINT32
*EventNumber
,
477 OUT EFI_PHYSICAL_ADDRESS
*EventLogLastEntry
480 TCG_DXE_DATA
*TcgData
;
482 TcgData
= TCG_DXE_DATA_FROM_THIS (This
);
484 if (TcgData
->BsCap
.TPMDeactivatedFlag
) {
485 return EFI_DEVICE_ERROR
;
488 if (AlgorithmId
!= TPM_ALG_SHA
) {
489 return EFI_UNSUPPORTED
;
492 return TcgDxeHashLogExtendEventI (
494 (UINT8
*) (UINTN
) HashData
,
496 (TCG_PCR_EVENT_HDR
*)TCGLogData
,
501 TCG_DXE_DATA mTcgDxeData
= {
506 TcgDxePassThroughToTpm
,
507 TcgDxeHashLogExtendEvent
510 sizeof (mTcgDxeData
.BsCap
),
517 &mTcgClientAcpiTemplate
,
518 &mTcgServerAcpiTemplate
,
525 Initialize the Event Log and log events passed from the PEI phase.
527 @retval EFI_SUCCESS Operation completed successfully.
528 @retval EFI_OUT_OF_RESOURCES Out of memory.
538 TCG_PCR_EVENT
*TcgEvent
;
539 EFI_PEI_HOB_POINTERS GuidHob
;
540 EFI_PHYSICAL_ADDRESS Lasa
;
542 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
543 Lasa
= mTcgClientAcpiTemplate
.Lasa
;
545 Status
= gBS
->AllocatePages (
548 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE
),
551 if (EFI_ERROR (Status
)) {
554 mTcgClientAcpiTemplate
.Lasa
= Lasa
;
556 // To initialize them as 0xFF is recommended
557 // because the OS can know the last entry for that.
559 SetMem ((VOID
*)(UINTN
)mTcgClientAcpiTemplate
.Lasa
, EFI_TCG_LOG_AREA_SIZE
, 0xFF);
560 mTcgClientAcpiTemplate
.Laml
= EFI_TCG_LOG_AREA_SIZE
;
563 Lasa
= mTcgServerAcpiTemplate
.Lasa
;
565 Status
= gBS
->AllocatePages (
568 EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE
),
571 if (EFI_ERROR (Status
)) {
574 mTcgServerAcpiTemplate
.Lasa
= Lasa
;
576 // To initialize them as 0xFF is recommended
577 // because the OS can know the last entry for that.
579 SetMem ((VOID
*)(UINTN
)mTcgServerAcpiTemplate
.Lasa
, EFI_TCG_LOG_AREA_SIZE
, 0xFF);
580 mTcgServerAcpiTemplate
.Laml
= EFI_TCG_LOG_AREA_SIZE
;
583 GuidHob
.Raw
= GetHobList ();
584 while (!EFI_ERROR (Status
) &&
585 (GuidHob
.Raw
= GetNextGuidHob (&gTcgEventEntryHobGuid
, GuidHob
.Raw
)) != NULL
) {
586 TcgEvent
= GET_GUID_HOB_DATA (GuidHob
.Guid
);
587 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
588 Status
= TcgDxeLogEventI (
590 (TCG_PCR_EVENT_HDR
*)TcgEvent
,
599 Measure and log an action string, and extend the measurement result into PCR[5].
601 @param[in] String A specific string that indicates an Action event.
603 @retval EFI_SUCCESS Operation completed successfully.
604 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
613 TCG_PCR_EVENT_HDR TcgEvent
;
615 TcgEvent
.PCRIndex
= 5;
616 TcgEvent
.EventType
= EV_EFI_ACTION
;
617 TcgEvent
.EventSize
= (UINT32
)AsciiStrLen (String
);
618 return TcgDxeHashLogExtendEventI (
628 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
630 @retval EFI_SUCCESS Operation completed successfully.
631 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
636 MeasureHandoffTables (
641 SMBIOS_TABLE_ENTRY_POINT
*SmbiosTable
;
642 TCG_PCR_EVENT_HDR TcgEvent
;
643 EFI_HANDOFF_TABLE_POINTERS HandoffTables
;
645 Status
= EfiGetSystemConfigurationTable (
646 &gEfiSmbiosTableGuid
,
647 (VOID
**) &SmbiosTable
650 if (!EFI_ERROR (Status
)) {
651 ASSERT (SmbiosTable
!= NULL
);
653 TcgEvent
.PCRIndex
= 1;
654 TcgEvent
.EventType
= EV_EFI_HANDOFF_TABLES
;
655 TcgEvent
.EventSize
= sizeof (HandoffTables
);
657 HandoffTables
.NumberOfTables
= 1;
658 HandoffTables
.TableEntry
[0].VendorGuid
= gEfiSmbiosTableGuid
;
659 HandoffTables
.TableEntry
[0].VendorTable
= SmbiosTable
;
661 DEBUG ((DEBUG_INFO
, "The Smbios Table starts at: 0x%x\n", SmbiosTable
->TableAddress
));
662 DEBUG ((DEBUG_INFO
, "The Smbios Table size: 0x%x\n", SmbiosTable
->TableLength
));
664 Status
= TcgDxeHashLogExtendEventI (
666 (UINT8
*)(UINTN
)SmbiosTable
->TableAddress
,
667 SmbiosTable
->TableLength
,
669 (UINT8
*)&HandoffTables
677 Measure and log Separator event, and extend the measurement result into a specific PCR.
679 @param[in] PCRIndex PCR index.
681 @retval EFI_SUCCESS Operation completed successfully.
682 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
687 MeasureSeparatorEvent (
688 IN TPM_PCRINDEX PCRIndex
691 TCG_PCR_EVENT_HDR TcgEvent
;
695 TcgEvent
.PCRIndex
= PCRIndex
;
696 TcgEvent
.EventType
= EV_SEPARATOR
;
697 TcgEvent
.EventSize
= (UINT32
)sizeof (EventData
);
698 return TcgDxeHashLogExtendEventI (
708 Read an EFI Variable.
710 This function allocates a buffer to return the contents of the variable. The caller is
711 responsible for freeing the buffer.
713 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
714 @param[in] VendorGuid A unique identifier for the vendor.
715 @param[out] VarSize The size of the variable data.
717 @return A pointer to the buffer to return the contents of the variable.Otherwise NULL.
724 IN EFI_GUID
*VendorGuid
,
732 Status
= gRT
->GetVariable (
739 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
743 VarData
= AllocatePool (*VarSize
);
744 if (VarData
!= NULL
) {
745 Status
= gRT
->GetVariable (
752 if (EFI_ERROR (Status
)) {
762 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
764 @param[in] PCRIndex PCR Index.
765 @param[in] EventType Event type.
766 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
767 @param[in] VendorGuid A unique identifier for the vendor.
768 @param[in] VarData The content of the variable data.
769 @param[in] VarSize The size of the variable data.
771 @retval EFI_SUCCESS Operation completed successfully.
772 @retval EFI_OUT_OF_RESOURCES Out of memory.
773 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
779 IN TPM_PCRINDEX PCRIndex
,
780 IN TCG_EVENTTYPE EventType
,
782 IN EFI_GUID
*VendorGuid
,
788 TCG_PCR_EVENT_HDR TcgEvent
;
790 EFI_VARIABLE_DATA
*VarLog
;
792 VarNameLength
= StrLen (VarName
);
793 TcgEvent
.PCRIndex
= PCRIndex
;
794 TcgEvent
.EventType
= EventType
;
795 TcgEvent
.EventSize
= (UINT32
)(sizeof (*VarLog
) + VarNameLength
* sizeof (*VarName
) + VarSize
796 - sizeof (VarLog
->UnicodeName
) - sizeof (VarLog
->VariableData
));
798 VarLog
= (EFI_VARIABLE_DATA
*)AllocatePool (TcgEvent
.EventSize
);
799 if (VarLog
== NULL
) {
800 return EFI_OUT_OF_RESOURCES
;
803 VarLog
->VariableName
= *VendorGuid
;
804 VarLog
->UnicodeNameLength
= VarNameLength
;
805 VarLog
->VariableDataLength
= VarSize
;
809 VarNameLength
* sizeof (*VarName
)
812 (CHAR16
*)VarLog
->UnicodeName
+ VarNameLength
,
817 Status
= TcgDxeHashLogExtendEventI (
829 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
831 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
832 @param[in] VendorGuid A unique identifier for the vendor.
833 @param[out] VarSize The size of the variable data.
834 @param[out] VarData Pointer to the content of the variable.
836 @retval EFI_SUCCESS Operation completed successfully.
837 @retval EFI_OUT_OF_RESOURCES Out of memory.
838 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
843 ReadAndMeasureBootVariable (
845 IN EFI_GUID
*VendorGuid
,
852 *VarData
= ReadVariable (VarName
, VendorGuid
, VarSize
);
853 if (*VarData
== NULL
) {
854 return EFI_NOT_FOUND
;
857 Status
= MeasureVariable (
859 EV_EFI_VARIABLE_BOOT
,
869 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
871 The EFI boot variables are BootOrder and Boot#### variables.
873 @retval EFI_SUCCESS Operation completed successfully.
874 @retval EFI_OUT_OF_RESOURCES Out of memory.
875 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
880 MeasureAllBootVariables (
891 Status
= ReadAndMeasureBootVariable (
893 &gEfiGlobalVariableGuid
,
897 if (Status
== EFI_NOT_FOUND
) {
900 ASSERT (BootOrder
!= NULL
);
902 if (EFI_ERROR (Status
)) {
903 FreePool (BootOrder
);
907 BootCount
/= sizeof (*BootOrder
);
908 for (Index
= 0; Index
< BootCount
; Index
++) {
909 UnicodeSPrint (mBootVarName
, sizeof (mBootVarName
), L
"Boot%04x", BootOrder
[Index
]);
910 Status
= ReadAndMeasureBootVariable (
912 &gEfiGlobalVariableGuid
,
916 if (!EFI_ERROR (Status
)) {
917 FreePool (BootVarData
);
921 FreePool (BootOrder
);
926 Ready to Boot Event notification handler.
928 Sequence of OS boot events is measured in this event notification handler.
930 @param[in] Event Event whose notification function is being invoked
931 @param[in] Context Pointer to the notification function's context
942 TPM_PCRINDEX PcrIndex
;
944 if (mBootAttempts
== 0) {
947 // Measure handoff tables.
949 Status
= MeasureHandoffTables ();
950 if (EFI_ERROR (Status
)) {
951 DEBUG ((EFI_D_ERROR
, "HOBs not Measured. Error!\n"));
955 // Measure BootOrder & Boot#### variables.
957 Status
= MeasureAllBootVariables ();
958 if (EFI_ERROR (Status
)) {
959 DEBUG ((EFI_D_ERROR
, "Boot Variables not Measured. Error!\n"));
963 // 1. This is the first boot attempt.
965 Status
= TcgMeasureAction (
966 EFI_CALLING_EFI_APPLICATION
968 ASSERT_EFI_ERROR (Status
);
971 // 2. Draw a line between pre-boot env and entering post-boot env.
973 for (PcrIndex
= 0; PcrIndex
< 8; PcrIndex
++) {
974 Status
= MeasureSeparatorEvent (PcrIndex
);
975 ASSERT_EFI_ERROR (Status
);
979 // 3. Measure GPT. It would be done in SAP driver.
983 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
987 // 5. Read & Measure variable. BootOrder already measured.
991 // 6. Not first attempt, meaning a return from last attempt
993 Status
= TcgMeasureAction (
994 EFI_RETURNING_FROM_EFI_APPLICATOIN
996 ASSERT_EFI_ERROR (Status
);
999 DEBUG ((EFI_D_INFO
, "TPM TcgDxe Measure Data when ReadyToBoot\n"));
1001 // Increase boot attempt counter.
1007 Install TCG ACPI Table when ACPI Table Protocol is available.
1009 A system's firmware uses an ACPI table to identify the system's TCG capabilities
1010 to the Post-Boot environment. The information in this ACPI table is not guaranteed
1011 to be valid until the Host Platform transitions from pre-boot state to post-boot state.
1013 @param[in] Event Event whose notification function is being invoked
1014 @param[in] Context Pointer to the notification function's context
1025 EFI_ACPI_TABLE_PROTOCOL
*AcpiTable
;
1028 Status
= gBS
->LocateProtocol (&gEfiAcpiTableProtocolGuid
, NULL
, (VOID
**)&AcpiTable
);
1029 if (EFI_ERROR (Status
)) {
1033 if (PcdGet8 (PcdTpmPlatformClass
) == TCG_PLATFORM_TYPE_CLIENT
) {
1036 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1037 // service of the ACPI table protocol to install it.
1039 Checksum
= CalculateCheckSum8 ((UINT8
*)&mTcgClientAcpiTemplate
, sizeof (mTcgClientAcpiTemplate
));
1040 mTcgClientAcpiTemplate
.Header
.Checksum
= Checksum
;
1042 Status
= AcpiTable
->InstallAcpiTable (
1044 &mTcgClientAcpiTemplate
,
1045 sizeof (mTcgClientAcpiTemplate
),
1051 // The ACPI table must be checksumed before calling the InstallAcpiTable()
1052 // service of the ACPI table protocol to install it.
1054 Checksum
= CalculateCheckSum8 ((UINT8
*)&mTcgServerAcpiTemplate
, sizeof (mTcgServerAcpiTemplate
));
1055 mTcgServerAcpiTemplate
.Header
.Checksum
= Checksum
;
1057 Status
= AcpiTable
->InstallAcpiTable (
1059 &mTcgServerAcpiTemplate
,
1060 sizeof (mTcgServerAcpiTemplate
),
1064 ASSERT_EFI_ERROR (Status
);
1068 Exit Boot Services Event notification handler.
1070 Measure invocation and success of ExitBootServices.
1072 @param[in] Event Event whose notification function is being invoked
1073 @param[in] Context Pointer to the notification function's context
1078 OnExitBootServices (
1086 // Measure invocation of ExitBootServices,
1088 Status
= TcgMeasureAction (
1089 EFI_EXIT_BOOT_SERVICES_INVOCATION
1091 ASSERT_EFI_ERROR (Status
);
1094 // Measure success of ExitBootServices
1096 Status
= TcgMeasureAction (
1097 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
1099 ASSERT_EFI_ERROR (Status
);
1103 Get TPM Deactivated state.
1105 @param[out] TPMDeactivatedFlag Returns TPM Deactivated state.
1107 @retval EFI_SUCCESS Operation completed successfully.
1108 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1113 OUT BOOLEAN
*TPMDeactivatedFlag
1117 TPM_STCLEAR_FLAGS VFlags
;
1119 Status
= TpmCommGetFlags (
1120 mTcgDxeData
.TpmHandle
,
1121 TPM_CAP_FLAG_VOLATILE
,
1125 if (!EFI_ERROR (Status
)) {
1126 *TPMDeactivatedFlag
= VFlags
.deactivated
;
1133 The driver's entry point.
1135 It publishes EFI TCG Protocol.
1137 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1138 @param[in] SystemTable A pointer to the EFI System Table.
1140 @retval EFI_SUCCESS The entry point is executed successfully.
1141 @retval other Some error occurs when executing this entry point.
1147 IN EFI_HANDLE ImageHandle
,
1148 IN EFI_SYSTEM_TABLE
*SystemTable
1155 mTcgDxeData
.TpmHandle
= (TIS_TPM_HANDLE
)(UINTN
)TPM_BASE_ADDRESS
;
1156 Status
= TisPcRequestUseTpm (mTcgDxeData
.TpmHandle
);
1157 if (EFI_ERROR (Status
)) {
1158 DEBUG ((EFI_D_ERROR
, "TPM not detected!\n"));
1162 Status
= GetTpmStatus (&mTcgDxeData
.BsCap
.TPMDeactivatedFlag
);
1163 if (EFI_ERROR (Status
)) {
1166 "Line %d in file " __FILE__
":\n "
1167 "DriverEntry: TPM not working properly\n",
1173 Status
= gBS
->InstallProtocolInterface (
1175 &gEfiTcgProtocolGuid
,
1176 EFI_NATIVE_INTERFACE
,
1177 &mTcgDxeData
.TcgProtocol
1180 // Install ACPI Table
1182 EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid
, TPL_CALLBACK
, InstallAcpiTable
, NULL
, &Registration
);
1184 if (!EFI_ERROR (Status
) && !mTcgDxeData
.BsCap
.TPMDeactivatedFlag
) {
1186 // Setup the log area and copy event log from hob list to it
1188 Status
= SetupEventLog ();
1189 ASSERT_EFI_ERROR (Status
);
1192 // Measure handoff tables, Boot#### variables etc.
1194 Status
= EfiCreateEventReadyToBootEx (
1201 Status
= gBS
->CreateEventEx (
1206 &gEfiEventExitBootServicesGuid
,