iPXE
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.
#define LINUX_INITRD_VENDOR_GUID
 Linux initrd fixed device path vendor GUID.

Functions

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

Variables

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

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

59#define LINUX_INITRD_VENDOR_GUID \
60 { 0x5568e427, 0x68fc, 0x4f3d, \
61 { 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } }

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ efi_file_free()

void efi_file_free ( struct refcnt * refcnt)
static

Free EFI file.

Parameters
refcntReference count

Definition at line 116 of file efi_file.c.

116 {
117 struct efi_file *file =
118 container_of ( refcnt, struct efi_file, refcnt );
119
120 image_put ( file->image );
121 free ( file );
122}
static void image_put(struct image *image)
Decrement reference count on an image.
Definition image.h:250
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
An EFI file.
Definition efi_file.c:76
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition efi_file.c:80
A reference counter.
Definition refcnt.h:27

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

Referenced by efi_file_open_image().

◆ efi_file_name()

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

130 {
131
132 return ( file == &efi_file_root ? "<root>" : file->name );
133}
static struct efi_file efi_file_root
Root directory.
Definition efi_file.c:108
const char * name
Filename.
Definition efi_file.c:86

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()

struct image * efi_file_find ( const char * name)
static

Find EFI file image.

Parameters
nameFilename
Return values
imageImage, or NULL

Definition at line 141 of file efi_file.c.

141 {
142 struct image *image;
143
144 /* Find image */
146 if ( strcasecmp ( image->name, name ) == 0 )
147 return image;
148 }
149
150 return NULL;
151}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
const char * name
Definition ath9k_hw.c:1986
#define for_each_image(image)
Iterate over all registered images.
Definition image.h:191
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition string.c:209
An executable image.
Definition image.h:24
char * name
Name.
Definition image.h:38

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

Referenced by efi_file_open().

◆ efi_file_len()

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

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

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()

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

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

References data, efi_file_reader::data, efi_file::file, efi_file_reader::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()

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

227 {
228 struct efi_file *file = reader->file;
229 struct image *image = file->image;
230
231 /* Read from file */
232 return efi_file_read_chunk ( reader, image->data, image->len );
233}
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:186
struct image * image
Image (if any)
Definition efi_file.c:84
const void * data
Read-only data.
Definition image.h:51
size_t len
Length of raw file image.
Definition image.h:56

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

Referenced by efi_file_image().

◆ efi_file_read_initrd()

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

241 {
242 struct efi_file *file = reader->file;
243 struct cpio_header cpio;
244 struct image *image;
245 const char *name;
246 size_t pad_len;
247 size_t cpio_len;
248 size_t name_len;
249 size_t len;
250 unsigned int i;
251
252 /* Read from file */
253 len = 0;
255
256 /* Skip hidden images */
257 if ( image->flags & IMAGE_HIDDEN )
258 continue;
259
260 /* Pad to alignment boundary */
261 pad_len = ( ( -reader->pos ) & ( INITRD_ALIGN - 1 ) );
262 if ( pad_len ) {
263 DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) pad\n",
264 efi_file_name ( file ), reader->pos,
265 ( reader->pos + pad_len ) );
266 }
267 len += efi_file_read_chunk ( reader, NULL, pad_len );
268
269 /* Read CPIO header(s), if applicable */
270 name = cpio_name ( image );
271 for ( i = 0 ; ( cpio_len = cpio_header ( image, i, &cpio ) );
272 i++ ) {
273 name_len = ( cpio_len - sizeof ( cpio ) );
274 pad_len = cpio_pad_len ( cpio_len );
275 DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) %s header\n",
276 efi_file_name ( file ), reader->pos,
277 ( reader->pos + cpio_len + pad_len ),
278 image->name );
279 len += efi_file_read_chunk ( reader, &cpio,
280 sizeof ( cpio ) );
281 len += efi_file_read_chunk ( reader, name, name_len );
282 len += efi_file_read_chunk ( reader, NULL, pad_len );
283 }
284
285 /* Read file data */
286 DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) %s\n",
287 efi_file_name ( file ), reader->pos,
288 ( reader->pos + image->len ), image->name );
289 len += efi_file_read_chunk ( reader, image->data, image->len );
290 }
291
292 return len;
293}
long pad_len
Definition bigint.h:31
size_t cpio_header(struct image *image, unsigned int index, struct cpio_header *cpio)
Construct CPIO header for image, if applicable.
Definition cpio.c:156
static size_t cpio_pad_len(size_t len)
Get CPIO header zero-padding length.
Definition cpio.h:82
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition cpio.h:71
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition efi_file.c:130
#define DBGC(...)
Definition compiler.h:505
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition image.h:86
#define INITRD_ALIGN
Initial ramdisk chunk alignment.
Definition initrd.h:17
A CPIO archive header.
Definition cpio.h:21
unsigned int flags
Flags.
Definition image.h:40

