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