]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
MdePkg/UefiFileHandleLib: Fix potential NULL dereference
[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
819 //\r
3868d06d 820 // We got info... do we have a name? if yes precede the current path with it...\r
b1f95a06 821 //\r
822 if (StrLen (FileInfo->FileName) == 0) {\r
46f43bc4 823 if (*FullFileName == NULL) {\r
a405b86d 824 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
46f43bc4 825 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
826 }\r
b1f95a06 827 FreePool(FileInfo);\r
828 break;\r
829 } else {\r
46f43bc4 830 if (*FullFileName == NULL) {\r
a405b86d 831 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
46f43bc4 832 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
833 }\r
a405b86d 834 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
b1f95a06 835 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);\r
46f43bc4 836 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
b1f95a06 837 FreePool(FileInfo);\r
838 }\r
839 }\r
840 //\r
841 // Move to the parent directory\r
842 //\r
843 Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);\r
844 if (EFI_ERROR (Status)) {\r
845 break;\r
846 }\r
847\r
848 FileHandleClose(CurrentHandle);\r
849 CurrentHandle = NextHigherHandle;\r
850 }\r
a405b86d 851 } else if (Status == EFI_NOT_FOUND) {\r
852 Status = EFI_SUCCESS;\r
853 ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
854 *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
b1f95a06 855 }\r
856\r
9095d37b
LG
857 if (*FullFileName != NULL &&\r
858 (*FullFileName)[StrLen(*FullFileName) - 1] == L'\\' &&\r
ad6f6f86
QS
859 StrLen(*FullFileName) > 1 &&\r
860 FileHandleIsDirectory(Handle) == EFI_NOT_FOUND\r
861 ) {\r
862 (*FullFileName)[StrLen(*FullFileName) - 1] = CHAR_NULL;\r
863 }\r
864\r
b1f95a06 865 if (CurrentHandle != NULL) {\r
866 CurrentHandle->Close (CurrentHandle);\r
867 }\r
868\r
869 if (EFI_ERROR(Status) && *FullFileName != NULL) {\r
9eb53ac3 870 FreePool(*FullFileName);\r
b1f95a06 871 }\r
872\r
873 return (Status);\r
874}\r
875\r
b3011f40 876/**\r
a405b86d 877 Function to read a single line from a file. The \n is not included in the returned\r
b3011f40 878 buffer. The returned buffer must be callee freed.\r
879\r
a405b86d 880 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
b3011f40 881 maintained and not changed for all operations with the same file.\r
882\r
4ff7e37b
ED
883 @param[in] Handle FileHandle to read from.\r
884 @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);\r
b3011f40 885\r
886 @return The line of text from the file.\r
887\r
888 @sa FileHandleReadLine\r
889**/\r
890CHAR16*\r
891EFIAPI\r
892FileHandleReturnLine(\r
893 IN EFI_FILE_HANDLE Handle,\r
894 IN OUT BOOLEAN *Ascii\r
895 )\r
896{\r
897 CHAR16 *RetVal;\r
898 UINTN Size;\r
899 EFI_STATUS Status;\r
900\r
901 Size = 0;\r
902 RetVal = NULL;\r
903\r
904 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
905 if (Status == EFI_BUFFER_TOO_SMALL) {\r
4eb59be3 906 RetVal = AllocateZeroPool(Size);\r
b3011f40 907 Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
908 }\r
909 ASSERT_EFI_ERROR(Status);\r
910 if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
911 FreePool(RetVal);\r
912 RetVal = NULL;\r
913 }\r
914 return (RetVal);\r
915}\r
916\r
b1f95a06 917/**\r
bfc2fc9e 918 Function to read a single line (up to but not including the \n) from a file.\r
b1f95a06 919\r
a405b86d 920 If the position upon start is 0, then the Ascii Boolean will be set. This should be\r
b3011f40 921 maintained and not changed for all operations with the same file.\r
ba180625
QS
922 The function will not return the \r and \n character in buffer. When an empty line is\r
923 read a CHAR_NULL character will be returned in buffer.\r
b3011f40 924\r
bfc2fc9e
QS
925 @param[in] Handle FileHandle to read from.\r
926 @param[in, out] Buffer The pointer to buffer to read into.\r
927 @param[in, out] Size The pointer to number of bytes in Buffer.\r
928 @param[in] Truncate If the buffer is large enough, this has no effect.\r
929 If the buffer is is too small and Truncate is TRUE,\r
930 the line will be truncated.\r
931 If the buffer is is too small and Truncate is FALSE,\r
932 then no read will occur.\r
933\r
934 @param[in, out] Ascii Boolean value for indicating whether the file is\r
935 Ascii (TRUE) or UCS2 (FALSE).\r
936\r
937 @retval EFI_SUCCESS The operation was successful. 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
bfc2fc9e
QS
941 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.\r
942 Size was updated to the minimum space required.\r
b1f95a06 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
bfc2fc9e 957 UINT64 FileSize;\r
b1f95a06 958 UINTN CharSize;\r
959 UINTN CountSoFar;\r
ba180625 960 UINTN CrCount;\r
b3011f40 961 UINT64 OriginalFilePosition;\r
b1f95a06 962\r
b1f95a06 963 if (Handle == NULL\r
b1f95a06 964 ||Size == NULL\r
183ecff5 965 ||(Buffer==NULL&&*Size!=0)\r
a405b86d 966 ){\r
967 return (EFI_INVALID_PARAMETER);\r
9095d37b
LG
968 }\r
969\r
ba180625 970 if (Buffer != NULL && *Size != 0) {\r
a405b86d 971 *Buffer = CHAR_NULL;\r
9095d37b
LG
972 }\r
973\r
bfc2fc9e
QS
974 Status = FileHandleGetSize (Handle, &FileSize);\r
975 if (EFI_ERROR (Status)) {\r
976 return Status;\r
977 } else if (FileSize == 0) {\r
ba180625 978 *Ascii = TRUE;\r
bfc2fc9e 979 return EFI_SUCCESS;\r
9095d37b
LG
980 }\r
981\r
b3011f40 982 FileHandleGetPosition(Handle, &OriginalFilePosition);\r
983 if (OriginalFilePosition == 0) {\r
984 CharSize = sizeof(CHAR16);\r
985 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r
986 ASSERT_EFI_ERROR(Status);\r
4eb59be3 987 if (CharBuffer == gUnicodeFileTag) {\r
b3011f40 988 *Ascii = FALSE;\r
989 } else {\r
990 *Ascii = TRUE;\r
991 FileHandleSetPosition(Handle, OriginalFilePosition);\r
992 }\r
993 }\r
b1f95a06 994\r
ba180625 995 CrCount = 0;\r
b1f95a06 996 for (CountSoFar = 0;;CountSoFar++){\r
b3011f40 997 CharBuffer = 0;\r
998 if (*Ascii) {\r
999 CharSize = sizeof(CHAR8);\r
1000 } else {\r
1001 CharSize = sizeof(CHAR16);\r
1002 }\r
b1f95a06 1003 Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r
a405b86d 1004 if ( EFI_ERROR(Status)\r
1005 || CharSize == 0\r
1006 || (CharBuffer == L'\n' && !(*Ascii))\r
1007 || (CharBuffer == '\n' && *Ascii)\r
1008 ){\r
b1f95a06 1009 break;\r
ba180625
QS
1010 } else if (\r
1011 (CharBuffer == L'\r' && !(*Ascii)) ||\r
1012 (CharBuffer == '\r' && *Ascii)\r
1013 ) {\r
1014 CrCount++;\r
1015 continue;\r
b1f95a06 1016 }\r
1017 //\r
1018 // if we have space save it...\r
1019 //\r
ba180625 1020 if ((CountSoFar+1-CrCount)*sizeof(CHAR16) < *Size){\r
b3011f40 1021 ASSERT(Buffer != NULL);\r
ba180625
QS
1022 ((CHAR16*)Buffer)[CountSoFar-CrCount] = CharBuffer;\r
1023 ((CHAR16*)Buffer)[CountSoFar+1-CrCount] = CHAR_NULL;\r
b1f95a06 1024 }\r
1025 }\r
1026\r
1027 //\r
1028 // if we ran out of space tell when...\r
1029 //\r
ba180625
QS
1030 if ((CountSoFar+1-CrCount)*sizeof(CHAR16) > *Size){\r
1031 *Size = (CountSoFar+1-CrCount)*sizeof(CHAR16);\r
a405b86d 1032 if (!Truncate) {\r
ba180625
QS
1033 if (Buffer != NULL && *Size != 0) {\r
1034 ZeroMem(Buffer, *Size);\r
1035 }\r
b3011f40 1036 FileHandleSetPosition(Handle, OriginalFilePosition);\r
ba180625 1037 return (EFI_BUFFER_TOO_SMALL);\r
b1f95a06 1038 } else {\r
b3011f40 1039 DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));\r
ba180625 1040 return (EFI_SUCCESS);\r
b1f95a06 1041 }\r
b3011f40 1042 }\r
1043\r
b1f95a06 1044 return (Status);\r
1045}\r
1046\r
1047/**\r
a86c73cd 1048 Function to write a line of text to a file.\r
9095d37b
LG
1049\r
1050 If the file is a Unicode file (with UNICODE file tag) then write the unicode\r
a86c73cd
QS
1051 text.\r
1052 If the file is an ASCII file then write the ASCII text.\r
9095d37b 1053 If the size of file is zero (without file tag at the beginning) then write\r
a86c73cd 1054 ASCII text as default.\r
b1f95a06 1055\r
b269f895
QS
1056 @param[in] Handle FileHandle to write to.\r
1057 @param[in] Buffer Buffer to write, if NULL the function will\r
1058 take no action and return EFI_SUCCESS.\r
b1f95a06 1059\r
b269f895
QS
1060 @retval EFI_SUCCESS The data was written.\r
1061 Buffer is NULL.\r
1062 @retval EFI_INVALID_PARAMETER Handle is NULL.\r
9095d37b 1063 @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII\r
a86c73cd 1064 string due to out of resources.\r
b1f95a06 1065\r
1066 @sa FileHandleWrite\r
1067**/\r
1068EFI_STATUS\r
1069EFIAPI\r
1070FileHandleWriteLine(\r
1071 IN EFI_FILE_HANDLE Handle,\r
1072 IN CHAR16 *Buffer\r
a405b86d 1073 )\r
1074{\r
a86c73cd
QS
1075 EFI_STATUS Status;\r
1076 CHAR16 CharBuffer;\r
1077 UINTN Size;\r
1cdc35c4 1078 UINTN Index;\r
a86c73cd
QS
1079 UINTN CharSize;\r
1080 UINT64 FileSize;\r
1081 UINT64 OriginalFilePosition;\r
1082 BOOLEAN Ascii;\r
1083 CHAR8 *AsciiBuffer;\r
b1f95a06 1084\r
b1f95a06 1085 if (Buffer == NULL) {\r
1086 return (EFI_SUCCESS);\r
1087 }\r
1088\r
183ecff5
JC
1089 if (Handle == NULL) {\r
1090 return (EFI_INVALID_PARAMETER);\r
1091 }\r
9095d37b 1092\r
a86c73cd
QS
1093 Ascii = FALSE;\r
1094 AsciiBuffer = NULL;\r
9095d37b 1095\r
a86c73cd 1096 Status = FileHandleGetPosition(Handle, &OriginalFilePosition);\r
b1f95a06 1097 if (EFI_ERROR(Status)) {\r
a86c73cd
QS
1098 return Status;\r
1099 }\r
9095d37b 1100\r
a86c73cd
QS
1101 Status = FileHandleSetPosition(Handle, 0);\r
1102 if (EFI_ERROR(Status)) {\r
1103 return Status;\r
b1f95a06 1104 }\r
9095d37b 1105\r
a86c73cd
QS
1106 Status = FileHandleGetSize(Handle, &FileSize);\r
1107 if (EFI_ERROR(Status)) {\r
1108 return Status;\r
1109 }\r
9095d37b 1110\r
a86c73cd
QS
1111 if (FileSize == 0) {\r
1112 Ascii = TRUE;\r
1113 } else {\r
1114 CharSize = sizeof (CHAR16);\r
1115 Status = FileHandleRead (Handle, &CharSize, &CharBuffer);\r
1116 ASSERT_EFI_ERROR (Status);\r
1117 if (CharBuffer == gUnicodeFileTag) {\r
1118 Ascii = FALSE;\r
1119 } else {\r
1120 Ascii = TRUE;\r
1121 }\r
1122 }\r
9095d37b 1123\r
a86c73cd
QS
1124 Status = FileHandleSetPosition(Handle, OriginalFilePosition);\r
1125 if (EFI_ERROR(Status)) {\r
1126 return Status;\r
1127 }\r
9095d37b 1128\r
a86c73cd
QS
1129 if (Ascii) {\r
1130 Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);\r
1131 AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);\r
1132 if (AsciiBuffer == NULL) {\r
1133 return EFI_OUT_OF_RESOURCES;\r
1134 }\r
ca2ac9d5 1135 UnicodeStrToAsciiStrS (Buffer, AsciiBuffer, Size);\r
1cdc35c4 1136 for (Index = 0; Index < Size; Index++) {\r
fbda8a34 1137 if ((AsciiBuffer[Index] & BIT7) != 0) {\r
1cdc35c4
QS
1138 FreePool(AsciiBuffer);\r
1139 return EFI_INVALID_PARAMETER;\r
1140 }\r
1141 }\r
9095d37b 1142\r
a86c73cd
QS
1143 Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);\r
1144 Status = FileHandleWrite(Handle, &Size, AsciiBuffer);\r
1145 if (EFI_ERROR(Status)) {\r
1146 FreePool (AsciiBuffer);\r
1147 return (Status);\r
1148 }\r
1149 Size = AsciiStrSize("\r\n") - sizeof(CHAR8);\r
1150 Status = FileHandleWrite(Handle, &Size, "\r\n");\r
1151 } else {\r
1152 if (OriginalFilePosition == 0) {\r
1153 Status = FileHandleSetPosition (Handle, sizeof(CHAR16));\r
1154 if (EFI_ERROR(Status)) {\r
1155 return Status;\r
1156 }\r
1157 }\r
1158 Size = StrSize(Buffer) - sizeof(CHAR16);\r
1159 Status = FileHandleWrite(Handle, &Size, Buffer);\r
1160 if (EFI_ERROR(Status)) {\r
1161 return (Status);\r
1162 }\r
1163 Size = StrSize(L"\r\n") - sizeof(CHAR16);\r
1164 Status = FileHandleWrite(Handle, &Size, L"\r\n");\r
1165 }\r
9095d37b 1166\r
a86c73cd
QS
1167 if (AsciiBuffer != NULL) {\r
1168 FreePool (AsciiBuffer);\r
1169 }\r
1170 return Status;\r
b1f95a06 1171}\r
b3011f40 1172\r
1173/**\r
1174 function to take a formatted argument and print it to a file.\r
1175\r
1176 @param[in] Handle the file handle for the file to write to\r
1177 @param[in] Format the format argument (see printlib for format specifier)\r
1178 @param[in] ... the variable arguments for the format\r
1179\r
3868d06d 1180 @retval EFI_SUCCESS the operation was successful\r
b3011f40 1181 @return other a return value from FileHandleWriteLine\r
1182\r
1183 @sa FileHandleWriteLine\r
1184**/\r
1185EFI_STATUS\r
1186EFIAPI\r
1187FileHandlePrintLine(\r
1188 IN EFI_FILE_HANDLE Handle,\r
1189 IN CONST CHAR16 *Format,\r
1190 ...\r
1191 )\r
1192{\r
1193 VA_LIST Marker;\r
1194 CHAR16 *Buffer;\r
1195 EFI_STATUS Status;\r
1196\r
b3011f40 1197 //\r
1198 // Get a buffer to print into\r
1199 //\r
ae591c14 1200 Buffer = AllocateZeroPool (PcdGet16 (PcdUefiFileHandleLibPrintBufferSize));\r
183ecff5
JC
1201 if (Buffer == NULL) {\r
1202 return (EFI_OUT_OF_RESOURCES);\r
1203 }\r
b3011f40 1204\r
1205 //\r
1206 // Print into our buffer\r
1207 //\r
3bbe68a3 1208 VA_START (Marker, Format);\r
ae591c14 1209 UnicodeVSPrint (Buffer, PcdGet16 (PcdUefiFileHandleLibPrintBufferSize), Format, Marker);\r
3bbe68a3 1210 VA_END (Marker);\r
b3011f40 1211\r
1212 //\r
1213 // Print buffer into file\r
1214 //\r
1215 Status = FileHandleWriteLine(Handle, Buffer);\r
1216\r
1217 //\r
a405b86d 1218 // Cleanup and return\r
b3011f40 1219 //\r
1220 FreePool(Buffer);\r
1221 return (Status);\r
1222}\r
1223\r
1224/**\r
1225 Function to determine if a FILE_HANDLE is at the end of the file.\r
1226\r
1227 This will NOT work on directories.\r
1228\r
183ecff5 1229 If Handle is NULL, then return False.\r
b3011f40 1230\r
1231 @param[in] Handle the file handle\r
1232\r
1233 @retval TRUE the position is at the end of the file\r
1234 @retval FALSE the position is not at the end of the file\r
1235**/\r
1236BOOLEAN\r
1237EFIAPI\r
1238FileHandleEof(\r
1239 IN EFI_FILE_HANDLE Handle\r
1240 )\r
1241{\r
1242 EFI_FILE_INFO *Info;\r
1243 UINT64 Pos;\r
1244 BOOLEAN RetVal;\r
1245\r
183ecff5
JC
1246 if (Handle == NULL) {\r
1247 return (FALSE);\r
1248 }\r
a405b86d 1249\r
b3011f40 1250 FileHandleGetPosition(Handle, &Pos);\r
1251 Info = FileHandleGetInfo (Handle);\r
a405b86d 1252\r
b3011f40 1253 if (Info == NULL) {\r
1254 return (FALSE);\r
a405b86d 1255 }\r
b3011f40 1256\r
183ecff5
JC
1257 FileHandleSetPosition(Handle, Pos);\r
1258\r
d54744cd 1259 if (Pos == Info->FileSize) {\r
1260 RetVal = TRUE;\r
1261 } else {\r
1262 RetVal = FALSE;\r
1263 }\r
b3011f40 1264\r
1265 FreePool (Info);\r
1266\r
1267 return (RetVal);\r
22d11953 1268}\r