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

Functions for gathering and parsing EFI EVENTS. More...

Include dependency graph for Events.c:

Go to the source code of this file.

Macros

#define MAX_EVENT_TABLE_SIZE   1024
 
#define MAX_EVENT_SCAN_SIZE   0x1000
 

Functions

VOID GetEventTypeStringAscii (CHAR8 *EventTypeBuff, UINTN EventTypeBuffSize, UINT32 EventType)
 
EFI_STATUS GetgEventSignalQueueMacMini (VOID)
 
EFI_STATUS BruteFind_gEventSignalQueueAddr2 (VOID)
 
EFI_STATUS BruteFind_gEventSignalQueueAddr (VOID)
 
BOOLEAN IsEventSignalQueueAddrResolved (VOID)
 
event_entry_list * GetAllEvents (VOID)
 
event_entry * FindEventByAddr (event_entry_list *EventList, C_PTR Address)
 
VOID FreeEvents (event_entry_list *EventList)
 
EFI_STATUS DumpAllEvents (event_entry_list *EventList)
 

Variables

C_PTR gEventSignalQueueAddr = 0
 

Detailed Description

Functions for gathering and parsing EFI EVENTS.

Author
Piotr Bania
Date
Feb 2013 Functions for gathering and parsing EFI EVENTS

Definition in file Events.c.

Macro Definition Documentation

#define MAX_EVENT_SCAN_SIZE   0x1000

Definition at line 15 of file Events.c.

Referenced by GetgEventSignalQueueMacMini().

#define MAX_EVENT_TABLE_SIZE   1024

Definition at line 14 of file Events.c.

Function Documentation

EFI_STATUS BruteFind_gEventSignalQueueAddr ( VOID  )

Function tries to find gEventSignalQueueAddr using signature approach. This version works by scanning the .data section of the original EFI firmware driver and looking for specified set of bytes (signature). Function is working with both x86 and x64 firmware.

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 214 of file Events.c.

References FindImageByAddr(), FreeImages(), GetAllImagesWithDescription(), GetPEHeaders(), gEventSignalQueueAddr, _image_entry::ImageBase, LogFullAscii(), EFI_IMAGE_SECTION_HEADER::Name, EFI_IMAGE_SECTION_HEADER::SizeOfRawData, and EFI_IMAGE_SECTION_HEADER::VirtualAddress.

Referenced by IsEventSignalQueueAddrResolved().

