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