RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
DumpMem.c File Reference

Functions for displaying and dumping memory map and PE files (including memory search) More...

Include dependency graph for DumpMem.c:

Go to the source code of this file.

Functions

EFI_STATUS DumpPEFile (C_PTR BaseAddr)
 
EFI_STATUS ScanMemoryForExecutables (VOID)
 
EFI_STATUS DumpFirmwareFromROM (VOID)
 
__inline C_PTR IsBadAddress (C_PTR Address)
 

Detailed Description

Functions for displaying and dumping memory map and PE files (including memory search)

Author
Piotr Bania
Date
Feb 2013 Functions for displaying and dumping memory map and PE files (including searching for them in memory).

Definition in file DumpMem.c.

Function Documentation

EFI_STATUS DumpFirmwareFromROM ( VOID  )

Function dumps firmware from ROM to file (mdump\firmware.dmp). The ROM address is constant and it is described by BIOS_ROM_BASE. Typically the size of firmware is smaller than (BIOS_ROM_END-BIOS_ROM_BASE) but in this case we have just decided to dump more.

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 259 of file DumpMem.c.

References BIOS_ROM_BASE, BIOS_ROM_END, DumpMemoryToFile(), EFI_STATUS(), and LogFullAscii().

Referenced by DrawBoxAndMenu().

