]>
Commit | Line | Data |
---|---|---|
d2b4564b | 1 | /** @file\r |
2 | Provides interface to EFI_FILE_HANDLE functionality.\r | |
3 | \r | |
ca2ac9d5 | 4 | Copyright (c) 2006 - 2016, 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 | |
e84fb6eb | 386 | Function to determine if a given handle is a directory handle.\r |
d2b4564b | 387 | \r |
e84fb6eb | 388 | Open the file information on the DirHandle and verify that the Attribute\r |
d2b4564b | 389 | includes EFI_FILE_DIRECTORY bit set.\r |
390 | \r | |
e84fb6eb | 391 | @param[in] DirHandle Handle to open file.\r |
d2b4564b | 392 | \r |
e84fb6eb QS |
393 | @retval EFI_SUCCESS DirHandle is a directory.\r |
394 | @retval EFI_INVALID_PARAMETER DirHandle is NULL. \r | |
395 | The file information returns from FileHandleGetInfo is NULL. \r | |
396 | @retval EFI_NOT_FOUND DirHandle is not a directory.\r | |
d2b4564b | 397 | **/\r |
398 | EFI_STATUS\r | |
399 | EFIAPI\r | |
400 | FileHandleIsDirectory (\r | |
401 | IN EFI_FILE_HANDLE DirHandle\r | |
402 | )\r | |
403 | {\r | |
404 | EFI_FILE_INFO *DirInfo;\r | |
405 | \r | |
183ecff5 JC |
406 | if (DirHandle == NULL) {\r |
407 | return (EFI_INVALID_PARAMETER);\r | |
408 | }\r | |
a405b86d | 409 | \r |
d2b4564b | 410 | //\r |
411 | // get the file information for DirHandle\r | |
412 | //\r | |
413 | DirInfo = FileHandleGetInfo (DirHandle);\r | |
a405b86d | 414 | \r |
d2b4564b | 415 | //\r |
416 | // Parse DirInfo\r | |
417 | //\r | |
418 | if (DirInfo == NULL) {\r | |
419 | //\r | |
420 | // We got nothing...\r | |
421 | //\r | |
422 | return (EFI_INVALID_PARAMETER);\r | |
a405b86d | 423 | }\r |
d2b4564b | 424 | if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r |
425 | //\r | |
426 | // Attributes say this is not a directory\r | |
427 | //\r | |
428 | FreePool (DirInfo);\r | |
429 | return (EFI_NOT_FOUND);\r | |
430 | }\r | |
431 | //\r | |
432 | // all good...\r | |
433 | //\r | |
434 | FreePool (DirInfo);\r | |
435 | return (EFI_SUCCESS);\r | |
436 | }\r | |
437 | \r | |
b0934ac4 | 438 | /** Retrieve first entry from a directory.\r |
439 | \r | |
440 | This function takes an open directory handle and gets information from the\r | |
441 | first entry in the directory. A buffer is allocated to contain\r | |
442 | the information and a pointer to the buffer is returned in *Buffer. The\r | |
443 | caller can use FileHandleFindNextFile() to get subsequent directory entries.\r | |
d2b4564b | 444 | \r |
b0934ac4 | 445 | The buffer will be freed by FileHandleFindNextFile() when the last directory\r |
446 | entry is read. Otherwise, the caller must free the buffer, using FreePool,\r | |
447 | when finished with it.\r | |
d2b4564b | 448 | \r |
b0934ac4 | 449 | @param[in] DirHandle The file handle of the directory to search.\r |
450 | @param[out] Buffer The pointer to pointer to buffer for file's information.\r | |
d2b4564b | 451 | \r |
452 | @retval EFI_SUCCESS Found the first file.\r | |
453 | @retval EFI_NOT_FOUND Cannot find the directory.\r | |
454 | @retval EFI_NO_MEDIA The device has no media.\r | |
455 | @retval EFI_DEVICE_ERROR The device reported an error.\r | |
456 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r | |
457 | @return Others status of FileHandleGetInfo, FileHandleSetPosition,\r | |
458 | or FileHandleRead\r | |
459 | **/\r | |
460 | EFI_STATUS\r | |
461 | EFIAPI\r | |
462 | FileHandleFindFirstFile (\r | |
463 | IN EFI_FILE_HANDLE DirHandle,\r | |
464 | OUT EFI_FILE_INFO **Buffer\r | |
465 | )\r | |
466 | {\r | |
467 | EFI_STATUS Status;\r | |
468 | UINTN BufferSize;\r | |
469 | \r | |
4eb59be3 | 470 | if (Buffer == NULL || DirHandle == NULL) {\r |
471 | return (EFI_INVALID_PARAMETER);\r | |
472 | }\r | |
d2b4564b | 473 | \r |
474 | //\r | |
475 | // verify that DirHandle is a directory\r | |
476 | //\r | |
477 | Status = FileHandleIsDirectory(DirHandle);\r | |
478 | if (EFI_ERROR(Status)) {\r | |
479 | return (Status);\r | |
a405b86d | 480 | }\r |
d2b4564b | 481 | \r |
4eb59be3 | 482 | //\r |
483 | // Allocate a buffer sized to struct size + enough for the string at the end\r | |
484 | //\r | |
485 | BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;\r | |
486 | *Buffer = AllocateZeroPool(BufferSize);\r | |
487 | if (*Buffer == NULL){\r | |
488 | return (EFI_OUT_OF_RESOURCES);\r | |
489 | }\r | |
490 | \r | |
d2b4564b | 491 | //\r |
a405b86d | 492 | // reset to the begining of the directory\r |
d2b4564b | 493 | //\r |
494 | Status = FileHandleSetPosition(DirHandle, 0);\r | |
495 | if (EFI_ERROR(Status)) {\r | |
4eb59be3 | 496 | FreePool(*Buffer);\r |
497 | *Buffer = NULL;\r | |
d2b4564b | 498 | return (Status);\r |
a405b86d | 499 | }\r |
d2b4564b | 500 | \r |
d2b4564b | 501 | //\r |
502 | // read in the info about the first file\r | |
503 | //\r | |
504 | Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);\r | |
505 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
74fa83fd | 506 | if (EFI_ERROR(Status) || BufferSize == 0) {\r |
d2b4564b | 507 | FreePool(*Buffer);\r |
508 | *Buffer = NULL;\r | |
74fa83fd | 509 | if (BufferSize == 0) {\r |
510 | return (EFI_NOT_FOUND);\r | |
511 | }\r | |
d2b4564b | 512 | return (Status);\r |
513 | }\r | |
514 | return (EFI_SUCCESS);\r | |
515 | }\r | |
d2b4564b | 516 | \r |
b0934ac4 | 517 | /** Retrieve next entries from a directory.\r |
518 | \r | |
519 | To use this function, the caller must first call the FileHandleFindFirstFile()\r | |
520 | function to get the first directory entry. Subsequent directory entries are\r | |
521 | retrieved by using the FileHandleFindNextFile() function. This function can\r | |
522 | be called several times to get each entry from the directory. If the call of\r | |
523 | FileHandleFindNextFile() retrieved the last directory entry, the next call of\r | |
524 | this function will set *NoFile to TRUE and free the buffer.\r | |
d2b4564b | 525 | \r |
b0934ac4 | 526 | @param[in] DirHandle The file handle of the directory.\r |
527 | @param[out] Buffer The pointer to buffer for file's information.\r | |
528 | @param[out] NoFile The pointer to boolean when last file is found.\r | |
d2b4564b | 529 | \r |
530 | @retval EFI_SUCCESS Found the next file, or reached last file\r | |
531 | @retval EFI_NO_MEDIA The device has no media.\r | |
532 | @retval EFI_DEVICE_ERROR The device reported an error.\r | |
533 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r | |
534 | **/\r | |
535 | EFI_STATUS\r | |
536 | EFIAPI\r | |
537 | FileHandleFindNextFile(\r | |
b0934ac4 | 538 | IN EFI_FILE_HANDLE DirHandle,\r |
539 | OUT EFI_FILE_INFO *Buffer,\r | |
540 | OUT BOOLEAN *NoFile\r | |
d2b4564b | 541 | )\r |
542 | {\r | |
543 | EFI_STATUS Status;\r | |
544 | UINTN BufferSize;\r | |
545 | \r | |
183ecff5 JC |
546 | if (DirHandle == NULL || Buffer == NULL || NoFile == NULL) {\r |
547 | return (EFI_INVALID_PARAMETER);\r | |
548 | }\r | |
d2b4564b | 549 | \r |
550 | //\r | |
551 | // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile\r | |
552 | //\r | |
553 | BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;\r | |
554 | \r | |
555 | //\r | |
556 | // read in the info about the next file\r | |
557 | //\r | |
558 | Status = FileHandleRead (DirHandle, &BufferSize, Buffer);\r | |
559 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
560 | if (EFI_ERROR(Status)) {\r | |
561 | return (Status);\r | |
562 | }\r | |
563 | \r | |
564 | //\r | |
565 | // If we read 0 bytes (but did not have erros) we already read in the last file.\r | |
566 | //\r | |
567 | if (BufferSize == 0) {\r | |
568 | FreePool(Buffer);\r | |
569 | *NoFile = TRUE;\r | |
570 | }\r | |
571 | \r | |
572 | return (EFI_SUCCESS);\r | |
573 | }\r | |
a405b86d | 574 | \r |
d2b4564b | 575 | /**\r |
576 | Retrieve the size of a file.\r | |
577 | \r | |
a405b86d | 578 | This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r |
d2b4564b | 579 | data.\r |
580 | \r | |
b269f895 QS |
581 | @param[in] FileHandle The file handle from which size is retrieved.\r |
582 | @param[out] Size The pointer to size.\r | |
d2b4564b | 583 | \r |
b269f895 QS |
584 | @retval EFI_SUCCESS Operation was completed sucessfully.\r |
585 | @retval EFI_DEVICE_ERROR Cannot access the file.\r | |
586 | @retval EFI_INVALID_PARAMETER FileHandle is NULL.\r | |
587 | Size is NULL.\r | |
d2b4564b | 588 | **/\r |
589 | EFI_STATUS\r | |
590 | EFIAPI\r | |
591 | FileHandleGetSize (\r | |
592 | IN EFI_FILE_HANDLE FileHandle,\r | |
593 | OUT UINT64 *Size\r | |
594 | )\r | |
595 | {\r | |
596 | EFI_FILE_INFO *FileInfo;\r | |
597 | \r | |
183ecff5 JC |
598 | if (FileHandle == NULL || Size == NULL) {\r |
599 | return (EFI_INVALID_PARAMETER);\r | |
600 | }\r | |
a405b86d | 601 | \r |
d2b4564b | 602 | //\r |
603 | // get the FileInfo structure\r | |
604 | //\r | |
605 | FileInfo = FileHandleGetInfo(FileHandle);\r | |
606 | if (FileInfo == NULL) {\r | |
607 | return (EFI_DEVICE_ERROR);\r | |
608 | }\r | |
609 | \r | |
610 | //\r | |
611 | // Assign the Size pointer to the correct value\r | |
612 | //\r | |
613 | *Size = FileInfo->FileSize;\r | |
a405b86d | 614 | \r |
d2b4564b | 615 | //\r |
616 | // free the FileInfo memory\r | |
617 | //\r | |
618 | FreePool(FileInfo);\r | |
619 | \r | |
620 | return (EFI_SUCCESS);\r | |
b1f95a06 | 621 | }\r |
622 | \r | |
a405b86d | 623 | /**\r |
624 | Set the size of a file.\r | |
625 | \r | |
a405b86d | 626 | This function changes the file size info from the FileHandle's EFI_FILE_INFO\r |
627 | data.\r | |
628 | \r | |
b269f895 QS |
629 | @param[in] FileHandle The file handle whose size is to be changed.\r |
630 | @param[in] Size The new size.\r | |
a405b86d | 631 | \r |
b269f895 QS |
632 | @retval EFI_SUCCESS The operation completed successfully.\r |
633 | @retval EFI_DEVICE_ERROR Cannot access the file.\r | |
634 | @retval EFI_INVALID_PARAMETER FileHandle is NULL.\r | |
a405b86d | 635 | **/\r |
636 | EFI_STATUS\r | |
637 | EFIAPI\r | |
638 | FileHandleSetSize (\r | |
639 | IN EFI_FILE_HANDLE FileHandle,\r | |
640 | IN UINT64 Size\r | |
641 | )\r | |
642 | {\r | |
643 | EFI_FILE_INFO *FileInfo;\r | |
644 | EFI_STATUS Status;\r | |
645 | \r | |
183ecff5 JC |
646 | if (FileHandle == NULL) {\r |
647 | return (EFI_INVALID_PARAMETER);\r | |
648 | }\r | |
a405b86d | 649 | \r |
650 | //\r | |
651 | // get the FileInfo structure\r | |
652 | //\r | |
653 | FileInfo = FileHandleGetInfo(FileHandle);\r | |
654 | if (FileInfo == NULL) {\r | |
655 | return (EFI_DEVICE_ERROR);\r | |
656 | }\r | |
657 | \r | |
658 | //\r | |
659 | // Assign the FileSize pointer to the new value\r | |
660 | //\r | |
661 | FileInfo->FileSize = Size;\r | |
662 | \r | |
663 | Status = FileHandleSetInfo(FileHandle, FileInfo);\r | |
664 | //\r | |
665 | // free the FileInfo memory\r | |
666 | //\r | |
667 | FreePool(FileInfo);\r | |
668 | \r | |
669 | return (Status);\r | |
670 | }\r | |
b1f95a06 | 671 | \r |
672 | /**\r | |
a405b86d | 673 | Safely append (on the left) with automatic string resizing given length of Destination and\r |
b1f95a06 | 674 | desired length of copy from Source.\r |
675 | \r | |
a405b86d | 676 | append the first D characters of Source to the end of Destination, where D is\r |
677 | the lesser of Count and the StrLen() of Source. If appending those D characters\r | |
678 | will fit within Destination (whose Size is given as CurrentSize) and\r | |
679 | still leave room for a NULL terminator, then those characters are appended,\r | |
680 | starting at the original terminating NULL of Destination, and a new terminating\r | |
ac255da6 | 681 | NULL is appended.\r |
b1f95a06 | 682 | \r |
683 | If appending D characters onto Destination will result in a overflow of the size\r | |
684 | given in CurrentSize the string will be grown such that the copy can be performed\r | |
685 | and CurrentSize will be updated to the new size.\r | |
686 | \r | |
a405b86d | 687 | If Source is NULL, there is nothing to append, just return the current buffer in\r |
b1f95a06 | 688 | Destination.\r |
689 | \r | |
183ecff5 | 690 | if Destination is NULL, then return error\r |
a405b86d | 691 | if Destination's current length (including NULL terminator) is already more then\r |
b1f95a06 | 692 | CurrentSize, then ASSERT()\r |
693 | \r | |
4ff7e37b ED |
694 | @param[in, out] Destination The String to append onto\r |
695 | @param[in, out] CurrentSize on call the number of bytes in Destination. On\r | |
b1f95a06 | 696 | return possibly the new size (still in bytes). if NULL\r |
697 | then allocate whatever is needed.\r | |
698 | @param[in] Source The String to append from\r | |
a405b86d | 699 | @param[in] Count Maximum number of characters to append. if 0 then\r |
b1f95a06 | 700 | all are appended.\r |
701 | \r | |
702 | @return Destination return the resultant string.\r | |
703 | **/\r | |
a405b86d | 704 | CHAR16*\r |
b1f95a06 | 705 | EFIAPI\r |
706 | StrnCatGrowLeft (\r | |
707 | IN OUT CHAR16 **Destination,\r | |
708 | IN OUT UINTN *CurrentSize,\r | |
709 | IN CONST CHAR16 *Source,\r | |
710 | IN UINTN Count\r | |
a405b86d | 711 | )\r |
712 | {\r | |
b1f95a06 | 713 | UINTN DestinationStartSize;\r |
714 | UINTN NewSize;\r | |
727a4c71 | 715 | UINTN CopySize;\r |
b1f95a06 | 716 | \r |
183ecff5 JC |
717 | if (Destination == NULL) {\r |
718 | return (NULL);\r | |
719 | }\r | |
b1f95a06 | 720 | \r |
721 | //\r | |
722 | // If there's nothing to do then just return Destination\r | |
723 | //\r | |
724 | if (Source == NULL) {\r | |
725 | return (*Destination);\r | |
726 | }\r | |
727 | \r | |
728 | //\r | |
729 | // allow for NULL pointers address as Destination\r | |
730 | //\r | |
731 | if (*Destination != NULL) {\r | |
732 | ASSERT(CurrentSize != 0);\r | |
733 | DestinationStartSize = StrSize(*Destination);\r | |
734 | ASSERT(DestinationStartSize <= *CurrentSize);\r | |
735 | } else {\r | |
736 | DestinationStartSize = 0;\r | |
737 | // ASSERT(*CurrentSize == 0);\r | |
738 | }\r | |
739 | \r | |
740 | //\r | |
741 | // Append all of Source?\r | |
742 | //\r | |
743 | if (Count == 0) {\r | |
d595d4b1 | 744 | Count = StrSize(Source);\r |
b1f95a06 | 745 | }\r |
746 | \r | |
747 | //\r | |
748 | // Test and grow if required\r | |
749 | //\r | |
750 | if (CurrentSize != NULL) {\r | |
751 | NewSize = *CurrentSize;\r | |
d595d4b1 | 752 | while (NewSize < (DestinationStartSize + Count)) {\r |
753 | NewSize += 2 * Count;\r | |
b1f95a06 | 754 | }\r |
755 | *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r | |
756 | *CurrentSize = NewSize;\r | |
757 | } else {\r | |
d595d4b1 | 758 | *Destination = AllocateZeroPool(Count+sizeof(CHAR16));\r |
b1f95a06 | 759 | }\r |
02a758cb | 760 | if (*Destination == NULL) {\r |
761 | return NULL;\r | |
762 | }\r | |
b1f95a06 | 763 | \r |
727a4c71 | 764 | CopySize = StrSize(*Destination);\r |
46f43bc4 | 765 | CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize);\r |
766 | CopyMem(*Destination, Source, Count-2);\r | |
b1f95a06 | 767 | return (*Destination);\r |
768 | }\r | |
769 | \r | |
770 | /**\r | |
a405b86d | 771 | Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the\r |
ad6f6f86 QS |
772 | directory 'stack'. If the file is a directory, then append the '\' char at the \r |
773 | end of name string. If it's not a directory, then the last '\' should not be \r | |
774 | added.\r | |
b1f95a06 | 775 | \r |
776 | if Handle is NULL, return EFI_INVALID_PARAMETER\r | |
777 | \r | |
778 | @param[in] Handle Handle to the Directory or File to create path to.\r | |
a405b86d | 779 | @param[out] FullFileName pointer to pointer to generated full file name. It\r |
b1f95a06 | 780 | is the responsibility of the caller to free this memory\r |
781 | with a call to FreePool().\r | |
782 | @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.\r | |
783 | @retval EFI_INVALID_PARAMETER Handle was NULL.\r | |
784 | @retval EFI_INVALID_PARAMETER FullFileName was NULL.\r | |
785 | @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r | |
786 | **/\r | |
787 | EFI_STATUS\r | |
788 | EFIAPI\r | |
789 | FileHandleGetFileName (\r | |
790 | IN CONST EFI_FILE_HANDLE Handle,\r | |
791 | OUT CHAR16 **FullFileName\r | |
a405b86d | 792 | )\r |
793 | {\r | |
b1f95a06 | 794 | EFI_STATUS Status;\r |
795 | UINTN Size;\r | |
796 | EFI_FILE_HANDLE CurrentHandle;\r | |
797 | EFI_FILE_HANDLE NextHigherHandle;\r | |
798 | EFI_FILE_INFO *FileInfo;\r | |
799 | \r | |
800 | Size = 0;\r | |
b1f95a06 | 801 | \r |
802 | //\r | |
803 | // Check our parameters\r | |
804 | //\r | |
805 | if (FullFileName == NULL || Handle == NULL) {\r | |
806 | return (EFI_INVALID_PARAMETER);\r | |
807 | }\r | |
808 | \r | |
d595d4b1 | 809 | *FullFileName = NULL;\r |
a405b86d | 810 | CurrentHandle = NULL;\r |
d595d4b1 | 811 | \r |
b1f95a06 | 812 | Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);\r |
813 | if (!EFI_ERROR(Status)) {\r | |
814 | //\r | |
815 | // Reverse out the current directory on the device\r | |
816 | //\r | |
817 | for (;;) {\r | |
818 | FileInfo = FileHandleGetInfo(CurrentHandle);\r | |
819 | if (FileInfo == NULL) {\r | |
820 | Status = EFI_OUT_OF_RESOURCES;\r | |
821 | break;\r | |
822 | } else {\r | |
823 | //\r | |
824 | // We got info... do we have a name? if yes preceed the current path with it...\r | |
825 | //\r | |
826 | if (StrLen (FileInfo->FileName) == 0) {\r | |
46f43bc4 | 827 | if (*FullFileName == NULL) {\r |
a405b86d | 828 | ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r |
46f43bc4 | 829 | *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r |
830 | }\r | |
b1f95a06 | 831 | FreePool(FileInfo);\r |
832 | break;\r | |
833 | } else {\r | |
46f43bc4 | 834 | if (*FullFileName == NULL) {\r |
a405b86d | 835 | ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r |
46f43bc4 | 836 | *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r |
837 | }\r | |
a405b86d | 838 | ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r |
b1f95a06 | 839 | *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);\r |
46f43bc4 | 840 | *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r |
b1f95a06 | 841 | FreePool(FileInfo);\r |
842 | }\r | |
843 | }\r | |
844 | //\r | |
845 | // Move to the parent directory\r | |
846 | //\r | |
847 | Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);\r | |
848 | if (EFI_ERROR (Status)) {\r | |
849 | break;\r | |
850 | }\r | |
851 | \r | |
852 | FileHandleClose(CurrentHandle);\r | |
853 | CurrentHandle = NextHigherHandle;\r | |
854 | }\r | |
a405b86d | 855 | } else if (Status == EFI_NOT_FOUND) {\r |
856 | Status = EFI_SUCCESS;\r | |
857 | ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r | |
858 | *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r | |
b1f95a06 | 859 | }\r |
860 | \r | |
ad6f6f86 QS |
861 | if (*FullFileName != NULL && \r |
862 | (*FullFileName)[StrLen(*FullFileName) - 1] == L'\\' && \r | |
863 | StrLen(*FullFileName) > 1 &&\r | |
864 | FileHandleIsDirectory(Handle) == EFI_NOT_FOUND\r | |
865 | ) {\r | |
866 | (*FullFileName)[StrLen(*FullFileName) - 1] = CHAR_NULL;\r | |
867 | }\r | |
868 | \r | |
b1f95a06 | 869 | if (CurrentHandle != NULL) {\r |
870 | CurrentHandle->Close (CurrentHandle);\r | |
871 | }\r | |
872 | \r | |
873 | if (EFI_ERROR(Status) && *FullFileName != NULL) {\r | |
9eb53ac3 | 874 | FreePool(*FullFileName);\r |
b1f95a06 | 875 | }\r |
876 | \r | |
877 | return (Status);\r | |
878 | }\r | |
879 | \r | |
b3011f40 | 880 | /**\r |
a405b86d | 881 | Function to read a single line from a file. The \n is not included in the returned\r |
b3011f40 | 882 | buffer. The returned buffer must be callee freed.\r |
883 | \r | |
a405b86d | 884 | If the position upon start is 0, then the Ascii Boolean will be set. This should be\r |
b3011f40 | 885 | maintained and not changed for all operations with the same file.\r |
886 | \r | |
4ff7e37b ED |
887 | @param[in] Handle FileHandle to read from.\r |
888 | @param[in, out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);\r | |
b3011f40 | 889 | \r |
890 | @return The line of text from the file.\r | |
891 | \r | |
892 | @sa FileHandleReadLine\r | |
893 | **/\r | |
894 | CHAR16*\r | |
895 | EFIAPI\r | |
896 | FileHandleReturnLine(\r | |
897 | IN EFI_FILE_HANDLE Handle,\r | |
898 | IN OUT BOOLEAN *Ascii\r | |
899 | )\r | |
900 | {\r | |
901 | CHAR16 *RetVal;\r | |
902 | UINTN Size;\r | |
903 | EFI_STATUS Status;\r | |
904 | \r | |
905 | Size = 0;\r | |
906 | RetVal = NULL;\r | |
907 | \r | |
908 | Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r | |
909 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
4eb59be3 | 910 | RetVal = AllocateZeroPool(Size);\r |
b3011f40 | 911 | Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r |
912 | }\r | |
913 | ASSERT_EFI_ERROR(Status);\r | |
914 | if (EFI_ERROR(Status) && (RetVal != NULL)) {\r | |
915 | FreePool(RetVal);\r | |
916 | RetVal = NULL;\r | |
917 | }\r | |
918 | return (RetVal);\r | |
919 | }\r | |
920 | \r | |
b1f95a06 | 921 | /**\r |
bfc2fc9e | 922 | Function to read a single line (up to but not including the \n) from a file.\r |
b1f95a06 | 923 | \r |
a405b86d | 924 | If the position upon start is 0, then the Ascii Boolean will be set. This should be\r |
b3011f40 | 925 | maintained and not changed for all operations with the same file.\r |
ba180625 QS |
926 | The function will not return the \r and \n character in buffer. When an empty line is\r |
927 | read a CHAR_NULL character will be returned in buffer.\r | |
b3011f40 | 928 | \r |
bfc2fc9e QS |
929 | @param[in] Handle FileHandle to read from.\r |
930 | @param[in, out] Buffer The pointer to buffer to read into.\r | |
931 | @param[in, out] Size The pointer to number of bytes in Buffer.\r | |
932 | @param[in] Truncate If the buffer is large enough, this has no effect.\r | |
933 | If the buffer is is too small and Truncate is TRUE,\r | |
934 | the line will be truncated.\r | |
935 | If the buffer is is too small and Truncate is FALSE,\r | |
936 | then no read will occur.\r | |
937 | \r | |
938 | @param[in, out] Ascii Boolean value for indicating whether the file is\r | |
939 | Ascii (TRUE) or UCS2 (FALSE).\r | |
940 | \r | |
941 | @retval EFI_SUCCESS The operation was successful. The line is stored in\r | |
b1f95a06 | 942 | Buffer.\r |
943 | @retval EFI_INVALID_PARAMETER Handle was NULL.\r | |
b1f95a06 | 944 | @retval EFI_INVALID_PARAMETER Size was NULL.\r |
bfc2fc9e QS |
945 | @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.\r |
946 | Size was updated to the minimum space required.\r | |
b1f95a06 | 947 | @sa FileHandleRead\r |
948 | **/\r | |
949 | EFI_STATUS\r | |
950 | EFIAPI\r | |
951 | FileHandleReadLine(\r | |
952 | IN EFI_FILE_HANDLE Handle,\r | |
b3011f40 | 953 | IN OUT CHAR16 *Buffer,\r |
b1f95a06 | 954 | IN OUT UINTN *Size,\r |
b3011f40 | 955 | IN BOOLEAN Truncate,\r |
956 | IN OUT BOOLEAN *Ascii\r | |
a405b86d | 957 | )\r |
958 | {\r | |
b1f95a06 | 959 | EFI_STATUS Status;\r |
960 | CHAR16 CharBuffer;\r | |
bfc2fc9e | 961 | UINT64 FileSize;\r |
b1f95a06 | 962 | UINTN CharSize;\r |
963 | UINTN CountSoFar;\r | |
ba180625 | 964 | UINTN CrCount;\r |
b3011f40 | 965 | UINT64 OriginalFilePosition;\r |
b1f95a06 | 966 | \r |
b1f95a06 | 967 | if (Handle == NULL\r |
b1f95a06 | 968 | ||Size == NULL\r |
183ecff5 | 969 | ||(Buffer==NULL&&*Size!=0)\r |
a405b86d | 970 | ){\r |
971 | return (EFI_INVALID_PARAMETER);\r | |
bfc2fc9e QS |
972 | } \r |
973 | \r | |
ba180625 | 974 | if (Buffer != NULL && *Size != 0) {\r |
a405b86d | 975 | *Buffer = CHAR_NULL;\r |
ba180625 | 976 | } \r |
bfc2fc9e QS |
977 | \r |
978 | Status = FileHandleGetSize (Handle, &FileSize);\r | |
979 | if (EFI_ERROR (Status)) {\r | |
980 | return Status;\r | |
981 | } else if (FileSize == 0) {\r | |
ba180625 | 982 | *Ascii = TRUE;\r |
bfc2fc9e QS |
983 | return EFI_SUCCESS;\r |
984 | } \r | |
985 | \r | |
b3011f40 | 986 | FileHandleGetPosition(Handle, &OriginalFilePosition);\r |
987 | if (OriginalFilePosition == 0) {\r | |
988 | CharSize = sizeof(CHAR16);\r | |
989 | Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r | |
990 | ASSERT_EFI_ERROR(Status);\r | |
4eb59be3 | 991 | if (CharBuffer == gUnicodeFileTag) {\r |
b3011f40 | 992 | *Ascii = FALSE;\r |
993 | } else {\r | |
994 | *Ascii = TRUE;\r | |
995 | FileHandleSetPosition(Handle, OriginalFilePosition);\r | |
996 | }\r | |
997 | }\r | |
b1f95a06 | 998 | \r |
ba180625 | 999 | CrCount = 0;\r |
b1f95a06 | 1000 | for (CountSoFar = 0;;CountSoFar++){\r |
b3011f40 | 1001 | CharBuffer = 0;\r |
1002 | if (*Ascii) {\r | |
1003 | CharSize = sizeof(CHAR8);\r | |
1004 | } else {\r | |
1005 | CharSize = sizeof(CHAR16);\r | |
1006 | }\r | |
b1f95a06 | 1007 | Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r |
a405b86d | 1008 | if ( EFI_ERROR(Status)\r |
1009 | || CharSize == 0\r | |
1010 | || (CharBuffer == L'\n' && !(*Ascii))\r | |
1011 | || (CharBuffer == '\n' && *Ascii)\r | |
1012 | ){\r | |
b1f95a06 | 1013 | break;\r |
ba180625 QS |
1014 | } else if (\r |
1015 | (CharBuffer == L'\r' && !(*Ascii)) ||\r | |
1016 | (CharBuffer == '\r' && *Ascii)\r | |
1017 | ) {\r | |
1018 | CrCount++;\r | |
1019 | continue;\r | |
b1f95a06 | 1020 | }\r |
1021 | //\r | |
1022 | // if we have space save it...\r | |
1023 | //\r | |
ba180625 | 1024 | if ((CountSoFar+1-CrCount)*sizeof(CHAR16) < *Size){\r |
b3011f40 | 1025 | ASSERT(Buffer != NULL);\r |
ba180625 QS |
1026 | ((CHAR16*)Buffer)[CountSoFar-CrCount] = CharBuffer;\r |
1027 | ((CHAR16*)Buffer)[CountSoFar+1-CrCount] = CHAR_NULL;\r | |
b1f95a06 | 1028 | }\r |
1029 | }\r | |
1030 | \r | |
1031 | //\r | |
1032 | // if we ran out of space tell when...\r | |
1033 | //\r | |
ba180625 QS |
1034 | if ((CountSoFar+1-CrCount)*sizeof(CHAR16) > *Size){\r |
1035 | *Size = (CountSoFar+1-CrCount)*sizeof(CHAR16);\r | |
a405b86d | 1036 | if (!Truncate) {\r |
ba180625 QS |
1037 | if (Buffer != NULL && *Size != 0) {\r |
1038 | ZeroMem(Buffer, *Size);\r | |
1039 | }\r | |
b3011f40 | 1040 | FileHandleSetPosition(Handle, OriginalFilePosition);\r |
ba180625 | 1041 | return (EFI_BUFFER_TOO_SMALL);\r |
b1f95a06 | 1042 | } else {\r |
b3011f40 | 1043 | DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));\r |
ba180625 | 1044 | return (EFI_SUCCESS);\r |
b1f95a06 | 1045 | }\r |
b3011f40 | 1046 | }\r |
1047 | \r | |
b1f95a06 | 1048 | return (Status);\r |
1049 | }\r | |
1050 | \r | |
1051 | /**\r | |
a86c73cd QS |
1052 | Function to write a line of text to a file.\r |
1053 | \r | |
1054 | If the file is a Unicode file (with UNICODE file tag) then write the unicode \r | |
1055 | text.\r | |
1056 | If the file is an ASCII file then write the ASCII text.\r | |
1057 | If the size of file is zero (without file tag at the beginning) then write \r | |
1058 | ASCII text as default.\r | |
b1f95a06 | 1059 | \r |
b269f895 QS |
1060 | @param[in] Handle FileHandle to write to.\r |
1061 | @param[in] Buffer Buffer to write, if NULL the function will\r | |
1062 | take no action and return EFI_SUCCESS.\r | |
b1f95a06 | 1063 | \r |
b269f895 QS |
1064 | @retval EFI_SUCCESS The data was written.\r |
1065 | Buffer is NULL.\r | |
1066 | @retval EFI_INVALID_PARAMETER Handle is NULL.\r | |
a86c73cd QS |
1067 | @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII \r |
1068 | string due to out of resources.\r | |
b1f95a06 | 1069 | \r |
1070 | @sa FileHandleWrite\r | |
1071 | **/\r | |
1072 | EFI_STATUS\r | |
1073 | EFIAPI\r | |
1074 | FileHandleWriteLine(\r | |
1075 | IN EFI_FILE_HANDLE Handle,\r | |
1076 | IN CHAR16 *Buffer\r | |
a405b86d | 1077 | )\r |
1078 | {\r | |
a86c73cd QS |
1079 | EFI_STATUS Status;\r |
1080 | CHAR16 CharBuffer;\r | |
1081 | UINTN Size;\r | |
1cdc35c4 | 1082 | UINTN Index;\r |
a86c73cd QS |
1083 | UINTN CharSize;\r |
1084 | UINT64 FileSize;\r | |
1085 | UINT64 OriginalFilePosition;\r | |
1086 | BOOLEAN Ascii;\r | |
1087 | CHAR8 *AsciiBuffer;\r | |
b1f95a06 | 1088 | \r |
b1f95a06 | 1089 | if (Buffer == NULL) {\r |
1090 | return (EFI_SUCCESS);\r | |
1091 | }\r | |
1092 | \r | |
183ecff5 JC |
1093 | if (Handle == NULL) {\r |
1094 | return (EFI_INVALID_PARAMETER);\r | |
1095 | }\r | |
a86c73cd QS |
1096 | \r |
1097 | Ascii = FALSE;\r | |
1098 | AsciiBuffer = NULL;\r | |
1099 | \r | |
1100 | Status = FileHandleGetPosition(Handle, &OriginalFilePosition);\r | |
b1f95a06 | 1101 | if (EFI_ERROR(Status)) {\r |
a86c73cd QS |
1102 | return Status;\r |
1103 | }\r | |
1104 | \r | |
1105 | Status = FileHandleSetPosition(Handle, 0);\r | |
1106 | if (EFI_ERROR(Status)) {\r | |
1107 | return Status;\r | |
b1f95a06 | 1108 | }\r |
a86c73cd QS |
1109 | \r |
1110 | Status = FileHandleGetSize(Handle, &FileSize);\r | |
1111 | if (EFI_ERROR(Status)) {\r | |
1112 | return Status;\r | |
1113 | }\r | |
1114 | \r | |
1115 | if (FileSize == 0) {\r | |
1116 | Ascii = TRUE;\r | |
1117 | } else {\r | |
1118 | CharSize = sizeof (CHAR16);\r | |
1119 | Status = FileHandleRead (Handle, &CharSize, &CharBuffer);\r | |
1120 | ASSERT_EFI_ERROR (Status);\r | |
1121 | if (CharBuffer == gUnicodeFileTag) {\r | |
1122 | Ascii = FALSE;\r | |
1123 | } else {\r | |
1124 | Ascii = TRUE;\r | |
1125 | }\r | |
1126 | }\r | |
1127 | \r | |
1128 | Status = FileHandleSetPosition(Handle, OriginalFilePosition);\r | |
1129 | if (EFI_ERROR(Status)) {\r | |
1130 | return Status;\r | |
1131 | }\r | |
1132 | \r | |
1133 | if (Ascii) {\r | |
1134 | Size = ( StrSize(Buffer) / sizeof(CHAR16) ) * sizeof(CHAR8);\r | |
1135 | AsciiBuffer = (CHAR8 *)AllocateZeroPool(Size);\r | |
1136 | if (AsciiBuffer == NULL) {\r | |
1137 | return EFI_OUT_OF_RESOURCES;\r | |
1138 | }\r | |
ca2ac9d5 | 1139 | UnicodeStrToAsciiStrS (Buffer, AsciiBuffer, Size);\r |
1cdc35c4 QS |
1140 | for (Index = 0; Index < Size; Index++) {\r |
1141 | if (!((AsciiBuffer[Index] >= 0) && (AsciiBuffer[Index] < 128))){\r | |
1142 | FreePool(AsciiBuffer);\r | |
1143 | return EFI_INVALID_PARAMETER;\r | |
1144 | }\r | |
1145 | }\r | |
a86c73cd QS |
1146 | \r |
1147 | Size = AsciiStrSize(AsciiBuffer) - sizeof(CHAR8);\r | |
1148 | Status = FileHandleWrite(Handle, &Size, AsciiBuffer);\r | |
1149 | if (EFI_ERROR(Status)) {\r | |
1150 | FreePool (AsciiBuffer);\r | |
1151 | return (Status);\r | |
1152 | }\r | |
1153 | Size = AsciiStrSize("\r\n") - sizeof(CHAR8);\r | |
1154 | Status = FileHandleWrite(Handle, &Size, "\r\n");\r | |
1155 | } else {\r | |
1156 | if (OriginalFilePosition == 0) {\r | |
1157 | Status = FileHandleSetPosition (Handle, sizeof(CHAR16));\r | |
1158 | if (EFI_ERROR(Status)) {\r | |
1159 | return Status;\r | |
1160 | }\r | |
1161 | }\r | |
1162 | Size = StrSize(Buffer) - sizeof(CHAR16);\r | |
1163 | Status = FileHandleWrite(Handle, &Size, Buffer);\r | |
1164 | if (EFI_ERROR(Status)) {\r | |
1165 | return (Status);\r | |
1166 | }\r | |
1167 | Size = StrSize(L"\r\n") - sizeof(CHAR16);\r | |
1168 | Status = FileHandleWrite(Handle, &Size, L"\r\n");\r | |
1169 | }\r | |
1170 | \r | |
1171 | if (AsciiBuffer != NULL) {\r | |
1172 | FreePool (AsciiBuffer);\r | |
1173 | }\r | |
1174 | return Status;\r | |
b1f95a06 | 1175 | }\r |
b3011f40 | 1176 | \r |
1177 | /**\r | |
1178 | function to take a formatted argument and print it to a file.\r | |
1179 | \r | |
1180 | @param[in] Handle the file handle for the file to write to\r | |
1181 | @param[in] Format the format argument (see printlib for format specifier)\r | |
1182 | @param[in] ... the variable arguments for the format\r | |
1183 | \r | |
1184 | @retval EFI_SUCCESS the operation was sucessful\r | |
1185 | @return other a return value from FileHandleWriteLine\r | |
1186 | \r | |
1187 | @sa FileHandleWriteLine\r | |
1188 | **/\r | |
1189 | EFI_STATUS\r | |
1190 | EFIAPI\r | |
1191 | FileHandlePrintLine(\r | |
1192 | IN EFI_FILE_HANDLE Handle,\r | |
1193 | IN CONST CHAR16 *Format,\r | |
1194 | ...\r | |
1195 | )\r | |
1196 | {\r | |
1197 | VA_LIST Marker;\r | |
1198 | CHAR16 *Buffer;\r | |
1199 | EFI_STATUS Status;\r | |
1200 | \r | |
b3011f40 | 1201 | //\r |
1202 | // Get a buffer to print into\r | |
1203 | //\r | |
ae591c14 | 1204 | Buffer = AllocateZeroPool (PcdGet16 (PcdUefiFileHandleLibPrintBufferSize));\r |
183ecff5 JC |
1205 | if (Buffer == NULL) {\r |
1206 | return (EFI_OUT_OF_RESOURCES);\r | |
1207 | }\r | |
b3011f40 | 1208 | \r |
1209 | //\r | |
1210 | // Print into our buffer\r | |
1211 | //\r | |
3bbe68a3 | 1212 | VA_START (Marker, Format);\r |
ae591c14 | 1213 | UnicodeVSPrint (Buffer, PcdGet16 (PcdUefiFileHandleLibPrintBufferSize), Format, Marker);\r |
3bbe68a3 | 1214 | VA_END (Marker);\r |
b3011f40 | 1215 | \r |
1216 | //\r | |
1217 | // Print buffer into file\r | |
1218 | //\r | |
1219 | Status = FileHandleWriteLine(Handle, Buffer);\r | |
1220 | \r | |
1221 | //\r | |
a405b86d | 1222 | // Cleanup and return\r |
b3011f40 | 1223 | //\r |
1224 | FreePool(Buffer);\r | |
1225 | return (Status);\r | |
1226 | }\r | |
1227 | \r | |
1228 | /**\r | |
1229 | Function to determine if a FILE_HANDLE is at the end of the file.\r | |
1230 | \r | |
1231 | This will NOT work on directories.\r | |
1232 | \r | |
183ecff5 | 1233 | If Handle is NULL, then return False.\r |
b3011f40 | 1234 | \r |
1235 | @param[in] Handle the file handle\r | |
1236 | \r | |
1237 | @retval TRUE the position is at the end of the file\r | |
1238 | @retval FALSE the position is not at the end of the file\r | |
1239 | **/\r | |
1240 | BOOLEAN\r | |
1241 | EFIAPI\r | |
1242 | FileHandleEof(\r | |
1243 | IN EFI_FILE_HANDLE Handle\r | |
1244 | )\r | |
1245 | {\r | |
1246 | EFI_FILE_INFO *Info;\r | |
1247 | UINT64 Pos;\r | |
1248 | BOOLEAN RetVal;\r | |
1249 | \r | |
183ecff5 JC |
1250 | if (Handle == NULL) {\r |
1251 | return (FALSE);\r | |
1252 | }\r | |
a405b86d | 1253 | \r |
b3011f40 | 1254 | FileHandleGetPosition(Handle, &Pos);\r |
1255 | Info = FileHandleGetInfo (Handle);\r | |
a405b86d | 1256 | \r |
b3011f40 | 1257 | if (Info == NULL) {\r |
1258 | return (FALSE);\r | |
a405b86d | 1259 | }\r |
b3011f40 | 1260 | \r |
183ecff5 JC |
1261 | FileHandleSetPosition(Handle, Pos);\r |
1262 | \r | |
d54744cd | 1263 | if (Pos == Info->FileSize) {\r |
1264 | RetVal = TRUE;\r | |
1265 | } else {\r | |
1266 | RetVal = FALSE;\r | |
1267 | }\r | |
b3011f40 | 1268 | \r |
1269 | FreePool (Info);\r | |
1270 | \r | |
1271 | return (RetVal);\r | |
22d11953 | 1272 | }\r |