VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c@ 108794

Last change on this file since 108794 was 108794, checked in by vboxsync, 2 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 36.4 KB
Line 
1/** @file
2 Provides interface to EFI_FILE_HANDLE functionality.
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7**/
8
9#include <Uefi.h>
10
11#include <Protocol/SimpleFileSystem.h>
12#include <Protocol/UnicodeCollation.h>
13
14#include <Guid/FileInfo.h>
15
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>
23
24CONST UINT16 gUnicodeFileTag = EFI_UNICODE_BYTE_ORDER_MARK;
25
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)
28
29/**
30 This function will retrieve the information about the file for the handle
31 specified and store it in allocated pool memory.
32
33 This function allocates a buffer to store the file's information. It is the
34 caller's responsibility to free the buffer
35
36 @param FileHandle The file handle of the file for which information is
37 being requested.
38
39 @retval NULL information could not be retrieved.
40
41 @return the information about the file
42**/
43EFI_FILE_INFO *
44EFIAPI
45FileHandleGetInfo (
46 IN EFI_FILE_HANDLE FileHandle
47 )
48{
49 EFI_FILE_INFO *FileInfo;
50 UINTN FileInfoSize;
51 EFI_STATUS Status;
52
53 if (FileHandle == NULL) {
54 return (NULL);
55 }
56
57 //
58 // Get the required size to allocate
59 //
60 FileInfoSize = 0;
61 FileInfo = NULL;
62 Status = FileHandle->GetInfo (
63 FileHandle,
64 &gEfiFileInfoGuid,
65 &FileInfoSize,
66 NULL
67 );
68 if (Status == EFI_BUFFER_TOO_SMALL) {
69 //
70 // error is expected. getting size to allocate
71 //
72 FileInfo = AllocateZeroPool (FileInfoSize);
73 if (FileInfo != NULL) {
74 //
75 // now get the information
76 //
77 Status = FileHandle->GetInfo (
78 FileHandle,
79 &gEfiFileInfoGuid,
80 &FileInfoSize,
81 FileInfo
82 );
83 //
84 // if we got an error free the memory and return NULL
85 //
86 if (EFI_ERROR (Status)) {
87 FreePool (FileInfo);
88 FileInfo = NULL;
89 }
90 }
91 }
92
93 return (FileInfo);
94}
95
96/**
97 This function sets the information about the file for the opened handle
98 specified.
99
100 @param[in] FileHandle The file handle of the file for which information
101 is being set.
102
103 @param[in] FileInfo The information to set.
104
105 @retval EFI_SUCCESS The information was set.
106 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
107 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
108 @retval EFI_NO_MEDIA The device has no medium.
109 @retval EFI_DEVICE_ERROR The device reported an error.
110 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
111 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
112 @retval EFI_ACCESS_DENIED The file was opened read only.
113 @retval EFI_VOLUME_FULL The volume is full.
114**/
115EFI_STATUS
116EFIAPI
117FileHandleSetInfo (
118 IN EFI_FILE_HANDLE FileHandle,
119 IN CONST EFI_FILE_INFO *FileInfo
120 )
121{
122 if ((FileHandle == NULL) || (FileInfo == NULL)) {
123 return (EFI_INVALID_PARAMETER);
124 }
125
126 //
127 // Set the info
128 //
129 return (FileHandle->SetInfo (
130 FileHandle,
131 &gEfiFileInfoGuid,
132 (UINTN)FileInfo->Size,
133 (EFI_FILE_INFO *)FileInfo
134 ));
135}
136
137/**
138 This function reads information from an opened file.
139
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.
152
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.
157
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
163 size.
164
165**/
166EFI_STATUS
167EFIAPI
168FileHandleRead (
169 IN EFI_FILE_HANDLE FileHandle,
170 IN OUT UINTN *BufferSize,
171 OUT VOID *Buffer
172 )
173{
174 if (FileHandle == NULL) {
175 return (EFI_INVALID_PARAMETER);
176 }
177
178 //
179 // Perform the read based on EFI_FILE_PROTOCOL
180 //
181 return (FileHandle->Read (FileHandle, BufferSize, Buffer));
182}
183
184/**
185 Write data to a file.
186
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.
193
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.
198
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.
207**/
208EFI_STATUS
209EFIAPI
210FileHandleWrite (
211 IN EFI_FILE_HANDLE FileHandle,
212 IN OUT UINTN *BufferSize,
213 IN VOID *Buffer
214 )
215{
216 if (FileHandle == NULL) {
217 return (EFI_INVALID_PARAMETER);
218 }
219
220 //
221 // Perform the write based on EFI_FILE_PROTOCOL
222 //
223 return (FileHandle->Write (FileHandle, BufferSize, Buffer));
224}
225
226/**
227 Close an open file handle.
228
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
231 closed.
232
233@param FileHandle the file handle to close.
234
235@retval EFI_SUCCESS the file handle was closed successfully.
236**/
237EFI_STATUS
238EFIAPI
239FileHandleClose (
240 IN EFI_FILE_HANDLE FileHandle
241 )
242{
243 EFI_STATUS Status;
244
245 if (FileHandle == NULL) {
246 return (EFI_INVALID_PARAMETER);
247 }
248
249 //
250 // Perform the Close based on EFI_FILE_PROTOCOL
251 //
252 Status = FileHandle->Close (FileHandle);
253 return Status;
254}
255
256/**
257 Delete a file and close the handle
258
259 This function closes and deletes a file. In all cases the file handle is closed.
260 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
261 returned, but the handle is still closed.
262
263 @param FileHandle the file handle to delete
264
265 @retval EFI_SUCCESS the file was closed successfully
266 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
267 deleted
268 @retval INVALID_PARAMETER One of the parameters has an invalid value.
269**/
270EFI_STATUS
271EFIAPI
272FileHandleDelete (
273 IN EFI_FILE_HANDLE FileHandle
274 )
275{
276 EFI_STATUS Status;
277
278 if (FileHandle == NULL) {
279 return (EFI_INVALID_PARAMETER);
280 }
281
282 //
283 // Perform the Delete based on EFI_FILE_PROTOCOL
284 //
285 Status = FileHandle->Delete (FileHandle);
286 return Status;
287}
288
289/**
290 Set the current position in a file.
291
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.
299
300 @param FileHandle The file handle on which the position is being set
301 @param Position Byte position from beginning of file
302
303 @retval EFI_SUCCESS Operation completed successfully.
304 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
305 directories.
306 @retval INVALID_PARAMETER One of the parameters has an invalid value.
307**/
308EFI_STATUS
309EFIAPI
310FileHandleSetPosition (
311 IN EFI_FILE_HANDLE FileHandle,
312 IN UINT64 Position
313 )
314{
315 if (FileHandle == NULL) {
316 return (EFI_INVALID_PARAMETER);
317 }
318
319 //
320 // Perform the SetPosition based on EFI_FILE_PROTOCOL
321 //
322 return (FileHandle->SetPosition (FileHandle, Position));
323}
324
325/**
326 Gets a file's current position
327
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.
332
333 @param FileHandle The open file handle on which to get the position.
334 @param Position Byte position from beginning of file.
335
336 @retval EFI_SUCCESS the operation completed successfully.
337 @retval INVALID_PARAMETER One of the parameters has an invalid value.
338 @retval EFI_UNSUPPORTED the request is not valid on directories.
339**/
340EFI_STATUS
341EFIAPI
342FileHandleGetPosition (
343 IN EFI_FILE_HANDLE FileHandle,
344 OUT UINT64 *Position
345 )
346{
347 if ((Position == NULL) || (FileHandle == NULL)) {
348 return (EFI_INVALID_PARAMETER);
349 }
350
351 //
352 // Perform the GetPosition based on EFI_FILE_PROTOCOL
353 //
354 return (FileHandle->GetPosition (FileHandle, Position));
355}
356
357/**
358 Flushes data on a file
359
360 This function flushes all modified data associated with a file to a device.
361
362 @param FileHandle The file handle on which to flush data
363
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.
370**/
371EFI_STATUS
372EFIAPI
373FileHandleFlush (
374 IN EFI_FILE_HANDLE FileHandle
375 )
376{
377 if (FileHandle == NULL) {
378 return (EFI_INVALID_PARAMETER);
379 }
380
381 //
382 // Perform the Flush based on EFI_FILE_PROTOCOL
383 //
384 return (FileHandle->Flush (FileHandle));
385}
386
387/**
388 Function to determine if a given handle is a directory handle.
389
390 Open the file information on the DirHandle and verify that the Attribute
391 includes EFI_FILE_DIRECTORY bit set.
392
393 @param[in] DirHandle Handle to open file.
394
395 @retval EFI_SUCCESS DirHandle is a directory.
396 @retval EFI_INVALID_PARAMETER DirHandle is NULL.
397 The file information returns from FileHandleGetInfo is NULL.
398 @retval EFI_NOT_FOUND DirHandle is not a directory.
399**/
400EFI_STATUS
401EFIAPI
402FileHandleIsDirectory (
403 IN EFI_FILE_HANDLE DirHandle
404 )
405{
406 EFI_FILE_INFO *DirInfo;
407
408 if (DirHandle == NULL) {
409 return (EFI_INVALID_PARAMETER);
410 }
411
412 //
413 // get the file information for DirHandle
414 //
415 DirInfo = FileHandleGetInfo (DirHandle);
416
417 //
418 // Parse DirInfo
419 //
420 if (DirInfo == NULL) {
421 //
422 // We got nothing...
423 //
424 return (EFI_INVALID_PARAMETER);
425 }
426
427 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
428 //
429 // Attributes say this is not a directory
430 //
431 FreePool (DirInfo);
432 return (EFI_NOT_FOUND);
433 }
434
435 //
436 // all good...
437 //
438 FreePool (DirInfo);
439 return (EFI_SUCCESS);
440}
441
442/** Retrieve first entry from a directory.
443
444 This function takes an open directory handle and gets information from the
445 first entry in the directory. A buffer is allocated to contain
446 the information and a pointer to the buffer is returned in *Buffer. The
447 caller can use FileHandleFindNextFile() to get subsequent directory entries.
448
449 The buffer will be freed by FileHandleFindNextFile() when the last directory
450 entry is read. Otherwise, the caller must free the buffer, using FreePool,
451 when finished with it.
452
453 @param[in] DirHandle The file handle of the directory to search.
454 @param[out] Buffer The pointer to pointer to buffer for file's information.
455
456 @retval EFI_SUCCESS Found the first file.
457 @retval EFI_NOT_FOUND Cannot find the directory.
458 @retval EFI_NO_MEDIA The device has no media.
459 @retval EFI_DEVICE_ERROR The device reported an error.
460 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
461 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
462 or FileHandleRead
463**/
464EFI_STATUS
465EFIAPI
466FileHandleFindFirstFile (
467 IN EFI_FILE_HANDLE DirHandle,
468 OUT EFI_FILE_INFO **Buffer
469 )
470{
471 EFI_STATUS Status;
472 UINTN BufferSize;
473
474 if ((Buffer == NULL) || (DirHandle == NULL)) {
475 return (EFI_INVALID_PARAMETER);
476 }
477
478 //
479 // verify that DirHandle is a directory
480 //
481 Status = FileHandleIsDirectory (DirHandle);
482 if (EFI_ERROR (Status)) {
483 return (Status);
484 }
485
486 //
487 // Allocate a buffer sized to struct size + enough for the string at the end
488 //
489 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
490 *Buffer = AllocateZeroPool (BufferSize);
491 if (*Buffer == NULL) {
492 return (EFI_OUT_OF_RESOURCES);
493 }
494
495 //
496 // reset to the beginning of the directory
497 //
498 Status = FileHandleSetPosition (DirHandle, 0);
499 if (EFI_ERROR (Status)) {
500 FreePool (*Buffer);
501 *Buffer = NULL;
502 return (Status);
503 }
504
505 //
506 // read in the info about the first file
507 //
508 Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);
509 ASSERT (Status != EFI_BUFFER_TOO_SMALL);
510 if (EFI_ERROR (Status) || (BufferSize == 0)) {
511 FreePool (*Buffer);
512 *Buffer = NULL;
513 if (BufferSize == 0) {
514 return (EFI_NOT_FOUND);
515 }
516
517 return (Status);
518 }
519
520 return (EFI_SUCCESS);
521}
522
523/** Retrieve next entries from a directory.
524
525 To use this function, the caller must first call the FileHandleFindFirstFile()
526 function to get the first directory entry. Subsequent directory entries are
527 retrieved by using the FileHandleFindNextFile() function. This function can
528 be called several times to get each entry from the directory. If the call of
529 FileHandleFindNextFile() retrieved the last directory entry, the next call of
530 this function will set *NoFile to TRUE and free the buffer.
531
532 @param[in] DirHandle The file handle of the directory.
533 @param[out] Buffer The pointer to buffer for file's information.
534 @param[out] NoFile The pointer to boolean when last file is found.
535
536 @retval EFI_SUCCESS Found the next file, or reached last file
537 @retval EFI_NO_MEDIA The device has no media.
538 @retval EFI_DEVICE_ERROR The device reported an error.
539 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
540**/
541EFI_STATUS
542EFIAPI
543FileHandleFindNextFile (
544 IN EFI_FILE_HANDLE DirHandle,
545 OUT EFI_FILE_INFO *Buffer,
546 OUT BOOLEAN *NoFile
547 )
548{
549 EFI_STATUS Status;
550 UINTN BufferSize;
551
552 if ((DirHandle == NULL) || (Buffer == NULL) || (NoFile == NULL)) {
553 return (EFI_INVALID_PARAMETER);
554 }
555
556 //
557 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
558 //
559 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
560
561 //
562 // read in the info about the next file
563 //
564 Status = FileHandleRead (DirHandle, &BufferSize, Buffer);
565 ASSERT (Status != EFI_BUFFER_TOO_SMALL);
566 if (EFI_ERROR (Status)) {
567 return (Status);
568 }
569
570 //
571 // If we read 0 bytes (but did not have erros) we already read in the last file.
572 //
573 if (BufferSize == 0) {
574 FreePool (Buffer);
575 *NoFile = TRUE;
576 }
577
578 return (EFI_SUCCESS);
579}
580
581/**
582 Retrieve the size of a file.
583
584 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
585 data.
586
587 @param[in] FileHandle The file handle from which size is retrieved.
588 @param[out] Size The pointer to size.
589
590 @retval EFI_SUCCESS Operation was completed successfully.
591 @retval EFI_DEVICE_ERROR Cannot access the file.
592 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
593 Size is NULL.
594**/
595EFI_STATUS
596EFIAPI
597FileHandleGetSize (
598 IN EFI_FILE_HANDLE FileHandle,
599 OUT UINT64 *Size
600 )
601{
602 EFI_FILE_INFO *FileInfo;
603
604 if ((FileHandle == NULL) || (Size == NULL)) {
605 return (EFI_INVALID_PARAMETER);
606 }
607
608 //
609 // get the FileInfo structure
610 //
611 FileInfo = FileHandleGetInfo (FileHandle);
612 if (FileInfo == NULL) {
613 return (EFI_DEVICE_ERROR);
614 }
615
616 //
617 // Assign the Size pointer to the correct value
618 //
619 *Size = FileInfo->FileSize;
620
621 //
622 // free the FileInfo memory
623 //
624 FreePool (FileInfo);
625
626 return (EFI_SUCCESS);
627}
628
629/**
630 Set the size of a file.
631
632 This function changes the file size info from the FileHandle's EFI_FILE_INFO
633 data.
634
635 @param[in] FileHandle The file handle whose size is to be changed.
636 @param[in] Size The new size.
637
638 @retval EFI_SUCCESS The operation completed successfully.
639 @retval EFI_DEVICE_ERROR Cannot access the file.
640 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
641**/
642EFI_STATUS
643EFIAPI
644FileHandleSetSize (
645 IN EFI_FILE_HANDLE FileHandle,
646 IN UINT64 Size
647 )
648{
649 EFI_FILE_INFO *FileInfo;
650 EFI_STATUS Status;
651
652 if (FileHandle == NULL) {
653 return (EFI_INVALID_PARAMETER);
654 }
655
656 //
657 // get the FileInfo structure
658 //
659 FileInfo = FileHandleGetInfo (FileHandle);
660 if (FileInfo == NULL) {
661 return (EFI_DEVICE_ERROR);
662 }
663
664 //
665 // Assign the FileSize pointer to the new value
666 //
667 FileInfo->FileSize = Size;
668
669 Status = FileHandleSetInfo (FileHandle, FileInfo);
670 //
671 // free the FileInfo memory
672 //
673 FreePool (FileInfo);
674
675 return (Status);
676}
677
678/**
679 Safely append (on the left) with automatic string resizing given length of Destination and
680 desired length of copy from Source.
681
682 append the first D characters of Source to the end of Destination, where D is
683 the lesser of Count and the StrLen() of Source. If appending those D characters
684 will fit within Destination (whose Size is given as CurrentSize) and
685 still leave room for a NULL terminator, then those characters are appended,
686 starting at the original terminating NULL of Destination, and a new terminating
687 NULL is appended.
688
689 If appending D characters onto Destination will result in a overflow of the size
690 given in CurrentSize the string will be grown such that the copy can be performed
691 and CurrentSize will be updated to the new size.
692
693 If Source is NULL, there is nothing to append, just return the current buffer in
694 Destination.
695
696 if Destination is NULL, then return error
697 if Destination's current length (including NULL terminator) is already more then
698 CurrentSize, then ASSERT()
699
700 @param[in, out] Destination The String to append onto
701 @param[in, out] CurrentSize on call the number of bytes in Destination. On
702 return possibly the new size (still in bytes). if NULL
703 then allocate whatever is needed.
704 @param[in] Source The String to append from
705 @param[in] Count Maximum number of characters to append. if 0 then
706 all are appended.
707
708 @return Destination return the resultant string.
709**/
710CHAR16 *
711EFIAPI
712StrnCatGrowLeft (
713 IN OUT CHAR16 **Destination,
714 IN OUT UINTN *CurrentSize,
715 IN CONST CHAR16 *Source,
716 IN UINTN Count
717 )
718{
719 UINTN DestinationStartSize;
720 UINTN NewSize;
721 UINTN CopySize;
722
723 if (Destination == NULL) {
724 return (NULL);
725 }
726
727 //
728 // If there's nothing to do then just return Destination
729 //
730 if (Source == NULL) {
731 return (*Destination);
732 }
733
734 //
735 // allow for NULL pointers address as Destination
736 //
737 if (*Destination != NULL) {
738 ASSERT (CurrentSize != 0);
739 DestinationStartSize = StrSize (*Destination);
740 ASSERT (DestinationStartSize <= *CurrentSize);
741 } else {
742 DestinationStartSize = 0;
743 // ASSERT(*CurrentSize == 0);
744 }
745
746 //
747 // Append all of Source?
748 //
749 if (Count == 0) {
750 Count = StrSize (Source);
751 }
752
753 //
754 // Test and grow if required
755 //
756 if (CurrentSize != NULL) {
757 NewSize = *CurrentSize;
758 while (NewSize < (DestinationStartSize + Count)) {
759 NewSize += 2 * Count;
760 }
761
762 *Destination = ReallocatePool (*CurrentSize, NewSize, *Destination);
763 *CurrentSize = NewSize;
764 } else {
765 *Destination = AllocateZeroPool (Count+sizeof (CHAR16));
766 }
767
768 if (*Destination == NULL) {
769 return NULL;
770 }
771
772 CopySize = StrSize (*Destination);
773 CopyMem ((*Destination)+((Count-2)/sizeof (CHAR16)), *Destination, CopySize);
774 CopyMem (*Destination, Source, Count-2);
775 return (*Destination);
776}
777
778/**
779 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
780 directory 'stack'. If the file is a directory, then append the '\' char at the
781 end of name string. If it's not a directory, then the last '\' should not be
782 added.
783
784 if Handle is NULL, return EFI_INVALID_PARAMETER
785
786 @param[in] Handle Handle to the Directory or File to create path to.
787 @param[out] FullFileName pointer to pointer to generated full file name. It
788 is the responsibility of the caller to free this memory
789 with a call to FreePool().
790 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
791 @retval EFI_INVALID_PARAMETER Handle was NULL.
792 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
793 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
794**/
795EFI_STATUS
796EFIAPI
797FileHandleGetFileName (
798 IN CONST EFI_FILE_HANDLE Handle,
799 OUT CHAR16 **FullFileName
800 )
801{
802 EFI_STATUS Status;
803 UINTN Size;
804 EFI_FILE_HANDLE CurrentHandle;
805 EFI_FILE_HANDLE NextHigherHandle;
806 EFI_FILE_INFO *FileInfo;
807
808 Size = 0;
809
810 //
811 // Check our parameters
812 //
813 if ((FullFileName == NULL) || (Handle == NULL)) {
814 return (EFI_INVALID_PARAMETER);
815 }
816
817 *FullFileName = NULL;
818 CurrentHandle = NULL;
819
820 Status = Handle->Open (Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);
821 if (!EFI_ERROR (Status)) {
822 //
823 // Reverse out the current directory on the device
824 //
825 for ( ; ;) {
826 FileInfo = FileHandleGetInfo (CurrentHandle);
827 if (FileInfo == NULL) {
828 Status = EFI_OUT_OF_RESOURCES;
829 break;
830 } else {
831 //
832 // Prepare to move to the parent directory.
833 // Also determine whether CurrentHandle refers to the Root directory.
834 //
835 Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);
836 //
837 // We got info... do we have a name? if yes precede the current path with it...
838 //
839 if ((StrLen (FileInfo->FileName) == 0) || EFI_ERROR (Status)) {
840 //
841 // Both FileInfo->FileName being '\0' and EFI_ERROR() suggest that
842 // CurrentHandle refers to the Root directory. As this loop ensures
843 // FullFileName is starting with '\\' at all times, signal success
844 // and exit the loop.
845 // While FileInfo->FileName could theoretically be a value other than
846 // '\0' or '\\', '\\' is guaranteed to be supported by the
847 // specification and hence its value can safely be ignored.
848 //
849 Status = EFI_SUCCESS;
850 if (*FullFileName == NULL) {
851 ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
852 *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);
853 }
854
855 FreePool (FileInfo);
856 break;
857 } else {
858 if (*FullFileName == NULL) {
859 ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
860 *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);
861 }
862
863 ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
864 *FullFileName = StrnCatGrowLeft (FullFileName, &Size, FileInfo->FileName, 0);
865 *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);
866 FreePool (FileInfo);
867 }
868 }
869
870 FileHandleClose (CurrentHandle);
871 //
872 // Move to the parent directory
873 //
874 CurrentHandle = NextHigherHandle;
875 }
876 } else if (Status == EFI_NOT_FOUND) {
877 Status = EFI_SUCCESS;
878 ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
879 *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);
880 }
881
882 if ((*FullFileName != NULL) &&
883 ((*FullFileName)[StrLen (*FullFileName) - 1] == L'\\') &&
884 (StrLen (*FullFileName) > 1) &&
885 (FileHandleIsDirectory (Handle) == EFI_NOT_FOUND)
886 )
887 {
888 (*FullFileName)[StrLen (*FullFileName) - 1] = CHAR_NULL;
889 }
890
891 if (CurrentHandle != NULL) {
892 CurrentHandle->Close (CurrentHandle);
893 }
894
895 if (EFI_ERROR (Status) && (*FullFileName != NULL)) {
896 FreePool (*FullFileName);
897 }
898
899 return (Status);
900}
901
902/**
903 Function to read a single line from a file. The \n is not included in the returned
904 buffer. The returned buffer must be callee freed.
905
906 If the position upon start is 0, then the Ascii Boolean will be set. This should be
907 maintained and not changed for all operations with the same file.
908
909 @param[in] Handle FileHandle to read from.
910 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
911
912 @return The line of text from the file.
913
914 @sa FileHandleReadLine
915**/
916CHAR16 *
917EFIAPI
918FileHandleReturnLine (
919 IN EFI_FILE_HANDLE Handle,
920 IN OUT BOOLEAN *Ascii
921 )
922{
923 CHAR16 *RetVal;
924 UINTN Size;
925 EFI_STATUS Status;
926
927 Size = 0;
928 RetVal = NULL;
929
930 Status = FileHandleReadLine (Handle, RetVal, &Size, FALSE, Ascii);
931 if (Status == EFI_BUFFER_TOO_SMALL) {
932 RetVal = AllocateZeroPool (Size);
933
934 if (RetVal == NULL) {
935 return NULL;
936 }
937
938 Status = FileHandleReadLine (Handle, RetVal, &Size, FALSE, Ascii);
939 }
940
941 ASSERT_EFI_ERROR (Status);
942 if (EFI_ERROR (Status) && (RetVal != NULL)) {
943 FreePool (RetVal);
944 RetVal = NULL;
945 }
946
947 return (RetVal);
948}
949
950/**
951 Function to read a single line (up to but not including the \n) from a file.
952
953 If the position upon start is 0, then the Ascii Boolean will be set. This should be
954 maintained and not changed for all operations with the same file.
955 The function will not return the \r and \n character in buffer. When an empty line is
956 read a CHAR_NULL character will be returned in buffer.
957
958 @param[in] Handle FileHandle to read from.
959 @param[in, out] Buffer The pointer to buffer to read into.
960 @param[in, out] Size The pointer to number of bytes in Buffer.
961 @param[in] Truncate If the buffer is large enough, this has no effect.
962 If the buffer is is too small and Truncate is TRUE,
963 the line will be truncated.
964 If the buffer is is too small and Truncate is FALSE,
965 then no read will occur.
966
967 @param[in, out] Ascii Boolean value for indicating whether the file is
968 Ascii (TRUE) or UCS2 (FALSE).
969
970 @retval EFI_SUCCESS The operation was successful. The line is stored in
971 Buffer.
972 @retval EFI_INVALID_PARAMETER Handle was NULL.
973 @retval EFI_INVALID_PARAMETER Size was NULL.
974 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
975 Size was updated to the minimum space required.
976 @sa FileHandleRead
977**/
978EFI_STATUS
979EFIAPI
980FileHandleReadLine (
981 IN EFI_FILE_HANDLE Handle,
982 IN OUT CHAR16 *Buffer,
983 IN OUT UINTN *Size,
984 IN BOOLEAN Truncate,
985 IN OUT BOOLEAN *Ascii
986 )
987{
988 EFI_STATUS Status;
989 CHAR16 CharBuffer;
990 UINT64 FileSize;
991 UINTN CharSize;
992 UINTN CountSoFar;
993 UINTN CrCount;
994 UINTN OldSize;
995 UINT64 OriginalFilePosition;
996
997 if ( (Handle == NULL)
998 || (Size == NULL)
999 || ((Buffer == NULL) && (*Size != 0))
1000 )
1001 {
1002 return (EFI_INVALID_PARAMETER);
1003 }
1004
1005 if ((Buffer != NULL) && (*Size != 0)) {
1006 *Buffer = CHAR_NULL;
1007 }
1008
1009 Status = FileHandleGetSize (Handle, &FileSize);
1010 if (EFI_ERROR (Status)) {
1011 return Status;
1012 } else if (FileSize == 0) {
1013 *Ascii = TRUE;
1014 return EFI_SUCCESS;
1015 }
1016
1017 FileHandleGetPosition (Handle, &OriginalFilePosition);
1018 if (OriginalFilePosition == 0) {
1019 CharSize = sizeof (CHAR16);
1020 Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
1021 ASSERT_EFI_ERROR (Status);
1022 if (CharBuffer == gUnicodeFileTag) {
1023 *Ascii = FALSE;
1024 } else {
1025 *Ascii = TRUE;
1026 FileHandleSetPosition (Handle, OriginalFilePosition);
1027 }
1028 }
1029
1030 CrCount = 0;
1031 for (CountSoFar = 0; ; CountSoFar++) {
1032 CharBuffer = 0;
1033 if (*Ascii) {
1034 CharSize = sizeof (CHAR8);
1035 } else {
1036 CharSize = sizeof (CHAR16);
1037 }
1038
1039 Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
1040 if ( EFI_ERROR (Status)
1041 || (CharSize == 0)
1042 || ((CharBuffer == L'\n') && !(*Ascii))
1043 || ((CharBuffer == '\n') && *Ascii)
1044 )
1045 {
1046 break;
1047 } else if (
1048 ((CharBuffer == L'\r') && !(*Ascii)) ||
1049 ((CharBuffer == '\r') && *Ascii)
1050 )
1051 {
1052 CrCount++;
1053 continue;
1054 }
1055
1056 //
1057 // if we have space save it...
1058 //
1059 if ((CountSoFar+1-CrCount)*sizeof (CHAR16) < *Size) {
1060 ASSERT (Buffer != NULL);
1061 ((CHAR16 *)Buffer)[CountSoFar-CrCount] = CharBuffer;
1062 ((CHAR16 *)Buffer)[CountSoFar+1-CrCount] = CHAR_NULL;
1063 }
1064 }
1065
1066 //
1067 // if we ran out of space tell when...
1068 //
1069 if ((CountSoFar+1-CrCount)*sizeof (CHAR16) > *Size) {
1070 OldSize = *Size;
1071 *Size = (CountSoFar+1-CrCount)*sizeof (CHAR16);
1072 if (!Truncate) {
1073 if ((Buffer != NULL) && (OldSize != 0)) {
1074 ZeroMem (Buffer, OldSize);
1075 }
1076
1077 FileHandleSetPosition (Handle, OriginalFilePosition);
1078 return (EFI_BUFFER_TOO_SMALL);
1079 } else {
1080 DEBUG ((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));
1081 return (EFI_SUCCESS);
1082 }
1083 }
1084
1085 return (Status);
1086}
1087
1088/**
1089 Function to write a line of text to a file.
1090
1091 If the file is a Unicode file (with UNICODE file tag) then write the unicode
1092 text.
1093 If the file is an ASCII file then write the ASCII text.
1094 If the size of file is zero (without file tag at the beginning) then write
1095 ASCII text as default.
1096
1097 @param[in] Handle FileHandle to write to.
1098 @param[in] Buffer Buffer to write, if NULL the function will
1099 take no action and return EFI_SUCCESS.
1100
1101 @retval EFI_SUCCESS The data was written.
1102 Buffer is NULL.
1103 @retval EFI_INVALID_PARAMETER Handle is NULL.
1104 @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
1105 string due to out of resources.
1106
1107 @sa FileHandleWrite
1108**/
1109EFI_STATUS
1110EFIAPI
1111FileHandleWriteLine (
1112 IN EFI_FILE_HANDLE Handle,
1113 IN CHAR16 *Buffer
1114 )
1115{
1116 EFI_STATUS Status;
1117 CHAR16 CharBuffer;
1118 UINTN Size;
1119 UINTN Index;
1120 UINTN CharSize;
1121 UINT64 FileSize;
1122 UINT64 OriginalFilePosition;
1123 BOOLEAN Ascii;
1124 CHAR8 *AsciiBuffer;
1125
1126 if (Buffer == NULL) {
1127 return (EFI_SUCCESS);
1128 }
1129
1130 if (Handle == NULL) {
1131 return (EFI_INVALID_PARAMETER);
1132 }
1133
1134 Ascii = FALSE;
1135 AsciiBuffer = NULL;
1136
1137 Status = FileHandleGetPosition (Handle, &OriginalFilePosition);
1138 if (EFI_ERROR (Status)) {
1139 return Status;
1140 }
1141
1142 Status = FileHandleSetPosition (Handle, 0);
1143 if (EFI_ERROR (Status)) {
1144 return Status;
1145 }
1146
1147 Status = FileHandleGetSize (Handle, &FileSize);
1148 if (EFI_ERROR (Status)) {
1149 return Status;
1150 }
1151
1152 if (FileSize == 0) {
1153 Ascii = TRUE;
1154 } else {
1155 CharSize = sizeof (CHAR16);
1156 Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
1157 ASSERT_EFI_ERROR (Status);
1158 if (CharBuffer == gUnicodeFileTag) {
1159 Ascii = FALSE;
1160 } else {
1161 Ascii = TRUE;
1162 }
1163 }
1164
1165 Status = FileHandleSetPosition (Handle, OriginalFilePosition);
1166 if (EFI_ERROR (Status)) {
1167 return Status;
1168 }
1169
1170 if (Ascii) {
1171 Size = (StrSize (Buffer) / sizeof (CHAR16)) * sizeof (CHAR8);
1172 AsciiBuffer = (CHAR8 *)AllocateZeroPool (Size);
1173 if (AsciiBuffer == NULL) {
1174 return EFI_OUT_OF_RESOURCES;
1175 }
1176
1177 UnicodeStrToAsciiStrS (Buffer, AsciiBuffer, Size);
1178 for (Index = 0; Index < Size; Index++) {
1179 if ((AsciiBuffer[Index] & BIT7) != 0) {
1180 FreePool (AsciiBuffer);
1181 return EFI_INVALID_PARAMETER;
1182 }
1183 }
1184
1185 Size = AsciiStrSize (AsciiBuffer) - sizeof (CHAR8);
1186 Status = FileHandleWrite (Handle, &Size, AsciiBuffer);
1187 if (EFI_ERROR (Status)) {
1188 FreePool (AsciiBuffer);
1189 return (Status);
1190 }
1191
1192 Size = AsciiStrSize ("\r\n") - sizeof (CHAR8);
1193 Status = FileHandleWrite (Handle, &Size, "\r\n");
1194 } else {
1195 if (OriginalFilePosition == 0) {
1196 Status = FileHandleSetPosition (Handle, sizeof (CHAR16));
1197 if (EFI_ERROR (Status)) {
1198 return Status;
1199 }
1200 }
1201
1202 Size = StrSize (Buffer) - sizeof (CHAR16);
1203 Status = FileHandleWrite (Handle, &Size, Buffer);
1204 if (EFI_ERROR (Status)) {
1205 return (Status);
1206 }
1207
1208 Size = StrSize (L"\r\n") - sizeof (CHAR16);
1209 Status = FileHandleWrite (Handle, &Size, L"\r\n");
1210 }
1211
1212 if (AsciiBuffer != NULL) {
1213 FreePool (AsciiBuffer);
1214 }
1215
1216 return Status;
1217}
1218
1219/**
1220 function to take a formatted argument and print it to a file.
1221
1222 @param[in] Handle the file handle for the file to write to
1223 @param[in] Format the format argument (see printlib for format specifier)
1224 @param[in] ... the variable arguments for the format
1225
1226 @retval EFI_SUCCESS the operation was successful
1227 @return other a return value from FileHandleWriteLine
1228
1229 @sa FileHandleWriteLine
1230**/
1231EFI_STATUS
1232EFIAPI
1233FileHandlePrintLine (
1234 IN EFI_FILE_HANDLE Handle,
1235 IN CONST CHAR16 *Format,
1236 ...
1237 )
1238{
1239 VA_LIST Marker;
1240 CHAR16 *Buffer;
1241 EFI_STATUS Status;
1242
1243 //
1244 // Get a buffer to print into
1245 //
1246 Buffer = AllocateZeroPool (PcdGet16 (PcdUefiFileHandleLibPrintBufferSize));
1247 if (Buffer == NULL) {
1248 return (EFI_OUT_OF_RESOURCES);
1249 }
1250
1251 //
1252 // Print into our buffer
1253 //
1254 VA_START (Marker, Format);
1255 UnicodeVSPrint (Buffer, PcdGet16 (PcdUefiFileHandleLibPrintBufferSize), Format, Marker);
1256 VA_END (Marker);
1257
1258 //
1259 // Print buffer into file
1260 //
1261 Status = FileHandleWriteLine (Handle, Buffer);
1262
1263 //
1264 // Cleanup and return
1265 //
1266 FreePool (Buffer);
1267 return (Status);
1268}
1269
1270/**
1271 Function to determine if a FILE_HANDLE is at the end of the file.
1272
1273 This will NOT work on directories.
1274
1275 If Handle is NULL, then return False.
1276
1277 @param[in] Handle the file handle
1278
1279 @retval TRUE the position is at the end of the file
1280 @retval FALSE the position is not at the end of the file
1281**/
1282BOOLEAN
1283EFIAPI
1284FileHandleEof (
1285 IN EFI_FILE_HANDLE Handle
1286 )
1287{
1288 EFI_FILE_INFO *Info;
1289 UINT64 Pos;
1290 BOOLEAN RetVal;
1291
1292 if (Handle == NULL) {
1293 return (FALSE);
1294 }
1295
1296 FileHandleGetPosition (Handle, &Pos);
1297 Info = FileHandleGetInfo (Handle);
1298
1299 if (Info == NULL) {
1300 return (FALSE);
1301 }
1302
1303 FileHandleSetPosition (Handle, Pos);
1304
1305 if (Pos == Info->FileSize) {
1306 RetVal = TRUE;
1307 } else {
1308 RetVal = FALSE;
1309 }
1310
1311 FreePool (Info);
1312
1313 return (RetVal);
1314}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette