1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/** @file
This library provides an implementation of Tpm2DeviceLib
using ARM64 SMC calls to request TPM service.
The implementation is only supporting the Command Response Buffer (CRB)
for sharing data with the TPM.
Copyright (c), Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h>
#include <Pi/PiMultiPhase.h>
#include <IndustryStandard/ArmFfaSvc.h>
#include <IndustryStandard/ArmFfaBootInfo.h>
#include <IndustryStandard/ArmFfaPartInfo.h>
#include <Guid/Tpm2ServiceFfa.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/Tpm2DeviceLib.h>
#include <Library/ArmFfaLib.h>
#include "Tpm2DeviceLibFfa.h"
UINT16 mFfaTpm2PartitionId = TPM2_FFA_PARTITION_ID_INVALID;
/**
Check the return status from the FF-A call and returns EFI_STATUS
@param EFI_LOAD_ERROR FF-A status code returned in x0
@retval EFI_SUCCESS The entry point is executed successfully.
**/
EFI_STATUS
TranslateTpmReturnStatus (
UINTN TpmReturnStatus
)
{
EFI_STATUS Status;
switch (TpmReturnStatus) {
case TPM2_FFA_SUCCESS_OK:
case TPM2_FFA_SUCCESS_OK_RESULTS_RETURNED:
Status = EFI_SUCCESS;
break;
case TPM2_FFA_ERROR_NOFUNC:
Status = EFI_NOT_FOUND;
break;
case TPM2_FFA_ERROR_NOTSUP:
Status = EFI_UNSUPPORTED;
break;
case TPM2_FFA_ERROR_INVARG:
Status = EFI_INVALID_PARAMETER;
break;
case TPM2_FFA_ERROR_INV_CRB_CTRL_DATA:
Status = EFI_COMPROMISED_DATA;
break;
case TPM2_FFA_ERROR_ALREADY:
Status = EFI_ALREADY_STARTED;
break;
case TPM2_FFA_ERROR_DENIED:
Status = EFI_ACCESS_DENIED;
break;
case TPM2_FFA_ERROR_NOMEM:
Status = EFI_OUT_OF_RESOURCES;
break;
default:
Status = EFI_DEVICE_ERROR;
}
return Status;
}
/**
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 The TPM partition information is wrong.
@retval EFI_DEVICE_ERROR An error occurred in communication with the TPM.
**/
EFI_STATUS
FfaTpm2GetServicePartitionId (
OUT UINT16 *PartitionId
)
{
EFI_STATUS Status;
UINT32 Count;
UINT32 Size;
EFI_FFA_PART_INFO_DESC *TpmPartInfo;
VOID *TxBuffer;
UINT64 TxBufferSize;
VOID *RxBuffer;
UINT64 RxBufferSize;
UINT16 PartId;
if (PartitionId == NULL) {
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
Status = ArmFfaLibPartitionIdGet (&PartId);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"Failed to get partition id. Status: %r\n",
Status
));
goto Exit;
}
Status = ArmFfaLibGetRxTxBuffers (
&TxBuffer,
&TxBufferSize,
&RxBuffer,
&RxBufferSize
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed to get Rx/Tx Buffer. Status: %r\n", Status));
goto Exit;
}
Status = ArmFfaLibPartitionInfoGet (
&gTpm2ServiceFfaGuid,
FFA_PART_INFO_FLAG_TYPE_DESC,
&Count,
&Size
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed to get Tpm2 partition info. Status: %r\n", Status));
goto RxRelease;
}
if ((Count != 1) || (Size < sizeof (EFI_FFA_PART_INFO_DESC))) {
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;
*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:
ArmFfaLibRxRelease (PartId);
Exit:
return Status;
}
/**
This function is used to get the TPM interface version.
@param[out] Version - Supplies the pointer to the TPM interface version.
@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
Tpm2GetInterfaceVersion (
OUT UINT32 *Version
)
{
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (Version == NULL) {
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
ZeroMem (&FfaDirectReq2Args, sizeof (DIRECT_MSG_ARGS));
FfaDirectReq2Args.Arg0 = TPM2_FFA_GET_INTERFACE_VERSION;
Status = ArmFfaLibMsgSendDirectReq2 (mFfaTpm2PartitionId, &gTpm2ServiceFfaGuid, &FfaDirectReq2Args);
while (Status == EFI_INTERRUPT_PENDING) {
// We are assuming vCPU0 of the TPM SP since it is UP.
Status = ArmFfaLibRun (mFfaTpm2PartitionId, 0x00, &FfaDirectReq2Args);
}
if (EFI_ERROR (Status)) {
goto Exit;
}
Status = TranslateTpmReturnStatus (FfaDirectReq2Args.Arg0);
if (!EFI_ERROR (Status)) {
*Version = FfaDirectReq2Args.Arg1;
}
Exit:
return Status;
}
/**
This function is used to get the TPM feature information.
@param[out] FeatureInfo - Supplies the pointer to the feature information.
@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
Tpm2GetFeatureInfo (
OUT UINT32 *FeatureInfo
)
{
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (FeatureInfo == NULL) {
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
ZeroMem (&FfaDirectReq2Args, sizeof (DIRECT_MSG_ARGS));
FfaDirectReq2Args.Arg0 = TPM2_FFA_GET_FEATURE_INFO;
FfaDirectReq2Args.Arg1 = TPM_SERVICE_FEATURE_SUPPORT_NOTIFICATION;
Status = ArmFfaLibMsgSendDirectReq2 (mFfaTpm2PartitionId, &gTpm2ServiceFfaGuid, &FfaDirectReq2Args);
while (Status == EFI_INTERRUPT_PENDING) {
// We are assuming vCPU0 of the TPM SP since it is UP.
Status = ArmFfaLibRun (mFfaTpm2PartitionId, 0x00, &FfaDirectReq2Args);
}
if (EFI_ERROR (Status)) {
goto Exit;
}
Status = TranslateTpmReturnStatus (FfaDirectReq2Args.Arg0);
Exit:
return Status;
}
/**
This service enables the sending of commands to the TPM2.
@param[in] FuncQualifier Function qualifier.
@param[in] LocalityQualifier Locality qualifier.
@retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
@retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
@retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
**/
EFI_STATUS
Tpm2ServiceStart (
IN UINT64 FuncQualifier,
IN UINT64 LocalityQualifier
)
{
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
ZeroMem (&FfaDirectReq2Args, sizeof (DIRECT_MSG_ARGS));
FfaDirectReq2Args.Arg0 = TPM2_FFA_START;
FfaDirectReq2Args.Arg1 = (FuncQualifier & 0xFF);
FfaDirectReq2Args.Arg2 = (LocalityQualifier & 0xFF);
Status = ArmFfaLibMsgSendDirectReq2 (mFfaTpm2PartitionId, &gTpm2ServiceFfaGuid, &FfaDirectReq2Args);
while (Status == EFI_INTERRUPT_PENDING) {
// We are assuming vCPU0 of the TPM SP since it is UP.
Status = ArmFfaLibRun (mFfaTpm2PartitionId, 0x00, &FfaDirectReq2Args);
}
if (EFI_ERROR (Status)) {
goto Exit;
}
Status = TranslateTpmReturnStatus (FfaDirectReq2Args.Arg0);
Exit:
return Status;
}
/**
Register TPM2 device notification.
@param[in] NotificationTypeQualifier Notification type qualifier.
@param[in] vCpuId vCPU ID.
@param[in] NotificationId Bitmap ID for the notification.
@retval EFI_SUCCESS The command was successfully sent to the device and a response was successfully received.
@retval Others Some error occurred in communication with the device.
**/
EFI_STATUS
Tpm2RegisterNotification (
IN BOOLEAN NotificationTypeQualifier,
IN UINT16 vCpuId,
IN UINT64 NotificationId
)
{
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
ZeroMem (&FfaDirectReq2Args, sizeof (DIRECT_MSG_ARGS));
FfaDirectReq2Args.Arg0 = TPM2_FFA_REGISTER_FOR_NOTIFICATION;
FfaDirectReq2Args.Arg1 = (NotificationTypeQualifier << 16 | vCpuId);
FfaDirectReq2Args.Arg2 = (NotificationId & 0xFF);
Status = ArmFfaLibMsgSendDirectReq2 (mFfaTpm2PartitionId, &gTpm2ServiceFfaGuid, &FfaDirectReq2Args);
while (Status == EFI_INTERRUPT_PENDING) {
// We are assuming vCPU0 of the TPM SP since it is UP.
Status = ArmFfaLibRun (mFfaTpm2PartitionId, 0x00, &FfaDirectReq2Args);
}
if (EFI_ERROR (Status)) {
goto Exit;
}
Status = TranslateTpmReturnStatus (FfaDirectReq2Args.Arg0);
Exit:
return Status;
}
/**
Unregister TPM2 device notification.
@retval EFI_SUCCESS The command was successfully sent to the device and a response was successfully received.
@retval Others Some error occurred in communication with the device.
**/
EFI_STATUS
Tpm2UnregisterNotification (
VOID
)
{
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
ZeroMem (&FfaDirectReq2Args, sizeof (DIRECT_MSG_ARGS));
FfaDirectReq2Args.Arg0 = TPM2_FFA_UNREGISTER_FROM_NOTIFICATION;
Status = ArmFfaLibMsgSendDirectReq2 (mFfaTpm2PartitionId, &gTpm2ServiceFfaGuid, &FfaDirectReq2Args);
while (Status == EFI_INTERRUPT_PENDING) {
// We are assuming vCPU0 of the TPM SP since it is UP.
Status = ArmFfaLibRun (mFfaTpm2PartitionId, 0x00, &FfaDirectReq2Args);
}
if (EFI_ERROR (Status)) {
goto Exit;
}
Status = TranslateTpmReturnStatus (FfaDirectReq2Args.Arg0);
Exit:
return Status;
}
/**
Issue a finished notification command to the TPM service over FF-A.
@retval EFI_SUCCESS The command was successfully sent to the device and a response was successfully received.
@retval Others Some error occurred in communication with the device.
**/
EFI_STATUS
Tpm2FinishNotified (
VOID
)
{
EFI_STATUS Status;
DIRECT_MSG_ARGS FfaDirectReq2Args;
if (mFfaTpm2PartitionId == TPM2_FFA_PARTITION_ID_INVALID) {
GetTpmServicePartitionId (&mFfaTpm2PartitionId);
}
ZeroMem (&FfaDirectReq2Args, sizeof (DIRECT_MSG_ARGS));
FfaDirectReq2Args.Arg0 = TPM2_FFA_FINISH_NOTIFIED;
Status = ArmFfaLibMsgSendDirectReq2 (mFfaTpm2PartitionId, &gTpm2ServiceFfaGuid, &FfaDirectReq2Args);
while (Status == EFI_INTERRUPT_PENDING) {
// We are assuming vCPU0 of the TPM SP since it is UP.
Status = ArmFfaLibRun (mFfaTpm2PartitionId, 0x00, &FfaDirectReq2Args);
}
if (EFI_ERROR (Status)) {
goto Exit;
}
Status = TranslateTpmReturnStatus (FfaDirectReq2Args.Arg0);
Exit:
return Status;
}
|