]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
Add Missing invocations to VA_END() for VA_START().
[mirror_edk2.git] / ShellPkg / Library / UefiFileHandleLib / UefiFileHandleLib.c
CommitLineData
d2b4564b 1/** @file\r
2 Provides interface to EFI_FILE_HANDLE functionality.\r
3\r
3bbe68a3 4 Copyright (c) 2006 - 2012, 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
b0934ac4 104 @retval EFI_SUCCESS The information was set.\r
a405b86d 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
b0934ac4 107 @retval EFI_NO_MEDIA The device has no medium.\r
108 @retval EFI_DEVICE_ERROR The device reported an error.\r
109 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
110 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
a405b86d 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
b0934ac4 117 IN EFI_FILE_HANDLE FileHandle,\r
d2b4564b 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
b0934ac4 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
d2b4564b 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
b0934ac4 269 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
d2b4564b 270**/\r
271EFI_STATUS\r
272EFIAPI\r
273FileHandleDelete (\r
b0934ac4 274 IN EFI_FILE_HANDLE FileHandle\r
d2b4564b 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
b0934ac4 311 IN EFI_FILE_HANDLE FileHandle,\r
312 IN UINT64 Position\r
d2b4564b 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
b0934ac4 444/** Retrieve first entry from a directory.\r
445\r
446 This function takes an open directory handle and gets information from the\r
447 first entry in the directory. A buffer is allocated to contain\r
448 the information and a pointer to the buffer is returned in *Buffer. The\r
449 caller can use FileHandleFindNextFile() to get subsequent directory entries.\r
d2b4564b 450\r
b0934ac4 451 The buffer will be freed by FileHandleFindNextFile() when the last directory\r
452 entry is read. Otherwise, the caller must free the buffer, using FreePool,\r
453 when finished with it.\r
d2b4564b 454\r
b0934ac4 455 @param[in] DirHandle The file handle of the directory to search.\r
456 @param[out] Buffer The pointer to pointer to buffer for file's information.\r
d2b4564b 457\r
458 @retval EFI_SUCCESS Found the first file.\r
459 @retval EFI_NOT_FOUND Cannot find the directory.\r
460 @retval EFI_NO_MEDIA The device has no media.\r
461 @retval EFI_DEVICE_ERROR The device reported an error.\r
462 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
463 @return Others status of FileHandleGetInfo, FileHandleSetPosition,\r
464 or FileHandleRead\r
465**/\r
466EFI_STATUS\r
467EFIAPI\r
468FileHandleFindFirstFile (\r
469 IN EFI_FILE_HANDLE DirHandle,\r
470 OUT EFI_FILE_INFO **Buffer\r
471 )\r
472{\r
473 EFI_STATUS Status;\r
474 UINTN BufferSize;\r
475\r
4eb59be3 476 if (Buffer == NULL || DirHandle == NULL) {\r
477 return (EFI_INVALID_PARAMETER);\r
478 }\r
d2b4564b 479\r
480 //\r
481 // verify that DirHandle is a directory\r
482 //\r
483 Status = FileHandleIsDirectory(DirHandle);\r
484 if (EFI_ERROR(Status)) {\r
485 return (Status);\r
a405b86d 486 }\r
d2b4564b 487\r
4eb59be3 488 //\r
489 // Allocate a buffer sized to struct size + enough for the string at the end\r
490 //\r
491 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;\r
492 *Buffer = AllocateZeroPool(BufferSize);\r
493 if (*Buffer == NULL){\r
494 return (EFI_OUT_OF_RESOURCES);\r
495 }\r
496\r
d2b4564b 497 //\r
a405b86d 498 // reset to the begining of the directory\r
d2b4564b 499 //\r
500 Status = FileHandleSetPosition(DirHandle, 0);\r
501 if (EFI_ERROR(Status)) {\r
4eb59be3 502 FreePool(*Buffer);\r
503 *Buffer = NULL;\r
d2b4564b 504 return (Status);\r
a405b86d 505 }\r
d2b4564b 506\r
d2b4564b 507 //\r
508 // read in the info about the first file\r
509 //\r
510 Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);\r
511 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
512 if (EFI_ERROR(Status)) {\r
513 FreePool(*Buffer);\r
514 *Buffer = NULL;\r
515 return (Status);\r
516 }\r
517 return (EFI_SUCCESS);\r
518}\r
d2b4564b 519\r
b0934ac4 520/** Retrieve next entries from a directory.\r
521\r
522 To use this function, the caller must first call the FileHandleFindFirstFile()\r
523 function to get the first directory entry. Subsequent directory entries are\r
524 retrieved by using the FileHandleFindNextFile() function. This function can\r
525 be called several times to get each entry from the directory. If the call of\r
526 FileHandleFindNextFile() retrieved the last directory entry, the next call of\r
527 this function will set *NoFile to TRUE and free the buffer.\r
d2b4564b 528\r
b0934ac4 529 @param[in] DirHandle The file handle of the directory.\r
530 @param[out] Buffer The pointer to buffer for file's information.\r
531 @param[out] NoFile The pointer to boolean when last file is found.\r
d2b4564b 532\r
533 @retval EFI_SUCCESS Found the next file, or reached last file\r
534 @retval EFI_NO_MEDIA The device has no media.\r
535 @retval EFI_DEVICE_ERROR The device reported an error.\r
536 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
537**/\r
538EFI_STATUS\r
539EFIAPI\r
540FileHandleFindNextFile(\r
b0934ac4 541 IN EFI_FILE_HANDLE DirHandle,\r
542 OUT EFI_FILE_INFO *Buffer,\r
543 OUT BOOLEAN *NoFile\r
d2b4564b 544 )\r
545{\r
546 EFI_STATUS Status;\r
547 UINTN BufferSize;\r
548\r
549 //\r
550 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL\r
551 //\r
552 ASSERT (DirHandle != NULL);\r
553 ASSERT (Buffer != NULL);\r
554 ASSERT (NoFile != NULL);\r
d2b4564b 555\r
556 //\r
557 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile\r
558 //\r
559 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;\r
560\r
561 //\r
562 // read in the info about the next file\r
563 //\r
564 Status = FileHandleRead (DirHandle, &BufferSize, Buffer);\r
565 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
566 if (EFI_ERROR(Status)) {\r
567 return (Status);\r
568 }\r
569\r
570 //\r
571 // If we read 0 bytes (but did not have erros) we already read in the last file.\r
572 //\r
573 if (BufferSize == 0) {\r
574 FreePool(Buffer);\r
575 *NoFile = TRUE;\r
576 }\r
577\r
578 return (EFI_SUCCESS);\r
579}\r
a405b86d 580\r
d2b4564b 581/**\r
582 Retrieve the size of a file.\r
583\r
584 if FileHandle is NULL then ASSERT()\r
585 if Size is NULL then ASSERT()\r
586\r
a405b86d 587 This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r
d2b4564b 588 data.\r
589\r
590 @param FileHandle file handle from which size is retrieved\r
591 @param Size pointer to size\r
592\r
593 @retval EFI_SUCCESS operation was completed sucessfully\r
594 @retval EFI_DEVICE_ERROR cannot access the file\r
595**/\r
596EFI_STATUS\r
597EFIAPI\r
598FileHandleGetSize (\r
599 IN EFI_FILE_HANDLE FileHandle,\r
600 OUT UINT64 *Size\r
601 )\r
602{\r
603 EFI_FILE_INFO *FileInfo;\r
604\r
605 //\r
606 // ASSERT for FileHandle or Size being NULL\r
607 //\r
608 ASSERT (FileHandle != NULL);\r
609 ASSERT (Size != NULL);\r
a405b86d 610\r
d2b4564b 611 //\r
612 // get the FileInfo structure\r
613 //\r
614 FileInfo = FileHandleGetInfo(FileHandle);\r
615 if (FileInfo == NULL) {\r
616 return (EFI_DEVICE_ERROR);\r
617 }\r
618\r
619 //\r
620 // Assign the Size pointer to the correct value\r
621 //\r
622 *Size = FileInfo->FileSize;\r
a405b86d 623\r
d2b4564b 624 //\r
625 // free the FileInfo memory\r
626 //\r
627 FreePool(FileInfo);\r
628\r
629 return (EFI_SUCCESS);\r
b1f95a06 630}\r
631\r
a405b86d 632/**\r
633 Set the size of a file.\r
634\r
635 If FileHandle is NULL then ASSERT().\r
636\r
637 This function changes the file size info from the FileHandle's EFI_FILE_INFO\r
638 data.\r
639\r
640 @param FileHandle File handle whose size is to be changed.\r
641 @param Size New size.\r
642\r
643 @retval EFI_SUCCESS operation was completed sucessfully.\r
644 @retval EFI_DEVICE_ERROR cannot access the file.\r
645**/\r
646EFI_STATUS\r
647EFIAPI\r
648FileHandleSetSize (\r
649 IN EFI_FILE_HANDLE FileHandle,\r
650 IN UINT64 Size\r
651 )\r
652{\r
653 EFI_FILE_INFO *FileInfo;\r
654 EFI_STATUS Status;\r
655\r
656 //\r
657 // ASSERT for FileHandle or Size being NULL\r
658 //\r
659 ASSERT (FileHandle != NULL);\r
660\r
661 //\r
662 // get the FileInfo structure\r
663 //\r
664 FileInfo = FileHandleGetInfo(FileHandle);\r
665 if (FileInfo == NULL) {\r
666 return (EFI_DEVICE_ERROR);\r
667 }\r
668\r
669 //\r
670 // Assign the FileSize pointer to the new value\r
671 //\r
672 FileInfo->FileSize = Size;\r
673\r
674 Status = FileHandleSetInfo(FileHandle, FileInfo);\r
675 //\r
676 // free the FileInfo memory\r
677 //\r
678 FreePool(FileInfo);\r
679\r
680 return (Status);\r
681}\r
b1f95a06 682\r
683/**\r
a405b86d 684 Safely append (on the left) with automatic string resizing given length of Destination and\r
b1f95a06 685 desired length of copy from Source.\r
686\r
a405b86d 687 append the first D characters of Source to the end of Destination, where D is\r
688 the lesser of Count and the StrLen() of Source. If appending those D characters\r
689 will fit within Destination (whose Size is given as CurrentSize) and\r
690 still leave room for a NULL terminator, then those characters are appended,\r
691 starting at the original terminating NULL of Destination, and a new terminating\r
ac255da6 692 NULL is appended.\r
b1f95a06 693\r
694 If appending D characters onto Destination will result in a overflow of the size\r
695 given in CurrentSize the string will be grown such that the copy can be performed\r
696 and CurrentSize will be updated to the new size.\r
697\r
a405b86d 698 If Source is NULL, there is nothing to append, just return the current buffer in\r
b1f95a06 699 Destination.\r
700\r
701 if Destination is NULL, then ASSERT()\r
a405b86d 702 if Destination's current length (including NULL terminator) is already more then\r
b1f95a06 703 CurrentSize, then ASSERT()\r
704\r
4ff7e37b
ED
705 @param[in, out] Destination The String to append onto\r
706 @param[in, out] CurrentSize on call the number of bytes in Destination. On\r
b1f95a06 707 return possibly the new size (still in bytes). if NULL\r
708 then allocate whatever is needed.\r
709 @param[in] Source The String to append from\r
a405b86d 710 @param[in] Count Maximum number of characters to append. if 0 then\r
b1f95a06 711 all are appended.\r
712\r
713 @return Destination return the resultant string.\r
714**/\r
a405b86d 715CHAR16*\r
b1f95a06 716EFIAPI\r
717StrnCatGrowLeft (\r
718 IN OUT CHAR16 **Destination,\r
719 IN OUT UINTN *CurrentSize,\r
720 IN CONST CHAR16 *Source,\r
721 IN UINTN Count\r
a405b86d 722 )\r
723{\r
b1f95a06 724 UINTN DestinationStartSize;\r
725 UINTN NewSize;\r
727a4c71 726 UINTN CopySize;\r
b1f95a06 727\r
728 //\r
729 // ASSERTs\r
730 //\r
731 ASSERT(Destination != NULL);\r
732\r
733 //\r
734 // If there's nothing to do then just return Destination\r
735 //\r
736 if (Source == NULL) {\r
737 return (*Destination);\r
738 }\r
739\r
740 //\r
741 // allow for NULL pointers address as Destination\r
742 //\r
743 if (*Destination != NULL) {\r
744 ASSERT(CurrentSize != 0);\r
745 DestinationStartSize = StrSize(*Destination);\r
746 ASSERT(DestinationStartSize <= *CurrentSize);\r
747 } else {\r
748 DestinationStartSize = 0;\r
749// ASSERT(*CurrentSize == 0);\r
750 }\r
751\r
752 //\r
753 // Append all of Source?\r
754 //\r
755 if (Count == 0) {\r
d595d4b1 756 Count = StrSize(Source);\r
b1f95a06 757 }\r
758\r
759 //\r
760 // Test and grow if required\r
761 //\r
762 if (CurrentSize != NULL) {\r
763 NewSize = *CurrentSize;\r
d595d4b1 764 while (NewSize < (DestinationStartSize + Count)) {\r
765 NewSize += 2 * Count;\r
b1f95a06 766 }\r
767 *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
768 *CurrentSize = NewSize;\r
769 } else {\r
d595d4b1 770 *Destination = AllocateZeroPool(Count+sizeof(CHAR16));\r
b1f95a06 771 }\r
02a758cb 772 if (*Destination == NULL) {\r
773 return NULL;\r
774 }\r
b1f95a06 775\r
727a4c71 776 CopySize = StrSize(*Destination);\r
46f43bc4 777 CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize);\r
778 CopyMem(*Destination, Source, Count-2);\r
b1f95a06 779 return (*Destination);\r
780}\r
781\r
782/**\r
a405b86d 783 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the\r
b1f95a06 784 directory 'stack'.\r
785\r
786 if Handle is NULL, return EFI_INVALID_PARAMETER\r
787\r
788 @param[in] Handle Handle to the Directory or File to create path to.\r
a405b86d 789 @param[out] FullFileName pointer to pointer to generated full file name. It\r
b1f95a06 790 is the responsibility of the caller to free this memory\r
791 with a call to FreePool().\r
792 @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.\r
793 @retval EFI_INVALID_PARAMETER Handle was NULL.\r
794 @retval EFI_INVALID_PARAMETER FullFileName was NULL.\r
795 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
796**/\r
797EFI_STATUS\r
798EFIAPI\r
799FileHandleGetFileName (\r
800 IN CONST EFI_FILE_HANDLE Handle,\r
801 OUT CHAR16 **FullFileName\r
a405b86d 802 )\r
803{\r
b1f95a06 804 EFI_STATUS Status;\r
805 UINTN Size;\r
806 EFI_FILE_HANDLE CurrentHandle;\r
807 EFI_FILE_HANDLE NextHigherHandle;\r
808 EFI_FILE_INFO *FileInfo;\r
809\r
810 Size = 0;\r
b1f95a06 811\r
812 //\r
813 // Check our parameters\r
814 //\r
815 if (FullFileName == NULL || Handle == NULL) {\r
816 return (EFI_INVALID_PARAMETER);\r
817 }\r
818\r
d595d4b1 819 *FullFileName = NULL;\r
a405b86d 820 CurrentHandle = NULL;\r
d595d4b1 821\r
b1f95a06 822 Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);\r
823 if (!EFI_ERROR(Status)) {\r
824 //\r
825 // Reverse out the current directory on the device\r
826 //\r
827 for (;;) {\r
828 FileInfo = FileHandleGetInfo(CurrentHandle);\r
829 if (FileInfo == NULL) {\r
830 Status = EFI_OUT_OF_RESOURCES;\r
831 break;\r
832 } else {\r
833 //\r
834 // We got info... do we have a name? if yes preceed the current path with it...\r
835 //\r
836 if (StrLen (FileInfo->FileName) == 0) {\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
b1f95a06 841 FreePool(FileInfo);\r
842 break;\r
843 } else {\r
46f43bc4 844 if (*FullFileName == NULL) {\r
a405b86d 845 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
46f43bc4 846 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
847 }\r
a405b86d 848 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
b1f95a06 849 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);\r
46f43bc4 850 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
b1f95a06 851 FreePool(FileInfo);\r
852 }\r
853 }\r
854 //\r
855 // Move to the parent directory\r
856 //\r
857 Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);\r
858 if (EFI_ERROR (Status)) {\r
859 break;\r
860 }\r
861\r
862 FileHandleClose(CurrentHandle);\r
863 CurrentHandle = NextHigherHandle;\r
864 }\r
a405b86d 865 } else if (Status == EFI_NOT_FOUND) {\r
866 Status = EFI_SUCCESS;\r
867 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
868 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
b1f95a06 869 }\r
870\r
871 if (CurrentHandle != NULL) {\r
872 CurrentHandle->Close (CurrentHandle);\r
873 }\r
874\r
875 if (EFI_ERROR(Status) && *FullFileName != NULL) {\r
9eb53ac3 876 FreePool(*FullFileName);\r
b1f95a06 877 }\r
878\r
879 return (Status);\r
880}\r
881\r
b3011f40 882/**\r
a405b86d 883 Function to read a single line from a file. The \n is not included in the returned\r
b3011f40 884 buffer. The returned buffer must be callee freed.\r
885\r
a405b86d 886 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
b3011f40 887 maintained and not changed for all operations with the same file.\r
888\r
4ff7e37b
ED
889 @param[in] Handle FileHandle to read from.\r
890 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);\r
b3011f40 891\r
892 @return The line of text from the file.\r
893\r
894 @sa FileHandleReadLine\r
895**/\r
896CHAR16*\r
897EFIAPI\r
898FileHandleReturnLine(\r
899 IN EFI_FILE_HANDLE Handle,\r
900 IN OUT BOOLEAN *Ascii\r
901 )\r
902{\r
903 CHAR16 *RetVal;\r
904 UINTN Size;\r
905 EFI_STATUS Status;\r
906\r
907 Size = 0;\r
908 RetVal = NULL;\r
909\r
910 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
911 if (Status == EFI_BUFFER_TOO_SMALL) {\r
4eb59be3 912 RetVal = AllocateZeroPool(Size);\r
b3011f40 913 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
914 }\r
915 ASSERT_EFI_ERROR(Status);\r
916 if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
917 FreePool(RetVal);\r
918 RetVal = NULL;\r
919 }\r
920 return (RetVal);\r
921}\r
922\r
b1f95a06 923/**\r
a405b86d 924 Function to read a single line (up to but not including the \n) from a EFI_FILE_HANDLE.\r
b1f95a06 925\r
a405b86d 926 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
b3011f40 927 maintained and not changed for all operations with the same file.\r
928\r
4ff7e37b
ED
929 @param[in] Handle FileHandle to read from\r
930 @param[in, out] Buffer pointer to buffer to read into\r
931 @param[in, out] Size pointer to number of bytes in buffer\r
932 @param[in] Truncate if TRUE then allows for truncation of the line to fit.\r
933 if FALSE will reset the position to the begining of the\r
934 line if the buffer is not large enough.\r
935 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);\r
b1f95a06 936\r
a405b86d 937 @retval EFI_SUCCESS the operation was sucessful. the line is stored in\r
b1f95a06 938 Buffer.\r
939 @retval EFI_INVALID_PARAMETER Handle was NULL.\r
b1f95a06 940 @retval EFI_INVALID_PARAMETER Size was NULL.\r
a405b86d 941 @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line.\r
b1f95a06 942 Size was updated to minimum space required.\r
943 @sa FileHandleRead\r
944**/\r
945EFI_STATUS\r
946EFIAPI\r
947FileHandleReadLine(\r
948 IN EFI_FILE_HANDLE Handle,\r
b3011f40 949 IN OUT CHAR16 *Buffer,\r
b1f95a06 950 IN OUT UINTN *Size,\r
b3011f40 951 IN BOOLEAN Truncate,\r
952 IN OUT BOOLEAN *Ascii\r
a405b86d 953 )\r
954{\r
b1f95a06 955 EFI_STATUS Status;\r
956 CHAR16 CharBuffer;\r
957 UINTN CharSize;\r
958 UINTN CountSoFar;\r
b3011f40 959 UINT64 OriginalFilePosition;\r
b1f95a06 960\r
961\r
962 if (Handle == NULL\r
b1f95a06 963 ||Size == NULL\r
a405b86d 964 ){\r
965 return (EFI_INVALID_PARAMETER);\r
966 }\r
967 if (Buffer == NULL) {\r
968 ASSERT(*Size == 0);\r
969 } else {\r
970 *Buffer = CHAR_NULL;\r
b1f95a06 971 }\r
b3011f40 972 FileHandleGetPosition(Handle, &OriginalFilePosition);\r
973 if (OriginalFilePosition == 0) {\r
974 CharSize = sizeof(CHAR16);\r
975 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r
976 ASSERT_EFI_ERROR(Status);\r
4eb59be3 977 if (CharBuffer == gUnicodeFileTag) {\r
b3011f40 978 *Ascii = FALSE;\r
979 } else {\r
980 *Ascii = TRUE;\r
981 FileHandleSetPosition(Handle, OriginalFilePosition);\r
982 }\r
983 }\r
b1f95a06 984\r
985 for (CountSoFar = 0;;CountSoFar++){\r
b3011f40 986 CharBuffer = 0;\r
987 if (*Ascii) {\r
988 CharSize = sizeof(CHAR8);\r
989 } else {\r
990 CharSize = sizeof(CHAR16);\r
991 }\r
b1f95a06 992 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r
a405b86d 993 if ( EFI_ERROR(Status)\r
994 || CharSize == 0\r
995 || (CharBuffer == L'\n' && !(*Ascii))\r
996 || (CharBuffer == '\n' && *Ascii)\r
997 ){\r
b1f95a06 998 break;\r
999 }\r
1000 //\r
1001 // if we have space save it...\r
1002 //\r
1003 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
b3011f40 1004 ASSERT(Buffer != NULL);\r
b1f95a06 1005 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
46f43bc4 1006 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
b1f95a06 1007 }\r
1008 }\r
1009\r
1010 //\r
1011 // if we ran out of space tell when...\r
1012 //\r
1013 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
1014 *Size = (CountSoFar+1)*sizeof(CHAR16);\r
a405b86d 1015 if (!Truncate) {\r
b3011f40 1016 FileHandleSetPosition(Handle, OriginalFilePosition);\r
b1f95a06 1017 } else {\r
b3011f40 1018 DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));\r
b1f95a06 1019 }\r
1020 return (EFI_BUFFER_TOO_SMALL);\r
1021 }\r
b3011f40 1022 while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
1023 Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
1024 }\r
1025\r
b1f95a06 1026 return (Status);\r
1027}\r
1028\r
1029/**\r
1030 function to write a line of unicode text to a file.\r
1031\r
1032 if Handle is NULL, ASSERT.\r
1033 if Buffer is NULL, do nothing. (return SUCCESS)\r
1034\r
1035 @param[in] Handle FileHandle to write to\r
1036 @param[in] Buffer Buffer to write\r
1037\r
1038 @retval EFI_SUCCESS the data was written.\r
1039 @retval other failure.\r
1040\r
1041 @sa FileHandleWrite\r
1042**/\r
1043EFI_STATUS\r
1044EFIAPI\r
1045FileHandleWriteLine(\r
1046 IN EFI_FILE_HANDLE Handle,\r
1047 IN CHAR16 *Buffer\r
a405b86d 1048 )\r
1049{\r
b1f95a06 1050 EFI_STATUS Status;\r
1051 UINTN Size;\r
1052\r
1053 ASSERT(Handle != NULL);\r
1054\r
1055 if (Buffer == NULL) {\r
1056 return (EFI_SUCCESS);\r
1057 }\r
1058\r
ac255da6 1059 Size = StrSize(Buffer) - sizeof(Buffer[0]);\r
b1f95a06 1060 Status = FileHandleWrite(Handle, &Size, Buffer);\r
1061 if (EFI_ERROR(Status)) {\r
1062 return (Status);\r
1063 }\r
ac255da6 1064 Size = StrSize(L"\r\n") - sizeof(CHAR16);\r
b1f95a06 1065 return FileHandleWrite(Handle, &Size, L"\r\n");\r
1066}\r
b3011f40 1067\r
1068/**\r
1069 function to take a formatted argument and print it to a file.\r
1070\r
1071 @param[in] Handle the file handle for the file to write to\r
1072 @param[in] Format the format argument (see printlib for format specifier)\r
1073 @param[in] ... the variable arguments for the format\r
1074\r
1075 @retval EFI_SUCCESS the operation was sucessful\r
1076 @return other a return value from FileHandleWriteLine\r
1077\r
1078 @sa FileHandleWriteLine\r
1079**/\r
1080EFI_STATUS\r
1081EFIAPI\r
1082FileHandlePrintLine(\r
1083 IN EFI_FILE_HANDLE Handle,\r
1084 IN CONST CHAR16 *Format,\r
1085 ...\r
1086 )\r
1087{\r
1088 VA_LIST Marker;\r
1089 CHAR16 *Buffer;\r
1090 EFI_STATUS Status;\r
1091\r
b3011f40 1092 //\r
1093 // Get a buffer to print into\r
1094 //\r
1095 Buffer = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
1096 ASSERT (Buffer != NULL);\r
1097\r
1098 //\r
1099 // Print into our buffer\r
1100 //\r
3bbe68a3 1101 VA_START (Marker, Format);\r
b3011f40 1102 UnicodeVSPrint (Buffer, PcdGet16 (PcdShellPrintBufferSize), Format, Marker);\r
3bbe68a3 1103 VA_END (Marker);\r
b3011f40 1104\r
1105 //\r
1106 // Print buffer into file\r
1107 //\r
1108 Status = FileHandleWriteLine(Handle, Buffer);\r
1109\r
1110 //\r
a405b86d 1111 // Cleanup and return\r
b3011f40 1112 //\r
1113 FreePool(Buffer);\r
1114 return (Status);\r
1115}\r
1116\r
1117/**\r
1118 Function to determine if a FILE_HANDLE is at the end of the file.\r
1119\r
1120 This will NOT work on directories.\r
1121\r
1122 If Handle is NULL, then ASSERT.\r
1123\r
1124 @param[in] Handle the file handle\r
1125\r
1126 @retval TRUE the position is at the end of the file\r
1127 @retval FALSE the position is not at the end of the file\r
1128**/\r
1129BOOLEAN\r
1130EFIAPI\r
1131FileHandleEof(\r
1132 IN EFI_FILE_HANDLE Handle\r
1133 )\r
1134{\r
1135 EFI_FILE_INFO *Info;\r
1136 UINT64 Pos;\r
1137 BOOLEAN RetVal;\r
1138\r
1139 //\r
1140 // ASSERT if Handle is NULL\r
1141 //\r
1142 ASSERT(Handle != NULL);\r
a405b86d 1143\r
b3011f40 1144 FileHandleGetPosition(Handle, &Pos);\r
1145 Info = FileHandleGetInfo (Handle);\r
1146 ASSERT(Info != NULL);\r
1147 FileHandleSetPosition(Handle, Pos);\r
a405b86d 1148\r
b3011f40 1149 if (Info == NULL) {\r
1150 return (FALSE);\r
a405b86d 1151 }\r
b3011f40 1152\r
d54744cd 1153 if (Pos == Info->FileSize) {\r
1154 RetVal = TRUE;\r
1155 } else {\r
1156 RetVal = FALSE;\r
1157 }\r
b3011f40 1158\r
1159 FreePool (Info);\r
1160\r
1161 return (RetVal);\r
22d11953 1162}\r