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