X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=FatPkg%2FEnhancedFatDxe%2FMisc.c;h=244b9dd921afb21cb7e1291cd06e3523bc42d492;hp=a06914fd190d3a0325bb45924c0503ead8f132c4;hb=e38f26a2f7f62b69fec2d84e83bcc4b45ddefdc1;hpb=b9ec93308b33bcb0bb37d6213a76c3fed0b5bc0b diff --git a/FatPkg/EnhancedFatDxe/Misc.c b/FatPkg/EnhancedFatDxe/Misc.c index a06914fd19..244b9dd921 100644 --- a/FatPkg/EnhancedFatDxe/Misc.c +++ b/FatPkg/EnhancedFatDxe/Misc.c @@ -1,7 +1,8 @@ -/*++ +/** @file + Miscellaneous functions. -Copyright (c) 2005, Intel Corporation -All rights reserved. This program and the accompanying materials are licensed and made available +Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -10,84 +11,323 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: +**/ - Misc.c +#include "Fat.h" +UINT8 mMonthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; -Abstract: +/** - Miscellaneous functions + Create the task -Revision History + @param IFile - The instance of the open file. + @param Token - A pointer to the token associated with the transaction. ---*/ + @return FAT_TASK * - Return the task instance. -#include "Fat.h" +**/ +FAT_TASK * +FatCreateTask ( + FAT_IFILE *IFile, + EFI_FILE_IO_TOKEN *Token + ) +{ + FAT_TASK *Task; + + Task = AllocateZeroPool (sizeof (*Task)); + if (Task != NULL) { + Task->Signature = FAT_TASK_SIGNATURE; + Task->IFile = IFile; + Task->FileIoToken = Token; + InitializeListHead (&Task->Subtasks); + InitializeListHead (&Task->Link); + } + return Task; +} + +/** + + Destroy the task. + + @param Task - The task to be destroyed. + +**/ +VOID +FatDestroyTask ( + FAT_TASK *Task + ) +{ + LIST_ENTRY *Link; + FAT_SUBTASK *Subtask; + + Link = GetFirstNode (&Task->Subtasks); + while (!IsNull (&Task->Subtasks, Link)) { + Subtask = CR (Link, FAT_SUBTASK, Link, FAT_SUBTASK_SIGNATURE); + Link = FatDestroySubtask (Subtask); + } + FreePool (Task); +} + +/** + + Wait all non-blocking requests complete. + + @param IFile - The instance of the open file. + +**/ +VOID +FatWaitNonblockingTask ( + FAT_IFILE *IFile + ) +{ + BOOLEAN TaskQueueEmpty; + + do { + EfiAcquireLock (&FatTaskLock); + TaskQueueEmpty = IsListEmpty (&IFile->Tasks); + EfiReleaseLock (&FatTaskLock); + } while (!TaskQueueEmpty); +} + +/** + + Remove the subtask from subtask list. + @param Subtask - The subtask to be removed. + + @return LIST_ENTRY * - The next node in the list. + +**/ +LIST_ENTRY * +FatDestroySubtask ( + FAT_SUBTASK *Subtask + ) +{ + LIST_ENTRY *Link; + + gBS->CloseEvent (Subtask->DiskIo2Token.Event); + + Link = RemoveEntryList (&Subtask->Link); + FreePool (Subtask); + + return Link; +} + +/** + + Execute the task. + + @param IFile - The instance of the open file. + @param Task - The task to be executed. + + @retval EFI_SUCCESS - The task was executed sucessfully. + @return other - An error occurred when executing the task. + +**/ EFI_STATUS -FatAccessVolumeDirty ( - IN FAT_VOLUME *Volume, - IN IO_MODE IoMode, - IN VOID *DirtyValue +FatQueueTask ( + IN FAT_IFILE *IFile, + IN FAT_TASK *Task ) -/*++ +{ + EFI_STATUS Status; + LIST_ENTRY *Link; + LIST_ENTRY *NextLink; + FAT_SUBTASK *Subtask; -Routine Description: + // + // Sometimes the Task doesn't contain any subtasks, signal the event directly. + // + if (IsListEmpty (&Task->Subtasks)) { + Task->FileIoToken->Status = EFI_SUCCESS; + gBS->SignalEvent (Task->FileIoToken->Event); + FreePool (Task); + return EFI_SUCCESS; + } - Set the volume as dirty or not + EfiAcquireLock (&FatTaskLock); + InsertTailList (&IFile->Tasks, &Task->Link); + EfiReleaseLock (&FatTaskLock); -Arguments: + Status = EFI_SUCCESS; + // + // Use NextLink to store the next link of the list, because Link might be remove from the + // doubly-linked list and get freed in the end of current loop. + // + // Also, list operation APIs like IsNull() and GetNextNode() are avoided during the loop, since + // they may check the validity of doubly-linked lists by traversing them. These APIs cannot + // handle list elements being removed during the traverse. + // + for ( Link = GetFirstNode (&Task->Subtasks), NextLink = GetNextNode (&Task->Subtasks, Link) + ; Link != &Task->Subtasks + ; Link = NextLink, NextLink = Link->ForwardLink + ) { + Subtask = CR (Link, FAT_SUBTASK, Link, FAT_SUBTASK_SIGNATURE); + if (Subtask->Write) { + + Status = IFile->OFile->Volume->DiskIo2->WriteDiskEx ( + IFile->OFile->Volume->DiskIo2, + IFile->OFile->Volume->MediaId, + Subtask->Offset, + &Subtask->DiskIo2Token, + Subtask->BufferSize, + Subtask->Buffer + ); + } else { + Status = IFile->OFile->Volume->DiskIo2->ReadDiskEx ( + IFile->OFile->Volume->DiskIo2, + IFile->OFile->Volume->MediaId, + Subtask->Offset, + &Subtask->DiskIo2Token, + Subtask->BufferSize, + Subtask->Buffer + ); + } + if (EFI_ERROR (Status)) { + break; + } + } + + if (EFI_ERROR (Status)) { + EfiAcquireLock (&FatTaskLock); + // + // Remove all the remaining subtasks when failure. + // We shouldn't remove all the tasks because the non-blocking requests have + // been submitted and cannot be canceled. + // + while (!IsNull (&Task->Subtasks, Link)) { + Subtask = CR (Link, FAT_SUBTASK, Link, FAT_SUBTASK_SIGNATURE); + Link = FatDestroySubtask (Subtask); + } - Volume - FAT file system volume. - IoMode - The access mode. - DirtyValue - Set the volume as dirty or not. + if (IsListEmpty (&Task->Subtasks)) { + RemoveEntryList (&Task->Link); + FreePool (Task); + } else { + // + // If one or more subtasks have been already submitted, set FileIoToken + // to NULL so that the callback won't signal the event. + // + Task->FileIoToken = NULL; + } -Returns: + EfiReleaseLock (&FatTaskLock); + } - EFI_SUCCESS - Set the new FAT entry value sucessfully. - other - An error occurred when operation the FAT entries. + return Status; +} ---*/ +/** + + Set the volume as dirty or not. + + @param Volume - FAT file system volume. + @param IoMode - The access mode. + @param DirtyValue - Set the volume as dirty or not. + + @retval EFI_SUCCESS - Set the new FAT entry value sucessfully. + @return other - An error occurred when operation the FAT entries. + +**/ +EFI_STATUS +FatAccessVolumeDirty ( + IN FAT_VOLUME *Volume, + IN IO_MODE IoMode, + IN VOID *DirtyValue + ) { UINTN WriteCount; WriteCount = Volume->FatEntrySize; - return FatDiskIo (Volume, IoMode, Volume->FatPos + WriteCount, WriteCount, DirtyValue); + return FatDiskIo (Volume, IoMode, Volume->FatPos + WriteCount, WriteCount, DirtyValue, NULL); } -EFI_STATUS -FatDiskIo ( - IN FAT_VOLUME *Volume, - IN IO_MODE IoMode, - IN UINT64 Offset, - IN UINTN BufferSize, - IN OUT VOID *Buffer +/** + Invoke a notification event. + + @param Event Event whose notification function is being invoked. + @param Context The pointer to the notification function's context, + which is implementation-dependent. + +**/ +VOID +EFIAPI +FatOnAccessComplete ( + IN EFI_EVENT Event, + IN VOID *Context ) -/*++ +{ + EFI_STATUS Status; + FAT_SUBTASK *Subtask; + FAT_TASK *Task; + + // + // Avoid someone in future breaks the below assumption. + // + ASSERT (EfiGetCurrentTpl () == FatTaskLock.Tpl); + + Subtask = (FAT_SUBTASK *) Context; + Task = Subtask->Task; + Status = Subtask->DiskIo2Token.TransactionStatus; + + ASSERT (Task->Signature == FAT_TASK_SIGNATURE); + ASSERT (Subtask->Signature == FAT_SUBTASK_SIGNATURE); + + // + // Remove the task unconditionally + // + FatDestroySubtask (Subtask); -Routine Description: + // + // Task->FileIoToken is NULL which means the task will be ignored (just recycle the subtask and task memory). + // + if (Task->FileIoToken != NULL) { + if (IsListEmpty (&Task->Subtasks) || EFI_ERROR (Status)) { + Task->FileIoToken->Status = Status; + gBS->SignalEvent (Task->FileIoToken->Event); + // + // Mark Task->FileIoToken to NULL so that the subtasks belonging to the task will be ignored. + // + Task->FileIoToken = NULL; + } + } - General disk access function + if (IsListEmpty (&Task->Subtasks)) { + RemoveEntryList (&Task->Link); + FreePool (Task); + } +} -Arguments: +/** - Volume - FAT file system volume. - IoMode - The access mode (disk read/write or cache access). - Offset - The starting byte offset to read from. - BufferSize - Size of Buffer. - Buffer - Buffer containing read data. + General disk access function. -Returns: + @param Volume - FAT file system volume. + @param IoMode - The access mode (disk read/write or cache access). + @param Offset - The starting byte offset to read from. + @param BufferSize - Size of Buffer. + @param Buffer - Buffer containing read data. + @param Task point to task instance. - EFI_SUCCESS - The operation is performed successfully. - EFI_VOLUME_CORRUPTED - The accesss is - Others - The status of read/write the disk + @retval EFI_SUCCESS - The operation is performed successfully. + @retval EFI_VOLUME_CORRUPTED - The accesss is + @return Others - The status of read/write the disk ---*/ +**/ +EFI_STATUS +FatDiskIo ( + IN FAT_VOLUME *Volume, + IN IO_MODE IoMode, + IN UINT64 Offset, + IN UINTN BufferSize, + IN OUT VOID *Buffer, + IN FAT_TASK *Task + ) { EFI_STATUS Status; EFI_DISK_IO_PROTOCOL *DiskIo; EFI_DISK_READ IoFunction; + FAT_SUBTASK *Subtask; // // Verify the IO is in devices range @@ -98,114 +338,112 @@ Returns: // // Access cache // - Status = FatAccessCache (Volume, CACHE_TYPE (IoMode), RAW_ACCESS (IoMode), Offset, BufferSize, Buffer); + Status = FatAccessCache (Volume, CACHE_TYPE (IoMode), RAW_ACCESS (IoMode), Offset, BufferSize, Buffer, Task); } else { // // Access disk directly // - DiskIo = Volume->DiskIo; - IoFunction = (IoMode == READ_DISK) ? DiskIo->ReadDisk : DiskIo->WriteDisk; - Status = IoFunction (DiskIo, Volume->MediaId, Offset, BufferSize, Buffer); + if (Task == NULL) { + // + // Blocking access + // + DiskIo = Volume->DiskIo; + IoFunction = (IoMode == ReadDisk) ? DiskIo->ReadDisk : DiskIo->WriteDisk; + Status = IoFunction (DiskIo, Volume->MediaId, Offset, BufferSize, Buffer); + } else { + // + // Non-blocking access + // + Subtask = AllocateZeroPool (sizeof (*Subtask)); + if (Subtask == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + Subtask->Signature = FAT_SUBTASK_SIGNATURE; + Subtask->Task = Task; + Subtask->Write = (BOOLEAN) (IoMode == WriteDisk); + Subtask->Offset = Offset; + Subtask->Buffer = Buffer; + Subtask->BufferSize = BufferSize; + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + FatOnAccessComplete, + Subtask, + &Subtask->DiskIo2Token.Event + ); + if (!EFI_ERROR (Status)) { + InsertTailList (&Task->Subtasks, &Subtask->Link); + } else { + FreePool (Subtask); + } + } + } } } if (EFI_ERROR (Status)) { Volume->DiskError = TRUE; - DEBUG ((EFI_D_INFO, "FatDiskIo: error %r\n", Status)); + DEBUG ((EFI_D_ERROR, "FatDiskIo: error %r\n", Status)); } return Status; } +/** + + Lock the volume. + +**/ VOID FatAcquireLock ( VOID ) -/*++ - -Routine Description: - - Lock the volume. - -Arguments: - - None. - -Returns: - - None. - ---*/ { EfiAcquireLock (&FatFsLock); } -BOOLEAN -FatIsLocked ( - VOID - ) -/*++ - -Routine Description: - - Get the locking status of the volume. - -Arguments: +/** - None. - -Returns: + Lock the volume. + If the lock is already in the acquired state, then EFI_ACCESS_DENIED is returned. + Otherwise, EFI_SUCCESS is returned. - TRUE - The volume is locked. - FALSE - The volume is not locked. + @retval EFI_SUCCESS - The volume is locked. + @retval EFI_ACCESS_DENIED - The volume could not be locked because it is already locked. ---*/ +**/ +EFI_STATUS +FatAcquireLockOrFail ( + VOID + ) { - return (BOOLEAN) (FatFsLock.Lock); + return EfiAcquireLockOrFail (&FatFsLock); } +/** + + Unlock the volume. + +**/ VOID FatReleaseLock ( VOID ) -/*++ - -Routine Description: - - Unlock the volume. - -Arguments: - - Null. - -Returns: - - None. - ---*/ { EfiReleaseLock (&FatFsLock); } -VOID -FatFreeDirEnt ( - IN FAT_DIRENT *DirEnt - ) -/*++ - -Routine Description: +/** Free directory entry. -Arguments: - - DirEnt - The directory entry to be freed. - -Returns: - - None. + @param DirEnt - The directory entry to be freed. ---*/ +**/ +VOID +FatFreeDirEnt ( + IN FAT_DIRENT *DirEnt + ) { if (DirEnt->FileString != NULL) { FreePool (DirEnt->FileString); @@ -214,25 +452,17 @@ Returns: FreePool (DirEnt); } -VOID -FatFreeVolume ( - IN FAT_VOLUME *Volume - ) -/*++ - -Routine Description: +/** Free volume structure (including the contents of directory cache and disk cache). -Arguments: + @param Volume - The volume structure to be freed. - Volume - The volume structure to be freed. - -Returns: - - None. - ---*/ +**/ +VOID +FatFreeVolume ( + IN FAT_VOLUME *Volume + ) { // // Free disk cache @@ -247,27 +477,19 @@ Returns: FreePool (Volume); } +/** + + Translate EFI time to FAT time. + + @param ETime - The time of EFI_TIME. + @param FTime - The time of FAT_DATE_TIME. + +**/ VOID FatEfiTimeToFatTime ( IN EFI_TIME *ETime, OUT FAT_DATE_TIME *FTime ) -/*++ - -Routine Description: - - Translate EFI time to FAT time. - -Arguments: - - ETime - The time of EFI_TIME. - FTime - The time of FAT_DATE_TIME. - -Returns: - - None. - ---*/ { // // ignores timezone info in source ETime @@ -287,27 +509,19 @@ Returns: FTime->Time.DoubleSecond = (UINT16) (ETime->Second / 2); } +/** + + Translate Fat time to EFI time. + + @param FTime - The time of FAT_DATE_TIME. + @param ETime - The time of EFI_TIME.. + +**/ VOID FatFatTimeToEfiTime ( IN FAT_DATE_TIME *FTime, OUT EFI_TIME *ETime ) -/*++ - -Routine Description: - - Translate Fat time to EFI time. - -Arguments: - - FTime - The time of FAT_DATE_TIME. - ETime - The time of EFI_TIME. - -Returns: - - None. - ---*/ { ETime->Year = (UINT16) (FTime->Date.Year + 1980); ETime->Month = (UINT8) FTime->Date.Month; @@ -320,53 +534,48 @@ Returns: ETime->Daylight = 0; } +/** + + Get Current FAT time. + + @param FatNow - Current FAT time. + +**/ VOID FatGetCurrentFatTime ( OUT FAT_DATE_TIME *FatNow ) -/*++ - -Routine Description: - - Get Current FAT time. +{ + EFI_STATUS Status; + EFI_TIME Now; -Arguments: + Status = gRT->GetTime (&Now, NULL); + if (!EFI_ERROR (Status)) { + FatEfiTimeToFatTime (&Now, FatNow); + } else { + ZeroMem (&Now, sizeof (EFI_TIME)); + Now.Year = 1980; + Now.Month = 1; + Now.Day = 1; + FatEfiTimeToFatTime (&Now, FatNow); + } +} - FatNow - Current FAT time. +/** -Returns: + Check whether a time is valid. - None. + @param Time - The time of EFI_TIME. ---*/ -{ - EFI_TIME Now; - gRT->GetTime (&Now, NULL); - FatEfiTimeToFatTime (&Now, FatNow); -} + @retval TRUE - The time is valid. + @retval FALSE - The time is not valid. +**/ BOOLEAN FatIsValidTime ( IN EFI_TIME *Time ) -/*++ - -Routine Description: - - Check whether a time is valid. - -Arguments: - - Time - The time of EFI_TIME. - -Returns: - - TRUE - The time is valid. - FALSE - The time is not valid. - ---*/ { - static UINT8 MonthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; UINTN Day; BOOLEAN ValidTime; @@ -393,7 +602,7 @@ Returns: // // Perform a more specific check of the day of the month // - Day = MonthDays[Time->Month - 1]; + Day = mMonthDays[Time->Month - 1]; if (Time->Month == 2 && IS_LEAP_YEAR (Time->Year)) { Day += 1; //