iPXE
efi_debug.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /**
00027  * @file
00028  *
00029  * EFI debugging utilities
00030  *
00031  */
00032 
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <errno.h>
00036 #include <ipxe/uuid.h>
00037 #include <ipxe/base16.h>
00038 #include <ipxe/vsprintf.h>
00039 #include <ipxe/efi/efi.h>
00040 #include <ipxe/efi/efi_utils.h>
00041 #include <ipxe/efi/Protocol/ComponentName.h>
00042 #include <ipxe/efi/Protocol/ComponentName2.h>
00043 #include <ipxe/efi/Protocol/DevicePathToText.h>
00044 #include <ipxe/efi/IndustryStandard/PeImage.h>
00045 
00046 /** Device path to text protocol */
00047 static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *efidpt;
00048 EFI_REQUEST_PROTOCOL ( EFI_DEVICE_PATH_TO_TEXT_PROTOCOL, &efidpt );
00049 
00050 /** Iscsi4Dxe module GUID */
00051 static EFI_GUID efi_iscsi4_dxe_guid = {
00052         0x4579b72d, 0x7ec4, 0x4dd4,
00053         { 0x84, 0x86, 0x08, 0x3c, 0x86, 0xb1, 0x82, 0xa7 }
00054 };
00055 
00056 /** VlanConfigDxe module GUID */
00057 static EFI_GUID efi_vlan_config_dxe_guid = {
00058         0xe4f61863, 0xfe2c, 0x4b56,
00059         { 0xa8, 0xf4, 0x08, 0x51, 0x9b, 0xc4, 0x39, 0xdf }
00060 };
00061 
00062 /** A well-known GUID */
00063 struct efi_well_known_guid {
00064         /** GUID */
00065         EFI_GUID *guid;
00066         /** Name */
00067         const char *name;
00068 };
00069 
00070 /** Well-known GUIDs */
00071 static struct efi_well_known_guid efi_well_known_guids[] = {
00072         { &efi_absolute_pointer_protocol_guid,
00073           "AbsolutePointer" },
00074         { &efi_acpi_table_protocol_guid,
00075           "AcpiTable" },
00076         { &efi_apple_net_boot_protocol_guid,
00077           "AppleNetBoot" },
00078         { &efi_arp_protocol_guid,
00079           "Arp" },
00080         { &efi_arp_service_binding_protocol_guid,
00081           "ArpSb" },
00082         { &efi_block_io_protocol_guid,
00083           "BlockIo" },
00084         { &efi_block_io2_protocol_guid,
00085           "BlockIo2" },
00086         { &efi_bus_specific_driver_override_protocol_guid,
00087           "BusSpecificDriverOverride" },
00088         { &efi_component_name_protocol_guid,
00089           "ComponentName" },
00090         { &efi_component_name2_protocol_guid,
00091           "ComponentName2" },
00092         { &efi_console_control_protocol_guid,
00093           "ConsoleControl" },
00094         { &efi_device_path_protocol_guid,
00095           "DevicePath" },
00096         { &efi_driver_binding_protocol_guid,
00097           "DriverBinding" },
00098         { &efi_dhcp4_protocol_guid,
00099           "Dhcp4" },
00100         { &efi_dhcp4_service_binding_protocol_guid,
00101           "Dhcp4Sb" },
00102         { &efi_disk_io_protocol_guid,
00103           "DiskIo" },
00104         { &efi_graphics_output_protocol_guid,
00105           "GraphicsOutput" },
00106         { &efi_hii_config_access_protocol_guid,
00107           "HiiConfigAccess" },
00108         { &efi_hii_font_protocol_guid,
00109           "HiiFont" },
00110         { &efi_ip4_protocol_guid,
00111           "Ip4" },
00112         { &efi_ip4_config_protocol_guid,
00113           "Ip4Config" },
00114         { &efi_ip4_service_binding_protocol_guid,
00115           "Ip4Sb" },
00116         { &efi_iscsi4_dxe_guid,
00117           "IScsi4Dxe" },
00118         { &efi_load_file_protocol_guid,
00119           "LoadFile" },
00120         { &efi_load_file2_protocol_guid,
00121           "LoadFile2" },
00122         { &efi_loaded_image_protocol_guid,
00123           "LoadedImage" },
00124         { &efi_loaded_image_device_path_protocol_guid,
00125           "LoadedImageDevicePath"},
00126         { &efi_managed_network_protocol_guid,
00127           "ManagedNetwork" },
00128         { &efi_managed_network_service_binding_protocol_guid,
00129           "ManagedNetworkSb" },
00130         { &efi_mtftp4_protocol_guid,
00131           "Mtftp4" },
00132         { &efi_mtftp4_service_binding_protocol_guid,
00133           "Mtftp4Sb" },
00134         { &efi_nii_protocol_guid,
00135           "Nii" },
00136         { &efi_nii31_protocol_guid,
00137           "Nii31" },
00138         { &efi_pci_io_protocol_guid,
00139           "PciIo" },
00140         { &efi_pci_root_bridge_io_protocol_guid,
00141           "PciRootBridgeIo" },
00142         { &efi_pxe_base_code_protocol_guid,
00143           "PxeBaseCode" },
00144         { &efi_serial_io_protocol_guid,
00145           "SerialIo" },
00146         { &efi_simple_file_system_protocol_guid,
00147           "SimpleFileSystem" },
00148         { &efi_simple_network_protocol_guid,
00149           "SimpleNetwork" },
00150         { &efi_simple_pointer_protocol_guid,
00151           "SimplePointer" },
00152         { &efi_simple_text_input_protocol_guid,
00153           "SimpleTextInput" },
00154         { &efi_simple_text_input_ex_protocol_guid,
00155           "SimpleTextInputEx" },
00156         { &efi_simple_text_output_protocol_guid,
00157           "SimpleTextOutput" },
00158         { &efi_tcg_protocol_guid,
00159           "Tcg" },
00160         { &efi_tcp4_protocol_guid,
00161           "Tcp4" },
00162         { &efi_tcp4_service_binding_protocol_guid,
00163           "Tcp4Sb" },
00164         { &efi_tree_protocol_guid,
00165           "TrEE" },
00166         { &efi_udp4_protocol_guid,
00167           "Udp4" },
00168         { &efi_udp4_service_binding_protocol_guid,
00169           "Udp4Sb" },
00170         { &efi_uga_draw_protocol_guid,
00171           "UgaDraw" },
00172         { &efi_unicode_collation_protocol_guid,
00173           "UnicodeCollation" },
00174         { &efi_usb_hc_protocol_guid,
00175           "UsbHc" },
00176         { &efi_usb2_hc_protocol_guid,
00177           "Usb2Hc" },
00178         { &efi_usb_io_protocol_guid,
00179           "UsbIo" },
00180         { &efi_vlan_config_protocol_guid,
00181           "VlanConfig" },
00182         { &efi_vlan_config_dxe_guid,
00183           "VlanConfigDxe" },
00184 };
00185 
00186 /**
00187  * Convert GUID to a printable string
00188  *
00189  * @v guid              GUID
00190  * @ret string          Printable string
00191  */
00192 const __attribute__ (( pure )) char * efi_guid_ntoa ( EFI_GUID *guid ) {
00193         union {
00194                 union uuid uuid;
00195                 EFI_GUID guid;
00196         } u;
00197         unsigned int i;
00198 
00199         /* Sanity check */
00200         if ( ! guid )
00201                 return NULL;
00202 
00203         /* Check for a match against well-known GUIDs */
00204         for ( i = 0 ; i < ( sizeof ( efi_well_known_guids ) /
00205                             sizeof ( efi_well_known_guids[0] ) ) ; i++ ) {
00206                 if ( memcmp ( guid, efi_well_known_guids[i].guid,
00207                               sizeof ( *guid ) ) == 0 ) {
00208                         return efi_well_known_guids[i].name;
00209                 }
00210         }
00211 
00212         /* Convert GUID to standard endianness */
00213         memcpy ( &u.guid, guid, sizeof ( u.guid ) );
00214         uuid_mangle ( &u.uuid );
00215         return uuid_ntoa ( &u.uuid );
00216 }
00217 
00218 /**
00219  * Name locate search type
00220  *
00221  * @v search_type       Locate search type
00222  * @ret name            Locate search type name
00223  */
00224 const __attribute__ (( pure )) char *
00225 efi_locate_search_type_name ( EFI_LOCATE_SEARCH_TYPE search_type ) {
00226         static char buf[16];
00227 
00228         switch ( search_type ) {
00229         case AllHandles :       return "AllHandles";
00230         case ByRegisterNotify:  return "ByRegisterNotify";
00231         case ByProtocol:        return "ByProtocol";
00232         default:
00233                 snprintf ( buf, sizeof ( buf ), "UNKNOWN<%d>", search_type );
00234                 return buf;
00235         }
00236 }
00237 
00238 /**
00239  * Name protocol open attributes
00240  *
00241  * @v attributes        Protocol open attributes
00242  * @ret name            Protocol open attributes name
00243  *
00244  * Returns a (static) string with characters for each set bit
00245  * corresponding to BY_(H)ANDLE_PROTOCOL, (G)ET_PROTOCOL,
00246  * (T)EST_PROTOCOL, BY_(C)HILD_CONTROLLER, BY_(D)RIVER, and
00247  * E(X)CLUSIVE.
00248  */
00249 const __attribute__ (( pure )) char *
00250 efi_open_attributes_name ( unsigned int attributes ) {
00251         static char attribute_chars[] = "HGTCDX";
00252         static char name[ sizeof ( attribute_chars ) ];
00253         char *tmp = name;
00254         unsigned int i;
00255 
00256         for ( i = 0 ; i < ( sizeof ( attribute_chars ) - 1 ) ; i++ ) {
00257                 if ( attributes & ( 1 << i ) )
00258                         *(tmp++) = attribute_chars[i];
00259         }
00260         *tmp = '\0';
00261 
00262         return name;
00263 }
00264 
00265 /**
00266  * Print list of openers of a given protocol on a given handle
00267  *
00268  * @v handle            EFI handle
00269  * @v protocol          Protocol GUID
00270  */
00271 void dbg_efi_openers ( EFI_HANDLE handle, EFI_GUID *protocol ) {
00272         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00273         EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *openers;
00274         EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *opener;
00275         UINTN count;
00276         unsigned int i;
00277         EFI_STATUS efirc;
00278         int rc;
00279 
00280         /* Sanity check */
00281         if ( ( ! handle ) || ( ! protocol ) ) {
00282                 printf ( "HANDLE %s could not retrieve openers for %s\n",
00283                          efi_handle_name ( handle ),
00284                          efi_guid_ntoa ( protocol ) );
00285                 return;
00286         }
00287 
00288         /* Retrieve list of openers */
00289         if ( ( efirc = bs->OpenProtocolInformation ( handle, protocol, &openers,
00290                                                      &count ) ) != 0 ) {
00291                 rc = -EEFI ( efirc );
00292                 printf ( "HANDLE %s could not retrieve openers for %s: %s\n",
00293                          efi_handle_name ( handle ),
00294                          efi_guid_ntoa ( protocol ), strerror ( rc ) );
00295                 return;
00296         }
00297 
00298         /* Dump list of openers */
00299         for ( i = 0 ; i < count ; i++ ) {
00300                 opener = &openers[i];
00301                 printf ( "HANDLE %s %s opened %dx (%s)",
00302                          efi_handle_name ( handle ),
00303                          efi_guid_ntoa ( protocol ), opener->OpenCount,
00304                          efi_open_attributes_name ( opener->Attributes ) );
00305                 printf ( " by %s", efi_handle_name ( opener->AgentHandle ) );
00306                 if ( opener->ControllerHandle == handle ) {
00307                         printf ( "\n" );
00308                 } else {
00309                         printf ( " for %s\n",
00310                                  efi_handle_name ( opener->ControllerHandle ) );
00311                 }
00312         }
00313 
00314         /* Free list */
00315         bs->FreePool ( openers );
00316 }
00317 
00318 /**
00319  * Print list of protocol handlers attached to a handle
00320  *
00321  * @v handle            EFI handle
00322  */
00323 void dbg_efi_protocols ( EFI_HANDLE handle ) {
00324         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00325         EFI_GUID **protocols;
00326         EFI_GUID *protocol;
00327         UINTN count;
00328         unsigned int i;
00329         EFI_STATUS efirc;
00330         int rc;
00331 
00332         /* Sanity check */
00333         if ( ! handle ) {
00334                 printf ( "HANDLE %p could not retrieve protocols\n", handle );
00335                 return;
00336         }
00337 
00338         /* Retrieve list of protocols */
00339         if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
00340                                                 &count ) ) != 0 ) {
00341                 rc = -EEFI ( efirc );
00342                 printf ( "HANDLE %s could not retrieve protocols: %s\n",
00343                          efi_handle_name ( handle ), strerror ( rc ) );
00344                 return;
00345         }
00346 
00347         /* Dump list of protocols */
00348         for ( i = 0 ; i < count ; i++ ) {
00349                 protocol = protocols[i];
00350                 printf ( "HANDLE %s %s supported\n", efi_handle_name ( handle ),
00351                          efi_guid_ntoa ( protocol ) );
00352                 dbg_efi_openers ( handle, protocol );
00353         }
00354 
00355         /* Free list */
00356         bs->FreePool ( protocols );
00357 }
00358 
00359 /**
00360  * Get textual representation of device path
00361  *
00362  * @v path              Device path
00363  * @ret text            Textual representation of device path, or NULL
00364  */
00365 const __attribute__ (( pure )) char *
00366 efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
00367         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00368         static char text[256];
00369         size_t len;
00370         CHAR16 *wtext;
00371 
00372         /* Sanity checks */
00373         if ( ! path ) {
00374                 DBG ( "[NULL DevicePath]" );
00375                 return NULL;
00376         }
00377 
00378         /* If we have no DevicePathToText protocol then use a raw hex string */
00379         if ( ! efidpt ) {
00380                 DBG ( "[No DevicePathToText]" );
00381                 len = efi_devpath_len ( path );
00382                 base16_encode ( path, len, text, sizeof ( text ) );
00383                 return text;
00384         }
00385 
00386         /* Convert path to a textual representation */
00387         wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
00388         if ( ! wtext )
00389                 return NULL;
00390 
00391         /* Store path in buffer */
00392         snprintf ( text, sizeof ( text ), "%ls", wtext );
00393 
00394         /* Free path */
00395         bs->FreePool ( wtext );
00396 
00397         return text;
00398 }
00399 
00400 /**
00401  * Get driver name
00402  *
00403  * @v wtf               Component name protocol
00404  * @ret name            Driver name, or NULL
00405  */
00406 static const char * efi_driver_name ( EFI_COMPONENT_NAME_PROTOCOL *wtf ) {
00407         static char name[64];
00408         CHAR16 *driver_name;
00409         EFI_STATUS efirc;
00410 
00411         /* Sanity check */
00412         if ( ! wtf ) {
00413                 DBG ( "[NULL ComponentName]" );
00414                 return NULL;
00415         }
00416 
00417         /* Try "eng" first; if that fails then try the first language */
00418         if ( ( ( efirc = wtf->GetDriverName ( wtf, "eng",
00419                                               &driver_name ) ) != 0 ) &&
00420              ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
00421                                               &driver_name ) ) != 0 ) ) {
00422                 return NULL;
00423         }
00424 
00425         /* Convert name from CHAR16 to char */
00426         snprintf ( name, sizeof ( name ), "%ls", driver_name );
00427         return name;
00428 }
00429 
00430 /**
00431  * Get driver name
00432  *
00433  * @v wtf               Component name protocol
00434  * @ret name            Driver name, or NULL
00435  */
00436 static const char * efi_driver_name2 ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
00437         static char name[64];
00438         CHAR16 *driver_name;
00439         EFI_STATUS efirc;
00440 
00441         /* Sanity check */
00442         if ( ! wtf ) {
00443                 DBG ( "[NULL ComponentName2]" );
00444                 return NULL;
00445         }
00446 
00447         /* Try "en" first; if that fails then try the first language */
00448         if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
00449                                               &driver_name ) ) != 0 ) &&
00450              ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
00451                                               &driver_name ) ) != 0 ) ) {
00452                 return NULL;
00453         }
00454 
00455         /* Convert name from CHAR16 to char */
00456         snprintf ( name, sizeof ( name ), "%ls", driver_name );
00457         return name;
00458 }
00459 
00460 /**
00461  * Get PE/COFF debug filename
00462  *
00463  * @v loaded            Loaded image
00464  * @ret name            PE/COFF debug filename, or NULL
00465  */
00466 static const char *
00467 efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
00468         static char buf[32];
00469         EFI_IMAGE_DOS_HEADER *dos;
00470         EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
00471         EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
00472         EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
00473         EFI_IMAGE_DATA_DIRECTORY *datadir;
00474         EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *debug;
00475         EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *codeview_nb10;
00476         EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY *codeview_rsds;
00477         EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY *codeview_mtoc;
00478         uint16_t dos_magic;
00479         uint32_t pe_magic;
00480         uint16_t opt_magic;
00481         uint32_t codeview_magic;
00482         size_t max_len;
00483         char *name;
00484         char *tmp;
00485 
00486         /* Sanity check */
00487         if ( ! loaded ) {
00488                 DBG ( "[NULL LoadedImage]" );
00489                 return NULL;
00490         }
00491 
00492         /* Parse DOS header */
00493         dos = loaded->ImageBase;
00494         if ( ! dos ) {
00495                 DBG ( "[Missing DOS header]" );
00496                 return NULL;
00497         }
00498         dos_magic = dos->e_magic;
00499         if ( dos_magic != EFI_IMAGE_DOS_SIGNATURE ) {
00500                 DBG ( "[Bad DOS signature %#04x]", dos_magic );
00501                 return NULL;
00502         }
00503         pe = ( loaded->ImageBase + dos->e_lfanew );
00504 
00505         /* Parse PE header */
00506         pe_magic = pe->Pe32.Signature;
00507         if ( pe_magic != EFI_IMAGE_NT_SIGNATURE ) {
00508                 DBG ( "[Bad PE signature %#08x]", pe_magic );
00509                 return NULL;
00510         }
00511         opt32 = &pe->Pe32.OptionalHeader;
00512         opt64 = &pe->Pe32Plus.OptionalHeader;
00513         opt_magic = opt32->Magic;
00514         if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ) {
00515                 datadir = opt32->DataDirectory;
00516         } else if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC ) {
00517                 datadir = opt64->DataDirectory;
00518         } else {
00519                 DBG ( "[Bad optional header signature %#04x]", opt_magic );
00520                 return NULL;
00521         }
00522 
00523         /* Parse data directory entry */
00524         if ( ! datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress ) {
00525                 DBG ( "[Empty debug directory entry]" );
00526                 return NULL;
00527         }
00528         debug = ( loaded->ImageBase +
00529                   datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress );
00530 
00531         /* Parse debug directory entry */
00532         if ( debug->Type != EFI_IMAGE_DEBUG_TYPE_CODEVIEW ) {
00533                 DBG ( "[Not a CodeView debug directory entry (type %d)]",
00534                       debug->Type );
00535                 return NULL;
00536         }
00537         codeview_nb10 = ( loaded->ImageBase + debug->RVA );
00538         codeview_rsds = ( loaded->ImageBase + debug->RVA );
00539         codeview_mtoc = ( loaded->ImageBase + debug->RVA );
00540         codeview_magic = codeview_nb10->Signature;
00541 
00542         /* Parse CodeView entry */
00543         if ( codeview_magic == CODEVIEW_SIGNATURE_NB10 ) {
00544                 name = ( ( void * ) ( codeview_nb10 + 1 ) );
00545         } else if ( codeview_magic == CODEVIEW_SIGNATURE_RSDS ) {
00546                 name = ( ( void * ) ( codeview_rsds + 1 ) );
00547         } else if ( codeview_magic == CODEVIEW_SIGNATURE_MTOC ) {
00548                 name = ( ( void * ) ( codeview_mtoc + 1 ) );
00549         } else {
00550                 DBG ( "[Bad CodeView signature %#08x]", codeview_magic );
00551                 return NULL;
00552         }
00553 
00554         /* Sanity check - avoid scanning endlessly through memory */
00555         max_len = EFI_PAGE_SIZE; /* Reasonably sane */
00556         if ( strnlen ( name, max_len ) == max_len ) {
00557                 DBG ( "[Excessively long or invalid CodeView name]" );
00558                 return NULL;
00559         }
00560 
00561         /* Skip any directory components.  We cannot modify this data
00562          * or create a temporary buffer, so do not use basename().
00563          */
00564         while ( ( ( tmp = strchr ( name, '/' ) ) != NULL ) ||
00565                 ( ( tmp = strchr ( name, '\\' ) ) != NULL ) ) {
00566                 name = ( tmp + 1 );
00567         }
00568 
00569         /* Copy base name to buffer */
00570         snprintf ( buf, sizeof ( buf ), "%s", name );
00571 
00572         /* Strip file suffix, if present */
00573         if ( ( tmp = strrchr ( name, '.' ) ) != NULL )
00574                 *tmp = '\0';
00575 
00576         return name;
00577 }
00578 
00579 /**
00580  * Get initial loaded image name
00581  *
00582  * @v loaded            Loaded image
00583  * @ret name            Initial loaded image name, or NULL
00584  */
00585 static const char *
00586 efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
00587 
00588         /* Sanity check */
00589         if ( ! loaded ) {
00590                 DBG ( "[NULL LoadedImage]" );
00591                 return NULL;
00592         }
00593 
00594         return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
00595 }
00596 
00597 /**
00598  * Get loaded image name from file path
00599  *
00600  * @v loaded            Loaded image
00601  * @ret name            Loaded image name, or NULL
00602  */
00603 static const char *
00604 efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
00605 
00606         /* Sanity check */
00607         if ( ! loaded ) {
00608                 DBG ( "[NULL LoadedImage]" );
00609                 return NULL;
00610         }
00611 
00612         return efi_devpath_text ( loaded->FilePath );
00613 }
00614 
00615 /**
00616  * Get console input handle name
00617  *
00618  * @v input             Simple text input protocol
00619  * @ret name            Console input handle name, or NULL
00620  */
00621 static const char *
00622 efi_conin_name ( EFI_SIMPLE_TEXT_INPUT_PROTOCOL *input ) {
00623 
00624         /* Check for match against ConIn */
00625         if ( input == efi_systab->ConIn )
00626                 return "ConIn";
00627 
00628         return NULL;
00629 }
00630 
00631 /**
00632  * Get console output handle name
00633  *
00634  * @v output            Simple text output protocol
00635  * @ret name            Console output handle name, or NULL
00636  */
00637 static const char *
00638 efi_conout_name ( EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *output ) {
00639 
00640         /* Check for match against ConOut */
00641         if ( output == efi_systab->ConOut )
00642                 return "ConOut";
00643 
00644         /* Check for match against StdErr (if different from ConOut) */
00645         if ( output == efi_systab->StdErr )
00646                 return "StdErr";
00647 
00648         return NULL;
00649 }
00650 
00651 /** An EFI handle name type */
00652 struct efi_handle_name_type {
00653         /** Protocol */
00654         EFI_GUID *protocol;
00655         /**
00656          * Get name
00657          *
00658          * @v interface         Protocol interface
00659          * @ret name            Name of handle, or NULL on failure
00660          */
00661         const char * ( * name ) ( void *interface );
00662 };
00663 
00664 /**
00665  * Define an EFI handle name type
00666  *
00667  * @v protocol          Protocol interface
00668  * @v name              Method to get name
00669  * @ret type            EFI handle name type
00670  */
00671 #define EFI_HANDLE_NAME_TYPE( protocol, name ) {        \
00672         (protocol),                                     \
00673         ( const char * ( * ) ( void * ) ) (name),       \
00674         }
00675 
00676 /** EFI handle name types */
00677 static struct efi_handle_name_type efi_handle_name_types[] = {
00678         /* Device path */
00679         EFI_HANDLE_NAME_TYPE ( &efi_device_path_protocol_guid,
00680                                efi_devpath_text ),
00681         /* Driver name (for driver image handles) */
00682         EFI_HANDLE_NAME_TYPE ( &efi_component_name2_protocol_guid,
00683                                efi_driver_name2 ),
00684         /* Driver name (via obsolete original ComponentName protocol) */
00685         EFI_HANDLE_NAME_TYPE ( &efi_component_name_protocol_guid,
00686                                efi_driver_name ),
00687         /* PE/COFF debug filename (for image handles) */
00688         EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
00689                                efi_pecoff_debug_name ),
00690         /* Loaded image device path (for image handles) */
00691         EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_device_path_protocol_guid,
00692                                efi_devpath_text ),
00693         /* First loaded image name (for the DxeCore image) */
00694         EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
00695                                efi_first_loaded_image_name ),
00696         /* Handle's loaded image file path (for image handles) */
00697         EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
00698                                efi_loaded_image_filepath_name ),
00699         /* Our standard input file handle */
00700         EFI_HANDLE_NAME_TYPE ( &efi_simple_text_input_protocol_guid,
00701                                efi_conin_name ),
00702         /* Our standard output and standard error file handles */
00703         EFI_HANDLE_NAME_TYPE ( &efi_simple_text_output_protocol_guid,
00704                                efi_conout_name ),
00705 };
00706 
00707 /**
00708  * Get name of an EFI handle
00709  *
00710  * @v handle            EFI handle
00711  * @ret text            Name of handle, or NULL
00712  */
00713 const __attribute__ (( pure )) char * efi_handle_name ( EFI_HANDLE handle ) {
00714         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00715         struct efi_handle_name_type *type;
00716         static char buf[256];
00717         size_t used = 0;
00718         EFI_GUID **protocols;
00719         UINTN count;
00720         unsigned int i;
00721         void *interface;
00722         const char *name;
00723         EFI_STATUS efirc;
00724 
00725         /* Fail immediately for NULL handles */
00726         if ( ! handle )
00727                 return NULL;
00728 
00729         /* Try each name type in turn */
00730         for ( i = 0 ; i < ( sizeof ( efi_handle_name_types ) /
00731                             sizeof ( efi_handle_name_types[0] ) ) ; i++ ) {
00732                 type = &efi_handle_name_types[i];
00733                 DBG2 ( "<%d", i );
00734 
00735                 /* Try to open the applicable protocol */
00736                 efirc = bs->OpenProtocol ( handle, type->protocol, &interface,
00737                                            efi_image_handle, handle,
00738                                            EFI_OPEN_PROTOCOL_GET_PROTOCOL );
00739                 if ( efirc != 0 ) {
00740                         DBG2 ( ">" );
00741                         continue;
00742                 }
00743 
00744                 /* Try to get name from this protocol */
00745                 DBG2 ( "-" );
00746                 name = type->name ( interface );
00747                 DBG2 ( "%c", ( name ? ( name[0] ? 'Y' : 'E' ) : 'N' ) );
00748 
00749                 /* Close protocol */
00750                 bs->CloseProtocol ( handle, type->protocol,
00751                                     efi_image_handle, handle );
00752                 DBG2 ( ">" );
00753 
00754                 /* Use this name, if possible */
00755                 if ( name && name[0] )
00756                         return name;
00757         }
00758 
00759         /* If no name is found, then use the raw handle value and a
00760          * list of installed protocols.
00761          */
00762         used = ssnprintf ( buf, sizeof ( buf ), "UNKNOWN<%p", handle );
00763         if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
00764                                                 &count ) ) == 0 ) {
00765                 for ( i = 0 ; i < count ; i++ ) {
00766                         used += ssnprintf ( ( buf + used ),
00767                                             ( sizeof ( buf ) - used ), ",%s",
00768                                             efi_guid_ntoa ( protocols[i] ) );
00769                 }
00770                 bs->FreePool ( protocols );
00771         }
00772         used += ssnprintf ( ( buf + used ), ( sizeof ( buf ) - used ), ">" );
00773         return buf;
00774 }