]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Image/Image.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / Image.c
index 6e34ffd3dbd128cb23df1252fb5cb642ced41056..1bf799ea2d3a0139cdc2e3886ce9e2875a58fcfe 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Core image handling services to load and unload PeImage.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+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
 http://opensource.org/licenses/bsd-license.php\r
@@ -13,12 +13,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include "DxeMain.h"\r
+#include "Image.h"\r
+\r
 //\r
 // 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
@@ -71,8 +70,12 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage  = {
   NULL,                       // RuntimeData\r
   NULL                        // LoadedImageDevicePath\r
 };\r
-\r
-\r
+//\r
+// The field is define for Loading modules at fixed address feature to tracker the PEI code\r
+// memory range usage. It is a bit mapped array in which every bit indicates the correspoding memory page\r
+// available or not. \r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED    UINT64                *mDxeCodeMemoryRangeUsageBitMap=NULL;\r
 \r
 /**\r
   Add the Image Services to EFI Boot Services Table and install the protocol\r
@@ -139,29 +142,234 @@ CoreInitializeImageServices (
 \r
   mCurrentImage = Image;\r
 \r
-  //\r
-  // Initialize spin lock\r
-  //\r
-  InitializeSpinLock (&mUnloadImageLock);\r
-\r
   //\r
   // Fill in DXE globals\r
   //\r
   gDxeCoreImageHandle = Image->Handle;\r
   gDxeCoreLoadedImage = &Image->Info;\r
 \r
+  if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {\r
+    //\r
+    // Export DXE Core PE Loader functionality for backward compatibility.\r
+    //\r
+    Status = CoreInstallProtocolInterface (\r
+      &mLoadPe32PrivateData.Handle,\r
+      &gEfiLoadPeImageProtocolGuid,\r
+      EFI_NATIVE_INTERFACE,\r
+      &mLoadPe32PrivateData.Pe32Image\r
+      );\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Read image file (specified by UserHandle) into user specified buffer with specified offset\r
+  and length.\r
+\r
+  @param  UserHandle             Image file handle\r
+  @param  Offset                 Offset to the source file\r
+  @param  ReadSize               For input, pointer of size to read; For output,\r
+                                 pointer of size actually read.\r
+  @param  Buffer                 Buffer to write into\r
+\r
+  @retval EFI_SUCCESS            Successfully read the specified part of file\r
+                                 into buffer.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CoreReadImageFile (\r
+  IN     VOID    *UserHandle,\r
+  IN     UINTN   Offset,\r
+  IN OUT UINTN   *ReadSize,\r
+  OUT    VOID    *Buffer\r
+  )\r
+{\r
+  UINTN               EndPosition;\r
+  IMAGE_FILE_HANDLE  *FHand;\r
+\r
+  FHand = (IMAGE_FILE_HANDLE  *)UserHandle;\r
+  ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE);\r
+\r
   //\r
-  // Export DXE Core PE Loader functionality\r
+  // Move data from our local copy of the file\r
   //\r
-  return CoreInstallProtocolInterface (\r
-           &mLoadPe32PrivateData.Handle,\r
-           &gEfiLoadPeImageProtocolGuid,\r
-           EFI_NATIVE_INTERFACE,\r
-           &mLoadPe32PrivateData.Pe32Image\r
-           );\r
+  EndPosition = Offset + *ReadSize;\r
+  if (EndPosition > FHand->SourceSize) {\r
+    *ReadSize = (UINT32)(FHand->SourceSize - Offset);\r
+  }\r
+  if (Offset >= FHand->SourceSize) {\r
+      *ReadSize = 0;\r
+  }\r
+\r
+  CopyMem (Buffer, (CHAR8 *)FHand->Source + Offset, *ReadSize);\r
+  return EFI_SUCCESS;\r
+}\r
+/**\r
+  To check memory usage bit map arry to figure out if the memory range the image will be loaded in is available or not. If \r
+  memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.\r
+  The function is only invoked when load modules at fixed address feature is enabled. \r
+  \r
+  @param  ImageBase                The base addres the image will be loaded at.\r
+  @param  ImageSize                The size of the image\r
+  \r
+  @retval EFI_SUCCESS              The memory range the image will be loaded in is available\r
+  @retval EFI_NOT_FOUND            The memory range the image will be loaded in is not available\r
+**/\r
+EFI_STATUS\r
+CheckAndMarkFixLoadingMemoryUsageBitMap (\r
+  IN  EFI_PHYSICAL_ADDRESS          ImageBase,\r
+  IN  UINTN                         ImageSize\r
+  )\r
+{\r
+   UINT32                             DxeCodePageNumber;\r
+   UINT64                             DxeCodeSize; \r
+   EFI_PHYSICAL_ADDRESS               DxeCodeBase;\r
+   UINTN                              BaseOffsetPageNumber;\r
+   UINTN                              TopOffsetPageNumber;\r
+   UINTN                              Index;\r
+   //\r
+   // The DXE code range includes RuntimeCodePage range and Boot time code range.\r
+   //  \r
+   DxeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
+   DxeCodePageNumber += PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
+   DxeCodeSize       = EFI_PAGES_TO_SIZE(DxeCodePageNumber);\r
+   DxeCodeBase       =  gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - DxeCodeSize;\r
+   \r
+   //\r
+   // If the memory usage bit map is not initialized,  do it. Every bit in the array \r
+   // indicate the status of the corresponding memory page, available or not\r
+   // \r
+   if (mDxeCodeMemoryRangeUsageBitMap == NULL) {\r
+     mDxeCodeMemoryRangeUsageBitMap = AllocateZeroPool(((DxeCodePageNumber/64) + 1)*sizeof(UINT64));\r
+   }\r
+   //\r
+   // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND\r
+   //\r
+   if (!gLoadFixedAddressCodeMemoryReady || mDxeCodeMemoryRangeUsageBitMap == NULL) {\r
+     return EFI_NOT_FOUND;\r
+   }\r
+   //\r
+   // Test the memory range for loading the image in the DXE code range.\r
+   //\r
+   if (gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress <  ImageBase + ImageSize ||\r
+       DxeCodeBase >  ImageBase) {\r
+     return EFI_NOT_FOUND;   \r
+   }   \r
+   //\r
+   // Test if the memory is avalaible or not.\r
+   // \r
+   BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - DxeCodeBase));\r
+   TopOffsetPageNumber  = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - DxeCodeBase));\r
+   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
+     if ((mDxeCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {\r
+       //\r
+       // This page is already used.\r
+       //\r
+       return EFI_NOT_FOUND;  \r
+     }\r
+   }\r
+   \r
+   //\r
+   // Being here means the memory range is available.  So mark the bits for the memory range\r
+   // \r
+   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
+     mDxeCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));\r
+   }\r
+   return  EFI_SUCCESS;   \r
 }\r
