]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - FatPkg/EnhancedFatDxe/ReadWrite.c
FatPkg: Refine casting expression result to bigger size
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / ReadWrite.c
... / ...
CommitLineData
1/** @file\r
2 Functions that perform file read/write.\r
3\r
4Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials are licensed and made available\r
6under the terms and conditions of the BSD License which accompanies this\r
7distribution. 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\r
16#include "Fat.h"\r
17\r
18/**\r
19\r
20 Get the file's position of the file.\r
21\r
22\r
23 @param FHand - The handle of file.\r
24 @param Position - The file's position of the file.\r
25\r
26 @retval EFI_SUCCESS - Get the info successfully.\r
27 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.\r
28 @retval EFI_UNSUPPORTED - The open file is not a file.\r
29\r
30**/\r
31EFI_STATUS\r
32EFIAPI\r
33FatGetPosition (\r
34 IN EFI_FILE_PROTOCOL *FHand,\r
35 OUT UINT64 *Position\r
36 )\r
37{\r
38 FAT_IFILE *IFile;\r
39 FAT_OFILE *OFile;\r
40\r
41 IFile = IFILE_FROM_FHAND (FHand);\r
42 OFile = IFile->OFile;\r
43\r
44 if (OFile->Error == EFI_NOT_FOUND) {\r
45 return EFI_DEVICE_ERROR;\r
46 }\r
47\r
48 if (OFile->ODir != NULL) {\r
49 return EFI_UNSUPPORTED;\r
50 }\r
51\r
52 *Position = IFile->Position;\r
53 return EFI_SUCCESS;\r
54}\r
55\r
56/**\r
57\r
58 Set the file's position of the file.\r
59\r
60 @param FHand - The handle of file.\r
61 @param Position - The file's position of the file.\r
62\r
63 @retval EFI_SUCCESS - Set the info successfully.\r
64 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.\r
65 @retval EFI_UNSUPPORTED - Set a directory with a not-zero position.\r
66\r
67**/\r
68EFI_STATUS\r
69EFIAPI\r
70FatSetPosition (\r
71 IN EFI_FILE_PROTOCOL *FHand,\r
72 IN UINT64 Position\r
73 )\r
74{\r
75 FAT_IFILE *IFile;\r
76 FAT_OFILE *OFile;\r
77\r
78 IFile = IFILE_FROM_FHAND (FHand);\r
79 OFile = IFile->OFile;\r
80\r
81 if (OFile->Error == EFI_NOT_FOUND) {\r
82 return EFI_DEVICE_ERROR;\r
83 }\r
84\r
85 FatWaitNonblockingTask (IFile);\r
86\r
87 //\r
88 // If this is a directory, we can only set back to position 0\r
89 //\r
90 if (OFile->ODir != NULL) {\r
91 if (Position != 0) {\r
92 //\r
93 // Reset current directory cursor;\r
94 //\r
95 return EFI_UNSUPPORTED;\r
96 }\r
97\r
98 FatResetODirCursor (OFile);\r
99 }\r
100 //\r
101 // Set the position\r
102 //\r
103 if (Position == (UINT64)-1) {\r
104 Position = OFile->FileSize;\r
105 }\r
106 //\r
107 // Set the position\r
108 //\r
109 IFile->Position = Position;\r
110 return EFI_SUCCESS;\r
111}\r
112\r
113/**\r
114\r
115 Get the file info from the open file of the IFile into Buffer.\r
116\r
117 @param IFile - The instance of the open file.\r
118 @param BufferSize - Size of Buffer.\r
119 @param Buffer - Buffer containing read data.\r
120\r
121 @retval EFI_SUCCESS - Get the file info successfully.\r
122 @retval other - An error occurred when operation the disk.\r
123\r
124**/\r
125EFI_STATUS\r
126FatIFileReadDir (\r
127 IN FAT_IFILE *IFile,\r
128 IN OUT UINTN *BufferSize,\r
129 OUT VOID *Buffer\r
130 )\r
131{\r
132 EFI_STATUS Status;\r
133 FAT_OFILE *OFile;\r
134 FAT_ODIR *ODir;\r
135 FAT_DIRENT *DirEnt;\r
136 UINT32 CurrentPos;\r
137\r
138 OFile = IFile->OFile;\r
139 ODir = OFile->ODir;\r
140 CurrentPos = ((UINT32) IFile->Position) / sizeof (FAT_DIRECTORY_ENTRY);\r
141\r
142 //\r
143 // We need to relocate the directory\r
144 //\r
145 if (CurrentPos < ODir->CurrentPos) {\r
146 //\r
147 // The directory cursor has been modified by another IFile, we reset the cursor\r
148 //\r
149 FatResetODirCursor (OFile);\r
150 }\r
151 //\r
152 // We seek the next directory entry's position\r
153 //\r
154 do {\r
155 Status = FatGetNextDirEnt (OFile, &DirEnt);\r
156 if (EFI_ERROR (Status) || DirEnt == NULL) {\r
157 //\r
158 // Something error occurred or reach the end of directory,\r
159 // return 0 buffersize\r
160 //\r
161 *BufferSize = 0;\r
162 goto Done;\r
163 }\r
164 } while (ODir->CurrentPos <= CurrentPos);\r
165 Status = FatGetDirEntInfo (OFile->Volume, DirEnt, BufferSize, Buffer);\r
166\r
167Done:\r
168 //\r
169 // Update IFile's Position\r
170 //\r
171 if (!EFI_ERROR (Status)) {\r
172 //\r
173 // Update IFile->Position, if everything is all right\r
174 //\r
175 CurrentPos = ODir->CurrentPos;\r
176 IFile->Position = CurrentPos * sizeof (FAT_DIRECTORY_ENTRY);\r
177 }\r
178\r
179 return Status;\r
180}\r
181\r
182/**\r
183\r
184 Get the file info from the open file of the IFile into Buffer.\r
185\r
186 @param FHand - The file handle to access.\r
187 @param IoMode - Indicate whether the access mode is reading or writing.\r
188 @param BufferSize - Size of Buffer.\r
189 @param Buffer - Buffer containing read data.\r
190 @param Token - A pointer to the token associated with the transaction.\r
191\r
192 @retval EFI_SUCCESS - Get the file info successfully.\r
193 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.\r
194 @retval EFI_VOLUME_CORRUPTED - The file type of open file is error.\r
195 @retval EFI_WRITE_PROTECTED - The disk is write protect.\r
196 @retval EFI_ACCESS_DENIED - The file is read-only.\r
197 @return other - An error occurred when operating on the disk.\r
198\r
199**/\r
200EFI_STATUS\r
201FatIFileAccess (\r
202 IN EFI_FILE_PROTOCOL *FHand,\r
203 IN IO_MODE IoMode,\r
204 IN OUT UINTN *BufferSize,\r
205 IN OUT VOID *Buffer,\r
206 IN EFI_FILE_IO_TOKEN *Token\r
207 )\r
208{\r
209 EFI_STATUS Status;\r
210 FAT_IFILE *IFile;\r
211 FAT_OFILE *OFile;\r
212 FAT_VOLUME *Volume;\r
213 UINT64 EndPosition;\r
214 FAT_TASK *Task;\r
215\r
216 IFile = IFILE_FROM_FHAND (FHand);\r
217 OFile = IFile->OFile;\r
218 Volume = OFile->Volume;\r
219 Task = NULL;\r
220\r
221 //\r
222 // Write to a directory is unsupported\r
223 //\r
224 if ((OFile->ODir != NULL) && (IoMode == WriteData)) {\r
225 return EFI_UNSUPPORTED;\r
226 }\r
227\r
228 if (OFile->Error == EFI_NOT_FOUND) {\r
229 return EFI_DEVICE_ERROR;\r
230 }\r
231\r
232 if (IoMode == ReadData) {\r
233 //\r
234 // If position is at EOF, then return device error\r
235 //\r
236 if (IFile->Position > OFile->FileSize) {\r
237 return EFI_DEVICE_ERROR;\r
238 }\r
239 } else {\r
240 //\r
241 // Check if the we can write data\r
242 //\r
243 if (Volume->ReadOnly) {\r
244 return EFI_WRITE_PROTECTED;\r
245 }\r
246\r
247 if (IFile->ReadOnly) {\r
248 return EFI_ACCESS_DENIED;\r
249 }\r
250 }\r
251\r
252 if (Token == NULL) {\r
253 FatWaitNonblockingTask (IFile);\r
254 } else {\r
255 //\r
256 // Caller shouldn't call the non-blocking interfaces if the low layer doesn't support DiskIo2.\r
257 // But if it calls, the below check can avoid crash.\r
258 //\r
259 if (FHand->Revision < EFI_FILE_PROTOCOL_REVISION2) {\r
260 return EFI_UNSUPPORTED;\r
261 }\r
262 Task = FatCreateTask (IFile, Token);\r
263 if (Task == NULL) {\r
264 return EFI_OUT_OF_RESOURCES;\r
265 }\r
266 }\r
267\r
268 FatAcquireLock ();\r
269\r
270 Status = OFile->Error;\r
271 if (!EFI_ERROR (Status)) {\r
272 if (OFile->ODir != NULL) {\r
273 //\r
274 // Read a directory is supported\r
275 //\r
276 ASSERT (IoMode == ReadData);\r
277 Status = FatIFileReadDir (IFile, BufferSize, Buffer);\r
278 OFile = NULL;\r
279 } else {\r
280 //\r
281 // Access a file\r
282 //\r
283 EndPosition = IFile->Position + *BufferSize;\r
284 if (EndPosition > OFile->FileSize) {\r
285 //\r
286 // The position goes beyond the end of file\r
287 //\r
288 if (IoMode == ReadData) {\r
289 //\r
290 // Adjust the actual size read\r
291 //\r
292 *BufferSize -= (UINTN) EndPosition - OFile->FileSize;\r
293 } else {\r
294 //\r
295 // We expand the file size of OFile\r
296 //\r
297 Status = FatGrowEof (OFile, EndPosition);\r
298 if (EFI_ERROR (Status)) {\r
299 //\r
300 // Must update the file's info into the file's Directory Entry\r
301 // and then flush the dirty cache info into disk.\r
302 //\r
303 *BufferSize = 0;\r
304 FatOFileFlush (OFile);\r
305 OFile = NULL;\r
306 goto Done;\r
307 }\r
308\r
309 FatUpdateDirEntClusterSizeInfo (OFile);\r
310 }\r
311 }\r
312\r
313 Status = FatAccessOFile (OFile, IoMode, (UINTN) IFile->Position, BufferSize, Buffer, Task);\r
314 IFile->Position += *BufferSize;\r
315 }\r
316 }\r
317\r
318 if (Token != NULL) {\r
319 if (!EFI_ERROR (Status)) {\r
320 Status = FatQueueTask (IFile, Task);\r
321 } else {\r
322 FatDestroyTask (Task);\r
323 }\r
324 }\r
325\r
326Done:\r
327 //\r
328 // On EFI_SUCCESS case, not calling FatCleanupVolume():\r
329 // 1) The Cache flush operation is avoided to enhance\r
330 // performance. Caller is responsible to call Flush() when necessary.\r
331 // 2) The volume dirty bit is probably set already, and is expected to be\r
332 // cleaned in subsequent Flush() or other operations.\r
333 // 3) Write operation doesn't affect OFile/IFile structure, so\r
334 // Reference checking is not necessary.\r
335 //\r
336 if (EFI_ERROR (Status)) {\r
337 Status = FatCleanupVolume (Volume, OFile, Status, NULL);\r
338 }\r
339\r
340 FatReleaseLock ();\r
341 return Status;\r
342}\r
343\r
344/**\r
345\r
346 Get the file info.\r
347\r
348 @param FHand - The handle of the file.\r
349 @param BufferSize - Size of Buffer.\r
350 @param Buffer - Buffer containing read data.\r
351\r
352\r
353 @retval EFI_SUCCESS - Get the file info successfully.\r
354 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.\r
355 @retval EFI_VOLUME_CORRUPTED - The file type of open file is error.\r
356 @return other - An error occurred when operation the disk.\r
357\r
358**/\r
359EFI_STATUS\r
360EFIAPI\r
361FatRead (\r
362 IN EFI_FILE_PROTOCOL *FHand,\r
363 IN OUT UINTN *BufferSize,\r
364 OUT VOID *Buffer\r
365 )\r
366{\r
367 return FatIFileAccess (FHand, ReadData, BufferSize, Buffer, NULL);\r
368}\r
369\r
370/**\r
371\r
372 Get the file info.\r
373\r
374 @param FHand - The handle of the file.\r
375 @param Token - A pointer to the token associated with the transaction.\r
376\r
377 @retval EFI_SUCCESS - Get the file info successfully.\r
378 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.\r
379 @retval EFI_VOLUME_CORRUPTED - The file type of open file is error.\r
380 @return other - An error occurred when operation the disk.\r
381\r
382**/\r
383EFI_STATUS\r
384EFIAPI\r
385FatReadEx (\r
386 IN EFI_FILE_PROTOCOL *FHand,\r
387 IN OUT EFI_FILE_IO_TOKEN *Token\r
388 )\r
389{\r
390 return FatIFileAccess (FHand, ReadData, &Token->BufferSize, Token->Buffer, Token);\r
391}\r
392\r
393/**\r
394\r
395 Write the content of buffer into files.\r
396\r
397 @param FHand - The handle of the file.\r
398 @param BufferSize - Size of Buffer.\r
399 @param Buffer - Buffer containing write data.\r
400\r
401 @retval EFI_SUCCESS - Set the file info successfully.\r
402 @retval EFI_WRITE_PROTECTED - The disk is write protect.\r
403 @retval EFI_ACCESS_DENIED - The file is read-only.\r
404 @retval EFI_DEVICE_ERROR - The OFile is not valid.\r
405 @retval EFI_UNSUPPORTED - The open file is not a file.\r
406 - The writing file size is larger than 4GB.\r
407 @return other - An error occurred when operation the disk.\r
408\r
409**/\r
410EFI_STATUS\r
411EFIAPI\r
412FatWrite (\r
413 IN EFI_FILE_PROTOCOL *FHand,\r
414 IN OUT UINTN *BufferSize,\r
415 IN VOID *Buffer\r
416 )\r
417{\r
418 return FatIFileAccess (FHand, WriteData, BufferSize, Buffer, NULL);\r
419}\r
420\r
421/**\r
422\r
423 Get the file info.\r
424\r
425 @param FHand - The handle of the file.\r
426 @param Token - A pointer to the token associated with the transaction.\r
427\r
428 @retval EFI_SUCCESS - Get the file info successfully.\r
429 @retval EFI_DEVICE_ERROR - Can not find the OFile for the file.\r
430 @retval EFI_VOLUME_CORRUPTED - The file type of open file is error.\r
431 @return other - An error occurred when operation the disk.\r
432\r
433**/\r
434EFI_STATUS\r
435EFIAPI\r
436FatWriteEx (\r
437 IN EFI_FILE_PROTOCOL *FHand,\r
438 IN OUT EFI_FILE_IO_TOKEN *Token\r
439 )\r
440{\r
441 return FatIFileAccess (FHand, WriteData, &Token->BufferSize, Token->Buffer, Token);\r
442}\r
443\r
444/**\r
445\r
446 This function reads data from a file or writes data to a file.\r
447 It uses OFile->PosRem to determine how much data can be accessed in one time.\r
448\r
449 @param OFile - The open file.\r
450 @param IoMode - Indicate whether the access mode is reading or writing.\r
451 @param Position - The position where data will be accessed.\r
452 @param DataBufferSize - Size of Buffer.\r
453 @param UserBuffer - Buffer containing data.\r
454 @param Task point to task instance.\r
455\r
456 @retval EFI_SUCCESS - Access the data successfully.\r
457 @return other - An error occurred when operating on the disk.\r
458\r
459**/\r
460EFI_STATUS\r
461FatAccessOFile (\r
462 IN FAT_OFILE *OFile,\r
463 IN IO_MODE IoMode,\r
464 IN UINTN Position,\r
465 IN OUT UINTN *DataBufferSize,\r
466 IN OUT UINT8 *UserBuffer,\r
467 IN FAT_TASK *Task\r
468 )\r
469{\r
470 FAT_VOLUME *Volume;\r
471 UINTN Len;\r
472 EFI_STATUS Status;\r
473 UINTN BufferSize;\r
474\r
475 BufferSize = *DataBufferSize;\r
476 Volume = OFile->Volume;\r
477 ASSERT_VOLUME_LOCKED (Volume);\r
478\r
479 Status = EFI_SUCCESS;\r
480 while (BufferSize > 0) {\r
481 //\r
482 // Seek the OFile to the file position\r
483 //\r
484 Status = FatOFilePosition (OFile, Position, BufferSize);\r
485 if (EFI_ERROR (Status)) {\r
486 break;\r
487 }\r
488 //\r
489 // Clip length to block run\r
490 //\r
491 Len = BufferSize > OFile->PosRem ? OFile->PosRem : BufferSize;\r
492\r
493 //\r
494 // Write the data\r
495 //\r
496 Status = FatDiskIo (Volume, IoMode, OFile->PosDisk, Len, UserBuffer, Task);\r
497 if (EFI_ERROR (Status)) {\r
498 break;\r
499 }\r
500 //\r
501 // Data was successfully accessed\r
502 //\r
503 Position += Len;\r
504 UserBuffer += Len;\r
505 BufferSize -= Len;\r
506 if (IoMode == WriteData) {\r
507 OFile->Dirty = TRUE;\r
508 OFile->Archive = TRUE;\r
509 }\r
510 //\r
511 // Make sure no outbound occurred\r
512 //\r
513 ASSERT (Position <= OFile->FileSize);\r
514 }\r
515 //\r
516 // Update the number of bytes accessed\r
517 //\r
518 *DataBufferSize -= BufferSize;\r
519 return Status;\r
520}\r
521\r
522/**\r
523\r
524 Expand OFile by appending zero bytes at the end of OFile.\r
525\r
526 @param OFile - The open file.\r
527 @param ExpandedSize - The number of zero bytes appended at the end of the file.\r
528\r
529 @retval EFI_SUCCESS - The file is expanded successfully.\r
530 @return other - An error occurred when expanding file.\r
531\r
532**/\r
533EFI_STATUS\r
534FatExpandOFile (\r
535 IN FAT_OFILE *OFile,\r
536 IN UINT64 ExpandedSize\r
537 )\r
538{\r
539 EFI_STATUS Status;\r
540 UINTN WritePos;\r
541\r
542 WritePos = OFile->FileSize;\r
543 Status = FatGrowEof (OFile, ExpandedSize);\r
544 if (!EFI_ERROR (Status)) {\r
545 Status = FatWriteZeroPool (OFile, WritePos);\r
546 }\r
547\r
548 return Status;\r
549}\r
550\r
551/**\r
552\r
553 Write zero pool from the WritePos to the end of OFile.\r
554\r
555 @param OFile - The open file to write zero pool.\r
556 @param WritePos - The number of zero bytes written.\r
557\r
558 @retval EFI_SUCCESS - Write the zero pool successfully.\r
559 @retval EFI_OUT_OF_RESOURCES - Not enough memory to perform the operation.\r
560 @return other - An error occurred when writing disk.\r
561\r
562**/\r
563EFI_STATUS\r
564FatWriteZeroPool (\r
565 IN FAT_OFILE *OFile,\r
566 IN UINTN WritePos\r
567 )\r
568{\r
569 EFI_STATUS Status;\r
570 VOID *ZeroBuffer;\r
571 UINTN AppendedSize;\r
572 UINTN BufferSize;\r
573 UINTN WriteSize;\r
574\r
575 AppendedSize = OFile->FileSize - WritePos;\r
576 BufferSize = AppendedSize;\r
577 if (AppendedSize > FAT_MAX_ALLOCATE_SIZE) {\r
578 //\r
579 // If the appended size is larger, maybe we can not allocate the whole\r
580 // memory once. So if the growed size is larger than 10M, we just\r
581 // allocate 10M memory (one healthy system should have 10M available\r
582 // memory), and then write the zerobuffer to the file several times.\r
583 //\r
584 BufferSize = FAT_MAX_ALLOCATE_SIZE;\r
585 }\r
586\r
587 ZeroBuffer = AllocateZeroPool (BufferSize);\r
588 if (ZeroBuffer == NULL) {\r
589 return EFI_OUT_OF_RESOURCES;\r
590 }\r
591\r
592 do {\r
593 WriteSize = AppendedSize > BufferSize ? BufferSize : (UINTN) AppendedSize;\r
594 AppendedSize -= WriteSize;\r
595 Status = FatAccessOFile (OFile, WriteData, WritePos, &WriteSize, ZeroBuffer, NULL);\r
596 if (EFI_ERROR (Status)) {\r
597 break;\r
598 }\r
599\r
600 WritePos += WriteSize;\r
601 } while (AppendedSize > 0);\r
602\r
603 FreePool (ZeroBuffer);\r
604 return Status;\r
605}\r
606\r
607/**\r
608\r
609 Truncate the OFile to smaller file size.\r
610\r
611 @param OFile - The open file.\r
612 @param TruncatedSize - The new file size.\r
613\r
614 @retval EFI_SUCCESS - The file is truncated successfully.\r
615 @return other - An error occurred when truncating file.\r
616\r
617**/\r
618EFI_STATUS\r
619FatTruncateOFile (\r
620 IN FAT_OFILE *OFile,\r
621 IN UINTN TruncatedSize\r
622 )\r
623{\r
624 OFile->FileSize = TruncatedSize;\r
625 return FatShrinkEof (OFile);\r
626}\r