iPXE
Data Structures | Defines | Functions | Variables
efi_local.c File Reference

EFI local file access. More...

#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/refcnt.h>
#include <ipxe/malloc.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/uri.h>
#include <ipxe/iobuf.h>
#include <ipxe/process.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/Protocol/SimpleFileSystem.h>
#include <ipxe/efi/Guid/FileInfo.h>
#include <ipxe/efi/Guid/FileSystemInfo.h>

Go to the source code of this file.

Data Structures

struct  efi_local
 An EFI local file. More...

Defines

#define EFI_LOCAL_BLKSIZE   4096
 Download blocksize.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void efi_local_close (struct efi_local *local, int rc)
 Close local file.
static void efi_local_step (struct efi_local *local)
 Local file process.
static int efi_local_check_volume_name (struct efi_local *local, EFI_HANDLE device, EFI_FILE_PROTOCOL *root, const char *volume)
 Check for matching volume name.
static int efi_local_open_root (struct efi_local *local, EFI_HANDLE device, EFI_FILE_PROTOCOL **root)
 Open root filesystem.
static int efi_local_open_volume (struct efi_local *local, const char *volume)
 Open root filesystem of specified volume.
static int efi_local_open_resolved (struct efi_local *local, const char *resolved)
 Open fully-resolved path.
static int efi_local_open_path (struct efi_local *local, const char *path)
 Open specified path.
static int efi_local_len (struct efi_local *local)
 Get file length.
static int efi_local_open (struct interface *xfer, struct uri *uri)
 Open local file.

Variables

static struct interface_operation efi_local_operations []
 Data transfer interface operations.
static struct interface_descriptor efi_local_xfer_desc
 Data transfer interface descriptor.
static struct process_descriptor efi_local_process_desc
 Process descriptor.
struct uri_opener
efi_local_uri_opener 
__uri_opener
 EFI local file URI opener.

Detailed Description

EFI local file access.

Definition in file efi_local.c.


Define Documentation

#define EFI_LOCAL_BLKSIZE   4096

Download blocksize.

Definition at line 52 of file efi_local.c.

Referenced by efi_local_step().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static void efi_local_close ( struct efi_local local,
int  rc 
) [static]

Close local file.

Parameters:
localLocal file
rcReason for close

Definition at line 77 of file efi_local.c.

References _EFI_FILE_PROTOCOL::Close, efi_local::file, intf_shutdown(), NULL, efi_local::process, process_del(), efi_local::root, and efi_local::xfer.

Referenced by efi_local_open(), and efi_local_step().

                                                                {

        /* Stop process */
        process_del ( &local->process );

        /* Shut down data transfer interface */
        intf_shutdown ( &local->xfer, rc );

        /* Close EFI file */
        if ( local->file ) {
                local->file->Close ( local->file );
                local->file = NULL;
        }

        /* Close EFI root directory */
        if ( local->root ) {
                local->root->Close ( local->root );
                local->root = NULL;
        }
}
static void efi_local_step ( struct efi_local local) [static]

Local file process.

Parameters:
localLocal file

Definition at line 103 of file efi_local.c.

