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

Functions for gathering, parsing and finding handles and images. More...

Include dependency graph for Images.c:

Go to the source code of this file.

Macros

#define check_flag(flag, text)   if (HandleList->HandleEntry[i].Flags & flag) PRINT_FUNC_ASCII(text); WriteLogFile(text)
 
#define check_modification(val1, val2, text)
 

Functions

VOID GetHandleTypeStringAscii (CHAR8 *HandleTypeBuff, UINTN HandleTypeBuffSize, UINT16 Flags)
 
CHAR16 *EFIAPI GetDevicePathTextForHandle (EFI_DEVICE_PATH_PROTOCOL *DevicePath)
 
EFI_STATUS CloseLoadedImageProtocol (EFI_HANDLE Handle)
 
EFI_STATUS GetLoadedImageProtocol (EFI_HANDLE Handle, EFI_LOADED_IMAGE_PROTOCOL **OutLoadedImage)
 
EFI_STATUS GetAllHandles (EFI_HANDLE **OutHandles)
 
CHAR16 *EFIAPI GetStringFromHandle2 (EFI_HANDLE Handle, CHAR8 *Language)
 
CHAR16 *EFIAPI GetStringFromHandle (EFI_HANDLE Handle, CHAR8 *Language)
 
VOID FreeHandles (handle_entry_list *HandleList)
 
handle_entry_list * GetAllHandlesWithDescription (VOID)
 
EFI_STATUS DumpAllHandles (handle_entry_list *HandleList)
 
VOID GetImagePathAndName (image_entry *ImageEntry, EFI_HANDLE Handle, EFI_LOADED_IMAGE_PROTOCOL *LoadedImageProt)
 
image_entry * AddImageToList (image_entry_list *ImageList, EFI_HANDLE Handle, EFI_LOADED_IMAGE_PROTOCOL *LoadedImageProt, BOOLEAN CalculateCrc)
 
image_entry_list * GetAllImagesWithDescription (handle_entry_list *HandleList, BOOLEAN CalculateCrc)
 
image_entry * FindImageByAddr (image_entry_list *ImageList, C_PTR Address)
 
image_entry * FindImageByHandle (image_entry_list *ImageList, EFI_HANDLE Handle)
 
EFI_STATUS DumpImageToDisk (image_entry *ImageEntry)
 
EFI_STATUS DumpAllImages (image_entry_list *ImageList, BOOLEAN DumpToDisk)
 
EFI_STATUS CheckImagesFunctionPointers (image_entry_list *ImageList)
 
VOID FreeImages (image_entry_list *ImageList)
 
VOID MyNewImageNotify (image_entry_list *OrgImageList, image_entry *NewImage)
 
VOID MyModImageNotify (image_entry_list *OrgImageList, image_entry *NewImage)
 
VOID MyNewEventCallback (event_entry_list *OrgEventList, event_entry *NewEvent)
 
VOID TestDumpHandles (VOID)
 

Variables

EFI_DEVICE_PATH_TO_TEXT_PROTOCOL * gDevPathToText
 

Detailed Description

Functions for gathering, parsing and finding handles and images.

Author
Piotr Bania
Date
Feb 2013 Functions for gathering, parsing and finding handles and images

From the Intel manuals:

  • All protocols have a handle which is associated with the protocol
  • Every device and executable image in UEFI has a handle protocol in the handle database
  • Every boot device must have a device path protocol to describe it

Definition in file Images.c.

Macro Definition Documentation

#define check_flag (   flag,
  text 
)    if (HandleList->HandleEntry[i].Flags & flag) PRINT_FUNC_ASCII(text); WriteLogFile(text)
#define check_modification (   val1,
  val2,
  text 
)
Value:
if (val1 != val2) { \
LogFullAscii("Index=%08x Found modification of %a %016lx -> %016lx\r\n", i, text, (MAX_PTR)val1, (MAX_PTR)val2); \
MCounter++; }

Referenced by CheckImagesFunctionPointers().

Function Documentation

image_entry* AddImageToList ( image_entry_list *  ImageList,
EFI_HANDLE  Handle,
EFI_LOADED_IMAGE_PROTOCOL *  LoadedImageProt,
BOOLEAN  CalculateCrc 
)

Function creates the new ImageEntry list (based on the old one) it also adds one new image entry to it.

Parameters
ImageListPointer to selected ImageEntry
HandleImage handle
LoadedImageProtPointer to openned LOADED_IMAGE_PROTOCOL
CalculateCrcIndicates whether function should calculate the crc32 value of the image code.
Return values
image_entry*On success a pointer to newly created image_entry is returned
NULLOn error

Definition at line 642 of file Images.c.

References _image_entry::Crc32, GetImagePathAndName(), GetPECodeChecksum(), _image_entry::Handle, _image_entry::ImageBase, _image_entry_list::ImageEntry, _image_entry::ImageSize, _image_entry::LoadedImageProtocol, MemAllocate(), MemCopy(), MemFree(), and _image_entry_list::NumOfEntries.

Referenced by hooked_LoadImage().

