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/initrd.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, const void *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 55 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 58 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 115 of file efi_file.c.

115  {
116  struct efi_file *file =
117  container_of ( refcnt, struct efi_file, refcnt );
118 
119  image_put ( file->image );
120  free ( file );
121 }
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
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:249
An EFI file.
Definition: efi_file.c:75

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 129 of file efi_file.c.

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

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 140 of file efi_file.c.

140  {
141  struct image *image;
142 
143  /* Find image */
144  for_each_image ( image ) {
145  if ( strcasecmp ( image->name, name ) == 0 )
146  return image;
147  }
148 
149  return NULL;
150 }
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:23
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:190
char * name
Name.
Definition: image.h:37
#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 158 of file efi_file.c.

158  {
159  struct efi_file_reader reader;
160 
161  /* If this is the root directory, then treat as length zero */
162  if ( ! file->read )
163  return 0;
164 
165  /* Initialise reader */
166  reader.file = file;
167  reader.pos = 0;
168  reader.data = NULL;
169  reader.len = 0;
170 
171  /* Perform dummy read to determine file length */
172  file->read ( &reader );
173 
174  return reader.pos;
175 }
An EFI virtual file reader.
Definition: efi_file.c:63
size_t pos
Current file position.
Definition: efi_file.c:87
struct efi_file * file
EFI file.
Definition: efi_file.c:65
size_t(* read)(struct efi_file_reader *reader)
Read from file.
Definition: efi_file.c:94
#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,
const void *  data,
size_t  len 
)
static

Read chunk of EFI file.

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

Definition at line 185 of file efi_file.c.

186  {
187  struct efi_file *file = reader->file;
188  size_t offset;
189 
190  /* Calculate offset into input data */
191  offset = ( file->pos - reader->pos );
192 
193  /* Consume input data range */
194  reader->pos += len;
195 
196  /* Calculate output length */
197  if ( offset < len ) {
198  len -= offset;
199  } else {
200  len = 0;
201  }
202  if ( len > reader->len )
203  len = reader->len;
204 
205  /* Copy or zero output data */
206  if ( data ) {
207  memcpy ( reader->data, ( data + offset ), len );
208  } else {
209  memset ( reader->data, 0, len );
210  }
211 
212  /* Consume output buffer */
213  file->pos += len;
214  reader->data += len;
215  reader->len -= len;
216 
217  return len;
218 }
void * data
Output data buffer.
Definition: efi_file.c:69
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
struct efi_file * file
EFI file.
Definition: efi_file.c:65
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t len
Length of output data buffer.
Definition: efi_file.c:71
ring len
Length.
Definition: dwmac.h:231
size_t pos
Position within virtual file.
Definition: efi_file.c:67
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
An EFI file.
Definition: efi_file.c:75
void * memset(void *dest, int character, size_t len) __nonnull

References data, efi_file_reader::data, efi_file_reader::file, efi_file::file, efi_file_reader::len, len, memcpy(), 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 226 of file efi_file.c.

226  {
227  struct efi_file *file = reader->file;
228  struct image *image = file->image;
229 
230  /* Read from file */
231  return efi_file_read_chunk ( reader, image->data, image->len );
232 }
const void * data
Read-only data.
Definition: image.h:50
An executable image.
Definition: image.h:23
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
struct efi_file * file
EFI file.
Definition: efi_file.c:65
size_t len
Length of raw file image.
Definition: image.h:55
struct image * image
Image (if any)
Definition: efi_file.c:83
static size_t efi_file_read_chunk(struct efi_file_reader *reader, const void *data, size_t len)
Read chunk of EFI file.
Definition: efi_file.c:185
An EFI file.
Definition: efi_file.c:75

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 240 of file efi_file.c.

