RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
MemoryMap.c
Go to the documentation of this file.
1 
11 #include "EfiScannerHeaders.h"
12 
13 
14 CHAR8 *memory_types[] = {
15  "Reserved",
16  "LoaderCode",
17  "LoaderData",
18  "BootServicesCode (BS_code)",
19  "BootServicesData (BS_data)",
20  "RuntimeServicesCode (RT_code)",
21  "RuntimeServicesData (RT_Data)",
22  "ConventionalMemory (available)",
23  "UnusableMemory",
24  "ACPIReclaimMemory (ACPI_recl)",
25  "ACPIMemoryNVS (ACPI_NVS)",
26  "MemoryMappedIO",
27  "MemoryMappedIOPortSpace",
28  "PalCode"};
29 
30 
42 VOID GetMemAttributesStringAscii(CHAR8 *MemAttributesBuff,
43  UINTN MemAttributesBuffSize,
44  UINT64 Attributes)
45 {
46  gBS->SetMem(MemAttributesBuff, MemAttributesBuffSize, 0);
47 
48  if (isMEM_ATTR_MEMORY_UC(Attributes))
49  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_UC |", MemAttributesBuff);
50  if (isMEM_ATTR_MEMORY_WC(Attributes))
51  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_WC |", MemAttributesBuff);
52  if (isMEM_ATTR_MEMORY_WT(Attributes))
53  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_WT |", MemAttributesBuff);
54  if (isMEM_ATTR_MEMORY_WB(Attributes))
55  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_WB |", MemAttributesBuff);
56  if (isMEM_ATTR_MEMORY_UCE(Attributes))
57  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_UCE |", MemAttributesBuff);
58  if (isMEM_ATTR_MEMORY_WP(Attributes))
59  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_WP |", MemAttributesBuff);
60  if (isMEM_ATTR_MEMORY_RP(Attributes))
61  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_RP |", MemAttributesBuff);
62  if (isMEM_ATTR_MEMORY_XP(Attributes))
63  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_XP |", MemAttributesBuff);
64  if (isMEM_ATTR_MEMORY_RUNTIME(Attributes))
65  AsciiSPrint(MemAttributesBuff, MemAttributesBuffSize-1, "%a EFI_MEMORY_RUNTIME |", MemAttributesBuff);
66 
67 }
68 
69 
70 
71 EFI_STATUS GetMemoryEntryGcdInformation(EFI_PHYSICAL_ADDRESS BaseAddress,
72  memory_map_entry *MemEntry)
73 {
74 
75 #if USE_DXE_SERVICES == 1
76  EFI_STATUS Status;
77  EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
78 
79  Status = gDS->GetMemorySpaceDescriptor(BaseAddress, &GcdDescriptor);
80 
81  if (EFI_ERROR(Status))
82  {
83 #if DEBUG_ON == 1
84  LogFullAscii("%a: error GetMemorySpaceDescriptor returned %08x\r\n",
85  __FUNCTION__, Status);
86 #endif
87 
88  return Status;
89  }
90 
91  MemEntry->BaseAddress = GcdDescriptor.BaseAddress;
92  MemEntry->Capabilities = GcdDescriptor.Capabilities;
93  MemEntry->GcdMemoryType = GcdDescriptor.GcdMemoryType;
94  MemEntry->Length = GcdDescriptor.Length;
95  MemEntry->ImageHandle = GcdDescriptor.ImageHandle;
96  MemEntry->DeviceHandle = GcdDescriptor.DeviceHandle;
97 #endif
98 
99  return EFI_SUCCESS;
100 }
101 
102 
115 {
116  EFI_STATUS Status;
117  UINTN MemoryMapSize;
118  EFI_MEMORY_DESCRIPTOR *MemoryMap;
119  UINTN MapKey;
120  UINTN DescriptorSize;
121  UINT32 DescriptorVersion;
122  UINTN Pages;
123  UINTN i;
124 
125  EFI_MEMORY_DESCRIPTOR *EfiMemoryMapEnd;
126  EFI_MEMORY_DESCRIPTOR *EfiEntry;
127  EFI_MEMORY_DESCRIPTOR *NextEfiEntry;
128 
129  memory_map_entry_list *MemMap;
130  memory_map_entry *MemMapEntry;
131 
132 
133  MemoryMap = NULL;
134  MemoryMapSize = 0;
135  Pages = 0;
136 
137 
138  Status = gBS->GetMemoryMap(&MemoryMapSize,
139  MemoryMap,
140  &MapKey,
141  &DescriptorSize,
142  &DescriptorVersion);
143 
144  if (Status == EFI_BUFFER_TOO_SMALL)
145  {
146 
147 #if DEBUG_ON == 1
148  LogFullAscii("%a: Buffer to small, need = 0x%08x(%d) bytes\r\n",
149  __FUNCTION__, MemoryMapSize, MemoryMapSize);
150 #endif
151 
152  MemoryMapSize += PAGE_SIZE;
153  Pages = EFI_SIZE_TO_PAGES(MemoryMapSize);
154 
155  // do not use AllocatePages here, mac apple firmware is buggy
156  // and it will fault when GetMemoryMap will be executed
157  // with memory from AllocatePages as a parameter
158 
159  //MemoryMap = AllocatePages (Pages);
160  MemoryMap = MemAllocate(MemoryMapSize);
161 
162 
163  // get memory map again
164  Status = gBS->GetMemoryMap(
165  &MemoryMapSize,
166  MemoryMap,
167  &MapKey,
168  &DescriptorSize,
169  &DescriptorVersion
170  );
171 
172  if (EFI_ERROR(Status))
173  {
174 #if DEBUG_ON == 1
175  LogFullAscii("%a: GetMemoryMap returned unknown error (2nd time), status = %08d\r\n",
176  __FUNCTION__, Status);
177 #endif
178  return 0;
179  }
180  }
181  else
182  {
183 #if DEBUG_ON == 1
184  LogFullAscii("%a: GetMemoryMap returned unknown error, status = %08x \n",
185  __FUNCTION__, Status);
186 #endif
187 
188  return 0;
189  }
190 
191 
192 #if DEBUG_ON == 1
193  LogFullAscii("%a: Memory map recevied, processing!\r\n", __FUNCTION__);
194 #endif
195 
196 
197  // initialize variables
198  EfiEntry = MemoryMap;
199  NextEfiEntry = NEXT_MEMORY_DESCRIPTOR(EfiEntry, DescriptorSize);
200  EfiMemoryMapEnd = (EFI_MEMORY_DESCRIPTOR*)((UINT8*)MemoryMap + MemoryMapSize);
201 
202 #define NUM_OF_MAP_ENTRIES ((MemoryMapSize / DescriptorSize)+1)
203 
204 
205  // allocate the memory for our structures
207  if (!MemMap)
208  {
209 #if DEBUG_ON == 1
210  LogFullAscii("%a: unable to allocate memory for MemMap\r\n",
211  __FUNCTION__);
212 #endif
213 
214  MemFree(MemoryMap);
215  return 0;
216  }
217 
219 
220  if (!MemMapEntry)
221  {
222 #if DEBUG_ON == 1
223  LogFullAscii("%a: unable to allocate memory for MemMapEntry\r\n",
224  __FUNCTION__);
225 #endif
226 
227  MemFree(MemMap);
228  MemFree(MemoryMap);
229  return 0;
230  }
231 
232 
233 
234  for (i = 0; i < (NUM_OF_MAP_ENTRIES - 1); i++)
235  {
236 
237  MemMapEntry[i].Type = EfiEntry->Type;
238  MemMapEntry[i].PhysicalStart = EfiEntry->PhysicalStart;
239  MemMapEntry[i].VirtualStart = EfiEntry->VirtualStart;
240  MemMapEntry[i].NumberOfPages = EfiEntry->NumberOfPages;
241  MemMapEntry[i].Attributes = EfiEntry->Attribute;
242  GetMemoryEntryGcdInformation(EfiEntry->PhysicalStart, &MemMapEntry[i]);
243 
244  EfiEntry = NEXT_MEMORY_DESCRIPTOR(EfiEntry, DescriptorSize);
245  }
246 
247 
248  MemMap->NumOfEntries = NUM_OF_MAP_ENTRIES - 1;
249  MemMap->MemoryMapEntry = MemMapEntry;
250  MemMap->MapKey = MapKey;
251 
252 
253 #undef NUM_OF_MAP_ENTRIES
254 
255  MemFree(MemoryMap);
256  return MemMap;
257 }
258 
259 
271 {
272  MemFree(MemMap->MemoryMapEntry);
273  MemFree(MemMap);
274 }
275 
276 
277 
278 
279 
280 
281 
295 {
296 
297 #if USE_DXE_SERVICES == 1
298  EFI_STATUS Status;
299  EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
300  UINTN i, NumOfDescriptors;
301 
302 
303  Status = gDS->GetMemorySpaceMap(&NumOfDescriptors, &MemSpaceMap);
304 
305  if (EFI_ERROR(Status))
306  {
307 #if DEBUG_ON == 1
308  LogFullAscii("%a: GetMemorySpaceMap returned %08x\r\n",
309  __FUNCTION__, Status);
310 #endif
311 
312  return Status;
313  }
314 
315 
316  for (i = 0; i < NumOfDescriptors; i++)
317  {
318  LogFullAscii("%08d: BaseAddr=%016lx Length=%016lx Attributes=%016lx\r\n",
319  i, MemSpaceMap[i].BaseAddress, MemSpaceMap[i].Length, MemSpaceMap[i].Attributes);
320  }
321 #endif
322 
323  return EFI_SUCCESS;
324 }
325 
326 
327 
344  EFI_PHYSICAL_ADDRESS Address)
345 {
346  UINTN i;
347  memory_map_entry *MapEntry;
348  EFI_PHYSICAL_ADDRESS PhysEnd;
349 
350 
351  if (!MemMap)
352  return 0;
353 
354  for (i = 0; i < MemMap->NumOfEntries; i++)
355  {
356  MapEntry = &MemMap->MemoryMapEntry[i];
357  PhysEnd = MapEntry->PhysicalStart + EFI_PAGES_TO_SIZE(MapEntry->NumberOfPages);
358 
359  if ((Address >= MapEntry->PhysicalStart) && (Address < PhysEnd))
360  return MapEntry;
361 
362  }
363 
364  return 0;
365 }
366 
367 
382  BOOLEAN DumpToDisk,
383  UINT32 SkipType)
384 {
385  UINTN i;
386  CHAR8 Attributes[256];
387  CHAR16 DmpFilePath[512];
388 
389  memory_map_entry *MapEntry;
390 
391  if (!MemMap)
392  return EFI_NOT_FOUND;
393 
394  LogFullAscii("--- BEGINING MEMORY MAP DUMP ---\r\n");
395  LogFullAscii("MapKey: %08x NumOfEntries=%08x\r\n", MemMap->MapKey, MemMap->NumOfEntries);
396 
397  for (i = 0; i < MemMap->NumOfEntries; i++)
398  {
399  MapEntry = &MemMap->MemoryMapEntry[i];
400 
401  GetMemAttributesStringAscii(Attributes, sizeof(Attributes), MapEntry->Attributes);
402 
403  LogFullAscii("%08d: PhysStart=%016lx PhysEnd=%016lx NumOfPages=%08llx VirtualStart=%016lx Attributes=%016lx (%a) Type=%08x (%a)\r\n",
404  i,
405  MapEntry->PhysicalStart,
406  MapEntry->PhysicalStart + EFI_PAGES_TO_SIZE(MapEntry->NumberOfPages),
407  MapEntry->NumberOfPages,
408  MapEntry->VirtualStart,
409  MapEntry->Attributes,
410  Attributes,
411  MapEntry->Type,
412  memory_types[MapEntry->Type]);
413 
414 
415 #if USE_DXE_SERVICES == 1
416  LogFullAscii("%08d: GcdLength=%08llx GcdMemoryType=%08x GcdBaseAddr=%016lx ImageHandle=%08x\r\n",
417  i,
418  MapEntry->Length,
419  MapEntry->GcdMemoryType,
420  MapEntry->BaseAddress,
421  MapEntry->ImageHandle);
422 #endif
423 
424  if (DumpToDisk)
425  {
426 
427  //"mdump\\%08x.dmp"
428  UnicodeSPrint(DmpFilePath, sizeof(DmpFilePath)-1, L"mdump\\%p.dmp",
429  MapEntry->PhysicalStart);
430 
431  LogFullAscii("%a: dumping to: %s \r\n", __FUNCTION__, DmpFilePath);
432 
433  // remember we always skip ReservedType (value = 0) if SkipType!=0
434  if (SkipType && ((MapEntry->Type == SkipType) || (MapEntry->Type == 0)))
435  {
436  LogFullAscii("%a: not dumping this entry to disk (type skipped)\r\n",
437  __FUNCTION__);
438  continue;
439  }
440 
441 
442  // dump it to file
444  (UINTN)EFI_PAGES_TO_SIZE(MapEntry->NumberOfPages),
445  DmpFilePath,
446  FALSE);
447 
448  } // if dump to disk
449  } // for map entry
450 
451 
452 #if USE_DXE_SERVICES == 1
454 #endif
455 
456  return EFI_SUCCESS;
457 }
458 
459 
460