References cpio_header(), cpio_name(), cpio_pad_len(), image::data, DBGC, efi_file_name(), efi_file_read_chunk(), efi_file::file, efi_file_reader::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()

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

305 {
306
307 /* Increment reference count */
308 ref_get ( &file->refcnt );
309
310 /* Return opened file */
311 *new = &file->file;
312
313 DBGC ( file, "EFIFILE %s opened via %ls\n",
314 efi_file_name ( file ), wname );
315 return 0;
316}
#define ref_get(refcnt)
Get additional reference to object.
Definition refcnt.h:93
struct refcnt refcnt
Reference count.
Definition efi_file.c:78

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()

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

Associate file with image.

Parameters
fileEFI file
imageImage

Definition at line 324 of file efi_file.c.

324 {
325
326 file->image = image;
327 file->name = image->name;
329}
static size_t efi_file_read_image(struct efi_file_reader *reader)
Read from image-backed file.
Definition efi_file.c:227

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

Referenced by efi_file_open_image(), and efi_file_read_dir().

◆ efi_file_open_image()

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

341 {
342 struct efi_file *file;
343
344 /* Allocate and initialise file */
345 file = zalloc ( sizeof ( *file ) );
346 if ( ! file )
348 ref_init ( &file->refcnt, efi_file_free );
349 memcpy ( &file->file, &efi_file_root.file, sizeof ( file->file ) );
350 memcpy ( &file->load, &efi_file_root.load, sizeof ( file->load ) );
352
353 /* Return opened file */
354 *new = &file->file;
355
356 DBGC ( file, "EFIFILE %s opened via %ls\n",
357 efi_file_name ( file ), wname );
358 return 0;
359}
#define EFI_OUT_OF_RESOURCES
Enumeration of EFI_STATUS.
static void efi_file_image(struct efi_file *file, struct image *image)
Associate file with image.
Definition efi_file.c:324
static void efi_file_free(struct refcnt *refcnt)
Free EFI file.
Definition efi_file.c:116
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition image.h:240
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition refcnt.h:65

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

Referenced by efi_file_open().

◆ efi_file_open()

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

373 {
374 struct efi_file *file = container_of ( this, struct efi_file, file );
375 char buf[ wcslen ( wname ) + 1 /* NUL */ ];
376 struct image *image;
377 char *name;
378 char *sep;
379
380 /* Convert name to ASCII */
381 snprintf ( buf, sizeof ( buf ), "%ls", wname );
382 name = buf;
383
384 /* Initial '\' indicates opening from the root directory */
385 while ( *name == '\\' ) {
386 file = &efi_file_root;
387 name++;
388 }
389
390 /* Strip redundant path separator characters */
391 while ( ( *name == '\\' ) || ( *name == '.' ) )
392 name++;
393
394 /* Allow root directory itself to be opened */
395 if ( ! *name )
396 return efi_file_open_fixed ( &efi_file_root, wname, new );
397
398 /* Fail unless opening from the root */
399 if ( file != &efi_file_root ) {
400 DBGC ( file, "EFIFILE %s is not a directory\n",
401 efi_file_name ( file ) );
402 return EFI_NOT_FOUND;
403 }
404
405 /* Fail unless opening read-only */
406 if ( mode != EFI_FILE_MODE_READ ) {
407 DBGC ( file, "EFIFILE %s cannot be opened in mode %#08llx\n",
408 name, mode );
409 return EFI_WRITE_PROTECTED;
410 }
411
412 /* Allow registered images to be opened */
413 if ( ( image = efi_file_find ( name ) ) != NULL )
414 return efi_file_open_image ( image, wname, new );
415
416 /* Allow magic initrd to be opened */
417 if ( strcasecmp ( name, efi_file_initrd.file.name ) == 0 ) {
418 return efi_file_open_fixed ( &efi_file_initrd.file, wname,
419 new );
420 }
421
422 /* Allow currently selected image to be opened as "grub*.efi",
423 * to work around buggy versions of the UEFI shim.
424 */
425 if ( ( strncasecmp ( name, "grub", 4 ) == 0 ) &&
426 ( ( sep = strrchr ( name, '.' ) ) != NULL ) &&
427 ( strcasecmp ( sep, ".efi" ) == 0 ) &&
428 ( ( image = find_image_tag ( &selected_image ) ) != NULL ) ) {
429 return efi_file_open_image ( image, wname, new );
430 }
431
432 DBGC ( file, "EFIFILE %ls does not exist\n", wname );
433 return EFI_NOT_FOUND;
434}
#define EFI_FILE_MODE_READ
#define EFI_WRITE_PROTECTED
Enumeration of EFI_STATUS.
#define EFI_NOT_FOUND
Enumeration of EFI_STATUS.
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:339
static struct efi_file_path efi_file_initrd
Magic initrd file.
Definition efi_file.c:109
static struct image * efi_file_find(const char *name)
Find EFI file image.
Definition efi_file.c:141
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:303
uint16_t mode
Acceleration mode.
Definition ena.h:15
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition image.c:393
struct image_tag selected_image
int strncasecmp(const char *first, const char *second, size_t max)
Compare case-insensitive strings.
Definition string.c:222
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition string.c:290
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383
size_t wcslen(const wchar_t *string)
Calculate length of wide-character string.
Definition wchar.c:57

