iPXE
Data Structures | Macros | Functions | Variables
efi_file.c File Reference

EFI file protocols. More...

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <wchar.h>
#include <ipxe/image.h>
#include <ipxe/cpio.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/SimpleFileSystem.h>
#include <ipxe/efi/Protocol/BlockIo.h>
#include <ipxe/efi/Protocol/DiskIo.h>
#include <ipxe/efi/Protocol/LoadFile2.h>
#include <ipxe/efi/Guid/FileInfo.h>
#include <ipxe/efi/Guid/FileSystemInfo.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/efi_file.h>

Go to the source code of this file.

Data Structures

struct  efi_file_reader
 An EFI virtual file reader. More...
 
struct  efi_file
 An EFI file. More...
 
struct  efi_file_path
 An EFI fixed device path file. More...
 

Macros

#define EFI_MEDIA_ID_MAGIC   0x69505845
 EFI media ID. More...
 
#define LINUX_INITRD_VENDOR_GUID
 Linux initrd fixed device path vendor GUID. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void efi_file_free (struct refcnt *refcnt)
 Free EFI file. More...
 
static const char * efi_file_name (struct efi_file *file)
 Get EFI file name (for debugging) More...
 
static struct imageefi_file_find (const char *name)
 Find EFI file image. More...
 
static size_t efi_file_len (struct efi_file *file)
 Get length of EFI file. More...
 
static size_t efi_file_read_chunk (struct efi_file_reader *reader, userptr_t data, size_t len)
 Read chunk of EFI file. More...
 
static size_t efi_file_read_image (struct efi_file_reader *reader)
 Read from image-backed file. More...
 
static size_t efi_file_read_initrd (struct efi_file_reader *reader)
 Read from magic initrd file. More...
 
static EFI_STATUS efi_file_open_fixed (struct efi_file *file, const wchar_t *wname, EFI_FILE_PROTOCOL **new)
 Open fixed file. More...
 
static void efi_file_image (struct efi_file *file, struct image *image)
 Associate file with image. More...
 
static EFI_STATUS efi_file_open_image (struct image *image, const wchar_t *wname, EFI_FILE_PROTOCOL **new)
 Open image-backed file. More...
 
static EFI_STATUS EFIAPI efi_file_open (EFI_FILE_PROTOCOL *this, EFI_FILE_PROTOCOL **new, CHAR16 *wname, UINT64 mode, UINT64 attributes __unused)
 Open file. More...
 
static EFI_STATUS EFIAPI efi_file_close (EFI_FILE_PROTOCOL *this)
 Close file. More...
 
static EFI_STATUS EFIAPI efi_file_delete (EFI_FILE_PROTOCOL *this)
 Close and delete file. More...
 
static EFI_STATUS efi_file_varlen (UINT64 *base, size_t base_len, const char *name, UINTN *len, VOID *data)
 Return variable-length data structure. More...
 
static EFI_STATUS efi_file_info (struct efi_file *file, UINTN *len, VOID *data)
 Return file information structure. More...
 
static EFI_STATUS efi_file_read_dir (struct efi_file *file, UINTN *len, VOID *data)
 Read directory entry. More...
 
static EFI_STATUS EFIAPI efi_file_read (EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data)
 Read from file. More...
 
static EFI_STATUS EFIAPI efi_file_write (EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data __unused)
 Write to file. More...
 
static EFI_STATUS EFIAPI efi_file_set_position (EFI_FILE_PROTOCOL *this, UINT64 position)
 Set file position. More...
 
static EFI_STATUS EFIAPI efi_file_get_position (EFI_FILE_PROTOCOL *this, UINT64 *position)
 Get file position. More...
 
static EFI_STATUS EFIAPI efi_file_get_info (EFI_FILE_PROTOCOL *this, EFI_GUID *type, UINTN *len, VOID *data)
 Get file information. More...
 
static EFI_STATUS EFIAPI efi_file_set_info (EFI_FILE_PROTOCOL *this, EFI_GUID *type, UINTN len __unused, VOID *data __unused)
 Set file information. More...
 
static EFI_STATUS EFIAPI efi_file_flush (EFI_FILE_PROTOCOL *this)
 Flush file modified data. More...
 
static EFI_STATUS EFIAPI efi_file_load (EFI_LOAD_FILE2_PROTOCOL *this, EFI_DEVICE_PATH_PROTOCOL *path __unused, BOOLEAN boot __unused, UINTN *len, VOID *data)
 Load file. More...
 
static EFI_STATUS EFIAPI efi_file_open_volume (EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *filesystem __unused, EFI_FILE_PROTOCOL **file)
 Open root directory. More...
 
static EFI_STATUS EFIAPI efi_block_io_reset (EFI_BLOCK_IO_PROTOCOL *this __unused, BOOLEAN extended)
 Dummy block I/O reset. More...
 
static EFI_STATUS EFIAPI efi_block_io_read_blocks (EFI_BLOCK_IO_PROTOCOL *this __unused, UINT32 MediaId, EFI_LBA lba, UINTN len, VOID *data)
 Dummy block I/O read. More...
 
static EFI_STATUS EFIAPI efi_block_io_write_blocks (EFI_BLOCK_IO_PROTOCOL *this __unused, UINT32 MediaId, EFI_LBA lba, UINTN len, VOID *data)
 Dummy block I/O write. More...
 
static EFI_STATUS EFIAPI efi_block_io_flush_blocks (EFI_BLOCK_IO_PROTOCOL *this __unused)
 Dummy block I/O flush. More...
 
static EFI_STATUS EFIAPI efi_disk_io_read_disk (EFI_DISK_IO_PROTOCOL *this __unused, UINT32 MediaId, UINT64 offset, UINTN len, VOID *data)
 Dummy disk I/O read. More...
 
static EFI_STATUS EFIAPI efi_disk_io_write_disk (EFI_DISK_IO_PROTOCOL *this __unused, UINT32 MediaId, UINT64 offset, UINTN len, VOID *data)
 Dummy disk I/O write. More...
 
static int efi_file_path_claim (struct efi_file_path *file)
 Claim use of fixed device path. More...
 
static int efi_file_path_install (struct efi_file_path *file)
 Install fixed device path file. More...
 
static void efi_file_path_uninstall (struct efi_file_path *file)
 Uninstall fixed device path file. More...
 
int efi_file_install (EFI_HANDLE handle)
 Install EFI simple file system protocol. More...
 
void efi_file_uninstall (EFI_HANDLE handle)
 Uninstall EFI simple file system protocol. More...
 

Variables

static struct efi_file efi_file_root
 Root directory. More...
 
static struct efi_file_path efi_file_initrd
 Magic initrd file. More...
 
struct {
   VENDOR_DEVICE_PATH   vendor
 