240  {
241  struct efi_file *file = reader->file;
242  struct cpio_header cpio;
243  struct image *image;
244  const char *name;
245  size_t pad_len;
246  size_t cpio_len;
247  size_t name_len;
248  size_t len;
249  unsigned int i;
250 
251  /* Read from file */
252  len = 0;
253  for_each_image ( image ) {
254 
255  /* Skip hidden images */
256  if ( image->flags & IMAGE_HIDDEN )
257  continue;
258 
259  /* Pad to alignment boundary */
260  pad_len = ( ( -reader->pos ) & ( INITRD_ALIGN - 1 ) );
261  if ( pad_len ) {
262  DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) pad\n",
263  efi_file_name ( file ), reader->pos,
264  ( reader->pos + pad_len ) );
265  }
266  len += efi_file_read_chunk ( reader, NULL, pad_len );
267 
268  /* Read CPIO header(s), if applicable */
269  name = cpio_name ( image );
270  for ( i = 0 ; ( cpio_len = cpio_header ( image, i, &cpio ) );
271  i++ ) {
272  name_len = ( cpio_len - sizeof ( cpio ) );
273  pad_len = cpio_pad_len ( cpio_len );
274  DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) %s header\n",
275  efi_file_name ( file ), reader->pos,
276  ( reader->pos + cpio_len + pad_len ),
277  image->name );
278  len += efi_file_read_chunk ( reader, &cpio,
279  sizeof ( cpio ) );
280  len += efi_file_read_chunk ( reader, name, name_len );
281  len += efi_file_read_chunk ( reader, NULL, 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:39
const char * name
Definition: ath9k_hw.c:1984
A CPIO archive header.
Definition: cpio.h:20
const void * data
Read-only data.
Definition: image.h:50
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
static size_t cpio_pad_len(size_t len)
Get CPIO header zero-padding length.
Definition: cpio.h:81
#define INITRD_ALIGN
Initial ramdisk chunk alignment.
Definition: initrd.h:16
struct efi_file * file
EFI file.
Definition: efi_file.c:65
size_t cpio_header(struct image *image, unsigned int index, struct cpio_header *cpio)
Construct CPIO header for image, if applicable.
Definition: cpio.c:155
ring len
Length.
Definition: dwmac.h:231
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:190
size_t len
Length of raw file image.
Definition: image.h:55
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:85
size_t pos
Position within virtual file.
Definition: efi_file.c:67
long pad_len
Definition: bigint.h:30
static size_t efi_file_read_chunk(struct efi_file_reader *reader, const void *data, size_t len)
Read chunk of EFI file.
Definition: efi_file.c:185
char * name
Name.
Definition: image.h:37
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
An EFI file.
Definition: efi_file.c:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition: cpio.h:70

References cpio_header(), cpio_name(), cpio_pad_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, image::len, len, image::name, name, NULL, pad_len, and efi_file_reader::pos.

◆ 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:77
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#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:129

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:23
static size_t efi_file_read_image(struct efi_file_reader *reader)
Read from image-backed file.
Definition: efi_file.c:226
struct image * image
Image (if any)
Definition: efi_file.c:83
size_t(* read)(struct efi_file_reader *reader)
Read from file.
Definition: efi_file.c:94
char * name
Name.
Definition: image.h:37
const char * name
Filename.
Definition: efi_file.c:85

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:115
#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:239
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#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:107
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
EFI_LOAD_FILE2_PROTOCOL load
EFI load file protocol.
Definition: efi_file.c:81
An EFI file.
Definition: efi_file.c:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129
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  /* Strip redundant path separator characters */
390  while ( ( *name == '\\' ) || ( *name == '.' ) )
391  name++;
392 
393  /* Allow root directory itself to be opened */
394  if ( ! *name )
395  return efi_file_open_fixed ( &efi_file_root, wname, new );
396 
397  /* Fail unless opening from the root */
398  if ( file != &efi_file_root ) {
399  DBGC ( file, "EFIFILE %s is not a directory\n",
400  efi_file_name ( file ) );
401  return EFI_NOT_FOUND;
402  }
403 
404  /* Fail unless opening read-only */
405  if ( mode != EFI_FILE_MODE_READ ) {
406  DBGC ( file, "EFIFILE %s cannot be opened in mode %#08llx\n",
407  name, mode );
408  return EFI_WRITE_PROTECTED;
409  }
410 
411  /* Allow registered images to be opened */
412  if ( ( image = efi_file_find ( name ) ) != NULL )
413  return efi_file_open_image ( image, wname, new );
414 
415  /* Allow magic initrd to be opened */
416  if ( strcasecmp ( name, efi_file_initrd.file.name ) == 0 ) {
417  return efi_file_open_fixed ( &efi_file_initrd.file, wname,
418  new );
419  }
420 
421  /* Allow currently selected image to be opened as "grub*.efi",
422  * to work around buggy versions of the UEFI shim.
423  */
424  if ( ( strncasecmp ( name, "grub", 4 ) == 0 ) &&
425  ( ( sep = strrchr ( name, '.' ) ) != NULL ) &&
426  ( strcasecmp ( sep, ".efi" ) == 0 ) &&
427  ( ( image = find_image_tag ( &selected_image ) ) != NULL ) ) {
428  return efi_file_open_image ( image, wname, new );
429  }
430 
431  DBGC ( file, "EFIFILE %ls does not exist\n", wname );
432  return EFI_NOT_FOUND;
433 }
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
uint16_t mode
Acceleration mode.
Definition: ena.h:26
#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:23
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
size_t wcslen(const wchar_t *string)
Calculate length of wide-character string.
Definition: wchar.c:56
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition: image.c:392
static struct efi_file_path efi_file_initrd
Magic initrd file.
Definition: efi_file.c:108
#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:107
#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:140
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
An EFI file.
Definition: efi_file.c:75
const char * name
Filename.
Definition: efi_file.c:85
struct efi_file file
EFI file.
Definition: efi_file.c:100
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

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(), mode, 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 441 of file efi_file.c.

