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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 * EFI debugging utilities
32 #include <ipxe/uuid.h>
33 #include <ipxe/base16.h>
34 #include <ipxe/efi/efi.h>
35 #include <ipxe/efi/efi_utils.h>
36 #include <ipxe/efi/Protocol/ComponentName.h>
37 #include <ipxe/efi/Protocol/ComponentName2.h>
38 #include <ipxe/efi/Protocol/DevicePathToText.h>
39 #include <ipxe/efi/IndustryStandard/PeImage.h>
41 /** Device path to text protocol */
42 static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *efidpt;
43 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 }
51 /** VlanConfigDxe module GUID */
52 static EFI_GUID efi_vlan_config_dxe_guid = {
53 0xe4f61863, 0xfe2c, 0x4b56,
54 { 0xa8, 0xf4, 0x08, 0x51, 0x9b, 0xc4, 0x39, 0xdf }
57 /** A well-known GUID */
58 struct efi_well_known_guid {
65 /** Well-known GUIDs */
66 static struct efi_well_known_guid efi_well_known_guids[] = {
67 { &efi_arp_protocol_guid,
69 { &efi_arp_service_binding_protocol_guid,
71 { &efi_block_io_protocol_guid,
73 { &efi_bus_specific_driver_override_protocol_guid,
74 "BusSpecificDriverOverride" },
75 { &efi_component_name_protocol_guid,
77 { &efi_component_name2_protocol_guid,
79 { &efi_device_path_protocol_guid,
81 { &efi_driver_binding_protocol_guid,
83 { &efi_dhcp4_protocol_guid,
85 { &efi_dhcp4_service_binding_protocol_guid,
87 { &efi_disk_io_protocol_guid,
89 { &efi_graphics_output_protocol_guid,
91 { &efi_hii_config_access_protocol_guid,
93 { &efi_ip4_protocol_guid,
95 { &efi_ip4_config_protocol_guid,
97 { &efi_ip4_service_binding_protocol_guid,
99 { &efi_iscsi4_dxe_guid,
101 { &efi_load_file_protocol_guid,
103 { &efi_load_file2_protocol_guid,
105 { &efi_loaded_image_protocol_guid,
107 { &efi_loaded_image_device_path_protocol_guid,
108 "LoadedImageDevicePath"},
109 { &efi_managed_network_protocol_guid,
111 { &efi_managed_network_service_binding_protocol_guid,
112 "ManagedNetworkSb" },
113 { &efi_mtftp4_protocol_guid,
115 { &efi_mtftp4_service_binding_protocol_guid,
117 { &efi_nii_protocol_guid,
119 { &efi_nii31_protocol_guid,
121 { &efi_pci_io_protocol_guid,
123 { &efi_pci_root_bridge_io_protocol_guid,
125 { &efi_pxe_base_code_protocol_guid,
127 { &efi_simple_file_system_protocol_guid,
128 "SimpleFileSystem" },
129 { &efi_simple_network_protocol_guid,
131 { &efi_tcg_protocol_guid,
133 { &efi_tcp4_protocol_guid,
135 { &efi_tcp4_service_binding_protocol_guid,
137 { &efi_udp4_protocol_guid,
139 { &efi_udp4_service_binding_protocol_guid,
141 { &efi_vlan_config_protocol_guid,
143 { &efi_vlan_config_dxe_guid,
148 * Convert GUID to a printable string
151 * @ret string Printable string
153 const char * efi_guid_ntoa ( EFI_GUID *guid ) {
164 /* Check for a match against well-known GUIDs */
165 for ( i = 0 ; i < ( sizeof ( efi_well_known_guids ) /
166 sizeof ( efi_well_known_guids[0] ) ) ; i++ ) {
167 if ( memcmp ( guid, efi_well_known_guids[i].guid,
168 sizeof ( *guid ) ) == 0 ) {
169 return efi_well_known_guids[i].name;
173 /* Convert GUID to standard endianness */
174 memcpy ( &u.guid, guid, sizeof ( u.guid ) );
175 uuid_mangle ( &u.uuid );
176 return uuid_ntoa ( &u.uuid );
180 * Name protocol open attributes
182 * @v attributes Protocol open attributes
183 * @ret name Protocol open attributes name
185 * Returns a (static) string with characters for each set bit
186 * corresponding to BY_(H)ANDLE_PROTOCOL, (G)ET_PROTOCOL,
187 * (T)EST_PROTOCOL, BY_(C)HILD_CONTROLLER, BY_(D)RIVER, and
190 static const char * efi_open_attributes_name ( unsigned int attributes ) {
191 static char attribute_chars[] = "HGTCDX";
192 static char name[ sizeof ( attribute_chars ) ];
196 for ( i = 0 ; i < ( sizeof ( attribute_chars ) - 1 ) ; i++ ) {
197 if ( attributes & ( 1 << i ) )
198 *(tmp++) = attribute_chars[i];
206 * Print list of openers of a given protocol on a given handle
208 * @v handle EFI handle
209 * @v protocol Protocol GUID
211 void dbg_efi_openers ( EFI_HANDLE handle, EFI_GUID *protocol ) {
212 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
213 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *openers;
214 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *opener;
221 if ( ( ! handle ) || ( ! protocol ) ) {
222 printf ( "EFI could not retrieve openers for %s on %p\n",
223 efi_guid_ntoa ( protocol ), handle );
227 /* Retrieve list of openers */
228 if ( ( efirc = bs->OpenProtocolInformation ( handle, protocol, &openers,
230 rc = -EEFI ( efirc );
231 printf ( "EFI could not retrieve openers for %s on %p: %s\n",
232 efi_guid_ntoa ( protocol ), handle, strerror ( rc ) );
236 /* Dump list of openers */
237 for ( i = 0 ; i < count ; i++ ) {
238 opener = &openers[i];
239 printf ( "HANDLE %p %s %s opened %dx (%s)",
240 handle, efi_handle_name ( handle ),
241 efi_guid_ntoa ( protocol ), opener->OpenCount,
242 efi_open_attributes_name ( opener->Attributes ) );
243 printf ( " by %p %s", opener->AgentHandle,
244 efi_handle_name ( opener->AgentHandle ) );
245 if ( opener->ControllerHandle == handle ) {
248 printf ( " for %p %s\n", opener->ControllerHandle,
249 efi_handle_name ( opener->ControllerHandle ) );
254 bs->FreePool ( openers );
258 * Print list of protocol handlers attached to a handle
260 * @v handle EFI handle
262 void dbg_efi_protocols ( EFI_HANDLE handle ) {
263 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
264 EFI_GUID **protocols;
273 printf ( "EFI could not retrieve protocols for %p\n", handle );
277 /* Retrieve list of protocols */
278 if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
280 rc = -EEFI ( efirc );
281 printf ( "EFI could not retrieve protocols for %p: %s\n",
282 handle, strerror ( rc ) );
286 /* Dump list of protocols */
287 for ( i = 0 ; i < count ; i++ ) {
288 protocol = protocols[i];
289 printf ( "HANDLE %p %s %s supported\n",
290 handle, efi_handle_name ( handle ),
291 efi_guid_ntoa ( protocol ) );
292 dbg_efi_openers ( handle, protocol );
296 bs->FreePool ( protocols );
300 * Get textual representation of device path
302 * @v path Device path
303 * @ret text Textual representation of device path, or NULL
305 const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
306 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
307 static char text[256];
316 DBG ( "[NULL DevicePath]" );
320 /* If we have no DevicePathToText protocol then use a raw hex string */
322 DBG ( "[No DevicePathToText]" );
324 end = efi_devpath_end ( path );
325 len = ( end - start );
326 max_len = ( ( sizeof ( text ) - 1 /* NUL */ ) / 2 /* "xx" */ );
329 base16_encode ( start, len, text );
333 /* Convert path to a textual representation */
334 wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
338 /* Store path in buffer */
339 snprintf ( text, sizeof ( text ), "%ls", wtext );
342 bs->FreePool ( wtext );
350 * @v wtf Component name protocol
351 * @ret name Driver name, or NULL
353 static const char * efi_driver_name ( EFI_COMPONENT_NAME_PROTOCOL *wtf ) {
354 static char name[64];
360 DBG ( "[NULL ComponentName]" );
364 /* Try "eng" first; if that fails then try the first language */
365 if ( ( ( efirc = wtf->GetDriverName ( wtf, "eng",
366 &driver_name ) ) != 0 ) &&
367 ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
368 &driver_name ) ) != 0 ) ) {
372 /* Convert name from CHAR16 to char */
373 snprintf ( name, sizeof ( name ), "%ls", driver_name );
380 * @v wtf Component name protocol
381 * @ret name Driver name, or NULL
383 static const char * efi_driver_name2 ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
384 static char name[64];
390 DBG ( "[NULL ComponentName2]" );
394 /* Try "en" first; if that fails then try the first language */
395 if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
396 &driver_name ) ) != 0 ) &&
397 ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
398 &driver_name ) ) != 0 ) ) {
402 /* Convert name from CHAR16 to char */
403 snprintf ( name, sizeof ( name ), "%ls", driver_name );
408 * Get PE/COFF debug filename
410 * @v loaded Loaded image
411 * @ret name PE/COFF debug filename, or NULL
414 efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
416 EFI_IMAGE_DOS_HEADER *dos;
417 EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
418 EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
419 EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
420 EFI_IMAGE_DATA_DIRECTORY *datadir;
421 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *debug;
422 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *codeview_nb10;
423 EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY *codeview_rsds;
424 EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY *codeview_mtoc;
428 uint32_t codeview_magic;
435 DBG ( "[NULL LoadedImage]" );
439 /* Parse DOS header */
440 dos = loaded->ImageBase;
442 DBG ( "[Missing DOS header]" );
445 dos_magic = dos->e_magic;
446 if ( dos_magic != EFI_IMAGE_DOS_SIGNATURE ) {
447 DBG ( "[Bad DOS signature %#04x]", dos_magic );
450 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 );
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;
466 DBG ( "[Bad optional header signature %#04x]", opt_magic );
470 /* Parse data directory entry */
471 if ( ! datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress ) {
472 DBG ( "[Empty debug directory entry]" );
475 debug = ( loaded->ImageBase +
476 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)]",
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;
489 /* Parse CodeView entry */
490 if ( codeview_magic == CODEVIEW_SIGNATURE_NB10 ) {
491 name = ( ( void * ) ( codeview_nb10 + 1 ) );
492 } else if ( codeview_magic == CODEVIEW_SIGNATURE_RSDS ) {
493 name = ( ( void * ) ( codeview_rsds + 1 ) );
494 } else if ( codeview_magic == CODEVIEW_SIGNATURE_MTOC ) {
495 name = ( ( void * ) ( codeview_mtoc + 1 ) );
497 DBG ( "[Bad CodeView signature %#08x]", codeview_magic );
501 /* Sanity check - avoid scanning endlessly through memory */
502 max_len = EFI_PAGE_SIZE; /* Reasonably sane */
503 if ( strnlen ( name, max_len ) == max_len ) {
504 DBG ( "[Excessively long or invalid CodeView name]" );
508 /* Skip any directory components. We cannot modify this data
509 * or create a temporary buffer, so do not use basename().
511 while ( ( ( tmp = strchr ( name, '/' ) ) != NULL ) ||
512 ( ( tmp = strchr ( name, '\\' ) ) != NULL ) ) {
516 /* Copy base name to buffer */
517 snprintf ( buf, sizeof ( buf ), "%s", name );
519 /* Strip file suffix, if present */
520 if ( ( tmp = strrchr ( name, '.' ) ) != NULL )
527 * Get initial loaded image name
529 * @v loaded Loaded image
530 * @ret name Initial loaded image name, or NULL
533 efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
537 DBG ( "[NULL LoadedImage]" );
541 return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
545 * Get loaded image name from file path
547 * @v loaded Loaded image
548 * @ret name Loaded image name, or NULL
551 efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
555 DBG ( "[NULL LoadedImage]" );
559 return efi_devpath_text ( loaded->FilePath );
562 /** An EFI handle name type */
563 struct efi_handle_name_type {
569 * @v interface Protocol interface
570 * @ret name Name of handle, or NULL on failure
572 const char * ( * name ) ( void *interface );
576 * Define an EFI handle name type
578 * @v protocol Protocol interface
579 * @v name Method to get name
580 * @ret type EFI handle name type
582 #define EFI_HANDLE_NAME_TYPE( protocol, name ) { \
584 ( const char * ( * ) ( void * ) ) (name), \
587 /** EFI handle name types */
588 static struct efi_handle_name_type efi_handle_name_types[] = {
590 EFI_HANDLE_NAME_TYPE ( &efi_device_path_protocol_guid,
592 /* Driver name (for driver image handles) */
593 EFI_HANDLE_NAME_TYPE ( &efi_component_name2_protocol_guid,
595 /* Driver name (via obsolete original ComponentName protocol) */
596 EFI_HANDLE_NAME_TYPE ( &efi_component_name_protocol_guid,
598 /* PE/COFF debug filename (for image handles) */
599 EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
600 efi_pecoff_debug_name ),
601 /* Loaded image device path (for image handles) */
602 EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_device_path_protocol_guid,
604 /* First loaded image name (for the DxeCore image) */
605 EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
606 efi_first_loaded_image_name ),
607 /* Handle's loaded image file path (for image handles) */
608 EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
609 efi_loaded_image_filepath_name ),
613 * Get name of an EFI handle
615 * @v handle EFI handle
616 * @ret text Name of handle, or NULL
618 const char * efi_handle_name ( EFI_HANDLE handle ) {
619 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
620 struct efi_handle_name_type *type;
626 /* Fail immediately for NULL handles */
630 /* Try each name type in turn */
631 for ( i = 0 ; i < ( sizeof ( efi_handle_name_types ) /
632 sizeof ( efi_handle_name_types[0] ) ) ; i++ ) {
633 type = &efi_handle_name_types[i];
636 /* Try to open the applicable protocol */
637 efirc = bs->OpenProtocol ( handle, type->protocol, &interface,
638 efi_image_handle, handle,
639 EFI_OPEN_PROTOCOL_GET_PROTOCOL );
645 /* Try to get name from this protocol */
647 name = type->name ( interface );
648 DBG2 ( "%c", ( name ? ( name[0] ? 'Y' : 'E' ) : 'N' ) );
651 bs->CloseProtocol ( handle, type->protocol,
652 efi_image_handle, handle );
655 /* Use this name, if possible */
656 if ( name && name[0] )