]>
Commit | Line | Data |
---|---|---|
d2b4564b | 1 | /** @file\r |
2 | Provides interface to EFI_FILE_HANDLE functionality.\r | |
3 | \r | |
4 | Copyright (c) 2006 - 2009, Intel Corporation\r | |
5 | All rights reserved. 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 | |
9 | \r | |
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 | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include <Uefi.h>\r | |
16 | \r | |
b1f95a06 | 17 | #include <Protocol/SimpleFileSystem.h>\r |
18 | \r | |
19 | #include <Guid/FileInfo.h>\r | |
20 | \r | |
d2b4564b | 21 | #include <Library/DebugLib.h>\r |
22 | #include <Library/MemoryAllocationLib.h>\r | |
b1f95a06 | 23 | #include <Library/BaseLib.h>\r |
24 | #include <Library/BaseMemoryLib.h>\r | |
d2b4564b | 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 | |
69817bf8 | 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 | |
d2b4564b | 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_GUID FileInfoGuid;\r | |
50 | EFI_FILE_INFO *pFileInfo;\r | |
51 | UINTN FileInfoSize;\r | |
52 | EFI_STATUS Status;\r | |
53 | \r | |
54 | //\r | |
55 | // ASSERT if FileHandle is NULL\r | |
56 | //\r | |
57 | ASSERT (FileHandle != NULL);\r | |
58 | \r | |
59 | //\r | |
60 | // Get the required size to allocate\r | |
61 | //\r | |
62 | FileInfoGuid = gEfiFileInfoGuid;\r | |
63 | FileInfoSize = 0;\r | |
64 | pFileInfo = NULL;\r | |
65 | Status = FileHandle->GetInfo(FileHandle, \r | |
66 | &FileInfoGuid, \r | |
67 | &FileInfoSize, \r | |
68 | pFileInfo);\r | |
69 | //\r | |
70 | // error is expected. getting size to allocate\r | |
71 | //\r | |
72 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
73 | pFileInfo = AllocateZeroPool(FileInfoSize);\r | |
74 | ASSERT (pFileInfo != NULL);\r | |
75 | //\r | |
76 | // now get the information\r | |
77 | //\r | |
78 | Status = FileHandle->GetInfo(FileHandle, \r | |
79 | &FileInfoGuid, \r | |
80 | &FileInfoSize, \r | |
81 | pFileInfo);\r | |
82 | //\r | |
83 | // if we got an error free the memory and return NULL\r | |
84 | //\r | |
85 | if (EFI_ERROR(Status)) {\r | |
86 | FreePool(pFileInfo);\r | |
87 | return NULL;\r | |
88 | }\r | |
89 | return (pFileInfo);\r | |
90 | }\r | |
91 | \r | |
92 | /**\r | |
93 | This function will set the information about the file for the opened handle \r | |
94 | specified.\r | |
95 | \r | |
96 | @param FileHandle The file handle of the file for which information \r | |
97 | is being set\r | |
98 | \r | |
99 | @param FileInfo The infotmation to set.\r | |
100 | \r | |
101 | @retval EFI_SUCCESS The information was set.\r | |
102 | @retval EFI_UNSUPPORTED The InformationType is not known.\r | |
103 | @retval EFI_NO_MEDIA The device has no medium.\r | |
104 | @retval EFI_DEVICE_ERROR The device reported an error.\r | |
105 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r | |
106 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r | |
107 | @retval EFI_ACCESS_DENIED The file was opened read only.\r | |
108 | @retval EFI_VOLUME_FULL The volume is full.\r | |
109 | **/\r | |
110 | EFI_STATUS\r | |
111 | EFIAPI\r | |
112 | FileHandleSetInfo (\r | |
113 | IN EFI_FILE_HANDLE FileHandle,\r | |
114 | IN CONST EFI_FILE_INFO *FileInfo\r | |
115 | )\r | |
116 | {\r | |
117 | EFI_GUID FileInfoGuid;\r | |
118 | \r | |
119 | //\r | |
120 | // ASSERT if the FileHandle or FileInfo is NULL\r | |
121 | //\r | |
122 | ASSERT (FileHandle != NULL);\r | |
123 | ASSERT (FileInfo != NULL);\r | |
124 | \r | |
125 | FileInfoGuid = gEfiFileInfoGuid;\r | |
126 | //\r | |
127 | // Set the info\r | |
128 | //\r | |
129 | return (FileHandle->SetInfo(FileHandle, \r | |
130 | &FileInfoGuid,\r | |
131 | (UINTN)FileInfo->Size,\r | |
132 | (EFI_FILE_INFO*)FileInfo));\r | |
133 | } \r | |
134 | \r | |
135 | /**\r | |
136 | This function reads information from an opened file.\r | |
137 | \r | |
138 | If FileHandle is not a directory, the function reads the requested number of \r | |
69817bf8 | 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 |
69817bf8 | 141 | end of the file. The file's current position is increased by the number of bytes \r |
d2b4564b | 142 | returned. If FileHandle is a directory, the function reads the directory entry \r |
69817bf8 | 143 | at the file's current position and returns the entry in Buffer. If the Buffer \r |
d2b4564b | 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 | |
149 | EFI_FILE_INFO is the structure returned as the directory entry.\r | |
150 | \r | |
151 | @param FileHandle the opened file handle\r | |
152 | @param BufferSize on input the size of buffer in bytes. on return \r | |
153 | the number of bytes written.\r | |
154 | @param Buffer the buffer to put read data into.\r | |
155 | \r | |
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 | |
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 | |
172 | //\r | |
173 | // ASSERT if FileHandle is NULL\r | |
174 | //\r | |
175 | ASSERT (FileHandle != NULL);\r | |
176 | \r | |
177 | //\r | |
178 | // Perform the read based on EFI_FILE_PROTOCOL\r | |
179 | //\r | |
180 | return (FileHandle->Read(FileHandle, BufferSize, Buffer));\r | |
181 | }\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 | |
69817bf8 | 190 | has been a data error during the write attempt (such as "volume space full"). \r |
d2b4564b | 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 | //\r | |
217 | // ASSERT if FileHandle is NULL\r | |
218 | //\r | |
219 | ASSERT (FileHandle != NULL);\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 | |
69817bf8 | 229 | This function closes a specified file handle. All "dirty" cached file data is \r |
d2b4564b | 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 sucessfully.\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 | // ASSERT if FileHandle is NULL\r | |
246 | //\r | |
247 | ASSERT (FileHandle != NULL);\r | |
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 | |
259 | If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is \r | |
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 | |
265 | @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not \r | |
266 | deleted\r | |
267 | @retval INVALID_PARAMETER One of the parameters has an invalid value.\r | |
268 | **/\r | |
269 | EFI_STATUS\r | |
270 | EFIAPI\r | |
271 | FileHandleDelete (\r | |
272 | IN EFI_FILE_HANDLE FileHandle\r | |
273 | )\r | |
274 | {\r | |
275 | EFI_STATUS Status;\r | |
276 | //\r | |
277 | // ASSERT if FileHandle is NULL\r | |
278 | //\r | |
279 | ASSERT (FileHandle != NULL);\r | |
280 | //\r | |
281 | // Perform the Delete based on EFI_FILE_PROTOCOL\r | |
282 | //\r | |
283 | Status = FileHandle->Delete(FileHandle);\r | |
284 | return Status;\r | |
285 | }\r | |
286 | \r | |
287 | /**\r | |
288 | Set the current position in a file.\r | |
289 | \r | |
290 | This function sets the current file position for the handle to the position \r | |
291 | supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r | |
292 | absolute positioning is supported, and seeking past the end of the file is \r | |
293 | allowed (a subsequent write would grow the file). Seeking to position \r | |
294 | 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r | |
295 | If FileHandle is a directory, the only position that may be set is zero. This \r | |
296 | has the effect of starting the read process of the directory entries over.\r | |
297 | \r | |
298 | @param FileHandle The file handle on which the position is being set\r | |
299 | @param Position Byte position from begining of file\r | |
300 | \r | |
301 | @retval EFI_SUCCESS Operation completed sucessfully.\r | |
302 | @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on \r | |
303 | directories.\r | |
304 | @retval INVALID_PARAMETER One of the parameters has an invalid value.\r | |
305 | **/\r | |
306 | EFI_STATUS\r | |
307 | EFIAPI\r | |
308 | FileHandleSetPosition (\r | |
309 | IN EFI_FILE_HANDLE FileHandle,\r | |
310 | IN UINT64 Position\r | |
311 | )\r | |
312 | {\r | |
313 | //\r | |
314 | // ASSERT if FileHandle is NULL\r | |
315 | //\r | |
316 | ASSERT (FileHandle != NULL);\r | |
317 | //\r | |
318 | // Perform the SetPosition based on EFI_FILE_PROTOCOL\r | |
319 | //\r | |
320 | return (FileHandle->SetPosition(FileHandle, Position));\r | |
321 | }\r | |
322 | \r | |
323 | /** \r | |
324 | Gets a file's current position\r | |
325 | \r | |
326 | This function retrieves the current file position for the file handle. For \r | |
327 | directories, the current file position has no meaning outside of the file \r | |
328 | system driver and as such the operation is not supported. An error is returned\r | |
329 | if FileHandle is a directory.\r | |
330 | \r | |
331 | @param FileHandle The open file handle on which to get the position.\r | |
332 | @param Position Byte position from begining of file.\r | |
333 | \r | |
334 | @retval EFI_SUCCESS the operation completed sucessfully.\r | |
335 | @retval INVALID_PARAMETER One of the parameters has an invalid value.\r | |
336 | @retval EFI_UNSUPPORTED the request is not valid on directories.\r | |
337 | **/\r | |
338 | EFI_STATUS\r | |
339 | EFIAPI\r | |
340 | FileHandleGetPosition (\r | |
341 | IN EFI_FILE_HANDLE FileHandle,\r | |
342 | OUT UINT64 *Position\r | |
343 | )\r | |
344 | {\r | |
345 | //\r | |
346 | // ASSERT if FileHandle is NULL\r | |
347 | //\r | |
348 | ASSERT (FileHandle != NULL);\r | |
349 | //\r | |
350 | // Perform the GetPosition based on EFI_FILE_PROTOCOL\r | |
351 | //\r | |
352 | return (FileHandle->GetPosition(FileHandle, Position));\r | |
353 | }\r | |
354 | /**\r | |
355 | Flushes data on a file\r | |
356 | \r | |
357 | This function flushes all modified data associated with a file to a device.\r | |
358 | \r | |
359 | @param FileHandle The file handle on which to flush data\r | |
360 | \r | |
361 | @retval EFI_SUCCESS The data was flushed.\r | |
362 | @retval EFI_NO_MEDIA The device has no media.\r | |
363 | @retval EFI_DEVICE_ERROR The device reported an error.\r | |
364 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r | |
365 | @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r | |
366 | @retval EFI_ACCESS_DENIED The file was opened for read only.\r | |
367 | **/\r | |
368 | EFI_STATUS\r | |
369 | EFIAPI\r | |
370 | FileHandleFlush (\r | |
371 | IN EFI_FILE_HANDLE FileHandle\r | |
372 | )\r | |
373 | {\r | |
374 | //\r | |
375 | // ASSERT if FileHandle is NULL\r | |
376 | //\r | |
377 | ASSERT (FileHandle != NULL);\r | |
378 | //\r | |
379 | // Perform the Flush based on EFI_FILE_PROTOCOL\r | |
380 | //\r | |
381 | return (FileHandle->Flush(FileHandle));\r | |
382 | }\r | |
383 | \r | |
384 | /**\r | |
385 | function to determine if a given handle is a directory handle\r | |
386 | \r | |
387 | if DirHandle is NULL then ASSERT()\r | |
388 | \r | |
389 | open the file information on the DirHandle and verify that the Attribute\r | |
390 | includes EFI_FILE_DIRECTORY bit set.\r | |
391 | \r | |
392 | @param DirHandle Handle to open file\r | |
393 | \r | |
394 | @retval EFI_SUCCESS DirHandle is a directory\r | |
395 | @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available\r | |
396 | @retval EFI_NOT_FOUND DirHandle is not a directory\r | |
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 | |
406 | //\r | |
407 | // ASSERT if DirHandle is NULL\r | |
408 | //\r | |
409 | ASSERT(DirHandle != NULL);\r | |
410 | \r | |
411 | //\r | |
412 | // get the file information for DirHandle\r | |
413 | //\r | |
414 | DirInfo = FileHandleGetInfo (DirHandle);\r | |
415 | \r | |
416 | //\r | |
417 | // Parse DirInfo\r | |
418 | //\r | |
419 | if (DirInfo == NULL) {\r | |
420 | //\r | |
421 | // We got nothing...\r | |
422 | //\r | |
423 | return (EFI_INVALID_PARAMETER);\r | |
424 | } \r | |
425 | if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r | |
426 | //\r | |
427 | // Attributes say this is not a directory\r | |
428 | //\r | |
429 | FreePool (DirInfo);\r | |
430 | return (EFI_NOT_FOUND);\r | |
431 | }\r | |
432 | //\r | |
433 | // all good...\r | |
434 | //\r | |
435 | FreePool (DirInfo);\r | |
436 | return (EFI_SUCCESS);\r | |
437 | }\r | |
438 | \r | |
439 | /**\r | |
440 | Retrieves the first file from a directory\r | |
441 | \r | |
69817bf8 | 442 | This function opens a directory and gets the first file's info in the \r |
d2b4564b | 443 | directory. Caller can use FileHandleFindNextFile() to get other files. When \r |
444 | complete the caller is responsible for calling FreePool() on Buffer.\r | |
445 | \r | |
446 | @param DirHandle The file handle of the directory to search\r | |
447 | @param Buffer Pointer to buffer for file's information\r | |
448 | \r | |
449 | @retval EFI_SUCCESS Found the first file.\r | |
450 | @retval EFI_NOT_FOUND Cannot find the directory.\r | |
451 | @retval EFI_NO_MEDIA The device has no media.\r | |
452 | @retval EFI_DEVICE_ERROR The device reported an error.\r | |
453 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r | |
454 | @return Others status of FileHandleGetInfo, FileHandleSetPosition,\r | |
455 | or FileHandleRead\r | |
456 | **/\r | |
457 | EFI_STATUS\r | |
458 | EFIAPI\r | |
459 | FileHandleFindFirstFile (\r | |
460 | IN EFI_FILE_HANDLE DirHandle,\r | |
461 | OUT EFI_FILE_INFO **Buffer\r | |
462 | )\r | |
463 | {\r | |
464 | EFI_STATUS Status;\r | |
465 | UINTN BufferSize;\r | |
466 | \r | |
467 | //\r | |
468 | // ASSERTs\r | |
469 | //\r | |
470 | ASSERT (DirHandle != NULL);\r | |
471 | ASSERT (Buffer != NULL);\r | |
472 | \r | |
473 | //\r | |
474 | // verify that DirHandle is a directory\r | |
475 | //\r | |
476 | Status = FileHandleIsDirectory(DirHandle);\r | |
477 | if (EFI_ERROR(Status)) {\r | |
478 | return (Status);\r | |
479 | } \r | |
480 | \r | |
481 | //\r | |
482 | // reset to the begining of the directory \r | |
483 | //\r | |
484 | Status = FileHandleSetPosition(DirHandle, 0);\r | |
485 | if (EFI_ERROR(Status)) {\r | |
486 | return (Status);\r | |
487 | } \r | |
488 | \r | |
489 | //\r | |
490 | // Allocate a buffer sized to struct size + enough for the string at the end\r | |
491 | //\r | |
492 | BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;\r | |
493 | *Buffer = AllocateZeroPool(BufferSize);\r | |
494 | ASSERT (*Buffer != NULL);\r | |
495 | \r | |
496 | //\r | |
497 | // read in the info about the first file\r | |
498 | //\r | |
499 | Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);\r | |
500 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
501 | if (EFI_ERROR(Status)) {\r | |
502 | FreePool(*Buffer);\r | |
503 | *Buffer = NULL;\r | |
504 | return (Status);\r | |
505 | }\r | |
506 | return (EFI_SUCCESS);\r | |
507 | }\r | |
508 | /**\r | |
509 | Retrieves the next file in a directory.\r | |
510 | \r | |
511 | To use this function, caller must call the FileHandleFindFirstFile() to get the \r | |
512 | first file, and then use this function get other files. This function can be \r | |
513 | called for several times to get each file's information in the directory. If \r | |
514 | the call of FileHandleFindNextFile() got the last file in the directory, the next \r | |
515 | call of this function has no file to get. *NoFile will be set to TRUE and the \r | |
516 | Buffer memory will be automatically freed. \r | |
517 | \r | |
518 | @param DirHandle the file handle of the directory\r | |
519 | @param Buffer pointer to buffer for file's information\r | |
520 | @param NoFile pointer to boolean when last file is found\r | |
521 | \r | |
522 | @retval EFI_SUCCESS Found the next file, or reached last file\r | |
523 | @retval EFI_NO_MEDIA The device has no media.\r | |
524 | @retval EFI_DEVICE_ERROR The device reported an error.\r | |
525 | @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r | |
526 | **/\r | |
527 | EFI_STATUS\r | |
528 | EFIAPI\r | |
529 | FileHandleFindNextFile(\r | |
530 | IN EFI_FILE_HANDLE DirHandle,\r | |
531 | OUT EFI_FILE_INFO *Buffer,\r | |
532 | OUT BOOLEAN *NoFile\r | |
533 | )\r | |
534 | {\r | |
535 | EFI_STATUS Status;\r | |
536 | UINTN BufferSize;\r | |
537 | \r | |
538 | //\r | |
539 | // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL\r | |
540 | //\r | |
541 | ASSERT (DirHandle != NULL);\r | |
542 | ASSERT (Buffer != NULL);\r | |
543 | ASSERT (NoFile != NULL);\r | |
d2b4564b | 544 | \r |
545 | //\r | |
546 | // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile\r | |
547 | //\r | |
548 | BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;\r | |
549 | \r | |
550 | //\r | |
551 | // read in the info about the next file\r | |
552 | //\r | |
553 | Status = FileHandleRead (DirHandle, &BufferSize, Buffer);\r | |
554 | ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r | |
555 | if (EFI_ERROR(Status)) {\r | |
556 | return (Status);\r | |
557 | }\r | |
558 | \r | |
559 | //\r | |
560 | // If we read 0 bytes (but did not have erros) we already read in the last file.\r | |
561 | //\r | |
562 | if (BufferSize == 0) {\r | |
563 | FreePool(Buffer);\r | |
564 | *NoFile = TRUE;\r | |
565 | }\r | |
566 | \r | |
567 | return (EFI_SUCCESS);\r | |
568 | }\r | |
569 | /**\r | |
570 | Retrieve the size of a file.\r | |
571 | \r | |
572 | if FileHandle is NULL then ASSERT()\r | |
573 | if Size is NULL then ASSERT()\r | |
574 | \r | |
69817bf8 | 575 | This function extracts the file size info from the FileHandle's EFI_FILE_INFO \r |
d2b4564b | 576 | data.\r |
577 | \r | |
578 | @param FileHandle file handle from which size is retrieved\r | |
579 | @param Size pointer to size\r | |
580 | \r | |
581 | @retval EFI_SUCCESS operation was completed sucessfully\r | |
582 | @retval EFI_DEVICE_ERROR cannot access the file\r | |
583 | **/\r | |
584 | EFI_STATUS\r | |
585 | EFIAPI\r | |
586 | FileHandleGetSize (\r | |
587 | IN EFI_FILE_HANDLE FileHandle,\r | |
588 | OUT UINT64 *Size\r | |
589 | )\r | |
590 | {\r | |
591 | EFI_FILE_INFO *FileInfo;\r | |
592 | \r | |
593 | //\r | |
594 | // ASSERT for FileHandle or Size being NULL\r | |
595 | //\r | |
596 | ASSERT (FileHandle != NULL);\r | |
597 | ASSERT (Size != NULL);\r | |
598 | \r | |
599 | //\r | |
600 | // get the FileInfo structure\r | |
601 | //\r | |
602 | FileInfo = FileHandleGetInfo(FileHandle);\r | |
603 | if (FileInfo == NULL) {\r | |
604 | return (EFI_DEVICE_ERROR);\r | |
605 | }\r | |
606 | \r | |
607 | //\r | |
608 | // Assign the Size pointer to the correct value\r | |
609 | //\r | |
610 | *Size = FileInfo->FileSize;\r | |
611 | \r | |
612 | //\r | |
613 | // free the FileInfo memory\r | |
614 | //\r | |
615 | FreePool(FileInfo);\r | |
616 | \r | |
617 | return (EFI_SUCCESS);\r | |
b1f95a06 | 618 | }\r |
619 | \r | |
620 | \r | |
621 | /**\r | |
622 | Safely append (on the left) with automatic string resizing given length of Destination and \r | |
623 | desired length of copy from Source.\r | |
624 | \r | |
625 | append the first D characters of Source to the end of Destination, where D is \r | |
626 | the lesser of Count and the StrLen() of Source. If appending those D characters \r | |
627 | will fit within Destination (whose Size is given as CurrentSize) and \r | |
628 | still leave room for a null terminator, then those characters are appended, \r | |
629 | starting at the original terminating null of Destination, and a new terminating \r | |
630 | null is appended.\r | |
631 | \r | |
632 | If appending D characters onto Destination will result in a overflow of the size\r | |
633 | given in CurrentSize the string will be grown such that the copy can be performed\r | |
634 | and CurrentSize will be updated to the new size.\r | |
635 | \r | |
636 | If Source is NULL, there is nothing to append, just return the current buffer in \r | |
637 | Destination.\r | |
638 | \r | |
639 | if Destination is NULL, then ASSERT()\r | |
640 | if Destination's current length (including NULL terminator) is already more then \r | |
641 | CurrentSize, then ASSERT()\r | |
642 | \r | |
b82bfcc1 | 643 | @param[in,out] Destination The String to append onto\r |
644 | @param[in,out] CurrentSize on call the number of bytes in Destination. On \r | |
b1f95a06 | 645 | return possibly the new size (still in bytes). if NULL\r |
646 | then allocate whatever is needed.\r | |
647 | @param[in] Source The String to append from\r | |
648 | @param[in] Count Maximum number of characters to append. if 0 then \r | |
649 | all are appended.\r | |
650 | \r | |
651 | @return Destination return the resultant string.\r | |
652 | **/\r | |
653 | CHAR16* \r | |
654 | EFIAPI\r | |
655 | StrnCatGrowLeft (\r | |
656 | IN OUT CHAR16 **Destination,\r | |
657 | IN OUT UINTN *CurrentSize,\r | |
658 | IN CONST CHAR16 *Source,\r | |
659 | IN UINTN Count\r | |
660 | ){\r | |
661 | UINTN DestinationStartSize;\r | |
662 | UINTN NewSize;\r | |
727a4c71 | 663 | UINTN CopySize;\r |
b1f95a06 | 664 | \r |
665 | //\r | |
666 | // ASSERTs\r | |
667 | //\r | |
668 | ASSERT(Destination != NULL);\r | |
669 | \r | |
670 | //\r | |
671 | // If there's nothing to do then just return Destination\r | |
672 | //\r | |
673 | if (Source == NULL) {\r | |
674 | return (*Destination);\r | |
675 | }\r | |
676 | \r | |
677 | //\r | |
678 | // allow for NULL pointers address as Destination\r | |
679 | //\r | |
680 | if (*Destination != NULL) {\r | |
681 | ASSERT(CurrentSize != 0);\r | |
682 | DestinationStartSize = StrSize(*Destination);\r | |
683 | ASSERT(DestinationStartSize <= *CurrentSize);\r | |
684 | } else {\r | |
685 | DestinationStartSize = 0;\r | |
686 | // ASSERT(*CurrentSize == 0);\r | |
687 | }\r | |
688 | \r | |
689 | //\r | |
690 | // Append all of Source?\r | |
691 | //\r | |
692 | if (Count == 0) {\r | |
d595d4b1 | 693 | Count = StrSize(Source);\r |
b1f95a06 | 694 | }\r |
695 | \r | |
696 | //\r | |
697 | // Test and grow if required\r | |
698 | //\r | |
699 | if (CurrentSize != NULL) {\r | |
700 | NewSize = *CurrentSize;\r | |
d595d4b1 | 701 | while (NewSize < (DestinationStartSize + Count)) {\r |
702 | NewSize += 2 * Count;\r | |
b1f95a06 | 703 | }\r |
704 | *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r | |
705 | *CurrentSize = NewSize;\r | |
706 | } else {\r | |
d595d4b1 | 707 | *Destination = AllocateZeroPool(Count+sizeof(CHAR16));\r |
b1f95a06 | 708 | }\r |
709 | \r | |
727a4c71 | 710 | CopySize = StrSize(*Destination);\r |
d595d4b1 | 711 | *Destination = CopyMem(*Destination+Count-sizeof(CHAR16), *Destination, CopySize);\r |
712 | *Destination = CopyMem(*Destination, Source, Count);\r | |
b1f95a06 | 713 | return (*Destination);\r |
714 | }\r | |
715 | \r | |
716 | /**\r | |
717 | Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the \r | |
718 | directory 'stack'.\r | |
719 | \r | |
720 | if Handle is NULL, return EFI_INVALID_PARAMETER\r | |
721 | \r | |
722 | @param[in] Handle Handle to the Directory or File to create path to.\r | |
723 | @param[out] FullFileName pointer to pointer to generated full file name. It \r | |
724 | is the responsibility of the caller to free this memory\r | |
725 | with a call to FreePool().\r | |
726 | @retval EFI_SUCCESS the operation was sucessful and the FullFileName is valid.\r | |
727 | @retval EFI_INVALID_PARAMETER Handle was NULL.\r | |
728 | @retval EFI_INVALID_PARAMETER FullFileName was NULL.\r | |
729 | @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r | |
730 | **/\r | |
731 | EFI_STATUS\r | |
732 | EFIAPI\r | |
733 | FileHandleGetFileName (\r | |
734 | IN CONST EFI_FILE_HANDLE Handle,\r | |
735 | OUT CHAR16 **FullFileName\r | |
736 | ){\r | |
737 | EFI_STATUS Status;\r | |
738 | UINTN Size;\r | |
739 | EFI_FILE_HANDLE CurrentHandle;\r | |
740 | EFI_FILE_HANDLE NextHigherHandle;\r | |
741 | EFI_FILE_INFO *FileInfo;\r | |
742 | \r | |
743 | Size = 0;\r | |
b1f95a06 | 744 | \r |
745 | //\r | |
746 | // Check our parameters\r | |
747 | //\r | |
748 | if (FullFileName == NULL || Handle == NULL) {\r | |
749 | return (EFI_INVALID_PARAMETER);\r | |
750 | }\r | |
751 | \r | |
d595d4b1 | 752 | *FullFileName = NULL;\r |
753 | \r | |
b1f95a06 | 754 | Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);\r |
755 | if (!EFI_ERROR(Status)) {\r | |
756 | //\r | |
757 | // Reverse out the current directory on the device\r | |
758 | //\r | |
759 | for (;;) {\r | |
760 | FileInfo = FileHandleGetInfo(CurrentHandle);\r | |
761 | if (FileInfo == NULL) {\r | |
762 | Status = EFI_OUT_OF_RESOURCES;\r | |
763 | break;\r | |
764 | } else {\r | |
765 | //\r | |
766 | // We got info... do we have a name? if yes preceed the current path with it...\r | |
767 | //\r | |
768 | if (StrLen (FileInfo->FileName) == 0) {\r | |
d595d4b1 | 769 | *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"/", 0);\r |
b1f95a06 | 770 | FreePool(FileInfo);\r |
771 | break;\r | |
772 | } else {\r | |
773 | *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);\r | |
d595d4b1 | 774 | *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"/", 0);\r |
b1f95a06 | 775 | FreePool(FileInfo);\r |
776 | }\r | |
777 | }\r | |
778 | //\r | |
779 | // Move to the parent directory\r | |
780 | //\r | |
781 | Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);\r | |
782 | if (EFI_ERROR (Status)) {\r | |
783 | break;\r | |
784 | }\r | |
785 | \r | |
786 | FileHandleClose(CurrentHandle);\r | |
787 | CurrentHandle = NextHigherHandle;\r | |
788 | }\r | |
789 | }\r | |
790 | \r | |
791 | if (CurrentHandle != NULL) {\r | |
792 | CurrentHandle->Close (CurrentHandle);\r | |
793 | }\r | |
794 | \r | |
795 | if (EFI_ERROR(Status) && *FullFileName != NULL) {\r | |
9eb53ac3 | 796 | FreePool(*FullFileName);\r |
b1f95a06 | 797 | }\r |
798 | \r | |
799 | return (Status);\r | |
800 | }\r | |
801 | \r | |
802 | /**\r | |
803 | Function to read a single line (up to but not including the \n) from a file.\r | |
804 | \r | |
805 | @param[in] Handle FileHandle to read from\r | |
9eb53ac3 | 806 | @param[in,out] Buffer pointer to buffer to read into\r |
807 | @param[in,out] Size pointer to number of bytes in buffer\r | |
b1f95a06 | 808 | @param[in[ Truncate if TRUE then allows for truncation of the line to fit.\r |
809 | if FALSE will reset the position to the begining of the \r | |
810 | line if the buffer is not large enough.\r | |
811 | \r | |
812 | @retval EFI_SUCCESS the operation was sucessful. the line is stored in \r | |
813 | Buffer.\r | |
814 | @retval EFI_INVALID_PARAMETER Handle was NULL.\r | |
815 | @retval EFI_INVALID_PARAMETER Buffer was NULL.\r | |
816 | @retval EFI_INVALID_PARAMETER Size was NULL.\r | |
817 | @retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line. \r | |
818 | Size was updated to minimum space required.\r | |
819 | @sa FileHandleRead\r | |
820 | **/\r | |
821 | EFI_STATUS\r | |
822 | EFIAPI\r | |
823 | FileHandleReadLine(\r | |
824 | IN EFI_FILE_HANDLE Handle,\r | |
825 | IN OUT VOID *Buffer,\r | |
826 | IN OUT UINTN *Size,\r | |
827 | IN BOOLEAN Truncate\r | |
828 | ){\r | |
829 | EFI_STATUS Status;\r | |
830 | CHAR16 CharBuffer;\r | |
831 | UINTN CharSize;\r | |
832 | UINTN CountSoFar;\r | |
833 | UINT64 Position;\r | |
834 | \r | |
835 | \r | |
836 | if (Handle == NULL\r | |
837 | ||Buffer == NULL\r | |
838 | ||Size == NULL\r | |
839 | ){\r | |
840 | return (EFI_INVALID_PARAMETER);\r | |
841 | }\r | |
842 | FileHandleGetPosition(Handle, &Position);\r | |
843 | \r | |
844 | for (CountSoFar = 0;;CountSoFar++){\r | |
845 | CharSize = sizeof(CharBuffer);\r | |
846 | Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r | |
847 | if ( EFI_ERROR(Status) \r | |
848 | || CharSize == 0 \r | |
849 | || CharBuffer == '\n'\r | |
850 | ){\r | |
851 | break;\r | |
852 | }\r | |
853 | //\r | |
854 | // if we have space save it...\r | |
855 | //\r | |
856 | if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r | |
857 | ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r | |
858 | ((CHAR16*)Buffer)[CountSoFar+1] = '\0';\r | |
859 | }\r | |
860 | }\r | |
861 | \r | |
862 | //\r | |
863 | // if we ran out of space tell when...\r | |
864 | //\r | |
865 | if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r | |
866 | *Size = (CountSoFar+1)*sizeof(CHAR16);\r | |
867 | if (Truncate == FALSE) {\r | |
868 | FileHandleSetPosition(Handle, Position);\r | |
869 | } else {\r | |
870 | DEBUG((DEBUG_WARN, "The line was truncated in ReadLine"));\r | |
871 | }\r | |
872 | return (EFI_BUFFER_TOO_SMALL);\r | |
873 | }\r | |
874 | *Size = (CountSoFar+1)*sizeof(CHAR16);\r | |
875 | return (Status);\r | |
876 | }\r | |
877 | \r | |
878 | /**\r | |
879 | function to write a line of unicode text to a file.\r | |
880 | \r | |
881 | if Handle is NULL, ASSERT.\r | |
882 | if Buffer is NULL, do nothing. (return SUCCESS)\r | |
883 | \r | |
884 | @param[in] Handle FileHandle to write to\r | |
885 | @param[in] Buffer Buffer to write\r | |
886 | \r | |
887 | @retval EFI_SUCCESS the data was written.\r | |
888 | @retval other failure.\r | |
889 | \r | |
890 | @sa FileHandleWrite\r | |
891 | **/\r | |
892 | EFI_STATUS\r | |
893 | EFIAPI\r | |
894 | FileHandleWriteLine(\r | |
895 | IN EFI_FILE_HANDLE Handle,\r | |
896 | IN CHAR16 *Buffer\r | |
897 | ){\r | |
898 | EFI_STATUS Status;\r | |
899 | UINTN Size;\r | |
900 | \r | |
901 | ASSERT(Handle != NULL);\r | |
902 | \r | |
903 | if (Buffer == NULL) {\r | |
904 | return (EFI_SUCCESS);\r | |
905 | }\r | |
906 | \r | |
907 | Size = StrLen(Buffer);\r | |
908 | Status = FileHandleWrite(Handle, &Size, Buffer);\r | |
909 | if (EFI_ERROR(Status)) {\r | |
910 | return (Status);\r | |
911 | }\r | |
912 | Size = StrLen(L"\r\n");\r | |
913 | return FileHandleWrite(Handle, &Size, L"\r\n");\r | |
914 | }\r |