]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[efi] Expose downloaded images via EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
authorMichael Brown <mcb30@ipxe.org>
Wed, 13 Mar 2013 22:36:32 +0000 (22:36 +0000)
committerMichael Brown <mcb30@ipxe.org>
Wed, 13 Mar 2013 22:42:16 +0000 (22:42 +0000)
Expose iPXE's images as a UEFI file system, allowing the booted image
to access all images downloaded by iPXE.

This functionality is complementary to the custom iPXE download
protocol.  The iPXE download protocol allows a booted image to utilise
iPXE to download arbitrary URIs, but requires the booted image to
specifically support the custom iPXE download protocol.  The new
functionality limits the booted image to accessing only files that
were already downloaded by iPXE (e.g. as part of a script), but can
work with any generic UEFI image (e.g. the UEFI shell).  Both
protocols are provided simultaneously, and are attached to the SNP
device handle.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/image/efi_image.c
src/include/ipxe/efi/Guid/FileInfo.h [new file with mode: 0644]
src/include/ipxe/efi/Guid/FileSystemInfo.h [new file with mode: 0644]
src/include/ipxe/efi/Protocol/BlockIo.h [new file with mode: 0644]
src/include/ipxe/efi/Protocol/SimpleFileSystem.h [new file with mode: 0644]
src/include/ipxe/efi/efi.h
src/include/ipxe/efi/efi_download.h [moved from src/include/ipxe/efi/ipxe_download.h with 97% similarity]
src/include/ipxe/efi/efi_file.h [new file with mode: 0644]
src/interface/efi/efi_download.c
src/interface/efi/efi_file.c [new file with mode: 0644]

index bee966e7134ff4d252a8b8c7f8721052e665c48d..4b04d36f486e92f1ba5ac67ef6a87bd20e508fc8 100644 (file)
@@ -21,7 +21,13 @@ FILE_LICENCE ( GPL2_OR_LATER );
 
 #include <errno.h>
 #include <stdlib.h>
+#include <wchar.h>
 #include <ipxe/efi/efi.h>
+#include <ipxe/efi/efi_snp.h>
+#include <ipxe/efi/efi_download.h>
+#include <ipxe/efi/efi_file.h>
+#include <ipxe/efi/efi_driver.h>
+#include <ipxe/efi/efi_strings.h>
 #include <ipxe/image.h>
 #include <ipxe/init.h>
 #include <ipxe/features.h>
