]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c
MdeModulePkg: remove PE/COFF header workaround for ELILO on IPF
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / PropertiesTable.c
index 3515993112813acd84e8fd8125e65e9befa3977a..05eb4f422b2fc39103351ed3ff9eee3b99fd04cf 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   UEFI PropertiesTable support\r
 \r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 2017, 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
@@ -36,26 +36,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \\r
   ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))\r
 \r
-#define IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE SIGNATURE_32 ('I','P','R','C')\r
-\r
-typedef struct {\r
-  UINT32                 Signature;\r
-  LIST_ENTRY             Link;\r
-  EFI_PHYSICAL_ADDRESS   CodeSegmentBase;\r
-  UINT64                 CodeSegmentSize;\r
-} IMAGE_PROPERTIES_RECORD_CODE_SECTION;\r
-\r
-#define IMAGE_PROPERTIES_RECORD_SIGNATURE SIGNATURE_32 ('I','P','R','D')\r
-\r
-typedef struct {\r
-  UINT32                 Signature;\r
-  LIST_ENTRY             Link;\r
-  EFI_PHYSICAL_ADDRESS   ImageBase;\r
-  UINT64                 ImageSize;\r
-  UINTN                  CodeSegmentCount;\r
-  LIST_ENTRY             CodeSegmentList;\r
-} IMAGE_PROPERTIES_RECORD;\r
-\r
 #define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P','D')\r
 \r
 typedef struct {\r
@@ -80,6 +60,10 @@ EFI_PROPERTIES_TABLE  mPropertiesTable = {
 \r
 EFI_LOCK           mPropertiesTableLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);\r
 \r
+BOOLEAN            mPropertiesTableEnable;\r
+\r
+BOOLEAN            mPropertiesTableEndOfDxe = FALSE;\r
+\r
 //\r
 // Below functions are for MemoryMap\r
 //\r
@@ -94,6 +78,7 @@ EFI_LOCK           mPropertiesTableLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTI
   @return  The number of bytes associated with the number of EFI_PAGEs specified\r
            by Pages.\r
 **/\r
+STATIC\r
 UINT64\r
 EfiPagesToSize (\r
   IN UINT64 Pages\r
@@ -113,6 +98,7 @@ EfiPagesToSize (
            by Size.\r
 \r
 **/\r
+STATIC\r
 UINT64\r
 EfiSizeToPages (\r
   IN UINT64 Size\r
@@ -124,6 +110,7 @@ EfiSizeToPages (
 /**\r
   Acquire memory lock on mPropertiesTableLock.\r
 **/\r
+STATIC\r
 VOID\r
 CoreAcquirePropertiesTableLock (\r
   VOID\r
@@ -135,6 +122,7 @@ CoreAcquirePropertiesTableLock (
 /**\r
   Release memory lock on mPropertiesTableLock.\r
 **/\r
+STATIC\r
 VOID\r
 CoreReleasePropertiesTableLock (\r
   VOID\r
@@ -143,35 +131,6 @@ CoreReleasePropertiesTableLock (
   CoreReleaseLock (&mPropertiesTableLock);\r
 }\r
 \r
-/**\r
-  Dump memory map.\r
-\r
-  @param  MemoryMap              A pointer to the buffer in which firmware places\r
-                                 the current memory map.\r
-  @param  MemoryMapSize          Size, in bytes, of the MemoryMap buffer.\r
-  @param  DescriptorSize         Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
-**/\r
-VOID\r
-DumpMemoryMap (\r
-  IN EFI_MEMORY_DESCRIPTOR  *MemoryMap,\r
-  IN UINTN                  MemoryMapSize,\r
-  IN UINTN                  DescriptorSize\r
-  )\r
-{\r
-  EFI_MEMORY_DESCRIPTOR       *MemoryMapEntry;\r
-  EFI_MEMORY_DESCRIPTOR       *MemoryMapEnd;\r
-  UINT64                      MemoryBlockLength;\r
-\r
-  DEBUG ((EFI_D_VERBOSE, "  MemoryMap:\n"));\r
-  MemoryMapEntry = MemoryMap;\r
-  MemoryMapEnd   = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + MemoryMapSize);\r
-  while (MemoryMapEntry < MemoryMapEnd) {\r
-    MemoryBlockLength = (UINT64) (EfiPagesToSize (MemoryMapEntry->NumberOfPages));\r
-    DEBUG ((EFI_D_VERBOSE, "    Entry(0x%02x) 0x%016lx - 0x%016lx (0x%016lx)\n", MemoryMapEntry->Type, MemoryMapEntry->PhysicalStart, MemoryMapEntry->PhysicalStart + MemoryBlockLength, MemoryMapEntry->Attribute));\r
-    MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
-  }\r
-}\r
-\r
 /**\r
   Sort memory map entries based upon PhysicalStart, from low to high.\r
 \r
@@ -180,6 +139,7 @@ DumpMemoryMap (
   @param  MemoryMapSize          Size, in bytes, of the MemoryMap buffer.\r
   @param  DescriptorSize         Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
 **/\r
+STATIC\r
 VOID\r
 SortMemoryMap (\r
   IN OUT EFI_MEMORY_DESCRIPTOR  *MemoryMap,\r
@@ -244,14 +204,24 @@ MergeMemoryMap (
     CopyMem (NewMemoryMapEntry, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r
     NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
 \r
-    MemoryBlockLength = (UINT64) (EfiPagesToSize (MemoryMapEntry->NumberOfPages));\r
-    if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&\r
-        (MemoryMapEntry->Type == NextMemoryMapEntry->Type) &&\r
-        (MemoryMapEntry->Attribute == NextMemoryMapEntry->Attribute) &&\r
-        ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart)) {\r
-      NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
-      MemoryMapEntry = NextMemoryMapEntry;\r
-    }\r
+    do {\r
+      MemoryBlockLength = (UINT64) (EfiPagesToSize (MemoryMapEntry->NumberOfPages));\r
+      if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&\r
+          (MemoryMapEntry->Type == NextMemoryMapEntry->Type) &&\r
+          (MemoryMapEntry->Attribute == NextMemoryMapEntry->Attribute) &&\r
+          ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart)) {\r
+        MemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
+        if (NewMemoryMapEntry != MemoryMapEntry) {\r
+          NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
+        }\r
+\r
+        NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r
+        continue;\r
+      } else {\r
+        MemoryMapEntry = PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r
+        break;\r
+      }\r
+    } while (TRUE);\r
 \r
     MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
     NewMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, DescriptorSize);\r
@@ -271,6 +241,7 @@ MergeMemoryMap (
   @param  MemoryMapSize          Size, in bytes, of the MemoryMap buffer.\r
   @param  DescriptorSize         Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
 **/\r
+STATIC\r
 VOID\r
 EnforceMemoryMapAttribute (\r
   IN OUT EFI_MEMORY_DESCRIPTOR  *MemoryMap,\r
@@ -304,59 +275,6 @@ EnforceMemoryMapAttribute (
   return ;\r
 }\r
 \r
-/**\r
-  Sort memory map entries whose type is EfiRuntimeServicesCode/EfiRuntimeServicesData,\r
-  from high to low.\r
-  This function assumes memory map is already from low to high, so it just reverts them.\r
-\r
-  @param  MemoryMap              A pointer to the buffer in which firmware places\r
-                                 the current memory map.\r
-  @param  MemoryMapSize          Size, in bytes, of the MemoryMap buffer.\r
-  @param  DescriptorSize         Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
-**/\r
-VOID\r
-RevertRuntimeMemoryMap (\r
-  IN OUT EFI_MEMORY_DESCRIPTOR  *MemoryMap,\r
-  IN UINTN                      MemoryMapSize,\r
-  IN UINTN                      DescriptorSize\r
-  )\r
-{\r
-  EFI_MEMORY_DESCRIPTOR       *MemoryMapEntry;\r
-  EFI_MEMORY_DESCRIPTOR       *MemoryMapEnd;\r
-  EFI_MEMORY_DESCRIPTOR       TempMemoryMap;\r
-\r
-  EFI_MEMORY_DESCRIPTOR       *RuntimeMapEntryBegin;\r
-  EFI_MEMORY_DESCRIPTOR       *RuntimeMapEntryEnd;\r
-\r
-  MemoryMapEntry = MemoryMap;\r
-  RuntimeMapEntryBegin = NULL;\r
-  RuntimeMapEntryEnd = NULL;\r
-  MemoryMapEnd   = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + MemoryMapSize);\r
-  while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {\r
-    if ((MemoryMapEntry->Type == EfiRuntimeServicesCode) ||\r
-        (MemoryMapEntry->Type == EfiRuntimeServicesData)) {\r
-      if (RuntimeMapEntryBegin == NULL) {\r
-        RuntimeMapEntryBegin = MemoryMapEntry;\r
-      }\r
-      RuntimeMapEntryEnd = MemoryMapEntry;\r
-    }\r
-    MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
-  }\r
-\r
-  MemoryMapEntry = RuntimeMapEntryBegin;\r
-  MemoryMapEnd = RuntimeMapEntryEnd;\r
-  while (MemoryMapEntry < MemoryMapEnd) {\r
-    CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r
-    CopyMem (MemoryMapEntry, MemoryMapEnd, sizeof(EFI_MEMORY_DESCRIPTOR));\r
-    CopyMem (MemoryMapEnd, &TempMemoryMap, sizeof(EFI_MEMORY_DESCRIPTOR));\r
-\r
-    MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
-    MemoryMapEnd = PREVIOUS_MEMORY_DESCRIPTOR (MemoryMapEnd, DescriptorSize);\r
-  }\r
-\r
-  return ;\r
-}\r
-\r
 /**\r
   Return the first image record, whose [ImageBase, ImageSize] covered by [Buffer, Length].\r
 \r
@@ -365,6 +283,7 @@ RevertRuntimeMemoryMap (
 \r
   @return first image record covered by [buffer, length]\r
 **/\r
+STATIC\r
 IMAGE_PROPERTIES_RECORD *\r
 GetImageRecordByAddress (\r
   IN EFI_PHYSICAL_ADDRESS  Buffer,\r
@@ -409,6 +328,7 @@ GetImageRecordByAddress (
   @param  OldRecord              A pointer to one old memory map entry.\r
   @param  DescriptorSize         Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
 **/\r
+STATIC\r
 UINTN\r
 SetNewRecord (\r
   IN IMAGE_PROPERTIES_RECORD       *ImageRecord,\r
@@ -447,7 +367,11 @@ SetNewRecord (
       //\r
       // DATA\r
       //\r
-      NewRecord->Type = EfiRuntimeServicesData;\r
+      if (!mPropertiesTableEnable) {\r
+        NewRecord->Type = TempRecord.Type;\r
+      } else {\r
+        NewRecord->Type = EfiRuntimeServicesData;\r
+      }\r
       NewRecord->PhysicalStart = TempRecord.PhysicalStart;\r
       NewRecord->VirtualStart  = 0;\r
       NewRecord->NumberOfPages = EfiSizeToPages(ImageRecordCodeSection->CodeSegmentBase - NewRecord->PhysicalStart);\r
@@ -460,7 +384,11 @@ SetNewRecord (
       //\r
       // CODE\r
       //\r
-      NewRecord->Type = EfiRuntimeServicesCode;\r
+      if (!mPropertiesTableEnable) {\r
+        NewRecord->Type = TempRecord.Type;\r
+      } else {\r
+        NewRecord->Type = EfiRuntimeServicesCode;\r
+      }\r
       NewRecord->PhysicalStart = ImageRecordCodeSection->CodeSegmentBase;\r
       NewRecord->VirtualStart  = 0;\r
       NewRecord->NumberOfPages = EfiSizeToPages(ImageRecordCodeSection->CodeSegmentSize);\r
@@ -484,7 +412,11 @@ SetNewRecord (
   // Final DATA\r
   //\r
   if (TempRecord.PhysicalStart < ImageEnd) {\r
-    NewRecord->Type = EfiRuntimeServicesData;\r
+    if (!mPropertiesTableEnable) {\r
+      NewRecord->Type = TempRecord.Type;\r
+    } else {\r
+      NewRecord->Type = EfiRuntimeServicesData;\r
+    }\r
     NewRecord->PhysicalStart = TempRecord.PhysicalStart;\r
     NewRecord->VirtualStart  = 0;\r
     NewRecord->NumberOfPages = EfiSizeToPages (ImageEnd - TempRecord.PhysicalStart);\r
@@ -504,6 +436,7 @@ SetNewRecord (
   @retval  0 no entry need to be splitted.\r
   @return  the max number of new splitted entries\r
 **/\r
+STATIC\r
 UINTN\r
 GetMaxSplitRecordCount (\r
   IN EFI_MEMORY_DESCRIPTOR *OldRecord\r
@@ -549,6 +482,7 @@ GetMaxSplitRecordCount (
   @retval  0 no entry is splitted.\r
   @return  the real number of splitted record.\r
 **/\r
+STATIC\r
 UINTN\r
 SplitRecord (\r
   IN EFI_MEMORY_DESCRIPTOR     *OldRecord,\r
@@ -564,6 +498,7 @@ SplitRecord (
   UINT64                  PhysicalEnd;\r
   UINTN                   NewRecordCount;\r
   UINTN                   TotalNewRecordCount;\r
+  BOOLEAN                 IsLastRecordData;\r
 \r
   if (MaxSplitRecordCount == 0) {\r
     CopyMem (NewRecord, OldRecord, DescriptorSize);\r
@@ -591,7 +526,17 @@ SplitRecord (
         // If this is still address in this record, need record.\r
         //\r
         NewRecord = PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize);\r
-        if (NewRecord->Type == EfiRuntimeServicesData) {\r
+        IsLastRecordData = FALSE;\r
+        if (!mPropertiesTableEnable) {\r
+          if ((NewRecord->Attribute & EFI_MEMORY_XP) != 0) {\r
+            IsLastRecordData = TRUE;\r
+          }\r
+        } else {\r
+          if (NewRecord->Type == EfiRuntimeServicesData) {\r
+            IsLastRecordData = TRUE;\r
+          }\r
+        }\r
+        if (IsLastRecordData) {\r
           //\r
           // Last record is DATA, just merge it.\r
           //\r
@@ -601,7 +546,11 @@ SplitRecord (
           // Last record is CODE, create a new DATA entry.\r
           //\r
           NewRecord = NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize);\r
-          NewRecord->Type = EfiRuntimeServicesData;\r
+          if (!mPropertiesTableEnable) {\r
+            NewRecord->Type = TempRecord.Type;\r
+          } else {\r
+            NewRecord->Type = EfiRuntimeServicesData;\r
+          }\r
           NewRecord->PhysicalStart = TempRecord.PhysicalStart;\r
           NewRecord->VirtualStart  = 0;\r
           NewRecord->NumberOfPages = TempRecord.NumberOfPages;\r
@@ -628,6 +577,11 @@ SplitRecord (
     TempRecord.NumberOfPages = EfiSizeToPages (PhysicalEnd - PhysicalStart);\r
   } while ((ImageRecord != NULL) && (PhysicalStart < PhysicalEnd));\r
 \r
+  //\r
+  // The logic in function SplitTable() ensures that TotalNewRecordCount will not be zero if the\r
+  // code reaches here.\r
+  //\r
+  ASSERT (TotalNewRecordCount != 0);\r
   return TotalNewRecordCount - 1;\r
 }\r
 \r
@@ -677,6 +631,7 @@ SplitRecord (
                                  the current memory map.\r
   @param  DescriptorSize         Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
 **/\r
+STATIC\r
 VOID\r
 SplitTable (\r
   IN OUT UINTN                  *MemoryMapSize,\r
@@ -756,7 +711,7 @@ SplitTable (
 }\r
 \r
 /**\r
-  This function for GetMemoryMap() with properties table.\r
+  This function for GetMemoryMap() with properties table capability.\r
 \r
   It calls original GetMemoryMap() to get the original memory map information. Then\r
   plus the additional memory map entries for PE Code/Data seperation.\r
@@ -789,7 +744,7 @@ SplitTable (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-CoreGetMemoryMapPropertiesTable (\r
+CoreGetMemoryMapWithSeparatedImageSection (\r
   IN OUT UINTN                  *MemoryMapSize,\r
   IN OUT EFI_MEMORY_DESCRIPTOR  *MemoryMap,\r
   OUT UINTN                     *MapKey,\r
@@ -821,6 +776,7 @@ CoreGetMemoryMapPropertiesTable (
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
     *MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;\r
   } else if (Status == EFI_SUCCESS) {\r
+    ASSERT (MemoryMap != NULL);\r
     if (OldMemoryMapSize - *MemoryMapSize < (*DescriptorSize) * AdditionalRecordCount) {\r
       *MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;\r
       //\r
@@ -844,19 +800,20 @@ CoreGetMemoryMapPropertiesTable (
 //\r
 \r
 /**\r
-  Set PropertiesTable accroding to PE/COFF image section alignment.\r
+  Set PropertiesTable according to PE/COFF image section alignment.\r
 \r
   @param  SectionAlignment    PE/COFF section alignment\r
 **/\r
+STATIC\r
 VOID\r
 SetPropertiesTableSectionAlignment (\r
   IN UINT32  SectionAlignment\r
   )\r
 {\r
-  if (((SectionAlignment & (SIZE_4KB - 1)) != 0) &&\r
+  if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) &&\r
       ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) != 0)) {\r
     DEBUG ((EFI_D_VERBOSE, "SetPropertiesTableSectionAlignment - Clear\n"));\r
-    mPropertiesTable.MemoryProtectionAttribute &= ~EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA;\r
+    mPropertiesTable.MemoryProtectionAttribute &= ~((UINT64)EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);\r
     gBS->GetMemoryMap = CoreGetMemoryMap;\r
     gBS->Hdr.CRC32 = 0;\r
     gBS->CalculateCrc32 ((UINT8 *)gBS, gBS->Hdr.HeaderSize, &gBS->Hdr.CRC32);\r
@@ -869,6 +826,7 @@ SetPropertiesTableSectionAlignment (
   @param  FirstImageRecordCodeSection    first code section in image record\r
   @param  SecondImageRecordCodeSection   second code section in image record\r
 **/\r
+STATIC\r
 VOID\r
 SwapImageRecordCodeSection (\r
   IN IMAGE_PROPERTIES_RECORD_CODE_SECTION      *FirstImageRecordCodeSection,\r
@@ -998,6 +956,7 @@ IsImageRecordCodeSectionValid (
   @param  FirstImageRecord   first image record.\r
   @param  SecondImageRecord  second image record.\r
 **/\r
+STATIC\r
 VOID\r
 SwapImageRecord (\r
   IN IMAGE_PROPERTIES_RECORD      *FirstImageRecord,\r
@@ -1024,6 +983,7 @@ SwapImageRecord (
 /**\r
   Sort image record based upon the ImageBase from low to high.\r
 **/\r
+STATIC\r
 VOID\r
 SortImageRecord (\r
   VOID\r
@@ -1069,6 +1029,7 @@ SortImageRecord (
 /**\r
   Dump image record.\r
 **/\r
+STATIC\r
 VOID\r
 DumpImageRecord (\r
   VOID\r
@@ -1115,11 +1076,15 @@ InsertImageRecord (
   IMAGE_PROPERTIES_RECORD              *ImageRecord;\r
   CHAR8                                *PdbPointer;\r
   IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;\r
-  UINT16                               Magic;\r
 \r
   DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage));\r
   DEBUG ((EFI_D_VERBOSE, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize));\r
 \r
+  if (mPropertiesTableEndOfDxe) {\r
+    DEBUG ((DEBUG_INFO, "Do not insert runtime image record after EndOfDxe\n"));\r
+    return ;\r
+  }\r
+\r
   ImageRecord = AllocatePool (sizeof(*ImageRecord));\r
   if (ImageRecord == NULL) {\r
     return ;\r
@@ -1160,32 +1125,19 @@ InsertImageRecord (
   //\r
   // Get SectionAlignment\r
   //\r
-  if (Hdr.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
-    //\r
-    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value\r
-    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the\r
-    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
-    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
-    //\r
-    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
-  } else {\r
-    //\r
-    // Get the magic value from the PE/COFF Optional Header\r
-    //\r
-    Magic = Hdr.Pe32->OptionalHeader.Magic;\r
-  }\r
-  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+  if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     SectionAlignment  = Hdr.Pe32->OptionalHeader.SectionAlignment;\r
   } else {\r
     SectionAlignment  = Hdr.Pe32Plus->OptionalHeader.SectionAlignment;\r
   }\r
 \r
   SetPropertiesTableSectionAlignment (SectionAlignment);\r
-  if ((SectionAlignment & (SIZE_4KB - 1)) != 0) {\r
-    DEBUG ((EFI_D_ERROR, "!!!!!!!!  InsertImageRecord - Section Alignment(0x%x) is not 4K  !!!!!!!!\n", SectionAlignment));\r
+  if ((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r
+    DEBUG ((EFI_D_WARN, "!!!!!!!!  InsertImageRecord - Section Alignment(0x%x) is not %dK  !!!!!!!!\n",\r
+      SectionAlignment, RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10));\r
     PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress);\r
     if (PdbPointer != NULL) {\r
-      DEBUG ((EFI_D_ERROR, "!!!!!!!!  Image - %a  !!!!!!!!\n", PdbPointer));\r
+      DEBUG ((EFI_D_WARN, "!!!!!!!!  Image - %a  !!!!!!!!\n", PdbPointer));\r
     }\r
     goto Finish;\r
   }\r
@@ -1269,24 +1221,25 @@ InsertImageRecord (
   InsertTailList (&mImagePropertiesPrivateData.ImageRecordList, &ImageRecord->Link);\r
   mImagePropertiesPrivateData.ImageRecordCount++;\r
 \r
-  SortImageRecord ();\r
-\r
   if (mImagePropertiesPrivateData.CodeSegmentCountMax < ImageRecord->CodeSegmentCount) {\r
     mImagePropertiesPrivateData.CodeSegmentCountMax = ImageRecord->CodeSegmentCount;\r
   }\r
 \r
+  SortImageRecord ();\r
+\r
 Finish:\r
   return ;\r
 }\r
 \r
 /**\r
-  Find image record accroding to image base and size.\r
+  Find image record according to image base and size.\r
 \r
   @param  ImageBase    Base of PE image\r
   @param  ImageSize    Size of PE image\r
 \r
   @return image record\r
 **/\r
+STATIC\r
 IMAGE_PROPERTIES_RECORD *\r
 FindImageRecord (\r
   IN EFI_PHYSICAL_ADDRESS  ImageBase,\r
@@ -1335,6 +1288,11 @@ RemoveImageRecord (
   DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage));\r
   DEBUG ((EFI_D_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize));\r
 \r
+  if (mPropertiesTableEndOfDxe) {\r
+    DEBUG ((DEBUG_INFO, "Do not remove runtime image record after EndOfDxe\n"));\r
+    return ;\r
+  }\r
+\r
   ImageRecord = FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize);\r
   if (ImageRecord == NULL) {\r
     DEBUG ((EFI_D_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n"));\r
@@ -1372,7 +1330,8 @@ InstallPropertiesTable (
   VOID                                    *Context\r
   )\r
 {\r
-  if (PcdGetBool (PropertiesTableEnable)) {\r
+  mPropertiesTableEndOfDxe = TRUE;\r
+  if (PcdGetBool (PcdPropertiesTableEnable)) {\r
     EFI_STATUS  Status;\r
 \r
     Status = gBS->InstallConfigurationTable (&gEfiPropertiesTableGuid, &mPropertiesTable);\r
@@ -1380,16 +1339,20 @@ InstallPropertiesTable (
 \r
     DEBUG ((EFI_D_INFO, "MemoryProtectionAttribute - 0x%016lx\n", mPropertiesTable.MemoryProtectionAttribute));\r
     if ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) == 0) {\r
+      DEBUG ((EFI_D_ERROR, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DATA is not set, "));\r
+      DEBUG ((EFI_D_ERROR, "because Runtime Driver Section Alignment is not %dK.\n", RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10));\r
       return ;\r
     }\r
 \r
-    gBS->GetMemoryMap = CoreGetMemoryMapPropertiesTable;\r
+    gBS->GetMemoryMap = CoreGetMemoryMapWithSeparatedImageSection;\r
     gBS->Hdr.CRC32 = 0;\r
     gBS->CalculateCrc32 ((UINT8 *)gBS, gBS->Hdr.HeaderSize, &gBS->Hdr.CRC32);\r
 \r
     DEBUG ((EFI_D_VERBOSE, "Total Image Count - 0x%x\n", mImagePropertiesPrivateData.ImageRecordCount));\r
     DEBUG ((EFI_D_VERBOSE, "Dump ImageRecord:\n"));\r
     DumpImageRecord ();\r
+\r
+    mPropertiesTableEnable = TRUE;\r
   }\r
 }\r
 \r