References __unused, 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, find_image_tag(), mode, name, NULL, selected_image, snprintf(), strcasecmp(), strncasecmp(), strrchr(), and wcslen().

◆ efi_file_close()

EFI_STATUS EFIAPI efi_file_close ( EFI_FILE_PROTOCOL * this)
static

Close file.

Parameters
thisEFI file
Return values
efircEFI status code

Definition at line 442 of file efi_file.c.

442 {
443 struct efi_file *file = container_of ( this, struct efi_file, file );
444
445 /* Close file */
446 DBGC ( file, "EFIFILE %s closed\n", efi_file_name ( file ) );
447 ref_put ( &file->refcnt );
448
449 return 0;
450}
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107

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

Referenced by efi_file_delete().

◆ efi_file_delete()

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

458 {
459 struct efi_file *file = container_of ( this, struct efi_file, file );
460
461 DBGC ( file, "EFIFILE %s cannot be deleted\n", efi_file_name ( file ) );
462
463 /* Close file */
464 efi_file_close ( this );
465
466 /* Warn of failure to delete */
468}
#define EFI_WARN_DELETE_FAILURE
Enumeration of EFI_STATUS.
static EFI_STATUS EFIAPI efi_file_close(EFI_FILE_PROTOCOL *this)
Close file.
Definition efi_file.c:442

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

◆ efi_file_varlen()

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

481 {
482 size_t name_len;
483
484 /* Calculate structure length */
485 name_len = strlen ( name );
486 *base = ( base_len + ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
487 if ( *len < *base ) {
488 *len = *base;
490 }
491
492 /* Copy data to buffer */
493 *len = *base;
494 memcpy ( data, base, base_len );
495 efi_snprintf ( ( data + base_len ), ( name_len + 1 /* NUL */ ),
496 "%s", name );
497
498 return 0;
499}
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
int efi_snprintf(wchar_t *wbuf, size_t wsize, const char *fmt,...)
Write a formatted string to a buffer.
uint32_t base
Base.
Definition librm.h:3
__WCHAR_TYPE__ wchar_t
Definition stddef.h:50
size_t strlen(const char *src)
Get length of string.
Definition string.c:244

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

Referenced by efi_file_get_info(), and efi_file_info().

◆ efi_file_info()

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

510 {
512 size_t file_len;
513
514 /* Get file length */
515 file_len = efi_file_len ( file );
516
517 /* Populate file information */
518 memset ( &info, 0, sizeof ( info ) );
519 info.FileSize = file_len;
520 info.PhysicalSize = file_len;
521 info.Attribute = EFI_FILE_READ_ONLY;
522 if ( file == &efi_file_root )
523 info.Attribute |= EFI_FILE_DIRECTORY;
524
526 file->name, len, data );
527}
#define SIZE_OF_EFI_FILE_INFO
The FileName field of the EFI_FILE_INFO data structure is variable length.
Definition FileInfo.h:65
#define EFI_FILE_DIRECTORY
#define EFI_FILE_READ_ONLY
u32 info
Definition ar9003_mac.h:0
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:480
static size_t efi_file_len(struct efi_file *file)
Get length of EFI file.
Definition efi_file.c:159

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

Referenced by efi_file_get_info(), and efi_file_read_dir().

◆ efi_file_read_dir()

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