441  {
442  struct efi_file *file = container_of ( this, struct efi_file, file );
443 
444  /* Close file */
445  DBGC ( file, "EFIFILE %s closed\n", efi_file_name ( file ) );
446  ref_put ( &file->refcnt );
447 
448  return 0;
449 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An EFI file.
Definition: efi_file.c:75
#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:129

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 457 of file efi_file.c.

457  {
458  struct efi_file *file = container_of ( this, struct efi_file, file );
459 
460  DBGC ( file, "EFIFILE %s cannot be deleted\n", efi_file_name ( file ) );
461 
462  /* Close file */
463  efi_file_close ( this );
464 
465  /* Warn of failure to delete */
467 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#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:441
#define EFI_WARN_DELETE_FAILURE
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:150
An EFI file.
Definition: efi_file.c:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

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 479 of file efi_file.c.

480  {
481  size_t name_len;
482 
483  /* Calculate structure length */
484  name_len = strlen ( name );
485  *base = ( base_len + ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
486  if ( *len < *base ) {
487  *len = *base;
488  return EFI_BUFFER_TOO_SMALL;
489  }
490 
491  /* Copy data to buffer */
492  *len = *base;
493  memcpy ( data, base, base_len );
494  efi_snprintf ( ( data + base_len ), ( name_len + 1 /* NUL */ ),
495  "%s", name );
496 
497  return 0;
498 }
uint32_t base
Base.
Definition: librm.h:138
const char * name
Definition: ath9k_hw.c:1984
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:119
void * memcpy(void *dest, const void *src, size_t len) __nonnull
ring len
Length.
Definition: dwmac.h:231
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
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 508 of file efi_file.c.

509  {
511  size_t file_len;
512 
513  /* Get file length */
514  file_len = efi_file_len ( file );
515 
516  /* Populate file information */
517  memset ( &info, 0, sizeof ( info ) );
518  info.FileSize = file_len;
519  info.PhysicalSize = file_len;
520  info.Attribute = EFI_FILE_READ_ONLY;
521  if ( file == &efi_file_root )
522  info.Attribute |= EFI_FILE_DIRECTORY;
523 
525  file->name, len, data );
526 }
u32 info
Definition: ar9003_mac.h:67
#define EFI_FILE_READ_ONLY
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#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:158
ring len
Length.
Definition: dwmac.h:231
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:107
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:479
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 536 of file efi_file.c.

537  {
538  EFI_STATUS efirc;
539  struct efi_file entry;
540  struct image *image;
541  unsigned int index;
542 
543  /* Construct directory entries for image-backed files */
544  index = file->pos;
545  for_each_image ( image ) {
546 
547  /* Skip hidden images */
548  if ( image->flags & IMAGE_HIDDEN )
549  continue;
550 
551  /* Skip preceding images */
552  if ( index-- )
553  continue;
554 
555  /* Construct directory entry */
556  efi_file_image ( &entry, image );
557  efirc = efi_file_info ( &entry, len, data );
558  if ( efirc == 0 )
559  file->pos++;
560  return efirc;
561  }
562 
563  /* No more entries */
564  *len = 0;
565  return 0;
566 }
unsigned int flags
Flags.
Definition: image.h:39
long index
Definition: bigint.h:62
An executable image.
Definition: image.h:23
static EFI_STATUS efi_file_info(struct efi_file *file, UINTN *len, VOID *data)
Return file information structure.
Definition: efi_file.c:508
size_t pos
Current file position.
Definition: efi_file.c:87
ring len
Length.
Definition: dwmac.h:231
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:190
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:85
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:75
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(), 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 576 of file efi_file.c.

577  {
578  struct efi_file *file = container_of ( this, struct efi_file, file );
579  struct efi_file_reader reader;
580  size_t pos = file->pos;
581 
582  /* If this is the root directory, then construct a directory entry */
583  if ( ! file->read )
584  return efi_file_read_dir ( file, len, data );
585 
586  /* Initialise reader */
587  reader.file = file;
588  reader.pos = 0;
589  reader.data = data;
590  reader.len = *len;
591 
592  /* Read from the file */
593  DBGC ( file, "EFIFILE %s read [%#08zx,%#08zx)\n",
594  efi_file_name ( file ), pos, ( ( size_t ) ( pos + *len ) ) );
595  *len = file->read ( &reader );
596  assert ( ( pos + *len ) == file->pos );
597 
598  return 0;
599 }
An EFI virtual file reader.
Definition: efi_file.c:63
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
size_t pos
Current file position.
Definition: efi_file.c:87
struct efi_file * file
EFI file.
Definition: efi_file.c:65
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
ring len
Length.
Definition: dwmac.h:231
static EFI_STATUS efi_file_read_dir(struct efi_file *file, UINTN *len, VOID *data)
Read directory entry.
Definition: efi_file.c:536
size_t pos
Position within virtual file.
Definition: efi_file.c:67
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:94
An EFI file.
Definition: efi_file.c:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

References assert(), container_of, data, efi_file_reader::data, DBGC, efi_file_name(), efi_file_read_dir(), efi_file_reader::file, efi_file::file, efi_file_reader::len, 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 609 of file efi_file.c.

610  {
611  struct efi_file *file = container_of ( this, struct efi_file, file );
612 
613  DBGC ( file, "EFIFILE %s cannot write [%#08zx, %#08zx)\n",
614  efi_file_name ( file ), file->pos,
615  ( ( size_t ) ( file->pos + *len ) ) );
616  return EFI_WRITE_PROTECTED;
617 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
ring len
Length.
Definition: dwmac.h:231
#define EFI_WRITE_PROTECTED
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:122
An EFI file.
Definition: efi_file.c:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

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 626 of file efi_file.c.

627  {
628  struct efi_file *file = container_of ( this, struct efi_file, file );
629  size_t len;
630 
631  /* Get file length */
632  len = efi_file_len ( file );
633 
634  /* Check for the magic end-of-file value */
635  if ( position == 0xffffffffffffffffULL )
636  position = len;
637 
638  /* Fail if we attempt to seek past the end of the file (since
639  * we do not support writes).
640  */
641  if ( position > len ) {
642  DBGC ( file, "EFIFILE %s cannot seek to %#08llx of %#08zx\n",
643  efi_file_name ( file ), position, len );
644  return EFI_UNSUPPORTED;
645  }
646 
647  /* Set position */
648  file->pos = position;
649  DBGC ( file, "EFIFILE %s position set to %#08zx\n",
650  efi_file_name ( file ), file->pos );
651 
652  return 0;
653 }
#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:79
static size_t efi_file_len(struct efi_file *file)
Get length of EFI file.
Definition: efi_file.c:158
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
ring len
Length.
Definition: dwmac.h:231
An EFI file.
Definition: efi_file.c:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

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 662 of file efi_file.c.

663  {
664  struct efi_file *file = container_of ( this, struct efi_file, file );
665 
666  *position = file->pos;
667  return 0;
668 }
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An EFI file.
Definition: efi_file.c:75

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 679 of file efi_file.c.

681  {
682  struct efi_file *file = container_of ( this, struct efi_file, file );
683  EFI_FILE_SYSTEM_INFO fsinfo;
684  struct image *image;
685 
686  /* Determine information to return */
687  if ( memcmp ( type, &efi_file_info_id, sizeof ( *type ) ) == 0 ) {
688 
689  /* Get file information */
690  DBGC ( file, "EFIFILE %s get file information\n",
691  efi_file_name ( file ) );
692  return efi_file_info ( file, len, data );
693 
694  } else if ( memcmp ( type, &efi_file_system_info_id,
695  sizeof ( *type ) ) == 0 ) {
696 
697  /* Get file system information */
698  DBGC ( file, "EFIFILE %s get file system information\n",
699  efi_file_name ( file ) );
700  memset ( &fsinfo, 0, sizeof ( fsinfo ) );
701  fsinfo.ReadOnly = 1;
703  fsinfo.VolumeSize += image->len;
704  return efi_file_varlen ( &fsinfo.Size,
706  len, data );
707  } else {
708 
709  DBGC ( file, "EFIFILE %s cannot get information of type %s\n",
710  efi_file_name ( file ), efi_guid_ntoa ( type ) );
711  return EFI_UNSUPPORTED;
712  }
713 }
#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:465
uint32_t type
Operating system type.
Definition: ena.h:12
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
static EFI_STATUS efi_file_info(struct efi_file *file, UINTN *len, VOID *data)
Return file information structure.
Definition: efi_file.c:508
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
ring len
Length.
Definition: dwmac.h:231
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:190
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:479
size_t len
Length of raw file image.
Definition: image.h:55
UINT64 VolumeSize
The number of bytes managed by the file system.
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_guid.c:725
EFI_GUID efi_file_info_id
File information GUID.
Definition: efi_guid.c:462
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:75
#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:129
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, image::len, 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 725 of file efi_file.c.

726  {
727  struct efi_file *file = container_of ( this, struct efi_file, file );
728 
729  DBGC ( file, "EFIFILE %s cannot set information of type %s\n",
731  return EFI_WRITE_PROTECTED;
732 }
uint32_t type
Operating system type.
Definition: ena.h:12
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#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_guid.c:725
An EFI file.
Definition: efi_file.c:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

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 743 of file efi_file.c.

743  {
744  struct efi_file *file = container_of ( this, struct efi_file, file );
745 
746  DBGC ( file, "EFIFILE %s flushed\n", efi_file_name ( file ) );
747  return 0;
748 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An EFI file.
Definition: efi_file.c:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

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 761 of file efi_file.c.

763  {
764  struct efi_file *file = container_of ( this, struct efi_file, load );
765  size_t max_len;
766  size_t file_len;
767  EFI_STATUS efirc;
768 
769  /* Calculate maximum length */
770  max_len = ( data ? *len : 0 );
771  DBGC ( file, "EFIFILE %s load at %p+%#zx\n",
772  efi_file_name ( file ), data, max_len );
773 
774  /* Check buffer size */
775  file_len = efi_file_len ( file );
776  if ( file_len > max_len ) {
777  *len = file_len;
778  return EFI_BUFFER_TOO_SMALL;
779  }
780 
781  /* Read from file */
782  if ( ( efirc = efi_file_read ( &file->file, len, data ) ) != 0 )
783  return efirc;
784 
785  return 0;
786 }
#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:576
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:119
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
static size_t efi_file_len(struct efi_file *file)
Get length of EFI file.
Definition: efi_file.c:158
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
ring len
Length.
Definition: dwmac.h:231
EFI_LOAD_FILE2_PROTOCOL load
EFI load file protocol.
Definition: efi_file.c:81
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:75
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

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

◆ 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 866 of file efi_file.c.

867  {
868 
869  DBGC ( &efi_file_root, "EFIFILE open volume\n" );
870  return efi_file_open_fixed ( &efi_file_root, L"<volume>", file );
871 }
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
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:107

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 881 of file efi_file.c.

881  {
882 
883  DBGC ( &efi_file_root, "EFIFILE block %sreset\n",
884  ( extended ? "extended " : "" ) );
885  return 0;
886 }
#define DBGC(...)
Definition: compiler.h:505
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:107

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 890 of file efi_file.c.

891  {
892 
893  DBGC ( &efi_file_root, "EFIFILE block read ID %#08x LBA %#08llx -> "
894  "%p+%zx\n", MediaId, ( ( unsigned long long ) lba ),
895  data, ( ( size_t ) len ) );
896  return EFI_NO_MEDIA;
897 }
#define DBGC(...)
Definition: compiler.h:505
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
ring len
Length.
Definition: dwmac.h:231
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:107
uint64_t lba
Starting block number.
Definition: int13.h:22
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 901 of file efi_file.c.

903  {
904 
905  DBGC ( &efi_file_root, "EFIFILE block write ID %#08x LBA %#08llx <- "
906  "%p+%zx\n", MediaId, ( ( unsigned long long ) lba ),
907  data, ( ( size_t ) len ) );
908  return EFI_NO_MEDIA;
909 }
#define DBGC(...)
Definition: compiler.h:505
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
ring len
Length.
Definition: dwmac.h:231
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:107
uint64_t lba
Starting block number.
Definition: int13.h:22
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 913 of file efi_file.c.

913  {
914 
915  DBGC ( &efi_file_root, "EFIFILE block flush\n" );
916  return 0;
917 }
#define DBGC(...)
Definition: compiler.h:505
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:107

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 939 of file efi_file.c.

940  {
941 
942  DBGC ( &efi_file_root, "EFIFILE disk read ID %#08x offset %#08llx -> "
943  "%p+%zx\n", MediaId, ( ( unsigned long long ) offset ),
944  data, ( ( size_t ) len ) );
945  return EFI_NO_MEDIA;
946 }
#define DBGC(...)
Definition: compiler.h:505
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
ring len
Length.
Definition: dwmac.h:231
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:107
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

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 950 of file efi_file.c.

951  {
952 
953  DBGC ( &efi_file_root, "EFIFILE disk write ID %#08x offset %#08llx <- "
954  "%p+%zx\n", MediaId, ( ( unsigned long long ) offset ),
955  data, ( ( size_t ) len ) );
956  return EFI_NO_MEDIA;
957 }
#define DBGC(...)
Definition: compiler.h:505
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
ring len
Length.
Definition: dwmac.h:231
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:107
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint16_t offset
Offset to command line.
Definition: bzimage.h:8

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 981 of file efi_file.c.

981  {
986  EFI_STATUS efirc;
987  int rc;
988 
989  /* Sanity check */
990  assert ( file->handle == NULL );
991 
992  /* Locate handle with this device path, if any */
993  end = file->path;
994  if ( ( ( efirc = bs->LocateDevicePath ( &efi_device_path_protocol_guid,
995  &end, &handle ) ) != 0 ) ||
996  ( end->Type != END_DEVICE_PATH_TYPE ) ) {
997  return 0;
998  }
999 
1000  /* Locate device path protocol on this handle */
1002  &old ) != 0 ) ) {
1003  DBGC ( file, "EFIFILE %s could not locate %s: %s\n",
1004  efi_file_name ( &file->file ),
1005  efi_devpath_text ( file->path ), strerror ( rc ) );
1006  return rc;
1007  }
1008 
1009  /* Uninstall device path protocol, leaving other protocols untouched */
1010  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1011  handle,
1013  NULL ) ) != 0 ) {
1014  rc = -EEFI ( efirc );
1015  DBGC ( file, "EFIFILE %s could not claim %s: %s\n",
1016  efi_file_name ( &file->file ),
1017  efi_devpath_text ( file->path ), strerror ( rc ) );
1018  return rc;
1019  }
1020 
1021  DBGC ( file, "EFIFILE %s claimed %s",
1022  efi_file_name ( &file->file ), efi_devpath_text ( file->path ) );
1023  DBGC ( file, " from %s\n", efi_handle_name ( handle ) );
1024  return 0;
1025 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
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:174
#define END_DEVICE_PATH_TYPE
Definition: DevicePath.h:1393
#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:79
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:247
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:652
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1930
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:2010
#define efi_open(handle, protocol, interface)
Open protocol for ephemeral use.
Definition: efi.h:443
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:168
EFI_DEVICE_PATH_PROTOCOL end
Definition: efi_file.c:814
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:61
EFI_LOCATE_DEVICE_PATH LocateDevicePath
Definition: UefiSpec.h:1971
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:129

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

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 1038 of file efi_file.c.

1038  {
1040  EFI_STATUS efirc;
1041  int rc;
1042 
1043  /* Sanity check */
1044  assert ( file->handle == NULL );
1045 
1046  /* Create a new handle with this device path */
1047  if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1048  &file->handle,
1050  &efi_load_file2_protocol_guid, &file->file.load,
1051  NULL ) ) != 0 ) {
1052  rc = -EEFI ( efirc );
1053  DBGC ( file, "EFIFILE %s could not install %s: %s\n",
1054  efi_file_name ( &file->file ),
1055  efi_devpath_text ( file->path ), strerror ( rc ) );
1056  return rc;
1057  }
1058 
1059  DBGC ( file, "EFIFILE %s installed as %s\n",
1060  efi_file_name ( &file->file ), efi_devpath_text ( file->path ) );
1061  return 0;
1062 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
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:174
#define DBGC(...)
Definition: compiler.h:505
EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES InstallMultipleProtocolInterfaces
Definition: UefiSpec.h:2009
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
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:247
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:268
EFI Boot Services Table.
Definition: UefiSpec.h:1930
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:168
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:129

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 1070 of file efi_file.c.

1070  {
1072  EFI_STATUS efirc;
1073  int rc;
1074 
1075  /* Do nothing if file is already uninstalled */
1076  if ( ! file->handle )
1077  return;
1078 
1079  /* Uninstall protocols. Do this via two separate calls, in
1080  * case another executable has already uninstalled the device
1081  * path protocol from our handle.
1082  */
1083  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1084  file->handle,
1086  NULL ) ) != 0 ) {
1087  rc = -EEFI ( efirc );
1088  DBGC ( file, "EFIFILE %s could not uninstall %s: %s\n",
1089  efi_file_name ( &file->file ),
1090  efi_devpath_text ( file->path ), strerror ( rc ) );
1091  /* Continue uninstalling */
1092  }
1093  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1094  file->handle,
1095  &efi_load_file2_protocol_guid, &file->file.load,
1096  NULL ) ) != 0 ) {
1097  rc = -EEFI ( efirc );
1098  DBGC ( file, "EFIFILE %s could not uninstall %s: %s\n",
1099  efi_file_name ( &file->file ),
1101  strerror ( rc ) );
1102  /* Continue uninstalling */
1103  }
1104 
1105  /* Mark handle as uninstalled */
1106  file->handle = NULL;
1107 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
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:174
#define DBGC(...)
Definition: compiler.h:505
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:79
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:247
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:268
EFI Boot Services Table.
Definition: UefiSpec.h:1930
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:2010
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:168
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_guid.c:725
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:129

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 1115 of file efi_file.c.

1115  {
1117  EFI_DISK_IO_PROTOCOL *diskio;
1118  struct image *image;
1119  EFI_STATUS efirc;
1120  int rc;
1121 
1122  /* Reset root directory state */
1123  efi_file_root.pos = 0;
1124 
1125  /* Install the simple file system protocol, block I/O
1126  * protocol, and disk I/O protocol. We don't have a block
1127  * device, but large parts of the EDK2 codebase make the
1128  * assumption that file systems are normally attached to block
1129  * devices, and so we create a dummy block device on the same
1130  * handle just to keep things looking normal.
1131  */
1132  if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1133  &handle,
1139  &efi_simple_file_system_protocol, NULL ) ) != 0 ) {
1140  rc = -EEFI ( efirc );
1141  DBGC ( handle, "Could not install simple file system "
1142  "protocols: %s\n", strerror ( rc ) );
1143  goto err_install;
1144  }
1145 
1146  /* The FAT filesystem driver has a bug: if a block device
1147  * contains no FAT filesystem but does have an
1148  * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL instance, the FAT driver
1149  * will assume that it must have previously installed the
1150  * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL. This causes the FAT
1151  * driver to claim control of our device, and to refuse to
1152  * stop driving it, which prevents us from later uninstalling
1153  * correctly.
1154  *
1155  * Work around this bug by opening the disk I/O protocol
1156  * ourselves, thereby preventing the FAT driver from opening
1157  * it.
1158  *
1159  * Note that the alternative approach of opening the block I/O
1160  * protocol (and thereby in theory preventing DiskIo from
1161  * attaching to the block I/O protocol) causes an endless loop
1162  * of calls to our DRIVER_STOP method when starting the EFI
1163  * shell. I have no idea why this is.
1164  */
1166  &diskio ) ) != 0 ) {
1167  DBGC ( handle, "Could not open disk I/O protocol: %s\n",
1168  strerror ( rc ) );
1170  goto err_open;
1171  }
1172  assert ( diskio == &efi_disk_io_protocol );
1173 
1174  /* Claim Linux initrd fixed device path */
1175  if ( ( rc = efi_file_path_claim ( &efi_file_initrd ) ) != 0 )
1176  goto err_initrd_claim;
1177 
1178  /* Install Linux initrd fixed device path file if non-empty */
1179  for_each_image ( image ) {
1180  if ( image->flags & IMAGE_HIDDEN )
1181  continue;
1182  if ( ( rc = efi_file_path_install ( &efi_file_initrd ) ) != 0 )
1183  goto err_initrd_install;
1184  break;
1185  }
1186 
1187  return 0;
1188 
1190  err_initrd_install:
1191  err_initrd_claim:
1193  err_open:
1195  handle,
1202  err_install:
1203  return rc;
1204 }
unsigned int flags
Flags.
Definition: image.h:39
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
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:174
static int efi_file_path_install(struct efi_file_path *file)
Install fixed device path file.
Definition: efi_file.c:1038
static void efi_file_path_uninstall(struct efi_file_path *file)
Uninstall fixed device path file.
Definition: efi_file.c:1070
static EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol
Dummy EFI block I/O protocol.
Definition: efi_file.c:928
#define DBGC(...)
Definition: compiler.h:505
EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES InstallMultipleProtocolInterfaces
Definition: UefiSpec.h:2009
#define DBGC_EFI_OPENERS(...)
Definition: efi.h:344
An executable image.
Definition: image.h:23
static EFI_DISK_IO_PROTOCOL efi_disk_io_protocol
Dummy EFI disk I/O protocol.
Definition: efi_file.c:960
EFI_GUID efi_disk_io_protocol_guid
Disk I/O protocol GUID.
Definition: efi_guid.c:188
size_t pos
Current file position.
Definition: efi_file.c:87
static struct efi_file_path efi_file_initrd
Magic initrd file.
Definition: efi_file.c:108
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void efi_close_by_driver(EFI_HANDLE handle, EFI_GUID *protocol)
Close protocol opened for persistent use by a driver.
Definition: efi_open.c:278
EFI_GUID efi_simple_file_system_protocol_guid
Simple file system protocol GUID.
Definition: efi_guid.c:336
#define efi_open_by_driver(handle, protocol, interface)
Open protocol for persistent use by a driver.
Definition: efi.h:473
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:107
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:190
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1930
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:85
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:2010
EFI_GUID efi_block_io_protocol_guid
Block I/O protocol GUID.
Definition: efi_guid.c:144
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab
static int efi_file_path_claim(struct efi_file_path *file)
Claim use of fixed device path.
Definition: efi_file.c:981
static EFI_SIMPLE_FILE_SYSTEM_PROTOCOL efi_simple_file_system_protocol
EFI simple file system protocol.
Definition: efi_file.c:874
uint16_t handle
Handle.
Definition: smbios.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), EFI_SYSTEM_TABLE::BootServices, DBGC, DBGC_EFI_OPENERS, EEFI, efi_block_io_protocol, efi_block_io_protocol_guid, efi_close_by_driver(), 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_open_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_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 1211 of file efi_file.c.

