]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Image/Image.c
MdeModulePkg PeiCore: PEI dispatcher need retry to process NOT_DISPATCHED FV
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Image / Image.c
index 0906548edecef3effe3388d699bfc2b44179007f..9c54192d86b7366c3ccedc608207e6c26b75ee4c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Pei Core Load Image Support\r
 \r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2015, 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
@@ -117,7 +117,8 @@ GetImageReadFunction (
 \r
   Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
   \r
-  if ((Private->PeiMemoryInstalled  && !(Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME))  &&\r
+  if (Private->PeiMemoryInstalled  && (((Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME) && PcdGetBool (PcdShadowPeimOnBoot)) || \r
+      ((Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME) && PcdGetBool (PcdShadowPeimOnS3Boot))) &&\r
       (EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_X64) || EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_IA32))) {\r
     // \r
     // Shadow algorithm makes lots of non ANSI C assumptions and only works for IA32 and X64 \r
@@ -328,6 +329,7 @@ GetPeCoffImageFixLoadingAssignedAddress(
   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
+  @param FileHandle      - Pointer to the FFS file header of the image.\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
@@ -335,10 +337,14 @@ GetPeCoffImageFixLoadingAssignedAddress(
 \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_WARN_BUFFER_TOO_SMALL \r
+                                There is not enough heap to allocate the requested size.\r
+                                This will not prevent the XIP image from being invoked.\r
 \r
 **/\r
 EFI_STATUS\r
 LoadAndRelocatePeCoffImage (\r
+  IN  EFI_PEI_FILE_HANDLE                       FileHandle,\r
   IN  VOID                                      *Pe32Data,\r
   OUT EFI_PHYSICAL_ADDRESS                      *ImageAddress,\r
   OUT UINT64                                    *ImageSize,\r
@@ -348,9 +354,16 @@ LoadAndRelocatePeCoffImage (
   EFI_STATUS                            Status;\r
   PE_COFF_LOADER_IMAGE_CONTEXT          ImageContext;\r
   PEI_CORE_INSTANCE                     *Private;\r
+  UINT64                                AlignImageSize;\r
+  BOOLEAN                               IsXipImage;\r
+  EFI_STATUS                            ReturnStatus;\r
+  BOOLEAN                               IsS3Boot;\r
+  BOOLEAN                               IsRegisterForShadow;\r
 \r
   Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
 \r
+  ReturnStatus = EFI_SUCCESS;\r
+  IsXipImage   = FALSE;\r
   ZeroMem (&ImageContext, sizeof (ImageContext));\r
   ImageContext.Handle = Pe32Data;\r
   Status              = GetImageReadFunction (&ImageContext);\r
@@ -361,10 +374,32 @@ LoadAndRelocatePeCoffImage (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+  \r
+  //\r
+  // Initilize local IsS3Boot and IsRegisterForShadow variable\r
+  //\r
+  IsS3Boot = FALSE;\r
+  if (Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME) {\r
+    IsS3Boot = TRUE;\r
+  }\r
+  IsRegisterForShadow = FALSE;\r
+  if ((Private->CurrentFileHandle == FileHandle) \r
+    && (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW)) {\r
+    IsRegisterForShadow = TRUE;\r
+  }\r
+\r
+  //\r
+  // XIP image that ImageAddress is same to Image handle.\r
+  //\r
+  if (ImageContext.ImageAddress == (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
+    IsXipImage = TRUE;\r
+  }\r
+\r
   //\r
   // When Image has no reloc section, it can't be relocated into memory.\r
   //\r
-  if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
+  if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && (\r
+      (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
     DEBUG ((EFI_D_INFO|EFI_D_LOAD, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));\r
   }\r
 \r
@@ -374,33 +409,72 @@ LoadAndRelocatePeCoffImage (
   ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
 \r
   //\r
-  // Allocate Memory for the image when memory is ready, boot mode is not S3, and image is relocatable.\r
+  // Allocate Memory for the image when memory is ready, and image is relocatable.\r
+  // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.\r
+  // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.\r
   //\r
-  if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
-    if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
+  if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (\r
+      (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
+    //\r
+    // Allocate more buffer to avoid buffer overflow.\r
+    //\r
+    if (ImageContext.IsTeImage) {\r
+      AlignImageSize = ImageContext.ImageSize + ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);\r
+    } else {\r
+      AlignImageSize = ImageContext.ImageSize;\r
+    }\r
+\r
+    if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
+      AlignImageSize += ImageContext.SectionAlignment;\r
+    }\r
+\r
+    if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
       Status = GetPeCoffImageFixLoadingAssignedAddress(&ImageContext, Private);\r
       if (EFI_ERROR (Status)){\r
         DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
         //\r
         // The PEIM is not assiged valid address, try to allocate page to load it.\r
         //\r
-        ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
+        ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize));\r
       }\r
     } else {\r
-      ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
-    }\r
-    ASSERT (ImageContext.ImageAddress != 0);\r
-    if (ImageContext.ImageAddress == 0) {\r
-      return EFI_OUT_OF_RESOURCES;\r
+      ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize));\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
+    if (ImageContext.ImageAddress != 0) {\r
+      //\r
+      // Adjust the Image Address to make sure it is section alignment.\r
+      //\r
+      if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
+        ImageContext.ImageAddress =\r
+            (ImageContext.ImageAddress + ImageContext.SectionAlignment - 1) &\r
+            ~((UINTN)ImageContext.SectionAlignment - 1);\r
+      }\r
+      //\r
+      // Fix alignment requirement when Load IPF TeImage into memory.\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
+    } else {\r
+      //\r
+      // No enough memory resource.\r
+      //\r
+      if (IsXipImage) {\r
+        //\r
+        // XIP image can still be invoked.\r
+        //\r
+        ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
+        ReturnStatus = EFI_WARN_BUFFER_TOO_SMALL;\r
+      } else {\r
+        //\r
+        // Non XIP image can't be loaded because no enough memory is allocated.\r
+        //\r
+        ASSERT (FALSE);\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
     }\r
   }\r
 \r
@@ -422,7 +496,7 @@ LoadAndRelocatePeCoffImage (
   //\r
   // Flush the instruction cache so the image data is written before we execute it\r
   //\r
-  if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
+  if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
     InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
   }\r
 \r
@@ -430,7 +504,7 @@ LoadAndRelocatePeCoffImage (
   *ImageSize    = ImageContext.ImageSize;\r
   *EntryPoint   = ImageContext.EntryPoint;\r
 \r
-  return EFI_SUCCESS;\r
+  return ReturnStatus;\r
 }\r
 \r
 /**\r
@@ -448,6 +522,9 @@ LoadAndRelocatePeCoffImage (
   @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
+  @retval EFI_WARN_BUFFER_TOO_SMALL \r
+                           There is not enough heap to allocate the requested size.\r
+                           This will not prevent the XIP image from being invoked.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -485,23 +562,27 @@ PeiLoadImageLoadImage (
   // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
   // is true, TE will be searched first).\r
   //\r
-  Status = PeiServicesFfsFindSectionData (\r
+  Status = PeiServicesFfsFindSectionData3 (\r
              SearchType1,\r
+             0,\r
              FileHandle,\r
-             &Pe32Data\r
+             &Pe32Data,\r
+             AuthenticationState\r
              );\r
   //\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
+    Status = PeiServicesFfsFindSectionData3 (\r
                SearchType2,\r
+               0,\r
                FileHandle,\r
-               &Pe32Data\r
+               &Pe32Data,\r
+               AuthenticationState\r
                );\r
     if (EFI_ERROR (Status)) {\r
       //\r
-      // PEI core only carry the loader function fro TE and PE32 executables\r
+      // PEI core only carry the loader function for TE and PE32 executables\r
       // If this two section does not exist, just return.\r
       //\r
       return Status;\r
@@ -512,6 +593,7 @@ PeiLoadImageLoadImage (
   // If memory is installed, perform the shadow operations\r
   //\r
   Status = LoadAndRelocatePeCoffImage (\r
+    FileHandle,\r
     Pe32Data,\r
     &ImageAddress,\r
     &ImageSize,\r
@@ -549,9 +631,9 @@ PeiLoadImageLoadImage (
 \r
   DEBUG_CODE_BEGIN ();\r
     CHAR8                              *AsciiString;\r
-    CHAR8                              AsciiBuffer[512];\r
+    CHAR8                              EfiFileName[512];\r
     INT32                              Index;\r
-    INT32                              Index1;\r
+    INT32                              StartIndex;\r
 \r
     //\r
     // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
@@ -571,19 +653,37 @@ PeiLoadImageLoadImage (
     AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
 \r
     if (AsciiString != NULL) {\r
-      for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {\r
-        if (AsciiString[Index] == '\\') {\r
-          break;\r
+      StartIndex = 0;\r
+      for (Index = 0; AsciiString[Index] != 0; Index++) {\r
+        if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r
+          StartIndex = Index + 1;\r
         }\r
       }\r
 \r
-      if (Index != 0) {\r
-        for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {\r
-          AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];\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~511.\r
+      // If the length is bigger than 511, trim the redudant characters to avoid overflow in array boundary.\r
+      //\r
+      for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
+        EfiFileName[Index] = AsciiString[Index + StartIndex];\r
+        if (EfiFileName[Index] == 0) {\r
+          EfiFileName[Index] = '.';\r
+        }\r
+        if (EfiFileName[Index] == '.') {\r
+          EfiFileName[Index + 1] = 'e';\r
+          EfiFileName[Index + 2] = 'f';\r
+          EfiFileName[Index + 3] = 'i';\r
+          EfiFileName[Index + 4] = 0;\r
+          break;\r
         }\r
-        AsciiBuffer [Index1] = '\0';\r
-        DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r
       }\r
+\r
+      if (Index == sizeof (EfiFileName) - 4) {\r
+        EfiFileName[Index] = 0;\r
+      }\r
+\r
+      DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName));\r
     }\r
 \r
   DEBUG_CODE_END ();\r
@@ -744,7 +844,7 @@ PeiLoadImage (
                           EntryPoint,\r
                           AuthenticationState\r
                           );\r
-      if (!EFI_ERROR (Status)) {\r
+      if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
         //\r
         // The shadowed PEIM must be relocatable.\r
         //\r
@@ -763,7 +863,7 @@ PeiLoadImage (
         if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
           return EFI_UNSUPPORTED;\r
         }\r
-        return Status;\r
+        return EFI_SUCCESS;\r
       }\r
     }\r
     Index++;\r