2 DiskIo driver that lays on every BlockIo protocol in the system.
3 DiskIo converts a block oriented device to a byte oriented device.
5 Disk access may have to handle unaligned request about sector boundaries.
7 UnderRun - The first byte is not on a sector boundary or the read request is
8 less than a sector in length.
9 Aligned - A read of N contiguous sectors.
10 OverRun - The last byte is not on a sector boundary.
12 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
13 This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26 // Driver binding protocol implementation for DiskIo driver.
28 EFI_DRIVER_BINDING_PROTOCOL gDiskIoDriverBinding
= {
29 DiskIoDriverBindingSupported
,
30 DiskIoDriverBindingStart
,
31 DiskIoDriverBindingStop
,
38 // Template for DiskIo private data structure.
39 // The pointer to BlockIo protocol interface is assigned dynamically.
41 DISK_IO_PRIVATE_DATA gDiskIoPrivateDataTemplate
= {
42 DISK_IO_PRIVATE_DATA_SIGNATURE
,
44 EFI_DISK_IO_PROTOCOL_REVISION
,
49 EFI_DISK_IO2_PROTOCOL_REVISION
,
58 Test to see if this driver supports ControllerHandle.
60 @param This Protocol instance pointer.
61 @param ControllerHandle Handle of device to test
62 @param RemainingDevicePath Optional parameter use to pick a specific child
65 @retval EFI_SUCCESS This driver supports this device
66 @retval EFI_ALREADY_STARTED This driver is already running on this device
67 @retval other This driver does not support this device
72 DiskIoDriverBindingSupported (
73 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
74 IN EFI_HANDLE ControllerHandle
,
75 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
79 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
82 // Open the IO Abstraction(s) needed to perform the supported test.
84 Status
= gBS
->OpenProtocol (
86 &gEfiBlockIoProtocolGuid
,
88 This
->DriverBindingHandle
,
90 EFI_OPEN_PROTOCOL_BY_DRIVER
92 if (EFI_ERROR (Status
)) {
97 // Close the I/O Abstraction(s) used to perform the supported test.
101 &gEfiBlockIoProtocolGuid
,
102 This
->DriverBindingHandle
,
110 Start this driver on ControllerHandle by opening a Block IO protocol and
111 installing a Disk IO protocol on ControllerHandle.
113 @param This Protocol instance pointer.
114 @param ControllerHandle Handle of device to bind driver to
115 @param RemainingDevicePath Optional parameter use to pick a specific child
118 @retval EFI_SUCCESS This driver is added to ControllerHandle
119 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
120 @retval other This driver does not support this device
125 DiskIoDriverBindingStart (
126 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
127 IN EFI_HANDLE ControllerHandle
,
128 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
132 DISK_IO_PRIVATE_DATA
*Instance
;
137 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
140 // Connect to the Block IO and Block IO2 interface on ControllerHandle.
142 Status
= gBS
->OpenProtocol (
144 &gEfiBlockIoProtocolGuid
,
145 (VOID
**) &gDiskIoPrivateDataTemplate
.BlockIo
,
146 This
->DriverBindingHandle
,
148 EFI_OPEN_PROTOCOL_BY_DRIVER
150 if (EFI_ERROR (Status
)) {
154 Status
= gBS
->OpenProtocol (
156 &gEfiBlockIo2ProtocolGuid
,
157 (VOID
**) &gDiskIoPrivateDataTemplate
.BlockIo2
,
158 This
->DriverBindingHandle
,
160 EFI_OPEN_PROTOCOL_BY_DRIVER
162 if (EFI_ERROR (Status
)) {
163 gDiskIoPrivateDataTemplate
.BlockIo2
= NULL
;
167 // Initialize the Disk IO device instance.
169 Instance
= AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA
), &gDiskIoPrivateDataTemplate
);
170 if (Instance
== NULL
) {
171 Status
= EFI_OUT_OF_RESOURCES
;
176 // The BlockSize and IoAlign of BlockIo and BlockIo2 should equal.
178 ASSERT ((Instance
->BlockIo2
== NULL
) ||
179 ((Instance
->BlockIo
->Media
->IoAlign
== Instance
->BlockIo2
->Media
->IoAlign
) &&
180 (Instance
->BlockIo
->Media
->BlockSize
== Instance
->BlockIo2
->Media
->BlockSize
)
183 InitializeListHead (&Instance
->TaskQueue
);
184 EfiInitializeLock (&Instance
->TaskQueueLock
, TPL_NOTIFY
);
185 Instance
->SharedWorkingBuffer
= AllocateAlignedPages (
186 EFI_SIZE_TO_PAGES (DATA_BUFFER_BLOCK_NUM
* Instance
->BlockIo
->Media
->BlockSize
),
187 Instance
->BlockIo
->Media
->IoAlign
189 if (Instance
->SharedWorkingBuffer
== NULL
) {
190 Status
= EFI_OUT_OF_RESOURCES
;
195 // Install protocol interfaces for the Disk IO device.
197 if (Instance
->BlockIo2
!= NULL
) {
198 Status
= gBS
->InstallMultipleProtocolInterfaces (
200 &gEfiDiskIoProtocolGuid
, &Instance
->DiskIo
,
201 &gEfiDiskIo2ProtocolGuid
, &Instance
->DiskIo2
,
205 Status
= gBS
->InstallMultipleProtocolInterfaces (
207 &gEfiDiskIoProtocolGuid
, &Instance
->DiskIo
,
213 if (EFI_ERROR (Status
)) {
214 if (Instance
!= NULL
&& Instance
->SharedWorkingBuffer
!= NULL
) {
216 Instance
->SharedWorkingBuffer
,
217 EFI_SIZE_TO_PAGES (DATA_BUFFER_BLOCK_NUM
* Instance
->BlockIo
->Media
->BlockSize
)
221 if (Instance
!= NULL
) {
227 &gEfiBlockIoProtocolGuid
,
228 This
->DriverBindingHandle
,
234 gBS
->RestoreTPL (OldTpl
);
239 Stop this driver on ControllerHandle by removing Disk IO protocol and closing
240 the Block IO protocol on ControllerHandle.
242 @param This Protocol instance pointer.
243 @param ControllerHandle Handle of device to stop driver on
244 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
245 children is zero stop the entire bus driver.
246 @param ChildHandleBuffer List of Child Handles to Stop.
248 @retval EFI_SUCCESS This driver is removed ControllerHandle
249 @retval other This driver was not removed from this device
254 DiskIoDriverBindingStop (
255 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
256 IN EFI_HANDLE ControllerHandle
,
257 IN UINTN NumberOfChildren
,
258 IN EFI_HANDLE
*ChildHandleBuffer
262 EFI_DISK_IO_PROTOCOL
*DiskIo
;
263 EFI_DISK_IO2_PROTOCOL
*DiskIo2
;
264 DISK_IO_PRIVATE_DATA
*Instance
;
268 // Get our context back.
270 Status
= gBS
->OpenProtocol (
272 &gEfiDiskIoProtocolGuid
,
274 This
->DriverBindingHandle
,
276 EFI_OPEN_PROTOCOL_GET_PROTOCOL
278 if (EFI_ERROR (Status
)) {
281 Status
= gBS
->OpenProtocol (
283 &gEfiDiskIo2ProtocolGuid
,
285 This
->DriverBindingHandle
,
287 EFI_OPEN_PROTOCOL_GET_PROTOCOL
289 if (EFI_ERROR (Status
)) {
293 Instance
= DISK_IO_PRIVATE_DATA_FROM_DISK_IO (DiskIo
);
295 if (DiskIo2
!= NULL
) {
297 // Call BlockIo2::Reset() to terminate any in-flight non-blocking I/O requests
299 ASSERT (Instance
->BlockIo2
!= NULL
);
300 Status
= Instance
->BlockIo2
->Reset (Instance
->BlockIo2
, FALSE
);
301 if (EFI_ERROR (Status
)) {
304 Status
= gBS
->UninstallMultipleProtocolInterfaces (
306 &gEfiDiskIoProtocolGuid
, &Instance
->DiskIo
,
307 &gEfiDiskIo2ProtocolGuid
, &Instance
->DiskIo2
,
311 Status
= gBS
->UninstallMultipleProtocolInterfaces (
313 &gEfiDiskIoProtocolGuid
, &Instance
->DiskIo
,
317 if (!EFI_ERROR (Status
)) {
320 EfiAcquireLock (&Instance
->TaskQueueLock
);
321 AllTaskDone
= IsListEmpty (&Instance
->TaskQueue
);
322 EfiReleaseLock (&Instance
->TaskQueueLock
);
323 } while (!AllTaskDone
);
326 Instance
->SharedWorkingBuffer
,
327 EFI_SIZE_TO_PAGES (DATA_BUFFER_BLOCK_NUM
* Instance
->BlockIo
->Media
->BlockSize
)
330 Status
= gBS
->CloseProtocol (
332 &gEfiBlockIoProtocolGuid
,
333 This
->DriverBindingHandle
,
336 ASSERT_EFI_ERROR (Status
);
337 if (DiskIo2
!= NULL
) {
338 Status
= gBS
->CloseProtocol (
340 &gEfiBlockIo2ProtocolGuid
,
341 This
->DriverBindingHandle
,
344 ASSERT_EFI_ERROR (Status
);
355 Destroy the sub task.
357 @param Subtask Subtask.
359 @return LIST_ENTRY * Pointer to the next link of subtask.
362 DiskIoDestroySubtask (
363 IN DISK_IO_PRIVATE_DATA
*Instance
,
364 IN DISK_IO_SUBTASK
*Subtask
368 Link
= RemoveEntryList (&Subtask
->Link
);
369 if (!Subtask
->Blocking
) {
370 if (Subtask
->WorkingBuffer
!= NULL
) {
372 Subtask
->WorkingBuffer
,
373 Subtask
->Length
< Instance
->BlockIo
->Media
->BlockSize
374 ? EFI_SIZE_TO_PAGES (Instance
->BlockIo
->Media
->BlockSize
)
375 : EFI_SIZE_TO_PAGES (Subtask
->Length
)
378 if (Subtask
->BlockIo2Token
.Event
!= NULL
) {
379 gBS
->CloseEvent (Subtask
->BlockIo2Token
.Event
);
388 The callback for the BlockIo2 ReadBlocksEx/WriteBlocksEx.
389 @param Event Event whose notification function is being invoked.
390 @param Context The pointer to the notification function's context,
391 which points to the DISK_IO_SUBTASK instance.
395 DiskIo2OnReadWriteComplete (
400 DISK_IO_SUBTASK
*Subtask
;
402 EFI_STATUS TransactionStatus
;
403 DISK_IO_PRIVATE_DATA
*Instance
;
405 gBS
->CloseEvent (Event
);
407 Subtask
= (DISK_IO_SUBTASK
*) Context
;
408 TransactionStatus
= Subtask
->BlockIo2Token
.TransactionStatus
;
409 Task
= Subtask
->Task
;
410 Instance
= Task
->Instance
;
412 ASSERT (Subtask
->Signature
== DISK_IO_SUBTASK_SIGNATURE
);
413 ASSERT (Instance
->Signature
== DISK_IO_PRIVATE_DATA_SIGNATURE
);
414 ASSERT (Task
->Signature
== DISK_IO2_TASK_SIGNATURE
);
416 if (Subtask
->WorkingBuffer
!= NULL
) {
417 if (!EFI_ERROR (TransactionStatus
) && (Task
->Token
!= NULL
) && !Subtask
->Write
) {
418 CopyMem (Subtask
->Buffer
, Subtask
->WorkingBuffer
+ Subtask
->Offset
, Subtask
->Length
);
422 // The WorkingBuffer of blocking subtask either points to SharedWorkingBuffer
423 // or will be used by non-blocking subtask which will be freed below.
425 if (!Subtask
->Blocking
) {
427 Subtask
->WorkingBuffer
,
428 Subtask
->Length
< Instance
->BlockIo
->Media
->BlockSize
429 ? EFI_SIZE_TO_PAGES (Instance
->BlockIo
->Media
->BlockSize
)
430 : EFI_SIZE_TO_PAGES (Subtask
->Length
)
434 RemoveEntryList (&Subtask
->Link
);
437 if (EFI_ERROR (TransactionStatus
) || IsListEmpty (&Task
->Subtasks
)) {
438 if (Task
->Token
!= NULL
) {
440 // Signal error status once the subtask is failed.
441 // Or signal the last status once the last subtask is finished.
443 Task
->Token
->TransactionStatus
= TransactionStatus
;
444 gBS
->SignalEvent (Task
->Token
->Event
);
447 // Mark token to NULL
453 if (IsListEmpty (&Task
->Subtasks
)) {
454 EfiAcquireLock (&Instance
->TaskQueueLock
);
455 RemoveEntryList (&Task
->Link
);
456 EfiReleaseLock (&Instance
->TaskQueueLock
);
465 @param Write TRUE: Write request; FALSE: Read request.
466 @param Lba The starting logical block address to read from on the device.
467 @param Offset The starting byte offset to read from the LBA.
468 @param Length The number of bytes to read from the device.
469 @param WorkingBuffer The aligned buffer to hold the data for reading or writing.
470 @param Buffer The buffer to hold the data for reading or writing.
471 @param Blocking TRUE: Blocking request; FALSE: Non-blocking request.
473 @return A pointer to the created subtask.
476 DiskIoCreateSubtask (
481 IN VOID
*WorkingBuffer
, OPTIONAL
486 DISK_IO_SUBTASK
*Subtask
;
489 Subtask
= AllocateZeroPool (sizeof (DISK_IO_SUBTASK
));
490 if (Subtask
== NULL
) {
493 Subtask
->Signature
= DISK_IO_SUBTASK_SIGNATURE
;
494 Subtask
->Write
= Write
;
496 Subtask
->Offset
= Offset
;
497 Subtask
->Length
= Length
;
498 Subtask
->WorkingBuffer
= WorkingBuffer
;
499 Subtask
->Buffer
= Buffer
;
500 Subtask
->Blocking
= Blocking
;
502 Status
= gBS
->CreateEvent (
505 DiskIo2OnReadWriteComplete
,
507 &Subtask
->BlockIo2Token
.Event
509 if (EFI_ERROR (Status
)) {
516 " %c:Lba/Offset/Length/WorkingBuffer/Buffer = %016lx/%08x/%08x/%08x/%08x\n",
517 Write
? 'W': 'R', Lba
, Offset
, Length
, WorkingBuffer
, Buffer
524 Create the subtask list.
526 @param Instance Pointer to the DISK_IO_PRIVATE_DATA.
527 @param Write TRUE: Write request; FALSE: Read request.
528 @param Offset The starting byte offset to read from the device.
529 @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.
530 @param Buffer A pointer to the buffer for the data.
531 @param Blocking TRUE: Blocking request; FALSE: Non-blocking request.
532 @param SharedWorkingBuffer The aligned buffer to hold the data for reading or writing.
533 @param Subtasks The subtask list header.
535 @retval TRUE The subtask list is created successfully.
536 @retval FALSE The subtask list is not created.
539 DiskIoCreateSubtaskList (
540 IN DISK_IO_PRIVATE_DATA
*Instance
,
546 IN VOID
*SharedWorkingBuffer
,
547 IN OUT LIST_ENTRY
*Subtasks
558 UINTN DataBufferSize
;
559 DISK_IO_SUBTASK
*Subtask
;
563 DEBUG ((EFI_D_BLKIO
, "DiskIo: Create subtasks for task: Offset/BufferSize/Buffer = %016lx/%08x/%08x\n", Offset
, BufferSize
, Buffer
));
565 BlockSize
= Instance
->BlockIo
->Media
->BlockSize
;
566 IoAlign
= Instance
->BlockIo
->Media
->IoAlign
;
571 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
572 BufferPtr
= (UINT8
*) Buffer
;
575 // Special handling for zero BufferSize
577 if (BufferSize
== 0) {
578 Subtask
= DiskIoCreateSubtask (Write
, Lba
, UnderRun
, 0, NULL
, BufferPtr
, Blocking
);
579 if (Subtask
== NULL
) {
582 InsertTailList (Subtasks
, &Subtask
->Link
);
587 Length
= MIN (BlockSize
- UnderRun
, BufferSize
);
589 WorkingBuffer
= SharedWorkingBuffer
;
591 WorkingBuffer
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (BlockSize
), IoAlign
);
592 if (WorkingBuffer
== NULL
) {
598 // A half write operation can be splitted to a blocking block-read and half write operation
599 // This can simplify the sub task processing logic
601 Subtask
= DiskIoCreateSubtask (FALSE
, Lba
, 0, BlockSize
, NULL
, WorkingBuffer
, TRUE
);
602 if (Subtask
== NULL
) {
605 InsertTailList (Subtasks
, &Subtask
->Link
);
608 Subtask
= DiskIoCreateSubtask (Write
, Lba
, UnderRun
, Length
, WorkingBuffer
, BufferPtr
, Blocking
);
609 if (Subtask
== NULL
) {
612 InsertTailList (Subtasks
, &Subtask
->Link
);
616 BufferSize
-= Length
;
620 OverRunLba
= Lba
+ DivU64x32Remainder (BufferSize
, BlockSize
, &OverRun
);
621 BufferSize
-= OverRun
;
625 WorkingBuffer
= SharedWorkingBuffer
;
627 WorkingBuffer
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (BlockSize
), IoAlign
);
628 if (WorkingBuffer
== NULL
) {
634 // A half write operation can be splitted to a blocking block-read and half write operation
635 // This can simplify the sub task processing logic
637 Subtask
= DiskIoCreateSubtask (FALSE
, OverRunLba
, 0, BlockSize
, NULL
, WorkingBuffer
, TRUE
);
638 if (Subtask
== NULL
) {
641 InsertTailList (Subtasks
, &Subtask
->Link
);
644 Subtask
= DiskIoCreateSubtask (Write
, OverRunLba
, 0, OverRun
, WorkingBuffer
, BufferPtr
, Blocking
);
645 if (Subtask
== NULL
) {
648 InsertTailList (Subtasks
, &Subtask
->Link
);
651 if (OverRunLba
> Lba
) {
653 // If the DiskIo maps directly to a BlockIo device do the read.
655 if (ALIGN_POINTER (BufferPtr
, IoAlign
) == BufferPtr
) {
656 Subtask
= DiskIoCreateSubtask (Write
, Lba
, 0, BufferSize
, NULL
, BufferPtr
, Blocking
);
657 if (Subtask
== NULL
) {
660 InsertTailList (Subtasks
, &Subtask
->Link
);
662 BufferPtr
+= BufferSize
;
663 Offset
+= BufferSize
;
664 BufferSize
-= BufferSize
;
669 // Use the allocated buffer instead of the original buffer
670 // to avoid alignment issue.
672 for (; Lba
< OverRunLba
; Lba
+= DATA_BUFFER_BLOCK_NUM
) {
673 DataBufferSize
= MIN (BufferSize
, DATA_BUFFER_BLOCK_NUM
* BlockSize
);
675 Subtask
= DiskIoCreateSubtask (Write
, Lba
, 0, DataBufferSize
, SharedWorkingBuffer
, BufferPtr
, Blocking
);
676 if (Subtask
== NULL
) {
679 InsertTailList (Subtasks
, &Subtask
->Link
);
681 BufferPtr
+= DataBufferSize
;
682 Offset
+= DataBufferSize
;
683 BufferSize
-= DataBufferSize
;
686 WorkingBuffer
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize
), IoAlign
);
687 if (WorkingBuffer
== NULL
) {
689 // If there is not enough memory, downgrade to blocking access
691 DEBUG ((EFI_D_VERBOSE
, "DiskIo: No enough memory so downgrade to blocking access\n"));
692 if (!DiskIoCreateSubtaskList (Instance
, Write
, Offset
, BufferSize
, BufferPtr
, TRUE
, SharedWorkingBuffer
, Subtasks
)) {
696 Subtask
= DiskIoCreateSubtask (Write
, Lba
, 0, BufferSize
, WorkingBuffer
, BufferPtr
, Blocking
);
697 if (Subtask
== NULL
) {
700 InsertTailList (Subtasks
, &Subtask
->Link
);
703 BufferPtr
+= BufferSize
;
704 Offset
+= BufferSize
;
705 BufferSize
-= BufferSize
;
710 ASSERT (BufferSize
== 0);
716 // Remove all the subtasks.
718 for (Link
= GetFirstNode (Subtasks
); !IsNull (Subtasks
, Link
); ) {
719 Subtask
= CR (Link
, DISK_IO_SUBTASK
, Link
, DISK_IO_SUBTASK_SIGNATURE
);
720 Link
= DiskIoDestroySubtask (Instance
, Subtask
);
726 Terminate outstanding asynchronous requests to a device.
728 @param This Indicates a pointer to the calling context.
730 @retval EFI_SUCCESS All outstanding requests were successfully terminated.
731 @retval EFI_DEVICE_ERROR The device reported an error while performing the cancel
737 IN EFI_DISK_IO2_PROTOCOL
*This
740 DISK_IO_PRIVATE_DATA
*Instance
;
744 Instance
= DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This
);
746 EfiAcquireLock (&Instance
->TaskQueueLock
);
748 for (Link
= GetFirstNode (&Instance
->TaskQueue
)
749 ; !IsNull (&Instance
->TaskQueue
, Link
)
750 ; Link
= GetNextNode (&Instance
->TaskQueue
, Link
)
752 Task
= CR (Link
, DISK_IO2_TASK
, Link
, DISK_IO2_TASK_SIGNATURE
);
754 if (Task
->Token
!= NULL
) {
755 Task
->Token
->TransactionStatus
= EFI_ABORTED
;
756 gBS
->SignalEvent (Task
->Token
->Event
);
758 // Set Token to NULL so that the further BlockIo2 responses will be ignored
764 EfiReleaseLock (&Instance
->TaskQueueLock
);
770 Common routine to access the disk.
772 @param Instance Pointer to the DISK_IO_PRIVATE_DATA.
773 @param Write TRUE: Write operation; FALSE: Read operation.
774 @param MediaId ID of the medium to access.
775 @param Offset The starting byte offset on the logical block I/O device to access.
776 @param Token A pointer to the token associated with the transaction.
777 If this field is NULL, synchronous/blocking IO is performed.
778 @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.
779 @param Buffer A pointer to the destination buffer for the data.
780 The caller is responsible either having implicit or explicit ownership of the buffer.
783 DiskIo2ReadWriteDisk (
784 IN DISK_IO_PRIVATE_DATA
*Instance
,
788 IN EFI_DISK_IO2_TOKEN
*Token
,
794 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
795 EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
;
796 EFI_BLOCK_IO_MEDIA
*Media
;
799 DISK_IO_SUBTASK
*Subtask
;
801 BOOLEAN TaskQueueEmpty
;
804 LIST_ENTRY
*SubtasksPtr
;
807 BlockIo
= Instance
->BlockIo
;
808 BlockIo2
= Instance
->BlockIo2
;
809 Media
= BlockIo
->Media
;
810 Status
= EFI_SUCCESS
;
811 Blocking
= ((Token
== NULL
) || (Token
->Event
== NULL
));
813 if (Media
->MediaId
!= MediaId
) {
814 return EFI_MEDIA_CHANGED
;
817 if (Write
&& Media
->ReadOnly
) {
818 return EFI_WRITE_PROTECTED
;
823 // Wait till pending async task is completed.
826 EfiAcquireLock (&Instance
->TaskQueueLock
);
827 TaskQueueEmpty
= IsListEmpty (&Instance
->TaskQueue
);
828 EfiReleaseLock (&Instance
->TaskQueueLock
);
829 } while (!TaskQueueEmpty
);
831 SubtasksPtr
= &Subtasks
;
833 Task
= AllocatePool (sizeof (DISK_IO2_TASK
));
835 return EFI_OUT_OF_RESOURCES
;
838 EfiAcquireLock (&Instance
->TaskQueueLock
);
839 InsertTailList (&Instance
->TaskQueue
, &Task
->Link
);
840 EfiReleaseLock (&Instance
->TaskQueueLock
);
842 Task
->Signature
= DISK_IO2_TASK_SIGNATURE
;
843 Task
->Instance
= Instance
;
846 SubtasksPtr
= &Task
->Subtasks
;
849 InitializeListHead (SubtasksPtr
);
850 if (!DiskIoCreateSubtaskList (Instance
, Write
, Offset
, BufferSize
, Buffer
, Blocking
, Instance
->SharedWorkingBuffer
, SubtasksPtr
)) {
854 return EFI_OUT_OF_RESOURCES
;
856 ASSERT (!IsListEmpty (SubtasksPtr
));
858 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
859 for (Link
= GetFirstNode (SubtasksPtr
); !IsNull (SubtasksPtr
, Link
); Link
= GetNextNode (SubtasksPtr
, Link
)) {
860 Subtask
= CR (Link
, DISK_IO_SUBTASK
, Link
, DISK_IO_SUBTASK_SIGNATURE
);
862 if (!Subtask
->Blocking
) {
863 Subtask
->Task
= Task
;
866 if (Subtask
->Write
) {
870 if (Subtask
->WorkingBuffer
!= NULL
) {
872 // A sub task before this one should be a block read operation, causing the WorkingBuffer filled with the entire one block data.
874 CopyMem (Subtask
->WorkingBuffer
+ Subtask
->Offset
, Subtask
->Buffer
, Subtask
->Length
);
877 if (Subtask
->Blocking
) {
878 Status
= BlockIo
->WriteBlocks (
882 Subtask
->Length
< Media
->BlockSize
? Media
->BlockSize
: Subtask
->Length
,
883 (Subtask
->WorkingBuffer
!= NULL
) ? Subtask
->WorkingBuffer
: Subtask
->Buffer
886 Status
= BlockIo2
->WriteBlocksEx (
890 &Subtask
->BlockIo2Token
,
891 Subtask
->Length
< Media
->BlockSize
? Media
->BlockSize
: Subtask
->Length
,
892 (Subtask
->WorkingBuffer
!= NULL
) ? Subtask
->WorkingBuffer
: Subtask
->Buffer
900 if (Subtask
->Blocking
) {
901 Status
= BlockIo
->ReadBlocks (
905 Subtask
->Length
< Media
->BlockSize
? Media
->BlockSize
: Subtask
->Length
,
906 (Subtask
->WorkingBuffer
!= NULL
) ? Subtask
->WorkingBuffer
: Subtask
->Buffer
908 if (!EFI_ERROR (Status
) && (Subtask
->WorkingBuffer
!= NULL
)) {
909 CopyMem (Subtask
->Buffer
, Subtask
->WorkingBuffer
+ Subtask
->Offset
, Subtask
->Length
);
912 Status
= BlockIo2
->ReadBlocksEx (
916 &Subtask
->BlockIo2Token
,
917 Subtask
->Length
< Media
->BlockSize
? Media
->BlockSize
: Subtask
->Length
,
918 (Subtask
->WorkingBuffer
!= NULL
) ? Subtask
->WorkingBuffer
: Subtask
->Buffer
923 if (EFI_ERROR (Status
)) {
928 gBS
->RaiseTPL (TPL_NOTIFY
);
931 // Remove all the remaining subtasks when failure.
932 // We shouldn't remove all the tasks because the non-blocking requests have been submitted and cannot be canceled.
934 if (EFI_ERROR (Status
)) {
935 while (!IsNull (SubtasksPtr
, Link
)) {
936 Subtask
= CR (Link
, DISK_IO_SUBTASK
, Link
, DISK_IO_SUBTASK_SIGNATURE
);
937 Link
= DiskIoDestroySubtask (Instance
, Subtask
);
942 // Remove all the blocking subtasks because the non-blocking callback only removes the non-blocking subtask.
944 for (Link
= GetFirstNode (SubtasksPtr
); !IsNull (SubtasksPtr
, Link
); ) {
945 Subtask
= CR (Link
, DISK_IO_SUBTASK
, Link
, DISK_IO_SUBTASK_SIGNATURE
);
946 if (Subtask
->Blocking
) {
947 Link
= DiskIoDestroySubtask (Instance
, Subtask
);
949 Link
= GetNextNode (SubtasksPtr
, Link
);
954 // It's possible that the callback runs before raising TPL to NOTIFY,
955 // so the subtasks list only contains blocking subtask.
956 // Remove the Task after the blocking subtasks are removed in above.
958 if (!Blocking
&& IsListEmpty (SubtasksPtr
)) {
959 EfiAcquireLock (&Instance
->TaskQueueLock
);
960 RemoveEntryList (&Task
->Link
);
961 EfiReleaseLock (&Instance
->TaskQueueLock
);
963 if (Task
->Token
!= NULL
) {
965 // Task->Token should be set to NULL by the DiskIo2OnReadWriteComplete
966 // It it's not, that means the non-blocking request was downgraded to blocking request.
968 DEBUG ((EFI_D_VERBOSE
, "DiskIo: Non-blocking request was downgraded to blocking request, signal event directly.\n"));
969 Task
->Token
->TransactionStatus
= Status
;
970 gBS
->SignalEvent (Task
->Token
->Event
);
976 gBS
->RestoreTPL (OldTpl
);
982 Reads a specified number of bytes from a device.
984 @param This Indicates a pointer to the calling context.
985 @param MediaId ID of the medium to be read.
986 @param Offset The starting byte offset on the logical block I/O device to read from.
987 @param Token A pointer to the token associated with the transaction.
988 If this field is NULL, synchronous/blocking IO is performed.
989 @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.
990 @param Buffer A pointer to the destination buffer for the data.
991 The caller is responsible either having implicit or explicit ownership of the buffer.
993 @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was read correctly from the device.
994 If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.
995 Event will be signaled upon completion.
996 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
997 @retval EFI_NO_MEDIA There is no medium in the device.
998 @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium.
999 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not valid for the device.
1000 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1006 IN EFI_DISK_IO2_PROTOCOL
*This
,
1009 IN OUT EFI_DISK_IO2_TOKEN
*Token
,
1010 IN UINTN BufferSize
,
1014 return DiskIo2ReadWriteDisk (
1015 DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This
),
1016 FALSE
, MediaId
, Offset
, Token
, BufferSize
, (UINT8
*) Buffer
1021 Writes a specified number of bytes to a device.
1023 @param This Indicates a pointer to the calling context.
1024 @param MediaId ID of the medium to be written.
1025 @param Offset The starting byte offset on the logical block I/O device to write to.
1026 @param Token A pointer to the token associated with the transaction.
1027 If this field is NULL, synchronous/blocking IO is performed.
1028 @param BufferSize The size in bytes of Buffer. The number of bytes to write to the device.
1029 @param Buffer A pointer to the buffer containing the data to be written.
1031 @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was written correctly to the device.
1032 If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.
1033 Event will be signaled upon completion.
1034 @retval EFI_WRITE_PROTECTED The device cannot be written to.
1035 @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation.
1036 @retval EFI_NO_MEDIA There is no medium in the device.
1037 @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium.
1038 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not valid for the device.
1039 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1044 DiskIo2WriteDiskEx (
1045 IN EFI_DISK_IO2_PROTOCOL
*This
,
1048 IN OUT EFI_DISK_IO2_TOKEN
*Token
,
1049 IN UINTN BufferSize
,
1053 return DiskIo2ReadWriteDisk (
1054 DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This
),
1055 TRUE
, MediaId
, Offset
, Token
, BufferSize
, (UINT8
*) Buffer
1060 The callback for the BlockIo2 FlushBlocksEx.
1061 @param Event Event whose notification function is being invoked.
1062 @param Context The pointer to the notification function's context,
1063 which points to the DISK_IO2_FLUSH_TASK instance.
1067 DiskIo2OnFlushComplete (
1072 DISK_IO2_FLUSH_TASK
*Task
;
1074 gBS
->CloseEvent (Event
);
1076 Task
= (DISK_IO2_FLUSH_TASK
*) Context
;
1077 ASSERT (Task
->Signature
== DISK_IO2_FLUSH_TASK_SIGNATURE
);
1078 Task
->Token
->TransactionStatus
= Task
->BlockIo2Token
.TransactionStatus
;
1079 gBS
->SignalEvent (Task
->Token
->Event
);
1083 Flushes all modified data to the physical device.
1085 @param This Indicates a pointer to the calling context.
1086 @param Token A pointer to the token associated with the transaction.
1087 If this field is NULL, synchronous/blocking IO is performed.
1089 @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was flushed successfully to the device.
1090 If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.
1091 Event will be signaled upon completion.
1092 @retval EFI_WRITE_PROTECTED The device cannot be written to.
1093 @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation.
1094 @retval EFI_NO_MEDIA There is no medium in the device.
1095 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1099 DiskIo2FlushDiskEx (
1100 IN EFI_DISK_IO2_PROTOCOL
*This
,
1101 IN OUT EFI_DISK_IO2_TOKEN
*Token
1105 DISK_IO2_FLUSH_TASK
*Task
;
1106 DISK_IO_PRIVATE_DATA
*Private
;
1108 Private
= DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This
);
1110 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1111 Task
= AllocatePool (sizeof (DISK_IO2_FLUSH_TASK
));
1113 return EFI_OUT_OF_RESOURCES
;
1116 Status
= gBS
->CreateEvent (
1119 DiskIo2OnFlushComplete
,
1121 &Task
->BlockIo2Token
.Event
1123 if (EFI_ERROR (Status
)) {
1127 Task
->Signature
= DISK_IO2_FLUSH_TASK_SIGNATURE
;
1128 Status
= Private
->BlockIo2
->FlushBlocksEx (Private
->BlockIo2
, &Task
->BlockIo2Token
);
1129 if (EFI_ERROR (Status
)) {
1130 gBS
->CloseEvent (Task
->BlockIo2Token
.Event
);
1134 Status
= Private
->BlockIo2
->FlushBlocksEx (Private
->BlockIo2
, NULL
);
1141 Read BufferSize bytes from Offset into Buffer.
1142 Reads may support reads that are not aligned on
1143 sector boundaries. There are three cases:
1144 UnderRun - The first byte is not on a sector boundary or the read request is
1145 less than a sector in length.
1146 Aligned - A read of N contiguous sectors.
1147 OverRun - The last byte is not on a sector boundary.
1149 @param This Protocol instance pointer.
1150 @param MediaId Id of the media, changes every time the media is replaced.
1151 @param Offset The starting byte offset to read from
1152 @param BufferSize Size of Buffer
1153 @param Buffer Buffer containing read data
1155 @retval EFI_SUCCESS The data was read correctly from the device.
1156 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
1157 @retval EFI_NO_MEDIA There is no media in the device.
1158 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1159 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
1160 valid for the device.
1166 IN EFI_DISK_IO_PROTOCOL
*This
,
1169 IN UINTN BufferSize
,
1173 return DiskIo2ReadWriteDisk (
1174 DISK_IO_PRIVATE_DATA_FROM_DISK_IO (This
),
1175 FALSE
, MediaId
, Offset
, NULL
, BufferSize
, (UINT8
*) Buffer
1181 Writes BufferSize bytes from Buffer into Offset.
1182 Writes may require a read modify write to support writes that are not
1183 aligned on sector boundaries. There are three cases:
1184 UnderRun - The first byte is not on a sector boundary or the write request
1185 is less than a sector in length. Read modify write is required.
1186 Aligned - A write of N contiguous sectors.
1187 OverRun - The last byte is not on a sector boundary. Read modified write
1190 @param This Protocol instance pointer.
1191 @param MediaId Id of the media, changes every time the media is replaced.
1192 @param Offset The starting byte offset to read from
1193 @param BufferSize Size of Buffer
1194 @param Buffer Buffer containing read data
1196 @retval EFI_SUCCESS The data was written correctly to the device.
1197 @retval EFI_WRITE_PROTECTED The device can not be written to.
1198 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1199 @retval EFI_NO_MEDIA There is no media in the device.
1200 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1201 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not
1202 valid for the device.
1208 IN EFI_DISK_IO_PROTOCOL
*This
,
1211 IN UINTN BufferSize
,
1215 return DiskIo2ReadWriteDisk (
1216 DISK_IO_PRIVATE_DATA_FROM_DISK_IO (This
),
1217 TRUE
, MediaId
, Offset
, NULL
, BufferSize
, (UINT8
*) Buffer
1222 The user Entry Point for module DiskIo. The user code starts with this function.
1224 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1225 @param[in] SystemTable A pointer to the EFI System Table.
1227 @retval EFI_SUCCESS The entry point is executed successfully.
1228 @retval other Some error occurs when executing this entry point.
1234 IN EFI_HANDLE ImageHandle
,
1235 IN EFI_SYSTEM_TABLE
*SystemTable
1241 // Install driver model protocol(s).
1243 Status
= EfiLibInstallDriverBindingComponentName2 (
1246 &gDiskIoDriverBinding
,
1248 &gDiskIoComponentName
,
1249 &gDiskIoComponentName2
1251 ASSERT_EFI_ERROR (Status
);