   EFI_DEVICE_PATH_PROTOCOL   end
 
efi_file_initrd_path
 Linux initrd fixed device path. More...
 
static EFI_SIMPLE_FILE_SYSTEM_PROTOCOL efi_simple_file_system_protocol
 EFI simple file system protocol. More...
 
static EFI_BLOCK_IO_MEDIA efi_block_io_media
 Dummy block I/O media. More...
 
static EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol
 Dummy EFI block I/O protocol. More...
 
static EFI_DISK_IO_PROTOCOL efi_disk_io_protocol
 Dummy EFI disk I/O protocol. More...
 

Detailed Description

EFI file protocols.

Definition in file efi_file.c.

Macro Definition Documentation

◆ EFI_MEDIA_ID_MAGIC

#define EFI_MEDIA_ID_MAGIC   0x69505845

EFI media ID.

Definition at line 54 of file efi_file.c.

◆ LINUX_INITRD_VENDOR_GUID

#define LINUX_INITRD_VENDOR_GUID
Value:
{ 0x5568e427, 0x68fc, 0x4f3d, \
{ 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } }

Linux initrd fixed device path vendor GUID.

Definition at line 57 of file efi_file.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ efi_file_free()

static void efi_file_free ( struct refcnt refcnt)
static

Free EFI file.

Parameters
refcntReference count

Definition at line 114 of file efi_file.c.

114  {
115  struct efi_file *file =
116  container_of ( refcnt, struct efi_file, refcnt );
117 
118  image_put ( file->image );
119  free ( file );
120 }
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
A reference counter.
Definition: refcnt.h:26
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:228
An EFI file.
Definition: efi_file.c:74

References container_of, efi_file::file, free, and image_put().

Referenced by efi_file_open_image().

◆ efi_file_name()

static const char* efi_file_name ( struct efi_file file)
static

Get EFI file name (for debugging)

Parameters
fileEFI file
Return values
nameName

Definition at line 128 of file efi_file.c.

128  {
129 
130  return ( file == &efi_file_root ? "<root>" : file->name );
131 }
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106

References efi_file_root, and efi_file::file.

Referenced by efi_file_close(), efi_file_delete(), efi_file_flush(), efi_file_get_info(), efi_file_load(), efi_file_open(), efi_file_open_fixed(), efi_file_open_image(), efi_file_path_claim(), efi_file_path_install(), efi_file_path_uninstall(), efi_file_read(), efi_file_read_initrd(), efi_file_set_info(), efi_file_set_position(), and efi_file_write().

◆ efi_file_find()

static struct image* efi_file_find ( const char *  name)
static

Find EFI file image.

Parameters
nameFilename
Return values
imageImage, or NULL

Definition at line 139 of file efi_file.c.

139  {
140  struct image *image;
141 
142  /* Find image */
143  for_each_image ( image ) {
144  if ( strcasecmp ( image->name, name ) == 0 )
145  return image;
146  }
147 
148  return NULL;
149 }
const char * name
Definition: ath9k_hw.c:1984
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:208
An executable image.
Definition: image.h:24
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:172
char * name
Name.
Definition: image.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References for_each_image, image::name, name, NULL, and strcasecmp().

Referenced by efi_file_open().

◆ efi_file_len()

static size_t efi_file_len ( struct efi_file file)
static

Get length of EFI file.

Parameters
fileEFI file
Return values
lenLength of file

Definition at line 157 of file efi_file.c.

157  {
158  struct efi_file_reader reader;
159 
160  /* If this is the root directory, then treat as length zero */
161  if ( ! file->read )
162  return 0;
163 
164  /* Initialise reader */
165  reader.file = file;
166  reader.pos = 0;
167  reader.data = NULL;
168  reader.len = 0;
169 
170  /* Perform dummy read to determine file length */
171  file->read ( &reader );
172 
173  return reader.pos;
174 }
An EFI virtual file reader.
Definition: efi_file.c:62
size_t pos
Current file position.
Definition: efi_file.c:86
struct efi_file * file
EFI file.
Definition: efi_file.c:64
size_t(* read)(struct efi_file_reader *reader)
Read from file.
Definition: efi_file.c:93
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References efi_file_reader::data, efi_file_reader::file, efi_file_reader::len, NULL, efi_file_reader::pos, and efi_file::read.

Referenced by efi_file_info(), efi_file_load(), and efi_file_set_position().

◆ efi_file_read_chunk()

static size_t efi_file_read_chunk ( struct efi_file_reader reader,
userptr_t  data,
size_t  len 
)
static

Read chunk of EFI file.

Parameters
readerEFI file reader
dataInput data, or UNULL to zero-fill
lenLength of input data
Return values
lenLength of output data

Definition at line 184 of file efi_file.c.

185  {
186  struct efi_file *file = reader->file;
187  size_t offset;
188 
189  /* Calculate offset into input data */
190  offset = ( file->pos - reader->pos );
191 
192  /* Consume input data range */
193  reader->pos += len;
194 
195  /* Calculate output length */
196  if ( offset < len ) {
197  len -= offset;
198  } else {
199  len = 0;
200  }
201  if ( len > reader->len )
202  len = reader->len;
203 
204  /* Copy or zero output data */
205  if ( data ) {
206  copy_from_user ( reader->data, data, offset, len );
207  } else {
208  memset ( reader->data, 0, len );
209  }
210 
211  /* Consume output buffer */
212  file->pos += len;
213  reader->data += len;
214  reader->len -= len;
215 
216  return len;
217 }
void * data
Output data buffer.
Definition: efi_file.c:68
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
struct efi_file * file
EFI file.
Definition: efi_file.c:64
size_t len
Length of output data buffer.
Definition: efi_file.c:70
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
size_t pos
Position within virtual file.
Definition: efi_file.c:66
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
An EFI file.
Definition: efi_file.c:74
void * memset(void *dest, int character, size_t len) __nonnull

References copy_from_user(), data, efi_file_reader::data, efi_file_reader::file, efi_file::file, len, efi_file_reader::len, memset(), offset, and efi_file_reader::pos.

Referenced by efi_file_read_image(), and efi_file_read_initrd().

◆ efi_file_read_image()

static size_t efi_file_read_image ( struct efi_file_reader reader)
static

Read from image-backed file.

Parameters
readerEFI file reader
Return values
lenLength read

Definition at line 225 of file efi_file.c.

225  {
226  struct efi_file *file = reader->file;
227  struct image *image = file->image;
228 
229  /* Read from file */
230  return efi_file_read_chunk ( reader, image->data, image->len );
231 }
userptr_t data
Raw file image.
Definition: image.h:41
An executable image.
Definition: image.h:24
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
struct efi_file * file
EFI file.
Definition: efi_file.c:64
static size_t efi_file_read_chunk(struct efi_file_reader *reader, userptr_t data, size_t len)
Read chunk of EFI file.
Definition: efi_file.c:184
size_t len
Length of raw file image.
Definition: image.h:43
struct image * image
Image (if any)
Definition: efi_file.c:82
An EFI file.
Definition: efi_file.c:74

References image::data, efi_file_read_chunk(), efi_file_reader::file, efi_file::file, efi_file::image, and image::len.

Referenced by efi_file_image().

◆ efi_file_read_initrd()

static size_t efi_file_read_initrd ( struct efi_file_reader reader)
static

Read from magic initrd file.

Parameters
readerEFI file reader
Return values
lenLength read

Definition at line 239 of file efi_file.c.

239  {
240  struct efi_file *file = reader->file;
241  struct cpio_header cpio;
242  struct image *image;
243  const char *name;
244  size_t pad_len;
245  size_t cpio_len;
246  size_t name_len;
247  size_t len;
248 
249  /* Read from file */
250  len = 0;
251  for_each_image ( image ) {
252 
253  /* Skip hidden images */
254  if ( image->flags & IMAGE_HIDDEN )
255  continue;
256 
257  /* Pad to alignment boundary */
258  pad_len = ( ( -reader->pos ) & ( INITRD_ALIGN - 1 ) );
259  if ( pad_len ) {
260  DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) pad\n",
261  efi_file_name ( file ), reader->pos,
262  ( reader->pos + pad_len ) );
263  }
264  len += efi_file_read_chunk ( reader, UNULL, pad_len );
265 
266  /* Read CPIO header, if applicable */
267  cpio_len = cpio_header ( image, &cpio );
268  if ( cpio_len ) {
269  name = cpio_name ( image );
270  name_len = cpio_name_len ( image );
271  pad_len = ( cpio_len - sizeof ( cpio ) - name_len );
272  DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) %s header\n",
273  efi_file_name ( file ), reader->pos,
274  ( reader->pos + cpio_len ), image->name );
275  len += efi_file_read_chunk ( reader,
276  virt_to_user ( &cpio ),
277  sizeof ( cpio ) );
278  len += efi_file_read_chunk ( reader,
279  virt_to_user ( name ),
280  name_len );
281  len += efi_file_read_chunk ( reader, UNULL, pad_len );
282  }
283 
284  /* Read file data */
285  DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) %s\n",
286  efi_file_name ( file ), reader->pos,
287  ( reader->pos + image->len ), image->name );
288  len += efi_file_read_chunk ( reader, image->data, image->len );
289  }
290 
291  return len;
292 }
unsigned int flags
Flags.
Definition: image.h:36
size_t cpio_name_len(struct image *image)
Get CPIO image filename.
Definition: cpio.c:56
const char * name
Definition: ath9k_hw.c:1984
userptr_t data
Raw file image.
Definition: image.h:41
A CPIO archive header.
Definition: cpio.h:19
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
struct efi_file * file
EFI file.
Definition: efi_file.c:64
static size_t efi_file_read_chunk(struct efi_file_reader *reader, userptr_t data, size_t len)
Read chunk of EFI file.
Definition: efi_file.c:184
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:172
size_t len
Length of raw file image.
Definition: image.h:43
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:73
size_t pos
Position within virtual file.
Definition: efi_file.c:66
long pad_len
Definition: bigint.h:30
#define INITRD_ALIGN
Alignment for CPIO archives within an initrd.
Definition: cpio.h:57
#define UNULL
Equivalent of NULL for user pointers.
Definition: uaccess.h:36
uint32_t len
Length.
Definition: ena.h:14
userptr_t virt_to_user(volatile const void *addr)
Convert virtual address to user pointer.
char * name
Name.
Definition: image.h:34
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128
size_t cpio_header(struct image *image, struct cpio_header *cpio)
Construct CPIO header for image, if applicable.
Definition: cpio.c:102
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition: cpio.h:66

References cpio_header(), cpio_name(), cpio_name_len(), image::data, DBGC, efi_file_name(), efi_file_read_chunk(), efi_file_reader::file, efi_file::file, image::flags, for_each_image, IMAGE_HIDDEN, INITRD_ALIGN, len, image::len, image::name, name, pad_len, efi_file_reader::pos, UNULL, and virt_to_user().

◆ efi_file_open_fixed()

static EFI_STATUS efi_file_open_fixed ( struct efi_file file,
const wchar_t wname,
EFI_FILE_PROTOCOL **  new 
)
static

Open fixed file.

Parameters
fileEFI file
wnameFilename
newNew EFI file
Return values
efircEFI status code

Definition at line 302 of file efi_file.c.

304  {
305 
306  /* Increment reference count */
307  ref_get ( &file->refcnt );
308 
309  /* Return opened file */
310  *new = &file->file;
311 
312  DBGC ( file, "EFIFILE %s opened via %ls\n",
313  efi_file_name ( file ), wname );
314  return 0;
315 }
struct refcnt refcnt
Reference count.
Definition: efi_file.c:76
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References DBGC, efi_file_name(), efi_file::file, ref_get, and efi_file::refcnt.

Referenced by efi_file_open(), and efi_file_open_volume().

◆ efi_file_image()

static void efi_file_image ( struct efi_file file,
struct image image 
)
static

Associate file with image.

Parameters
fileEFI file
imageImage

Definition at line 323 of file efi_file.c.

323  {
324 
325  file->image = image;
326  file->name = image->name;
327  file->read = efi_file_read_image;
328 }
An executable image.
Definition: image.h:24
static size_t efi_file_read_image(struct efi_file_reader *reader)
Read from image-backed file.
Definition: efi_file.c:225
struct image * image
Image (if any)
Definition: efi_file.c:82
size_t(* read)(struct efi_file_reader *reader)
Read from file.
Definition: efi_file.c:93
char * name
Name.
Definition: image.h:34
const char * name
Filename.
Definition: efi_file.c:84

References efi_file_read_image(), efi_file::image, image::name, efi_file::name, and efi_file::read.

