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

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