diff options
Diffstat (limited to 'src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe')
6 files changed, 2012 insertions, 0 deletions
diff --git a/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/ComponentName.c b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/ComponentName.c new file mode 100644 index 00000000..b17b4a9c --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/ComponentName.c @@ -0,0 +1,183 @@ +/** @file + UEFI Component Name(2) protocol implementation for DiskIo driver. + +Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "DiskIo.h" + +// +// EFI Component Name Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gDiskIoComponentName = { + DiskIoComponentNameGetDriverName, + DiskIoComponentNameGetControllerName, + "eng" +}; + +// +// EFI Component Name 2 Protocol +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gDiskIoComponentName2 = { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) DiskIoComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) DiskIoComponentNameGetControllerName, + "en" +}; + +// +// Driver name table for DiskIo module. +// It is shared by the implementation of ComponentName & ComponentName2 Protocol. +// +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE mDiskIoDriverNameTable[] = { + { + "eng;en", + (CHAR16 *)L"Generic Disk I/O Driver" + }, + { + NULL, + NULL + } +}; + + + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param DriverName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DiskIoComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + mDiskIoDriverNameTable, + DriverName, + (BOOLEAN)(This == &gDiskIoComponentName) + ); +} + + + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param ControllerHandle[in] The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param ChildHandle[in] The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param ControllerName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DiskIoComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + return EFI_UNSUPPORTED; +} diff --git a/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIo.c b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIo.c new file mode 100644 index 00000000..311b9b64 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIo.c @@ -0,0 +1,1262 @@ +/** @file + DiskIo driver that lays on every BlockIo protocol in the system. + DiskIo converts a block oriented device to a byte oriented device. + + Disk access may have to handle unaligned request about sector boundaries. + There are three cases: + UnderRun - The first byte is not on a sector boundary or the read request is + less than a sector in length. + Aligned - A read of N contiguous sectors. + OverRun - The last byte is not on a sector boundary. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "DiskIo.h" + +// +// Driver binding protocol implementation for DiskIo driver. +// +EFI_DRIVER_BINDING_PROTOCOL gDiskIoDriverBinding = { + DiskIoDriverBindingSupported, + DiskIoDriverBindingStart, + DiskIoDriverBindingStop, + 0xa, + NULL, + NULL +}; + +// +// Template for DiskIo private data structure. +// The pointer to BlockIo protocol interface is assigned dynamically. +// +DISK_IO_PRIVATE_DATA gDiskIoPrivateDataTemplate = { + DISK_IO_PRIVATE_DATA_SIGNATURE, + { + EFI_DISK_IO_PROTOCOL_REVISION, + DiskIoReadDisk, + DiskIoWriteDisk + }, + { + EFI_DISK_IO2_PROTOCOL_REVISION, + DiskIo2Cancel, + DiskIo2ReadDiskEx, + DiskIo2WriteDiskEx, + DiskIo2FlushDiskEx + } +}; + +/** + Test to see if this driver supports ControllerHandle. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to test + @param RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +DiskIoDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + + // + // Open the IO Abstraction(s) needed to perform the supported test. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiBlockIoProtocolGuid, + (VOID **) &BlockIo, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Close the I/O Abstraction(s) used to perform the supported test. + // + gBS->CloseProtocol ( + ControllerHandle, + &gEfiBlockIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + return EFI_SUCCESS; +} + + +/** + Start this driver on ControllerHandle by opening a Block IO protocol and + installing a Disk IO protocol on ControllerHandle. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to bind driver to + @param RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +DiskIoDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ) +{ + EFI_STATUS Status; + DISK_IO_PRIVATE_DATA *Instance; + EFI_TPL OldTpl; + + Instance = NULL; + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + + // + // Connect to the Block IO and Block IO2 interface on ControllerHandle. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiBlockIoProtocolGuid, + (VOID **) &gDiskIoPrivateDataTemplate.BlockIo, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + goto ErrorExit1; + } + + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiBlockIo2ProtocolGuid, + (VOID **) &gDiskIoPrivateDataTemplate.BlockIo2, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + gDiskIoPrivateDataTemplate.BlockIo2 = NULL; + } + + // + // Initialize the Disk IO device instance. + // + Instance = AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA), &gDiskIoPrivateDataTemplate); + if (Instance == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ErrorExit; + } + + // + // The BlockSize and IoAlign of BlockIo and BlockIo2 should equal. + // + ASSERT ((Instance->BlockIo2 == NULL) || + ((Instance->BlockIo->Media->IoAlign == Instance->BlockIo2->Media->IoAlign) && + (Instance->BlockIo->Media->BlockSize == Instance->BlockIo2->Media->BlockSize) + )); + + InitializeListHead (&Instance->TaskQueue); + EfiInitializeLock (&Instance->TaskQueueLock, TPL_NOTIFY); + Instance->SharedWorkingBuffer = AllocateAlignedPages ( + EFI_SIZE_TO_PAGES (PcdGet32 (PcdDiskIoDataBufferBlockNum) * Instance->BlockIo->Media->BlockSize), + Instance->BlockIo->Media->IoAlign + ); + if (Instance->SharedWorkingBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ErrorExit; + } + + // + // Install protocol interfaces for the Disk IO device. + // + if (Instance->BlockIo2 != NULL) { + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiDiskIoProtocolGuid, &Instance->DiskIo, + &gEfiDiskIo2ProtocolGuid, &Instance->DiskIo2, + NULL + ); + } else { + Status = gBS->InstallMultipleProtocolInterfaces ( + &ControllerHandle, + &gEfiDiskIoProtocolGuid, &Instance->DiskIo, + NULL + ); + } + +ErrorExit: + if (EFI_ERROR (Status)) { + if (Instance != NULL && Instance->SharedWorkingBuffer != NULL) { + FreeAlignedPages ( + Instance->SharedWorkingBuffer, + EFI_SIZE_TO_PAGES (PcdGet32 (PcdDiskIoDataBufferBlockNum) * Instance->BlockIo->Media->BlockSize) + ); + } + + if (Instance != NULL) { + FreePool (Instance); + } + + gBS->CloseProtocol ( + ControllerHandle, + &gEfiBlockIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + } + +ErrorExit1: + gBS->RestoreTPL (OldTpl); + return Status; +} + +/** + Stop this driver on ControllerHandle by removing Disk IO protocol and closing + the Block IO protocol on ControllerHandle. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to stop driver on + @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +DiskIoDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + EFI_DISK_IO_PROTOCOL *DiskIo; + EFI_DISK_IO2_PROTOCOL *DiskIo2; + DISK_IO_PRIVATE_DATA *Instance; + BOOLEAN AllTaskDone; + + // + // Get our context back. + // + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDiskIoProtocolGuid, + (VOID **) &DiskIo, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + Status = gBS->OpenProtocol ( + ControllerHandle, + &gEfiDiskIo2ProtocolGuid, + (VOID **) &DiskIo2, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + DiskIo2 = NULL; + } + + Instance = DISK_IO_PRIVATE_DATA_FROM_DISK_IO (DiskIo); + + if (DiskIo2 != NULL) { + // + // Call BlockIo2::Reset() to terminate any in-flight non-blocking I/O requests + // + ASSERT (Instance->BlockIo2 != NULL); + Status = Instance->BlockIo2->Reset (Instance->BlockIo2, FALSE); + if (EFI_ERROR (Status)) { + return Status; + } + Status = gBS->UninstallMultipleProtocolInterfaces ( + ControllerHandle, + &gEfiDiskIoProtocolGuid, &Instance->DiskIo, + &gEfiDiskIo2ProtocolGuid, &Instance->DiskIo2, + NULL + ); + } else { + Status = gBS->UninstallMultipleProtocolInterfaces ( + ControllerHandle, + &gEfiDiskIoProtocolGuid, &Instance->DiskIo, + NULL + ); + } + if (!EFI_ERROR (Status)) { + + do { + EfiAcquireLock (&Instance->TaskQueueLock); + AllTaskDone = IsListEmpty (&Instance->TaskQueue); + EfiReleaseLock (&Instance->TaskQueueLock); + } while (!AllTaskDone); + + FreeAlignedPages ( + Instance->SharedWorkingBuffer, + EFI_SIZE_TO_PAGES (PcdGet32 (PcdDiskIoDataBufferBlockNum) * Instance->BlockIo->Media->BlockSize) + ); + + Status = gBS->CloseProtocol ( + ControllerHandle, + &gEfiBlockIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + ASSERT_EFI_ERROR (Status); + if (DiskIo2 != NULL) { + Status = gBS->CloseProtocol ( + ControllerHandle, + &gEfiBlockIo2ProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + ASSERT_EFI_ERROR (Status); + } + + FreePool (Instance); + } + + return Status; +} + + +/** + Destroy the sub task. + + @param Instance Pointer to the DISK_IO_PRIVATE_DATA. + @param Subtask Subtask. + + @return LIST_ENTRY * Pointer to the next link of subtask. +**/ +LIST_ENTRY * +DiskIoDestroySubtask ( + IN DISK_IO_PRIVATE_DATA *Instance, + IN DISK_IO_SUBTASK *Subtask + ) +{ + LIST_ENTRY *Link; + + if (Subtask->Task != NULL) { + EfiAcquireLock (&Subtask->Task->SubtasksLock); + } + Link = RemoveEntryList (&Subtask->Link); + if (Subtask->Task != NULL) { + EfiReleaseLock (&Subtask->Task->SubtasksLock); + } + + if (!Subtask->Blocking) { + if (Subtask->WorkingBuffer != NULL) { + FreeAlignedPages ( + Subtask->WorkingBuffer, + Subtask->Length < Instance->BlockIo->Media->BlockSize + ? EFI_SIZE_TO_PAGES (Instance->BlockIo->Media->BlockSize) + : EFI_SIZE_TO_PAGES (Subtask->Length) + ); + } + if (Subtask->BlockIo2Token.Event != NULL) { + gBS->CloseEvent (Subtask->BlockIo2Token.Event); + } + } + FreePool (Subtask); + + return Link; +} + +/** + The callback for the BlockIo2 ReadBlocksEx/WriteBlocksEx. + @param Event Event whose notification function is being invoked. + @param Context The pointer to the notification function's context, + which points to the DISK_IO_SUBTASK instance. +**/ +VOID +EFIAPI +DiskIo2OnReadWriteComplete ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + DISK_IO_SUBTASK *Subtask; + DISK_IO2_TASK *Task; + EFI_STATUS TransactionStatus; + DISK_IO_PRIVATE_DATA *Instance; + + Subtask = (DISK_IO_SUBTASK *) Context; + TransactionStatus = Subtask->BlockIo2Token.TransactionStatus; + Task = Subtask->Task; + Instance = Task->Instance; + + ASSERT (Subtask->Signature == DISK_IO_SUBTASK_SIGNATURE); + ASSERT (Instance->Signature == DISK_IO_PRIVATE_DATA_SIGNATURE); + ASSERT (Task->Signature == DISK_IO2_TASK_SIGNATURE); + + if ((Subtask->WorkingBuffer != NULL) && !EFI_ERROR (TransactionStatus) && + (Task->Token != NULL) && !Subtask->Write + ) { + CopyMem (Subtask->Buffer, Subtask->WorkingBuffer + Subtask->Offset, Subtask->Length); + } + + DiskIoDestroySubtask (Instance, Subtask); + + if (EFI_ERROR (TransactionStatus) || IsListEmpty (&Task->Subtasks)) { + if (Task->Token != NULL) { + // + // Signal error status once the subtask is failed. + // Or signal the last status once the last subtask is finished. + // + Task->Token->TransactionStatus = TransactionStatus; + gBS->SignalEvent (Task->Token->Event); + + // + // Mark token to NULL indicating the Task is a dead task. + // + Task->Token = NULL; + } + } +} + +/** + Create the subtask. + + @param Write TRUE: Write request; FALSE: Read request. + @param Lba The starting logical block address to read from on the device. + @param Offset The starting byte offset to read from the LBA. + @param Length The number of bytes to read from the device. + @param WorkingBuffer The aligned buffer to hold the data for reading or writing. + @param Buffer The buffer to hold the data for reading or writing. + @param Blocking TRUE: Blocking request; FALSE: Non-blocking request. + + @return A pointer to the created subtask. +**/ +DISK_IO_SUBTASK * +DiskIoCreateSubtask ( + IN BOOLEAN Write, + IN UINT64 Lba, + IN UINT32 Offset, + IN UINTN Length, + IN VOID *WorkingBuffer, OPTIONAL + IN VOID *Buffer, + IN BOOLEAN Blocking + ) +{ + DISK_IO_SUBTASK *Subtask; + EFI_STATUS Status; + + Subtask = AllocateZeroPool (sizeof (DISK_IO_SUBTASK)); + if (Subtask == NULL) { + return NULL; + } + Subtask->Signature = DISK_IO_SUBTASK_SIGNATURE; + Subtask->Write = Write; + Subtask->Lba = Lba; + Subtask->Offset = Offset; + Subtask->Length = Length; + Subtask->WorkingBuffer = WorkingBuffer; + Subtask->Buffer = Buffer; + Subtask->Blocking = Blocking; + if (!Blocking) { + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + DiskIo2OnReadWriteComplete, + Subtask, + &Subtask->BlockIo2Token.Event + ); + if (EFI_ERROR (Status)) { + FreePool (Subtask); + return NULL; + } + } + DEBUG (( + EFI_D_BLKIO, + " %c:Lba/Offset/Length/WorkingBuffer/Buffer = %016lx/%08x/%08x/%08x/%08x\n", + Write ? 'W': 'R', Lba, Offset, Length, WorkingBuffer, Buffer + )); + + return Subtask; +} + +/** + Create the subtask list. + + @param Instance Pointer to the DISK_IO_PRIVATE_DATA. + @param Write TRUE: Write request; FALSE: Read request. + @param Offset The starting byte offset to read from the device. + @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device. + @param Buffer A pointer to the buffer for the data. + @param Blocking TRUE: Blocking request; FALSE: Non-blocking request. + @param SharedWorkingBuffer The aligned buffer to hold the data for reading or writing. + @param Subtasks The subtask list header. + + @retval TRUE The subtask list is created successfully. + @retval FALSE The subtask list is not created. +**/ +BOOLEAN +DiskIoCreateSubtaskList ( + IN DISK_IO_PRIVATE_DATA *Instance, + IN BOOLEAN Write, + IN UINT64 Offset, + IN UINTN BufferSize, + IN VOID *Buffer, + IN BOOLEAN Blocking, + IN VOID *SharedWorkingBuffer, + IN OUT LIST_ENTRY *Subtasks + ) +{ + UINT32 BlockSize; + UINT32 IoAlign; + UINT64 Lba; + UINT64 OverRunLba; + UINT32 UnderRun; + UINT32 OverRun; + UINT8 *BufferPtr; + UINTN Length; + UINTN DataBufferSize; + DISK_IO_SUBTASK *Subtask; + VOID *WorkingBuffer; + LIST_ENTRY *Link; + + DEBUG ((EFI_D_BLKIO, "DiskIo: Create subtasks for task: Offset/BufferSize/Buffer = %016lx/%08x/%08x\n", Offset, BufferSize, Buffer)); + + BlockSize = Instance->BlockIo->Media->BlockSize; + IoAlign = Instance->BlockIo->Media->IoAlign; + if (IoAlign == 0) { + IoAlign = 1; + } + + Lba = DivU64x32Remainder (Offset, BlockSize, &UnderRun); + BufferPtr = (UINT8 *) Buffer; + + // + // Special handling for zero BufferSize + // + if (BufferSize == 0) { + Subtask = DiskIoCreateSubtask (Write, Lba, UnderRun, 0, NULL, BufferPtr, Blocking); + if (Subtask == NULL) { + goto Done; + } + InsertTailList (Subtasks, &Subtask->Link); + return TRUE; + } + + if (UnderRun != 0) { + Length = MIN (BlockSize - UnderRun, BufferSize); + if (Blocking) { + WorkingBuffer = SharedWorkingBuffer; + } else { + WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BlockSize), IoAlign); + if (WorkingBuffer == NULL) { + goto Done; + } + } + if (Write) { + // + // A half write operation can be splitted to a blocking block-read and half write operation + // This can simplify the sub task processing logic + // + Subtask = DiskIoCreateSubtask (FALSE, Lba, 0, BlockSize, NULL, WorkingBuffer, TRUE); + if (Subtask == NULL) { + goto Done; + } + InsertTailList (Subtasks, &Subtask->Link); + } + + Subtask = DiskIoCreateSubtask (Write, Lba, UnderRun, Length, WorkingBuffer, BufferPtr, Blocking); + if (Subtask == NULL) { + goto Done; + } + InsertTailList (Subtasks, &Subtask->Link); + + BufferPtr += Length; + Offset += Length; + BufferSize -= Length; + Lba ++; + } + + OverRunLba = Lba + DivU64x32Remainder (BufferSize, BlockSize, &OverRun); + BufferSize -= OverRun; + + if (OverRun != 0) { + if (Blocking) { + WorkingBuffer = SharedWorkingBuffer; + } else { + WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BlockSize), IoAlign); + if (WorkingBuffer == NULL) { + goto Done; + } + } + if (Write) { + // + // A half write operation can be splitted to a blocking block-read and half write operation + // This can simplify the sub task processing logic + // + Subtask = DiskIoCreateSubtask (FALSE, OverRunLba, 0, BlockSize, NULL, WorkingBuffer, TRUE); + if (Subtask == NULL) { + goto Done; + } + InsertTailList (Subtasks, &Subtask->Link); + } + + Subtask = DiskIoCreateSubtask (Write, OverRunLba, 0, OverRun, WorkingBuffer, BufferPtr + BufferSize, Blocking); + if (Subtask == NULL) { + goto Done; + } + InsertTailList (Subtasks, &Subtask->Link); + } + + if (OverRunLba > Lba) { + // + // If the DiskIo maps directly to a BlockIo device do the read. + // + if (ALIGN_POINTER (BufferPtr, IoAlign) == BufferPtr) { + Subtask = DiskIoCreateSubtask (Write, Lba, 0, BufferSize, NULL, BufferPtr, Blocking); + if (Subtask == NULL) { + goto Done; + } + InsertTailList (Subtasks, &Subtask->Link); + + BufferPtr += BufferSize; + Offset += BufferSize; + BufferSize -= BufferSize; + + } else { + if (Blocking) { + // + // Use the allocated buffer instead of the original buffer + // to avoid alignment issue. + // + for (; Lba < OverRunLba; Lba += PcdGet32 (PcdDiskIoDataBufferBlockNum)) { + DataBufferSize = MIN (BufferSize, PcdGet32 (PcdDiskIoDataBufferBlockNum) * BlockSize); + + Subtask = DiskIoCreateSubtask (Write, Lba, 0, DataBufferSize, SharedWorkingBuffer, BufferPtr, Blocking); + if (Subtask == NULL) { + goto Done; + } + InsertTailList (Subtasks, &Subtask->Link); + + BufferPtr += DataBufferSize; + Offset += DataBufferSize; + BufferSize -= DataBufferSize; + } + } else { + WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), IoAlign); + if (WorkingBuffer == NULL) { + // + // If there is not enough memory, downgrade to blocking access + // + DEBUG ((EFI_D_VERBOSE, "DiskIo: No enough memory so downgrade to blocking access\n")); + if (!DiskIoCreateSubtaskList (Instance, Write, Offset, BufferSize, BufferPtr, TRUE, SharedWorkingBuffer, Subtasks)) { + goto Done; + } + } else { + Subtask = DiskIoCreateSubtask (Write, Lba, 0, BufferSize, WorkingBuffer, BufferPtr, Blocking); + if (Subtask == NULL) { + goto Done; + } + InsertTailList (Subtasks, &Subtask->Link); + } + + BufferPtr += BufferSize; + Offset += BufferSize; + BufferSize -= BufferSize; + } + } + } + + ASSERT (BufferSize == 0); + + return TRUE; + +Done: + // + // Remove all the subtasks. + // + for (Link = GetFirstNode (Subtasks); !IsNull (Subtasks, Link); ) { + Subtask = CR (Link, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE); + Link = DiskIoDestroySubtask (Instance, Subtask); + } + return FALSE; +} + +/** + Terminate outstanding asynchronous requests to a device. + + @param This Indicates a pointer to the calling context. + + @retval EFI_SUCCESS All outstanding requests were successfully terminated. + @retval EFI_DEVICE_ERROR The device reported an error while performing the cancel + operation. +**/ +EFI_STATUS +EFIAPI +DiskIo2Cancel ( + IN EFI_DISK_IO2_PROTOCOL *This + ) +{ + DISK_IO_PRIVATE_DATA *Instance; + DISK_IO2_TASK *Task; + LIST_ENTRY *Link; + + Instance = DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This); + + EfiAcquireLock (&Instance->TaskQueueLock); + + for (Link = GetFirstNode (&Instance->TaskQueue) + ; !IsNull (&Instance->TaskQueue, Link) + ; Link = GetNextNode (&Instance->TaskQueue, Link) + ) { + Task = CR (Link, DISK_IO2_TASK, Link, DISK_IO2_TASK_SIGNATURE); + + if (Task->Token != NULL) { + Task->Token->TransactionStatus = EFI_ABORTED; + gBS->SignalEvent (Task->Token->Event); + // + // Set Token to NULL so that the further BlockIo2 responses will be ignored + // + Task->Token = NULL; + } + } + + EfiReleaseLock (&Instance->TaskQueueLock); + + return EFI_SUCCESS; +} + +/** + Remove the completed tasks from Instance->TaskQueue. Completed tasks are those who don't have any subtasks. + + @param Instance Pointer to the DISK_IO_PRIVATE_DATA. + + @retval TRUE The Instance->TaskQueue is empty after the completed tasks are removed. + @retval FALSE The Instance->TaskQueue is not empty after the completed tasks are removed. +**/ +BOOLEAN +DiskIo2RemoveCompletedTask ( + IN DISK_IO_PRIVATE_DATA *Instance + ) +{ + BOOLEAN QueueEmpty; + LIST_ENTRY *Link; + DISK_IO2_TASK *Task; + + QueueEmpty = TRUE; + + EfiAcquireLock (&Instance->TaskQueueLock); + for (Link = GetFirstNode (&Instance->TaskQueue); !IsNull (&Instance->TaskQueue, Link); ) { + Task = CR (Link, DISK_IO2_TASK, Link, DISK_IO2_TASK_SIGNATURE); + if (IsListEmpty (&Task->Subtasks)) { + Link = RemoveEntryList (&Task->Link); + ASSERT (Task->Token == NULL); + FreePool (Task); + } else { + Link = GetNextNode (&Instance->TaskQueue, Link); + QueueEmpty = FALSE; + } + } + EfiReleaseLock (&Instance->TaskQueueLock); + + return QueueEmpty; +} + +/** + Common routine to access the disk. + + @param Instance Pointer to the DISK_IO_PRIVATE_DATA. + @param Write TRUE: Write operation; FALSE: Read operation. + @param MediaId ID of the medium to access. + @param Offset The starting byte offset on the logical block I/O device to access. + @param Token A pointer to the token associated with the transaction. + If this field is NULL, synchronous/blocking IO is performed. + @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device. + @param Buffer A pointer to the destination buffer for the data. + The caller is responsible either having implicit or explicit ownership of the buffer. +**/ +EFI_STATUS +DiskIo2ReadWriteDisk ( + IN DISK_IO_PRIVATE_DATA *Instance, + IN BOOLEAN Write, + IN UINT32 MediaId, + IN UINT64 Offset, + IN EFI_DISK_IO2_TOKEN *Token, + IN UINTN BufferSize, + IN UINT8 *Buffer + ) +{ + EFI_STATUS Status; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + EFI_BLOCK_IO2_PROTOCOL *BlockIo2; + EFI_BLOCK_IO_MEDIA *Media; + LIST_ENTRY *Link; + LIST_ENTRY *NextLink; + LIST_ENTRY Subtasks; + DISK_IO_SUBTASK *Subtask; + DISK_IO2_TASK *Task; + EFI_TPL OldTpl; + BOOLEAN Blocking; + BOOLEAN SubtaskBlocking; + LIST_ENTRY *SubtasksPtr; + + Task = NULL; + BlockIo = Instance->BlockIo; + BlockIo2 = Instance->BlockIo2; + Media = BlockIo->Media; + Status = EFI_SUCCESS; + Blocking = (BOOLEAN) ((Token == NULL) || (Token->Event == NULL)); + + if (Blocking) { + // + // Wait till pending async task is completed. + // + while (!DiskIo2RemoveCompletedTask (Instance)); + + SubtasksPtr = &Subtasks; + } else { + DiskIo2RemoveCompletedTask (Instance); + Task = AllocatePool (sizeof (DISK_IO2_TASK)); + if (Task == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + EfiAcquireLock (&Instance->TaskQueueLock); + InsertTailList (&Instance->TaskQueue, &Task->Link); + EfiReleaseLock (&Instance->TaskQueueLock); + + Task->Signature = DISK_IO2_TASK_SIGNATURE; + Task->Instance = Instance; + Task->Token = Token; + EfiInitializeLock (&Task->SubtasksLock, TPL_NOTIFY); + + SubtasksPtr = &Task->Subtasks; + } + + InitializeListHead (SubtasksPtr); + if (!DiskIoCreateSubtaskList (Instance, Write, Offset, BufferSize, Buffer, Blocking, Instance->SharedWorkingBuffer, SubtasksPtr)) { + if (Task != NULL) { + FreePool (Task); + } + return EFI_OUT_OF_RESOURCES; + } + ASSERT (!IsListEmpty (SubtasksPtr)); + + OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + for ( Link = GetFirstNode (SubtasksPtr), NextLink = GetNextNode (SubtasksPtr, Link) + ; !IsNull (SubtasksPtr, Link) + ; Link = NextLink, NextLink = GetNextNode (SubtasksPtr, NextLink) + ) { + Subtask = CR (Link, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE); + Subtask->Task = Task; + SubtaskBlocking = Subtask->Blocking; + + ASSERT ((Subtask->Length % Media->BlockSize == 0) || (Subtask->Length < Media->BlockSize)); + + if (Subtask->Write) { + // + // Write + // + if (Subtask->WorkingBuffer != NULL) { + // + // A sub task before this one should be a block read operation, causing the WorkingBuffer filled with the entire one block data. + // + CopyMem (Subtask->WorkingBuffer + Subtask->Offset, Subtask->Buffer, Subtask->Length); + } + + if (SubtaskBlocking) { + Status = BlockIo->WriteBlocks ( + BlockIo, + MediaId, + Subtask->Lba, + (Subtask->Length % Media->BlockSize == 0) ? Subtask->Length : Media->BlockSize, + (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer + ); + } else { + Status = BlockIo2->WriteBlocksEx ( + BlockIo2, + MediaId, + Subtask->Lba, + &Subtask->BlockIo2Token, + (Subtask->Length % Media->BlockSize == 0) ? Subtask->Length : Media->BlockSize, + (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer + ); + } + + } else { + // + // Read + // + if (SubtaskBlocking) { + Status = BlockIo->ReadBlocks ( + BlockIo, + MediaId, + Subtask->Lba, + (Subtask->Length % Media->BlockSize == 0) ? Subtask->Length : Media->BlockSize, + (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer + ); + if (!EFI_ERROR (Status) && (Subtask->WorkingBuffer != NULL)) { + CopyMem (Subtask->Buffer, Subtask->WorkingBuffer + Subtask->Offset, Subtask->Length); + } + } else { + Status = BlockIo2->ReadBlocksEx ( + BlockIo2, + MediaId, + Subtask->Lba, + &Subtask->BlockIo2Token, + (Subtask->Length % Media->BlockSize == 0) ? Subtask->Length : Media->BlockSize, + (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer + ); + } + } + + if (SubtaskBlocking || EFI_ERROR (Status)) { + // + // Make sure the subtask list only contains non-blocking subtasks. + // Remove failed non-blocking subtasks as well because the callback won't be called. + // + DiskIoDestroySubtask (Instance, Subtask); + } + + if (EFI_ERROR (Status)) { + break; + } + } + + gBS->RaiseTPL (TPL_NOTIFY); + + // + // 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. + // + if (EFI_ERROR (Status)) { + while (!IsNull (SubtasksPtr, NextLink)) { + Subtask = CR (NextLink, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE); + NextLink = DiskIoDestroySubtask (Instance, Subtask); + } + } + + // + // It's possible that the non-blocking subtasks finish before raising TPL to NOTIFY, + // so the subtasks list might be empty at this point. + // + if (!Blocking && IsListEmpty (SubtasksPtr)) { + EfiAcquireLock (&Instance->TaskQueueLock); + RemoveEntryList (&Task->Link); + EfiReleaseLock (&Instance->TaskQueueLock); + + if (!EFI_ERROR (Status) && (Task->Token != NULL)) { + // + // Task->Token should be set to NULL by the DiskIo2OnReadWriteComplete + // It it's not, that means the non-blocking request was downgraded to blocking request. + // + DEBUG ((EFI_D_VERBOSE, "DiskIo: Non-blocking request was downgraded to blocking request, signal event directly.\n")); + Task->Token->TransactionStatus = Status; + gBS->SignalEvent (Task->Token->Event); + } + + FreePool (Task); + } + + gBS->RestoreTPL (OldTpl); + + return Status; +} + +/** + Reads a specified number of bytes from a device. + + @param This Indicates a pointer to the calling context. + @param MediaId ID of the medium to be read. + @param Offset The starting byte offset on the logical block I/O device to read from. + @param Token A pointer to the token associated with the transaction. + If this field is NULL, synchronous/blocking IO is performed. + @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device. + @param Buffer A pointer to the destination buffer for the data. + The caller is responsible either having implicit or explicit ownership of the buffer. + + @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was read correctly from the device. + If Event is not NULL (asynchronous I/O): The request was successfully queued for processing. + Event will be signaled upon completion. + @retval EFI_DEVICE_ERROR The device reported an error while performing the write. + @retval EFI_NO_MEDIA There is no medium in the device. + @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium. + @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not valid for the device. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +DiskIo2ReadDiskEx ( + IN EFI_DISK_IO2_PROTOCOL *This, + IN UINT32 MediaId, + IN UINT64 Offset, + IN OUT EFI_DISK_IO2_TOKEN *Token, + IN UINTN BufferSize, + OUT VOID *Buffer + ) +{ + return DiskIo2ReadWriteDisk ( + DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This), + FALSE, MediaId, Offset, Token, BufferSize, (UINT8 *) Buffer + ); +} + +/** + Writes a specified number of bytes to a device. + + @param This Indicates a pointer to the calling context. + @param MediaId ID of the medium to be written. + @param Offset The starting byte offset on the logical block I/O device to write to. + @param Token A pointer to the token associated with the transaction. + If this field is NULL, synchronous/blocking IO is performed. + @param BufferSize The size in bytes of Buffer. The number of bytes to write to the device. + @param Buffer A pointer to the buffer containing the data to be written. + + @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was written correctly to the device. + If Event is not NULL (asynchronous I/O): The request was successfully queued for processing. + Event will be signaled upon completion. + @retval EFI_WRITE_PROTECTED The device cannot be written to. + @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation. + @retval EFI_NO_MEDIA There is no medium in the device. + @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium. + @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not valid for the device. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +DiskIo2WriteDiskEx ( + IN EFI_DISK_IO2_PROTOCOL *This, + IN UINT32 MediaId, + IN UINT64 Offset, + IN OUT EFI_DISK_IO2_TOKEN *Token, + IN UINTN BufferSize, + IN VOID *Buffer + ) +{ + return DiskIo2ReadWriteDisk ( + DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This), + TRUE, MediaId, Offset, Token, BufferSize, (UINT8 *) Buffer + ); +} + +/** + The callback for the BlockIo2 FlushBlocksEx. + @param Event Event whose notification function is being invoked. + @param Context The pointer to the notification function's context, + which points to the DISK_IO2_FLUSH_TASK instance. +**/ +VOID +EFIAPI +DiskIo2OnFlushComplete ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + DISK_IO2_FLUSH_TASK *Task; + + gBS->CloseEvent (Event); + + Task = (DISK_IO2_FLUSH_TASK *) Context; + ASSERT (Task->Signature == DISK_IO2_FLUSH_TASK_SIGNATURE); + Task->Token->TransactionStatus = Task->BlockIo2Token.TransactionStatus; + gBS->SignalEvent (Task->Token->Event); + + FreePool (Task); +} + +/** + Flushes all modified data to the physical device. + + @param This Indicates a pointer to the calling context. + @param Token A pointer to the token associated with the transaction. + If this field is NULL, synchronous/blocking IO is performed. + + @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was flushed successfully to the device. + If Event is not NULL (asynchronous I/O): The request was successfully queued for processing. + Event will be signaled upon completion. + @retval EFI_WRITE_PROTECTED The device cannot be written to. + @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation. + @retval EFI_NO_MEDIA There is no medium in the device. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. +**/ +EFI_STATUS +EFIAPI +DiskIo2FlushDiskEx ( + IN EFI_DISK_IO2_PROTOCOL *This, + IN OUT EFI_DISK_IO2_TOKEN *Token + ) +{ + EFI_STATUS Status; + DISK_IO2_FLUSH_TASK *Task; + DISK_IO_PRIVATE_DATA *Private; + + Private = DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This); + + if ((Token != NULL) && (Token->Event != NULL)) { + Task = AllocatePool (sizeof (DISK_IO2_FLUSH_TASK)); + if (Task == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Status = gBS->CreateEvent ( + EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + DiskIo2OnFlushComplete, + Task, + &Task->BlockIo2Token.Event + ); + if (EFI_ERROR (Status)) { + FreePool (Task); + return Status; + } + Task->Signature = DISK_IO2_FLUSH_TASK_SIGNATURE; + Task->Token = Token; + Status = Private->BlockIo2->FlushBlocksEx (Private->BlockIo2, &Task->BlockIo2Token); + if (EFI_ERROR (Status)) { + gBS->CloseEvent (Task->BlockIo2Token.Event); + FreePool (Task); + } + } else { + Status = Private->BlockIo2->FlushBlocksEx (Private->BlockIo2, NULL); + } + + return Status; +} + +/** + Read BufferSize bytes from Offset into Buffer. + Reads may support reads that are not aligned on + sector boundaries. There are three cases: + UnderRun - The first byte is not on a sector boundary or the read request is + less than a sector in length. + Aligned - A read of N contiguous sectors. + OverRun - The last byte is not on a sector boundary. + + @param This Protocol instance pointer. + @param MediaId Id of the media, changes every time the media is replaced. + @param Offset The starting byte offset to read from + @param BufferSize Size of Buffer + @param Buffer Buffer containing read data + + @retval EFI_SUCCESS The data was read correctly from the device. + @retval EFI_DEVICE_ERROR The device reported an error while performing the read. + @retval EFI_NO_MEDIA There is no media in the device. + @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device. + @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not + valid for the device. + +**/ +EFI_STATUS +EFIAPI +DiskIoReadDisk ( + IN EFI_DISK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN UINT64 Offset, + IN UINTN BufferSize, + OUT VOID *Buffer + ) +{ + return DiskIo2ReadWriteDisk ( + DISK_IO_PRIVATE_DATA_FROM_DISK_IO (This), + FALSE, MediaId, Offset, NULL, BufferSize, (UINT8 *) Buffer + ); +} + + +/** + Writes BufferSize bytes from Buffer into Offset. + Writes may require a read modify write to support writes that are not + aligned on sector boundaries. There are three cases: + UnderRun - The first byte is not on a sector boundary or the write request + is less than a sector in length. Read modify write is required. + Aligned - A write of N contiguous sectors. + OverRun - The last byte is not on a sector boundary. Read modified write + required. + + @param This Protocol instance pointer. + @param MediaId Id of the media, changes every time the media is replaced. + @param Offset The starting byte offset to read from + @param BufferSize Size of Buffer + @param Buffer Buffer containing read data + + @retval EFI_SUCCESS The data was written correctly to the device. + @retval EFI_WRITE_PROTECTED The device can not be written to. + @retval EFI_DEVICE_ERROR The device reported an error while performing the write. + @retval EFI_NO_MEDIA There is no media in the device. + @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device. + @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not + valid for the device. + +**/ +EFI_STATUS +EFIAPI +DiskIoWriteDisk ( + IN EFI_DISK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN UINT64 Offset, + IN UINTN BufferSize, + IN VOID *Buffer + ) +{ + return DiskIo2ReadWriteDisk ( + DISK_IO_PRIVATE_DATA_FROM_DISK_IO (This), + TRUE, MediaId, Offset, NULL, BufferSize, (UINT8 *) Buffer + ); +} + +/** + The user Entry Point for module DiskIo. The user code starts with this function. + + @param[in] ImageHandle The firmware allocated handle for the EFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point is executed successfully. + @retval other Some error occurs when executing this entry point. + +**/ +EFI_STATUS +EFIAPI +InitializeDiskIo ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Install driver model protocol(s). + // + Status = EfiLibInstallDriverBindingComponentName2 ( + ImageHandle, + SystemTable, + &gDiskIoDriverBinding, + ImageHandle, + &gDiskIoComponentName, + &gDiskIoComponentName2 + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} diff --git a/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIo.h b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIo.h new file mode 100644 index 00000000..351d0ad9 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIo.h @@ -0,0 +1,467 @@ +/** @file + Master header file for DiskIo driver. It includes the module private defininitions. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR> +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _DISK_IO_H_ +#define _DISK_IO_H_ + +#include <Uefi.h> +#include <Protocol/BlockIo.h> +#include <Protocol/BlockIo2.h> +#include <Protocol/DiskIo2.h> +#include <Protocol/ComponentName.h> +#include <Protocol/DriverBinding.h> +#include <Protocol/DiskIo.h> +#include <Library/DebugLib.h> +#include <Library/UefiDriverEntryPoint.h> +#include <Library/UefiLib.h> +#include <Library/BaseLib.h> +#include <Library/BaseMemoryLib.h> +#include <Library/MemoryAllocationLib.h> +#include <Library/UefiBootServicesTableLib.h> + +#define DISK_IO_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('d', 's', 'k', 'I') +typedef struct { + UINT32 Signature; + + EFI_DISK_IO_PROTOCOL DiskIo; + EFI_DISK_IO2_PROTOCOL DiskIo2; + EFI_BLOCK_IO_PROTOCOL *BlockIo; + EFI_BLOCK_IO2_PROTOCOL *BlockIo2; + + UINT8 *SharedWorkingBuffer; + + EFI_LOCK TaskQueueLock; + LIST_ENTRY TaskQueue; +} DISK_IO_PRIVATE_DATA; +#define DISK_IO_PRIVATE_DATA_FROM_DISK_IO(a) CR (a, DISK_IO_PRIVATE_DATA, DiskIo, DISK_IO_PRIVATE_DATA_SIGNATURE) +#define DISK_IO_PRIVATE_DATA_FROM_DISK_IO2(a) CR (a, DISK_IO_PRIVATE_DATA, DiskIo2, DISK_IO_PRIVATE_DATA_SIGNATURE) + +#define DISK_IO2_TASK_SIGNATURE SIGNATURE_32 ('d', 'i', 'a', 't') +typedef struct { + UINT32 Signature; + LIST_ENTRY Link; /// < link to other task + EFI_LOCK SubtasksLock; + LIST_ENTRY Subtasks; /// < header of subtasks + EFI_DISK_IO2_TOKEN *Token; + DISK_IO_PRIVATE_DATA *Instance; +} DISK_IO2_TASK; + +#define DISK_IO2_FLUSH_TASK_SIGNATURE SIGNATURE_32 ('d', 'i', 'f', 't') +typedef struct { + UINT32 Signature; + EFI_BLOCK_IO2_TOKEN BlockIo2Token; + EFI_DISK_IO2_TOKEN *Token; +} DISK_IO2_FLUSH_TASK; + +#define DISK_IO_SUBTASK_SIGNATURE SIGNATURE_32 ('d', 'i', 's', 't') +typedef struct { + // + // UnderRun: Offset != 0, Length < BlockSize + // OverRun: Offset == 0, Length < BlockSize + // Middle: Offset is block aligned, Length is multiple of block size + // + UINT32 Signature; + LIST_ENTRY Link; + BOOLEAN Write; + UINT64 Lba; + UINT32 Offset; + UINTN Length; + UINT8 *WorkingBuffer; /// < NULL indicates using "Buffer" directly + UINT8 *Buffer; + BOOLEAN Blocking; + + // + // Following fields are for DiskIo2 + // + DISK_IO2_TASK *Task; + EFI_BLOCK_IO2_TOKEN BlockIo2Token; +} DISK_IO_SUBTASK; + +// +// Global Variables +// +extern EFI_DRIVER_BINDING_PROTOCOL gDiskIoDriverBinding; +extern EFI_COMPONENT_NAME_PROTOCOL gDiskIoComponentName; +extern EFI_COMPONENT_NAME2_PROTOCOL gDiskIoComponentName2; + +// +// Prototypes +// Driver model protocol interface +// +/** + Test to see if this driver supports ControllerHandle. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to test + @param RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver supports this device + @retval EFI_ALREADY_STARTED This driver is already running on this device + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +DiskIoDriverBindingSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Start this driver on ControllerHandle by opening a Block IO protocol and + installing a Disk IO protocol on ControllerHandle. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to bind driver to + @param RemainingDevicePath Optional parameter use to pick a specific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHandle + @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle + @retval other This driver does not support this device + +**/ +EFI_STATUS +EFIAPI +DiskIoDriverBindingStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL + ); + +/** + Stop this driver on ControllerHandle by removing Disk IO protocol and closing + the Block IO protocol on ControllerHandle. + + @param This Protocol instance pointer. + @param ControllerHandle Handle of device to stop driver on + @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of + children is zero stop the entire bus driver. + @param ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandle + @retval other This driver was not removed from this device + +**/ +EFI_STATUS +EFIAPI +DiskIoDriverBindingStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +// +// Disk I/O Protocol Interface +// +/** + Read BufferSize bytes from Offset into Buffer. + Reads may support reads that are not aligned on + sector boundaries. There are three cases: + UnderRun - The first byte is not on a sector boundary or the read request is + less than a sector in length. + Aligned - A read of N contiguous sectors. + OverRun - The last byte is not on a sector boundary. + + @param This Protocol instance pointer. + @param MediaId Id of the media, changes every time the media is replaced. + @param Offset The starting byte offset to read from + @param BufferSize Size of Buffer + @param Buffer Buffer containing read data + + @retval EFI_SUCCESS The data was read correctly from the device. + @retval EFI_DEVICE_ERROR The device reported an error while performing the read. + @retval EFI_NO_MEDIA There is no media in the device. + @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device. + @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not + valid for the device. + +**/ +EFI_STATUS +EFIAPI +DiskIoReadDisk ( + IN EFI_DISK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN UINT64 Offset, + IN UINTN BufferSize, + OUT VOID *Buffer + ); + +/** + Writes BufferSize bytes from Buffer into Offset. + Writes may require a read modify write to support writes that are not + aligned on sector boundaries. There are three cases: + UnderRun - The first byte is not on a sector boundary or the write request + is less than a sector in length. Read modify write is required. + Aligned - A write of N contiguous sectors. + OverRun - The last byte is not on a sector boundary. Read modified write + required. + + @param This Protocol instance pointer. + @param MediaId Id of the media, changes every time the media is replaced. + @param Offset The starting byte offset to read from + @param BufferSize Size of Buffer + @param Buffer Buffer containing read data + + @retval EFI_SUCCESS The data was written correctly to the device. + @retval EFI_WRITE_PROTECTED The device can not be written to. + @retval EFI_DEVICE_ERROR The device reported an error while performing the write. + @retval EFI_NO_MEDIA There is no media in the device. + @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device. + @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not + valid for the device. + +**/ +EFI_STATUS +EFIAPI +DiskIoWriteDisk ( + IN EFI_DISK_IO_PROTOCOL *This, + IN UINT32 MediaId, + IN UINT64 Offset, + IN UINTN BufferSize, + IN VOID *Buffer + ); + + +/** + Terminate outstanding asynchronous requests to a device. + + @param This Indicates a pointer to the calling context. + + @retval EFI_SUCCESS All outstanding requests were successfully terminated. + @retval EFI_DEVICE_ERROR The device reported an error while performing the cancel + operation. +**/ +EFI_STATUS +EFIAPI +DiskIo2Cancel ( + IN EFI_DISK_IO2_PROTOCOL *This + ); + +/** + Reads a specified number of bytes from a device. + + @param This Indicates a pointer to the calling context. + @param MediaId ID of the medium to be read. + @param Offset The starting byte offset on the logical block I/O device to read from. + @param Token A pointer to the token associated with the transaction. + If this field is NULL, synchronous/blocking IO is performed. + @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device. + @param Buffer A pointer to the destination buffer for the data. + The caller is responsible either having implicit or explicit ownership of the buffer. + + @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was read correctly from the device. + If Event is not NULL (asynchronous I/O): The request was successfully queued for processing. + Event will be signaled upon completion. + @retval EFI_DEVICE_ERROR The device reported an error while performing the write. + @retval EFI_NO_MEDIA There is no medium in the device. + @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium. + @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not valid for the device. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +DiskIo2ReadDiskEx ( + IN EFI_DISK_IO2_PROTOCOL *This, + IN UINT32 MediaId, + IN UINT64 Offset, + IN OUT EFI_DISK_IO2_TOKEN *Token, + IN UINTN BufferSize, + OUT VOID *Buffer + ); + +/** + Writes a specified number of bytes to a device. + + @param This Indicates a pointer to the calling context. + @param MediaId ID of the medium to be written. + @param Offset The starting byte offset on the logical block I/O device to write to. + @param Token A pointer to the token associated with the transaction. + If this field is NULL, synchronous/blocking IO is performed. + @param BufferSize The size in bytes of Buffer. The number of bytes to write to the device. + @param Buffer A pointer to the buffer containing the data to be written. + + @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was written correctly to the device. + If Event is not NULL (asynchronous I/O): The request was successfully queued for processing. + Event will be signaled upon completion. + @retval EFI_WRITE_PROTECTED The device cannot be written to. + @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation. + @retval EFI_NO_MEDIA There is no medium in the device. + @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium. + @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not valid for the device. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. + +**/ +EFI_STATUS +EFIAPI +DiskIo2WriteDiskEx ( + IN EFI_DISK_IO2_PROTOCOL *This, + IN UINT32 MediaId, + IN UINT64 Offset, + IN EFI_DISK_IO2_TOKEN *Token, + IN UINTN BufferSize, + IN VOID *Buffer + ); + +/** + Flushes all modified data to the physical device. + + @param This Indicates a pointer to the calling context. + @param Token A pointer to the token associated with the transaction. + If this field is NULL, synchronous/blocking IO is performed. + + @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was flushed successfully to the device. + If Event is not NULL (asynchronous I/O): The request was successfully queued for processing. + Event will be signaled upon completion. + @retval EFI_WRITE_PROTECTED The device cannot be written to. + @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation. + @retval EFI_NO_MEDIA There is no medium in the device. + @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources. +**/ +EFI_STATUS +EFIAPI +DiskIo2FlushDiskEx ( + IN EFI_DISK_IO2_PROTOCOL *This, + IN OUT EFI_DISK_IO2_TOKEN *Token + ); + +// +// EFI Component Name Functions +// +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form of a + Unicode string. If the driver specified by This has a user readable name in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver specified + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified + in RFC 4646 or ISO 639-2 language code format. + + @param DriverName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specified by + This and the language specified by Language was + returned in DriverName. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER DriverName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DiskIoComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + + +/** + Retrieves a Unicode string that is the user readable name of the controller + that is being managed by a driver. + + This function retrieves the user readable name of the controller specified by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specified by + Language, then a pointer to the controller name is returned in ControllerName, + and EFI_SUCCESS is returned. If the driver specified by This is not currently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does not + support the language specified by Language, then EFI_UNSUPPORTED is returned. + + @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or + EFI_COMPONENT_NAME_PROTOCOL instance. + + @param ControllerHandle[in] The handle of a controller that the driver + specified by This is managing. This handle + specifies the controller whose name is to be + returned. + + @param ChildHandle[in] The handle of the child controller to retrieve + the name of. This is an optional parameter that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus drivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of a + child controller. + + @param Language[in] A pointer to a Null-terminated ASCII string + array indicating the language. This is the + language of the driver name that the caller is + requesting, and it must match one of the + languages specified in SupportedLanguages. The + number of languages supported by a driver is up + to the driver writer. Language is specified in + RFC 4646 or ISO 639-2 language code format. + + @param ControllerName[out] A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle and + ChildHandle in the language specified by + Language from the point of view of the driver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable name in + the language specified by Language for the + driver specified by This was returned in + DriverName. + + @retval EFI_INVALID_PARAMETER ControllerHandle is NULL. + + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid + EFI_HANDLE. + + @retval EFI_INVALID_PARAMETER Language is NULL. + + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + + @retval EFI_UNSUPPORTED The driver specified by This is not currently + managing the controller specified by + ControllerHandle and ChildHandle. + + @retval EFI_UNSUPPORTED The driver specified by This does not support + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +DiskIoComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + + +#endif diff --git a/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxe.inf b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxe.inf new file mode 100644 index 00000000..b8942ce4 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxe.inf @@ -0,0 +1,65 @@ +## @file +# Module that lays Disk I/O protocol on every Block I/O protocol. +# +# This module produces Disk I/O protocol to abstract the block accesses +# of the Block I/O protocol to a more general offset-length protocol +# to provide byte-oriented access to block media. It adds this protocol +# to any Block I/O interface that appears in the system that does not +# already have a Disk I/O protocol. File systems and other disk access +# code utilize the Disk I/O protocol. +# +# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR> +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = DiskIoDxe + MODULE_UNI_FILE = DiskIoDxe.uni + FILE_GUID = 6B38F7B4-AD98-40e9-9093-ACA2B5A253C4 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = InitializeDiskIo + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# +# DRIVER_BINDING = gDiskIoDriverBinding +# COMPONENT_NAME = gDiskIoComponentName +# COMPONENT_NAME2 = gDiskIoComponentName2 +# + +[Sources] + ComponentName.c + DiskIo.h + DiskIo.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + UefiBootServicesTableLib + MemoryAllocationLib + BaseMemoryLib + BaseLib + UefiLib + UefiDriverEntryPoint + DebugLib + PcdLib + +[Protocols] + gEfiDiskIoProtocolGuid ## BY_START + gEfiDiskIo2ProtocolGuid ## BY_START + gEfiBlockIoProtocolGuid ## TO_START + gEfiBlockIo2ProtocolGuid ## TO_START + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdDiskIoDataBufferBlockNum ## SOMETIMES_CONSUMES + +[UserExtensions.TianoCore."ExtraFiles"] + DiskIoDxeExtra.uni diff --git a/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxe.uni b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxe.uni new file mode 100644 index 00000000..3bdf78d4 --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxe.uni @@ -0,0 +1,21 @@ +// /** @file
+// Module that lays Disk I/O protocol on every Block I/O protocol.
+//
+// This module produces Disk I/O protocol to abstract the block accesses
+// of the Block I/O protocol to a more general offset-length protocol
+// to provide byte-oriented access to block media. It adds this protocol
+// to any Block I/O interface that appears in the system that does not
+// already have a Disk I/O protocol. File systems and other disk access
+// code utilize the Disk I/O protocol.
+//
+// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT #language en-US "Lays Disk I/O protocol on every Block I/O protocol"
+
+#string STR_MODULE_DESCRIPTION #language en-US "This module produces Disk I/O protocol to abstract the block accesses of the Block I/O protocol to a more general offset-length protocol to provide byte-oriented access to block media. It adds this protocol to any Block I/O interface that appears in the system that does not already have a Disk I/O protocol. File systems and other disk access code utilize the Disk I/O protocol."
+
diff --git a/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxeExtra.uni b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxeExtra.uni new file mode 100644 index 00000000..a63822cc --- /dev/null +++ b/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal/Disk/DiskIoDxe/DiskIoDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file
+// DiskIoDxe Localized Strings and Content
+//
+// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_PROPERTIES_MODULE_NAME
+#language en-US
+"Disk I/O DXE Driver"
+
+
|