Referenced by efi_file_open_image(), and efi_file_read_dir().

◆ efi_file_open_image()

static EFI_STATUS efi_file_open_image ( struct image image,
const wchar_t wname,
EFI_FILE_PROTOCOL **  new 
)
static

Open image-backed file.

Parameters
imageImage
wnameFilename
newNew EFI file
Return values
efircEFI status code

Definition at line 338 of file efi_file.c.

340  {
341  struct efi_file *file;
342 
343  /* Allocate and initialise file */
344  file = zalloc ( sizeof ( *file ) );
345  if ( ! file )
346  return EFI_OUT_OF_RESOURCES;
347  ref_init ( &file->refcnt, efi_file_free );
348  memcpy ( &file->file, &efi_file_root.file, sizeof ( file->file ) );
349  memcpy ( &file->load, &efi_file_root.load, sizeof ( file->load ) );
351 
352  /* Return opened file */
353  *new = &file->file;
354 
355  DBGC ( file, "EFIFILE %s opened via %ls\n",
356  efi_file_name ( file ), wname );
357  return 0;
358 }
static void efi_file_free(struct refcnt *refcnt)
Free EFI file.
Definition: efi_file.c:114
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:218
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define EFI_OUT_OF_RESOURCES
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:123
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
EFI_LOAD_FILE2_PROTOCOL load
EFI load file protocol.
Definition: efi_file.c:80
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128
static void efi_file_image(struct efi_file *file, struct image *image)
Associate file with image.
Definition: efi_file.c:323

References DBGC, efi_file_free(), efi_file_image(), efi_file_name(), efi_file_root, EFI_OUT_OF_RESOURCES, efi_file::file, image_get(), efi_file::load, memcpy(), ref_init, and zalloc().

Referenced by efi_file_open().

◆ efi_file_open()

static EFI_STATUS EFIAPI efi_file_open ( EFI_FILE_PROTOCOL this,
EFI_FILE_PROTOCOL **  new,
CHAR16 wname,
UINT64  mode,
UINT64 attributes  __unused 
)
static

Open file.

Parameters
thisEFI file
Return values
newNew EFI file
Parameters
wnameFilename
modeFile mode
attributesFile attributes (for newly-created files)
Return values
efircEFI status code

Definition at line 371 of file efi_file.c.

372  {
373  struct efi_file *file = container_of ( this, struct efi_file, file );
374  char buf[ wcslen ( wname ) + 1 /* NUL */ ];
375  struct image *image;
376  char *name;
377  char *sep;
378 
379  /* Convert name to ASCII */
380  snprintf ( buf, sizeof ( buf ), "%ls", wname );
381  name = buf;
382 
383  /* Initial '\' indicates opening from the root directory */
384  while ( *name == '\\' ) {
385  file = &efi_file_root;
386  name++;
387  }
388 
389  /* Allow root directory itself to be opened */
390  if ( ( name[0] == '\0' ) || ( name[0] == '.' ) )
391  return efi_file_open_fixed ( &efi_file_root, wname, new );
392 
393  /* Fail unless opening from the root */
394  if ( file != &efi_file_root ) {
395  DBGC ( file, "EFIFILE %s is not a directory\n",
396  efi_file_name ( file ) );
397  return EFI_NOT_FOUND;
398  }
399 
400  /* Fail unless opening read-only */
401  if ( mode != EFI_FILE_MODE_READ ) {
402  DBGC ( file, "EFIFILE %s cannot be opened in mode %#08llx\n",
403  name, mode );
404  return EFI_WRITE_PROTECTED;
405  }
406 
407  /* Allow registered images to be opened */
408  if ( ( image = efi_file_find ( name ) ) != NULL )
409  return efi_file_open_image ( image, wname, new );
410 
411  /* Allow magic initrd to be opened */
412  if ( strcasecmp ( name, efi_file_initrd.file.name ) == 0 ) {
413  return efi_file_open_fixed ( &efi_file_initrd.file, wname,
414  new );
415  }
416 
417  /* Allow currently selected image to be opened as "grub*.efi",
418  * to work around buggy versions of the UEFI shim.
419  */
420  if ( ( strncasecmp ( name, "grub", 4 ) == 0 ) &&
421  ( ( sep = strrchr ( name, '.' ) ) != NULL ) &&
422  ( strcasecmp ( sep, ".efi" ) == 0 ) &&
423  ( ( image = find_image_tag ( &selected_image ) ) != NULL ) ) {
424  return efi_file_open_image ( image, wname, new );
425  }
426 
427  DBGC ( file, "EFIFILE %ls does not exist\n", wname );
428  return EFI_NOT_FOUND;
429 }
struct image_tag selected_image
const char * name
Definition: ath9k_hw.c:1984
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:289
static EFI_STATUS efi_file_open_image(struct image *image, const wchar_t *wname, EFI_FILE_PROTOCOL **new)
Open image-backed file.
Definition: efi_file.c:338
#define DBGC(...)
Definition: compiler.h:505
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:208
int strncasecmp(const char *first, const char *second, size_t max)
Compare case-insensitive strings.
Definition: string.c:221
An executable image.
Definition: image.h:24
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
size_t wcslen(const wchar_t *string)
Calculate length of wide-character string.
Definition: wchar.c:41
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition: image.c:338
static struct efi_file_path efi_file_initrd
Magic initrd file.
Definition: efi_file.c:107
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static EFI_STATUS efi_file_open_fixed(struct efi_file *file, const wchar_t *wname, EFI_FILE_PROTOCOL **new)
Open fixed file.
Definition: efi_file.c:302
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
#define EFI_WRITE_PROTECTED
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:122
#define EFI_NOT_FOUND
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:128
#define EFI_FILE_MODE_READ
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
static struct image * efi_file_find(const char *name)
Find EFI file image.
Definition: efi_file.c:139
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
An EFI file.
Definition: efi_file.c:74
const char * name
Filename.
Definition: efi_file.c:84
struct efi_file file
EFI file.
Definition: efi_file.c:99
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References container_of, DBGC, efi_file_find(), efi_file_initrd, EFI_FILE_MODE_READ, efi_file_name(), efi_file_open_fixed(), efi_file_open_image(), efi_file_root, EFI_NOT_FOUND, EFI_WRITE_PROTECTED, efi_file::file, efi_file_path::file, find_image_tag(), efi_file::name, name, NULL, selected_image, snprintf(), strcasecmp(), strncasecmp(), strrchr(), and wcslen().

◆ efi_file_close()

static EFI_STATUS EFIAPI efi_file_close ( EFI_FILE_PROTOCOL this)
static

Close file.

Parameters
thisEFI file
Return values
efircEFI status code

Definition at line 437 of file efi_file.c.