215 {
216  UINTN i;
218  EFI_IMAGE_NT_HEADERS32 *pPE32;
220  BOOLEAN Found;
221  C_PTR *p;
222 
223 
224  image_entry_list *ImageList;
225  image_entry *ImageEntry;
226 
227  ImageList = GetAllImagesWithDescription(NULL, FALSE);
228  if (!ImageList)
229  return EFI_NOT_FOUND;
230 
231  // find the image with CreateEventEx
232  ImageEntry = FindImageByAddr(ImageList, (C_PTR)gBS->CreateEventEx);
233 
234  if (!ImageEntry)
235  {
236 #if DEBUG_ON == 1
237  LogFullAscii("%a: unable to locate image with CreateEventEx\r\n",
238  __FUNCTION__);
239 #endif
240  return EFI_NOT_FOUND;
241  }
242 
243 #if DEBUG_ON == 1
244  LogFullAscii("%a: Image=%016lx with CreateEventEx=%016lx\r\n",
245  __FUNCTION__,
246  (MAX_PTR)ImageEntry->ImageBase,
247  (MAX_PTR)gBS->CreateEventEx);
248 #endif
249 
250  if (!GetPEHeaders((C_PTR)ImageEntry->ImageBase, &pMZ, &pPE32, &pSH))
251  {
252  FreeImages(ImageList);
253  return EFI_NOT_FOUND;
254  }
255 
256 
257  // the .data section should be within first 3 sections
258  Found = FALSE;
259  for (i = 0; i < 3; i++, pSH++)
260  {
261  if ((pSH->Name[0] == '.') && (pSH->Name[1] == 'd') &&
262  (pSH->Name[2] == 'a') && (pSH->Name[3] == 't') &&
263  (pSH->Name[4] == 'a'))
264  {
265  Found = TRUE;
266  break;
267  }
268  }
269 
270  if (!Found)
271  {
272 #if DEBUG_ON == 1
273  LogFullAscii("%a: unable to locate data section\r\n",
274  __FUNCTION__);
275 #endif
276  FreeImages(ImageList);
277  return EFI_NOT_FOUND;
278  }
279 
280 
281  // scan entire data section
282  p = (C_PTR*)((C_PTR)pSH->VirtualAddress + (C_PTR)ImageEntry->ImageBase);
283 
284 
285 #if DEBUG_ON == 1
286  LogFullAscii("%a: scanning %016lx\r\n", __FUNCTION__, (MAX_PTR)p);
287 #endif
288 
289  for (i = 0; i < (pSH->SizeOfRawData-(sizeof(C_PTR)*3)); i++, p = (C_PTR*)((UCHAR*)p + 1))
290  {
291 
292 
293 #if defined(_WIN64)
294  // on x64 we need to find
295  //.data:00000000CE3BE548 qword_CE3BE548 dq 1Fh
296  //.data:00000000CE3BE548
297  //.data:00000000CE3BE550 dq 8 --> can be 0x10
298  //.data:00000000CE3BE558 dq 0
299  //.data:00000000CE3BE560 gEventSignalQueue dq 0A1EAD28h
300  // or
301  //.data:000000007FFEBC00 qword_7FFEBC00 dq 1Fh
302  //.data:000000007FFEBC00
303  //.data:000000007FFEBC08 dq 10h
304  //.data:000000007FFEBC10 dq 1
305  //.data:000000007FFEBC18 dq 0
306  //.data:000000007FFEBC20 gEventSignalQueu dq 7FD5C028h ; DATA XREF: CreateEventEx+1ABo
307 
308 
309  if (p[0] == 0x1F)
310  {
311  if ((p[1] == 0x8) && (p[2] == 0x0))
312  gEventSignalQueueAddr = (C_PTR)&p[3];
313  else if ((p[1] == 0x10) && p[2] == 0x1)
314  gEventSignalQueueAddr = (C_PTR)&p[4]; // vmware way
315  else
316  {
317 #if DEBUG_ON == 1
318  LogFullAscii("%a: Skipping %016lx because p[0]=%016lx p[1]=016llx p[2]=016llx\r\n",
319  __FUNCTION__, p, p[0], p[1], p[2]);
320 #endif
321  continue;
322  }
323 
324 #if DEBUG_ON == 1
325  LogFullAscii("%a: found gEventSignalQueueAddr at %016lx\r\n",
326  __FUNCTION__, gEventSignalQueueAddr);
327 #endif
328 
329  FreeImages(ImageList);
330  return EFI_SUCCESS;
331 
332  }
333 
334 
335 #else
336  // on x86 we need to test for:
337  //.data:7FFED374 dword_7FFED374 dd 1Fh
338  //.data:7FFED374
339  //.data:7FFED378 dd 10h ----> not const!!!
340  //.data:7FFED37C dd 1
341  //.data:7FFED380 gEventSignalQueueAddr dd 7DC0ED1Ch
342  //
343  // on virtual box it looks bit different:
344  //.data:3FD52C3C dword_3FD52C3C dd 1Fh
345  //.data:3FD52C40 dd 10h
346  //.data:3FD52C44 dd 1
347  //.data:3FD52C48 dword_3FD52C48 dd 0
348  //.data:3FD52C4C gEventSignalQueueAddr dd 3EC8681Ch
349 
350 
351 
352 
353  if (p[0] == 0x1F)// && (p[2] == 0x1) && (p[3] != 0))
354  {
355  if ((p[2] == 0x1) && (p[3] != 0))
356  gEventSignalQueueAddr = (C_PTR)&p[3];
357  else if ((p[1] == 0x10) && (p[2] == 0x1))
358  gEventSignalQueueAddr = (C_PTR)&p[4]; // virtualbox 32bit
359  else
360  {
361 #if DEBUG_ON == 1
362  LogFullAscii("%a: Skipping %p because p[0]=%p p[1]=%p p[2]=%p\r\n",
363  __FUNCTION__, p, p[0], p[1], p[2]);
364 #endif
365  continue;
366  }
367 
368 
369 
370 #if DEBUG_ON == 1
371  LogFullAscii("##################################\r\n");
372  LogFullAscii("%a: found gEventSignalQueueAddr at %08x\r\n",
373  __FUNCTION__, (C_PTR)&p[3]);
374 #endif
375 
376  FreeImages(ImageList);
377  return EFI_SUCCESS;
378 
379  }
380 #endif
381 
382  }
383 
384 
385  FreeImages(ImageList);
386  return EFI_NOT_FOUND;
387 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS BruteFind_gEventSignalQueueAddr2 ( VOID  )

Function tries to find gEventSignalQueueAddr using signature approach. This version of the function was done especially to handle Asrock mobo. But it also works with vmware and x64 firmware. Use it only on x64 firmware. (just an alternative method)

asrock x64 firmware: .text:00000000CE393EE0 mov rdx, [rsp+38h+arg_28] .text:00000000CE393EE5 lea rcx, gEventSignalQueue .text:00000000CE393EEC add rdx, 10h .text:00000000CE393EF0 call sub_CE3B3D24

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 136 of file Events.c.

References gEventSignalQueueAddr, and LogFullAscii().

Referenced by IsEventSignalQueueAddrResolved().

137 {
138 
139  // only for x64
140 #if defined(_WIN64)
141  UINTN i;
142  UCHAR *p;
143  UINT64 temp, part_addr, temp_val1, temp_val2;
144 
145 #define BRUTE_EVENT_SEARCH_SIZE (0xCE393F2D - 0xCE393CD8)
146 
147  temp = (UINT64)gBS->CreateEventEx;
148  part_addr = (UINT64)gBS->CreateEventEx >> 28;
149  p = (UCHAR*)temp;
150 
151  for (i = 0; i < BRUTE_EVENT_SEARCH_SIZE; i++, p++)
152  {
153  // is it lea instruction? (48 8D)
154  if ((p[0] != 0x48) || (p[1] != 0x8D))
155  continue;
156 
157 
158  temp = (UINT64)(*(UINT32*)(p + 3) + (UINT64)p + 7);
159 
160  // first check, make sure the address is "in range"
161  // of the current module
162  // and make sure there are at least 8 zero bytes before it
163  if ((part_addr != (temp >> 28)) || (*(UINT64*)(temp-8) != 0))
164  {
165  //PRINT_FUNC(L"Invalid prospect!\r\n");
166  continue;
167  }
168 
169  // next test
170  // *temp -> must have the higher 32bits equal to zero + value must be > 16bit max value
171  // *(temp+8) -> must have the higher 32bits equal to zero
172 
173  temp_val1 = *(UINT64*)temp;
174  temp_val2 = *(UINT64*)(temp + 8);
175 
176  //PRINT_FUNC(L"temp_val1=%016lx temp_val2=%016lx\r\n", temp_val1, temp_val2);
177 
178  if (((temp_val1 >> 32) || (temp_val2 >> 32)) || !(temp_val1 & 0xFFFF0000))
179  {
180  //PRINT_FUNC(L"Invalid prospect2!\r\n");
181  continue;
182  }
183 
184 
185  gEventSignalQueueAddr = temp;
186 
187 #if DEBUG_ON == 1
188  LogFullAscii("%a: BRUTE gEventSignalQueueAddr = %016lx\r\n",
189  __FUNCTION__, gEventSignalQueueAddr);
190 #endif
191 
192  return EFI_SUCCESS;
193 
194  }
195 #endif
196 
197  return EFI_NOT_FOUND;
198 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS DumpAllEvents ( event_entry_list *  EventList)

Function enumarates and dumps all events found in the event list. The output is shown on the screen, written to the log file or serial port depending on the current library policy.

Parameters
EventListPointer to the event list gathered from GetAllEvents function
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 584 of file Events.c.

References EVENT_TYPE_BUFF_SIZE, _event_entry_list::EventEntry, _image_entry::FilePath, FindImageByAddr(), FreeImages(), GetAllImagesWithDescription(), GetEventTypeStringAscii(), _image_entry::ImageBase, _image_entry::ImageSize, LogFullAscii(), _image_entry::Name, IEVENT::NotifyFunction, _event_entry_list::NumOfEntries, and IEVENT::Type.

Referenced by CommandEventInfo(), DrawBoxAndMenu(), and TestDumpHandles().

585 {
586  UINTN i;
587  event_entry *Event;
588 
589  image_entry_list *ImageList;
590  image_entry *ImageEntry;
591 
592  CHAR8 EventTypeBuff[EVENT_TYPE_BUFF_SIZE];
593 
594  if (!EventList)
595  return EFI_NOT_FOUND;
596 
597  ImageList = GetAllImagesWithDescription(NULL, FALSE);
598  if (!ImageList)
599  return EFI_NOT_FOUND;
600 
601 
602  for (i = 0; i < EventList->NumOfEntries; i++)
603  {
604  Event = (event_entry*)&EventList->EventEntry[i];
605  ImageEntry = FindImageByAddr(ImageList, (C_PTR)Event->NotifyFunction);
606 
607  GetEventTypeStringAscii(EventTypeBuff, sizeof(EventTypeBuff), Event->Type);
608 
609 
610  if (ImageEntry)
611  {
612 
613  LogFullAscii("[%08d] EventFunc = %016lx InImage: %016lx-%016lx Type: %08x - %a ImageName: %a ImagePath: %a\r\n",
614  i,
615  (MAX_PTR)Event->NotifyFunction,
616  (MAX_PTR)ImageEntry->ImageBase,
617  (MAX_PTR)((C_PTR)ImageEntry->ImageBase + ImageEntry->ImageSize),
618  Event->Type,
619  EventTypeBuff,
620  ImageEntry->Name,
621  ImageEntry->FilePath);
622  }
623  else
624  {
625 
626  LogFullAscii("[%08d] EventFunc = %016lx InImage: NO-IMAGE-FOUND Type: %08x - %a\r\n",
627  i,
628  (MAX_PTR)Event->NotifyFunction,
629  Event->Type,
630  EventTypeBuff);
631  }
632 
633  }
634 
635  FreeImages(ImageList);
636  return EFI_SUCCESS;
637 }

Here is the call graph for this function:

Here is the caller graph for this function:

event_entry* FindEventByAddr ( event_entry_list *  EventList,
C_PTR  Address 
)

Function finds event entry in the events list by address of the notify function.

Parameters
EventListPointer to the event list gathered from GetAllEvents function
AddressAddress of wanted notify function
Return values
PointerOn success function returns pointer to the located event_entry
NULLOn error

Definition at line 531 of file Events.c.

References _event_entry_list::EventEntry, IEVENT::NotifyFunction, and _event_entry_list::NumOfEntries.

Referenced by CommandEventInfo(), and NotifyEventScannerTimer().

532 {
533 
534  UINTN i;
535  event_entry *EventEntry;
536 
537  if (!EventList)
538  return 0;
539 
540  for (i = 0; i < EventList->NumOfEntries; i++)
541  {
542  EventEntry = &EventList->EventEntry[i];
543 
544  if ((C_PTR)EventEntry->NotifyFunction == Address)
545  return EventEntry;
546  }
547 
548  return 0;
549 }

Here is the caller graph for this function:

VOID FreeEvents ( event_entry_list *  EventList)

Function frees the event list

Parameters
EventListPointer to the event list gathered from GetAllEvents function
Return values
NONE

Definition at line 563 of file Events.c.

References _event_entry_list::EventEntry, and MemFree().

Referenced by CommandEventInfo(), DrawBoxAndMenu(), and UnInstallEventScanner().

564 {
565  MemFree(EventList->EventEntry);
566  MemFree(EventList);
567 }

Here is the call graph for this function:

Here is the caller graph for this function:

event_entry_list* GetAllEvents ( VOID  )

Function is responsible for gathering all events from the queue. Unfortunatelly it may be vulnerable to the race condition since we haven't takedovered the original spinlock. In other words we are forced to use the gEventSignalQueueAddr without synchronization.

EFI events are constructs that are used to provide communication between EFI modules.

The event_entry_list returned by this function must be freed by the FreeEvents function.

Return values
PointerOn success pointer to event_entry_list
NULLOn error

Definition at line 448 of file Events.c.

References EVENT_SIGNATURE, _event_entry_list::EventEntry, gEventSignalQueueAddr, IsEventSignalQueueAddrResolved(), LogFullAscii(), MemAllocate(), MemCopy(), MemFree(), _event_entry_list::NumOfEntries, and IEVENT::Signature.

Referenced by CommandEventInfo(), DrawBoxAndMenu(), InstallEventScanner(), and TestDumpHandles().

449 {
450  event_entry_list *EventList;
451  event_entry *EventEntry;
452 
453  IEVENT *Event;
454  LIST_ENTRY *Head;
455  LIST_ENTRY *Link;
456 
457 
458  UINTN i, NumOfEvents;
459 
461  return 0;
462 
463 
464  // firstly calculate the number of IEVENTs structures
465  NumOfEvents = 0;
466  Head = (LIST_ENTRY*)gEventSignalQueueAddr;
467 
468  // check the event signature
469  Link = Head->ForwardLink;
470  Event = CR(Link, IEVENT, SignalLink, EVENT_SIGNATURE);
471 
472  if (Event->Signature != EVENT_SIGNATURE)
473  {
474 #if DEBUG_ON == 1
475  LogFullAscii("%a: invalid Event signature %08x -> %08x\r\n",
476  __FUNCTION__, Event->Signature, EVENT_SIGNATURE);
477 #endif
478  return 0;
479  }
480 
481  for (Link = Head->ForwardLink; Link != Head; Link = Link->ForwardLink)
482  NumOfEvents++;
483 
484 
485  //LogFullAscii("GetAllEvents: Event signature %08x -> %08x | NumOfEvents=%d \r\n",
486  // Event->Signature, EVENT_SIGNATURE, NumOfEvents);
487 
488 
489  // allocate memory
490  EventList = (event_entry_list*)MemAllocate(sizeof(event_entry_list));
491  if (!EventList)
492  return 0;
493 
494  EventEntry = (event_entry*)MemAllocate((NumOfEvents+1)*sizeof(event_entry));
495  if (!EventEntry)
496  {
497  MemFree(EventList);
498  return 0;
499  }
500 
501  for (i = 0, Link = Head->ForwardLink; ((Link != Head) && (i < NumOfEvents));
502  Link = Link->ForwardLink, i++)
503  {
504  Event = CR(Link, IEVENT, SignalLink, EVENT_SIGNATURE);
505 
506  // copy each event structure (ievent == event_entry)
507  MemCopy(&EventEntry[i], Event, sizeof(IEVENT));
508  }
509 
510  EventList->NumOfEntries = NumOfEvents;
511  EventList->EventEntry = EventEntry;
512 
513 
514  return EventList;
515 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID GetEventTypeStringAscii ( CHAR8 *  EventTypeBuff,
UINTN  EventTypeBuffSize,
UINT32  EventType 
)

Function converts EventType to text representation.

Parameters
EventTypeBuffPointer to output buffer
EventTypeBuffSizeSize of the output buffer
EventTypeValue of the selected event type
Return values
NONE

Definition at line 36 of file Events.c.

References isEVENT_TYPE_EXIT_BOOT_SERVICES(), isEVENT_TYPE_NOTIFY_SIGNAL(), isEVENT_TYPE_NOTIFY_WAIT(), isEVENT_TYPE_RUNTIME(), isEVENT_TYPE_TIMER(), and isEVENT_TYPE_VIRTUAL_ADDRESS_CHANGE().

Referenced by CommandEventInfo(), DumpAllEvents(), and NotifyEventScannerTimer().

39 {
40  gBS->SetMem(EventTypeBuff, EventTypeBuffSize, 0);
41 
42  if (isEVENT_TYPE_TIMER(EventType))
43  AsciiSPrint(EventTypeBuff, EventTypeBuffSize-1, " TIMER |");
44  if (isEVENT_TYPE_RUNTIME(EventType))
45  AsciiSPrint(EventTypeBuff, EventTypeBuffSize-1, "%a RUNTIME |", EventTypeBuff);
46  if (isEVENT_TYPE_NOTIFY_WAIT(EventType))
47  AsciiSPrint(EventTypeBuff, EventTypeBuffSize-1, "%a NOTIFY_WAIT |", EventTypeBuff);
48  if (isEVENT_TYPE_NOTIFY_SIGNAL(EventType))
49  AsciiSPrint(EventTypeBuff, EventTypeBuffSize-1, "%a NOTIFY_SIGNAL |", EventTypeBuff);
50  if (isEVENT_TYPE_EXIT_BOOT_SERVICES(EventType))
51  AsciiSPrint(EventTypeBuff, EventTypeBuffSize-1, "%a SIGNAL_EXIT_BOOT_SERVICES |", EventTypeBuff);
53  AsciiSPrint(EventTypeBuff, EventTypeBuffSize-1, "%a SIGNAL_VADDR_CHANGE |", EventTypeBuff);
54 
55  if ((EventTypeBuff[0] == 0) || (EventType == 0))
56  AsciiSPrint(EventTypeBuff, EventTypeBuffSize-1, " UNKNOWN");
57 
58 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS GetgEventSignalQueueMacMini ( VOID  )

Function tries to find gEventSignalQueueAddr using signature approach. This version of the function was done especially to handle Apple Mac Mini firmware.

Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 77 of file Events.c.

References gEventSignalQueueAddr, LogFullAscii(), and MAX_EVENT_SCAN_SIZE.

Referenced by IsEventSignalQueueAddrResolved().

78 {
79  UINTN i;
80  UCHAR *p;
81  C_PTR temp;
82 
83  // we need to do it this way to avoid type cast warnings
84  temp = (C_PTR)gBS->CreateEvent;
85  p = (UCHAR*)(temp - MAX_EVENT_SCAN_SIZE);
86 
87 
88 #if DEBUG_ON == 1
89  LogFullAscii("%a: Scanning from %016lx\r\n", __FUNCTION__, p);
90 #endif
91 
92  for (i = 0; i < (MAX_EVENT_SCAN_SIZE*2); i++, p++)
93  {
94 
95  //48 83 C2 10 48 8D
96  if ((*(UINT32*)p == 0x10C28348) && (*(UINT16*)(p + 4) == 0x8D48))
97  {
98  p += 4;
99  gEventSignalQueueAddr = *(UINT32*)(p + 3) + (UINT64)p + 7;
100 
101 #if DEBUG_ON == 1
102  LogFullAscii("%a: gEventSignalQueueAddr = %08x \r\n",
103  __FUNCTION__, gEventSignalQueueAddr);
104 #endif
105  return EFI_SUCCESS;
106  }
107  }
108 
109 
110  return EFI_NOT_FOUND;
111 }

Here is the call graph for this function:

Here is the caller graph for this function:

BOOLEAN IsEventSignalQueueAddrResolved ( VOID  )

Function tries to find gEventSignalQueueAddr using different set of functions.

Return values
TRUEOn success (gEventSignalQueueAddr was found)
FALSEOn error (gEventSignalQueueAddr was not found)

Definition at line 401 of file Events.c.

References BruteFind_gEventSignalQueueAddr(), BruteFind_gEventSignalQueueAddr2(), GetgEventSignalQueueMacMini(), gEventSignalQueueAddr, and LogFullAscii().

Referenced by GetAllEvents().

402 {
403  if (gEventSignalQueueAddr == 0)
404  {
406 
407  if (gEventSignalQueueAddr == 0)
408  {
409  if (EFI_ERROR(GetgEventSignalQueueMacMini()))
410  {
411  if (EFI_ERROR(BruteFind_gEventSignalQueueAddr2()))
412  {
413 
414 #if DEBUG_ON == 1
415  LogFullAscii("%a: Error unable to locate gEventSignalQueue\r\n",
416  __FUNCTION__);
417 #endif
418  return FALSE;
419  }
420  }
421  }
422  }
423 
424  return TRUE;
425 }

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation