]>
git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/Misc.c
2 Miscellaneous functions.
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 UINT8 mMonthDays
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
23 @param IFile - The instance of the open file.
24 @param Token - A pointer to the token associated with the transaction.
26 @return FAT_TASK * - Return the task instance.
32 EFI_FILE_IO_TOKEN
*Token
37 Task
= AllocateZeroPool (sizeof (*Task
));
39 Task
->Signature
= FAT_TASK_SIGNATURE
;
41 Task
->FileIoToken
= Token
;
42 InitializeListHead (&Task
->Subtasks
);
43 InitializeListHead (&Task
->Link
);
52 @param Task - The task to be destroyed.
63 Link
= GetFirstNode (&Task
->Subtasks
);
64 while (!IsNull (&Task
->Subtasks
, Link
)) {
65 Subtask
= CR (Link
, FAT_SUBTASK
, Link
, FAT_SUBTASK_SIGNATURE
);
66 Link
= FatDestroySubtask (Subtask
);
73 Wait all non-blocking requests complete.
75 @param IFile - The instance of the open file.
79 FatWaitNonblockingTask (
83 BOOLEAN TaskQueueEmpty
;
86 EfiAcquireLock (&FatTaskLock
);
87 TaskQueueEmpty
= IsListEmpty (&IFile
->Tasks
);
88 EfiReleaseLock (&FatTaskLock
);
89 } while (!TaskQueueEmpty
);
94 Remove the subtask from subtask list.
96 @param Subtask - The subtask to be removed.
98 @return LIST_ENTRY * - The next node in the list.
108 gBS
->CloseEvent (Subtask
->DiskIo2Token
.Event
);
110 Link
= RemoveEntryList (&Subtask
->Link
);
120 @param IFile - The instance of the open file.
121 @param Task - The task to be executed.
123 @retval EFI_SUCCESS - The task was executed sucessfully.
124 @return other - An error occurred when executing the task.
135 LIST_ENTRY
*NextLink
;
136 FAT_SUBTASK
*Subtask
;
139 // Sometimes the Task doesn't contain any subtasks, signal the event directly.
141 if (IsListEmpty (&Task
->Subtasks
)) {
142 Task
->FileIoToken
->Status
= EFI_SUCCESS
;
143 gBS
->SignalEvent (Task
->FileIoToken
->Event
);
148 EfiAcquireLock (&FatTaskLock
);
149 InsertTailList (&IFile
->Tasks
, &Task
->Link
);
150 EfiReleaseLock (&FatTaskLock
);
152 Status
= EFI_SUCCESS
;
154 // Use NextLink to store the next link of the list, because Link might be remove from the
155 // doubly-linked list and get freed in the end of current loop.
157 // Also, list operation APIs like IsNull() and GetNextNode() are avoided during the loop, since
158 // they may check the validity of doubly-linked lists by traversing them. These APIs cannot
159 // handle list elements being removed during the traverse.
161 for ( Link
= GetFirstNode (&Task
->Subtasks
), NextLink
= GetNextNode (&Task
->Subtasks
, Link
)
162 ; Link
!= &Task
->Subtasks
163 ; Link
= NextLink
, NextLink
= Link
->ForwardLink
165 Subtask
= CR (Link
, FAT_SUBTASK
, Link
, FAT_SUBTASK_SIGNATURE
);
166 if (Subtask
->Write
) {
168 Status
= IFile
->OFile
->Volume
->DiskIo2
->WriteDiskEx (
169 IFile
->OFile
->Volume
->DiskIo2
,
170 IFile
->OFile
->Volume
->MediaId
,
172 &Subtask
->DiskIo2Token
,
177 Status
= IFile
->OFile
->Volume
->DiskIo2
->ReadDiskEx (
178 IFile
->OFile
->Volume
->DiskIo2
,
179 IFile
->OFile
->Volume
->MediaId
,
181 &Subtask
->DiskIo2Token
,
186 if (EFI_ERROR (Status
)) {
191 if (EFI_ERROR (Status
)) {
192 EfiAcquireLock (&FatTaskLock
);
194 // Remove all the remaining subtasks when failure.
195 // We shouldn't remove all the tasks because the non-blocking requests have
196 // been submitted and cannot be canceled.
198 while (!IsNull (&Task
->Subtasks
, Link
)) {
199 Subtask
= CR (Link
, FAT_SUBTASK
, Link
, FAT_SUBTASK_SIGNATURE
);
200 Link
= FatDestroySubtask (Subtask
);
203 if (IsListEmpty (&Task
->Subtasks
)) {
204 RemoveEntryList (&Task
->Link
);
208 // If one or more subtasks have been already submitted, set FileIoToken
209 // to NULL so that the callback won't signal the event.
211 Task
->FileIoToken
= NULL
;
214 EfiReleaseLock (&FatTaskLock
);
222 Set the volume as dirty or not.
224 @param Volume - FAT file system volume.
225 @param IoMode - The access mode.
226 @param DirtyValue - Set the volume as dirty or not.
228 @retval EFI_SUCCESS - Set the new FAT entry value sucessfully.
229 @return other - An error occurred when operation the FAT entries.
233 FatAccessVolumeDirty (
234 IN FAT_VOLUME
*Volume
,
241 WriteCount
= Volume
->FatEntrySize
;
242 return FatDiskIo (Volume
, IoMode
, Volume
->FatPos
+ WriteCount
, WriteCount
, DirtyValue
, NULL
);
246 Invoke a notification event.
248 @param Event Event whose notification function is being invoked.
249 @param Context The pointer to the notification function's context,
250 which is implementation-dependent.
255 FatOnAccessComplete (
261 FAT_SUBTASK
*Subtask
;
265 // Avoid someone in future breaks the below assumption.
267 ASSERT (EfiGetCurrentTpl () == FatTaskLock
.Tpl
);
269 Subtask
= (FAT_SUBTASK
*) Context
;
270 Task
= Subtask
->Task
;
271 Status
= Subtask
->DiskIo2Token
.TransactionStatus
;
273 ASSERT (Task
->Signature
== FAT_TASK_SIGNATURE
);
274 ASSERT (Subtask
->Signature
== FAT_SUBTASK_SIGNATURE
);
277 // Remove the task unconditionally
279 FatDestroySubtask (Subtask
);
282 // Task->FileIoToken is NULL which means the task will be ignored (just recycle the subtask and task memory).
284 if (Task
->FileIoToken
!= NULL
) {
285 if (IsListEmpty (&Task
->Subtasks
) || EFI_ERROR (Status
)) {
286 Task
->FileIoToken
->Status
= Status
;
287 gBS
->SignalEvent (Task
->FileIoToken
->Event
);
289 // Mark Task->FileIoToken to NULL so that the subtasks belonging to the task will be ignored.
291 Task
->FileIoToken
= NULL
;
295 if (IsListEmpty (&Task
->Subtasks
)) {
296 RemoveEntryList (&Task
->Link
);
303 General disk access function.
305 @param Volume - FAT file system volume.
306 @param IoMode - The access mode (disk read/write or cache access).
307 @param Offset - The starting byte offset to read from.
308 @param BufferSize - Size of Buffer.
309 @param Buffer - Buffer containing read data.
310 @param Task point to task instance.
312 @retval EFI_SUCCESS - The operation is performed successfully.
313 @retval EFI_VOLUME_CORRUPTED - The accesss is
314 @return Others - The status of read/write the disk
319 IN FAT_VOLUME
*Volume
,
328 EFI_DISK_IO_PROTOCOL
*DiskIo
;
329 EFI_DISK_READ IoFunction
;
330 FAT_SUBTASK
*Subtask
;
333 // Verify the IO is in devices range
335 Status
= EFI_VOLUME_CORRUPTED
;
336 if (Offset
+ BufferSize
<= Volume
->VolumeSize
) {
337 if (CACHE_ENABLED (IoMode
)) {
341 Status
= FatAccessCache (Volume
, CACHE_TYPE (IoMode
), RAW_ACCESS (IoMode
), Offset
, BufferSize
, Buffer
, Task
);
344 // Access disk directly
350 DiskIo
= Volume
->DiskIo
;
351 IoFunction
= (IoMode
== ReadDisk
) ? DiskIo
->ReadDisk
: DiskIo
->WriteDisk
;
352 Status
= IoFunction (DiskIo
, Volume
->MediaId
, Offset
, BufferSize
, Buffer
);
355 // Non-blocking access
357 Subtask
= AllocateZeroPool (sizeof (*Subtask
));
358 if (Subtask
== NULL
) {
359 Status
= EFI_OUT_OF_RESOURCES
;
361 Subtask
->Signature
= FAT_SUBTASK_SIGNATURE
;
362 Subtask
->Task
= Task
;
363 Subtask
->Write
= (BOOLEAN
) (IoMode
== WriteDisk
);
364 Subtask
->Offset
= Offset
;
365 Subtask
->Buffer
= Buffer
;
366 Subtask
->BufferSize
= BufferSize
;
367 Status
= gBS
->CreateEvent (
372 &Subtask
->DiskIo2Token
.Event
374 if (!EFI_ERROR (Status
)) {
375 InsertTailList (&Task
->Subtasks
, &Subtask
->Link
);
384 if (EFI_ERROR (Status
)) {
385 Volume
->DiskError
= TRUE
;
386 DEBUG ((EFI_D_ERROR
, "FatDiskIo: error %r\n", Status
));
402 EfiAcquireLock (&FatFsLock
);
408 If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned.
409 Otherwise, EFI_SUCCESS is returned.
411 @retval EFI_SUCCESS - The volume is locked.
412 @retval EFI_ACCESS_DENIED - The volume could not be locked because it is already locked.
416 FatAcquireLockOrFail (
420 return EfiAcquireLockOrFail (&FatFsLock
);
433 EfiReleaseLock (&FatFsLock
);
438 Free directory entry.
440 @param DirEnt - The directory entry to be freed.
445 IN FAT_DIRENT
*DirEnt
448 if (DirEnt
->FileString
!= NULL
) {
449 FreePool (DirEnt
->FileString
);
457 Free volume structure (including the contents of directory cache and disk cache).
459 @param Volume - The volume structure to be freed.
464 IN FAT_VOLUME
*Volume
470 if (Volume
->CacheBuffer
!= NULL
) {
471 FreePool (Volume
->CacheBuffer
);
474 // Free directory cache
476 FatCleanupODirCache (Volume
);
482 Translate EFI time to FAT time.
484 @param ETime - The time of EFI_TIME.
485 @param FTime - The time of FAT_DATE_TIME.
489 FatEfiTimeToFatTime (
491 OUT FAT_DATE_TIME
*FTime
495 // ignores timezone info in source ETime
497 if (ETime
->Year
> 1980) {
498 FTime
->Date
.Year
= (UINT16
) (ETime
->Year
- 1980);
501 if (ETime
->Year
>= 1980 + FAT_MAX_YEAR_FROM_1980
) {
502 FTime
->Date
.Year
= FAT_MAX_YEAR_FROM_1980
;
505 FTime
->Date
.Month
= ETime
->Month
;
506 FTime
->Date
.Day
= ETime
->Day
;
507 FTime
->Time
.Hour
= ETime
->Hour
;
508 FTime
->Time
.Minute
= ETime
->Minute
;
509 FTime
->Time
.DoubleSecond
= (UINT16
) (ETime
->Second
/ 2);
514 Translate Fat time to EFI time.
516 @param FTime - The time of FAT_DATE_TIME.
517 @param ETime - The time of EFI_TIME..
521 FatFatTimeToEfiTime (
522 IN FAT_DATE_TIME
*FTime
,
526 ETime
->Year
= (UINT16
) (FTime
->Date
.Year
+ 1980);
527 ETime
->Month
= (UINT8
) FTime
->Date
.Month
;
528 ETime
->Day
= (UINT8
) FTime
->Date
.Day
;
529 ETime
->Hour
= (UINT8
) FTime
->Time
.Hour
;
530 ETime
->Minute
= (UINT8
) FTime
->Time
.Minute
;
531 ETime
->Second
= (UINT8
) (FTime
->Time
.DoubleSecond
* 2);
532 ETime
->Nanosecond
= 0;
533 ETime
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
539 Get Current FAT time.
541 @param FatNow - Current FAT time.
545 FatGetCurrentFatTime (
546 OUT FAT_DATE_TIME
*FatNow
552 Status
= gRT
->GetTime (&Now
, NULL
);
553 if (!EFI_ERROR (Status
)) {
554 FatEfiTimeToFatTime (&Now
, FatNow
);
556 ZeroMem (&Now
, sizeof (EFI_TIME
));
560 FatEfiTimeToFatTime (&Now
, FatNow
);
566 Check whether a time is valid.
568 @param Time - The time of EFI_TIME.
570 @retval TRUE - The time is valid.
571 @retval FALSE - The time is not valid.
585 // Check the fields for range problems
586 // Fat can only support from 1980
588 if (Time
->Year
< 1980 ||
596 Time
->Nanosecond
> 999999999
603 // Perform a more specific check of the day of the month
605 Day
= mMonthDays
[Time
->Month
- 1];
606 if (Time
->Month
== 2 && IS_LEAP_YEAR (Time
->Year
)) {
609 // 1 extra day this month
612 if (Time
->Day
> Day
) {