]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Fix an issue that there may be an infinite loop in the CoreInitializeMemoryServices...
authorrsun3 <rsun3@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 13 Jul 2011 08:40:55 +0000 (08:40 +0000)
committerrsun3 <rsun3@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 13 Jul 2011 08:40:55 +0000 (08:40 +0000)
Signed-off-by: rsun3
Reviewed-by: mdkinney
Reviewed-by: lgao4
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12015 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Core/Dxe/Gcd/Gcd.c

index 0ec75ca5021c48951b48ab555fdfc8cdac765226..ab8677a972740016fbb44ec77e3b9e019a07c0df 100644 (file)
@@ -1930,9 +1930,9 @@ CoreInitializeMemoryServices (
   EFI_PHYSICAL_ADDRESS               MaxMemoryBaseAddress;\r
   UINT64                             MaxMemoryLength;\r
   UINT64                             MaxMemoryAttributes;\r
-  EFI_PHYSICAL_ADDRESS               MaxAddress;\r
+  EFI_PHYSICAL_ADDRESS               TestedMemoryBaseAddress;\r
+  UINT64                             TestedMemoryLength;\r
   EFI_PHYSICAL_ADDRESS               HighAddress;\r
-  EFI_HOB_RESOURCE_DESCRIPTOR        *MaxResourceHob;\r
   EFI_HOB_GUID_TYPE                  *GuidHob;\r
   UINT32                              ReservedCodePageNumber;\r
 \r
@@ -1952,7 +1952,6 @@ CoreInitializeMemoryServices (
   // Initialize Local Variables\r
   //\r
   PhitResourceHob       = NULL;\r
-  MaxResourceHob        = NULL;\r
   ResourceHob           = NULL;\r
   BaseAddress           = 0;\r
   Length                = 0;\r
@@ -1989,43 +1988,68 @@ CoreInitializeMemoryServices (
   }\r
 \r
   //\r
-  // Find the Resource Descriptor HOB that contains range FreeMemoryBaseAddress..FreeMemoryLength\r
+  // Find the Resource Descriptor HOB that contains PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop\r
   //\r
   Length = 0;\r
   Found  = FALSE;\r
   for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+    //\r
+    // Skip all HOBs except Resource Descriptor HOBs\r
+    //\r
+    if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+      continue;\r
+    }\r
 \r
-    if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+    //\r
+    // Skip Resource Descriptor HOBs that do not describe tested system memory\r
+    //\r
+    ResourceHob = Hob.ResourceDescriptor;\r
+    if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {\r
+      continue;\r
+    }\r
+    if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {\r
+      continue;\r
+    }\r
 \r
-      ResourceHob = Hob.ResourceDescriptor;\r
+    //\r
+    // Skip Resource Descriptor HOBs that do not contain the PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop\r
+    //\r
+    if (PhitHob->EfiFreeMemoryBottom < ResourceHob->PhysicalStart) {\r
+      continue;\r
+    }\r
+    if (PhitHob->EfiFreeMemoryTop > (ResourceHob->PhysicalStart + ResourceHob->ResourceLength)) {\r
+      continue;\r
+    }\r
 \r
-      if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY                                       &&\r
-          (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES    ) {\r
-\r
-        if (PhitHob->EfiFreeMemoryBottom >= ResourceHob->PhysicalStart                         &&\r
-            PhitHob->EfiFreeMemoryTop    <= (ResourceHob->PhysicalStart + ResourceHob->ResourceLength)    ) {\r
-\r
-          //\r
-          // Cache the resource descriptor HOB for the memory region described by the PHIT HOB\r
-          //\r
-          PhitResourceHob = ResourceHob;\r
-          Found = TRUE;\r
-\r
-          Attributes  = PhitResourceHob->ResourceAttribute;\r
-          BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);\r
-          Length      = PageAlignLength  (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - BaseAddress);\r
-          if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {\r
-            BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);\r
-            Length      = PageAlignLength  (PhitHob->EfiFreeMemoryTop - BaseAddress);\r
-            if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {\r
-              BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);\r
-              Length      = PageAlignLength  ((UINT64)((UINTN)*HobStart - BaseAddress));\r
-            }\r
-          }\r
-          break;\r
-        }\r
+    //\r
+    // Cache the resource descriptor HOB for the memory region described by the PHIT HOB\r
+    //\r
+    PhitResourceHob = ResourceHob;\r
+    Found = TRUE;\r
+\r
+    //\r
+    // Compute range between PHIT EfiFreeMemoryTop and the end of the Resource Descriptor HOB\r
+    //\r
+    Attributes  = PhitResourceHob->ResourceAttribute;\r
+    BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);\r
+    Length      = PageAlignLength  (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - BaseAddress);\r
+    if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {\r
+      //\r
+      // If that range is not large enough to intialize the DXE Core, then \r
+      // Compute range between PHIT EfiFreeMemoryBottom and PHIT EfiFreeMemoryTop\r
+      //\r
+      BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);\r
+      Length      = PageAlignLength  (PhitHob->EfiFreeMemoryTop - BaseAddress);\r
+      if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {\r
+        //\r
+        // If that range is not large enough to intialize the DXE Core, then \r
+        // Compute range between the start of the Resource Descriptor HOB and the start of the HOB List\r
+        //\r
+        BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);\r
+        Length      = PageAlignLength  ((UINT64)((UINTN)*HobStart - BaseAddress));\r
       }\r
     }\r
