2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 * EFI debugging utilities
36 #include <ipxe/uuid.h>
37 #include <ipxe/base16.h>
38 #include <ipxe/efi/efi.h>
39 #include <ipxe/efi/efi_utils.h>
40 #include <ipxe/efi/Protocol/ComponentName.h>
41 #include <ipxe/efi/Protocol/ComponentName2.h>
42 #include <ipxe/efi/Protocol/DevicePathToText.h>
43 #include <ipxe/efi/IndustryStandard/PeImage.h>
45 /** Device path to text protocol */
46 static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *efidpt;
47 EFI_REQUEST_PROTOCOL ( EFI_DEVICE_PATH_TO_TEXT_PROTOCOL, &efidpt );
49 /** Iscsi4Dxe module GUID */
50 static EFI_GUID efi_iscsi4_dxe_guid = {
51 0x4579b72d, 0x7ec4, 0x4dd4,
52 { 0x84, 0x86, 0x08, 0x3c, 0x86, 0xb1, 0x82, 0xa7 }
55 /** VlanConfigDxe module GUID */
56 static EFI_GUID efi_vlan_config_dxe_guid = {
57 0xe4f61863, 0xfe2c, 0x4b56,
58 { 0xa8, 0xf4, 0x08, 0x51, 0x9b, 0xc4, 0x39, 0xdf }
61 /** A well-known GUID */
62 struct efi_well_known_guid {
69 /** Well-known GUIDs */
70 static struct efi_well_known_guid efi_well_known_guids[] = {
71 { &efi_arp_protocol_guid,
73 { &efi_arp_service_binding_protocol_guid,
75 { &efi_block_io_protocol_guid,
77 { &efi_bus_specific_driver_override_protocol_guid,
78 "BusSpecificDriverOverride" },
79 { &efi_component_name_protocol_guid,
81 { &efi_component_name2_protocol_guid,
83 { &efi_device_path_protocol_guid,
85 { &efi_driver_binding_protocol_guid,
87 { &efi_dhcp4_protocol_guid,
89 { &efi_dhcp4_service_binding_protocol_guid,
91 { &efi_disk_io_protocol_guid,
93 { &efi_graphics_output_protocol_guid,
95 { &efi_hii_config_access_protocol_guid,
97 { &efi_ip4_protocol_guid,
99 { &efi_ip4_config_protocol_guid,
101 { &efi_ip4_service_binding_protocol_guid,
103 { &efi_iscsi4_dxe_guid,
105 { &efi_load_file_protocol_guid,
107 { &efi_load_file2_protocol_guid,
109 { &efi_loaded_image_protocol_guid,
111 { &efi_loaded_image_device_path_protocol_guid,
112 "LoadedImageDevicePath"},
113 { &efi_managed_network_protocol_guid,
115 { &efi_managed_network_service_binding_protocol_guid,
116 "ManagedNetworkSb" },
117 { &efi_mtftp4_protocol_guid,
119 { &efi_mtftp4_service_binding_protocol_guid,
121 { &efi_nii_protocol_guid,
123 { &efi_nii31_protocol_guid,
125 { &efi_pci_io_protocol_guid,
127 { &efi_pci_root_bridge_io_protocol_guid,
129 { &efi_pxe_base_code_protocol_guid,
131 { &efi_simple_file_system_protocol_guid,
132 "SimpleFileSystem" },
133 { &efi_simple_network_protocol_guid,
135 { &efi_tcg_protocol_guid,
137 { &efi_tcp4_protocol_guid,
139 { &efi_tcp4_service_binding_protocol_guid,
141 { &efi_udp4_protocol_guid,
143 { &efi_udp4_service_binding_protocol_guid,
145 { &efi_vlan_config_protocol_guid,
147 { &efi_vlan_config_dxe_guid,
152 * Convert GUID to a printable string
155 * @ret string Printable string
157 const char * efi_guid_ntoa ( EFI_GUID *guid ) {
168 /* Check for a match against well-known GUIDs */
169 for ( i = 0 ; i < ( sizeof ( efi_well_known_guids ) /
170 sizeof ( efi_well_known_guids[0] ) ) ; i++ ) {
171 if ( memcmp ( guid, efi_well_known_guids[i].guid,
172 sizeof ( *guid ) ) == 0 ) {
173 return efi_well_known_guids[i].name;
177 /* Convert GUID to standard endianness */
178 memcpy ( &u.guid, guid, sizeof ( u.guid ) );
179 uuid_mangle ( &u.uuid );
180 return uuid_ntoa ( &u.uuid );
184 * Name protocol open attributes
186 * @v attributes Protocol open attributes
187 * @ret name Protocol open attributes name
189 * Returns a (static) string with characters for each set bit
190 * corresponding to BY_(H)ANDLE_PROTOCOL, (G)ET_PROTOCOL,
191 * (T)EST_PROTOCOL, BY_(C)HILD_CONTROLLER, BY_(D)RIVER, and
194 static const char * efi_open_attributes_name ( unsigned int attributes ) {
195 static char attribute_chars[] = "HGTCDX";
196 static char name[ sizeof ( attribute_chars ) ];
200 for ( i = 0 ; i < ( sizeof ( attribute_chars ) - 1 ) ; i++ ) {
201 if ( attributes & ( 1 << i ) )
202 *(tmp++) = attribute_chars[i];
210 * Print list of openers of a given protocol on a given handle
212 * @v handle EFI handle
213 * @v protocol Protocol GUID
215 void dbg_efi_openers ( EFI_HANDLE handle, EFI_GUID *protocol ) {
216 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
217 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *openers;
218 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *opener;
225 if ( ( ! handle ) || ( ! protocol ) ) {
226 printf ( "EFI could not retrieve openers for %s on %p\n",
227 efi_guid_ntoa ( protocol ), handle );
231 /* Retrieve list of openers */
232 if ( ( efirc = bs->OpenProtocolInformation ( handle, protocol, &openers,
234 rc = -EEFI ( efirc );
235 printf ( "EFI could not retrieve openers for %s on %p: %s\n",
236 efi_guid_ntoa ( protocol ), handle, strerror ( rc ) );
240 /* Dump list of openers */
241 for ( i = 0 ; i < count ; i++ ) {
242 opener = &openers[i];
243 printf ( "HANDLE %p %s %s opened %dx (%s)",
244 handle, efi_handle_name ( handle ),
245 efi_guid_ntoa ( protocol ), opener->OpenCount,
246 efi_open_attributes_name ( opener->Attributes ) );
247 printf ( " by %p %s", opener->AgentHandle,
248 efi_handle_name ( opener->AgentHandle ) );
249 if ( opener->ControllerHandle == handle ) {
252 printf ( " for %p %s\n", opener->ControllerHandle,
253 efi_handle_name ( opener->ControllerHandle ) );
258 bs->FreePool ( openers );
262 * Print list of protocol handlers attached to a handle
264 * @v handle EFI handle
266 void dbg_efi_protocols ( EFI_HANDLE handle ) {
267 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
268 EFI_GUID **protocols;
277 printf ( "EFI could not retrieve protocols for %p\n", handle );
281 /* Retrieve list of protocols */
282 if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
284 rc = -EEFI ( efirc );
285 printf ( "EFI could not retrieve protocols for %p: %s\n",
286 handle, strerror ( rc ) );
290 /* Dump list of protocols */
291 for ( i = 0 ; i < count ; i++ ) {
292 protocol = protocols[i];
293 printf ( "HANDLE %p %s %s supported\n",
294 handle, efi_handle_name ( handle ),
295 efi_guid_ntoa ( protocol ) );
296 dbg_efi_openers ( handle, protocol );
300 bs->FreePool ( protocols );
304 * Get textual representation of device path
306 * @v path Device path
307 * @ret text Textual representation of device path, or NULL
309 const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
310 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
311 static char text[256];
320 DBG ( "[NULL DevicePath]" );
324 /* If we have no DevicePathToText protocol then use a raw hex string */
326 DBG ( "[No DevicePathToText]" );
328 end = efi_devpath_end ( path );
329 len = ( end - start );
330 max_len = ( ( sizeof ( text ) - 1 /* NUL */ ) / 2 /* "xx" */ );
333 base16_encode ( start, len, text, sizeof ( text ) );
337 /* Convert path to a textual representation */
338 wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
342 /* Store path in buffer */
343 snprintf ( text, sizeof ( text ), "%ls", wtext );
346 bs->FreePool ( wtext );
354 * @v wtf Component name protocol
355 * @ret name Driver name, or NULL
357 static const char * efi_driver_name ( EFI_COMPONENT_NAME_PROTOCOL *wtf ) {
358 static char name[64];
364 DBG ( "[NULL ComponentName]" );
368 /* Try "eng" first; if that fails then try the first language */
369 if ( ( ( efirc = wtf->GetDriverName ( wtf, "eng",
370 &driver_name ) ) != 0 ) &&
371 ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
372 &driver_name ) ) != 0 ) ) {
376 /* Convert name from CHAR16 to char */
377 snprintf ( name, sizeof ( name ), "%ls", driver_name );
384 * @v wtf Component name protocol
385 * @ret name Driver name, or NULL
387 static const char * efi_driver_name2 ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
388 static char name[64];
394 DBG ( "[NULL ComponentName2]" );
398 /* Try "en" first; if that fails then try the first language */
399 if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
400 &driver_name ) ) != 0 ) &&
401 ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
402 &driver_name ) ) != 0 ) ) {
406 /* Convert name from CHAR16 to char */
407 snprintf ( name, sizeof ( name ), "%ls", driver_name );
412 * Get PE/COFF debug filename
414 * @v loaded Loaded image
415 * @ret name PE/COFF debug filename, or NULL
418 efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
420 EFI_IMAGE_DOS_HEADER *dos;
421 EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
422 EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
423 EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
424 EFI_IMAGE_DATA_DIRECTORY *datadir;
425 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *debug;
426 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *codeview_nb10;
427 EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY *codeview_rsds;
428 EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY *codeview_mtoc;
432 uint32_t codeview_magic;
439 DBG ( "[NULL LoadedImage]" );
443 /* Parse DOS header */
444 dos = loaded->ImageBase;
446 DBG ( "[Missing DOS header]" );
449 dos_magic = dos->e_magic;
450 if ( dos_magic != EFI_IMAGE_DOS_SIGNATURE ) {
451 DBG ( "[Bad DOS signature %#04x]", dos_magic );
454 pe = ( loaded->ImageBase + dos->e_lfanew );
456 /* Parse PE header */
457 pe_magic = pe->Pe32.Signature;
458 if ( pe_magic != EFI_IMAGE_NT_SIGNATURE ) {
459 DBG ( "[Bad PE signature %#08x]", pe_magic );
462 opt32 = &pe->Pe32.OptionalHeader;
463 opt64 = &pe->Pe32Plus.OptionalHeader;
464 opt_magic = opt32->Magic;
465 if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ) {
466 datadir = opt32->DataDirectory;
467 } else if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC ) {
468 datadir = opt64->DataDirectory;
470 DBG ( "[Bad optional header signature %#04x]", opt_magic );
474 /* Parse data directory entry */
475 if ( ! datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress ) {
476 DBG ( "[Empty debug directory entry]" );
479 debug = ( loaded->ImageBase +
480 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)]",
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;
493 /* Parse CodeView entry */
494 if ( codeview_magic == CODEVIEW_SIGNATURE_NB10 ) {
495 name = ( ( void * ) ( codeview_nb10 + 1 ) );
496 } else if ( codeview_magic == CODEVIEW_SIGNATURE_RSDS ) {
497 name = ( ( void * ) ( codeview_rsds + 1 ) );
498 } else if ( codeview_magic == CODEVIEW_SIGNATURE_MTOC ) {
499 name = ( ( void * ) ( codeview_mtoc + 1 ) );
501 DBG ( "[Bad CodeView signature %#08x]", codeview_magic );
505 /* Sanity check - avoid scanning endlessly through memory */
506 max_len = EFI_PAGE_SIZE; /* Reasonably sane */
507 if ( strnlen ( name, max_len ) == max_len ) {
508 DBG ( "[Excessively long or invalid CodeView name]" );
512 /* Skip any directory components. We cannot modify this data
513 * or create a temporary buffer, so do not use basename().
515 while ( ( ( tmp = strchr ( name, '/' ) ) != NULL ) ||
516 ( ( tmp = strchr ( name, '\\' ) ) != NULL ) ) {
520 /* Copy base name to buffer */
521 snprintf ( buf, sizeof ( buf ), "%s", name );
523 /* Strip file suffix, if present */
524 if ( ( tmp = strrchr ( name, '.' ) ) != NULL )
531 * Get initial loaded image name
533 * @v loaded Loaded image
534 * @ret name Initial loaded image name, or NULL
537 efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
541 DBG ( "[NULL LoadedImage]" );
545 return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
549 * Get loaded image name from file path
551 * @v loaded Loaded image
552 * @ret name Loaded image name, or NULL
555 efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
559 DBG ( "[NULL LoadedImage]" );
563 return efi_devpath_text ( loaded->FilePath );
566 /** An EFI handle name type */
567 struct efi_handle_name_type {
573 * @v interface Protocol interface
574 * @ret name Name of handle, or NULL on failure
576 const char * ( * name ) ( void *interface );
580 * Define an EFI handle name type
582 * @v protocol Protocol interface
583 * @v name Method to get name
584 * @ret type EFI handle name type
586 #define EFI_HANDLE_NAME_TYPE( protocol, name ) { \
588 ( const char * ( * ) ( void * ) ) (name), \
591 /** EFI handle name types */
592 static struct efi_handle_name_type efi_handle_name_types[] = {
594 EFI_HANDLE_NAME_TYPE ( &efi_device_path_protocol_guid,
596 /* Driver name (for driver image handles) */
597 EFI_HANDLE_NAME_TYPE ( &efi_component_name2_protocol_guid,
599 /* Driver name (via obsolete original ComponentName protocol) */
600 EFI_HANDLE_NAME_TYPE ( &efi_component_name_protocol_guid,
602 /* PE/COFF debug filename (for image handles) */
603 EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
604 efi_pecoff_debug_name ),
605 /* Loaded image device path (for image handles) */
606 EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_device_path_protocol_guid,
608 /* First loaded image name (for the DxeCore image) */
609 EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
610 efi_first_loaded_image_name ),
611 /* Handle's loaded image file path (for image handles) */
612 EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
613 efi_loaded_image_filepath_name ),
617 * Get name of an EFI handle
619 * @v handle EFI handle
620 * @ret text Name of handle, or NULL
622 const char * efi_handle_name ( EFI_HANDLE handle ) {
623 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
624 struct efi_handle_name_type *type;
630 /* Fail immediately for NULL handles */
634 /* Try each name type in turn */
635 for ( i = 0 ; i < ( sizeof ( efi_handle_name_types ) /
636 sizeof ( efi_handle_name_types[0] ) ) ; i++ ) {
637 type = &efi_handle_name_types[i];
640 /* Try to open the applicable protocol */
641 efirc = bs->OpenProtocol ( handle, type->protocol, &interface,
642 efi_image_handle, handle,
643 EFI_OPEN_PROTOCOL_GET_PROTOCOL );
649 /* Try to get name from this protocol */
651 name = type->name ( interface );
652 DBG2 ( "%c", ( name ? ( name[0] ? 'Y' : 'E' ) : 'N' ) );
655 bs->CloseProtocol ( handle, type->protocol,
656 efi_image_handle, handle );
659 /* Use this name, if possible */
660 if ( name && name[0] )