]> git.proxmox.com Git - mirror_edk2.git/commitdiff
In order to make sure the image is section alignment, after allocate buffer for TE...
authorydong10 <ydong10@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 27 Feb 2013 02:09:01 +0000 (02:09 +0000)
committerydong10 <ydong10@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 27 Feb 2013 02:09:01 +0000 (02:09 +0000)
Signed-off-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@14145 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Core/Pei/Image/Image.c

index 41069e7a31fd2f562012614de815821741fe444e..85e5e3fcf3ec35074e5d757ba5f716e3275eb925 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Pei Core Load Image Support\r
 \r
-Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2013, 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
@@ -348,6 +348,7 @@ LoadAndRelocatePeCoffImage (
   EFI_STATUS                            Status;\r
   PE_COFF_LOADER_IMAGE_CONTEXT          ImageContext;\r
   PEI_CORE_INSTANCE                     *Private;\r
+  UINT64                                AlignImageSize;\r
 \r
   Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
 \r
@@ -377,6 +378,19 @@ LoadAndRelocatePeCoffImage (
   // Allocate Memory for the image when memory is ready, boot mode is not S3, and image is relocatable.\r
   //\r
   if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\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) {\r
       Status = GetPeCoffImageFixLoadingAssignedAddress(&ImageContext, Private);\r
       if (EFI_ERROR (Status)){\r
@@ -384,10 +398,10 @@ LoadAndRelocatePeCoffImage (
         //\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
+      ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize));\r
     }\r
     ASSERT (ImageContext.ImageAddress != 0);\r
     if (ImageContext.ImageAddress == 0) {\r
@@ -395,6 +409,15 @@ LoadAndRelocatePeCoffImage (
     }\r
 \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