+    break;\r
   }\r
 \r
   //\r
@@ -2038,51 +2062,66 @@ CoreInitializeMemoryServices (
   // region that is big enough to initialize the DXE core.  Always skip the PHIT Resource HOB.\r
   // The max address must be within the physically addressible range for the processor.\r
   //\r
-  MaxMemoryLength = 0;\r
-  MaxAddress      = MAX_ADDRESS;\r
-  do {\r
-    HighAddress = 0;\r
-    Found       = FALSE;\r
+  HighAddress = MAX_ADDRESS;\r
+  for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
     //\r
-    // Search for a tested memory region that is below MaxAddress\r
+    // Skip the Resource Descriptor HOB that contains the PHIT\r
     //\r
-    for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
-\r
-      //\r
-      // See if this is a resource descriptor HOB that does not contain the PHIT.\r
-      //\r
-      if (Hob.ResourceDescriptor != PhitResourceHob && GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+    if (Hob.ResourceDescriptor == PhitResourceHob) {\r
+      continue;\r
+    }\r
+    //\r
+    // Skip all HOBs except Resource Descriptor HOBs\r
+    //\r
+    if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+      continue;\r
+    }\r
 \r
-        ResourceHob = Hob.ResourceDescriptor;\r
-        //\r
-        // See if this resource descrior HOB describes tested system memory below MaxAddress\r
-        //\r
-        if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
-           (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES &&\r
-            ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MaxAddress) {\r
-          //\r
-          // See if this is the highest tested system memory region below MaxAddress\r
-          //\r
-          if (ResourceHob->PhysicalStart > HighAddress) {\r
-\r
-            MaxResourceHob = ResourceHob;\r
-            HighAddress = MaxResourceHob->PhysicalStart;\r
-            Found = TRUE;\r
-          }\r
-        }\r
-      }\r
+    //\r
+    // Skip Resource Descriptor HOBs that do not describe tested system memory below MAX_ADDRESS\r
+    //\r
+    ResourceHob = Hob.ResourceDescriptor;\r
+    if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {\r
+      continue;\r
     }\r
-    if (Found) {\r
-      //\r
-      // Compute the size of the tested memory region below MaxAddrees\r
-      //\r
-      MaxMemoryBaseAddress = PageAlignAddress (MaxResourceHob->PhysicalStart);\r
-      MaxMemoryLength      = PageAlignLength  (MaxResourceHob->PhysicalStart + MaxResourceHob->ResourceLength - MaxMemoryBaseAddress);\r
-      MaxMemoryAttributes  = MaxResourceHob->ResourceAttribute;\r
+    if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {\r
+      continue;\r
+    }\r
+    if ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength) > (EFI_PHYSICAL_ADDRESS)MAX_ADDRESS) {\r
+      continue;\r
     }\r
-    MaxAddress = ResourceHob->PhysicalStart;\r
-  } while (Found && MaxMemoryLength < MINIMUM_INITIAL_MEMORY_SIZE);\r
+    \r
+    //\r
+    // Skip Resource Descriptor HOBs that are below a previously found Resource Descriptor HOB\r
+    //\r
+    if (HighAddress != (EFI_PHYSICAL_ADDRESS)MAX_ADDRESS && ResourceHob->PhysicalStart <= HighAddress) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Skip Resource Descriptor HOBs that are not large enough to initilize the DXE Core\r
+    //\r
+    TestedMemoryBaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);\r
+    TestedMemoryLength      = PageAlignLength  (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - TestedMemoryBaseAddress);\r
+    if (TestedMemoryLength < MINIMUM_INITIAL_MEMORY_SIZE) {\r
+      continue;\r
+    }\r
+    \r
+    //\r
+    // Save the Resource Descriptor HOB context that is large enough to initilize the DXE Core\r
+    //\r
+    MaxMemoryBaseAddress = TestedMemoryBaseAddress;\r
+    MaxMemoryLength      = TestedMemoryLength;\r
+    MaxMemoryAttributes  = ResourceHob->ResourceAttribute; \r
+    HighAddress          = ResourceHob->PhysicalStart;\r
+  }\r
 \r
+  //\r
+  // If Length is not large enough to initialize the DXE Core or a Resource \r
+  // Descriptor HOB was found above the PHIT HOB that is large enough to initialize \r
+  // the DXE Core, then use the range described by the Resource Descriptor \r
+  // HOB that was found above the PHIT HOB.\r
+  //\r
   if ((Length < MINIMUM_INITIAL_MEMORY_SIZE) ||\r
       (MaxMemoryBaseAddress > BaseAddress && MaxMemoryLength >= MINIMUM_INITIAL_MEMORY_SIZE)) {\r
     BaseAddress = MaxMemoryBaseAddress;\r