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

EFI block device protocols. More...

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ipxe/refcnt.h>
#include <ipxe/list.h>
#include <ipxe/uri.h>
#include <ipxe/interface.h>
#include <ipxe/blockdev.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/retry.h>
#include <ipxe/timer.h>
#include <ipxe/process.h>
#include <ipxe/sanboot.h>
#include <ipxe/iso9660.h>
#include <ipxe/acpi.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/BlockIo.h>
#include <ipxe/efi/Protocol/SimpleFileSystem.h>
#include <ipxe/efi/Protocol/AcpiTable.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_snp.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_block.h>

Go to the source code of this file.

Data Structures

struct  efi_block_vendor_path
 An iPXE EFI block device vendor device path. More...
struct  efi_block_data
 EFI SAN device private data. More...
struct  efi_acpi_table
 An installed ACPI table. More...

Defines

#define IPXE_BLOCK_DEVICE_PATH_GUID
 iPXE EFI block device vendor device path GUID

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 EFI_REQUEST_PROTOCOL (EFI_ACPI_TABLE_PROTOCOL,&acpi)
static int efi_block_rw (struct san_device *sandev, uint64_t lba, void *data, size_t len, int(*sandev_rw)(struct san_device *sandev, uint64_t lba, unsigned int count, userptr_t buffer))
 Read from or write to EFI block device.
static EFI_STATUS EFIAPI efi_block_io_reset (EFI_BLOCK_IO_PROTOCOL *block_io, BOOLEAN verify __unused)
 Reset EFI block device.
static EFI_STATUS EFIAPI efi_block_io_read (EFI_BLOCK_IO_PROTOCOL *block_io, UINT32 media __unused, EFI_LBA lba, UINTN len, VOID *data)
 Read from EFI block device.
static EFI_STATUS EFIAPI efi_block_io_write (EFI_BLOCK_IO_PROTOCOL *block_io, UINT32 media __unused, EFI_LBA lba, UINTN len, VOID *data)
 Write to EFI block device.
static EFI_STATUS EFIAPI efi_block_io_flush (EFI_BLOCK_IO_PROTOCOL *block_io)
 Flush data to EFI block device.
static void efi_block_connect (struct san_device *sandev)
 Connect all possible drivers to EFI block device.
static int efi_block_hook (unsigned int drive, struct uri **uris, unsigned int count, unsigned int flags)
 Hook EFI block device.
static void efi_block_unhook (unsigned int drive)
 Unhook EFI block device.
static LIST_HEAD (efi_acpi_tables)
 List of installed ACPI tables.
static int efi_block_install (struct acpi_header *hdr)
 Install ACPI table.
static int efi_block_describe (void)
 Describe EFI block devices.
static int efi_block_boot_image (struct san_device *sandev, EFI_HANDLE handle, const char *filename, EFI_HANDLE *image)
 Try booting from child device of EFI block device.
static int efi_block_boot (unsigned int drive, const char *filename)
 Boot from EFI block device.
 PROVIDE_SANBOOT (efi, san_hook, efi_block_hook)
 PROVIDE_SANBOOT (efi, san_unhook, efi_block_unhook)
 PROVIDE_SANBOOT (efi, san_describe, efi_block_describe)
 PROVIDE_SANBOOT (efi, san_boot, efi_block_boot)

Variables

static EFI_ACPI_TABLE_PROTOCOLacpi
 ACPI table protocol protocol.
static wchar_t efi_block_boot_filename [] = EFI_REMOVABLE_MEDIA_FILE_NAME
 Boot filename.
static EFI_GUID ipxe_block_device_path_guid = IPXE_BLOCK_DEVICE_PATH_GUID
 iPXE EFI block device vendor device path GUID

Detailed Description

EFI block device protocols.

Definition in file efi_block.c.


Define Documentation

Value:
{ 0x8998b594, 0xf531, 0x4e87,                                   \
          { 0x8b, 0xdf, 0x8f, 0x88, 0x54, 0x3e, 0x99, 0xd4 } }

iPXE EFI block device vendor device path GUID

Definition at line 68 of file efi_block.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int efi_block_rw ( struct san_device sandev,
uint64_t  lba,
void *  data,
size_t  len,
int(*)(struct san_device *sandev, uint64_t lba, unsigned int count, userptr_t buffer sandev_rw 
) [static]

Read from or write to EFI block device.

Parameters:
sandevSAN device
lbaStarting LBA
dataData buffer
lenSize of buffer
sandev_rwSAN device read/write method
Return values:
rcReturn status code

Definition at line 108 of file efi_block.c.

References block, EFI_BLOCK_IO_MEDIA::BlockSize, count, DBGC, san_device::drive, EINVAL, len, efi_block_data::media, san_device::priv, rc, sandev_rw(), strerror(), and virt_to_user().

Referenced by efi_block_io_read(), and efi_block_io_write().

                                                                     {
        struct efi_block_data *block = sandev->priv;
        unsigned int count;
        int rc;

        /* Sanity check */
        count = ( len / block->media.BlockSize );
        if ( ( count * block->media.BlockSize ) != len ) {
                DBGC ( sandev, "EFIBLK %#02x impossible length %#zx\n",
                       sandev->drive, len );
                return -EINVAL;
        }

        /* Read from / write to block device */
        if ( ( rc = sandev_rw ( sandev, lba, count,
                                virt_to_user ( data ) ) ) != 0 ) {
                DBGC ( sandev, "EFIBLK %#02x I/O failed: %s\n",
                       sandev->drive, strerror ( rc ) );
                return rc;
        }

        return 0;
}
static EFI_STATUS EFIAPI efi_block_io_reset ( EFI_BLOCK_IO_PROTOCOL block_io,
BOOLEAN verify  __unused 
) [static]

Reset EFI block device.

Parameters:
block_ioBlock I/O protocol
verifyPerform extended verification
Return values:
efircEFI status code

Definition at line 143 of file efi_block.c.

References block, container_of, DBGC2, san_device::drive, efi_snp_claim(), efi_snp_release(), EFIRC, rc, efi_block_data::sandev, and sandev_reset().

Referenced by efi_block_hook().

                                                                        {
        struct efi_block_data *block =
                container_of ( block_io, struct efi_block_data, block_io );
        struct san_device *sandev = block->sandev;
        int rc;

        DBGC2 ( sandev, "EFIBLK %#02x reset\n", sandev->drive );
        efi_snp_claim();
        rc = sandev_reset ( sandev );
        efi_snp_release();
        return EFIRC ( rc );
}
static EFI_STATUS EFIAPI efi_block_io_read ( EFI_BLOCK_IO_PROTOCOL block_io,
UINT32 media  __unused,
EFI_LBA  lba,
UINTN  len,
VOID data 
) [static]

Read from EFI block device.

Parameters:
block_ioBlock I/O protocol
mediaMedia identifier
lbaStarting LBA
lenSize of buffer
dataData buffer
Return values:
efircEFI status code

Definition at line 168 of file efi_block.c.

References block, container_of, DBGC2, san_device::drive, efi_block_rw(), efi_snp_claim(), efi_snp_release(), EFIRC, rc, efi_block_data::sandev, and sandev_read().

Referenced by efi_block_hook().

                                                         {
        struct efi_block_data *block =
                container_of ( block_io, struct efi_block_data, block_io );
        struct san_device *sandev = block->sandev;
        int rc;

        DBGC2 ( sandev, "EFIBLK %#02x read LBA %#08llx to %p+%#08zx\n",
                sandev->drive, lba, data, ( ( size_t ) len ) );
        efi_snp_claim();
        rc = efi_block_rw ( sandev, lba, data, len, sandev_read );
        efi_snp_release();
        return EFIRC ( rc );
}
static EFI_STATUS EFIAPI efi_block_io_write ( EFI_BLOCK_IO_PROTOCOL block_io,
UINT32 media  __unused,
EFI_LBA  lba,
UINTN  len,
VOID data 
) [static]

Write to EFI block device.

Parameters:
block_ioBlock I/O protocol
mediaMedia identifier
lbaStarting LBA
lenSize of buffer
dataData buffer
Return values:
efircEFI status code

Definition at line 194 of file efi_block.c.

References block, container_of, DBGC2, san_device::drive, efi_block_rw(), efi_snp_claim(), efi_snp_release(), EFIRC, rc, efi_block_data::sandev, and sandev_write().

Referenced by efi_block_hook().

                                                          {
        struct efi_block_data *block =
                container_of ( block_io, struct efi_block_data, block_io );
        struct san_device *sandev = block->sandev;
        int rc;

        DBGC2 ( sandev, "EFIBLK %#02x write LBA %#08llx from %p+%#08zx\n",
                sandev->drive, lba, data, ( ( size_t ) len ) );
        efi_snp_claim();
        rc = efi_block_rw ( sandev, lba, data, len, sandev_write );
        efi_snp_release();
        return EFIRC ( rc );
}
static EFI_STATUS EFIAPI efi_block_io_flush ( EFI_BLOCK_IO_PROTOCOL block_io) [static]

Flush data to EFI block device.

Parameters:
block_ioBlock I/O protocol
Return values:
efircEFI status code

Definition at line 216 of file efi_block.c.

References block, container_of, DBGC2, san_device::drive, and efi_block_data::sandev.

Referenced by efi_block_hook().

                                                       {
        struct efi_block_data *block =
                container_of ( block_io, struct efi_block_data, block_io );
        struct san_device *sandev = block->sandev;

        DBGC2 ( sandev, "EFIBLK %#02x flush\n", sandev->drive );

        /* Nothing to do */
        return 0;
}
static void efi_block_connect ( struct san_device sandev) [static]

Connect all possible drivers to EFI block device.

Parameters:
sandevSAN device

Definition at line 232 of file efi_block.c.

References block, EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::ConnectController, DBGC, DBGC2, DBGC2_EFI_PROTOCOLS, san_device::drive, EEFI, efi_systab, efi_block_data::handle, NULL, san_device::priv, rc, and strerror().

Referenced by efi_block_boot(), and efi_block_hook().

                                                            {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        struct efi_block_data *block = sandev->priv;
        EFI_STATUS efirc;
        int rc;

        /* Try to connect all possible drivers to this block device */
        if ( ( efirc = bs->ConnectController ( block->handle, NULL,
                                               NULL, 1 ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( sandev, "EFIBLK %#02x could not connect drivers: %s\n",
                       sandev->drive, strerror ( rc ) );
                /* May not be an error; may already be connected */
        }
        DBGC2 ( sandev, "EFIBLK %#02x supports protocols:\n", sandev->drive );
        DBGC2_EFI_PROTOCOLS ( sandev, block->handle );
}
static int efi_block_hook ( unsigned int  drive,
struct uri **  uris,
unsigned int  count,
unsigned int  flags 
) [static]

Hook EFI block device.

Parameters:
driveDrive number
urisList of URIs
countNumber of URIs
flagsFlags
Return values:
driveDrive number, or negative error

Definition at line 259 of file efi_block.c.

References alloc_sandev(), block_device_capacity::blksize, san_device::blksize_shift, block, efi_block_data::block_io, block_device_capacity::blocks, EFI_BLOCK_IO_MEDIA::BlockSize, EFI_SYSTEM_TABLE::BootServices, san_device::capacity, DBG, DBGC, san_device::drive, drive, EEFI, efi_block_connect(), efi_block_io_flush(), efi_block_io_protocol_guid, EFI_BLOCK_IO_PROTOCOL_REVISION3, efi_block_io_read(), efi_block_io_reset(), efi_block_io_write(), efi_device_path_protocol_guid, efi_devpath_len(), efi_devpath_text(), efi_snprintf(), efi_systab, end, END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, ENODEV, ENOMEM, ENOTTY, _EFI_BLOCK_IO_PROTOCOL::FlushBlocks, format_uri(), VENDOR_DEVICE_PATH::Guid, efi_block_data::handle, HARDWARE_DEVICE_PATH, VENDOR_DEVICE_PATH::Header, HW_VENDOR_DP, EFI_BOOT_SERVICES::InstallMultipleProtocolInterfaces, ipxe_block_device_path_guid, last_opened_snpdev(), EFI_BLOCK_IO_MEDIA::LastBlock, len, EFI_DEVICE_PATH_PROTOCOL::Length, EFI_BLOCK_IO_MEDIA::LogicalBlocksPerPhysicalBlock, efi_block_data::media, _EFI_BLOCK_IO_PROTOCOL::Media, EFI_BLOCK_IO_MEDIA::MediaPresent, memcpy(), NULL, efi_snp_device::path, efi_block_data::path, san_device::priv, rc, _EFI_BLOCK_IO_PROTOCOL::ReadBlocks, register_sandev(), _EFI_BLOCK_IO_PROTOCOL::Reset, _EFI_BLOCK_IO_PROTOCOL::Revision, efi_block_data::sandev, sandev_put(), strerror(), EFI_DEVICE_PATH_PROTOCOL::SubType, EFI_DEVICE_PATH_PROTOCOL::Type, EFI_BOOT_SERVICES::UninstallMultipleProtocolInterfaces, unregister_sandev(), efi_block_vendor_path::uri, efi_block_vendor_path::vendor, vendor, and _EFI_BLOCK_IO_PROTOCOL::WriteBlocks.

                                                                     {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_DEVICE_PATH_PROTOCOL *end;
        struct efi_block_vendor_path *vendor;
        struct efi_snp_device *snpdev;
        struct san_device *sandev;
        struct efi_block_data *block;
        size_t prefix_len;
        size_t uri_len;
        size_t vendor_len;
        size_t len;
        char *uri_buf;
        EFI_STATUS efirc;
        int rc;

        /* Sanity check */
        if ( ! count ) {
                DBG ( "EFIBLK has no URIs\n" );
                rc = -ENOTTY;
                goto err_no_uris;
        }

        /* Find an appropriate parent device handle */
        snpdev = last_opened_snpdev();
        if ( ! snpdev ) {
                DBG ( "EFIBLK could not identify SNP device\n" );
                rc = -ENODEV;
                goto err_no_snpdev;
        }

        /* Calculate length of private data */
        prefix_len = efi_devpath_len ( snpdev->path );
        uri_len = format_uri ( uris[0], NULL, 0 );
        vendor_len = ( sizeof ( *vendor ) +
                       ( ( uri_len + 1 /* NUL */ ) * sizeof ( wchar_t ) ) );
        len = ( sizeof ( *block ) + uri_len + 1 /* NUL */ + prefix_len +
                vendor_len + sizeof ( *end ) );

        /* Allocate and initialise structure */
        sandev = alloc_sandev ( uris, count, len );
        if ( ! sandev ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        block = sandev->priv;
        block->sandev = sandev;
        block->media.MediaPresent = 1;
        block->media.LogicalBlocksPerPhysicalBlock = 1;
        block->block_io.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION3;
        block->block_io.Media = &block->media;
        block->block_io.Reset = efi_block_io_reset;
        block->block_io.ReadBlocks = efi_block_io_read;
        block->block_io.WriteBlocks = efi_block_io_write;
        block->block_io.FlushBlocks = efi_block_io_flush;
        uri_buf = ( ( ( void * ) block ) + sizeof ( *block ) );
        block->path = ( ( ( void * ) uri_buf ) + uri_len + 1 /* NUL */ );

        /* Construct device path */
        memcpy ( block->path, snpdev->path, prefix_len );
        vendor = ( ( ( void * ) block->path ) + prefix_len );
        vendor->vendor.Header.Type = HARDWARE_DEVICE_PATH;
        vendor->vendor.Header.SubType = HW_VENDOR_DP;
        vendor->vendor.Header.Length[0] = ( vendor_len & 0xff );
        vendor->vendor.Header.Length[1] = ( vendor_len >> 8 );
        memcpy ( &vendor->vendor.Guid, &ipxe_block_device_path_guid,
                 sizeof ( vendor->vendor.Guid ) );
        format_uri ( uris[0], uri_buf, ( uri_len + 1 /* NUL */ ) );
        efi_snprintf ( vendor->uri, ( uri_len + 1 /* NUL */ ), "%s", uri_buf );
        end = ( ( ( void * ) vendor ) + vendor_len );
        end->Type = END_DEVICE_PATH_TYPE;
        end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
        end->Length[0] = sizeof ( *end );
        DBGC ( sandev, "EFIBLK %#02x has device path %s\n",
               drive, efi_devpath_text ( block->path ) );

        /* Register SAN device */
        if ( ( rc = register_sandev ( sandev, drive, flags ) ) != 0 ) {
                DBGC ( sandev, "EFIBLK %#02x could not register: %s\n",
                       drive, strerror ( rc ) );
                goto err_register;
        }

        /* Update media descriptor */
        block->media.BlockSize =
                ( sandev->capacity.blksize << sandev->blksize_shift );
        block->media.LastBlock =
                ( ( sandev->capacity.blocks >> sandev->blksize_shift ) - 1 );

        /* Install protocols */
        if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
                        &block->handle,
                        &efi_block_io_protocol_guid, &block->block_io,
                        &efi_device_path_protocol_guid, block->path,
                        NULL ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( sandev, "EFIBLK %#02x could not install protocols: %s\n",
                       sandev->drive, strerror ( rc ) );
                goto err_install;
        }

        /* Connect all possible protocols */
        efi_block_connect ( sandev );

        return drive;

        bs->UninstallMultipleProtocolInterfaces (
                        block->handle,
                        &efi_block_io_protocol_guid, &block->block_io,
                        &efi_device_path_protocol_guid, block->path, NULL );
 err_install:
        unregister_sandev ( sandev );
 err_register:
        sandev_put ( sandev );
 err_alloc:
 err_no_snpdev:
 err_no_uris:
        return rc;
}
static void efi_block_unhook ( unsigned int  drive) [static]

Unhook EFI block device.

Parameters:
driveDrive number

Definition at line 384 of file efi_block.c.

References block, efi_block_data::block_io, EFI_SYSTEM_TABLE::BootServices, DBG, efi_block_io_protocol_guid, efi_device_path_protocol_guid, efi_systab, efi_block_data::handle, NULL, efi_block_data::path, san_device::priv, sandev_find(), sandev_put(), EFI_BOOT_SERVICES::UninstallMultipleProtocolInterfaces, and unregister_sandev().

                                                    {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        struct san_device *sandev;
        struct efi_block_data *block;

        /* Find SAN device */
        sandev = sandev_find ( drive );
        if ( ! sandev ) {
                DBG ( "EFIBLK cannot find drive %#02x\n", drive );
                return;
        }
        block = sandev->priv;

        /* Uninstall protocols */
        bs->UninstallMultipleProtocolInterfaces (
                        block->handle,
                        &efi_block_io_protocol_guid, &block->block_io,
                        &efi_device_path_protocol_guid, block->path, NULL );

        /* Unregister SAN device */
        unregister_sandev ( sandev );

        /* Drop reference to drive */
        sandev_put ( sandev );
}
static LIST_HEAD ( efi_acpi_tables  ) [static]

List of installed ACPI tables.

static int efi_block_install ( struct acpi_header hdr) [static]

Install ACPI table.

Parameters:
hdrACPI description header
Return values:
rcReturn status code

Definition at line 427 of file efi_block.c.

References acpi_fix_checksum(), acpi_name(), DBGC, DBGC_HDA, EEFI, ENOMEM, free, _EFI_ACPI_TABLE_PROTOCOL::InstallAcpiTable, efi_acpi_table::key, le32_to_cpu, acpi_header::length, efi_acpi_table::list, list_add_tail, list_del, acpi_header::oem_id, acpi_header::oem_table_id, rc, acpi_header::signature, strerror(), strncpy(), and zalloc().

Referenced by efi_block_describe().

                                                         {
        size_t len = le32_to_cpu ( hdr->length );
        struct efi_acpi_table *installed;
        EFI_STATUS efirc;
        int rc;

        /* Allocate installed table record */
        installed = zalloc ( sizeof ( *installed ) );
        if ( ! installed ) {
                rc = -ENOMEM;
                goto err_alloc;
        }

        /* Fill in common parameters */
        strncpy ( hdr->oem_id, "FENSYS", sizeof ( hdr->oem_id ) );
        strncpy ( hdr->oem_table_id, "iPXE", sizeof ( hdr->oem_table_id ) );

        /* Fix up ACPI checksum */
        acpi_fix_checksum ( hdr );

        /* Install table */
        if ( ( efirc = acpi->InstallAcpiTable ( acpi, hdr, len,
                                                &installed->key ) ) != 0 ){
                rc = -EEFI ( efirc );
                DBGC ( acpi, "EFIBLK could not install %s: %s\n",
                       acpi_name ( hdr->signature ), strerror ( rc ) );
                DBGC_HDA ( acpi, 0, hdr, len );
                goto err_install;
        }

        /* Add to list of installed tables */
        list_add_tail ( &installed->list, &efi_acpi_tables );

        DBGC ( acpi, "EFIBLK installed %s as ACPI table %#lx:\n",
               acpi_name ( hdr->signature ),
               ( ( unsigned long ) installed->key ) );
        DBGC_HDA ( acpi, 0, hdr, len );
        return 0;

        list_del ( &installed->list );
 err_install:
        free ( installed );
 err_alloc:
        return rc;
}
static int efi_block_describe ( void  ) [static]

Describe EFI block devices.

Return values:
rcReturn status code

Definition at line 478 of file efi_block.c.

References acpi_install(), DBG, DBGC, EEFI, efi_block_install(), ENOTSUP, free, efi_acpi_table::key, key, efi_acpi_table::list, list_del, list_for_each_entry_safe, rc, strerror(), and _EFI_ACPI_TABLE_PROTOCOL::UninstallAcpiTable.

                                       {
        struct efi_acpi_table *installed;
        struct efi_acpi_table *tmp;
        UINTN key;
        EFI_STATUS efirc;
        int rc;

        /* Sanity check */
        if ( ! acpi ) {
                DBG ( "EFIBLK has no ACPI table protocol\n" );
                return -ENOTSUP;
        }

        /* Uninstall any existing ACPI tables */
        list_for_each_entry_safe ( installed, tmp, &efi_acpi_tables, list ) {
                key = installed->key;
                if ( ( efirc = acpi->UninstallAcpiTable ( acpi, key ) ) != 0 ) {
                        rc = -EEFI ( efirc );
                        DBGC ( acpi, "EFIBLK could not uninstall ACPI table "
                               "%#lx: %s\n", ( ( unsigned long ) key ),
                               strerror ( rc ) );
                        /* Continue anyway */
                }
                list_del ( &installed->list );
                free ( installed );
        }

        /* Install ACPI tables */
        if ( ( rc = acpi_install ( efi_block_install ) ) != 0 )  {
                DBGC ( acpi, "EFIBLK could not install ACPI tables: %s\n",
                       strerror ( rc ) );
                return rc;
        }

        return 0;
}
static int efi_block_boot_image ( struct san_device sandev,
EFI_HANDLE  handle,
const char *  filename,
EFI_HANDLE image 
) [static]

Try booting from child device of EFI block device.

Parameters:
sandevSAN device
handleEFI handle
filenameFilename (or NULL to use default)
imageImage handle to fill in
Return values:
rcReturn status code

Definition at line 524 of file efi_block.c.

References block, EFI_SYSTEM_TABLE::BootServices, DBGC, san_device::drive, EEFI, efi_block_boot_filename, efi_device_path_protocol_guid, efi_devpath_end(), efi_devpath_len(), efi_devpath_text(), efi_image_handle, EFI_OPEN_PROTOCOL_GET_PROTOCOL, efi_sprintf, efi_systab, end, END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, ENOMEM, ENOTTY, FALSE, free, FILEPATH_DEVICE_PATH::Header, EFI_DEVICE_PATH_PROTOCOL::Length, EFI_BOOT_SERVICES::LoadImage, MEDIA_DEVICE_PATH, MEDIA_FILEPATH_DP, memcmp(), memcpy(), NULL, EFI_BOOT_SERVICES::OpenProtocol, efi_block_data::path, FILEPATH_DEVICE_PATH::PathName, san_device::priv, rc, SIZE_OF_FILEPATH_DEVICE_PATH, strerror(), strlen(), EFI_DEVICE_PATH_PROTOCOL::SubType, EFI_DEVICE_PATH_PROTOCOL::Type, and zalloc().

Referenced by efi_block_boot().

                                                                            {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        struct efi_block_data *block = sandev->priv;
        union {
                EFI_DEVICE_PATH_PROTOCOL *path;
                void *interface;
        } path;
        EFI_DEVICE_PATH_PROTOCOL *boot_path;
        FILEPATH_DEVICE_PATH *filepath;
        EFI_DEVICE_PATH_PROTOCOL *end;
        size_t prefix_len;
        size_t filepath_len;
        size_t boot_path_len;
        EFI_STATUS efirc;
        int rc;

        /* Identify device path */
        if ( ( efirc = bs->OpenProtocol ( handle,
                                          &efi_device_path_protocol_guid,
                                          &path.interface, efi_image_handle,
                                          handle,
                                          EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
                DBGC ( sandev, "EFIBLK %#02x found filesystem with no device "
                       "path??", sandev->drive );
                rc = -EEFI ( efirc );
                goto err_open_device_path;
        }

        /* Check if this device is a child of our block device */
        prefix_len = efi_devpath_len ( block->path );
        if ( memcmp ( path.path, block->path, prefix_len ) != 0 ) {
                /* Not a child device */
                rc = -ENOTTY;
                goto err_not_child;
        }
        DBGC ( sandev, "EFIBLK %#02x found child device %s\n",
               sandev->drive, efi_devpath_text ( path.path ) );

        /* Construct device path for boot image */
        end = efi_devpath_end ( path.path );
        prefix_len = ( ( ( void * ) end ) - ( ( void * ) path.path ) );
        filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
                         ( filename ?
                           ( ( strlen ( filename ) + 1 /* NUL */ ) *
                             sizeof ( filepath->PathName[0] ) ) :
                           sizeof ( efi_block_boot_filename ) ) );
        boot_path_len = ( prefix_len + filepath_len + sizeof ( *end ) );
        boot_path = zalloc ( boot_path_len );
        if ( ! boot_path ) {
                rc = -ENOMEM;
                goto err_alloc_path;
        }
        memcpy ( boot_path, path.path, prefix_len );
        filepath = ( ( ( void * ) boot_path ) + prefix_len );
        filepath->Header.Type = MEDIA_DEVICE_PATH;
        filepath->Header.SubType = MEDIA_FILEPATH_DP;
        filepath->Header.Length[0] = ( filepath_len & 0xff );
        filepath->Header.Length[1] = ( filepath_len >> 8 );
        if ( filename ) {
                efi_sprintf ( filepath->PathName, "%s", filename );
        } else {
                memcpy ( filepath->PathName, efi_block_boot_filename,
                         sizeof ( efi_block_boot_filename ) );
        }
        end = ( ( ( void * ) filepath ) + filepath_len );
        end->Type = END_DEVICE_PATH_TYPE;
        end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
        end->Length[0] = sizeof ( *end );
        DBGC ( sandev, "EFIBLK %#02x trying to load %s\n",
               sandev->drive, efi_devpath_text ( boot_path ) );

        /* Try loading boot image from this device */
        if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, boot_path,
                                       NULL, 0, image ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( sandev, "EFIBLK %#02x could not load image: %s\n",
                       sandev->drive, strerror ( rc ) );
                goto err_load_image;
        }

        /* Success */
        rc = 0;

 err_load_image:
        free ( boot_path );
 err_alloc_path:
 err_not_child:
 err_open_device_path:
        return rc;
}
static int efi_block_boot ( unsigned int  drive,
const char *  filename 
) [static]

Boot from EFI block device.

Parameters:
driveDrive number
filenameFilename (or NULL to use default)
Return values:
rcReturn status code

Definition at line 623 of file efi_block.c.

References EFI_SYSTEM_TABLE::BootServices, ByProtocol, count, DBG, DBGC, san_device::drive, EEFI, efi_block_boot_image(), efi_block_connect(), efi_simple_file_system_protocol_guid, efi_snp_claim(), efi_snp_release(), efi_systab, ENODEV, ENOENT, EFI_BOOT_SERVICES::FreePool, EFI_BOOT_SERVICES::LocateHandleBuffer, NULL, rc, sandev_find(), EFI_BOOT_SERVICES::StartImage, strerror(), and EFI_BOOT_SERVICES::UnloadImage.

                                                                       {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        struct san_device *sandev;
        EFI_HANDLE *handles;
        EFI_HANDLE image = NULL;
        UINTN count;
        unsigned int i;
        EFI_STATUS efirc;
        int rc;

        /* Find SAN device */
        sandev = sandev_find ( drive );
        if ( ! sandev ) {
                DBG ( "EFIBLK cannot find drive %#02x\n", drive );
                rc = -ENODEV;
                goto err_sandev_find;
        }

        /* Release SNP devices */
        efi_snp_release();

        /* Connect all possible protocols */
        efi_block_connect ( sandev );

        /* Locate all handles supporting the Simple File System protocol */
        if ( ( efirc = bs->LocateHandleBuffer (
                        ByProtocol, &efi_simple_file_system_protocol_guid,
                        NULL, &count, &handles ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( sandev, "EFIBLK %#02x cannot locate file systems: %s\n",
                       sandev->drive, strerror ( rc ) );
                goto err_locate_file_systems;
        }

        /* Try booting from any available child device containing a
         * suitable boot image.  This is something of a wild stab in
         * the dark, but should end up conforming to user expectations
         * most of the time.
         */
        rc = -ENOENT;
        for ( i = 0 ; i < count ; i++ ) {
                if ( ( rc = efi_block_boot_image ( sandev, handles[i], filename,
                                                   &image ) ) != 0 )
                        continue;
                DBGC ( sandev, "EFIBLK %#02x found boot image\n",
                       sandev->drive );
                efirc = bs->StartImage ( image, NULL, NULL );
                rc = ( efirc ? -EEFI ( efirc ) : 0 );
                bs->UnloadImage ( image );
                DBGC ( sandev, "EFIBLK %#02x boot image returned: %s\n",
                       sandev->drive, strerror ( rc ) );
                break;
        }

        bs->FreePool ( handles );
 err_locate_file_systems:
        efi_snp_claim();
 err_sandev_find:
        return rc;
}
PROVIDE_SANBOOT ( efi  ,
san_hook  ,
efi_block_hook   
)
PROVIDE_SANBOOT ( efi  ,
san_boot  ,
efi_block_boot   
)

Variable Documentation

ACPI table protocol protocol.

Definition at line 61 of file efi_block.c.

Referenced by ibft_install(), and nii_pci_open().

wchar_t efi_block_boot_filename[] = EFI_REMOVABLE_MEDIA_FILE_NAME [static]

Boot filename.

Definition at line 65 of file efi_block.c.

Referenced by efi_block_boot_image().

iPXE EFI block device vendor device path GUID

Definition at line 74 of file efi_block.c.

Referenced by efi_block_hook().