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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * EFI debugging utilities
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <ipxe/uuid.h>
  34. #include <ipxe/base16.h>
  35. #include <ipxe/efi/efi.h>
  36. #include <ipxe/efi/efi_utils.h>
  37. #include <ipxe/efi/Protocol/ComponentName.h>
  38. #include <ipxe/efi/Protocol/ComponentName2.h>
  39. #include <ipxe/efi/Protocol/DevicePathToText.h>
  40. #include <ipxe/efi/IndustryStandard/PeImage.h>
  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. /** Iscsi4Dxe module GUID */
  45. static EFI_GUID efi_iscsi4_dxe_guid = {
  46. 0x4579b72d, 0x7ec4, 0x4dd4,
  47. { 0x84, 0x86, 0x08, 0x3c, 0x86, 0xb1, 0x82, 0xa7 }
  48. };
  49. /** VlanConfigDxe module GUID */
  50. static EFI_GUID efi_vlan_config_dxe_guid = {
  51. 0xe4f61863, 0xfe2c, 0x4b56,
  52. { 0xa8, 0xf4, 0x08, 0x51, 0x9b, 0xc4, 0x39, 0xdf }
  53. };
  54. /** A well-known GUID */
  55. struct efi_well_known_guid {
  56. /** GUID */
  57. EFI_GUID *guid;
  58. /** Name */
  59. const char *name;
  60. };
  61. /** Well-known GUIDs */
  62. static struct efi_well_known_guid efi_well_known_guids[] = {
  63. { &efi_arp_protocol_guid,
  64. "Arp" },
  65. { &efi_arp_service_binding_protocol_guid,
  66. "ArpSb" },
  67. { &efi_block_io_protocol_guid,
  68. "BlockIo" },
  69. { &efi_bus_specific_driver_override_protocol_guid,
  70. "BusSpecificDriverOverride" },
  71. { &efi_component_name_protocol_guid,
  72. "ComponentName" },
  73. { &efi_component_name2_protocol_guid,
  74. "ComponentName2" },
  75. { &efi_device_path_protocol_guid,
  76. "DevicePath" },
  77. { &efi_driver_binding_protocol_guid,
  78. "DriverBinding" },
  79. { &efi_dhcp4_protocol_guid,
  80. "Dhcp4" },
  81. { &efi_dhcp4_service_binding_protocol_guid,
  82. "Dhcp4Sb" },
  83. { &efi_disk_io_protocol_guid,
  84. "DiskIo" },
  85. { &efi_graphics_output_protocol_guid,
  86. "GraphicsOutput" },
  87. { &efi_hii_config_access_protocol_guid,
  88. "HiiConfigAccess" },
  89. { &efi_ip4_protocol_guid,
  90. "Ip4" },
  91. { &efi_ip4_config_protocol_guid,
  92. "Ip4Config" },
  93. { &efi_ip4_service_binding_protocol_guid,
  94. "Ip4Sb" },
  95. { &efi_iscsi4_dxe_guid,
  96. "IScsi4Dxe" },
  97. { &efi_load_file_protocol_guid,
  98. "LoadFile" },
  99. { &efi_load_file2_protocol_guid,
  100. "LoadFile2" },
  101. { &efi_loaded_image_protocol_guid,
  102. "LoadedImage" },
  103. { &efi_loaded_image_device_path_protocol_guid,
  104. "LoadedImageDevicePath"},
  105. { &efi_managed_network_protocol_guid,
  106. "ManagedNetwork" },
  107. { &efi_managed_network_service_binding_protocol_guid,
  108. "ManagedNetworkSb" },
  109. { &efi_mtftp4_protocol_guid,
  110. "Mtftp4" },
  111. { &efi_mtftp4_service_binding_protocol_guid,
  112. "Mtftp4Sb" },
  113. { &efi_nii_protocol_guid,
  114. "Nii" },
  115. { &efi_nii31_protocol_guid,
  116. "Nii31" },
  117. { &efi_pci_io_protocol_guid,
  118. "PciIo" },
  119. { &efi_pci_root_bridge_io_protocol_guid,
  120. "PciRootBridgeIo" },
  121. { &efi_pxe_base_code_protocol_guid,
  122. "PxeBaseCode" },
  123. { &efi_simple_file_system_protocol_guid,
  124. "SimpleFileSystem" },
  125. { &efi_simple_network_protocol_guid,
  126. "SimpleNetwork" },
  127. { &efi_tcg_protocol_guid,
  128. "Tcg" },
  129. { &efi_tcp4_protocol_guid,
  130. "Tcp4" },
  131. { &efi_tcp4_service_binding_protocol_guid,
  132. "Tcp4Sb" },
  133. { &efi_udp4_protocol_guid,
  134. "Udp4" },
  135. { &efi_udp4_service_binding_protocol_guid,
  136. "Udp4Sb" },
  137. { &efi_vlan_config_protocol_guid,
  138. "VlanConfig" },
  139. { &efi_vlan_config_dxe_guid,
  140. "VlanConfigDxe" },
  141. };
  142. /**
  143. * Convert GUID to a printable string
  144. *
  145. * @v guid GUID
  146. * @ret string Printable string
  147. */
  148. const char * efi_guid_ntoa ( EFI_GUID *guid ) {
  149. union {
  150. union uuid uuid;
  151. EFI_GUID guid;
  152. } u;
  153. unsigned int i;
  154. /* Sanity check */
  155. if ( ! guid )
  156. return NULL;
  157. /* Check for a match against well-known GUIDs */
  158. for ( i = 0 ; i < ( sizeof ( efi_well_known_guids ) /
  159. sizeof ( efi_well_known_guids[0] ) ) ; i++ ) {
  160. if ( memcmp ( guid, efi_well_known_guids[i].guid,
  161. sizeof ( *guid ) ) == 0 ) {
  162. return efi_well_known_guids[i].name;
  163. }
  164. }
  165. /* Convert GUID to standard endianness */
  166. memcpy ( &u.guid, guid, sizeof ( u.guid ) );
  167. uuid_mangle ( &u.uuid );
  168. return uuid_ntoa ( &u.uuid );
  169. }
  170. /**
  171. * Name protocol open attributes
  172. *
  173. * @v attributes Protocol open attributes
  174. * @ret name Protocol open attributes name
  175. *
  176. * Returns a (static) string with characters for each set bit
  177. * corresponding to BY_(H)ANDLE_PROTOCOL, (G)ET_PROTOCOL,
  178. * (T)EST_PROTOCOL, BY_(C)HILD_CONTROLLER, BY_(D)RIVER, and
  179. * E(X)CLUSIVE.
  180. */
  181. static const char * efi_open_attributes_name ( unsigned int attributes ) {
  182. static char attribute_chars[] = "HGTCDX";
  183. static char name[ sizeof ( attribute_chars ) ];
  184. char *tmp = name;
  185. unsigned int i;
  186. for ( i = 0 ; i < ( sizeof ( attribute_chars ) - 1 ) ; i++ ) {
  187. if ( attributes & ( 1 << i ) )
  188. *(tmp++) = attribute_chars[i];
  189. }
  190. *tmp = '\0';
  191. return name;
  192. }
  193. /**
  194. * Print list of openers of a given protocol on a given handle
  195. *
  196. * @v handle EFI handle
  197. * @v protocol Protocol GUID
  198. */
  199. void dbg_efi_openers ( EFI_HANDLE handle, EFI_GUID *protocol ) {
  200. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  201. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *openers;
  202. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *opener;
  203. UINTN count;
  204. unsigned int i;
  205. EFI_STATUS efirc;
  206. int rc;
  207. /* Sanity check */
  208. if ( ( ! handle ) || ( ! protocol ) ) {
  209. printf ( "EFI could not retrieve openers for %s on %p\n",
  210. efi_guid_ntoa ( protocol ), handle );
  211. return;
  212. }
  213. /* Retrieve list of openers */
  214. if ( ( efirc = bs->OpenProtocolInformation ( handle, protocol, &openers,
  215. &count ) ) != 0 ) {
  216. rc = -EEFI ( efirc );
  217. printf ( "EFI could not retrieve openers for %s on %p: %s\n",
  218. efi_guid_ntoa ( protocol ), handle, strerror ( rc ) );
  219. return;
  220. }
  221. /* Dump list of openers */
  222. for ( i = 0 ; i < count ; i++ ) {
  223. opener = &openers[i];
  224. printf ( "HANDLE %p %s %s opened %dx (%s)",
  225. handle, efi_handle_name ( handle ),
  226. efi_guid_ntoa ( protocol ), opener->OpenCount,
  227. efi_open_attributes_name ( opener->Attributes ) );
  228. printf ( " by %p %s", opener->AgentHandle,
  229. efi_handle_name ( opener->AgentHandle ) );
  230. if ( opener->ControllerHandle == handle ) {
  231. printf ( "\n" );
  232. } else {
  233. printf ( " for %p %s\n", opener->ControllerHandle,
  234. efi_handle_name ( opener->ControllerHandle ) );
  235. }
  236. }
  237. /* Free list */
  238. bs->FreePool ( openers );
  239. }
  240. /**
  241. * Print list of protocol handlers attached to a handle
  242. *
  243. * @v handle EFI handle
  244. */
  245. void dbg_efi_protocols ( EFI_HANDLE handle ) {
  246. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  247. EFI_GUID **protocols;
  248. EFI_GUID *protocol;
  249. UINTN count;
  250. unsigned int i;
  251. EFI_STATUS efirc;
  252. int rc;
  253. /* Sanity check */
  254. if ( ! handle ) {
  255. printf ( "EFI could not retrieve protocols for %p\n", handle );
  256. return;
  257. }
  258. /* Retrieve list of protocols */
  259. if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
  260. &count ) ) != 0 ) {
  261. rc = -EEFI ( efirc );
  262. printf ( "EFI could not retrieve protocols for %p: %s\n",
  263. handle, strerror ( rc ) );
  264. return;
  265. }
  266. /* Dump list of protocols */
  267. for ( i = 0 ; i < count ; i++ ) {
  268. protocol = protocols[i];
  269. printf ( "HANDLE %p %s %s supported\n",
  270. handle, efi_handle_name ( handle ),
  271. efi_guid_ntoa ( protocol ) );
  272. dbg_efi_openers ( handle, protocol );
  273. }
  274. /* Free list */
  275. bs->FreePool ( protocols );
  276. }
  277. /**
  278. * Get textual representation of device path
  279. *
  280. * @v path Device path
  281. * @ret text Textual representation of device path, or NULL
  282. */
  283. const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
  284. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  285. static char text[256];
  286. void *start;
  287. void *end;
  288. size_t max_len;
  289. size_t len;
  290. CHAR16 *wtext;
  291. /* Sanity checks */
  292. if ( ! path ) {
  293. DBG ( "[NULL DevicePath]" );
  294. return NULL;
  295. }
  296. /* If we have no DevicePathToText protocol then use a raw hex string */
  297. if ( ! efidpt ) {
  298. DBG ( "[No DevicePathToText]" );
  299. start = path;
  300. end = efi_devpath_end ( path );
  301. len = ( end - start );
  302. max_len = ( ( sizeof ( text ) - 1 /* NUL */ ) / 2 /* "xx" */ );
  303. if ( len > max_len )
  304. len = max_len;
  305. base16_encode ( start, len, text, sizeof ( text ) );
  306. return text;
  307. }
  308. /* Convert path to a textual representation */
  309. wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
  310. if ( ! wtext )
  311. return NULL;
  312. /* Store path in buffer */
  313. snprintf ( text, sizeof ( text ), "%ls", wtext );
  314. /* Free path */
  315. bs->FreePool ( wtext );
  316. return text;
  317. }
  318. /**
  319. * Get driver name
  320. *
  321. * @v wtf Component name protocol
  322. * @ret name Driver name, or NULL
  323. */
  324. static const char * efi_driver_name ( EFI_COMPONENT_NAME_PROTOCOL *wtf ) {
  325. static char name[64];
  326. CHAR16 *driver_name;
  327. EFI_STATUS efirc;
  328. /* Sanity check */
  329. if ( ! wtf ) {
  330. DBG ( "[NULL ComponentName]" );
  331. return NULL;
  332. }
  333. /* Try "eng" first; if that fails then try the first language */
  334. if ( ( ( efirc = wtf->GetDriverName ( wtf, "eng",
  335. &driver_name ) ) != 0 ) &&
  336. ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
  337. &driver_name ) ) != 0 ) ) {
  338. return NULL;
  339. }
  340. /* Convert name from CHAR16 to char */
  341. snprintf ( name, sizeof ( name ), "%ls", driver_name );
  342. return name;
  343. }
  344. /**
  345. * Get driver name
  346. *
  347. * @v wtf Component name protocol
  348. * @ret name Driver name, or NULL
  349. */
  350. static const char * efi_driver_name2 ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
  351. static char name[64];
  352. CHAR16 *driver_name;
  353. EFI_STATUS efirc;
  354. /* Sanity check */
  355. if ( ! wtf ) {
  356. DBG ( "[NULL ComponentName2]" );
  357. return NULL;
  358. }
  359. /* Try "en" first; if that fails then try the first language */
  360. if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
  361. &driver_name ) ) != 0 ) &&
  362. ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
  363. &driver_name ) ) != 0 ) ) {
  364. return NULL;
  365. }
  366. /* Convert name from CHAR16 to char */
  367. snprintf ( name, sizeof ( name ), "%ls", driver_name );
  368. return name;
  369. }
  370. /**
  371. * Get PE/COFF debug filename
  372. *
  373. * @v loaded Loaded image
  374. * @ret name PE/COFF debug filename, or NULL
  375. */
  376. static const char *
  377. efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  378. static char buf[32];
  379. EFI_IMAGE_DOS_HEADER *dos;
  380. EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
  381. EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
  382. EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
  383. EFI_IMAGE_DATA_DIRECTORY *datadir;
  384. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *debug;
  385. EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *codeview_nb10;
  386. EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY *codeview_rsds;
  387. EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY *codeview_mtoc;
  388. uint16_t dos_magic;
  389. uint32_t pe_magic;
  390. uint16_t opt_magic;
  391. uint32_t codeview_magic;
  392. size_t max_len;
  393. char *name;
  394. char *tmp;
  395. /* Sanity check */
  396. if ( ! loaded ) {
  397. DBG ( "[NULL LoadedImage]" );
  398. return NULL;
  399. }
  400. /* Parse DOS header */
  401. dos = loaded->ImageBase;
  402. if ( ! dos ) {
  403. DBG ( "[Missing DOS header]" );
  404. return NULL;
  405. }
  406. dos_magic = dos->e_magic;
  407. if ( dos_magic != EFI_IMAGE_DOS_SIGNATURE ) {
  408. DBG ( "[Bad DOS signature %#04x]", dos_magic );
  409. return NULL;
  410. }
  411. pe = ( loaded->ImageBase + dos->e_lfanew );
  412. /* Parse PE header */
  413. pe_magic = pe->Pe32.Signature;
  414. if ( pe_magic != EFI_IMAGE_NT_SIGNATURE ) {
  415. DBG ( "[Bad PE signature %#08x]", pe_magic );
  416. return NULL;
  417. }
  418. opt32 = &pe->Pe32.OptionalHeader;
  419. opt64 = &pe->Pe32Plus.OptionalHeader;
  420. opt_magic = opt32->Magic;
  421. if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ) {
  422. datadir = opt32->DataDirectory;
  423. } else if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC ) {
  424. datadir = opt64->DataDirectory;
  425. } else {
  426. DBG ( "[Bad optional header signature %#04x]", opt_magic );
  427. return NULL;
  428. }
  429. /* Parse data directory entry */
  430. if ( ! datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress ) {
  431. DBG ( "[Empty debug directory entry]" );
  432. return NULL;
  433. }
  434. debug = ( loaded->ImageBase +
  435. datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress );
  436. /* Parse debug directory entry */
  437. if ( debug->Type != EFI_IMAGE_DEBUG_TYPE_CODEVIEW ) {
  438. DBG ( "[Not a CodeView debug directory entry (type %d)]",
  439. debug->Type );
  440. return NULL;
  441. }
  442. codeview_nb10 = ( loaded->ImageBase + debug->RVA );
  443. codeview_rsds = ( loaded->ImageBase + debug->RVA );
  444. codeview_mtoc = ( loaded->ImageBase + debug->RVA );
  445. codeview_magic = codeview_nb10->Signature;
  446. /* Parse CodeView entry */
  447. if ( codeview_magic == CODEVIEW_SIGNATURE_NB10 ) {
  448. name = ( ( void * ) ( codeview_nb10 + 1 ) );
  449. } else if ( codeview_magic == CODEVIEW_SIGNATURE_RSDS ) {
  450. name = ( ( void * ) ( codeview_rsds + 1 ) );
  451. } else if ( codeview_magic == CODEVIEW_SIGNATURE_MTOC ) {
  452. name = ( ( void * ) ( codeview_mtoc + 1 ) );
  453. } else {
  454. DBG ( "[Bad CodeView signature %#08x]", codeview_magic );
  455. return NULL;
  456. }
  457. /* Sanity check - avoid scanning endlessly through memory */
  458. max_len = EFI_PAGE_SIZE; /* Reasonably sane */
  459. if ( strnlen ( name, max_len ) == max_len ) {
  460. DBG ( "[Excessively long or invalid CodeView name]" );
  461. return NULL;
  462. }
  463. /* Skip any directory components. We cannot modify this data
  464. * or create a temporary buffer, so do not use basename().
  465. */
  466. while ( ( ( tmp = strchr ( name, '/' ) ) != NULL ) ||
  467. ( ( tmp = strchr ( name, '\\' ) ) != NULL ) ) {
  468. name = ( tmp + 1 );
  469. }
  470. /* Copy base name to buffer */
  471. snprintf ( buf, sizeof ( buf ), "%s", name );
  472. /* Strip file suffix, if present */
  473. if ( ( tmp = strrchr ( name, '.' ) ) != NULL )
  474. *tmp = '\0';
  475. return name;
  476. }
  477. /**
  478. * Get initial loaded image name
  479. *
  480. * @v loaded Loaded image
  481. * @ret name Initial loaded image name, or NULL
  482. */
  483. static const char *
  484. efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  485. /* Sanity check */
  486. if ( ! loaded ) {
  487. DBG ( "[NULL LoadedImage]" );
  488. return NULL;
  489. }
  490. return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
  491. }
  492. /**
  493. * Get loaded image name from file path
  494. *
  495. * @v loaded Loaded image
  496. * @ret name Loaded image name, or NULL
  497. */
  498. static const char *
  499. efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  500. /* Sanity check */
  501. if ( ! loaded ) {
  502. DBG ( "[NULL LoadedImage]" );
  503. return NULL;
  504. }
  505. return efi_devpath_text ( loaded->FilePath );
  506. }
  507. /** An EFI handle name type */
  508. struct efi_handle_name_type {
  509. /** Protocol */
  510. EFI_GUID *protocol;
  511. /**
  512. * Get name
  513. *
  514. * @v interface Protocol interface
  515. * @ret name Name of handle, or NULL on failure
  516. */
  517. const char * ( * name ) ( void *interface );
  518. };
  519. /**
  520. * Define an EFI handle name type
  521. *
  522. * @v protocol Protocol interface
  523. * @v name Method to get name
  524. * @ret type EFI handle name type
  525. */
  526. #define EFI_HANDLE_NAME_TYPE( protocol, name ) { \
  527. (protocol), \
  528. ( const char * ( * ) ( void * ) ) (name), \
  529. }
  530. /** EFI handle name types */
  531. static struct efi_handle_name_type efi_handle_name_types[] = {
  532. /* Device path */
  533. EFI_HANDLE_NAME_TYPE ( &efi_device_path_protocol_guid,
  534. efi_devpath_text ),
  535. /* Driver name (for driver image handles) */
  536. EFI_HANDLE_NAME_TYPE ( &efi_component_name2_protocol_guid,
  537. efi_driver_name2 ),
  538. /* Driver name (via obsolete original ComponentName protocol) */
  539. EFI_HANDLE_NAME_TYPE ( &efi_component_name_protocol_guid,
  540. efi_driver_name ),
  541. /* PE/COFF debug filename (for image handles) */
  542. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  543. efi_pecoff_debug_name ),
  544. /* Loaded image device path (for image handles) */
  545. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_device_path_protocol_guid,
  546. efi_devpath_text ),
  547. /* First loaded image name (for the DxeCore image) */
  548. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  549. efi_first_loaded_image_name ),
  550. /* Handle's loaded image file path (for image handles) */
  551. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  552. efi_loaded_image_filepath_name ),
  553. };
  554. /**
  555. * Get name of an EFI handle
  556. *
  557. * @v handle EFI handle
  558. * @ret text Name of handle, or NULL
  559. */
  560. const char * efi_handle_name ( EFI_HANDLE handle ) {
  561. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  562. struct efi_handle_name_type *type;
  563. unsigned int i;
  564. void *interface;
  565. const char *name;
  566. EFI_STATUS efirc;
  567. /* Fail immediately for NULL handles */
  568. if ( ! handle )
  569. return NULL;
  570. /* Try each name type in turn */
  571. for ( i = 0 ; i < ( sizeof ( efi_handle_name_types ) /
  572. sizeof ( efi_handle_name_types[0] ) ) ; i++ ) {
  573. type = &efi_handle_name_types[i];
  574. DBG2 ( "<%d", i );
  575. /* Try to open the applicable protocol */
  576. efirc = bs->OpenProtocol ( handle, type->protocol, &interface,
  577. efi_image_handle, handle,
  578. EFI_OPEN_PROTOCOL_GET_PROTOCOL );
  579. if ( efirc != 0 ) {
  580. DBG2 ( ">" );
  581. continue;
  582. }
  583. /* Try to get name from this protocol */
  584. DBG2 ( "-" );
  585. name = type->name ( interface );
  586. DBG2 ( "%c", ( name ? ( name[0] ? 'Y' : 'E' ) : 'N' ) );
  587. /* Close protocol */
  588. bs->CloseProtocol ( handle, type->protocol,
  589. efi_image_handle, handle );
  590. DBG2 ( ">" );
  591. /* Use this name, if possible */
  592. if ( name && name[0] )
  593. return name;
  594. }
  595. return "UNKNOWN";
  596. }