2 Initialize TPM device and measure FVs before handing off control to DXE.
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.
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 <Guid/TcgEventHob.h>
24 #include <Library/DebugLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/PeiServicesLib.h>
27 #include <Library/PeimEntryPoint.h>
28 #include <Library/TpmCommLib.h>
29 #include <Library/HobLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/PeiServicesTablePointerLib.h>
35 BOOLEAN mImageInMemory
= FALSE
;
37 EFI_PEI_PPI_DESCRIPTOR mTpmInitializedPpiList
= {
38 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
39 &gPeiTpmInitializedPpiGuid
,
44 Lock physical presence if needed.
46 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
47 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
48 @param[in] Ppi Address of the PPI that was installed.
50 @retval EFI_SUCCESS Operation completed successfully.
55 PhysicalPresencePpiNotifyCallback (
56 IN EFI_PEI_SERVICES
**PeiServices
,
57 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
62 Measure and record the Firmware Volum Information once FvInfoPPI install.
64 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
65 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
66 @param[in] Ppi Address of the PPI that was installed.
68 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
69 @return Others Fail to measure FV.
74 FirmwareVolmeInfoPpiNotifyCallback (
75 IN EFI_PEI_SERVICES
**PeiServices
,
76 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
80 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList
[] = {
82 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
,
83 &gPeiLockPhysicalPresencePpiGuid
,
84 PhysicalPresencePpiNotifyCallback
87 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
88 &gEfiPeiFirmwareVolumeInfoPpiGuid
,
89 FirmwareVolmeInfoPpiNotifyCallback
93 CHAR8 mSCrtmVersion
[] = "{D20BC7C6-A1A5-415c-AE85-38290AB6BE04}";
95 EFI_PLATFORM_FIRMWARE_BLOB mMeasuredFvInfo
[FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)];
96 UINT32 mMeasuredFvIndex
= 0;
99 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
100 and build a GUIDed HOB recording the event which will be passed to the DXE phase and
101 added into the Event Log.
103 @param[in] PeiServices Describes the list of possible PEI Services.
104 @param[in] HashData Physical address of the start of the data buffer
105 to be hashed, extended, and logged.
106 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData.
107 @param[in] TpmHandle TPM handle.
108 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
109 @param[in] NewEventData Pointer to the new event data.
111 @retval EFI_SUCCESS Operation completed successfully.
112 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
113 @retval EFI_DEVICE_ERROR The command was unsuccessful.
118 IN EFI_PEI_SERVICES
**PeiServices
,
120 IN UINTN HashDataLen
,
121 IN TIS_TPM_HANDLE TpmHandle
,
122 IN TCG_PCR_EVENT_HDR
*NewEventHdr
,
123 IN UINT8
*NewEventData
130 if (HashDataLen
!= 0) {
131 Status
= TpmCommHashAll (
136 ASSERT_EFI_ERROR (Status
);
139 Status
= TpmCommExtend (
142 &NewEventHdr
->Digest
,
143 NewEventHdr
->PCRIndex
,
146 ASSERT_EFI_ERROR (Status
);
148 HobData
= BuildGuidHob (
149 &gTcgEventEntryHobGuid
,
150 sizeof (*NewEventHdr
) + NewEventHdr
->EventSize
152 if (HobData
== NULL
) {
153 return EFI_OUT_OF_RESOURCES
;
156 CopyMem (HobData
, NewEventHdr
, sizeof (*NewEventHdr
));
157 HobData
= (VOID
*) ((UINT8
*)HobData
+ sizeof (*NewEventHdr
));
158 CopyMem (HobData
, NewEventData
, NewEventHdr
->EventSize
);
163 Measure CRTM version.
165 @param[in] PeiServices Describes the list of possible PEI Services.
166 @param[in] TpmHandle TPM handle.
168 @retval EFI_SUCCESS Operation completed successfully.
169 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
170 @retval EFI_DEVICE_ERROR The command was unsuccessful.
176 IN EFI_PEI_SERVICES
**PeiServices
,
177 IN TIS_TPM_HANDLE TpmHandle
180 TCG_PCR_EVENT_HDR TcgEventHdr
;
183 // Here, only a static GUID is measured instead of real CRTM version.
184 // OEMs should get real CRTM version string and measure it.
187 TcgEventHdr
.PCRIndex
= 0;
188 TcgEventHdr
.EventType
= EV_S_CRTM_VERSION
;
189 TcgEventHdr
.EventSize
= sizeof (mSCrtmVersion
);
190 return HashLogExtendEvent (
192 (UINT8
*)&mSCrtmVersion
,
193 TcgEventHdr
.EventSize
,
196 (UINT8
*)&mSCrtmVersion
202 Add it into the measured FV list after the FV is measured successfully.
204 @param[in] FvBase Base address of FV image.
205 @param[in] FvLength Length of FV image.
207 @retval EFI_SUCCESS Fv image is measured successfully
208 or it has been already measured.
209 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
210 @retval EFI_DEVICE_ERROR The command was unsuccessful.
216 IN EFI_PHYSICAL_ADDRESS FvBase
,
222 EFI_PLATFORM_FIRMWARE_BLOB FvBlob
;
223 TCG_PCR_EVENT_HDR TcgEventHdr
;
224 TIS_TPM_HANDLE TpmHandle
;
226 TpmHandle
= (TIS_TPM_HANDLE
) (UINTN
) TPM_BASE_ADDRESS
;
229 // Check whether FV is in the measured FV list.
231 for (Index
= 0; Index
< mMeasuredFvIndex
; Index
++) {
232 if (mMeasuredFvInfo
[Index
].BlobBase
== FvBase
) {
238 // Measure and record the FV to the TPM
240 FvBlob
.BlobBase
= FvBase
;
241 FvBlob
.BlobLength
= FvLength
;
243 DEBUG ((DEBUG_INFO
, "The FV which is measured by TcgPei starts at: 0x%x\n", FvBlob
.BlobBase
));
244 DEBUG ((DEBUG_INFO
, "The FV which is measured by TcgPei has the size: 0x%x\n", FvBlob
.BlobLength
));
246 TcgEventHdr
.PCRIndex
= 0;
247 TcgEventHdr
.EventType
= EV_EFI_PLATFORM_FIRMWARE_BLOB
;
248 TcgEventHdr
.EventSize
= sizeof (FvBlob
);
250 Status
= HashLogExtendEvent (
251 (EFI_PEI_SERVICES
**) GetPeiServicesTablePointer(),
252 (UINT8
*) (UINTN
) FvBlob
.BlobBase
,
253 (UINTN
) FvBlob
.BlobLength
,
258 ASSERT_EFI_ERROR (Status
);
261 // Add new FV into the measured FV list.
263 ASSERT (mMeasuredFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
));
264 if (mMeasuredFvIndex
< FixedPcdGet32 (PcdPeiCoreMaxFvSupported
)) {
265 mMeasuredFvInfo
[mMeasuredFvIndex
].BlobBase
= FvBase
;
266 mMeasuredFvInfo
[mMeasuredFvIndex
++].BlobLength
= FvLength
;
275 @param[in] PeiServices Describes the list of possible PEI Services.
276 @param[in] TpmHandle TPM handle.
278 @retval EFI_SUCCESS Operation completed successfully.
279 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
280 @retval EFI_DEVICE_ERROR The command was unsuccessful.
286 IN EFI_PEI_SERVICES
**PeiServices
,
287 IN TIS_TPM_HANDLE TpmHandle
292 EFI_PEI_FV_HANDLE VolumeHandle
;
293 EFI_FV_INFO VolumeInfo
;
294 EFI_PEI_FIRMWARE_VOLUME_PPI
*FvPpi
;
299 // Traverse all firmware volume instances of Static Core Root of Trust for Measurement
300 // (S-CRTM), this firmware volume measure policy can be modified/enhanced by special
301 // platform for special CRTM TPM measuring.
303 Status
= PeiServicesFfsFindNextVolume (FvInstances
, &VolumeHandle
);
304 if (EFI_ERROR (Status
)) {
309 // Measure and record the firmware volume that is dispatched by PeiCore
311 Status
= PeiServicesFfsGetVolumeInfo (VolumeHandle
, &VolumeInfo
);
312 ASSERT_EFI_ERROR (Status
);
314 // Locate the corresponding FV_PPI according to founded FV's format guid
316 Status
= PeiServicesLocatePpi (
317 &VolumeInfo
.FvFormat
,
322 if (!EFI_ERROR (Status
)) {
323 MeasureFvImage ((EFI_PHYSICAL_ADDRESS
) (UINTN
) VolumeInfo
.FvStart
, VolumeInfo
.FvSize
);
333 Measure and record the Firmware Volum Information once FvInfoPPI install.
335 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
336 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
337 @param[in] Ppi Address of the PPI that was installed.
339 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
340 @return Others Fail to measure FV.
345 FirmwareVolmeInfoPpiNotifyCallback (
346 IN EFI_PEI_SERVICES
**PeiServices
,
347 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
351 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI
*Fv
;
353 EFI_PEI_FIRMWARE_VOLUME_PPI
*FvPpi
;
355 Fv
= (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI
*) Ppi
;
358 // The PEI Core can not dispatch or load files from memory mapped FVs that do not support FvPpi.
360 Status
= PeiServicesLocatePpi (
366 if (EFI_ERROR (Status
)) {
371 // This is an FV from an FFS file, and the parent FV must have already been measured,
372 // No need to measure twice, so just returns
374 if (Fv
->ParentFvName
!= NULL
|| Fv
->ParentFileName
!= NULL
) {
378 return MeasureFvImage ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Fv
->FvInfo
, Fv
->FvInfoSize
);
382 Lock physical presence if needed.
384 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
385 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
386 @param[in] Ppi Address of the PPI that was installed.
388 @retval EFI_SUCCESS Operation completed successfully.
389 @retval EFI_ABORTED physicalPresenceCMDEnable is locked.
390 @retval EFI_DEVICE_ERROR The command was unsuccessful.
395 PhysicalPresencePpiNotifyCallback (
396 IN EFI_PEI_SERVICES
**PeiServices
,
397 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
402 PEI_LOCK_PHYSICAL_PRESENCE_PPI
*LockPhysicalPresencePpi
;
403 BOOLEAN LifetimeLock
;
405 TIS_TPM_HANDLE TpmHandle
;
407 TpmHandle
= (TIS_TPM_HANDLE
) (UINTN
) TPM_BASE_ADDRESS
;
408 LockPhysicalPresencePpi
= (PEI_LOCK_PHYSICAL_PRESENCE_PPI
*) Ppi
;
410 if (!LockPhysicalPresencePpi
->LockPhysicalPresence ((CONST EFI_PEI_SERVICES
**) PeiServices
)) {
415 // Lock TPM physical presence.
418 Status
= TpmCommGetCapability (PeiServices
, TpmHandle
, NULL
, &LifetimeLock
, &CmdEnable
);
419 if (EFI_ERROR (Status
)) {
426 // physicalPresenceCMDEnable is locked, can't change.
432 // Enable physical presence command
433 // It is necessary in order to lock physical presence
435 Status
= TpmCommPhysicalPresence (
438 TPM_PHYSICAL_PRESENCE_CMD_ENABLE
440 if (EFI_ERROR (Status
)) {
446 // Lock physical presence
448 Status
= TpmCommPhysicalPresence (
451 TPM_PHYSICAL_PRESENCE_LOCK
457 Check if TPM chip is activeated or not.
459 @param[in] PeiServices Describes the list of possible PEI Services.
460 @param[in] TpmHandle TPM handle.
462 @retval TRUE TPM is activated.
463 @retval FALSE TPM is deactivated.
469 IN EFI_PEI_SERVICES
**PeiServices
,
470 IN TIS_TPM_HANDLE TpmHandle
476 Status
= TpmCommGetCapability (PeiServices
, TpmHandle
, &Deactivated
, NULL
, NULL
);
477 if (EFI_ERROR (Status
)) {
480 return (BOOLEAN
)(!Deactivated
);
484 Do measurement after memory is ready.
486 @param[in] PeiServices Describes the list of possible PEI Services.
488 @retval EFI_SUCCESS Operation completed successfully.
489 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
490 @retval EFI_DEVICE_ERROR The command was unsuccessful.
496 IN EFI_PEI_SERVICES
**PeiServices
500 TIS_TPM_HANDLE TpmHandle
;
502 TpmHandle
= (TIS_TPM_HANDLE
)(UINTN
)TPM_BASE_ADDRESS
;
503 Status
= TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
)TpmHandle
);
504 if (EFI_ERROR (Status
)) {
508 if (IsTpmUsable (PeiServices
, TpmHandle
)) {
509 Status
= MeasureCRTMVersion (PeiServices
, TpmHandle
);
510 ASSERT_EFI_ERROR (Status
);
512 Status
= MeasureMainBios (PeiServices
, TpmHandle
);
517 // 1). for the FvInfoPpi services to measure and record
518 // the additional Fvs to TPM
519 // 2). for the OperatorPresencePpi service to determine whether to
522 Status
= PeiServicesNotifyPpi (&mNotifyList
[0]);
523 ASSERT_EFI_ERROR (Status
);
529 Entry point of this module.
531 @param[in] FileHandle Handle of the file being invoked.
532 @param[in] PeiServices Describes the list of possible PEI Services.
540 IN EFI_PEI_FILE_HANDLE FileHandle
,
541 IN CONST EFI_PEI_SERVICES
**PeiServices
545 EFI_BOOT_MODE BootMode
;
546 TIS_TPM_HANDLE TpmHandle
;
548 if (PcdGetBool (PcdHideTpmSupport
) && PcdGetBool (PcdHideTpm
)) {
549 return EFI_UNSUPPORTED
;
552 Status
= (**PeiServices
).RegisterForShadow(FileHandle
);
553 if (Status
== EFI_ALREADY_STARTED
) {
554 mImageInMemory
= TRUE
;
555 } else if (Status
== EFI_NOT_FOUND
) {
556 ASSERT_EFI_ERROR (Status
);
559 if (!mImageInMemory
) {
561 // Initialize TPM device
563 Status
= PeiServicesGetBootMode (&BootMode
);
564 ASSERT_EFI_ERROR (Status
);
566 TpmHandle
= (TIS_TPM_HANDLE
)(UINTN
)TPM_BASE_ADDRESS
;
567 Status
= TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
)TpmHandle
);
568 if (EFI_ERROR (Status
)) {
569 DEBUG ((DEBUG_ERROR
, "TPM not detected!\n"));
573 Status
= TpmCommStartup ((EFI_PEI_SERVICES
**)PeiServices
, TpmHandle
, BootMode
);
574 if (EFI_ERROR (Status
) ) {
577 Status
= TpmCommContinueSelfTest ((EFI_PEI_SERVICES
**)PeiServices
, TpmHandle
);
578 if (EFI_ERROR (Status
)) {
581 Status
= PeiServicesInstallPpi (&mTpmInitializedPpiList
);
582 ASSERT_EFI_ERROR (Status
);
585 if (mImageInMemory
) {
586 Status
= PeimEntryMP ((EFI_PEI_SERVICES
**)PeiServices
);
587 if (EFI_ERROR (Status
)) {