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