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