538 {
539 EFI_STATUS efirc;
540 struct efi_file entry;
541 struct image *image;
542 unsigned int index;
543
544 /* Construct directory entries for image-backed files */
545 index = file->pos;
547
548 /* Skip hidden images */
549 if ( image->flags & IMAGE_HIDDEN )
550 continue;
551
552 /* Skip preceding images */
553 if ( index-- )
554 continue;
555
556 /* Construct directory entry */
557 efi_file_image ( &entry, image );
558 efirc = efi_file_info ( &entry, len, data );
559 if ( efirc == 0 )
560 file->pos++;
561 return efirc;
562 }
563
564 /* No more entries */
565 *len = 0;
566 return 0;
567}
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
long index
Definition bigint.h:65
static EFI_STATUS efi_file_info(struct efi_file *file, UINTN *len, VOID *data)
Return file information structure.
Definition efi_file.c:509

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

Referenced by efi_file_read().

◆ efi_file_read()

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

578 {
579 struct efi_file *file = container_of ( this, struct efi_file, file );
580 struct efi_file_reader reader;
581 size_t pos = file->pos;
582
583 /* If this is the root directory, then construct a directory entry */
584 if ( ! file->read )
585 return efi_file_read_dir ( file, len, data );
586
587 /* Initialise reader */
588 reader.file = file;
589 reader.pos = 0;
590 reader.data = data;
591 reader.len = *len;
592
593 /* Read from the file */
594 DBGC ( file, "EFIFILE %s read [%#08zx,%#08zx)\n",
595 efi_file_name ( file ), pos, ( ( size_t ) ( pos + *len ) ) );
596 *len = file->read ( &reader );
597 assert ( ( pos + *len ) == file->pos );
598
599 return 0;
600}
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
static EFI_STATUS efi_file_read_dir(struct efi_file *file, UINTN *len, VOID *data)
Read directory entry.
Definition efi_file.c:537

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

Referenced by efi_file_load().

◆ efi_file_write()

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

611 {
612 struct efi_file *file = container_of ( this, struct efi_file, file );
613
614 DBGC ( file, "EFIFILE %s cannot write [%#08zx, %#08zx)\n",
615 efi_file_name ( file ), file->pos,
616 ( ( size_t ) ( file->pos + *len ) ) );
617 return EFI_WRITE_PROTECTED;
618}

References __unused, container_of, data, DBGC, efi_file_name(), EFI_WRITE_PROTECTED, EFIAPI, efi_file::file, len, and VOID.

◆ efi_file_set_position()

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

628 {
629 struct efi_file *file = container_of ( this, struct efi_file, file );
630 size_t len;
631
632 /* Get file length */
633 len = efi_file_len ( file );
634
635 /* Check for the magic end-of-file value */
636 if ( position == 0xffffffffffffffffULL )
637 position = len;
638
639 /* Fail if we attempt to seek past the end of the file (since
640 * we do not support writes).
641 */
642 if ( position > len ) {
643 DBGC ( file, "EFIFILE %s cannot seek to %#08llx of %#08zx\n",
644 efi_file_name ( file ), position, len );
645 return EFI_UNSUPPORTED;
646 }
647
648 /* Set position */
649 file->pos = position;
650 DBGC ( file, "EFIFILE %s position set to %#08zx\n",
651 efi_file_name ( file ), file->pos );
652
653 return 0;
654}
#define EFI_UNSUPPORTED
Enumeration of EFI_STATUS.

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

◆ efi_file_get_position()

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

664 {
665 struct efi_file *file = container_of ( this, struct efi_file, file );
666
667 *position = file->pos;
668 return 0;
669}

References container_of, EFIAPI, and efi_file::file.

◆ efi_file_get_info()

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

