]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Image/Image.c
Clean up AtaBus driver to not depend on PI IdeControllerInit Protocol definitions
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Image / Image.c
index 23e9ee58d6d3b5a18c6d9f34fa547c20ccdf9128..b7c1f96e0fcb68a6ef14b85fd8dd00fda2c5010b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Pei Core Load Image Support\r
   \r
-Copyright (c) 2006 - 2008, Intel Corporation                                                         \r
+Copyright (c) 2006 - 2010, Intel Corporation                                                         \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
@@ -14,29 +14,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "PeiMain.h"\r
 \r
-/**\r
-  The wrapper function of PeiLoadImageLoadImage().\r
-\r
-  @param This            - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
-  @param FileHandle      - Pointer to the FFS file header of the image.\r
-  @param ImageAddressArg - Pointer to PE/TE image.\r
-  @param ImageSizeArg    - Size of PE/TE image.\r
-  @param EntryPoint      - Pointer to entry point of specified image file for output.\r
-  @param AuthenticationState - Pointer to attestation authentication state of image.\r
-\r
-  @return Status of PeiLoadImageLoadImage().\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-PeiLoadImageLoadImageWrapper (\r
-  IN     CONST EFI_PEI_LOAD_FILE_PPI  *This,\r
-  IN     EFI_PEI_FILE_HANDLE          FileHandle,\r
-  OUT    EFI_PHYSICAL_ADDRESS         *ImageAddressArg,  OPTIONAL\r
-  OUT    UINT64                       *ImageSizeArg,     OPTIONAL\r
-  OUT    EFI_PHYSICAL_ADDRESS         *EntryPoint,\r
-  OUT    UINT32                       *AuthenticationState\r
-  );\r
 \r
 EFI_PEI_LOAD_FILE_PPI   mPeiLoadImagePpi = {\r
   PeiLoadImageLoadImageWrapper\r
@@ -77,9 +54,11 @@ PeiImageRead (
 \r
   Destination8  = Buffer;\r
   Source8       = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
-  Length        = *ReadSize;\r
-  while ((Length--) > 0) {\r
-    *(Destination8++) = *(Source8++);\r
+  if (Destination8 != Source8) {\r
+    Length        = *ReadSize;\r
+    while ((Length--) > 0) {\r
+      *(Destination8++) = *(Source8++);\r
+    }\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -99,14 +78,21 @@ GetImageReadFunction (
   IN      PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
   )\r
 {\r
+  PEI_CORE_INSTANCE  *Private;\r
   VOID*  MemoryBuffer;\r
 \r
-  MemoryBuffer = AllocatePages (0x400 / EFI_PAGE_SIZE + 1);\r
-  ASSERT (MemoryBuffer != NULL);\r
+  Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
 \r
-  CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);\r
+  if (!Private->PeiMemoryInstalled || (Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME)) {\r
+    ImageContext->ImageRead = PeiImageRead;\r
+  } else {\r
+    MemoryBuffer = AllocatePages (0x400 / EFI_PAGE_SIZE + 1);\r
+    ASSERT (MemoryBuffer != NULL);\r
+\r
+    CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);\r
 \r
-  ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\r
+    ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\r
+  }\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -114,7 +100,7 @@ GetImageReadFunction (
 /**\r
 \r
   Loads and relocates a PE/COFF image into memory.\r
-\r
+  If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.\r
 \r
   @param Pe32Data        - The base address of the PE/COFF file that is to be loaded and relocated\r
   @param ImageAddress    - The base address of the relocated PE/COFF image\r
@@ -123,7 +109,6 @@ GetImageReadFunction (
 \r
   @retval EFI_SUCCESS           The file was loaded and relocated\r
   @retval EFI_OUT_OF_RESOURCES  There was not enough memory to load and relocate the PE/COFF file\r
-  @retval EFI_INVALID_PARAMETER The image withou .reloc section can't be relocated.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -136,6 +121,9 @@ LoadAndRelocatePeCoffImage (
 {\r
   EFI_STATUS                            Status;\r
   PE_COFF_LOADER_IMAGE_CONTEXT          ImageContext;\r
+  PEI_CORE_INSTANCE                     *Private;\r
+\r
+  Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
 \r
   ZeroMem (&ImageContext, sizeof (ImageContext));\r
   ImageContext.Handle = Pe32Data;\r
@@ -151,22 +139,32 @@ LoadAndRelocatePeCoffImage (
   // When Image has no reloc section, it can't be relocated into memory.\r
   //\r
   if (ImageContext.RelocationsStripped) {\r
-    DEBUG ((EFI_D_ERROR, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));\r
-    return EFI_INVALID_PARAMETER;\r
+    DEBUG ((EFI_D_INFO, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));\r
   }\r
+  \r
   //\r
-  // Allocate Memory for the image\r
+  // Set default base address to current image address.\r
   //\r
-  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
-  ASSERT (ImageContext.ImageAddress != 0);\r
+  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
   \r
   //\r
-  // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
+  // Allocate Memory for the image when memory is ready, boot mode is not S3, and image is relocatable.\r
   //\r
-  if (ImageContext.IsTeImage) {\r
-    ImageContext.ImageAddress = ImageContext.ImageAddress + \r
-                                ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
-                                sizeof (EFI_TE_IMAGE_HEADER);\r
+  if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
+    ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
+    ASSERT (ImageContext.ImageAddress != 0);\r
+    if (ImageContext.ImageAddress == 0) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    \r
+    //\r
+    // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
+    //\r
+    if (ImageContext.IsTeImage) {\r
+      ImageContext.ImageAddress = ImageContext.ImageAddress + \r
+                                  ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
+                                  sizeof (EFI_TE_IMAGE_HEADER);\r
+    }\r
   }\r
 \r
   //\r
@@ -187,7 +185,9 @@ LoadAndRelocatePeCoffImage (
   //\r
   // Flush the instruction cache so the image data is written before we execute it\r
   //\r
-  InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
+  if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
+    InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
+  }\r
 \r
   *ImageAddress = ImageContext.ImageAddress;\r
   *ImageSize    = ImageContext.ImageSize;\r
@@ -229,8 +229,6 @@ PeiLoadImageLoadImage (
   UINT64                      ImageSize;\r
   EFI_PHYSICAL_ADDRESS        ImageEntryPoint;\r
   UINT16                      Machine;\r
-  PEI_CORE_INSTANCE           *Private;\r
-  VOID                        *EntryPointArg;\r
   EFI_SECTION_TYPE            SearchType1;\r
   EFI_SECTION_TYPE            SearchType2;\r
 \r
@@ -245,6 +243,7 @@ PeiLoadImageLoadImage (
     SearchType1 = EFI_SECTION_PE32;\r
     SearchType2 = EFI_SECTION_TE;\r
   }\r
+\r
   //\r
   // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst \r
   // is true, TE will be searched first).\r
@@ -272,45 +271,35 @@ PeiLoadImageLoadImage (
     }\r
   }\r
   \r
-  Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
+  //\r
+  // If memory is installed, perform the shadow operations\r
+  //\r
+  Status = LoadAndRelocatePeCoffImage (\r
+    Pe32Data,\r
+    &ImageAddress,\r
+    &ImageSize,\r
+    &ImageEntryPoint\r
+  );\r
 \r
-  if (Private->PeiMemoryInstalled && \r
-      (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
-    //\r
-    // If memory is installed, perform the shadow operations\r
-    //\r
-    Status = LoadAndRelocatePeCoffImage (\r
-      Pe32Data,\r
-      &ImageAddress,\r
-      &ImageSize,\r
-      &ImageEntryPoint\r
-    );\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
 \r
-    //\r
-    // Got the entry point from the loaded Pe32Data\r
-    //\r
-    Pe32Data    = (VOID *) ((UINTN) ImageAddress);\r
-    *EntryPoint = ImageEntryPoint;\r
-  } else {\r
-    //\r
-    // Retrieve the entry point from the PE/COFF or TE image header\r
-    //\r
-    ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) Pe32Data;\r
-    Status = PeCoffLoaderGetEntryPoint (Pe32Data, &EntryPointArg);\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-    *EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) EntryPointArg;\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
   }\r
+\r
+  //\r
+  // Got the entry point from the loaded Pe32Data\r
+  //\r
+  Pe32Data    = (VOID *) ((UINTN) ImageAddress);\r
+  *EntryPoint = ImageEntryPoint;\r
   \r
   Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
   \r
   if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
-    return EFI_UNSUPPORTED;  \r
+    if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
+      return EFI_UNSUPPORTED;\r
+    }\r
   }\r
 \r
   if (ImageAddressArg != NULL) {\r
@@ -330,7 +319,7 @@ PeiLoadImageLoadImage (
     //\r
     // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
     //\r
-    if (Machine != IMAGE_FILE_MACHINE_IA64) {\r
+    if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
       DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
     } else {\r
       //\r
@@ -403,6 +392,66 @@ PeiLoadImageLoadImageWrapper (
            );\r
 }\r
 \r
+/**\r
+  Check whether the input image has the relocation.\r
+\r
+  @param  Pe32Data   Pointer to the PE/COFF or TE image.\r
+\r
+  @retval TRUE       Relocation is stripped.\r
+  @retval FALSE      Relocation is not stripped.\r
+\r
+**/\r
+BOOLEAN\r
+RelocationIsStrip (\r
+  IN VOID  *Pe32Data\r
+  )\r
+{\r
+  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION  Hdr;\r
+  EFI_IMAGE_DOS_HEADER                 *DosHdr;\r
+\r
+  ASSERT (Pe32Data != NULL);\r
+\r
+  DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
+  if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
+    //\r
+    // DOS image header is present, so read the PE header after the DOS image header.\r
+    //\r
+    Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
+  } else {\r
+    //\r
+    // DOS image header is not present, so PE header is at the image base.\r
+    //\r
+    Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
+  }\r
+\r
+  //\r
+  // Three cases with regards to relocations:\r
+  // - Image has base relocs, RELOCS_STRIPPED==0    => image is relocatable\r
+  // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r
+  // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r
+  //   has no base relocs to apply\r
+  // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r
+  //\r
+  // Look at the file header to determine if relocations have been stripped, and\r
+  // save this info in the image context for later use.\r
+  //\r
+  if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
+    if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r
+      return TRUE;\r
+    } else {\r
+      return FALSE;\r
+    }\r
+  } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE)  {\r
+    if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r
+      return TRUE;\r
+    } else {\r
+      return FALSE;\r
+    }\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
 /**\r
   Routine to load image file for subsequent execution by LoadFile Ppi.\r
   If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE \r
@@ -410,6 +459,7 @@ PeiLoadImageLoadImageWrapper (
 \r
   @param PeiServices     - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
   @param FileHandle      - Pointer to the FFS file header of the image.\r
+  @param PeimState       - The dispatch state of the input PEIM handle.\r
   @param EntryPoint      - Pointer to entry point of specified image file for output.\r
   @param AuthenticationState - Pointer to attestation authentication state of image.\r
 \r
@@ -422,6 +472,7 @@ EFI_STATUS
 PeiLoadImage (\r
   IN     CONST EFI_PEI_SERVICES       **PeiServices,\r
   IN     EFI_PEI_FILE_HANDLE          FileHandle,\r
+  IN     UINT8                        PeimState,\r
   OUT    EFI_PHYSICAL_ADDRESS         *EntryPoint,\r
   OUT    UINT32                       *AuthenticationState\r
   )\r
@@ -432,7 +483,9 @@ PeiLoadImage (
   EFI_PEI_LOAD_FILE_PPI   *LoadFile;\r
   EFI_PHYSICAL_ADDRESS    ImageAddress;\r
   UINT64                  ImageSize;\r
+  BOOLEAN                 IsStrip;\r
 \r
+  IsStrip = FALSE;\r
   //\r
   // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
   // one at a time, until one reports EFI_SUCCESS.\r
@@ -455,25 +508,31 @@ PeiLoadImage (
                           AuthenticationState\r
                           );\r
       if (!EFI_ERROR (Status)) {\r
+        //\r
+        // The shadowed PEIM must be relocatable.\r
+        //\r
+        if (PeimState == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
+          IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r
+          ASSERT (!IsStrip);\r
+          if (IsStrip) {\r
+            return EFI_UNSUPPORTED;\r
+          }\r
+        }\r
+\r
+        //\r
+        // The image to be started must have the machine type supported by PeiCore.\r
+        //\r
+        ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
+        if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
+          return EFI_UNSUPPORTED;\r
+        }\r
         return Status;\r
       }\r
     }\r
     Index++;\r
   } while (!EFI_ERROR (PpiStatus));\r
 \r
-  //\r
-  // If no instances reports EFI_SUCCESS, then build-in support for\r
-  // the PE32+/TE XIP image format is used.\r
-  //\r
-  Status = PeiLoadImageLoadImage (\r
-            PeiServices, \r
-            FileHandle, \r
-            NULL, \r
-            NULL, \r
-            EntryPoint, \r
-            AuthenticationState\r
-            );\r
-  return Status;\r
+  return PpiStatus;\r
 }\r
 \r
 \r
@@ -510,3 +569,4 @@ InitializeImageServices (
 \r
 \r
 \r
+\r