References assert, io_buffer::data, DBGC, EEFI, EFI_LOCAL_BLKSIZE, efi_local_close(), ENOMEM, efi_local::file, free_iob(), iob_disown, iob_put, efi_local::len, NULL, rc, _EFI_FILE_PROTOCOL::Read, size, strerror(), efi_local::xfer, xfer_alloc_iob(), xfer_deliver_iob(), xfer_seek(), and xfer_window().

                                                       {
        EFI_FILE_PROTOCOL *file = local->file;
        struct io_buffer *iobuf = NULL;
        size_t remaining;
        size_t frag_len;
        UINTN size;
        EFI_STATUS efirc;
        int rc;

        /* Wait until data transfer interface is ready */
        if ( ! xfer_window ( &local->xfer ) )
                return;

        /* Presize receive buffer */
        remaining = local->len;
        xfer_seek ( &local->xfer, remaining );
        xfer_seek ( &local->xfer, 0 );

        /* Get file contents */
        while ( remaining ) {

                /* Calculate length for this fragment */
                frag_len = remaining;
                if ( frag_len > EFI_LOCAL_BLKSIZE )
                        frag_len = EFI_LOCAL_BLKSIZE;

                /* Allocate I/O buffer */
                iobuf = xfer_alloc_iob ( &local->xfer, frag_len );
                if ( ! iobuf ) {
                        rc = -ENOMEM;
                        goto err;
                }

                /* Read block */
                size = frag_len;
                if ( ( efirc = file->Read ( file, &size, iobuf->data ) ) != 0 ){
                        rc = -EEFI ( efirc );
                        DBGC ( local, "LOCAL %p could not read from file: %s\n",
                               local, strerror ( rc ) );
                        goto err;
                }
                assert ( size <= frag_len );
                iob_put ( iobuf, size );

                /* Deliver data */
                if ( ( rc = xfer_deliver_iob ( &local->xfer,
                                               iob_disown ( iobuf ) ) ) != 0 ) {
                        DBGC ( local, "LOCAL %p could not deliver data: %s\n",
                               local, strerror ( rc ) );
                        goto err;
                }

                /* Move to next block */
                remaining -= frag_len;
        }

        /* Close download */
        efi_local_close ( local, 0 );

        return;

 err:
        free_iob ( iobuf );
        efi_local_close ( local, rc );
}
static int efi_local_check_volume_name ( struct efi_local local,
EFI_HANDLE  device,
EFI_FILE_PROTOCOL root,
const char *  volume 
) [static]

Check for matching volume name.

Parameters:
localLocal file
deviceDevice handle
rootRoot filesystem handle
volumeVolume name
Return values:
rcReturn status code

Definition at line 192 of file efi_local.c.

References asprintf(), DBGC, DBGC2, EEFI, efi_file_system_info_id, efi_handle_name(), ENOENT, ENOMEM, free, _EFI_FILE_PROTOCOL::GetInfo, info, malloc(), NULL, rc, size, strcasecmp(), strerror(), and EFI_FILE_SYSTEM_INFO::VolumeLabel.