682 {
683 struct efi_file *file = container_of ( this, struct efi_file, file );
685 struct image *image;
686
687 /* Determine information to return */
688 if ( memcmp ( type, &efi_file_info_id, sizeof ( *type ) ) == 0 ) {
689
690 /* Get file information */
691 DBGC ( file, "EFIFILE %s get file information\n",
692 efi_file_name ( file ) );
693 return efi_file_info ( file, len, data );
694
695 } else if ( memcmp ( type, &efi_file_system_info_id,
696 sizeof ( *type ) ) == 0 ) {
697
698 /* Get file system information */
699 DBGC ( file, "EFIFILE %s get file system information\n",
700 efi_file_name ( file ) );
701 memset ( &fsinfo, 0, sizeof ( fsinfo ) );
702 fsinfo.ReadOnly = 1;
704 fsinfo.VolumeSize += image->len;
705 return efi_file_varlen ( &fsinfo.Size,
707 len, data );
708 } else {
709
710 DBGC ( file, "EFIFILE %s cannot get information of type %s\n",
711 efi_file_name ( file ), efi_guid_ntoa ( type ) );
712 return EFI_UNSUPPORTED;
713 }
714}
#define SIZE_OF_EFI_FILE_SYSTEM_INFO
The VolumeLabel field of the EFI_FILE_SYSTEM_INFO data structure is variable length.
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition efi_guid.c:726
EFI_GUID efi_file_system_info_id
File system information GUID.
Definition efi_guid.c:466
EFI_GUID efi_file_info_id
File information GUID.
Definition efi_guid.c:463
uint32_t type
Operating system type.
Definition ena.h:1
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
BOOLEAN ReadOnly
TRUE if the volume only supports read access.
UINT64 VolumeSize
The number of bytes managed by the file system.
UINT64 Size
The size of the EFI_FILE_SYSTEM_INFO structure, including the Null-terminated VolumeLabel string.

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, EFIAPI, 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, VOID, and EFI_FILE_SYSTEM_INFO::VolumeSize.

◆ efi_file_set_info()

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

