[edk2-devel] [PATCH v4 6/7] StandaloneMmPkg: StandaloneMmCore: Parsing new MM communicate header

Kun Qin posted 7 patches 4 years, 1 month ago
[edk2-devel] [PATCH v4 6/7] StandaloneMmPkg: StandaloneMmCore: Parsing new MM communicate header
Posted by Kun Qin 4 years, 1 month ago
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3398
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3430

MM communicate protocols are expanded with EFI_MM_COMMUNICATE_HEADER_V3
structure that cooperates with updated field types and flexible array.
The PiSmmCore implementation is updated to detect and process incoming
data accordingly.

Two checks are also performed to prevent legacy communicate data or
unsupported data is fed into MM core under agreed header guid.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>

Signed-off-by: Kun Qin <kuqin12@gmail.com>
---

Notes:
    v3:
    - Newly added
    
    v4:
    - Rebased with uncrusitify changes.

 StandaloneMmPkg/Core/StandaloneMmCore.c   | 35 ++++++++++++++++----
 StandaloneMmPkg/Core/StandaloneMmCore.inf |  1 +
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c
index d221f1d1115d..8afb22493cb2 100644
--- a/StandaloneMmPkg/Core/StandaloneMmCore.c
+++ b/StandaloneMmPkg/Core/StandaloneMmCore.c
@@ -338,8 +338,12 @@ MmEntryPoint (
   IN CONST EFI_MM_ENTRY_CONTEXT  *MmEntryContext
   )
 {
-  EFI_STATUS                 Status;
-  EFI_MM_COMMUNICATE_HEADER  *CommunicateHeader;
+  EFI_STATUS                    Status;
+  EFI_MM_COMMUNICATE_HEADER_V3  *CommunicateHeader;
+  EFI_MM_COMMUNICATE_HEADER     *LegacyCommunicateHeader;
+  EFI_GUID                      *CommGuid;
+  VOID                          *CommData;
+  UINTN                         CommHeaderSize;
 
   DEBUG ((DEBUG_INFO, "MmEntryPoint ...\n"));
 
@@ -377,19 +381,36 @@ MmEntryPoint (
       gMmCorePrivate->CommunicationBuffer = 0;
       gMmCorePrivate->ReturnStatus        = EFI_INVALID_PARAMETER;
     } else {
-      CommunicateHeader           = (EFI_MM_COMMUNICATE_HEADER *)(UINTN)gMmCorePrivate->CommunicationBuffer;
-      gMmCorePrivate->BufferSize -= OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
+      CommGuid = &((EFI_MM_COMMUNICATE_HEADER_V3 *)(UINTN)gMmCorePrivate->CommunicationBuffer)->HeaderGuid;
+      //
+      // Check if the signature matches EFI_MM_COMMUNICATE_HEADER_V3 definition
+      //
+      if (CompareGuid (CommGuid, &gCommunicateHeaderV3Guid)) {
+        CommunicateHeader = (EFI_MM_COMMUNICATE_HEADER_V3 *)(UINTN)gMmCorePrivate->CommunicationBuffer;
+        ASSERT (CommunicateHeader->Signature == EFI_MM_COMMUNICATE_HEADER_V3_SIGNATURE);
+        ASSERT (CommunicateHeader->Version <= EFI_MM_COMMUNICATE_HEADER_V3_VERSION);
+        CommGuid       = &CommunicateHeader->MessageGuid;
+        CommData       = CommunicateHeader->MessageData;
+        CommHeaderSize = sizeof (EFI_MM_COMMUNICATE_HEADER_V3);
+      } else {
+        LegacyCommunicateHeader = (EFI_MM_COMMUNICATE_HEADER *)(UINTN)gMmCorePrivate->CommunicationBuffer;
+        CommGuid                = &LegacyCommunicateHeader->HeaderGuid;
+        CommData                = LegacyCommunicateHeader->Data;
+        CommHeaderSize          = OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
+      }
+
+      gMmCorePrivate->BufferSize -= CommHeaderSize;
       Status                      = MmiManage (
-                                      &CommunicateHeader->HeaderGuid,
+                                      CommGuid,
                                       NULL,
-                                      CommunicateHeader->Data,
+                                      CommData,
                                       (UINTN *)&gMmCorePrivate->BufferSize
                                       );
       //
       // Update CommunicationBuffer, BufferSize and ReturnStatus
       // Communicate service finished, reset the pointer to CommBuffer to NULL
       //
-      gMmCorePrivate->BufferSize         += OFFSET_OF (EFI_MM_COMMUNICATE_HEADER, Data);
+      gMmCorePrivate->BufferSize         += CommHeaderSize;
       gMmCorePrivate->CommunicationBuffer = 0;
       gMmCorePrivate->ReturnStatus        = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;
     }
diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.inf b/StandaloneMmPkg/Core/StandaloneMmCore.inf
index c44b9ff33303..e2e6cd32beee 100644
--- a/StandaloneMmPkg/Core/StandaloneMmCore.inf
+++ b/StandaloneMmPkg/Core/StandaloneMmCore.inf
@@ -75,6 +75,7 @@ [Guids]
   gEfiEventLegacyBootGuid
   gEfiEventExitBootServicesGuid
   gEfiEventReadyToBootGuid
+  gCommunicateHeaderV3Guid    ## CONSUMES   ## GUID # Communicate header
 
 #
 # This configuration fails for CLANGPDB, which does not support PIE in the GCC
-- 
2.34.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#85314): https://edk2.groups.io/g/devel/message/85314
Mute This Topic: https://groups.io/mt/88253685/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-