Referenced by efi_local_open_volume().

                                                              {
        EFI_FILE_SYSTEM_INFO *info;
        UINTN size;
        char *label;
        EFI_STATUS efirc;
        int rc;

        /* Get length of file system information */
        size = 0;
        root->GetInfo ( root, &efi_file_system_info_id, &size, NULL );

        /* Allocate file system information */
        info = malloc ( size );
        if ( ! info ) {
                rc = -ENOMEM;
                goto err_alloc_info;
        }

        /* Get file system information */
        if ( ( efirc = root->GetInfo ( root, &efi_file_system_info_id, &size,
                                       info ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( local, "LOCAL %p could not get file system info on %s: "
                       "%s\n", local, efi_handle_name ( device ),
                       strerror ( rc ) );
                goto err_get_info;
        }
        DBGC2 ( local, "LOCAL %p found %s with label \"%ls\"\n",
                local, efi_handle_name ( device ), info->VolumeLabel );

        /* Construct volume label for comparison */
        if ( asprintf ( &label, "%ls", info->VolumeLabel ) < 0 ) {
                rc = -ENOMEM;
                goto err_alloc_label;
        }

        /* Compare volume label */
        if ( strcasecmp ( volume, label ) != 0 ) {
                rc = -ENOENT;
                goto err_compare;
        }

        /* Success */
        rc = 0;

 err_compare:
        free ( label );
 err_alloc_label:
 err_get_info:
        free ( info );
 err_alloc_info:
        return rc;
}
static int efi_local_open_root ( struct efi_local local,
EFI_HANDLE  device,
EFI_FILE_PROTOCOL **  root 
) [static]

Open root filesystem.

Parameters:
localLocal file
deviceDevice handle
rootRoot filesystem handle to fill in
Return values:
rcReturn status code

Definition at line 257 of file efi_local.c.

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseProtocol, DBGC, EEFI, efi_handle_name(), efi_image_handle, EFI_OPEN_PROTOCOL_GET_PROTOCOL, efi_simple_file_system_protocol_guid, efi_systab, fs, EFI_BOOT_SERVICES::OpenProtocol, rc, strerror(), and u.

Referenced by efi_local_open_volume().

                                                            {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        union {
                void *interface;
                EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs;
        } u;
        EFI_STATUS efirc;
        int rc;

        /* Open file system protocol */
        if ( ( efirc = bs->OpenProtocol ( device,
                                          &efi_simple_file_system_protocol_guid,
                                          &u.interface, efi_image_handle,
                                          device,
                                          EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
                rc = -EEFI ( efirc );
                DBGC ( local, "LOCAL %p could not open filesystem on %s: %s\n",
                       local, efi_handle_name ( device ), strerror ( rc ) );
                goto err_filesystem;
        }

        /* Open root directory */
        if ( ( efirc = u.fs->OpenVolume ( u.fs, root ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( local, "LOCAL %p could not open volume on %s: %s\n",
                       local, efi_handle_name ( device ), strerror ( rc ) );
                goto err_volume;
        }

        /* Success */
        rc = 0;

 err_volume:
        bs->CloseProtocol ( device, &efi_simple_file_system_protocol_guid,
                            efi_image_handle, device );
 err_filesystem:
        return rc;
}
static int efi_local_open_volume ( struct efi_local local,
const char *  volume 
) [static]

Open root filesystem of specified volume.

Parameters:
localLocal file
volumeVolume name, or NULL to use loaded image's device
Return values:
rcReturn status code

Definition at line 304 of file efi_local.c.

References EFI_SYSTEM_TABLE::BootServices, ByProtocol, _EFI_FILE_PROTOCOL::Close, DBGC, device, EFI_LOADED_IMAGE_PROTOCOL::DeviceHandle, EEFI, efi_handle_name(), efi_loaded_image, efi_local_check_volume_name(), efi_local_open_root(), efi_simple_file_system_protocol_guid, efi_systab, ENOENT, EFI_BOOT_SERVICES::FreePool, EFI_BOOT_SERVICES::LocateHandleBuffer, NULL, protocol, rc, efi_local::root, root, and strerror().

Referenced by efi_local_open().

                                                        {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_GUID *protocol = &efi_simple_file_system_protocol_guid;
        int ( * check ) ( struct efi_local *local, EFI_HANDLE device,
                          EFI_FILE_PROTOCOL *root, const char *volume );
        EFI_FILE_PROTOCOL *root;
        EFI_HANDLE *handles;
        EFI_HANDLE device;
        UINTN num_handles;
        UINTN i;
        EFI_STATUS efirc;
        int rc;

        /* Identify candidate handles */
        if ( volume ) {
                /* Locate all filesystem handles */
                if ( ( efirc = bs->LocateHandleBuffer ( ByProtocol, protocol,
                                                        NULL, &num_handles,
                                                        &handles ) ) != 0 ) {
                        rc = -EEFI ( efirc );
                        DBGC ( local, "LOCAL %p could not enumerate handles: "
                               "%s\n", local, strerror ( rc ) );
                        return rc;
                }
                check = efi_local_check_volume_name;
        } else {
                /* Use our loaded image's device handle */
                handles = &efi_loaded_image->DeviceHandle;
                num_handles = 1;
                check = NULL;
        }

        /* Find matching handle */
        for ( i = 0 ; i < num_handles ; i++ ) {

                /* Get this device handle */
                device = handles[i];

                /* Open root directory */
                if ( ( rc = efi_local_open_root ( local, device, &root ) ) != 0)
                        continue;

                /* Check volume name, if applicable */
                if ( ( check == NULL ) ||
                     ( ( rc = check ( local, device, root, volume ) ) == 0 ) ) {
                        DBGC ( local, "LOCAL %p using %s",
                               local, efi_handle_name ( device ) );
                        if ( volume )
                                DBGC ( local, " with label \"%s\"", volume );
                        DBGC ( local, "\n" );
                        local->root = root;
                        break;
                }

                /* Close root directory */
                root->Close ( root );
        }

        /* Free handles, if applicable */
        if ( volume )
                bs->FreePool ( handles );

        /* Fail if we found no matching handle */
        if ( ! local->root ) {
                DBGC ( local, "LOCAL %p found no matching handle\n", local );
                return -ENOENT;
        }

        return 0;
}
static int efi_local_open_resolved ( struct efi_local local,
const char *  resolved 
) [static]

Open fully-resolved path.

Parameters:
localLocal file
resolvedResolved path
Return values:
rcReturn status code

Definition at line 383 of file efi_local.c.

References DBGC, EEFI, EFI_FILE_MODE_READ, efi_snprintf(), efi_local::file, name, _EFI_FILE_PROTOCOL::Open, rc, efi_local::root, strerror(), and strlen().

Referenced by efi_local_open_path().

                                                            {
        size_t name_len = strlen ( resolved );
        CHAR16 name[ name_len + 1 /* wNUL */ ];
        EFI_FILE_PROTOCOL *file;
        EFI_STATUS efirc;
        int rc;

        /* Construct filename */
        efi_snprintf ( name, ( name_len + 1 /* wNUL */ ), "%s", resolved );

        /* Open file */
        if ( ( efirc = local->root->Open ( local->root, &file, name,
                                           EFI_FILE_MODE_READ, 0 ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( local, "LOCAL %p could not open \"%s\": %s\n",
                       local, resolved, strerror ( rc ) );
                return rc;
        }
        local->file = file;

        return 0;
}
static int efi_local_open_path ( struct efi_local local,
const char *  path 
) [static]

Open specified path.

Parameters:
localLocal file
pathPath to file
Return values:
rcReturn status code

Definition at line 414 of file efi_local.c.

References assert, base, container_of, DBGC, DBGC2, efi_devpath_len(), efi_loaded_image, efi_local_open_resolved(), END_DEVICE_PATH_TYPE, ENOMEM, EFI_LOADED_IMAGE_PROTOCOL::FilePath, free, Header, FILEPATH_DEVICE_PATH::Header, len, EFI_DEVICE_PATH_PROTOCOL::Length, memset(), FILEPATH_DEVICE_PATH::PathName, rc, resolve_path(), snprintf(), and EFI_DEVICE_PATH_PROTOCOL::Type.

Referenced by efi_local_open().

                                                                             {
        FILEPATH_DEVICE_PATH *fp = container_of ( efi_loaded_image->FilePath,
                                                  FILEPATH_DEVICE_PATH, Header);
        size_t fp_len = ( fp ? efi_devpath_len ( &fp->Header ) : 0 );
        char base[ fp_len / 2 /* Cannot exceed this length */ ];
        size_t remaining = sizeof ( base );
        size_t len;
        char *resolved;
        char *tmp;
        int rc;

        /* Construct base path to our own image, if possible */
        memset ( base, 0, sizeof ( base ) );
        tmp = base;
        while ( fp && ( fp->Header.Type != END_DEVICE_PATH_TYPE ) ) {
                len = snprintf ( tmp, remaining, "%ls", fp->PathName );
                assert ( len < remaining );
                tmp += len;
                remaining -= len;
                fp = ( ( ( void * ) fp ) + ( ( fp->Header.Length[1] << 8 ) |
                                             fp->Header.Length[0] ) );
        }
        DBGC2 ( local, "LOCAL %p base path \"%s\"\n",
                local, base );

        /* Convert to sane path separators */
        for ( tmp = base ; *tmp ; tmp++ ) {
                if ( *tmp == '\\' )
                        *tmp = '/';
        }

        /* Resolve path */
        resolved = resolve_path ( base, path );
        if ( ! resolved ) {
                rc = -ENOMEM;
                goto err_resolve;
        }

        /* Convert to insane path separators */
        for ( tmp = resolved ; *tmp ; tmp++ ) {
                if ( *tmp == '/' )
                        *tmp = '\\';
        }
        DBGC ( local, "LOCAL %p using \"%s\"\n",
               local, resolved );

        /* Open resolved path */
        if ( ( rc = efi_local_open_resolved ( local, resolved ) ) != 0 )
                goto err_open;

 err_open:
        free ( resolved );
 err_resolve:
        return rc;
}
static int efi_local_len ( struct efi_local local) [static]

Get file length.

Parameters:
localLocal file
Return values:
rcReturn status code

Definition at line 476 of file efi_local.c.

References DBGC, EEFI, efi_file_info_id, ENOMEM, efi_local::file, EFI_FILE_INFO::FileSize, free, _EFI_FILE_PROTOCOL::GetInfo, info, efi_local::len, malloc(), NULL, rc, size, and strerror().

Referenced by efi_local_open().

                                                     {
        EFI_FILE_PROTOCOL *file = local->file;
        EFI_FILE_INFO *info;
        EFI_STATUS efirc;
        UINTN size;
        int rc;

        /* Get size of file information */
        size = 0;
        file->GetInfo ( file, &efi_file_info_id, &size, NULL );

        /* Allocate file information */
        info = malloc ( size );
        if ( ! info ) {
                rc = -ENOMEM;
                goto err_alloc;
        }

        /* Get file information */
        if ( ( efirc = file->GetInfo ( file, &efi_file_info_id, &size,
                                       info ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( local, "LOCAL %p could not get file info: %s\n",
                       local, strerror ( rc ) );
                goto err_info;
        }

        /* Record file length */
        local->len = info->FileSize;

        /* Success */
        rc = 0;

 err_info:
        free ( info );
 err_alloc:
        return rc;
}
static int efi_local_open ( struct interface xfer,
struct uri uri 
) [static]

Open local file.

Parameters:
xferData transfer interface
uriRequest URI
Return values:
rcReturn status code

Definition at line 522 of file efi_local.c.

References efi_local_close(), efi_local_len(), efi_local_open_path(), efi_local_open_volume(), ENOMEM, uri::host, intf_init(), intf_plug_plug(), NULL, uri::opaque, uri::path, efi_local::process, process_init(), rc, ref_init, ref_put, efi_local::refcnt, efi_local::xfer, and zalloc().

                                                                      {
        struct efi_local *local;
        const char *volume;
        const char *path;
        int rc;

        /* Parse URI */
        volume = ( ( uri->host && uri->host[0] ) ? uri->host : NULL );
        path = ( uri->opaque ? uri->opaque : uri->path );

        /* Allocate and initialise structure */
        local = zalloc ( sizeof ( *local ) );
        if ( ! local ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        ref_init ( &local->refcnt, NULL );
        intf_init ( &local->xfer, &efi_local_xfer_desc, &local->refcnt );
        process_init ( &local->process, &efi_local_process_desc,
                       &local->refcnt );

        /* Open specified volume */
        if ( ( rc = efi_local_open_volume ( local, volume ) ) != 0 )
                goto err_open_root;

        /* Open specified path */
        if ( ( rc = efi_local_open_path ( local, path ) ) != 0 )
                goto err_open_file;

        /* Get length of file */
        if ( ( rc = efi_local_len ( local ) ) != 0 )
                goto err_len;

        /* Attach to parent interface, mortalise self, and return */
        intf_plug_plug ( &local->xfer, xfer );
        ref_put ( &local->refcnt );
        return 0;

 err_len:
 err_open_file:
 err_open_root:
        efi_local_close ( local, 0 );
        ref_put ( &local->refcnt );
 err_alloc:
        return rc;
}

Variable Documentation

Initial value:

Data transfer interface operations.

Definition at line 170 of file efi_local.c.

Initial value:

Data transfer interface descriptor.

Definition at line 176 of file efi_local.c.

Initial value:

Process descriptor.

Definition at line 180 of file efi_local.c.

struct uri_opener efi_local_uri_opener __uri_opener
Initial value:
 {
        .scheme = "file",
        .open   = efi_local_open,
}

EFI local file URI opener.

Definition at line 570 of file efi_local.c.