Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

efi_debug.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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/BlockIo.h>
  33. #include <ipxe/efi/Protocol/BusSpecificDriverOverride.h>
  34. #include <ipxe/efi/Protocol/ComponentName.h>
  35. #include <ipxe/efi/Protocol/ComponentName2.h>
  36. #include <ipxe/efi/Protocol/DevicePath.h>
  37. #include <ipxe/efi/Protocol/DevicePathToText.h>
  38. #include <ipxe/efi/Protocol/DiskIo.h>
  39. #include <ipxe/efi/Protocol/DriverBinding.h>
  40. #include <ipxe/efi/Protocol/LoadFile.h>
  41. #include <ipxe/efi/Protocol/LoadFile2.h>
  42. #include <ipxe/efi/Protocol/LoadedImage.h>
  43. #include <ipxe/efi/Protocol/PciIo.h>
  44. #include <ipxe/efi/Protocol/PciRootBridgeIo.h>
  45. #include <ipxe/efi/Protocol/SimpleFileSystem.h>
  46. #include <ipxe/efi/Protocol/SimpleNetwork.h>
  47. #include <ipxe/efi/IndustryStandard/PeImage.h>
  48. /** Block I/O protocol GUID */
  49. static EFI_GUID efi_block_io_protocol_guid
  50. = EFI_BLOCK_IO_PROTOCOL_GUID;
  51. /** Bus specific driver override protocol GUID */
  52. static EFI_GUID efi_bus_specific_driver_override_protocol_guid
  53. = EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL_GUID;
  54. /** Component name protocol GUID */
  55. static EFI_GUID efi_component_name_protocol_guid
  56. = EFI_COMPONENT_NAME_PROTOCOL_GUID;
  57. /** Component name 2 protocol GUID */
  58. static EFI_GUID efi_component_name2_protocol_guid
  59. = EFI_COMPONENT_NAME2_PROTOCOL_GUID;
  60. /** Device path protocol GUID */
  61. static EFI_GUID efi_device_path_protocol_guid
  62. = EFI_DEVICE_PATH_PROTOCOL_GUID;
  63. /** Disk I/O protocol GUID */
  64. static EFI_GUID efi_disk_io_protocol_guid
  65. = EFI_DISK_IO_PROTOCOL_GUID;
  66. /** Driver binding protocol GUID */
  67. static EFI_GUID efi_driver_binding_protocol_guid
  68. = EFI_DRIVER_BINDING_PROTOCOL_GUID;
  69. /** Load file protocol GUID */
  70. static EFI_GUID efi_load_file_protocol_guid
  71. = EFI_LOAD_FILE_PROTOCOL_GUID;
  72. /** Load file 2 protocol GUID */
  73. static EFI_GUID efi_load_file2_protocol_guid
  74. = EFI_LOAD_FILE2_PROTOCOL_GUID;
  75. /** Loaded image protocol GUID */
  76. static EFI_GUID efi_loaded_image_protocol_guid
  77. = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  78. /** Loaded image device path protocol GUID */
  79. static EFI_GUID efi_loaded_image_device_path_protocol_guid
  80. = EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
  81. /** PCI I/O protocol GUID */
  82. static EFI_GUID efi_pci_io_protocol_guid
  83. = EFI_PCI_IO_PROTOCOL_GUID;
  84. /** PCI root bridge I/O protocol GUID */
  85. static EFI_GUID efi_pci_root_bridge_io_protocol_guid
  86. = EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_GUID;
  87. /** Simple file system protocol GUID */
  88. static EFI_GUID efi_simple_file_system_protocol_guid
  89. = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  90. /** Simple network protocol guid */
  91. static EFI_GUID efi_simple_network_protocol_guid
  92. = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
  93. /** Device path to text protocol */
  94. static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *efidpt;
  95. EFI_REQUEST_PROTOCOL ( EFI_DEVICE_PATH_TO_TEXT_PROTOCOL, &efidpt );
  96. /** A well-known GUID */
  97. struct efi_well_known_guid {
  98. /** GUID */
  99. EFI_GUID *guid;
  100. /** Name */
  101. const char *name;
  102. };
  103. /** Well-known GUIDs */
  104. static struct efi_well_known_guid efi_well_known_guids[] = {
  105. { &efi_block_io_protocol_guid,
  106. "BlockIo" },
  107. { &efi_bus_specific_driver_override_protocol_guid,
  108. "BusSpecificDriverOverride" },
  109. { &efi_component_name2_protocol_guid,
  110. "ComponentName2" },
  111. { &efi_component_name_protocol_guid,
  112. "ComponentName" },
  113. { &efi_device_path_protocol_guid,
  114. "DevicePath" },
  115. { &efi_driver_binding_protocol_guid,
  116. "DriverBinding" },
  117. { &efi_disk_io_protocol_guid,
  118. "DiskIo" },
  119. { &efi_load_file_protocol_guid,
  120. "LoadFile" },
  121. { &efi_load_file2_protocol_guid,
  122. "LoadFile2" },
  123. { &efi_loaded_image_protocol_guid,
  124. "LoadedImage" },
  125. { &efi_loaded_image_device_path_protocol_guid,
  126. "LoadedImageDevicePath"},
  127. { &efi_pci_io_protocol_guid,
  128. "PciIo" },
  129. { &efi_pci_root_bridge_io_protocol_guid,
  130. "PciRootBridgeIo" },
  131. { &efi_simple_file_system_protocol_guid,
  132. "SimpleFileSystem" },
  133. { &efi_simple_network_protocol_guid,
  134. "SimpleNetwork" },
  135. };
  136. /**
  137. * Convert GUID to a printable string
  138. *
  139. * @v guid GUID
  140. * @ret string Printable string
  141. */
  142. const char * efi_guid_ntoa ( EFI_GUID *guid ) {
  143. union {
  144. union uuid uuid;
  145. EFI_GUID guid;
  146. } u;
  147. unsigned int i;
  148. /* Check for a match against well-known GUIDs */
  149. for ( i = 0 ; i < ( sizeof ( efi_well_known_guids ) /
  150. sizeof ( efi_well_known_guids[0] ) ) ; i++ ) {
  151. if ( memcmp ( guid, efi_well_known_guids[i].guid,
  152. sizeof ( *guid ) ) == 0 ) {
  153. return efi_well_known_guids[i].name;
  154. }
  155. }
  156. /* Convert GUID to standard endianness */
  157. memcpy ( &u.guid, guid, sizeof ( u.guid ) );
  158. uuid_mangle ( &u.uuid );
  159. return uuid_ntoa ( &u.uuid );
  160. }
  161. /**
  162. * Print list of protocol handlers attached to a handle
  163. *
  164. * @v handle EFI handle
  165. */
  166. void dbg_efi_protocols ( EFI_HANDLE handle ) {
  167. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  168. EFI_GUID **protocols;
  169. UINTN count;
  170. unsigned int i;
  171. EFI_STATUS efirc;
  172. int rc;
  173. /* Retrieve list of protocols */
  174. if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
  175. &count ) ) != 0 ) {
  176. rc = -EEFI ( efirc );
  177. printf ( "EFI could not retrieve protocols for %p: %s\n",
  178. handle, strerror ( rc ) );
  179. return;
  180. }
  181. /* Dump list of protocols */
  182. for ( i = 0 ; i < count ; i++ )
  183. printf ( "%s\n", efi_guid_ntoa ( protocols[i] ) );
  184. /* Free list */
  185. bs->FreePool ( protocols );
  186. }
  187. /**
  188. * Get textual representation of device path
  189. *
  190. * @v path Device path
  191. * @ret text Textual representation of device path, or NULL
  192. */
  193. const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
  194. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  195. static char text[256];
  196. CHAR16 *wtext;
  197. /* Convert path to a textual representation */
  198. if ( ! efidpt )
  199. return NULL;
  200. wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
  201. if ( ! wtext )
  202. return NULL;
  203. /* Store path in buffer */
  204. snprintf ( text, sizeof ( text ), "%ls", wtext );
  205. /* Free path */
  206. bs->FreePool ( wtext );
  207. return text;
  208. }
  209. /**
  210. * Get driver name
  211. *
  212. * @v wtf Component name protocol
  213. * @ret name Driver name, or NULL
  214. */
  215. static const char * efi_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
  216. static char name[64];
  217. CHAR16 *driver_name;
  218. EFI_STATUS efirc;
  219. /* Try "en" first; if that fails then try the first language */
  220. if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
  221. &driver_name ) ) != 0 ) &&
  222. ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
  223. &driver_name ) ) != 0 ) ) {
  224. return NULL;
  225. }
  226. /* Convert name from CHAR16 to char */
  227. snprintf ( name, sizeof ( name ), "%ls", driver_name );
  228. return name;
  229. }
  230. /**
  231. * Get PE/COFF debug filename
  232. *
  233. * @v loaded Loaded image
  234. * @ret name PE/COFF debug filename, or NULL
  235. */
  236. static const char *
  237. efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  238. static char buf[32];
  239. EFI_IMAGE_DOS_HEADER *dos = loaded->ImageBase;
  240. EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
  241. EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
  242. EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
  243. EFI_IMAGE_DATA_DIRECTORY *datadir;
  244. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *debug;
  245. EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *codeview;
  246. size_t max_len;
  247. char *name;
  248. char *tmp;
  249. /* Parse DOS header */
  250. if ( ! dos ) {
  251. DBGC ( loaded, "Missing DOS header\n" );
  252. return NULL;
  253. }
  254. if ( dos->e_magic != EFI_IMAGE_DOS_SIGNATURE ) {
  255. DBGC ( loaded, "Bad DOS signature\n" );
  256. return NULL;
  257. }
  258. pe = ( loaded->ImageBase + dos->e_lfanew );
  259. /* Parse PE header */
  260. if ( pe->Pe32.Signature != EFI_IMAGE_NT_SIGNATURE ) {
  261. DBGC ( loaded, "Bad PE signature\n" );
  262. return NULL;
  263. }
  264. opt32 = &pe->Pe32.OptionalHeader;
  265. opt64 = &pe->Pe32Plus.OptionalHeader;
  266. if ( opt32->Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ) {
  267. datadir = opt32->DataDirectory;
  268. } else if ( opt64->Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC ) {
  269. datadir = opt64->DataDirectory;
  270. } else {
  271. DBGC ( loaded, "Bad optional header signature\n" );
  272. return NULL;
  273. }
  274. /* Parse data directory entry */
  275. if ( ! datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress ) {
  276. DBGC ( loaded, "Empty debug directory entry\n" );
  277. return NULL;
  278. }
  279. debug = ( loaded->ImageBase +
  280. datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress );
  281. /* Parse debug directory entry */
  282. if ( debug->Type != EFI_IMAGE_DEBUG_TYPE_CODEVIEW ) {
  283. DBGC ( loaded, "Not a CodeView debug directory entry\n" );
  284. return NULL;
  285. }
  286. codeview = ( loaded->ImageBase + debug->RVA );
  287. /* Parse CodeView entry */
  288. if ( codeview->Signature != CODEVIEW_SIGNATURE_NB10 ) {
  289. DBGC ( loaded, "Bad CodeView signature\n" );
  290. return NULL;
  291. }
  292. name = ( ( ( void * ) codeview ) + sizeof ( *codeview ) );
  293. /* Sanity check - avoid scanning endlessly through memory */
  294. max_len = EFI_PAGE_SIZE; /* Reasonably sane */
  295. if ( strnlen ( name, max_len ) == max_len ) {
  296. DBGC ( loaded, "Excessively long or invalid CodeView name\n" );
  297. return NULL;
  298. }
  299. /* Skip any directory components. We cannot modify this data
  300. * or create a temporary buffer, so do not use basename().
  301. */
  302. while ( ( ( tmp = strchr ( name, '/' ) ) != NULL ) ||
  303. ( ( tmp = strchr ( name, '\\' ) ) != NULL ) ) {
  304. name = ( tmp + 1 );
  305. }
  306. /* Copy base name to buffer */
  307. snprintf ( buf, sizeof ( buf ), "%s", name );
  308. /* Strip file suffix, if present */
  309. if ( ( tmp = strrchr ( name, '.' ) ) != NULL )
  310. *tmp = '\0';
  311. return name;
  312. }
  313. /**
  314. * Get initial loaded image name
  315. *
  316. * @v loaded Loaded image
  317. * @ret name Initial loaded image name, or NULL
  318. */
  319. static const char *
  320. efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  321. return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
  322. }
  323. /**
  324. * Get loaded image name from file path
  325. *
  326. * @v loaded Loaded image
  327. * @ret name Loaded image name, or NULL
  328. */
  329. static const char *
  330. efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  331. return efi_devpath_text ( loaded->FilePath );
  332. }
  333. /** An EFI handle name type */
  334. struct efi_handle_name_type {
  335. /** Protocol */
  336. EFI_GUID *protocol;
  337. /**
  338. * Get name
  339. *
  340. * @v interface Protocol interface
  341. * @ret name Name of handle, or NULL on failure
  342. */
  343. const char * ( * name ) ( void *interface );
  344. };
  345. /**
  346. * Define an EFI handle name type
  347. *
  348. * @v protocol Protocol interface
  349. * @v name Method to get name
  350. * @ret type EFI handle name type
  351. */
  352. #define EFI_HANDLE_NAME_TYPE( protocol, name ) { \
  353. (protocol), \
  354. ( const char * ( * ) ( void * ) ) (name), \
  355. }
  356. /** EFI handle name types */
  357. static struct efi_handle_name_type efi_handle_name_types[] = {
  358. /* Device path */
  359. EFI_HANDLE_NAME_TYPE ( &efi_device_path_protocol_guid,
  360. efi_devpath_text ),
  361. /* Driver name (for driver image handles) */
  362. EFI_HANDLE_NAME_TYPE ( &efi_component_name2_protocol_guid,
  363. efi_driver_name ),
  364. /* PE/COFF debug filename (for image handles) */
  365. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  366. efi_pecoff_debug_name ),
  367. /* Loaded image device path (for image handles) */
  368. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_device_path_protocol_guid,
  369. efi_devpath_text ),
  370. /* First loaded image name (for the DxeCore image) */
  371. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  372. efi_first_loaded_image_name ),
  373. /* Handle's loaded image file path (for image handles) */
  374. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  375. efi_loaded_image_filepath_name ),
  376. };
  377. /**
  378. * Get name of an EFI handle
  379. *
  380. * @v handle EFI handle
  381. * @ret text Name of handle, or NULL
  382. */
  383. const char * efi_handle_name ( EFI_HANDLE handle ) {
  384. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  385. struct efi_handle_name_type *type;
  386. unsigned int i;
  387. void *interface;
  388. const char *name;
  389. EFI_STATUS efirc;
  390. /* Fail immediately for NULL handles */
  391. if ( ! handle )
  392. return NULL;
  393. /* Try each name type in turn */
  394. for ( i = 0 ; i < ( sizeof ( efi_handle_name_types ) /
  395. sizeof ( efi_handle_name_types[0] ) ) ; i++ ) {
  396. type = &efi_handle_name_types[i];
  397. /* Try to open the applicable protocol */
  398. efirc = bs->OpenProtocol ( handle, type->protocol, &interface,
  399. efi_image_handle, handle,
  400. EFI_OPEN_PROTOCOL_GET_PROTOCOL );
  401. if ( efirc != 0 )
  402. continue;
  403. /* Try to get name from this protocol */
  404. name = type->name ( interface );
  405. /* Close protocol */
  406. bs->CloseProtocol ( handle, type->protocol,
  407. efi_image_handle, handle );
  408. /* Use this name, if possible */
  409. if ( name && name[0] )
  410. return name;
  411. }
  412. return "UNKNOWN";
  413. }
  414. /**
  415. * Get textual representation of device path for a handle
  416. *
  417. * @v handle EFI handle
  418. * @ret text Textual representation of device path, or NULL
  419. */
  420. const char * efi_handle_devpath_text ( EFI_HANDLE handle ) {
  421. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  422. union {
  423. EFI_DEVICE_PATH_PROTOCOL *path;
  424. void *interface;
  425. } path;
  426. const char *text;
  427. EFI_STATUS efirc;
  428. /* Obtain device path, if any */
  429. if ( ( efirc = bs->OpenProtocol ( handle,
  430. &efi_device_path_protocol_guid,
  431. &path.interface, efi_image_handle,
  432. handle,
  433. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  434. return NULL;
  435. }
  436. /* Format device path */
  437. text = efi_devpath_text ( path.path );
  438. /* Close device path */
  439. bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
  440. efi_image_handle, handle );
  441. return text;
  442. }