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 |
6163cc98 | 5 | This program and the accompanying materials are licensed and made available\r |
b9ec9330 QH |
6 | under the terms and conditions of the BSD License which accompanies this\r |
7 | distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | \r | |
cae7420b | 14 | **/\r |
b9ec9330 | 15 | \r |
cae7420b | 16 | #include "Fat.h"\r |
b9ec9330 | 17 | \r |
cae7420b | 18 | /**\r |
b9ec9330 | 19 | \r |
cae7420b | 20 | Get the file's position of the file.\r |
b9ec9330 | 21 | \r |
b9ec9330 | 22 | \r |
cae7420b DB |
23 | @param FHand - The handle of file.\r |
24 | @param Position - The file's position of the file.\r | |
b9ec9330 | 25 | \r |
cae7420b DB |
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 | |
b9ec9330 | 29 | \r |
cae7420b | 30 | **/\r |
b9ec9330 QH |
31 | EFI_STATUS\r |
32 | EFIAPI\r | |
33 | FatGetPosition (\r | |
dba03ba1 | 34 | IN EFI_FILE_PROTOCOL *FHand,\r |
b9ec9330 QH |
35 | OUT UINT64 *Position\r |
36 | )\r | |
b9ec9330 QH |
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 | |
cae7420b DB |
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 | |
b9ec9330 QH |
68 | EFI_STATUS\r |
69 | EFIAPI\r | |
70 | FatSetPosition (\r | |
dba03ba1 QH |
71 | IN EFI_FILE_PROTOCOL *FHand,\r |
72 | IN UINT64 Position\r | |
b9ec9330 | 73 | )\r |
b9ec9330 QH |
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 | |
149d6335 RN |
84 | \r |
85 | FatWaitNonblockingTask (IFile);\r | |
86 | \r | |
b9ec9330 QH |
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 | |
c4ba493e | 103 | if (Position == (UINT64)-1) {\r |
b9ec9330 QH |
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 | |
cae7420b DB |
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 | |
b9ec9330 QH |
125 | EFI_STATUS\r |
126 | FatIFileReadDir (\r | |
127 | IN FAT_IFILE *IFile,\r | |
128 | IN OUT UINTN *BufferSize,\r | |
129 | OUT VOID *Buffer\r | |
130 | )\r | |
b9ec9330 QH |
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 | |
167 | Done:\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 | |
64b25f5d | 176 | IFile->Position = CurrentPos * sizeof (FAT_DIRECTORY_ENTRY);\r |
b9ec9330 QH |
177 | }\r |
178 | \r | |
179 | return Status;\r | |
180 | }\r | |
181 | \r | |
cae7420b DB |
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 | |
b9ec9330 QH |
200 | EFI_STATUS\r |
201 | FatIFileAccess (\r | |
dba03ba1 | 202 | IN EFI_FILE_PROTOCOL *FHand,\r |
b9ec9330 QH |
203 | IN IO_MODE IoMode,\r |
204 | IN OUT UINTN *BufferSize,\r | |
149d6335 RN |
205 | IN OUT VOID *Buffer,\r |
206 | IN EFI_FILE_IO_TOKEN *Token\r | |
b9ec9330 | 207 | )\r |
b9ec9330 QH |
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 | |
149d6335 | 214 | FAT_TASK *Task;\r |
b9ec9330 QH |
215 | \r |
216 | IFile = IFILE_FROM_FHAND (FHand);\r | |
217 | OFile = IFile->OFile;\r | |
218 | Volume = OFile->Volume;\r | |
149d6335 | 219 | Task = NULL;\r |
b9ec9330 | 220 | \r |
55248f85 RN |
221 | //\r |
222 | // Write to a directory is unsupported\r | |
223 | //\r | |
c1680e88 | 224 | if ((OFile->ODir != NULL) && (IoMode == WriteData)) {\r |
55248f85 RN |
225 | return EFI_UNSUPPORTED;\r |
226 | }\r | |
227 | \r | |
b9ec9330 QH |
228 | if (OFile->Error == EFI_NOT_FOUND) {\r |
229 | return EFI_DEVICE_ERROR;\r | |
230 | }\r | |
231 | \r | |
c1680e88 | 232 | if (IoMode == ReadData) {\r |
b9ec9330 QH |
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 | |
149d6335 RN |
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 | |
b9ec9330 QH |
268 | FatAcquireLock ();\r |
269 | \r | |
270 | Status = OFile->Error;\r | |
271 | if (!EFI_ERROR (Status)) {\r | |
272 | if (OFile->ODir != NULL) {\r | |
273 | //\r | |
55248f85 | 274 | // Read a directory is supported\r |
b9ec9330 | 275 | //\r |
c1680e88 | 276 | ASSERT (IoMode == ReadData);\r |
55248f85 | 277 | Status = FatIFileReadDir (IFile, BufferSize, Buffer);\r |
b9ec9330 QH |
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 | |
c1680e88 | 288 | if (IoMode == ReadData) {\r |
b9ec9330 QH |
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 | |
149d6335 | 313 | Status = FatAccessOFile (OFile, IoMode, (UINTN) IFile->Position, BufferSize, Buffer, Task);\r |
b9ec9330 QH |
314 | IFile->Position += *BufferSize;\r |
315 | }\r | |
316 | }\r | |
317 | \r | |
149d6335 RN |
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 | |
b9ec9330 | 324 | }\r |
149d6335 RN |
325 | \r |
326 | Done:\r | |
b9ec9330 QH |
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 | |
149d6335 RN |
336 | if (EFI_ERROR (Status)) {\r |
337 | Status = FatCleanupVolume (Volume, OFile, Status, NULL);\r | |
338 | }\r | |
339 | \r | |
b9ec9330 QH |
340 | FatReleaseLock ();\r |
341 | return Status;\r | |
342 | }\r | |
343 | \r | |
cae7420b DB |
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 | |
b9ec9330 QH |
359 | EFI_STATUS\r |
360 | EFIAPI\r | |
361 | FatRead (\r | |
dba03ba1 QH |
362 | IN EFI_FILE_PROTOCOL *FHand,\r |
363 | IN OUT UINTN *BufferSize,\r | |
364 | OUT VOID *Buffer\r | |
b9ec9330 | 365 | )\r |
cae7420b DB |
366 | {\r |
367 | return FatIFileAccess (FHand, ReadData, BufferSize, Buffer, NULL);\r | |
368 | }\r | |
b9ec9330 | 369 | \r |
cae7420b | 370 | /**\r |
b9ec9330 QH |
371 | \r |
372 | Get the file info.\r | |
373 | \r | |
cae7420b DB |
374 | @param FHand - The handle of the file.\r |
375 | @param Token - A pointer to the token associated with the transaction.\r | |
b9ec9330 | 376 | \r |
cae7420b DB |
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 | |
149d6335 | 381 | \r |
cae7420b | 382 | **/\r |
149d6335 RN |
383 | EFI_STATUS\r |
384 | EFIAPI\r | |
385 | FatReadEx (\r | |
386 | IN EFI_FILE_PROTOCOL *FHand,\r | |
387 | IN OUT EFI_FILE_IO_TOKEN *Token\r | |
388 | )\r | |
cae7420b DB |
389 | {\r |
390 | return FatIFileAccess (FHand, ReadData, &Token->BufferSize, Token->Buffer, Token);\r | |
391 | }\r | |
149d6335 | 392 | \r |
cae7420b | 393 | /**\r |
149d6335 | 394 | \r |
cae7420b | 395 | Write the content of buffer into files.\r |
149d6335 | 396 | \r |
cae7420b DB |
397 | @param FHand - The handle of the file.\r |
398 | @param BufferSize - Size of Buffer.\r | |
399 | @param Buffer - Buffer containing write data.\r | |
149d6335 | 400 | \r |
cae7420b DB |
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 | |
b9ec9330 | 408 | \r |
cae7420b | 409 | **/\r |
b9ec9330 QH |
410 | EFI_STATUS\r |
411 | EFIAPI\r | |
412 | FatWrite (\r | |
dba03ba1 QH |
413 | IN EFI_FILE_PROTOCOL *FHand,\r |
414 | IN OUT UINTN *BufferSize,\r | |
415 | IN VOID *Buffer\r | |
b9ec9330 | 416 | )\r |
cae7420b DB |
417 | {\r |
418 | return FatIFileAccess (FHand, WriteData, BufferSize, Buffer, NULL);\r | |
419 | }\r | |
b9ec9330 | 420 | \r |
cae7420b | 421 | /**\r |
b9ec9330 | 422 | \r |
cae7420b | 423 | Get the file info.\r |
b9ec9330 | 424 | \r |
cae7420b DB |
425 | @param FHand - The handle of the file.\r |
426 | @param Token - A pointer to the token associated with the transaction.\r | |
b9ec9330 | 427 | \r |
cae7420b DB |
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 | |
149d6335 | 432 | \r |
cae7420b | 433 | **/\r |
149d6335 RN |
434 | EFI_STATUS\r |
435 | EFIAPI\r | |
436 | FatWriteEx (\r | |
437 | IN EFI_FILE_PROTOCOL *FHand,\r | |
438 | IN OUT EFI_FILE_IO_TOKEN *Token\r | |
439 | )\r | |
cae7420b DB |
440 | {\r |
441 | return FatIFileAccess (FHand, WriteData, &Token->BufferSize, Token->Buffer, Token);\r | |
442 | }\r | |
149d6335 | 443 | \r |
cae7420b | 444 | /**\r |
149d6335 | 445 | \r |
cae7420b DB |
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 | |
149d6335 | 448 | \r |
cae7420b DB |
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 | |
149d6335 | 455 | \r |
cae7420b DB |
456 | @retval EFI_SUCCESS - Access the data successfully.\r |
457 | @return other - An error occurred when operating on the disk.\r | |
b9ec9330 | 458 | \r |
cae7420b | 459 | **/\r |
b9ec9330 QH |
460 | EFI_STATUS\r |
461 | FatAccessOFile (\r | |
462 | IN FAT_OFILE *OFile,\r | |
463 | IN IO_MODE IoMode,\r | |
464 | IN UINTN Position,\r | |
465 | IN OUT UINTN *DataBufferSize,\r | |
149d6335 RN |
466 | IN OUT UINT8 *UserBuffer,\r |
467 | IN FAT_TASK *Task\r | |
b9ec9330 | 468 | )\r |
b9ec9330 QH |
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 | |
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 | |
500 | //\r | |
501 | // Data was successfully accessed\r | |
502 | //\r | |
503 | Position += Len;\r | |
504 | UserBuffer += Len;\r | |
505 | BufferSize -= Len;\r | |
c1680e88 | 506 | if (IoMode == WriteData) {\r |
b9ec9330 QH |
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 | |
cae7420b | 522 | /**\r |
b9ec9330 QH |
523 | \r |
524 | Expand OFile by appending zero bytes at the end of OFile.\r | |
525 | \r | |
cae7420b DB |
526 | @param OFile - The open file.\r |
527 | @param ExpandedSize - The number of zero bytes appended at the end of the file.\r | |
b9ec9330 | 528 | \r |
cae7420b DB |
529 | @retval EFI_SUCCESS - The file is expanded successfully.\r |
530 | @return other - An error occurred when expanding file.\r | |
b9ec9330 | 531 | \r |
cae7420b DB |
532 | **/\r |
533 | EFI_STATUS\r | |
534 | FatExpandOFile (\r | |
535 | IN FAT_OFILE *OFile,\r | |
536 | IN UINT64 ExpandedSize\r | |
537 | )\r | |
b9ec9330 QH |
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 | |
cae7420b | 551 | /**\r |
b9ec9330 QH |
552 | \r |
553 | Write zero pool from the WritePos to the end of OFile.\r | |
554 | \r | |
cae7420b DB |
555 | @param OFile - The open file to write zero pool.\r |
556 | @param WritePos - The number of zero bytes written.\r | |
b9ec9330 | 557 | \r |
cae7420b DB |
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 | |
b9ec9330 | 561 | \r |
cae7420b DB |
562 | **/\r |
563 | EFI_STATUS\r | |
564 | FatWriteZeroPool (\r | |
565 | IN FAT_OFILE *OFile,\r | |
566 | IN UINTN WritePos\r | |
567 | )\r | |
b9ec9330 QH |
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 | |
c1680e88 | 595 | Status = FatAccessOFile (OFile, WriteData, WritePos, &WriteSize, ZeroBuffer, NULL);\r |
b9ec9330 QH |
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 | |
cae7420b | 607 | /**\r |
b9ec9330 QH |
608 | \r |
609 | Truncate the OFile to smaller file size.\r | |
610 | \r | |
cae7420b DB |
611 | @param OFile - The open file.\r |
612 | @param TruncatedSize - The new file size.\r | |
b9ec9330 | 613 | \r |
cae7420b DB |
614 | @retval EFI_SUCCESS - The file is truncated successfully.\r |
615 | @return other - An error occurred when truncating file.\r | |
b9ec9330 | 616 | \r |
cae7420b DB |
617 | **/\r |
618 | EFI_STATUS\r | |
619 | FatTruncateOFile (\r | |
620 | IN FAT_OFILE *OFile,\r | |
621 | IN UINTN TruncatedSize\r | |
622 | )\r | |
b9ec9330 QH |
623 | {\r |
624 | OFile->FileSize = TruncatedSize;\r | |
625 | return FatShrinkEof (OFile);\r | |
626 | }\r |