SecurityPkg/Library: separate logics geting TPM2 information with FF-A

This is preparation patch to add Tpm2DeviceSecLibFfa for SEC
used in PeilessSec.
In SEC phase, DynamicPcd used for cacahing TPM2 information
couldn't be used.
To resolve this, writes wrapper functions to get TPM2 information
so that in the wrapper functions used in SEC wouldn't use the
related DyanmicPcd.

Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
This commit is contained in:
Levi Yun 2025-07-22 14:33:08 +01:00 committed by mergify[bot]
parent ba6a8eb045
commit ee9950d3fb
6 changed files with 162 additions and 44 deletions

View file

@ -34,6 +34,9 @@
#ifndef TPM2_DEVICE_LIB_FFA_H_
#define TPM2_DEVICE_LIB_FFA_H_
#define TPM2_FFA_INTERFACE_TYPE_UNKNOWN 0xFF
#define TPM2_FFA_PARTITION_ID_INVALID 0x0000
/**
This function is used to get the TPM interface version.
@ -187,6 +190,22 @@ FfaTpm2RequestUseTpm (
VOID
);
/**
This function is used to get the TPM service partition id via FF-A
@param[out] PartitionId - Supplies the pointer to the TPM service partition id.
@retval EFI_SUCCESS The TPM command was successfully sent to the TPM
and the response was copied to the Output buffer.
@retval EFI_INVALID_PARAMETER The TPM command buffer is NULL or the TPM command
buffer size is 0.
@retval EFI_DEVICE_ERROR An error occurred in communication with the TPM.
**/
EFI_STATUS
FfaTpm2GetServicePartitionId (
OUT UINT16 *PartitionId
);
/**
Dump PTP register information.
@ -210,4 +229,35 @@ InternalTpm2DeviceLibFfaConstructor (
VOID
);
/**
This function validate TPM interface type for TPM service over FF-A.
@retval EFI_SUCCESS TPM interface type is valid.
@retval EFI_UNSUPPORTED TPM interface type is invalid.
**/
EFI_STATUS
EFIAPI
ValidateTpmInterfaceType (
VOID
);
/**
This function is used to get the TPM service partition id.
@param[out] PartitionId - Supplies the pointer to the TPM service partition id.
@retval EFI_SUCCESS The TPM command was successfully sent to the TPM
and the response was copied to the Output buffer.
@retval EFI_INVALID_PARAMETER The TPM command buffer is NULL or the TPM command
buffer size is 0.
@retval EFI_DEVICE_ERROR An error occurred in communication with the TPM.
**/
EFI_STATUS
EFIAPI
GetTpmServicePartitionId (
OUT UINT16 *PartitionId
);
#endif /* _TPM2_DEVICE_LIB_SMC_H_ */

View file

@ -29,6 +29,7 @@
Tpm2DeviceLibFfaBase.c
Tpm2Ptp.c
Tpm2DeviceLibFfa.h
Tpm2InfoFfa.c
[Packages]
MdePkg/MdePkg.dec

View file

