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

Functions related to memory operations (including dumping memory) More...

Include dependency graph for Memory.c:

Go to the source code of this file.

Functions

VOID * MemAllocate (UINTN Size)
 
__inline VOID MemFree (VOID *Mem)
 
__inline VOID MemCopy (VOID *Dest, VOID *Src, UINTN Size)
 
__inline VOID MemZero (VOID *Mem, UINTN Size)
 
__inline EFI_STATUS MemRead (VOID *Dest, VOID *Src, UINTN Size)
 
EFI_STATUS DumpMemoryToFile (C_PTR Address, UINTN Size, CHAR16 *FilePath, BOOLEAN UseProxy)
 

Detailed Description

Functions related to memory operations (including dumping memory)

Author
Piotr Bania
Date
Feb 2013 Functions related to memory operations (including dumping memory)

Definition in file Memory.c.

Function Documentation

EFI_STATUS DumpMemoryToFile ( C_PTR  Address,
UINTN  Size,
CHAR16 *  FilePath,
BOOLEAN  UseProxy 
)

Function dumps memory from Address to (Address + Size) to file.

If UseProxy is set to TRUE then before we will execute File->Write the source memory will be copied to our newly allocated buffer. This decreases the performance but on the other hand it increases the stability. On real systems for example when you are trying to dump images from memory and you want to do this directly (without the memory proxy) your machine will often freeze. Obviously in some cases this method will not work due to the gigantic Size parameter. Additionally maybe we should disable this option when doing a full memory dump (just to keep the memory layout intact).

Parameters
AddressAddress inidicating memory to dump
SizeSize of memory to dump
FilePathPointer to file path (unicode)
UseProxyShould we copy the source to our memory firstly
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 174 of file Memory.c.

References EFI_STATUS(), LogFullAscii(), MemAllocate(), MemCopy(), MemFree(), and OpenFile().

Referenced by CommandDumpMem(), DumpFirmwareFromROM(), DumpImageToDisk(), DumpMemoryMap(), DumpPEFile(), DumpTEFile(), and hooked_LoadImage().

