123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855 |
- /** @file
- Initialize TPM device and measure FVs before handing off control to DXE.
- Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
- **/
- #include <PiPei.h>
- #include <IndustryStandard/Tpm12.h>
- #include <IndustryStandard/UefiTcgPlatform.h>
- #include <Ppi/FirmwareVolumeInfo.h>
- #include <Ppi/FirmwareVolumeInfo2.h>
- #include <Ppi/LockPhysicalPresence.h>
- #include <Ppi/TpmInitialized.h>
- #include <Ppi/FirmwareVolume.h>
- #include <Ppi/EndOfPeiPhase.h>
- #include <Ppi/FirmwareVolumeInfoMeasurementExcluded.h>
- #include <Guid/TcgEventHob.h>
- #include <Guid/MeasuredFvHob.h>
- #include <Guid/TpmInstance.h>
- #include <Library/DebugLib.h>
- #include <Library/BaseMemoryLib.h>
- #include <Library/PeiServicesLib.h>
- #include <Library/PeimEntryPoint.h>
- #include <Library/HobLib.h>
- #include <Library/PcdLib.h>
- #include <Library/PeiServicesTablePointerLib.h>
- #include <Library/BaseLib.h>
- #include <Library/MemoryAllocationLib.h>
- #include <Library/ReportStatusCodeLib.h>
- #include <Library/Tpm12DeviceLib.h>
- #include <Library/Tpm12CommandLib.h>
- #include <Library/BaseCryptLib.h>
- #include <Library/PerformanceLib.h>
- BOOLEAN mImageInMemory = FALSE;
- EFI_PEI_PPI_DESCRIPTOR mTpmInitializedPpiList = {
- EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
- &gPeiTpmInitializedPpiGuid,
- NULL
- };
- EFI_PEI_PPI_DESCRIPTOR mTpmInitializationDonePpiList = {
- EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
- &gPeiTpmInitializationDonePpiGuid,
- NULL
- };
- //
- // Number of firmware blobs to grow by each time we run out of room
- //
- #define FIRMWARE_BLOB_GROWTH_STEP 4
- EFI_PLATFORM_FIRMWARE_BLOB *mMeasuredBaseFvInfo;
- UINT32 mMeasuredMaxBaseFvIndex = 0;
- UINT32 mMeasuredBaseFvIndex = 0;
- EFI_PLATFORM_FIRMWARE_BLOB *mMeasuredChildFvInfo;
- UINT32 mMeasuredMaxChildFvIndex = 0;
- UINT32 mMeasuredChildFvIndex = 0;
- EFI_PEI_FIRMWARE_VOLUME_INFO_MEASUREMENT_EXCLUDED_PPI *mMeasurementExcludedFvPpi;
- /**
- Lock physical presence if needed.
- @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
- @param[in] NotifyDescriptor Address of the notification descriptor data structure.
- @param[in] Ppi Address of the PPI that was installed.
- @retval EFI_SUCCESS Operation completed successfully.
- **/
- EFI_STATUS
- EFIAPI
- PhysicalPresencePpiNotifyCallback (
- IN EFI_PEI_SERVICES **PeiServices,
- IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
- IN VOID *Ppi
- );
- /**
- Measure and record the Firmware Volume Information once FvInfoPPI install.
- @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
- @param[in] NotifyDescriptor Address of the notification descriptor data structure.
- @param[in] Ppi Address of the PPI that was installed.
- @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
- @return Others Fail to measure FV.
- **/
- EFI_STATUS
- EFIAPI
- FirmwareVolumeInfoPpiNotifyCallback (
- IN EFI_PEI_SERVICES **PeiServices,
- IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
- IN VOID *Ppi
- );
- /**
- Record all measured Firmware Volume Information into a Guid Hob
- @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
- @param[in] NotifyDescriptor Address of the notification descriptor data structure.
- @param[in] Ppi Address of the PPI that was installed.
- @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
- @return Others Fail to measure FV.
- **/
- EFI_STATUS
- EFIAPI
- EndofPeiSignalNotifyCallBack (
- IN EFI_PEI_SERVICES **PeiServices,
- IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
- IN VOID *Ppi
- );
- EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList[] = {
- {
- EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
- &gPeiLockPhysicalPresencePpiGuid,
- PhysicalPresencePpiNotifyCallback
- },
- {
- EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
- &gEfiPeiFirmwareVolumeInfoPpiGuid,
- FirmwareVolumeInfoPpiNotifyCallback
- },
- {
- EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
- &gEfiPeiFirmwareVolumeInfo2PpiGuid,
- FirmwareVolumeInfoPpiNotifyCallback
- },
- {
- (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
- &gEfiEndOfPeiSignalPpiGuid,
- EndofPeiSignalNotifyCallBack
- }
- };
- /**
- Record all measured Firmware Voluem Information into a Guid Hob
- Guid Hob payload layout is
- UINT32 *************************** FIRMWARE_BLOB number
- EFI_PLATFORM_FIRMWARE_BLOB******** BLOB Array
- @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
- @param[in] NotifyDescriptor Address of the notification descriptor data structure.
- @param[in] Ppi Address of the PPI that was installed.
- @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
- @return Others Fail to measure FV.
- **/
- EFI_STATUS
- EFIAPI
- EndofPeiSignalNotifyCallBack (
- IN EFI_PEI_SERVICES **PeiServices,
- IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
- IN VOID *Ppi
- )
- {
- MEASURED_HOB_DATA *MeasuredHobData;
- MeasuredHobData = NULL;
- PERF_CALLBACK_BEGIN (&gEfiEndOfPeiSignalPpiGuid);
- //
- // Create a Guid hob to save all measured Fv
- //
- MeasuredHobData = BuildGuidHob(
- &gMeasuredFvHobGuid,
- sizeof(UINTN) + sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredBaseFvIndex + mMeasuredChildFvIndex)
- );
- if (MeasuredHobData != NULL){
- //
- // Save measured FV info enty number
- //
- MeasuredHobData->Num = mMeasuredBaseFvIndex + mMeasuredChildFvIndex;
- //
- // Save measured base Fv info
- //
- CopyMem (MeasuredHobData->MeasuredFvBuf, mMeasuredBaseFvInfo, sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredBaseFvIndex));
- //
- // Save measured child Fv info
- //
- CopyMem (&MeasuredHobData->MeasuredFvBuf[mMeasuredBaseFvIndex] , mMeasuredChildFvInfo, sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredChildFvIndex));
- }
- PERF_CALLBACK_END (&gEfiEndOfPeiSignalPpiGuid);
- return EFI_SUCCESS;
- }
- /**
- Single function calculates SHA1 digest value for all raw data. It
- combines Sha1Init(), Sha1Update() and Sha1Final().
- @param[in] Data Raw data to be digested.
- @param[in] DataLen Size of the raw data.
- @param[out] Digest Pointer to a buffer that stores the final digest.
- @retval EFI_SUCCESS Always successfully calculate the final digest.
- **/
- EFI_STATUS
- EFIAPI
- TpmCommHashAll (
- IN CONST UINT8 *Data,
- IN UINTN DataLen,
- OUT TPM_DIGEST *Digest
- )
- {
- VOID *Sha1Ctx;
- UINTN CtxSize;
- CtxSize = Sha1GetContextSize ();
- Sha1Ctx = AllocatePool (CtxSize);
- ASSERT (Sha1Ctx != NULL);
- Sha1Init (Sha1Ctx);
- Sha1Update (Sha1Ctx, Data, DataLen);
- Sha1Final (Sha1Ctx, (UINT8 *)Digest);
- FreePool (Sha1Ctx);
- return EFI_SUCCESS;
- }
- /**
- Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
- and build a GUIDed HOB recording the event which will be passed to the DXE phase and
- added into the Event Log.
- @param[in] PeiServices Describes the list of possible PEI Services.
- @param[in] HashData Physical address of the start of the data buffer
- to be hashed, extended, and logged.
- @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData.
- @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
- @param[in] NewEventData Pointer to the new event data.
- @retval EFI_SUCCESS Operation completed successfully.
- @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
- @retval EFI_DEVICE_ERROR The command was unsuccessful.
- **/
- EFI_STATUS
- HashLogExtendEvent (
- IN EFI_PEI_SERVICES **PeiServices,
- IN UINT8 *HashData,
- IN UINTN HashDataLen,
- IN TCG_PCR_EVENT_HDR *NewEventHdr,
- IN UINT8 *NewEventData
- )
- {
- EFI_STATUS Status;
- VOID *HobData;
- if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {
- return EFI_DEVICE_ERROR;
- }
- HobData = NULL;
- if (HashDataLen != 0) {
- Status = TpmCommHashAll (
- HashData,
- HashDataLen,
- &NewEventHdr->Digest
- );
- if (EFI_ERROR (Status)) {
- goto Done;
- }
- }
- Status = Tpm12Extend (
- &NewEventHdr->Digest,
- NewEventHdr->PCRIndex,
- NULL
- );
- if (EFI_ERROR (Status)) {
- goto Done;
- }
- HobData = BuildGuidHob (
- &gTcgEventEntryHobGuid,
- sizeof (*NewEventHdr) + NewEventHdr->EventSize
- );
- if (HobData == NULL) {
- Status = EFI_OUT_OF_RESOURCES;
- goto Done;
- }
- CopyMem (HobData, NewEventHdr, sizeof (*NewEventHdr));
- HobData = (VOID *) ((UINT8*)HobData + sizeof (*NewEventHdr));
- CopyMem (HobData, NewEventData, NewEventHdr->EventSize);
- Done:
- if ((Status == EFI_DEVICE_ERROR) || (Status == EFI_TIMEOUT)) {
- DEBUG ((EFI_D_ERROR, "HashLogExtendEvent - %r. Disable TPM.\n", Status));
- BuildGuidHob (&gTpmErrorHobGuid,0);
- REPORT_STATUS_CODE (
- EFI_ERROR_CODE | EFI_ERROR_MINOR,
- (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
- );
- Status = EFI_DEVICE_ERROR;
- }
- return Status;
- }
- /**
- Measure CRTM version.
- @param[in] PeiServices Describes the list of possible PEI Services.
- @retval EFI_SUCCESS Operation completed successfully.
- @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
- @retval EFI_DEVICE_ERROR The command was unsuccessful.
- **/
- EFI_STATUS
- EFIAPI
- MeasureCRTMVersion (
- IN EFI_PEI_SERVICES **PeiServices
- )
- {
- TCG_PCR_EVENT_HDR TcgEventHdr;
- //
- // Use FirmwareVersion string to represent CRTM version.
- // OEMs should get real CRTM version string and measure it.
- //
- TcgEventHdr.PCRIndex = 0;
- TcgEventHdr.EventType = EV_S_CRTM_VERSION;
- TcgEventHdr.EventSize = (UINT32) StrSize((CHAR16*)PcdGetPtr (PcdFirmwareVersionString));
- return HashLogExtendEvent (
- PeiServices,
- (UINT8*)PcdGetPtr (PcdFirmwareVersionString),
- TcgEventHdr.EventSize,
- &TcgEventHdr,
- (UINT8*)PcdGetPtr (PcdFirmwareVersionString)
- );
- }
- /**
- Measure FV image.
- Add it into the measured FV list after the FV is measured successfully.
- @param[in] FvBase Base address of FV image.
- @param[in] FvLength Length of FV image.
- @retval EFI_SUCCESS Fv image is measured successfully
- or it has been already measured.
- @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
- @retval EFI_DEVICE_ERROR The command was unsuccessful.
- **/
- EFI_STATUS
- EFIAPI
- MeasureFvImage (
- IN EFI_PHYSICAL_ADDRESS FvBase,
- IN UINT64 FvLength
- )
- {
- UINT32 Index;
- EFI_STATUS Status;
- EFI_PLATFORM_FIRMWARE_BLOB FvBlob;
- TCG_PCR_EVENT_HDR TcgEventHdr;
- //
- // Check if it is in Excluded FV list
- //
- if (mMeasurementExcludedFvPpi != NULL) {
- for (Index = 0; Index < mMeasurementExcludedFvPpi->Count; Index ++) {
- if (mMeasurementExcludedFvPpi->Fv[Index].FvBase == FvBase) {
- DEBUG ((DEBUG_INFO, "The FV which is excluded by TcgPei starts at: 0x%x\n", FvBase));
- DEBUG ((DEBUG_INFO, "The FV which is excluded by TcgPei has the size: 0x%x\n", FvLength));
- return EFI_SUCCESS;
- }
- }
- }
- //
- // Check whether FV is in the measured FV list.
- //
- for (Index = 0; Index < mMeasuredBaseFvIndex; Index ++) {
- if (mMeasuredBaseFvInfo[Index].BlobBase == FvBase) {
- return EFI_SUCCESS;
- }
- }
- //
- // Measure and record the FV to the TPM
- //
- FvBlob.BlobBase = FvBase;
- FvBlob.BlobLength = FvLength;
- DEBUG ((DEBUG_INFO, "The FV which is measured by TcgPei starts at: 0x%x\n", FvBlob.BlobBase));
- DEBUG ((DEBUG_INFO, "The FV which is measured by TcgPei has the size: 0x%x\n", FvBlob.BlobLength));
- TcgEventHdr.PCRIndex = 0;
- TcgEventHdr.EventType = EV_EFI_PLATFORM_FIRMWARE_BLOB;
- TcgEventHdr.EventSize = sizeof (FvBlob);
- Status = HashLogExtendEvent (
- (EFI_PEI_SERVICES **) GetPeiServicesTablePointer(),
- (UINT8*) (UINTN) FvBlob.BlobBase,
- (UINTN) FvBlob.BlobLength,
- &TcgEventHdr,
- (UINT8*) &FvBlob
- );
- //
- // Add new FV into the measured FV list.
- //
- if (mMeasuredBaseFvIndex >= mMeasuredMaxBaseFvIndex) {
- mMeasuredBaseFvInfo = ReallocatePool (
- sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * mMeasuredMaxBaseFvIndex,
- sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredMaxBaseFvIndex + FIRMWARE_BLOB_GROWTH_STEP),
- mMeasuredBaseFvInfo
- );
- ASSERT (mMeasuredBaseFvInfo != NULL);
- mMeasuredMaxBaseFvIndex = mMeasuredMaxBaseFvIndex + FIRMWARE_BLOB_GROWTH_STEP;
- }
- mMeasuredBaseFvInfo[mMeasuredBaseFvIndex].BlobBase = FvBase;
- mMeasuredBaseFvInfo[mMeasuredBaseFvIndex].BlobLength = FvLength;
- mMeasuredBaseFvIndex++;
- return Status;
- }
- /**
- Measure main BIOS.
- @param[in] PeiServices Describes the list of possible PEI Services.
- @retval EFI_SUCCESS Operation completed successfully.
- @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
- @retval EFI_DEVICE_ERROR The command was unsuccessful.
- **/
- EFI_STATUS
- EFIAPI
- MeasureMainBios (
- IN EFI_PEI_SERVICES **PeiServices
- )
- {
- EFI_STATUS Status;
- UINT32 FvInstances;
- EFI_PEI_FV_HANDLE VolumeHandle;
- EFI_FV_INFO VolumeInfo;
- EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
- FvInstances = 0;
- while (TRUE) {
- //
- // Traverse all firmware volume instances of Static Core Root of Trust for Measurement
- // (S-CRTM), this firmware volume measure policy can be modified/enhanced by special
- // platform for special CRTM TPM measuring.
- //
- Status = PeiServicesFfsFindNextVolume (FvInstances, &VolumeHandle);
- if (EFI_ERROR (Status)) {
- break;
- }
- //
- // Measure and record the firmware volume that is dispatched by PeiCore
- //
- Status = PeiServicesFfsGetVolumeInfo (VolumeHandle, &VolumeInfo);
- ASSERT_EFI_ERROR (Status);
- //
- // Locate the corresponding FV_PPI according to founded FV's format guid
- //
- Status = PeiServicesLocatePpi (
- &VolumeInfo.FvFormat,
- 0,
- NULL,
- (VOID**)&FvPpi
- );
- if (!EFI_ERROR (Status)) {
- MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) VolumeInfo.FvStart, VolumeInfo.FvSize);
- }
- FvInstances++;
- }
- return EFI_SUCCESS;
- }
- /**
- Measure and record the Firmware Voluem Information once FvInfoPPI install.
- @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
- @param[in] NotifyDescriptor Address of the notification descriptor data structure.
- @param[in] Ppi Address of the PPI that was installed.
- @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
- @return Others Fail to measure FV.
- **/
- EFI_STATUS
- EFIAPI
- FirmwareVolumeInfoPpiNotifyCallback (
- IN EFI_PEI_SERVICES **PeiServices,
- IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
- IN VOID *Ppi
- )
- {
- EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *Fv;
- EFI_STATUS Status;
- EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
- UINTN Index;
- Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *) Ppi;
- //
- // The PEI Core can not dispatch or load files from memory mapped FVs that do not support FvPpi.
- //
- Status = PeiServicesLocatePpi (
- &Fv->FvFormat,
- 0,
- NULL,
- (VOID**)&FvPpi
- );
- if (EFI_ERROR (Status)) {
- return EFI_SUCCESS;
- }
- //
- // This is an FV from an FFS file, and the parent FV must have already been measured,
- // No need to measure twice, so just record the FV and return
- //
- if (Fv->ParentFvName != NULL || Fv->ParentFileName != NULL ) {
- if (mMeasuredChildFvIndex >= mMeasuredMaxChildFvIndex) {
- mMeasuredChildFvInfo = ReallocatePool (
- sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * mMeasuredMaxChildFvIndex,
- sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredMaxChildFvIndex + FIRMWARE_BLOB_GROWTH_STEP),
- mMeasuredChildFvInfo
- );
- ASSERT (mMeasuredChildFvInfo != NULL);
- mMeasuredMaxChildFvIndex = mMeasuredMaxChildFvIndex + FIRMWARE_BLOB_GROWTH_STEP;
- }
- //
- // Check whether FV is in the measured child FV list.
- //
- for (Index = 0; Index < mMeasuredChildFvIndex; Index++) {
- if (mMeasuredChildFvInfo[Index].BlobBase == (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo) {
- return EFI_SUCCESS;
- }
- }
- mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobBase = (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo;
- mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobLength = Fv->FvInfoSize;
- mMeasuredChildFvIndex++;
- return EFI_SUCCESS;
- }
- return MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo, Fv->FvInfoSize);
- }
- /**
- Set physicalPresenceLifetimeLock, physicalPresenceHWEnable and physicalPresenceCMDEnable bit by corresponding PCDs.
- And lock physical presence if needed.
- @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
- @param[in] NotifyDescriptor Address of the notification descriptor data structure.
- @param[in] Ppi Address of the PPI that was installed.
- @retval EFI_SUCCESS Operation completed successfully.
- @retval EFI_ABORTED physicalPresenceCMDEnable is locked.
- @retval EFI_DEVICE_ERROR The command was unsuccessful.
- **/
- EFI_STATUS
- EFIAPI
- PhysicalPresencePpiNotifyCallback (
- IN EFI_PEI_SERVICES **PeiServices,
- IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
- IN VOID *Ppi
- )
- {
- EFI_STATUS Status;
- TPM_PERMANENT_FLAGS TpmPermanentFlags;
- PEI_LOCK_PHYSICAL_PRESENCE_PPI *LockPhysicalPresencePpi;
- TPM_PHYSICAL_PRESENCE PhysicalPresenceValue;
- Status = Tpm12GetCapabilityFlagPermanent (&TpmPermanentFlags);
- if (EFI_ERROR (Status)) {
- return Status;
- }
- //
- // 1. Set physicalPresenceLifetimeLock, physicalPresenceHWEnable and physicalPresenceCMDEnable bit by PCDs.
- //
- if (PcdGetBool (PcdPhysicalPresenceLifetimeLock) && !TpmPermanentFlags.physicalPresenceLifetimeLock) {
- //
- // Lock TPM LifetimeLock is required, and LifetimeLock is not locked yet.
- //
- PhysicalPresenceValue = TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK;
- TpmPermanentFlags.physicalPresenceLifetimeLock = TRUE;
- if (PcdGetBool (PcdPhysicalPresenceCmdEnable)) {
- PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_CMD_ENABLE;
- TpmPermanentFlags.physicalPresenceCMDEnable = TRUE;
- } else {
- PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_CMD_DISABLE;
- TpmPermanentFlags.physicalPresenceCMDEnable = FALSE;
- }
- if (PcdGetBool (PcdPhysicalPresenceHwEnable)) {
- PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_HW_ENABLE;
- } else {
- PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_HW_DISABLE;
- }
- Status = Tpm12PhysicalPresence (
- PhysicalPresenceValue
- );
- if (EFI_ERROR (Status)) {
- return Status;
- }
- }
- //
- // 2. Lock physical presence if it is required.
- //
- LockPhysicalPresencePpi = (PEI_LOCK_PHYSICAL_PRESENCE_PPI *) Ppi;
- if (!LockPhysicalPresencePpi->LockPhysicalPresence ((CONST EFI_PEI_SERVICES**) PeiServices)) {
- return EFI_SUCCESS;
- }
- if (!TpmPermanentFlags.physicalPresenceCMDEnable) {
- if (TpmPermanentFlags.physicalPresenceLifetimeLock) {
- //
- // physicalPresenceCMDEnable is locked, can't change.
- //
- return EFI_ABORTED;
- }
- //
- // Enable physical presence command
- // It is necessary in order to lock physical presence
- //
- Status = Tpm12PhysicalPresence (
- TPM_PHYSICAL_PRESENCE_CMD_ENABLE
- );
- if (EFI_ERROR (Status)) {
- return Status;
- }
- }
- //
- // Lock physical presence
- //
- Status = Tpm12PhysicalPresence (
- TPM_PHYSICAL_PRESENCE_LOCK
- );
- return Status;
- }
- /**
- Check if TPM chip is activated or not.
- @param[in] PeiServices Describes the list of possible PEI Services.
- @retval TRUE TPM is activated.
- @retval FALSE TPM is deactivated.
- **/
- BOOLEAN
- IsTpmUsable (
- VOID
- )
- {
- EFI_STATUS Status;
- TPM_PERMANENT_FLAGS TpmPermanentFlags;
- Status = Tpm12GetCapabilityFlagPermanent (&TpmPermanentFlags);
- if (EFI_ERROR (Status)) {
- return FALSE;
- }
- return (BOOLEAN)(!TpmPermanentFlags.deactivated);
- }
- /**
- Do measurement after memory is ready.
- @param[in] PeiServices Describes the list of possible PEI Services.
- @retval EFI_SUCCESS Operation completed successfully.
- @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
- @retval EFI_DEVICE_ERROR The command was unsuccessful.
- **/
- EFI_STATUS
- EFIAPI
- PeimEntryMP (
- IN EFI_PEI_SERVICES **PeiServices
- )
- {
- EFI_STATUS Status;
- Status = PeiServicesLocatePpi (
- &gEfiPeiFirmwareVolumeInfoMeasurementExcludedPpiGuid,
- 0,
- NULL,
- (VOID**)&mMeasurementExcludedFvPpi
- );
- // Do not check status, because it is optional
- Status = Tpm12RequestUseTpm ();
- if (EFI_ERROR (Status)) {
- return Status;
- }
- if (IsTpmUsable ()) {
- if (PcdGet8 (PcdTpmScrtmPolicy) == 1) {
- Status = MeasureCRTMVersion (PeiServices);
- }
- Status = MeasureMainBios (PeiServices);
- }
- //
- // Post callbacks:
- // 1). for the FvInfoPpi services to measure and record
- // the additional Fvs to TPM
- // 2). for the OperatorPresencePpi service to determine whether to
- // lock the TPM
- //
- Status = PeiServicesNotifyPpi (&mNotifyList[0]);
- ASSERT_EFI_ERROR (Status);
- return Status;
- }
- /**
- Entry point of this module.
- @param[in] FileHandle Handle of the file being invoked.
- @param[in] PeiServices Describes the list of possible PEI Services.
- @return Status.
- **/
- EFI_STATUS
- EFIAPI
- PeimEntryMA (
- IN EFI_PEI_FILE_HANDLE FileHandle,
- IN CONST EFI_PEI_SERVICES **PeiServices
- )
- {
- EFI_STATUS Status;
- EFI_STATUS Status2;
- EFI_BOOT_MODE BootMode;
- if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){
- DEBUG ((EFI_D_ERROR, "No TPM12 instance required!\n"));
- return EFI_UNSUPPORTED;
- }
- if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {
- DEBUG ((EFI_D_ERROR, "TPM error!\n"));
- return EFI_DEVICE_ERROR;
- }
- //
- // Initialize TPM device
- //
- Status = PeiServicesGetBootMode (&BootMode);
- ASSERT_EFI_ERROR (Status);
- //
- // In S3 path, skip shadow logic. no measurement is required
- //
- if (BootMode != BOOT_ON_S3_RESUME) {
- Status = (**PeiServices).RegisterForShadow(FileHandle);
- if (Status == EFI_ALREADY_STARTED) {
- mImageInMemory = TRUE;
- } else if (Status == EFI_NOT_FOUND) {
- ASSERT_EFI_ERROR (Status);
- }
- }
- if (!mImageInMemory) {
- Status = Tpm12RequestUseTpm ();
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "TPM not detected!\n"));
- goto Done;
- }
- if (PcdGet8 (PcdTpmInitializationPolicy) == 1) {
- if (BootMode == BOOT_ON_S3_RESUME) {
- Status = Tpm12Startup (TPM_ST_STATE);
- } else {
- Status = Tpm12Startup (TPM_ST_CLEAR);
- }
- if (EFI_ERROR (Status) ) {
- goto Done;
- }
- }
- //
- // TpmSelfTest is optional on S3 path, skip it to save S3 time
- //
- if (BootMode != BOOT_ON_S3_RESUME) {
- Status = Tpm12ContinueSelfTest ();
- if (EFI_ERROR (Status)) {
- goto Done;
- }
- }
- //
- // Only intall TpmInitializedPpi on success
- //
- Status = PeiServicesInstallPpi (&mTpmInitializedPpiList);
- ASSERT_EFI_ERROR (Status);
- }
- if (mImageInMemory) {
- Status = PeimEntryMP ((EFI_PEI_SERVICES**)PeiServices);
- return Status;
- }
- Done:
- if (EFI_ERROR (Status)) {
- DEBUG ((EFI_D_ERROR, "TPM error! Build Hob\n"));
- BuildGuidHob (&gTpmErrorHobGuid,0);
- REPORT_STATUS_CODE (
- EFI_ERROR_CODE | EFI_ERROR_MINOR,
- (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
- );
- }
- //
- // Always intall TpmInitializationDonePpi no matter success or fail.
- // Other driver can know TPM initialization state by TpmInitializedPpi.
- //
- Status2 = PeiServicesInstallPpi (&mTpmInitializationDonePpiList);
- ASSERT_EFI_ERROR (Status2);
- return Status;
- }
|