2 Provides interface to EFI_FILE_HANDLE functionality.
4 Copyright (c) 2006 - 2009, Intel Corporation
5 All rights reserved. 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
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.
17 #include <Protocol/SimpleFileSystem.h>
19 #include <Guid/FileInfo.h>
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
26 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
27 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
30 This function will retrieve the information about the file for the handle
31 specified and store it in allocated pool memory.
33 This function allocates a buffer to store the file's information. It is the
34 caller's responsibility to free the buffer
36 @param FileHandle The file handle of the file for which information is
39 @retval NULL information could not be retrieved.
41 @return the information about the file
46 IN EFI_FILE_HANDLE FileHandle
49 EFI_GUID FileInfoGuid
;
50 EFI_FILE_INFO
*pFileInfo
;
55 // ASSERT if FileHandle is NULL
57 ASSERT (FileHandle
!= NULL
);
60 // Get the required size to allocate
62 FileInfoGuid
= gEfiFileInfoGuid
;
65 Status
= FileHandle
->GetInfo(FileHandle
,
70 // error is expected. getting size to allocate
72 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
73 pFileInfo
= AllocateZeroPool(FileInfoSize
);
74 ASSERT (pFileInfo
!= NULL
);
76 // now get the information
78 Status
= FileHandle
->GetInfo(FileHandle
,
83 // if we got an error free the memory and return NULL
85 if (EFI_ERROR(Status
)) {
93 This function will set the information about the file for the opened handle
96 @param FileHandle The file handle of the file for which information
99 @param FileInfo The infotmation to set.
101 @retval EFI_SUCCESS The information was set.
102 @retval EFI_UNSUPPORTED The InformationType is not known.
103 @retval EFI_NO_MEDIA The device has no medium.
104 @retval EFI_DEVICE_ERROR The device reported an error.
105 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
106 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
107 @retval EFI_ACCESS_DENIED The file was opened read only.
108 @retval EFI_VOLUME_FULL The volume is full.
113 IN EFI_FILE_HANDLE FileHandle
,
114 IN CONST EFI_FILE_INFO
*FileInfo
117 EFI_GUID FileInfoGuid
;
120 // ASSERT if the FileHandle or FileInfo is NULL
122 ASSERT (FileHandle
!= NULL
);
123 ASSERT (FileInfo
!= NULL
);
125 FileInfoGuid
= gEfiFileInfoGuid
;
129 return (FileHandle
->SetInfo(FileHandle
,
131 (UINTN
)FileInfo
->Size
,
132 (EFI_FILE_INFO
*)FileInfo
));
136 This function reads information from an opened file.
138 If FileHandle is not a directory, the function reads the requested number of
139 bytes from the file at the file's current position and returns them in Buffer.
140 If the read goes beyond the end of the file, the read length is truncated to the
141 end of the file. The file's current position is increased by the number of bytes
142 returned. If FileHandle is a directory, the function reads the directory entry
143 at the file's current position and returns the entry in Buffer. If the Buffer
144 is not large enough to hold the current directory entry, then
145 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
146 BufferSize is set to be the size of the buffer needed to read the entry. On
147 success, the current position is updated to the next directory entry. If there
148 are no more directory entries, the read returns a zero-length buffer.
149 EFI_FILE_INFO is the structure returned as the directory entry.
151 @param FileHandle the opened file handle
152 @param BufferSize on input the size of buffer in bytes. on return
153 the number of bytes written.
154 @param Buffer the buffer to put read data into.
156 @retval EFI_SUCCESS Data was read.
157 @retval EFI_NO_MEDIA The device has no media.
158 @retval EFI_DEVICE_ERROR The device reported an error.
159 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
160 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
167 IN EFI_FILE_HANDLE FileHandle
,
168 IN OUT UINTN
*BufferSize
,
173 // ASSERT if FileHandle is NULL
175 ASSERT (FileHandle
!= NULL
);
178 // Perform the read based on EFI_FILE_PROTOCOL
180 return (FileHandle
->Read(FileHandle
, BufferSize
, Buffer
));
185 Write data to a file.
187 This function writes the specified number of bytes to the file at the current
188 file position. The current file position is advanced the actual number of bytes
189 written, which is returned in BufferSize. Partial writes only occur when there
190 has been a data error during the write attempt (such as "volume space full").
191 The file is automatically grown to hold the data if required. Direct writes to
192 opened directories are not supported.
194 @param FileHandle The opened file for writing
195 @param BufferSize on input the number of bytes in Buffer. On output
196 the number of bytes written.
197 @param Buffer the buffer containing data to write is stored.
199 @retval EFI_SUCCESS Data was written.
200 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
201 @retval EFI_NO_MEDIA The device has no media.
202 @retval EFI_DEVICE_ERROR The device reported an error.
203 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
204 @retval EFI_WRITE_PROTECTED The device is write-protected.
205 @retval EFI_ACCESS_DENIED The file was open for read only.
206 @retval EFI_VOLUME_FULL The volume is full.
211 IN EFI_FILE_HANDLE FileHandle
,
212 IN OUT UINTN
*BufferSize
,
217 // ASSERT if FileHandle is NULL
219 ASSERT (FileHandle
!= NULL
);
221 // Perform the write based on EFI_FILE_PROTOCOL
223 return (FileHandle
->Write(FileHandle
, BufferSize
, Buffer
));
227 Close an open file handle.
229 This function closes a specified file handle. All "dirty" cached file data is
230 flushed to the device, and the file is closed. In all cases the handle is
233 @param FileHandle the file handle to close.
235 @retval EFI_SUCCESS the file handle was closed sucessfully.
240 IN EFI_FILE_HANDLE FileHandle
245 // ASSERT if FileHandle is NULL
247 ASSERT (FileHandle
!= NULL
);
249 // Perform the Close based on EFI_FILE_PROTOCOL
251 Status
= FileHandle
->Close(FileHandle
);
256 Delete a file and close the handle
258 This function closes and deletes a file. In all cases the file handle is closed.
259 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
260 returned, but the handle is still closed.
262 @param FileHandle the file handle to delete
264 @retval EFI_SUCCESS the file was closed sucessfully
265 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
267 @retval INVALID_PARAMETER One of the parameters has an invalid value.
272 IN EFI_FILE_HANDLE FileHandle
277 // ASSERT if FileHandle is NULL
279 ASSERT (FileHandle
!= NULL
);
281 // Perform the Delete based on EFI_FILE_PROTOCOL
283 Status
= FileHandle
->Delete(FileHandle
);
288 Set the current position in a file.
290 This function sets the current file position for the handle to the position
291 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
292 absolute positioning is supported, and seeking past the end of the file is
293 allowed (a subsequent write would grow the file). Seeking to position
294 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
295 If FileHandle is a directory, the only position that may be set is zero. This
296 has the effect of starting the read process of the directory entries over.
298 @param FileHandle The file handle on which the position is being set
299 @param Position Byte position from begining of file
301 @retval EFI_SUCCESS Operation completed sucessfully.
302 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
304 @retval INVALID_PARAMETER One of the parameters has an invalid value.
308 FileHandleSetPosition (
309 IN EFI_FILE_HANDLE FileHandle
,
314 // ASSERT if FileHandle is NULL
316 ASSERT (FileHandle
!= NULL
);
318 // Perform the SetPosition based on EFI_FILE_PROTOCOL
320 return (FileHandle
->SetPosition(FileHandle
, Position
));
324 Gets a file's current position
326 This function retrieves the current file position for the file handle. For
327 directories, the current file position has no meaning outside of the file
328 system driver and as such the operation is not supported. An error is returned
329 if FileHandle is a directory.
331 @param FileHandle The open file handle on which to get the position.
332 @param Position Byte position from begining of file.
334 @retval EFI_SUCCESS the operation completed sucessfully.
335 @retval INVALID_PARAMETER One of the parameters has an invalid value.
336 @retval EFI_UNSUPPORTED the request is not valid on directories.
340 FileHandleGetPosition (
341 IN EFI_FILE_HANDLE FileHandle
,
346 // ASSERT if FileHandle is NULL
348 ASSERT (FileHandle
!= NULL
);
350 // Perform the GetPosition based on EFI_FILE_PROTOCOL
352 return (FileHandle
->GetPosition(FileHandle
, Position
));
355 Flushes data on a file
357 This function flushes all modified data associated with a file to a device.
359 @param FileHandle The file handle on which to flush data
361 @retval EFI_SUCCESS The data was flushed.
362 @retval EFI_NO_MEDIA The device has no media.
363 @retval EFI_DEVICE_ERROR The device reported an error.
364 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
365 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
366 @retval EFI_ACCESS_DENIED The file was opened for read only.
371 IN EFI_FILE_HANDLE FileHandle
375 // ASSERT if FileHandle is NULL
377 ASSERT (FileHandle
!= NULL
);
379 // Perform the Flush based on EFI_FILE_PROTOCOL
381 return (FileHandle
->Flush(FileHandle
));
385 function to determine if a given handle is a directory handle
387 if DirHandle is NULL then ASSERT()
389 open the file information on the DirHandle and verify that the Attribute
390 includes EFI_FILE_DIRECTORY bit set.
392 @param DirHandle Handle to open file
394 @retval EFI_SUCCESS DirHandle is a directory
395 @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available
396 @retval EFI_NOT_FOUND DirHandle is not a directory
400 FileHandleIsDirectory (
401 IN EFI_FILE_HANDLE DirHandle
404 EFI_FILE_INFO
*DirInfo
;
407 // ASSERT if DirHandle is NULL
409 ASSERT(DirHandle
!= NULL
);
412 // get the file information for DirHandle
414 DirInfo
= FileHandleGetInfo (DirHandle
);
419 if (DirInfo
== NULL
) {
423 return (EFI_INVALID_PARAMETER
);
425 if ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
427 // Attributes say this is not a directory
430 return (EFI_NOT_FOUND
);
436 return (EFI_SUCCESS
);
440 Retrieves the first file from a directory
442 This function opens a directory and gets the first file's info in the
443 directory. Caller can use FileHandleFindNextFile() to get other files. When
444 complete the caller is responsible for calling FreePool() on Buffer.
446 @param DirHandle The file handle of the directory to search
447 @param Buffer Pointer to buffer for file's information
449 @retval EFI_SUCCESS Found the first file.
450 @retval EFI_NOT_FOUND Cannot find the directory.
451 @retval EFI_NO_MEDIA The device has no media.
452 @retval EFI_DEVICE_ERROR The device reported an error.
453 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
454 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
459 FileHandleFindFirstFile (
460 IN EFI_FILE_HANDLE DirHandle
,
461 OUT EFI_FILE_INFO
**Buffer
470 ASSERT (DirHandle
!= NULL
);
471 ASSERT (Buffer
!= NULL
);
474 // verify that DirHandle is a directory
476 Status
= FileHandleIsDirectory(DirHandle
);
477 if (EFI_ERROR(Status
)) {
482 // reset to the begining of the directory
484 Status
= FileHandleSetPosition(DirHandle
, 0);
485 if (EFI_ERROR(Status
)) {
490 // Allocate a buffer sized to struct size + enough for the string at the end
492 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
493 *Buffer
= AllocateZeroPool(BufferSize
);
494 ASSERT (*Buffer
!= NULL
);
497 // read in the info about the first file
499 Status
= FileHandleRead (DirHandle
, &BufferSize
, *Buffer
);
500 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
501 if (EFI_ERROR(Status
)) {
506 return (EFI_SUCCESS
);
509 Retrieves the next file in a directory.
511 To use this function, caller must call the FileHandleFindFirstFile() to get the
512 first file, and then use this function get other files. This function can be
513 called for several times to get each file's information in the directory. If
514 the call of FileHandleFindNextFile() got the last file in the directory, the next
515 call of this function has no file to get. *NoFile will be set to TRUE and the
516 Buffer memory will be automatically freed.
518 @param DirHandle the file handle of the directory
519 @param Buffer pointer to buffer for file's information
520 @param NoFile pointer to boolean when last file is found
522 @retval EFI_SUCCESS Found the next file, or reached last file
523 @retval EFI_NO_MEDIA The device has no media.
524 @retval EFI_DEVICE_ERROR The device reported an error.
525 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
529 FileHandleFindNextFile(
530 IN EFI_FILE_HANDLE DirHandle
,
531 OUT EFI_FILE_INFO
*Buffer
,
539 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL
541 ASSERT (DirHandle
!= NULL
);
542 ASSERT (Buffer
!= NULL
);
543 ASSERT (NoFile
!= NULL
);
546 // verify that DirHandle is a directory
548 Status
= FileHandleIsDirectory(DirHandle
);
549 if (EFI_ERROR(Status
)) {
554 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
556 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
559 // read in the info about the next file
561 Status
= FileHandleRead (DirHandle
, &BufferSize
, Buffer
);
562 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
563 if (EFI_ERROR(Status
)) {
568 // If we read 0 bytes (but did not have erros) we already read in the last file.
570 if (BufferSize
== 0) {
575 return (EFI_SUCCESS
);
578 Retrieve the size of a file.
580 if FileHandle is NULL then ASSERT()
581 if Size is NULL then ASSERT()
583 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
586 @param FileHandle file handle from which size is retrieved
587 @param Size pointer to size
589 @retval EFI_SUCCESS operation was completed sucessfully
590 @retval EFI_DEVICE_ERROR cannot access the file
595 IN EFI_FILE_HANDLE FileHandle
,
599 EFI_FILE_INFO
*FileInfo
;
602 // ASSERT for FileHandle or Size being NULL
604 ASSERT (FileHandle
!= NULL
);
605 ASSERT (Size
!= NULL
);
608 // get the FileInfo structure
610 FileInfo
= FileHandleGetInfo(FileHandle
);
611 if (FileInfo
== NULL
) {
612 return (EFI_DEVICE_ERROR
);
616 // Assign the Size pointer to the correct value
618 *Size
= FileInfo
->FileSize
;
621 // free the FileInfo memory
625 return (EFI_SUCCESS
);
630 Safely append (on the left) with automatic string resizing given length of Destination and
631 desired length of copy from Source.
633 append the first D characters of Source to the end of Destination, where D is
634 the lesser of Count and the StrLen() of Source. If appending those D characters
635 will fit within Destination (whose Size is given as CurrentSize) and
636 still leave room for a null terminator, then those characters are appended,
637 starting at the original terminating null of Destination, and a new terminating
640 If appending D characters onto Destination will result in a overflow of the size
641 given in CurrentSize the string will be grown such that the copy can be performed
642 and CurrentSize will be updated to the new size.
644 If Source is NULL, there is nothing to append, just return the current buffer in
647 if Destination is NULL, then ASSERT()
648 if Destination's current length (including NULL terminator) is already more then
649 CurrentSize, then ASSERT()
651 @param[in,out] Destination The String to append onto
652 @param[in,out] CurrentSize on call the number of bytes in Destination. On
653 return possibly the new size (still in bytes). if NULL
654 then allocate whatever is needed.
655 @param[in] Source The String to append from
656 @param[in] Count Maximum number of characters to append. if 0 then
659 @return Destination return the resultant string.
664 IN OUT CHAR16
**Destination
,
665 IN OUT UINTN
*CurrentSize
,
666 IN CONST CHAR16
*Source
,
669 UINTN DestinationStartSize
;
676 ASSERT(Destination
!= NULL
);
679 // If there's nothing to do then just return Destination
681 if (Source
== NULL
) {
682 return (*Destination
);
686 // allow for NULL pointers address as Destination
688 if (*Destination
!= NULL
) {
689 ASSERT(CurrentSize
!= 0);
690 DestinationStartSize
= StrSize(*Destination
);
691 ASSERT(DestinationStartSize
<= *CurrentSize
);
693 DestinationStartSize
= 0;
694 // ASSERT(*CurrentSize == 0);
698 // Append all of Source?
701 Count
= StrLen(Source
);
705 // Test and grow if required
707 if (CurrentSize
!= NULL
) {
708 NewSize
= *CurrentSize
;
709 while (NewSize
< (DestinationStartSize
+ (Count
*sizeof(CHAR16
)))) {
710 NewSize
+= 2 * Count
* sizeof(CHAR16
);
712 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
713 *CurrentSize
= NewSize
;
715 *Destination
= AllocateZeroPool((Count
+1)*sizeof(CHAR16
));
718 CopySize
= StrSize(*Destination
);
719 *Destination
= CopyMem((*Destination
)+StrLen(Source
), *Destination
, CopySize
);
720 *Destination
= CopyMem(*Destination
, Source
, StrLen(Source
));
721 return (*Destination
);
725 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
728 if Handle is NULL, return EFI_INVALID_PARAMETER
730 @param[in] Handle Handle to the Directory or File to create path to.
731 @param[out] FullFileName pointer to pointer to generated full file name. It
732 is the responsibility of the caller to free this memory
733 with a call to FreePool().
734 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
735 @retval EFI_INVALID_PARAMETER Handle was NULL.
736 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
737 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
741 FileHandleGetFileName (
742 IN CONST EFI_FILE_HANDLE Handle
,
743 OUT CHAR16
**FullFileName
747 EFI_FILE_HANDLE CurrentHandle
;
748 EFI_FILE_HANDLE NextHigherHandle
;
749 EFI_FILE_INFO
*FileInfo
;
752 *FullFileName
= NULL
;
755 // Check our parameters
757 if (FullFileName
== NULL
|| Handle
== NULL
) {
758 return (EFI_INVALID_PARAMETER
);
761 Status
= Handle
->Open(Handle
, &CurrentHandle
, L
".", EFI_FILE_MODE_READ
, 0);
762 if (!EFI_ERROR(Status
)) {
764 // Reverse out the current directory on the device
767 FileInfo
= FileHandleGetInfo(CurrentHandle
);
768 if (FileInfo
== NULL
) {
769 Status
= EFI_OUT_OF_RESOURCES
;
773 // We got info... do we have a name? if yes preceed the current path with it...
775 if (StrLen (FileInfo
->FileName
) == 0) {
776 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
780 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, FileInfo
->FileName
, 0);
781 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
786 // Move to the parent directory
788 Status
= CurrentHandle
->Open (CurrentHandle
, &NextHigherHandle
, L
"..", EFI_FILE_MODE_READ
, 0);
789 if (EFI_ERROR (Status
)) {
793 FileHandleClose(CurrentHandle
);
794 CurrentHandle
= NextHigherHandle
;
798 if (CurrentHandle
!= NULL
) {
799 CurrentHandle
->Close (CurrentHandle
);
802 if (EFI_ERROR(Status
) && *FullFileName
!= NULL
) {
803 FreePool(*FullFileName
);
810 Function to read a single line (up to but not including the \n) from a file.
812 @param[in] Handle FileHandle to read from
813 @param[in,out] Buffer pointer to buffer to read into
814 @param[in,out] Size pointer to number of bytes in buffer
815 @param[in[ Truncate if TRUE then allows for truncation of the line to fit.
816 if FALSE will reset the position to the begining of the
817 line if the buffer is not large enough.
819 @retval EFI_SUCCESS the operation was sucessful. the line is stored in
821 @retval EFI_INVALID_PARAMETER Handle was NULL.
822 @retval EFI_INVALID_PARAMETER Buffer was NULL.
823 @retval EFI_INVALID_PARAMETER Size was NULL.
824 @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line.
825 Size was updated to minimum space required.
831 IN EFI_FILE_HANDLE Handle
,
847 return (EFI_INVALID_PARAMETER
);
849 FileHandleGetPosition(Handle
, &Position
);
851 for (CountSoFar
= 0;;CountSoFar
++){
852 CharSize
= sizeof(CharBuffer
);
853 Status
= FileHandleRead(Handle
, &CharSize
, &CharBuffer
);
854 if ( EFI_ERROR(Status
)
856 || CharBuffer
== '\n'
861 // if we have space save it...
863 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
864 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
865 ((CHAR16
*)Buffer
)[CountSoFar
+1] = '\0';
870 // if we ran out of space tell when...
872 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
873 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
874 if (Truncate
== FALSE
) {
875 FileHandleSetPosition(Handle
, Position
);
877 DEBUG((DEBUG_WARN
, "The line was truncated in ReadLine"));
879 return (EFI_BUFFER_TOO_SMALL
);
881 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
886 function to write a line of unicode text to a file.
888 if Handle is NULL, ASSERT.
889 if Buffer is NULL, do nothing. (return SUCCESS)
891 @param[in] Handle FileHandle to write to
892 @param[in] Buffer Buffer to write
894 @retval EFI_SUCCESS the data was written.
895 @retval other failure.
902 IN EFI_FILE_HANDLE Handle
,
908 ASSERT(Handle
!= NULL
);
910 if (Buffer
== NULL
) {
911 return (EFI_SUCCESS
);
914 Size
= StrLen(Buffer
);
915 Status
= FileHandleWrite(Handle
, &Size
, Buffer
);
916 if (EFI_ERROR(Status
)) {
919 Size
= StrLen(L
"\r\n");
920 return FileHandleWrite(Handle
, &Size
, L
"\r\n");