727 {
728 struct efi_file *file = container_of ( this, struct efi_file, file );
729
730 DBGC ( file, "EFIFILE %s cannot set information of type %s\n",
732 return EFI_WRITE_PROTECTED;
733}

References __unused, container_of, data, DBGC, efi_file_name(), efi_guid_ntoa(), EFI_WRITE_PROTECTED, efi_file::file, len, type, and VOID.

◆ efi_file_flush()

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

744 {
745 struct efi_file *file = container_of ( this, struct efi_file, file );
746
747 DBGC ( file, "EFIFILE %s flushed\n", efi_file_name ( file ) );
748 return 0;
749}

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

◆ efi_file_load()

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

764 {
765 struct efi_file *file = container_of ( this, struct efi_file, load );
766 size_t max_len;
767 size_t file_len;
768 EFI_STATUS efirc;
769
770 /* Calculate maximum length */
771 max_len = ( data ? *len : 0 );
772 DBGC ( file, "EFIFILE %s load at %p+%#zx\n",
773 efi_file_name ( file ), data, max_len );
774
775 /* Check buffer size */
776 file_len = efi_file_len ( file );
777 if ( file_len > max_len ) {
778 *len = file_len;
780 }
781
782 /* Read from file */
783 if ( ( efirc = efi_file_read ( &file->file, len, data ) ) != 0 )
784 return efirc;
785
786 return 0;
787}
static EFI_STATUS EFIAPI efi_file_read(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data)
Read from file.
Definition efi_file.c:577
EFI_LOAD_FILE2_PROTOCOL load
EFI load file protocol.
Definition efi_file.c:82

References __unused, 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 VOID.

◆ efi_file_open_volume()

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

868 {
869
870 DBGC ( &efi_file_root, "EFIFILE open volume\n" );
871 return efi_file_open_fixed ( &efi_file_root, L"<volume>", file );
872}

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

◆ efi_block_io_reset()

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

Dummy block I/O reset.

Definition at line 882 of file efi_file.c.

882 {
883
884 DBGC ( &efi_file_root, "EFIFILE block %sreset\n",
885 ( extended ? "extended " : "" ) );
886 return 0;
887}

References __unused, DBGC, and efi_file_root.

◆ efi_block_io_read_blocks()

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

892 {
893
894 DBGC ( &efi_file_root, "EFIFILE block read ID %#08x LBA %#08llx -> "
895 "%p+%zx\n", MediaId, ( ( unsigned long long ) lba ),
896 data, ( ( size_t ) len ) );
897 return EFI_NO_MEDIA;
898}
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
uint64_t lba
Starting block number.
Definition int13.h:11

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

◆ efi_block_io_write_blocks()

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

904 {
905
906 DBGC ( &efi_file_root, "EFIFILE block write ID %#08x LBA %#08llx <- "
907 "%p+%zx\n", MediaId, ( ( unsigned long long ) lba ),
908 data, ( ( size_t ) len ) );
909 return EFI_NO_MEDIA;
910}

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

◆ efi_block_io_flush_blocks()

EFI_STATUS EFIAPI efi_block_io_flush_blocks ( EFI_BLOCK_IO_PROTOCOL *this __unused)
static

Dummy block I/O flush.

Definition at line 914 of file efi_file.c.

914 {
915
916 DBGC ( &efi_file_root, "EFIFILE block flush\n" );
917 return 0;
918}

References __unused, DBGC, and efi_file_root.

◆ efi_disk_io_read_disk()

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

941 {
942
943 DBGC ( &efi_file_root, "EFIFILE disk read ID %#08x offset %#08llx -> "
944 "%p+%zx\n", MediaId, ( ( unsigned long long ) offset ),
945 data, ( ( size_t ) len ) );
946 return EFI_NO_MEDIA;
947}

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

◆ efi_disk_io_write_disk()

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

952 {
953
954 DBGC ( &efi_file_root, "EFIFILE disk write ID %#08x offset %#08llx <- "
955 "%p+%zx\n", MediaId, ( ( unsigned long long ) offset ),
956 data, ( ( size_t ) len ) );
957 return EFI_NO_MEDIA;
958}

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

◆ efi_file_path_claim()

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

982 {
983 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
987 EFI_STATUS efirc;
988 int rc;
989
990 /* Sanity check */
991 assert ( file->handle == NULL );
992
993 /* Locate handle with this device path, if any */
994 end = file->path;
995 if ( ( ( efirc = bs->LocateDevicePath ( &efi_device_path_protocol_guid,
996 &end, &handle ) ) != 0 ) ||
997 ( end->Type != END_DEVICE_PATH_TYPE ) ) {
998 return 0;
999 }
1000
1001 /* Locate device path protocol on this handle */
1003 &old ) != 0 ) ) {
1004 DBGC ( file, "EFIFILE %s could not locate %s: %s\n",
1005 efi_file_name ( &file->file ),
1006 efi_devpath_text ( file->path ), strerror ( rc ) );
1007 return rc;
1008 }
1009
1010 /* Uninstall device path protocol, leaving other protocols untouched */
1011 if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1012 handle,
1014 NULL ) ) != 0 ) {
1015 rc = -EEFI ( efirc );
1016 DBGC ( file, "EFIFILE %s could not claim %s: %s\n",
1017 efi_file_name ( &file->file ),
1018 efi_devpath_text ( file->path ), strerror ( rc ) );
1019 return rc;
1020 }
1021
1022 DBGC ( file, "EFIFILE %s claimed %s",
1023 efi_file_name ( &file->file ), efi_devpath_text ( file->path ) );
1024 DBGC ( file, " from %s\n", efi_handle_name ( handle ) );
1025 return 0;
1026}
#define END_DEVICE_PATH_TYPE
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
int old
Definition bitops.h:65
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition efi_debug.c:652
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition efi_debug.c:247
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition efi_guid.c:169
#define efi_open(handle, protocol, interface)
Open protocol for ephemeral use.
Definition efi.h:444
#define EFI_HANDLE
Definition efi.h:53
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition efi.h:175
EFI_SYSTEM_TABLE * efi_systab
uint16_t handle
Handle.
Definition smbios.h:5
uint32_t end
Ending offset.
Definition netvsc.h:7
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
EFI Boot Services Table.
Definition UefiSpec.h:1931
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition UefiSpec.h:2011
EFI_LOCATE_DEVICE_PATH LocateDevicePath
Definition UefiSpec.h:1972
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition DevicePath.h:46
EFI_DEVICE_PATH_PROTOCOL * path
Device path.
Definition efi_file.c:103
struct efi_file file
EFI file.
Definition efi_file.c:101
EFI_HANDLE handle
EFI handle.
Definition efi_file.c:105

References assert, DBGC, EEFI, efi_device_path_protocol_guid, efi_devpath_text(), efi_file_name(), EFI_HANDLE, 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(), and EFI_BOOT_SERVICES::UninstallMultipleProtocolInterfaces.

Referenced by efi_file_install().

◆ efi_file_path_install()

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

1039 {
1040 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1041 EFI_STATUS efirc;
1042 int rc;
1043
1044 /* Sanity check */
1045 assert ( file->handle == NULL );
1046
1047 /* Create a new handle with this device path */
1048 if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1049 &file->handle,
1052 NULL ) ) != 0 ) {
1053 rc = -EEFI ( efirc );
1054 DBGC ( file, "EFIFILE %s could not install %s: %s\n",
1055 efi_file_name ( &file->file ),
1056 efi_devpath_text ( file->path ), strerror ( rc ) );
1057 return rc;
1058 }
1059
1060 DBGC ( file, "EFIFILE %s installed as %s\n",
1061 efi_file_name ( &file->file ), efi_devpath_text ( file->path ) );
1062 return 0;
1063}
EFI_GUID efi_load_file2_protocol_guid
Load file 2 protocol GUID.
Definition efi_guid.c:269
EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES InstallMultipleProtocolInterfaces
Definition UefiSpec.h:2010

