]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c
MdeModulePkg: Refine code to make it more readable.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / PropertiesTable.c
index 5e409eba6f76d806a7c98447780629686e24cd68..34867375b157b782499d354364449da32649b53a 100644 (file)
@@ -94,6 +94,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 +114,7 @@ EfiPagesToSize (
            by Size.\r
 \r
 **/\r
+STATIC\r
 UINT64\r
 EfiSizeToPages (\r
   IN UINT64 Size\r
@@ -124,6 +126,7 @@ EfiSizeToPages (
 /**\r
   Acquire memory lock on mPropertiesTableLock.\r
 **/\r
+STATIC\r
 VOID\r
 CoreAcquirePropertiesTableLock (\r
   VOID\r
@@ -135,6 +138,7 @@ CoreAcquirePropertiesTableLock (
 /**\r
   Release memory lock on mPropertiesTableLock.\r
 **/\r
+STATIC\r
 VOID\r
 CoreReleasePropertiesTableLock (\r
   VOID\r
@@ -143,35 +147,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 +155,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
@@ -224,6 +200,7 @@ SortMemoryMap (
                                  it is the size of new memory map after merge.\r
   @param  DescriptorSize         Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
 **/\r
+STATIC\r
 VOID\r
 MergeMemoryMap (\r
   IN OUT EFI_MEMORY_DESCRIPTOR  *MemoryMap,\r
@@ -271,6 +248,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 +282,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 +290,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 +335,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
@@ -504,6 +431,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
@@ -543,11 +471,13 @@ GetMaxSplitRecordCount (
                                  The caller gurantee the buffer size be 1 +\r
                                  (SplitRecordCount * DescriptorSize) calculated\r
                                  below.\r
+  @param  MaxSplitRecordCount    The max number of splitted entries\r
   @param  DescriptorSize         Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
 \r
   @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
@@ -676,6 +606,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
@@ -786,6 +717,7 @@ SplitTable (
   @retval EFI_INVALID_PARAMETER  One of the parameters has an invalid value.\r
 \r
 **/\r
+STATIC\r
 EFI_STATUS\r
 EFIAPI\r
 CoreGetMemoryMapPropertiesTable (\r
@@ -847,15 +779,16 @@ CoreGetMemoryMapPropertiesTable (
 \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 & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 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
@@ -868,6 +801,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
@@ -891,6 +825,7 @@ SwapImageRecordCodeSection (
 \r
   @param  ImageRecord    image record to be sorted\r
 **/\r
+STATIC\r
 VOID\r
 SortImageRecordCodeSection (\r
   IN IMAGE_PROPERTIES_RECORD              *ImageRecord\r
@@ -934,13 +869,14 @@ SortImageRecordCodeSection (
 }\r
 \r
 /**\r
-  Check if code section in image record is valid\r
+  Check if code section in image record is valid.\r
 \r
   @param  ImageRecord    image record to be checked\r
 \r
   @retval TRUE  image record is valid\r
   @retval FALSE image record is invalid\r
 **/\r
+STATIC\r
 BOOLEAN\r
 IsImageRecordCodeSectionValid (\r
   IN IMAGE_PROPERTIES_RECORD              *ImageRecord\r
@@ -997,6 +933,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
@@ -1023,6 +960,7 @@ SwapImageRecord (
 /**\r
   Sort image record based upon the ImageBase from low to high.\r
 **/\r
+STATIC\r
 VOID\r
 SortImageRecord (\r
   VOID\r
@@ -1066,8 +1004,9 @@ SortImageRecord (
 }\r
 \r
 /**\r
-  Dump image record\r
+  Dump image record.\r
 **/\r
+STATIC\r
 VOID\r
 DumpImageRecord (\r
   VOID\r
@@ -1107,15 +1046,14 @@ InsertImageRecord (
   EFI_IMAGE_DOS_HEADER                 *DosHdr;\r
   UINT32                               PeCoffHeaderOffset;\r
   UINT32                               SectionAlignment;\r
-  UINT16                               ImageType;\r
   EFI_IMAGE_SECTION_HEADER             *Section;\r
   EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION  Hdr;\r
-  UINT16                               Magic;\r
   UINT8                                *Name;\r
   UINTN                                Index;\r
   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
@@ -1153,13 +1091,12 @@ InsertImageRecord (
   Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *) (UINTN) ImageAddress + PeCoffHeaderOffset);\r
   if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
     DEBUG ((EFI_D_VERBOSE, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr.Pe32->Signature));\r
-       // It might be image in SMM.\r
+    // It might be image in SMM.\r
     goto Finish;\r
   }\r
 \r
   //\r
-  // Measuring PE/COFF Image Header;\r
-  // But CheckSum field and SECURITY data directory (certificate) are excluded\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
@@ -1169,20 +1106,22 @@ InsertImageRecord (
     //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
     //\r
     Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
-    ImageType         = Hdr.Pe32->OptionalHeader.Subsystem;\r
-    SectionAlignment  = Hdr.Pe32->OptionalHeader.SectionAlignment;\r
   } else {\r
     //\r
     // Get the magic value from the PE/COFF Optional Header\r
     //\r
     Magic = Hdr.Pe32->OptionalHeader.Magic;\r
-    ImageType         = Hdr.Pe32Plus->OptionalHeader.Subsystem;\r
+  }\r
+  if (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 & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
+    DEBUG ((EFI_D_ERROR, "!!!!!!!!  InsertImageRecord - Section Alignment(0x%x) is not %dK  !!!!!!!!\n",\r
+      SectionAlignment, EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT >> 10));\r
     PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress);\r
     if (PdbPointer != NULL) {\r
       DEBUG ((EFI_D_ERROR, "!!!!!!!!  Image - %a  !!!!!!!!\n", PdbPointer));\r
@@ -1287,6 +1226,7 @@ Finish:
 \r
   @return image record\r
 **/\r
+STATIC\r
 IMAGE_PROPERTIES_RECORD *\r
 FindImageRecord (\r
   IN EFI_PHYSICAL_ADDRESS  ImageBase,\r
@@ -1372,7 +1312,7 @@ InstallPropertiesTable (
   VOID                                    *Context\r
   )\r
 {\r
-  if (PcdGetBool (PropertiesTableEnable)) {\r
+  if (PcdGetBool (PcdPropertiesTableEnable)) {\r
     EFI_STATUS  Status;\r
 \r
     Status = gBS->InstallConfigurationTable (&gEfiPropertiesTableGuid, &mPropertiesTable);\r