2 Provides interface to EFI_FILE_HANDLE functionality.
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
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_FILE_INFO
*pFileInfo
;
54 // ASSERT if FileHandle is NULL
56 ASSERT (FileHandle
!= NULL
);
59 // Get the required size to allocate
63 Status
= FileHandle
->GetInfo(FileHandle
,
68 // error is expected. getting size to allocate
70 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
71 pFileInfo
= AllocateZeroPool(FileInfoSize
);
72 ASSERT (pFileInfo
!= NULL
);
74 // now get the information
76 Status
= FileHandle
->GetInfo(FileHandle
,
81 // if we got an error free the memory and return NULL
83 if (EFI_ERROR(Status
)) {
91 This function will set the information about the file for the opened handle
94 @param FileHandle The file handle of the file for which information
97 @param FileInfo The infotmation to set.
99 @retval EFI_SUCCESS The information was set.
100 @retval EFI_UNSUPPORTED The InformationType is not known.
101 @retval EFI_NO_MEDIA The device has no medium.
102 @retval EFI_DEVICE_ERROR The device reported an error.
103 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
104 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
105 @retval EFI_ACCESS_DENIED The file was opened read only.
106 @retval EFI_VOLUME_FULL The volume is full.
111 IN EFI_FILE_HANDLE FileHandle
,
112 IN CONST EFI_FILE_INFO
*FileInfo
117 // ASSERT if the FileHandle or FileInfo is NULL
119 ASSERT (FileHandle
!= NULL
);
120 ASSERT (FileInfo
!= NULL
);
125 return (FileHandle
->SetInfo(FileHandle
,
127 (UINTN
)FileInfo
->Size
,
128 (EFI_FILE_INFO
*)FileInfo
));
132 This function reads information from an opened file.
134 If FileHandle is not a directory, the function reads the requested number of
135 bytes from the file at the file's current position and returns them in Buffer.
136 If the read goes beyond the end of the file, the read length is truncated to the
137 end of the file. The file's current position is increased by the number of bytes
138 returned. If FileHandle is a directory, the function reads the directory entry
139 at the file's current position and returns the entry in Buffer. If the Buffer
140 is not large enough to hold the current directory entry, then
141 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
142 BufferSize is set to be the size of the buffer needed to read the entry. On
143 success, the current position is updated to the next directory entry. If there
144 are no more directory entries, the read returns a zero-length buffer.
145 EFI_FILE_INFO is the structure returned as the directory entry.
147 @param FileHandle the opened file handle
148 @param BufferSize on input the size of buffer in bytes. on return
149 the number of bytes written.
150 @param Buffer the buffer to put read data into.
152 @retval EFI_SUCCESS Data was read.
153 @retval EFI_NO_MEDIA The device has no media.
154 @retval EFI_DEVICE_ERROR The device reported an error.
155 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
156 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
163 IN EFI_FILE_HANDLE FileHandle
,
164 IN OUT UINTN
*BufferSize
,
169 // ASSERT if FileHandle is NULL
171 ASSERT (FileHandle
!= NULL
);
174 // Perform the read based on EFI_FILE_PROTOCOL
176 return (FileHandle
->Read(FileHandle
, BufferSize
, Buffer
));
181 Write data to a file.
183 This function writes the specified number of bytes to the file at the current
184 file position. The current file position is advanced the actual number of bytes
185 written, which is returned in BufferSize. Partial writes only occur when there
186 has been a data error during the write attempt (such as "volume space full").
187 The file is automatically grown to hold the data if required. Direct writes to
188 opened directories are not supported.
190 @param FileHandle The opened file for writing
191 @param BufferSize on input the number of bytes in Buffer. On output
192 the number of bytes written.
193 @param Buffer the buffer containing data to write is stored.
195 @retval EFI_SUCCESS Data was written.
196 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
197 @retval EFI_NO_MEDIA The device has no media.
198 @retval EFI_DEVICE_ERROR The device reported an error.
199 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
200 @retval EFI_WRITE_PROTECTED The device is write-protected.
201 @retval EFI_ACCESS_DENIED The file was open for read only.
202 @retval EFI_VOLUME_FULL The volume is full.
207 IN EFI_FILE_HANDLE FileHandle
,
208 IN OUT UINTN
*BufferSize
,
213 // ASSERT if FileHandle is NULL
215 ASSERT (FileHandle
!= NULL
);
217 // Perform the write based on EFI_FILE_PROTOCOL
219 return (FileHandle
->Write(FileHandle
, BufferSize
, Buffer
));
223 Close an open file handle.
225 This function closes a specified file handle. All "dirty" cached file data is
226 flushed to the device, and the file is closed. In all cases the handle is
229 @param FileHandle the file handle to close.
231 @retval EFI_SUCCESS the file handle was closed sucessfully.
236 IN EFI_FILE_HANDLE FileHandle
241 // ASSERT if FileHandle is NULL
243 ASSERT (FileHandle
!= NULL
);
245 // Perform the Close based on EFI_FILE_PROTOCOL
247 Status
= FileHandle
->Close(FileHandle
);
252 Delete a file and close the handle
254 This function closes and deletes a file. In all cases the file handle is closed.
255 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
256 returned, but the handle is still closed.
258 @param FileHandle the file handle to delete
260 @retval EFI_SUCCESS the file was closed sucessfully
261 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
263 @retval INVALID_PARAMETER One of the parameters has an invalid value.
268 IN EFI_FILE_HANDLE FileHandle
273 // ASSERT if FileHandle is NULL
275 ASSERT (FileHandle
!= NULL
);
277 // Perform the Delete based on EFI_FILE_PROTOCOL
279 Status
= FileHandle
->Delete(FileHandle
);
284 Set the current position in a file.
286 This function sets the current file position for the handle to the position
287 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
288 absolute positioning is supported, and seeking past the end of the file is
289 allowed (a subsequent write would grow the file). Seeking to position
290 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
291 If FileHandle is a directory, the only position that may be set is zero. This
292 has the effect of starting the read process of the directory entries over.
294 @param FileHandle The file handle on which the position is being set
295 @param Position Byte position from begining of file
297 @retval EFI_SUCCESS Operation completed sucessfully.
298 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
300 @retval INVALID_PARAMETER One of the parameters has an invalid value.
304 FileHandleSetPosition (
305 IN EFI_FILE_HANDLE FileHandle
,
310 // ASSERT if FileHandle is NULL
312 ASSERT (FileHandle
!= NULL
);
314 // Perform the SetPosition based on EFI_FILE_PROTOCOL
316 return (FileHandle
->SetPosition(FileHandle
, Position
));
320 Gets a file's current position
322 This function retrieves the current file position for the file handle. For
323 directories, the current file position has no meaning outside of the file
324 system driver and as such the operation is not supported. An error is returned
325 if FileHandle is a directory.
327 @param FileHandle The open file handle on which to get the position.
328 @param Position Byte position from begining of file.
330 @retval EFI_SUCCESS the operation completed sucessfully.
331 @retval INVALID_PARAMETER One of the parameters has an invalid value.
332 @retval EFI_UNSUPPORTED the request is not valid on directories.
336 FileHandleGetPosition (
337 IN EFI_FILE_HANDLE FileHandle
,
342 // ASSERT if FileHandle is NULL
344 ASSERT (FileHandle
!= NULL
);
346 // Perform the GetPosition based on EFI_FILE_PROTOCOL
348 return (FileHandle
->GetPosition(FileHandle
, Position
));
351 Flushes data on a file
353 This function flushes all modified data associated with a file to a device.
355 @param FileHandle The file handle on which to flush data
357 @retval EFI_SUCCESS The data was flushed.
358 @retval EFI_NO_MEDIA The device has no media.
359 @retval EFI_DEVICE_ERROR The device reported an error.
360 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
361 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
362 @retval EFI_ACCESS_DENIED The file was opened for read only.
367 IN EFI_FILE_HANDLE FileHandle
371 // ASSERT if FileHandle is NULL
373 ASSERT (FileHandle
!= NULL
);
375 // Perform the Flush based on EFI_FILE_PROTOCOL
377 return (FileHandle
->Flush(FileHandle
));
381 function to determine if a given handle is a directory handle
383 if DirHandle is NULL then ASSERT()
385 open the file information on the DirHandle and verify that the Attribute
386 includes EFI_FILE_DIRECTORY bit set.
388 @param DirHandle Handle to open file
390 @retval EFI_SUCCESS DirHandle is a directory
391 @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available
392 @retval EFI_NOT_FOUND DirHandle is not a directory
396 FileHandleIsDirectory (
397 IN EFI_FILE_HANDLE DirHandle
400 EFI_FILE_INFO
*DirInfo
;
403 // ASSERT if DirHandle is NULL
405 ASSERT(DirHandle
!= NULL
);
408 // get the file information for DirHandle
410 DirInfo
= FileHandleGetInfo (DirHandle
);
415 if (DirInfo
== NULL
) {
419 return (EFI_INVALID_PARAMETER
);
421 if ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
423 // Attributes say this is not a directory
426 return (EFI_NOT_FOUND
);
432 return (EFI_SUCCESS
);
436 Retrieves the first file from a directory
438 This function opens a directory and gets the first file's info in the
439 directory. Caller can use FileHandleFindNextFile() to get other files. When
440 complete the caller is responsible for calling FreePool() on Buffer.
442 @param DirHandle The file handle of the directory to search
443 @param Buffer Pointer to buffer for file's information
445 @retval EFI_SUCCESS Found the first file.
446 @retval EFI_NOT_FOUND Cannot find the directory.
447 @retval EFI_NO_MEDIA The device has no media.
448 @retval EFI_DEVICE_ERROR The device reported an error.
449 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
450 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
455 FileHandleFindFirstFile (
456 IN EFI_FILE_HANDLE DirHandle
,
457 OUT EFI_FILE_INFO
**Buffer
466 ASSERT (DirHandle
!= NULL
);
467 ASSERT (Buffer
!= NULL
);
470 // verify that DirHandle is a directory
472 Status
= FileHandleIsDirectory(DirHandle
);
473 if (EFI_ERROR(Status
)) {
478 // reset to the begining of the directory
480 Status
= FileHandleSetPosition(DirHandle
, 0);
481 if (EFI_ERROR(Status
)) {
486 // Allocate a buffer sized to struct size + enough for the string at the end
488 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
489 *Buffer
= AllocateZeroPool(BufferSize
);
490 ASSERT (*Buffer
!= NULL
);
493 // read in the info about the first file
495 Status
= FileHandleRead (DirHandle
, &BufferSize
, *Buffer
);
496 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
497 if (EFI_ERROR(Status
)) {
502 return (EFI_SUCCESS
);
505 Retrieves the next file in a directory.
507 To use this function, caller must call the FileHandleFindFirstFile() to get the
508 first file, and then use this function get other files. This function can be
509 called for several times to get each file's information in the directory. If
510 the call of FileHandleFindNextFile() got the last file in the directory, the next
511 call of this function has no file to get. *NoFile will be set to TRUE and the
512 Buffer memory will be automatically freed.
514 @param DirHandle the file handle of the directory
515 @param Buffer pointer to buffer for file's information
516 @param NoFile pointer to boolean when last file is found
518 @retval EFI_SUCCESS Found the next file, or reached last file
519 @retval EFI_NO_MEDIA The device has no media.
520 @retval EFI_DEVICE_ERROR The device reported an error.
521 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
525 FileHandleFindNextFile(
526 IN EFI_FILE_HANDLE DirHandle
,
527 OUT EFI_FILE_INFO
*Buffer
,
535 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL
537 ASSERT (DirHandle
!= NULL
);
538 ASSERT (Buffer
!= NULL
);
539 ASSERT (NoFile
!= NULL
);
542 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
544 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
547 // read in the info about the next file
549 Status
= FileHandleRead (DirHandle
, &BufferSize
, Buffer
);
550 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
551 if (EFI_ERROR(Status
)) {
556 // If we read 0 bytes (but did not have erros) we already read in the last file.
558 if (BufferSize
== 0) {
563 return (EFI_SUCCESS
);
566 Retrieve the size of a file.
568 if FileHandle is NULL then ASSERT()
569 if Size is NULL then ASSERT()
571 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
574 @param FileHandle file handle from which size is retrieved
575 @param Size pointer to size
577 @retval EFI_SUCCESS operation was completed sucessfully
578 @retval EFI_DEVICE_ERROR cannot access the file
583 IN EFI_FILE_HANDLE FileHandle
,
587 EFI_FILE_INFO
*FileInfo
;
590 // ASSERT for FileHandle or Size being NULL
592 ASSERT (FileHandle
!= NULL
);
593 ASSERT (Size
!= NULL
);
596 // get the FileInfo structure
598 FileInfo
= FileHandleGetInfo(FileHandle
);
599 if (FileInfo
== NULL
) {
600 return (EFI_DEVICE_ERROR
);
604 // Assign the Size pointer to the correct value
606 *Size
= FileInfo
->FileSize
;
609 // free the FileInfo memory
613 return (EFI_SUCCESS
);
618 Safely append (on the left) with automatic string resizing given length of Destination and
619 desired length of copy from Source.
621 append the first D characters of Source to the end of Destination, where D is
622 the lesser of Count and the StrLen() of Source. If appending those D characters
623 will fit within Destination (whose Size is given as CurrentSize) and
624 still leave room for a null terminator, then those characters are appended,
625 starting at the original terminating null of Destination, and a new terminating
628 If appending D characters onto Destination will result in a overflow of the size
629 given in CurrentSize the string will be grown such that the copy can be performed
630 and CurrentSize will be updated to the new size.
632 If Source is NULL, there is nothing to append, just return the current buffer in
635 if Destination is NULL, then ASSERT()
636 if Destination's current length (including NULL terminator) is already more then
637 CurrentSize, then ASSERT()
639 @param[in,out] Destination The String to append onto
640 @param[in,out] CurrentSize on call the number of bytes in Destination. On
641 return possibly the new size (still in bytes). if NULL
642 then allocate whatever is needed.
643 @param[in] Source The String to append from
644 @param[in] Count Maximum number of characters to append. if 0 then
647 @return Destination return the resultant string.
652 IN OUT CHAR16
**Destination
,
653 IN OUT UINTN
*CurrentSize
,
654 IN CONST CHAR16
*Source
,
657 UINTN DestinationStartSize
;
664 ASSERT(Destination
!= NULL
);
667 // If there's nothing to do then just return Destination
669 if (Source
== NULL
) {
670 return (*Destination
);
674 // allow for NULL pointers address as Destination
676 if (*Destination
!= NULL
) {
677 ASSERT(CurrentSize
!= 0);
678 DestinationStartSize
= StrSize(*Destination
);
679 ASSERT(DestinationStartSize
<= *CurrentSize
);
681 DestinationStartSize
= 0;
682 // ASSERT(*CurrentSize == 0);
686 // Append all of Source?
689 Count
= StrSize(Source
);
693 // Test and grow if required
695 if (CurrentSize
!= NULL
) {
696 NewSize
= *CurrentSize
;
697 while (NewSize
< (DestinationStartSize
+ Count
)) {
698 NewSize
+= 2 * Count
;
700 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
701 *CurrentSize
= NewSize
;
703 *Destination
= AllocateZeroPool(Count
+sizeof(CHAR16
));
706 CopySize
= StrSize(*Destination
);
707 CopyMem((*Destination
)+((Count
-2)/sizeof(CHAR16
)), *Destination
, CopySize
);
708 CopyMem(*Destination
, Source
, Count
-2);
709 return (*Destination
);
713 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
716 if Handle is NULL, return EFI_INVALID_PARAMETER
718 @param[in] Handle Handle to the Directory or File to create path to.
719 @param[out] FullFileName pointer to pointer to generated full file name. It
720 is the responsibility of the caller to free this memory
721 with a call to FreePool().
722 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
723 @retval EFI_INVALID_PARAMETER Handle was NULL.
724 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
725 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
729 FileHandleGetFileName (
730 IN CONST EFI_FILE_HANDLE Handle
,
731 OUT CHAR16
**FullFileName
735 EFI_FILE_HANDLE CurrentHandle
;
736 EFI_FILE_HANDLE NextHigherHandle
;
737 EFI_FILE_INFO
*FileInfo
;
742 // Check our parameters
744 if (FullFileName
== NULL
|| Handle
== NULL
) {
745 return (EFI_INVALID_PARAMETER
);
748 *FullFileName
= NULL
;
750 Status
= Handle
->Open(Handle
, &CurrentHandle
, L
".", EFI_FILE_MODE_READ
, 0);
751 if (!EFI_ERROR(Status
)) {
753 // Reverse out the current directory on the device
756 FileInfo
= FileHandleGetInfo(CurrentHandle
);
757 if (FileInfo
== NULL
) {
758 Status
= EFI_OUT_OF_RESOURCES
;
762 // We got info... do we have a name? if yes preceed the current path with it...
764 if (StrLen (FileInfo
->FileName
) == 0) {
765 if (*FullFileName
== NULL
) {
766 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
771 if (*FullFileName
== NULL
) {
772 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
774 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, FileInfo
->FileName
, 0);
775 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
780 // Move to the parent directory
782 Status
= CurrentHandle
->Open (CurrentHandle
, &NextHigherHandle
, L
"..", EFI_FILE_MODE_READ
, 0);
783 if (EFI_ERROR (Status
)) {
787 FileHandleClose(CurrentHandle
);
788 CurrentHandle
= NextHigherHandle
;
792 if (CurrentHandle
!= NULL
) {
793 CurrentHandle
->Close (CurrentHandle
);
796 if (EFI_ERROR(Status
) && *FullFileName
!= NULL
) {
797 FreePool(*FullFileName
);
804 Function to read a single line (up to but not including the \n) from a file.
806 @param[in] Handle FileHandle to read from
807 @param[in,out] Buffer pointer to buffer to read into
808 @param[in,out] Size pointer to number of bytes in buffer
809 @param[in[ Truncate if TRUE then allows for truncation of the line to fit.
810 if FALSE will reset the position to the begining of the
811 line if the buffer is not large enough.
813 @retval EFI_SUCCESS the operation was sucessful. the line is stored in
815 @retval EFI_INVALID_PARAMETER Handle was NULL.
816 @retval EFI_INVALID_PARAMETER Buffer was NULL.
817 @retval EFI_INVALID_PARAMETER Size was NULL.
818 @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line.
819 Size was updated to minimum space required.
825 IN EFI_FILE_HANDLE Handle
,
841 return (EFI_INVALID_PARAMETER
);
843 FileHandleGetPosition(Handle
, &Position
);
845 for (CountSoFar
= 0;;CountSoFar
++){
846 CharSize
= sizeof(CharBuffer
);
847 Status
= FileHandleRead(Handle
, &CharSize
, &CharBuffer
);
848 if ( EFI_ERROR(Status
)
850 || CharBuffer
== '\n'
855 // if we have space save it...
857 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
858 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
859 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
864 // if we ran out of space tell when...
866 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
867 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
868 if (Truncate
== FALSE
) {
869 FileHandleSetPosition(Handle
, Position
);
871 DEBUG((DEBUG_WARN
, "The line was truncated in ReadLine"));
873 return (EFI_BUFFER_TOO_SMALL
);
875 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
880 function to write a line of unicode text to a file.
882 if Handle is NULL, ASSERT.
883 if Buffer is NULL, do nothing. (return SUCCESS)
885 @param[in] Handle FileHandle to write to
886 @param[in] Buffer Buffer to write
888 @retval EFI_SUCCESS the data was written.
889 @retval other failure.
896 IN EFI_FILE_HANDLE Handle
,
902 ASSERT(Handle
!= NULL
);
904 if (Buffer
== NULL
) {
905 return (EFI_SUCCESS
);
908 Size
= StrLen(Buffer
);
909 Status
= FileHandleWrite(Handle
, &Size
, Buffer
);
910 if (EFI_ERROR(Status
)) {
913 Size
= StrLen(L
"\r\n");
914 return FileHandleWrite(Handle
, &Size
, L
"\r\n");