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