437  {
438  struct efi_file *file = container_of ( this, struct efi_file, file );
439 
440  /* Close file */
441  DBGC ( file, "EFIFILE %s closed\n", efi_file_name ( file ) );
442  ref_put ( &file->refcnt );
443 
444  return 0;
445 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An EFI file.
Definition: efi_file.c:74
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References container_of, DBGC, efi_file_name(), efi_file::file, and ref_put.

Referenced by efi_file_delete().

◆ efi_file_delete()

static EFI_STATUS EFIAPI efi_file_delete ( EFI_FILE_PROTOCOL this)
static

Close and delete file.

Parameters
thisEFI file
Return values
efircEFI status code

Definition at line 453 of file efi_file.c.

453  {
454  struct efi_file *file = container_of ( this, struct efi_file, file );
455 
456  DBGC ( file, "EFIFILE %s cannot be deleted\n", efi_file_name ( file ) );
457 
458  /* Close file */
459  efi_file_close ( this );
460 
461  /* Warn of failure to delete */
463 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static EFI_STATUS EFIAPI efi_file_close(EFI_FILE_PROTOCOL *this)
Close file.
Definition: efi_file.c:437
#define EFI_WARN_DELETE_FAILURE
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:150
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References container_of, DBGC, efi_file_close(), efi_file_name(), EFI_WARN_DELETE_FAILURE, and efi_file::file.

◆ efi_file_varlen()

static EFI_STATUS efi_file_varlen ( UINT64 base,
size_t  base_len,
const char *  name,
UINTN len,
VOID data 
)
static

Return variable-length data structure.

Parameters
baseBase data structure (starting with UINT64)
base_lenLength of base data structure
nameName to append to base data structure
lenLength of data buffer
dataData buffer
Return values
efircEFI status code

Definition at line 475 of file efi_file.c.

476  {
477  size_t name_len;
478 
479  /* Calculate structure length */
480  name_len = strlen ( name );
481  *base = ( base_len + ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
482  if ( *len < *base ) {
483  *len = *base;
484  return EFI_BUFFER_TOO_SMALL;
485  }
486 
487  /* Copy data to buffer */
488  *len = *base;
489  memcpy ( data, base, base_len );
490  efi_snprintf ( ( data + base_len ), ( name_len + 1 /* NUL */ ),
491  "%s", name );
492 
493  return 0;
494 }
const char * name
Definition: ath9k_hw.c:1984
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:119
static const void * base
Base address.
Definition: crypto.h:335
void * memcpy(void *dest, const void *src, size_t len) __nonnull
int efi_snprintf(wchar_t *wbuf, size_t wsize, const char *fmt,...)
Write a formatted string to a buffer.
Definition: efi_strings.c:106
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22

References base, data, EFI_BUFFER_TOO_SMALL, efi_snprintf(), len, memcpy(), name, and strlen().

Referenced by efi_file_get_info(), and efi_file_info().

◆ efi_file_info()

static EFI_STATUS efi_file_info ( struct efi_file file,
UINTN len,
VOID data 
)
static

Return file information structure.

Parameters
fileEFI file
lenLength of data buffer
dataData buffer
Return values
efircEFI status code

Definition at line 504 of file efi_file.c.

505  {
507  size_t file_len;
508 
509  /* Get file length */
510  file_len = efi_file_len ( file );
511 
512  /* Populate file information */
513  memset ( &info, 0, sizeof ( info ) );
514  info.FileSize = file_len;
515  info.PhysicalSize = file_len;
516  info.Attribute = EFI_FILE_READ_ONLY;
517  if ( file == &efi_file_root )
518  info.Attribute |= EFI_FILE_DIRECTORY;
519 
521  file->name, len, data );
522 }
u32 info
Definition: ar9003_mac.h:67
#define EFI_FILE_READ_ONLY
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define EFI_FILE_DIRECTORY
#define SIZE_OF_EFI_FILE_INFO
The FileName field of the EFI_FILE_INFO data structure is variable length.
Definition: FileInfo.h:64
static size_t efi_file_len(struct efi_file *file)
Get length of EFI file.
Definition: efi_file.c:157
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
static EFI_STATUS efi_file_varlen(UINT64 *base, size_t base_len, const char *name, UINTN *len, VOID *data)
Return variable-length data structure.
Definition: efi_file.c:475
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
void * memset(void *dest, int character, size_t len) __nonnull

References data, EFI_FILE_DIRECTORY, efi_file_len(), EFI_FILE_READ_ONLY, efi_file_root, efi_file_varlen(), efi_file::file, info, len, memset(), and SIZE_OF_EFI_FILE_INFO.

Referenced by efi_file_get_info(), and efi_file_read_dir().

◆ efi_file_read_dir()

static EFI_STATUS efi_file_read_dir ( struct efi_file file,
UINTN len,
VOID data 
)
static

Read directory entry.

Parameters
fileEFI file
lenLength to read
dataData buffer
Return values
efircEFI status code

Definition at line 532 of file efi_file.c.

533  {
534  EFI_STATUS efirc;
535  struct efi_file entry;
536  struct image *image;
537  unsigned int index;
538 
539  /* Construct directory entries for image-backed files */
540  index = file->pos;
541  for_each_image ( image ) {
542 
543  /* Skip hidden images */
544  if ( image->flags & IMAGE_HIDDEN )
545  continue;
546 
547  /* Skip preceding images */
548  if ( index-- )
549  continue;
550 
551  /* Construct directory entry */
552  efi_file_image ( &entry, image );
553  efirc = efi_file_info ( &entry, len, data );
554  if ( efirc == 0 )
555  file->pos++;
556  return efirc;
557  }
558 
559  /* No more entries */
560  *len = 0;
561  return 0;
562 }
unsigned int flags
Flags.
Definition: image.h:36
An executable image.
Definition: image.h:24
static EFI_STATUS efi_file_info(struct efi_file *file, UINTN *len, VOID *data)
Return file information structure.
Definition: efi_file.c:504
size_t pos
Current file position.
Definition: efi_file.c:86
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:172
union aes_table_entry entry[256]
Table entries, indexed by S(N)
Definition: aes.c:26
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:73
uint32_t len
Length.
Definition: ena.h:14
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21
An EFI file.
Definition: efi_file.c:74
static void efi_file_image(struct efi_file *file, struct image *image)
Associate file with image.
Definition: efi_file.c:323

References data, efi_file_image(), efi_file_info(), entry, image::flags, for_each_image, IMAGE_HIDDEN, index, len, and efi_file::pos.

Referenced by efi_file_read().

◆ efi_file_read()

static EFI_STATUS EFIAPI efi_file_read ( EFI_FILE_PROTOCOL this,
UINTN len,
VOID data 
)
static

Read from file.

Parameters
thisEFI file
lenLength to read
dataData buffer
Return values
efircEFI status code

Definition at line 572 of file efi_file.c.

573  {
574  struct efi_file *file = container_of ( this, struct efi_file, file );
575  struct efi_file_reader reader;
576  size_t pos = file->pos;
577 
578  /* If this is the root directory, then construct a directory entry */
579  if ( ! file->read )
580  return efi_file_read_dir ( file, len, data );
581 
582  /* Initialise reader */
583  reader.file = file;
584  reader.pos = 0;
585  reader.data = data;
586  reader.len = *len;
587 
588  /* Read from the file */
589  DBGC ( file, "EFIFILE %s read [%#08zx,%#08zx)\n",
590  efi_file_name ( file ), pos, ( ( size_t ) ( pos + *len ) ) );
591  *len = file->read ( &reader );
592  assert ( ( pos + *len ) == file->pos );
593 
594  return 0;
595 }
An EFI virtual file reader.
Definition: efi_file.c:62
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
size_t pos
Current file position.
Definition: efi_file.c:86
struct efi_file * file
EFI file.
Definition: efi_file.c:64
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static EFI_STATUS efi_file_read_dir(struct efi_file *file, UINTN *len, VOID *data)
Read directory entry.
Definition: efi_file.c:532
size_t pos
Position within virtual file.
Definition: efi_file.c:66
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
size_t(* read)(struct efi_file_reader *reader)
Read from file.
Definition: efi_file.c:93
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References assert(), container_of, data, efi_file_reader::data, DBGC, efi_file_name(), efi_file_read_dir(), efi_file_reader::file, efi_file::file, len, efi_file_reader::len, efi_file_reader::pos, efi_file::pos, and efi_file::read.

Referenced by efi_file_load().

◆ efi_file_write()

static EFI_STATUS EFIAPI efi_file_write ( EFI_FILE_PROTOCOL this,
UINTN len,
VOID *data  __unused 
)
static

Write to file.

Parameters
thisEFI file
lenLength to write
dataData buffer
Return values
efircEFI status code

Definition at line 605 of file efi_file.c.

606  {
607  struct efi_file *file = container_of ( this, struct efi_file, file );
608 
609  DBGC ( file, "EFIFILE %s cannot write [%#08zx, %#08zx)\n",
610  efi_file_name ( file ), file->pos,
611  ( ( size_t ) ( file->pos + *len ) ) );
612  return EFI_WRITE_PROTECTED;
613 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
#define EFI_WRITE_PROTECTED
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:122
uint32_t len
Length.
Definition: ena.h:14
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References container_of, DBGC, efi_file_name(), EFI_WRITE_PROTECTED, efi_file::file, and len.

◆ efi_file_set_position()

static EFI_STATUS EFIAPI efi_file_set_position ( EFI_FILE_PROTOCOL this,
UINT64  position 
)
static

Set file position.

Parameters
thisEFI file
positionNew file position
Return values
efircEFI status code

Definition at line 622 of file efi_file.c.

623  {
624  struct efi_file *file = container_of ( this, struct efi_file, file );
625  size_t len;
626 
627  /* Get file length */
628  len = efi_file_len ( file );
629 
630  /* Check for the magic end-of-file value */
631  if ( position == 0xffffffffffffffffULL )
632  position = len;
633 
634  /* Fail if we attempt to seek past the end of the file (since
635  * we do not support writes).
636  */
637  if ( position > len ) {
638  DBGC ( file, "EFIFILE %s cannot seek to %#08llx of %#08zx\n",
639  efi_file_name ( file ), position, len );
640  return EFI_UNSUPPORTED;
641  }
642 
643  /* Set position */
644  file->pos = position;
645  DBGC ( file, "EFIFILE %s position set to %#08zx\n",
646  efi_file_name ( file ), file->pos );
647 
648  return 0;
649 }
#define EFI_UNSUPPORTED
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:117
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
static size_t efi_file_len(struct efi_file *file)
Get length of EFI file.
Definition: efi_file.c:157
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
uint32_t len
Length.
Definition: ena.h:14
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References container_of, DBGC, efi_file_len(), efi_file_name(), EFI_UNSUPPORTED, efi_file::file, and len.

◆ efi_file_get_position()

static EFI_STATUS EFIAPI efi_file_get_position ( EFI_FILE_PROTOCOL this,
UINT64 position 
)
static

Get file position.

Parameters
thisEFI file
Return values
positionNew file position
efircEFI status code

Definition at line 658 of file efi_file.c.

659  {
660  struct efi_file *file = container_of ( this, struct efi_file, file );
661 
662  *position = file->pos;
663  return 0;
664 }
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An EFI file.
Definition: efi_file.c:74

References container_of, and efi_file::file.

◆ efi_file_get_info()

static EFI_STATUS EFIAPI efi_file_get_info ( EFI_FILE_PROTOCOL this,
EFI_GUID type,
UINTN len,
VOID data 
)
static

Get file information.

Parameters
thisEFI file
typeType of information
lenBuffer size
dataBuffer
Return values
efircEFI status code

Definition at line 675 of file efi_file.c.

677  {
678  struct efi_file *file = container_of ( this, struct efi_file, file );
679  EFI_FILE_SYSTEM_INFO fsinfo;
680  struct image *image;
681 
682  /* Determine information to return */
683  if ( memcmp ( type, &efi_file_info_id, sizeof ( *type ) ) == 0 ) {
684 
685  /* Get file information */
686  DBGC ( file, "EFIFILE %s get file information\n",
687  efi_file_name ( file ) );
688  return efi_file_info ( file, len, data );
689 
690  } else if ( memcmp ( type, &efi_file_system_info_id,
691  sizeof ( *type ) ) == 0 ) {
692 
693  /* Get file system information */
694  DBGC ( file, "EFIFILE %s get file system information\n",
695  efi_file_name ( file ) );
696  memset ( &fsinfo, 0, sizeof ( fsinfo ) );
697  fsinfo.ReadOnly = 1;
699  fsinfo.VolumeSize += image->len;
700  return efi_file_varlen ( &fsinfo.Size,
702  len, data );
703  } else {
704 
705  DBGC ( file, "EFIFILE %s cannot get information of type %s\n",
706  efi_file_name ( file ), efi_guid_ntoa ( type ) );
707  return EFI_UNSUPPORTED;
708  }
709 }
#define EFI_UNSUPPORTED
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:117
BOOLEAN ReadOnly
TRUE if the volume only supports read access.
EFI_GUID efi_file_system_info_id
File system information GUID.
Definition: efi_guid.c:393
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:24
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
static EFI_STATUS efi_file_info(struct efi_file *file, UINTN *len, VOID *data)
Return file information structure.
Definition: efi_file.c:504
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:172
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_debug.c:254
static EFI_STATUS efi_file_varlen(UINT64 *base, size_t base_len, const char *name, UINTN *len, VOID *data)
Return variable-length data structure.
Definition: efi_file.c:475
size_t len
Length of raw file image.
Definition: image.h:43
UINT64 VolumeSize
The number of bytes managed by the file system.
uint32_t len
Length.
Definition: ena.h:14
uint32_t type
Operating system type.
Definition: ena.h:12
EFI_GUID efi_file_info_id
File information GUID.
Definition: efi_guid.c:390
uint8_t data[48]
Additional event data.
Definition: ena.h:22
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
UINT64 Size
The size of the EFI_FILE_SYSTEM_INFO structure, including the Null-terminated VolumeLabel string.
An EFI file.
Definition: efi_file.c:74
#define SIZE_OF_EFI_FILE_SYSTEM_INFO
The VolumeLabel field of the EFI_FILE_SYSTEM_INFO data structure is variable length.
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128
void * memset(void *dest, int character, size_t len) __nonnull

References container_of, data, DBGC, efi_file_info(), efi_file_info_id, efi_file_name(), efi_file_system_info_id, efi_file_varlen(), efi_guid_ntoa(), EFI_UNSUPPORTED, efi_file::file, for_each_image, len, image::len, memcmp(), memset(), EFI_FILE_SYSTEM_INFO::ReadOnly, EFI_FILE_SYSTEM_INFO::Size, SIZE_OF_EFI_FILE_SYSTEM_INFO, type, and EFI_FILE_SYSTEM_INFO::VolumeSize.

◆ efi_file_set_info()

static EFI_STATUS EFIAPI efi_file_set_info ( EFI_FILE_PROTOCOL this,
EFI_GUID type,
UINTN len  __unused,
VOID *data  __unused 
)
static

Set file information.

Parameters
thisEFI file
typeType of information
lenBuffer size
dataBuffer
Return values
efircEFI status code

Definition at line 721 of file efi_file.c.

722  {
723  struct efi_file *file = container_of ( this, struct efi_file, file );
724 
725  DBGC ( file, "EFIFILE %s cannot set information of type %s\n",
727  return EFI_WRITE_PROTECTED;
728 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
#define EFI_WRITE_PROTECTED
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:122
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_debug.c:254
uint32_t type
Operating system type.
Definition: ena.h:12
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References container_of, DBGC, efi_file_name(), efi_guid_ntoa(), EFI_WRITE_PROTECTED, efi_file::file, and type.

◆ efi_file_flush()

static EFI_STATUS EFIAPI efi_file_flush ( EFI_FILE_PROTOCOL this)
static

Flush file modified data.

Parameters
thisEFI file
typeType of information
lenBuffer size
dataBuffer
Return values
efircEFI status code

Definition at line 739 of file efi_file.c.

739  {
740  struct efi_file *file = container_of ( this, struct efi_file, file );
741 
742  DBGC ( file, "EFIFILE %s flushed\n", efi_file_name ( file ) );
743  return 0;
744 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References container_of, DBGC, efi_file_name(), and efi_file::file.

◆ efi_file_load()

static EFI_STATUS EFIAPI efi_file_load ( EFI_LOAD_FILE2_PROTOCOL this,
EFI_DEVICE_PATH_PROTOCOL *path  __unused,
BOOLEAN boot  __unused,
UINTN len,
VOID data 
)
static

Load file.

Parameters
thisEFI file loader
pathFile path
bootBoot policy
lenBuffer size
dataBuffer, or NULL
Return values
efircEFI status code

Definition at line 757 of file efi_file.c.

759  {
760  struct efi_file *file = container_of ( this, struct efi_file, load );
761  size_t max_len;
762  size_t file_len;
763  EFI_STATUS efirc;
764 
765  /* Calculate maximum length */
766  max_len = ( data ? *len : 0 );
767  DBGC ( file, "EFIFILE %s load at %p+%#zx\n",
768  efi_file_name ( file ), data, max_len );
769 
770  /* Check buffer size */
771  file_len = efi_file_len ( file );
772  if ( file_len > max_len ) {
773  *len = file_len;
774  return EFI_BUFFER_TOO_SMALL;
775  }
776 
777  /* Read from file */
778  if ( ( efirc = efi_file_read ( &file->file, len, data ) ) != 0 )
779  return efirc;
780 
781  return 0;
782 }
uint16_t max_len
Maximum length (in bytes)
Definition: ntlm.h:18
#define DBGC(...)
Definition: compiler.h:505
static EFI_STATUS EFIAPI efi_file_read(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data)
Read from file.
Definition: efi_file.c:572
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:119
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
static size_t efi_file_len(struct efi_file *file)
Get length of EFI file.
Definition: efi_file.c:157
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
EFI_LOAD_FILE2_PROTOCOL load
EFI load file protocol.
Definition: efi_file.c:80
uint32_t len
Length.
Definition: ena.h:14
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
uint8_t data[48]
Additional event data.
Definition: ena.h:22
An EFI file.
Definition: efi_file.c:74
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References container_of, data, DBGC, EFI_BUFFER_TOO_SMALL, efi_file_len(), efi_file_name(), efi_file_read(), efi_file::file, len, efi_file::load, and max_len.

◆ efi_file_open_volume()

static EFI_STATUS EFIAPI efi_file_open_volume ( EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *filesystem  __unused,
EFI_FILE_PROTOCOL **  file 
)
static

Open root directory.

Parameters
filesystemEFI simple file system
Return values
fileEFI file handle
efircEFI status code

Definition at line 862 of file efi_file.c.

863  {
864 
865  DBGC ( &efi_file_root, "EFIFILE open volume\n" );
866  return efi_file_open_fixed ( &efi_file_root, L"<volume>", file );
867 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
static EFI_STATUS efi_file_open_fixed(struct efi_file *file, const wchar_t *wname, EFI_FILE_PROTOCOL **new)
Open fixed file.
Definition: efi_file.c:302
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106

References DBGC, efi_file_open_fixed(), efi_file_root, and efi_file_path::file.

◆ efi_block_io_reset()

static EFI_STATUS EFIAPI efi_block_io_reset ( EFI_BLOCK_IO_PROTOCOL *this  __unused,
BOOLEAN  extended 
)
static

Dummy block I/O reset.

Definition at line 877 of file efi_file.c.

877  {
878 
879  DBGC ( &efi_file_root, "EFIFILE block %sreset\n",
880  ( extended ? "extended " : "" ) );
881  return 0;
882 }
#define DBGC(...)
Definition: compiler.h:505
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106

References DBGC, and efi_file_root.

◆ efi_block_io_read_blocks()

static EFI_STATUS EFIAPI efi_block_io_read_blocks ( EFI_BLOCK_IO_PROTOCOL *this  __unused,
UINT32  MediaId,
EFI_LBA  lba,
UINTN  len,
VOID data 
)
static

Dummy block I/O read.

Definition at line 886 of file efi_file.c.

887  {
888 
889  DBGC ( &efi_file_root, "EFIFILE block read ID %#08x LBA %#08llx -> "
890  "%p+%zx\n", MediaId, ( ( unsigned long long ) lba ),
891  data, ( ( size_t ) len ) );
892  return EFI_NO_MEDIA;
893 }
uint32_t lba
Start address.
Definition: scsi.h:23
#define DBGC(...)
Definition: compiler.h:505
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22

References data, DBGC, efi_file_root, EFI_NO_MEDIA, lba, and len.

◆ efi_block_io_write_blocks()

static EFI_STATUS EFIAPI efi_block_io_write_blocks ( EFI_BLOCK_IO_PROTOCOL *this  __unused,
UINT32  MediaId,
EFI_LBA  lba,
UINTN  len,
VOID data 
)
static

Dummy block I/O write.

Definition at line 897 of file efi_file.c.

899  {
900 
901  DBGC ( &efi_file_root, "EFIFILE block write ID %#08x LBA %#08llx <- "
902  "%p+%zx\n", MediaId, ( ( unsigned long long ) lba ),
903  data, ( ( size_t ) len ) );
904  return EFI_NO_MEDIA;
905 }
uint32_t lba
Start address.
Definition: scsi.h:23
#define DBGC(...)
Definition: compiler.h:505
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22

References data, DBGC, efi_file_root, EFI_NO_MEDIA, lba, and len.

◆ efi_block_io_flush_blocks()

static EFI_STATUS EFIAPI efi_block_io_flush_blocks ( EFI_BLOCK_IO_PROTOCOL *this  __unused)
static

Dummy block I/O flush.

Definition at line 909 of file efi_file.c.

909  {
910 
911  DBGC ( &efi_file_root, "EFIFILE block flush\n" );
912  return 0;
913 }
#define DBGC(...)
Definition: compiler.h:505
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106

References DBGC, and efi_file_root.

◆ efi_disk_io_read_disk()

static EFI_STATUS EFIAPI efi_disk_io_read_disk ( EFI_DISK_IO_PROTOCOL *this  __unused,
UINT32  MediaId,
UINT64  offset,
UINTN  len,
VOID data 
)
static

Dummy disk I/O read.

Definition at line 935 of file efi_file.c.

936  {
937 
938  DBGC ( &efi_file_root, "EFIFILE disk read ID %#08x offset %#08llx -> "
939  "%p+%zx\n", MediaId, ( ( unsigned long long ) offset ),
940  data, ( ( size_t ) len ) );
941  return EFI_NO_MEDIA;
942 }
#define DBGC(...)
Definition: compiler.h:505
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22

References data, DBGC, efi_file_root, EFI_NO_MEDIA, len, and offset.

◆ efi_disk_io_write_disk()

static EFI_STATUS EFIAPI efi_disk_io_write_disk ( EFI_DISK_IO_PROTOCOL *this  __unused,
UINT32  MediaId,
UINT64  offset,
UINTN  len,
VOID data 
)
static

Dummy disk I/O write.

Definition at line 946 of file efi_file.c.

947  {
948 
949  DBGC ( &efi_file_root, "EFIFILE disk write ID %#08x offset %#08llx <- "
950  "%p+%zx\n", MediaId, ( ( unsigned long long ) offset ),
951  data, ( ( size_t ) len ) );
952  return EFI_NO_MEDIA;
953 }
#define DBGC(...)
Definition: compiler.h:505
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22

References data, DBGC, efi_file_root, EFI_NO_MEDIA, len, and offset.

◆ efi_file_path_claim()

static int efi_file_path_claim ( struct efi_file_path file)
static

Claim use of fixed device path.

Parameters
fileFixed device path file
Return values
rcReturn status code

The design choice in Linux of using a single fixed device path is unfortunately messy to support, since device paths must be unique within a system. When multiple bootloaders are used (e.g. GRUB loading iPXE loading Linux) then only one bootloader can ever install the device path onto a handle. Bootloaders must therefore be prepared to locate an existing handle and uninstall its device path protocol instance before installing a new handle with the required device path.

Definition at line 977 of file efi_file.c.

977  {
981  VOID *old;
982  EFI_STATUS efirc;
983  int rc;
984 
985  /* Sanity check */
986  assert ( file->handle == NULL );
987 
988  /* Locate handle with this device path, if any */
989  end = file->path;
990  if ( ( ( efirc = bs->LocateDevicePath ( &efi_device_path_protocol_guid,
991  &end, &handle ) ) != 0 ) ||
992  ( end->Type != END_DEVICE_PATH_TYPE ) ) {
993  return 0;
994  }
995 
996  /* Locate device path protocol on this handle */
997  if ( ( ( efirc = bs->HandleProtocol ( handle,
999  &old ) ) != 0 ) ) {
1000  rc = -EEFI ( efirc );
1001  DBGC ( file, "EFIFILE %s could not locate %s: %s\n",
1002  efi_file_name ( &file->file ),
1003  efi_devpath_text ( file->path ), strerror ( rc ) );
1004  return rc;
1005  }
1006 
1007  /* Uninstall device path protocol, leaving other protocols untouched */
1008  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1009  handle,
1011  NULL ) ) != 0 ) {
1012  rc = -EEFI ( efirc );
1013  DBGC ( file, "EFIFILE %s could not claim %s: %s\n",
1014  efi_file_name ( &file->file ),
1015  efi_devpath_text ( file->path ), strerror ( rc ) );
1016  return rc;
1017  }
1018 
1019  DBGC ( file, "EFIFILE %s claimed %s",
1020  efi_file_name ( &file->file ), efi_devpath_text ( file->path ) );
1021  DBGC ( file, " from %s\n", efi_handle_name ( handle ) );
1022  return 0;
1023 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
#define END_DEVICE_PATH_TYPE
Definition: DevicePath.h:1371
#define DBGC(...)
Definition: compiler.h:505
int old
Definition: bitops.h:64
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:45
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
EFI_HANDLE_PROTOCOL HandleProtocol
Definition: UefiSpec.h:1954
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:461
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:808
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:1997
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:143
#define VOID
Undeclared type.
Definition: Base.h:271
EFI_DEVICE_PATH_PROTOCOL end
Definition: efi_file.c:810
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
UINT8 Type
0x01 Hardware Device Path.
Definition: DevicePath.h:46
EFI_SYSTEM_TABLE * efi_systab
uint16_t handle
Handle.
Definition: smbios.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
Definition: efi.h:59
EFI_LOCATE_DEVICE_PATH LocateDevicePath
Definition: UefiSpec.h:1958
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References assert(), EFI_SYSTEM_TABLE::BootServices, DBGC, EEFI, efi_device_path_protocol_guid, efi_devpath_text(), efi_file_name(), efi_handle_name(), efi_systab, end, END_DEVICE_PATH_TYPE, efi_file::file, efi_file_path::file, handle, EFI_BOOT_SERVICES::HandleProtocol, EFI_BOOT_SERVICES::LocateDevicePath, NULL, old, rc, strerror(), EFI_DEVICE_PATH_PROTOCOL::Type, EFI_BOOT_SERVICES::UninstallMultipleProtocolInterfaces, and VOID.

Referenced by efi_file_install().

◆ efi_file_path_install()

static int efi_file_path_install ( struct efi_file_path file)
static

Install fixed device path file.

Parameters
fileFixed device path file
Return values
rcReturn status code

Linux 5.7 added the ability to autodetect an initrd by searching for a handle via a fixed vendor-specific "Linux initrd device path" and then locating and using the EFI_LOAD_FILE2_PROTOCOL instance on that handle.

Definition at line 1036 of file efi_file.c.

1036  {
1038  EFI_STATUS efirc;
1039  int rc;
1040 
1041  /* Sanity check */
1042  assert ( file->handle == NULL );
1043 
1044  /* Create a new handle with this device path */
1045  if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1046  &file->handle,
1048  &efi_load_file2_protocol_guid, &file->file.load,
1049  NULL ) ) != 0 ) {
1050  rc = -EEFI ( efirc );
1051  DBGC ( file, "EFIFILE %s could not install %s: %s\n",
1052  efi_file_name ( &file->file ),
1053  efi_devpath_text ( file->path ), strerror ( rc ) );
1054  return rc;
1055  }
1056 
1057  DBGC ( file, "EFIFILE %s installed as %s\n",
1058  efi_file_name ( &file->file ), efi_devpath_text ( file->path ) );
1059  return 0;
1060 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
#define DBGC(...)
Definition: compiler.h:505
EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES InstallMultipleProtocolInterfaces
Definition: UefiSpec.h:1996
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:461
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI_GUID efi_load_file2_protocol_guid
Load file 2 protocol GUID.
Definition: efi_guid.c:239
EFI Boot Services Table.
Definition: UefiSpec.h:1917
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:143
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References assert(), EFI_SYSTEM_TABLE::BootServices, DBGC, EEFI, efi_device_path_protocol_guid, efi_devpath_text(), efi_file_name(), efi_load_file2_protocol_guid, efi_systab, efi_file::file, efi_file_path::file, EFI_BOOT_SERVICES::InstallMultipleProtocolInterfaces, NULL, rc, and strerror().

Referenced by efi_file_install().

◆ efi_file_path_uninstall()

static void efi_file_path_uninstall ( struct efi_file_path file)
static

Uninstall fixed device path file.

Parameters
fileFixed device path file
Return values
rcReturn status code

Definition at line 1068 of file efi_file.c.

1068  {
1070  EFI_STATUS efirc;
1071  int rc;
1072 
1073  /* Do nothing if file is already uninstalled */
1074  if ( ! file->handle )
1075  return;
1076 
1077  /* Uninstall protocols. Do this via two separate calls, in
1078  * case another executable has already uninstalled the device
1079  * path protocol from our handle.
1080  */
1081  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1082  file->handle,
1084  NULL ) ) != 0 ) {
1085  rc = -EEFI ( efirc );
1086  DBGC ( file, "EFIFILE %s could not uninstall %s: %s\n",
1087  efi_file_name ( &file->file ),
1088  efi_devpath_text ( file->path ), strerror ( rc ) );
1089  /* Continue uninstalling */
1090  }
1091  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1092  file->handle,
1093  &efi_load_file2_protocol_guid, &file->file.load,
1094  NULL ) ) != 0 ) {
1095  rc = -EEFI ( efirc );
1096  DBGC ( file, "EFIFILE %s could not uninstall %s: %s\n",
1097  efi_file_name ( &file->file ),
1099  strerror ( rc ) );
1100  /* Continue uninstalling */
1101  }
1102 
1103  /* Mark handle as uninstalled */
1104  file->handle = NULL;
1105 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:461
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_debug.c:254
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI_GUID efi_load_file2_protocol_guid
Load file 2 protocol GUID.
Definition: efi_guid.c:239
EFI Boot Services Table.
Definition: UefiSpec.h:1917
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:1997
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:143
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128

