]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
bd3a4a7b52361cfb82926aeae9fdb5c6b772cde9
[mirror_edk2.git] / ShellPkg / Library / BaseFileHandleLib / BaseFileHandleLib.c
1 /** @file
2 Provides interface to EFI_FILE_HANDLE functionality.
3
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved. <BR>
5 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 *FileInfo;
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 FileInfo = NULL;
66 Status = FileHandle->GetInfo(FileHandle,
67 &gEfiFileInfoGuid,
68 &FileInfoSize,
69 FileInfo);
70 //
71 // error is expected. getting size to allocate
72 //
73 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
74 FileInfo = AllocateZeroPool(FileInfoSize);
75 ASSERT (FileInfo != NULL);
76 //
77 // now get the information
78 //
79 Status = FileHandle->GetInfo(FileHandle,
80 &gEfiFileInfoGuid,
81 &FileInfoSize,
82 FileInfo);
83 //
84 // if we got an error free the memory and return NULL
85 //
86 if (EFI_ERROR(Status)) {
87 FreePool(FileInfo);
88 return NULL;
89 }
90 return (FileInfo);
91 }
92
93 /**
94 This function sets the information about the file for the opened handle
95 specified.
96
97 @param[in] FileHandle The file handle of the file for which information
98 is being set.
99
100 @param[in] FileInfo The information to set.
101
102 @retval EFI_SUCCESS The information was set.
103 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
104 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
105 @retval EFI_NO_MEDIA The device has no medium.
106 @retval EFI_DEVICE_ERROR The device reported an error.
107 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
108 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
109 @retval EFI_ACCESS_DENIED The file was opened read only.
110 @retval EFI_VOLUME_FULL The volume is full.
111 **/
112 EFI_STATUS
113 EFIAPI
114 FileHandleSetInfo (
115 IN EFI_FILE_HANDLE FileHandle,
116 IN CONST EFI_FILE_INFO *FileInfo
117 )
118 {
119
120 //
121 // ASSERT if the FileHandle or FileInfo is NULL
122 //
123 ASSERT (FileHandle != NULL);
124 ASSERT (FileInfo != NULL);
125
126 //
127 // Set the info
128 //
129 return (FileHandle->SetInfo(FileHandle,
130 &gEfiFileInfoGuid,
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 if (Position == NULL) {
346 return (EFI_INVALID_PARAMETER);
347 }
348 //
349 // ASSERT if FileHandle is NULL
350 //
351 ASSERT (FileHandle != NULL);
352 //
353 // Perform the GetPosition based on EFI_FILE_PROTOCOL
354 //
355 return (FileHandle->GetPosition(FileHandle, Position));
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 **/
371 EFI_STATUS
372 EFIAPI
373 FileHandleFlush (
374 IN EFI_FILE_HANDLE FileHandle
375 )
376 {
377 //
378 // ASSERT if FileHandle is NULL
379 //
380 ASSERT (FileHandle != NULL);
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 if DirHandle is NULL then ASSERT()
391
392 open the file information on the DirHandle and verify that the Attribute
393 includes EFI_FILE_DIRECTORY bit set.
394
395 @param DirHandle Handle to open file
396
397 @retval EFI_SUCCESS DirHandle is a directory
398 @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available
399 @retval EFI_NOT_FOUND DirHandle is not a directory
400 **/
401 EFI_STATUS
402 EFIAPI
403 FileHandleIsDirectory (
404 IN EFI_FILE_HANDLE DirHandle
405 )
406 {
407 EFI_FILE_INFO *DirInfo;
408
409 //
410 // ASSERT if DirHandle is NULL
411 //
412 ASSERT(DirHandle != NULL);
413
414 //
415 // get the file information for DirHandle
416 //
417 DirInfo = FileHandleGetInfo (DirHandle);
418
419 //
420 // Parse DirInfo
421 //
422 if (DirInfo == NULL) {
423 //
424 // We got nothing...
425 //
426 return (EFI_INVALID_PARAMETER);
427 }
428 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
429 //
430 // Attributes say this is not a directory
431 //
432 FreePool (DirInfo);
433 return (EFI_NOT_FOUND);
434 }
435 //
436 // all good...
437 //
438 FreePool (DirInfo);
439 return (EFI_SUCCESS);
440 }
441
442 /**
443 Retrieves the first file from a directory
444
445 This function opens a directory and gets the first file's info in the
446 directory. Caller can use FileHandleFindNextFile() to get other files. When
447 complete the caller is responsible for calling FreePool() on Buffer.
448
449 @param DirHandle The file handle of the directory to search
450 @param Buffer Pointer to buffer for file's information
451
452 @retval EFI_SUCCESS Found the first file.
453 @retval EFI_NOT_FOUND Cannot find the directory.
454 @retval EFI_NO_MEDIA The device has no media.
455 @retval EFI_DEVICE_ERROR The device reported an error.
456 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
457 @return Others status of FileHandleGetInfo, FileHandleSetPosition,
458 or FileHandleRead
459 **/
460 EFI_STATUS
461 EFIAPI
462 FileHandleFindFirstFile (
463 IN EFI_FILE_HANDLE DirHandle,
464 OUT EFI_FILE_INFO **Buffer
465 )
466 {
467 EFI_STATUS Status;
468 UINTN BufferSize;
469
470 //
471 // ASSERTs
472 //
473 ASSERT (DirHandle != NULL);
474 ASSERT (Buffer != NULL);
475
476 //
477 // verify that DirHandle is a directory
478 //
479 Status = FileHandleIsDirectory(DirHandle);
480 if (EFI_ERROR(Status)) {
481 return (Status);
482 }
483
484 //
485 // reset to the begining of the directory
486 //
487 Status = FileHandleSetPosition(DirHandle, 0);
488 if (EFI_ERROR(Status)) {
489 return (Status);
490 }
491
492 //
493 // Allocate a buffer sized to struct size + enough for the string at the end
494 //
495 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
496 *Buffer = AllocateZeroPool(BufferSize);
497 ASSERT (*Buffer != NULL);
498
499 //
500 // read in the info about the first file
501 //
502 Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);
503 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
504 if (EFI_ERROR(Status)) {
505 FreePool(*Buffer);
506 *Buffer = NULL;
507 return (Status);
508 }
509 return (EFI_SUCCESS);
510 }
511 /**
512 Retrieves the next file in a directory.
513
514 To use this function, caller must call the FileHandleFindFirstFile() to get the
515 first file, and then use this function get other files. This function can be
516 called for several times to get each file's information in the directory. If
517 the call of FileHandleFindNextFile() got the last file in the directory, the next
518 call of this function has no file to get. *NoFile will be set to TRUE and the
519 Buffer memory will be automatically freed.
520
521 @param DirHandle the file handle of the directory
522 @param Buffer pointer to buffer for file's information
523 @param NoFile pointer to boolean when last file is found
524
525 @retval EFI_SUCCESS Found the next file, or reached last file
526 @retval EFI_NO_MEDIA The device has no media.
527 @retval EFI_DEVICE_ERROR The device reported an error.
528 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
529 **/
530 EFI_STATUS
531 EFIAPI
532 FileHandleFindNextFile(
533 IN EFI_FILE_HANDLE DirHandle,
534 OUT EFI_FILE_INFO *Buffer,
535 OUT BOOLEAN *NoFile
536 )
537 {
538 EFI_STATUS Status;
539 UINTN BufferSize;
540
541 //
542 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL
543 //
544 ASSERT (DirHandle != NULL);
545 ASSERT (Buffer != NULL);
546 ASSERT (NoFile != NULL);
547
548 //
549 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
550 //
551 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
552
553 //
554 // read in the info about the next file
555 //
556 Status = FileHandleRead (DirHandle, &BufferSize, Buffer);
557 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
558 if (EFI_ERROR(Status)) {
559 return (Status);
560 }
561
562 //
563 // If we read 0 bytes (but did not have erros) we already read in the last file.
564 //
565 if (BufferSize == 0) {
566 FreePool(Buffer);
567 *NoFile = TRUE;
568 }
569
570 return (EFI_SUCCESS);
571 }
572
573 /**
574 Retrieve the size of a file.
575
576 if FileHandle is NULL then ASSERT()
577 if Size is NULL then ASSERT()
578
579 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
580 data.
581
582 @param FileHandle file handle from which size is retrieved
583 @param Size pointer to size
584
585 @retval EFI_SUCCESS operation was completed sucessfully
586 @retval EFI_DEVICE_ERROR cannot access the file
587 **/
588 EFI_STATUS
589 EFIAPI
590 FileHandleGetSize (
591 IN EFI_FILE_HANDLE FileHandle,
592 OUT UINT64 *Size
593 )
594 {
595 EFI_FILE_INFO *FileInfo;
596
597 //
598 // ASSERT for FileHandle or Size being NULL
599 //
600 ASSERT (FileHandle != NULL);
601 ASSERT (Size != NULL);
602
603 //
604 // get the FileInfo structure
605 //
606 FileInfo = FileHandleGetInfo(FileHandle);
607 if (FileInfo == NULL) {
608 return (EFI_DEVICE_ERROR);
609 }
610
611 //
612 // Assign the Size pointer to the correct value
613 //
614 *Size = FileInfo->FileSize;
615
616 //
617 // free the FileInfo memory
618 //
619 FreePool(FileInfo);
620
621 return (EFI_SUCCESS);
622 }
623
624 /**
625 Set the size of a file.
626
627 If FileHandle is NULL then ASSERT().
628
629 This function changes the file size info from the FileHandle's EFI_FILE_INFO
630 data.
631
632 @param FileHandle File handle whose size is to be changed.
633 @param Size New size.
634
635 @retval EFI_SUCCESS operation was completed sucessfully.
636 @retval EFI_DEVICE_ERROR cannot access the file.
637 **/
638 EFI_STATUS
639 EFIAPI
640 FileHandleSetSize (
641 IN EFI_FILE_HANDLE FileHandle,
642 IN UINT64 Size
643 )
644 {
645 EFI_FILE_INFO *FileInfo;
646 EFI_STATUS Status;
647
648 //
649 // ASSERT for FileHandle or Size being NULL
650 //
651 ASSERT (FileHandle != NULL);
652
653 //
654 // get the FileInfo structure
655 //
656 FileInfo = FileHandleGetInfo(FileHandle);
657 if (FileInfo == NULL) {
658 return (EFI_DEVICE_ERROR);
659 }
660
661 //
662 // Assign the FileSize pointer to the new value
663 //
664 FileInfo->FileSize = Size;
665
666 Status = FileHandleSetInfo(FileHandle, FileInfo);
667 //
668 // free the FileInfo memory
669 //
670 FreePool(FileInfo);
671
672 return (Status);
673 }
674
675 /**
676 Safely append (on the left) with automatic string resizing given length of Destination and
677 desired length of copy from Source.
678
679 append the first D characters of Source to the end of Destination, where D is
680 the lesser of Count and the StrLen() of Source. If appending those D characters
681 will fit within Destination (whose Size is given as CurrentSize) and
682 still leave room for a NULL terminator, then those characters are appended,
683 starting at the original terminating NULL of Destination, and a new terminating
684 NULL is appended.
685
686 If appending D characters onto Destination will result in a overflow of the size
687 given in CurrentSize the string will be grown such that the copy can be performed
688 and CurrentSize will be updated to the new size.
689
690 If Source is NULL, there is nothing to append, just return the current buffer in
691 Destination.
692
693 if Destination is NULL, then ASSERT()
694 if Destination's current length (including NULL terminator) is already more then
695 CurrentSize, then ASSERT()
696
697 @param[in,out] Destination The String to append onto
698 @param[in,out] CurrentSize on call the number of bytes in Destination. On
699 return possibly the new size (still in bytes). if NULL
700 then allocate whatever is needed.
701 @param[in] Source The String to append from
702 @param[in] Count Maximum number of characters to append. if 0 then
703 all are appended.
704
705 @return Destination return the resultant string.
706 **/
707 CHAR16*
708 EFIAPI
709 StrnCatGrowLeft (
710 IN OUT CHAR16 **Destination,
711 IN OUT UINTN *CurrentSize,
712 IN CONST CHAR16 *Source,
713 IN UINTN Count
714 )
715 {
716 UINTN DestinationStartSize;
717 UINTN NewSize;
718 UINTN CopySize;
719
720 //
721 // ASSERTs
722 //
723 ASSERT(Destination != NULL);
724
725 //
726 // If there's nothing to do then just return Destination
727 //
728 if (Source == NULL) {
729 return (*Destination);
730 }
731
732 //
733 // allow for NULL pointers address as Destination
734 //
735 if (*Destination != NULL) {
736 ASSERT(CurrentSize != 0);
737 DestinationStartSize = StrSize(*Destination);
738 ASSERT(DestinationStartSize <= *CurrentSize);
739 } else {
740 DestinationStartSize = 0;
741 // ASSERT(*CurrentSize == 0);
742 }
743
744 //
745 // Append all of Source?
746 //
747 if (Count == 0) {
748 Count = StrSize(Source);
749 }
750
751 //
752 // Test and grow if required
753 //
754 if (CurrentSize != NULL) {
755 NewSize = *CurrentSize;
756 while (NewSize < (DestinationStartSize + Count)) {
757 NewSize += 2 * Count;
758 }
759 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
760 *CurrentSize = NewSize;
761 } else {
762 *Destination = AllocateZeroPool(Count+sizeof(CHAR16));
763 }
764
765 CopySize = StrSize(*Destination);
766 CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize);
767 CopyMem(*Destination, Source, Count-2);
768 return (*Destination);
769 }
770
771 /**
772 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
773 directory 'stack'.
774
775 if Handle is NULL, return EFI_INVALID_PARAMETER
776
777 @param[in] Handle Handle to the Directory or File to create path to.
778 @param[out] FullFileName pointer to pointer to generated full file name. It
779 is the responsibility of the caller to free this memory
780 with a call to FreePool().
781 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
782 @retval EFI_INVALID_PARAMETER Handle was NULL.
783 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
784 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
785 **/
786 EFI_STATUS
787 EFIAPI
788 FileHandleGetFileName (
789 IN CONST EFI_FILE_HANDLE Handle,
790 OUT CHAR16 **FullFileName
791 )
792 {
793 EFI_STATUS Status;
794 UINTN Size;
795 EFI_FILE_HANDLE CurrentHandle;
796 EFI_FILE_HANDLE NextHigherHandle;
797 EFI_FILE_INFO *FileInfo;
798
799 Size = 0;
800
801 //
802 // Check our parameters
803 //
804 if (FullFileName == NULL || Handle == NULL) {
805 return (EFI_INVALID_PARAMETER);
806 }
807
808 *FullFileName = NULL;
809 CurrentHandle = NULL;
810
811 Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);
812 if (!EFI_ERROR(Status)) {
813 //
814 // Reverse out the current directory on the device
815 //
816 for (;;) {
817 FileInfo = FileHandleGetInfo(CurrentHandle);
818 if (FileInfo == NULL) {
819 Status = EFI_OUT_OF_RESOURCES;
820 break;
821 } else {
822 //
823 // We got info... do we have a name? if yes preceed the current path with it...
824 //
825 if (StrLen (FileInfo->FileName) == 0) {
826 if (*FullFileName == NULL) {
827 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
828 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
829 }
830 FreePool(FileInfo);
831 break;
832 } else {
833 if (*FullFileName == NULL) {
834 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
835 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
836 }
837 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
838 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);
839 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
840 FreePool(FileInfo);
841 }
842 }
843 //
844 // Move to the parent directory
845 //
846 Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);
847 if (EFI_ERROR (Status)) {
848 break;
849 }
850
851 FileHandleClose(CurrentHandle);
852 CurrentHandle = NextHigherHandle;
853 }
854 } else if (Status == EFI_NOT_FOUND) {
855 Status = EFI_SUCCESS;
856 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
857 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
858 }
859
860 if (CurrentHandle != NULL) {
861 CurrentHandle->Close (CurrentHandle);
862 }
863
864 if (EFI_ERROR(Status) && *FullFileName != NULL) {
865 FreePool(*FullFileName);
866 }
867
868 return (Status);
869 }
870
871 /**
872 Function to read a single line from a file. The \n is not included in the returned
873 buffer. The returned buffer must be callee freed.
874
875 If the position upon start is 0, then the Ascii Boolean will be set. This should be
876 maintained and not changed for all operations with the same file.
877
878 @param[in] Handle FileHandle to read from.
879 @param[in,out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
880
881 @return The line of text from the file.
882
883 @sa FileHandleReadLine
884 **/
885 CHAR16*
886 EFIAPI
887 FileHandleReturnLine(
888 IN EFI_FILE_HANDLE Handle,
889 IN OUT BOOLEAN *Ascii
890 )
891 {
892 CHAR16 *RetVal;
893 UINTN Size;
894 EFI_STATUS Status;
895
896 Size = 0;
897 RetVal = NULL;
898
899 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
900 if (Status == EFI_BUFFER_TOO_SMALL) {
901 RetVal = AllocatePool(Size);
902 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
903 }
904 ASSERT_EFI_ERROR(Status);
905 if (EFI_ERROR(Status) && (RetVal != NULL)) {
906 FreePool(RetVal);
907 RetVal = NULL;
908 }
909 return (RetVal);
910 }
911
912 /**
913 Function to read a single line (up to but not including the \n) from a EFI_FILE_HANDLE.
914
915 If the position upon start is 0, then the Ascii Boolean will be set. This should be
916 maintained and not changed for all operations with the same file.
917
918 @param[in] Handle FileHandle to read from
919 @param[in,out] Buffer pointer to buffer to read into
920 @param[in,out] Size pointer to number of bytes in buffer
921 @param[in] Truncate if TRUE then allows for truncation of the line to fit.
922 if FALSE will reset the position to the begining of the
923 line if the buffer is not large enough.
924 @param[in,out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
925
926 @retval EFI_SUCCESS the operation was sucessful. the line is stored in
927 Buffer.
928 @retval EFI_INVALID_PARAMETER Handle was NULL.
929 @retval EFI_INVALID_PARAMETER Size was NULL.
930 @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line.
931 Size was updated to minimum space required.
932 @sa FileHandleRead
933 **/
934 EFI_STATUS
935 EFIAPI
936 FileHandleReadLine(
937 IN EFI_FILE_HANDLE Handle,
938 IN OUT CHAR16 *Buffer,
939 IN OUT UINTN *Size,
940 IN BOOLEAN Truncate,
941 IN OUT BOOLEAN *Ascii
942 )
943 {
944 EFI_STATUS Status;
945 CHAR16 CharBuffer;
946 UINTN CharSize;
947 UINTN CountSoFar;
948 UINT64 OriginalFilePosition;
949
950
951 if (Handle == NULL
952 ||Size == NULL
953 ){
954 return (EFI_INVALID_PARAMETER);
955 }
956 if (Buffer == NULL) {
957 ASSERT(*Size == 0);
958 } else {
959 *Buffer = CHAR_NULL;
960 }
961 FileHandleGetPosition(Handle, &OriginalFilePosition);
962 if (OriginalFilePosition == 0) {
963 CharSize = sizeof(CHAR16);
964 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);
965 ASSERT_EFI_ERROR(Status);
966 if (CharBuffer == UnicodeFileTag) {
967 *Ascii = FALSE;
968 } else {
969 *Ascii = TRUE;
970 FileHandleSetPosition(Handle, OriginalFilePosition);
971 }
972 }
973
974 for (CountSoFar = 0;;CountSoFar++){
975 CharBuffer = 0;
976 if (*Ascii) {
977 CharSize = sizeof(CHAR8);
978 } else {
979 CharSize = sizeof(CHAR16);
980 }
981 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);
982 if ( EFI_ERROR(Status)
983 || CharSize == 0
984 || (CharBuffer == L'\n' && !(*Ascii))
985 || (CharBuffer == '\n' && *Ascii)
986 ){
987 break;
988 }
989 //
990 // if we have space save it...
991 //
992 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
993 ASSERT(Buffer != NULL);
994 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
995 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
996 }
997 }
998
999 //
1000 // if we ran out of space tell when...
1001 //
1002 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
1003 *Size = (CountSoFar+1)*sizeof(CHAR16);
1004 if (!Truncate) {
1005 FileHandleSetPosition(Handle, OriginalFilePosition);
1006 } else {
1007 DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));
1008 }
1009 return (EFI_BUFFER_TOO_SMALL);
1010 }
1011 while(Buffer[StrLen(Buffer)-1] == L'\r') {
1012 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
1013 }
1014
1015 return (Status);
1016 }
1017
1018 /**
1019 function to write a line of unicode text to a file.
1020
1021 if Handle is NULL, ASSERT.
1022 if Buffer is NULL, do nothing. (return SUCCESS)
1023
1024 @param[in] Handle FileHandle to write to
1025 @param[in] Buffer Buffer to write
1026
1027 @retval EFI_SUCCESS the data was written.
1028 @retval other failure.
1029
1030 @sa FileHandleWrite
1031 **/
1032 EFI_STATUS
1033 EFIAPI
1034 FileHandleWriteLine(
1035 IN EFI_FILE_HANDLE Handle,
1036 IN CHAR16 *Buffer
1037 )
1038 {
1039 EFI_STATUS Status;
1040 UINTN Size;
1041
1042 ASSERT(Handle != NULL);
1043
1044 if (Buffer == NULL) {
1045 return (EFI_SUCCESS);
1046 }
1047
1048 Size = StrSize(Buffer) - sizeof(Buffer[0]);
1049 Status = FileHandleWrite(Handle, &Size, Buffer);
1050 if (EFI_ERROR(Status)) {
1051 return (Status);
1052 }
1053 Size = StrSize(L"\r\n") - sizeof(CHAR16);
1054 return FileHandleWrite(Handle, &Size, L"\r\n");
1055 }
1056
1057 /**
1058 function to take a formatted argument and print it to a file.
1059
1060 @param[in] Handle the file handle for the file to write to
1061 @param[in] Format the format argument (see printlib for format specifier)
1062 @param[in] ... the variable arguments for the format
1063
1064 @retval EFI_SUCCESS the operation was sucessful
1065 @return other a return value from FileHandleWriteLine
1066
1067 @sa FileHandleWriteLine
1068 **/
1069 EFI_STATUS
1070 EFIAPI
1071 FileHandlePrintLine(
1072 IN EFI_FILE_HANDLE Handle,
1073 IN CONST CHAR16 *Format,
1074 ...
1075 )
1076 {
1077 VA_LIST Marker;
1078 CHAR16 *Buffer;
1079 EFI_STATUS Status;
1080
1081 VA_START (Marker, Format);
1082
1083 //
1084 // Get a buffer to print into
1085 //
1086 Buffer = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
1087 ASSERT (Buffer != NULL);
1088
1089 //
1090 // Print into our buffer
1091 //
1092 UnicodeVSPrint (Buffer, PcdGet16 (PcdShellPrintBufferSize), Format, Marker);
1093
1094 //
1095 // Print buffer into file
1096 //
1097 Status = FileHandleWriteLine(Handle, Buffer);
1098
1099 //
1100 // Cleanup and return
1101 //
1102 FreePool(Buffer);
1103 return (Status);
1104 }
1105
1106 /**
1107 Function to determine if a FILE_HANDLE is at the end of the file.
1108
1109 This will NOT work on directories.
1110
1111 If Handle is NULL, then ASSERT.
1112
1113 @param[in] Handle the file handle
1114
1115 @retval TRUE the position is at the end of the file
1116 @retval FALSE the position is not at the end of the file
1117 **/
1118 BOOLEAN
1119 EFIAPI
1120 FileHandleEof(
1121 IN EFI_FILE_HANDLE Handle
1122 )
1123 {
1124 EFI_FILE_INFO *Info;
1125 UINT64 Pos;
1126 BOOLEAN RetVal;
1127
1128 //
1129 // ASSERT if Handle is NULL
1130 //
1131 ASSERT(Handle != NULL);
1132
1133 FileHandleGetPosition(Handle, &Pos);
1134 Info = FileHandleGetInfo (Handle);
1135 ASSERT(Info != NULL);
1136 FileHandleSetPosition(Handle, Pos);
1137
1138 if (Info == NULL) {
1139 return (FALSE);
1140 }
1141
1142 if (Pos == Info->FileSize) {
1143 RetVal = TRUE;
1144 } else {
1145 RetVal = FALSE;
1146 }
1147
1148 FreePool (Info);
1149
1150 return (RetVal);
1151 }