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