]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
ed068243d6976caa7f72e17762dd07bfb2eccda2
[mirror_edk2.git] / ShellPkg / Library / BaseFileHandleLib / BaseFileHandleLib.c
1 /** @file
2 Provides interface to EFI_FILE_HANDLE functionality.
3
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
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.
12
13 **/
14
15 #include <Uefi.h>
16
17 #include <Protocol/SimpleFileSystem.h>
18
19 #include <Guid/FileInfo.h>
20
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
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 **/
43 EFI_FILE_INFO*
44 EFIAPI
45 FileHandleGetInfo (
46 IN EFI_FILE_HANDLE FileHandle
47 )
48 {
49 EFI_FILE_INFO *pFileInfo;
50 UINTN FileInfoSize;
51 EFI_STATUS Status;
52
53 //
54 // ASSERT if FileHandle is NULL
55 //
56 ASSERT (FileHandle != NULL);
57
58 //
59 // Get the required size to allocate
60 //
61 FileInfoSize = 0;
62 pFileInfo = NULL;
63 Status = FileHandle->GetInfo(FileHandle,
64 &gEfiFileInfoGuid,
65 &FileInfoSize,
66 pFileInfo);
67 //
68 // error is expected. getting size to allocate
69 //
70 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
71 pFileInfo = AllocateZeroPool(FileInfoSize);
72 ASSERT (pFileInfo != NULL);
73 //
74 // now get the information
75 //
76 Status = FileHandle->GetInfo(FileHandle,
77 &gEfiFileInfoGuid,
78 &FileInfoSize,
79 pFileInfo);
80 //
81 // if we got an error free the memory and return NULL
82 //
83 if (EFI_ERROR(Status)) {
84 FreePool(pFileInfo);
85 return NULL;
86 }
87 return (pFileInfo);
88 }
89
90 /**
91 This function will set the information about the file for the opened handle
92 specified.
93
94 @param FileHandle The file handle of the file for which information
95 is being set
96
97 @param FileInfo The infotmation to set.
98
99 @retval EFI_SUCCESS The information was set.
100 @retval EFI_UNSUPPORTED The InformationType is not known.
101 @retval EFI_NO_MEDIA The device has no medium.
102 @retval EFI_DEVICE_ERROR The device reported an error.
103 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
104 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
105 @retval EFI_ACCESS_DENIED The file was opened read only.
106 @retval EFI_VOLUME_FULL The volume is full.
107 **/
108 EFI_STATUS
109 EFIAPI
110 FileHandleSetInfo (
111 IN EFI_FILE_HANDLE FileHandle,
112 IN CONST EFI_FILE_INFO *FileInfo
113 )
114 {
115
116 //
117 // ASSERT if the FileHandle or FileInfo is NULL
118 //
119 ASSERT (FileHandle != NULL);
120 ASSERT (FileInfo != NULL);
121
122 //
123 // Set the info
124 //
125 return (FileHandle->SetInfo(FileHandle,
126 &gEfiFileInfoGuid,
127 (UINTN)FileInfo->Size,
128 (EFI_FILE_INFO*)FileInfo));
129 }
130
131 /**
132 This function reads information from an opened file.
133
134 If FileHandle is not a directory, the function reads the requested number of
135 bytes from the file at the file's current position and returns them in Buffer.
136 If the read goes beyond the end of the file, the read length is truncated to the
137 end of the file. The file's current position is increased by the number of bytes
138 returned. If FileHandle is a directory, the function reads the directory entry
139 at the file's current position and returns the entry in Buffer. If the Buffer
140 is not large enough to hold the current directory entry, then
141 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
142 BufferSize is set to be the size of the buffer needed to read the entry. On
143 success, the current position is updated to the next directory entry. If there
144 are no more directory entries, the read returns a zero-length buffer.
145 EFI_FILE_INFO is the structure returned as the directory entry.
146
147 @param FileHandle the opened file handle
148 @param BufferSize on input the size of buffer in bytes. on return
149 the number of bytes written.
150 @param Buffer the buffer to put read data into.
151
152 @retval EFI_SUCCESS Data was read.
153 @retval EFI_NO_MEDIA The device has no media.
154 @retval EFI_DEVICE_ERROR The device reported an error.
155 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
156 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
157 size.
158
159 **/
160 EFI_STATUS
161 EFIAPI
162 FileHandleRead(
163 IN EFI_FILE_HANDLE FileHandle,
164 IN OUT UINTN *BufferSize,
165 OUT VOID *Buffer
166 )
167 {
168 //
169 // ASSERT if FileHandle is NULL
170 //
171 ASSERT (FileHandle != NULL);
172
173 //
174 // Perform the read based on EFI_FILE_PROTOCOL
175 //
176 return (FileHandle->Read(FileHandle, BufferSize, Buffer));
177 }
178
179
180 /**
181 Write data to a file.
182
183 This function writes the specified number of bytes to the file at the current
184 file position. The current file position is advanced the actual number of bytes
185 written, which is returned in BufferSize. Partial writes only occur when there
186 has been a data error during the write attempt (such as "volume space full").
187 The file is automatically grown to hold the data if required. Direct writes to
188 opened directories are not supported.
189
190 @param FileHandle The opened file for writing
191 @param BufferSize on input the number of bytes in Buffer. On output
192 the number of bytes written.
193 @param Buffer the buffer containing data to write is stored.
194
195 @retval EFI_SUCCESS Data was written.
196 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
197 @retval EFI_NO_MEDIA The device has no media.
198 @retval EFI_DEVICE_ERROR The device reported an error.
199 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
200 @retval EFI_WRITE_PROTECTED The device is write-protected.
201 @retval EFI_ACCESS_DENIED The file was open for read only.
202 @retval EFI_VOLUME_FULL The volume is full.
203 **/
204 EFI_STATUS
205 EFIAPI
206 FileHandleWrite(
207 IN EFI_FILE_HANDLE FileHandle,
208 IN OUT UINTN *BufferSize,
209 IN VOID *Buffer
210 )
211 {
212 //
213 // ASSERT if FileHandle is NULL
214 //
215 ASSERT (FileHandle != NULL);
216 //
217 // Perform the write based on EFI_FILE_PROTOCOL
218 //
219 return (FileHandle->Write(FileHandle, BufferSize, Buffer));
220 }
221
222 /**
223 Close an open file handle.
224
225 This function closes a specified file handle. All "dirty" cached file data is
226 flushed to the device, and the file is closed. In all cases the handle is
227 closed.
228
229 @param FileHandle the file handle to close.
230
231 @retval EFI_SUCCESS the file handle was closed sucessfully.
232 **/
233 EFI_STATUS
234 EFIAPI
235 FileHandleClose (
236 IN EFI_FILE_HANDLE FileHandle
237 )
238 {
239 EFI_STATUS Status;
240 //
241 // ASSERT if FileHandle is NULL
242 //
243 ASSERT (FileHandle != NULL);
244 //
245 // Perform the Close based on EFI_FILE_PROTOCOL
246 //
247 Status = FileHandle->Close(FileHandle);
248 return Status;
249 }
250
251 /**
252 Delete a file and close the handle
253
254 This function closes and deletes a file. In all cases the file handle is closed.
255 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
256 returned, but the handle is still closed.
257
258 @param FileHandle the file handle to delete
259
260 @retval EFI_SUCCESS the file was closed sucessfully
261 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not
262 deleted
263 @retval INVALID_PARAMETER One of the parameters has an invalid value.
264 **/
265 EFI_STATUS
266 EFIAPI
267 FileHandleDelete (
268 IN EFI_FILE_HANDLE FileHandle
269 )
270 {
271 EFI_STATUS Status;
272 //
273 // ASSERT if FileHandle is NULL
274 //
275 ASSERT (FileHandle != NULL);
276 //
277 // Perform the Delete based on EFI_FILE_PROTOCOL
278 //
279 Status = FileHandle->Delete(FileHandle);
280 return Status;
281 }
282
283 /**
284 Set the current position in a file.
285
286 This function sets the current file position for the handle to the position
287 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
288 absolute positioning is supported, and seeking past the end of the file is
289 allowed (a subsequent write would grow the file). Seeking to position
290 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
291 If FileHandle is a directory, the only position that may be set is zero. This
292 has the effect of starting the read process of the directory entries over.
293
294 @param FileHandle The file handle on which the position is being set
295 @param Position Byte position from begining of file
296
297 @retval EFI_SUCCESS Operation completed sucessfully.
298 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
299 directories.
300 @retval INVALID_PARAMETER One of the parameters has an invalid value.
301 **/
302 EFI_STATUS
303 EFIAPI
304 FileHandleSetPosition (
305 IN EFI_FILE_HANDLE FileHandle,
306 IN UINT64 Position
307 )
308 {
309 //
310 // ASSERT if FileHandle is NULL
311 //
312 ASSERT (FileHandle != NULL);
313 //
314 // Perform the SetPosition based on EFI_FILE_PROTOCOL
315 //
316 return (FileHandle->SetPosition(FileHandle, Position));
317 }
318
319 /**
320 Gets a file's current position
321
322 This function retrieves the current file position for the file handle. For
323 directories, the current file position has no meaning outside of the file
324 system driver and as such the operation is not supported. An error is returned
325 if FileHandle is a directory.
326
327 @param FileHandle The open file handle on which to get the position.
328 @param Position Byte position from begining of file.
329
330 @retval EFI_SUCCESS the operation completed sucessfully.
331 @retval INVALID_PARAMETER One of the parameters has an invalid value.
332 @retval EFI_UNSUPPORTED the request is not valid on directories.
333 **/
334 EFI_STATUS
335 EFIAPI
336 FileHandleGetPosition (
337 IN EFI_FILE_HANDLE FileHandle,
338 OUT UINT64 *Position
339 )
340 {
341 //
342 // ASSERT if FileHandle is NULL
343 //
344 ASSERT (FileHandle != NULL);
345 //
346 // Perform the GetPosition based on EFI_FILE_PROTOCOL
347 //
348 return (FileHandle->GetPosition(FileHandle, Position));
349 }
350 /**
351 Flushes data on a file
352
353 This function flushes all modified data associated with a file to a device.
354
355 @param FileHandle The file handle on which to flush data
356
357 @retval EFI_SUCCESS The data was flushed.
358 @retval EFI_NO_MEDIA The device has no media.
359 @retval EFI_DEVICE_ERROR The device reported an error.
360 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
361 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
362 @retval EFI_ACCESS_DENIED The file was opened for read only.
363 **/
364 EFI_STATUS
365 EFIAPI
366 FileHandleFlush (
367 IN EFI_FILE_HANDLE FileHandle
368 )
369 {
370 //
371 // ASSERT if FileHandle is NULL
372 //
373 ASSERT (FileHandle != NULL);
374 //
375 // Perform the Flush based on EFI_FILE_PROTOCOL
376 //
377 return (FileHandle->Flush(FileHandle));
378 }
379
380 /**
381 function to determine if a given handle is a directory handle
382
383 if DirHandle is NULL then ASSERT()
384
385 open the file information on the DirHandle and verify that the Attribute
386 includes EFI_FILE_DIRECTORY bit set.
387
388 @param DirHandle Handle to open file
389
390 @retval EFI_SUCCESS DirHandle is a directory
391 @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available
392 @retval EFI_NOT_FOUND DirHandle is not a directory
393 **/
394 EFI_STATUS
395 EFIAPI
396 FileHandleIsDirectory (
397 IN EFI_FILE_HANDLE DirHandle
398 )
399 {
400 EFI_FILE_INFO *DirInfo;
401
402 //
403 // ASSERT if DirHandle is NULL
404 //
405 ASSERT(DirHandle != NULL);
406
407 //
408 // get the file information for DirHandle
409 //
410 DirInfo = FileHandleGetInfo (DirHandle);
411
412 //
413 // Parse DirInfo
414 //
415 if (DirInfo == NULL) {
416 //
417 // We got nothing...
418 //
419 return (EFI_INVALID_PARAMETER);
420 }
421 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
422 //
423 // Attributes say this is not a directory
424 //
425 FreePool (DirInfo);
426 return (EFI_NOT_FOUND);
427 }
428 //
429 // all good...
430 //
431 FreePool (DirInfo);
432 return (EFI_SUCCESS);
433 }
434
435 /**
436 Retrieves the first file from a directory
437
438 This function opens a directory and gets the first file's info in the
439 directory. Caller can use FileHandleFindNextFile() to get other files. When
440 complete the caller is responsible for calling FreePool() on Buffer.
441
442 @param DirHandle The file handle of the directory to search
443 @param Buffer Pointer to buffer for file's information
444
445 @retval EFI_SUCCESS Found the first file.
446 @retval EFI_NOT_FOUND Cannot find the directory.
447 @retval EFI_NO_MEDIA The device has no media.
448 @retval EFI_DEVICE_ERROR The device reported an error.
449 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
450 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
451 or FileHandleRead
452 **/
453 EFI_STATUS
454 EFIAPI
455 FileHandleFindFirstFile (
456 IN EFI_FILE_HANDLE DirHandle,
457 OUT EFI_FILE_INFO **Buffer
458 )
459 {
460 EFI_STATUS Status;
461 UINTN BufferSize;
462
463 //
464 // ASSERTs
465 //
466 ASSERT (DirHandle != NULL);
467 ASSERT (Buffer != NULL);
468
469 //
470 // verify that DirHandle is a directory
471 //
472 Status = FileHandleIsDirectory(DirHandle);
473 if (EFI_ERROR(Status)) {
474 return (Status);
475 }
476
477 //
478 // reset to the begining of the directory
479 //
480 Status = FileHandleSetPosition(DirHandle, 0);
481 if (EFI_ERROR(Status)) {
482 return (Status);
483 }
484
485 //
486 // Allocate a buffer sized to struct size + enough for the string at the end
487 //
488 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
489 *Buffer = AllocateZeroPool(BufferSize);
490 ASSERT (*Buffer != NULL);
491
492 //
493 // read in the info about the first file
494 //
495 Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);
496 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
497 if (EFI_ERROR(Status)) {
498 FreePool(*Buffer);
499 *Buffer = NULL;
500 return (Status);
501 }
502 return (EFI_SUCCESS);
503 }
504 /**
505 Retrieves the next file in a directory.
506
507 To use this function, caller must call the FileHandleFindFirstFile() to get the
508 first file, and then use this function get other files. This function can be
509 called for several times to get each file's information in the directory. If
510 the call of FileHandleFindNextFile() got the last file in the directory, the next
511 call of this function has no file to get. *NoFile will be set to TRUE and the
512 Buffer memory will be automatically freed.
513
514 @param DirHandle the file handle of the directory
515 @param Buffer pointer to buffer for file's information
516 @param NoFile pointer to boolean when last file is found
517
518 @retval EFI_SUCCESS Found the next file, or reached last file
519 @retval EFI_NO_MEDIA The device has no media.
520 @retval EFI_DEVICE_ERROR The device reported an error.
521 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
522 **/
523 EFI_STATUS
524 EFIAPI
525 FileHandleFindNextFile(
526 IN EFI_FILE_HANDLE DirHandle,
527 OUT EFI_FILE_INFO *Buffer,
528 OUT BOOLEAN *NoFile
529 )
530 {
531 EFI_STATUS Status;
532 UINTN BufferSize;
533
534 //
535 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL
536 //
537 ASSERT (DirHandle != NULL);
538 ASSERT (Buffer != NULL);
539 ASSERT (NoFile != NULL);
540
541 //
542 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
543 //
544 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
545
546 //
547 // read in the info about the next file
548 //
549 Status = FileHandleRead (DirHandle, &BufferSize, Buffer);
550 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
551 if (EFI_ERROR(Status)) {
552 return (Status);
553 }
554
555 //
556 // If we read 0 bytes (but did not have erros) we already read in the last file.
557 //
558 if (BufferSize == 0) {
559 FreePool(Buffer);
560 *NoFile = TRUE;
561 }
562
563 return (EFI_SUCCESS);
564 }
565 /**
566 Retrieve the size of a file.
567
568 if FileHandle is NULL then ASSERT()
569 if Size is NULL then ASSERT()
570
571 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
572 data.
573
574 @param FileHandle file handle from which size is retrieved
575 @param Size pointer to size
576
577 @retval EFI_SUCCESS operation was completed sucessfully
578 @retval EFI_DEVICE_ERROR cannot access the file
579 **/
580 EFI_STATUS
581 EFIAPI
582 FileHandleGetSize (
583 IN EFI_FILE_HANDLE FileHandle,
584 OUT UINT64 *Size
585 )
586 {
587 EFI_FILE_INFO *FileInfo;
588
589 //
590 // ASSERT for FileHandle or Size being NULL
591 //
592 ASSERT (FileHandle != NULL);
593 ASSERT (Size != NULL);
594
595 //
596 // get the FileInfo structure
597 //
598 FileInfo = FileHandleGetInfo(FileHandle);
599 if (FileInfo == NULL) {
600 return (EFI_DEVICE_ERROR);
601 }
602
603 //
604 // Assign the Size pointer to the correct value
605 //
606 *Size = FileInfo->FileSize;
607
608 //
609 // free the FileInfo memory
610 //
611 FreePool(FileInfo);
612
613 return (EFI_SUCCESS);
614 }
615
616
617 /**
618 Safely append (on the left) with automatic string resizing given length of Destination and
619 desired length of copy from Source.
620
621 append the first D characters of Source to the end of Destination, where D is
622 the lesser of Count and the StrLen() of Source. If appending those D characters
623 will fit within Destination (whose Size is given as CurrentSize) and
624 still leave room for a null terminator, then those characters are appended,
625 starting at the original terminating null of Destination, and a new terminating
626 null is appended.
627
628 If appending D characters onto Destination will result in a overflow of the size
629 given in CurrentSize the string will be grown such that the copy can be performed
630 and CurrentSize will be updated to the new size.
631
632 If Source is NULL, there is nothing to append, just return the current buffer in
633 Destination.
634
635 if Destination is NULL, then ASSERT()
636 if Destination's current length (including NULL terminator) is already more then
637 CurrentSize, then ASSERT()
638
639 @param[in,out] Destination The String to append onto
640 @param[in,out] CurrentSize on call the number of bytes in Destination. On
641 return possibly the new size (still in bytes). if NULL
642 then allocate whatever is needed.
643 @param[in] Source The String to append from
644 @param[in] Count Maximum number of characters to append. if 0 then
645 all are appended.
646
647 @return Destination return the resultant string.
648 **/
649 CHAR16*
650 EFIAPI
651 StrnCatGrowLeft (
652 IN OUT CHAR16 **Destination,
653 IN OUT UINTN *CurrentSize,
654 IN CONST CHAR16 *Source,
655 IN UINTN Count
656 ){
657 UINTN DestinationStartSize;
658 UINTN NewSize;
659 UINTN CopySize;
660
661 //
662 // ASSERTs
663 //
664 ASSERT(Destination != NULL);
665
666 //
667 // If there's nothing to do then just return Destination
668 //
669 if (Source == NULL) {
670 return (*Destination);
671 }
672
673 //
674 // allow for NULL pointers address as Destination
675 //
676 if (*Destination != NULL) {
677 ASSERT(CurrentSize != 0);
678 DestinationStartSize = StrSize(*Destination);
679 ASSERT(DestinationStartSize <= *CurrentSize);
680 } else {
681 DestinationStartSize = 0;
682 // ASSERT(*CurrentSize == 0);
683 }
684
685 //
686 // Append all of Source?
687 //
688 if (Count == 0) {
689 Count = StrSize(Source);
690 }
691
692 //
693 // Test and grow if required
694 //
695 if (CurrentSize != NULL) {
696 NewSize = *CurrentSize;
697 while (NewSize < (DestinationStartSize + Count)) {
698 NewSize += 2 * Count;
699 }
700 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
701 *CurrentSize = NewSize;
702 } else {
703 *Destination = AllocateZeroPool(Count+sizeof(CHAR16));
704 }
705
706 CopySize = StrSize(*Destination);
707 CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize);
708 CopyMem(*Destination, Source, Count-2);
709 return (*Destination);
710 }
711
712 /**
713 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
714 directory 'stack'.
715
716 if Handle is NULL, return EFI_INVALID_PARAMETER
717
718 @param[in] Handle Handle to the Directory or File to create path to.
719 @param[out] FullFileName pointer to pointer to generated full file name. It
720 is the responsibility of the caller to free this memory
721 with a call to FreePool().
722 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
723 @retval EFI_INVALID_PARAMETER Handle was NULL.
724 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
725 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
726 **/
727 EFI_STATUS
728 EFIAPI
729 FileHandleGetFileName (
730 IN CONST EFI_FILE_HANDLE Handle,
731 OUT CHAR16 **FullFileName
732 ){
733 EFI_STATUS Status;
734 UINTN Size;
735 EFI_FILE_HANDLE CurrentHandle;
736 EFI_FILE_HANDLE NextHigherHandle;
737 EFI_FILE_INFO *FileInfo;
738
739 Size = 0;
740
741 //
742 // Check our parameters
743 //
744 if (FullFileName == NULL || Handle == NULL) {
745 return (EFI_INVALID_PARAMETER);
746 }
747
748 *FullFileName = NULL;
749
750 Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);
751 if (!EFI_ERROR(Status)) {
752 //
753 // Reverse out the current directory on the device
754 //
755 for (;;) {
756 FileInfo = FileHandleGetInfo(CurrentHandle);
757 if (FileInfo == NULL) {
758 Status = EFI_OUT_OF_RESOURCES;
759 break;
760 } else {
761 //
762 // We got info... do we have a name? if yes preceed the current path with it...
763 //
764 if (StrLen (FileInfo->FileName) == 0) {
765 if (*FullFileName == NULL) {
766 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
767 }
768 FreePool(FileInfo);
769 break;
770 } else {
771 if (*FullFileName == NULL) {
772 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
773 }
774 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);
775 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
776 FreePool(FileInfo);
777 }
778 }
779 //
780 // Move to the parent directory
781 //
782 Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);
783 if (EFI_ERROR (Status)) {
784 break;
785 }
786
787 FileHandleClose(CurrentHandle);
788 CurrentHandle = NextHigherHandle;
789 }
790 }
791
792 if (CurrentHandle != NULL) {
793 CurrentHandle->Close (CurrentHandle);
794 }
795
796 if (EFI_ERROR(Status) && *FullFileName != NULL) {
797 FreePool(*FullFileName);
798 }
799
800 return (Status);
801 }
802
803 /**
804 Function to read a single line (up to but not including the \n) from a file.
805
806 @param[in] Handle FileHandle to read from
807 @param[in,out] Buffer pointer to buffer to read into
808 @param[in,out] Size pointer to number of bytes in buffer
809 @param[in[ Truncate if TRUE then allows for truncation of the line to fit.
810 if FALSE will reset the position to the begining of the
811 line if the buffer is not large enough.
812
813 @retval EFI_SUCCESS the operation was sucessful. the line is stored in
814 Buffer.
815 @retval EFI_INVALID_PARAMETER Handle was NULL.
816 @retval EFI_INVALID_PARAMETER Buffer was NULL.
817 @retval EFI_INVALID_PARAMETER Size was NULL.
818 @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line.
819 Size was updated to minimum space required.
820 @sa FileHandleRead
821 **/
822 EFI_STATUS
823 EFIAPI
824 FileHandleReadLine(
825 IN EFI_FILE_HANDLE Handle,
826 IN OUT VOID *Buffer,
827 IN OUT UINTN *Size,
828 IN BOOLEAN Truncate
829 ){
830 EFI_STATUS Status;
831 CHAR16 CharBuffer;
832 UINTN CharSize;
833 UINTN CountSoFar;
834 UINT64 Position;
835
836
837 if (Handle == NULL
838 ||Buffer == NULL
839 ||Size == NULL
840 ){
841 return (EFI_INVALID_PARAMETER);
842 }
843 FileHandleGetPosition(Handle, &Position);
844
845 for (CountSoFar = 0;;CountSoFar++){
846 CharSize = sizeof(CharBuffer);
847 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);
848 if ( EFI_ERROR(Status)
849 || CharSize == 0
850 || CharBuffer == '\n'
851 ){
852 break;
853 }
854 //
855 // if we have space save it...
856 //
857 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
858 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
859 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
860 }
861 }
862
863 //
864 // if we ran out of space tell when...
865 //
866 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
867 *Size = (CountSoFar+1)*sizeof(CHAR16);
868 if (Truncate == FALSE) {
869 FileHandleSetPosition(Handle, Position);
870 } else {
871 DEBUG((DEBUG_WARN, "The line was truncated in ReadLine"));
872 }
873 return (EFI_BUFFER_TOO_SMALL);
874 }
875 *Size = (CountSoFar+1)*sizeof(CHAR16);
876 return (Status);
877 }
878
879 /**
880 function to write a line of unicode text to a file.
881
882 if Handle is NULL, ASSERT.
883 if Buffer is NULL, do nothing. (return SUCCESS)
884
885 @param[in] Handle FileHandle to write to
886 @param[in] Buffer Buffer to write
887
888 @retval EFI_SUCCESS the data was written.
889 @retval other failure.
890
891 @sa FileHandleWrite
892 **/
893 EFI_STATUS
894 EFIAPI
895 FileHandleWriteLine(
896 IN EFI_FILE_HANDLE Handle,
897 IN CHAR16 *Buffer
898 ){
899 EFI_STATUS Status;
900 UINTN Size;
901
902 ASSERT(Handle != NULL);
903
904 if (Buffer == NULL) {
905 return (EFI_SUCCESS);
906 }
907
908 Size = StrLen(Buffer);
909 Status = FileHandleWrite(Handle, &Size, Buffer);
910 if (EFI_ERROR(Status)) {
911 return (Status);
912 }
913 Size = StrLen(L"\r\n");
914 return FileHandleWrite(Handle, &Size, L"\r\n");
915 }