RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Memory.c
Go to the documentation of this file.
1 
11 #include "EfiScannerHeaders.h"
12 
13 
25 VOID *MemAllocate(UINTN Size)
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 }
47 
48 
58 __inline VOID MemFree(VOID *Mem)
59 {
60  if (Mem)
61  gBS->FreePool(Mem);
62 
63 }
64 
65 
77 __inline VOID MemCopy(VOID *Dest, VOID *Src, UINTN Size)
78 {
79  gBS->CopyMem(Dest, Src, Size);
80 }
81 
94 __inline VOID MemZero(VOID *Mem, UINTN Size)
95 {
96  gBS->SetMem(Mem, Size, 0);
97 }
98 
99 
100 
117 __inline EFI_STATUS MemRead(VOID *Dest, VOID *Src, UINTN Size)
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 }
145 
146 
175  UINTN Size,
176  CHAR16 *FilePath,
177  BOOLEAN UseProxy)
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 }
281 
282 
283 
284 
285