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