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