///\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
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
/** @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
\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
)\r
{\r
EFI_STATUS Status;\r
+ EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
\r
if (_gMmRevision != 0) {\r
//\r
//\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