2 Provides interface to EFI_FILE_HANDLE functionality.
4 Copyright (c) 2006 - 2011, 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
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>
18 #include <Protocol/UnicodeCollation.h>
20 #include <Guid/FileInfo.h>
22 #include <Library/DebugLib.h>
23 #include <Library/MemoryAllocationLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/FileHandleLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/PrintLib.h>
30 CONST UINT16 gUnicodeFileTag
= EFI_UNICODE_BYTE_ORDER_MARK
;
32 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
33 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
36 This function will retrieve the information about the file for the handle
37 specified and store it in allocated pool memory.
39 This function allocates a buffer to store the file's information. It is the
40 caller's responsibility to free the buffer
42 @param FileHandle The file handle of the file for which information is
45 @retval NULL information could not be retrieved.
47 @return the information about the file
52 IN EFI_FILE_HANDLE FileHandle
55 EFI_FILE_INFO
*FileInfo
;
59 if (FileHandle
== NULL
) {
64 // Get the required size to allocate
68 Status
= FileHandle
->GetInfo(FileHandle
,
72 if (Status
== EFI_BUFFER_TOO_SMALL
){
74 // error is expected. getting size to allocate
76 FileInfo
= AllocateZeroPool(FileInfoSize
);
78 // now get the information
80 Status
= FileHandle
->GetInfo(FileHandle
,
85 // if we got an error free the memory and return NULL
87 if (EFI_ERROR(Status
)) {
96 This function sets the information about the file for the opened handle
99 @param[in] FileHandle The file handle of the file for which information
102 @param[in] FileInfo The information to set.
104 @retval EFI_SUCCESS The information was set.
105 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
106 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
107 @retval EFI_NO_MEDIA The device has no medium.
108 @retval EFI_DEVICE_ERROR The device reported an error.
109 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
110 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
111 @retval EFI_ACCESS_DENIED The file was opened read only.
112 @retval EFI_VOLUME_FULL The volume is full.
117 IN EFI_FILE_HANDLE FileHandle
,
118 IN CONST EFI_FILE_INFO
*FileInfo
123 // ASSERT if the FileHandle or FileInfo is NULL
125 ASSERT (FileHandle
!= NULL
);
126 ASSERT (FileInfo
!= NULL
);
131 return (FileHandle
->SetInfo(FileHandle
,
133 (UINTN
)FileInfo
->Size
,
134 (EFI_FILE_INFO
*)FileInfo
));
138 This function reads information from an opened file.
140 If FileHandle is not a directory, the function reads the requested number of
141 bytes from the file at the file's current position and returns them in Buffer.
142 If the read goes beyond the end of the file, the read length is truncated to the
143 end of the file. The file's current position is increased by the number of bytes
144 returned. If FileHandle is a directory, the function reads the directory entry
145 at the file's current position and returns the entry in Buffer. If the Buffer
146 is not large enough to hold the current directory entry, then
147 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
148 BufferSize is set to be the size of the buffer needed to read the entry. On
149 success, the current position is updated to the next directory entry. If there
150 are no more directory entries, the read returns a zero-length buffer.
151 EFI_FILE_INFO is the structure returned as the directory entry.
153 @param FileHandle the opened file handle
154 @param BufferSize on input the size of buffer in bytes. on return
155 the number of bytes written.
156 @param Buffer the buffer to put read data into.
158 @retval EFI_SUCCESS Data was read.
159 @retval EFI_NO_MEDIA The device has no media.
160 @retval EFI_DEVICE_ERROR The device reported an error.
161 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
162 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
169 IN EFI_FILE_HANDLE FileHandle
,
170 IN OUT UINTN
*BufferSize
,
175 // ASSERT if FileHandle is NULL
177 ASSERT (FileHandle
!= NULL
);
180 // Perform the read based on EFI_FILE_PROTOCOL
182 return (FileHandle
->Read(FileHandle
, BufferSize
, Buffer
));
187 Write data to a file.
189 This function writes the specified number of bytes to the file at the current
190 file position. The current file position is advanced the actual number of bytes
191 written, which is returned in BufferSize. Partial writes only occur when there
192 has been a data error during the write attempt (such as "volume space full").
193 The file is automatically grown to hold the data if required. Direct writes to
194 opened directories are not supported.
196 @param FileHandle The opened file for writing
197 @param BufferSize on input the number of bytes in Buffer. On output
198 the number of bytes written.
199 @param Buffer the buffer containing data to write is stored.
201 @retval EFI_SUCCESS Data was written.
202 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
203 @retval EFI_NO_MEDIA The device has no media.
204 @retval EFI_DEVICE_ERROR The device reported an error.
205 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
206 @retval EFI_WRITE_PROTECTED The device is write-protected.
207 @retval EFI_ACCESS_DENIED The file was open for read only.
208 @retval EFI_VOLUME_FULL The volume is full.
213 IN EFI_FILE_HANDLE FileHandle
,
214 IN OUT UINTN
*BufferSize
,
219 // ASSERT if FileHandle is NULL
221 ASSERT (FileHandle
!= NULL
);
223 // Perform the write based on EFI_FILE_PROTOCOL
225 return (FileHandle
->Write(FileHandle
, BufferSize
, Buffer
));
229 Close an open file handle.
231 This function closes a specified file handle. All "dirty" cached file data is
232 flushed to the device, and the file is closed. In all cases the handle is
235 @param FileHandle the file handle to close.
237 @retval EFI_SUCCESS the file handle was closed sucessfully.
242 IN EFI_FILE_HANDLE FileHandle
247 // ASSERT if FileHandle is NULL
249 ASSERT (FileHandle
!= NULL
);
251 // Perform the Close based on EFI_FILE_PROTOCOL
253 Status
= FileHandle
->Close(FileHandle
);
258 Delete a file and close the handle
260 This function closes and deletes a file. In all cases the file handle is closed.
261 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
262 returned, but the handle is still closed.
264 @param FileHandle the file handle to delete
266 @retval EFI_SUCCESS the file was closed sucessfully
267 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
269 @retval INVALID_PARAMETER One of the parameters has an invalid value.
274 IN EFI_FILE_HANDLE FileHandle
279 // ASSERT if FileHandle is NULL
281 ASSERT (FileHandle
!= NULL
);
283 // Perform the Delete based on EFI_FILE_PROTOCOL
285 Status
= FileHandle
->Delete(FileHandle
);
290 Set the current position in a file.
292 This function sets the current file position for the handle to the position
293 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
294 absolute positioning is supported, and seeking past the end of the file is
295 allowed (a subsequent write would grow the file). Seeking to position
296 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
297 If FileHandle is a directory, the only position that may be set is zero. This
298 has the effect of starting the read process of the directory entries over.
300 @param FileHandle The file handle on which the position is being set
301 @param Position Byte position from begining of file
303 @retval EFI_SUCCESS Operation completed sucessfully.
304 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
306 @retval INVALID_PARAMETER One of the parameters has an invalid value.
310 FileHandleSetPosition (
311 IN EFI_FILE_HANDLE FileHandle
,
316 // ASSERT if FileHandle is NULL
318 ASSERT (FileHandle
!= NULL
);
320 // Perform the SetPosition based on EFI_FILE_PROTOCOL
322 return (FileHandle
->SetPosition(FileHandle
, Position
));
326 Gets a file's current position
328 This function retrieves the current file position for the file handle. For
329 directories, the current file position has no meaning outside of the file
330 system driver and as such the operation is not supported. An error is returned
331 if FileHandle is a directory.
333 @param FileHandle The open file handle on which to get the position.
334 @param Position Byte position from begining of file.
336 @retval EFI_SUCCESS the operation completed sucessfully.
337 @retval INVALID_PARAMETER One of the parameters has an invalid value.
338 @retval EFI_UNSUPPORTED the request is not valid on directories.
342 FileHandleGetPosition (
343 IN EFI_FILE_HANDLE FileHandle
,
347 if (Position
== NULL
) {
348 return (EFI_INVALID_PARAMETER
);
351 // ASSERT if FileHandle is NULL
353 ASSERT (FileHandle
!= NULL
);
355 // Perform the GetPosition based on EFI_FILE_PROTOCOL
357 return (FileHandle
->GetPosition(FileHandle
, Position
));
360 Flushes data on a file
362 This function flushes all modified data associated with a file to a device.
364 @param FileHandle The file handle on which to flush data
366 @retval EFI_SUCCESS The data was flushed.
367 @retval EFI_NO_MEDIA The device has no media.
368 @retval EFI_DEVICE_ERROR The device reported an error.
369 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
370 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
371 @retval EFI_ACCESS_DENIED The file was opened for read only.
376 IN EFI_FILE_HANDLE FileHandle
380 // ASSERT if FileHandle is NULL
382 ASSERT (FileHandle
!= NULL
);
384 // Perform the Flush based on EFI_FILE_PROTOCOL
386 return (FileHandle
->Flush(FileHandle
));
390 function to determine if a given handle is a directory handle
392 if DirHandle is NULL then ASSERT()
394 open the file information on the DirHandle and verify that the Attribute
395 includes EFI_FILE_DIRECTORY bit set.
397 @param DirHandle Handle to open file
399 @retval EFI_SUCCESS DirHandle is a directory
400 @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available
401 @retval EFI_NOT_FOUND DirHandle is not a directory
405 FileHandleIsDirectory (
406 IN EFI_FILE_HANDLE DirHandle
409 EFI_FILE_INFO
*DirInfo
;
412 // ASSERT if DirHandle is NULL
414 ASSERT(DirHandle
!= NULL
);
417 // get the file information for DirHandle
419 DirInfo
= FileHandleGetInfo (DirHandle
);
424 if (DirInfo
== NULL
) {
428 return (EFI_INVALID_PARAMETER
);
430 if ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
432 // Attributes say this is not a directory
435 return (EFI_NOT_FOUND
);
441 return (EFI_SUCCESS
);
445 Retrieves the first file from a directory
447 This function opens a directory and gets the first file's info in the
448 directory. Caller can use FileHandleFindNextFile() to get other files. When
449 complete the caller is responsible for calling FreePool() on Buffer.
451 @param DirHandle The file handle of the directory to search
452 @param Buffer Pointer to buffer for file's information
454 @retval EFI_SUCCESS Found the first file.
455 @retval EFI_NOT_FOUND Cannot find the directory.
456 @retval EFI_NO_MEDIA The device has no media.
457 @retval EFI_DEVICE_ERROR The device reported an error.
458 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
459 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
464 FileHandleFindFirstFile (
465 IN EFI_FILE_HANDLE DirHandle
,
466 OUT EFI_FILE_INFO
**Buffer
472 if (Buffer
== NULL
|| DirHandle
== NULL
) {
473 return (EFI_INVALID_PARAMETER
);
477 // verify that DirHandle is a directory
479 Status
= FileHandleIsDirectory(DirHandle
);
480 if (EFI_ERROR(Status
)) {
485 // Allocate a buffer sized to struct size + enough for the string at the end
487 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
488 *Buffer
= AllocateZeroPool(BufferSize
);
489 if (*Buffer
== NULL
){
490 return (EFI_OUT_OF_RESOURCES
);
494 // reset to the begining of the directory
496 Status
= FileHandleSetPosition(DirHandle
, 0);
497 if (EFI_ERROR(Status
)) {
504 // read in the info about the first file
506 Status
= FileHandleRead (DirHandle
, &BufferSize
, *Buffer
);
507 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
508 if (EFI_ERROR(Status
)) {
513 return (EFI_SUCCESS
);
516 Retrieves the next file in a directory.
518 To use this function, caller must call the FileHandleFindFirstFile() to get the
519 first file, and then use this function get other files. This function can be
520 called for several times to get each file's information in the directory. If
521 the call of FileHandleFindNextFile() got the last file in the directory, the next
522 call of this function has no file to get. *NoFile will be set to TRUE and the
523 Buffer memory will be automatically freed.
525 @param DirHandle the file handle of the directory
526 @param Buffer pointer to buffer for file's information
527 @param NoFile pointer to boolean when last file is found
529 @retval EFI_SUCCESS Found the next file, or reached last file
530 @retval EFI_NO_MEDIA The device has no media.
531 @retval EFI_DEVICE_ERROR The device reported an error.
532 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
536 FileHandleFindNextFile(
537 IN EFI_FILE_HANDLE DirHandle
,
538 OUT EFI_FILE_INFO
*Buffer
,
546 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL
548 ASSERT (DirHandle
!= NULL
);
549 ASSERT (Buffer
!= NULL
);
550 ASSERT (NoFile
!= NULL
);
553 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
555 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
558 // read in the info about the next file
560 Status
= FileHandleRead (DirHandle
, &BufferSize
, Buffer
);
561 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
562 if (EFI_ERROR(Status
)) {
567 // If we read 0 bytes (but did not have erros) we already read in the last file.
569 if (BufferSize
== 0) {
574 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
);
629 Set the size of a file.
631 If FileHandle is NULL then ASSERT().
633 This function changes the file size info from the FileHandle's EFI_FILE_INFO
636 @param FileHandle File handle whose size is to be changed.
637 @param Size New size.
639 @retval EFI_SUCCESS operation was completed sucessfully.
640 @retval EFI_DEVICE_ERROR cannot access the file.
645 IN EFI_FILE_HANDLE FileHandle
,
649 EFI_FILE_INFO
*FileInfo
;
653 // ASSERT for FileHandle or Size being NULL
655 ASSERT (FileHandle
!= NULL
);
658 // get the FileInfo structure
660 FileInfo
= FileHandleGetInfo(FileHandle
);
661 if (FileInfo
== NULL
) {
662 return (EFI_DEVICE_ERROR
);
666 // Assign the FileSize pointer to the new value
668 FileInfo
->FileSize
= Size
;
670 Status
= FileHandleSetInfo(FileHandle
, FileInfo
);
672 // free the FileInfo memory
680 Safely append (on the left) with automatic string resizing given length of Destination and
681 desired length of copy from Source.
683 append the first D characters of Source to the end of Destination, where D is
684 the lesser of Count and the StrLen() of Source. If appending those D characters
685 will fit within Destination (whose Size is given as CurrentSize) and
686 still leave room for a NULL terminator, then those characters are appended,
687 starting at the original terminating NULL of Destination, and a new terminating
690 If appending D characters onto Destination will result in a overflow of the size
691 given in CurrentSize the string will be grown such that the copy can be performed
692 and CurrentSize will be updated to the new size.
694 If Source is NULL, there is nothing to append, just return the current buffer in
697 if Destination is NULL, then ASSERT()
698 if Destination's current length (including NULL terminator) is already more then
699 CurrentSize, then ASSERT()
701 @param[in, out] Destination The String to append onto
702 @param[in, out] CurrentSize on call the number of bytes in Destination. On
703 return possibly the new size (still in bytes). if NULL
704 then allocate whatever is needed.
705 @param[in] Source The String to append from
706 @param[in] Count Maximum number of characters to append. if 0 then
709 @return Destination return the resultant string.
714 IN OUT CHAR16
**Destination
,
715 IN OUT UINTN
*CurrentSize
,
716 IN CONST CHAR16
*Source
,
720 UINTN DestinationStartSize
;
727 ASSERT(Destination
!= NULL
);
730 // If there's nothing to do then just return Destination
732 if (Source
== NULL
) {
733 return (*Destination
);
737 // allow for NULL pointers address as Destination
739 if (*Destination
!= NULL
) {
740 ASSERT(CurrentSize
!= 0);
741 DestinationStartSize
= StrSize(*Destination
);
742 ASSERT(DestinationStartSize
<= *CurrentSize
);
744 DestinationStartSize
= 0;
745 // ASSERT(*CurrentSize == 0);
749 // Append all of Source?
752 Count
= StrSize(Source
);
756 // Test and grow if required
758 if (CurrentSize
!= NULL
) {
759 NewSize
= *CurrentSize
;
760 while (NewSize
< (DestinationStartSize
+ Count
)) {
761 NewSize
+= 2 * Count
;
763 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
764 *CurrentSize
= NewSize
;
766 *Destination
= AllocateZeroPool(Count
+sizeof(CHAR16
));
769 CopySize
= StrSize(*Destination
);
770 CopyMem((*Destination
)+((Count
-2)/sizeof(CHAR16
)), *Destination
, CopySize
);
771 CopyMem(*Destination
, Source
, Count
-2);
772 return (*Destination
);
776 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
779 if Handle is NULL, return EFI_INVALID_PARAMETER
781 @param[in] Handle Handle to the Directory or File to create path to.
782 @param[out] FullFileName pointer to pointer to generated full file name. It
783 is the responsibility of the caller to free this memory
784 with a call to FreePool().
785 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
786 @retval EFI_INVALID_PARAMETER Handle was NULL.
787 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
788 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
792 FileHandleGetFileName (
793 IN CONST EFI_FILE_HANDLE Handle
,
794 OUT CHAR16
**FullFileName
799 EFI_FILE_HANDLE CurrentHandle
;
800 EFI_FILE_HANDLE NextHigherHandle
;
801 EFI_FILE_INFO
*FileInfo
;
806 // Check our parameters
808 if (FullFileName
== NULL
|| Handle
== NULL
) {
809 return (EFI_INVALID_PARAMETER
);
812 *FullFileName
= NULL
;
813 CurrentHandle
= NULL
;
815 Status
= Handle
->Open(Handle
, &CurrentHandle
, L
".", EFI_FILE_MODE_READ
, 0);
816 if (!EFI_ERROR(Status
)) {
818 // Reverse out the current directory on the device
821 FileInfo
= FileHandleGetInfo(CurrentHandle
);
822 if (FileInfo
== NULL
) {
823 Status
= EFI_OUT_OF_RESOURCES
;
827 // We got info... do we have a name? if yes preceed the current path with it...
829 if (StrLen (FileInfo
->FileName
) == 0) {
830 if (*FullFileName
== NULL
) {
831 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
832 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
837 if (*FullFileName
== NULL
) {
838 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
839 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
841 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
842 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, FileInfo
->FileName
, 0);
843 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
848 // Move to the parent directory
850 Status
= CurrentHandle
->Open (CurrentHandle
, &NextHigherHandle
, L
"..", EFI_FILE_MODE_READ
, 0);
851 if (EFI_ERROR (Status
)) {
855 FileHandleClose(CurrentHandle
);
856 CurrentHandle
= NextHigherHandle
;
858 } else if (Status
== EFI_NOT_FOUND
) {
859 Status
= EFI_SUCCESS
;
860 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
861 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
864 if (CurrentHandle
!= NULL
) {
865 CurrentHandle
->Close (CurrentHandle
);
868 if (EFI_ERROR(Status
) && *FullFileName
!= NULL
) {
869 FreePool(*FullFileName
);
876 Function to read a single line from a file. The \n is not included in the returned
877 buffer. The returned buffer must be callee freed.
879 If the position upon start is 0, then the Ascii Boolean will be set. This should be
880 maintained and not changed for all operations with the same file.
882 @param[in] Handle FileHandle to read from.
883 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
885 @return The line of text from the file.
887 @sa FileHandleReadLine
891 FileHandleReturnLine(
892 IN EFI_FILE_HANDLE Handle
,
893 IN OUT BOOLEAN
*Ascii
903 Status
= FileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
904 if (Status
== EFI_BUFFER_TOO_SMALL
) {
905 RetVal
= AllocateZeroPool(Size
);
906 Status
= FileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
908 ASSERT_EFI_ERROR(Status
);
909 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
917 Function to read a single line (up to but not including the \n) from a EFI_FILE_HANDLE.
919 If the position upon start is 0, then the Ascii Boolean will be set. This should be
920 maintained and not changed for all operations with the same file.
922 @param[in] Handle FileHandle to read from
923 @param[in, out] Buffer pointer to buffer to read into
924 @param[in, out] Size pointer to number of bytes in buffer
925 @param[in] Truncate if TRUE then allows for truncation of the line to fit.
926 if FALSE will reset the position to the begining of the
927 line if the buffer is not large enough.
928 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
930 @retval EFI_SUCCESS the operation was sucessful. the line is stored in
932 @retval EFI_INVALID_PARAMETER Handle was NULL.
933 @retval EFI_INVALID_PARAMETER Size was NULL.
934 @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line.
935 Size was updated to minimum space required.
941 IN EFI_FILE_HANDLE Handle
,
942 IN OUT CHAR16
*Buffer
,
945 IN OUT BOOLEAN
*Ascii
952 UINT64 OriginalFilePosition
;
958 return (EFI_INVALID_PARAMETER
);
960 if (Buffer
== NULL
) {
965 FileHandleGetPosition(Handle
, &OriginalFilePosition
);
966 if (OriginalFilePosition
== 0) {
967 CharSize
= sizeof(CHAR16
);
968 Status
= FileHandleRead(Handle
, &CharSize
, &CharBuffer
);
969 ASSERT_EFI_ERROR(Status
);
970 if (CharBuffer
== gUnicodeFileTag
) {
974 FileHandleSetPosition(Handle
, OriginalFilePosition
);
978 for (CountSoFar
= 0;;CountSoFar
++){
981 CharSize
= sizeof(CHAR8
);
983 CharSize
= sizeof(CHAR16
);
985 Status
= FileHandleRead(Handle
, &CharSize
, &CharBuffer
);
986 if ( EFI_ERROR(Status
)
988 || (CharBuffer
== L
'\n' && !(*Ascii
))
989 || (CharBuffer
== '\n' && *Ascii
)
994 // if we have space save it...
996 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
997 ASSERT(Buffer
!= NULL
);
998 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
999 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
1004 // if we ran out of space tell when...
1006 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
1007 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
1009 FileHandleSetPosition(Handle
, OriginalFilePosition
);
1011 DEBUG((DEBUG_WARN
, "The line was truncated in FileHandleReadLine"));
1013 return (EFI_BUFFER_TOO_SMALL
);
1015 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
1016 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
1023 function to write a line of unicode text to a file.
1025 if Handle is NULL, ASSERT.
1026 if Buffer is NULL, do nothing. (return SUCCESS)
1028 @param[in] Handle FileHandle to write to
1029 @param[in] Buffer Buffer to write
1031 @retval EFI_SUCCESS the data was written.
1032 @retval other failure.
1038 FileHandleWriteLine(
1039 IN EFI_FILE_HANDLE Handle
,
1046 ASSERT(Handle
!= NULL
);
1048 if (Buffer
== NULL
) {
1049 return (EFI_SUCCESS
);
1052 Size
= StrSize(Buffer
) - sizeof(Buffer
[0]);
1053 Status
= FileHandleWrite(Handle
, &Size
, Buffer
);
1054 if (EFI_ERROR(Status
)) {
1057 Size
= StrSize(L
"\r\n") - sizeof(CHAR16
);
1058 return FileHandleWrite(Handle
, &Size
, L
"\r\n");
1062 function to take a formatted argument and print it to a file.
1064 @param[in] Handle the file handle for the file to write to
1065 @param[in] Format the format argument (see printlib for format specifier)
1066 @param[in] ... the variable arguments for the format
1068 @retval EFI_SUCCESS the operation was sucessful
1069 @return other a return value from FileHandleWriteLine
1071 @sa FileHandleWriteLine
1075 FileHandlePrintLine(
1076 IN EFI_FILE_HANDLE Handle
,
1077 IN CONST CHAR16
*Format
,
1085 VA_START (Marker
, Format
);
1088 // Get a buffer to print into
1090 Buffer
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
1091 ASSERT (Buffer
!= NULL
);
1094 // Print into our buffer
1096 UnicodeVSPrint (Buffer
, PcdGet16 (PcdShellPrintBufferSize
), Format
, Marker
);
1099 // Print buffer into file
1101 Status
= FileHandleWriteLine(Handle
, Buffer
);
1104 // Cleanup and return
1111 Function to determine if a FILE_HANDLE is at the end of the file.
1113 This will NOT work on directories.
1115 If Handle is NULL, then ASSERT.
1117 @param[in] Handle the file handle
1119 @retval TRUE the position is at the end of the file
1120 @retval FALSE the position is not at the end of the file
1125 IN EFI_FILE_HANDLE Handle
1128 EFI_FILE_INFO
*Info
;
1133 // ASSERT if Handle is NULL
1135 ASSERT(Handle
!= NULL
);
1137 FileHandleGetPosition(Handle
, &Pos
);
1138 Info
= FileHandleGetInfo (Handle
);
1139 ASSERT(Info
!= NULL
);
1140 FileHandleSetPosition(Handle
, Pos
);
1146 if (Pos
== Info
->FileSize
) {