選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

efi_debug.c 20KB

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