From b2dceaa496fad428f5e90d908caa6649140318fe Mon Sep 17 00:00:00 2001
From: Olivier Martin <olivier.martin@arm.com>
Date: Wed, 6 Jul 2011 10:59:01 +0100
Subject: [PATCH] MdeModulePkg/DxeCore: Fix the loop to find the highest
 memory region to fix the DXE allocations

There was a wrong assignment which could lead to an infinte loop in DXE core in some conditions..
---
 MdeModulePkg/Core/Dxe/Gcd/Gcd.c |   59 ++++++++++++++------------------------
 1 files changed, 22 insertions(+), 37 deletions(-)
 mode change 100644 => 100755 MdeModulePkg/Core/Dxe/Gcd/Gcd.c

diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
old mode 100644
new mode 100755
index 0ec75ca..645cdc2
--- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
+++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
@@ -2038,50 +2038,35 @@ CoreInitializeMemoryServices (
   // region that is big enough to initialize the DXE core.  Always skip the PHIT Resource HOB.
   // The max address must be within the physically addressible range for the processor.
   //
-  MaxMemoryLength = 0;
-  MaxAddress      = MAX_ADDRESS;
-  do {
-    HighAddress = 0;
-    Found       = FALSE;
+
+  //
+  // Search for a tested memory region that is above MaxAddress
+  //
+  MaxAddress = 0;
+  for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
     //
-    // Search for a tested memory region that is below MaxAddress
+    // See if this is a resource descriptor HOB that does not contain the PHIT.
     //
-    for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
+    if (Hob.ResourceDescriptor != PhitResourceHob && GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
 
+      ResourceHob = Hob.ResourceDescriptor;
       //
-      // See if this is a resource descriptor HOB that does not contain the PHIT.
+      // See if this resource descrior HOB describes tested system memory below MaxAddress
       //
-      if (Hob.ResourceDescriptor != PhitResourceHob && GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
-
-        ResourceHob = Hob.ResourceDescriptor;
-        //
-        // See if this resource descrior HOB describes tested system memory below MaxAddress
-        //
-        if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
-           (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES &&
-            ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MaxAddress) {
-          //
-          // See if this is the highest tested system memory region below MaxAddress
-          //
-          if (ResourceHob->PhysicalStart > HighAddress) {
-
-            MaxResourceHob = ResourceHob;
-            HighAddress = MaxResourceHob->PhysicalStart;
-            Found = TRUE;
-          }
-        }
+      if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
+         (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES &&
+          ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MaxAddress &&
+          ResourceHob->ResourceLength > MINIMUM_INITIAL_MEMORY_SIZE)
+      {
+        MaxMemoryBaseAddress = PageAlignAddress (MaxResourceHob->PhysicalStart);
+        MaxMemoryLength      = PageAlignLength  (MaxResourceHob->PhysicalStart + MaxResourceHob->ResourceLength - MaxMemoryBaseAddress);
+        MaxMemoryAttributes  = MaxResourceHob->ResourceAttribute;
+
+        // Update Max Address for this new Resource HOB
+        MaxAddress = ResourceHob->PhysicalStart;
       }
     }
-    if (Found) {
-      //
-      // Compute the size of the tested memory region below MaxAddrees
-      //
-      MaxMemoryBaseAddress = PageAlignAddress (MaxResourceHob->PhysicalStart);
-      MaxMemoryLength      = PageAlignLength  (MaxResourceHob->PhysicalStart + MaxResourceHob->ResourceLength - MaxMemoryBaseAddress);
-      MaxMemoryAttributes  = MaxResourceHob->ResourceAttribute;
-    }
-    MaxAddress = ResourceHob->PhysicalStart;
-  } while (Found && MaxMemoryLength < MINIMUM_INITIAL_MEMORY_SIZE);
+  }
 
   if ((Length < MINIMUM_INITIAL_MEMORY_SIZE) ||
       (MaxMemoryBaseAddress > BaseAddress && MaxMemoryLength >= MINIMUM_INITIAL_MEMORY_SIZE)) {
-- 
1.7.5.1

