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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, "SimpleFileSystem" },
  58. { EFI_SIMPLE_NETWORK_PROTOCOL_GUID, "SimpleNetwork" },
  59. };
  60. /**
  61. * Convert GUID to a printable string
  62. *
  63. * @v guid GUID
  64. * @ret string Printable string
  65. */
  66. const char * efi_guid_ntoa ( EFI_GUID *guid ) {
  67. union {
  68. union uuid uuid;
  69. EFI_GUID guid;
  70. } u;
  71. unsigned int i;
  72. /* Check for a match against well-known GUIDs */
  73. for ( i = 0 ; i < ( sizeof ( efi_well_known_guids ) /
  74. sizeof ( efi_well_known_guids[0] ) ) ; i++ ) {
  75. if ( memcmp ( guid, &efi_well_known_guids[i].guid,
  76. sizeof ( *guid ) ) == 0 ) {
  77. return efi_well_known_guids[i].name;
  78. }
  79. }
  80. /* Convert GUID to standard endianness */
  81. memcpy ( &u.guid, guid, sizeof ( u.guid ) );
  82. uuid_mangle ( &u.uuid );
  83. return uuid_ntoa ( &u.uuid );
  84. }
  85. /**
  86. * Print list of protocol handlers attached to a handle
  87. *
  88. * @v handle EFI handle
  89. */
  90. void dbg_efi_protocols ( EFI_HANDLE handle ) {
  91. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  92. EFI_GUID **protocols;
  93. UINTN count;
  94. unsigned int i;
  95. EFI_STATUS efirc;
  96. int rc;
  97. /* Retrieve list of protocols */
  98. if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
  99. &count ) ) != 0 ) {
  100. rc = -EEFI ( efirc );
  101. printf ( "EFI could not retrieve protocols for %p: %s\n",
  102. handle, strerror ( rc ) );
  103. return;
  104. }
  105. /* Dump list of protocols */
  106. for ( i = 0 ; i < count ; i++ )
  107. printf ( "%s\n", efi_guid_ntoa ( protocols[i] ) );
  108. /* Free list */
  109. bs->FreePool ( protocols );
  110. }
  111. /**
  112. * Get textual representation of device path
  113. *
  114. * @v path Device path
  115. * @ret text Textual representation of device path, or NULL
  116. */
  117. const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
  118. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  119. static char text[256];
  120. CHAR16 *wtext;
  121. /* Convert path to a textual representation */
  122. if ( ! efidpt )
  123. return NULL;
  124. wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
  125. if ( ! wtext )
  126. return NULL;
  127. /* Store path in buffer */
  128. snprintf ( text, sizeof ( text ), "%ls", wtext );
  129. /* Free path */
  130. bs->FreePool ( wtext );
  131. return text;
  132. }
  133. /**
  134. * Get textual representation of device path for a handle
  135. *
  136. * @v handle EFI handle
  137. * @ret text Textual representation of device path, or NULL
  138. */
  139. const char * efi_handle_devpath_text ( EFI_HANDLE handle ) {
  140. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  141. union {
  142. EFI_DEVICE_PATH_PROTOCOL *path;
  143. void *interface;
  144. } path;
  145. const char *text;
  146. EFI_STATUS efirc;
  147. /* Obtain device path, if any */
  148. if ( ( efirc = bs->OpenProtocol ( handle,
  149. &efi_device_path_protocol_guid,
  150. &path.interface, efi_image_handle,
  151. handle,
  152. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  153. return NULL;
  154. }
  155. /* Format device path */
  156. text = efi_devpath_text ( path.path );
  157. /* Close device path */
  158. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  159. efi_image_handle, handle );
  160. return text;
  161. }