260 {
261  EFI_STATUS Status;
262  CHAR16 DmpFilePath[512];
263 
264 
265  UnicodeSPrint(DmpFilePath, sizeof(DmpFilePath)-1, L"mdump\\firmware.dmp");
266 
267 
268 #if DEBUG_ON == 1
269  LogFullAscii("%a: Dumping to %s (BiosRomBase=%08x BiosRomEnd=%08x)\r\n",
270  __FUNCTION__, DmpFilePath, BIOS_ROM_BASE, BIOS_ROM_END);
271 #endif
272 
273 
274  Status = DumpMemoryToFile((C_PTR)BIOS_ROM_BASE, (UINTN)(BIOS_ROM_END - BIOS_ROM_BASE), DmpFilePath, TRUE);
275  if (EFI_ERROR(Status))
276  return Status;
277 
278 
279 #if DEBUG_ON == 1
280  LogFullAscii("%a: memory dumped correctly.\r\n", __FUNCTION__);
281 #endif
282 
283  return EFI_SUCCESS;
284 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS DumpPEFile ( C_PTR  BaseAddr)

Function dumps Portable Executable file to file. (by default "bdump\ADDR.dmp")

Parameters
BaseAddrAddress to the located Portable Executable file in memory.
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 30 of file DumpMem.c.

References DumpMemoryToFile(), EFI_IMAGE_DOS_HEADER::e_lfanew, EFI_IMAGE_NT_SIGNATURE, LogFullAscii(), MAX_PE_MEM, EFI_IMAGE_NT_HEADERS64::OptionalHeader, EFI_IMAGE_NT_HEADERS64::Signature, and EFI_IMAGE_OPTIONAL_HEADER64::SizeOfImage.

Referenced by ScanMemoryForExecutables().

31 {
32 
35  UINTN Size;
36  VOID *Mem;
37 
38  CHAR16 DmpFilePath[512];
39  CHAR8 *p;
40 
41  Mem = (VOID*)BaseAddr;
42 
43 #if DEBUG_ON == 1
44  LogFullAscii("%a: trying to dump %016lx.\r\n", __FUNCTION__, (MAX_PTR)BaseAddr);
45 #endif
46 
47 
48  pMZ = (EFI_IMAGE_DOS_HEADER*)Mem;
49 
50  if (pMZ->e_lfanew >= (MAX_PE_MEM/2))
51  {
52  LogFullAscii("%a: error invalid e_lfanew.\r\n", __FUNCTION__);
53  return EFI_NOT_FOUND;
54  }
55 
56  pPE = (EFI_IMAGE_NT_HEADERS64*)((C_PTR)Mem + pMZ->e_lfanew);
57  p = (CHAR8*)((C_PTR)Mem + pMZ->e_lfanew);
58 
60  {
61 #if DEBUG_ON == 1
62  LogFullAscii("%a: error invalid PE signature.\r\n", __FUNCTION__);
63 #endif
64 
65  return EFI_NOT_FOUND;
66  }
67 
68  // check the image size now
69  if (((BaseAddr + pPE->OptionalHeader.SizeOfImage) < BaseAddr) ||
70  (pPE->OptionalHeader.SizeOfImage == 0))
71  {
72 #if DEBUG_ON == 1
73  LogFullAscii("%a: error invalid size of image.\r\n", __FUNCTION__);
74 #endif
75 
76  return EFI_NOT_FOUND;
77  }
78 
79 
80  // everything seems to be ok, lets dump it
81  Size = pPE->OptionalHeader.SizeOfImage;
82 
83  //
84  // warning: do not exceed the filename length (openfile will return error)
85  //
86 
87  UnicodeSPrint(DmpFilePath, sizeof(DmpFilePath)-1, L"bdump\\%p.dmp", BaseAddr);
88 
89 #if DEBUG_ON == 1
90  LogFullAscii("%a: Dumping to %s (ImageSize=%08x)\r\n",
91  __FUNCTION__, DmpFilePath, Size);
92 #endif
93 
94  // dump the file
95  DumpMemoryToFile(BaseAddr, Size, DmpFilePath, FALSE);
96 
97 
98 #if DEBUG_ON == 1
99  LogFullAscii("%a: memory dumped correctly.\r\n", __FUNCTION__);
100 #endif
101 
102  return EFI_SUCCESS;
103 }

Here is the call graph for this function:

Here is the caller graph for this function:

__inline C_PTR IsBadAddress ( C_PTR  Address)

Functions tests if Address equals to StartAddr from bad addrs list (from the policy). If so it returns the EndAddr from the policy. This is done to filter the memory areas which cause machine freeze. Otherwise it returns 0.

Definition at line 295 of file DumpMem.c.

References _policy::BadAddrs, _bad_addr_entry::EndAddr, gEfiScannerPolicy, _policy::NumOfBadAddrs, and _bad_addr_entry::StartAddr.

Referenced by ScanMemoryForExecutables().

296 {
297  UINTN i;
298 
300  return 0;
301 
302  for (i = 0; i < gEfiScannerPolicy.NumOfBadAddrs; i++)
303  if (gEfiScannerPolicy.BadAddrs[i].StartAddr == Address)
305 
306  return 0;
307 }

Here is the caller graph for this function:

EFI_STATUS ScanMemoryForExecutables ( VOID  )

Function scans memory for Portable Executable files and dumps them to file. It can cause machine freeze on custom mobos.

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 121 of file DumpMem.c.

References DumpPEFile(), DumpTEFile(), EFI_STATUS(), FindImageByAddr(), FreeImages(), GetAllImagesWithDescription(), IsBadAddress(), LogFullAscii(), MEM_END_PROBE, MEM_START_PROBE, MemRead(), and TE_SIGNATURE.

Referenced by DrawBoxAndMenu(), and TestDumpHandles().

122 {
123  EFI_STATUS Status;
124  ULONG32 MemProbe;
125 
126  image_entry_list *ImageList;
127  image_entry *ImageEntry;
128 
129  C_PTR MemStart;
130  C_PTR MemEnd;
131  C_PTR NewAddr;
132 
133 
134  LogFullAscii(": Probing memory for executables...\r\n");
135 
136  ImageList = GetAllImagesWithDescription(NULL, FALSE);
137  if (!ImageList)
138  return EFI_NOT_FOUND;
139 
140 
141  // I have tried various tricks to pass the system-freeze
142  // effect which happens while probing specific memory addresses
143  // on mac and asrock mobo. I have tried hooking page fault
144  // and general protection fault handlers however they are not
145  // even executed - machine just freezes that is all.
146  // At this point it would be probably best to create some sort of
147  // black list of addresses although on the other hand
148  // it is still far from perfect solution. Additionally maybe
149  // the user should try dumping the memory map without the
150  // "ReservedMemory" type since it seems this is the only type
151  // causes those problems.
152  // "Memory mapped hardware that causes the computer to freeze
153  // when your memory probe steps on it"
154 
155  // asrock fails:
156  // 0xcf800000 - 0xdfa10000 (0xdf9ffff8)
157  // 0xf7500000 - 0xf7600000
158  // it took 14hours on virtualbox to parse entire 4gb range
159 
160 
161  MemEnd = MEM_END_PROBE;
162  for (MemStart = MEM_START_PROBE; MemStart < MemEnd; MemStart++)
163  {
164 
165 #if CHECK_BAD_ADDRS == 1
166  NewAddr = IsBadAddress(MemStart);
167 
168  if (NewAddr)
169  {
170 #if DEBUG_ON == 1
171  LogFullAscii("%a: changing Addr=%016lx to %016lx because it was found in the bad addr list!\r\n",
172  __FUNCTION__, (MAX_PTR)MemStart, (MAX_PTR)NewAddr);
173 #endif
174 
175  MemStart = NewAddr - 1;
176  continue;
177  }
178 #endif
179 
180  //
181  // On mac mini probing ~0x8b800000 causes machine hang
182  //
183  // On vmware it causes panic when reaching the PCI space (?)
184  // - W110: MONITOR PANIC: vcpu-0:NOT_IMPLEMENTED devices/pci/pci_monitor.c:243
185  // apparently this happens when we are trying to read 32bit value across page boundaries
186  // so lets skip such cases. (0xE2078FFD was one of such regions)
187 
188  if ((MemStart & EFI_PAGE_MASK) >= 0xFFC)
189  {
190  MemStart = MemStart + (EFI_PAGE_MASK - (MemStart & EFI_PAGE_MASK));
191  continue;
192  }
193 
194 
195 #if DEBUG_ON == 1
196  if (((ULONG32)MemStart & 0xFFFFF) == 0)
197  LogFullAscii("%a: scanning %llx\r\n", __FUNCTION__, (MAX_PTR)MemStart);
198 #endif
199 
200  Status = MemRead(&MemProbe, (VOID*)MemStart, sizeof(MemProbe));
201 
202  // MZ signature
203  if (MemProbe == 0x00005a4d)
204  {
205  ImageEntry = FindImageByAddr(ImageList, MemStart);
206 
207  if (!ImageEntry)
208  {
209  LogFullAscii("Found MZ signature at: %016lx, hidden executable? \r\n", (MAX_PTR)MemStart);
210  DumpPEFile(MemStart);
211  }
212  else
213  {
214  LogFullAscii("Found MZ signature at: %016lx, not hidden. \r\n", (MAX_PTR)MemStart);
215  DumpPEFile(MemStart);
216  }
217  }
218  // TE signature
219  else if ((USHORT)MemProbe == TE_SIGNATURE)
220  {
221  ImageEntry = FindImageByAddr(ImageList, MemStart);
222 
223  if (!ImageEntry)
224  {
225  LogFullAscii("Found TE signature at: %016lx, hidden executable? \r\n", (MAX_PTR)MemStart);
226  DumpTEFile(MemStart);
227  }
228  else
229  {
230  LogFullAscii("Found TE signature at: %016lx, not hidden. \r\n", (MAX_PTR)MemStart);
231  DumpTEFile(MemStart);
232  }
233  }
234 
235  }
236 
237  LogFullAscii("Scanning memory done! \r\n");
238  FreeImages(ImageList);
239 
240  return EFI_SUCCESS;
241 }

Here is the call graph for this function:

Here is the caller graph for this function: