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