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