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