2 Provides interface to EFI_FILE_HANDLE functionality.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Protocol/SimpleFileSystem.h>
12 #include <Protocol/UnicodeCollation.h>
14 #include <Guid/FileInfo.h>
16 #include <Library/DebugLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/FileHandleLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/PrintLib.h>
24 CONST UINT16 gUnicodeFileTag
= EFI_UNICODE_BYTE_ORDER_MARK
;
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
*FileInfo
;
53 if (FileHandle
== NULL
) {
58 // Get the required size to allocate
62 Status
= FileHandle
->GetInfo(FileHandle
,
66 if (Status
== EFI_BUFFER_TOO_SMALL
){
68 // error is expected. getting size to allocate
70 FileInfo
= AllocateZeroPool(FileInfoSize
);
72 // now get the information
74 Status
= FileHandle
->GetInfo(FileHandle
,
79 // if we got an error free the memory and return NULL
81 if (EFI_ERROR(Status
) && (FileInfo
!= NULL
)) {
90 This function sets the information about the file for the opened handle
93 @param[in] FileHandle The file handle of the file for which information
96 @param[in] FileInfo The information to set.
98 @retval EFI_SUCCESS The information was set.
99 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
100 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
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
116 if (FileHandle
== NULL
|| FileInfo
== NULL
) {
117 return (EFI_INVALID_PARAMETER
);
123 return (FileHandle
->SetInfo(FileHandle
,
125 (UINTN
)FileInfo
->Size
,
126 (EFI_FILE_INFO
*)FileInfo
));
130 This function reads information from an opened file.
132 If FileHandle is not a directory, the function reads the requested number of
133 bytes from the file at the file's current position and returns them in Buffer.
134 If the read goes beyond the end of the file, the read length is truncated to the
135 end of the file. The file's current position is increased by the number of bytes
136 returned. If FileHandle is a directory, the function reads the directory entry
137 at the file's current position and returns the entry in Buffer. If the Buffer
138 is not large enough to hold the current directory entry, then
139 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
140 BufferSize is set to be the size of the buffer needed to read the entry. On
141 success, the current position is updated to the next directory entry. If there
142 are no more directory entries, the read returns a zero-length buffer.
143 EFI_FILE_INFO is the structure returned as the directory entry.
145 @param FileHandle the opened file handle
146 @param BufferSize on input the size of buffer in bytes. on return
147 the number of bytes written.
148 @param Buffer the buffer to put read data into.
150 @retval EFI_SUCCESS Data was read.
151 @retval EFI_NO_MEDIA The device has no media.
152 @retval EFI_DEVICE_ERROR The device reported an error.
153 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
154 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
161 IN EFI_FILE_HANDLE FileHandle
,
162 IN OUT UINTN
*BufferSize
,
166 if (FileHandle
== NULL
) {
167 return (EFI_INVALID_PARAMETER
);
171 // Perform the read based on EFI_FILE_PROTOCOL
173 return (FileHandle
->Read(FileHandle
, BufferSize
, Buffer
));
178 Write data to a file.
180 This function writes the specified number of bytes to the file at the current
181 file position. The current file position is advanced the actual number of bytes
182 written, which is returned in BufferSize. Partial writes only occur when there
183 has been a data error during the write attempt (such as "volume space full").
184 The file is automatically grown to hold the data if required. Direct writes to
185 opened directories are not supported.
187 @param FileHandle The opened file for writing
188 @param BufferSize on input the number of bytes in Buffer. On output
189 the number of bytes written.
190 @param Buffer the buffer containing data to write is stored.
192 @retval EFI_SUCCESS Data was written.
193 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
194 @retval EFI_NO_MEDIA The device has no media.
195 @retval EFI_DEVICE_ERROR The device reported an error.
196 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
197 @retval EFI_WRITE_PROTECTED The device is write-protected.
198 @retval EFI_ACCESS_DENIED The file was open for read only.
199 @retval EFI_VOLUME_FULL The volume is full.
204 IN EFI_FILE_HANDLE FileHandle
,
205 IN OUT UINTN
*BufferSize
,
209 if (FileHandle
== NULL
) {
210 return (EFI_INVALID_PARAMETER
);
214 // Perform the write based on EFI_FILE_PROTOCOL
216 return (FileHandle
->Write(FileHandle
, BufferSize
, Buffer
));
220 Close an open file handle.
222 This function closes a specified file handle. All "dirty" cached file data is
223 flushed to the device, and the file is closed. In all cases the handle is
226 @param FileHandle the file handle to close.
228 @retval EFI_SUCCESS the file handle was closed successfully.
233 IN EFI_FILE_HANDLE FileHandle
238 if (FileHandle
== NULL
) {
239 return (EFI_INVALID_PARAMETER
);
243 // Perform the Close based on EFI_FILE_PROTOCOL
245 Status
= FileHandle
->Close(FileHandle
);
250 Delete a file and close the handle
252 This function closes and deletes a file. In all cases the file handle is closed.
253 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
254 returned, but the handle is still closed.
256 @param FileHandle the file handle to delete
258 @retval EFI_SUCCESS the file was closed successfully
259 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
261 @retval INVALID_PARAMETER One of the parameters has an invalid value.
266 IN EFI_FILE_HANDLE FileHandle
271 if (FileHandle
== NULL
) {
272 return (EFI_INVALID_PARAMETER
);
276 // Perform the Delete based on EFI_FILE_PROTOCOL
278 Status
= FileHandle
->Delete(FileHandle
);
283 Set the current position in a file.
285 This function sets the current file position for the handle to the position
286 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
287 absolute positioning is supported, and seeking past the end of the file is
288 allowed (a subsequent write would grow the file). Seeking to position
289 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
290 If FileHandle is a directory, the only position that may be set is zero. This
291 has the effect of starting the read process of the directory entries over.
293 @param FileHandle The file handle on which the position is being set
294 @param Position Byte position from beginning of file
296 @retval EFI_SUCCESS Operation completed successfully.
297 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
299 @retval INVALID_PARAMETER One of the parameters has an invalid value.
303 FileHandleSetPosition (
304 IN EFI_FILE_HANDLE FileHandle
,
308 if (FileHandle
== NULL
) {
309 return (EFI_INVALID_PARAMETER
);
313 // Perform the SetPosition based on EFI_FILE_PROTOCOL
315 return (FileHandle
->SetPosition(FileHandle
, Position
));
319 Gets a file's current position
321 This function retrieves the current file position for the file handle. For
322 directories, the current file position has no meaning outside of the file
323 system driver and as such the operation is not supported. An error is returned
324 if FileHandle is a directory.
326 @param FileHandle The open file handle on which to get the position.
327 @param Position Byte position from beginning of file.
329 @retval EFI_SUCCESS the operation completed successfully.
330 @retval INVALID_PARAMETER One of the parameters has an invalid value.
331 @retval EFI_UNSUPPORTED the request is not valid on directories.
335 FileHandleGetPosition (
336 IN EFI_FILE_HANDLE FileHandle
,
340 if (Position
== NULL
|| FileHandle
== NULL
) {
341 return (EFI_INVALID_PARAMETER
);
345 // Perform the GetPosition based on EFI_FILE_PROTOCOL
347 return (FileHandle
->GetPosition(FileHandle
, Position
));
350 Flushes data on a file
352 This function flushes all modified data associated with a file to a device.
354 @param FileHandle The file handle on which to flush data
356 @retval EFI_SUCCESS The data was flushed.
357 @retval EFI_NO_MEDIA The device has no media.
358 @retval EFI_DEVICE_ERROR The device reported an error.
359 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
360 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
361 @retval EFI_ACCESS_DENIED The file was opened for read only.
366 IN EFI_FILE_HANDLE FileHandle
369 if (FileHandle
== NULL
) {
370 return (EFI_INVALID_PARAMETER
);
374 // Perform the Flush based on EFI_FILE_PROTOCOL
376 return (FileHandle
->Flush(FileHandle
));
380 Function to determine if a given handle is a directory handle.
382 Open the file information on the DirHandle and verify that the Attribute
383 includes EFI_FILE_DIRECTORY bit set.
385 @param[in] DirHandle Handle to open file.
387 @retval EFI_SUCCESS DirHandle is a directory.
388 @retval EFI_INVALID_PARAMETER DirHandle is NULL.
389 The file information returns from FileHandleGetInfo is NULL.
390 @retval EFI_NOT_FOUND DirHandle is not a directory.
394 FileHandleIsDirectory (
395 IN EFI_FILE_HANDLE DirHandle
398 EFI_FILE_INFO
*DirInfo
;
400 if (DirHandle
== NULL
) {
401 return (EFI_INVALID_PARAMETER
);
405 // get the file information for DirHandle
407 DirInfo
= FileHandleGetInfo (DirHandle
);
412 if (DirInfo
== NULL
) {
416 return (EFI_INVALID_PARAMETER
);
418 if ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
420 // Attributes say this is not a directory
423 return (EFI_NOT_FOUND
);
429 return (EFI_SUCCESS
);
432 /** Retrieve first entry from a directory.
434 This function takes an open directory handle and gets information from the
435 first entry in the directory. A buffer is allocated to contain
436 the information and a pointer to the buffer is returned in *Buffer. The
437 caller can use FileHandleFindNextFile() to get subsequent directory entries.
439 The buffer will be freed by FileHandleFindNextFile() when the last directory
440 entry is read. Otherwise, the caller must free the buffer, using FreePool,
441 when finished with it.
443 @param[in] DirHandle The file handle of the directory to search.
444 @param[out] Buffer The pointer to pointer to buffer for file's information.
446 @retval EFI_SUCCESS Found the first file.
447 @retval EFI_NOT_FOUND Cannot find the directory.
448 @retval EFI_NO_MEDIA The device has no media.
449 @retval EFI_DEVICE_ERROR The device reported an error.
450 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
451 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
456 FileHandleFindFirstFile (
457 IN EFI_FILE_HANDLE DirHandle
,
458 OUT EFI_FILE_INFO
**Buffer
464 if (Buffer
== NULL
|| DirHandle
== NULL
) {
465 return (EFI_INVALID_PARAMETER
);
469 // verify that DirHandle is a directory
471 Status
= FileHandleIsDirectory(DirHandle
);
472 if (EFI_ERROR(Status
)) {
477 // Allocate a buffer sized to struct size + enough for the string at the end
479 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
480 *Buffer
= AllocateZeroPool(BufferSize
);
481 if (*Buffer
== NULL
){
482 return (EFI_OUT_OF_RESOURCES
);
486 // reset to the beginning of the directory
488 Status
= FileHandleSetPosition(DirHandle
, 0);
489 if (EFI_ERROR(Status
)) {
496 // read in the info about the first file
498 Status
= FileHandleRead (DirHandle
, &BufferSize
, *Buffer
);
499 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
500 if (EFI_ERROR(Status
) || BufferSize
== 0) {
503 if (BufferSize
== 0) {
504 return (EFI_NOT_FOUND
);
508 return (EFI_SUCCESS
);
511 /** Retrieve next entries from a directory.
513 To use this function, the caller must first call the FileHandleFindFirstFile()
514 function to get the first directory entry. Subsequent directory entries are
515 retrieved by using the FileHandleFindNextFile() function. This function can
516 be called several times to get each entry from the directory. If the call of
517 FileHandleFindNextFile() retrieved the last directory entry, the next call of
518 this function will set *NoFile to TRUE and free the buffer.
520 @param[in] DirHandle The file handle of the directory.
521 @param[out] Buffer The pointer to buffer for file's information.
522 @param[out] NoFile The pointer to boolean when last file is found.
524 @retval EFI_SUCCESS Found the next file, or reached last file
525 @retval EFI_NO_MEDIA The device has no media.
526 @retval EFI_DEVICE_ERROR The device reported an error.
527 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
531 FileHandleFindNextFile(
532 IN EFI_FILE_HANDLE DirHandle
,
533 OUT EFI_FILE_INFO
*Buffer
,
540 if (DirHandle
== NULL
|| Buffer
== NULL
|| NoFile
== NULL
) {
541 return (EFI_INVALID_PARAMETER
);
545 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
547 BufferSize
= FIND_XXXXX_FILE_BUFFER_SIZE
;
550 // read in the info about the next file
552 Status
= FileHandleRead (DirHandle
, &BufferSize
, Buffer
);
553 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
554 if (EFI_ERROR(Status
)) {
559 // If we read 0 bytes (but did not have erros) we already read in the last file.
561 if (BufferSize
== 0) {
566 return (EFI_SUCCESS
);
570 Retrieve the size of a file.
572 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
575 @param[in] FileHandle The file handle from which size is retrieved.
576 @param[out] Size The pointer to size.
578 @retval EFI_SUCCESS Operation was completed successfully.
579 @retval EFI_DEVICE_ERROR Cannot access the file.
580 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
586 IN EFI_FILE_HANDLE FileHandle
,
590 EFI_FILE_INFO
*FileInfo
;
592 if (FileHandle
== NULL
|| Size
== NULL
) {
593 return (EFI_INVALID_PARAMETER
);
597 // get the FileInfo structure
599 FileInfo
= FileHandleGetInfo(FileHandle
);
600 if (FileInfo
== NULL
) {
601 return (EFI_DEVICE_ERROR
);
605 // Assign the Size pointer to the correct value
607 *Size
= FileInfo
->FileSize
;
610 // free the FileInfo memory
614 return (EFI_SUCCESS
);
618 Set the size of a file.
620 This function changes the file size info from the FileHandle's EFI_FILE_INFO
623 @param[in] FileHandle The file handle whose size is to be changed.
624 @param[in] Size The new size.
626 @retval EFI_SUCCESS The operation completed successfully.
627 @retval EFI_DEVICE_ERROR Cannot access the file.
628 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
633 IN EFI_FILE_HANDLE FileHandle
,
637 EFI_FILE_INFO
*FileInfo
;
640 if (FileHandle
== NULL
) {
641 return (EFI_INVALID_PARAMETER
);
645 // get the FileInfo structure
647 FileInfo
= FileHandleGetInfo(FileHandle
);
648 if (FileInfo
== NULL
) {
649 return (EFI_DEVICE_ERROR
);
653 // Assign the FileSize pointer to the new value
655 FileInfo
->FileSize
= Size
;
657 Status
= FileHandleSetInfo(FileHandle
, FileInfo
);
659 // free the FileInfo memory
667 Safely append (on the left) with automatic string resizing given length of Destination and
668 desired length of copy from Source.
670 append the first D characters of Source to the end of Destination, where D is
671 the lesser of Count and the StrLen() of Source. If appending those D characters
672 will fit within Destination (whose Size is given as CurrentSize) and
673 still leave room for a NULL terminator, then those characters are appended,
674 starting at the original terminating NULL of Destination, and a new terminating
677 If appending D characters onto Destination will result in a overflow of the size
678 given in CurrentSize the string will be grown such that the copy can be performed
679 and CurrentSize will be updated to the new size.
681 If Source is NULL, there is nothing to append, just return the current buffer in
684 if Destination is NULL, then return error
685 if Destination's current length (including NULL terminator) is already more then
686 CurrentSize, then ASSERT()
688 @param[in, out] Destination The String to append onto
689 @param[in, out] CurrentSize on call the number of bytes in Destination. On
690 return possibly the new size (still in bytes). if NULL
691 then allocate whatever is needed.
692 @param[in] Source The String to append from
693 @param[in] Count Maximum number of characters to append. if 0 then
696 @return Destination return the resultant string.
701 IN OUT CHAR16
**Destination
,
702 IN OUT UINTN
*CurrentSize
,
703 IN CONST CHAR16
*Source
,
707 UINTN DestinationStartSize
;
711 if (Destination
== NULL
) {
716 // If there's nothing to do then just return Destination
718 if (Source
== NULL
) {
719 return (*Destination
);
723 // allow for NULL pointers address as Destination
725 if (*Destination
!= NULL
) {
726 ASSERT(CurrentSize
!= 0);
727 DestinationStartSize
= StrSize(*Destination
);
728 ASSERT(DestinationStartSize
<= *CurrentSize
);
730 DestinationStartSize
= 0;
731 // ASSERT(*CurrentSize == 0);
735 // Append all of Source?
738 Count
= StrSize(Source
);
742 // Test and grow if required
744 if (CurrentSize
!= NULL
) {
745 NewSize
= *CurrentSize
;
746 while (NewSize
< (DestinationStartSize
+ Count
)) {
747 NewSize
+= 2 * Count
;
749 *Destination
= ReallocatePool(*CurrentSize
, NewSize
, *Destination
);
750 *CurrentSize
= NewSize
;
752 *Destination
= AllocateZeroPool(Count
+sizeof(CHAR16
));
754 if (*Destination
== NULL
) {
758 CopySize
= StrSize(*Destination
);
759 CopyMem((*Destination
)+((Count
-2)/sizeof(CHAR16
)), *Destination
, CopySize
);
760 CopyMem(*Destination
, Source
, Count
-2);
761 return (*Destination
);
765 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
766 directory 'stack'. If the file is a directory, then append the '\' char at the
767 end of name string. If it's not a directory, then the last '\' should not be
770 if Handle is NULL, return EFI_INVALID_PARAMETER
772 @param[in] Handle Handle to the Directory or File to create path to.
773 @param[out] FullFileName pointer to pointer to generated full file name. It
774 is the responsibility of the caller to free this memory
775 with a call to FreePool().
776 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
777 @retval EFI_INVALID_PARAMETER Handle was NULL.
778 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
779 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
783 FileHandleGetFileName (
784 IN CONST EFI_FILE_HANDLE Handle
,
785 OUT CHAR16
**FullFileName
790 EFI_FILE_HANDLE CurrentHandle
;
791 EFI_FILE_HANDLE NextHigherHandle
;
792 EFI_FILE_INFO
*FileInfo
;
797 // Check our parameters
799 if (FullFileName
== NULL
|| Handle
== NULL
) {
800 return (EFI_INVALID_PARAMETER
);
803 *FullFileName
= NULL
;
804 CurrentHandle
= NULL
;
806 Status
= Handle
->Open(Handle
, &CurrentHandle
, L
".", EFI_FILE_MODE_READ
, 0);
807 if (!EFI_ERROR(Status
)) {
809 // Reverse out the current directory on the device
812 FileInfo
= FileHandleGetInfo(CurrentHandle
);
813 if (FileInfo
== NULL
) {
814 Status
= EFI_OUT_OF_RESOURCES
;
818 // We got info... do we have a name? if yes precede the current path with it...
820 if (StrLen (FileInfo
->FileName
) == 0) {
821 if (*FullFileName
== NULL
) {
822 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
823 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
828 if (*FullFileName
== NULL
) {
829 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
830 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
832 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
833 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, FileInfo
->FileName
, 0);
834 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
839 // Move to the parent directory
841 Status
= CurrentHandle
->Open (CurrentHandle
, &NextHigherHandle
, L
"..", EFI_FILE_MODE_READ
, 0);
842 if (EFI_ERROR (Status
)) {
846 FileHandleClose(CurrentHandle
);
847 CurrentHandle
= NextHigherHandle
;
849 } else if (Status
== EFI_NOT_FOUND
) {
850 Status
= EFI_SUCCESS
;
851 ASSERT((*FullFileName
== NULL
&& Size
== 0) || (*FullFileName
!= NULL
));
852 *FullFileName
= StrnCatGrowLeft(FullFileName
, &Size
, L
"\\", 0);
855 if (*FullFileName
!= NULL
&&
856 (*FullFileName
)[StrLen(*FullFileName
) - 1] == L
'\\' &&
857 StrLen(*FullFileName
) > 1 &&
858 FileHandleIsDirectory(Handle
) == EFI_NOT_FOUND
860 (*FullFileName
)[StrLen(*FullFileName
) - 1] = CHAR_NULL
;
863 if (CurrentHandle
!= NULL
) {
864 CurrentHandle
->Close (CurrentHandle
);
867 if (EFI_ERROR(Status
) && *FullFileName
!= NULL
) {
868 FreePool(*FullFileName
);
875 Function to read a single line from a file. The \n is not included in the returned
876 buffer. The returned buffer must be callee freed.
878 If the position upon start is 0, then the Ascii Boolean will be set. This should be
879 maintained and not changed for all operations with the same file.
881 @param[in] Handle FileHandle to read from.
882 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
884 @return The line of text from the file.
886 @sa FileHandleReadLine
890 FileHandleReturnLine(
891 IN EFI_FILE_HANDLE Handle
,
892 IN OUT BOOLEAN
*Ascii
902 Status
= FileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
903 if (Status
== EFI_BUFFER_TOO_SMALL
) {
904 RetVal
= AllocateZeroPool(Size
);
905 Status
= FileHandleReadLine(Handle
, RetVal
, &Size
, FALSE
, Ascii
);
907 ASSERT_EFI_ERROR(Status
);
908 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
916 Function to read a single line (up to but not including the \n) from a file.
918 If the position upon start is 0, then the Ascii Boolean will be set. This should be
919 maintained and not changed for all operations with the same file.
920 The function will not return the \r and \n character in buffer. When an empty line is
921 read a CHAR_NULL character will be returned in buffer.
923 @param[in] Handle FileHandle to read from.
924 @param[in, out] Buffer The pointer to buffer to read into.
925 @param[in, out] Size The pointer to number of bytes in Buffer.
926 @param[in] Truncate If the buffer is large enough, this has no effect.
927 If the buffer is is too small and Truncate is TRUE,
928 the line will be truncated.
929 If the buffer is is too small and Truncate is FALSE,
930 then no read will occur.
932 @param[in, out] Ascii Boolean value for indicating whether the file is
933 Ascii (TRUE) or UCS2 (FALSE).
935 @retval EFI_SUCCESS The operation was successful. The line is stored in
937 @retval EFI_INVALID_PARAMETER Handle was NULL.
938 @retval EFI_INVALID_PARAMETER Size was NULL.
939 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
940 Size was updated to the minimum space required.
946 IN EFI_FILE_HANDLE Handle
,
947 IN OUT CHAR16
*Buffer
,
950 IN OUT BOOLEAN
*Ascii
959 UINT64 OriginalFilePosition
;
963 ||(Buffer
==NULL
&&*Size
!=0)
965 return (EFI_INVALID_PARAMETER
);
968 if (Buffer
!= NULL
&& *Size
!= 0) {
972 Status
= FileHandleGetSize (Handle
, &FileSize
);
973 if (EFI_ERROR (Status
)) {
975 } else if (FileSize
== 0) {
980 FileHandleGetPosition(Handle
, &OriginalFilePosition
);
981 if (OriginalFilePosition
== 0) {
982 CharSize
= sizeof(CHAR16
);
983 Status
= FileHandleRead(Handle
, &CharSize
, &CharBuffer
);
984 ASSERT_EFI_ERROR(Status
);
985 if (CharBuffer
== gUnicodeFileTag
) {
989 FileHandleSetPosition(Handle
, OriginalFilePosition
);
994 for (CountSoFar
= 0;;CountSoFar
++){
997 CharSize
= sizeof(CHAR8
);
999 CharSize
= sizeof(CHAR16
);
1001 Status
= FileHandleRead(Handle
, &CharSize
, &CharBuffer
);
1002 if ( EFI_ERROR(Status
)
1004 || (CharBuffer
== L
'\n' && !(*Ascii
))
1005 || (CharBuffer
== '\n' && *Ascii
)
1009 (CharBuffer
== L
'\r' && !(*Ascii
)) ||
1010 (CharBuffer
== '\r' && *Ascii
)
1016 // if we have space save it...
1018 if ((CountSoFar
+1-CrCount
)*sizeof(CHAR16
) < *Size
){
1019 ASSERT(Buffer
!= NULL
);
1020 ((CHAR16
*)Buffer
)[CountSoFar
-CrCount
] = CharBuffer
;
1021 ((CHAR16
*)Buffer
)[CountSoFar
+1-CrCount
] = CHAR_NULL
;
1026 // if we ran out of space tell when...
1028 if ((CountSoFar
+1-CrCount
)*sizeof(CHAR16
) > *Size
){
1029 *Size
= (CountSoFar
+1-CrCount
)*sizeof(CHAR16
);
1031 if (Buffer
!= NULL
&& *Size
!= 0) {
1032 ZeroMem(Buffer
, *Size
);
1034 FileHandleSetPosition(Handle
, OriginalFilePosition
);
1035 return (EFI_BUFFER_TOO_SMALL
);
1037 DEBUG((DEBUG_WARN
, "The line was truncated in FileHandleReadLine"));
1038 return (EFI_SUCCESS
);
1046 Function to write a line of text to a file.
1048 If the file is a Unicode file (with UNICODE file tag) then write the unicode
1050 If the file is an ASCII file then write the ASCII text.
1051 If the size of file is zero (without file tag at the beginning) then write
1052 ASCII text as default.
1054 @param[in] Handle FileHandle to write to.
1055 @param[in] Buffer Buffer to write, if NULL the function will
1056 take no action and return EFI_SUCCESS.
1058 @retval EFI_SUCCESS The data was written.
1060 @retval EFI_INVALID_PARAMETER Handle is NULL.
1061 @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
1062 string due to out of resources.
1068 FileHandleWriteLine(
1069 IN EFI_FILE_HANDLE Handle
,
1079 UINT64 OriginalFilePosition
;
1083 if (Buffer
== NULL
) {
1084 return (EFI_SUCCESS
);
1087 if (Handle
== NULL
) {
1088 return (EFI_INVALID_PARAMETER
);
1094 Status
= FileHandleGetPosition(Handle
, &OriginalFilePosition
);
1095 if (EFI_ERROR(Status
)) {
1099 Status
= FileHandleSetPosition(Handle
, 0);
1100 if (EFI_ERROR(Status
)) {
1104 Status
= FileHandleGetSize(Handle
, &FileSize
);
1105 if (EFI_ERROR(Status
)) {
1109 if (FileSize
== 0) {
1112 CharSize
= sizeof (CHAR16
);
1113 Status
= FileHandleRead (Handle
, &CharSize
, &CharBuffer
);
1114 ASSERT_EFI_ERROR (Status
);
1115 if (CharBuffer
== gUnicodeFileTag
) {
1122 Status
= FileHandleSetPosition(Handle
, OriginalFilePosition
);
1123 if (EFI_ERROR(Status
)) {
1128 Size
= ( StrSize(Buffer
) / sizeof(CHAR16
) ) * sizeof(CHAR8
);
1129 AsciiBuffer
= (CHAR8
*)AllocateZeroPool(Size
);
1130 if (AsciiBuffer
== NULL
) {
1131 return EFI_OUT_OF_RESOURCES
;
1133 UnicodeStrToAsciiStrS (Buffer
, AsciiBuffer
, Size
);
1134 for (Index
= 0; Index
< Size
; Index
++) {
1135 if ((AsciiBuffer
[Index
] & BIT7
) != 0) {
1136 FreePool(AsciiBuffer
);
1137 return EFI_INVALID_PARAMETER
;
1141 Size
= AsciiStrSize(AsciiBuffer
) - sizeof(CHAR8
);
1142 Status
= FileHandleWrite(Handle
, &Size
, AsciiBuffer
);
1143 if (EFI_ERROR(Status
)) {
1144 FreePool (AsciiBuffer
);
1147 Size
= AsciiStrSize("\r\n") - sizeof(CHAR8
);
1148 Status
= FileHandleWrite(Handle
, &Size
, "\r\n");
1150 if (OriginalFilePosition
== 0) {
1151 Status
= FileHandleSetPosition (Handle
, sizeof(CHAR16
));
1152 if (EFI_ERROR(Status
)) {
1156 Size
= StrSize(Buffer
) - sizeof(CHAR16
);
1157 Status
= FileHandleWrite(Handle
, &Size
, Buffer
);
1158 if (EFI_ERROR(Status
)) {
1161 Size
= StrSize(L
"\r\n") - sizeof(CHAR16
);
1162 Status
= FileHandleWrite(Handle
, &Size
, L
"\r\n");
1165 if (AsciiBuffer
!= NULL
) {
1166 FreePool (AsciiBuffer
);
1172 function to take a formatted argument and print it to a file.
1174 @param[in] Handle the file handle for the file to write to
1175 @param[in] Format the format argument (see printlib for format specifier)
1176 @param[in] ... the variable arguments for the format
1178 @retval EFI_SUCCESS the operation was successful
1179 @return other a return value from FileHandleWriteLine
1181 @sa FileHandleWriteLine
1185 FileHandlePrintLine(
1186 IN EFI_FILE_HANDLE Handle
,
1187 IN CONST CHAR16
*Format
,
1196 // Get a buffer to print into
1198 Buffer
= AllocateZeroPool (PcdGet16 (PcdUefiFileHandleLibPrintBufferSize
));
1199 if (Buffer
== NULL
) {
1200 return (EFI_OUT_OF_RESOURCES
);
1204 // Print into our buffer
1206 VA_START (Marker
, Format
);
1207 UnicodeVSPrint (Buffer
, PcdGet16 (PcdUefiFileHandleLibPrintBufferSize
), Format
, Marker
);
1211 // Print buffer into file
1213 Status
= FileHandleWriteLine(Handle
, Buffer
);
1216 // Cleanup and return
1223 Function to determine if a FILE_HANDLE is at the end of the file.
1225 This will NOT work on directories.
1227 If Handle is NULL, then return False.
1229 @param[in] Handle the file handle
1231 @retval TRUE the position is at the end of the file
1232 @retval FALSE the position is not at the end of the file
1237 IN EFI_FILE_HANDLE Handle
1240 EFI_FILE_INFO
*Info
;
1244 if (Handle
== NULL
) {
1248 FileHandleGetPosition(Handle
, &Pos
);
1249 Info
= FileHandleGetInfo (Handle
);
1255 FileHandleSetPosition(Handle
, Pos
);
1257 if (Pos
== Info
->FileSize
) {