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