1211  {
1213  EFI_STATUS efirc;
1214  int rc;
1215 
1216  /* Uninstall Linux initrd fixed device path file */
1218 
1219  /* Close our own disk I/O protocol */
1221 
1222  /* We must install the file system protocol first, since
1223  * otherwise the EDK2 code will attempt to helpfully uninstall
1224  * it when the block I/O protocol is uninstalled, leading to a
1225  * system lock-up.
1226  */
1227  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1228  handle,
1234  &efi_block_io_protocol, NULL ) ) != 0 ) {
1235  rc = -EEFI ( efirc );
1236  DBGC ( handle, "Could not uninstall simple file system "
1237  "protocols: %s\n", strerror ( rc ) );
1238  /* Oh dear */
1239  }
1240 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
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:174
static void efi_file_path_uninstall(struct efi_file_path *file)
Uninstall fixed device path file.
Definition: efi_file.c:1070
static EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol
Dummy EFI block I/O protocol.
Definition: efi_file.c:928
#define DBGC(...)
Definition: compiler.h:505
static EFI_DISK_IO_PROTOCOL efi_disk_io_protocol
Dummy EFI disk I/O protocol.
Definition: efi_file.c:960
EFI_GUID efi_disk_io_protocol_guid
Disk I/O protocol GUID.
Definition: efi_guid.c:188
static struct efi_file_path efi_file_initrd
Magic initrd file.
Definition: efi_file.c:108
void efi_close_by_driver(EFI_HANDLE handle, EFI_GUID *protocol)
Close protocol opened for persistent use by a driver.
Definition: efi_open.c:278
EFI_GUID efi_simple_file_system_protocol_guid
Simple file system protocol GUID.
Definition: efi_guid.c:336
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1930
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:2010
EFI_GUID efi_block_io_protocol_guid
Block I/O protocol GUID.
Definition: efi_guid.c:144
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:874
uint16_t handle
Handle.
Definition: smbios.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References EFI_SYSTEM_TABLE::BootServices, DBGC, EEFI, efi_block_io_protocol, efi_block_io_protocol_guid, efi_close_by_driver(), efi_disk_io_protocol, efi_disk_io_protocol_guid, efi_file_initrd, efi_file_path_uninstall(), 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:725
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:679
static EFI_STATUS EFIAPI efi_file_get_position(EFI_FILE_PROTOCOL *this, UINT64 *position)
Get file position.
Definition: efi_file.c:662
static EFI_STATUS EFIAPI efi_file_read(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data)
Read from file.
Definition: efi_file.c:576
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:761
static EFI_STATUS EFIAPI efi_file_set_position(EFI_FILE_PROTOCOL *this, UINT64 position)
Set file position.
Definition: efi_file.c:626
static EFI_STATUS EFIAPI efi_file_close(EFI_FILE_PROTOCOL *this)
Close file.
Definition: efi_file.c:441
static EFI_STATUS EFIAPI efi_file_flush(EFI_FILE_PROTOCOL *this)
Flush file modified data.
Definition: efi_file.c:743
static EFI_STATUS EFIAPI efi_file_write(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data __unused)
Write to file.
Definition: efi_file.c:609
#define EFI_FILE_PROTOCOL_REVISION
static EFI_STATUS EFIAPI efi_file_delete(EFI_FILE_PROTOCOL *this)
Close and delete file.
Definition: efi_file.c:457
#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 107 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:725
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:679
static EFI_STATUS EFIAPI efi_file_get_position(EFI_FILE_PROTOCOL *this, UINT64 *position)
Get file position.
Definition: efi_file.c:662
static struct @437 efi_file_initrd_path
Linux initrd fixed device path.
static EFI_STATUS EFIAPI efi_file_read(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data)
Read from file.
Definition: efi_file.c:576
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:761
static EFI_STATUS EFIAPI efi_file_set_position(EFI_FILE_PROTOCOL *this, UINT64 position)
Set file position.
Definition: efi_file.c:626
static EFI_STATUS EFIAPI efi_file_close(EFI_FILE_PROTOCOL *this)
Close file.
Definition: efi_file.c:441
static EFI_STATUS EFIAPI efi_file_flush(EFI_FILE_PROTOCOL *this)
Flush file modified data.
Definition: efi_file.c:743
static EFI_STATUS EFIAPI efi_file_write(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data __unused)
Write to file.
Definition: efi_file.c:609
#define EFI_FILE_PROTOCOL_REVISION
static EFI_STATUS EFIAPI efi_file_delete(EFI_FILE_PROTOCOL *this)
Close and delete file.
Definition: efi_file.c:457
#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:240
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 108 of file efi_file.c.

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

