2 Initialize TPM device and measure FVs before handing off control to DXE.
4 Copyright (c) 2005 - 2012, 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.
17 #include <IndustryStandard/Tpm12.h>
18 #include <IndustryStandard/UefiTcgPlatform.h>
19 #include <Ppi/FirmwareVolumeInfo.h>
20 #include <Ppi/LockPhysicalPresence.h>
21 #include <Ppi/TpmInitialized.h>
22 #include <Ppi/FirmwareVolume.h>
23 #include <Ppi/EndOfPeiPhase.h>
25 #include <Guid/TcgEventHob.h>
26 #include <Guid/TrustedFvHob.h>
28 #include <Library/DebugLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/PeiServicesLib.h>
31 #include <Library/PeimEntryPoint.h>
32 #include <Library/TpmCommLib.h>
33 #include <Library/HobLib.h>
34 #include <Library/PcdLib.h>
35 #include <Library/PeiServicesTablePointerLib.h>
36 #include <Library/BaseLib.h>
40 BOOLEAN mImageInMemory
= FALSE
;
42 EFI_PEI_PPI_DESCRIPTOR mTpmInitializedPpiList
= {
43 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
44 &gPeiTpmInitializedPpiGuid
,
48 EFI_PLATFORM_FIRMWARE_BLOB mMeasuredBaseFvInfo
[FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)];
49 UINT32 mMeasuredBaseFvIndex
= 0;
51 EFI_PLATFORM_FIRMWARE_BLOB mMeasuredChildFvInfo
[FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)];
52 UINT32 mMeasuredChildFvIndex
= 0;
55 Lock physical presence if needed.
57 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
58 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
59 @param[in] Ppi Address of the PPI that was installed.
61 @retval EFI_SUCCESS Operation completed successfully.
66 PhysicalPresencePpiNotifyCallback (
67 IN EFI_PEI_SERVICES
**PeiServices
,
68 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
73 Measure and record the Firmware Volum Information once FvInfoPPI install.
75 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
76 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
77 @param[in] Ppi Address of the PPI that was installed.
79 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
80 @return Others Fail to measure FV.
85 FirmwareVolmeInfoPpiNotifyCallback (
86 IN EFI_PEI_SERVICES
**PeiServices
,
87 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
92 Record all measured Firmware Volum Information into a Guid Hob
94 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
95 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
96 @param[in] Ppi Address of the PPI that was installed.
98 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
99 @return Others Fail to measure FV.
104 EndofPeiSignalNotifyCallBack (
105 IN EFI_PEI_SERVICES
**PeiServices
,
106 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
110 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList
[] = {
112 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
,
113 &gPeiLockPhysicalPresencePpiGuid
,
114 PhysicalPresencePpiNotifyCallback
117 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
,
118 &gEfiPeiFirmwareVolumeInfoPpiGuid
,
119 FirmwareVolmeInfoPpiNotifyCallback
122 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
123 &gEfiEndOfPeiSignalPpiGuid
,
124 EndofPeiSignalNotifyCallBack
129 Record all measured Firmware Volum Information into a Guid Hob
130 Guid Hob payload layout is
132 UINT32 *************************** FIRMWARE_BLOB number
133 EFI_PLATFORM_FIRMWARE_BLOB******** BLOB Array
135 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
136 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
137 @param[in] Ppi Address of the PPI that was installed.
139 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
140 @return Others Fail to measure FV.
145 EndofPeiSignalNotifyCallBack (
146 IN EFI_PEI_SERVICES
**PeiServices
,
147 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
156 // Create a Guid hob to save all trusted Fv
158 HobData
= BuildGuidHob(
160 sizeof(UINTN
) + sizeof(EFI_PLATFORM_FIRMWARE_BLOB
) * (mMeasuredBaseFvIndex
+ mMeasuredChildFvIndex
)
163 if (HobData
!= NULL
){
165 // Save measured FV info enty number
167 *(UINT32
*)HobData
= mMeasuredBaseFvIndex
+ mMeasuredChildFvIndex
;
169 HobData
+= sizeof(UINT32
);
171 // Save measured base Fv info
173 CopyMem (HobData
, mMeasuredBaseFvInfo
, sizeof(EFI_PLATFORM_FIRMWARE_BLOB
) * (mMeasuredBaseFvIndex
));
175 HobData
+= sizeof(EFI_PLATFORM_FIRMWARE_BLOB
) * (mMeasuredBaseFvIndex
);
177 // Save measured child Fv info
179 CopyMem (HobData
, mMeasuredChildFvInfo
, sizeof(EFI_PLATFORM_FIRMWARE_BLOB
) * (mMeasuredChildFvIndex
));
186 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
187 and build a GUIDed HOB recording the event which will be passed to the DXE phase and
188 added into the Event Log.
190 @param[in] PeiServices Describes the list of possible PEI Services.
191 @param[in] HashData Physical address of the start of the data buffer
192 to be hashed, extended, and logged.
193 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData.
194 @param[in] TpmHandle TPM handle.
195 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
196 @param[in] NewEventData Pointer to the new event data.
198 @retval EFI_SUCCESS Operation completed successfully.
199 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
200 @retval EFI_DEVICE_ERROR The command was unsuccessful.
205 IN EFI_PEI_SERVICES
**PeiServices
,
207 IN UINTN HashDataLen
,
208 IN TIS_TPM_HANDLE TpmHandle
,
209 IN TCG_PCR_EVENT_HDR
*NewEventHdr
,
210 IN UINT8
*NewEventData
217 if (HashDataLen
!= 0) {
218 Status
= TpmCommHashAll (
223 ASSERT_EFI_ERROR (Status
);
226 Status
= TpmCommExtend (
229 &NewEventHdr
->Digest
,
230 NewEventHdr
->PCRIndex
,
233 ASSERT_EFI_ERROR (Status
);
235 HobData
= BuildGuidHob (
236 &gTcgEventEntryHobGuid
,
237 sizeof (*NewEventHdr
) + NewEventHdr
->EventSize
239 if (HobData
== NULL
) {
240 return EFI_OUT_OF_RESOURCES
;
243 CopyMem (HobData
, NewEventHdr
, sizeof (*NewEventHdr
));
244 HobData
= (VOID
*) ((UINT8
*)HobData
+ sizeof (*NewEventHdr
));
245 CopyMem (HobData
, NewEventData
, NewEventHdr
->EventSize
);
250 Measure CRTM version.
252 @param[in] PeiServices Describes the list of possible PEI Services.
253 @param[in] TpmHandle TPM handle.
255 @retval EFI_SUCCESS Operation completed successfully.
256 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
257 @retval EFI_DEVICE_ERROR The command was unsuccessful.
263 IN EFI_PEI_SERVICES
**PeiServices
,
264 IN TIS_TPM_HANDLE TpmHandle
267 TCG_PCR_EVENT_HDR TcgEventHdr
;
270 // Use FirmwareVersion string to represent CRTM version.
271 // OEMs should get real CRTM version string and measure it.
274 TcgEventHdr
.PCRIndex
= 0;
275 TcgEventHdr
.EventType
= EV_S_CRTM_VERSION
;
276 TcgEventHdr
.EventSize
= (UINT32
) StrSize((CHAR16
*)PcdGetPtr (PcdFirmwareVersionString
));
278 return HashLogExtendEvent (
280 (UINT8
*)PcdGetPtr (PcdFirmwareVersionString
),
281 TcgEventHdr
.EventSize
,
284 (UINT8
*)PcdGetPtr (PcdFirmwareVersionString
)
290 Add it into the measured FV list after the FV is measured successfully.
292 @param[in] FvBase Base address of FV image.
293 @param[in] FvLength Length of FV image.
295 @retval EFI_SUCCESS Fv image is measured successfully
296 or it has been already measured.
297 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
298 @retval EFI_DEVICE_ERROR The command was unsuccessful.
304 IN EFI_PHYSICAL_ADDRESS FvBase
,
310 EFI_PLATFORM_FIRMWARE_BLOB FvBlob
;
311 TCG_PCR_EVENT_HDR TcgEventHdr
;
312 TIS_TPM_HANDLE TpmHandle
;
314 TpmHandle
= (TIS_TPM_HANDLE
) (UINTN
) TPM_BASE_ADDRESS
;
317 // Check whether FV is in the measured FV list.
319 for (Index
= 0; Index
< mMeasuredBaseFvIndex
; Index
++) {
320 if (mMeasuredBaseFvInfo
[Index
].BlobBase
== FvBase
) {
326 // Measure and record the FV to the TPM
328 FvBlob
.BlobBase
= FvBase
;
329 FvBlob
.BlobLength
= FvLength
;
331 DEBUG ((DEBUG_INFO
, "The FV which is measured by TcgPei starts at: 0x%x\n", FvBlob
.BlobBase
));
332 DEBUG ((DEBUG_INFO
, "The FV which is measured by TcgPei has the size: 0x%x\n", FvBlob
.BlobLength
));
334 TcgEventHdr
.PCRIndex
= 0;
335 TcgEventHdr
.EventType
= EV_EFI_PLATFORM_FIRMWARE_BLOB
;
336 TcgEventHdr
.EventSize
= sizeof (FvBlob
);
338 Status
= HashLogExtendEvent (
339 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer(),
340 (UINT8
*) (UINTN
) FvBlob
.BlobBase
,
341 (UINTN
) FvBlob
.BlobLength
,
346 ASSERT_EFI_ERROR (Status
);
349 // Add new FV into the measured FV list.
351 ASSERT (mMeasuredBaseFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
));
352 if (mMeasuredBaseFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)) {
353 mMeasuredBaseFvInfo
[mMeasuredBaseFvIndex
].BlobBase
= FvBase
;
354 mMeasuredBaseFvInfo
[mMeasuredBaseFvIndex
].BlobLength
= FvLength
;
355 mMeasuredBaseFvIndex
++;
364 @param[in] PeiServices Describes the list of possible PEI Services.
365 @param[in] TpmHandle TPM handle.
367 @retval EFI_SUCCESS Operation completed successfully.
368 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
369 @retval EFI_DEVICE_ERROR The command was unsuccessful.
375 IN EFI_PEI_SERVICES
**PeiServices
,
376 IN TIS_TPM_HANDLE TpmHandle
381 EFI_PEI_FV_HANDLE VolumeHandle
;
382 EFI_FV_INFO VolumeInfo
;
383 EFI_PEI_FIRMWARE_VOLUME_PPI
*FvPpi
;
388 // Traverse all firmware volume instances of Static Core Root of Trust for Measurement
389 // (S-CRTM), this firmware volume measure policy can be modified/enhanced by special
390 // platform for special CRTM TPM measuring.
392 Status
= PeiServicesFfsFindNextVolume (FvInstances
, &VolumeHandle
);
393 if (EFI_ERROR (Status
)) {
398 // Measure and record the firmware volume that is dispatched by PeiCore
400 Status
= PeiServicesFfsGetVolumeInfo (VolumeHandle
, &VolumeInfo
);
401 ASSERT_EFI_ERROR (Status
);
403 // Locate the corresponding FV_PPI according to founded FV's format guid
405 Status
= PeiServicesLocatePpi (
406 &VolumeInfo
.FvFormat
,
411 if (!EFI_ERROR (Status
)) {
412 MeasureFvImage ((EFI_PHYSICAL_ADDRESS
) (UINTN
) VolumeInfo
.FvStart
, VolumeInfo
.FvSize
);
422 Measure and record the Firmware Volum Information once FvInfoPPI install.
424 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
425 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
426 @param[in] Ppi Address of the PPI that was installed.
428 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
429 @return Others Fail to measure FV.
434 FirmwareVolmeInfoPpiNotifyCallback (
435 IN EFI_PEI_SERVICES
**PeiServices
,
436 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
440 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI
*Fv
;
442 EFI_PEI_FIRMWARE_VOLUME_PPI
*FvPpi
;
444 Fv
= (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI
*) Ppi
;
447 // The PEI Core can not dispatch or load files from memory mapped FVs that do not support FvPpi.
449 Status
= PeiServicesLocatePpi (
455 if (EFI_ERROR (Status
)) {
460 // This is an FV from an FFS file, and the parent FV must have already been measured,
461 // No need to measure twice, so just record the FV and return
463 if (Fv
->ParentFvName
!= NULL
|| Fv
->ParentFileName
!= NULL
) {
465 ASSERT (mMeasuredChildFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
));
466 if (mMeasuredChildFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)) {
467 mMeasuredChildFvInfo
[mMeasuredChildFvIndex
].BlobBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Fv
->FvInfo
;
468 mMeasuredChildFvInfo
[mMeasuredChildFvIndex
].BlobLength
= Fv
->FvInfoSize
;
469 mMeasuredChildFvIndex
++;
474 return MeasureFvImage ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Fv
->FvInfo
, Fv
->FvInfoSize
);
478 Set physicalPresenceLifetimeLock, physicalPresenceHWEnable and physicalPresenceCMDEnable bit by corresponding PCDs.
479 And lock physical presence if needed.
481 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
482 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
483 @param[in] Ppi Address of the PPI that was installed.
485 @retval EFI_SUCCESS Operation completed successfully.
486 @retval EFI_ABORTED physicalPresenceCMDEnable is locked.
487 @retval EFI_DEVICE_ERROR The command was unsuccessful.
492 PhysicalPresencePpiNotifyCallback (
493 IN EFI_PEI_SERVICES
**PeiServices
,
494 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
499 PEI_LOCK_PHYSICAL_PRESENCE_PPI
*LockPhysicalPresencePpi
;
500 BOOLEAN LifetimeLock
;
502 TIS_TPM_HANDLE TpmHandle
;
503 TPM_PHYSICAL_PRESENCE PhysicalPresenceValue
;
505 TpmHandle
= (TIS_TPM_HANDLE
) (UINTN
) TPM_BASE_ADDRESS
;
507 Status
= TpmCommGetCapability (PeiServices
, TpmHandle
, NULL
, &LifetimeLock
, &CmdEnable
);
508 if (EFI_ERROR (Status
)) {
513 // 1. Set physicalPresenceLifetimeLock, physicalPresenceHWEnable and physicalPresenceCMDEnable bit by PCDs.
515 if (PcdGetBool (PcdPhysicalPresenceLifetimeLock
) && !LifetimeLock
) {
517 // Lock TPM LifetimeLock is required, and LifetimeLock is not locked yet.
519 PhysicalPresenceValue
= TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK
;
521 if (PcdGetBool (PcdPhysicalPresenceCmdEnable
)) {
522 PhysicalPresenceValue
|= TPM_PHYSICAL_PRESENCE_CMD_ENABLE
;
525 PhysicalPresenceValue
|= TPM_PHYSICAL_PRESENCE_CMD_DISABLE
;
529 if (PcdGetBool (PcdPhysicalPresenceHwEnable
)) {
530 PhysicalPresenceValue
|= TPM_PHYSICAL_PRESENCE_HW_ENABLE
;
532 PhysicalPresenceValue
|= TPM_PHYSICAL_PRESENCE_HW_DISABLE
;
535 Status
= TpmCommPhysicalPresence (
538 PhysicalPresenceValue
540 if (EFI_ERROR (Status
)) {
546 // 2. Lock physical presence if it is required.
548 LockPhysicalPresencePpi
= (PEI_LOCK_PHYSICAL_PRESENCE_PPI
*) Ppi
;
549 if (!LockPhysicalPresencePpi
->LockPhysicalPresence ((CONST EFI_PEI_SERVICES
**) PeiServices
)) {
556 // physicalPresenceCMDEnable is locked, can't change.
562 // Enable physical presence command
563 // It is necessary in order to lock physical presence
565 Status
= TpmCommPhysicalPresence (
568 TPM_PHYSICAL_PRESENCE_CMD_ENABLE
570 if (EFI_ERROR (Status
)) {
576 // Lock physical presence
578 Status
= TpmCommPhysicalPresence (
581 TPM_PHYSICAL_PRESENCE_LOCK
587 Check if TPM chip is activeated or not.
589 @param[in] PeiServices Describes the list of possible PEI Services.
590 @param[in] TpmHandle TPM handle.
592 @retval TRUE TPM is activated.
593 @retval FALSE TPM is deactivated.
599 IN EFI_PEI_SERVICES
**PeiServices
,
600 IN TIS_TPM_HANDLE TpmHandle
606 Status
= TpmCommGetCapability (PeiServices
, TpmHandle
, &Deactivated
, NULL
, NULL
);
607 if (EFI_ERROR (Status
)) {
610 return (BOOLEAN
)(!Deactivated
);
614 Do measurement after memory is ready.
616 @param[in] PeiServices Describes the list of possible PEI Services.
618 @retval EFI_SUCCESS Operation completed successfully.
619 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
620 @retval EFI_DEVICE_ERROR The command was unsuccessful.
626 IN EFI_PEI_SERVICES
**PeiServices
630 TIS_TPM_HANDLE TpmHandle
;
632 TpmHandle
= (TIS_TPM_HANDLE
)(UINTN
)TPM_BASE_ADDRESS
;
633 Status
= TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
)TpmHandle
);
634 if (EFI_ERROR (Status
)) {
638 if (IsTpmUsable (PeiServices
, TpmHandle
)) {
639 Status
= MeasureCRTMVersion (PeiServices
, TpmHandle
);
640 ASSERT_EFI_ERROR (Status
);
642 Status
= MeasureMainBios (PeiServices
, TpmHandle
);
647 // 1). for the FvInfoPpi services to measure and record
648 // the additional Fvs to TPM
649 // 2). for the OperatorPresencePpi service to determine whether to
652 Status
= PeiServicesNotifyPpi (&mNotifyList
[0]);
653 ASSERT_EFI_ERROR (Status
);
659 Entry point of this module.
661 @param[in] FileHandle Handle of the file being invoked.
662 @param[in] PeiServices Describes the list of possible PEI Services.
670 IN EFI_PEI_FILE_HANDLE FileHandle
,
671 IN CONST EFI_PEI_SERVICES
**PeiServices
675 EFI_BOOT_MODE BootMode
;
676 TIS_TPM_HANDLE TpmHandle
;
678 if (PcdGetBool (PcdHideTpmSupport
) && PcdGetBool (PcdHideTpm
)) {
679 return EFI_UNSUPPORTED
;
683 // Initialize TPM device
685 Status
= PeiServicesGetBootMode (&BootMode
);
686 ASSERT_EFI_ERROR (Status
);
689 // In S3 path, skip shadow logic. no measurement is required
691 if (BootMode
!= BOOT_ON_S3_RESUME
) {
692 Status
= (**PeiServices
).RegisterForShadow(FileHandle
);
693 if (Status
== EFI_ALREADY_STARTED
) {
694 mImageInMemory
= TRUE
;
695 } else if (Status
== EFI_NOT_FOUND
) {
696 ASSERT_EFI_ERROR (Status
);
700 if (!mImageInMemory
) {
701 TpmHandle
= (TIS_TPM_HANDLE
)(UINTN
)TPM_BASE_ADDRESS
;
702 Status
= TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
)TpmHandle
);
703 if (EFI_ERROR (Status
)) {
704 DEBUG ((DEBUG_ERROR
, "TPM not detected!\n"));
708 Status
= TpmCommStartup ((EFI_PEI_SERVICES
**)PeiServices
, TpmHandle
, BootMode
);
709 if (EFI_ERROR (Status
) ) {
712 Status
= TpmCommContinueSelfTest ((EFI_PEI_SERVICES
**)PeiServices
, TpmHandle
);
713 if (EFI_ERROR (Status
)) {
716 Status
= PeiServicesInstallPpi (&mTpmInitializedPpiList
);
717 ASSERT_EFI_ERROR (Status
);
720 if (mImageInMemory
) {
721 Status
= PeimEntryMP ((EFI_PEI_SERVICES
**)PeiServices
);
722 if (EFI_ERROR (Status
)) {