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
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