- ResHob = (EFI_HOB_RESOURCE_DESCRIPTOR *)GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,(VOID *)((UINTN)ResHob + ResHob->Header.HobLength)); \r
- while (ResHob != NULL) {\r
- if (ResHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {\r
- InsertSystemMemoryResources(ResourceList, ResHob);\r
- }\r
- ResHob = (EFI_HOB_RESOURCE_DESCRIPTOR *)GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,(VOID *)((UINTN)ResHob + ResHob->Header.HobLength)); \r
+ if (!IsListEmpty (&AttachedResources)) {\r
+ // See if we can merge the attached resource with other resources\r
+\r
+ NewResource = (BDS_SYSTEM_MEMORY_RESOURCE*)GetFirstNode (&AttachedResources);\r
+ Link = RemoveEntryList (&NewResource->Link);\r
+ while (!IsListEmpty (&AttachedResources)) {\r
+ // Merge resources\r
+ Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)Link;\r
+\r
+ // Ensure they overlap each other\r
+ ASSERT(\r
+ ((NewResource->PhysicalStart >= Resource->PhysicalStart) && (NewResource->PhysicalStart < (Resource->PhysicalStart + Resource->ResourceLength))) ||\r
+ (((NewResource->PhysicalStart + NewResource->ResourceLength) >= Resource->PhysicalStart) && ((NewResource->PhysicalStart + NewResource->ResourceLength) < (Resource->PhysicalStart + Resource->ResourceLength)))\r
+ );\r
+\r
+ NewResourceEnd = MAX (NewResource->PhysicalStart + NewResource->ResourceLength, Resource->PhysicalStart + Resource->ResourceLength);\r
+ NewResource->PhysicalStart = MIN (NewResource->PhysicalStart, Resource->PhysicalStart);\r
+ NewResource->ResourceLength = NewResourceEnd - NewResource->PhysicalStart;\r
+\r
+ Link = RemoveEntryList (Link);\r
+ }\r
+ } else {\r
+ // None of the Resource of the list is attached to this ResHob. Create a new entry for it\r
+ NewResource = AllocateZeroPool (sizeof(BDS_SYSTEM_MEMORY_RESOURCE));\r
+ NewResource->PhysicalStart = ResHob->PhysicalStart;\r
+ NewResource->ResourceLength = ResHob->ResourceLength;\r
+ }\r
+ InsertTailList (ResourceList, &NewResource->Link);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+GetSystemMemoryResources (\r
+ IN LIST_ENTRY *ResourceList\r
+ )\r
+{\r
+ EFI_HOB_RESOURCE_DESCRIPTOR *ResHob;\r
+\r
+ InitializeListHead (ResourceList);\r
+\r
+ // Find the first System Memory Resource Descriptor\r
+ ResHob = (EFI_HOB_RESOURCE_DESCRIPTOR *)GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);\r
+ while ((ResHob != NULL) && (ResHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY)) {\r
+ ResHob = (EFI_HOB_RESOURCE_DESCRIPTOR *)GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,(VOID *)((UINTN)ResHob + ResHob->Header.HobLength));\r
+ }\r
+\r
+ // Did not find any\r
+ if (ResHob == NULL) {\r
+ return EFI_NOT_FOUND;\r
+ } else {\r
+ InsertSystemMemoryResources (ResourceList, ResHob);\r
+ }\r
+\r
+ ResHob = (EFI_HOB_RESOURCE_DESCRIPTOR *)GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR,(VOID *)((UINTN)ResHob + ResHob->Header.HobLength));\r
+ while (ResHob != NULL) {\r
+ if (ResHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {\r
+ InsertSystemMemoryResources (ResourceList, ResHob);\r