iPXE
efi_local.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2016 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 (at your option) 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 #include <string.h>
00027 #include <strings.h>
00028 #include <stdio.h>
00029 #include <errno.h>
00030 #include <assert.h>
00031 #include <ipxe/refcnt.h>
00032 #include <ipxe/malloc.h>
00033 #include <ipxe/xfer.h>
00034 #include <ipxe/open.h>
00035 #include <ipxe/uri.h>
00036 #include <ipxe/iobuf.h>
00037 #include <ipxe/process.h>
00038 #include <ipxe/efi/efi.h>
00039 #include <ipxe/efi/efi_strings.h>
00040 #include <ipxe/efi/efi_utils.h>
00041 #include <ipxe/efi/Protocol/SimpleFileSystem.h>
00042 #include <ipxe/efi/Guid/FileInfo.h>
00043 #include <ipxe/efi/Guid/FileSystemInfo.h>
00044 
00045 /** @file
00046  *
00047  * EFI local file access
00048  *
00049  */
00050 
00051 /** Download blocksize */
00052 #define EFI_LOCAL_BLKSIZE 4096
00053 
00054 /** An EFI local file */
00055 struct efi_local {
00056         /** Reference count */
00057         struct refcnt refcnt;
00058         /** Data transfer interface */
00059         struct interface xfer;
00060         /** Download process */
00061         struct process process;
00062 
00063         /** EFI root directory */
00064         EFI_FILE_PROTOCOL *root;
00065         /** EFI file */
00066         EFI_FILE_PROTOCOL *file;
00067         /** Length of file */
00068         size_t len;
00069 };
00070 
00071 /**
00072  * Close local file
00073  *
00074  * @v local             Local file
00075  * @v rc                Reason for close
00076  */
00077 static void efi_local_close ( struct efi_local *local, int rc ) {
00078 
00079         /* Stop process */
00080         process_del ( &local->process );
00081 
00082         /* Shut down data transfer interface */
00083         intf_shutdown ( &local->xfer, rc );
00084 
00085         /* Close EFI file */
00086         if ( local->file ) {
00087                 local->file->Close ( local->file );
00088                 local->file = NULL;
00089         }
00090 
00091         /* Close EFI root directory */
00092         if ( local->root ) {
00093                 local->root->Close ( local->root );
00094                 local->root = NULL;
00095         }
00096 }
00097 
00098 /**
00099  * Local file process
00100  *
00101  * @v local             Local file
00102  */
00103 static void efi_local_step ( struct efi_local *local ) {
00104         EFI_FILE_PROTOCOL *file = local->file;
00105         struct io_buffer *iobuf = NULL;
00106         size_t remaining;
00107         size_t frag_len;
00108         UINTN size;
00109         EFI_STATUS efirc;
00110         int rc;
00111 
00112         /* Wait until data transfer interface is ready */
00113         if ( ! xfer_window ( &local->xfer ) )
00114                 return;
00115 
00116         /* Presize receive buffer */
00117         remaining = local->len;
00118         xfer_seek ( &local->xfer, remaining );
00119         xfer_seek ( &local->xfer, 0 );
00120 
00121         /* Get file contents */
00122         while ( remaining ) {
00123 
00124                 /* Calculate length for this fragment */
00125                 frag_len = remaining;
00126                 if ( frag_len > EFI_LOCAL_BLKSIZE )
00127                         frag_len = EFI_LOCAL_BLKSIZE;
00128 
00129                 /* Allocate I/O buffer */
00130                 iobuf = xfer_alloc_iob ( &local->xfer, frag_len );
00131                 if ( ! iobuf ) {
00132                         rc = -ENOMEM;
00133                         goto err;
00134                 }
00135 
00136                 /* Read block */
00137                 size = frag_len;
00138                 if ( ( efirc = file->Read ( file, &size, iobuf->data ) ) != 0 ){
00139                         rc = -EEFI ( efirc );
00140                         DBGC ( local, "LOCAL %p could not read from file: %s\n",
00141                                local, strerror ( rc ) );
00142                         goto err;
00143                 }
00144                 assert ( size <= frag_len );
00145                 iob_put ( iobuf, size );
00146 
00147                 /* Deliver data */
00148                 if ( ( rc = xfer_deliver_iob ( &local->xfer,
00149                                                iob_disown ( iobuf ) ) ) != 0 ) {
00150                         DBGC ( local, "LOCAL %p could not deliver data: %s\n",
00151                                local, strerror ( rc ) );
00152                         goto err;
00153                 }
00154 
00155                 /* Move to next block */
00156                 remaining -= frag_len;
00157         }
00158 
00159         /* Close download */
00160         efi_local_close ( local, 0 );
00161 
00162         return;
00163 
00164  err:
00165         free_iob ( iobuf );
00166         efi_local_close ( local, rc );
00167 }
00168 
00169 /** Data transfer interface operations */
00170 static struct interface_operation efi_local_operations[] = {
00171         INTF_OP ( xfer_window_changed, struct efi_local *, efi_local_step ),
00172         INTF_OP ( intf_close, struct efi_local *, efi_local_close ),
00173 };
00174 
00175 /** Data transfer interface descriptor */
00176 static struct interface_descriptor efi_local_xfer_desc =
00177         INTF_DESC ( struct efi_local, xfer, efi_local_operations );
00178 
00179 /** Process descriptor */
00180 static struct process_descriptor efi_local_process_desc =
00181         PROC_DESC_ONCE ( struct efi_local, process, efi_local_step );
00182 
00183 /**
00184  * Check for matching volume name
00185  *
00186  * @v local             Local file
00187  * @v device            Device handle
00188  * @v root              Root filesystem handle
00189  * @v volume            Volume name
00190  * @ret rc              Return status code
00191  */
00192 static int efi_local_check_volume_name ( struct efi_local *local,
00193                                          EFI_HANDLE device,
00194                                          EFI_FILE_PROTOCOL *root,
00195                                          const char *volume ) {
00196         EFI_FILE_SYSTEM_INFO *info;
00197         UINTN size;
00198         char *label;
00199         EFI_STATUS efirc;
00200         int rc;
00201 
00202         /* Get length of file system information */
00203         size = 0;
00204         root->GetInfo ( root, &efi_file_system_info_id, &size, NULL );
00205 
00206         /* Allocate file system information */
00207         info = malloc ( size );
00208         if ( ! info ) {
00209                 rc = -ENOMEM;
00210                 goto err_alloc_info;
00211         }
00212 
00213         /* Get file system information */
00214         if ( ( efirc = root->GetInfo ( root, &efi_file_system_info_id, &size,
00215                                        info ) ) != 0 ) {
00216                 rc = -EEFI ( efirc );
00217                 DBGC ( local, "LOCAL %p could not get file system info on %s: "
00218                        "%s\n", local, efi_handle_name ( device ),
00219                        strerror ( rc ) );
00220                 goto err_get_info;
00221         }
00222         DBGC2 ( local, "LOCAL %p found %s with label \"%ls\"\n",
00223                 local, efi_handle_name ( device ), info->VolumeLabel );
00224 
00225         /* Construct volume label for comparison */
00226         if ( asprintf ( &label, "%ls", info->VolumeLabel ) < 0 ) {
00227                 rc = -ENOMEM;
00228                 goto err_alloc_label;
00229         }
00230 
00231         /* Compare volume label */
00232         if ( strcasecmp ( volume, label ) != 0 ) {
00233                 rc = -ENOENT;
00234                 goto err_compare;
00235         }
00236 
00237         /* Success */
00238         rc = 0;
00239 
00240  err_compare:
00241         free ( label );
00242  err_alloc_label:
00243  err_get_info:
00244         free ( info );
00245  err_alloc_info:
00246         return rc;
00247 }
00248 
00249 /**
00250  * Open root filesystem
00251  *
00252  * @v local             Local file
00253  * @v device            Device handle
00254  * @v root              Root filesystem handle to fill in
00255  * @ret rc              Return status code
00256  */
00257 static int efi_local_open_root ( struct efi_local *local, EFI_HANDLE device,
00258                                  EFI_FILE_PROTOCOL **root ) {
00259         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00260         union {
00261                 void *interface;
00262                 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs;
00263         } u;
00264         EFI_STATUS efirc;
00265         int rc;
00266 
00267         /* Open file system protocol */
00268         if ( ( efirc = bs->OpenProtocol ( device,
00269                                           &efi_simple_file_system_protocol_guid,
00270                                           &u.interface, efi_image_handle,
00271                                           device,
00272                                           EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
00273                 rc = -EEFI ( efirc );
00274                 DBGC ( local, "LOCAL %p could not open filesystem on %s: %s\n",
00275                        local, efi_handle_name ( device ), strerror ( rc ) );
00276                 goto err_filesystem;
00277         }
00278 
00279         /* Open root directory */
00280         if ( ( efirc = u.fs->OpenVolume ( u.fs, root ) ) != 0 ) {
00281                 rc = -EEFI ( efirc );
00282                 DBGC ( local, "LOCAL %p could not open volume on %s: %s\n",
00283                        local, efi_handle_name ( device ), strerror ( rc ) );
00284                 goto err_volume;
00285         }
00286 
00287         /* Success */
00288         rc = 0;
00289 
00290  err_volume:
00291         bs->CloseProtocol ( device, &efi_simple_file_system_protocol_guid,
00292                             efi_image_handle, device );
00293  err_filesystem:
00294         return rc;
00295 }
00296 
00297 /**
00298  * Open root filesystem of specified volume
00299  *
00300  * @v local             Local file
00301  * @v volume            Volume name, or NULL to use loaded image's device
00302  * @ret rc              Return status code
00303  */
00304 static int efi_local_open_volume ( struct efi_local *local,
00305                                    const char *volume ) {
00306         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00307         EFI_GUID *protocol = &efi_simple_file_system_protocol_guid;
00308         int ( * check ) ( struct efi_local *local, EFI_HANDLE device,
00309                           EFI_FILE_PROTOCOL *root, const char *volume );
00310         EFI_FILE_PROTOCOL *root;
00311         EFI_HANDLE *handles;
00312         EFI_HANDLE device;
00313         UINTN num_handles;
00314         UINTN i;
00315         EFI_STATUS efirc;
00316         int rc;
00317 
00318         /* Identify candidate handles */
00319         if ( volume ) {
00320                 /* Locate all filesystem handles */
00321                 if ( ( efirc = bs->LocateHandleBuffer ( ByProtocol, protocol,
00322                                                         NULL, &num_handles,
00323                                                         &handles ) ) != 0 ) {
00324                         rc = -EEFI ( efirc );
00325                         DBGC ( local, "LOCAL %p could not enumerate handles: "
00326                                "%s\n", local, strerror ( rc ) );
00327                         return rc;
00328                 }
00329                 check = efi_local_check_volume_name;
00330         } else {
00331                 /* Use our loaded image's device handle */
00332                 handles = &efi_loaded_image->DeviceHandle;
00333                 num_handles = 1;
00334                 check = NULL;
00335         }
00336 
00337         /* Find matching handle */
00338         for ( i = 0 ; i < num_handles ; i++ ) {
00339 
00340                 /* Get this device handle */
00341                 device = handles[i];
00342 
00343                 /* Open root directory */
00344                 if ( ( rc = efi_local_open_root ( local, device, &root ) ) != 0)
00345                         continue;
00346 
00347                 /* Check volume name, if applicable */
00348                 if ( ( check == NULL ) ||
00349                      ( ( rc = check ( local, device, root, volume ) ) == 0 ) ) {
00350                         DBGC ( local, "LOCAL %p using %s",
00351                                local, efi_handle_name ( device ) );
00352                         if ( volume )
00353                                 DBGC ( local, " with label \"%s\"", volume );
00354                         DBGC ( local, "\n" );
00355                         local->root = root;
00356                         break;
00357                 }
00358 
00359                 /* Close root directory */
00360                 root->Close ( root );
00361         }
00362 
00363         /* Free handles, if applicable */
00364         if ( volume )
00365                 bs->FreePool ( handles );
00366 
00367         /* Fail if we found no matching handle */
00368         if ( ! local->root ) {
00369                 DBGC ( local, "LOCAL %p found no matching handle\n", local );
00370                 return -ENOENT;
00371         }
00372 
00373         return 0;
00374 }
00375 
00376 /**
00377  * Open fully-resolved path
00378  *
00379  * @v local             Local file
00380  * @v resolved          Resolved path
00381  * @ret rc              Return status code
00382  */
00383 static int efi_local_open_resolved ( struct efi_local *local,
00384                                      const char *resolved ) {
00385         size_t name_len = strlen ( resolved );
00386         CHAR16 name[ name_len + 1 /* wNUL */ ];
00387         EFI_FILE_PROTOCOL *file;
00388         EFI_STATUS efirc;
00389         int rc;
00390 
00391         /* Construct filename */
00392         efi_snprintf ( name, ( name_len + 1 /* wNUL */ ), "%s", resolved );
00393 
00394         /* Open file */
00395         if ( ( efirc = local->root->Open ( local->root, &file, name,
00396                                            EFI_FILE_MODE_READ, 0 ) ) != 0 ) {
00397                 rc = -EEFI ( efirc );
00398                 DBGC ( local, "LOCAL %p could not open \"%s\": %s\n",
00399                        local, resolved, strerror ( rc ) );
00400                 return rc;
00401         }
00402         local->file = file;
00403 
00404         return 0;
00405 }
00406 
00407 /**
00408  * Open specified path
00409  *
00410  * @v local             Local file
00411  * @v path              Path to file
00412  * @ret rc              Return status code
00413  */
00414 static int efi_local_open_path ( struct efi_local *local, const char *path ) {
00415         FILEPATH_DEVICE_PATH *fp = container_of ( efi_loaded_image->FilePath,
00416                                                   FILEPATH_DEVICE_PATH, Header);
00417         size_t fp_len = ( fp ? efi_devpath_len ( &fp->Header ) : 0 );
00418         char base[ fp_len / 2 /* Cannot exceed this length */ ];
00419         size_t remaining = sizeof ( base );
00420         size_t len;
00421         char *resolved;
00422         char *tmp;
00423         int rc;
00424 
00425         /* Construct base path to our own image, if possible */
00426         memset ( base, 0, sizeof ( base ) );
00427         tmp = base;
00428         while ( fp && ( fp->Header.Type != END_DEVICE_PATH_TYPE ) ) {
00429                 len = snprintf ( tmp, remaining, "%ls", fp->PathName );
00430                 assert ( len < remaining );
00431                 tmp += len;
00432                 remaining -= len;
00433                 fp = ( ( ( void * ) fp ) + ( ( fp->Header.Length[1] << 8 ) |
00434                                              fp->Header.Length[0] ) );
00435         }
00436         DBGC2 ( local, "LOCAL %p base path \"%s\"\n",
00437                 local, base );
00438 
00439         /* Convert to sane path separators */
00440         for ( tmp = base ; *tmp ; tmp++ ) {
00441                 if ( *tmp == '\\' )
00442                         *tmp = '/';
00443         }
00444 
00445         /* Resolve path */
00446         resolved = resolve_path ( base, path );
00447         if ( ! resolved ) {
00448                 rc = -ENOMEM;
00449                 goto err_resolve;
00450         }
00451 
00452         /* Convert to insane path separators */
00453         for ( tmp = resolved ; *tmp ; tmp++ ) {
00454                 if ( *tmp == '/' )
00455                         *tmp = '\\';
00456         }
00457         DBGC ( local, "LOCAL %p using \"%s\"\n",
00458                local, resolved );
00459 
00460         /* Open resolved path */
00461         if ( ( rc = efi_local_open_resolved ( local, resolved ) ) != 0 )
00462                 goto err_open;
00463 
00464  err_open:
00465         free ( resolved );
00466  err_resolve:
00467         return rc;
00468 }
00469 
00470 /**
00471  * Get file length
00472  *
00473  * @v local             Local file
00474  * @ret rc              Return status code
00475  */
00476 static int efi_local_len ( struct efi_local *local ) {
00477         EFI_FILE_PROTOCOL *file = local->file;
00478         EFI_FILE_INFO *info;
00479         EFI_STATUS efirc;
00480         UINTN size;
00481         int rc;
00482 
00483         /* Get size of file information */
00484         size = 0;
00485         file->GetInfo ( file, &efi_file_info_id, &size, NULL );
00486 
00487         /* Allocate file information */
00488         info = malloc ( size );
00489         if ( ! info ) {
00490                 rc = -ENOMEM;
00491                 goto err_alloc;
00492         }
00493 
00494         /* Get file information */
00495         if ( ( efirc = file->GetInfo ( file, &efi_file_info_id, &size,
00496                                        info ) ) != 0 ) {
00497                 rc = -EEFI ( efirc );
00498                 DBGC ( local, "LOCAL %p could not get file info: %s\n",
00499                        local, strerror ( rc ) );
00500                 goto err_info;
00501         }
00502 
00503         /* Record file length */
00504         local->len = info->FileSize;
00505 
00506         /* Success */
00507         rc = 0;
00508 
00509  err_info:
00510         free ( info );
00511  err_alloc:
00512         return rc;
00513 }
00514 
00515 /**
00516  * Open local file
00517  *
00518  * @v xfer              Data transfer interface
00519  * @v uri               Request URI
00520  * @ret rc              Return status code
00521  */
00522 static int efi_local_open ( struct interface *xfer, struct uri *uri ) {
00523         struct efi_local *local;
00524         const char *volume;
00525         const char *path;
00526         int rc;
00527 
00528         /* Parse URI */
00529         volume = ( ( uri->host && uri->host[0] ) ? uri->host : NULL );
00530         path = ( uri->opaque ? uri->opaque : uri->path );
00531 
00532         /* Allocate and initialise structure */
00533         local = zalloc ( sizeof ( *local ) );
00534         if ( ! local ) {
00535                 rc = -ENOMEM;
00536                 goto err_alloc;
00537         }
00538         ref_init ( &local->refcnt, NULL );
00539         intf_init ( &local->xfer, &efi_local_xfer_desc, &local->refcnt );
00540         process_init ( &local->process, &efi_local_process_desc,
00541                        &local->refcnt );
00542 
00543         /* Open specified volume */
00544         if ( ( rc = efi_local_open_volume ( local, volume ) ) != 0 )
00545                 goto err_open_root;
00546 
00547         /* Open specified path */
00548         if ( ( rc = efi_local_open_path ( local, path ) ) != 0 )
00549                 goto err_open_file;
00550 
00551         /* Get length of file */
00552         if ( ( rc = efi_local_len ( local ) ) != 0 )
00553                 goto err_len;
00554 
00555         /* Attach to parent interface, mortalise self, and return */
00556         intf_plug_plug ( &local->xfer, xfer );
00557         ref_put ( &local->refcnt );
00558         return 0;
00559 
00560  err_len:
00561  err_open_file:
00562  err_open_root:
00563         efi_local_close ( local, 0 );
00564         ref_put ( &local->refcnt );
00565  err_alloc:
00566         return rc;
00567 }
00568 
00569 /** EFI local file URI opener */
00570 struct uri_opener efi_local_uri_opener __uri_opener = {
00571         .scheme = "file",
00572         .open   = efi_local_open,
00573 };