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 %s could not retrieve protocols\n",
00335                          efi_handle_name ( handle ) );
00336                 return;
00337         }
00338 
00339         /* Retrieve list of protocols */
00340         if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
00341                                                 &count ) ) != 0 ) {
00342                 rc = -EEFI ( efirc );
00343                 printf ( "HANDLE %s could not retrieve protocols: %s\n",
00344                          efi_handle_name ( handle ), strerror ( rc ) );
00345                 return;
00346         }
00347 
00348         /* Dump list of protocols */
00349         for ( i = 0 ; i < count ; i++ ) {
00350                 protocol = protocols[i];
00351                 printf ( "HANDLE %s %s supported\n", efi_handle_name ( handle ),
00352                          efi_guid_ntoa ( protocol ) );
00353                 dbg_efi_openers ( handle, protocol );
00354         }
00355 
00356         /* Free list */
00357         bs->FreePool ( protocols );
00358 }
00359 
00360 /**
00361  * Get textual representation of device path
00362  *
00363  * @v path              Device path
00364  * @ret text            Textual representation of device path, or NULL
00365  */
00366 const __attribute__ (( pure )) char *
00367 efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
00368         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00369         static char text[256];
00370         size_t len;
00371         CHAR16 *wtext;
00372 
00373         /* Sanity checks */
00374         if ( ! path ) {
00375                 DBG ( "[NULL DevicePath]" );
00376                 return NULL;
00377         }
00378 
00379         /* If we have no DevicePathToText protocol then use a raw hex string */
00380         if ( ! efidpt ) {
00381                 DBG ( "[No DevicePathToText]" );
00382                 len = efi_devpath_len ( path );
00383                 base16_encode ( path, len, text, sizeof ( text ) );
00384                 return text;
00385         }
00386 
00387         /* Convert path to a textual representation */
00388         wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
00389         if ( ! wtext )
00390                 return NULL;
00391 
00392         /* Store path in buffer */
00393         snprintf ( text, sizeof ( text ), "%ls", wtext );
00394 
00395         /* Free path */
00396         bs->FreePool ( wtext );
00397 
00398         return text;
00399 }
00400 
00401 /**
00402  * Get driver name
00403  *
00404  * @v wtf               Component name protocol
00405  * @ret name            Driver name, or NULL
00406  */
00407 static const char * efi_driver_name ( EFI_COMPONENT_NAME_PROTOCOL *wtf ) {
00408         static char name[64];
00409         CHAR16 *driver_name;
00410         EFI_STATUS efirc;
00411 
00412         /* Sanity check */
00413         if ( ! wtf ) {
00414                 DBG ( "[NULL ComponentName]" );
00415                 return NULL;
00416         }
00417 
00418         /* Try "eng" first; if that fails then try the first language */
00419         if ( ( ( efirc = wtf->GetDriverName ( wtf, "eng",
00420                                               &driver_name ) ) != 0 ) &&
00421              ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
00422                                               &driver_name ) ) != 0 ) ) {
00423                 return NULL;
00424         }
00425 
00426         /* Convert name from CHAR16 to char */
00427         snprintf ( name, sizeof ( name ), "%ls", driver_name );
00428         return name;
00429 }
00430 
00431 /**
00432  * Get driver name
00433  *
00434  * @v wtf               Component name protocol
00435  * @ret name            Driver name, or NULL
00436  */
00437 static const char * efi_driver_name2 ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
00438         static char name[64];
00439         CHAR16 *driver_name;
00440         EFI_STATUS efirc;
00441 
00442         /* Sanity check */
00443         if ( ! wtf ) {
00444                 DBG ( "[NULL ComponentName2]" );
00445                 return NULL;
00446         }
00447 
00448         /* Try "en" first; if that fails then try the first language */
00449         if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
00450                                               &driver_name ) ) != 0 ) &&
00451              ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
00452                                               &driver_name ) ) != 0 ) ) {
00453                 return NULL;
00454         }
00455 
00456         /* Convert name from CHAR16 to char */
00457         snprintf ( name, sizeof ( name ), "%ls", driver_name );
00458         return name;
00459 }
00460 
00461 /**
00462  * Get PE/COFF debug filename
00463  *
00464  * @v loaded            Loaded image
00465  * @ret name            PE/COFF debug filename, or NULL
00466  */
00467 static const char *
00468 efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
00469         static char buf[32];
00470         EFI_IMAGE_DOS_HEADER *dos;
00471         EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
00472         EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
00473         EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
00474         EFI_IMAGE_DATA_DIRECTORY *datadir;
00475         EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *debug;
00476         EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *codeview_nb10;
00477         EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY *codeview_rsds;
00478         EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY *codeview_mtoc;
00479         uint16_t dos_magic;
00480         uint32_t pe_magic;
00481         uint16_t opt_magic;
00482         uint32_t codeview_magic;
00483         size_t max_len;
00484         char *name;
00485         char *tmp;
00486 
00487         /* Sanity check */
00488         if ( ! loaded ) {
00489                 DBG ( "[NULL LoadedImage]" );
00490                 return NULL;
00491         }
00492 
00493         /* Parse DOS header */
00494         dos = loaded->ImageBase;
00495         if ( ! dos ) {
00496                 DBG ( "[Missing DOS header]" );
00497                 return NULL;
00498         }
00499         dos_magic = dos->e_magic;
00500         if ( dos_magic != EFI_IMAGE_DOS_SIGNATURE ) {
00501                 DBG ( "[Bad DOS signature %#04x]", dos_magic );
00502                 return NULL;
00503         }
00504         pe = ( loaded->ImageBase + dos->e_lfanew );
00505 
00506         /* Parse PE header */
00507         pe_magic = pe->Pe32.Signature;
00508         if ( pe_magic != EFI_IMAGE_NT_SIGNATURE ) {
00509                 DBG ( "[Bad PE signature %#08x]", pe_magic );
00510                 return NULL;
00511         }
00512         opt32 = &pe->Pe32.OptionalHeader;
00513         opt64 = &pe->Pe32Plus.OptionalHeader;
00514         opt_magic = opt32->Magic;
00515         if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ) {
00516                 datadir = opt32->DataDirectory;
00517         } else if ( opt_magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC ) {
00518                 datadir = opt64->DataDirectory;
00519         } else {
00520                 DBG ( "[Bad optional header signature %#04x]", opt_magic );
00521                 return NULL;
00522         }
00523 
00524         /* Parse data directory entry */
00525         if ( ! datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress ) {
00526                 DBG ( "[Empty debug directory entry]" );
00527                 return NULL;
00528         }
00529         debug = ( loaded->ImageBase +
00530                   datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress );
00531 
00532         /* Parse debug directory entry */
00533         if ( debug->Type != EFI_IMAGE_DEBUG_TYPE_CODEVIEW ) {
00534                 DBG ( "[Not a CodeView debug directory entry (type %d)]",
00535                       debug->Type );
00536                 return NULL;
00537         }
00538         codeview_nb10 = ( loaded->ImageBase + debug->RVA );
00539         codeview_rsds = ( loaded->ImageBase + debug->RVA );
00540         codeview_mtoc = ( loaded->ImageBase + debug->RVA );
00541         codeview_magic = codeview_nb10->Signature;
00542 
00543         /* Parse CodeView entry */
00544         if ( codeview_magic == CODEVIEW_SIGNATURE_NB10 ) {
00545                 name = ( ( void * ) ( codeview_nb10 + 1 ) );
00546         } else if ( codeview_magic == CODEVIEW_SIGNATURE_RSDS ) {
00547                 name = ( ( void * ) ( codeview_rsds + 1 ) );
00548         } else if ( codeview_magic == CODEVIEW_SIGNATURE_MTOC ) {
00549                 name = ( ( void * ) ( codeview_mtoc + 1 ) );
00550         } else {
00551                 DBG ( "[Bad CodeView signature %#08x]", codeview_magic );
00552                 return NULL;
00553         }
00554 
00555         /* Sanity check - avoid scanning endlessly through memory */
00556         max_len = EFI_PAGE_SIZE; /* Reasonably sane */
00557         if ( strnlen ( name, max_len ) == max_len ) {
00558                 DBG ( "[Excessively long or invalid CodeView name]" );
00559                 return NULL;
00560         }
00561 
00562         /* Skip any directory components.  We cannot modify this data
00563          * or create a temporary buffer, so do not use basename().
00564          */
00565         while ( ( ( tmp = strchr ( name, '/' ) ) != NULL ) ||
00566                 ( ( tmp = strchr ( name, '\\' ) ) != NULL ) ) {
00567                 name = ( tmp + 1 );
00568         }
00569 
00570         /* Copy base name to buffer */
00571         snprintf ( buf, sizeof ( buf ), "%s", name );
00572 
00573         /* Strip file suffix, if present */
00574         if ( ( tmp = strrchr ( name, '.' ) ) != NULL )
00575                 *tmp = '\0';
00576 
00577         return name;
00578 }
00579 
00580 /**
00581  * Get initial loaded image name
00582  *
00583  * @v loaded            Loaded image
00584  * @ret name            Initial loaded image name, or NULL
00585  */
00586 static const char *
00587 efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
00588 
00589         /* Sanity check */
00590         if ( ! loaded ) {
00591                 DBG ( "[NULL LoadedImage]" );
00592                 return NULL;
00593         }
00594 
00595         return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
00596 }
00597 
00598 /**
00599  * Get loaded image name from file path
00600  *
00601  * @v loaded            Loaded image
00602  * @ret name            Loaded image name, or NULL
00603  */
00604 static const char *
00605 efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
00606 
00607         /* Sanity check */
00608         if ( ! loaded ) {
00609                 DBG ( "[NULL LoadedImage]" );
00610                 return NULL;
00611         }
00612 
00613         return efi_devpath_text ( loaded->FilePath );
00614 }
00615 
00616 /**
00617  * Get console input handle name
00618  *
00619  * @v input             Simple text input protocol
00620  * @ret name            Console input handle name, or NULL
00621  */
00622 static const char *
00623 efi_conin_name ( EFI_SIMPLE_TEXT_INPUT_PROTOCOL *input ) {
00624 
00625         /* Check for match against ConIn */
00626         if ( input == efi_systab->ConIn )
00627                 return "ConIn";
00628 
00629         return NULL;
00630 }
00631 
00632 /**
00633  * Get console output handle name
00634  *
00635  * @v output            Simple text output protocol
00636  * @ret name            Console output handle name, or NULL
00637  */
00638 static const char *
00639 efi_conout_name ( EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *output ) {
00640 
00641         /* Check for match against ConOut */
00642         if ( output == efi_systab->ConOut )
00643                 return "ConOut";
00644 
00645         /* Check for match against StdErr (if different from ConOut) */
00646         if ( output == efi_systab->StdErr )
00647                 return "StdErr";
00648 
00649         return NULL;
00650 }
00651 
00652 /** An EFI handle name type */
00653 struct efi_handle_name_type {
00654         /** Protocol */
00655         EFI_GUID *protocol;
00656         /**
00657          * Get name
00658          *
00659          * @v interface         Protocol interface
00660          * @ret name            Name of handle, or NULL on failure
00661          */
00662         const char * ( * name ) ( void *interface );
00663 };
00664 
00665 /**
00666  * Define an EFI handle name type
00667  *
00668  * @v protocol          Protocol interface
00669  * @v name              Method to get name
00670  * @ret type            EFI handle name type
00671  */
00672 #define EFI_HANDLE_NAME_TYPE( protocol, name ) {        \
00673         (protocol),                                     \
00674         ( const char * ( * ) ( void * ) ) (name),       \
00675         }
00676 
00677 /** EFI handle name types */
00678 static struct efi_handle_name_type efi_handle_name_types[] = {
00679         /* Device path */
00680         EFI_HANDLE_NAME_TYPE ( &efi_device_path_protocol_guid,
00681                                efi_devpath_text ),
00682         /* Driver name (for driver image handles) */
00683         EFI_HANDLE_NAME_TYPE ( &efi_component_name2_protocol_guid,
00684                                efi_driver_name2 ),
00685         /* Driver name (via obsolete original ComponentName protocol) */
00686         EFI_HANDLE_NAME_TYPE ( &efi_component_name_protocol_guid,
00687                                efi_driver_name ),
00688         /* PE/COFF debug filename (for image handles) */
00689         EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
00690                                efi_pecoff_debug_name ),
00691         /* Loaded image device path (for image handles) */
00692         EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_device_path_protocol_guid,
00693                                efi_devpath_text ),
00694         /* First loaded image name (for the DxeCore image) */
00695         EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
00696                                efi_first_loaded_image_name ),
00697         /* Handle's loaded image file path (for image handles) */
00698         EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
00699                                efi_loaded_image_filepath_name ),
00700         /* Our standard input file handle */
00701         EFI_HANDLE_NAME_TYPE ( &efi_simple_text_input_protocol_guid,
00702                                efi_conin_name ),
00703         /* Our standard output and standard error file handles */
00704         EFI_HANDLE_NAME_TYPE ( &efi_simple_text_output_protocol_guid,
00705                                efi_conout_name ),
00706 };
00707 
00708 /**
00709  * Get name of an EFI handle
00710  *
00711  * @v handle            EFI handle
00712  * @ret text            Name of handle, or NULL
00713  */
00714 const __attribute__ (( pure )) char * efi_handle_name ( EFI_HANDLE handle ) {
00715         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00716         struct efi_handle_name_type *type;
00717         static char buf[256];
00718         size_t used = 0;
00719         EFI_GUID **protocols;
00720         UINTN count;
00721         unsigned int i;
00722         void *interface;
00723         const char *name;
00724         EFI_STATUS efirc;
00725 
00726         /* Fail immediately for NULL handles */
00727         if ( ! handle )
00728                 return NULL;
00729 
00730         /* Try each name type in turn */
00731         for ( i = 0 ; i < ( sizeof ( efi_handle_name_types ) /
00732                             sizeof ( efi_handle_name_types[0] ) ) ; i++ ) {
00733                 type = &efi_handle_name_types[i];
00734                 DBG2 ( "<%d", i );
00735 
00736                 /* Try to open the applicable protocol */
00737                 efirc = bs->OpenProtocol ( handle, type->protocol, &interface,
00738                                            efi_image_handle, handle,
00739                                            EFI_OPEN_PROTOCOL_GET_PROTOCOL );
00740                 if ( efirc != 0 ) {
00741                         DBG2 ( ">" );
00742                         continue;
00743                 }
00744 
00745                 /* Try to get name from this protocol */
00746                 DBG2 ( "-" );
00747                 name = type->name ( interface );
00748                 DBG2 ( "%c", ( name ? ( name[0] ? 'Y' : 'E' ) : 'N' ) );
00749 
00750                 /* Close protocol */
00751                 bs->CloseProtocol ( handle, type->protocol,
00752                                     efi_image_handle, handle );
00753                 DBG2 ( ">" );
00754 
00755                 /* Use this name, if possible */
00756                 if ( name && name[0] )
00757                         return name;
00758         }
00759 
00760         /* If no name is found, then use the raw handle value and a
00761          * list of installed protocols.
00762          */
00763         used = ssnprintf ( buf, sizeof ( buf ), "UNKNOWN<%p", handle );
00764         if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
00765                                                 &count ) ) == 0 ) {
00766                 for ( i = 0 ; i < count ; i++ ) {
00767                         used += ssnprintf ( ( buf + used ),
00768                                             ( sizeof ( buf ) - used ), ",%s",
00769                                             efi_guid_ntoa ( protocols[i] ) );
00770                 }
00771                 bs->FreePool ( protocols );
00772         }
00773         used += ssnprintf ( ( buf + used ), ( sizeof ( buf ) - used ), ">" );
00774         return buf;
00775 }