+/**\r
+\r
+  Get the fixed loadding address from image header assigned by build tool. This function only be called\r
+  when Loading module at Fixed address feature enabled.\r
 \r
+  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
+                                    image that needs to be examined by this function.\r
+  @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .\r
+  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.\r
 \r
+**/\r
+EFI_STATUS\r
+GetPeCoffImageFixLoadingAssignedAddress(\r
+  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
+  )\r
+{\r
+   UINTN                              SectionHeaderOffset;\r
+   EFI_STATUS                         Status;\r
+   EFI_IMAGE_SECTION_HEADER           SectionHeader;\r
+   EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;\r
+   UINT16                             Index;\r
+   UINTN                              Size;\r
+   UINT16                             NumberOfSections;\r
+   IMAGE_FILE_HANDLE                  *Handle;\r
+   UINT64                             ValueInSectionHeader;\r
+                             \r
+\r
+   Status = EFI_NOT_FOUND;\r
\r
+   //\r
+   // Get PeHeader pointer\r
+   //\r
+   Handle = (IMAGE_FILE_HANDLE*)ImageContext->Handle;\r
+   ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )Handle->Source + ImageContext->PeCoffHeaderOffset);\r
+   SectionHeaderOffset = (UINTN)(\r
+                                 ImageContext->PeCoffHeaderOffset +\r
+                                 sizeof (UINT32) +\r
+                                 sizeof (EFI_IMAGE_FILE_HEADER) +\r
+                                 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
+                                 );\r
+   NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
+\r
+   //\r
+   // Get base address from the first section header that doesn't point to code section.\r
+   //\r
+   for (Index = 0; Index < NumberOfSections; Index++) {\r
+     //\r
+     // Read section header from file\r
+     //\r
+     Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
+     Status = ImageContext->ImageRead (\r
+                              ImageContext->Handle,\r
+                              SectionHeaderOffset,\r
+                              &Size,\r
+                              &SectionHeader\r
+                              );\r
+     if (EFI_ERROR (Status)) {\r
+       return Status;\r
+     }\r
+     \r
+     Status = EFI_NOT_FOUND;\r
+     \r
+     if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
+       //\r
+       // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header\r
+       // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an \r
+       // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations  \r
+       // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero\r
+       //\r
+       ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
+       if (ValueInSectionHeader != 0) {\r
+         //\r
+         // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext \r
+         // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset\r
+         // relative to top address\r
+         //\r
+         if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) < 0) {\r
+                ImageContext->ImageAddress = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress + (INT64)(INTN)ImageContext->ImageAddress;\r
+         }\r
+         //\r
+         // Check if the memory range is avaliable.\r
+         //\r
+         Status = CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext->ImageAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));\r
+       }\r
+       break; \r
+     }\r
+     SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
+   }\r
+   DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status = %r \n", (VOID *)(UINTN)(ImageContext->ImageAddress), Status));\r
+   return Status;\r
+}\r
 /**\r
   Loads, relocates, and invokes a PE/COFF image\r
 \r
@@ -196,11 +404,7 @@ CoreLoadPeImage (
   EFI_STATUS                Status;\r
   BOOLEAN                   DstBufAlocated;\r
   UINTN                     Size;\r
-  UINTN                     LinkTimeBase;\r
-  EFI_TCG_PLATFORM_PROTOCOL *TcgPlatformProtocol;\r
-  IMAGE_FILE_HANDLE         *FHandle;\r
 \r
-  FHandle = NULL;\r
   ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext));\r
 \r
   Image->ImageContext.Handle    = Pe32Handle;\r
@@ -245,10 +449,6 @@ CoreLoadPeImage (
     Image->ImageContext.ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;\r
     return EFI_UNSUPPORTED;\r
   }\r
-  //\r
-  // Get the image base address in the original PeImage.\r
-  //\r
-  LinkTimeBase = (UINTN) Image->ImageContext.ImageAddress;\r
 \r
   //\r
   // Allocate memory of the correct memory type aligned on the required image boundry\r
@@ -275,21 +475,43 @@ CoreLoadPeImage (
     // no modules whose preferred load addresses are below 1MB.\r
     //\r
     Status = EFI_OUT_OF_RESOURCES;\r
-    if (Image->ImageContext.ImageAddress >= 0x100000 || Image->ImageContext.RelocationsStripped) {\r
-      Status = CoreAllocatePages (\r
-                 AllocateAddress,\r
-                 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),\r
-                 Image->NumberOfPages,\r
-                 &Image->ImageContext.ImageAddress\r
-                 );\r
-    }\r
-    if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) {\r
-      Status = CoreAllocatePages (\r
-                 AllocateAnyPages,\r
-                 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),\r
-                 Image->NumberOfPages,\r
-                 &Image->ImageContext.ImageAddress\r
-                 );\r
+    //\r
+    // If Loading Module At Fixed Address feature is enabled, the module should be loaded to\r
+    // a specified address.\r
+    //\r
+    if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 ) {\r
+      Status = GetPeCoffImageFixLoadingAssignedAddress (&(Image->ImageContext));\r
+\r
+      if (EFI_ERROR (Status))  {\r
+          //\r
+         // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver.\r
+         //\r
+          DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n"));\r
+        \r
+          Status = CoreAllocatePages (\r
+                     AllocateAnyPages,\r
+                     (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),\r
+                     Image->NumberOfPages,\r
+                     &Image->ImageContext.ImageAddress\r
+                     );         \r
+      } \r
+    } else {\r
+      if (Image->ImageContext.ImageAddress >= 0x100000 || Image->ImageContext.RelocationsStripped) {\r
+        Status = CoreAllocatePages (\r
+                   AllocateAddress,\r
+                   (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),\r
+                   Image->NumberOfPages,\r
+                   &Image->ImageContext.ImageAddress\r
+                   );\r
+      }\r
+      if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) {\r
+        Status = CoreAllocatePages (\r
+                   AllocateAnyPages,\r
+                   (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),\r
+                   Image->NumberOfPages,\r
+                   &Image->ImageContext.ImageAddress\r
+                   );\r
+      }\r
     }\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
@@ -321,9 +543,11 @@ CoreLoadPeImage (
   }\r
 \r
   Image->ImageBasePage = Image->ImageContext.ImageAddress;\r
-  Image->ImageContext.ImageAddress =\r
-      (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) &\r
-      ~((UINTN)Image->ImageContext.SectionAlignment - 1);\r
+  if (!Image->ImageContext.IsTeImage) {\r
+    Image->ImageContext.ImageAddress =\r
+        (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) &\r
+        ~((UINTN)Image->ImageContext.SectionAlignment - 1);\r
+  }\r
 \r
   //\r
   // Load the image from the file into the allocated memory\r
@@ -348,29 +572,6 @@ CoreLoadPeImage (
     }\r
   }\r
 \r
-  //\r
-  // Measure the image before applying fixup\r
-  //\r
-  Status = CoreLocateProtocol (\r
-             &gEfiTcgPlatformProtocolGuid,\r
-             NULL,\r
-             (VOID **) &TcgPlatformProtocol\r
-             );\r
-  if (!EFI_ERROR (Status)) {\r
-    FHandle = (IMAGE_FILE_HANDLE *) Image->ImageContext.Handle;\r
-    Status = TcgPlatformProtocol->MeasurePeImage (\r
-                                    BootPolicy,\r
-                                    (EFI_PHYSICAL_ADDRESS) (UINTN) FHandle->Source,\r
-                                    FHandle->SourceSize,\r
-                                    LinkTimeBase,\r
-                                    Image->ImageContext.ImageType,\r
-                                    Image->Info.DeviceHandle,\r
-                                    Image->Info.FilePath\r
-                                    );\r
-\r
-    ASSERT_EFI_ERROR (Status);\r
-  }\r
-\r
   //\r
   // Relocate the image in memory\r
   //\r
@@ -402,7 +603,7 @@ CoreLoadPeImage (
     // Locate the EBC interpreter protocol\r
     //\r
     Status = CoreLocateProtocol (&gEfiEbcProtocolGuid, NULL, (VOID **)&Image->Ebc);\r
-    if (EFI_ERROR(Status)) {\r
+    if (EFI_ERROR(Status) || Image->Ebc == NULL) {\r
       DEBUG ((DEBUG_LOAD | DEBUG_ERROR, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n"));\r
       goto Done;\r
     }\r
@@ -477,23 +678,26 @@ CoreLoadPeImage (
     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
+           FUNCTION_ENTRY_POINT (Image->ImageContext.EntryPoint)));\r
 \r
 \r
     //\r
-    // Print Module Name by Pdb file path\r
+    // Print Module Name by Pdb file path.\r
+    // Windows and Unix style file path are all trimmed correctly.\r
     //\r
     if (Image->ImageContext.PdbPointer != NULL) {\r
       StartIndex = 0;\r
       for (Index = 0; Image->ImageContext.PdbPointer[Index] != 0; Index++) {\r
-        if (Image->ImageContext.PdbPointer[Index] == '\\') {\r
+        if ((Image->ImageContext.PdbPointer[Index] == '\\') || (Image->ImageContext.PdbPointer[Index] == '/')) {\r
           StartIndex = Index + 1;\r
         }\r
       }\r
       //\r
       // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
+      // The PDB file name is limited in the range of 0~255.\r
+      // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.\r
       //\r
-      for (Index = 0; Index < sizeof (EfiFileName); Index++) {\r
+      for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
         EfiFileName[Index] = Image->ImageContext.PdbPointer[Index + StartIndex];\r
         if (EfiFileName[Index] == 0) {\r
           EfiFileName[Index] = '.';\r
@@ -506,6 +710,10 @@ CoreLoadPeImage (
           break;\r
         }\r
       }\r
+\r
+      if (Index == sizeof (EfiFileName) - 4) {\r
+        EfiFileName[Index] = 0;\r
+      }\r
       DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));\r
     }\r
     DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
@@ -566,6 +774,155 @@ CoreLoadedImageInfo (
 }\r
 \r
 \r
+/**\r
+  Unloads EFI image from memory.\r
+\r
+  @param  Image                   EFI image\r
+  @param  FreePage                Free allocated pages\r
+\r
+**/\r
+VOID\r
+CoreUnloadAndCloseImage (\r
+  IN LOADED_IMAGE_PRIVATE_DATA  *Image,\r
+  IN BOOLEAN                    FreePage\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+  UINTN                               HandleCount;\r
+  EFI_HANDLE                          *HandleBuffer;\r
+  UINTN                               HandleIndex;\r
+  EFI_GUID                            **ProtocolGuidArray;\r
+  UINTN                               ArrayCount;\r
+  UINTN                               ProtocolIndex;\r
+  EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;\r
+  UINTN                               OpenInfoCount;\r
+  UINTN                               OpenInfoIndex;\r
+\r
+  if (Image->Ebc != NULL) {\r
+    //\r
+    // If EBC protocol exists we must perform cleanups for this image.\r
+    //\r
+    Image->Ebc->UnloadImage (Image->Ebc, Image->Handle);\r
+  }\r
+\r
+  //\r
+  // Unload image, free Image->ImageContext->ModHandle\r
+  //\r
+  PeCoffLoaderUnloadImage (&Image->ImageContext);\r
+\r
+  //\r
+  // Free our references to the image handle\r
+  //\r
+  if (Image->Handle != NULL) {\r
+\r
+    Status = CoreLocateHandleBuffer (\r
+               AllHandles,\r
+               NULL,\r
+               NULL,\r
+               &HandleCount,\r
+               &HandleBuffer\r
+               );\r
+    if (!EFI_ERROR (Status)) {\r
+      for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r
+        Status = CoreProtocolsPerHandle (\r
+                   HandleBuffer[HandleIndex],\r
+                   &ProtocolGuidArray,\r
+                   &ArrayCount\r
+                   );\r
+        if (!EFI_ERROR (Status)) {\r
+          for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {\r
+            Status = CoreOpenProtocolInformation (\r
+                       HandleBuffer[HandleIndex],\r
+                       ProtocolGuidArray[ProtocolIndex],\r
+                       &OpenInfo,\r
+                       &OpenInfoCount\r
+                       );\r
+            if (!EFI_ERROR (Status)) {\r
+              for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {\r
+                if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) {\r
+                  Status = CoreCloseProtocol (\r
+                             HandleBuffer[HandleIndex],\r
+                             ProtocolGuidArray[ProtocolIndex],\r
+                             Image->Handle,\r
+                             OpenInfo[OpenInfoIndex].ControllerHandle\r
+                             );\r
+                }\r
+              }\r
+              if (OpenInfo != NULL) {\r
+                CoreFreePool(OpenInfo);\r
+              }\r
+            }\r
+          }\r
+          if (ProtocolGuidArray != NULL) {\r
+            CoreFreePool(ProtocolGuidArray);\r
+          }\r
+        }\r
+      }\r
+      if (HandleBuffer != NULL) {\r
+        CoreFreePool (HandleBuffer);\r
+      }\r
+    }\r
+\r
+    CoreRemoveDebugImageInfoEntry (Image->Handle);\r
+\r
+    Status = CoreUninstallProtocolInterface (\r
+               Image->Handle,\r
+               &gEfiLoadedImageDevicePathProtocolGuid,\r
+               Image->LoadedImageDevicePath\r
+               );\r
+\r
+    Status = CoreUninstallProtocolInterface (\r
+               Image->Handle,\r
+               &gEfiLoadedImageProtocolGuid,\r
+               &Image->Info\r
+               );\r
+\r
+    if (Image->ImageContext.HiiResourceData != 0) {\r
+      Status = CoreUninstallProtocolInterface (\r
+                 Image->Handle,\r
+                 &gEfiHiiPackageListProtocolGuid,\r
+                 (VOID *) (UINTN) Image->ImageContext.HiiResourceData\r
+                 );\r
+    }\r
+\r
+  }\r
+\r
+  if (Image->RuntimeData != NULL) {\r
+    if (Image->RuntimeData->Link.ForwardLink != NULL) {\r
+      //\r
+      // Remove the Image from the Runtime Image list as we are about to Free it!\r
+      //\r
+      RemoveEntryList (&Image->RuntimeData->Link);\r
+    }\r
+    CoreFreePool (Image->RuntimeData);\r
+  }\r
+\r
+  //\r
+  // Free the Image from memory\r
+  //\r
+  if ((Image->ImageBasePage != 0) && FreePage) {\r
+    CoreFreePages (Image->ImageBasePage, Image->NumberOfPages);\r
+  }\r
+\r
+  //\r
+  // Done with the Image structure\r
+  //\r
+  if (Image->Info.FilePath != NULL) {\r
+    CoreFreePool (Image->Info.FilePath);\r
+  }\r
+\r
+  if (Image->LoadedImageDevicePath != NULL) {\r
+    CoreFreePool (Image->LoadedImageDevicePath);\r
+  }\r
+\r
+  if (Image->FixupData != NULL) {\r
+    CoreFreePool (Image->FixupData);\r
+  }\r
+\r
+  CoreFreePool (Image);\r
+}\r
+\r
+\r
 /**\r
   Loads an EFI image into memory and returns a handle to the image.\r
 \r
@@ -645,19 +1002,63 @@ CoreLoadImageCommon (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  //\r
-  // Get simple read access to the source file\r
-  //\r
+  ZeroMem (&FHand, sizeof (IMAGE_FILE_HANDLE));\r
+  FHand.Signature  = IMAGE_FILE_HANDLE_SIGNATURE;\r
   OriginalFilePath = FilePath;\r
-  Status = CoreOpenImageFile (\r
-             BootPolicy,\r
-             SourceBuffer,\r
-             SourceSize,\r
-             &FilePath,\r
-             &DeviceHandle,\r
-             &FHand,\r
-             &AuthenticationStatus\r
-             );\r
+  HandleFilePath   = FilePath;\r
+  DeviceHandle     = NULL;\r
+  Status           = EFI_SUCCESS;\r
+  AuthenticationStatus = 0;\r
+  //\r
+  // If the caller passed a copy of the file, then just use it\r
+  //\r
+  if (SourceBuffer != NULL) {\r
+    FHand.Source     = SourceBuffer;\r
+    FHand.SourceSize = SourceSize;\r
+    CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+    if (SourceSize > 0) {\r
+      Status = EFI_SUCCESS;\r
+    } else {\r
+      Status = EFI_LOAD_ERROR;\r
+    }\r
+  } else {\r
+    if (FilePath == NULL) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Get the source file buffer by its device path.\r
+    //\r
+    FHand.Source = GetFileBufferByFilePath (\r
+                      BootPolicy, \r
+                      FilePath,\r
+                      &FHand.SourceSize,\r
+                      &AuthenticationStatus\r
+                      );\r
+    if (FHand.Source == NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    } else {\r
+      //\r
+      // Try to get the image device handle by checking the match protocol.\r
+      //\r
+      FHand.FreeBuffer = TRUE;\r
+      Status = CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+      if (EFI_ERROR (Status)) {\r
+        HandleFilePath = FilePath;\r
+        Status = CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+        if (EFI_ERROR (Status)) {\r
+          if (!BootPolicy) {\r
+            HandleFilePath = FilePath;\r
+            Status = CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+          }\r
+          if (EFI_ERROR (Status)) {\r
+            HandleFilePath = FilePath;\r
+            Status = CoreLocateDevicePath (&gEfiLoadFileProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
   if (Status == EFI_ALREADY_STARTED) {\r
     Image = NULL;\r
     goto Done;\r
@@ -694,12 +1095,13 @@ CoreLoadImageCommon (
   // Pull out just the file portion of the DevicePath for the LoadedImage FilePath\r
   //\r
   FilePath = OriginalFilePath;\r
-  Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
-  if (!EFI_ERROR (Status)) {\r
-    FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
-    FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );\r
+  if (DeviceHandle != NULL) {\r
+    Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
+    if (!EFI_ERROR (Status)) {\r
+      FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
+      FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );\r
+    }\r
   }\r
-\r
   //\r
   // Initialize the fields for an internal driver\r
   //\r
@@ -790,6 +1192,21 @@ CoreLoadImageCommon (
     goto Done;\r
   }\r
 \r
+  //\r
+  // Install HII Package List Protocol onto the image handle\r
+  //\r
+  if (Image->ImageContext.HiiResourceData != 0) {\r
+    Status = CoreInstallProtocolInterface (\r
+               &Image->Handle,\r
+               &gEfiHiiPackageListProtocolGuid,\r
+               EFI_NATIVE_INTERFACE,\r
+               (VOID *) (UINTN) Image->ImageContext.HiiResourceData\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
+    }\r
+  }\r
+\r
   //\r
   // Success.  Return the image handle\r
   //\r
@@ -860,8 +1277,12 @@ CoreLoadImage (
   )\r
 {\r
   EFI_STATUS    Status;\r
+  UINT64        Tick;\r
 \r
-  PERF_START (NULL, "LoadImage", NULL, 0);\r
+  Tick = 0;\r
+  PERF_CODE (\r
+    Tick = GetPerformanceCounter ();\r
+  );\r
 \r
   Status = CoreLoadImageCommon (\r
              BootPolicy,\r
@@ -876,7 +1297,8 @@ CoreLoadImage (
              EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION | EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION\r
              );\r
 \r
-  PERF_END (NULL, "LoadImage", NULL, 0);\r
+  PERF_START (*ImageHandle, "LoadImage:", NULL, Tick);\r
+  PERF_END (*ImageHandle, "LoadImage:", NULL, 0);\r
 \r
   return Status;\r
 }\r
@@ -979,10 +1401,18 @@ CoreStartImage (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // The image to be started must have the machine type supported by DxeCore.\r
+  //\r
+  ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine));\r
+  if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   //\r
   // Don't profile Objects or invalid start requests\r
   //\r
-  PERF_START (ImageHandle, START_IMAGE_TOK, NULL, 0);\r
+  PERF_START (ImageHandle, "StartImage:", NULL, 0);\r
 \r
 \r
   //\r
@@ -1002,7 +1432,7 @@ CoreStartImage (
   //\r
   Image->JumpBuffer = AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);\r
   if (Image->JumpBuffer == NULL) {\r
-    PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0);\r
+    PERF_END (ImageHandle, "StartImage:", NULL, 0);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   Image->JumpContext = ALIGN_POINTER (Image->JumpBuffer, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);\r
@@ -1060,7 +1490,7 @@ CoreStartImage (
   DEBUG_CODE_BEGIN ();\r
     if (Image->ExitDataSize != 0 || Image->ExitData != NULL) {\r
 \r
-      DEBUG ((DEBUG_LOAD, "StartImage: ExitDataSize %d, ExitData %x", Image->ExitDataSize, Image->ExitData));\r
+      DEBUG ((DEBUG_LOAD, "StartImage: ExitDataSize %d, ExitData %p", (UINT32)Image->ExitDataSize, Image->ExitData));\r
       if (Image->ExitData != NULL) {\r
         DEBUG ((DEBUG_LOAD, " (%hs)", Image->ExitData));\r
       }\r
@@ -1098,155 +1528,10 @@ CoreStartImage (
   //\r
   // Done\r
   //\r
-  PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0);\r
+  PERF_END (ImageHandle, "StartImage:", NULL, 0);\r
   return Status;\r
 }\r
 \r
-\r
-\r
-/**\r
-  Unloads EFI image from memory.\r
-\r
-  @param  Image                   EFI image\r
-  @param  FreePage                Free allocated pages\r
-\r
-**/\r
-VOID\r
-CoreUnloadAndCloseImage (\r
-  IN LOADED_IMAGE_PRIVATE_DATA  *Image,\r
-  IN BOOLEAN                    FreePage\r
-  )\r
-{\r
-  EFI_STATUS                          Status;\r
-  UINTN                               HandleCount;\r
-  EFI_HANDLE                          *HandleBuffer;\r
-  UINTN                               HandleIndex;\r
-  EFI_GUID                            **ProtocolGuidArray;\r
-  UINTN                               ArrayCount;\r
-  UINTN                               ProtocolIndex;\r
-  EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;\r
-  UINTN                               OpenInfoCount;\r
-  UINTN                               OpenInfoIndex;\r
-\r
-  if (Image->Ebc != NULL) {\r
-    //\r
-    // If EBC protocol exists we must perform cleanups for this image.\r
-    //\r
-    Image->Ebc->UnloadImage (Image->Ebc, Image->Handle);\r
-  }\r
-\r
-  //\r
-  // Unload image, free Image->ImageContext->ModHandle\r
-  //\r
-  PeCoffLoaderUnloadImage (&Image->ImageContext);\r
-\r
-  //\r
-  // Free our references to the image handle\r
-  //\r
-  if (Image->Handle != NULL) {\r
-\r
-    Status = CoreLocateHandleBuffer (\r
-               AllHandles,\r
-               NULL,\r
-               NULL,\r
-               &HandleCount,\r
-               &HandleBuffer\r
-               );\r
-    if (!EFI_ERROR (Status)) {\r
-      for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r
-        Status = CoreProtocolsPerHandle (\r
-                   HandleBuffer[HandleIndex],\r
-                   &ProtocolGuidArray,\r
-                   &ArrayCount\r
-                   );\r
-        if (!EFI_ERROR (Status)) {\r
-          for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {\r
-            Status = CoreOpenProtocolInformation (\r
-                       HandleBuffer[HandleIndex],\r
-                       ProtocolGuidArray[ProtocolIndex],\r
-                       &OpenInfo,\r
-                       &OpenInfoCount\r
-                       );\r
-            if (!EFI_ERROR (Status)) {\r
-              for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {\r
-                if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) {\r
-                  Status = CoreCloseProtocol (\r
-                             HandleBuffer[HandleIndex],\r
-                             ProtocolGuidArray[ProtocolIndex],\r
-                             Image->Handle,\r
-                             OpenInfo[OpenInfoIndex].ControllerHandle\r
-                             );\r
-                }\r
-              }\r
-              if (OpenInfo != NULL) {\r
-                CoreFreePool(OpenInfo);\r
-              }\r
-            }\r
-          }\r
-          if (ProtocolGuidArray != NULL) {\r
-            CoreFreePool(ProtocolGuidArray);\r
-          }\r
-        }\r
-      }\r
-      if (HandleBuffer != NULL) {\r
-        CoreFreePool (HandleBuffer);\r
-      }\r
-    }\r
-\r
-    CoreRemoveDebugImageInfoEntry (Image->Handle);\r
-\r
-    Status = CoreUninstallProtocolInterface (\r
-               Image->Handle,\r
-               &gEfiLoadedImageDevicePathProtocolGuid,\r
-               Image->LoadedImageDevicePath\r
-               );\r
-\r
-    Status = CoreUninstallProtocolInterface (\r
-               Image->Handle,\r
-               &gEfiLoadedImageProtocolGuid,\r
-               &Image->Info\r
-               );\r
-\r
-  }\r
-\r
-  if (Image->RuntimeData != NULL) {\r
-    if (Image->RuntimeData->Link.ForwardLink != NULL) {\r
-      //\r
-      // Remove the Image from the Runtime Image list as we are about to Free it!\r
-      //\r
-      RemoveEntryList (&Image->RuntimeData->Link);\r
-    }\r
-    CoreFreePool (Image->RuntimeData);\r
-  }\r
-\r
-  //\r
-  // Free the Image from memory\r
-  //\r
-  if ((Image->ImageBasePage != 0) && FreePage) {\r
-    CoreFreePages (Image->ImageBasePage, Image->NumberOfPages);\r
-  }\r
-\r
-  //\r
-  // Done with the Image structure\r
-  //\r
-  if (Image->Info.FilePath != NULL) {\r
-    CoreFreePool (Image->Info.FilePath);\r
-  }\r
-\r
-  if (Image->LoadedImageDevicePath != NULL) {\r
-    CoreFreePool (Image->LoadedImageDevicePath);\r
-  }\r
-\r
-  if (Image->FixupData != NULL) {\r
-    CoreFreePool (Image->FixupData);\r
-  }\r
-\r
-  CoreFreePool (Image);\r
-}\r
-\r
-\r
-\r
-\r
 /**\r
   Terminates the currently loaded EFI image and returns control to boot services.\r
 \r
@@ -1370,14 +1655,6 @@ CoreUnloadImage (
   EFI_STATUS                 Status;\r
   LOADED_IMAGE_PRIVATE_DATA  *Image;\r
 \r
-  //\r
-  // Prevent possible reentrance to this function\r
-  // for the same ImageHandle\r
-  //\r
-  if (!AcquireSpinLockOrFail (&mUnloadImageLock)) {\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-\r
   Image = CoreLoadedImageInfo (ImageHandle);\r
   if (Image == NULL ) {\r
     //\r
@@ -1412,7 +1689,6 @@ CoreUnloadImage (
   }\r
 \r
 Done:\r
-  ReleaseSpinLock (&mUnloadImageLock);\r
   return Status;\r
 }\r
 \r