You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_debug.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /**
  21. * @file
  22. *
  23. * EFI debugging utilities
  24. *
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <ipxe/uuid.h>
  30. #include <ipxe/efi/efi.h>
  31. #include <ipxe/efi/efi_driver.h>
  32. #include <ipxe/efi/Protocol/DevicePath.h>
  33. #include <ipxe/efi/Protocol/DevicePathToText.h>
  34. #include <ipxe/efi/Protocol/BlockIo.h>
  35. #include <ipxe/efi/Protocol/DiskIo.h>
  36. #include <ipxe/efi/Protocol/SimpleFileSystem.h>
  37. #include <ipxe/efi/Protocol/SimpleNetwork.h>
  38. /** Device path protocol GUID */
  39. static EFI_GUID efi_device_path_protocol_guid
  40. = EFI_DEVICE_PATH_PROTOCOL_GUID;
  41. /** Device path to text protocol */
  42. static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *efidpt;
  43. EFI_REQUEST_PROTOCOL ( EFI_DEVICE_PATH_TO_TEXT_PROTOCOL, &efidpt );
  44. /** A well-known GUID */
  45. struct efi_well_known_guid {
  46. /** GUID */
  47. EFI_GUID guid;
  48. /** Name */
  49. const char *name;
  50. };
  51. /** Well-known GUIDs */
  52. static struct efi_well_known_guid efi_well_known_guids[] = {
  53. { EFI_BLOCK_IO_PROTOCOL_GUID, "BlockIo" },
  54. { EFI_DISK_IO_PROTOCOL_GUID, "DiskIo" },
  55. { EFI_DEVICE_PATH_PROTOCOL_GUID, "DevicePath" },
  56. { EFI_LOADED_IMAGE_PROTOCOL_GUID, "LoadedImage" },
  57. { EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID, "LoadedImageDevicePath" },
  58. { EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, "SimpleFileSystem" },
  59. { EFI_SIMPLE_NETWORK_PROTOCOL_GUID, "SimpleNetwork" },
  60. };
  61. /**
  62. * Convert GUID to a printable string
  63. *
  64. * @v guid GUID
  65. * @ret string Printable string
  66. */
  67. const char * efi_guid_ntoa ( EFI_GUID *guid ) {
  68. union {
  69. union uuid uuid;
  70. EFI_GUID guid;
  71. } u;
  72. unsigned int i;
  73. /* Check for a match against well-known GUIDs */
  74. for ( i = 0 ; i < ( sizeof ( efi_well_known_guids ) /
  75. sizeof ( efi_well_known_guids[0] ) ) ; i++ ) {
  76. if ( memcmp ( guid, &efi_well_known_guids[i].guid,
  77. sizeof ( *guid ) ) == 0 ) {
  78. return efi_well_known_guids[i].name;
  79. }
  80. }
  81. /* Convert GUID to standard endianness */
  82. memcpy ( &u.guid, guid, sizeof ( u.guid ) );
  83. uuid_mangle ( &u.uuid );
  84. return uuid_ntoa ( &u.uuid );
  85. }
  86. /**
  87. * Print list of protocol handlers attached to a handle
  88. *
  89. * @v handle EFI handle
  90. */
  91. void dbg_efi_protocols ( EFI_HANDLE handle ) {
  92. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  93. EFI_GUID **protocols;
  94. UINTN count;
  95. unsigned int i;
  96. EFI_STATUS efirc;
  97. int rc;
  98. /* Retrieve list of protocols */
  99. if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
  100. &count ) ) != 0 ) {
  101. rc = -EEFI ( efirc );
  102. printf ( "EFI could not retrieve protocols for %p: %s\n",
  103. handle, strerror ( rc ) );
  104. return;
  105. }
  106. /* Dump list of protocols */
  107. for ( i = 0 ; i < count ; i++ )
  108. printf ( "%s\n", efi_guid_ntoa ( protocols[i] ) );
  109. /* Free list */
  110. bs->FreePool ( protocols );
  111. }
  112. /**
  113. * Get textual representation of device path
  114. *
  115. * @v path Device path
  116. * @ret text Textual representation of device path, or NULL
  117. */
  118. const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
  119. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  120. static char text[256];
  121. CHAR16 *wtext;
  122. /* Convert path to a textual representation */
  123. if ( ! efidpt )
  124. return NULL;
  125. wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
  126. if ( ! wtext )
  127. return NULL;
  128. /* Store path in buffer */
  129. snprintf ( text, sizeof ( text ), "%ls", wtext );
  130. /* Free path */
  131. bs->FreePool ( wtext );
  132. return text;
  133. }
  134. /**
  135. * Get textual representation of device path for a handle
  136. *
  137. * @v handle EFI handle
  138. * @ret text Textual representation of device path, or NULL
  139. */
  140. const char * efi_handle_devpath_text ( EFI_HANDLE handle ) {
  141. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  142. union {
  143. EFI_DEVICE_PATH_PROTOCOL *path;
  144. void *interface;
  145. } path;
  146. const char *text;
  147. EFI_STATUS efirc;
  148. /* Obtain device path, if any */
  149. if ( ( efirc = bs->OpenProtocol ( handle,
  150. &efi_device_path_protocol_guid,
  151. &path.interface, efi_image_handle,
  152. handle,
  153. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  154. return NULL;
  155. }
  156. /* Format device path */
  157. text = efi_devpath_text ( path.path );
  158. /* Close device path */
  159. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  160. efi_image_handle, handle );
  161. return text;
  162. }