]> 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 4035c756526a58064918fb94506d87013dcaa379..b7c1f96e0fcb68a6ef14b85fd8dd00fda2c5010b 100644 (file)
@@ -1,6 +1,7 @@
-/*++\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+/** @file\r
+  Pei Core Load Image Support\r
+  \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
@@ -9,140 +10,43 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
 \r
-Module Name:\r
-\r
-  Image.c\r
-\r
-Abstract:\r
-\r
-  Pei Core Load Image Support\r
-\r
---*/\r
-\r
-#include <PeiMain.h>\r
-\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
-\r
-Arguments:\r
-\r
-  FileHandle - The handle to the PE/COFF file\r
-  FileOffset - The offset, in bytes, into the file to read\r
-  ReadSize   - The number of bytes to read from the file starting at FileOffset\r
-  Buffer     - A pointer to the buffer to read the data into.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
+**/\r
 \r
---*/  \r
-\r
-EFI_STATUS\r
-PeiLoadImageLoadImage (\r
-  IN     EFI_PEI_SERVICES             **PeiServices,\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
+#include "PeiMain.h"\r
 \r
-Routine Description:\r
-\r
-  Routine for loading file image.\r
-\r
-Arguments:\r
-\r
-  PeiServices          - The PEI core services table.\r
-  FileHandle           - Pointer to the FFS file header of the image.\r
-  ImageAddressArg      - Pointer to PE/TE image.\r
-  ImageSizeArg         - Size of PE/TE image.\r
-  EntryPoint           - Pointer to entry point of specified image file for output.\r
-  AuthenticationState  - Pointer to attestation authentication state of image.\r
-\r
-Returns:\r
-\r
-  Status - EFI_SUCCESS    - Image is successfully loaded.\r
-           EFI_NOT_FOUND  - Fail to locate necessary PPI\r
-           Others         - Fail to load file.\r
-\r
---*/\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
-\r
-Routine Description:\r
-\r
-  The wrapper function of PeiLoadImageLoadImage().\r
-\r
-Arguments:\r
-\r
-  This                 - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
-  PeiServices          - The PEI core services table.\r
-  FileHandle           - Pointer to the FFS file header of the image.\r
-  ImageAddressArg      - Pointer to PE/TE image.\r
-  ImageSizeArg         - Size of PE/TE image.\r
-  EntryPoint           - Pointer to entry point of specified image file for output.\r
-  AuthenticationState  - Pointer to attestation authentication state of image.\r
-\r
-Returns:\r
-\r
-  EFI_STATUS.\r
-  \r
---*/ \r
-;\r
 \r
-STATIC EFI_PEI_LOAD_FILE_PPI   mPeiLoadImagePpi = {\r
+EFI_PEI_LOAD_FILE_PPI   mPeiLoadImagePpi = {\r
   PeiLoadImageLoadImageWrapper\r
 };\r
 \r
 \r
-STATIC EFI_PEI_PPI_DESCRIPTOR     gPpiLoadFilePpiList = {\r
+EFI_PEI_PPI_DESCRIPTOR     gPpiLoadFilePpiList = {\r
   (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
   &gEfiPeiLoadFilePpiGuid,\r
   &mPeiLoadImagePpi\r
 };\r
 \r
+/**\r
+\r
+  Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
+\r
+\r
+  @param FileHandle      - The handle to the PE/COFF file\r
+  @param FileOffset      - The offset, in bytes, into the file to read\r
+  @param ReadSize        - The number of bytes to read from the file starting at FileOffset\r
+  @param Buffer          - A pointer to the buffer to read the data into.\r
+\r
+  @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiImageRead (\r
   IN     VOID    *FileHandle,\r
   IN     UINTN   FileOffset,\r
-  IN OUT UINTN   *ReadSize,\r
+  IN     UINTN   *ReadSize,\r
   OUT    VOID    *Buffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
-\r
-Arguments:\r
-\r
-  FileHandle - The handle to the PE/COFF file\r
-  FileOffset - The offset, in bytes, into the file to read\r
-  ReadSize   - The number of bytes to read from the file starting at FileOffset\r
-  Buffer     - A pointer to the buffer to read the data into.\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
-\r
---*/\r
 {\r
   CHAR8 *Destination8;\r
   CHAR8 *Source8;\r
@@ -150,49 +54,63 @@ Returns:
 \r
   Destination8  = Buffer;\r
   Source8       = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
-  Length        = *ReadSize;\r
-  while (Length--) {\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
 }\r
 \r
+/**\r
+\r
+  Support routine to get the Image read file function.\r
+\r
+  @param ImageContext    - The context of the image being loaded\r
+\r
+  @retval EFI_SUCCESS - If Image function location is found\r
+\r
+**/\r
 EFI_STATUS\r
 GetImageReadFunction (\r
   IN      PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Support routine to return the Image Read\r
-\r
-Arguments:\r
+{\r
+  PEI_CORE_INSTANCE  *Private;\r
+  VOID*  MemoryBuffer;\r
 \r
-  PeiServices   - PEI Services Table\r
+  Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
 \r
-  ImageContext  - The context of the image being loaded\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
-Returns:\r
+    CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);\r
 \r
-  EFI_SUCCESS - If Image function location is found\r
+    ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\r
+  }\r
 \r
---*/\r
-{\r
-  VOID*  MemoryBuffer;\r
+  return EFI_SUCCESS;\r
+}\r
 \r
-  MemoryBuffer = AllocatePages (0x400 / EFI_PAGE_SIZE + 1);\r
-  ASSERT (MemoryBuffer != NULL);\r
+/**\r
 \r
-  CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);\r
+  Loads and relocates a PE/COFF image into memory.\r
+  If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.\r
 \r
-  ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\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
+  @param ImageSize       - The size of the relocated PE/COFF image\r
+  @param EntryPoint      - The entry point of the relocated PE/COFF image\r
 \r
-  return EFI_SUCCESS;\r
-}\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
 \r
-STATIC\r
+**/\r
 EFI_STATUS\r
 LoadAndRelocatePeCoffImage (\r
   IN  VOID                                      *Pe32Data,\r
@@ -200,32 +118,12 @@ LoadAndRelocatePeCoffImage (
   OUT UINT64                                    *ImageSize,\r
   OUT EFI_PHYSICAL_ADDRESS                      *EntryPoint\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Loads and relocates a PE/COFF image into memory.\r
-\r
-Arguments:\r
-\r
-  Pe32Data         - The base address of the PE/COFF file that is to be loaded and relocated\r
-\r
-  ImageAddress     - The base address of the relocated PE/COFF image\r
-\r
-  ImageSize        - The size of the relocated PE/COFF image\r
-\r
-  EntryPoint       - The entry point of the relocated PE/COFF image\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS   - The file was loaded and relocated\r
-\r
-  EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
-\r
---*/\r
 {\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
@@ -241,22 +139,32 @@ Returns:
   // 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", (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
@@ -277,7 +185,9 @@ Returns:
   //\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
@@ -286,37 +196,32 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Loads a PEIM into memory for subsequent execution. If there are compressed \r
+  images or images that need to be relocated into memory for performance reasons, \r
+  this service performs that transformation.\r
+\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 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
+  @retval EFI_SUCCESS      Image is successfully loaded.\r
+  @retval EFI_NOT_FOUND    Fail to locate necessary PPI.\r
+  @retval EFI_UNSUPPORTED  Image Machine Type is not supported.\r
+\r
+**/\r
 EFI_STATUS\r
 PeiLoadImageLoadImage (\r
-  IN     EFI_PEI_SERVICES             **PeiServices,\r
+  IN     CONST EFI_PEI_SERVICES       **PeiServices,\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
-\r
-Routine Description:\r
-\r
-  Routine for loading file image.\r
-\r
-Arguments:\r
-\r
-  PeiServices          - The PEI core services table.\r
-  FileHandle           - Pointer to the FFS file header of the image.\r
-  ImageAddressArg      - Pointer to PE/TE image.\r
-  ImageSizeArg         - Size of PE/TE image.\r
-  EntryPoint           - Pointer to entry point of specified image file for output.\r
-  AuthenticationState  - Pointer to attestation authentication state of image.\r
-\r
-Returns:\r
-\r
-  Status - EFI_SUCCESS    - Image is successfully loaded.\r
-           EFI_NOT_FOUND  - Fail to locate necessary PPI\r
-           Others         - Fail to load file.\r
-\r
---*/\r
 {\r
   EFI_STATUS                  Status;\r
   VOID                        *Pe32Data;\r
@@ -324,27 +229,36 @@ Returns:
   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
   *EntryPoint          = 0;\r
   ImageSize            = 0;\r
   *AuthenticationState = 0;\r
 \r
+  if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r
+    SearchType1 = EFI_SECTION_TE;\r
+    SearchType2 = EFI_SECTION_PE32;\r
+  } else {\r
+    SearchType1 = EFI_SECTION_PE32;\r
+    SearchType2 = EFI_SECTION_TE;\r
+  }\r
+\r
   //\r
-  // Try to find a TE section.\r
+  // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst \r
+  // is true, TE will be searched first).\r
   //\r
   Status = PeiServicesFfsFindSectionData (\r
-             EFI_SECTION_TE,\r
+             SearchType1,\r
              FileHandle,\r
              &Pe32Data\r
              );\r
   //\r
-  // If we didn't find a TE section, try to find a PE32 section.\r
+  // If we didn't find a first exe section, try to find the second exe section.\r
   //\r
   if (EFI_ERROR (Status)) {\r
     Status = PeiServicesFfsFindSectionData (\r
-               EFI_SECTION_PE32,\r
+               SearchType2,\r
                FileHandle,\r
                &Pe32Data\r
                );\r
@@ -357,45 +271,35 @@ Returns:
     }\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
@@ -415,13 +319,13 @@ Returns:
     //\r
     // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
     //\r
-    if (Machine != IMAGE_FILE_MACHINE_IA64) {\r
-      DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", (UINTN) ImageAddress, *EntryPoint));\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
       // For IPF Image, the real entry point should be print.\r
       //\r
-      DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", (UINTN) ImageAddress, (UINTN) (*(UINT64 *)(UINTN)*EntryPoint)));\r
+      DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));\r
     }\r
     \r
     //\r
@@ -454,6 +358,19 @@ Returns:
 }\r
 \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
@@ -464,27 +381,6 @@ PeiLoadImageLoadImageWrapper (
   OUT    EFI_PHYSICAL_ADDRESS         *EntryPoint,\r
   OUT    UINT32                       *AuthenticationState\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  The wrapper function of PeiLoadImageLoadImage().\r
-\r
-Arguments:\r
-\r
-  This                 - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
-  PeiServices          - The PEI core services table.\r
-  FileHandle           - Pointer to the FFS file header of the image.\r
-  ImageAddressArg      - Pointer to PE/TE image.\r
-  ImageSizeArg         - Size of PE/TE image.\r
-  EntryPoint           - Pointer to entry point of specified image file for output.\r
-  AuthenticationState  - Pointer to attestation authentication state of image.\r
-\r
-Returns:\r
-\r
-  EFI_STATUS.\r
-  \r
---*/      \r
 {\r
   return PeiLoadImageLoadImage (\r
            GetPeiServicesTablePointer (),\r
@@ -496,33 +392,90 @@ Returns:
            );\r
 }\r
 \r
-EFI_STATUS\r
-PeiLoadImage (\r
-  IN     EFI_PEI_SERVICES             **PeiServices,\r
-  IN     EFI_PEI_FILE_HANDLE          FileHandle,\r
-  OUT    EFI_PHYSICAL_ADDRESS         *EntryPoint,\r
-  OUT    UINT32                       *AuthenticationState\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
+{\r
+  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION  Hdr;\r
+  EFI_IMAGE_DOS_HEADER                 *DosHdr;\r
 \r
-Routine Description:\r
+  ASSERT (Pe32Data != NULL);\r
 \r
-  Routine for load image file.\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
-Arguments:\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
-  PeiServices          - The PEI core services table.\r
-  FileHandle           - Pointer to the FFS file header of the image.\r
-  EntryPoint           - Pointer to entry point of specified image file for output.\r
-  AuthenticationState  - Pointer to attestation authentication state of image.\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
+  XIP image format is used.\r
 \r
-Returns:\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
-  Status - EFI_SUCCESS    - Image is successfully loaded.\r
-           EFI_NOT_FOUND  - Fail to locate necessary PPI\r
-           Others         - Fail to load file.\r
-  \r
---*/    \r
+  @retval EFI_SUCCESS    - Image is successfully loaded.\r
+  @retval EFI_NOT_FOUND  - Fail to locate necessary PPI\r
+  @retval Others         - Fail to load file.\r
+\r
+**/\r
+EFI_STATUS\r
+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
 {\r
   EFI_STATUS              PpiStatus;\r
   EFI_STATUS              Status;\r
@@ -530,7 +483,9 @@ Returns:
   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
@@ -553,49 +508,48 @@ Returns:
                           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
-VOID\r
-InitializeImageServices (\r
-  IN  PEI_CORE_INSTANCE   *PrivateData,\r
-  IN  PEI_CORE_INSTANCE   *OldCoreData\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   Install Pei Load File PPI.\r
 \r
-Arguments:\r
-\r
-  PrivateData    - Pointer to PEI_CORE_INSTANCE.\r
-  OldCoreData    - Pointer to PEI_CORE_INSTANCE.\r
 \r
-Returns:\r
+  @param PrivateData     - Pointer to PEI_CORE_INSTANCE.\r
+  @param OldCoreData     - Pointer to PEI_CORE_INSTANCE.\r
 \r
-  NONE.\r
-  \r
---*/      \r
+**/\r
+VOID\r
+InitializeImageServices (\r
+  IN  PEI_CORE_INSTANCE   *PrivateData,\r
+  IN  PEI_CORE_INSTANCE   *OldCoreData\r
+  )\r
 {\r
   if (OldCoreData == NULL) {\r
     //\r
@@ -615,3 +569,4 @@ Returns:
 \r
 \r
 \r
+\r