References EFI_SYSTEM_TABLE::BootServices, DBGC, EEFI, efi_device_path_protocol_guid, efi_devpath_text(), efi_file_name(), efi_guid_ntoa(), efi_load_file2_protocol_guid, efi_systab, efi_file::file, efi_file_path::file, NULL, rc, strerror(), and EFI_BOOT_SERVICES::UninstallMultipleProtocolInterfaces.

Referenced by efi_file_install(), and efi_file_uninstall().

◆ efi_file_install()

int efi_file_install ( EFI_HANDLE  handle)

Install EFI simple file system protocol.

Parameters
handleEFI handle
Return values
rcReturn status code

Definition at line 1113 of file efi_file.c.

1113  {
1115  union {
1116  EFI_DISK_IO_PROTOCOL *diskio;
1117  void *interface;
1118  } diskio;
1119  struct image *image;
1120  EFI_STATUS efirc;
1121  int rc;
1122 
1123  /* Reset root directory state */
1124  efi_file_root.pos = 0;
1125 
1126  /* Install the simple file system protocol, block I/O
1127  * protocol, and disk I/O protocol. We don't have a block
1128  * device, but large parts of the EDK2 codebase make the
1129  * assumption that file systems are normally attached to block
1130  * devices, and so we create a dummy block device on the same
1131  * handle just to keep things looking normal.
1132  */
1133  if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1134  &handle,
1140  &efi_simple_file_system_protocol, NULL ) ) != 0 ) {
1141  rc = -EEFI ( efirc );
1142  DBGC ( handle, "Could not install simple file system "
1143  "protocols: %s\n", strerror ( rc ) );
1144  goto err_install;
1145  }
1146 
1147  /* The FAT filesystem driver has a bug: if a block device
1148  * contains no FAT filesystem but does have an
1149  * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL instance, the FAT driver
1150  * will assume that it must have previously installed the
1151  * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL. This causes the FAT
1152  * driver to claim control of our device, and to refuse to
1153  * stop driving it, which prevents us from later uninstalling
1154  * correctly.
1155  *
1156  * Work around this bug by opening the disk I/O protocol
1157  * ourselves, thereby preventing the FAT driver from opening
1158  * it.
1159  *
1160  * Note that the alternative approach of opening the block I/O
1161  * protocol (and thereby in theory preventing DiskIo from
1162  * attaching to the block I/O protocol) causes an endless loop
1163  * of calls to our DRIVER_STOP method when starting the EFI
1164  * shell. I have no idea why this is.
1165  */
1166  if ( ( efirc = bs->OpenProtocol ( handle, &efi_disk_io_protocol_guid,
1167  &diskio.interface, efi_image_handle,
1168  handle,
1169  EFI_OPEN_PROTOCOL_BY_DRIVER ) ) != 0){
1170  rc = -EEFI ( efirc );
1171  DBGC ( handle, "Could not open disk I/O protocol: %s\n",
1172  strerror ( rc ) );
1174  goto err_open;
1175  }
1176  assert ( diskio.diskio == &efi_disk_io_protocol );
1177 
1178  /* Claim Linux initrd fixed device path */
1179  if ( ( rc = efi_file_path_claim ( &efi_file_initrd ) ) != 0 )
1180  goto err_initrd_claim;
1181 
1182  /* Install Linux initrd fixed device path file if non-empty */
1183  for_each_image ( image ) {
1184  if ( image->flags & IMAGE_HIDDEN )
1185  continue;
1186  if ( ( rc = efi_file_path_install ( &efi_file_initrd ) ) != 0 )
1187  goto err_initrd_install;
1188  break;
1189  }
1190 
1191  return 0;
1192 
1194  err_initrd_install:
1195  err_initrd_claim:
1198  err_open:
1200  handle,
1207  err_install:
1208  return rc;
1209 }
unsigned int flags
Flags.
Definition: image.h:36
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
static int efi_file_path_install(struct efi_file_path *file)
Install fixed device path file.
Definition: efi_file.c:1036
static void efi_file_path_uninstall(struct efi_file_path *file)
Uninstall fixed device path file.
Definition: efi_file.c:1068
static EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol
Dummy EFI block I/O protocol.
Definition: efi_file.c:924
#define DBGC(...)
Definition: compiler.h:505
#define EFI_OPEN_PROTOCOL_BY_DRIVER
Definition: UefiSpec.h:1347
EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES InstallMultipleProtocolInterfaces
Definition: UefiSpec.h:1996
#define DBGC_EFI_OPENERS(...)
Definition: efi.h:321
An executable image.
Definition: image.h:24
EFI_CLOSE_PROTOCOL CloseProtocol
Definition: UefiSpec.h:1987
static EFI_DISK_IO_PROTOCOL efi_disk_io_protocol
Dummy EFI disk I/O protocol.
Definition: efi_file.c:956
EFI_GUID efi_disk_io_protocol_guid
Disk I/O protocol GUID.
Definition: efi_guid.c:163
size_t pos
Current file position.
Definition: efi_file.c:86
static struct efi_file_path efi_file_initrd
Magic initrd file.
Definition: efi_file.c:107
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
An object interface.
Definition: interface.h:124
EFI_GUID efi_simple_file_system_protocol_guid
Simple file system protocol GUID.
Definition: efi_guid.c:303
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
This protocol is used to abstract Block I/O interfaces.
Definition: DiskIo.h:100
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:172
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:34
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:73
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:1997
EFI_GUID efi_block_io_protocol_guid
Block I/O protocol GUID.
Definition: efi_guid.c:119
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab
EFI_OPEN_PROTOCOL OpenProtocol
Definition: UefiSpec.h:1986
static int efi_file_path_claim(struct efi_file_path *file)
Claim use of fixed device path.
Definition: efi_file.c:977
static EFI_SIMPLE_FILE_SYSTEM_PROTOCOL efi_simple_file_system_protocol
EFI simple file system protocol.
Definition: efi_file.c:870
uint16_t handle
Handle.
Definition: smbios.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseProtocol, DBGC, DBGC_EFI_OPENERS, EEFI, efi_block_io_protocol, efi_block_io_protocol_guid, efi_disk_io_protocol, efi_disk_io_protocol_guid, efi_file_initrd, efi_file_path_claim(), efi_file_path_install(), efi_file_path_uninstall(), efi_file_root, efi_image_handle, EFI_OPEN_PROTOCOL_BY_DRIVER, efi_simple_file_system_protocol, efi_simple_file_system_protocol_guid, efi_systab, image::flags, for_each_image, handle, IMAGE_HIDDEN, EFI_BOOT_SERVICES::InstallMultipleProtocolInterfaces, NULL, EFI_BOOT_SERVICES::OpenProtocol, efi_file::pos, rc, strerror(), and EFI_BOOT_SERVICES::UninstallMultipleProtocolInterfaces.

