]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmPkg/BdsLib: Fixed manipulation of the Memory Map returned by GetMemoryMap()
authorOlivier Martin <olivier.martin@arm.com>
Thu, 27 Jun 2013 18:18:24 +0000 (18:18 +0000)
committeroliviermartin <oliviermartin@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 27 Jun 2013 18:18:24 +0000 (18:18 +0000)
The UEFI specification mandates that software uses the DescriptorSize returned
by the GetMemoryMap() function to find the start of each EFI_MEMORY_DESCRIPTOR
in the MemoryMap array. This allows for future expansion of the EFI_MEMORY_DESCRIPTOR.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Olivier Martin <olivier.martin@arm.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14447 6f19259b-4bc3-4df7-8a09-765794883524

ArmPkg/Library/BdsLib/BdsLinuxFdt.c

index 82177402045bfb269d3f6eb62e5de26f37431efc..1927de72641a1aa3ad33a0d565982c67a6757cef 100644 (file)
@@ -354,6 +354,7 @@ PrepareFdt (
   UINT64                CpuReleaseAddr;\r
   UINTN                 MemoryMapSize;\r
   EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
+  EFI_MEMORY_DESCRIPTOR *MemoryMapPtr;\r
   UINTN                 MapKey;\r
   UINTN                 DescriptorSize;\r
   UINT32                DescriptorVersion;\r
@@ -483,24 +484,32 @@ PrepareFdt (
   MemoryMapSize = 0;\r
   Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    // The UEFI specification advises to allocate more memory for the MemoryMap buffer between successive\r
+    // calls to GetMemoryMap(), since allocation of the new buffer may potentially increase memory map size.\r
     Pages = EFI_SIZE_TO_PAGES (MemoryMapSize) + 1;\r
     MemoryMap = AllocatePages (Pages);\r
+    if (MemoryMap == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto FAIL_COMPLETE_FDT;\r
+    }\r
     Status = gBS->GetMemoryMap (&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);\r
   }\r
 \r
   // Go through the list and add the reserved region to the Device Tree\r
   if (!EFI_ERROR(Status)) {\r
-    for (Index = 0; Index < (MemoryMapSize / sizeof(EFI_MEMORY_DESCRIPTOR)); Index++) {\r
-      if (IsLinuxReservedRegion ((EFI_MEMORY_TYPE)MemoryMap[Index].Type)) {\r
+    MemoryMapPtr = MemoryMap;\r
+    for (Index = 0; Index < (MemoryMapSize / DescriptorSize); Index++) {\r
+      if (IsLinuxReservedRegion ((EFI_MEMORY_TYPE)MemoryMapPtr->Type)) {\r
         DEBUG((DEBUG_VERBOSE, "Reserved region of type %d [0x%X, 0x%X]\n",\r
-            MemoryMap[Index].Type,\r
-            (UINTN)MemoryMap[Index].PhysicalStart,\r
-            (UINTN)(MemoryMap[Index].PhysicalStart + MemoryMap[Index].NumberOfPages * EFI_PAGE_SIZE)));\r
-        err = fdt_add_mem_rsv(fdt, MemoryMap[Index].PhysicalStart, MemoryMap[Index].NumberOfPages * EFI_PAGE_SIZE);\r
+            MemoryMapPtr->Type,\r
+            (UINTN)MemoryMapPtr->PhysicalStart,\r
+            (UINTN)(MemoryMapPtr->PhysicalStart + MemoryMapPtr->NumberOfPages * EFI_PAGE_SIZE)));\r
+        err = fdt_add_mem_rsv(fdt, MemoryMapPtr->PhysicalStart, MemoryMapPtr->NumberOfPages * EFI_PAGE_SIZE);\r
         if (err != 0) {\r
           Print(L"Warning: Fail to add 'memreserve' (err:%d)\n", err);\r
         }\r
       }\r
+      MemoryMapPtr = (EFI_MEMORY_DESCRIPTOR*)((UINTN)MemoryMapPtr + DescriptorSize);\r
     }\r
   }\r
 \r