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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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 %p could not retrieve protocols\n", handle );
  311. return;
  312. }
  313. /* Retrieve list of protocols */
  314. if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
  315. &count ) ) != 0 ) {
  316. rc = -EEFI ( efirc );
  317. printf ( "HANDLE %s could not retrieve protocols: %s\n",
  318. efi_handle_name ( handle ), strerror ( rc ) );
  319. return;
  320. }
  321. /* Dump list of protocols */
  322. for ( i = 0 ; i < count ; i++ ) {
  323. protocol = protocols[i];
  324. printf ( "HANDLE %s %s supported\n", efi_handle_name ( handle ),
  325. efi_guid_ntoa ( protocol ) );
  326. dbg_efi_openers ( handle, protocol );
  327. }
  328. /* Free list */
  329. bs->FreePool ( protocols );
  330. }
  331. /**
  332. * Get textual representation of device path
  333. *
  334. * @v path Device path
  335. * @ret text Textual representation of device path, or NULL
  336. */
  337. const __attribute__ (( pure )) char *
  338. efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
  339. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  340. static char text[256];
  341. size_t len;
  342. CHAR16 *wtext;
  343. /* Sanity checks */
  344. if ( ! path ) {
  345. DBG ( "[NULL DevicePath]" );
  346. return NULL;
  347. }
  348. /* If we have no DevicePathToText protocol then use a raw hex string */
  349. if ( ! efidpt ) {
  350. DBG ( "[No DevicePathToText]" );
  351. len = efi_devpath_len ( path );
  352. base16_encode ( path, len, text, sizeof ( text ) );
  353. return text;
  354. }
  355. /* Convert path to a textual representation */
  356. wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
  357. if ( ! wtext )
  358. return NULL;
  359. /* Store path in buffer */
  360. snprintf ( text, sizeof ( text ), "%ls", wtext );
  361. /* Free path */
  362. bs->FreePool ( wtext );
  363. return text;
  364. }
  365. /**
  366. * Get driver name
  367. *
  368. * @v wtf Component name protocol
  369. * @ret name Driver name, or NULL
  370. */
  371. static const char * efi_driver_name ( EFI_COMPONENT_NAME_PROTOCOL *wtf ) {
  372. static char name[64];
  373. CHAR16 *driver_name;
  374. EFI_STATUS efirc;
  375. /* Sanity check */
  376. if ( ! wtf ) {
  377. DBG ( "[NULL ComponentName]" );
  378. return NULL;
  379. }
  380. /* Try "eng" first; if that fails then try the first language */
  381. if ( ( ( efirc = wtf->GetDriverName ( wtf, "eng",
  382. &driver_name ) ) != 0 ) &&
  383. ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
  384. &driver_name ) ) != 0 ) ) {
  385. return NULL;
  386. }
  387. /* Convert name from CHAR16 to char */
  388. snprintf ( name, sizeof ( name ), "%ls", driver_name );
  389. return name;
  390. }
  391. /**
  392. * Get driver name
  393. *
  394. * @v wtf Component name protocol
  395. * @ret name Driver name, or NULL
  396. */
  397. static const char * efi_driver_name2 ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
  398. static char name[64];
  399. CHAR16 *driver_name;
  400. EFI_STATUS efirc;
  401. /* Sanity check */
  402. if ( ! wtf ) {
  403. DBG ( "[NULL ComponentName2]" );
  404. return NULL;
  405. }
  406. /* Try "en" first; if that fails then try the first language */
  407. if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
  408. &driver_name ) ) != 0 ) &&
  409. ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
  410. &driver_name ) ) != 0 ) ) {
  411. return NULL;
  412. }
  413. /* Convert name from CHAR16 to char */
  414. snprintf ( name, sizeof ( name ), "%ls", driver_name );
  415. return name;
  416. }
  417. /**
  418. * Get PE/COFF debug filename
  419. *
  420. * @v loaded Loaded image
  421. * @ret name PE/COFF debug filename, or NULL
  422. */
  423. static const char *
  424. efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  425. static char buf[32];
  426. EFI_IMAGE_DOS_HEADER *dos;
  427. EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
  428. EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
  429. EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
  430. EFI_IMAGE_DATA_DIRECTORY *datadir;
  431. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *debug;
  432. EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *codeview_nb10;
  433. EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY *codeview_rsds;
  434. EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY *codeview_mtoc;
  435. uint16_t dos_magic;
  436. uint32_t pe_magic;
  437. uint16_t opt_magic;
  438. uint32_t codeview_magic;
  439. size_t max_len;
  440. char *name;
  441. char *tmp;
  442. /* Sanity check */
  443. if ( ! loaded ) {
  444. DBG ( "[NULL LoadedImage]" );
  445. return NULL;
  446. }
  447. /* Parse DOS header */
  448. dos = loaded->ImageBase;
  449. if ( ! dos ) {
  450. DBG ( "[Missing DOS header]" );
  451. return NULL;
  452. }
  453. dos_magic = dos->e_magic;
  454. if ( dos_magic != EFI_IMAGE_DOS_SIGNATURE ) {
  455. DBG ( "[Bad DOS signature %#04x]", dos_magic );
  456. return NULL;
  457. }
  458. pe = ( loaded->ImageBase + dos->e_lfanew );
  459. /* Parse PE header */
  460. pe_magic = pe->Pe32.Signature;
  461. if ( pe_magic != EFI_IMAGE_NT_SIGNATURE ) {
  462. DBG ( "[Bad PE signature %#08x]", pe_magic );
  463. return NULL;
  464. }
  465. opt32 = &pe->Pe32.OptionalHeader;
  466. opt64 = &pe->Pe32Plus.OptionalHeader;
  467. opt_magic = opt32->Magic;
  468. if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ) {
  469. datadir = opt32->DataDirectory;
  470. } else if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC ) {
  471. datadir = opt64->DataDirectory;
  472. } else {
  473. DBG ( "[Bad optional header signature %#04x]", opt_magic );
  474. return NULL;
  475. }
  476. /* Parse data directory entry */
  477. if ( ! datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress ) {
  478. DBG ( "[Empty debug directory entry]" );
  479. return NULL;
  480. }
  481. debug = ( loaded->ImageBase +
  482. datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress );
  483. /* Parse debug directory entry */
  484. if ( debug->Type != EFI_IMAGE_DEBUG_TYPE_CODEVIEW ) {
  485. DBG ( "[Not a CodeView debug directory entry (type %d)]",
  486. debug->Type );
  487. return NULL;
  488. }
  489. codeview_nb10 = ( loaded->ImageBase + debug->RVA );
  490. codeview_rsds = ( loaded->ImageBase + debug->RVA );
  491. codeview_mtoc = ( loaded->ImageBase + debug->RVA );
  492. codeview_magic = codeview_nb10->Signature;
  493. /* Parse CodeView entry */
  494. if ( codeview_magic == CODEVIEW_SIGNATURE_NB10 ) {
  495. name = ( ( void * ) ( codeview_nb10 + 1 ) );
  496. } else if ( codeview_magic == CODEVIEW_SIGNATURE_RSDS ) {
  497. name = ( ( void * ) ( codeview_rsds + 1 ) );
  498. } else if ( codeview_magic == CODEVIEW_SIGNATURE_MTOC ) {
  499. name = ( ( void * ) ( codeview_mtoc + 1 ) );
  500. } else {
  501. DBG ( "[Bad CodeView signature %#08x]", codeview_magic );
  502. return NULL;
  503. }
  504. /* Sanity check - avoid scanning endlessly through memory */
  505. max_len = EFI_PAGE_SIZE; /* Reasonably sane */
  506. if ( strnlen ( name, max_len ) == max_len ) {
  507. DBG ( "[Excessively long or invalid CodeView name]" );
  508. return NULL;
  509. }
  510. /* Skip any directory components. We cannot modify this data
  511. * or create a temporary buffer, so do not use basename().
  512. */
  513. while ( ( ( tmp = strchr ( name, '/' ) ) != NULL ) ||
  514. ( ( tmp = strchr ( name, '\\' ) ) != NULL ) ) {
  515. name = ( tmp + 1 );
  516. }
  517. /* Copy base name to buffer */
  518. snprintf ( buf, sizeof ( buf ), "%s", name );
  519. /* Strip file suffix, if present */
  520. if ( ( tmp = strrchr ( name, '.' ) ) != NULL )
  521. *tmp = '\0';
  522. return name;
  523. }
  524. /**
  525. * Get initial loaded image name
  526. *
  527. * @v loaded Loaded image
  528. * @ret name Initial loaded image name, or NULL
  529. */
  530. static const char *
  531. efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  532. /* Sanity check */
  533. if ( ! loaded ) {
  534. DBG ( "[NULL LoadedImage]" );
  535. return NULL;
  536. }
  537. return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
  538. }
  539. /**
  540. * Get loaded image name from file path
  541. *
  542. * @v loaded Loaded image
  543. * @ret name Loaded image name, or NULL
  544. */
  545. static const char *
  546. efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
  547. /* Sanity check */
  548. if ( ! loaded ) {
  549. DBG ( "[NULL LoadedImage]" );
  550. return NULL;
  551. }
  552. return efi_devpath_text ( loaded->FilePath );
  553. }
  554. /**
  555. * Get console input handle name
  556. *
  557. * @v input Simple text input protocol
  558. * @ret name Console input handle name, or NULL
  559. */
  560. static const char *
  561. efi_conin_name ( EFI_SIMPLE_TEXT_INPUT_PROTOCOL *input ) {
  562. /* Check for match against ConIn */
  563. if ( input == efi_systab->ConIn )
  564. return "ConIn";
  565. return NULL;
  566. }
  567. /**
  568. * Get console output handle name
  569. *
  570. * @v output Simple text output protocol
  571. * @ret name Console output handle name, or NULL
  572. */
  573. static const char *
  574. efi_conout_name ( EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *output ) {
  575. /* Check for match against ConOut */
  576. if ( output == efi_systab->ConOut )
  577. return "ConOut";
  578. /* Check for match against StdErr (if different from ConOut) */
  579. if ( output == efi_systab->StdErr )
  580. return "StdErr";
  581. return NULL;
  582. }
  583. /** An EFI handle name type */
  584. struct efi_handle_name_type {
  585. /** Protocol */
  586. EFI_GUID *protocol;
  587. /**
  588. * Get name
  589. *
  590. * @v interface Protocol interface
  591. * @ret name Name of handle, or NULL on failure
  592. */
  593. const char * ( * name ) ( void *interface );
  594. };
  595. /**
  596. * Define an EFI handle name type
  597. *
  598. * @v protocol Protocol interface
  599. * @v name Method to get name
  600. * @ret type EFI handle name type
  601. */
  602. #define EFI_HANDLE_NAME_TYPE( protocol, name ) { \
  603. (protocol), \
  604. ( const char * ( * ) ( void * ) ) (name), \
  605. }
  606. /** EFI handle name types */
  607. static struct efi_handle_name_type efi_handle_name_types[] = {
  608. /* Device path */
  609. EFI_HANDLE_NAME_TYPE ( &efi_device_path_protocol_guid,
  610. efi_devpath_text ),
  611. /* Driver name (for driver image handles) */
  612. EFI_HANDLE_NAME_TYPE ( &efi_component_name2_protocol_guid,
  613. efi_driver_name2 ),
  614. /* Driver name (via obsolete original ComponentName protocol) */
  615. EFI_HANDLE_NAME_TYPE ( &efi_component_name_protocol_guid,
  616. efi_driver_name ),
  617. /* PE/COFF debug filename (for image handles) */
  618. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  619. efi_pecoff_debug_name ),
  620. /* Loaded image device path (for image handles) */
  621. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_device_path_protocol_guid,
  622. efi_devpath_text ),
  623. /* First loaded image name (for the DxeCore image) */
  624. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  625. efi_first_loaded_image_name ),
  626. /* Handle's loaded image file path (for image handles) */
  627. EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
  628. efi_loaded_image_filepath_name ),
  629. /* Our standard input file handle */
  630. EFI_HANDLE_NAME_TYPE ( &efi_simple_text_input_protocol_guid,
  631. efi_conin_name ),
  632. /* Our standard output and standard error file handles */
  633. EFI_HANDLE_NAME_TYPE ( &efi_simple_text_output_protocol_guid,
  634. efi_conout_name ),
  635. };
  636. /**
  637. * Get name of an EFI handle
  638. *
  639. * @v handle EFI handle
  640. * @ret text Name of handle, or NULL
  641. */
  642. const __attribute__ (( pure )) char * efi_handle_name ( EFI_HANDLE handle ) {
  643. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  644. struct efi_handle_name_type *type;
  645. static char buf[256];
  646. size_t used = 0;
  647. EFI_GUID **protocols;
  648. UINTN count;
  649. unsigned int i;
  650. void *interface;
  651. const char *name;
  652. EFI_STATUS efirc;
  653. /* Fail immediately for NULL handles */
  654. if ( ! handle )
  655. return NULL;
  656. /* Try each name type in turn */
  657. for ( i = 0 ; i < ( sizeof ( efi_handle_name_types ) /
  658. sizeof ( efi_handle_name_types[0] ) ) ; i++ ) {
  659. type = &efi_handle_name_types[i];
  660. DBG2 ( "<%d", i );
  661. /* Try to open the applicable protocol */
  662. efirc = bs->OpenProtocol ( handle, type->protocol, &interface,
  663. efi_image_handle, handle,
  664. EFI_OPEN_PROTOCOL_GET_PROTOCOL );
  665. if ( efirc != 0 ) {
  666. DBG2 ( ">" );
  667. continue;
  668. }
  669. /* Try to get name from this protocol */
  670. DBG2 ( "-" );
  671. name = type->name ( interface );
  672. DBG2 ( "%c", ( name ? ( name[0] ? 'Y' : 'E' ) : 'N' ) );
  673. /* Close protocol */
  674. bs->CloseProtocol ( handle, type->protocol,
  675. efi_image_handle, handle );
  676. DBG2 ( ">" );
  677. /* Use this name, if possible */
  678. if ( name && name[0] )
  679. return name;
  680. }
  681. /* If no name is found, then use the raw handle value and a
  682. * list of installed protocols.
  683. */
  684. used = ssnprintf ( buf, sizeof ( buf ), "UNKNOWN<%p", handle );
  685. if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
  686. &count ) ) == 0 ) {
  687. for ( i = 0 ; i < count ; i++ ) {
  688. used += ssnprintf ( ( buf + used ),
  689. ( sizeof ( buf ) - used ), ",%s",
  690. efi_guid_ntoa ( protocols[i] ) );
  691. }
  692. bs->FreePool ( protocols );
  693. }
  694. used += ssnprintf ( ( buf + used ), ( sizeof ( buf ) - used ), ">" );
  695. return buf;
  696. }