2 Provides interface to EFI_FILE_HANDLE functionality.
4 Copyright (c) 2006 - 2010, 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>
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>
25 #include <Library/FileHandleLib.h>
26 #include <Library/PcdLib.h>
27 #include <Library/PrintLib.h>
29 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
30 #define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
33 This function will retrieve the information about the file for the handle
34 specified and store it in allocated pool memory.
36 This function allocates a buffer to store the file's information. It is the
37 caller's responsibility to free the buffer
39 @param FileHandle The file handle of the file for which information is
42 @retval NULL information could not be retrieved.
44 @return the information about the file
49 IN EFI_FILE_HANDLE FileHandle
52 EFI_FILE_INFO
*FileInfo
;
57 // ASSERT if FileHandle is NULL
59 ASSERT (FileHandle
!= NULL
);
62 // Get the required size to allocate
66 Status
= FileHandle
->GetInfo(FileHandle
,
71 // error is expected. getting size to allocate
73 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
74 FileInfo
= AllocateZeroPool(FileInfoSize
);
75 ASSERT (FileInfo
!= NULL
);
77 // now get the information
79 Status
= FileHandle
->GetInfo(FileHandle
,
84 // if we got an error free the memory and return NULL
86 if (EFI_ERROR(Status
)) {
94 This function sets the information about the file for the opened handle
97 @param[in] FileHandle The file handle of the file for which information
100 @param[in] FileInfo The information to set.
102 @retval EFI_SUCCESS The information was set.
103 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
104 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
105 @retval EFI_NO_MEDIA The device has no medium.
106 @retval EFI_DEVICE_ERROR The device reported an error.
107 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
108 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
109 @retval EFI_ACCESS_DENIED The file was opened read only.
110 @retval EFI_VOLUME_FULL The volume is full.
115 IN EFI_FILE_HANDLE FileHandle
,
116 IN CONST EFI_FILE_INFO
*FileInfo
121 // ASSERT if the FileHandle or FileInfo is NULL
123 ASSERT (FileHandle
!= NULL
);
124 ASSERT (FileInfo
!= NULL
);
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
,
345 if (Position
== NULL
) {
346 return (EFI_INVALID_PARAMETER
);
349 // ASSERT if FileHandle is NULL
351 ASSERT (FileHandle
!= NULL
);
353 // Perform the GetPosition based on EFI_FILE_PROTOCOL
355 return (FileHandle
->GetPosition(FileHandle
, Position
));
358 Flushes data on a file
360 This function flushes all modified data associated with a file to a device.
362 @param FileHandle The file handle on which to flush data
364 @retval EFI_SUCCESS The data was flushed.
365 @retval EFI_NO_MEDIA The device has no media.
366 @retval EFI_DEVICE_ERROR The device reported an error.
367 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
368 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
369 @retval EFI_ACCESS_DENIED The file was opened for read only.
374 IN EFI_FILE_HANDLE FileHandle
378 // ASSERT if FileHandle is NULL
380 ASSERT (FileHandle
!= NULL
);
382 // Perform the Flush based on EFI_FILE_PROTOCOL
384 return (FileHandle
->Flush(FileHandle
));
388 function to determine if a given handle is a directory handle
390 if DirHandle is NULL then ASSERT()
392 open the file information on the DirHandle and verify that the Attribute
393 includes EFI_FILE_DIRECTORY bit set.
395 @param DirHandle Handle to open file
397 @retval EFI_SUCCESS DirHandle is a directory
398 @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available
399 @retval EFI_NOT_FOUND DirHandle is not a directory
403 FileHandleIsDirectory (
404 IN EFI_FILE_HANDLE DirHandle
407 EFI_FILE_INFO
*DirInfo
;
410 // ASSERT if DirHandle is NULL
412 ASSERT(DirHandle
!= NULL
);
415 // get the file information for DirHandle
417 DirInfo
= FileHandleGetInfo (DirHandle
);
422 if (DirInfo
== NULL
) {
426 return (EFI_INVALID_PARAMETER
);
428 if ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
430 // Attributes say this is not a directory
433 return (EFI_NOT_FOUND
);
439 return (EFI_SUCCESS
);
443 Retrieves the first file from a directory
445 This function opens a directory and gets the first file's info in the
446 directory. Caller can use FileHandleFindNextFile() to get other files. When
447 complete the caller is responsible for calling FreePool() on Buffer.
449 @param DirHandle The file handle of the directory to search
450 @param Buffer Pointer to buffer for file's information
452 @retval EFI_SUCCESS Found the first file.
453 @retval EFI_NOT_FOUND Cannot find the directory.
454 @retval EFI_NO_MEDIA The device has no media.
455 @retval EFI_DEVICE_ERROR The device reported an error.
456 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
457 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
462 FileHandleFindFirstFile (
463 IN EFI_FILE_HANDLE DirHandle
,
464 OUT EFI_FILE_INFO
**Buffer
473 ASSERT (DirHandle
!= NULL
);
474 ASSERT (Buffer
!= NULL
);
477 // verify that DirHandle is a directory
479 Status
= FileHandleIsDirectory(DirHandle
);
480 if (EFI_ERROR(Status
)) {
485 // reset to the begining of the directory
487 Status
= FileHandleSetPosition(DirHandle
, 0);
488 if (EFI_ERROR(Status
)) {
493 // Allocate a buffer sized to struct size + enough for the string at the end
495 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
496 *Buffer
= AllocateZeroPool(BufferSize
);
497 ASSERT (*Buffer
!= NULL
);
500 // read in the info about the first file
502 Status
= FileHandleRead (DirHandle
, &BufferSize
, *Buffer
);
503 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
504 if (EFI_ERROR(Status
)) {
509 return (EFI_SUCCESS
);
512 Retrieves the next file in a directory.
514 To use this function, caller must call the FileHandleFindFirstFile() to get the
515 first file, and then use this function get other files. This function can be
516 called for several times to get each file's information in the directory. If
517 the call of FileHandleFindNextFile() got the last file in the directory, the next
518 call of this function has no file to get. *NoFile will be set to TRUE and the
519 Buffer memory will be automatically freed.
521 @param DirHandle the file handle of the directory
522 @param Buffer pointer to buffer for file's information
523 @param NoFile pointer to boolean when last file is found
525 @retval EFI_SUCCESS Found the next file, or reached last file
526 @retval EFI_NO_MEDIA The device has no media.
527 @retval EFI_DEVICE_ERROR The device reported an error.
528 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
532 FileHandleFindNextFile(
533 IN EFI_FILE_HANDLE DirHandle
,
534 OUT EFI_FILE_INFO
*Buffer
,
542 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL
544 ASSERT (DirHandle
!= NULL
);
545 ASSERT (Buffer
!= NULL
);
546 ASSERT (NoFile
!= NULL
);
549 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
551 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
554 // read in the info about the next file
556 Status
= FileHandleRead (DirHandle
, &BufferSize
, Buffer
);
557 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
558 if (EFI_ERROR(Status
)) {
563 // If we read 0 bytes (but did not have erros) we already read in the last file.
565 if (BufferSize
== 0) {
570 return (EFI_SUCCESS
);
574 Retrieve the size of a file.
576 if FileHandle is NULL then ASSERT()
577 if Size is NULL then ASSERT()
579 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
582 @param FileHandle file handle from which size is retrieved
583 @param Size pointer to size
585 @retval EFI_SUCCESS operation was completed sucessfully
586 @retval EFI_DEVICE_ERROR cannot access the file
591 IN EFI_FILE_HANDLE FileHandle
,
595 EFI_FILE_INFO
*FileInfo
;
598 // ASSERT for FileHandle or Size being NULL
600 ASSERT (FileHandle
!= NULL
);
601 ASSERT (Size
!= NULL
);
604 // get the FileInfo structure
606 FileInfo
= FileHandleGetInfo(FileHandle
);
607 if (FileInfo
== NULL
) {
608 return (EFI_DEVICE_ERROR
);
612 // Assign the Size pointer to the correct value
614 *Size
= FileInfo
->FileSize
;
617 // free the FileInfo memory
621 return (EFI_SUCCESS
);
625 Set the size of a file.
627 If FileHandle is NULL then ASSERT().
629 This function changes the file size info from the FileHandle's EFI_FILE_INFO
632 @param FileHandle File handle whose size is to be changed.
633 @param Size New size.
635 @retval EFI_SUCCESS operation was completed sucessfully.
636 @retval EFI_DEVICE_ERROR cannot access the file.
641 IN EFI_FILE_HANDLE FileHandle
,
645 EFI_FILE_INFO
*FileInfo
;
649 // ASSERT for FileHandle or Size being NULL
651 ASSERT (FileHandle
!= NULL
);
654 // get the FileInfo structure
656 FileInfo
= FileHandleGetInfo(FileHandle
);
657 if (FileInfo
== NULL
) {
658 return (EFI_DEVICE_ERROR
);
662 // Assign the FileSize pointer to the new value
664 FileInfo
->FileSize
= Size
;
666 Status
= FileHandleSetInfo(FileHandle
, FileInfo
);
668 // free the FileInfo memory
676 Safely append (on the left) with automatic string resizing given length of Destination and
677 desired length of copy from Source.
679 append the first D characters of Source to the end of Destination, where D is
680 the lesser of Count and the StrLen() of Source. If appending those D characters
681 will fit within Destination (whose Size is given as CurrentSize) and
682 still leave room for a NULL terminator, then those characters are appended,
683 starting at the original terminating NULL of Destination, and a new terminating
686 If appending D characters onto Destination will result in a overflow of the size
687 given in CurrentSize the string will be grown such that the copy can be performed
688 and CurrentSize will be updated to the new size.
690 If Source is NULL, there is nothing to append, just return the current buffer in
693 if Destination is NULL, then ASSERT()
694 if Destination's current length (including NULL terminator) is already more then
695 CurrentSize, then ASSERT()
697 @param[in,out] Destination The String to append onto
698 @param[in,out] CurrentSize on call the number of bytes in Destination. On
699 return possibly the new size (still in bytes). if NULL
700 then allocate whatever is needed.
701 @param[in] Source The String to append from
702 @param[in] Count Maximum number of characters to append. if 0 then
705 @return Destination return the resultant string.
710 IN OUT CHAR16
**Destination
,
711 IN OUT UINTN
*CurrentSize
,
712 IN CONST CHAR16
*Source
,
716 UINTN DestinationStartSize
;
723 ASSERT(Destination
!= NULL
);
726 // If there's nothing to do then just return Destination
728 if (Source
== NULL
) {
729 return (*Destination
);
733 // allow for NULL pointers address as Destination
735 if (*Destination
!= NULL
) {
736 ASSERT(CurrentSize
!= 0);
737 DestinationStartSize
= StrSize(*Destination
);
738 ASSERT(DestinationStartSize
<= *CurrentSize
);
740 DestinationStartSize
= 0;
741 // ASSERT(*CurrentSize == 0);
745 // Append all of Source?
748 Count
= StrSize(Source
);
752 // Test and grow if required
754 if (CurrentSize
!= NULL
) {
755 NewSize
= *CurrentSize
;
756 while (NewSize
< (DestinationStartSize
+ Count
)) {
757 NewSize
+= 2 * Count
;
759 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
760 *CurrentSize
= NewSize
;
762 *Destination
= AllocateZeroPool(Count
+sizeof(CHAR16
));
765 CopySize
= StrSize(*Destination
);
766 CopyMem((*Destination
)+((Count
-2)/sizeof(CHAR16
)), *Destination
, CopySize
);
767 CopyMem(*Destination
, Source
, Count
-2);
768 return (*Destination
);
772 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
775 if Handle is NULL, return EFI_INVALID_PARAMETER
777 @param[in] Handle Handle to the Directory or File to create path to.
778 @param[out] FullFileName pointer to pointer to generated full file name. It
779 is the responsibility of the caller to free this memory
780 with a call to FreePool().
781 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
782 @retval EFI_INVALID_PARAMETER Handle was NULL.
783 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
784 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
788 FileHandleGetFileName (
789 IN CONST EFI_FILE_HANDLE Handle
,
790 OUT CHAR16
**FullFileName
795 EFI_FILE_HANDLE CurrentHandle
;
796 EFI_FILE_HANDLE NextHigherHandle
;
797 EFI_FILE_INFO
*FileInfo
;
802 // Check our parameters
804 if (FullFileName
== NULL
|| Handle
== NULL
) {
805 return (EFI_INVALID_PARAMETER
);
808 *FullFileName
= NULL
;
809 CurrentHandle
= NULL
;
811 Status
= Handle
->Open(Handle
, &CurrentHandle
, L
".", EFI_FILE_MODE_READ
, 0);
812 if (!EFI_ERROR(Status
)) {
814 // Reverse out the current directory on the device
817 FileInfo
= FileHandleGetInfo(CurrentHandle
);
818 if (FileInfo
== NULL
) {
819 Status
= EFI_OUT_OF_RESOURCES
;
823 // We got info... do we have a name? if yes preceed the current path with it...
825 if (StrLen (FileInfo
->FileName
) == 0) {
826 if (*FullFileName
== NULL
) {
827 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
828 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
833 if (*FullFileName
== NULL
) {
834 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
835 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
837 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
838 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, FileInfo
->FileName
, 0);
839 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
844 // Move to the parent directory
846 Status
= CurrentHandle
->Open (CurrentHandle
, &NextHigherHandle
, L
"..", EFI_FILE_MODE_READ
, 0);
847 if (EFI_ERROR (Status
)) {
851 FileHandleClose(CurrentHandle
);
852 CurrentHandle
= NextHigherHandle
;
854 } else if (Status
== EFI_NOT_FOUND
) {
855 Status
= EFI_SUCCESS
;
856 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
857 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
860 if (CurrentHandle
!= NULL
) {
861 CurrentHandle
->Close (CurrentHandle
);
864 if (EFI_ERROR(Status
) && *FullFileName
!= NULL
) {
865 FreePool(*FullFileName
);
872 Function to read a single line from a file. The \n is not included in the returned
873 buffer. The returned buffer must be callee freed.
875 If the position upon start is 0, then the Ascii Boolean will be set. This should be
876 maintained and not changed for all operations with the same file.
878 @param[in] Handle FileHandle to read from.
879 @param[in,out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
881 @return The line of text from the file.
883 @sa FileHandleReadLine
887 FileHandleReturnLine(
888 IN EFI_FILE_HANDLE Handle
,
889 IN OUT BOOLEAN
*Ascii
899 Status
= FileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
900 if (Status
== EFI_BUFFER_TOO_SMALL
) {
901 RetVal
= AllocatePool(Size
);
902 Status
= FileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
904 ASSERT_EFI_ERROR(Status
);
905 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
913 Function to read a single line (up to but not including the \n) from a EFI_FILE_HANDLE.
915 If the position upon start is 0, then the Ascii Boolean will be set. This should be
916 maintained and not changed for all operations with the same file.
918 @param[in] Handle FileHandle to read from
919 @param[in,out] Buffer pointer to buffer to read into
920 @param[in,out] Size pointer to number of bytes in buffer
921 @param[in] Truncate if TRUE then allows for truncation of the line to fit.
922 if FALSE will reset the position to the begining of the
923 line if the buffer is not large enough.
924 @param[in,out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
926 @retval EFI_SUCCESS the operation was sucessful. the line is stored in
928 @retval EFI_INVALID_PARAMETER Handle was NULL.
929 @retval EFI_INVALID_PARAMETER Size was NULL.
930 @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line.
931 Size was updated to minimum space required.
937 IN EFI_FILE_HANDLE Handle
,
938 IN OUT CHAR16
*Buffer
,
941 IN OUT BOOLEAN
*Ascii
948 UINT64 OriginalFilePosition
;
954 return (EFI_INVALID_PARAMETER
);
956 if (Buffer
== NULL
) {
961 FileHandleGetPosition(Handle
, &OriginalFilePosition
);
962 if (OriginalFilePosition
== 0) {
963 CharSize
= sizeof(CHAR16
);
964 Status
= FileHandleRead(Handle
, &CharSize
, &CharBuffer
);
965 ASSERT_EFI_ERROR(Status
);
966 if (CharBuffer
== UnicodeFileTag
) {
970 FileHandleSetPosition(Handle
, OriginalFilePosition
);
974 for (CountSoFar
= 0;;CountSoFar
++){
977 CharSize
= sizeof(CHAR8
);
979 CharSize
= sizeof(CHAR16
);
981 Status
= FileHandleRead(Handle
, &CharSize
, &CharBuffer
);
982 if ( EFI_ERROR(Status
)
984 || (CharBuffer
== L
'\n' && !(*Ascii
))
985 || (CharBuffer
== '\n' && *Ascii
)
990 // if we have space save it...
992 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
993 ASSERT(Buffer
!= NULL
);
994 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
995 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
1000 // if we ran out of space tell when...
1002 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
1003 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
1005 FileHandleSetPosition(Handle
, OriginalFilePosition
);
1007 DEBUG((DEBUG_WARN
, "The line was truncated in FileHandleReadLine"));
1009 return (EFI_BUFFER_TOO_SMALL
);
1011 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
1012 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
1019 function to write a line of unicode text to a file.
1021 if Handle is NULL, ASSERT.
1022 if Buffer is NULL, do nothing. (return SUCCESS)
1024 @param[in] Handle FileHandle to write to
1025 @param[in] Buffer Buffer to write
1027 @retval EFI_SUCCESS the data was written.
1028 @retval other failure.
1034 FileHandleWriteLine(
1035 IN EFI_FILE_HANDLE Handle
,
1042 ASSERT(Handle
!= NULL
);
1044 if (Buffer
== NULL
) {
1045 return (EFI_SUCCESS
);
1048 Size
= StrSize(Buffer
) - sizeof(Buffer
[0]);
1049 Status
= FileHandleWrite(Handle
, &Size
, Buffer
);
1050 if (EFI_ERROR(Status
)) {
1053 Size
= StrSize(L
"\r\n") - sizeof(CHAR16
);
1054 return FileHandleWrite(Handle
, &Size
, L
"\r\n");
1058 function to take a formatted argument and print it to a file.
1060 @param[in] Handle the file handle for the file to write to
1061 @param[in] Format the format argument (see printlib for format specifier)
1062 @param[in] ... the variable arguments for the format
1064 @retval EFI_SUCCESS the operation was sucessful
1065 @return other a return value from FileHandleWriteLine
1067 @sa FileHandleWriteLine
1071 FileHandlePrintLine(
1072 IN EFI_FILE_HANDLE Handle
,
1073 IN CONST CHAR16
*Format
,
1081 VA_START (Marker
, Format
);
1084 // Get a buffer to print into
1086 Buffer
= AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize
));
1087 ASSERT (Buffer
!= NULL
);
1090 // Print into our buffer
1092 UnicodeVSPrint (Buffer
, PcdGet16 (PcdShellPrintBufferSize
), Format
, Marker
);
1095 // Print buffer into file
1097 Status
= FileHandleWriteLine(Handle
, Buffer
);
1100 // Cleanup and return
1107 Function to determine if a FILE_HANDLE is at the end of the file.
1109 This will NOT work on directories.
1111 If Handle is NULL, then ASSERT.
1113 @param[in] Handle the file handle
1115 @retval TRUE the position is at the end of the file
1116 @retval FALSE the position is not at the end of the file
1121 IN EFI_FILE_HANDLE Handle
1124 EFI_FILE_INFO
*Info
;
1129 // ASSERT if Handle is NULL
1131 ASSERT(Handle
!= NULL
);
1133 FileHandleGetPosition(Handle
, &Pos
);
1134 Info
= FileHandleGetInfo (Handle
);
1135 ASSERT(Info
!= NULL
);
1136 FileHandleSetPosition(Handle
, Pos
);
1142 if (Pos
== Info
->FileSize
) {