iPXE
efi_file.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * EFI file protocols
30  *
31  */
32 
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <errno.h>
39 #include <wchar.h>
40 #include <ipxe/image.h>
41 #include <ipxe/cpio.h>
42 #include <ipxe/efi/efi.h>
47 #include <ipxe/efi/Guid/FileInfo.h>
49 #include <ipxe/efi/efi_strings.h>
50 #include <ipxe/efi/efi_path.h>
51 #include <ipxe/efi/efi_file.h>
52 
53 /** EFI media ID */
54 #define EFI_MEDIA_ID_MAGIC 0x69505845
55 
56 /** Linux initrd fixed device path vendor GUID */
57 #define LINUX_INITRD_VENDOR_GUID \
58  { 0x5568e427, 0x68fc, 0x4f3d, \
59  { 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } }
60 
61 /** An EFI virtual file reader */
63  /** EFI file */
64  struct efi_file *file;
65  /** Position within virtual file */
66  size_t pos;
67  /** Output data buffer */
68  void *data;
69  /** Length of output data buffer */
70  size_t len;
71 };
72 
73 /** An EFI file */
74 struct efi_file {
75  /** Reference count */
76  struct refcnt refcnt;
77  /** EFI file protocol */
79  /** EFI load file protocol */
81  /** Image (if any) */
82  struct image *image;
83  /** Filename */
84  const char *name;
85  /** Current file position */
86  size_t pos;
87  /**
88  * Read from file
89  *
90  * @v reader File reader
91  * @ret len Length read
92  */
93  size_t ( * read ) ( struct efi_file_reader *reader );
94 };
95 
96 /** An EFI fixed device path file */
97 struct efi_file_path {
98  /** EFI file */
99  struct efi_file file;
100  /** Device path */
102  /** EFI handle */
104 };
105 
106 static struct efi_file efi_file_root;
108 
109 /**
110  * Free EFI file
111  *
112  * @v refcnt Reference count
113  */
114 static void efi_file_free ( struct refcnt *refcnt ) {
115  struct efi_file *file =
116  container_of ( refcnt, struct efi_file, refcnt );
117 
118  image_put ( file->image );
119  free ( file );
120 }
121 
122 /**
123  * Get EFI file name (for debugging)
124  *
125  * @v file EFI file
126  * @ret name Name
127  */
128 static const char * efi_file_name ( struct efi_file *file ) {
129 
130  return ( file == &efi_file_root ? "<root>" : file->name );
131 }
132 
133 /**
134  * Find EFI file image
135  *
136  * @v name Filename
137  * @ret image Image, or NULL
138  */
139 static struct image * efi_file_find ( const char *name ) {
140  struct image *image;
141 
142  /* Find image */
143  for_each_image ( image ) {
144  if ( strcasecmp ( image->name, name ) == 0 )
145  return image;
146  }
147 
148  return NULL;
149 }
150 
151 /**
152  * Get length of EFI file
153  *
154  * @v file EFI file
155  * @ret len Length of file
156  */
157 static size_t efi_file_len ( struct efi_file *file ) {
158  struct efi_file_reader reader;
159 
160  /* If this is the root directory, then treat as length zero */
161  if ( ! file->read )
162  return 0;
163 
164  /* Initialise reader */
165  reader.file = file;
166  reader.pos = 0;
167  reader.data = NULL;
168  reader.len = 0;
169 
170  /* Perform dummy read to determine file length */
171  file->read ( &reader );
172 
173  return reader.pos;
174 }
175 
176 /**
177  * Read chunk of EFI file
178  *
179  * @v reader EFI file reader
180  * @v data Input data, or UNULL to zero-fill
181  * @v len Length of input data
182  * @ret len Length of output data
183  */
184 static size_t efi_file_read_chunk ( struct efi_file_reader *reader,
185  userptr_t data, size_t len ) {
186  struct efi_file *file = reader->file;
187  size_t offset;
188 
189  /* Calculate offset into input data */
190  offset = ( file->pos - reader->pos );
191 
192  /* Consume input data range */
193  reader->pos += len;
194 
195  /* Calculate output length */
196  if ( offset < len ) {
197  len -= offset;
198  } else {
199  len = 0;
200  }
201  if ( len > reader->len )
202  len = reader->len;
203 
204  /* Copy or zero output data */
205  if ( data ) {
206  copy_from_user ( reader->data, data, offset, len );
207  } else {
208  memset ( reader->data, 0, len );
209  }
210 
211  /* Consume output buffer */
212  file->pos += len;
213  reader->data += len;
214  reader->len -= len;
215 
216  return len;
217 }
218 
219 /**
220  * Read from image-backed file
221  *
222  * @v reader EFI file reader
223  * @ret len Length read
224  */
225 static size_t efi_file_read_image ( struct efi_file_reader *reader ) {
226  struct efi_file *file = reader->file;
227  struct image *image = file->image;
228 
229  /* Read from file */
230  return efi_file_read_chunk ( reader, image->data, image->len );
231 }
232 
233 /**
234  * Read from magic initrd file
235  *
236  * @v reader EFI file reader
237  * @ret len Length read
238  */
239 static size_t efi_file_read_initrd ( struct efi_file_reader *reader ) {
240  struct efi_file *file = reader->file;
241  struct cpio_header cpio;
242  struct image *image;
243  const char *name;
244  size_t pad_len;
245  size_t cpio_len;
246  size_t name_len;
247  size_t len;
248  unsigned int i;
249 
250  /* Read from file */
251  len = 0;
252  for_each_image ( image ) {
253 
254  /* Skip hidden images */
255  if ( image->flags & IMAGE_HIDDEN )
256  continue;
257 
258  /* Pad to alignment boundary */
259  pad_len = ( ( -reader->pos ) & ( INITRD_ALIGN - 1 ) );
260  if ( pad_len ) {
261  DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) pad\n",
262  efi_file_name ( file ), reader->pos,
263  ( reader->pos + pad_len ) );
264  }
265  len += efi_file_read_chunk ( reader, UNULL, pad_len );
266 
267  /* Read CPIO header(s), if applicable */
268  name = cpio_name ( image );
269  for ( i = 0 ; ( cpio_len = cpio_header ( image, i, &cpio ) );
270  i++ ) {
271  name_len = ( cpio_len - sizeof ( cpio ) );
272  pad_len = cpio_pad_len ( cpio_len );
273  DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) %s header\n",
274  efi_file_name ( file ), reader->pos,
275  ( reader->pos + cpio_len + pad_len ),
276  image->name );
277  len += efi_file_read_chunk ( reader,
278  virt_to_user ( &cpio ),
279  sizeof ( cpio ) );
280  len += efi_file_read_chunk ( reader,
281  virt_to_user ( name ),
282  name_len );
283  len += efi_file_read_chunk ( reader, UNULL, pad_len );
284  }
285 
286  /* Read file data */
287  DBGC ( file, "EFIFILE %s [%#08zx,%#08zx) %s\n",
288  efi_file_name ( file ), reader->pos,
289  ( reader->pos + image->len ), image->name );
290  len += efi_file_read_chunk ( reader, image->data, image->len );
291  }
292 
293  return len;
294 }
295 
296 /**
297  * Open fixed file
298  *
299  * @v file EFI file
300  * @v wname Filename
301  * @v new New EFI file
302  * @ret efirc EFI status code
303  */
305  const wchar_t *wname,
306  EFI_FILE_PROTOCOL **new ) {
307 
308  /* Increment reference count */
309  ref_get ( &file->refcnt );
310 
311  /* Return opened file */
312  *new = &file->file;
313 
314  DBGC ( file, "EFIFILE %s opened via %ls\n",
315  efi_file_name ( file ), wname );
316  return 0;
317 }
318 
319 /**
320  * Associate file with image
321  *
322  * @v file EFI file
323  * @v image Image
324  */
325 static void efi_file_image ( struct efi_file *file, struct image *image ) {
326 
327  file->image = image;
328  file->name = image->name;
329  file->read = efi_file_read_image;
330 }
331 
332 /**
333  * Open image-backed file
334  *
335  * @v image Image
336  * @v wname Filename
337  * @v new New EFI file
338  * @ret efirc EFI status code
339  */
341  const wchar_t *wname,
342  EFI_FILE_PROTOCOL **new ) {
343  struct efi_file *file;
344 
345  /* Allocate and initialise file */
346  file = zalloc ( sizeof ( *file ) );
347  if ( ! file )
348  return EFI_OUT_OF_RESOURCES;
349  ref_init ( &file->refcnt, efi_file_free );
350  memcpy ( &file->file, &efi_file_root.file, sizeof ( file->file ) );
351  memcpy ( &file->load, &efi_file_root.load, sizeof ( file->load ) );
353 
354  /* Return opened file */
355  *new = &file->file;
356 
357  DBGC ( file, "EFIFILE %s opened via %ls\n",
358  efi_file_name ( file ), wname );
359  return 0;
360 }
361 
362 /**
363  * Open file
364  *
365  * @v this EFI file
366  * @ret new New EFI file
367  * @v wname Filename
368  * @v mode File mode
369  * @v attributes File attributes (for newly-created files)
370  * @ret efirc EFI status code
371  */
372 static EFI_STATUS EFIAPI
374  CHAR16 *wname, UINT64 mode, UINT64 attributes __unused ) {
375  struct efi_file *file = container_of ( this, struct efi_file, file );
376  char buf[ wcslen ( wname ) + 1 /* NUL */ ];
377  struct image *image;
378  char *name;
379  char *sep;
380 
381  /* Convert name to ASCII */
382  snprintf ( buf, sizeof ( buf ), "%ls", wname );
383  name = buf;
384 
385  /* Initial '\' indicates opening from the root directory */
386  while ( *name == '\\' ) {
387  file = &efi_file_root;
388  name++;
389  }
390 
391  /* Allow root directory itself to be opened */
392  if ( ( name[0] == '\0' ) || ( name[0] == '.' ) )
393  return efi_file_open_fixed ( &efi_file_root, wname, new );
394 
395  /* Fail unless opening from the root */
396  if ( file != &efi_file_root ) {
397  DBGC ( file, "EFIFILE %s is not a directory\n",
398  efi_file_name ( file ) );
399  return EFI_NOT_FOUND;
400  }
401 
402  /* Fail unless opening read-only */
403  if ( mode != EFI_FILE_MODE_READ ) {
404  DBGC ( file, "EFIFILE %s cannot be opened in mode %#08llx\n",
405  name, mode );
406  return EFI_WRITE_PROTECTED;
407  }
408 
409  /* Allow registered images to be opened */
410  if ( ( image = efi_file_find ( name ) ) != NULL )
411  return efi_file_open_image ( image, wname, new );
412 
413  /* Allow magic initrd to be opened */
414  if ( strcasecmp ( name, efi_file_initrd.file.name ) == 0 ) {
415  return efi_file_open_fixed ( &efi_file_initrd.file, wname,
416  new );
417  }
418 
419  /* Allow currently selected image to be opened as "grub*.efi",
420  * to work around buggy versions of the UEFI shim.
421  */
422  if ( ( strncasecmp ( name, "grub", 4 ) == 0 ) &&
423  ( ( sep = strrchr ( name, '.' ) ) != NULL ) &&
424  ( strcasecmp ( sep, ".efi" ) == 0 ) &&
425  ( ( image = find_image_tag ( &selected_image ) ) != NULL ) ) {
426  return efi_file_open_image ( image, wname, new );
427  }
428 
429  DBGC ( file, "EFIFILE %ls does not exist\n", wname );
430  return EFI_NOT_FOUND;
431 }
432 
433 /**
434  * Close file
435  *
436  * @v this EFI file
437  * @ret efirc EFI status code
438  */
440  struct efi_file *file = container_of ( this, struct efi_file, file );
441 
442  /* Close file */
443  DBGC ( file, "EFIFILE %s closed\n", efi_file_name ( file ) );
444  ref_put ( &file->refcnt );
445 
446  return 0;
447 }
448 
449 /**
450  * Close and delete file
451  *
452  * @v this EFI file
453  * @ret efirc EFI status code
454  */
456  struct efi_file *file = container_of ( this, struct efi_file, file );
457 
458  DBGC ( file, "EFIFILE %s cannot be deleted\n", efi_file_name ( file ) );
459 
460  /* Close file */
461  efi_file_close ( this );
462 
463  /* Warn of failure to delete */
465 }
466 
467 /**
468  * Return variable-length data structure
469  *
470  * @v base Base data structure (starting with UINT64)
471  * @v base_len Length of base data structure
472  * @v name Name to append to base data structure
473  * @v len Length of data buffer
474  * @v data Data buffer
475  * @ret efirc EFI status code
476  */
477 static EFI_STATUS efi_file_varlen ( UINT64 *base, size_t base_len,
478  const char *name, UINTN *len, VOID *data ) {
479  size_t name_len;
480 
481  /* Calculate structure length */
482  name_len = strlen ( name );
483  *base = ( base_len + ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
484  if ( *len < *base ) {
485  *len = *base;
486  return EFI_BUFFER_TOO_SMALL;
487  }
488 
489  /* Copy data to buffer */
490  *len = *base;
491  memcpy ( data, base, base_len );
492  efi_snprintf ( ( data + base_len ), ( name_len + 1 /* NUL */ ),
493  "%s", name );
494 
495  return 0;
496 }
497 
498 /**
499  * Return file information structure
500  *
501  * @v file EFI file
502  * @v len Length of data buffer
503  * @v data Data buffer
504  * @ret efirc EFI status code
505  */
507  VOID *data ) {
509  size_t file_len;
510 
511  /* Get file length */
512  file_len = efi_file_len ( file );
513 
514  /* Populate file information */
515  memset ( &info, 0, sizeof ( info ) );
516  info.FileSize = file_len;
517  info.PhysicalSize = file_len;
518  info.Attribute = EFI_FILE_READ_ONLY;
519  if ( file == &efi_file_root )
520  info.Attribute |= EFI_FILE_DIRECTORY;
521 
523  file->name, len, data );
524 }
525 
526 /**
527  * Read directory entry
528  *
529  * @v file EFI file
530  * @v len Length to read
531  * @v data Data buffer
532  * @ret efirc EFI status code
533  */
535  VOID *data ) {
536  EFI_STATUS efirc;
537  struct efi_file entry;
538  struct image *image;
539  unsigned int index;
540 
541  /* Construct directory entries for image-backed files */
542  index = file->pos;
543  for_each_image ( image ) {
544 
545  /* Skip hidden images */
546  if ( image->flags & IMAGE_HIDDEN )
547  continue;
548 
549  /* Skip preceding images */
550  if ( index-- )
551  continue;
552 
553  /* Construct directory entry */
554  efi_file_image ( &entry, image );
555  efirc = efi_file_info ( &entry, len, data );
556  if ( efirc == 0 )
557  file->pos++;
558  return efirc;
559  }
560 
561  /* No more entries */
562  *len = 0;
563  return 0;
564 }
565 
566 /**
567  * Read from file
568  *
569  * @v this EFI file
570  * @v len Length to read
571  * @v data Data buffer
572  * @ret efirc EFI status code
573  */
575  UINTN *len, VOID *data ) {
576  struct efi_file *file = container_of ( this, struct efi_file, file );
577  struct efi_file_reader reader;
578  size_t pos = file->pos;
579 
580  /* If this is the root directory, then construct a directory entry */
581  if ( ! file->read )
582  return efi_file_read_dir ( file, len, data );
583 
584  /* Initialise reader */
585  reader.file = file;
586  reader.pos = 0;
587  reader.data = data;
588  reader.len = *len;
589 
590  /* Read from the file */
591  DBGC ( file, "EFIFILE %s read [%#08zx,%#08zx)\n",
592  efi_file_name ( file ), pos, ( ( size_t ) ( pos + *len ) ) );
593  *len = file->read ( &reader );
594  assert ( ( pos + *len ) == file->pos );
595 
596  return 0;
597 }
598 
599 /**
600  * Write to file
601  *
602  * @v this EFI file
603  * @v len Length to write
604  * @v data Data buffer
605  * @ret efirc EFI status code
606  */
608  UINTN *len, VOID *data __unused ) {
609  struct efi_file *file = container_of ( this, struct efi_file, file );
610 
611  DBGC ( file, "EFIFILE %s cannot write [%#08zx, %#08zx)\n",
612  efi_file_name ( file ), file->pos,
613  ( ( size_t ) ( file->pos + *len ) ) );
614  return EFI_WRITE_PROTECTED;
615 }
616 
617 /**
618  * Set file position
619  *
620  * @v this EFI file
621  * @v position New file position
622  * @ret efirc EFI status code
623  */
625  UINT64 position ) {
626  struct efi_file *file = container_of ( this, struct efi_file, file );
627  size_t len;
628 
629  /* Get file length */
630  len = efi_file_len ( file );
631 
632  /* Check for the magic end-of-file value */
633  if ( position == 0xffffffffffffffffULL )
634  position = len;
635 
636  /* Fail if we attempt to seek past the end of the file (since
637  * we do not support writes).
638  */
639  if ( position > len ) {
640  DBGC ( file, "EFIFILE %s cannot seek to %#08llx of %#08zx\n",
641  efi_file_name ( file ), position, len );
642  return EFI_UNSUPPORTED;
643  }
644 
645  /* Set position */
646  file->pos = position;
647  DBGC ( file, "EFIFILE %s position set to %#08zx\n",
648  efi_file_name ( file ), file->pos );
649 
650  return 0;
651 }
652 
653 /**
654  * Get file position
655  *
656  * @v this EFI file
657  * @ret position New file position
658  * @ret efirc EFI status code
659  */
661  UINT64 *position ) {
662  struct efi_file *file = container_of ( this, struct efi_file, file );
663 
664  *position = file->pos;
665  return 0;
666 }
667 
668 /**
669  * Get file information
670  *
671  * @v this EFI file
672  * @v type Type of information
673  * @v len Buffer size
674  * @v data Buffer
675  * @ret efirc EFI status code
676  */
678  EFI_GUID *type,
679  UINTN *len, VOID *data ) {
680  struct efi_file *file = container_of ( this, struct efi_file, file );
681  EFI_FILE_SYSTEM_INFO fsinfo;
682  struct image *image;
683 
684  /* Determine information to return */
685  if ( memcmp ( type, &efi_file_info_id, sizeof ( *type ) ) == 0 ) {
686 
687  /* Get file information */
688  DBGC ( file, "EFIFILE %s get file information\n",
689  efi_file_name ( file ) );
690  return efi_file_info ( file, len, data );
691 
692  } else if ( memcmp ( type, &efi_file_system_info_id,
693  sizeof ( *type ) ) == 0 ) {
694 
695  /* Get file system information */
696  DBGC ( file, "EFIFILE %s get file system information\n",
697  efi_file_name ( file ) );
698  memset ( &fsinfo, 0, sizeof ( fsinfo ) );
699  fsinfo.ReadOnly = 1;
701  fsinfo.VolumeSize += image->len;
702  return efi_file_varlen ( &fsinfo.Size,
704  len, data );
705  } else {
706 
707  DBGC ( file, "EFIFILE %s cannot get information of type %s\n",
708  efi_file_name ( file ), efi_guid_ntoa ( type ) );
709  return EFI_UNSUPPORTED;
710  }
711 }
712 
713 /**
714  * Set file information
715  *
716  * @v this EFI file
717  * @v type Type of information
718  * @v len Buffer size
719  * @v data Buffer
720  * @ret efirc EFI status code
721  */
722 static EFI_STATUS EFIAPI
725  struct efi_file *file = container_of ( this, struct efi_file, file );
726 
727  DBGC ( file, "EFIFILE %s cannot set information of type %s\n",
729  return EFI_WRITE_PROTECTED;
730 }
731 
732 /**
733  * Flush file modified data
734  *
735  * @v this EFI file
736  * @v type Type of information
737  * @v len Buffer size
738  * @v data Buffer
739  * @ret efirc EFI status code
740  */
742  struct efi_file *file = container_of ( this, struct efi_file, file );
743 
744  DBGC ( file, "EFIFILE %s flushed\n", efi_file_name ( file ) );
745  return 0;
746 }
747 
748 /**
749  * Load file
750  *
751  * @v this EFI file loader
752  * @v path File path
753  * @v boot Boot policy
754  * @v len Buffer size
755  * @v data Buffer, or NULL
756  * @ret efirc EFI status code
757  */
758 static EFI_STATUS EFIAPI
761  BOOLEAN boot __unused, UINTN *len, VOID *data ) {
762  struct efi_file *file = container_of ( this, struct efi_file, load );
763  size_t max_len;
764  size_t file_len;
765  EFI_STATUS efirc;
766 
767  /* Calculate maximum length */
768  max_len = ( data ? *len : 0 );
769  DBGC ( file, "EFIFILE %s load at %p+%#zx\n",
770  efi_file_name ( file ), data, max_len );
771 
772  /* Check buffer size */
773  file_len = efi_file_len ( file );
774  if ( file_len > max_len ) {
775  *len = file_len;
776  return EFI_BUFFER_TOO_SMALL;
777  }
778 
779  /* Read from file */
780  if ( ( efirc = efi_file_read ( &file->file, len, data ) ) != 0 )
781  return efirc;
782 
783  return 0;
784 }
785 
786 /** Root directory */
787 static struct efi_file efi_file_root = {
788  .refcnt = REF_INIT ( ref_no_free ),
789  .file = {
790  .Revision = EFI_FILE_PROTOCOL_REVISION,
791  .Open = efi_file_open,
792  .Close = efi_file_close,
793  .Delete = efi_file_delete,
794  .Read = efi_file_read,
795  .Write = efi_file_write,
796  .GetPosition = efi_file_get_position,
797  .SetPosition = efi_file_set_position,
798  .GetInfo = efi_file_get_info,
799  .SetInfo = efi_file_set_info,
800  .Flush = efi_file_flush,
801  },
802  .load = {
803  .LoadFile = efi_file_load,
804  },
805  .image = NULL,
806  .name = "",
807 };
808 
809 /** Linux initrd fixed device path */
810 static struct {
813 } __attribute__ (( packed )) efi_file_initrd_path = {
814  .vendor = {
815  .Header = {
816  .Type = MEDIA_DEVICE_PATH,
817  .SubType = MEDIA_VENDOR_DP,
818  .Length[0] = sizeof ( efi_file_initrd_path.vendor ),
819  },
820  .Guid = LINUX_INITRD_VENDOR_GUID,
821  },
822  .end = {
823  .Type = END_DEVICE_PATH_TYPE,
825  .Length[0] = sizeof ( efi_file_initrd_path.end ),
826  },
827 };
828 
829 /** Magic initrd file */
830 static struct efi_file_path efi_file_initrd = {
831  .file = {
832  .refcnt = REF_INIT ( ref_no_free ),
833  .file = {
834  .Revision = EFI_FILE_PROTOCOL_REVISION,
835  .Open = efi_file_open,
836  .Close = efi_file_close,
837  .Delete = efi_file_delete,
838  .Read = efi_file_read,
839  .Write = efi_file_write,
840  .GetPosition = efi_file_get_position,
841  .SetPosition = efi_file_set_position,
842  .GetInfo = efi_file_get_info,
843  .SetInfo = efi_file_set_info,
844  .Flush = efi_file_flush,
845  },
846  .load = {
847  .LoadFile = efi_file_load,
848  },
849  .image = NULL,
850  .name = "initrd.magic",
851  .read = efi_file_read_initrd,
852  },
853  .path = &efi_file_initrd_path.vendor.Header,
854 };
855 
856 /**
857  * Open root directory
858  *
859  * @v filesystem EFI simple file system
860  * @ret file EFI file handle
861  * @ret efirc EFI status code
862  */
863 static EFI_STATUS EFIAPI
865  EFI_FILE_PROTOCOL **file ) {
866 
867  DBGC ( &efi_file_root, "EFIFILE open volume\n" );
868  return efi_file_open_fixed ( &efi_file_root, L"<volume>", file );
869 }
870 
871 /** EFI simple file system protocol */
874  .OpenVolume = efi_file_open_volume,
875 };
876 
877 /** Dummy block I/O reset */
878 static EFI_STATUS EFIAPI
880 
881  DBGC ( &efi_file_root, "EFIFILE block %sreset\n",
882  ( extended ? "extended " : "" ) );
883  return 0;
884 }
885 
886 /** Dummy block I/O read */
887 static EFI_STATUS EFIAPI
889  EFI_LBA lba, UINTN len, VOID *data ) {
890 
891  DBGC ( &efi_file_root, "EFIFILE block read ID %#08x LBA %#08llx -> "
892  "%p+%zx\n", MediaId, ( ( unsigned long long ) lba ),
893  data, ( ( size_t ) len ) );
894  return EFI_NO_MEDIA;
895 }
896 
897 /** Dummy block I/O write */
898 static EFI_STATUS EFIAPI
900  UINT32 MediaId, EFI_LBA lba, UINTN len,
901  VOID *data ) {
902 
903  DBGC ( &efi_file_root, "EFIFILE block write ID %#08x LBA %#08llx <- "
904  "%p+%zx\n", MediaId, ( ( unsigned long long ) lba ),
905  data, ( ( size_t ) len ) );
906  return EFI_NO_MEDIA;
907 }
908 
909 /** Dummy block I/O flush */
910 static EFI_STATUS EFIAPI
912 
913  DBGC ( &efi_file_root, "EFIFILE block flush\n" );
914  return 0;
915 }
916 
917 /** Dummy block I/O media */
920  .MediaPresent = TRUE,
921  .ReadOnly = TRUE,
922  .BlockSize = 1,
923 };
924 
925 /** Dummy EFI block I/O protocol */
928  .Media = &efi_block_io_media,
929  .Reset = efi_block_io_reset,
930  .ReadBlocks = efi_block_io_read_blocks,
931  .WriteBlocks = efi_block_io_write_blocks,
932  .FlushBlocks = efi_block_io_flush_blocks,
933 };
934 
935 /** Dummy disk I/O read */
936 static EFI_STATUS EFIAPI
938  UINT64 offset, UINTN len, VOID *data ) {
939 
940  DBGC ( &efi_file_root, "EFIFILE disk read ID %#08x offset %#08llx -> "
941  "%p+%zx\n", MediaId, ( ( unsigned long long ) offset ),
942  data, ( ( size_t ) len ) );
943  return EFI_NO_MEDIA;
944 }
945 
946 /** Dummy disk I/O write */
947 static EFI_STATUS EFIAPI
949  UINT64 offset, UINTN len, VOID *data ) {
950 
951  DBGC ( &efi_file_root, "EFIFILE disk write ID %#08x offset %#08llx <- "
952  "%p+%zx\n", MediaId, ( ( unsigned long long ) offset ),
953  data, ( ( size_t ) len ) );
954  return EFI_NO_MEDIA;
955 }
956 
957 /** Dummy EFI disk I/O protocol */
960  .ReadDisk = efi_disk_io_read_disk,
961  .WriteDisk = efi_disk_io_write_disk,
962 };
963 
964 /**
965  * Claim use of fixed device path
966  *
967  * @v file Fixed device path file
968  * @ret rc Return status code
969  *
970  * The design choice in Linux of using a single fixed device path is
971  * unfortunately messy to support, since device paths must be unique
972  * within a system. When multiple bootloaders are used (e.g. GRUB
973  * loading iPXE loading Linux) then only one bootloader can ever
974  * install the device path onto a handle. Bootloaders must therefore
975  * be prepared to locate an existing handle and uninstall its device
976  * path protocol instance before installing a new handle with the
977  * required device path.
978  */
979 static int efi_file_path_claim ( struct efi_file_path *file ) {
983  VOID *old;
984  EFI_STATUS efirc;
985  int rc;
986 
987  /* Sanity check */
988  assert ( file->handle == NULL );
989 
990  /* Locate handle with this device path, if any */
991  end = file->path;
992  if ( ( ( efirc = bs->LocateDevicePath ( &efi_device_path_protocol_guid,
993  &end, &handle ) ) != 0 ) ||
994  ( end->Type != END_DEVICE_PATH_TYPE ) ) {
995  return 0;
996  }
997 
998  /* Locate device path protocol on this handle */
999  if ( ( ( efirc = bs->HandleProtocol ( handle,
1001  &old ) ) != 0 ) ) {
1002  rc = -EEFI ( efirc );
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 }
1026 
1027 /**
1028  * Install fixed device path file
1029  *
1030  * @v file Fixed device path file
1031  * @ret rc Return status code
1032  *
1033  * Linux 5.7 added the ability to autodetect an initrd by searching
1034  * for a handle via a fixed vendor-specific "Linux initrd device path"
1035  * and then locating and using the EFI_LOAD_FILE2_PROTOCOL instance on
1036  * that handle.
1037  */
1038 static int efi_file_path_install ( struct efi_file_path *file ) {
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,
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 }
1063 
1064 /**
1065  * Uninstall fixed device path file
1066  *
1067  * @v file Fixed device path file
1068  * @ret rc Return status code
1069  */
1070 static void efi_file_path_uninstall ( struct efi_file_path *file ) {
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,
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 }
1108 
1109 /**
1110  * Install EFI simple file system protocol
1111  *
1112  * @v handle EFI handle
1113  * @ret rc Return status code
1114  */
1117  union {
1118  EFI_DISK_IO_PROTOCOL *diskio;
1119  void *interface;
1120  } diskio;
1121  struct image *image;
1122  EFI_STATUS efirc;
1123  int rc;
1124 
1125  /* Reset root directory state */
1126  efi_file_root.pos = 0;
1127 
1128  /* Install the simple file system protocol, block I/O
1129  * protocol, and disk I/O protocol. We don't have a block
1130  * device, but large parts of the EDK2 codebase make the
1131  * assumption that file systems are normally attached to block
1132  * devices, and so we create a dummy block device on the same
1133  * handle just to keep things looking normal.
1134  */
1135  if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1136  &handle,
1142  &efi_simple_file_system_protocol, NULL ) ) != 0 ) {
1143  rc = -EEFI ( efirc );
1144  DBGC ( handle, "Could not install simple file system "
1145  "protocols: %s\n", strerror ( rc ) );
1146  goto err_install;
1147  }
1148 
1149  /* The FAT filesystem driver has a bug: if a block device
1150  * contains no FAT filesystem but does have an
1151  * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL instance, the FAT driver
1152  * will assume that it must have previously installed the
1153  * EFI_SIMPLE_FILE_SYSTEM_PROTOCOL. This causes the FAT
1154  * driver to claim control of our device, and to refuse to
1155  * stop driving it, which prevents us from later uninstalling
1156  * correctly.
1157  *
1158  * Work around this bug by opening the disk I/O protocol
1159  * ourselves, thereby preventing the FAT driver from opening
1160  * it.
1161  *
1162  * Note that the alternative approach of opening the block I/O
1163  * protocol (and thereby in theory preventing DiskIo from
1164  * attaching to the block I/O protocol) causes an endless loop
1165  * of calls to our DRIVER_STOP method when starting the EFI
1166  * shell. I have no idea why this is.
1167  */
1168  if ( ( efirc = bs->OpenProtocol ( handle, &efi_disk_io_protocol_guid,
1169  &diskio.interface, efi_image_handle,
1170  handle,
1171  EFI_OPEN_PROTOCOL_BY_DRIVER ) ) != 0){
1172  rc = -EEFI ( efirc );
1173  DBGC ( handle, "Could not open disk I/O protocol: %s\n",
1174  strerror ( rc ) );
1176  goto err_open;
1177  }
1178  assert ( diskio.diskio == &efi_disk_io_protocol );
1179 
1180  /* Claim Linux initrd fixed device path */
1181  if ( ( rc = efi_file_path_claim ( &efi_file_initrd ) ) != 0 )
1182  goto err_initrd_claim;
1183 
1184  /* Install Linux initrd fixed device path file if non-empty */
1185  for_each_image ( image ) {
1186  if ( image->flags & IMAGE_HIDDEN )
1187  continue;
1188  if ( ( rc = efi_file_path_install ( &efi_file_initrd ) ) != 0 )
1189  goto err_initrd_install;
1190  break;
1191  }
1192 
1193  return 0;
1194 
1196  err_initrd_install:
1197  err_initrd_claim:
1200  err_open:
1202  handle,
1209  err_install:
1210  return rc;
1211 }
1212 
1213 /**
1214  * Uninstall EFI simple file system protocol
1215  *
1216  * @v handle EFI handle
1217  */
1220  EFI_STATUS efirc;
1221  int rc;
1222 
1223  /* Uninstall Linux initrd fixed device path file */
1225 
1226  /* Close our own disk I/O protocol */
1229 
1230  /* We must install the file system protocol first, since
1231  * otherwise the EDK2 code will attempt to helpfully uninstall
1232  * it when the block I/O protocol is uninstalled, leading to a
1233  * system lock-up.
1234  */
1235  if ( ( efirc = bs->UninstallMultipleProtocolInterfaces (
1236  handle,
1242  &efi_block_io_protocol, NULL ) ) != 0 ) {
1243  rc = -EEFI ( efirc );
1244  DBGC ( handle, "Could not uninstall simple file system "
1245  "protocols: %s\n", strerror ( rc ) );
1246  /* Oh dear */
1247  }
1248 }
VENDOR_DEVICE_PATH vendor
Definition: efi_file.c:811
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:723
#define __attribute__(x)
Definition: compiler.h:10
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:937
void * data
Output data buffer.
Definition: efi_file.c:68
struct image_tag selected_image
unsigned int flags
Flags.
Definition: image.h:36
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
uint32_t base
Base.
Definition: librm.h:252
static void efi_file_free(struct refcnt *refcnt)
Free EFI file.
Definition: efi_file.c:114
#define EFI_UNSUPPORTED
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:117
struct refcnt refcnt
Reference count.
Definition: efi_file.c:76
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
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:373
BOOLEAN ReadOnly
TRUE if the volume only supports read access.
An EFI virtual file reader.
Definition: efi_file.c:62
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:677
EFI file protocols.
UINT64 Revision
The version of the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
userptr_t data
Raw file image.
Definition: image.h:41
u32 info
Definition: ar9003_mac.h:67
static EFI_STATUS EFIAPI efi_file_get_position(EFI_FILE_PROTOCOL *this, UINT64 *position)
Get file position.
Definition: efi_file.c:660
static int efi_file_path_install(struct efi_file_path *file)
Install fixed device path file.
Definition: efi_file.c:1038
A CPIO archive header.
Definition: cpio.h:20
#define END_DEVICE_PATH_TYPE
Definition: DevicePath.h:1393
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:289
EFI_GUID efi_file_system_info_id
File system information GUID.
Definition: efi_guid.c:398
128 bit buffer containing a unique identifier value.
Definition: Base.h:215
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
Error codes.
__SIZE_TYPE__ size_t
Definition: stdint.h:6
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:340
static void efi_file_path_uninstall(struct efi_file_path *file)
Uninstall fixed device path file.
Definition: efi_file.c:1070
EFI strings.
unsigned char BOOLEAN
Disk IO protocol as defined in the UEFI 2.0 specification.
uint32_t type
Operating system type.
Definition: ena.h:12
static EFI_BLOCK_IO_PROTOCOL efi_block_io_protocol
Dummy EFI block I/O protocol.
Definition: efi_file.c:926
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:411
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition: image.h:219
#define DBGC(...)
Definition: compiler.h:505
This protocol provides control over block devices.
Definition: BlockIo.h:216
long index
Definition: bigint.h:62
unsigned int UINT32
Definition: ProcessorBind.h:98
#define EFI_OPEN_PROTOCOL_BY_DRIVER
Definition: UefiSpec.h:1357
EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES InstallMultipleProtocolInterfaces
Definition: UefiSpec.h:2009
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:208
int old
Definition: bitops.h:64
unsigned short CHAR16
void efi_file_uninstall(EFI_HANDLE handle)
Uninstall EFI simple file system protocol.
Definition: efi_file.c:1218
static EFI_STATUS EFIAPI efi_file_read(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data)
Read from file.
Definition: efi_file.c:574
int strncasecmp(const char *first, const char *second, size_t max)
Compare case-insensitive strings.
Definition: string.c:221
#define DBGC_EFI_OPENERS(...)
Definition: efi.h:322
An executable image.
Definition: image.h:24
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:45
#define EFI_FILE_READ_ONLY
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:888
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:119
EFI_FILE_PROTOCOL file
EFI file protocol.
Definition: efi_file.c:78
#define EFI_FILE_DIRECTORY
EFI_CLOSE_PROTOCOL CloseProtocol
Definition: UefiSpec.h:2000
#define SIZE_OF_EFI_FILE_INFO
The FileName field of the EFI_FILE_INFO data structure is variable length.
Definition: FileInfo.h:64
static EFI_DISK_IO_PROTOCOL efi_disk_io_protocol
Dummy EFI disk I/O protocol.
Definition: efi_file.c:958
static size_t cpio_pad_len(size_t len)
Get CPIO header zero-padding length.
Definition: cpio.h:84
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:759
#define EFI_NO_MEDIA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:126
#define EFI_OUT_OF_RESOURCES
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:123
size_t wcslen(const wchar_t *string)
Calculate length of wide-character string.
Definition: wchar.c:41
EFI_GUID efi_disk_io_protocol_guid
Disk I/O protocol GUID.
Definition: efi_guid.c:164
CPIO archives.
static size_t efi_file_len(struct efi_file *file)
Get length of EFI file.
Definition: efi_file.c:157
int efi_file_install(EFI_HANDLE handle)
Install EFI simple file system protocol.
Definition: efi_file.c:1115
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:948
A reference counter.
Definition: refcnt.h:26
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition: image.c:357
static size_t efi_file_read_image(struct efi_file_reader *reader)
Read from image-backed file.
Definition: efi_file.c:225
static EFI_STATUS efi_file_info(struct efi_file *file, UINTN *len, VOID *data)
Return file information structure.
Definition: efi_file.c:506
#define MEDIA_VENDOR_DP
Media vendor device path subtype.
Definition: DevicePath.h:1095
size_t pos
Current file position.
Definition: efi_file.c:86
struct efi_file * file
EFI file.
Definition: efi_file.c:64
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:864
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t cpio_header(struct image *image, unsigned int index, struct cpio_header *cpio)
Construct CPIO header for image, if applicable.
Definition: cpio.c:151
static struct @429 efi_file_initrd_path
Linux initrd fixed device path.
uint32_t userptr_t
A pointer to a user buffer.
Definition: libkir.h:159
size_t len
Length of output data buffer.
Definition: efi_file.c:70
static struct efi_file_path efi_file_initrd
Magic initrd file.
Definition: efi_file.c:107
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An object interface.
Definition: interface.h:124
SimpleFileSystem protocol as defined in the UEFI 2.0 specification.
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:304
Executable images.
EFI_GUID efi_simple_file_system_protocol_guid
Simple file system protocol GUID.
Definition: efi_guid.c:304
UINT64 EFI_LBA
Logical block address.
Definition: UefiBaseType.h:47
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
The Vendor Device Path allows the creation of vendor-defined Device Paths.
Definition: DevicePath.h:142
static struct efi_file efi_file_root
Root directory.
Definition: efi_file.c:106
int efi_snprintf(wchar_t *wbuf, size_t wsize, const char *fmt,...)
Write a formatted string to a buffer.
Definition: efi_strings.c:106
EFI_HANDLE_PROTOCOL HandleProtocol
Definition: UefiSpec.h:1967
#define EFI_DISK_IO_PROTOCOL_REVISION
Definition: DiskIo.h:90
static size_t efi_file_read_chunk(struct efi_file_reader *reader, userptr_t data, size_t len)
Read chunk of EFI file.
Definition: efi_file.c:184
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:463
uint8_t filesystem
System type.
Definition: eltorito.h:24
This protocol is used to abstract Block I/O interfaces.
Definition: DiskIo.h:100
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:810
static EFI_STATUS EFIAPI efi_file_set_position(EFI_FILE_PROTOCOL *this, UINT64 position)
Set file position.
Definition: efi_file.c:624
#define EFI_WRITE_PROTECTED
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:122
#define LINUX_INITRD_VENDOR_GUID
Linux initrd fixed device path vendor GUID.
Definition: efi_file.c:57
#define for_each_image(image)
Iterate over all registered images.
Definition: image.h:172
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_debug.c:256
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
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:477
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
EFI_GUID efi_load_file2_protocol_guid
Load file 2 protocol GUID.
Definition: efi_guid.c:240
#define EFIAPI
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:640
EFI Boot Services Table.
Definition: UefiSpec.h:1930
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:34
size_t len
Length of raw file image.
Definition: image.h:43
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92
#define IMAGE_HIDDEN
Image will be hidden from enumeration.
Definition: image.h:73
EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES UninstallMultipleProtocolInterfaces
Definition: UefiSpec.h:2010
static EFI_STATUS EFIAPI efi_file_close(EFI_FILE_PROTOCOL *this)
Close file.
Definition: efi_file.c:439
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
uint64_t lba
Starting block number.
Definition: int13.h:22
static void image_put(struct image *image)
Decrement reference count on an image.
Definition: image.h:229
static EFI_STATUS EFIAPI efi_file_flush(EFI_FILE_PROTOCOL *this)
Flush file modified data.
Definition: efi_file.c:741
EFI device paths.
#define MEDIA_DEVICE_PATH
Definition: DevicePath.h:1011
UINT64 UINTN
Unsigned value of native width.
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:144
UINT64 VolumeSize
The number of bytes managed by the file system.
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:899
static EFI_STATUS EFIAPI efi_file_write(EFI_FILE_PROTOCOL *this, UINTN *len, VOID *data __unused)
Write to file.
Definition: efi_file.c:607
#define VOID
Undeclared type.
Definition: Base.h:271
unsigned long long UINT64
Definition: ProcessorBind.h:96
Provides a GUID and a data structure that can be used with EFI_FILE_PROTOCOL.SetInfo() and EFI_FILE_P...
#define TRUE
Definition: tlan.h:46
#define EFI_FILE_PROTOCOL_REVISION
#define EFI_WARN_DELETE_FAILURE
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:150
EFI_DEVICE_PATH_PROTOCOL end
Definition: efi_file.c:812
EFI API.
#define EFI_MEDIA_ID_MAGIC
EFI media ID.
Definition: efi_file.c:54
EFI_HANDLE handle
EFI handle.
Definition: efi_file.c:103
static EFI_STATUS efi_file_read_dir(struct efi_file *file, UINTN *len, VOID *data)
Read directory entry.
Definition: efi_file.c:534
size_t pos
Position within virtual file.
Definition: efi_file.c:66
Block IO read only mode data and updated only via members of BlockIO.
Definition: BlockIo.h:130
EFI_LOAD_FILE2_PROTOCOL load
EFI load file protocol.
Definition: efi_file.c:80
long pad_len
Definition: bigint.h:30
static EFI_STATUS EFIAPI efi_file_delete(EFI_FILE_PROTOCOL *this)
Close and delete file.
Definition: efi_file.c:455
#define END_ENTIRE_DEVICE_PATH_SUBTYPE
Definition: DevicePath.h:1394
#define INITRD_ALIGN
Alignment for CPIO archives within an initrd.
Definition: cpio.h:64
#define EFI_NOT_FOUND
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:128
#define UNULL
Equivalent of NULL for user pointers.
Definition: uaccess.h:42
EFI_GUID efi_block_io_protocol_guid
Block I/O protocol GUID.
Definition: efi_guid.c:120
#define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION
EFI_GUID efi_file_info_id
File information GUID.
Definition: efi_guid.c:395
#define EFI_FILE_MODE_READ
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
userptr_t virt_to_user(volatile const void *addr)
Convert virtual address to user pointer.
Block IO protocol as defined in the UEFI 2.0 specification.
uint8_t data[48]
Additional event data.
Definition: ena.h:22
struct image * image
Image (if any)
Definition: efi_file.c:82
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:77
EFI_DEVICE_PATH_PROTOCOL * path
Device path.
Definition: efi_file.c:101
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:879
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
UINT8 Type
0x01 Hardware Device Path.
Definition: DevicePath.h:46
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
EFI_SYSTEM_TABLE * efi_systab
EFI_OPEN_PROTOCOL OpenProtocol
Definition: UefiSpec.h:1999
UINT32 MediaId
The curent media Id.
Definition: BlockIo.h:134
static int efi_file_path_claim(struct efi_file_path *file)
Claim use of fixed device path.
Definition: efi_file.c:979
static EFI_STATUS EFIAPI efi_block_io_flush_blocks(EFI_BLOCK_IO_PROTOCOL *this __unused)
Dummy block I/O flush.
Definition: efi_file.c:911
UINT64 Revision
The revision to which the block IO interface adheres.
Definition: BlockIo.h:222
size_t(* read)(struct efi_file_reader *reader)
Read from file.
Definition: efi_file.c:93
static EFI_SIMPLE_FILE_SYSTEM_PROTOCOL efi_simple_file_system_protocol
EFI simple file system protocol.
Definition: efi_file.c:872
An EFI fixed device path file.
Definition: efi_file.c:97
static size_t efi_file_read_initrd(struct efi_file_reader *reader)
Read from magic initrd file.
Definition: efi_file.c:239
uint16_t handle
Handle.
Definition: smbios.h:16
Load File protocol as defined in the UEFI 2.0 specification.
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
char * name
Name.
Definition: image.h:34
#define EFI_BLOCK_IO_PROTOCOL_REVISION
Definition: BlockIo.h:204
uint32_t len
Length.
Definition: ena.h:14
UINT64 Revision
The revision to which the disk I/O interface adheres.
Definition: DiskIo.h:106
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:101
static struct image * efi_file_find(const char *name)
Find EFI file image.
Definition: efi_file.c:139
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
UINT64 Size
The size of the EFI_FILE_SYSTEM_INFO structure, including the Null-terminated VolumeLabel string.
String functions.
Provides a GUID and a data structure that can be used with EFI_FILE_PROTOCOL.GetInfo() or EFI_FILE_PR...
static EFI_BLOCK_IO_MEDIA efi_block_io_media
Dummy block I/O media.
Definition: efi_file.c:918
An EFI file.
Definition: efi_file.c:74
The EFI_FILE_PROTOCOL provides file IO access to supported file systems.
Definition: efi.h:59
EFI_LOCATE_DEVICE_PATH LocateDevicePath
Definition: UefiSpec.h:1971
const char * name
Filename.
Definition: efi_file.c:84
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
The EFI_LOAD_FILE_PROTOCOL is a simple protocol used to obtain files from arbitrary devices.
Definition: LoadFile2.h:74
#define SIZE_OF_EFI_FILE_SYSTEM_INFO
The VolumeLabel field of the EFI_FILE_SYSTEM_INFO data structure is variable length.
struct efi_file file
EFI file.
Definition: efi_file.c:99
String functions.
static const char * efi_file_name(struct efi_file *file)
Get EFI file name (for debugging)
Definition: efi_file.c:128
void * memset(void *dest, int character, size_t len) __nonnull
static const char * cpio_name(struct image *image)
Get CPIO image name.
Definition: cpio.h:73
static void efi_file_image(struct efi_file *file, struct image *image)
Associate file with image.
Definition: efi_file.c:325