]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Image/Image.c
Switch DxeCore to use DxeCoreMemoryAllocationLib for NT32 platform.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / Image.c
index 0770399cd99bf522648af6cb267331b879148be4..54254879caebeaa40604d69731a46e4aba9f7bc3 100644 (file)
@@ -1,8 +1,7 @@
-/** @file \r
-\r
+/** @file\r
   Core image handling services to load and unload PeImage.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation\r
+Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
 All rights reserved. This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -18,6 +17,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 // Module Globals\r
 //\r
 \r
+SPIN_LOCK                  mUnloadImageLock;\r
+\r
 LOADED_IMAGE_PRIVATE_DATA  *mCurrentImage = NULL;\r
 \r
 LOAD_PE32_IMAGE_PRIVATE_DATA  mLoadPe32PrivateData = {\r
@@ -77,7 +78,7 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage  = {
   Add the Image Services to EFI Boot Services Table and install the protocol\r
   interfaces for this image.\r
 \r
-  @param  HobStart                The HOB to initialize \r
+  @param  HobStart                The HOB to initialize\r
 \r
   @return Status code.\r
 \r
@@ -138,6 +139,11 @@ CoreInitializeImageServices (
 \r
   mCurrentImage = Image;\r
 \r
+  //\r
+  // Initialize spin lock\r
+  //\r
+  InitializeSpinLock (&mUnloadImageLock);\r
+\r
   //\r
   // Fill in DXE globals\r
   //\r
@@ -159,27 +165,27 @@ CoreInitializeImageServices (
 /**\r
   Loads, relocates, and invokes a PE/COFF image\r
 \r
-  @param  BootPolicy              If TRUE, indicates that the request originates \r
-                                  from the boot manager, and that the boot \r
-                                  manager is attempting to load FilePath as a \r
-                                  boot selection. \r
-  @param  Pe32Handle              The handle of PE32 image \r
-  @param  Image                   PE image to be loaded \r
-  @param  DstBuffer               The buffer to store the image \r
-  @param  EntryPoint              A pointer to the entry point \r
-  @param  Attribute               The bit mask of attributes to set for the load \r
-                                  PE image \r
-\r
-  @retval EFI_SUCCESS             The file was loaded, relocated, and invoked \r
-  @retval EFI_OUT_OF_RESOURCES    There was not enough memory to load and \r
-                                  relocate the PE/COFF file \r
-  @retval EFI_INVALID_PARAMETER   Invalid parameter \r
+  @param  BootPolicy              If TRUE, indicates that the request originates\r
+                                  from the boot manager, and that the boot\r
+                                  manager is attempting to load FilePath as a\r
+                                  boot selection.\r
+  @param  Pe32Handle              The handle of PE32 image\r
+  @param  Image                   PE image to be loaded\r
+  @param  DstBuffer               The buffer to store the image\r
+  @param  EntryPoint              A pointer to the entry point\r
+  @param  Attribute               The bit mask of attributes to set for the load\r
+                                  PE image\r
+\r
+  @retval EFI_SUCCESS             The file was loaded, relocated, and invoked\r
+  @retval EFI_OUT_OF_RESOURCES    There was not enough memory to load and\r
+                                  relocate the PE/COFF file\r
+  @retval EFI_INVALID_PARAMETER   Invalid parameter\r
   @retval EFI_BUFFER_TOO_SMALL    Buffer for image is too small\r
 \r
 **/\r
 EFI_STATUS\r
 CoreLoadPeImage (\r
-  IN BOOLEAN                     BootPolicy,  \r
+  IN BOOLEAN                     BootPolicy,\r
   IN VOID                        *Pe32Handle,\r
   IN LOADED_IMAGE_PRIVATE_DATA   *Image,\r
   IN EFI_PHYSICAL_ADDRESS        DstBuffer    OPTIONAL,\r
@@ -217,7 +223,7 @@ CoreLoadPeImage (
       return EFI_UNSUPPORTED;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Set EFI memory type based on ImageType\r
   //\r
@@ -397,6 +403,7 @@ CoreLoadPeImage (
     //\r
     Status = CoreLocateProtocol (&gEfiEbcProtocolGuid, NULL, (VOID **)&Image->Ebc);\r
     if (EFI_ERROR(Status)) {\r
+      DEBUG ((DEBUG_LOAD | DEBUG_ERROR, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));\r
       goto Done;\r
     }\r
 \r
@@ -416,8 +423,8 @@ CoreLoadPeImage (
     Status = Image->Ebc->CreateThunk (\r
                            Image->Ebc,\r
                            Image->Handle,\r
-                           (VOID *)(UINTN)Image->ImageContext.EntryPoint,\r
-                           (VOID **)&Image->EntryPoint\r
+                           (VOID *)(UINTN) Image->ImageContext.EntryPoint,\r
+                           (VOID **) &Image->EntryPoint\r
                            );\r
     if (EFI_ERROR(Status)) {\r
       goto Done;\r
@@ -465,22 +472,14 @@ CoreLoadPeImage (
     UINTN Index;\r
     UINTN StartIndex;\r
     CHAR8 EfiFileName[256];\r
-    \r
-    if (Image->ImageContext.Machine != IMAGE_FILE_MACHINE_IA64) {\r
-      DEBUG ((DEBUG_INFO | DEBUG_LOAD, \r
-              "Loading driver at 0x%10p EntryPoint=0x%10p ", \r
-              (VOID *)(UINTN)Image->ImageContext.ImageAddress, \r
-              (VOID *)(UINTN)Image->ImageContext.EntryPoint));\r
-    } else {\r
-      //\r
-      // For IPF Image, the real entry point should be print.\r
-      //      \r
-      DEBUG ((DEBUG_INFO | DEBUG_LOAD, \r
-              "Loading driver at 0x%10p EntryPoint=0x%10p ", \r
-              (VOID *)(UINTN)Image->ImageContext.ImageAddress, \r
-              (VOID *)(UINTN)(*(UINT64 *)(UINTN)Image->ImageContext.EntryPoint)));\r
-    }\r
-    \r
+\r
+\r
+    DEBUG ((DEBUG_INFO | DEBUG_LOAD,\r
+           "Loading driver at 0x%11p EntryPoint=0x%11p ",\r
+           (VOID *)(UINTN) Image->ImageContext.ImageAddress,\r
+           FUNCTION_ENTRY_POINT ((UINTN) Image->ImageContext.EntryPoint)));\r
+\r
+\r
     //\r
     // Print Module Name by Pdb file path\r
     //\r
@@ -537,7 +536,7 @@ Done:
 /**\r
   Get the image's private data from its handle.\r
 \r
-  @param  ImageHandle             The image handle \r
+  @param  ImageHandle             The image handle\r
 \r
   @return Return the image private data associated with ImageHandle.\r
 \r
@@ -559,7 +558,7 @@ CoreLoadedImageInfo (
   if (!EFI_ERROR (Status)) {\r
     Image = LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage);\r
   } else {\r
-    DEBUG ((DEBUG_LOAD, "CoreLoadedImageInfo: Not an ImageHandle %x\n", ImageHandle));\r
+    DEBUG ((DEBUG_LOAD, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle));\r
     Image = NULL;\r
   }\r
 \r
@@ -570,40 +569,39 @@ CoreLoadedImageInfo (
 /**\r
   Loads an EFI image into memory and returns a handle to the image.\r
 \r
-  @param  BootPolicy              If TRUE, indicates that the request originates \r
-                                  from the boot manager, and that the boot \r
-                                  manager is attempting to load FilePath as a \r
-                                  boot selection. \r
-  @param  ParentImageHandle       The caller's image handle. \r
-  @param  FilePath                The specific file path from which the image is \r
-                                  loaded. \r
-  @param  SourceBuffer            If not NULL, a pointer to the memory location \r
-                                  containing a copy of the image to be loaded. \r
-  @param  SourceSize              The size in bytes of SourceBuffer. \r
-  @param  DstBuffer               The buffer to store the image \r
-  @param  NumberOfPages           If not NULL, it inputs a pointer to the page \r
-                                  number of DstBuffer and outputs a pointer to \r
-                                  the page number of the image. If this number is \r
-                                  not enough,  return EFI_BUFFER_TOO_SMALL and \r
-                                  this parameter contains the required number. \r
-  @param  ImageHandle             Pointer to the returned image handle that is \r
-                                  created when the image is successfully loaded. \r
-  @param  EntryPoint              A pointer to the entry point \r
-  @param  Attribute               The bit mask of attributes to set for the load \r
-                                  PE image \r
-\r
-  @retval EFI_SUCCESS             The image was loaded into memory. \r
-  @retval EFI_NOT_FOUND           The FilePath was not found. \r
-  @retval EFI_INVALID_PARAMETER   One of the parameters has an invalid value. \r
-  @retval EFI_BUFFER_TOO_SMALL    The buffer is too small \r
-  @retval EFI_UNSUPPORTED         The image type is not supported, or the device \r
-                                  path cannot be parsed to locate the proper \r
-                                  protocol for loading the file. \r
-  @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient \r
+  @param  BootPolicy              If TRUE, indicates that the request originates\r
+                                  from the boot manager, and that the boot\r
+                                  manager is attempting to load FilePath as a\r
+                                  boot selection.\r
+  @param  ParentImageHandle       The caller's image handle.\r
+  @param  FilePath                The specific file path from which the image is\r
+                                  loaded.\r
+  @param  SourceBuffer            If not NULL, a pointer to the memory location\r
+                                  containing a copy of the image to be loaded.\r
+  @param  SourceSize              The size in bytes of SourceBuffer.\r
+  @param  DstBuffer               The buffer to store the image\r
+  @param  NumberOfPages           If not NULL, it inputs a pointer to the page\r
+                                  number of DstBuffer and outputs a pointer to\r
+                                  the page number of the image. If this number is\r
+                                  not enough,  return EFI_BUFFER_TOO_SMALL and\r
+                                  this parameter contains the required number.\r
+  @param  ImageHandle             Pointer to the returned image handle that is\r
+                                  created when the image is successfully loaded.\r
+  @param  EntryPoint              A pointer to the entry point\r
+  @param  Attribute               The bit mask of attributes to set for the load\r
+                                  PE image\r
+\r
+  @retval EFI_SUCCESS             The image was loaded into memory.\r
+  @retval EFI_NOT_FOUND           The FilePath was not found.\r
+  @retval EFI_INVALID_PARAMETER   One of the parameters has an invalid value.\r
+  @retval EFI_BUFFER_TOO_SMALL    The buffer is too small\r
+  @retval EFI_UNSUPPORTED         The image type is not supported, or the device\r
+                                  path cannot be parsed to locate the proper\r
+                                  protocol for loading the file.\r
+  @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient\r
                                   resources.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 CoreLoadImageCommon (\r
   IN  BOOLEAN                          BootPolicy,\r
@@ -699,7 +697,7 @@ CoreLoadImageCommon (
   Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
   if (!EFI_ERROR (Status)) {\r
     FilePathSize = CoreDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
-    FilePath = (EFI_DEVICE_PATH_PROTOCOL *) ( ((UINT8 *)FilePath) + FilePathSize );\r
+    FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );\r
   }\r
 \r
   //\r
@@ -749,7 +747,7 @@ CoreLoadImageCommon (
 \r
   if (NumberOfPages != NULL) {\r
     *NumberOfPages = Image->NumberOfPages;\r
-  }  \r
+  }\r
 \r
   //\r
   // Register the image in the Debug Image Info Table if the attribute is set\r
@@ -827,26 +825,26 @@ Done:
 /**\r
   Loads an EFI image into memory and returns a handle to the image.\r
 \r
-  @param  BootPolicy              If TRUE, indicates that the request originates \r
-                                  from the boot manager, and that the boot \r
-                                  manager is attempting to load FilePath as a \r
-                                  boot selection. \r
-  @param  ParentImageHandle       The caller's image handle. \r
-  @param  FilePath                The specific file path from which the image is \r
-                                  loaded. \r
-  @param  SourceBuffer            If not NULL, a pointer to the memory location \r
-                                  containing a copy of the image to be loaded. \r
-  @param  SourceSize              The size in bytes of SourceBuffer. \r
-  @param  ImageHandle             Pointer to the returned image handle that is \r
-                                  created when the image is successfully loaded. \r
-\r
-  @retval EFI_SUCCESS             The image was loaded into memory. \r
-  @retval EFI_NOT_FOUND           The FilePath was not found. \r
-  @retval EFI_INVALID_PARAMETER   One of the parameters has an invalid value. \r
-  @retval EFI_UNSUPPORTED         The image type is not supported, or the device \r
-                                  path cannot be parsed to locate the proper \r
-                                  protocol for loading the file. \r
-  @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient \r
+  @param  BootPolicy              If TRUE, indicates that the request originates\r
+                                  from the boot manager, and that the boot\r
+                                  manager is attempting to load FilePath as a\r
+                                  boot selection.\r
+  @param  ParentImageHandle       The caller's image handle.\r
+  @param  FilePath                The specific file path from which the image is\r
+                                  loaded.\r
+  @param  SourceBuffer            If not NULL, a pointer to the memory location\r
+                                  containing a copy of the image to be loaded.\r
+  @param  SourceSize              The size in bytes of SourceBuffer.\r
+  @param  ImageHandle             Pointer to the returned image handle that is\r
+                                  created when the image is successfully loaded.\r
+\r
+  @retval EFI_SUCCESS             The image was loaded into memory.\r
+  @retval EFI_NOT_FOUND           The FilePath was not found.\r
+  @retval EFI_INVALID_PARAMETER   One of the parameters has an invalid value.\r
+  @retval EFI_UNSUPPORTED         The image type is not supported, or the device\r
+                                  path cannot be parsed to locate the proper\r
+                                  protocol for loading the file.\r
+  @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient\r
                                   resources.\r
 \r
 **/\r
@@ -888,29 +886,29 @@ CoreLoadImage (
 /**\r
   Loads an EFI image into memory and returns a handle to the image with extended parameters.\r
 \r
-  @param  This                    Calling context \r
-  @param  ParentImageHandle       The caller's image handle. \r
-  @param  FilePath                The specific file path from which the image is \r
-                                  loaded. \r
-  @param  SourceBuffer            If not NULL, a pointer to the memory location \r
-                                  containing a copy of the image to be loaded. \r
-  @param  SourceSize              The size in bytes of SourceBuffer. \r
-  @param  DstBuffer               The buffer to store the image. \r
-  @param  NumberOfPages           For input, specifies the space size of the \r
-                                  image by caller if not NULL. For output, \r
-                                  specifies the actual space size needed. \r
-  @param  ImageHandle             Image handle for output. \r
-  @param  EntryPoint              Image entry point for output. \r
-  @param  Attribute               The bit mask of attributes to set for the load \r
-                                  PE image. \r
-\r
-  @retval EFI_SUCCESS             The image was loaded into memory. \r
-  @retval EFI_NOT_FOUND           The FilePath was not found. \r
-  @retval EFI_INVALID_PARAMETER   One of the parameters has an invalid value. \r
-  @retval EFI_UNSUPPORTED         The image type is not supported, or the device \r
-                                  path cannot be parsed to locate the proper \r
-                                  protocol for loading the file. \r
-  @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient \r
+  @param  This                    Calling context\r
+  @param  ParentImageHandle       The caller's image handle.\r
+  @param  FilePath                The specific file path from which the image is\r
+                                  loaded.\r
+  @param  SourceBuffer            If not NULL, a pointer to the memory location\r
+                                  containing a copy of the image to be loaded.\r
+  @param  SourceSize              The size in bytes of SourceBuffer.\r
+  @param  DstBuffer               The buffer to store the image.\r
+  @param  NumberOfPages           For input, specifies the space size of the\r
+                                  image by caller if not NULL. For output,\r
+                                  specifies the actual space size needed.\r
+  @param  ImageHandle             Image handle for output.\r
+  @param  EntryPoint              Image entry point for output.\r
+  @param  Attribute               The bit mask of attributes to set for the load\r
+                                  PE image.\r
+\r
+  @retval EFI_SUCCESS             The image was loaded into memory.\r
+  @retval EFI_NOT_FOUND           The FilePath was not found.\r
+  @retval EFI_INVALID_PARAMETER   One of the parameters has an invalid value.\r
+  @retval EFI_UNSUPPORTED         The image type is not supported, or the device\r
+                                  path cannot be parsed to locate the proper\r
+                                  protocol for loading the file.\r
+  @retval EFI_OUT_OF_RESOURCES    Image was not loaded due to insufficient\r
                                   resources.\r
 \r
 **/\r
@@ -947,18 +945,18 @@ CoreLoadImageEx (
 /**\r
   Transfer control to a loaded image's entry point.\r
 \r
-  @param  ImageHandle             Handle of image to be started. \r
-  @param  ExitDataSize            Pointer of the size to ExitData \r
-  @param  ExitData                Pointer to a pointer to a data buffer that \r
-                                  includes a Null-terminated Unicode string, \r
-                                  optionally followed by additional binary data. \r
-                                  The string is a description that the caller may \r
-                                  use to further indicate the reason for the \r
-                                  image's exit. \r
-\r
-  @retval EFI_INVALID_PARAMETER   Invalid parameter \r
-  @retval EFI_OUT_OF_RESOURCES    No enough buffer to allocate \r
-  @retval EFI_SUCCESS             Successfully transfer control to the image's \r
+  @param  ImageHandle             Handle of image to be started.\r
+  @param  ExitDataSize            Pointer of the size to ExitData\r
+  @param  ExitData                Pointer to a pointer to a data buffer that\r
+                                  includes a Null-terminated Unicode string,\r
+                                  optionally followed by additional binary data.\r
+                                  The string is a description that the caller may\r
+                                  use to further indicate the reason for the\r
+                                  image's exit.\r
+\r
+  @retval EFI_INVALID_PARAMETER   Invalid parameter\r
+  @retval EFI_OUT_OF_RESOURCES    No enough buffer to allocate\r
+  @retval EFI_SUCCESS             Successfully transfer control to the image's\r
                                   entry point.\r
 \r
 **/\r
@@ -1028,7 +1026,7 @@ CoreStartImage (
     //\r
     DEBUG_CODE_BEGIN ();\r
       if (EFI_ERROR (Image->Status)) {\r
-        DEBUG ((DEBUG_ERROR, "Error: Image at %10p start failed: %r\n", Image->Info.ImageBase, Image->Status));\r
+        DEBUG ((DEBUG_ERROR, "Error: Image at %11p start failed: %r\n", Image->Info.ImageBase, Image->Status));\r
       }\r
     DEBUG_CODE_END ();\r
 \r
@@ -1062,12 +1060,7 @@ CoreStartImage (
   DEBUG_CODE_BEGIN ();\r
     if (Image->ExitDataSize != 0 || Image->ExitData != NULL) {\r
 \r
-      DEBUG (\r
-        (DEBUG_LOAD,\r
-        "StartImage: ExitDataSize %d, ExitData %x",\r
-                            Image->ExitDataSize,\r
-        Image->ExitData)\r
-        );\r
+      DEBUG ((DEBUG_LOAD, "StartImage: ExitDataSize %d, ExitData %x", Image->ExitDataSize, Image->ExitData));\r
       if (Image->ExitData != NULL) {\r
         DEBUG ((DEBUG_LOAD, " (%hs)", Image->ExitData));\r
       }\r
@@ -1114,7 +1107,7 @@ CoreStartImage (
 /**\r
   Unloads EFI image from memory.\r
 \r
-  @param  Image                   EFI image \r
+  @param  Image                   EFI image\r
   @param  FreePage                Free allocated pages\r
 \r
 **/\r
@@ -1257,23 +1250,23 @@ CoreUnloadAndCloseImage (
 /**\r
   Terminates the currently loaded EFI image and returns control to boot services.\r
 \r
-  @param  ImageHandle             Handle that identifies the image. This \r
-                                  parameter is passed to the image on entry. \r
-  @param  Status                  The image's exit code. \r
-  @param  ExitDataSize            The size, in bytes, of ExitData. Ignored if \r
-                                  ExitStatus is EFI_SUCCESS. \r
-  @param  ExitData                Pointer to a data buffer that includes a \r
-                                  Null-terminated Unicode string, optionally \r
-                                  followed by additional binary data. The string \r
-                                  is a description that the caller may use to \r
-                                  further indicate the reason for the image's \r
-                                  exit. \r
-\r
-  @retval EFI_INVALID_PARAMETER   Image handle is NULL or it is not current \r
-                                  image. \r
-  @retval EFI_SUCCESS             Successfully terminates the currently loaded \r
-                                  EFI image. \r
-  @retval EFI_ACCESS_DENIED       Should never reach there. \r
+  @param  ImageHandle             Handle that identifies the image. This\r
+                                  parameter is passed to the image on entry.\r
+  @param  Status                  The image's exit code.\r
+  @param  ExitDataSize            The size, in bytes, of ExitData. Ignored if\r
+                                  ExitStatus is EFI_SUCCESS.\r
+  @param  ExitData                Pointer to a data buffer that includes a\r
+                                  Null-terminated Unicode string, optionally\r
+                                  followed by additional binary data. The string\r
+                                  is a description that the caller may use to\r
+                                  further indicate the reason for the image's\r
+                                  exit.\r
+\r
+  @retval EFI_INVALID_PARAMETER   Image handle is NULL or it is not current\r
+                                  image.\r
+  @retval EFI_SUCCESS             Successfully terminates the currently loaded\r
+                                  EFI image.\r
+  @retval EFI_ACCESS_DENIED       Should never reach there.\r
   @retval EFI_OUT_OF_RESOURCES    Could not allocate pool\r
 \r
 **/\r
@@ -1292,9 +1285,9 @@ CoreExit (
   //\r
   // Prevent possible reentrance to this function\r
   // for the same ImageHandle\r
-  // \r
-  OldTpl = CoreRaiseTpl (TPL_NOTIFY); \r
\r
+  //\r
+  OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
+\r
   Image = CoreLoadedImageInfo (ImageHandle);\r
   if (Image == NULL_HANDLE) {\r
     Status = EFI_INVALID_PARAMETER;\r
@@ -1359,12 +1352,12 @@ Done:
 /**\r
   Unloads an image.\r
 \r
-  @param  ImageHandle             Handle that identifies the image to be \r
-                                  unloaded. \r
+  @param  ImageHandle             Handle that identifies the image to be\r
+                                  unloaded.\r
 \r
-  @retval EFI_SUCCESS             The image has been unloaded. \r
-  @retval EFI_UNSUPPORTED         The image has been sarted, and does not support \r
-                                  unload. \r
+  @retval EFI_SUCCESS             The image has been unloaded.\r
+  @retval EFI_UNSUPPORTED         The image has been sarted, and does not support\r
+                                  unload.\r
   @retval EFI_INVALID_PARAMPETER  ImageHandle is not a valid image handle.\r
 \r
 **/\r
@@ -1376,14 +1369,15 @@ CoreUnloadImage (
 {\r
   EFI_STATUS                 Status;\r
   LOADED_IMAGE_PRIVATE_DATA  *Image;\r
-  EFI_TPL                    OldTpl;\r
 \r
   //\r
   // Prevent possible reentrance to this function\r
   // for the same ImageHandle\r
-  // \r
-  OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
\r
+  //\r
+  if (!AcquireSpinLockOrFail (&mUnloadImageLock)) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   Image = CoreLoadedImageInfo (ImageHandle);\r
   if (Image == NULL ) {\r
     //\r
@@ -1418,7 +1412,7 @@ CoreUnloadImage (
   }\r
 \r
 Done:\r
-  CoreRestoreTpl (OldTpl);\r
+  ReleaseSpinLock (&mUnloadImageLock);\r
   return Status;\r
 }\r
 \r
@@ -1427,11 +1421,11 @@ Done:
 /**\r
   Unload the specified image.\r
 \r
-  @param  This                    Indicates the calling context. \r
-  @param  ImageHandle             The specified image handle. \r
+  @param  This                    Indicates the calling context.\r
+  @param  ImageHandle             The specified image handle.\r
 \r
-  @retval EFI_INVALID_PARAMETER   Image handle is NULL. \r
-  @retval EFI_UNSUPPORTED         Attempt to unload an unsupported image. \r
+  @retval EFI_INVALID_PARAMETER   Image handle is NULL.\r
+  @retval EFI_UNSUPPORTED         Attempt to unload an unsupported image.\r
   @retval EFI_SUCCESS             Image successfully unloaded.\r
 \r
 **/\r