Referenced by efi_image_exec().

◆ efi_file_uninstall()

void efi_file_uninstall ( EFI_HANDLE  handle)

Uninstall EFI simple file system protocol.

Parameters
handleEFI handle

Definition at line 1216 of file efi_file.c.

1216  {
1218  EFI_STATUS efirc;
1219  int rc;
1220 
1221  /* Uninstall Linux initrd fixed device path file */
1223 
1224  /* Close our own disk I/O protocol */
1227 
1228  /* We must install the file system protocol first, since
1229  * otherwise the EDK2 code will attempt to helpfully uninstall
1230  * it when the block I/O protocol is uninstalled, leading to a
1231  * system lock-up.
1232  */
1233  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1234  handle,
1240  &efi_block_io_protocol, NULL ) ) != 0 ) {
1241  rc = -EEFI ( efirc );
1242  DBGC ( handle, "Could not uninstall simple file system "
1243  "protocols: %s\n", strerror ( rc ) );
1244  /* Oh dear */
1245  }
1246 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
static void efi_file_path_uninstall(struct efi_file_path *file)
Uninstall fixed device path file.
Definition: efi_file.c:1068
static EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol
Dummy EFI block I/O protocol.
Definition: efi_file.c:924
#define DBGC(...)
Definition: compiler.h:505
EFI_CLOSE_PROTOCOL CloseProtocol
Definition: UefiSpec.h:1987
static EFI_DISK_IO_PROTOCOL efi_disk_io_protocol
Dummy EFI disk I/O protocol.
Definition: efi_file.c:956
EFI_GUID efi_disk_io_protocol_guid
Disk I/O protocol GUID.
Definition: efi_guid.c:163
static struct efi_file_path efi_file_initrd
Magic initrd file.
Definition: efi_file.c:107
EFI_GUID efi_simple_file_system_protocol_guid
Simple file system protocol GUID.
Definition: efi_guid.c:303
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:34
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:1997
EFI_GUID efi_block_io_protocol_guid
Block I/O protocol GUID.
Definition: efi_guid.c:119
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab
static EFI_SIMPLE_FILE_SYSTEM_PROTOCOL efi_simple_file_system_protocol
EFI simple file system protocol.
Definition: efi_file.c:870
uint16_t handle
Handle.
Definition: smbios.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseProtocol, DBGC, EEFI, efi_block_io_protocol, efi_block_io_protocol_guid, efi_disk_io_protocol, efi_disk_io_protocol_guid, efi_file_initrd, efi_file_path_uninstall(), efi_image_handle, efi_simple_file_system_protocol, efi_simple_file_system_protocol_guid, efi_systab, handle, NULL, rc, strerror(), and EFI_BOOT_SERVICES::UninstallMultipleProtocolInterfaces.

