]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
Fix several Unicode issues that fails VS2005.
[mirror_edk2.git] / ShellPkg / Library / BaseFileHandleLib / BaseFileHandleLib.c
CommitLineData
d2b4564b 1/** @file\r
2 Provides interface to EFI_FILE_HANDLE functionality.\r
3\r
4Copyright (c) 2006 - 2009, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Uefi.h>\r
16\r
17#include <Library/ShellLib.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20\r
21#include <Protocol/SimpleFileSystem.h>\r
22\r
23#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)\r
24#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
25\r
26/**\r
27 This function will retrieve the information about the file for the handle \r
28 specified and store it in allocated pool memory.\r
29\r
69817bf8 30 This function allocates a buffer to store the file's information. It is the \r
31 caller's responsibility to free the buffer\r
d2b4564b 32\r
33 @param FileHandle The file handle of the file for which information is \r
34 being requested.\r
35\r
36 @retval NULL information could not be retrieved.\r
37\r
38 @return the information about the file\r
39**/\r
40EFI_FILE_INFO*\r
41EFIAPI\r
42FileHandleGetInfo (\r
43 IN EFI_FILE_HANDLE FileHandle\r
44 )\r
45{\r
46 EFI_GUID FileInfoGuid;\r
47 EFI_FILE_INFO *pFileInfo;\r
48 UINTN FileInfoSize;\r
49 EFI_STATUS Status;\r
50\r
51 //\r
52 // ASSERT if FileHandle is NULL\r
53 //\r
54 ASSERT (FileHandle != NULL);\r
55\r
56 //\r
57 // Get the required size to allocate\r
58 //\r
59 FileInfoGuid = gEfiFileInfoGuid;\r
60 FileInfoSize = 0;\r
61 pFileInfo = NULL;\r
62 Status = FileHandle->GetInfo(FileHandle, \r
63 &FileInfoGuid, \r
64 &FileInfoSize, \r
65 pFileInfo);\r
66 //\r
67 // error is expected. getting size to allocate\r
68 //\r
69 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
70 pFileInfo = AllocateZeroPool(FileInfoSize);\r
71 ASSERT (pFileInfo != NULL);\r
72 //\r
73 // now get the information\r
74 //\r
75 Status = FileHandle->GetInfo(FileHandle, \r
76 &FileInfoGuid, \r
77 &FileInfoSize, \r
78 pFileInfo);\r
79 //\r
80 // if we got an error free the memory and return NULL\r
81 //\r
82 if (EFI_ERROR(Status)) {\r
83 FreePool(pFileInfo);\r
84 return NULL;\r
85 }\r
86 return (pFileInfo);\r
87}\r
88\r
89/**\r
90 This function will set the information about the file for the opened handle \r
91 specified.\r
92\r
93 @param FileHandle The file handle of the file for which information \r
94 is being set\r
95\r
96 @param FileInfo The infotmation to set.\r
97\r
98 @retval EFI_SUCCESS The information was set.\r
99 @retval EFI_UNSUPPORTED The InformationType is not known.\r
100 @retval EFI_NO_MEDIA The device has no medium.\r
101 @retval EFI_DEVICE_ERROR The device reported an error.\r
102 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
103 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
104 @retval EFI_ACCESS_DENIED The file was opened read only.\r
105 @retval EFI_VOLUME_FULL The volume is full.\r
106**/\r
107EFI_STATUS\r
108EFIAPI\r
109FileHandleSetInfo (\r
110 IN EFI_FILE_HANDLE FileHandle,\r
111 IN CONST EFI_FILE_INFO *FileInfo\r
112 )\r
113{\r
114 EFI_GUID FileInfoGuid;\r
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
122 FileInfoGuid = gEfiFileInfoGuid;\r
123 //\r
124 // Set the info\r
125 //\r
126 return (FileHandle->SetInfo(FileHandle, \r
127 &FileInfoGuid,\r
128 (UINTN)FileInfo->Size,\r
129 (EFI_FILE_INFO*)FileInfo));\r
130} \r
131\r
132/**\r
133 This function reads information from an opened file.\r
134\r
135 If FileHandle is not a directory, the function reads the requested number of \r
69817bf8 136 bytes from the file at the file's current position and returns them in Buffer. \r
d2b4564b 137 If the read goes beyond the end of the file, the read length is truncated to the\r
69817bf8 138 end of the file. The file's current position is increased by the number of bytes \r
d2b4564b 139 returned. If FileHandle is a directory, the function reads the directory entry \r
69817bf8 140 at the file's current position and returns the entry in Buffer. If the Buffer \r
d2b4564b 141 is not large enough to hold the current directory entry, then \r
142 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated. \r
143 BufferSize is set to be the size of the buffer needed to read the entry. On \r
144 success, the current position is updated to the next directory entry. If there \r
145 are no more directory entries, the read returns a zero-length buffer. \r
146 EFI_FILE_INFO is the structure returned as the directory entry.\r
147\r
148 @param FileHandle the opened file handle\r
149 @param BufferSize on input the size of buffer in bytes. on return \r
150 the number of bytes written.\r
151 @param Buffer the buffer to put read data into.\r
152\r
153 @retval EFI_SUCCESS Data was read.\r
154 @retval EFI_NO_MEDIA The device has no media.\r
155 @retval EFI_DEVICE_ERROR The device reported an error.\r
156 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
157 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required \r
158 size.\r
159\r
160**/\r
161EFI_STATUS\r
162EFIAPI\r
163FileHandleRead(\r
164 IN EFI_FILE_HANDLE FileHandle,\r
165 IN OUT UINTN *BufferSize,\r
166 OUT VOID *Buffer\r
167 )\r
168{\r
169 //\r
170 // ASSERT if FileHandle is NULL\r
171 //\r
172 ASSERT (FileHandle != NULL);\r
173\r
174 //\r
175 // Perform the read based on EFI_FILE_PROTOCOL\r
176 //\r
177 return (FileHandle->Read(FileHandle, BufferSize, Buffer));\r
178}\r
179\r
180\r
181/**\r
182 Write data to a file.\r
183\r
184 This function writes the specified number of bytes to the file at the current \r
185 file position. The current file position is advanced the actual number of bytes \r
186 written, which is returned in BufferSize. Partial writes only occur when there \r
69817bf8 187 has been a data error during the write attempt (such as "volume space full"). \r
d2b4564b 188 The file is automatically grown to hold the data if required. Direct writes to \r
189 opened directories are not supported.\r
190\r
191 @param FileHandle The opened file for writing\r
192 @param BufferSize on input the number of bytes in Buffer. On output\r
193 the number of bytes written.\r
194 @param Buffer the buffer containing data to write is stored.\r
195\r
196 @retval EFI_SUCCESS Data was written.\r
197 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.\r
198 @retval EFI_NO_MEDIA The device has no media.\r
199 @retval EFI_DEVICE_ERROR The device reported an error.\r
200 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
201 @retval EFI_WRITE_PROTECTED The device is write-protected.\r
202 @retval EFI_ACCESS_DENIED The file was open for read only.\r
203 @retval EFI_VOLUME_FULL The volume is full.\r
204**/\r
205EFI_STATUS\r
206EFIAPI\r
207FileHandleWrite(\r
208 IN EFI_FILE_HANDLE FileHandle,\r
209 IN OUT UINTN *BufferSize,\r
210 IN VOID *Buffer\r
211 )\r
212{\r
213 //\r
214 // ASSERT if FileHandle is NULL\r
215 //\r
216 ASSERT (FileHandle != NULL);\r
217 //\r
218 // Perform the write based on EFI_FILE_PROTOCOL\r
219 //\r
220 return (FileHandle->Write(FileHandle, BufferSize, Buffer));\r
221}\r
222\r
223/** \r
224 Close an open file handle.\r
225\r
69817bf8 226 This function closes a specified file handle. All "dirty" cached file data is \r
d2b4564b 227 flushed to the device, and the file is closed. In all cases the handle is \r
228 closed.\r
229\r
230@param FileHandle the file handle to close.\r
231\r
232@retval EFI_SUCCESS the file handle was closed sucessfully.\r
233**/\r
234EFI_STATUS\r
235EFIAPI\r
236FileHandleClose (\r
237 IN EFI_FILE_HANDLE FileHandle\r
238 )\r
239{\r
240 EFI_STATUS Status;\r
241 //\r
242 // ASSERT if FileHandle is NULL\r
243 //\r
244 ASSERT (FileHandle != NULL);\r
245 //\r
246 // Perform the Close based on EFI_FILE_PROTOCOL\r
247 //\r
248 Status = FileHandle->Close(FileHandle);\r
249 return Status;\r
250}\r
251\r
252/**\r
253 Delete a file and close the handle\r
254\r
255 This function closes and deletes a file. In all cases the file handle is closed.\r
256 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is \r
257 returned, but the handle is still closed.\r
258\r
259 @param FileHandle the file handle to delete\r
260\r
261 @retval EFI_SUCCESS the file was closed sucessfully\r
262 @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not \r
263 deleted\r
264 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
265**/\r
266EFI_STATUS\r
267EFIAPI\r
268FileHandleDelete (\r
269 IN EFI_FILE_HANDLE FileHandle\r
270 )\r
271{\r
272 EFI_STATUS Status;\r
273 //\r
274 // ASSERT if FileHandle is NULL\r
275 //\r
276 ASSERT (FileHandle != NULL);\r
277 //\r
278 // Perform the Delete based on EFI_FILE_PROTOCOL\r
279 //\r
280 Status = FileHandle->Delete(FileHandle);\r
281 return Status;\r
282}\r
283\r
284/**\r
285 Set the current position in a file.\r
286\r
287 This function sets the current file position for the handle to the position \r
288 supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r
289 absolute positioning is supported, and seeking past the end of the file is \r
290 allowed (a subsequent write would grow the file). Seeking to position \r
291 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r
292 If FileHandle is a directory, the only position that may be set is zero. This \r
293 has the effect of starting the read process of the directory entries over.\r
294\r
295 @param FileHandle The file handle on which the position is being set\r
296 @param Position Byte position from begining of file\r
297\r
298 @retval EFI_SUCCESS Operation completed sucessfully.\r
299 @retval EFI_UNSUPPORTED the seek request for non-zero is not valid on \r
300 directories.\r
301 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
302**/\r
303EFI_STATUS\r
304EFIAPI\r
305FileHandleSetPosition (\r
306 IN EFI_FILE_HANDLE FileHandle,\r
307 IN UINT64 Position\r
308 )\r
309{\r
310 //\r
311 // ASSERT if FileHandle is NULL\r
312 //\r
313 ASSERT (FileHandle != NULL);\r
314 //\r
315 // Perform the SetPosition based on EFI_FILE_PROTOCOL\r
316 //\r
317 return (FileHandle->SetPosition(FileHandle, Position));\r
318}\r
319\r
320/** \r
321 Gets a file's current position\r
322\r
323 This function retrieves the current file position for the file handle. For \r
324 directories, the current file position has no meaning outside of the file \r
325 system driver and as such the operation is not supported. An error is returned\r
326 if FileHandle is a directory.\r
327\r
328 @param FileHandle The open file handle on which to get the position.\r
329 @param Position Byte position from begining of file.\r
330\r
331 @retval EFI_SUCCESS the operation completed sucessfully.\r
332 @retval INVALID_PARAMETER One of the parameters has an invalid value.\r
333 @retval EFI_UNSUPPORTED the request is not valid on directories.\r
334**/\r
335EFI_STATUS\r
336EFIAPI\r
337FileHandleGetPosition (\r
338 IN EFI_FILE_HANDLE FileHandle,\r
339 OUT UINT64 *Position\r
340 )\r
341{\r
342 //\r
343 // ASSERT if FileHandle is NULL\r
344 //\r
345 ASSERT (FileHandle != NULL);\r
346 //\r
347 // Perform the GetPosition based on EFI_FILE_PROTOCOL\r
348 //\r
349 return (FileHandle->GetPosition(FileHandle, Position));\r
350}\r
351/**\r
352 Flushes data on a file\r
353 \r
354 This function flushes all modified data associated with a file to a device.\r
355\r
356 @param FileHandle The file handle on which to flush data\r
357\r
358 @retval EFI_SUCCESS The data was flushed.\r
359 @retval EFI_NO_MEDIA The device has no media.\r
360 @retval EFI_DEVICE_ERROR The device reported an error.\r
361 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
362 @retval EFI_WRITE_PROTECTED The file or medium is write protected.\r
363 @retval EFI_ACCESS_DENIED The file was opened for read only.\r
364**/\r
365EFI_STATUS\r
366EFIAPI\r
367FileHandleFlush (\r
368 IN EFI_FILE_HANDLE FileHandle\r
369 )\r
370{\r
371 //\r
372 // ASSERT if FileHandle is NULL\r
373 //\r
374 ASSERT (FileHandle != NULL);\r
375 //\r
376 // Perform the Flush based on EFI_FILE_PROTOCOL\r
377 //\r
378 return (FileHandle->Flush(FileHandle));\r
379}\r
380\r
381/**\r
382 function to determine if a given handle is a directory handle\r
383\r
384 if DirHandle is NULL then ASSERT()\r
385\r
386 open the file information on the DirHandle and verify that the Attribute\r
387 includes EFI_FILE_DIRECTORY bit set.\r
388\r
389 @param DirHandle Handle to open file\r
390\r
391 @retval EFI_SUCCESS DirHandle is a directory\r
392 @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available\r
393 @retval EFI_NOT_FOUND DirHandle is not a directory\r
394**/\r
395EFI_STATUS\r
396EFIAPI\r
397FileHandleIsDirectory (\r
398 IN EFI_FILE_HANDLE DirHandle\r
399 )\r
400{\r
401 EFI_FILE_INFO *DirInfo;\r
402\r
403 //\r
404 // ASSERT if DirHandle is NULL\r
405 //\r
406 ASSERT(DirHandle != NULL);\r
407 \r
408 //\r
409 // get the file information for DirHandle\r
410 //\r
411 DirInfo = FileHandleGetInfo (DirHandle);\r
412 \r
413 //\r
414 // Parse DirInfo\r
415 //\r
416 if (DirInfo == NULL) {\r
417 //\r
418 // We got nothing...\r
419 //\r
420 return (EFI_INVALID_PARAMETER);\r
421 } \r
422 if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r
423 //\r
424 // Attributes say this is not a directory\r
425 //\r
426 FreePool (DirInfo);\r
427 return (EFI_NOT_FOUND);\r
428 }\r
429 //\r
430 // all good...\r
431 //\r
432 FreePool (DirInfo);\r
433 return (EFI_SUCCESS);\r
434}\r
435\r
436/**\r
437 Retrieves the first file from a directory\r
438\r
69817bf8 439 This function opens a directory and gets the first file's info in the \r
d2b4564b 440 directory. Caller can use FileHandleFindNextFile() to get other files. When \r
441 complete the caller is responsible for calling FreePool() on Buffer.\r
442\r
443 @param DirHandle The file handle of the directory to search\r
444 @param Buffer Pointer to buffer for file's information\r
445\r
446 @retval EFI_SUCCESS Found the first file.\r
447 @retval EFI_NOT_FOUND Cannot find the directory.\r
448 @retval EFI_NO_MEDIA The device has no media.\r
449 @retval EFI_DEVICE_ERROR The device reported an error.\r
450 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
451 @return Others status of FileHandleGetInfo, FileHandleSetPosition,\r
452 or FileHandleRead\r
453**/\r
454EFI_STATUS\r
455EFIAPI\r
456FileHandleFindFirstFile (\r
457 IN EFI_FILE_HANDLE DirHandle,\r
458 OUT EFI_FILE_INFO **Buffer\r
459 )\r
460{\r
461 EFI_STATUS Status;\r
462 UINTN BufferSize;\r
463\r
464 //\r
465 // ASSERTs\r
466 //\r
467 ASSERT (DirHandle != NULL);\r
468 ASSERT (Buffer != NULL);\r
469\r
470 //\r
471 // verify that DirHandle is a directory\r
472 //\r
473 Status = FileHandleIsDirectory(DirHandle);\r
474 if (EFI_ERROR(Status)) {\r
475 return (Status);\r
476 } \r
477\r
478 //\r
479 // reset to the begining of the directory \r
480 //\r
481 Status = FileHandleSetPosition(DirHandle, 0);\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 ASSERT (*Buffer != NULL);\r
492\r
493 //\r
494 // read in the info about the first file\r
495 //\r
496 Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);\r
497 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
498 if (EFI_ERROR(Status)) {\r
499 FreePool(*Buffer);\r
500 *Buffer = NULL;\r
501 return (Status);\r
502 }\r
503 return (EFI_SUCCESS);\r
504}\r
505/**\r
506 Retrieves the next file in a directory.\r
507\r
508 To use this function, caller must call the FileHandleFindFirstFile() to get the \r
509 first file, and then use this function get other files. This function can be \r
510 called for several times to get each file's information in the directory. If \r
511 the call of FileHandleFindNextFile() got the last file in the directory, the next \r
512 call of this function has no file to get. *NoFile will be set to TRUE and the \r
513 Buffer memory will be automatically freed. \r
514\r
515 @param DirHandle the file handle of the directory\r
516 @param Buffer pointer to buffer for file's information\r
517 @param NoFile pointer to boolean when last file is found\r
518\r
519 @retval EFI_SUCCESS Found the next file, or reached last file\r
520 @retval EFI_NO_MEDIA The device has no media.\r
521 @retval EFI_DEVICE_ERROR The device reported an error.\r
522 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
523**/\r
524EFI_STATUS\r
525EFIAPI\r
526FileHandleFindNextFile(\r
527 IN EFI_FILE_HANDLE DirHandle,\r
528 OUT EFI_FILE_INFO *Buffer,\r
529 OUT BOOLEAN *NoFile\r
530 )\r
531{\r
532 EFI_STATUS Status;\r
533 UINTN BufferSize;\r
534\r
535 //\r
536 // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL\r
537 //\r
538 ASSERT (DirHandle != NULL);\r
539 ASSERT (Buffer != NULL);\r
540 ASSERT (NoFile != NULL);\r
541 \r
542 //\r
543 // verify that DirHandle is a directory\r
544 //\r
545 Status = FileHandleIsDirectory(DirHandle);\r
546 if (EFI_ERROR(Status)) {\r
547 return (Status);\r
548 } \r
549\r
550 //\r
551 // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile\r
552 //\r
553 BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;\r
554\r
555 //\r
556 // read in the info about the next file\r
557 //\r
558 Status = FileHandleRead (DirHandle, &BufferSize, Buffer);\r
559 ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
560 if (EFI_ERROR(Status)) {\r
561 return (Status);\r
562 }\r
563\r
564 //\r
565 // If we read 0 bytes (but did not have erros) we already read in the last file.\r
566 //\r
567 if (BufferSize == 0) {\r
568 FreePool(Buffer);\r
569 *NoFile = TRUE;\r
570 }\r
571\r
572 return (EFI_SUCCESS);\r
573}\r
574/**\r
575 Retrieve the size of a file.\r
576\r
577 if FileHandle is NULL then ASSERT()\r
578 if Size is NULL then ASSERT()\r
579\r
69817bf8 580 This function extracts the file size info from the FileHandle's EFI_FILE_INFO \r
d2b4564b 581 data.\r
582\r
583 @param FileHandle file handle from which size is retrieved\r
584 @param Size pointer to size\r
585\r
586 @retval EFI_SUCCESS operation was completed sucessfully\r
587 @retval EFI_DEVICE_ERROR cannot access the file\r
588**/\r
589EFI_STATUS\r
590EFIAPI\r
591FileHandleGetSize (\r
592 IN EFI_FILE_HANDLE FileHandle,\r
593 OUT UINT64 *Size\r
594 )\r
595{\r
596 EFI_FILE_INFO *FileInfo;\r
597\r
598 //\r
599 // ASSERT for FileHandle or Size being NULL\r
600 //\r
601 ASSERT (FileHandle != NULL);\r
602 ASSERT (Size != NULL);\r
603 \r
604 //\r
605 // get the FileInfo structure\r
606 //\r
607 FileInfo = FileHandleGetInfo(FileHandle);\r
608 if (FileInfo == NULL) {\r
609 return (EFI_DEVICE_ERROR);\r
610 }\r
611\r
612 //\r
613 // Assign the Size pointer to the correct value\r
614 //\r
615 *Size = FileInfo->FileSize;\r
616 \r
617 //\r
618 // free the FileInfo memory\r
619 //\r
620 FreePool(FileInfo);\r
621\r
622 return (EFI_SUCCESS);\r
623}