Home | History | Annotate | Download | only in Library
      1 /** @file
      2   Provides interface to EFI_FILE_HANDLE functionality.
      3 
      4   Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
      5   This program and the accompanying materials
      6   are licensed and made available under the terms and conditions of the BSD License
      7   which accompanies this distribution.  The full text of the license may be found at
      8   http://opensource.org/licenses/bsd-license.php
      9 
     10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     12 
     13 **/
     14 
     15 #ifndef _FILE_HANDLE_LIBRARY_HEADER_
     16 #define _FILE_HANDLE_LIBRARY_HEADER_
     17 
     18 #include <Protocol/SimpleFileSystem.h>
     19 
     20 /// The tag for use in identifying UNICODE files.
     21 /// If the file is UNICODE, the first 16 bits of the file will equal this value.
     22 extern CONST UINT16 gUnicodeFileTag;
     23 
     24 /**
     25   This function retrieves information about the file for the handle
     26   specified and stores it in the allocated pool memory.
     27 
     28   This function allocates a buffer to store the file's information. It is the
     29   caller's responsibility to free the buffer.
     30 
     31   @param[in] FileHandle         The file handle of the file for which information is
     32                                 being requested.
     33 
     34   @retval NULL                  Information could not be retrieved.
     35   @retval !NULL                 The information about the file.
     36 **/
     37 EFI_FILE_INFO*
     38 EFIAPI
     39 FileHandleGetInfo (
     40   IN EFI_FILE_HANDLE            FileHandle
     41   );
     42 
     43 /**
     44   This function sets the information about the file for the opened handle
     45   specified.
     46 
     47   @param[in]  FileHandle        The file handle of the file for which information
     48                                 is being set.
     49 
     50   @param[in]  FileInfo          The information to set.
     51 
     52   @retval EFI_SUCCESS           The information was set.
     53   @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
     54   @retval EFI_UNSUPPORTED       The FileHandle does not support FileInfo.
     55   @retval EFI_NO_MEDIA          The device has no medium.
     56   @retval EFI_DEVICE_ERROR      The device reported an error.
     57   @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.
     58   @retval EFI_WRITE_PROTECTED   The file or medium is write protected.
     59   @retval EFI_ACCESS_DENIED     The file was opened read only.
     60   @retval EFI_VOLUME_FULL       The volume is full.
     61 **/
     62 EFI_STATUS
     63 EFIAPI
     64 FileHandleSetInfo (
     65   IN EFI_FILE_HANDLE            FileHandle,
     66   IN CONST EFI_FILE_INFO        *FileInfo
     67   );
     68 
     69 /**
     70   This function reads information from an opened file.
     71 
     72   If FileHandle is not a directory, the function reads the requested number of
     73   bytes from the file at the file's current position and returns them in Buffer.
     74   If the read goes beyond the end of the file, the read length is truncated to the
     75   end of the file. The file's current position is increased by the number of bytes
     76   returned.  If FileHandle is a directory, the function reads the directory entry
     77   at the file's current position and returns the entry in Buffer. If the Buffer
     78   is not large enough to hold the current directory entry, then
     79   EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
     80   BufferSize is set to be the size of the buffer needed to read the entry. On
     81   success, the current position is updated to the next directory entry. If there
     82   are no more directory entries, the read returns a zero-length buffer.
     83   EFI_FILE_INFO is the structure returned as the directory entry.
     84 
     85   @param[in] FileHandle          The opened file handle.
     86   @param[in, out] BufferSize     On input, the size of buffer in bytes.  On return,
     87                                  the number of bytes written.
     88   @param[out] Buffer             The buffer to put read data into.
     89 
     90   @retval EFI_SUCCESS         Data was read.
     91   @retval EFI_NO_MEDIA          The device has no media.
     92   @retval EFI_DEVICE_ERROR  The device reported an error.
     93   @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.
     94   @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
     95                                 size.
     96 
     97 **/
     98 EFI_STATUS
     99 EFIAPI
    100 FileHandleRead(
    101   IN EFI_FILE_HANDLE            FileHandle,
    102   IN OUT UINTN                  *BufferSize,
    103   OUT VOID                      *Buffer
    104   );
    105 
    106 /**
    107   Write data to a file.
    108 
    109   This function writes the specified number of bytes to the file at the current
    110   file position. The current file position is advanced the actual number of bytes
    111   written, which is returned in BufferSize. Partial writes only occur when there
    112   has been a data error during the write attempt (such as "volume space full").
    113   The file is automatically grown to hold the data if required. Direct writes to
    114   opened directories are not supported.
    115 
    116   @param[in] FileHandle          The opened file for writing.
    117   @param[in, out] BufferSize     On input, the number of bytes in Buffer.  On output,
    118                                  the number of bytes written.
    119   @param[in] Buffer              The buffer containing data to write is stored.
    120 
    121   @retval EFI_SUCCESS         Data was written.
    122   @retval EFI_UNSUPPORTED       Writes to an open directory are not supported.
    123   @retval EFI_NO_MEDIA          The device has no media.
    124   @retval EFI_DEVICE_ERROR  The device reported an error.
    125   @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.
    126   @retval EFI_WRITE_PROTECTED The device is write-protected.
    127   @retval EFI_ACCESS_DENIED The file was opened for read only.
    128   @retval EFI_VOLUME_FULL The volume is full.
    129 **/
    130 EFI_STATUS
    131 EFIAPI
    132 FileHandleWrite(
    133   IN EFI_FILE_HANDLE            FileHandle,
    134   IN OUT UINTN                  *BufferSize,
    135   IN VOID                       *Buffer
    136   );
    137 
    138 /**
    139   Close an open file handle.
    140 
    141   This function closes a specified file handle. All "dirty" cached file data is
    142   flushed to the device, and the file is closed. In all cases the handle is
    143   closed.
    144 
    145   @param[in] FileHandle           The file handle to close.
    146 
    147   @retval EFI_SUCCESS             The file handle was closed successfully.
    148 **/
    149 EFI_STATUS
    150 EFIAPI
    151 FileHandleClose (
    152   IN EFI_FILE_HANDLE            FileHandle
    153   );
    154 
    155 /**
    156   Delete a file and close the handle.
    157 
    158   This function closes and deletes a file. In all cases the file handle is closed.
    159   If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
    160   returned, but the handle is still closed.
    161 
    162   @param[in] FileHandle             The file handle to delete.
    163 
    164   @retval EFI_SUCCESS               The file was closed successfully.
    165   @retval EFI_WARN_DELETE_FAILURE   The handle was closed, but the file was not
    166                                     deleted.
    167   @retval INVALID_PARAMETER         One of the parameters has an invalid value.
    168 **/
    169 EFI_STATUS
    170 EFIAPI
    171 FileHandleDelete (
    172   IN EFI_FILE_HANDLE    FileHandle
    173   );
    174 
    175 /**
    176   Set the current position in a file.
    177 
    178   This function sets the current file position for the handle to the position
    179   supplied. With the exception of moving to position 0xFFFFFFFFFFFFFFFF, only
    180   absolute positioning is supported, and moving past the end of the file is
    181   allowed (a subsequent write would grow the file). Moving to position
    182   0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
    183   If FileHandle is a directory, the only position that may be set is zero. This
    184   has the effect of starting the read process of the directory entries over again.
    185 
    186   @param[in] FileHandle         The file handle on which the position is being set.
    187   @param[in] Position           The byte position from the beginning of the file.
    188 
    189   @retval EFI_SUCCESS           The operation completed successfully.
    190   @retval EFI_UNSUPPORTED       The request for non-zero is not valid on
    191                                 directories.
    192   @retval INVALID_PARAMETER     One of the parameters has an invalid value.
    193 **/
    194 EFI_STATUS
    195 EFIAPI
    196 FileHandleSetPosition (
    197   IN EFI_FILE_HANDLE    FileHandle,
    198   IN UINT64             Position
    199   );
    200 
    201 /**
    202   Gets a file's current position.
    203 
    204   This function retrieves the current file position for the file handle. For
    205   directories, the current file position has no meaning outside of the file
    206   system driver. As such, the operation is not supported. An error is returned
    207   if FileHandle is a directory.
    208 
    209   @param[in] FileHandle         The open file handle on which to get the position.
    210   @param[out] Position          The byte position from beginning of file.
    211 
    212   @retval EFI_SUCCESS           The operation completed successfully.
    213   @retval INVALID_PARAMETER     One of the parameters has an invalid value.
    214   @retval EFI_UNSUPPORTED       The request is not valid on directories.
    215 **/
    216 EFI_STATUS
    217 EFIAPI
    218 FileHandleGetPosition (
    219   IN EFI_FILE_HANDLE            FileHandle,
    220   OUT UINT64                    *Position
    221   );
    222 /**
    223   Flushes data on a file.
    224 
    225   This function flushes all modified data associated with a file to a device.
    226 
    227   @param[in] FileHandle         The file handle on which to flush data.
    228 
    229   @retval EFI_SUCCESS           The data was flushed.
    230   @retval EFI_NO_MEDIA          The device has no media.
    231   @retval EFI_DEVICE_ERROR      The device reported an error.
    232   @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.
    233   @retval EFI_WRITE_PROTECTED   The file or medium is write protected.
    234   @retval EFI_ACCESS_DENIED     The file was opened for read only.
    235 **/
    236 EFI_STATUS
    237 EFIAPI
    238 FileHandleFlush (
    239   IN EFI_FILE_HANDLE            FileHandle
    240   );
    241 
    242 /**
    243   Function to determine if a given handle is a directory handle.
    244 
    245   Open the file information on the DirHandle and verify that the Attribute
    246   includes EFI_FILE_DIRECTORY bit set.
    247 
    248   @param[in] DirHandle          Handle to open file.
    249 
    250   @retval EFI_SUCCESS           DirHandle is a directory.
    251   @retval EFI_INVALID_PARAMETER DirHandle is NULL.
    252                                 The file information returns from FileHandleGetInfo is NULL.
    253   @retval EFI_NOT_FOUND         DirHandle is not a directory.
    254 **/
    255 EFI_STATUS
    256 EFIAPI
    257 FileHandleIsDirectory (
    258   IN EFI_FILE_HANDLE            DirHandle
    259   );
    260 
    261 /** Retrieve first entry from a directory.
    262 
    263   This function takes an open directory handle and gets information from the
    264   first entry in the directory.  A buffer is allocated to contain
    265   the information and a pointer to the buffer is returned in *Buffer.  The
    266   caller can use FileHandleFindNextFile() to get subsequent directory entries.
    267 
    268   The buffer will be freed by FileHandleFindNextFile() when the last directory
    269   entry is read.  Otherwise, the caller must free the buffer, using FreePool,
    270   when finished with it.
    271 
    272   @param[in]  DirHandle         The file handle of the directory to search.
    273   @param[out] Buffer            The pointer to pointer to buffer for file's information.
    274 
    275   @retval EFI_SUCCESS           Found the first file.
    276   @retval EFI_NOT_FOUND         Cannot find the directory.
    277   @retval EFI_NO_MEDIA          The device has no media.
    278   @retval EFI_DEVICE_ERROR      The device reported an error.
    279   @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.
    280   @return Others                The status of FileHandleGetInfo, FileHandleSetPosition,
    281                                 or FileHandleRead.
    282 **/
    283 EFI_STATUS
    284 EFIAPI
    285 FileHandleFindFirstFile (
    286   IN EFI_FILE_HANDLE            DirHandle,
    287   OUT EFI_FILE_INFO             **Buffer
    288   );
    289 
    290 /** Retrieve next entries from a directory.
    291 
    292   To use this function, the caller must first call the FileHandleFindFirstFile()
    293   function to get the first directory entry.  Subsequent directory entries are
    294   retrieved by using the FileHandleFindNextFile() function.  This function can
    295   be called several times to get each entry from the directory.  If the call of
    296   FileHandleFindNextFile() retrieved the last directory entry, the next call of
    297   this function will set *NoFile to TRUE and free the buffer.
    298 
    299   @param[in]  DirHandle         The file handle of the directory.
    300   @param[out] Buffer            The pointer to buffer for file's information.
    301   @param[out] NoFile            The pointer to boolean when last file is found.
    302 
    303   @retval EFI_SUCCESS           Found the next file, or reached last file.
    304   @retval EFI_NO_MEDIA          The device has no media.
    305   @retval EFI_DEVICE_ERROR      The device reported an error.
    306   @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.
    307 **/
    308 EFI_STATUS
    309 EFIAPI
    310 FileHandleFindNextFile(
    311   IN EFI_FILE_HANDLE             DirHandle,
    312   OUT EFI_FILE_INFO              *Buffer,
    313   OUT BOOLEAN                    *NoFile
    314   );
    315 
    316 /**
    317   Retrieve the size of a file.
    318 
    319   This function extracts the file size info from the FileHandle's EFI_FILE_INFO
    320   data.
    321 
    322   @param[in] FileHandle         The file handle from which size is retrieved.
    323   @param[out] Size              The pointer to size.
    324 
    325   @retval EFI_SUCCESS           Operation was completed successfully.
    326   @retval EFI_DEVICE_ERROR      Cannot access the file.
    327   @retval EFI_INVALID_PARAMETER FileHandle is NULL.
    328                                 Size is NULL.
    329 **/
    330 EFI_STATUS
    331 EFIAPI
    332 FileHandleGetSize (
    333   IN EFI_FILE_HANDLE            FileHandle,
    334   OUT UINT64                    *Size
    335   );
    336 
    337 /**
    338   Set the size of a file.
    339 
    340   This function changes the file size info from the FileHandle's EFI_FILE_INFO
    341   data.
    342 
    343   @param[in] FileHandle         The file handle whose size is to be changed.
    344   @param[in] Size               The new size.
    345 
    346   @retval EFI_SUCCESS           The operation completed successfully.
    347   @retval EFI_DEVICE_ERROR      Cannot access the file.
    348   @retval EFI_INVALID_PARAMETER FileHandle is NULL.
    349 **/
    350 EFI_STATUS
    351 EFIAPI
    352 FileHandleSetSize (
    353   IN EFI_FILE_HANDLE            FileHandle,
    354   IN UINT64                     Size
    355   );
    356 
    357 /**
    358   Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
    359   directory 'stack'. If the file is a directory, then append the '\' char at the
    360   end of name string. If it's not a directory, then the last '\' should not be
    361   added.
    362 
    363   @param[in] Handle             Handle to the Directory or File to create path to.
    364   @param[out] FullFileName      Pointer to pointer to generated full file name.  It
    365                                 is the responsibility of the caller to free this memory
    366                                 with a call to FreePool().
    367   @retval EFI_SUCCESS           The operation was successful and FullFileName is valid.
    368   @retval EFI_INVALID_PARAMETER Handle was NULL.
    369   @retval EFI_INVALID_PARAMETER FullFileName was NULL.
    370   @retval EFI_OUT_OF_MEMORY     A memory allocation failed.
    371 **/
    372 EFI_STATUS
    373 EFIAPI
    374 FileHandleGetFileName (
    375   IN CONST EFI_FILE_HANDLE      Handle,
    376   OUT CHAR16                    **FullFileName
    377   );
    378 
    379 /**
    380   Function to read a single line (up to but not including the \n) from a file.
    381 
    382   If the position upon start is 0, then the Ascii Boolean will be set.  This should be
    383   maintained and not changed for all operations with the same file.
    384   The function will not return the \r and \n character in buffer. When an empty line is
    385   read a CHAR_NULL character will be returned in buffer.
    386 
    387   @param[in]       Handle        FileHandle to read from.
    388   @param[in, out]  Buffer        The pointer to buffer to read into.
    389   @param[in, out]  Size          The pointer to number of bytes in Buffer.
    390   @param[in]       Truncate      If the buffer is large enough, this has no effect.
    391                                  If the buffer is is too small and Truncate is TRUE,
    392                                  the line will be truncated.
    393                                  If the buffer is is too small and Truncate is FALSE,
    394                                  then no read will occur.
    395 
    396   @param[in, out]  Ascii         Boolean value for indicating whether the file is
    397                                  Ascii (TRUE) or UCS2 (FALSE).
    398 
    399   @retval EFI_SUCCESS           The operation was successful.  The line is stored in
    400                                 Buffer.
    401   @retval EFI_INVALID_PARAMETER Handle was NULL.
    402   @retval EFI_INVALID_PARAMETER Size was NULL.
    403   @retval EFI_BUFFER_TOO_SMALL  Size was not large enough to store the line.
    404                                 Size was updated to the minimum space required.
    405   @sa FileHandleRead
    406 **/
    407 EFI_STATUS
    408 EFIAPI
    409 FileHandleReadLine(
    410   IN EFI_FILE_HANDLE            Handle,
    411   IN OUT CHAR16                 *Buffer,
    412   IN OUT UINTN                  *Size,
    413   IN BOOLEAN                    Truncate,
    414   IN OUT BOOLEAN                *Ascii
    415   );
    416 
    417 /**
    418   Function to read a single line from a file. The \n is not included in the returned
    419   buffer.  The returned buffer must be callee freed.
    420 
    421   If the position upon start is 0, then the Ascii Boolean will be set.  This should be
    422   maintained and not changed for all operations with the same file.
    423 
    424   @param[in]       Handle        FileHandle to read from.
    425   @param[in, out]  Ascii         Boolean value for indicating whether the file is
    426                                  Ascii (TRUE) or UCS2 (FALSE).
    427 
    428   @return                       The line of text from the file.
    429 
    430   @sa FileHandleReadLine
    431 **/
    432 CHAR16*
    433 EFIAPI
    434 FileHandleReturnLine(
    435   IN EFI_FILE_HANDLE            Handle,
    436   IN OUT BOOLEAN                *Ascii
    437   );
    438 
    439 /**
    440   Function to write a line of text to a file.
    441 
    442   If the file is a Unicode file (with UNICODE file tag) then write the unicode
    443   text.
    444   If the file is an ASCII file then write the ASCII text.
    445   If the size of file is zero (without file tag at the beginning) then write
    446   ASCII text as default.
    447 
    448   @param[in]     Handle         FileHandle to write to.
    449   @param[in]     Buffer         Buffer to write, if NULL the function will
    450                                 take no action and return EFI_SUCCESS.
    451 
    452   @retval  EFI_SUCCESS            The data was written.
    453                                   Buffer is NULL.
    454   @retval  EFI_INVALID_PARAMETER  Handle is NULL.
    455   @retval  EFI_OUT_OF_RESOURCES   Unable to allocate temporary space for ASCII
    456                                   string due to out of resources.
    457 
    458   @sa FileHandleWrite
    459 **/
    460 EFI_STATUS
    461 EFIAPI
    462 FileHandleWriteLine(
    463   IN EFI_FILE_HANDLE Handle,
    464   IN CHAR16          *Buffer
    465   );
    466 
    467 /**
    468   Function to take a formatted argument and print it to a file.
    469 
    470   @param[in] Handle   The file handle for the file to write to.
    471   @param[in] Format   The format argument (see printlib for the format specifier).
    472   @param[in] ...      The variable arguments for the format.
    473 
    474   @retval EFI_SUCCESS The operation was successful.
    475   @retval other       A return value from FileHandleWriteLine.
    476 
    477   @sa FileHandleWriteLine
    478 **/
    479 EFI_STATUS
    480 EFIAPI
    481 FileHandlePrintLine(
    482   IN EFI_FILE_HANDLE  Handle,
    483   IN CONST CHAR16     *Format,
    484   ...
    485   );
    486 
    487 /**
    488   Function to determine if a FILE_HANDLE is at the end of the file.
    489 
    490   This will NOT work on directories.
    491 
    492   If Handle is NULL, then ASSERT().
    493 
    494   @param[in] Handle     The file handle.
    495 
    496   @retval TRUE          The position is at the end of the file.
    497   @retval FALSE         The position is not at the end of the file.
    498 **/
    499 BOOLEAN
    500 EFIAPI
    501 FileHandleEof(
    502   IN EFI_FILE_HANDLE Handle
    503   );
    504 
    505 #endif //_FILE_HANDLE_LIBRARY_HEADER_
    506 
    507