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