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

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