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