◆ vendor

Definition at line 813 of file efi_file.c.

◆ end

Definition at line 814 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:1393
static struct @437 efi_file_initrd_path
Linux initrd fixed device path.
#define MEDIA_VENDOR_DP
Media vendor device path subtype.
Definition: DevicePath.h:1095
#define LINUX_INITRD_VENDOR_GUID
Linux initrd fixed device path vendor GUID.
Definition: efi_file.c:58
#define MEDIA_DEVICE_PATH
Definition: DevicePath.h:1011
#define END_ENTIRE_DEVICE_PATH_SUBTYPE
Definition: DevicePath.h:1394

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:866
#define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION

EFI simple file system protocol.

Definition at line 874 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:55

Dummy block I/O media.

Definition at line 920 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:890
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:901
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:881
static EFI_STATUS EFIAPI efi_block_io_flush_blocks(EFI_BLOCK_IO_PROTOCOL *this __unused)
Dummy block I/O flush.
Definition: efi_file.c:913
#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:920

Dummy EFI block I/O protocol.

Definition at line 928 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:939
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:950
#define EFI_DISK_IO_PROTOCOL_REVISION
Definition: DiskIo.h:90

Dummy EFI disk I/O protocol.

Definition at line 960 of file efi_file.c.

Referenced by efi_file_install(), and efi_file_uninstall().