646 {
647  image_entry *ImageEntryOld, *ImageEntryNew, *ImageEntry;
648 
649 
650  if (!ImageList)
651  return 0;
652 
653  ImageEntryOld = ImageList->ImageEntry;
654  ImageEntryNew = (image_entry*)MemAllocate((ImageList->NumOfEntries + 1) * sizeof(image_entry));
655 
656  if (!ImageEntryNew)
657  return 0;
658 
659  // copy everything from the old list
660  MemCopy(ImageEntryNew, ImageEntryOld, ImageList->NumOfEntries*sizeof(image_entry));
661 
662  // add new entry
663  ImageEntry = (image_entry*)&ImageEntryNew[ImageList->NumOfEntries];
664 
665  ImageEntry->Handle = Handle;
666  ImageEntry->LoadedImageProtocol = LoadedImageProt;
667  ImageEntry->ImageBase = LoadedImageProt->ImageBase;
668  ImageEntry->ImageSize = LoadedImageProt->ImageSize;
669 
670  if (CalculateCrc)
671  ImageEntry->Crc32 = GetPECodeChecksum((C_PTR)LoadedImageProt->ImageBase);
672 
673  GetImagePathAndName(ImageEntry, Handle, LoadedImageProt);
674 
675  // free the old list and link the new one
676  ImageList->ImageEntry = ImageEntryNew;
677  ImageList->NumOfEntries++;
678 
679  MemFree(ImageEntryOld);
680  return ImageEntry;
681 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS CheckImagesFunctionPointers ( image_entry_list *  ImageList)

Function enumerates the entire image list and it checks whether pointers to RuntimeServices, BootServices and SystemTables are different across different images. Everything is reported to the screen, log file or serial port depending on the framework policy.

Parameters
ImageListPointer to image list
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 961 of file Images.c.

References check_modification, _image_entry::Crc32, _image_entry::FilePath, _image_entry::Handle, _image_entry::ImageBase, _image_entry_list::ImageEntry, _image_entry::ImageSize, _image_entry::LoadedImageProtocol, LogFullAscii(), _image_entry::Name, and _image_entry_list::NumOfEntries.

Referenced by DrawBoxAndMenu(), and TestDumpHandles().

962 {
963  UINTN i, MCounter;
964  image_entry *ImageEntry;
965  C_PTR LinkBootServices, OldLinkBootServices;
966  C_PTR LinkRuntimeServices, OldLinkRuntimeServices;
967  C_PTR LinkSystemTable, OldLinkSystemTable;
968 
969  if (!ImageList)
970  return EFI_NOT_FOUND;
971 
972 
973  OldLinkSystemTable = 0;
974  OldLinkBootServices = 0;
975  OldLinkRuntimeServices = 0;
976 
977  MCounter = 0;
978 
979 
980  LogFullAscii("--- CHECKING IMAGES ---\r\n");
981 
982 
983  for (i = 0; i < ImageList->NumOfEntries; i++)
984  {
985  ImageEntry = &ImageList->ImageEntry[i];
986  LinkSystemTable = (C_PTR)ImageEntry->LoadedImageProtocol->SystemTable;
987  LinkBootServices = (C_PTR)ImageEntry->LoadedImageProtocol->SystemTable->BootServices;
988  LinkRuntimeServices = (C_PTR)ImageEntry->LoadedImageProtocol->SystemTable->RuntimeServices;
989 
990 
991 
992  LogFullAscii("Index=%08x Handle=%08x ImageBase=%016lx ImageSize=%08llx Crc32=%08x Name=%a FilePath=%a\r\n",
993  i,
994  ImageEntry->Handle,
995  (MAX_PTR)ImageEntry->ImageBase,
996  ImageEntry->ImageSize,
997  ImageEntry->Crc32,
998  ImageEntry->Name,
999  ImageEntry->FilePath);
1000 
1001  LogFullAscii("Index=%08x SystemTable=%016lx BootServices=%016lx RuntimeServices=%016lx\r\n",
1002  i,
1003  (MAX_PTR)LinkSystemTable,
1004  (MAX_PTR)LinkBootServices,
1005  (MAX_PTR)LinkRuntimeServices);
1006 
1007 
1008 #define check_modification(val1, val2, text) if (val1 != val2) { \
1009  LogFullAscii("Index=%08x Found modification of %a %016lx -> %016lx\r\n", i, text, (MAX_PTR)val1, (MAX_PTR)val2); \
1010  MCounter++; }
1011 
1012 
1013  // first pass? nope so apply the checks
1014  if (OldLinkSystemTable)
1015  {
1016  check_modification(OldLinkSystemTable, LinkSystemTable, "SystemTable");
1017  check_modification(OldLinkBootServices, LinkBootServices, "BootServices");
1018  check_modification(OldLinkRuntimeServices, LinkRuntimeServices, "RuntimeServices");
1019  }
1020 
1021 
1022 #undef check_modification
1023 
1024  OldLinkSystemTable = LinkSystemTable;
1025  OldLinkBootServices = LinkBootServices;
1026  OldLinkRuntimeServices = LinkRuntimeServices;
1027 
1028  }
1029 
1030 
1031 
1032 
1033  LogFullAscii("Found %d modifications\r\n", MCounter);
1034  LogFullAscii("--- DONE ---\r\n");
1035 
1036  return EFI_SUCCESS;
1037 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS CloseLoadedImageProtocol ( EFI_HANDLE  Handle)

Function closes the previously opended LoadedImageProtocol for specified handle.

Parameters
HandleSpecified handle
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 124 of file Images.c.

References EFI_STATUS(), and gImageHandle.

Referenced by FreeImages().

125 {
126  EFI_STATUS Status;
127 
128  Status = gBS->CloseProtocol(Handle,
129  &gEfiLoadedImageProtocolGuid,
130  gImageHandle,
131  NULL);
132 
133  return Status;
134 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS DumpAllHandles ( handle_entry_list *  HandleList)

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

Parameters
HandleListPointer to the handle list gathered from GetAllHandlesWithDescription function
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 530 of file Images.c.

References _handle_entry::Flags, GetHandleTypeStringAscii(), _handle_entry::Handle, _handle_entry_list::HandleEntry, LogFullAscii(), and _handle_entry_list::NumOfEntries.

Referenced by DrawBoxAndMenu(), and TestDumpHandles().

531 {
532  UINTN i;
533  CHAR8 HandleTypeAscii[256];
534 
535  if (!HandleList || !HandleList->HandleEntry)
536  return EFI_NOT_FOUND;
537 
538 #define check_flag(flag, text) if (HandleList->HandleEntry[i].Flags & flag) PRINT_FUNC_ASCII(text); WriteLogFile(text)
539 
540 
541  LogFullAscii("--- BEGINING HANDLE DUMP ---\r\n");
542 
543  for (i = 0; i < HandleList->NumOfEntries; i++)
544  {
545  /*
546  LogFullAscii("%08x HandleValue=%08x Flags=%08x ", i + 1, HandleList->HandleEntry[i].Handle, HandleList->HandleEntry[i].Flags);
547  check_flag(HTYPE_IMAGE_HANDLE, "HTYPE_IMAGE_HANDLE |");
548  check_flag(HTYPE_DRIVER_BINDING_HANDLE, "HTYPE_DRIVER_BINDING_HANDLE |");
549  check_flag(HTYPE_COMPONENT_NAME_HANDLE, "HTYPE_COMPONENT_NAME_HANDLE |");
550  check_flag(HTYPE_DEVICE_HANDLE, "HTYPE_DEVICE_HANDLE |");
551  check_flag(HTYPE_DRIVER_CONFIGURATION_HANDLE, "HTYPE_DRIVER_CONFIGURATION_HANDLE |");
552  check_flag(HTYPE_DRIVER_DIAGNOSTICS_HANDLE, "HTYPE_DRIVER_DIAGNOSTICS_HANDLE |");
553  LogFullAscii("\r\n");
554  */
555 
556  GetHandleTypeStringAscii(HandleTypeAscii,
557  sizeof(HandleTypeAscii),
558  HandleList->HandleEntry[i].Flags);
559 
560  LogFullAscii("%08x HandleValue=%08x Flags=%08x %a\r\n",
561  i + 1,
562  HandleList->HandleEntry[i].Handle,
563  HandleList->HandleEntry[i].Flags,
564  HandleTypeAscii);
565 
566  }
567 
568 #undef check_flag
569 
570  LogFullAscii("--- END OF HANDLE DUMP ---\r\n");
571 
572 
573  return EFI_SUCCESS;
574 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS DumpAllImages ( image_entry_list *  ImageList,
BOOLEAN  DumpToDisk 
)

Function dumps all image on the ImageList either just to screen or to disk also (to "dump\ImageBase.dmp")

Parameters
ImageListPointer to image list
DumpToDiskIndicates whether images will be dumped to disk or not
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 906 of file Images.c.

References _image_entry::Crc32, DumpImageToDisk(), _image_entry::FilePath, _image_entry::Handle, _image_entry::ImageBase, _image_entry_list::ImageEntry, _image_entry::ImageSize, LogFullAscii(), _image_entry::Name, and _image_entry_list::NumOfEntries.

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

908 {
909 
910  UINTN i;
911  image_entry *ImageEntry;
912 
913 
914  if (!ImageList)
915  return EFI_NOT_FOUND;
916 
917 
918  LogFullAscii("--- BEGINING IMAGES DUMP ---\r\n");
919 
920 
921  for (i = 0; i < ImageList->NumOfEntries; i++)
922  {
923  ImageEntry = &ImageList->ImageEntry[i];
924 
925 
926  LogFullAscii("Index=%08x Handle=%08x ImageBase=%016lx ImageSize=%08llx Crc32=%08x Name=%a FilePath=%a\r\n",
927  i,
928  ImageEntry->Handle,
929  (MAX_PTR)ImageEntry->ImageBase,
930  ImageEntry->ImageSize,
931  ImageEntry->Crc32,
932  ImageEntry->Name,
933  ImageEntry->FilePath);
934 
935 
936  if (DumpToDisk)
937  DumpImageToDisk(ImageEntry);
938 
939  }
940 
941 
942  LogFullAscii("--- END OF IMAGE DUMP ---\r\n");
943 
944  return EFI_SUCCESS;
945 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS DumpImageToDisk ( image_entry *  ImageEntry)

Function dumps selected image to disk (to "dump\ImageBase.dmp")

Parameters
ImageEntryPointer to the image entry which will be dumped
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 869 of file Images.c.

References DumpMemoryToFile(), _image_entry::ImageBase, _image_entry::ImageSize, and LogFullAscii().

Referenced by DumpAllImages().

870 {
871  CHAR16 DmpFilePath[512];
872 
873  //
874  // warning: do not exceed the filename length (openfile will return error)
875  //
876 
877  UnicodeSPrint(DmpFilePath, sizeof(DmpFilePath)-1, L"dump\\%p.dmp",
878  ImageEntry->ImageBase);
879 
880 
881 #if DEBUG_ON == 1
882  LogFullAscii("%a: Dumping to %s (size=%08x)\r\n",
883  __FUNCTION__,
884  DmpFilePath,
885  ImageEntry->ImageSize);
886 #endif
887 
888  return DumpMemoryToFile((C_PTR)ImageEntry->ImageBase,
889  (UINTN)ImageEntry->ImageSize,
890  DmpFilePath,
891  TRUE);
892 }

Here is the call graph for this function:

Here is the caller graph for this function:

image_entry* FindImageByAddr ( image_entry_list *  ImageList,
C_PTR  Address 
)

Function finds image entry in the image list by address. Function checks if the selected address points somewhere inside the image body.

Parameters
ImageListPointer to the image list gathered from GetAllImagesWithDescription function
AddressAddress of wanted image (doesn't have to be exact image base)
Return values
PointerOn success function returns pointer to the located image_entry
NULLOn error

Definition at line 804 of file Images.c.

References _image_entry::ImageBase, _image_entry_list::ImageEntry, _image_entry::ImageSize, and _image_entry_list::NumOfEntries.

Referenced by BruteFind_gEventSignalQueueAddr(), CommandDriverInfo(), DumpAllEvents(), DumpEFITables(), and ScanMemoryForExecutables().

806 {
807  UINTN i;
808  image_entry *ImageEntry;
809 
810  if (!ImageList)
811  return 0;
812 
813  for (i = 0; i < ImageList->NumOfEntries; i++)
814  {
815  ImageEntry = &ImageList->ImageEntry[i];
816 
817  if ((Address >= (C_PTR)ImageEntry->ImageBase) &&
818  (Address < ((C_PTR)ImageEntry->ImageBase + ImageEntry->ImageSize)))
819  return ImageEntry;
820  }
821 
822  return 0;
823 }

Here is the caller graph for this function:

image_entry* FindImageByHandle ( image_entry_list *  ImageList,
EFI_HANDLE  Handle 
)

Function finds image entry in the image list by handle value

Parameters
ImageListPointer to the image list gathered from GetAllImagesWithDescription function
HandleValue of wanted handle
Return values
PointerOn success function returns pointer to the located image_entry
NULLOn error

Definition at line 838 of file Images.c.

References _image_entry::Handle, _image_entry_list::ImageEntry, and _image_entry_list::NumOfEntries.

Referenced by NotifyExitBootServices().

840 {
841  UINTN i;
842  image_entry *ImageEntry;
843 
844  if (!ImageList)
845  return 0;
846 
847  for (i = 0; i < ImageList->NumOfEntries; i++)
848  {
849  ImageEntry = &ImageList->ImageEntry[i];
850 
851  if (ImageEntry->Handle == Handle)
852  return ImageEntry;
853  }
854 
855  return 0;
856 }

Here is the caller graph for this function:

VOID FreeHandles ( handle_entry_list *  HandleList)

Function frees the handle list

Parameters
HandleListPointer to the handle list gathered from GetAllHandlesWithDescription function
Return values
NONE

Definition at line 400 of file Images.c.

References _handle_entry_list::HandleEntry, and MemFree().

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

401 {
402  MemFree(HandleList->HandleEntry);
403  MemFree(HandleList);
404 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID FreeImages ( image_entry_list *  ImageList)

Function frees the image list

Parameters
ImageListPointer to the image list gathered from GetAllImagesWithDescription function
Return values
NONE

Definition at line 1051 of file Images.c.

References CloseLoadedImageProtocol(), _image_entry::Handle, _image_entry_list::ImageEntry, _image_entry::LoadedImageProtocol, MemFree(), and _image_entry_list::NumOfEntries.

Referenced by BruteFind_gEventSignalQueueAddr(), CommandDriverInfo(), DrawBoxAndMenu(), DumpAllEvents(), InstallSniffImage(), NotifyExitBootServices(), ScanMemoryForExecutables(), TestDumpHandles(), and UnInstallSniffImage().

1052 {
1053  UINTN i;
1054  image_entry *ImageEntry;
1055 
1056  for (i = 0; i < ImageList->NumOfEntries; i++)
1057  {
1058  ImageEntry = &ImageList->ImageEntry[i];
1059 
1060  if (ImageEntry->LoadedImageProtocol)
1061  CloseLoadedImageProtocol(ImageEntry->Handle);
1062  }
1063 
1064  MemFree(ImageList->ImageEntry);
1065  MemFree(ImageList);
1066 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS GetAllHandles ( EFI_HANDLE **  OutHandles)

Function gets all system handles and writes them to the allocated memory. Pointer to this allocated memory with handles list is returned in OutHandles variable. (last entry is null)

Parameters
OutHandlesReturned pointer to system handles list
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 195 of file Images.c.

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

Referenced by GetAllHandlesWithDescription().

196 {
197  EFI_STATUS Status;
198  UINTN Size;
199 
200  EFI_HANDLE *Handles;
201 
202  Size = 0;
203  Handles = NULL;
204  Status = gBS->LocateHandle(AllHandles, NULL, NULL, &Size, Handles);
205 
206 
207  if (Status == EFI_BUFFER_TOO_SMALL)
208  {
209  Handles = MemAllocate(Size + sizeof(EFI_HANDLE));
210  if (!Handles)
211  return EFI_NOT_FOUND;
212 
213  // try one more time
214  Status = gBS->LocateHandle(AllHandles, NULL, NULL, &Size, Handles);
215  if (EFI_ERROR(Status))
216  {
217 #if DEBUG_ON == 1
218  LogFullAscii("%a: error LocateHandle() failed, status = %08x \r\n",
219  __FUNCTION__, Status);
220 #endif
221 
222  MemFree(Handles);
223  return EFI_NOT_FOUND;
224  }
225 
226 
227  Handles[Size / sizeof(EFI_HANDLE)] = NULL;
228  *OutHandles = Handles;
229  return Status;
230  }
231  else if (EFI_ERROR(Status))
232  {
233 #if DEBUG_ON == 1
234  LogFullAscii("%a: unable to get all handles, status = %08x \r\n",
235  __FUNCTION__, Status);
236 #endif
237 
238  return Status;
239  }
240 
241 
242 #if DEBUG_ON == 1
243  LogFullAscii("%a: uknown status = %08x \r\n", __FUNCTION__, Status);
244 #endif
245  return Status;
246 }

Here is the call graph for this function:

Here is the caller graph for this function:

handle_entry_list* GetAllHandlesWithDescription ( VOID  )

Function is responsible for gathering all system handles. Requestor is responsible for deallocating the memory through FreeHandles function.

Return values
PointerOn success pointer to handle_entry_list
NULLOn error

Definition at line 419 of file Images.c.

References EFI_STATUS(), GetAllHandles(), _handle_entry::Handle, _handle_entry_list::HandleEntry, LogFullAscii(), MemAllocate(), MemFree(), _handle_entry_list::NumOfEntries, setHANDLE_TYPE_COMPONENT_NAME_HANDLE(), setHANDLE_TYPE_DEVICE_HANDLE(), setHANDLE_TYPE_DRIVER_BINDING_HANDLE(), setHANDLE_TYPE_DRIVER_CONFIGURATION_HANDLE(), setHANDLE_TYPE_DRIVER_DIAGNOSTICS_HANDLE(), and setHANDLE_TYPE_IMAGE_HANDLE().

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

420 {
421  UINTN i, MemSize;
422  EFI_STATUS Status;
423  EFI_HANDLE *AllHandles;
424 
425  EFI_GUID **ProtocolGuidArray;
426  UINTN ProtoArrayCount;
427  UINTN ProtoIndex;
428 
429  handle_entry_list *HandleList;
430  handle_entry *HandleEntry;
431 
432 
433  Status = GetAllHandles(&AllHandles);
434 
435  if (EFI_ERROR(Status))
436  {
437 #if DEBUG_ON == 1
438  LogFullAscii("%a: calling GetAllHandles() failed\r\n",
439  __FUNCTION__);
440 #endif
441  return 0;
442  }
443 
444 
445  // calculate the needed memory
446  MemSize = sizeof(handle_entry);
447  for (i = 0; AllHandles[i] != NULL; i++)
448  MemSize += sizeof(handle_entry);
449 
450 
451  HandleList = (handle_entry_list*)MemAllocate(sizeof(handle_entry_list));
452  if (!HandleList)
453  {
454  MemFree(AllHandles);
455  return 0;
456  }
457 
458  HandleEntry = (handle_entry*)MemAllocate(MemSize);
459  if (!HandleEntry)
460  {
461  MemFree(HandleList);
462  MemFree(AllHandles);
463  return 0;
464  }
465 
466  HandleList->HandleEntry = HandleEntry;
467  HandleList->NumOfEntries = (MemSize / sizeof(handle_entry)) - 1;
468 
469 
470  // get the handle information (protocols per handle)
471  for (i = 0; AllHandles[i] != NULL; i++)
472  {
473  Status = gBS->ProtocolsPerHandle(AllHandles[i],
474  &ProtocolGuidArray,
475  &ProtoArrayCount);
476 
477  HandleEntry[i].Handle = AllHandles[i];
478 
479  if (EFI_ERROR(Status))
480  continue;
481 
482 
483  // user needs to free the ProtocolGuidArray
484  for (ProtoIndex = 0; ProtoIndex < ProtoArrayCount; ProtoIndex++)
485  {
486 
487  if (CompareGuid(ProtocolGuidArray[ProtoIndex], &gEfiLoadedImageProtocolGuid))
488  setHANDLE_TYPE_IMAGE_HANDLE(&HandleEntry[i].Flags);
489 
490  if (CompareGuid(ProtocolGuidArray[ProtoIndex], &gEfiDriverBindingProtocolGuid))
491  setHANDLE_TYPE_DRIVER_BINDING_HANDLE(&HandleEntry[i].Flags);
492 
493  if (CompareGuid(ProtocolGuidArray[ProtoIndex], &gEfiComponentNameProtocolGuid))
494  setHANDLE_TYPE_COMPONENT_NAME_HANDLE(&HandleEntry[i].Flags);
495 
496  if (CompareGuid(ProtocolGuidArray[ProtoIndex], &gEfiDevicePathProtocolGuid))
497  setHANDLE_TYPE_DEVICE_HANDLE(&HandleEntry[i].Flags);
498 
499  if (CompareGuid(ProtocolGuidArray[ProtoIndex], &gEfiDriverConfigurationProtocolGuid))
500  setHANDLE_TYPE_DRIVER_CONFIGURATION_HANDLE(&HandleEntry[i].Flags);
501 
502  if (CompareGuid(ProtocolGuidArray[ProtoIndex], &gEfiDriverDiagnosticsProtocolGuid))
503  setHANDLE_TYPE_DRIVER_DIAGNOSTICS_HANDLE(&HandleEntry[i].Flags);
504  }
505 
506  MemFree(ProtocolGuidArray);
507  }
508 
509 
510 
511  MemFree(AllHandles);
512  return HandleList;
513 }

Here is the call graph for this function:

Here is the caller graph for this function:

image_entry_list* GetAllImagesWithDescription ( handle_entry_list *  HandleList,
BOOLEAN  CalculateCrc 
)

Function creates the image list based on the system handles. Requestor is responsible for deallocating the memory through FreeImages function.

Parameters
HandleListPointer to handle list (can be null)
CalculateCrcIndicates whether function should calculate the crc32 value of the image code.
Return values
image_entry_list*On success a pointer to created image_entry_list
NULLOn error

Definition at line 701 of file Images.c.

References _image_entry::Crc32, EFI_STATUS(), FreeHandles(), GetAllHandlesWithDescription(), GetImagePathAndName(), GetLoadedImageProtocol(), GetPECodeChecksum(), _handle_entry::Handle, _image_entry::Handle, _handle_entry_list::HandleEntry, _image_entry::ImageBase, _image_entry_list::ImageEntry, _image_entry::ImageSize, _image_entry::LoadedImageProtocol, MAX_IMAGE_TEXTBUFF_LEN, MemAllocate(), MemFree(), _handle_entry_list::NumOfEntries, and _image_entry_list::NumOfEntries.

Referenced by BruteFind_gEventSignalQueueAddr(), CommandDriverInfo(), DrawBoxAndMenu(), DumpAllEvents(), DumpEFITables(), InstallSniffImage(), NotifyExitBootServices(), ScanMemoryForExecutables(), and TestDumpHandles().

703 {
704  UINTN i, j, MemSize;
705  image_entry *ImageEntry;
706  image_entry_list *ImageList;
707  EFI_STATUS Status;
708 
709  BOOLEAN NewHandleList;
710  EFI_LOADED_IMAGE_PROTOCOL *LoadedImageProt;
711 
712 
713 
714  NewHandleList = FALSE;
715 
716  if (!HandleList)
717  {
718  NewHandleList = TRUE;
719  HandleList = GetAllHandlesWithDescription();
720  }
721 
722  if (!HandleList)
723  return 0;
724 
725  MemSize = HandleList->NumOfEntries * sizeof(image_entry);
726  ImageList = (image_entry_list*)MemAllocate(sizeof(image_entry_list));
727 
728  if (!ImageList)
729  {
730  if (NewHandleList)
731  FreeHandles(HandleList);
732  return 0;
733  }
734 
735  // allocate memory for maximum number of entries
736  ImageEntry = (image_entry*)MemAllocate(MemSize);
737  if (!ImageEntry)
738  {
739  MemFree(ImageList);
740  if (NewHandleList)
741  FreeHandles(HandleList);
742  return 0;
743  }
744 
745  ImageList->ImageEntry = ImageEntry;
746  ImageList->NumOfEntries = 0;
747 
748  // parse all handles and try to open the LoadedImageProtocol for it
749  // it looks like a bruteforcing method but there is a possibility
750  // to limit it by checking whether selected handle has
751  // image bit on
752 
753 
754  for (i = 0, j = 0; i < HandleList->NumOfEntries; i++)
755  {
756  Status = GetLoadedImageProtocol(HandleList->HandleEntry[i].Handle, &LoadedImageProt);
757 
758  // not an image or unknown error
759  if (EFI_ERROR(Status) || !LoadedImageProt)
760  continue;
761 
762  ImageEntry[j].Handle = HandleList->HandleEntry[i].Handle;
763  ImageEntry[j].LoadedImageProtocol = LoadedImageProt;
764  ImageEntry[j].ImageBase = LoadedImageProt->ImageBase;
765  ImageEntry[j].ImageSize = LoadedImageProt->ImageSize;
766 
767 
768  if (CalculateCrc)
769  ImageEntry[j].Crc32 = GetPECodeChecksum((C_PTR)LoadedImageProt->ImageBase);
770 
771  AsciiSPrint(ImageEntry[j].FilePath, MAX_IMAGE_TEXTBUFF_LEN, "UNKNOWN");
772  AsciiSPrint(ImageEntry[j].Name, MAX_IMAGE_TEXTBUFF_LEN, "UNKNOWN");
773 
774  // try to get the name and path
775  GetImagePathAndName(&ImageEntry[j], ImageEntry[j].Handle, LoadedImageProt);
776 
777  ImageList->NumOfEntries++;
778  j++;
779 
780  }
781 
782  if (NewHandleList)
783  FreeHandles(HandleList);
784 
785 
786  return ImageList;
787 }

Here is the call graph for this function:

Here is the caller graph for this function:

CHAR16* EFIAPI GetDevicePathTextForHandle ( EFI_DEVICE_PATH_PROTOCOL *  DevicePath)

Function converts DevicePath to text. (caller needs to free the allocated memory)

Parameters
DevicePath
Return values
CHAR16*Pointer to unicode path

Definition at line 86 of file Images.c.

References EFI_STATUS(), and gDevPathToText.

Referenced by GetImagePathAndName(), and GetLoadedImageProtocol().

87 {
88 
89  CHAR16* out;
90  EFI_STATUS Status;
91 
92  if (gDevPathToText == NULL)
93  {
94  // apple mac firmware does no support this protocol
95  Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid,
96  NULL,
97  (VOID **)&gDevPathToText);
98 
99  if (EFI_ERROR(Status))
100  return 0;
101  }
102 
103  out = gDevPathToText->ConvertDevicePathToText(DevicePath, TRUE, FALSE);
104 
105  //the memory is allocated from EFI boot services memory.
106  //it needs to be freed by the calling party
107  return out;
108 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID GetHandleTypeStringAscii ( CHAR8 *  HandleTypeBuff,
UINTN  HandleTypeBuffSize,
UINT16  Flags 
)

Function converts HandleType to text representation.

Parameters
HandleTypeBuffPointer to output buffer
HandleTypeBuffSizeSize of the output buffer
FlagsValue of the selected handle type
Return values
NONE

Definition at line 39 of file Images.c.

References isHANDLE_TYPE_BUS_DRIVER(), isHANDLE_TYPE_CHILD_HANDLE(), isHANDLE_TYPE_COMPONENT_NAME_HANDLE(), isHANDLE_TYPE_CONTROLLER_HANDLE(), isHANDLE_TYPE_DEVICE_DRIVER(), isHANDLE_TYPE_DEVICE_HANDLE(), isHANDLE_TYPE_DRIVER_BINDING_HANDLE(), isHANDLE_TYPE_DRIVER_CONFIGURATION_HANDLE(), isHANDLE_TYPE_DRIVER_DIAGNOSTICS_HANDLE(), isHANDLE_TYPE_IMAGE_HANDLE(), isHANDLE_TYPE_PARENT_HANDLE(), and isHANDLE_TYPE_UNKNOWN().

Referenced by DumpAllHandles().

42 {
43  gBS->SetMem(HandleTypeBuff, HandleTypeBuffSize, 0);
44 
45  if (isHANDLE_TYPE_UNKNOWN(Flags))
46  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, " TYPE_UNKNOWN |");
47  if (isHANDLE_TYPE_IMAGE_HANDLE(Flags))
48  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_IMAGE_HANDLE |", HandleTypeBuff);
50  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_DRIVER_BINDING_HANDLE |", HandleTypeBuff);
51  if (isHANDLE_TYPE_DEVICE_DRIVER(Flags))
52  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_DEVICE_DRIVER |", HandleTypeBuff);
53  if (isHANDLE_TYPE_BUS_DRIVER(Flags))
54  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_BUS_DRIVER |", HandleTypeBuff);
56  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_DRIVER_CONFIGURATION_HANDLE |", HandleTypeBuff);
58  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_DRIVER_DIAGNOSTICS_HANDLE |", HandleTypeBuff);
60  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_COMPONENT_NAME_HANDLE |", HandleTypeBuff);
61  if (isHANDLE_TYPE_DEVICE_HANDLE(Flags))
62  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_DEVICE_HANDLE |", HandleTypeBuff);
63  if (isHANDLE_TYPE_PARENT_HANDLE(Flags))
64  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_PARENT_HANDLE |", HandleTypeBuff);
66  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_CONTROLLER_HANDLE |", HandleTypeBuff);
67  if (isHANDLE_TYPE_CHILD_HANDLE(Flags))
68  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, "%a TYPE_CHILD_HANDLE |", HandleTypeBuff);
69 
70  if (HandleTypeBuff[0] == 0)
71  AsciiSPrint(HandleTypeBuff, HandleTypeBuffSize-1, " UNKNOWN");
72 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID GetImagePathAndName ( image_entry *  ImageEntry,
EFI_HANDLE  Handle,
EFI_LOADED_IMAGE_PROTOCOL *  LoadedImageProt 
)

Function get path and file name associated with selected image entry. Both path and file name will be stored to the ImageEntry structure.

Parameters
ImageEntryPointer to selected ImageEntry
HandleImage handle
LoadedImageProtPointer to openned LOADED_IMAGE_PROTOCOL
Return values
NONE

Definition at line 590 of file Images.c.

References _image_entry::FilePath, GetDevicePathTextForHandle(), GetStringFromHandle(), MAX_IMAGE_TEXTBUFF_LEN, MemFree(), and _image_entry::Name.

Referenced by AddImageToList(), and GetAllImagesWithDescription().

593 {
594  CHAR16 *FilePath;
595  CHAR16 *Name;
596  CHAR8 Buffer[512];
597 
598  // try to get the name
599  FilePath = GetDevicePathTextForHandle(LoadedImageProt->FilePath);
600  Name = GetStringFromHandle(Handle, "en");
601 
602  if (!Name)
603  Name = GetStringFromHandle(Handle, "eng");
604 
605  AsciiSPrint((CHAR8*)&ImageEntry->FilePath, MAX_IMAGE_TEXTBUFF_LEN, "UNKNOWN");
606  AsciiSPrint((CHAR8*)&ImageEntry->Name, MAX_IMAGE_TEXTBUFF_LEN, "UNKNOWN");
607 
608  if (FilePath)
609  {
610 
611  UnicodeStrToAsciiStr(FilePath, Buffer);
612  AsciiSPrint((CHAR8*)&ImageEntry->FilePath, MAX_IMAGE_TEXTBUFF_LEN, "%a", Buffer);
613  MemFree(FilePath);
614  }
615 
616  if (Name)
617  {
618 
619  UnicodeStrToAsciiStr(Name, Buffer);
620  AsciiSPrint((CHAR8*)&ImageEntry->Name, MAX_IMAGE_TEXTBUFF_LEN, "%a", Buffer);
621  MemFree(Name);
622  }
623 }

Here is the call graph for this function:

Here is the caller graph for this function:

EFI_STATUS GetLoadedImageProtocol ( EFI_HANDLE  Handle,
EFI_LOADED_IMAGE_PROTOCOL **  OutLoadedImage 
)

Function returns pointer to EFI_LOADED_IMAGE_PROTOCOL for specified Handle. HandleProtocol queries a handle to determine if it supports a specified protocol.

Parameters
HandleSpecified handle
OutLoadedImagePointer to variable that will hold the EFI_LOADED_IMAGE_PROTOCOL
Return values
EFI_SUCCESSOn success
EFI_ERROR*On error

Definition at line 151 of file Images.c.

References EFI_STATUS(), GetDevicePathTextForHandle(), and LogFullAscii().

Referenced by GetAllImagesWithDescription(), and hooked_LoadImage().

153 {
154  EFI_STATUS Status;
155 
156  Status = gBS->HandleProtocol(Handle,
157  &gEfiLoadedImageProtocolGuid,
158  (VOID*)OutLoadedImage);
159 
160  if (EFI_ERROR(Status))
161  {
162 #if DEBUG_ON == 2
163  LogFullAscii("%a: error in HandleProtocol, error = %08x \r\n",
164  __FUNCTION__, Status);
165 #endif
166 
167  return Status;
168  }
169 
170 
171 #if DEBUG_ON == 2
172  LogFullAscii("%a: Imagebase: %016lx \r\n", __FUNCTION__, XLoadedImage->ImageBase);
173  LogFullAscii("%a: Name: %s\r\n", __FUNCTION__, GetDevicePathTextForHandle(XLoadedImage->FilePath));
174 #endif
175 
176  return EFI_SUCCESS;
177 
178 }

Here is the call graph for this function:

Here is the caller graph for this function:

CHAR16* EFIAPI GetStringFromHandle ( EFI_HANDLE  Handle,
CHAR8 *  Language 
)

Function tries to get the name associated with handle through the component name2 protocol and component name protocol. Selected handle needs to support it.

Parameters
HandleSpecified handle
LanguageAscii string defining the language ("en")
Return values
PointerOn success pointer to unicode name
NULLOn error

Definition at line 328 of file Images.c.

References EFI_STATUS(), gImageHandle, and LogFullAscii().

Referenced by GetImagePathAndName().

330 {
331  EFI_STATUS Status;
332  CHAR16 *OutString;
333  EFI_COMPONENT_NAME2_PROTOCOL *ComponentNameStruct;
334 
335 
336 
337  Status = gBS->OpenProtocol(Handle,
338  &gEfiComponentName2ProtocolGuid,
339  (VOID**)&ComponentNameStruct,
340  gImageHandle,
341  NULL,
342  EFI_OPEN_PROTOCOL_GET_PROTOCOL);
343 
344  if (!EFI_ERROR(Status))
345  {
346  Status = ComponentNameStruct->GetDriverName(ComponentNameStruct,
347  (CHAR8*)Language,
348  &OutString);
349 
350  if (!EFI_ERROR(Status))
351  return OutString;
352 
353  }
354 
355  // try the next one
356  Status = gBS->OpenProtocol(Handle,
357  &gEfiComponentNameProtocolGuid,
358  (VOID**)&ComponentNameStruct,
359  gImageHandle,
360  NULL,
361  EFI_OPEN_PROTOCOL_GET_PROTOCOL);
362 
363  if (EFI_ERROR(Status))
364  {
365 #if DEBUG_ON == 2
366  LogFullAscii("%a: unable to open protocol2, status = %08x \r\n",
367  __FUNCTION__, Status);
368 #endif
369  return 0;
370  }
371 
372  Status = ComponentNameStruct->GetDriverName(ComponentNameStruct,
373  (CHAR8*)Language,
374  &OutString);
375 
376  if (EFI_ERROR(Status))
377  {
378 #if DEBUG_ON == 2
379  LogFullAscii("%a: GetDriverName failed, status = %08x \r\n",
380  __FUNCTION__, Status);
381 #endif
382  return 0;
383  }
384 
385 
386  return OutString;
387 }

Here is the call graph for this function:

Here is the caller graph for this function:

CHAR16* EFIAPI GetStringFromHandle2 ( EFI_HANDLE  Handle,
CHAR8 *  Language 
)

Function tries to get the name associated with handle through the component name protocol. Selected handle needs to support it.

Parameters
HandleSpecified handle
LanguageAscii string defining the language ("en")
Return values
PointerOn success pointer to unicode name
NULLOn error

Definition at line 267 of file Images.c.

References EFI_STATUS(), gImageHandle, and LogFullAscii().

269 {
270 
271  EFI_STATUS Status;
272  CHAR16 *OutString;
273  EFI_COMPONENT_NAME2_PROTOCOL *ComponentNameStruct;
274 
275 
276  // try the next one
277  Status = gBS->OpenProtocol(Handle,
278  &gEfiComponentNameProtocolGuid,
279  (VOID**)&ComponentNameStruct,
280  gImageHandle,
281  NULL,
282  EFI_OPEN_PROTOCOL_GET_PROTOCOL);
283 
284  if (EFI_ERROR(Status))
285  {
286 #if DEBUG_ON == 2
287  LogFullAscii("%a: unable to open protocol (component name), status = %08x \r\n",
288  __FUNCTION__, Status);
289 #endif
290  return 0;
291  }
292 
293  Status = ComponentNameStruct->GetDriverName(ComponentNameStruct,
294  (CHAR8*)Language,
295  &OutString);
296 
297 
298  if (EFI_ERROR(Status))
299  {
300 #if DEBUG_ON == 2
301  LogFullAscii("%a: GetDriverName failed, status = %08x \r\n",
302  __FUNCTION__, Status);
303 #endif
304  return 0;
305  }
306 
307 
308  return OutString;
309 }

Here is the call graph for this function:

VOID MyModImageNotify ( image_entry_list *  OrgImageList,
image_entry *  NewImage 
)

Definition at line 1082 of file Images.c.

References _image_entry::FilePath, LogFileAscii(), and _image_entry::Name.

Referenced by TestDumpHandles().

1083 {
1084  LogFileAscii("MOD List=%p NewImage=%p Name=%a\r\n", OrgImageList, NewImage,
1085  NewImage->Name, NewImage->FilePath);
1086 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID MyNewEventCallback ( event_entry_list *  OrgEventList,
event_entry *  NewEvent 
)

Definition at line 1089 of file Images.c.

References LogFileAscii().

Referenced by TestDumpHandles().

1090 {
1091  LogFileAscii("New Event list=%p newevent=%p\r\n", OrgEventList, NewEvent);
1092 
1093 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID MyNewImageNotify ( image_entry_list *  OrgImageList,
image_entry *  NewImage 
)

Definition at line 1074 of file Images.c.

References _image_entry::FilePath, LogFileAscii(), and _image_entry::Name.

Referenced by TestDumpHandles().

1075 {
1076  LogFileAscii("NEW List=%p NewImage=%p Name=%a Path=%a\r\n", OrgImageList, NewImage,
1077  NewImage->Name, NewImage->FilePath);
1078 }

Here is the call graph for this function:

Here is the caller graph for this function:

VOID TestDumpHandles ( VOID  )

Definition at line 1097 of file Images.c.

References CheckImagesFunctionPointers(), ContextX64Test(), DumpAllEvents(), DumpAllHandles(), DumpAllImages(), DumpGDT(), DumpIDT(), FreeHandles(), FreeImages(), GetAllEvents(), GetAllHandlesWithDescription(), GetAllImagesWithDescription(), InstallEventScanner(), InstallSniffImage(), MyModImageNotify(), MyNewEventCallback(), MyNewImageNotify(), PRINT_FUNC, ScanMemoryForExecutables(), and ScreenShowSplashLogo().

1098 {
1099 
1100  handle_entry_list *HandleList;
1101  image_entry_list *ImageList;
1102  event_entry_list *EventList;
1103 
1105 
1106  HandleList = GetAllHandlesWithDescription();
1107  if (!HandleList)
1108  {
1109  PRINT_FUNC(L"TestDumpHandles: error executing GetAllHandlesWithDescription\r\n");
1110  return;
1111  }
1112 
1113 
1114  ImageList = GetAllImagesWithDescription(HandleList, TRUE);
1115  if (!ImageList)
1116  {
1117  PRINT_FUNC(L"TestDumpHandles: error executing GetAllImagesWithDescription\r\n");
1118  return;
1119  }
1120 
1121 
1122  DumpAllHandles(HandleList);
1123 
1124  DumpAllImages(ImageList, TRUE);
1125  CheckImagesFunctionPointers(ImageList);
1126 
1127  FreeImages(ImageList);
1128  FreeHandles(HandleList);
1129 
1130 
1131  // ImageList = GetAllImagesWithDescription(NULL, TRUE);
1132  //DumpAllImages(ImageList, TRUE);
1133 
1134 
1135  EventList = GetAllEvents();
1136  if (!EventList)
1137  {
1138  PRINT_FUNC(L"TestDumpHandles: error executing GetAllEvents \r\n");
1139  return;
1140  }
1141 
1142  DumpAllEvents(EventList);
1143 
1144  ContextX64Test();
1145 
1146  DumpIDT();
1147  DumpGDT();
1148 
1150 
1151  //DisplayDumpMemoryMap(TRUE);
1153 
1155 
1156 }

Here is the call graph for this function:

Variable Documentation

EFI_DEVICE_PATH_TO_TEXT_PROTOCOL* gDevPathToText

global variable that hold the DEVICE_PATH_TO_TEXT protocol pointer.

Definition at line 24 of file Images.c.

Referenced by GetDevicePathTextForHandle().