References assert, 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()

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

1071 {
1072 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1073 EFI_STATUS efirc;
1074 int rc;
1075
1076 /* Do nothing if file is already uninstalled */
1077 if ( ! file->handle )
1078 return;
1079
1080 /* Uninstall protocols. Do this via two separate calls, in
1081 * case another executable has already uninstalled the device
1082 * path protocol from our handle.
1083 */
1084 if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1085 file->handle,
1087 NULL ) ) != 0 ) {
1088 rc = -EEFI ( efirc );
1089 DBGC ( file, "EFIFILE %s could not uninstall %s: %s\n",
1090 efi_file_name ( &file->file ),
1091 efi_devpath_text ( file->path ), strerror ( rc ) );
1092 /* Continue uninstalling */
1093 }
1094 if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1095 file->handle,
1097 NULL ) ) != 0 ) {
1098 rc = -EEFI ( efirc );
1099 DBGC ( file, "EFIFILE %s could not uninstall %s: %s\n",
1100 efi_file_name ( &file->file ),
1102 strerror ( rc ) );
1103 /* Continue uninstalling */
1104 }
1105
1106 /* Mark handle as uninstalled */
1107 file->handle = NULL;
1108}

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

1116 {
1117 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1118 EFI_DISK_IO_PROTOCOL *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,
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 */
1167 &diskio ) ) != 0 ) {
1168 DBGC ( handle, "Could not open disk I/O protocol: %s\n",
1169 strerror ( rc ) );
1171 goto err_open;
1172 }
1173 assert ( diskio == &efi_disk_io_protocol );
1174
1175 /* Claim Linux initrd fixed device path */
1176 if ( ( rc = efi_file_path_claim ( &efi_file_initrd ) ) != 0 )
1177 goto err_initrd_claim;
1178
1179 /* Install Linux initrd fixed device path file if non-empty */
1180 for_each_image ( image ) {
1181 if ( image->flags & IMAGE_HIDDEN )
1182 continue;
1183 if ( ( rc = efi_file_path_install ( &efi_file_initrd ) ) != 0 )
1184 goto err_initrd_install;
1185 break;
1186 }
1187
1188 return 0;
1189
1191 err_initrd_install:
1192 err_initrd_claim:
1194 err_open:
1196 handle,
1203 err_install:
1204 return rc;
1205}
struct _EFI_DISK_IO_PROTOCOL EFI_DISK_IO_PROTOCOL
Definition DiskIo.h:29
static EFI_SIMPLE_FILE_SYSTEM_PROTOCOL efi_simple_file_system_protocol
EFI simple file system protocol.
Definition efi_file.c:875
static void efi_file_path_uninstall(struct efi_file_path *file)
Uninstall fixed device path file.
Definition efi_file.c:1071
static EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol
Dummy EFI block I/O protocol.
Definition efi_file.c:929
static int efi_file_path_install(struct efi_file_path *file)
Install fixed device path file.
Definition efi_file.c:1039
static int efi_file_path_claim(struct efi_file_path *file)
Claim use of fixed device path.
Definition efi_file.c:982
static EFI_DISK_IO_PROTOCOL efi_disk_io_protocol
Dummy EFI disk I/O protocol.
Definition efi_file.c:961
EFI_GUID efi_block_io_protocol_guid
Block I/O protocol GUID.
Definition efi_guid.c:145
EFI_GUID efi_disk_io_protocol_guid
Disk I/O protocol GUID.
Definition efi_guid.c:189
EFI_GUID efi_simple_file_system_protocol_guid
Simple file system protocol GUID.
Definition efi_guid.c:337
void efi_close_by_driver(EFI_HANDLE handle, EFI_GUID *protocol)
Close protocol opened for persistent use by a driver.
Definition efi_open.c:279
#define efi_open_by_driver(handle, protocol, interface)
Open protocol for persistent use by a driver.
Definition efi.h:474
#define DBGC_EFI_OPENERS(...)
Definition efi.h:345

References assert, 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_HANDLE, 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, 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 1212 of file efi_file.c.

