]>
git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/ReadWrite.c
3 Copyright (c) 2005 - 2009, Intel Corporation
4 All rights reserved. This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Functions that perform file read/write
30 IN EFI_FILE_PROTOCOL
*FHand
,
37 Get the file's position of the file.
41 FHand - The handle of file.
42 Position - The file's position of the file.
46 EFI_SUCCESS - Get the info successfully.
47 EFI_DEVICE_ERROR - Can not find the OFile for the file.
48 EFI_UNSUPPORTED - The open file is not a file.
55 IFile
= IFILE_FROM_FHAND (FHand
);
58 if (OFile
->Error
== EFI_NOT_FOUND
) {
59 return EFI_DEVICE_ERROR
;
62 if (OFile
->ODir
!= NULL
) {
63 return EFI_UNSUPPORTED
;
66 *Position
= IFile
->Position
;
73 IN EFI_FILE_PROTOCOL
*FHand
,
80 Set the file's position of the file.
84 FHand - The handle of file.
85 Position - The file's position of the file.
89 EFI_SUCCESS - Set the info successfully.
90 EFI_DEVICE_ERROR - Can not find the OFile for the file.
91 EFI_UNSUPPORTED - Set a directory with a not-zero position.
98 IFile
= IFILE_FROM_FHAND (FHand
);
101 if (OFile
->Error
== EFI_NOT_FOUND
) {
102 return EFI_DEVICE_ERROR
;
105 // If this is a directory, we can only set back to position 0
107 if (OFile
->ODir
!= NULL
) {
110 // Reset current directory cursor;
112 return EFI_UNSUPPORTED
;
115 FatResetODirCursor (OFile
);
120 if (Position
== (UINT64
)-1) {
121 Position
= OFile
->FileSize
;
126 IFile
->Position
= Position
;
133 IN OUT UINTN
*BufferSize
,
140 Get the file info from the open file of the IFile into Buffer.
144 IFile - The instance of the open file.
145 BufferSize - Size of Buffer.
146 Buffer - Buffer containing read data.
150 EFI_SUCCESS - Get the file info successfully.
151 other - An error occurred when operation the disk.
161 OFile
= IFile
->OFile
;
163 CurrentPos
= ((UINT32
) IFile
->Position
) / sizeof (FAT_DIRECTORY_ENTRY
);
166 // We need to relocate the directory
168 if (CurrentPos
< ODir
->CurrentPos
) {
170 // The directory cursor has been modified by another IFile, we reset the cursor
172 FatResetODirCursor (OFile
);
175 // We seek the next directory entry's position
178 Status
= FatGetNextDirEnt (OFile
, &DirEnt
);
179 if (EFI_ERROR (Status
) || DirEnt
== NULL
) {
181 // Something error occurred or reach the end of directory,
182 // return 0 buffersize
187 } while (ODir
->CurrentPos
<= CurrentPos
);
188 Status
= FatGetDirEntInfo (OFile
->Volume
, DirEnt
, BufferSize
, Buffer
);
192 // Update IFile's Position
194 if (!EFI_ERROR (Status
)) {
196 // Update IFile->Position, if everything is all right
198 CurrentPos
= ODir
->CurrentPos
;
199 IFile
->Position
= (UINT64
) (CurrentPos
* sizeof (FAT_DIRECTORY_ENTRY
));
207 IN EFI_FILE_PROTOCOL
*FHand
,
209 IN OUT UINTN
*BufferSize
,
216 Get the file info from the open file of the IFile into Buffer.
220 FHand - The file handle to access.
221 IoMode - Indicate whether the access mode is reading or writing.
222 BufferSize - Size of Buffer.
223 Buffer - Buffer containing read data.
227 EFI_SUCCESS - Get the file info successfully.
228 EFI_DEVICE_ERROR - Can not find the OFile for the file.
229 EFI_VOLUME_CORRUPTED - The file type of open file is error.
230 EFI_WRITE_PROTECTED - The disk is write protect.
231 EFI_ACCESS_DENIED - The file is read-only.
232 other - An error occurred when operating on the disk.
242 IFile
= IFILE_FROM_FHAND (FHand
);
243 OFile
= IFile
->OFile
;
244 Volume
= OFile
->Volume
;
246 if (OFile
->Error
== EFI_NOT_FOUND
) {
247 return EFI_DEVICE_ERROR
;
250 if (IoMode
== READ_DATA
) {
252 // If position is at EOF, then return device error
254 if (IFile
->Position
> OFile
->FileSize
) {
255 return EFI_DEVICE_ERROR
;
259 // Check if the we can write data
261 if (Volume
->ReadOnly
) {
262 return EFI_WRITE_PROTECTED
;
265 if (IFile
->ReadOnly
) {
266 return EFI_ACCESS_DENIED
;
272 Status
= OFile
->Error
;
273 if (!EFI_ERROR (Status
)) {
274 if (OFile
->ODir
!= NULL
) {
276 // Access a directory
278 Status
= EFI_UNSUPPORTED
;
279 if (IoMode
== READ_DATA
) {
281 // Read a directory is supported
283 Status
= FatIFileReadDir (IFile
, BufferSize
, Buffer
);
291 EndPosition
= IFile
->Position
+ *BufferSize
;
292 if (EndPosition
> OFile
->FileSize
) {
294 // The position goes beyond the end of file
296 if (IoMode
== READ_DATA
) {
298 // Adjust the actual size read
300 *BufferSize
-= (UINTN
) EndPosition
- OFile
->FileSize
;
303 // We expand the file size of OFile
305 Status
= FatGrowEof (OFile
, EndPosition
);
306 if (EFI_ERROR (Status
)) {
308 // Must update the file's info into the file's Directory Entry
309 // and then flush the dirty cache info into disk.
312 FatOFileFlush (OFile
);
317 FatUpdateDirEntClusterSizeInfo (OFile
);
321 Status
= FatAccessOFile (OFile
, IoMode
, (UINTN
) IFile
->Position
, BufferSize
, Buffer
);
322 IFile
->Position
+= *BufferSize
;
327 if (EFI_ERROR (Status
)) {
328 Status
= FatCleanupVolume (Volume
, OFile
, Status
);
331 // On EFI_SUCCESS case, not calling FatCleanupVolume():
332 // 1) The Cache flush operation is avoided to enhance
333 // performance. Caller is responsible to call Flush() when necessary.
334 // 2) The volume dirty bit is probably set already, and is expected to be
335 // cleaned in subsequent Flush() or other operations.
336 // 3) Write operation doesn't affect OFile/IFile structure, so
337 // Reference checking is not necessary.
346 IN EFI_FILE_PROTOCOL
*FHand
,
347 IN OUT UINTN
*BufferSize
,
358 FHand - The handle of the file.
359 BufferSize - Size of Buffer.
360 Buffer - Buffer containing read data.
364 EFI_SUCCESS - Get the file info successfully.
365 EFI_DEVICE_ERROR - Can not find the OFile for the file.
366 EFI_VOLUME_CORRUPTED - The file type of open file is error.
367 other - An error occurred when operation the disk.
371 return FatIFileAccess (FHand
, READ_DATA
, BufferSize
, Buffer
);
377 IN EFI_FILE_PROTOCOL
*FHand
,
378 IN OUT UINTN
*BufferSize
,
385 Write the content of buffer into files.
389 FHand - The handle of the file.
390 BufferSize - Size of Buffer.
391 Buffer - Buffer containing write data.
395 EFI_SUCCESS - Set the file info successfully.
396 EFI_WRITE_PROTECTED - The disk is write protect.
397 EFI_ACCESS_DENIED - The file is read-only.
398 EFI_DEVICE_ERROR - The OFile is not valid.
399 EFI_UNSUPPORTED - The open file is not a file.
400 - The writing file size is larger than 4GB.
401 other - An error occurred when operation the disk.
405 return FatIFileAccess (FHand
, WRITE_DATA
, BufferSize
, Buffer
);
413 IN OUT UINTN
*DataBufferSize
,
414 IN OUT UINT8
*UserBuffer
420 This function reads data from a file or writes data to a file.
421 It uses OFile->PosRem to determine how much data can be accessed in one time.
425 OFile - The open file.
426 IoMode - Indicate whether the access mode is reading or writing.
427 Position - The position where data will be accessed.
428 DataBufferSize - Size of Buffer.
429 UserBuffer - Buffer containing data.
433 EFI_SUCCESS - Access the data successfully.
434 other - An error occurred when operating on the disk.
443 BufferSize
= *DataBufferSize
;
444 Volume
= OFile
->Volume
;
445 ASSERT_VOLUME_LOCKED (Volume
);
447 Status
= EFI_SUCCESS
;
448 while (BufferSize
> 0) {
450 // Seek the OFile to the file position
452 Status
= FatOFilePosition (OFile
, Position
, BufferSize
);
453 if (EFI_ERROR (Status
)) {
457 // Clip length to block run
459 Len
= BufferSize
> OFile
->PosRem
? OFile
->PosRem
: BufferSize
;
464 Status
= FatDiskIo (Volume
, IoMode
, OFile
->PosDisk
, Len
, UserBuffer
);
465 if (EFI_ERROR (Status
)) {
469 // Data was successfully accessed
474 if (IoMode
== WRITE_DATA
) {
476 OFile
->Archive
= TRUE
;
479 // Make sure no outbound occurred
481 ASSERT (Position
<= OFile
->FileSize
);
484 // Update the number of bytes accessed
486 *DataBufferSize
-= BufferSize
;
493 IN UINT64 ExpandedSize
499 Expand OFile by appending zero bytes at the end of OFile.
503 OFile - The open file.
504 ExpandedSize - The number of zero bytes appended at the end of the file.
508 EFI_SUCCESS - The file is expanded successfully.
509 other - An error occurred when expanding file.
516 WritePos
= OFile
->FileSize
;
517 Status
= FatGrowEof (OFile
, ExpandedSize
);
518 if (!EFI_ERROR (Status
)) {
519 Status
= FatWriteZeroPool (OFile
, WritePos
);
534 Write zero pool from the WritePos to the end of OFile.
538 OFile - The open file to write zero pool.
539 WritePos - The number of zero bytes written.
543 EFI_SUCCESS - Write the zero pool successfully.
544 EFI_OUT_OF_RESOURCES - Not enough memory to perform the operation.
545 other - An error occurred when writing disk.
555 AppendedSize
= OFile
->FileSize
- WritePos
;
556 BufferSize
= AppendedSize
;
557 if (AppendedSize
> FAT_MAX_ALLOCATE_SIZE
) {
559 // If the appended size is larger, maybe we can not allocate the whole
560 // memory once. So if the growed size is larger than 10M, we just
561 // allocate 10M memory (one healthy system should have 10M available
562 // memory), and then write the zerobuffer to the file several times.
564 BufferSize
= FAT_MAX_ALLOCATE_SIZE
;
567 ZeroBuffer
= AllocateZeroPool (BufferSize
);
568 if (ZeroBuffer
== NULL
) {
569 return EFI_OUT_OF_RESOURCES
;
573 WriteSize
= AppendedSize
> BufferSize
? BufferSize
: (UINTN
) AppendedSize
;
574 AppendedSize
-= WriteSize
;
575 Status
= FatAccessOFile (OFile
, WRITE_DATA
, WritePos
, &WriteSize
, ZeroBuffer
);
576 if (EFI_ERROR (Status
)) {
580 WritePos
+= WriteSize
;
581 } while (AppendedSize
> 0);
583 FreePool (ZeroBuffer
);
590 IN UINTN TruncatedSize
596 Truncate the OFile to smaller file size.
600 OFile - The open file.
601 TruncatedSize - The new file size.
605 EFI_SUCCESS - The file is truncated successfully.
606 other - An error occurred when truncating file.
610 OFile
->FileSize
= TruncatedSize
;
611 return FatShrinkEof (OFile
);