@ -21,8 +21,7 @@
#include "Tpm2DeviceLibFfa.h"
TPM2_PTP_INTERFACE_TYPE mActiveTpmInterfaceType;
UINT8 mCRBIdleByPass;
UINT8 mCRBIdleByPass;
/**
Return cached PTP CRB interface IdleByPass state.
@ -52,28 +51,18 @@ InternalTpm2DeviceLibFfaConstructor (
{
EFI_STATUS Status;
mActiveTpmInterfaceType = PcdGet8 (PcdActiveTpmInterfaceType);
mCRBIdleByPass = 0xFF;
mCRBIdleByPass = 0xFF;
if (PcdGet64 (PcdTpmBaseAddress) == 0) {
Status = EFI_NO_MAPPING;
goto Exit;
}
//
// Start by checking the PCD out of the gate and read from the CRB if it is invalid
//
if (mActiveTpmInterfaceType == 0xFF) {
mActiveTpmInterfaceType = Tpm2GetPtpInterface ((VOID *)(UINTN)PcdGet64 (PcdTpmBaseAddress));
PcdSet8S (PcdActiveTpmInterfaceType, mActiveTpmInterfaceType);
}
if (mActiveTpmInterfaceType != Tpm2PtpInterfaceCrb) {
Status = EFI_UNSUPPORTED;
Status = ValidateTpmInterfaceType ();
if (EFI_ERROR (Status)) {
goto Exit;
}
DEBUG ((DEBUG_INFO, "Setting Tpm Active Interface Type %d\n", mActiveTpmInterfaceType));
mCRBIdleByPass = Tpm2GetIdleByPass ((VOID *)(UINTN)PcdGet64 (PcdTpmBaseAddress));
Status = EFI_SUCCESS;

View file

@ -0,0 +1,87 @@
/** @file
This library provides an interfaces to access DynamicPcds used
in Tpm2DeviceLibFfa.
Copyright (c) 2025, Arm Ltd. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
#include <Library/Tpm2DeviceLib.h>
#include <Uefi/UefiBaseType.h>
#include "Tpm2DeviceLibFfa.h"
TPM2_PTP_INTERFACE_TYPE mActiveTpmInterfaceType;
/**
This function validate TPM interface type for TPM service over FF-A.
@retval EFI_SUCCESS TPM interface type is valid.
@retval EFI_UNSUPPORTED TPM interface type is invalid.
**/
EFI_STATUS
ValidateTpmInterfaceType (
VOID
)
{
mActiveTpmInterfaceType = PcdGet8 (PcdActiveTpmInterfaceType);
//
// Start by checking the PCD out of the gate and read from the CRB if it is invalid
//
if (mActiveTpmInterfaceType == TPM2_FFA_INTERFACE_TYPE_UNKNOWN) {
mActiveTpmInterfaceType = Tpm2GetPtpInterface ((VOID *)(UINTN)PcdGet64 (PcdTpmBaseAddress));
PcdSet8S (PcdActiveTpmInterfaceType, mActiveTpmInterfaceType);
}
if (mActiveTpmInterfaceType != Tpm2PtpInterfaceCrb) {
return EFI_UNSUPPORTED;
}
DEBUG ((DEBUG_INFO, "Setting Tpm Active Interface Type %d\n", mActiveTpmInterfaceType));
return EFI_SUCCESS;
}
/**
This function is used to get the TPM service partition id.
@param[out] PartitionId - Supplies the pointer to the TPM service partition id.
@retval EFI_SUCCESS The TPM command was successfully sent to the TPM
and the response was copied to the Output buffer.
@retval EFI_INVALID_PARAMETER The TPM command buffer is NULL or the TPM command
buffer size is 0.
@retval EFI_DEVICE_ERROR An error occurred in communication with the TPM.
**/
EFI_STATUS
EFIAPI
GetTpmServicePartitionId (
OUT UINT16 *PartitionId
)
{
EFI_STATUS Status;
if (PartitionId == NULL) {
return EFI_INVALID_PARAMETER;
}
if (PcdGet16 (PcdTpmServiceFfaPartitionId) != TPM2_FFA_PARTITION_ID_INVALID) {
*PartitionId = PcdGet16 (PcdTpmServiceFfaPartitionId);
return EFI_SUCCESS;
}
Status = FfaTpm2GetServicePartitionId (PartitionId);
if (!EFI_ERROR (Status)) {
PcdSet16S (PcdTpmServiceFfaPartitionId, *PartitionId);
}
return Status;
}

View file

@ -29,6 +29,7 @@
Tpm2DeviceLibFfaBase.c
Tpm2Ptp.c
Tpm2DeviceLibFfa.h
Tpm2InfoFfa.c
[Packages]
MdePkg/MdePkg.dec

