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

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