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