Referenced by efi_image_exec().

Variable Documentation

◆ efi_file_root

static struct efi_file efi_file_root
static
Initial value:
= {
.refcnt = REF_INIT ( ref_no_free ),
.file = {
.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,
},
.load = {
.LoadFile = efi_file_load,
},
.image = NULL,
.name = "",
}
static EFI_STATUS EFIAPI efi_file_set_info(EFI_FILE_PROTOCOL *this, EFI_GUID *type, UINTN len __unused, VOID *data __unused)
Set file information.
Definition: efi_file.c:721
static EFI_STATUS EFIAPI efi_file_open(EFI_FILE_PROTOCOL *this, EFI_FILE_PROTOCOL **new, CHAR16 *wname, UINT64 mode, UINT64 attributes __unused)
Open file.
Definition: efi_file.c:371
static EFI_STATUS EFIAPI efi_file_get_info(EFI_FILE_PROTOCOL *this, EFI_GUID *type, UINTN *len, VOID *data)
Get file information.
Definition: efi_file.c:675
static EFI_STATUS EFIAPI efi_file_get_position(EFI_FILE_PROTOCOL *this, UINT64 *position)
Get file position.
Definition: efi_file.c:658
static EFI_STATUS EFIAPI efi_file_read(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data)
Read from file.
Definition: efi_file.c:572
static EFI_STATUS EFIAPI efi_file_load(EFI_LOAD_FILE2_PROTOCOL *this, EFI_DEVICE_PATH_PROTOCOL *path __unused, BOOLEAN boot __unused, UINTN *len, VOID *data)
Load file.
Definition: efi_file.c:757
static EFI_STATUS EFIAPI efi_file_set_position(EFI_FILE_PROTOCOL *this, UINT64 position)
Set file position.
Definition: efi_file.c:622
static EFI_STATUS EFIAPI efi_file_close(EFI_FILE_PROTOCOL *this)
Close file.
Definition: efi_file.c:437
static EFI_STATUS EFIAPI efi_file_flush(EFI_FILE_PROTOCOL *this)
Flush file modified data.
Definition: efi_file.c:739
static EFI_STATUS EFIAPI efi_file_write(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data __unused)
Write to file.
Definition: efi_file.c:605
#define EFI_FILE_PROTOCOL_REVISION
static EFI_STATUS EFIAPI efi_file_delete(EFI_FILE_PROTOCOL *this)
Close and delete file.
Definition: efi_file.c:453
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:77
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:101
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