View file

@ -26,7 +26,7 @@
#include "Tpm2DeviceLibFfa.h"
UINT32 mFfaTpm2PartitionId = MAX_UINT32;
UINT16 mFfaTpm2PartitionId = TPM2_FFA_PARTITION_ID_INVALID;
/**
Check the return status from the FF-A call and returns EFI_STATUS
@ -84,7 +84,7 @@ TranslateTpmReturnStatus (
}
/**
This function is used to get the TPM service partition id.
This function is used to get the TPM service partition id via FF-A.
@param[out] PartitionId - Supplies the pointer to the TPM service partition id.
@ -92,11 +92,12 @@ TranslateTpmReturnStatus (
and the response was copied to the Output buffer.
@retval EFI_INVALID_PARAMETER The TPM command buffer is NULL or the TPM command
buffer size is 0.
@retval EFI_DEVICE_ERROR The TPM partition information is wrong.
@retval EFI_DEVICE_ERROR An error occurred in communication with the TPM.
**/
EFI_STATUS
GetTpmServicePartitionId (
OUT UINT32 *PartitionId
FfaTpm2GetServicePartitionId (
OUT UINT16 *PartitionId
)
{
EFI_STATUS Status;
@ -114,20 +115,6 @@ GetTpmServicePartitionId (
goto Exit;
}
if (mFfaTpm2PartitionId != MAX_UINT32) {
*PartitionId = mFfaTpm2PartitionId;
Status = EFI_SUCCESS;
goto Exit;
}
if (PcdGet16 (PcdTpmServiceFfaPartitionId) != 0) {
mFfaTpm2PartitionId = PcdGet16 (PcdTpmServiceFfaPartitionId);
*PartitionId = mFfaTpm2PartitionId;
Status = EFI_SUCCESS;
goto Exit;
}
Status = ArmFfaLibPartitionIdGet (&PartId);
if (EFI_ERROR (Status)) {
DEBUG ((
@ -164,11 +151,14 @@ GetTpmServicePartitionId (
Status = EFI_INVALID_PARAMETER;
DEBUG ((DEBUG_ERROR, "Invalid partition Info(%g). Count: %d, Size: %d\n", &gTpm2ServiceFfaGuid, Count, Size));
} else {
TpmPartInfo = (EFI_FFA_PART_INFO_DESC *)RxBuffer;
mFfaTpm2PartitionId = TpmPartInfo->PartitionId;
*PartitionId = mFfaTpm2PartitionId;
Status = PcdSet16S (PcdTpmServiceFfaPartitionId, mFfaTpm2PartitionId);
TpmPartInfo = (EFI_FFA_PART_INFO_DESC *)RxBuffer;
*PartitionId = TpmPartInfo->PartitionId;
if (TpmPartInfo->PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
/*
* Tpm partition id never be TPM2_FFA_PARTITION_ID_INVALID.
*/
Status = EFI_DEVICE_ERROR;
}
}
RxRelease:
@ -202,7 +192,7 @@ Tpm2GetInterfaceVersion (
goto Exit;
}
if (mFfaTpm2PartitionId == MAX_UINT32) {
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
@ -253,7 +243,7 @@ Tpm2GetFeatureInfo (
goto Exit;
}
if (mFfaTpm2PartitionId == MAX_UINT32) {
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
@ -296,7 +286,7 @@ Tpm2ServiceStart (
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (mFfaTpm2PartitionId == MAX_UINT32) {
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
@ -341,7 +331,7 @@ Tpm2RegisterNotification (
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (mFfaTpm2PartitionId == MAX_UINT32) {
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
@ -380,7 +370,7 @@ Tpm2UnregisterNotification (
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (mFfaTpm2PartitionId == MAX_UINT32) {
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
@ -417,7 +407,7 @@ Tpm2FinishNotified (
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (mFfaTpm2PartitionId == MAX_UINT32) {
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}