]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
MdePkg\Library\UefiFileHandleLib: Make FileHandleWriteLine support both ASCII and...
[mirror_edk2.git] / MdePkg / Library / UefiFileHandleLib / UefiFileHandleLib.c
1 /** @file
2 Provides interface to EFI_FILE_HANDLE functionality.
3
4 Copyright (c) 2006 - 2015, 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) && (FileInfo != NULL)) {
88 FreePool(FileInfo);
89 FileInfo = 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 if (FileHandle == NULL || FileInfo == NULL) {
123 return (EFI_INVALID_PARAMETER);
124 }
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 if (FileHandle == NULL) {
173 return (EFI_INVALID_PARAMETER);
174 }
175
176 //
177 // Perform the read based on EFI_FILE_PROTOCOL
178 //
179 return (FileHandle->Read(FileHandle, BufferSize, Buffer));
180 }
181
182
183 /**
184 Write data to a file.
185
186 This function writes the specified number of bytes to the file at the current
187 file position. The current file position is advanced the actual number of bytes
188 written, which is returned in BufferSize. Partial writes only occur when there
189 has been a data error during the write attempt (such as "volume space full").
190 The file is automatically grown to hold the data if required. Direct writes to
191 opened directories are not supported.
192
193 @param FileHandle The opened file for writing
194 @param BufferSize on input the number of bytes in Buffer. On output
195 the number of bytes written.
196 @param Buffer the buffer containing data to write is stored.
197
198 @retval EFI_SUCCESS Data was written.
199 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
200 @retval EFI_NO_MEDIA The device has no media.
201 @retval EFI_DEVICE_ERROR The device reported an error.
202 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
203 @retval EFI_WRITE_PROTECTED The device is write-protected.
204 @retval EFI_ACCESS_DENIED The file was open for read only.
205 @retval EFI_VOLUME_FULL The volume is full.
206 **/
207 EFI_STATUS
208 EFIAPI
209 FileHandleWrite(
210 IN EFI_FILE_HANDLE FileHandle,
211 IN OUT UINTN *BufferSize,
212 IN VOID *Buffer
213 )
214 {
215 if (FileHandle == NULL) {
216 return (EFI_INVALID_PARAMETER);
217 }
218
219 //
220 // Perform the write based on EFI_FILE_PROTOCOL
221 //
222 return (FileHandle->Write(FileHandle, BufferSize, Buffer));
223 }
224
225 /**
226 Close an open file handle.
227
228 This function closes a specified file handle. All "dirty" cached file data is
229 flushed to the device, and the file is closed. In all cases the handle is
230 closed.
231
232 @param FileHandle the file handle to close.
233
234 @retval EFI_SUCCESS the file handle was closed sucessfully.
235 **/
236 EFI_STATUS
237 EFIAPI
238 FileHandleClose (
239 IN EFI_FILE_HANDLE FileHandle
240 )
241 {
242 EFI_STATUS Status;
243
244 if (FileHandle == NULL) {
245 return (EFI_INVALID_PARAMETER);
246 }
247
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 if (FileHandle == NULL) {
278 return (EFI_INVALID_PARAMETER);
279 }
280
281 //
282 // Perform the Delete based on EFI_FILE_PROTOCOL
283 //
284 Status = FileHandle->Delete(FileHandle);
285 return Status;
286 }
287
288 /**
289 Set the current position in a file.
290
291 This function sets the current file position for the handle to the position
292 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only
293 absolute positioning is supported, and seeking past the end of the file is
294 allowed (a subsequent write would grow the file). Seeking to position
295 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
296 If FileHandle is a directory, the only position that may be set is zero. This
297 has the effect of starting the read process of the directory entries over.
298
299 @param FileHandle The file handle on which the position is being set
300 @param Position Byte position from begining of file
301
302 @retval EFI_SUCCESS Operation completed sucessfully.
303 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on
304 directories.
305 @retval INVALID_PARAMETER One of the parameters has an invalid value.
306 **/
307 EFI_STATUS
308 EFIAPI
309 FileHandleSetPosition (
310 IN EFI_FILE_HANDLE FileHandle,
311 IN UINT64 Position
312 )
313 {
314 if (FileHandle == NULL) {
315 return (EFI_INVALID_PARAMETER);
316 }
317
318 //
319 // Perform the SetPosition based on EFI_FILE_PROTOCOL
320 //
321 return (FileHandle->SetPosition(FileHandle, Position));
322 }
323
324 /**
325 Gets a file's current position
326
327 This function retrieves the current file position for the file handle. For
328 directories, the current file position has no meaning outside of the file
329 system driver and as such the operation is not supported. An error is returned
330 if FileHandle is a directory.
331
332 @param FileHandle The open file handle on which to get the position.
333 @param Position Byte position from begining of file.
334
335 @retval EFI_SUCCESS the operation completed sucessfully.
336 @retval INVALID_PARAMETER One of the parameters has an invalid value.
337 @retval EFI_UNSUPPORTED the request is not valid on directories.
338 **/
339 EFI_STATUS
340 EFIAPI
341 FileHandleGetPosition (
342 IN EFI_FILE_HANDLE FileHandle,
343 OUT UINT64 *Position
344 )
345 {
346 if (Position == NULL || FileHandle == NULL) {
347 return (EFI_INVALID_PARAMETER);
348 }
349
350 //
351 // Perform the GetPosition based on EFI_FILE_PROTOCOL
352 //
353 return (FileHandle->GetPosition(FileHandle, Position));
354 }
355 /**
356 Flushes data on a file
357
358 This function flushes all modified data associated with a file to a device.
359
360 @param FileHandle The file handle on which to flush data
361
362 @retval EFI_SUCCESS The data was flushed.
363 @retval EFI_NO_MEDIA The device has no media.
364 @retval EFI_DEVICE_ERROR The device reported an error.
365 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
366 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
367 @retval EFI_ACCESS_DENIED The file was opened for read only.
368 **/
369 EFI_STATUS
370 EFIAPI
371 FileHandleFlush (
372 IN EFI_FILE_HANDLE FileHandle
373 )
374 {
375 if (FileHandle == NULL) {
376 return (EFI_INVALID_PARAMETER);
377 }
378
379 //
380 // Perform the Flush based on EFI_FILE_PROTOCOL
381 //
382 return (FileHandle->Flush(FileHandle));
383 }
384
385 /**
386 Function to determine if a given handle is a directory handle.
387
388 Open the file information on the DirHandle and verify that the Attribute
389 includes EFI_FILE_DIRECTORY bit set.
390
391 @param[in] DirHandle Handle to open file.
392
393 @retval EFI_SUCCESS DirHandle is a directory.
394 @retval EFI_INVALID_PARAMETER DirHandle is NULL.
395 The file information returns from FileHandleGetInfo is NULL.
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 if (DirHandle == NULL) {
407 return (EFI_INVALID_PARAMETER);
408 }
409
410 //
411 // get the file information for DirHandle
412 //
413 DirInfo = FileHandleGetInfo (DirHandle);
414
415 //
416 // Parse DirInfo
417 //
418 if (DirInfo == NULL) {
419 //
420 // We got nothing...
421 //
422 return (EFI_INVALID_PARAMETER);
423 }
424 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {
425 //
426 // Attributes say this is not a directory
427 //
428 FreePool (DirInfo);
429 return (EFI_NOT_FOUND);
430 }
431 //
432 // all good...
433 //
434 FreePool (DirInfo);
435 return (EFI_SUCCESS);
436 }
437
438 /** Retrieve first entry from a directory.
439
440 This function takes an open directory handle and gets information from the
441 first entry in the directory. A buffer is allocated to contain
442 the information and a pointer to the buffer is returned in *Buffer. The
443 caller can use FileHandleFindNextFile() to get subsequent directory entries.
444
445 The buffer will be freed by FileHandleFindNextFile() when the last directory
446 entry is read. Otherwise, the caller must free the buffer, using FreePool,
447 when finished with it.
448
449 @param[in] DirHandle The file handle of the directory to search.
450 @param[out] Buffer The pointer to 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 if (Buffer == NULL || DirHandle == NULL) {
471 return (EFI_INVALID_PARAMETER);
472 }
473
474 //
475 // verify that DirHandle is a directory
476 //
477 Status = FileHandleIsDirectory(DirHandle);
478 if (EFI_ERROR(Status)) {
479 return (Status);
480 }
481
482 //
483 // Allocate a buffer sized to struct size + enough for the string at the end
484 //
485 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
486 *Buffer = AllocateZeroPool(BufferSize);
487 if (*Buffer == NULL){
488 return (EFI_OUT_OF_RESOURCES);
489 }
490
491 //
492 // reset to the begining of the directory
493 //
494 Status = FileHandleSetPosition(DirHandle, 0);
495 if (EFI_ERROR(Status)) {
496 FreePool(*Buffer);
497 *Buffer = NULL;
498 return (Status);
499 }
500
501 //
502 // read in the info about the first file
503 //
504 Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);
505 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
506 if (EFI_ERROR(Status) || BufferSize == 0) {
507 FreePool(*Buffer);
508 *Buffer = NULL;
509 if (BufferSize == 0) {
510 return (EFI_NOT_FOUND);
511 }
512 return (Status);
513 }
514 return (EFI_SUCCESS);
515 }
516
517 /** Retrieve next entries from a directory.
518
519 To use this function, the caller must first call the FileHandleFindFirstFile()
520 function to get the first directory entry. Subsequent directory entries are
521 retrieved by using the FileHandleFindNextFile() function. This function can
522 be called several times to get each entry from the directory. If the call of
523 FileHandleFindNextFile() retrieved the last directory entry, the next call of
524 this function will set *NoFile to TRUE and free the buffer.
525
526 @param[in] DirHandle The file handle of the directory.
527 @param[out] Buffer The pointer to buffer for file's information.
528 @param[out] NoFile The pointer to boolean when last file is found.
529
530 @retval EFI_SUCCESS Found the next file, or reached last file
531 @retval EFI_NO_MEDIA The device has no media.
532 @retval EFI_DEVICE_ERROR The device reported an error.
533 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
534 **/
535 EFI_STATUS
536 EFIAPI
537 FileHandleFindNextFile(
538 IN EFI_FILE_HANDLE DirHandle,
539 OUT EFI_FILE_INFO *Buffer,
540 OUT BOOLEAN *NoFile
541 )
542 {
543 EFI_STATUS Status;
544 UINTN BufferSize;
545
546 if (DirHandle == NULL || Buffer == NULL || NoFile == NULL) {
547 return (EFI_INVALID_PARAMETER);
548 }
549
550 //
551 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile
552 //
553 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;
554
555 //
556 // read in the info about the next file
557 //
558 Status = FileHandleRead (DirHandle, &BufferSize, Buffer);
559 ASSERT(Status != EFI_BUFFER_TOO_SMALL);
560 if (EFI_ERROR(Status)) {
561 return (Status);
562 }
563
564 //
565 // If we read 0 bytes (but did not have erros) we already read in the last file.
566 //
567 if (BufferSize == 0) {
568 FreePool(Buffer);
569 *NoFile = TRUE;
570 }
571
572 return (EFI_SUCCESS);
573 }
574
575 /**
576 Retrieve the size of a file.
577
578 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
579 data.
580
581 @param[in] FileHandle The file handle from which size is retrieved.
582 @param[out] Size The pointer to size.
583
584 @retval EFI_SUCCESS Operation was completed sucessfully.
585 @retval EFI_DEVICE_ERROR Cannot access the file.
586 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
587 Size is NULL.
588 **/
589 EFI_STATUS
590 EFIAPI
591 FileHandleGetSize (
592 IN EFI_FILE_HANDLE FileHandle,
593 OUT UINT64 *Size
594 )
595 {
596 EFI_FILE_INFO *FileInfo;
597
598 if (FileHandle == NULL || Size == NULL) {
599 return (EFI_INVALID_PARAMETER);
600 }
601
602 //
603 // get the FileInfo structure
604 //
605 FileInfo = FileHandleGetInfo(FileHandle);
606 if (FileInfo == NULL) {
607 return (EFI_DEVICE_ERROR);
608 }
609
610 //
611 // Assign the Size pointer to the correct value
612 //
613 *Size = FileInfo->FileSize;
614
615 //
616 // free the FileInfo memory
617 //
618 FreePool(FileInfo);
619
620 return (EFI_SUCCESS);
621 }
622
623 /**
624 Set the size of a file.
625
626 This function changes the file size info from the FileHandle's EFI_FILE_INFO
627 data.
628
629 @param[in] FileHandle The file handle whose size is to be changed.
630 @param[in] Size The new size.
631
632 @retval EFI_SUCCESS The operation completed successfully.
633 @retval EFI_DEVICE_ERROR Cannot access the file.
634 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
635 **/
636 EFI_STATUS
637 EFIAPI
638 FileHandleSetSize (
639 IN EFI_FILE_HANDLE FileHandle,
640 IN UINT64 Size
641 )
642 {
643 EFI_FILE_INFO *FileInfo;
644 EFI_STATUS Status;
645
646 if (FileHandle == NULL) {
647 return (EFI_INVALID_PARAMETER);
648 }
649
650 //
651 // get the FileInfo structure
652 //
653 FileInfo = FileHandleGetInfo(FileHandle);
654 if (FileInfo == NULL) {
655 return (EFI_DEVICE_ERROR);
656 }
657
658 //
659 // Assign the FileSize pointer to the new value
660 //
661 FileInfo->FileSize = Size;
662
663 Status = FileHandleSetInfo(FileHandle, FileInfo);
664 //
665 // free the FileInfo memory
666 //
667 FreePool(FileInfo);
668
669 return (Status);
670 }
671
672 /**
673 Safely append (on the left) with automatic string resizing given length of Destination and
674 desired length of copy from Source.
675
676 append the first D characters of Source to the end of Destination, where D is
677 the lesser of Count and the StrLen() of Source. If appending those D characters
678 will fit within Destination (whose Size is given as CurrentSize) and
679 still leave room for a NULL terminator, then those characters are appended,
680 starting at the original terminating NULL of Destination, and a new terminating
681 NULL is appended.
682
683 If appending D characters onto Destination will result in a overflow of the size
684 given in CurrentSize the string will be grown such that the copy can be performed
685 and CurrentSize will be updated to the new size.
686
687 If Source is NULL, there is nothing to append, just return the current buffer in
688 Destination.
689
690 if Destination is NULL, then return error
691 if Destination's current length (including NULL terminator) is already more then
692 CurrentSize, then ASSERT()
693
694 @param[in, out] Destination The String to append onto
695 @param[in, out] CurrentSize on call the number of bytes in Destination. On
696 return possibly the new size (still in bytes). if NULL
697 then allocate whatever is needed.
698 @param[in] Source The String to append from
699 @param[in] Count Maximum number of characters to append. if 0 then
700 all are appended.
701
702 @return Destination return the resultant string.
703 **/
704 CHAR16*
705 EFIAPI
706 StrnCatGrowLeft (
707 IN OUT CHAR16 **Destination,
708 IN OUT UINTN *CurrentSize,
709 IN CONST CHAR16 *Source,
710 IN UINTN Count
711 )
712 {
713 UINTN DestinationStartSize;
714 UINTN NewSize;
715 UINTN CopySize;
716
717 if (Destination == NULL) {
718 return (NULL);
719 }
720
721 //
722 // If there's nothing to do then just return Destination
723 //
724 if (Source == NULL) {
725 return (*Destination);
726 }
727
728 //
729 // allow for NULL pointers address as Destination
730 //
731 if (*Destination != NULL) {
732 ASSERT(CurrentSize != 0);
733 DestinationStartSize = StrSize(*Destination);
734 ASSERT(DestinationStartSize <= *CurrentSize);
735 } else {
736 DestinationStartSize = 0;
737 // ASSERT(*CurrentSize == 0);
738 }
739
740 //
741 // Append all of Source?
742 //
743 if (Count == 0) {
744 Count = StrSize(Source);
745 }
746
747 //
748 // Test and grow if required
749 //
750 if (CurrentSize != NULL) {
751 NewSize = *CurrentSize;
752 while (NewSize < (DestinationStartSize + Count)) {
753 NewSize += 2 * Count;
754 }
755 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);
756 *CurrentSize = NewSize;
757 } else {
758 *Destination = AllocateZeroPool(Count+sizeof(CHAR16));
759 }
760 if (*Destination == NULL) {
761 return NULL;
762 }
763
764 CopySize = StrSize(*Destination);
765 CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize);
766 CopyMem(*Destination, Source, Count-2);
767 return (*Destination);
768 }
769
770 /**
771 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
772 directory 'stack'.
773
774 if Handle is NULL, return EFI_INVALID_PARAMETER
775
776 @param[in] Handle Handle to the Directory or File to create path to.
777 @param[out] FullFileName pointer to pointer to generated full file name. It
778 is the responsibility of the caller to free this memory
779 with a call to FreePool().
780 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.
781 @retval EFI_INVALID_PARAMETER Handle was NULL.
782 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
783 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
784 **/
785 EFI_STATUS
786 EFIAPI
787 FileHandleGetFileName (
788 IN CONST EFI_FILE_HANDLE Handle,
789 OUT CHAR16 **FullFileName
790 )
791 {
792 EFI_STATUS Status;
793 UINTN Size;
794 EFI_FILE_HANDLE CurrentHandle;
795 EFI_FILE_HANDLE NextHigherHandle;
796 EFI_FILE_INFO *FileInfo;
797
798 Size = 0;
799
800 //
801 // Check our parameters
802 //
803 if (FullFileName == NULL || Handle == NULL) {
804 return (EFI_INVALID_PARAMETER);
805 }
806
807 *FullFileName = NULL;
808 CurrentHandle = NULL;
809
810 Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);
811 if (!EFI_ERROR(Status)) {
812 //
813 // Reverse out the current directory on the device
814 //
815 for (;;) {
816 FileInfo = FileHandleGetInfo(CurrentHandle);
817 if (FileInfo == NULL) {
818 Status = EFI_OUT_OF_RESOURCES;
819 break;
820 } else {
821 //
822 // We got info... do we have a name? if yes preceed the current path with it...
823 //
824 if (StrLen (FileInfo->FileName) == 0) {
825 if (*FullFileName == NULL) {
826 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
827 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
828 }
829 FreePool(FileInfo);
830 break;
831 } else {
832 if (*FullFileName == NULL) {
833 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
834 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
835 }
836 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
837 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);
838 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
839 FreePool(FileInfo);
840 }
841 }
842 //
843 // Move to the parent directory
844 //
845 Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);
846 if (EFI_ERROR (Status)) {
847 break;
848 }
849
850 FileHandleClose(CurrentHandle);
851 CurrentHandle = NextHigherHandle;
852 }
853 } else if (Status == EFI_NOT_FOUND) {
854 Status = EFI_SUCCESS;
855 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));
856 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);
857 }
858
859 if (CurrentHandle != NULL) {
860 CurrentHandle->Close (CurrentHandle);
861 }
862
863 if (EFI_ERROR(Status) && *FullFileName != NULL) {
864 FreePool(*FullFileName);
865 }
866
867 return (Status);
868 }
869
870 /**
871 Function to read a single line from a file. The \n is not included in the returned
872 buffer. The returned buffer must be callee freed.
873
874 If the position upon start is 0, then the Ascii Boolean will be set. This should be
875 maintained and not changed for all operations with the same file.
876
877 @param[in] Handle FileHandle to read from.
878 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
879
880 @return The line of text from the file.
881
882 @sa FileHandleReadLine
883 **/
884 CHAR16*
885 EFIAPI
886 FileHandleReturnLine(
887 IN EFI_FILE_HANDLE Handle,
888 IN OUT BOOLEAN *Ascii
889 )
890 {
891 CHAR16 *RetVal;
892 UINTN Size;
893 EFI_STATUS Status;
894
895 Size = 0;
896 RetVal = NULL;
897
898 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
899 if (Status == EFI_BUFFER_TOO_SMALL) {
900 RetVal = AllocateZeroPool(Size);
901 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
902 }
903 ASSERT_EFI_ERROR(Status);
904 if (EFI_ERROR(Status) && (RetVal != NULL)) {
905 FreePool(RetVal);
906 RetVal = NULL;
907 }
908 return (RetVal);
909 }
910
911 /**
912 Function to read a single line (up to but not including the \n) from a file.
913
914 If the position upon start is 0, then the Ascii Boolean will be set. This should be
915 maintained and not changed for all operations with the same file.
916
917 @param[in] Handle FileHandle to read from.
918 @param[in, out] Buffer The pointer to buffer to read into.
919 @param[in, out] Size The pointer to number of bytes in Buffer.
920 @param[in] Truncate If the buffer is large enough, this has no effect.
921 If the buffer is is too small and Truncate is TRUE,
922 the line will be truncated.
923 If the buffer is is too small and Truncate is FALSE,
924 then no read will occur.
925
926 @param[in, out] Ascii Boolean value for indicating whether the file is
927 Ascii (TRUE) or UCS2 (FALSE).
928
929 @retval EFI_SUCCESS The operation was successful. The line is stored in
930 Buffer.
931 @retval EFI_INVALID_PARAMETER Handle was NULL.
932 @retval EFI_INVALID_PARAMETER Size was NULL.
933 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
934 Size was updated to the minimum space required.
935 @sa FileHandleRead
936 **/
937 EFI_STATUS
938 EFIAPI
939 FileHandleReadLine(
940 IN EFI_FILE_HANDLE Handle,
941 IN OUT CHAR16 *Buffer,
942 IN OUT UINTN *Size,
943 IN BOOLEAN Truncate,
944 IN OUT BOOLEAN *Ascii
945 )
946 {
947 EFI_STATUS Status;
948 CHAR16 CharBuffer;
949 UINT64 FileSize;
950 UINTN CharSize;
951 UINTN CountSoFar;
952 UINT64 OriginalFilePosition;
953
954 if (Handle == NULL
955 ||Size == NULL
956 ||(Buffer==NULL&&*Size!=0)
957 ){
958 return (EFI_INVALID_PARAMETER);
959 }
960
961 if (Buffer != NULL) {
962 *Buffer = CHAR_NULL;
963 }
964
965 Status = FileHandleGetSize (Handle, &FileSize);
966 if (EFI_ERROR (Status)) {
967 return Status;
968 } else if (FileSize == 0) {
969 return EFI_SUCCESS;
970 }
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 text to a file.
1031
1032 If the file is a Unicode file (with UNICODE file tag) then write the unicode
1033 text.
1034 If the file is an ASCII file then write the ASCII text.
1035 If the size of file is zero (without file tag at the beginning) then write
1036 ASCII text as default.
1037
1038 @param[in] Handle FileHandle to write to.
1039 @param[in] Buffer Buffer to write, if NULL the function will
1040 take no action and return EFI_SUCCESS.
1041
1042 @retval EFI_SUCCESS The data was written.
1043 Buffer is NULL.
1044 @retval EFI_INVALID_PARAMETER Handle is NULL.
1045 @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
1046 string due to out of resources.
1047
1048 @sa FileHandleWrite
1049 **/
1050 EFI_STATUS
1051 EFIAPI
1052 FileHandleWriteLine(
1053 IN EFI_FILE_HANDLE Handle,
1054 IN CHAR16 *Buffer
1055 )
1056 {
1057 EFI_STATUS Status;
1058 CHAR16 CharBuffer;
1059 UINTN Size;
1060 UINTN CharSize;
1061 UINT64 FileSize;
1062 UINT64 OriginalFilePosition;
1063 BOOLEAN Ascii;
1064 CHAR8 *AsciiBuffer;
1065
1066 if (Buffer == NULL) {
1067 return (EFI_SUCCESS);
1068 }
1069
1070 if (Handle == NULL) {
1071 return (EFI_INVALID_PARAMETER);
1072 }
1073
1074 Ascii = FALSE;
1075 AsciiBuffer = NULL;
1076
1077 Status = FileHandleGetPosition(Handle, &OriginalFilePosition);
1078 if (EFI_ERROR(Status)) {
1079 return Status;
1080 }
1081
1082 Status = FileHandleSetPosition(Handle, 0);
1083 if (EFI_ERROR(Status)) {
1084 return Status;
1085 }
1086
1087 Status = FileHandleGetSize(Handle, &FileSize);
1088 if (EFI_ERROR(Status)) {
1089 return Status;
1090 }
1091
1092 if (FileSize == 0) {
1093 Ascii = TRUE;
1094 } else {
1095 CharSize = sizeof (CHAR16);
1096 Status = FileHandleRead (Handle, &CharSize, &CharBuffer);
1097 ASSERT_EFI_ERROR (Status);
1098 if (CharBuffer == gUnicodeFileTag) {
1099 Ascii = FALSE;
1100 } else {
1101 Ascii = TRUE;
1102 }
1103 }
1104
1105 Status = FileHandleSetPosition(Handle, OriginalFilePosition);
1106 if (EFI_ERROR(Status)) {
1107 return Status;
1108 }
1109
1110 if (Ascii) {
1111 Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);
1112 AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);
1113 if (AsciiBuffer == NULL) {
1114 return EFI_OUT_OF_RESOURCES;
1115 }
1116 UnicodeStrToAsciiStr (Buffer, AsciiBuffer);
1117
1118 Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);
1119 Status = FileHandleWrite(Handle, &Size, AsciiBuffer);
1120 if (EFI_ERROR(Status)) {
1121 FreePool (AsciiBuffer);
1122 return (Status);
1123 }
1124 Size = AsciiStrSize("\r\n") - sizeof(CHAR8);
1125 Status = FileHandleWrite(Handle, &Size, "\r\n");
1126 } else {
1127 if (OriginalFilePosition == 0) {
1128 Status = FileHandleSetPosition (Handle, sizeof(CHAR16));
1129 if (EFI_ERROR(Status)) {
1130 return Status;
1131 }
1132 }
1133 Size = StrSize(Buffer) - sizeof(CHAR16);
1134 Status = FileHandleWrite(Handle, &Size, Buffer);
1135 if (EFI_ERROR(Status)) {
1136 return (Status);
1137 }
1138 Size = StrSize(L"\r\n") - sizeof(CHAR16);
1139 Status = FileHandleWrite(Handle, &Size, L"\r\n");
1140 }
1141
1142 if (AsciiBuffer != NULL) {
1143 FreePool (AsciiBuffer);
1144 }
1145 return Status;
1146 }
1147
1148 /**
1149 function to take a formatted argument and print it to a file.
1150
1151 @param[in] Handle the file handle for the file to write to
1152 @param[in] Format the format argument (see printlib for format specifier)
1153 @param[in] ... the variable arguments for the format
1154
1155 @retval EFI_SUCCESS the operation was sucessful
1156 @return other a return value from FileHandleWriteLine
1157
1158 @sa FileHandleWriteLine
1159 **/
1160 EFI_STATUS
1161 EFIAPI
1162 FileHandlePrintLine(
1163 IN EFI_FILE_HANDLE Handle,
1164 IN CONST CHAR16 *Format,
1165 ...
1166 )
1167 {
1168 VA_LIST Marker;
1169 CHAR16 *Buffer;
1170 EFI_STATUS Status;
1171
1172 //
1173 // Get a buffer to print into
1174 //
1175 Buffer = AllocateZeroPool (PcdGet16 (PcdUefiFileHandleLibPrintBufferSize));
1176 if (Buffer == NULL) {
1177 return (EFI_OUT_OF_RESOURCES);
1178 }
1179
1180 //
1181 // Print into our buffer
1182 //
1183 VA_START (Marker, Format);
1184 UnicodeVSPrint (Buffer, PcdGet16 (PcdUefiFileHandleLibPrintBufferSize), Format, Marker);
1185 VA_END (Marker);
1186
1187 //
1188 // Print buffer into file
1189 //
1190 Status = FileHandleWriteLine(Handle, Buffer);
1191
1192 //
1193 // Cleanup and return
1194 //
1195 FreePool(Buffer);
1196 return (Status);
1197 }
1198
1199 /**
1200 Function to determine if a FILE_HANDLE is at the end of the file.
1201
1202 This will NOT work on directories.
1203
1204 If Handle is NULL, then return False.
1205
1206 @param[in] Handle the file handle
1207
1208 @retval TRUE the position is at the end of the file
1209 @retval FALSE the position is not at the end of the file
1210 **/
1211 BOOLEAN
1212 EFIAPI
1213 FileHandleEof(
1214 IN EFI_FILE_HANDLE Handle
1215 )
1216 {
1217 EFI_FILE_INFO *Info;
1218 UINT64 Pos;
1219 BOOLEAN RetVal;
1220
1221 if (Handle == NULL) {
1222 return (FALSE);
1223 }
1224
1225 FileHandleGetPosition(Handle, &Pos);
1226 Info = FileHandleGetInfo (Handle);
1227
1228 if (Info == NULL) {
1229 return (FALSE);
1230 }
1231
1232 FileHandleSetPosition(Handle, Pos);
1233
1234 if (Pos == Info->FileSize) {
1235 RetVal = TRUE;
1236 } else {
1237 RetVal = FALSE;
1238 }
1239
1240 FreePool (Info);
1241
1242 return (RetVal);
1243 }