2 Initialize TPM2 device and measure FVs before handing off control to DXE.
4 Copyright (c) 2013 - 2015, 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/UefiTcgPlatform.h>
18 #include <Ppi/FirmwareVolumeInfo.h>
19 #include <Ppi/FirmwareVolumeInfo2.h>
20 #include <Ppi/LockPhysicalPresence.h>
21 #include <Ppi/TpmInitialized.h>
22 #include <Ppi/FirmwareVolume.h>
23 #include <Ppi/EndOfPeiPhase.h>
24 #include <Ppi/FirmwareVolumeInfoMeasurementExcluded.h>
26 #include <Guid/TcgEventHob.h>
27 #include <Guid/MeasuredFvHob.h>
28 #include <Guid/TpmInstance.h>
30 #include <Library/DebugLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/PeiServicesLib.h>
33 #include <Library/PeimEntryPoint.h>
34 #include <Library/Tpm2CommandLib.h>
35 #include <Library/Tpm2DeviceLib.h>
36 #include <Library/HashLib.h>
37 #include <Library/HobLib.h>
38 #include <Library/PcdLib.h>
39 #include <Library/PeiServicesTablePointerLib.h>
40 #include <Protocol/TrEEProtocol.h>
41 #include <Library/PerformanceLib.h>
42 #include <Library/MemoryAllocationLib.h>
43 #include <Library/ReportStatusCodeLib.h>
45 #define PERF_ID_TREE_PEI 0x3080
49 TREE_EVENT_LOG_FORMAT LogFormat
;
50 } TREE_EVENT_INFO_STRUCT
;
52 TREE_EVENT_INFO_STRUCT mTreeEventInfo
[] = {
53 {&gTcgEventEntryHobGuid
, TREE_EVENT_LOG_FORMAT_TCG_1_2
},
56 BOOLEAN mImageInMemory
= FALSE
;
57 EFI_PEI_FILE_HANDLE mFileHandle
;
59 EFI_PEI_PPI_DESCRIPTOR mTpmInitializedPpiList
= {
60 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
61 &gPeiTpmInitializedPpiGuid
,
65 EFI_PLATFORM_FIRMWARE_BLOB
*mMeasuredBaseFvInfo
;
66 UINT32 mMeasuredBaseFvIndex
= 0;
68 EFI_PLATFORM_FIRMWARE_BLOB
*mMeasuredChildFvInfo
;
69 UINT32 mMeasuredChildFvIndex
= 0;
72 Measure and record the Firmware Volum Information once FvInfoPPI install.
74 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
75 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
76 @param[in] Ppi Address of the PPI that was installed.
78 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
79 @return Others Fail to measure FV.
84 FirmwareVolmeInfoPpiNotifyCallback (
85 IN EFI_PEI_SERVICES
**PeiServices
,
86 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
91 Record all measured Firmware Volum Information into a Guid Hob
93 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
94 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
95 @param[in] Ppi Address of the PPI that was installed.
97 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
98 @return Others Fail to measure FV.
103 EndofPeiSignalNotifyCallBack (
104 IN EFI_PEI_SERVICES
**PeiServices
,
105 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
109 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList
[] = {
111 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
,
112 &gEfiPeiFirmwareVolumeInfoPpiGuid
,
113 FirmwareVolmeInfoPpiNotifyCallback
116 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
,
117 &gEfiPeiFirmwareVolumeInfo2PpiGuid
,
118 FirmwareVolmeInfoPpiNotifyCallback
121 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
122 &gEfiEndOfPeiSignalPpiGuid
,
123 EndofPeiSignalNotifyCallBack
127 EFI_PEI_FIRMWARE_VOLUME_INFO_MEASUREMENT_EXCLUDED_PPI
*mMeasurementExcludedFvPpi
;
130 This function get digest from digest list.
132 @param HashAlg digest algorithm
133 @param DigestList digest list
136 @retval EFI_SUCCESS Sha1Digest is found and returned.
137 @retval EFI_NOT_FOUND Sha1Digest is not found.
140 Tpm2GetDigestFromDigestList (
141 IN TPMI_ALG_HASH HashAlg
,
142 IN TPML_DIGEST_VALUES
*DigestList
,
149 DigestSize
= GetHashSizeFromAlgo (HashAlg
);
150 for (Index
= 0; Index
< DigestList
->count
; Index
++) {
151 if (DigestList
->digests
[Index
].hashAlg
== HashAlg
) {
154 &DigestList
->digests
[Index
].digest
,
161 return EFI_NOT_FOUND
;
165 Record all measured Firmware Volum Information into a Guid Hob
166 Guid Hob payload layout is
168 UINT32 *************************** FIRMWARE_BLOB number
169 EFI_PLATFORM_FIRMWARE_BLOB******** BLOB Array
171 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
172 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
173 @param[in] Ppi Address of the PPI that was installed.
175 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
176 @return Others Fail to measure FV.
181 EndofPeiSignalNotifyCallBack (
182 IN EFI_PEI_SERVICES
**PeiServices
,
183 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
187 MEASURED_HOB_DATA
*MeasuredHobData
;
189 MeasuredHobData
= NULL
;
192 // Create a Guid hob to save all measured Fv
194 MeasuredHobData
= BuildGuidHob(
196 sizeof(UINTN
) + sizeof(EFI_PLATFORM_FIRMWARE_BLOB
) * (mMeasuredBaseFvIndex
+ mMeasuredChildFvIndex
)
199 if (MeasuredHobData
!= NULL
){
201 // Save measured FV info enty number
203 MeasuredHobData
->Num
= mMeasuredBaseFvIndex
+ mMeasuredChildFvIndex
;
206 // Save measured base Fv info
208 CopyMem (MeasuredHobData
->MeasuredFvBuf
, mMeasuredBaseFvInfo
, sizeof(EFI_PLATFORM_FIRMWARE_BLOB
) * (mMeasuredBaseFvIndex
));
211 // Save measured child Fv info
213 CopyMem (&MeasuredHobData
->MeasuredFvBuf
[mMeasuredBaseFvIndex
] , mMeasuredChildFvInfo
, sizeof(EFI_PLATFORM_FIRMWARE_BLOB
) * (mMeasuredChildFvIndex
));
220 Add a new entry to the Event Log.
222 @param[in] DigestList A list of digest.
223 @param[in,out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
224 @param[in] NewEventData Pointer to the new event data.
226 @retval EFI_SUCCESS The new event log entry was added.
227 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
231 IN TPML_DIGEST_VALUES
*DigestList
,
232 IN OUT TCG_PCR_EVENT_HDR
*NewEventHdr
,
233 IN UINT8
*NewEventData
239 EFI_STATUS RetStatus
;
241 RetStatus
= EFI_SUCCESS
;
242 for (Index
= 0; Index
< sizeof(mTreeEventInfo
)/sizeof(mTreeEventInfo
[0]); Index
++) {
243 DEBUG ((EFI_D_INFO
, " LogFormat - 0x%08x\n", mTreeEventInfo
[Index
].LogFormat
));
244 switch (mTreeEventInfo
[Index
].LogFormat
) {
245 case TREE_EVENT_LOG_FORMAT_TCG_1_2
:
246 Status
= Tpm2GetDigestFromDigestList (TPM_ALG_SHA1
, DigestList
, &NewEventHdr
->Digest
);
247 if (!EFI_ERROR (Status
)) {
248 HobData
= BuildGuidHob (
249 &gTcgEventEntryHobGuid
,
250 sizeof (*NewEventHdr
) + NewEventHdr
->EventSize
252 if (HobData
== NULL
) {
253 RetStatus
= EFI_OUT_OF_RESOURCES
;
257 CopyMem (HobData
, NewEventHdr
, sizeof (*NewEventHdr
));
258 HobData
= (VOID
*) ((UINT8
*)HobData
+ sizeof (*NewEventHdr
));
259 CopyMem (HobData
, NewEventData
, NewEventHdr
->EventSize
);
269 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
270 and build a GUIDed HOB recording the event which will be passed to the DXE phase and
271 added into the Event Log.
273 @param[in] Flags Bitmap providing additional information.
274 @param[in] HashData Physical address of the start of the data buffer
275 to be hashed, extended, and logged.
276 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData.
277 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
278 @param[in] NewEventData Pointer to the new event data.
280 @retval EFI_SUCCESS Operation completed successfully.
281 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
282 @retval EFI_DEVICE_ERROR The command was unsuccessful.
289 IN UINTN HashDataLen
,
290 IN TCG_PCR_EVENT_HDR
*NewEventHdr
,
291 IN UINT8
*NewEventData
295 TPML_DIGEST_VALUES DigestList
;
297 if (GetFirstGuidHob (&gTpmErrorHobGuid
) != NULL
) {
298 return EFI_DEVICE_ERROR
;
301 Status
= HashAndExtend (
302 NewEventHdr
->PCRIndex
,
307 if (!EFI_ERROR (Status
)) {
308 if ((Flags
& TREE_EXTEND_ONLY
) == 0) {
309 Status
= LogHashEvent (&DigestList
, NewEventHdr
, NewEventData
);
313 if (Status
== EFI_DEVICE_ERROR
) {
314 DEBUG ((EFI_D_ERROR
, "HashLogExtendEvent - %r. Disable TPM.\n", Status
));
315 BuildGuidHob (&gTpmErrorHobGuid
,0);
317 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
318 (PcdGet32 (PcdStatusCodeSubClassTpmDevice
) | EFI_P_EC_INTERFACE_ERROR
)
326 Measure CRTM version.
328 @retval EFI_SUCCESS Operation completed successfully.
329 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
330 @retval EFI_DEVICE_ERROR The command was unsuccessful.
338 TCG_PCR_EVENT_HDR TcgEventHdr
;
341 // Use FirmwareVersion string to represent CRTM version.
342 // OEMs should get real CRTM version string and measure it.
345 TcgEventHdr
.PCRIndex
= 0;
346 TcgEventHdr
.EventType
= EV_S_CRTM_VERSION
;
347 TcgEventHdr
.EventSize
= (UINT32
) StrSize((CHAR16
*)PcdGetPtr (PcdFirmwareVersionString
));
349 return HashLogExtendEvent (
351 (UINT8
*)PcdGetPtr (PcdFirmwareVersionString
),
352 TcgEventHdr
.EventSize
,
354 (UINT8
*)PcdGetPtr (PcdFirmwareVersionString
)
360 Add it into the measured FV list after the FV is measured successfully.
362 @param[in] FvBase Base address of FV image.
363 @param[in] FvLength Length of FV image.
365 @retval EFI_SUCCESS Fv image is measured successfully
366 or it has been already measured.
367 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
368 @retval EFI_DEVICE_ERROR The command was unsuccessful.
373 IN EFI_PHYSICAL_ADDRESS FvBase
,
379 EFI_PLATFORM_FIRMWARE_BLOB FvBlob
;
380 TCG_PCR_EVENT_HDR TcgEventHdr
;
383 // Check if it is in Excluded FV list
385 if (mMeasurementExcludedFvPpi
!= NULL
) {
386 for (Index
= 0; Index
< mMeasurementExcludedFvPpi
->Count
; Index
++) {
387 if (mMeasurementExcludedFvPpi
->Fv
[Index
].FvBase
== FvBase
) {
388 DEBUG ((DEBUG_INFO
, "The FV which is excluded by TrEEPei starts at: 0x%x\n", FvBase
));
389 DEBUG ((DEBUG_INFO
, "The FV which is excluded by TrEEPei has the size: 0x%x\n", FvLength
));
396 // Check whether FV is in the measured FV list.
398 for (Index
= 0; Index
< mMeasuredBaseFvIndex
; Index
++) {
399 if (mMeasuredBaseFvInfo
[Index
].BlobBase
== FvBase
) {
405 // Measure and record the FV to the TPM
407 FvBlob
.BlobBase
= FvBase
;
408 FvBlob
.BlobLength
= FvLength
;
410 DEBUG ((DEBUG_INFO
, "The FV which is measured by TrEEPei starts at: 0x%x\n", FvBlob
.BlobBase
));
411 DEBUG ((DEBUG_INFO
, "The FV which is measured by TrEEPei has the size: 0x%x\n", FvBlob
.BlobLength
));
413 TcgEventHdr
.PCRIndex
= 0;
414 TcgEventHdr
.EventType
= EV_EFI_PLATFORM_FIRMWARE_BLOB
;
415 TcgEventHdr
.EventSize
= sizeof (FvBlob
);
417 Status
= HashLogExtendEvent (
419 (UINT8
*) (UINTN
) FvBlob
.BlobBase
,
420 (UINTN
) FvBlob
.BlobLength
,
426 // Add new FV into the measured FV list.
428 ASSERT (mMeasuredBaseFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
));
429 if (mMeasuredBaseFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)) {
430 mMeasuredBaseFvInfo
[mMeasuredBaseFvIndex
].BlobBase
= FvBase
;
431 mMeasuredBaseFvInfo
[mMeasuredBaseFvIndex
].BlobLength
= FvLength
;
432 mMeasuredBaseFvIndex
++;
441 @retval EFI_SUCCESS Operation completed successfully.
442 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
443 @retval EFI_DEVICE_ERROR The command was unsuccessful.
453 EFI_PEI_FV_HANDLE VolumeHandle
;
454 EFI_FV_INFO VolumeInfo
;
455 EFI_PEI_FIRMWARE_VOLUME_PPI
*FvPpi
;
457 PERF_START_EX (mFileHandle
, "EventRec", "TrEEPei", 0, PERF_ID_TREE_PEI
);
461 // Traverse all firmware volume instances of Static Core Root of Trust for Measurement
462 // (S-CRTM), this firmware volume measure policy can be modified/enhanced by special
463 // platform for special CRTM TPM measuring.
465 Status
= PeiServicesFfsFindNextVolume (FvInstances
, &VolumeHandle
);
466 if (EFI_ERROR (Status
)) {
471 // Measure and record the firmware volume that is dispatched by PeiCore
473 Status
= PeiServicesFfsGetVolumeInfo (VolumeHandle
, &VolumeInfo
);
474 ASSERT_EFI_ERROR (Status
);
476 // Locate the corresponding FV_PPI according to founded FV's format guid
478 Status
= PeiServicesLocatePpi (
479 &VolumeInfo
.FvFormat
,
484 if (!EFI_ERROR (Status
)) {
485 MeasureFvImage ((EFI_PHYSICAL_ADDRESS
) (UINTN
) VolumeInfo
.FvStart
, VolumeInfo
.FvSize
);
490 PERF_END_EX (mFileHandle
, "EventRec", "TrEEPei", 0, PERF_ID_TREE_PEI
+ 1);
496 Measure and record the Firmware Volum Information once FvInfoPPI install.
498 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
499 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
500 @param[in] Ppi Address of the PPI that was installed.
502 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
503 @return Others Fail to measure FV.
508 FirmwareVolmeInfoPpiNotifyCallback (
509 IN EFI_PEI_SERVICES
**PeiServices
,
510 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
514 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI
*Fv
;
516 EFI_PEI_FIRMWARE_VOLUME_PPI
*FvPpi
;
519 Fv
= (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI
*) Ppi
;
522 // The PEI Core can not dispatch or load files from memory mapped FVs that do not support FvPpi.
524 Status
= PeiServicesLocatePpi (
530 if (EFI_ERROR (Status
)) {
535 // This is an FV from an FFS file, and the parent FV must have already been measured,
536 // No need to measure twice, so just record the FV and return
538 if (Fv
->ParentFvName
!= NULL
|| Fv
->ParentFileName
!= NULL
) {
540 ASSERT (mMeasuredChildFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
));
541 if (mMeasuredChildFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)) {
543 // Check whether FV is in the measured child FV list.
545 for (Index
= 0; Index
< mMeasuredChildFvIndex
; Index
++) {
546 if (mMeasuredChildFvInfo
[Index
].BlobBase
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Fv
->FvInfo
) {
550 mMeasuredChildFvInfo
[mMeasuredChildFvIndex
].BlobBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Fv
->FvInfo
;
551 mMeasuredChildFvInfo
[mMeasuredChildFvIndex
].BlobLength
= Fv
->FvInfoSize
;
552 mMeasuredChildFvIndex
++;
557 return MeasureFvImage ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Fv
->FvInfo
, Fv
->FvInfoSize
);
561 Do measurement after memory is ready.
563 @param[in] PeiServices Describes the list of possible PEI Services.
565 @retval EFI_SUCCESS Operation completed successfully.
566 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
567 @retval EFI_DEVICE_ERROR The command was unsuccessful.
572 IN EFI_PEI_SERVICES
**PeiServices
577 Status
= PeiServicesLocatePpi (
578 &gEfiPeiFirmwareVolumeInfoMeasurementExcludedPpiGuid
,
581 (VOID
**)&mMeasurementExcludedFvPpi
583 // Do not check status, because it is optional
585 mMeasuredBaseFvInfo
= (EFI_PLATFORM_FIRMWARE_BLOB
*) AllocateZeroPool (sizeof (EFI_PLATFORM_FIRMWARE_BLOB
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
586 ASSERT (mMeasuredBaseFvInfo
!= NULL
);
587 mMeasuredChildFvInfo
= (EFI_PLATFORM_FIRMWARE_BLOB
*) AllocateZeroPool (sizeof (EFI_PLATFORM_FIRMWARE_BLOB
) * PcdGet32 (PcdPeiCoreMaxFvSupported
));
588 ASSERT (mMeasuredChildFvInfo
!= NULL
);
590 if (PcdGet8 (PcdTpm2ScrtmPolicy
) == 1) {
591 Status
= MeasureCRTMVersion ();
594 Status
= MeasureMainBios ();
598 // for the FvInfoPpi services to measure and record
599 // the additional Fvs to TPM
601 Status
= PeiServicesNotifyPpi (&mNotifyList
[0]);
602 ASSERT_EFI_ERROR (Status
);
608 Entry point of this module.
610 @param[in] FileHandle Handle of the file being invoked.
611 @param[in] PeiServices Describes the list of possible PEI Services.
619 IN EFI_PEI_FILE_HANDLE FileHandle
,
620 IN CONST EFI_PEI_SERVICES
**PeiServices
624 EFI_BOOT_MODE BootMode
;
626 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &gEfiTpmDeviceInstanceNoneGuid
) ||
627 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid
), &gEfiTpmDeviceInstanceTpm12Guid
)){
628 DEBUG ((EFI_D_ERROR
, "No TPM2 instance required!\n"));
629 return EFI_UNSUPPORTED
;
633 // Update for Performance optimization
635 Status
= Tpm2RequestUseTpm ();
636 if (EFI_ERROR (Status
)) {
637 DEBUG ((DEBUG_ERROR
, "TPM not detected!\n"));
641 Status
= PeiServicesGetBootMode (&BootMode
);
642 ASSERT_EFI_ERROR (Status
);
645 // In S3 path, skip shadow logic. no measurement is required
647 if (BootMode
!= BOOT_ON_S3_RESUME
) {
648 Status
= (**PeiServices
).RegisterForShadow(FileHandle
);
649 if (Status
== EFI_ALREADY_STARTED
) {
650 mImageInMemory
= TRUE
;
651 mFileHandle
= FileHandle
;
652 } else if (Status
== EFI_NOT_FOUND
) {
653 ASSERT_EFI_ERROR (Status
);
657 if (!mImageInMemory
) {
659 // Initialize TPM device
661 if (PcdGet8 (PcdTpm2InitializationPolicy
) == 1) {
662 if (BootMode
== BOOT_ON_S3_RESUME
) {
663 Status
= Tpm2Startup (TPM_SU_STATE
);
664 if (EFI_ERROR (Status
) ) {
665 Status
= Tpm2Startup (TPM_SU_CLEAR
);
668 Status
= Tpm2Startup (TPM_SU_CLEAR
);
670 if (EFI_ERROR (Status
) ) {
676 // TpmSelfTest is optional on S3 path, skip it to save S3 time
678 if (BootMode
!= BOOT_ON_S3_RESUME
) {
679 if (PcdGet8 (PcdTpm2SelfTestPolicy
) == 1) {
680 Status
= Tpm2SelfTest (NO
);
681 if (EFI_ERROR (Status
)) {
687 Status
= PeiServicesInstallPpi (&mTpmInitializedPpiList
);
688 ASSERT_EFI_ERROR (Status
);
691 if (mImageInMemory
) {
692 Status
= PeimEntryMP ((EFI_PEI_SERVICES
**)PeiServices
);
693 if (EFI_ERROR (Status
)) {