1212 {
1213 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1214 EFI_STATUS efirc;
1215 int rc;
1216
1217 /* Uninstall Linux initrd fixed device path file */
1219
1220 /* Close our own disk I/O protocol */
1222
1223 /* We must install the file system protocol first, since
1224 * otherwise the EDK2 code will attempt to helpfully uninstall
1225 * it when the block I/O protocol is uninstalled, leading to a
1226 * system lock-up.
1227 */
1228 if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1229 handle,
1235 &efi_block_io_protocol, NULL ) ) != 0 ) {
1236 rc = -EEFI ( efirc );
1237 DBGC ( handle, "Could not uninstall simple file system "
1238 "protocols: %s\n", strerror ( rc ) );
1239 /* Oh dear */
1240 }
1241}

References 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_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

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 = "",
}
#define EFI_FILE_PROTOCOL_REVISION
static EFI_STATUS EFIAPI efi_file_delete(EFI_FILE_PROTOCOL *this)
Close and delete file.
Definition efi_file.c:458
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:680
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:726
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:372
static EFI_STATUS EFIAPI efi_file_write(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data __unused)
Write to file.
Definition efi_file.c:610
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:762
static EFI_STATUS EFIAPI efi_file_flush(EFI_FILE_PROTOCOL *this)
Flush file modified data.
Definition efi_file.c:744
static EFI_STATUS EFIAPI efi_file_set_position(EFI_FILE_PROTOCOL *this, UINT64 position)
Set file position.
Definition efi_file.c:627
static EFI_STATUS EFIAPI efi_file_get_position(EFI_FILE_PROTOCOL *this, UINT64 *position)
Get file position.
Definition efi_file.c:663
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition refcnt.c:102
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition refcnt.h:78

Root directory.

Definition at line 108 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

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 struct @155172135212117201227002307145202243102020027056 efi_file_initrd_path
Linux initrd fixed device path.
static size_t efi_file_read_initrd(struct efi_file_reader *reader)
Read from magic initrd file.
Definition efi_file.c:241

Magic initrd file.

Definition at line 109 of file efi_file.c.

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

◆ vendor

Definition at line 814 of file efi_file.c.

◆ end

Definition at line 815 of file efi_file.c.

◆ [struct]

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 MEDIA_DEVICE_PATH
#define END_ENTIRE_DEVICE_PATH_SUBTYPE
#define MEDIA_VENDOR_DP
Media vendor device path subtype.
#define LINUX_INITRD_VENDOR_GUID
Linux initrd fixed device path vendor GUID.
Definition efi_file.c:59

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,
}
#define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
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:867

EFI simple file system protocol.

Definition at line 875 of file efi_file.c.

875 {
877 .OpenVolume = efi_file_open_volume,
878};

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 EFI_MEDIA_ID_MAGIC
EFI media ID.
Definition efi_file.c:56
#define TRUE
Definition tlan.h:46

Dummy block I/O media.

Definition at line 921 of file efi_file.c.

921 {
922 .MediaId = EFI_MEDIA_ID_MAGIC,
923 .MediaPresent = TRUE,
924 .ReadOnly = TRUE,
925 .BlockSize = 1,
926};

◆ 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,
}
#define EFI_BLOCK_IO_PROTOCOL_REVISION
Definition BlockIo.h:205
static EFI_STATUS EFIAPI efi_block_io_reset(EFI_BLOCK_IO_PROTOCOL *block_io, BOOLEAN verify __unused)
Reset EFI block device.
Definition efi_block.c:132
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:902
static EFI_BLOCK_IO_MEDIA efi_block_io_media
Dummy block I/O media.
Definition efi_file.c:921
static EFI_STATUS EFIAPI efi_block_io_flush_blocks(EFI_BLOCK_IO_PROTOCOL *this __unused)
Dummy block I/O flush.
Definition efi_file.c:914
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:891

Dummy EFI block I/O protocol.

Definition at line 929 of file efi_file.c.

929 {
931 .Media = &efi_block_io_media,
932 .Reset = efi_block_io_reset,
933 .ReadBlocks = efi_block_io_read_blocks,
934 .WriteBlocks = efi_block_io_write_blocks,
935 .FlushBlocks = efi_block_io_flush_blocks,
936};

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,
}
#define EFI_DISK_IO_PROTOCOL_REVISION
Definition DiskIo.h:91
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:951
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:940

Dummy EFI disk I/O protocol.

Definition at line 961 of file efi_file.c.

961 {
963 .ReadDisk = efi_disk_io_read_disk,
964 .WriteDisk = efi_disk_io_write_disk,
965};

Referenced by efi_file_install(), and efi_file_uninstall().