]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg: Support standalone MM Driver Unload capability
authorJiaxin Wu <Jiaxin.wu@intel.com>
Mon, 1 Mar 2021 04:06:44 +0000 (12:06 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Thu, 18 Mar 2021 04:16:21 +0000 (04:16 +0000)
https://bugzilla.tianocore.org/show_bug.cgi?id=3240

This patch is to support standalone MM Driver Unload capability
by providing _DriverUnloadHandler() function.

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Cc: Siyuan Fu <siyuan.fu@intel.com>
Signed-off-by: Jiaxin Wu <Jiaxin.wu@intel.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
MdePkg/Include/Library/StandaloneMmDriverEntryPoint.h
MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.c
MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf

index 25b2d8d68d56bb794abaec7ee16ef5bfcf949a48..12f7886640efef329c2656844f4fe2beb0a115dd 100644 (file)
@@ -18,6 +18,11 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 ///\r
 extern CONST UINT32                   _gMmRevision;\r
 \r
+///\r
+/// Declare the number of unload handler in the image.\r
+///\r
+extern CONST UINT8                    _gDriverUnloadImageCount;\r
+\r
 /**\r
   The entry point of PE/COFF Image for a Standalone MM Driver.\r
 \r
@@ -122,4 +127,24 @@ ProcessModuleEntryPointList (
   IN EFI_MM_SYSTEM_TABLE    *MmSystemTable\r
   );\r
 \r
+/**\r
+  Autogenerated function that calls a set of module unload handlers.\r
+\r
+  This function must be called from the unload handler registered by _ModuleEntryPoint().\r
+  This function calls the set of module unload handlers.\r
+  This function is autogenerated by build tools and those build tools are responsible\r
+  for collecting the module unload handlers and calling them in a specified order.\r
+\r
+  @param  ImageHandle  The image handle of the DXE Driver, DXE Runtime Driver, DXE SMM Driver, or UEFI Driver.\r
+\r
+  @retval  EFI_SUCCESS  The unload handlers executed normally.\r
+  @retval  !EFI_SUCCESS The unload handlers failed to execute normally.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ProcessModuleUnloadList (\r
+  IN EFI_HANDLE  ImageHandle\r
+  );\r
+\r
 #endif\r
index 2c41e23a032e04278ceaa85ec6a3929b0eef587c..d74c9bdfed60ace100340a4143b3ca250c284056 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Entry point to a Standalone MM driver.\r
 \r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>\r
 Copyright (c) 2016 - 2018, ARM Ltd. All rights reserved.<BR>\r
 Copyright (c) 2018, Linaro, Limited. All rights reserved.<BR>\r
 \r
@@ -11,11 +11,51 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 \r
 #include <PiMm.h>\r
 \r
+#include <Protocol/LoadedImage.h>\r
 #include <Library/BaseLib.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/MmServicesTableLib.h>\r
 #include <Library/StandaloneMmDriverEntryPoint.h>\r
 \r
+/**\r
+  Unloads an image from memory.\r
+\r
+  This function is a callback that a driver registers to do cleanup\r
+  when the UnloadImage boot service function is called.\r
+\r
+  @param  ImageHandle The handle to the image to unload.\r
+\r
+  @return Status returned by all unload().\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+_DriverUnloadHandler (\r
+  EFI_HANDLE ImageHandle\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // If an UnloadImage() handler is specified, then call it\r
+  //\r
+  Status = ProcessModuleUnloadList (ImageHandle);\r
+\r
+  //\r
+  // If the driver specific unload handler does not return an error, then call all of the\r
+  // library destructors.  If the unload handler returned an error, then the driver can not be\r
+  // unloaded, and the library destructors should not be called\r
+  //\r
+  if (!EFI_ERROR (Status)) {\r
+    ProcessLibraryDestructorList (ImageHandle, gMmst);\r
+  }\r
+\r
+  //\r
+  // Return the status from the driver specific unload handler\r
+  //\r
+  return Status;\r
+}\r
+\r
 /**\r
   The entry point of PE/COFF Image for a Standalone MM Driver.\r
 \r
@@ -46,6 +86,7 @@ _ModuleEntryPoint (
   )\r
 {\r
   EFI_STATUS                 Status;\r
+  EFI_LOADED_IMAGE_PROTOCOL  *LoadedImage;\r
 \r
   if (_gMmRevision != 0) {\r
     //\r
@@ -62,6 +103,19 @@ _ModuleEntryPoint (
   //\r
   ProcessLibraryConstructorList (ImageHandle, MmSystemTable);\r
 \r
+  //\r
+  //  Install unload handler...\r
+  //\r
+  if (_gDriverUnloadImageCount != 0) {\r
+    Status = gMmst->MmHandleProtocol (\r
+                      ImageHandle,\r
+                      &gEfiLoadedImageProtocolGuid,\r
+                      (VOID **)&LoadedImage\r
+                      );\r
+    ASSERT_EFI_ERROR (Status);\r
+    LoadedImage->Unload = _DriverUnloadHandler;\r
+  }\r
+\r
   //\r
   // Call the driver entry point\r
   //\r
index 2f33a6d4ad7a9bcf871f28522e40f5832d29e075..9303566309152e5c372ee690fd338881f7b790be 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # Module entry point library for Standalone MM driver.\r
 #\r
-# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>\r
 # Copyright (c) 2016-2018, ARM Ltd. All rights reserved.<BR>\r
 # Copyright (c) 2018, Linaro, Limited. All rights reserved.<BR>\r
 #\r
@@ -36,3 +36,6 @@
   BaseLib\r
   DebugLib\r
   MmServicesTableLib\r
+\r
+[Protocols]\r
+  gEfiLoadedImageProtocolGuid      ## SOMETIMES_CONSUMES\r