2 DiskIo driver that layers it's self on every Block IO protocol in the system.
3 DiskIo converts a block oriented device to a byte oriented device.
5 ReadDisk may have to do reads that are not aligned on 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 - 2008, Intel Corporation. <BR>
13 All rights reserved. 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.
25 EFI_DRIVER_BINDING_PROTOCOL gDiskIoDriverBinding
= {
26 DiskIoDriverBindingSupported
,
27 DiskIoDriverBindingStart
,
28 DiskIoDriverBindingStop
,
34 DISK_IO_PRIVATE_DATA gDiskIoPrivateDataTemplate
= {
35 DISK_IO_PRIVATE_DATA_SIGNATURE
,
37 EFI_DISK_IO_PROTOCOL_REVISION
,
46 Test to see if this driver supports ControllerHandle.
48 @param This Protocol instance pointer.
49 @param ControllerHandle Handle of device to test
50 @param RemainingDevicePath Optional parameter use to pick a specific child
53 @retval EFI_SUCCESS This driver supports this device
54 @retval EFI_ALREADY_STARTED This driver is already running on this device
55 @retval other This driver does not support this device
60 DiskIoDriverBindingSupported (
61 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
62 IN EFI_HANDLE ControllerHandle
,
63 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
67 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
70 // Open the IO Abstraction(s) needed to perform the supported test.
72 Status
= gBS
->OpenProtocol (
74 &gEfiBlockIoProtocolGuid
,
76 This
->DriverBindingHandle
,
78 EFI_OPEN_PROTOCOL_BY_DRIVER
80 if (EFI_ERROR (Status
)) {
85 // Close the I/O Abstraction(s) used to perform the supported test.
89 &gEfiBlockIoProtocolGuid
,
90 This
->DriverBindingHandle
,
98 Start this driver on ControllerHandle by opening a Block IO protocol and
99 installing a Disk IO protocol on ControllerHandle.
101 @param This Protocol instance pointer.
102 @param ControllerHandle Handle of device to bind driver to
103 @param RemainingDevicePath Optional parameter use to pick a specific child
106 @retval EFI_SUCCESS This driver is added to ControllerHandle
107 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
108 @retval other This driver does not support this device
113 DiskIoDriverBindingStart (
114 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
115 IN EFI_HANDLE ControllerHandle
,
116 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
120 DISK_IO_PRIVATE_DATA
*Private
;
125 // Connect to the Block IO interface on ControllerHandle.
127 Status
= gBS
->OpenProtocol (
129 &gEfiBlockIoProtocolGuid
,
130 (VOID
**) &gDiskIoPrivateDataTemplate
.BlockIo
,
131 This
->DriverBindingHandle
,
133 EFI_OPEN_PROTOCOL_BY_DRIVER
135 if (EFI_ERROR (Status
)) {
140 // Initialize the Disk IO device instance.
142 Private
= AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA
), &gDiskIoPrivateDataTemplate
);
143 if (Private
== NULL
) {
144 Status
= EFI_OUT_OF_RESOURCES
;
149 // Install protocol interfaces for the Disk IO device.
151 Status
= gBS
->InstallProtocolInterface (
153 &gEfiDiskIoProtocolGuid
,
154 EFI_NATIVE_INTERFACE
,
159 if (EFI_ERROR (Status
)) {
161 if (Private
!= NULL
) {
167 &gEfiBlockIoProtocolGuid
,
168 This
->DriverBindingHandle
,
178 Stop this driver on ControllerHandle by removing Disk IO protocol and closing
179 the Block IO protocol on ControllerHandle.
181 @param This Protocol instance pointer.
182 @param ControllerHandle Handle of device to stop driver on
183 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
184 children is zero stop the entire bus driver.
185 @param ChildHandleBuffer List of Child Handles to Stop.
187 @retval EFI_SUCCESS This driver is removed ControllerHandle
188 @retval other This driver was not removed from this device
193 DiskIoDriverBindingStop (
194 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
195 IN EFI_HANDLE ControllerHandle
,
196 IN UINTN NumberOfChildren
,
197 IN EFI_HANDLE
*ChildHandleBuffer
201 EFI_DISK_IO_PROTOCOL
*DiskIo
;
202 DISK_IO_PRIVATE_DATA
*Private
;
205 // Get our context back.
207 Status
= gBS
->OpenProtocol (
209 &gEfiDiskIoProtocolGuid
,
211 This
->DriverBindingHandle
,
213 EFI_OPEN_PROTOCOL_GET_PROTOCOL
215 if (EFI_ERROR (Status
)) {
216 return EFI_UNSUPPORTED
;
219 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (DiskIo
);
221 Status
= gBS
->UninstallProtocolInterface (
223 &gEfiDiskIoProtocolGuid
,
226 if (!EFI_ERROR (Status
)) {
228 Status
= gBS
->CloseProtocol (
230 &gEfiBlockIoProtocolGuid
,
231 This
->DriverBindingHandle
,
236 if (!EFI_ERROR (Status
)) {
246 Read BufferSize bytes from Offset into Buffer.
247 Reads may support reads that are not aligned on
248 sector boundaries. There are three cases:
249 UnderRun - The first byte is not on a sector boundary or the read request is
250 less than a sector in length.
251 Aligned - A read of N contiguous sectors.
252 OverRun - The last byte is not on a sector boundary.
254 @param This Protocol instance pointer.
255 @param MediaId Id of the media, changes every time the media is replaced.
256 @param Offset The starting byte offset to read from
257 @param BufferSize Size of Buffer
258 @param Buffer Buffer containing read data
260 @retval EFI_SUCCESS The data was read correctly from the device.
261 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
262 @retval EFI_NO_MEDIA There is no media in the device.
263 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
264 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
265 valid for the device.
271 IN EFI_DISK_IO_PROTOCOL
*This
,
279 DISK_IO_PRIVATE_DATA
*Private
;
280 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
281 EFI_BLOCK_IO_MEDIA
*Media
;
287 BOOLEAN TransactionComplete
;
288 UINTN WorkingBufferSize
;
289 UINT8
*WorkingBuffer
;
293 UINTN IsBufferAligned
;
294 UINTN DataBufferSize
;
297 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (This
);
299 BlockIo
= Private
->BlockIo
;
300 Media
= BlockIo
->Media
;
301 BlockSize
= Media
->BlockSize
;
303 if (Media
->MediaId
!= MediaId
) {
304 return EFI_MEDIA_CHANGED
;
307 WorkingBuffer
= Buffer
;
308 WorkingBufferSize
= BufferSize
;
311 // Allocate a temporary buffer for operation
313 DataBufferSize
= BlockSize
* DATA_BUFFER_BLOCK_NUM
;
315 if (Media
->IoAlign
> 1) {
316 PreData
= AllocatePool (DataBufferSize
+ Media
->IoAlign
);
317 Data
= PreData
- ((UINTN
) PreData
& (Media
->IoAlign
- 1)) + Media
->IoAlign
;
319 PreData
= AllocatePool (DataBufferSize
);
323 if (PreData
== NULL
) {
324 return EFI_OUT_OF_RESOURCES
;
327 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
329 Length
= BlockSize
- UnderRun
;
330 TransactionComplete
= FALSE
;
332 Status
= EFI_SUCCESS
;
335 // Offset starts in the middle of an Lba, so read the entire block.
337 Status
= BlockIo
->ReadBlocks (
345 if (EFI_ERROR (Status
)) {
349 if (Length
> BufferSize
) {
351 TransactionComplete
= TRUE
;
354 CopyMem (WorkingBuffer
, Data
+ UnderRun
, Length
);
356 WorkingBuffer
+= Length
;
358 WorkingBufferSize
-= Length
;
359 if (WorkingBufferSize
== 0) {
366 OverRunLba
= Lba
+ DivU64x32Remainder (WorkingBufferSize
, BlockSize
, &OverRun
);
368 if (!TransactionComplete
&& WorkingBufferSize
>= BlockSize
) {
370 // If the DiskIo maps directly to a BlockIo device do the read.
373 WorkingBufferSize
-= OverRun
;
376 // Check buffer alignment
378 IsBufferAligned
= (UINTN
) WorkingBuffer
& (UINTN
) (Media
->IoAlign
- 1);
380 if (Media
->IoAlign
<= 1 || IsBufferAligned
== 0) {
382 // Alignment is satisfied, so read them together
384 Status
= BlockIo
->ReadBlocks (
392 if (EFI_ERROR (Status
)) {
396 WorkingBuffer
+= WorkingBufferSize
;
400 // Use the allocated buffer instead of the original buffer
401 // to avoid alignment issue.
402 // Here, the allocated buffer (8-byte align) can satisfy the alignment
406 if (WorkingBufferSize
<= DataBufferSize
) {
408 // It is the last calling to readblocks in this loop
410 DataBufferSize
= WorkingBufferSize
;
414 Status
= BlockIo
->ReadBlocks (
421 if (EFI_ERROR (Status
)) {
425 CopyMem (WorkingBuffer
, Data
, DataBufferSize
);
426 WorkingBufferSize
-= DataBufferSize
;
427 WorkingBuffer
+= DataBufferSize
;
428 Lba
+= DATA_BUFFER_BLOCK_NUM
;
433 if (!TransactionComplete
&& OverRun
!= 0) {
435 // Last read is not a complete block.
437 Status
= BlockIo
->ReadBlocks (
445 if (EFI_ERROR (Status
)) {
449 CopyMem (WorkingBuffer
, Data
, OverRun
);
453 if (PreData
!= NULL
) {
462 Read BufferSize bytes from Offset into Buffer.
463 Writes may require a read modify write to support writes that are not
464 aligned on sector boundaries. There are three cases:
465 UnderRun - The first byte is not on a sector boundary or the write request
466 is less than a sector in length. Read modify write is required.
467 Aligned - A write of N contiguous sectors.
468 OverRun - The last byte is not on a sector boundary. Read modified write
471 @param This Protocol instance pointer.
472 @param MediaId Id of the media, changes every time the media is replaced.
473 @param Offset The starting byte offset to read from
474 @param BufferSize Size of Buffer
475 @param Buffer Buffer containing read data
477 @retval EFI_SUCCESS The data was written correctly to the device.
478 @retval EFI_WRITE_PROTECTED The device can not be written to.
479 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
480 @retval EFI_NO_MEDIA There is no media in the device.
481 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
482 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not
483 valid for the device.
489 IN EFI_DISK_IO_PROTOCOL
*This
,
497 DISK_IO_PRIVATE_DATA
*Private
;
498 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
499 EFI_BLOCK_IO_MEDIA
*Media
;
505 BOOLEAN TransactionComplete
;
506 UINTN WorkingBufferSize
;
507 UINT8
*WorkingBuffer
;
511 UINTN IsBufferAligned
;
512 UINTN DataBufferSize
;
515 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (This
);
517 BlockIo
= Private
->BlockIo
;
518 Media
= BlockIo
->Media
;
519 BlockSize
= Media
->BlockSize
;
521 if (Media
->ReadOnly
) {
522 return EFI_WRITE_PROTECTED
;
525 if (Media
->MediaId
!= MediaId
) {
526 return EFI_MEDIA_CHANGED
;
529 DataBufferSize
= BlockSize
* DATA_BUFFER_BLOCK_NUM
;
531 if (Media
->IoAlign
> 1) {
532 PreData
= AllocatePool (DataBufferSize
+ Media
->IoAlign
);
533 Data
= PreData
- ((UINTN
) PreData
& (Media
->IoAlign
- 1)) + Media
->IoAlign
;
535 PreData
= AllocatePool (DataBufferSize
);
539 if (PreData
== NULL
) {
540 return EFI_OUT_OF_RESOURCES
;
543 WorkingBuffer
= Buffer
;
544 WorkingBufferSize
= BufferSize
;
546 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
548 Length
= BlockSize
- UnderRun
;
549 TransactionComplete
= FALSE
;
551 Status
= EFI_SUCCESS
;
554 // Offset starts in the middle of an Lba, so do read modify write.
556 Status
= BlockIo
->ReadBlocks (
564 if (EFI_ERROR (Status
)) {
568 if (Length
> BufferSize
) {
570 TransactionComplete
= TRUE
;
573 CopyMem (Data
+ UnderRun
, WorkingBuffer
, Length
);
575 Status
= BlockIo
->WriteBlocks (
582 if (EFI_ERROR (Status
)) {
586 WorkingBuffer
+= Length
;
587 WorkingBufferSize
-= Length
;
588 if (WorkingBufferSize
== 0) {
595 OverRunLba
= Lba
+ DivU64x32Remainder (WorkingBufferSize
, BlockSize
, &OverRun
);
597 if (!TransactionComplete
&& WorkingBufferSize
>= BlockSize
) {
599 // If the DiskIo maps directly to a BlockIo device do the write.
602 WorkingBufferSize
-= OverRun
;
605 // Check buffer alignment
607 IsBufferAligned
= (UINTN
) WorkingBuffer
& (UINTN
) (Media
->IoAlign
- 1);
609 if (Media
->IoAlign
<= 1 || IsBufferAligned
== 0) {
611 // Alignment is satisfied, so write them together
613 Status
= BlockIo
->WriteBlocks (
621 if (EFI_ERROR (Status
)) {
625 WorkingBuffer
+= WorkingBufferSize
;
629 // The buffer parameter is not aligned with the request
630 // So use the allocated instead.
631 // It can fit almost all the cases.
635 if (WorkingBufferSize
<= DataBufferSize
) {
637 // It is the last calling to writeblocks in this loop
639 DataBufferSize
= WorkingBufferSize
;
643 CopyMem (Data
, WorkingBuffer
, DataBufferSize
);
644 Status
= BlockIo
->WriteBlocks (
651 if (EFI_ERROR (Status
)) {
655 WorkingBufferSize
-= DataBufferSize
;
656 WorkingBuffer
+= DataBufferSize
;
657 Lba
+= DATA_BUFFER_BLOCK_NUM
;
658 } while (!LastWrite
);
662 if (!TransactionComplete
&& OverRun
!= 0) {
664 // Last bit is not a complete block, so do a read modify write.
666 Status
= BlockIo
->ReadBlocks (
674 if (EFI_ERROR (Status
)) {
678 CopyMem (Data
, WorkingBuffer
, OverRun
);
680 Status
= BlockIo
->WriteBlocks (
687 if (EFI_ERROR (Status
)) {
693 if (PreData
!= NULL
) {
702 The user Entry Point for module DiskIo. The user code starts with this function.
704 @param[in] ImageHandle The firmware allocated handle for the EFI image.
705 @param[in] SystemTable A pointer to the EFI System Table.
707 @retval EFI_SUCCESS The entry point is executed successfully.
708 @retval other Some error occurs when executing this entry point.
714 IN EFI_HANDLE ImageHandle
,
715 IN EFI_SYSTEM_TABLE
*SystemTable
721 // Install driver model protocol(s).
723 Status
= EfiLibInstallDriverBindingComponentName2 (
726 &gDiskIoDriverBinding
,
728 &gDiskIoComponentName
,
729 &gDiskIoComponentName2
731 ASSERT_EFI_ERROR (Status
);