iPXE
efi_utils.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 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 
00020 FILE_LICENCE ( GPL2_OR_LATER );
00021 
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <ipxe/efi/efi.h>
00026 #include <ipxe/efi/efi_pci.h>
00027 #include <ipxe/efi/efi_utils.h>
00028 
00029 /** @file
00030  *
00031  * EFI utilities
00032  *
00033  */
00034 
00035 /**
00036  * Find end of device path
00037  *
00038  * @v path              Path to device
00039  * @ret path_end        End of device path
00040  */
00041 EFI_DEVICE_PATH_PROTOCOL * efi_devpath_end ( EFI_DEVICE_PATH_PROTOCOL *path ) {
00042 
00043         while ( path->Type != END_DEVICE_PATH_TYPE ) {
00044                 path = ( ( ( void * ) path ) +
00045                          /* There's this amazing new-fangled thing known as
00046                           * a UINT16, but who wants to use one of those? */
00047                          ( ( path->Length[1] << 8 ) | path->Length[0] ) );
00048         }
00049 
00050         return path;
00051 }
00052 
00053 /**
00054  * Find length of device path (excluding terminator)
00055  *
00056  * @v path              Path to device
00057  * @ret path_len        Length of device path
00058  */
00059 size_t efi_devpath_len ( EFI_DEVICE_PATH_PROTOCOL *path ) {
00060         EFI_DEVICE_PATH_PROTOCOL *end = efi_devpath_end ( path );
00061 
00062         return ( ( ( void * ) end ) - ( ( void * ) path ) );
00063 }
00064 
00065 /**
00066  * Locate parent device supporting a given protocol
00067  *
00068  * @v device            EFI device handle
00069  * @v protocol          Protocol GUID
00070  * @v parent            Parent EFI device handle to fill in
00071  * @ret rc              Return status code
00072  */
00073 int efi_locate_device ( EFI_HANDLE device, EFI_GUID *protocol,
00074                         EFI_HANDLE *parent ) {
00075         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00076         union {
00077                 EFI_DEVICE_PATH_PROTOCOL *path;
00078                 void *interface;
00079         } path;
00080         EFI_DEVICE_PATH_PROTOCOL *devpath;
00081         EFI_STATUS efirc;
00082         int rc;
00083 
00084         /* Get device path */
00085         if ( ( efirc = bs->OpenProtocol ( device,
00086                                           &efi_device_path_protocol_guid,
00087                                           &path.interface,
00088                                           efi_image_handle, device,
00089                                           EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
00090                 rc = -EEFI ( efirc );
00091                 DBGC ( device, "EFIDEV %s cannot open device path: %s\n",
00092                        efi_handle_name ( device ), strerror ( rc ) );
00093                 goto err_open_device_path;
00094         }
00095         devpath = path.path;
00096 
00097         /* Check for presence of specified protocol */
00098         if ( ( efirc = bs->LocateDevicePath ( protocol, &devpath,
00099                                               parent ) ) != 0 ) {
00100                 rc = -EEFI ( efirc );
00101                 DBGC ( device, "EFIDEV %s has no parent supporting %s: %s\n",
00102                        efi_handle_name ( device ),
00103                        efi_guid_ntoa ( protocol ), strerror ( rc ) );
00104                 goto err_locate_protocol;
00105         }
00106 
00107         /* Success */
00108         rc = 0;
00109 
00110  err_locate_protocol:
00111         bs->CloseProtocol ( device, &efi_device_path_protocol_guid,
00112                             efi_image_handle, device );
00113  err_open_device_path:
00114         return rc;
00115 }
00116 
00117 /**
00118  * Add EFI device as child of another EFI device
00119  *
00120  * @v parent            EFI parent device handle
00121  * @v child             EFI child device handle
00122  * @ret rc              Return status code
00123  */
00124 int efi_child_add ( EFI_HANDLE parent, EFI_HANDLE child ) {
00125         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00126         void *devpath;
00127         EFI_STATUS efirc;
00128         int rc;
00129 
00130         /* Re-open the device path protocol */
00131         if ( ( efirc = bs->OpenProtocol ( parent,
00132                                           &efi_device_path_protocol_guid,
00133                                           &devpath,
00134                                           efi_image_handle, child,
00135                                           EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
00136                                           ) ) != 0 ) {
00137                 rc = -EEFI ( efirc );
00138                 DBGC ( parent, "EFIDEV %s could not add child",
00139                        efi_handle_name ( parent ) );
00140                 DBGC ( parent, " %s: %s\n",
00141                        efi_handle_name ( child ), strerror ( rc ) );
00142                 DBGC_EFI_OPENERS ( parent, parent,
00143                                    &efi_device_path_protocol_guid );
00144                 return rc;
00145         }
00146 
00147         DBGC2 ( parent, "EFIDEV %s added child", efi_handle_name ( parent ) );
00148         DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
00149         return 0;
00150 }
00151 
00152 /**
00153  * Remove EFI device as child of another EFI device
00154  *
00155  * @v parent            EFI parent device handle
00156  * @v child             EFI child device handle
00157  */
00158 void efi_child_del ( EFI_HANDLE parent, EFI_HANDLE child ) {
00159         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00160 
00161         bs->CloseProtocol ( parent, &efi_device_path_protocol_guid,
00162                             efi_image_handle, child );
00163         DBGC2 ( parent, "EFIDEV %s removed child", efi_handle_name ( parent ) );
00164         DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
00165 }
00166 
00167 /**
00168  * Get underlying PCI device information
00169  *
00170  * @v device            EFI device handle
00171  * @v prefix            Device name prefix
00172  * @v dev               Generic device to fill in
00173  * @ret rc              Return status code
00174  */
00175 static int efi_pci_info ( EFI_HANDLE device, const char *prefix,
00176                           struct device *dev ) {
00177         EFI_HANDLE pci_device;
00178         struct pci_device pci;
00179         int rc;
00180 
00181         /* Find parent PCI device */
00182         if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid,
00183                                         &pci_device ) ) != 0 ) {
00184                 DBGC ( device, "EFIDEV %s is not a PCI device: %s\n",
00185                        efi_handle_name ( device ), strerror ( rc ) );
00186                 return rc;
00187         }
00188 
00189         /* Get PCI device information */
00190         if ( ( rc = efipci_info ( pci_device, &pci ) ) != 0 ) {
00191                 DBGC ( device, "EFIDEV %s could not get PCI information: %s\n",
00192                        efi_handle_name ( device ), strerror ( rc ) );
00193                 return rc;
00194         }
00195 
00196         /* Populate device information */
00197         memcpy ( &dev->desc, &pci.dev.desc, sizeof ( dev->desc ) );
00198         snprintf ( dev->name, sizeof ( dev->name ), "%s-%s",
00199                    prefix, pci.dev.name );
00200 
00201         return 0;
00202 }
00203 
00204 /**
00205  * Get underlying device information
00206  *
00207  * @v device            EFI device handle
00208  * @v prefix            Device name prefix
00209  * @v dev               Generic device to fill in
00210  */
00211 void efi_device_info ( EFI_HANDLE device, const char *prefix,
00212                        struct device *dev ) {
00213         int rc;
00214 
00215         /* Try getting underlying PCI device information */
00216         if ( ( rc = efi_pci_info ( device, prefix, dev ) ) == 0 )
00217                 return;
00218 
00219         /* If we cannot get any underlying device information, fall
00220          * back to providing information about the EFI handle.
00221          */
00222         DBGC ( device, "EFIDEV %s could not get underlying device "
00223                "information\n", efi_handle_name ( device ) );
00224         dev->desc.bus_type = BUS_TYPE_EFI;
00225         snprintf ( dev->name, sizeof ( dev->name ), "%s-%p", prefix, device );
00226 }