Root directory.

Definition at line 106 of file efi_file.c.

Referenced by efi_block_io_flush_blocks(), efi_block_io_read_blocks(), efi_block_io_reset(), efi_block_io_write_blocks(), efi_disk_io_read_disk(), efi_disk_io_write_disk(), efi_file_info(), efi_file_install(), efi_file_name(), efi_file_open(), efi_file_open_image(), and efi_file_open_volume().

◆ efi_file_initrd

static struct efi_file_path efi_file_initrd
static
Initial value:
= {
.file = {
.refcnt = REF_INIT ( ref_no_free ),
.file = {
.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,
},
.load = {
.LoadFile = efi_file_load,
},
.image = NULL,
.name = "initrd.magic",
},
.path = &efi_file_initrd_path.vendor.Header,
}
static EFI_STATUS EFIAPI efi_file_set_info(EFI_FILE_PROTOCOL *this, EFI_GUID *type, UINTN len __unused, VOID *data __unused)
Set file information.
Definition: efi_file.c:721
static EFI_STATUS EFIAPI efi_file_open(EFI_FILE_PROTOCOL *this, EFI_FILE_PROTOCOL **new, CHAR16 *wname, UINT64 mode, UINT64 attributes __unused)
Open file.
Definition: efi_file.c:371
static EFI_STATUS EFIAPI efi_file_get_info(EFI_FILE_PROTOCOL *this, EFI_GUID *type, UINTN *len, VOID *data)
Get file information.
Definition: efi_file.c:675
static EFI_STATUS EFIAPI efi_file_get_position(EFI_FILE_PROTOCOL *this, UINT64 *position)
Get file position.
Definition: efi_file.c:658
static EFI_STATUS EFIAPI efi_file_read(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data)
Read from file.
Definition: efi_file.c:572
static EFI_STATUS EFIAPI efi_file_load(EFI_LOAD_FILE2_PROTOCOL *this, EFI_DEVICE_PATH_PROTOCOL *path __unused, BOOLEAN boot __unused, UINTN *len, VOID *data)
Load file.
Definition: efi_file.c:757
static EFI_STATUS EFIAPI efi_file_set_position(EFI_FILE_PROTOCOL *this, UINT64 position)
Set file position.
Definition: efi_file.c:622
static EFI_STATUS EFIAPI efi_file_close(EFI_FILE_PROTOCOL *this)
Close file.
Definition: efi_file.c:437
static EFI_STATUS EFIAPI efi_file_flush(EFI_FILE_PROTOCOL *this)
Flush file modified data.
Definition: efi_file.c:739
static EFI_STATUS EFIAPI efi_file_write(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data __unused)
Write to file.
Definition: efi_file.c:605
#define EFI_FILE_PROTOCOL_REVISION
static EFI_STATUS EFIAPI efi_file_delete(EFI_FILE_PROTOCOL *this)
Close and delete file.
Definition: efi_file.c:453
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:77
static size_t efi_file_read_initrd(struct efi_file_reader *reader)
Read from magic initrd file.
Definition: efi_file.c:239
static struct @428 efi_file_initrd_path
Linux initrd fixed device path.
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:101
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

Magic initrd file.

Definition at line 107 of file efi_file.c.

Referenced by efi_file_install(), efi_file_open(), and efi_file_uninstall().

◆ vendor

Definition at line 809 of file efi_file.c.

◆ end

Definition at line 810 of file efi_file.c.

Referenced by efi_file_path_claim().

◆ efi_file_initrd_path

struct { ... } efi_file_initrd_path
Initial value:
= {
.vendor = {
.Header = {
.SubType = MEDIA_VENDOR_DP,
.Length[0] = sizeof ( efi_file_initrd_path.vendor ),
},
},
.end = {
.Length[0] = sizeof ( efi_file_initrd_path.end ),
},
}
#define END_DEVICE_PATH_TYPE
Definition: DevicePath.h:1371
#define MEDIA_VENDOR_DP
Media vendor device path subtype.
Definition: DevicePath.h:1075
#define LINUX_INITRD_VENDOR_GUID
Linux initrd fixed device path vendor GUID.
Definition: efi_file.c:57
#define MEDIA_DEVICE_PATH
Definition: DevicePath.h:991
#define END_ENTIRE_DEVICE_PATH_SUBTYPE
Definition: DevicePath.h:1372
static struct @428 efi_file_initrd_path
Linux initrd fixed device path.

Linux initrd fixed device path.

◆ efi_simple_file_system_protocol

EFI_SIMPLE_FILE_SYSTEM_PROTOCOL efi_simple_file_system_protocol
static
Initial value:
= {
.OpenVolume = efi_file_open_volume,
}
static EFI_STATUS EFIAPI efi_file_open_volume(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *filesystem __unused, EFI_FILE_PROTOCOL **file)
Open root directory.
Definition: efi_file.c:862
#define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION

EFI simple file system protocol.

Definition at line 870 of file efi_file.c.

Referenced by efi_file_install(), and efi_file_uninstall().

◆ efi_block_io_media

EFI_BLOCK_IO_MEDIA efi_block_io_media
static
Initial value:
= {
.MediaId = EFI_MEDIA_ID_MAGIC,
.MediaPresent = TRUE,
.ReadOnly = TRUE,
.BlockSize = 1,
}
#define TRUE
Definition: tlan.h:46
#define EFI_MEDIA_ID_MAGIC
EFI media ID.
Definition: efi_file.c:54

Dummy block I/O media.

Definition at line 916 of file efi_file.c.

◆ efi_block_io_protocol

EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol
static
Initial value:
= {
.Media = &efi_block_io_media,
.ReadBlocks = efi_block_io_read_blocks,
.WriteBlocks = efi_block_io_write_blocks,
.FlushBlocks = efi_block_io_flush_blocks,
}
static EFI_STATUS EFIAPI efi_block_io_read_blocks(EFI_BLOCK_IO_PROTOCOL *this __unused, UINT32 MediaId, EFI_LBA lba, UINTN len, VOID *data)
Dummy block I/O read.
Definition: efi_file.c:886
static EFI_STATUS EFIAPI efi_block_io_write_blocks(EFI_BLOCK_IO_PROTOCOL *this __unused, UINT32 MediaId, EFI_LBA lba, UINTN len, VOID *data)
Dummy block I/O write.
Definition: efi_file.c:897
static EFI_STATUS EFIAPI efi_block_io_reset(EFI_BLOCK_IO_PROTOCOL *this __unused, BOOLEAN extended)
Dummy block I/O reset.
Definition: efi_file.c:877
static EFI_STATUS EFIAPI efi_block_io_flush_blocks(EFI_BLOCK_IO_PROTOCOL *this __unused)
Dummy block I/O flush.
Definition: efi_file.c:909
#define EFI_BLOCK_IO_PROTOCOL_REVISION
Definition: BlockIo.h:204
static EFI_BLOCK_IO_MEDIA efi_block_io_media
Dummy block I/O media.
Definition: efi_file.c:916

Dummy EFI block I/O protocol.

Definition at line 924 of file efi_file.c.

Referenced by efi_file_install(), and efi_file_uninstall().

◆ efi_disk_io_protocol

EFI_DISK_IO_PROTOCOL efi_disk_io_protocol
static
Initial value:
= {
.ReadDisk = efi_disk_io_read_disk,
.WriteDisk = efi_disk_io_write_disk,
}
static EFI_STATUS EFIAPI efi_disk_io_read_disk(EFI_DISK_IO_PROTOCOL *this __unused, UINT32 MediaId, UINT64 offset, UINTN len, VOID *data)
Dummy disk I/O read.
Definition: efi_file.c:935
static EFI_STATUS EFIAPI efi_disk_io_write_disk(EFI_DISK_IO_PROTOCOL *this __unused, UINT32 MediaId, UINT64 offset, UINTN len, VOID *data)
Dummy disk I/O write.
Definition: efi_file.c:946
#define EFI_DISK_IO_PROTOCOL_REVISION
Definition: DiskIo.h:90

Dummy EFI disk I/O protocol.

Definition at line 956 of file efi_file.c.

Referenced by efi_file_install(), and efi_file_uninstall().