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