Add a CommonMmServicesLib library will be used by both traditional
SMM and Standalone MM drivers

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jagadeesh Ujja <jagadeesh.u...@arm.com>
---
 MdePkg/Include/Library/CommonMmServicesLibrary.h                   | 131 
++++++++++++
 MdePkg/Library/CommonMmServicesLibrary/CommonMmServicesLibrary.c   | 224 
++++++++++++++++++++
 MdePkg/Library/CommonMmServicesLibrary/CommonMmServicesLibrary.inf |  42 ++++
 3 files changed, 397 insertions(+)

diff --git a/MdePkg/Include/Library/CommonMmServicesLibrary.h 
b/MdePkg/Include/Library/CommonMmServicesLibrary.h
new file mode 100644
index 0000000..0ed7572
--- /dev/null
+++ b/MdePkg/Include/Library/CommonMmServicesLibrary.h
@@ -0,0 +1,131 @@
+/** @file
+  Wrapper functions consumed by traditional SMM drivers and
+  Standalone MM Drivers
+
+  Copyright (c) 2018, ARM Limited. All rights reserved.<BR>
+  This program and the accompanying materials are licensed and made available
+  under the terms and conditions of the BSD License which accompanies this
+  distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+#include <PiSmm.h>
+#include <PiMm.h>
+
+/**
+  Return the first Protocol Interface that matches the Protocol GUID. If
+  Registration is pasased in return a Protocol Instance that was just add
+  to the system. If Retistration is NULL return the first Protocol Interface
+  you find.
+
+  @param  Protocol               The protocol to search for
+  @param  Registration           Optional Registration Key returned from
+                                 RegisterProtocolNotify()
+  @param  Interface              Return the Protocol interface (instance).
+
+  @retval EFI_SUCCESS            If a valid Interface is returned
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_NOT_FOUND          Protocol interface not found
+
+**/
+EFI_STATUS
+EFIAPI
+MmstLocateProtocol(
+  IN  EFI_GUID  *Protocol,
+  IN  VOID      *Registration, OPTIONAL
+  OUT VOID      **Interface
+  );
+
+
+/**
+  Registers a handler to execute within MM.
+
+  @param  Handler        Handler service funtion pointer.
+  @param  HandlerType    Points to the handler type or NULL for root MI 
handlers.
+  @param  DispatchHandle On return, contains a unique handle which can be used 
to
+          later unregister the handler function.
+
+  @retval EFI_SUCCESS           Handler register success.
+  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+MmstiHandlerRegister (
+  IN  EFI_MM_HANDLER_ENTRY_POINT    Handler,
+  IN  CONST EFI_GUID                *HandlerType  OPTIONAL,
+  OUT EFI_HANDLE                    *DispatchHandle
+  );
+
+/**
+  Locates the requested handle(s) and returns them in Buffer.
+
+  @param  SearchType             The type of search to perform to locate the
+                                 handles
+  @param  Protocol               The protocol to search for
+  @param  SearchKey              Dependant on SearchType
+  @param  BufferSize             On input the size of Buffer.  On output the
+                                 size of data returned.
+  @param  Buffer                 The buffer to return the results in
+
+  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
+                                 returned in BufferSize.
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
+                                 returns them in Buffer.
+
+**/
+EFI_STATUS
+EFIAPI
+MmstLocateHandle (
+  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
+  IN     EFI_GUID                *Protocol   OPTIONAL,
+  IN     VOID                    *SearchKey  OPTIONAL,
+  IN OUT UINTN                   *BufferSize,
+  OUT    EFI_HANDLE              *Buffer
+  );
+
+/**
+  Queries a handle to determine if it supports a specified protocol.
+
+  @param  UserHandle             The handle being queried.
+  @param  Protocol               The published unique identifier of the 
protocol.
+  @param  Interface              Supplies the address where a pointer to the
+                                 corresponding Protocol Interface is returned.
+
+  @return The requested protocol interface for the handle
+
+**/
+EFI_STATUS
+EFIAPI
+MmstHandleProtocol (
+  IN  EFI_HANDLE  UserHandle,
+  IN  EFI_GUID    *Protocol,
+  OUT VOID        **Interface
+  );
+
+/**
+  Wrapper function to Smm/MmInstallProtocolInterfaceNotify.  This is the 
public API which
+  Calls the private one which contains a BOOLEAN parameter for notifications
+
+  @param  UserHandle             The handle to install the protocol handler on,
+                                 or NULL if a new handle is to be allocated
+  @param  Protocol               The protocol to add to the handle
+  @param  InterfaceType          Indicates whether Interface is supplied in
+                                 native form.
+  @param  Interface              The interface for the protocol being added
+
+  @return Status code
+
+**/
+EFI_STATUS
+EFIAPI
+MmstInstallProtocolInterface (
+  IN OUT EFI_HANDLE      *UserHandle,
+  IN EFI_GUID            *Protocol,
+  IN EFI_INTERFACE_TYPE  InterfaceType,
+  IN VOID                *Interface
+  );
diff --git a/MdePkg/Library/CommonMmServicesLibrary/CommonMmServicesLibrary.c 
b/MdePkg/Library/CommonMmServicesLibrary/CommonMmServicesLibrary.c
new file mode 100644
index 0000000..59ebd90
--- /dev/null
+++ b/MdePkg/Library/CommonMmServicesLibrary/CommonMmServicesLibrary.c
@@ -0,0 +1,224 @@
+/** @file
+  Wrapper functions consumed by traditional SMM drivers and
+  Standalone MM Drivers
+
+  Copyright (c) 2018, ARM Limited. All rights reserved.<BR>
+  This program and the accompanying materials are licensed and made available
+  under the terms and conditions of the BSD License which accompanies this
+  distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <Library/CommonMmServicesLibrary.h>
+#include <Library/StandaloneMmServicesTableLib.h>
+#include <Library/SmmServicesTableLib.h>
+
+/**
+  Return the first Protocol Interface that matches the Protocol GUID. If
+  Registration is pasased in return a Protocol Instance that was just add
+  to the system. If Retistration is NULL return the first Protocol Interface
+  you find.
+
+  @param  Protocol               The protocol to search for
+  @param  Registration           Optional Registration Key returned from
+                                 RegisterProtocolNotify()
+  @param  Interface              Return the Protocol interface (instance).
+
+  @retval EFI_SUCCESS            If a valid Interface is returned
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_NOT_FOUND          Protocol interface not found
+
+**/
+EFI_STATUS
+EFIAPI
+MmstLocateProtocol(
+  IN  EFI_GUID  *Protocol,
+  IN  VOID      *Registration, OPTIONAL
+  OUT VOID      **Interface
+  )
+{
+  EFI_STATUS  Status;
+
+  if (!PcdGetBool (PcdStandaloneMmVariableEnabled)) {
+    Status = gSmst->SmmLocateProtocol (
+                      Protocol,
+                      Registration,
+                      Interface);
+  } else {
+    Status = gMmst->MmLocateProtocol (
+                      Protocol,
+                      Registration,
+                      Interface);
+  }
+
+  return Status;
+}
+
+/**
+  Registers a handler to execute within MM.
+
+  @param  Handler        Handler service funtion pointer.
+  @param  HandlerType    Points to the handler type or NULL for root MI 
handlers.
+  @param  DispatchHandle On return, contains a unique handle which can be used 
to later
+          unregister the handler function.
+
+  @retval EFI_SUCCESS           Handler register success.
+  @retval EFI_INVALID_PARAMETER Handler or DispatchHandle is NULL.
+
+**/
+
+EFI_STATUS
+EFIAPI
+MmstiHandlerRegister (
+  IN  EFI_MM_HANDLER_ENTRY_POINT    Handler,
+  IN  CONST EFI_GUID                *HandlerType  OPTIONAL,
+  OUT EFI_HANDLE                    *DispatchHandle
+  )
+{
+  EFI_STATUS  Status;
+
+  if (!PcdGetBool (PcdStandaloneMmVariableEnabled)) {
+    Status = gSmst->SmiHandlerRegister (
+                      Handler,
+                      HandlerType,
+                      DispatchHandle);
+  } else {
+    Status = gMmst->MmiHandlerRegister (
+                      Handler,
+                      HandlerType,
+                      DispatchHandle);
+  }
+  return Status;
+}
+
+/**
+  Locates the requested handle(s) and returns them in Buffer.
+
+  @param  SearchType             The type of search to perform to locate the
+                                 handles
+  @param  Protocol               The protocol to search for
+  @param  SearchKey              Dependant on SearchType
+  @param  BufferSize             On input the size of Buffer.  On output the
+                                 size of data returned.
+  @param  Buffer                 The buffer to return the results in
+
+  @retval EFI_BUFFER_TOO_SMALL   Buffer too small, required buffer size is
+                                 returned in BufferSize.
+  @retval EFI_INVALID_PARAMETER  Invalid parameter
+  @retval EFI_SUCCESS            Successfully found the requested handle(s) and
+                                 returns them in Buffer.
+
+**/
+EFI_STATUS
+EFIAPI
+MmstLocateHandle (
+  IN     EFI_LOCATE_SEARCH_TYPE  SearchType,
+  IN     EFI_GUID                *Protocol   OPTIONAL,
+  IN     VOID                    *SearchKey  OPTIONAL,
+  IN OUT UINTN                   *BufferSize,
+  OUT    EFI_HANDLE              *Buffer
+  )
+{
+  EFI_STATUS  Status;
+
+  if (PcdGetBool (PcdStandaloneMmVariableEnabled)) {
+    Status = gMmst->MmLocateHandle (
+                      SearchType,
+                      Protocol,
+                      SearchKey,
+                      BufferSize,
+                      Buffer
+                      );
+  } else {
+    Status = gSmst->SmmLocateHandle (
+                      SearchType,
+                      Protocol,
+                      SearchKey,
+                      BufferSize,
+                      Buffer
+                      );
+  }
+  return Status;
+}
+
+/**
+  Queries a handle to determine if it supports a specified protocol.
+
+  @param  UserHandle             The handle being queried.
+  @param  Protocol               The published unique identifier of the 
protocol.
+  @param  Interface              Supplies the address where a pointer to the
+                                 corresponding Protocol Interface is returned.
+
+  @return The requested protocol interface for the handle
+
+**/
+EFI_STATUS
+EFIAPI
+MmstHandleProtocol (
+  IN  EFI_HANDLE  UserHandle,
+  IN  EFI_GUID    *Protocol,
+  OUT VOID        **Interface
+  )
+{
+  if (PcdGetBool (PcdStandaloneMmVariableEnabled)) {
+    return gMmst->MmHandleProtocol (
+                    UserHandle,
+                    Protocol,
+                    Interface
+                    );
+  } else {
+    return gSmst->SmmHandleProtocol (
+                    UserHandle,
+                    Protocol,
+                    Interface
+                    );
+  }
+}
+
+/**
+  Wrapper function to Smm/MmInstallProtocolInterfaceNotify.  This is the 
public API which
+  Calls the private one which contains a BOOLEAN parameter for notifications
+
+  @param  UserHandle             The handle to install the protocol handler on,
+                                 or NULL if a new handle is to be allocated
+  @param  Protocol               The protocol to add to the handle
+  @param  InterfaceType          Indicates whether Interface is supplied in
+                                 native form.
+  @param  Interface              The interface for the protocol being added
+
+  @return Status code
+
+**/
+
+EFI_STATUS
+EFIAPI
+MmstInstallProtocolInterface (
+  IN OUT EFI_HANDLE      *UserHandle,
+  IN EFI_GUID            *Protocol,
+  IN EFI_INTERFACE_TYPE  InterfaceType,
+  IN VOID                *Interface
+  )
+{
+  EFI_STATUS  Status;
+
+  if (!PcdGetBool (PcdStandaloneMmVariableEnabled)) {
+    Status = gSmst->SmmInstallProtocolInterface (
+                      UserHandle,
+                      Protocol,
+                      InterfaceType,
+                      Interface
+                      );
+  } else {
+    Status = gMmst->MmInstallProtocolInterface (
+                      UserHandle,
+                      Protocol,
+                      InterfaceType,
+                      Interface
+                      );
+  }
+  return Status;
+}
diff --git a/MdePkg/Library/CommonMmServicesLibrary/CommonMmServicesLibrary.inf 
b/MdePkg/Library/CommonMmServicesLibrary/CommonMmServicesLibrary.inf
new file mode 100644
index 0000000..8604474
--- /dev/null
+++ b/MdePkg/Library/CommonMmServicesLibrary/CommonMmServicesLibrary.inf
@@ -0,0 +1,42 @@
+## @file
+#  Wrapper functions consumed by traditional SMM drivers and
+#  Standalone MM Drivers
+#
+#  Copyright (c) 2018, ARM Limited. All rights reserved.<BR>
+#  This program and the accompanying materials are licensed and made available
+#  under the terms and conditions of the BSD License which accompanies this
+#  distribution.  The full text of the license may be found at
+#  http://opensource.org/licenses/bsd-license.php
+#
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR 
IMPLIED.
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = CommonMmServicesLib
+  MODULE_UNI_FILE                = CommonMmServicesLib.uni
+  FILE_GUID                      = 2FF370E4-8B93-4EDA-A177-91296322E3B3
+  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = CommonMmServicesLib|DXE_RUNTIME_DRIVER 
DXE_SMM_DRIVER MM_STANDALONE
+
+#
+# The following information is for reference only and not required by the 
build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
+#
+
+[Sources]
+  CommonMmServicesLibrary.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[LibraryClasses]
+  PcdLib
+
+[Pcd]
+  gEfiMdeModulePkgTokenSpaceGuid.PcdStandaloneMmVariableEnabled
-- 
2.7.4

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to