178 {
179  EFI_STATUS Status;
180  EFI_FILE_PROTOCOL *File;
181  UINTN SizeCopy;
182  UCHAR *Mem;
183 
184  if ((FilePath == 0) || (Size == 0))
185  return EFI_NOT_FOUND;
186 
187  // create destination file
188  Status = OpenFile(FilePath,
189  EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ | EFI_FILE_MODE_CREATE,
190  &File);
191 
192  if (EFI_ERROR(Status))
193  {
194 
195 #if DEBUG_ON == 1
196  LogFullAscii("%a: unable to create dump file, status = %08x\r\n",
197  __FUNCTION__, Status);
198 #endif
199 
200  // after EFI_DEVICE_ERROR all the file system operations are not working
201  if (Status == EFI_DEVICE_ERROR)
202  LogFullAscii("%a: OpenFile returned EFI_DEVICE_ERROR (FATAL), trying to reset!\r\n",
203  __FUNCTION__);
204 
205  return Status;
206  }
207 
208 #if DEBUG_ON == 1
209  LogFullAscii("%a: writing %08x bytes\r\n", __FUNCTION__, Size);
210 #endif
211 
212  SizeCopy = Size;
213 
214  if (UseProxy)
215  {
216  // allocate memory and copy the data there firstly
217  Mem = (UCHAR*)MemAllocate(Size);
218  if (Mem)
219  {
220 #if DEBUG_ON == 1
221  LogFullAscii("%a: storing to mem firstly Mem=%p\r\n", __FUNCTION__, Mem);
222 #endif
223 
224  MemCopy((VOID*)Mem, (VOID*)Address, Size);
225  Status = File->Write(File, &SizeCopy, (VOID*)Mem);
226  File->Close(File);
227  MemFree(Mem);
228 
229 
230  if (EFI_ERROR(Status))
231  {
232 #if DEBUG_ON == 1
233  LogFullAscii("%a: Error writing to file failed, status = %08x\r\n",
234  __FUNCTION__, Status);
235 #endif
236  return Status;
237  }
238 
239 
240 #if DEBUG_ON == 1
241  LogFullAscii("%a: region %016lx-%016lx (size=%08x) dumped (written %08x bytes)\r\n",
242  __FUNCTION__,
243  (MAX_PTR)Address,
244  (MAX_PTR)(Address + Size),
245  Size,
246  SizeCopy);
247 #endif
248 
249  return EFI_SUCCESS;
250  }
251  }
252 
253 #if DEBUG_ON == 1
254  LogFullAscii("%a: using direct read mode\r\n", __FUNCTION__);
255 #endif
256 
257  Status = File->Write(File, &SizeCopy, (VOID*)Address);
258  File->Close(File);
259 
260  if (EFI_ERROR(Status))
261  {
262 #if DEBUG_ON == 1
263  LogFullAscii("%a: Error writing to file failed, status = %08x\r\n",
264  __FUNCTION__, Status);
265 #endif
266  return Status;
267  }
268 
269 
270 #if DEBUG_ON == 1
271  LogFullAscii("%a: region %016lx-%016lx (size=%08x) dumped (written %08x bytes)\r\n",
272  __FUNCTION__,
273  (MAX_PTR)Address,
274  (MAX_PTR)(Address + Size),
275  Size,
276  SizeCopy);
277 #endif
278 
279  return EFI_SUCCESS;
280 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID* MemAllocate ( UINTN  Size)

Function allocates memory and fills it with zeros.

Parameters
SizeRequested size
Return values
PointerOn success function returns pointer to the newly allocated memory
NULLOn error

Definition at line 25 of file Memory.c.

References EFI_STATUS(), and LogFullAscii().

Referenced by AddImageToList(), DumpMemoryToFile(), GetAllEvents(), GetAllHandles(), GetAllHandlesWithDescription(), GetAllImagesWithDescription(), GetMemoryMapList(), ImageAllocateStruct(), and ReadConfiguration().

26 {
27  EFI_STATUS Status;
28  VOID *Mem;
29 
30  Status = gBS->AllocatePool(EfiRuntimeServicesData,
31  Size,
32  (VOID**)&Mem);
33 
34  if (EFI_ERROR(Status))
35  {
36 #if DEBUG_ON == 1
37  LogFullAscii("%a: Unable to allocate memory size = %08x # FATAL ERROR\r\n",
38  __FUNCTION__, Size);
39 #endif
40  ASSERT(!EFI_ERROR(Status));
41  return 0;
42  }
43 
44  gBS->SetMem(Mem, Size, 0);
45  return Mem;
46 }

Here is the call graph for this function:

Here is the caller graph for this function:

__inline VOID MemCopy ( VOID *  Dest,
VOID *  Src,
UINTN  Size 
)

Function copies source memory to destination memory.

Parameters
DestPointer to destination memory
SrcPointer to source memory
SizeSize of data to copy
Return values
NONE

Definition at line 77 of file Memory.c.

Referenced by AddImageToList(), DumpMemoryToFile(), GetAllEvents(), GetArguments(), MemRead(), and ParseConfiguration().

78 {
79  gBS->CopyMem(Dest, Src, Size);
80 }

Here is the caller graph for this function:

__inline VOID MemFree ( VOID *  Mem)

Function frees allocated memory.

Parameters
MemPointer to the allocated memory
Return values
NONE

Definition at line 58 of file Memory.c.

Referenced by AddImageToList(), DumpMemoryToFile(), FreeEvents(), FreeHandles(), FreeImages(), FreeMemMap(), GetAllEvents(), GetAllHandles(), GetAllHandlesWithDescription(), GetAllImagesWithDescription(), GetImagePathAndName(), GetMemoryMapList(), ImageAllocateStruct(), ImageFree(), and ReadConfiguration().

59 {
60  if (Mem)
61  gBS->FreePool(Mem);
62 
63 }

Here is the caller graph for this function:

__inline EFI_STATUS MemRead ( VOID *  Dest,
VOID *  Src,
UINTN  Size 
)

Function reads selected portion (Size) of memory (Src) to Dest. In current states it uses CopyMem from BootServices but it can be freely changed to other "memcpy" implementation. Due to EFI memory characteristics you can probe all available memory without a page fault (at least in theory).

Parameters
DestPointer to destination memory
SrcPointer to source memory
SizeSize of data to copy
Return values
EFI_SUCCESS

Definition at line 117 of file Memory.c.

References MemCopy().

Referenced by ScanMemoryForExecutables().

118 {
119  switch (Size)
120  {
121  default:
122  MemCopy(Dest, Src, Size);
123  break;
124 
125  case 1:
126  *(UINT8*)Dest = *(UINT8*)Src;
127  break;
128 
129  case 2:
130  *(UINT16*)Dest = *(UINT16*)Src;
131  break;
132 
133  case 4:
134  *(UINT32*)Dest = *(UINT32*)Src;
135  break;
136 
137  case 8:
138  *(UINT64*)Dest = *(UINT64*)Src;
139  break;
140  }
141 
142 
143  return EFI_SUCCESS;
144 }

Here is the call graph for this function:

Here is the caller graph for this function:

__inline VOID MemZero ( VOID *  Mem,
UINTN  Size 
)

Function zeros memory.

Parameters
MemPointer to destination memory
SizeSize of data to nullify
Return values
NONE

Definition at line 94 of file Memory.c.

Referenced by ComConsoleStart(), InstallEventScanner(), and ProcessCommand().

95 {
96  gBS->SetMem(Mem, Size, 0);
97 }

Here is the caller graph for this function: