BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3625

DxeTpmMeasurementLib supports TPM based measurement in DXE phase.
After TeeMeasurementProtocol is introduced, TD based measurement needs
to be supported in DxeTpmMeasurementLib as well.

In TpmMeasureAndLogData, TEE based measurement will be first called.
If it failed, TPM based measurement will be called sequentially.
Currently there is an assumption that TEE based measurement and
TPM based measurement won't be exist at the same time.If the
assumption is not true in the future, we will revisit here then.

Cc: Michael D Kinney <michael.d.kin...@intel.com>
Cc: Liming Gao <gaolim...@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang....@intel.com>
Cc: Jiewen Yao <jiewen....@intel.com>
Cc: Jian J Wang <jian.j.w...@intel.com>
Cc: Sami Mujawar <sami.muja...@arm.com>
Signed-off-by: Min Xu <min.m...@intel.com>
---
 .../DxeTpmMeasurementLib.c                    | 88 ++++++++++++++++++-
 .../DxeTpmMeasurementLib.inf                  |  5 +-
 2 files changed, 88 insertions(+), 5 deletions(-)

diff --git a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c 
b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
index 061136ee7860..1b856cfc7a0d 100644
--- a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
+++ b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
@@ -19,7 +19,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include <Guid/Acpi.h>
 #include <IndustryStandard/Acpi.h>
-
+#include <Protocol/TeeMeasurement.h>
+#include <Protocol/TdProtocol.h>
 
 
 /**
@@ -149,6 +150,73 @@ Tpm20MeasureAndLogData (
   return Status;
 }
 
+/**
+  Tee measure and log data, and extend the measurement result into a
+  specific TEE MR.
+
+  @param[in]  PcrIndex         PCR Index.
+  @param[in]  EventType        Event type.
+  @param[in]  EventLog         Measurement event log.
+  @param[in]  LogLen           Event log length in bytes.
+  @param[in]  HashData         The start of the data buffer to be hashed, 
extended.
+  @param[in]  HashDataLen      The length, in bytes, of the buffer referenced 
by HashData
+
+  @retval EFI_SUCCESS           Operation completed successfully.
+  @retval EFI_UNSUPPORTED       Tdx device not available.
+  @retval EFI_OUT_OF_RESOURCES  Out of memory.
+  @retval EFI_DEVICE_ERROR      The operation was unsuccessful.
+**/
+EFI_STATUS
+EFIAPI
+TeeMeasureAndLogData (
+  IN UINT32             PcrIndex,
+  IN UINT32             EventType,
+  IN VOID               *EventLog,
+  IN UINT32             LogLen,
+  IN VOID               *HashData,
+  IN UINT64             HashDataLen
+  )
+{
+  EFI_STATUS                    Status;
+  EFI_TEE_MEASUREMENT_PROTOCOL  *TeeProtocol;
+  EFI_TEE_EVENT                 *EfiTeeEvent;
+  UINT32                        MrIndex;
+
+  Status = gBS->LocateProtocol (&gEfiTeeMeasurementProtocolGuid, NULL, (VOID 
**) &TeeProtocol);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = TeeProtocol->MapPcrToMrIndex (TeeProtocol, PcrIndex, &MrIndex);
+  if (EFI_ERROR (Status)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  EfiTeeEvent = (EFI_TEE_EVENT *) AllocateZeroPool (LogLen + sizeof 
(EFI_TEE_EVENT));
+  if(EfiTeeEvent == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  EfiTeeEvent->Size = (UINT32) LogLen + sizeof (EFI_TEE_EVENT) - sizeof 
(EfiTeeEvent->Event);
+  EfiTeeEvent->Header.HeaderSize    = sizeof (EFI_TEE_EVENT_HEADER);
+  EfiTeeEvent->Header.HeaderVersion = EFI_TEE_EVENT_HEADER_VERSION;
+  EfiTeeEvent->Header.MrIndex       = MrIndex;
+  EfiTeeEvent->Header.EventType     = EventType;
+  CopyMem (&EfiTeeEvent->Event[0], EventLog, LogLen);
+
+  Status = TeeProtocol->HashLogExtendEvent (
+                           TeeProtocol,
+                           0,
+                           (EFI_PHYSICAL_ADDRESS) (UINTN) HashData,
+                           HashDataLen,
+                           EfiTeeEvent
+                           );
+  FreePool (EfiTeeEvent);
+
+  return Status;
+}
+
+
 /**
   Tpm measure and log data, and extend the measurement result into a specific 
PCR.
 
@@ -178,9 +246,9 @@ TpmMeasureAndLogData (
   EFI_STATUS  Status;
 
   //
-  // Try to measure using Tpm20 protocol
+  // Try to measure using Tee measurement protocol
   //
-  Status = Tpm20MeasureAndLogData(
+  Status = TeeMeasureAndLogData (
              PcrIndex,
              EventType,
              EventLog,
@@ -189,6 +257,20 @@ TpmMeasureAndLogData (
              HashDataLen
              );
 
+  if (EFI_ERROR (Status)) {
+    //
+    // Try to measure using Tpm20 protocol
+    //
+    Status = Tpm20MeasureAndLogData(
+               PcrIndex,
+               EventType,
+               EventLog,
+               LogLen,
+               HashData,
+               HashDataLen
+               );
+  }
+
   if (EFI_ERROR (Status)) {
     //
     // Try to measure using Tpm1.2 protocol
diff --git a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf 
b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
index 7d41bc41f95d..501d5d66e0fb 100644
--- a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
+++ b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.inf
@@ -40,5 +40,6 @@
   UefiBootServicesTableLib
 
 [Protocols]
-  gEfiTcgProtocolGuid           ## SOMETIMES_CONSUMES
-  gEfiTcg2ProtocolGuid          ## SOMETIMES_CONSUMES
+  gEfiTcgProtocolGuid               ## SOMETIMES_CONSUMES
+  gEfiTcg2ProtocolGuid              ## SOMETIMES_CONSUMES
+  gEfiTeeMeasurementProtocolGuid    ## SOMETIMES_CONSUMES
-- 
2.29.2.windows.2



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#82796): https://edk2.groups.io/g/devel/message/82796
Mute This Topic: https://groups.io/mt/86646140/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to