]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/TdxLib/Rtmr.c
MdePkg: Add TdxLib to wrap Tdx operations
[mirror_edk2.git] / MdePkg / Library / TdxLib / Rtmr.c
CommitLineData
c3001cb7
MX
1/** @file\r
2\r
3 Extends one of the RTMR measurement registers in TDCS with the provided\r
4 extension data in memory.\r
5\r
6 Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.<BR>\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10\r
11#include <Library/BaseLib.h>\r
12#include <Library/DebugLib.h>\r
13#include <Uefi/UefiBaseType.h>\r
14#include <Library/TdxLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
16#include <IndustryStandard/Tpm20.h>\r
17#include <IndustryStandard/Tdx.h>\r
18\r
19#define RTMR_COUNT 4\r
20#define TD_EXTEND_BUFFER_LEN (64 + 48)\r
21\r
22UINT8 mExtendBuffer[TD_EXTEND_BUFFER_LEN];\r
23\r
24/**\r
25 This function extends one of the RTMR measurement register\r
26 in TDCS with the provided extension data in memory.\r
27 RTMR extending supports SHA384 which length is 48 bytes.\r
28\r
29 @param[in] Data Point to the data to be extended\r
30 @param[in] DataLen Length of the data. Must be 48\r
31 @param[in] Index RTMR index\r
32\r
33 @return EFI_SUCCESS\r
34 @return EFI_INVALID_PARAMETER\r
35 @return EFI_DEVICE_ERROR\r
36\r
37**/\r
38EFI_STATUS\r
39EFIAPI\r
40TdExtendRtmr (\r
41 IN UINT32 *Data,\r
42 IN UINT32 DataLen,\r
43 IN UINT8 Index\r
44 )\r
45{\r
46 EFI_STATUS Status;\r
47 UINT64 TdCallStatus;\r
48 UINT8 *ExtendBuffer;\r
49\r
50 Status = EFI_SUCCESS;\r
51\r
52 ASSERT (Data != NULL);\r
53 ASSERT (DataLen == SHA384_DIGEST_SIZE);\r
54 ASSERT (Index >= 0 && Index < RTMR_COUNT);\r
55\r
56 if ((Data == NULL) || (DataLen != SHA384_DIGEST_SIZE) || (Index >= RTMR_COUNT)) {\r
57 return EFI_INVALID_PARAMETER;\r
58 }\r
59\r
60 // TD.RTMR.EXTEND requires 64B-aligned guest physical address of\r
61 // 48B-extension data. We use ALIGN_POINTER(Pointer, 64) to get\r
62 // the 64B-aligned guest physical address.\r
63 ExtendBuffer = ALIGN_POINTER (mExtendBuffer, 64);\r
64 ASSERT (((UINTN)ExtendBuffer & 0x3f) == 0);\r
65\r
66 ZeroMem (ExtendBuffer, SHA384_DIGEST_SIZE);\r
67 CopyMem (ExtendBuffer, Data, SHA384_DIGEST_SIZE);\r
68\r
69 TdCallStatus = TdCall (TDCALL_TDEXTENDRTMR, (UINT64)(UINTN)ExtendBuffer, Index, 0, 0);\r
70\r
71 if (TdCallStatus == TDX_EXIT_REASON_SUCCESS) {\r
72 Status = EFI_SUCCESS;\r
73 } else if (TdCallStatus == TDX_EXIT_REASON_OPERAND_INVALID) {\r
74 Status = EFI_INVALID_PARAMETER;\r
75 } else {\r
76 Status = EFI_DEVICE_ERROR;\r
77 }\r
78\r
79 if (Status != EFI_SUCCESS) {\r
80 DEBUG ((DEBUG_ERROR, "Error returned from TdExtendRtmr call - 0x%lx\n", TdCallStatus));\r
81 }\r
82\r
83 return Status;\r
84}\r