Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

efi_debug.c 20KB

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