@@ -34,79 +40,75 @@ static EFI_GUID efi_loaded_image_protocol_guid =
        EFI_LOADED_IMAGE_PROTOCOL_GUID;
 
 /**
- * Create a Unicode command line for the image
+ * Create device path for image
+ *
+ * @v image            EFI image
+ * @v parent           Parent device path
+ * @ret path           Device path, or NULL on failure
+ *
+ * The caller must eventually free() the device path.
+ */
+static EFI_DEVICE_PATH_PROTOCOL * efi_image_path ( struct image *image,
+                                                  EFI_DEVICE_PATH *parent ) {
+       EFI_DEVICE_PATH_PROTOCOL *path;
+       FILEPATH_DEVICE_PATH *filepath;
+       EFI_DEVICE_PATH_PROTOCOL *end;
+       size_t name_len;
+       size_t prefix_len;
+       size_t filepath_len;
+       size_t len;
+
+       /* Calculate device path lengths */
+       end = efi_devpath_end ( parent );
+       prefix_len = ( ( void * ) end - ( void * ) parent );
+       name_len = strlen ( image->name );
+       filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
+                        ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
+       len = ( prefix_len + filepath_len + sizeof ( *end ) );
+
+       /* Allocate device path */
+       path = zalloc ( len );
+       if ( ! path )
+               return NULL;
+
+       /* Construct device path */
+       memcpy ( path, parent, prefix_len );
+       filepath = ( ( ( void * ) 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 );
+       efi_snprintf ( filepath->PathName, ( name_len + 1 /* NUL */ ),
+                      "%s", image->name );
+       end = ( ( ( void * ) filepath ) + filepath_len );
+       end->Type = END_DEVICE_PATH_TYPE;
+       end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
+       end->Length[0] = sizeof ( *end );
+
+       return path;
+}
+
+/**
+ * Create command line for image
  *
  * @v image             EFI image
- * @v devpath_out       Device path to pass to image (output)
- * @v cmdline_out       Unicode command line (output)
- * @v cmdline_len_out   Length of command line in bytes (output)
- * @ret rc              Return status code
+ * @ret cmdline                Command line, or NULL on failure
  */
-static int efi_image_make_cmdline ( struct image *image,
-                                   EFI_DEVICE_PATH **devpath_out,
-                                   VOID **cmdline_out,
-                                   UINT32 *cmdline_len_out ) {
-       char *uri;
-       size_t uri_len;
-       FILEPATH_DEVICE_PATH *devpath;
-       EFI_DEVICE_PATH *endpath;
-       size_t devpath_len;
-       CHAR16 *cmdline;
-       UINT32 cmdline_len;
-       size_t args_len = 0;
-       UINT32 i;
-
-       /* Get the URI string of the image */
-       uri_len = unparse_uri ( NULL, 0, image->uri, URI_ALL ) + 1;
-
-       /* Compute final command line length */
-       if ( image->cmdline ) {
-               args_len = strlen ( image->cmdline ) + 1;
-       }
-       cmdline_len = args_len + uri_len;
+static wchar_t * efi_image_cmdline ( struct image *image ) {
+       wchar_t *cmdline;
+       size_t len;
 
-       /* Allocate space for the uri, final command line and device path */
-       cmdline = malloc ( cmdline_len * sizeof ( CHAR16 ) + uri_len
-                          + SIZE_OF_FILEPATH_DEVICE_PATH
-                          + uri_len * sizeof ( CHAR16 )
-                          + sizeof ( EFI_DEVICE_PATH ) );
+       len = ( strlen ( image->name ) +
+               ( image->cmdline ?
+                 ( 1 /* " " */ + strlen ( image->cmdline ) ) : 0 ) );
+       cmdline = zalloc ( ( len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
        if ( ! cmdline )
-               return -ENOMEM;
-       uri = (char *) ( cmdline + cmdline_len );
-       devpath = (FILEPATH_DEVICE_PATH *) ( uri + uri_len );
-       endpath = (EFI_DEVICE_PATH *) ( (char *) devpath
-                                       + SIZE_OF_FILEPATH_DEVICE_PATH
-                                       + uri_len * sizeof ( CHAR16 ) );
-
-       /* Build the iPXE device path */
-       devpath->Header.Type = MEDIA_DEVICE_PATH;
-       devpath->Header.SubType = MEDIA_FILEPATH_DP;
-       devpath_len = SIZE_OF_FILEPATH_DEVICE_PATH
-                       + uri_len * sizeof ( CHAR16 );
-       devpath->Header.Length[0] = devpath_len & 0xFF;
-       devpath->Header.Length[1] = devpath_len >> 8;
-       endpath->Type = END_DEVICE_PATH_TYPE;
-       endpath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
-       endpath->Length[0] = 4;
-       endpath->Length[1] = 0;
-       unparse_uri ( uri, uri_len, image->uri, URI_ALL );
-
-       /* Convert to Unicode */
-       for ( i = 0 ; i < uri_len ; i++ ) {
-               cmdline[i] = uri[i];
-               devpath->PathName[i] = uri[i];
-       }
-       if ( image->cmdline ) {
-               cmdline[uri_len - 1] = ' ';
-       }
-       for ( i = 0 ; i < args_len ; i++ ) {
-               cmdline[i + uri_len] = image->cmdline[i];
-       }
-
-       *devpath_out = &devpath->Header;
-       *cmdline_out = cmdline;
-       *cmdline_len_out = cmdline_len * sizeof ( CHAR16 );
-       return 0;
+               return NULL;
+       efi_snprintf ( cmdline, ( len + 1 /* NUL */ ), "%s%s%s",
+                      image->name,
+                      ( image->cmdline ? " " : "" ),
+                      ( image->cmdline ? image->cmdline : "" ) );
+       return cmdline;
 }
 
 /**
@@ -117,19 +119,60 @@ static int efi_image_make_cmdline ( struct image *image,
  */
 static int efi_image_exec ( struct image *image ) {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
+       struct efi_snp_device *snpdev;
+       EFI_DEVICE_PATH_PROTOCOL *path;
        union {
                EFI_LOADED_IMAGE_PROTOCOL *image;
                void *interface;
        } loaded;
        EFI_HANDLE handle;
-       EFI_HANDLE device_handle = NULL;
-       UINTN exit_data_size;
-       CHAR16 *exit_data;
+       wchar_t *cmdline;
        EFI_STATUS efirc;
        int rc;
 
+       /* Find an appropriate device handle to use */
+       snpdev = last_opened_snpdev();
+       if ( ! snpdev ) {
+               DBGC ( image, "EFIIMAGE %p could not identify SNP device\n",
+                      image );
+               rc = -ENODEV;
+               goto err_no_snpdev;
+       }
+
+       /* Install file I/O protocols */
+       if ( ( rc = efi_file_install ( &snpdev->handle ) ) != 0 ) {
+               DBGC ( image, "EFIIMAGE %p could not install file protocol: "
+                      "%s\n", image, strerror ( rc ) );
+               goto err_file_install;
+       }
+
+       /* Install iPXE download protocol */
+       if ( ( rc = efi_download_install ( &snpdev->handle ) ) != 0 ) {
+               DBGC ( image, "EFIIMAGE %p could not install iPXE download "
+                      "protocol: %s\n", image, strerror ( rc ) );
+               goto err_download_install;
+       }
+
+       /* Create device path for image */
+       path = efi_image_path ( image, &snpdev->path );
+       if ( ! path ) {
+               DBGC ( image, "EFIIMAGE %p could not create device path\n",
+                      image );
+               rc = -ENOMEM;
+               goto err_image_path;
+       }
+
+       /* Create command line for image */
+       cmdline = efi_image_cmdline ( image );
+       if ( ! cmdline ) {
+               DBGC ( image, "EFIIMAGE %p could not create command line\n",
+                      image );
+               rc = -ENOMEM;
+               goto err_cmdline;
+       }
+
        /* Attempt loading image */
-       if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
+       if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, path,
                                       user_to_virt ( image->data, 0 ),
                                       image->len, &handle ) ) != 0 ) {
                /* Not an EFI image */
@@ -149,22 +192,19 @@ static int efi_image_exec ( struct image *image ) {
                goto err_open_protocol;
        }
 
-       /* Pass an iPXE download protocol to the image */
-       if ( ( rc = efi_download_install ( &device_handle ) ) != 0 ) {
-               DBGC ( image, "EFIIMAGE %p could not install iPXE download "
-                      "protocol: %s\n", image, strerror ( rc ) );
-               goto err_download_install;
-       }
-       loaded.image->DeviceHandle = device_handle;
-       loaded.image->ParentHandle = efi_loaded_image;
-       if ( ( rc = efi_image_make_cmdline ( image, &loaded.image->FilePath,
-                                      &loaded.image->LoadOptions,
-                                      &loaded.image->LoadOptionsSize ) ) != 0 )
-               goto err_make_cmdline;
+       /* Sanity checks */
+       assert ( loaded.image->ParentHandle == efi_image_handle );
+       assert ( loaded.image->DeviceHandle == snpdev->handle );
+       assert ( loaded.image->LoadOptionsSize == 0 );
+       assert ( loaded.image->LoadOptions == NULL );
+
+       /* Set command line */
+       loaded.image->LoadOptions = cmdline;
+       loaded.image->LoadOptionsSize =
+               ( ( wcslen ( cmdline ) + 1 /* NUL */ ) * sizeof ( wchar_t ) );
 
        /* Start the image */
-       if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
-                                       &exit_data ) ) != 0 ) {
+       if ( ( efirc = bs->StartImage ( handle, 0, NULL ) ) != 0 ) {
                DBGC ( image, "EFIIMAGE %p returned with status %s\n",
                       image, efi_strerror ( efirc ) );
                rc = EFIRC_TO_RC ( efirc );
@@ -175,17 +215,24 @@ static int efi_image_exec ( struct image *image ) {
        rc = 0;
 
  err_start_image:
-       free ( loaded.image->LoadOptions );
- err_make_cmdline:
-       efi_download_uninstall ( device_handle );
- err_download_install:
  err_open_protocol:
        /* Unload the image.  We can't leave it loaded, because we
         * have no "unload" operation.
         */
-       bs->UnloadImage ( handle );
+       if ( ( efirc = bs->UnloadImage ( handle ) ) != 0 ) {
+               DBGC ( image, "EFIIMAGE %p could not unload: %s\n",
+                      image, efi_strerror ( efirc ) );
+       }
  err_load_image:
-
+       free ( cmdline );
+ err_cmdline:
+       free ( path );
+ err_image_path:
+       efi_download_uninstall ( snpdev->handle );
+ err_download_install:
+       efi_file_uninstall ( snpdev->handle );
+ err_file_install:
+ err_no_snpdev:
        return rc;
 }
 
diff --git a/src/include/ipxe/efi/Guid/FileInfo.h b/src/include/ipxe/efi/Guid/FileInfo.h
new file mode 100644 (file)
index 0000000..21fd389
--- /dev/null
@@ -0,0 +1,73 @@
+/** @file
+  Provides a GUID and a data structure that can be used with EFI_FILE_PROTOCOL.SetInfo()
+  and EFI_FILE_PROTOCOL.GetInfo() to set or get generic file information.
+  This GUID is defined in UEFI specification.
+
+Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials are licensed and made available under
+the terms and conditions of the BSD License that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __FILE_INFO_H__
+#define __FILE_INFO_H__
+
+FILE_LICENCE ( BSD3 );
+
+#define EFI_FILE_INFO_ID \
+  { \
+    0x9576e92, 0x6d3f, 0x11d2, {0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
+  }
+
+typedef struct {
+  ///
+  /// The size of the EFI_FILE_INFO structure, including the Null-terminated FileName string.
+  ///
+  UINT64    Size;
+  ///
+  /// The size of the file in bytes.
+  ///
+  UINT64    FileSize;
+  ///
+  /// PhysicalSize The amount of physical space the file consumes on the file system volume.
+  ///
+  UINT64    PhysicalSize;
+  ///
+  /// The time the file was created.
+  ///
+  EFI_TIME  CreateTime;
+  ///
+  /// The time when the file was last accessed.
+  ///
+  EFI_TIME  LastAccessTime;
+  ///
+  /// The time when the file's contents were last modified.
+  ///
+  EFI_TIME  ModificationTime;
+  ///
+  /// The attribute bits for the file.
+  ///
+  UINT64    Attribute;
+  ///
+  /// The Null-terminated name of the file.
+  ///
+  CHAR16    FileName[1];
+} EFI_FILE_INFO;
+
+///
+/// The FileName field of the EFI_FILE_INFO data structure is variable length.
+/// Whenever code needs to know the size of the EFI_FILE_INFO data structure, it needs to
+/// be the size of the data structure without the FileName field.  The following macro
+/// computes this size correctly no matter how big the FileName array is declared.
+/// This is required to make the EFI_FILE_INFO data structure ANSI compilant.
+///
+#define SIZE_OF_EFI_FILE_INFO OFFSET_OF (EFI_FILE_INFO, FileName)
+
+extern EFI_GUID gEfiFileInfoGuid;
+
+#endif
diff --git a/src/include/ipxe/efi/Guid/FileSystemInfo.h b/src/include/ipxe/efi/Guid/FileSystemInfo.h
new file mode 100644 (file)
index 0000000..504b793
--- /dev/null
@@ -0,0 +1,65 @@
+/** @file
+  Provides a GUID and a data structure that can be used with EFI_FILE_PROTOCOL.GetInfo()
+  or EFI_FILE_PROTOCOL.SetInfo() to get or set information about the system's volume.
+  This GUID is defined in UEFI specification.
+
+Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials are licensed and made available under
+the terms and conditions of the BSD License that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __FILE_SYSTEM_INFO_H__
+#define __FILE_SYSTEM_INFO_H__
+
+FILE_LICENCE ( BSD3 );
+
+#define EFI_FILE_SYSTEM_INFO_ID \
+  { \
+    0x9576e93, 0x6d3f, 0x11d2, {0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
+  }
+
+typedef struct {
+  ///
+  /// The size of the EFI_FILE_SYSTEM_INFO structure, including the Null-terminated VolumeLabel string.
+  ///
+  UINT64  Size;
+  ///
+  /// TRUE if the volume only supports read access.
+  ///
+  BOOLEAN ReadOnly;
+  ///
+  /// The number of bytes managed by the file system.
+  ///
+  UINT64  VolumeSize;
+  ///
+  /// The number of available bytes for use by the file system.
+  ///
+  UINT64  FreeSpace;
+  ///
+  /// The nominal block size by which files are typically grown.
+  ///
+  UINT32  BlockSize;
+  ///
+  /// The Null-terminated string that is the volume's label.
+  ///
+  CHAR16  VolumeLabel[1];
+} EFI_FILE_SYSTEM_INFO;
+
+///
+/// The VolumeLabel field of the EFI_FILE_SYSTEM_INFO data structure is variable length.
+/// Whenever code needs to know the size of the EFI_FILE_SYSTEM_INFO data structure, it needs
+/// to be the size of the data structure without the VolumeLable field.  The following macro
+/// computes this size correctly no matter how big the VolumeLable array is declared.
+/// This is required to make the EFI_FILE_SYSTEM_INFO data structure ANSI compilant.
+///
+#define SIZE_OF_EFI_FILE_SYSTEM_INFO  OFFSET_OF (EFI_FILE_SYSTEM_INFO, VolumeLabel)
+
+extern EFI_GUID gEfiFileSystemInfoGuid;
+
+#endif
diff --git a/src/include/ipxe/efi/Protocol/BlockIo.h b/src/include/ipxe/efi/Protocol/BlockIo.h
new file mode 100644 (file)
index 0000000..f45154b
--- /dev/null
@@ -0,0 +1,243 @@
+/** @file
+  Block IO protocol as defined in the UEFI 2.0 specification.
+
+  The Block IO protocol is used to abstract block devices like hard drives,
+  DVD-ROMs and floppy drives.
+
+  Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __BLOCK_IO_H__
+#define __BLOCK_IO_H__
+
+FILE_LICENCE ( BSD3 );
+
+#define EFI_BLOCK_IO_PROTOCOL_GUID \
+  { \
+    0x964e5b21, 0x6459, 0x11d2, {0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
+  }
+
+typedef struct _EFI_BLOCK_IO_PROTOCOL  EFI_BLOCK_IO_PROTOCOL;
+
+///
+/// Protocol GUID name defined in EFI1.1.
+///
+#define BLOCK_IO_PROTOCOL       EFI_BLOCK_IO_PROTOCOL_GUID
+
+///
+/// Protocol defined in EFI1.1.
+///
+typedef EFI_BLOCK_IO_PROTOCOL   EFI_BLOCK_IO;
+
+/**
+  Reset the Block Device.
+
+  @param  This                 Indicates a pointer to the calling context.
+  @param  ExtendedVerification Driver may perform diagnostics on reset.
+
+  @retval EFI_SUCCESS          The device was reset.
+  @retval EFI_DEVICE_ERROR     The device is not functioning properly and could
+                               not be reset.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_BLOCK_RESET)(
+  IN EFI_BLOCK_IO_PROTOCOL          *This,
+  IN BOOLEAN                        ExtendedVerification
+  );
+
+/**
+  Read BufferSize bytes from Lba into Buffer.
+
+  @param  This       Indicates a pointer to the calling context.
+  @param  MediaId    Id of the media, changes every time the media is replaced.
+  @param  Lba        The starting Logical Block Address to read from
+  @param  BufferSize Size of Buffer, must be a multiple of device block size.
+  @param  Buffer     A pointer to the destination buffer for the data. The caller is
+                     responsible for either having implicit or explicit ownership of the buffer.
+
+  @retval EFI_SUCCESS           The data was read correctly from the device.
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing the read.
+  @retval EFI_NO_MEDIA          There is no media in the device.
+  @retval EFI_MEDIA_CHANGED     The MediaId does not matched the current device.
+  @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.
+  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
+                                or the buffer is not on proper alignment.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_BLOCK_READ)(
+  IN EFI_BLOCK_IO_PROTOCOL          *This,
+  IN UINT32                         MediaId,
+  IN EFI_LBA                        Lba,
+  IN UINTN                          BufferSize,
+  OUT VOID                          *Buffer
+  );
+
+/**
+  Write BufferSize bytes from Lba into Buffer.
+
+  @param  This       Indicates a pointer to the calling context.
+  @param  MediaId    The media ID that the write request is for.
+  @param  Lba        The starting logical block address to be written. The caller is
+                     responsible for writing to only legitimate locations.
+  @param  BufferSize Size of Buffer, must be a multiple of device block size.
+  @param  Buffer     A pointer to the source buffer for the data.
+
+  @retval EFI_SUCCESS           The data was written correctly to the device.
+  @retval EFI_WRITE_PROTECTED   The device can not be written to.
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing the write.
+  @retval EFI_NO_MEDIA          There is no media in the device.
+  @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.
+  @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.
+  @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
+                                or the buffer is not on proper alignment.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_BLOCK_WRITE)(
+  IN EFI_BLOCK_IO_PROTOCOL          *This,
+  IN UINT32                         MediaId,
+  IN EFI_LBA                        Lba,
+  IN UINTN                          BufferSize,
+  IN VOID                           *Buffer
+  );
+
+/**
+  Flush the Block Device.
+
+  @param  This              Indicates a pointer to the calling context.
+
+  @retval EFI_SUCCESS       All outstanding data was written to the device
+  @retval EFI_DEVICE_ERROR  The device reported an error while writting back the data
+  @retval EFI_NO_MEDIA      There is no media in the device.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_BLOCK_FLUSH)(
+  IN EFI_BLOCK_IO_PROTOCOL  *This
+  );
+
+/**
+  Block IO read only mode data and updated only via members of BlockIO
+**/
+typedef struct {
+  ///
+  /// The curent media Id. If the media changes, this value is changed.
+  ///
+  UINT32  MediaId;
+
+  ///
+  /// TRUE if the media is removable; otherwise, FALSE.
+  ///
+  BOOLEAN RemovableMedia;
+
+  ///
+  /// TRUE if there is a media currently present in the device;
+  /// othersise, FALSE. THis field shows the media present status
+  /// as of the most recent ReadBlocks() or WriteBlocks() call.
+  ///
+  BOOLEAN MediaPresent;
+
+  ///
+  /// TRUE if LBA 0 is the first block of a partition; otherwise
+  /// FALSE. For media with only one partition this would be TRUE.
+  ///
+  BOOLEAN LogicalPartition;
+
+  ///
+  /// TRUE if the media is marked read-only otherwise, FALSE.
+  /// This field shows the read-only status as of the most recent WriteBlocks () call.
+  ///
+  BOOLEAN ReadOnly;
+
+  ///
+  /// TRUE if the WriteBlock () function caches write data.
+  ///
+  BOOLEAN WriteCaching;
+
+  ///
+  /// The intrinsic block size of the device. If the media changes, then
+  /// this field is updated.
+  ///
+  UINT32  BlockSize;
+
+  ///
+  /// Supplies the alignment requirement for any buffer to read or write block(s).
+  ///
+  UINT32  IoAlign;
+
+  ///
+  /// The last logical block address on the device.
+  /// If the media changes, then this field is updated.
+  ///
+  EFI_LBA LastBlock;
+
+  ///
+  /// Only present if EFI_BLOCK_IO_PROTOCOL.Revision is greater than or equal to
+  /// EFI_BLOCK_IO_PROTOCOL_REVISION2. Returns the first LBA is aligned to
+  /// a physical block boundary.
+  ///
+  EFI_LBA LowestAlignedLba;
+
+  ///
+  /// Only present if EFI_BLOCK_IO_PROTOCOL.Revision is greater than or equal to
+  /// EFI_BLOCK_IO_PROTOCOL_REVISION2. Returns the number of logical blocks
+  /// per physical block.
+  ///
+  UINT32 LogicalBlocksPerPhysicalBlock;
+
+  ///
+  /// Only present if EFI_BLOCK_IO_PROTOCOL.Revision is greater than or equal to
+  /// EFI_BLOCK_IO_PROTOCOL_REVISION3. Returns the optimal transfer length
+  /// granularity as a number of logical blocks.
+  ///
+  UINT32 OptimalTransferLengthGranularity;
+} EFI_BLOCK_IO_MEDIA;
+
+#define EFI_BLOCK_IO_PROTOCOL_REVISION  0x00010000
+#define EFI_BLOCK_IO_PROTOCOL_REVISION2 0x00020001
+#define EFI_BLOCK_IO_PROTOCOL_REVISION3 0x00020031
+
+///
+/// Revision defined in EFI1.1.
+///
+#define EFI_BLOCK_IO_INTERFACE_REVISION   EFI_BLOCK_IO_PROTOCOL_REVISION
+
+///
+///  This protocol provides control over block devices.
+///
+struct _EFI_BLOCK_IO_PROTOCOL {
+  ///
+  /// The revision to which the block IO interface adheres. All future
+  /// revisions must be backwards compatible. If a future version is not
+  /// back wards compatible, it is not the same GUID.
+  ///
+  UINT64              Revision;
+  ///
+  /// Pointer to the EFI_BLOCK_IO_MEDIA data for this device.
+  ///
+  EFI_BLOCK_IO_MEDIA  *Media;
+
+  EFI_BLOCK_RESET     Reset;
+  EFI_BLOCK_READ      ReadBlocks;
+  EFI_BLOCK_WRITE     WriteBlocks;
+  EFI_BLOCK_FLUSH     FlushBlocks;
+
+};
+
+extern EFI_GUID gEfiBlockIoProtocolGuid;
+
+#endif
diff --git a/src/include/ipxe/efi/Protocol/SimpleFileSystem.h b/src/include/ipxe/efi/Protocol/SimpleFileSystem.h
new file mode 100644 (file)
index 0000000..1981808
--- /dev/null
@@ -0,0 +1,403 @@
+/** @file
+  SimpleFileSystem protocol as defined in the UEFI 2.0 specification.
+
+  The SimpleFileSystem protocol is the programmatic access to the FAT (12,16,32)
+  file system specified in UEFI 2.0. It can also be used to abstract a file
+  system other than FAT.
+
+  UEFI 2.0 can boot from any valid EFI image contained in a SimpleFileSystem.
+
+Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials are licensed and made available under
+the terms and conditions of the BSD License that accompanies this distribution.
+The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php.
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __SIMPLE_FILE_SYSTEM_H__
+#define __SIMPLE_FILE_SYSTEM_H__
+
+FILE_LICENCE ( BSD3 );
+
+#define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID \
+  { \
+    0x964e5b22, 0x6459, 0x11d2, {0x8e, 0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
+  }
+
+typedef struct _EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL;
+
+typedef struct _EFI_FILE_PROTOCOL         EFI_FILE_PROTOCOL;
+typedef struct _EFI_FILE_PROTOCOL         *EFI_FILE_HANDLE;
+
+///
+/// Protocol GUID name defined in EFI1.1.
+///
+#define SIMPLE_FILE_SYSTEM_PROTOCOL       EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID
+
+///
+/// Protocol name defined in EFI1.1.
+///
+typedef EFI_SIMPLE_FILE_SYSTEM_PROTOCOL   EFI_FILE_IO_INTERFACE;
+typedef EFI_FILE_PROTOCOL                 EFI_FILE;
+
+/**
+  Open the root directory on a volume.
+
+  @param  This A pointer to the volume to open the root directory.
+  @param  Root A pointer to the location to return the opened file handle for the
+               root directory.
+
+  @retval EFI_SUCCESS          The device was opened.
+  @retval EFI_UNSUPPORTED      This volume does not support the requested file system type.
+  @retval EFI_NO_MEDIA         The device has no medium.
+  @retval EFI_DEVICE_ERROR     The device reported an error.
+  @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
+  @retval EFI_ACCESS_DENIED    The service denied access to the file.
+  @retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
+  @retval EFI_MEDIA_CHANGED    The device has a different medium in it or the medium is no
+                               longer supported. Any existing file handles for this volume are
+                               no longer valid. To access the files on the new medium, the
+                               volume must be reopened with OpenVolume().
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME)(
+  IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL    *This,
+  OUT EFI_FILE_PROTOCOL                 **Root
+  );
+
+#define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION  0x00010000
+
+///
+/// Revision defined in EFI1.1
+///
+#define EFI_FILE_IO_INTERFACE_REVISION  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
+
+struct _EFI_SIMPLE_FILE_SYSTEM_PROTOCOL {
+  ///
+  /// The version of the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL. The version
+  /// specified by this specification is 0x00010000. All future revisions
+  /// must be backwards compatible.
+  ///
+  UINT64                                      Revision;
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume;
+};
+
+/**
+  Opens a new file relative to the source file's location.
+
+  @param  This       A pointer to the EFI_FILE_PROTOCOL instance that is the file
+                     handle to the source location. This would typically be an open
+                     handle to a directory.
+  @param  NewHandle  A pointer to the location to return the opened handle for the new
+                     file.
+  @param  FileName   The Null-terminated string of the name of the file to be opened.
+                     The file name may contain the following path modifiers: "\", ".",
+                     and "..".
+  @param  OpenMode   The mode to open the file. The only valid combinations that the
+                     file may be opened with are: Read, Read/Write, or Create/Read/Write.
+  @param  Attributes Only valid for EFI_FILE_MODE_CREATE, in which case these are the
+                     attribute bits for the newly created file.
+
+  @retval EFI_SUCCESS          The file was opened.
+  @retval EFI_NOT_FOUND        The specified file could not be found on the device.
+  @retval EFI_NO_MEDIA         The device has no medium.
+  @retval EFI_MEDIA_CHANGED    The device has a different medium in it or the medium is no
+                               longer supported.
+  @retval EFI_DEVICE_ERROR     The device reported an error.
+  @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
+  @retval EFI_WRITE_PROTECTED  An attempt was made to create a file, or open a file for write
+                               when the media is write-protected.
+  @retval EFI_ACCESS_DENIED    The service denied access to the file.
+  @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
+  @retval EFI_VOLUME_FULL      The volume is full.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_OPEN)(
+  IN EFI_FILE_PROTOCOL        *This,
+  OUT EFI_FILE_PROTOCOL       **NewHandle,
+  IN CHAR16                   *FileName,
+  IN UINT64                   OpenMode,
+  IN UINT64                   Attributes
+  );
+
+//
+// Open modes
+//
+#define EFI_FILE_MODE_READ    0x0000000000000001ULL
+#define EFI_FILE_MODE_WRITE   0x0000000000000002ULL
+#define EFI_FILE_MODE_CREATE  0x8000000000000000ULL
+
+//
+// File attributes
+//
+#define EFI_FILE_READ_ONLY  0x0000000000000001ULL
+#define EFI_FILE_HIDDEN     0x0000000000000002ULL
+#define EFI_FILE_SYSTEM     0x0000000000000004ULL
+#define EFI_FILE_RESERVED   0x0000000000000008ULL
+#define EFI_FILE_DIRECTORY  0x0000000000000010ULL
+#define EFI_FILE_ARCHIVE    0x0000000000000020ULL
+#define EFI_FILE_VALID_ATTR 0x0000000000000037ULL
+
+/**
+  Closes a specified file handle.
+
+  @param  This          A pointer to the EFI_FILE_PROTOCOL instance that is the file
+                        handle to close.
+
+  @retval EFI_SUCCESS   The file was closed.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_CLOSE)(
+  IN EFI_FILE_PROTOCOL  *This
+  );
+
+/**
+  Close and delete the file handle.
+
+  @param  This                     A pointer to the EFI_FILE_PROTOCOL instance that is the
+                                   handle to the file to delete.
+
+  @retval EFI_SUCCESS              The file was closed and deleted, and the handle was closed.
+  @retval EFI_WARN_DELETE_FAILURE  The handle was closed, but the file was not deleted.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_DELETE)(
+  IN EFI_FILE_PROTOCOL  *This
+  );
+
+/**
+  Reads data from a file.
+
+  @param  This       A pointer to the EFI_FILE_PROTOCOL instance that is the file
+                     handle to read data from.
+  @param  BufferSize On input, the size of the Buffer. On output, the amount of data
+                     returned in Buffer. In both cases, the size is measured in bytes.
+  @param  Buffer     The buffer into which the data is read.
+
+  @retval EFI_SUCCESS          Data was read.
+  @retval EFI_NO_MEDIA         The device has no medium.
+  @retval EFI_DEVICE_ERROR     The device reported an error.
+  @retval EFI_DEVICE_ERROR     An attempt was made to read from a deleted file.
+  @retval EFI_DEVICE_ERROR     On entry, the current file position is beyond the end of the file.
+  @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
+  @retval EFI_BUFFER_TO_SMALL  The BufferSize is too small to read the current directory
+                               entry. BufferSize has been updated with the size
+                               needed to complete the request.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_READ)(
+  IN EFI_FILE_PROTOCOL        *This,
+  IN OUT UINTN                *BufferSize,
+  OUT VOID                    *Buffer
+  );
+
+/**
+  Writes data to a file.
+
+  @param  This       A pointer to the EFI_FILE_PROTOCOL instance that is the file
+                     handle to write data to.
+  @param  BufferSize On input, the size of the Buffer. On output, the amount of data
+                     actually written. In both cases, the size is measured in bytes.
+  @param  Buffer     The buffer of data to write.
+
+  @retval EFI_SUCCESS          Data was written.
+  @retval EFI_UNSUPPORTED      Writes to open directory files are not supported.
+  @retval EFI_NO_MEDIA         The device has no medium.
+  @retval EFI_DEVICE_ERROR     The device reported an error.
+  @retval EFI_DEVICE_ERROR     An attempt was made to write to a deleted file.
+  @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
+  @retval EFI_WRITE_PROTECTED  The file or medium is write-protected.
+  @retval EFI_ACCESS_DENIED    The file was opened read only.
+  @retval EFI_VOLUME_FULL      The volume is full.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_WRITE)(
+  IN EFI_FILE_PROTOCOL        *This,
+  IN OUT UINTN                *BufferSize,
+  IN VOID                     *Buffer
+  );
+
+/**
+  Sets a file's current position.
+
+  @param  This            A pointer to the EFI_FILE_PROTOCOL instance that is the
+                          file handle to set the requested position on.
+  @param  Position        The byte position from the start of the file to set.
+
+  @retval EFI_SUCCESS      The position was set.
+  @retval EFI_UNSUPPORTED  The seek request for nonzero is not valid on open
+                           directories.
+  @retval EFI_DEVICE_ERROR An attempt was made to set the position of a deleted file.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_SET_POSITION)(
+  IN EFI_FILE_PROTOCOL        *This,
+  IN UINT64                   Position
+  );
+
+/**
+  Returns a file's current position.
+
+  @param  This            A pointer to the EFI_FILE_PROTOCOL instance that is the file
+                          handle to get the current position on.
+  @param  Position        The address to return the file's current position value.
+
+  @retval EFI_SUCCESS      The position was returned.
+  @retval EFI_UNSUPPORTED  The request is not valid on open directories.
+  @retval EFI_DEVICE_ERROR An attempt was made to get the position from a deleted file.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_GET_POSITION)(
+  IN EFI_FILE_PROTOCOL        *This,
+  OUT UINT64                  *Position
+  );
+
+/**
+  Returns information about a file.
+
+  @param  This            A pointer to the EFI_FILE_PROTOCOL instance that is the file
+                          handle the requested information is for.
+  @param  InformationType The type identifier for the information being requested.
+  @param  BufferSize      On input, the size of Buffer. On output, the amount of data
+                          returned in Buffer. In both cases, the size is measured in bytes.
+  @param  Buffer          A pointer to the data buffer to return. The buffer's type is
+                          indicated by InformationType.
+
+  @retval EFI_SUCCESS          The information was returned.
+  @retval EFI_UNSUPPORTED      The InformationType is not known.
+  @retval EFI_NO_MEDIA         The device has no medium.
+  @retval EFI_DEVICE_ERROR     The device reported an error.
+  @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
+  @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
+                               BufferSize has been updated with the size needed to complete
+                               the request.
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_GET_INFO)(
+  IN EFI_FILE_PROTOCOL        *This,
+  IN EFI_GUID                 *InformationType,
+  IN OUT UINTN                *BufferSize,
+  OUT VOID                    *Buffer
+  );
+
+/**
+  Sets information about a file.
+
+  @param  File            A pointer to the EFI_FILE_PROTOCOL instance that is the file
+                          handle the information is for.
+  @param  InformationType The type identifier for the information being set.
+  @param  BufferSize      The size, in bytes, of Buffer.
+  @param  Buffer          A pointer to the data buffer to write. The buffer's type is
+                          indicated by InformationType.
+
+  @retval EFI_SUCCESS          The information was set.
+  @retval EFI_UNSUPPORTED      The InformationType is not known.
+  @retval EFI_NO_MEDIA         The device has no medium.
+  @retval EFI_DEVICE_ERROR     The device reported an error.
+  @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
+  @retval EFI_WRITE_PROTECTED  InformationType is EFI_FILE_INFO_ID and the media is
+                               read-only.
+  @retval EFI_WRITE_PROTECTED  InformationType is EFI_FILE_PROTOCOL_SYSTEM_INFO_ID
+                               and the media is read only.
+  @retval EFI_WRITE_PROTECTED  InformationType is EFI_FILE_SYSTEM_VOLUME_LABEL_ID
+                               and the media is read-only.
+  @retval EFI_ACCESS_DENIED    An attempt is made to change the name of a file to a
+                               file that is already present.
+  @retval EFI_ACCESS_DENIED    An attempt is being made to change the EFI_FILE_DIRECTORY
+                               Attribute.
+  @retval EFI_ACCESS_DENIED    An attempt is being made to change the size of a directory.
+  @retval EFI_ACCESS_DENIED    InformationType is EFI_FILE_INFO_ID and the file was opened
+                               read-only and an attempt is being made to modify a field
+                               other than Attribute.
+  @retval EFI_VOLUME_FULL      The volume is full.
+  @retval EFI_BAD_BUFFER_SIZE  BufferSize is smaller than the size of the type indicated
+                               by InformationType.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_SET_INFO)(
+  IN EFI_FILE_PROTOCOL        *This,
+  IN EFI_GUID                 *InformationType,
+  IN UINTN                    BufferSize,
+  IN VOID                     *Buffer
+  );
+
+/**
+  Flushes all modified data associated with a file to a device.
+
+  @param  This A pointer to the EFI_FILE_PROTOCOL instance that is the file
+               handle to flush.
+
+  @retval EFI_SUCCESS          The data was flushed.
+  @retval EFI_NO_MEDIA         The device has no medium.
+  @retval EFI_DEVICE_ERROR     The device reported an error.
+  @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
+  @retval EFI_WRITE_PROTECTED  The file or medium is write-protected.
+  @retval EFI_ACCESS_DENIED    The file was opened read-only.
+  @retval EFI_VOLUME_FULL      The volume is full.
+
+**/
+typedef
+EFI_STATUS
+(EFIAPI *EFI_FILE_FLUSH)(
+  IN EFI_FILE_PROTOCOL  *This
+  );
+
+#define EFI_FILE_PROTOCOL_REVISION   0x00010000
+//
+// Revision defined in EFI1.1.
+//
+#define EFI_FILE_REVISION   EFI_FILE_PROTOCOL_REVISION
+
+///
+/// The EFI_FILE_PROTOCOL provides file IO access to supported file systems.
+/// An EFI_FILE_PROTOCOL provides access to a file's or directory's contents,
+/// and is also a reference to a location in the directory tree of the file system
+/// in which the file resides. With any given file handle, other files may be opened
+/// relative to this file's location, yielding new file handles.
+///
+struct _EFI_FILE_PROTOCOL {
+  ///
+  /// The version of the EFI_FILE_PROTOCOL interface. The version specified
+  /// by this specification is 0x00010000. Future versions are required
+  /// to be backward compatible to version 1.0.
+  ///
+  UINT64                Revision;
+  EFI_FILE_OPEN         Open;
+  EFI_FILE_CLOSE        Close;
+  EFI_FILE_DELETE       Delete;
+  EFI_FILE_READ         Read;
+  EFI_FILE_WRITE        Write;
+  EFI_FILE_GET_POSITION GetPosition;
+  EFI_FILE_SET_POSITION SetPosition;
+  EFI_FILE_GET_INFO     GetInfo;
+  EFI_FILE_SET_INFO     SetInfo;
+  EFI_FILE_FLUSH        Flush;
+};
+
+
+extern EFI_GUID gEfiSimpleFileSystemProtocolGuid;
+
+#endif
index b5ce7df9e2ea43c05db78086eae5bbea7e28598f..8a216b5373f6afe9cfafc0dfb87b1ce5b73c4906 100644 (file)
@@ -142,7 +142,5 @@ extern EFI_SYSTEM_TABLE *efi_systab;
 extern const char * efi_strerror ( EFI_STATUS efirc );
 extern EFI_STATUS efi_init ( EFI_HANDLE image_handle,
                             EFI_SYSTEM_TABLE *systab );
-extern int efi_download_install ( EFI_HANDLE *device_handle );
-extern void efi_download_uninstall ( EFI_HANDLE device_handle );
 
 #endif /* _IPXE_EFI_H */
similarity index 97%
rename from src/include/ipxe/efi/ipxe_download.h
rename to src/include/ipxe/efi/efi_download.h
index 22e3cef0808425a8df0b2dc1927eb0dbf2012773..3ce4972219b92f66bc7c931bafedee6c3f023406 100644 (file)
@@ -151,4 +151,7 @@ struct _IPXE_DOWNLOAD_PROTOCOL {
     0x3eaeaebd, 0xdecf, 0x493b, { 0x9b, 0xd1, 0xcd, 0xb2, 0xde, 0xca, 0xe7, 0x19 } \
   }
 
+extern int efi_download_install ( EFI_HANDLE *device );
+extern void efi_download_uninstall ( EFI_HANDLE device );
+
 #endif /* _IPXE_DOWNLOAD_H */
diff --git a/src/include/ipxe/efi/efi_file.h b/src/include/ipxe/efi/efi_file.h
new file mode 100644 (file)
index 0000000..0d75cf5
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef _IPXE_EFI_FILE_H
+#define _IPXE_EFI_FILE_H
+
+/** @file
+ *
+ * EFI file protocols
+ *
+ */
+
+extern int efi_file_install ( EFI_HANDLE *handle );
+extern void efi_file_uninstall ( EFI_HANDLE handle );
+
+#endif /* _IPXE_EFI_FILE_H */
index 250946e28372c7bf53f4ed1c0d7153bae1dd678a..7b19ad3a2ddcfa43759c031390bc5cf41e1de017 100644 (file)
@@ -25,7 +25,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #include <ipxe/iobuf.h>
 #include <ipxe/xfer.h>
 #include <ipxe/efi/efi.h>
-#include <ipxe/efi/ipxe_download.h>
+#include <ipxe/efi/efi_download.h>
 
 /** iPXE download protocol GUID */
 static EFI_GUID ipxe_download_protocol_guid
@@ -187,47 +187,39 @@ static IPXE_DOWNLOAD_PROTOCOL ipxe_download_protocol_interface = {
 };
 
 /**
- * Create a new device handle with a iPXE download protocol attached to it.
+ * Install iPXE download protocol
  *
- * @v device_handle    Newly created device handle (output)
+ * @v handle           EFI handle
  * @ret rc             Return status code
  */
-int efi_download_install ( EFI_HANDLE *device_handle ) {
+int efi_download_install ( EFI_HANDLE *handle ) {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_STATUS efirc;
-       EFI_HANDLE handle = NULL;
-       if (efi_loaded_image->DeviceHandle) { /* TODO: ensure handle is the NIC (maybe efi_image has a better way to indicate the handle doing SNP?) */
-               handle = efi_loaded_image->DeviceHandle;
-       }
 
-       DBG ( "Installing ipxe protocol interface (%p)... ",
-             &ipxe_download_protocol_interface );
        efirc = bs->InstallMultipleProtocolInterfaces (
-                       &handle,
+                       handle,
                        &ipxe_download_protocol_guid,
                        &ipxe_download_protocol_interface,
                        NULL );
        if ( efirc ) {
-               DBG ( "failed (%s)\n", efi_strerror ( efirc ) );
+               DBG ( "Could not install download protocol: %s\n",
+                     efi_strerror ( efirc ) );
                return EFIRC_TO_RC ( efirc );
        }
 
-       DBG ( "success (%p)\n", handle );
-       *device_handle = handle;
        return 0;
 }
 
 /**
- * Remove the iPXE download protocol from the given handle, and if nothing
- * else is attached, destroy the handle.
+ * Uninstall iPXE download protocol
  *
- * @v device_handle    EFI device handle to remove from
+ * @v handle           EFI handle
  */
-void efi_download_uninstall ( EFI_HANDLE device_handle ) {
+void efi_download_uninstall ( EFI_HANDLE handle ) {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
 
        bs->UninstallMultipleProtocolInterfaces (
-                       device_handle,
-                       ipxe_download_protocol_guid,
-                       ipxe_download_protocol_interface );
+                       handle,
+                       &ipxe_download_protocol_guid,
+                       &ipxe_download_protocol_interface, NULL );
 }
diff --git a/src/interface/efi/efi_file.c b/src/interface/efi/efi_file.c
new file mode 100644 (file)
index 0000000..ffe25fd
--- /dev/null
@@ -0,0 +1,594 @@
+/*
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+/**
+ * @file
+ *
+ * EFI file protocols
+ *
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <wchar.h>
+#include <ipxe/image.h>
+#include <ipxe/efi/efi.h>
+#include <ipxe/efi/Protocol/SimpleFileSystem.h>
+#include <ipxe/efi/Protocol/BlockIo.h>
+#include <ipxe/efi/Guid/FileInfo.h>
+#include <ipxe/efi/Guid/FileSystemInfo.h>
+#include <ipxe/efi/efi_strings.h>
+#include <ipxe/efi/efi_file.h>
+
+/** EFI simple file system protocol GUID */
+static EFI_GUID efi_simple_file_system_protocol_guid
+       = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
+
+/** EFI file information GUID */
+static EFI_GUID efi_file_info_id = EFI_FILE_INFO_ID;
+
+/** EFI file system information GUID */
+static EFI_GUID efi_file_system_info_id = EFI_FILE_SYSTEM_INFO_ID;
+
+/** EFI block I/O protocol GUID */
+static EFI_GUID efi_block_io_protocol_guid
+       = EFI_BLOCK_IO_PROTOCOL_GUID;
+
+/** EFI media ID */
+#define EFI_MEDIA_ID_MAGIC 0x69505845
+
+/** An image exposed as an EFI file */
+struct efi_file {
+       /** EFI file protocol */
+       EFI_FILE_PROTOCOL file;
+       /** Image */
+       struct image *image;
+       /** Current file position */
+       size_t pos;
+};
+
+static struct efi_file efi_file_root;
+
+/**
+ * Get EFI file name (for debugging)
+ *
+ * @v file             EFI file
+ * @ret name           Name
+ */
+static const char * efi_file_name ( struct efi_file *file ) {
+
+       return ( file->image ? file->image->name : "<root>" );
+}
+
+/**
+ * Open file
+ *
+ * @v this             EFI file
+ * @ret new            New EFI file
+ * @v wname            Filename
+ * @v mode             File mode
+ * @v attributes       File attributes (for newly-created files)
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI
+efi_file_open ( EFI_FILE_PROTOCOL *this, EFI_FILE_PROTOCOL **new,
+               CHAR16 *wname, UINT64 mode __unused,
+               UINT64 attributes __unused ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+       char name[ wcslen ( wname ) + 1 /* NUL */ ];
+       struct efi_file *new_file;
+       struct image *image;
+
+       /* Initial '\' indicates opening from the root directory */
+       while ( *wname == L'\\' ) {
+               file = &efi_file_root;
+               wname++;
+       }
+
+       /* Allow root directory itself to be opened */
+       if ( ( wname[0] == L'\0' ) || ( wname[0] == L'.' ) ) {
+               *new = &efi_file_root.file;
+               return 0;
+       }
+
+       /* Fail unless opening from the root */
+       if ( file->image ) {
+               DBGC ( file, "EFIFILE %s is not a directory\n",
+                      efi_file_name ( file ) );
+               return EFI_NOT_FOUND;
+       }
+
+       /* Identify image */
+       snprintf ( name, sizeof ( name ), "%ls", wname );
+       image = find_image ( name );
+       if ( ! image ) {
+               DBGC ( file, "EFIFILE \"%s\" does not exist\n", name );
+               return EFI_NOT_FOUND;
+       }
+
+       /* Fail unless opening read-only */
+       if ( mode != EFI_FILE_MODE_READ ) {
+               DBGC ( file, "EFIFILE %s cannot be opened in mode %#08llx\n",
+                      image->name, mode );
+               return EFI_WRITE_PROTECTED;
+       }
+
+       /* Allocate and initialise file */
+       new_file = zalloc ( sizeof ( *new_file ) );
+       memcpy ( &new_file->file, &efi_file_root.file,
+                sizeof ( new_file->file ) );
+       new_file->image = image_get ( image );
+       *new = &new_file->file;
+       DBGC ( new_file, "EFIFILE %s opened\n", efi_file_name ( new_file ) );
+
+       return 0;
+}
+
+/**
+ * Close file
+ *
+ * @v this             EFI file
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI efi_file_close ( EFI_FILE_PROTOCOL *this ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+
+       /* Do nothing if this is the root */
+       if ( ! file->image )
+               return 0;
+
+       /* Close file */
+       DBGC ( file, "EFIFILE %s closed\n", efi_file_name ( file ) );
+       image_put ( file->image );
+       free ( file );
+
+       return 0;
+}
+
+/**
+ * Close and delete file
+ *
+ * @v this             EFI file
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI efi_file_delete ( EFI_FILE_PROTOCOL *this ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+
+       DBGC ( file, "EFIFILE %s cannot be deleted\n", efi_file_name ( file ) );
+
+       /* Close file */
+       efi_file_close ( this );
+
+       /* Warn of failure to delete */
+       return EFI_WARN_DELETE_FAILURE;
+}
+
+/**
+ * Return variable-length data structure
+ *
+ * @v base             Base data structure (starting with UINT64)
+ * @v base_len         Length of base data structure
+ * @v name             Name to append to base data structure
+ * @v len              Length of data buffer
+ * @v data             Data buffer
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS efi_file_varlen ( UINT64 *base, size_t base_len,
+                                   const char *name, UINTN *len, VOID *data ) {
+       size_t name_len;
+
+       /* Calculate structure length */
+       name_len = strlen ( name );
+       *base = ( base_len + ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
+       if ( *len < *base ) {
+               *len = *base;
+               return EFI_BUFFER_TOO_SMALL;
+       }
+
+       /* Copy data to buffer */
+       *len = *base;
+       memcpy ( data, base, base_len );
+       efi_snprintf ( ( data + base_len ), ( name_len + 1 /* NUL */ ),
+                      "%s", name );
+
+       return 0;
+}
+
+/**
+ * Return file information structure
+ *
+ * @v image            Image, or NULL for the root directory
+ * @v len              Length of data buffer
+ * @v data             Data buffer
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS efi_file_info ( struct image *image, UINTN *len,
+                                 VOID *data ) {
+       EFI_FILE_INFO info;
+       const char *name;
+
+       /* Populate file information */
+       memset ( &info, 0, sizeof ( info ) );
+       if ( image ) {
+               info.FileSize = image->len;
+               info.PhysicalSize = image->len;
+               info.Attribute = EFI_FILE_READ_ONLY;
+               name = image->name;
+       } else {
+               info.Attribute = ( EFI_FILE_READ_ONLY | EFI_FILE_DIRECTORY );
+               name = "";
+       }
+
+       return efi_file_varlen ( &info.Size, SIZE_OF_EFI_FILE_INFO, name,
+                                len, data );
+}
+
+/**
+ * Read directory entry
+ *
+ * @v file             EFI file
+ * @v len              Length to read
+ * @v data             Data buffer
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS efi_file_read_dir ( struct efi_file *file, UINTN *len,
+                                     VOID *data ) {
+       EFI_STATUS efirc;
+       struct image *image;
+       unsigned int index;
+
+       /* Construct directory entry at current position */
+       index = file->pos;
+       for_each_image ( image ) {
+               if ( index-- == 0 ) {
+                       efirc = efi_file_info ( image, len, data );
+                       if ( efirc == 0 )
+                               file->pos++;
+                       return efirc;
+               }
+       }
+
+       /* No more entries */
+       *len = 0;
+       return 0;
+}
+
+/**
+ * Read from file
+ *
+ * @v this             EFI file
+ * @v len              Length to read
+ * @v data             Data buffer
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI efi_file_read ( EFI_FILE_PROTOCOL *this,
+                                        UINTN *len, VOID *data ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+       size_t remaining;
+
+       /* If this is the root directory, then construct a directory entry */
+       if ( ! file->image )
+               return efi_file_read_dir ( file, len, data );
+
+       /* Read from the file */
+       remaining = ( file->image->len - file->pos );
+       if ( *len > remaining )
+               *len = remaining;
+       DBGC ( file, "EFIFILE %s read [%#08zx,%#08zx)\n",
+              efi_file_name ( file ), file->pos,
+              ( ( size_t ) ( file->pos + *len ) ) );
+       copy_from_user ( data, file->image->data, file->pos, *len );
+       file->pos += *len;
+       return 0;
+}
+
+/**
+ * Write to file
+ *
+ * @v this             EFI file
+ * @v len              Length to write
+ * @v data             Data buffer
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI efi_file_write ( EFI_FILE_PROTOCOL *this,
+                                         UINTN *len, VOID *data __unused ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+
+       DBGC ( file, "EFIFILE %s cannot write [%#08zx, %#08zx)\n",
+              efi_file_name ( file ), file->pos,
+              ( ( size_t ) ( file->pos + *len ) ) );
+       return EFI_WRITE_PROTECTED;
+}
+
+/**
+ * Set file position
+ *
+ * @v this             EFI file
+ * @v position         New file position
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI efi_file_set_position ( EFI_FILE_PROTOCOL *this,
+                                                UINT64 position ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+
+       /* If this is the root directory, reset to the start */
+       if ( ! file->image ) {
+               DBGC ( file, "EFIFILE root directory rewound\n" );
+               file->pos = 0;
+               return 0;
+       }
+
+       /* Check for the magic end-of-file value */
+       if ( position == 0xffffffffffffffffULL )
+               position = file->image->len;
+
+       /* Fail if we attempt to seek past the end of the file (since
+        * we do not support writes).
+        */
+       if ( position > file->image->len ) {
+               DBGC ( file, "EFIFILE %s cannot seek to %#08llx of %#08zx\n",
+                      efi_file_name ( file ), position, file->image->len );
+               return EFI_UNSUPPORTED;
+       }
+
+       /* Set position */
+       file->pos = position;
+       DBGC ( file, "EFIFILE %s position set to %#08zx\n",
+              efi_file_name ( file ), file->pos );
+
+       return 0;
+}
+
+/**
+ * Get file position
+ *
+ * @v this             EFI file
+ * @ret position       New file position
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI efi_file_get_position ( EFI_FILE_PROTOCOL *this,
+                                                UINT64 *position ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+
+       *position = file->pos;
+       return 0;
+}
+
+/**
+ * Get file information
+ *
+ * @v this             EFI file
+ * @v type             Type of information
+ * @v len              Buffer size
+ * @v data             Buffer
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI efi_file_get_info ( EFI_FILE_PROTOCOL *this,
+                                            EFI_GUID *type,
+                                            UINTN *len, VOID *data ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+       EFI_FILE_SYSTEM_INFO fsinfo;
+       struct image *image;
+
+       /* Determine information to return */
+       if ( memcmp ( type, &efi_file_info_id, sizeof ( *type ) ) == 0 ) {
+
+               /* Get file information */
+               DBGC ( file, "EFIFILE %s get file information\n",
+                      efi_file_name ( file ) );
+               return efi_file_info ( file->image, len, data );
+
+       } else if ( memcmp ( type, &efi_file_system_info_id,
+                            sizeof ( *type ) ) == 0 ) {
+
+               /* Get file system information */
+               DBGC ( file, "EFIFILE %s get file system information\n",
+                      efi_file_name ( file ) );
+               memset ( &fsinfo, 0, sizeof ( fsinfo ) );
+               fsinfo.ReadOnly = 1;
+               for_each_image ( image )
+                       fsinfo.VolumeSize += image->len;
+               return efi_file_varlen ( &fsinfo.Size,
+                                        SIZE_OF_EFI_FILE_SYSTEM_INFO, "iPXE",
+                                        len, data );
+       } else {
+
+               DBGC ( file, "EFIFILE %s cannot get information of type %s\n",
+                      efi_file_name ( file ),
+                      uuid_ntoa ( ( union uuid * ) type ) );
+               return EFI_UNSUPPORTED;
+       }
+}
+
+/**
+ * Set file information
+ *
+ * @v this             EFI file
+ * @v type             Type of information
+ * @v len              Buffer size
+ * @v data             Buffer
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI
+efi_file_set_info ( EFI_FILE_PROTOCOL *this, EFI_GUID *type,
+                   UINTN len __unused, VOID *data __unused ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+
+       DBGC ( file, "EFIFILE %s cannot set information of type %s\n",
+              efi_file_name ( file ), uuid_ntoa ( ( union uuid * ) type ) );
+       return EFI_WRITE_PROTECTED;
+}
+
+/**
+ * Flush file modified data
+ *
+ * @v this             EFI file
+ * @v type             Type of information
+ * @v len              Buffer size
+ * @v data             Buffer
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI efi_file_flush ( EFI_FILE_PROTOCOL *this ) {
+       struct efi_file *file = container_of ( this, struct efi_file, file );
+
+       DBGC ( file, "EFIFILE %s flushed\n", efi_file_name ( file ) );
+       return 0;
+}
+
+/** Root directory */
+static struct efi_file efi_file_root = {
+       .file = {
+               .Revision = EFI_FILE_PROTOCOL_REVISION,
+               .Open = efi_file_open,
+               .Close = efi_file_close,
+               .Delete = efi_file_delete,
+               .Read = efi_file_read,
+               .Write = efi_file_write,
+               .GetPosition = efi_file_get_position,
+               .SetPosition = efi_file_set_position,
+               .GetInfo = efi_file_get_info,
+               .SetInfo = efi_file_set_info,
+               .Flush = efi_file_flush,
+       },
+       .image = NULL,
+};
+
+/**
+ * Open root directory
+ *
+ * @v filesystem       EFI simple file system
+ * @ret file           EFI file handle
+ * @ret efirc          EFI status code
+ */
+static EFI_STATUS EFIAPI
+efi_file_open_volume ( EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *filesystem __unused,
+                      EFI_FILE_PROTOCOL **file ) {
+
+       *file = &efi_file_root.file;
+       return 0;
+}
+
+/** EFI simple file system protocol */
+static EFI_SIMPLE_FILE_SYSTEM_PROTOCOL efi_simple_file_system_protocol = {
+       .Revision = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION,
+       .OpenVolume = efi_file_open_volume,
+};
+
+/** Dummy block I/O reset */
+static EFI_STATUS EFIAPI
+efi_block_io_reset ( EFI_BLOCK_IO_PROTOCOL *this __unused,
+                    BOOLEAN extended __unused ) {
+       return 0;
+}
+
+/** Dummy block I/O read */
+static EFI_STATUS EFIAPI
+efi_block_io_read_blocks ( EFI_BLOCK_IO_PROTOCOL *this __unused,
+                          UINT32 MediaId __unused, EFI_LBA lba __unused,
+                          UINTN len __unused, VOID *data __unused ) {
+       return EFI_DEVICE_ERROR;
+}
+
+/** Dummy block I/O write */
+static EFI_STATUS EFIAPI
+efi_block_io_write_blocks ( EFI_BLOCK_IO_PROTOCOL *this __unused,
+                           UINT32 MediaId __unused, EFI_LBA lba __unused,
+                           UINTN len __unused, VOID *data __unused ) {
+       return EFI_DEVICE_ERROR;
+}
+
+/** Dummy block I/O flush */
+static EFI_STATUS EFIAPI
+efi_block_io_flush_blocks ( EFI_BLOCK_IO_PROTOCOL *this __unused ) {
+       return 0;
+}
+
+/** Dummy block I/O media */
+static EFI_BLOCK_IO_MEDIA efi_block_io_media = {
+       .MediaId = EFI_MEDIA_ID_MAGIC,
+       .MediaPresent = 1,
+       .ReadOnly = 1,
+       .BlockSize = 1,
+};
+
+/** Dummy EFI block I/O protocol */
+static EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol = {
+       .Revision = EFI_BLOCK_IO_PROTOCOL_REVISION,
+       .Media = &efi_block_io_media,
+       .Reset = efi_block_io_reset,
+       .ReadBlocks = efi_block_io_read_blocks,
+       .WriteBlocks = efi_block_io_write_blocks,
+       .FlushBlocks = efi_block_io_flush_blocks,
+};
+
+/**
+ * Install EFI simple file system protocol
+ *
+ * @v handle           EFI handle
+ * @ret rc             Return status code
+ */
+int efi_file_install ( EFI_HANDLE *handle ) {
+       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
+       EFI_STATUS efirc;
+
+       /* Install the simple file system protocol and the block I/O
+        * protocol.  We don't have a block device, but large parts of
+        * the EDK2 codebase make the assumption that file systems are
+        * normally attached to block devices, and so we create a
+        * dummy block device on the same handle just to keep things
+        * looking normal.
+        */
+       if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
+                       handle,
+                       &efi_block_io_protocol_guid,
+                       &efi_block_io_protocol,
+                       &efi_simple_file_system_protocol_guid,
+                       &efi_simple_file_system_protocol, NULL ) ) != 0 ) {
+               DBGC ( handle, "Could not install simple file system protocol: "
+                      "%s\n", efi_strerror ( efirc ) );
+               return EFIRC_TO_RC ( efirc );
+       }
+
+       return 0;
+}
+
+/**
+ * Uninstall EFI simple file system protocol
+ *
+ * @v handle           EFI handle
+ */
+void efi_file_uninstall ( EFI_HANDLE handle ) {
+       EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
+
+       /* We must install the file system protocol first, since
+        * otherwise the EDK2 code will attempt to helpfully uninstall
+        * it when the block I/O protocol is uninstalled, leading to a
+        * system lock-up.
+        */
+       bs->UninstallMultipleProtocolInterfaces (
+                       handle,
+                       &efi_simple_file_system_protocol_guid,
+                       &efi_simple_file_system_protocol,
+                       &efi_block_io_protocol_guid,
+                       &efi_block_io_protocol, NULL );
+}