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