2 Functions that perform file read/write.
4 Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
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.
20 Get the file's position of the file.
23 @param FHand - The handle of file.
24 @param Position - The file's position of the file.
26 @retval EFI_SUCCESS - Get the info successfully.
27 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.
28 @retval EFI_UNSUPPORTED - The open file is not a file.
34 IN EFI_FILE_PROTOCOL
*FHand
,
41 IFile
= IFILE_FROM_FHAND (FHand
);
44 if (OFile
->Error
== EFI_NOT_FOUND
) {
45 return EFI_DEVICE_ERROR
;
48 if (OFile
->ODir
!= NULL
) {
49 return EFI_UNSUPPORTED
;
52 *Position
= IFile
->Position
;
58 Set the file's position of the file.
60 @param FHand - The handle of file.
61 @param Position - The file's position of the file.
63 @retval EFI_SUCCESS - Set the info successfully.
64 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.
65 @retval EFI_UNSUPPORTED - Set a directory with a not-zero position.
71 IN EFI_FILE_PROTOCOL
*FHand
,
78 IFile
= IFILE_FROM_FHAND (FHand
);
81 if (OFile
->Error
== EFI_NOT_FOUND
) {
82 return EFI_DEVICE_ERROR
;
85 FatWaitNonblockingTask (IFile
);
88 // If this is a directory, we can only set back to position 0
90 if (OFile
->ODir
!= NULL
) {
93 // Reset current directory cursor;
95 return EFI_UNSUPPORTED
;
98 FatResetODirCursor (OFile
);
103 if (Position
== (UINT64
)-1) {
104 Position
= OFile
->FileSize
;
109 IFile
->Position
= Position
;
115 Get the file info from the open file of the IFile into Buffer.
117 @param IFile - The instance of the open file.
118 @param BufferSize - Size of Buffer.
119 @param Buffer - Buffer containing read data.
121 @retval EFI_SUCCESS - Get the file info successfully.
122 @retval other - An error occurred when operation the disk.
128 IN OUT UINTN
*BufferSize
,
138 OFile
= IFile
->OFile
;
140 CurrentPos
= ((UINT32
) IFile
->Position
) / sizeof (FAT_DIRECTORY_ENTRY
);
143 // We need to relocate the directory
145 if (CurrentPos
< ODir
->CurrentPos
) {
147 // The directory cursor has been modified by another IFile, we reset the cursor
149 FatResetODirCursor (OFile
);
152 // We seek the next directory entry's position
155 Status
= FatGetNextDirEnt (OFile
, &DirEnt
);
156 if (EFI_ERROR (Status
) || DirEnt
== NULL
) {
158 // Something error occurred or reach the end of directory,
159 // return 0 buffersize
164 } while (ODir
->CurrentPos
<= CurrentPos
);
165 Status
= FatGetDirEntInfo (OFile
->Volume
, DirEnt
, BufferSize
, Buffer
);
169 // Update IFile's Position
171 if (!EFI_ERROR (Status
)) {
173 // Update IFile->Position, if everything is all right
175 CurrentPos
= ODir
->CurrentPos
;
176 IFile
->Position
= CurrentPos
* sizeof (FAT_DIRECTORY_ENTRY
);
184 Get the file info from the open file of the IFile into Buffer.
186 @param FHand - The file handle to access.
187 @param IoMode - Indicate whether the access mode is reading or writing.
188 @param BufferSize - Size of Buffer.
189 @param Buffer - Buffer containing read data.
190 @param Token - A pointer to the token associated with the transaction.
192 @retval EFI_SUCCESS - Get the file info successfully.
193 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.
194 @retval EFI_VOLUME_CORRUPTED - The file type of open file is error.
195 @retval EFI_WRITE_PROTECTED - The disk is write protect.
196 @retval EFI_ACCESS_DENIED - The file is read-only.
197 @return other - An error occurred when operating on the disk.
202 IN EFI_FILE_PROTOCOL
*FHand
,
204 IN OUT UINTN
*BufferSize
,
206 IN EFI_FILE_IO_TOKEN
*Token
216 IFile
= IFILE_FROM_FHAND (FHand
);
217 OFile
= IFile
->OFile
;
218 Volume
= OFile
->Volume
;
222 // Write to a directory is unsupported
224 if ((OFile
->ODir
!= NULL
) && (IoMode
== WriteData
)) {
225 return EFI_UNSUPPORTED
;
228 if (OFile
->Error
== EFI_NOT_FOUND
) {
229 return EFI_DEVICE_ERROR
;
232 if (IoMode
== ReadData
) {
234 // If position is at EOF, then return device error
236 if (IFile
->Position
> OFile
->FileSize
) {
237 return EFI_DEVICE_ERROR
;
241 // Check if the we can write data
243 if (Volume
->ReadOnly
) {
244 return EFI_WRITE_PROTECTED
;
247 if (IFile
->ReadOnly
) {
248 return EFI_ACCESS_DENIED
;
253 FatWaitNonblockingTask (IFile
);
256 // Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.
257 // But if it calls, the below check can avoid crash.
259 if (FHand
->Revision
< EFI_FILE_PROTOCOL_REVISION2
) {
260 return EFI_UNSUPPORTED
;
262 Task
= FatCreateTask (IFile
, Token
);
264 return EFI_OUT_OF_RESOURCES
;
270 Status
= OFile
->Error
;
271 if (!EFI_ERROR (Status
)) {
272 if (OFile
->ODir
!= NULL
) {
274 // Read a directory is supported
276 ASSERT (IoMode
== ReadData
);
277 Status
= FatIFileReadDir (IFile
, BufferSize
, Buffer
);
283 EndPosition
= IFile
->Position
+ *BufferSize
;
284 if (EndPosition
> OFile
->FileSize
) {
286 // The position goes beyond the end of file
288 if (IoMode
== ReadData
) {
290 // Adjust the actual size read
292 *BufferSize
-= (UINTN
) EndPosition
- OFile
->FileSize
;
295 // We expand the file size of OFile
297 Status
= FatGrowEof (OFile
, EndPosition
);
298 if (EFI_ERROR (Status
)) {
300 // Must update the file's info into the file's Directory Entry
301 // and then flush the dirty cache info into disk.
304 FatOFileFlush (OFile
);
309 FatUpdateDirEntClusterSizeInfo (OFile
);
313 Status
= FatAccessOFile (OFile
, IoMode
, (UINTN
) IFile
->Position
, BufferSize
, Buffer
, Task
);
314 IFile
->Position
+= *BufferSize
;
319 if (!EFI_ERROR (Status
)) {
320 Status
= FatQueueTask (IFile
, Task
);
322 FatDestroyTask (Task
);
328 // On EFI_SUCCESS case, not calling FatCleanupVolume():
329 // 1) The Cache flush operation is avoided to enhance
330 // performance. Caller is responsible to call Flush() when necessary.
331 // 2) The volume dirty bit is probably set already, and is expected to be
332 // cleaned in subsequent Flush() or other operations.
333 // 3) Write operation doesn't affect OFile/IFile structure, so
334 // Reference checking is not necessary.
336 if (EFI_ERROR (Status
)) {
337 Status
= FatCleanupVolume (Volume
, OFile
, Status
, NULL
);
348 @param FHand - The handle of the file.
349 @param BufferSize - Size of Buffer.
350 @param Buffer - Buffer containing read data.
353 @retval EFI_SUCCESS - Get the file info successfully.
354 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.
355 @retval EFI_VOLUME_CORRUPTED - The file type of open file is error.
356 @return other - An error occurred when operation the disk.
362 IN EFI_FILE_PROTOCOL
*FHand
,
363 IN OUT UINTN
*BufferSize
,
367 return FatIFileAccess (FHand
, ReadData
, BufferSize
, Buffer
, NULL
);
374 @param FHand - The handle of the file.
375 @param Token - A pointer to the token associated with the transaction.
377 @retval EFI_SUCCESS - Get the file info successfully.
378 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.
379 @retval EFI_VOLUME_CORRUPTED - The file type of open file is error.
380 @return other - An error occurred when operation the disk.
386 IN EFI_FILE_PROTOCOL
*FHand
,
387 IN OUT EFI_FILE_IO_TOKEN
*Token
390 return FatIFileAccess (FHand
, ReadData
, &Token
->BufferSize
, Token
->Buffer
, Token
);
395 Write the content of buffer into files.
397 @param FHand - The handle of the file.
398 @param BufferSize - Size of Buffer.
399 @param Buffer - Buffer containing write data.
401 @retval EFI_SUCCESS - Set the file info successfully.
402 @retval EFI_WRITE_PROTECTED - The disk is write protect.
403 @retval EFI_ACCESS_DENIED - The file is read-only.
404 @retval EFI_DEVICE_ERROR - The OFile is not valid.
405 @retval EFI_UNSUPPORTED - The open file is not a file.
406 - The writing file size is larger than 4GB.
407 @return other - An error occurred when operation the disk.
413 IN EFI_FILE_PROTOCOL
*FHand
,
414 IN OUT UINTN
*BufferSize
,
418 return FatIFileAccess (FHand
, WriteData
, BufferSize
, Buffer
, NULL
);
425 @param FHand - The handle of the file.
426 @param Token - A pointer to the token associated with the transaction.
428 @retval EFI_SUCCESS - Get the file info successfully.
429 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.
430 @retval EFI_VOLUME_CORRUPTED - The file type of open file is error.
431 @return other - An error occurred when operation the disk.
437 IN EFI_FILE_PROTOCOL
*FHand
,
438 IN OUT EFI_FILE_IO_TOKEN
*Token
441 return FatIFileAccess (FHand
, WriteData
, &Token
->BufferSize
, Token
->Buffer
, Token
);
446 This function reads data from a file or writes data to a file.
447 It uses OFile->PosRem to determine how much data can be accessed in one time.
449 @param OFile - The open file.
450 @param IoMode - Indicate whether the access mode is reading or writing.
451 @param Position - The position where data will be accessed.
452 @param DataBufferSize - Size of Buffer.
453 @param UserBuffer - Buffer containing data.
454 @param Task point to task instance.
456 @retval EFI_SUCCESS - Access the data successfully.
457 @return other - An error occurred when operating on the disk.
465 IN OUT UINTN
*DataBufferSize
,
466 IN OUT UINT8
*UserBuffer
,
475 BufferSize
= *DataBufferSize
;
476 Volume
= OFile
->Volume
;
477 ASSERT_VOLUME_LOCKED (Volume
);
479 Status
= EFI_SUCCESS
;
480 while (BufferSize
> 0) {
482 // Seek the OFile to the file position
484 Status
= FatOFilePosition (OFile
, Position
, BufferSize
);
485 if (EFI_ERROR (Status
)) {
489 // Clip length to block run
491 Len
= BufferSize
> OFile
->PosRem
? OFile
->PosRem
: BufferSize
;
496 Status
= FatDiskIo (Volume
, IoMode
, OFile
->PosDisk
, Len
, UserBuffer
, Task
);
497 if (EFI_ERROR (Status
)) {
501 // Data was successfully accessed
506 if (IoMode
== WriteData
) {
508 OFile
->Archive
= TRUE
;
511 // Make sure no outbound occurred
513 ASSERT (Position
<= OFile
->FileSize
);
516 // Update the number of bytes accessed
518 *DataBufferSize
-= BufferSize
;
524 Expand OFile by appending zero bytes at the end of OFile.
526 @param OFile - The open file.
527 @param ExpandedSize - The number of zero bytes appended at the end of the file.
529 @retval EFI_SUCCESS - The file is expanded successfully.
530 @return other - An error occurred when expanding file.
536 IN UINT64 ExpandedSize
542 WritePos
= OFile
->FileSize
;
543 Status
= FatGrowEof (OFile
, ExpandedSize
);
544 if (!EFI_ERROR (Status
)) {
545 Status
= FatWriteZeroPool (OFile
, WritePos
);
553 Write zero pool from the WritePos to the end of OFile.
555 @param OFile - The open file to write zero pool.
556 @param WritePos - The number of zero bytes written.
558 @retval EFI_SUCCESS - Write the zero pool successfully.
559 @retval EFI_OUT_OF_RESOURCES - Not enough memory to perform the operation.
560 @return other - An error occurred when writing disk.
575 AppendedSize
= OFile
->FileSize
- WritePos
;
576 BufferSize
= AppendedSize
;
577 if (AppendedSize
> FAT_MAX_ALLOCATE_SIZE
) {
579 // If the appended size is larger, maybe we can not allocate the whole
580 // memory once. So if the growed size is larger than 10M, we just
581 // allocate 10M memory (one healthy system should have 10M available
582 // memory), and then write the zerobuffer to the file several times.
584 BufferSize
= FAT_MAX_ALLOCATE_SIZE
;
587 ZeroBuffer
= AllocateZeroPool (BufferSize
);
588 if (ZeroBuffer
== NULL
) {
589 return EFI_OUT_OF_RESOURCES
;
593 WriteSize
= AppendedSize
> BufferSize
? BufferSize
: (UINTN
) AppendedSize
;
594 AppendedSize
-= WriteSize
;
595 Status
= FatAccessOFile (OFile
, WriteData
, WritePos
, &WriteSize
, ZeroBuffer
, NULL
);
596 if (EFI_ERROR (Status
)) {
600 WritePos
+= WriteSize
;
601 } while (AppendedSize
> 0);
603 FreePool (ZeroBuffer
);
609 Truncate the OFile to smaller file size.
611 @param OFile - The open file.
612 @param TruncatedSize - The new file size.
614 @retval EFI_SUCCESS - The file is truncated successfully.
615 @return other - An error occurred when truncating file.
621 IN UINTN TruncatedSize
624 OFile
->FileSize
= TruncatedSize
;
625 return FatShrinkEof (OFile
);