RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
ComConsole.c
Go to the documentation of this file.
1 #include "EfiScannerHeaders.h"
2 
3 
4 UCHAR ComGetChar(VOID);
5 VOID ComPutChar(UCHAR c);
6 
7 
8 #define KEY_ESCAPE 0x1B
9 #define KEY_ENTER 0x0D
10 #define KEY_BACKSPACE 0x7f //0x08
11 
12 
13 typedef struct _cargs
14 {
15  UINTN argc;
16  char command[16];
17 
18  char arg1[32];
19  char arg2[32];
20  char arg3[32];
21 
25 
26 } cargs;
27 
28 
29 
30 CHAR8 *AsciiStrChr (CHAR8 *Str, CHAR8 Char)
31 {
32  for (; *Str != Char; ++Str)
33  {
34  if (*Str == '\0')
35  return NULL;
36  }
37 
38  return (CHAR8 *) Str;
39 }
40 
41 
48 UINTN GetArguments(CHAR8 *Cmd, cargs *OutArgs)
49 {
50  CHAR8 *p, *pp;
51  UINTN NumOfArgs, itemp;
52 
53  NumOfArgs = 0;
54 
55  p = AsciiStrChr(Cmd, ' ');
56  if (!p)
57  {
58  // no arguments just the command
59  AsciiSPrint(OutArgs->command, sizeof(OutArgs->command) - 1, "%a", Cmd);
60  return 0;
61  }
62 
63  itemp = (UINTN)(p - Cmd);
64  if (itemp >= sizeof(OutArgs->command))
65  return 0;
66 
67  MemCopy(OutArgs->command, Cmd, itemp);
68 
69 #define get_arg(argx, argx_val) { pp = AsciiStrChr(++p, ' '); \
70  if (!pp) return NumOfArgs; \
71  itemp = (UINTN)(pp - p); \
72  if ((itemp >= (sizeof(OutArgs->argx) - 1)) || (itemp < 1)) return NumOfArgs; \
73  MemCopy(OutArgs->argx, p, itemp); \
74  OutArgs->argx_val = (ULONG32)AsciiStrHexToUintn(OutArgs->argx); \
75  NumOfArgs++; \
76  p = pp; \
77  OutArgs->argc = NumOfArgs; }
78 
79  get_arg(arg1, arg1_val);
80  get_arg(arg2, arg2_val);
81  get_arg(arg3, arg3_val);
82  OutArgs->argc = NumOfArgs;
83 
84 #undef get_arg
85 
86  return NumOfArgs;
87 }
88 
89 
90 
99 {
100  CHAR8 Attributes[256];
101  memory_map_entry *MapEntry;
102  memory_map_entry_list *MemMap;
103 
104  MemMap = GetMemoryMapList();
105  if (!MemMap)
106  {
107  LogFullAscii("%a: Error: GetMemoryMapList() returned 0\r\n", __FUNCTION__);
108  return EFI_NOT_FOUND;
109  }
110 
111  if (Args->argc > 0)
112  {
113  MapEntry = FindMapEntryByAddr(MemMap, (EFI_PHYSICAL_ADDRESS)Args->arg1_val);
114  LogFullAscii("%a: meminfo looking for address = %x \r\n", __FUNCTION__, Args->arg1_val);
115 
116  if (MapEntry)
117  {
118  GetMemAttributesStringAscii(Attributes, sizeof(Attributes), MapEntry->Attributes);
119  LogFullAscii("%a: Map Entry Found: PhysStart=%016lx NumOfPages=%08llx VirtualStart=%016lx Attributes=%016lx (%a)\r\n",
120  __FUNCTION__,
121  MapEntry->PhysicalStart,
122  MapEntry->NumberOfPages,
123  MapEntry->VirtualStart,
124  MapEntry->Attributes,
125  Attributes);
126  }
127  else
128  LogFullAscii("%a: meminfo entry not found\r\n", __FUNCTION__);
129 
130  }
131  else
132  DumpMemoryMap(MemMap, FALSE, 0);
133 
134  FreeMemMap(MemMap);
135  return EFI_SUCCESS;
136 }
137 
138 
147 {
148  image_entry_list *ImageList;
149  image_entry *ImageEntry;
150 
151  ImageList = GetAllImagesWithDescription(NULL, TRUE);
152  if (!ImageList)
153  {
154  LogFullAscii("error: GetAllImagesWithDescription returned 0\r\n");
155  return EFI_NOT_FOUND;
156  }
157 
158  if (Args->argc > 0)
159  {
160  ImageEntry = FindImageByAddr(ImageList, (C_PTR)Args->arg1_val);
161  if (ImageEntry)
162  {
163  LogFullAscii("%a: image entry found -> handle=%08x imagebase=%p imagesize=%x name=%a filepath=%a\r\n",
164  __FUNCTION__,
165  ImageEntry->Handle,
166  ImageEntry->ImageBase,
167  ImageEntry->ImageSize,
168  ImageEntry->Name,
169  ImageEntry->FilePath);
170 
171  }
172  else
173  {
174  LogFullAscii("%a: unable to locate image, address = %x!\r\n",
175  __FUNCTION__, Args->arg1_val);
176  FreeImages(ImageList);
177  return EFI_NOT_FOUND;
178  }
179  }
180  else
181  {
182  DumpAllImages(ImageList, FALSE);
183  }
184 
185 
186  FreeImages(ImageList);
187  return EFI_SUCCESS;
188 }
189 
190 
198 {
199  CHAR16 DmpFilePath[64];
200 
201  if (Args->argc != 3)
202  {
203  LogFullAscii("%a: invalid number of arguments \r\n",
204  __FUNCTION__, Args->argc);
205  return EFI_NOT_FOUND;
206  }
207 
208 
209  LogFullAscii("%a: storing range %x-%x to %a\r\n",
210  __FUNCTION__,
211  Args->arg1_val,
212  Args->arg1_val + Args->arg2_val,
213  Args->arg3);
214 
215  UnicodeSPrint(DmpFilePath, sizeof(DmpFilePath)-1, L"%a", Args->arg3);
216  DumpMemoryToFile((C_PTR)Args->arg1_val, Args->arg2_val, DmpFilePath, FALSE);
217 
218  return EFI_SUCCESS;
219 }
220 
221 
229 {
230  C_PTR Addr;
231 
233 
234  if (Args->argc == 0)
235  {
236  DumpEFITables(FALSE);
237  }
238  else
239  {
240  Addr = EFITableGetAddr(Args->arg1);
241  if (Addr)
242  LogFullAscii("%a: function %a resolved to %p \r\n",
243  __FUNCTION__, Args->arg1, Addr);
244  else
245  LogFullAscii("%a: unable to resolve function %a\r\n",
246  __FUNCTION__, Args->arg1);
247  }
248 
249 
250  return EFI_SUCCESS;
251 }
252 
262 {
263  CHAR8 EventTypeBuff[128];
264  event_entry_list *EventList;
265  event_entry *EventEntry;
266  UINTN i, Type;
267 
268  EventList = GetAllEvents();
269  if (!EventList)
270  {
271  LogFullAscii("%a: unable to receive EventList\r\n", __FUNCTION__);
272  return EFI_NOT_FOUND;
273  }
274 
275  if (Args->argc == 0)
276  {
277  DumpAllEvents(EventList);
278  FreeEvents(EventList);
279  return EFI_SUCCESS;
280  }
281 
282 
283  if (strcmp(Args->arg1, "type") == 0)
284  {
285  Type = 0;
286 
287  if (strcmp(Args->arg2, "TIMER") == 0)
288  Type = EVT_TIMER;
289  else if (strcmp(Args->arg2, "RUNTIME") == 0)
290  Type = EVT_RUNTIME;
291  else if (strcmp(Args->arg2, "NOTIFY_WAIT") == 0)
292  Type = EVT_NOTIFY_WAIT;
293  else if (strcmp(Args->arg2, "NOTIFY_SIGNAL") == 0)
294  Type = EVT_NOTIFY_SIGNAL;
295  else if (strcmp(Args->arg2, "SIGNAL_EXIT") == 0)
296  Type = EVT_SIGNAL_EXIT_BOOT_SERVICES;
297  else if (strcmp(Args->arg2, "SIGNAL_VA_CHANGE") == 0)
298  Type = EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE;
299 
300  for (i = 0; i < EventList->NumOfEntries; i++)
301  {
302  EventEntry = &EventList->EventEntry[i];
303 
304  if ((EventEntry->Type & Type) == Type)
305  {
306  GetEventTypeStringAscii(EventTypeBuff, sizeof(EventTypeBuff), EventEntry->Type);
307  LogFullAscii("%a: event entry found addr = %p, function ptr = %p, type = %a\r\n",
308  __FUNCTION__,
309  EventEntry,
310  EventEntry->NotifyFunction,
311  EventTypeBuff);
312  }
313  }
314 
315  }
316  else if (strcmp(Args->arg1, "addr") == 0)
317  {
318  EventEntry = FindEventByAddr(EventList, Args->arg2_val);
319 
320  if (EventEntry)
321  {
322  GetEventTypeStringAscii(EventTypeBuff, sizeof(EventTypeBuff), EventEntry->Type);
323  LogFullAscii("%a: event entry found addr = %p, function ptr = %p, type = %a\r\n",
324  __FUNCTION__,
325  EventEntry,
326  EventEntry->NotifyFunction,
327  EventTypeBuff);
328  }
329  else
330  LogFullAscii("%a: unable to find event entry for addr = %x\r\n",
331  __FUNCTION__, Args->arg2_val);
332  }
333  else
334  LogFullAscii("%a: invalid command syntax\r\n", __FUNCTION__);
335 
336  FreeEvents(EventList);
337  return EFI_SUCCESS;
338 }
339 
340 
346 {
347 
348  LogFullAscii("-----------------------------------------------\r\n");
349  LogFullAscii("%a: RDFU CONSOLE HELP \r\n", __FUNCTION__);
350  LogFullAscii("%a: available commands\r\n", __FUNCTION__);
351  LogFullAscii("-----------------------------------------------\r\n");
352  LogFullAscii("show <idt/gdt/context>\r\n");
353  LogFullAscii("meminfo <ADDRESS - optional>\r\n");
354  LogFullAscii("driverinfo <ADDRESS - optional>\r\n");
355  LogFullAscii("dumpmem <STARTADDR> <SIZE> <FILENAME>\r\n");
356  LogFullAscii("function <EFI FUNCTION NAME>\r\n");
357  LogFullAscii("eventinfo\r\n");
358  LogFullAscii("eventinfo addr <ADDRESS>\r\n");
359  LogFullAscii("eventinfo type <TIMER/RUNTIME/NOTIFY_WAIT/NOTIFY_SIGNAL/SIGNAL_EXIT/SIGNAL_VA_CHANGE>\r\n");
360  LogFullAscii("-----------------------------------------------\r\n");
361 
362  return EFI_SUCCESS;
363 }
364 
365 
371 {
372  cargs Args;
373 
374  MemZero(&Args, sizeof(Args));
375  GetArguments(Cmd, &Args);
376 
377  //AsciiPrint("Main commands: '%a' \r\n", Args.command);
378  //AsciiPrint("Arg1: '%a' (%x)\r\n", Args.arg1, Args.arg1_val);
379  //AsciiPrint("Arg2: '%a' (%x)\r\n", Args.arg2, Args.arg2_val);
380  //AsciiPrint("Arg3: '%a' (%x)\r\n", Args.arg3, Args.arg3_val);
381 
382 
383  if (strcmp(Args.command, "show") == 0)
384  {
385  if (strcmp(Args.arg1, "gdt") == 0)
386  DumpGDT();
387  else if (strcmp(Args.arg1, "idt") == 0)
388  DumpIDT();
389  else if (strcmp(Args.arg1, "context") == 0)
390  ContextX64Test();
391  else
392  LogFullAscii("%a: '%a' invalid syntax\r\n", __FUNCTION__, Cmd);
393  }
394  else if (strcmp(Args.command, "meminfo") == 0)
395  {
396  CommandMemInfo(&Args);
397  }
398  else if (strcmp(Args.command, "driverinfo") == 0)
399  {
400  CommandDriverInfo(&Args);
401  }
402  else if (strcmp(Args.command, "dumpmem") == 0)
403  {
404  CommandDumpMem(&Args);
405  }
406  else if (strcmp(Args.command, "function") == 0)
407  {
408  CommandFunction(&Args);
409  }
410  else if (strcmp(Args.command, "eventinfo") == 0)
411  {
412  CommandEventInfo(&Args);
413  }
414  else if (strcmp(Args.command, "help") == 0)
415  {
416  CommandHelp();
417  }
418  else
419  {
420  LogFullAscii("%a: unknown command\r\n", __FUNCTION__);
421 
422  }
423 
424 
425  return EFI_SUCCESS;
426 }
427 
434 {
435  UCHAR Key;
436  CHAR8 Cmd[512];
437  int Index;
438 
439  //gEfiScannerPolicy.OpUseComPort = FALSE;
440 
441  Index = -1;
442  MemZero(&Cmd, sizeof(Cmd));
443 
444 
445  CommandHelp();
446 
447  while (1)
448  {
449  Key = ComGetChar();
450  ComPutChar(Key);
451 
452  if (Key == KEY_ESCAPE)
453  break;
454  else if (Key == KEY_BACKSPACE)
455  {
456  if (Index >= 0)
457  Cmd[Index--] = 0;
458  continue;
459  }
460  else if (Key == KEY_ENTER)
461  {
462  AsciiPrint("Got command: %a\r\n", Cmd);
463 
464  // add the 'space'
465  Cmd[++Index] = ' ';
466  ProcessCommand(Cmd);
467 
468  MemZero(&Cmd, sizeof(Cmd));
469  Index = -1;
470 
471  LogFullAscii("console ready\r\n");
472  continue;
473  }
474 
475  Index++;
476  if (Index >= (sizeof(Cmd) - 1))
477  {
478  Index = 0;
479  MemZero(&Cmd, sizeof(Cmd));
480  }
481  Cmd[Index] = Key;
482 
483  //AsciiPrint("read: %x", SingleChar);
484 
485  gBS->Stall(10);
486  }
487 
488 
489 
490  return EFI_SUCCESS;
491 }