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 - 2007, Intel Corporation
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.
24 // Include common header file for this module.
26 #include "CommonHeader.h"
30 EFI_DRIVER_BINDING_PROTOCOL gDiskIoDriverBinding
= {
31 DiskIoDriverBindingSupported
,
32 DiskIoDriverBindingStart
,
33 DiskIoDriverBindingStop
,
39 DISK_IO_PRIVATE_DATA gDiskIoPrivateDataTemplate
= {
40 DISK_IO_PRIVATE_DATA_SIGNATURE
,
42 EFI_DISK_IO_PROTOCOL_REVISION
,
51 Test to see if this driver supports ControllerHandle.
53 @param This Protocol instance pointer.
54 @param ControllerHandle Handle of device to test
55 @param RemainingDevicePath Optional parameter use to pick a specific child
58 @retval EFI_SUCCESS This driver supports this device
59 @retval EFI_ALREADY_STARTED This driver is already running on this device
60 @retval other This driver does not support this device
65 DiskIoDriverBindingSupported (
66 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
67 IN EFI_HANDLE ControllerHandle
,
68 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
72 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
75 // Open the IO Abstraction(s) needed to perform the supported test.
77 Status
= gBS
->OpenProtocol (
79 &gEfiBlockIoProtocolGuid
,
81 This
->DriverBindingHandle
,
83 EFI_OPEN_PROTOCOL_BY_DRIVER
85 if (EFI_ERROR (Status
)) {
90 // Close the I/O Abstraction(s) used to perform the supported test.
94 &gEfiBlockIoProtocolGuid
,
95 This
->DriverBindingHandle
,
103 Start this driver on ControllerHandle by opening a Block IO protocol and
104 installing a Disk IO protocol on ControllerHandle.
106 @param This Protocol instance pointer.
107 @param ControllerHandle Handle of device to bind driver to
108 @param RemainingDevicePath Optional parameter use to pick a specific child
111 @retval EFI_SUCCESS This driver is added to ControllerHandle
112 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
113 @retval other This driver does not support this device
118 DiskIoDriverBindingStart (
119 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
120 IN EFI_HANDLE ControllerHandle
,
121 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
125 DISK_IO_PRIVATE_DATA
*Private
;
130 // Connect to the Block IO interface on ControllerHandle.
132 Status
= gBS
->OpenProtocol (
134 &gEfiBlockIoProtocolGuid
,
135 (VOID
**) &gDiskIoPrivateDataTemplate
.BlockIo
,
136 This
->DriverBindingHandle
,
138 EFI_OPEN_PROTOCOL_BY_DRIVER
140 if (EFI_ERROR (Status
)) {
145 // Initialize the Disk IO device instance.
147 Private
= AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA
), &gDiskIoPrivateDataTemplate
);
148 if (Private
== NULL
) {
149 Status
= EFI_OUT_OF_RESOURCES
;
154 // Install protocol interfaces for the Disk IO device.
156 Status
= gBS
->InstallProtocolInterface (
158 &gEfiDiskIoProtocolGuid
,
159 EFI_NATIVE_INTERFACE
,
164 if (EFI_ERROR (Status
)) {
166 if (Private
!= NULL
) {
172 &gEfiBlockIoProtocolGuid
,
173 This
->DriverBindingHandle
,
183 Stop this driver on ControllerHandle by removing Disk IO protocol and closing
184 the Block IO protocol on ControllerHandle.
186 @param This Protocol instance pointer.
187 @param ControllerHandle Handle of device to stop driver on
188 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
189 children is zero stop the entire bus driver.
190 @param ChildHandleBuffer List of Child Handles to Stop.
192 @retval EFI_SUCCESS This driver is removed ControllerHandle
193 @retval other This driver was not removed from this device
198 DiskIoDriverBindingStop (
199 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
200 IN EFI_HANDLE ControllerHandle
,
201 IN UINTN NumberOfChildren
,
202 IN EFI_HANDLE
*ChildHandleBuffer
206 EFI_DISK_IO_PROTOCOL
*DiskIo
;
207 DISK_IO_PRIVATE_DATA
*Private
;
210 // Get our context back.
212 Status
= gBS
->OpenProtocol (
214 &gEfiDiskIoProtocolGuid
,
216 This
->DriverBindingHandle
,
218 EFI_OPEN_PROTOCOL_GET_PROTOCOL
220 if (EFI_ERROR (Status
)) {
221 return EFI_UNSUPPORTED
;
224 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (DiskIo
);
226 Status
= gBS
->UninstallProtocolInterface (
228 &gEfiDiskIoProtocolGuid
,
231 if (!EFI_ERROR (Status
)) {
233 Status
= gBS
->CloseProtocol (
235 &gEfiBlockIoProtocolGuid
,
236 This
->DriverBindingHandle
,
241 if (!EFI_ERROR (Status
)) {
251 Read BufferSize bytes from Offset into Buffer.
252 Reads may support reads that are not aligned on
253 sector boundaries. There are three cases:
254 UnderRun - The first byte is not on a sector boundary or the read request is
255 less than a sector in length.
256 Aligned - A read of N contiguous sectors.
257 OverRun - The last byte is not on a sector boundary.
259 @param This Protocol instance pointer.
260 @param MediaId Id of the media, changes every time the media is replaced.
261 @param Offset The starting byte offset to read from
262 @param BufferSize Size of Buffer
263 @param Buffer Buffer containing read data
265 @retval EFI_SUCCESS The data was read correctly from the device.
266 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
267 @retval EFI_NO_MEDIA There is no media in the device.
268 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
269 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
270 valid for the device.
276 IN EFI_DISK_IO_PROTOCOL
*This
,
284 DISK_IO_PRIVATE_DATA
*Private
;
285 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
286 EFI_BLOCK_IO_MEDIA
*Media
;
292 BOOLEAN TransactionComplete
;
293 UINTN WorkingBufferSize
;
294 UINT8
*WorkingBuffer
;
298 UINTN IsBufferAligned
;
299 UINTN DataBufferSize
;
302 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (This
);
304 BlockIo
= Private
->BlockIo
;
305 Media
= BlockIo
->Media
;
306 BlockSize
= Media
->BlockSize
;
308 if (Media
->MediaId
!= MediaId
) {
309 return EFI_MEDIA_CHANGED
;
312 WorkingBuffer
= Buffer
;
313 WorkingBufferSize
= BufferSize
;
316 // Allocate a temporary buffer for operation
318 DataBufferSize
= BlockSize
* DATA_BUFFER_BLOCK_NUM
;
320 if (Media
->IoAlign
> 1) {
321 PreData
= AllocatePool (DataBufferSize
+ Media
->IoAlign
);
322 Data
= PreData
- ((UINTN
) PreData
& (Media
->IoAlign
- 1)) + Media
->IoAlign
;
324 PreData
= AllocatePool (DataBufferSize
);
328 if (PreData
== NULL
) {
329 return EFI_OUT_OF_RESOURCES
;
332 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
334 Length
= BlockSize
- UnderRun
;
335 TransactionComplete
= FALSE
;
337 Status
= EFI_SUCCESS
;
340 // Offset starts in the middle of an Lba, so read the entire block.
342 Status
= BlockIo
->ReadBlocks (
350 if (EFI_ERROR (Status
)) {
354 if (Length
> BufferSize
) {
356 TransactionComplete
= TRUE
;
359 CopyMem (WorkingBuffer
, Data
+ UnderRun
, Length
);
361 WorkingBuffer
+= Length
;
363 WorkingBufferSize
-= Length
;
364 if (WorkingBufferSize
== 0) {
371 OverRunLba
= Lba
+ DivU64x32Remainder (WorkingBufferSize
, BlockSize
, &OverRun
);
373 if (!TransactionComplete
&& WorkingBufferSize
>= BlockSize
) {
375 // If the DiskIo maps directly to a BlockIo device do the read.
378 WorkingBufferSize
-= OverRun
;
381 // Check buffer alignment
383 IsBufferAligned
= (UINTN
) WorkingBuffer
& (UINTN
) (Media
->IoAlign
- 1);
385 if (Media
->IoAlign
<= 1 || IsBufferAligned
== 0) {
387 // Alignment is satisfied, so read them together
389 Status
= BlockIo
->ReadBlocks (
397 if (EFI_ERROR (Status
)) {
401 WorkingBuffer
+= WorkingBufferSize
;
405 // Use the allocated buffer instead of the original buffer
406 // to avoid alignment issue.
407 // Here, the allocated buffer (8-byte align) can satisfy the alignment
411 if (WorkingBufferSize
<= DataBufferSize
) {
413 // It is the last calling to readblocks in this loop
415 DataBufferSize
= WorkingBufferSize
;
419 Status
= BlockIo
->ReadBlocks (
426 if (EFI_ERROR (Status
)) {
430 CopyMem (WorkingBuffer
, Data
, DataBufferSize
);
431 WorkingBufferSize
-= DataBufferSize
;
432 WorkingBuffer
+= DataBufferSize
;
433 Lba
+= DATA_BUFFER_BLOCK_NUM
;
438 if (!TransactionComplete
&& OverRun
!= 0) {
440 // Last read is not a complete block.
442 Status
= BlockIo
->ReadBlocks (
450 if (EFI_ERROR (Status
)) {
454 CopyMem (WorkingBuffer
, Data
, OverRun
);
458 if (PreData
!= NULL
) {
467 Read BufferSize bytes from Offset into Buffer.
468 Writes may require a read modify write to support writes that are not
469 aligned on sector boundaries. There are three cases:
470 UnderRun - The first byte is not on a sector boundary or the write request
471 is less than a sector in length. Read modify write is required.
472 Aligned - A write of N contiguous sectors.
473 OverRun - The last byte is not on a sector boundary. Read modified write
476 @param This Protocol instance pointer.
477 @param MediaId Id of the media, changes every time the media is replaced.
478 @param Offset The starting byte offset to read from
479 @param BufferSize Size of Buffer
480 @param Buffer Buffer containing read data
482 @retval EFI_SUCCESS The data was written correctly to the device.
483 @retval EFI_WRITE_PROTECTED The device can not be written to.
484 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
485 @retval EFI_NO_MEDIA There is no media in the device.
486 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
487 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not
488 valid for the device.
494 IN EFI_DISK_IO_PROTOCOL
*This
,
502 DISK_IO_PRIVATE_DATA
*Private
;
503 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
504 EFI_BLOCK_IO_MEDIA
*Media
;
510 BOOLEAN TransactionComplete
;
511 UINTN WorkingBufferSize
;
512 UINT8
*WorkingBuffer
;
516 UINTN IsBufferAligned
;
517 UINTN DataBufferSize
;
520 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (This
);
522 BlockIo
= Private
->BlockIo
;
523 Media
= BlockIo
->Media
;
524 BlockSize
= Media
->BlockSize
;
526 if (Media
->ReadOnly
) {
527 return EFI_WRITE_PROTECTED
;
530 if (Media
->MediaId
!= MediaId
) {
531 return EFI_MEDIA_CHANGED
;
534 DataBufferSize
= BlockSize
* DATA_BUFFER_BLOCK_NUM
;
536 if (Media
->IoAlign
> 1) {
537 PreData
= AllocatePool (DataBufferSize
+ Media
->IoAlign
);
538 Data
= PreData
- ((UINTN
) PreData
& (Media
->IoAlign
- 1)) + Media
->IoAlign
;
540 PreData
= AllocatePool (DataBufferSize
);
544 if (PreData
== NULL
) {
545 return EFI_OUT_OF_RESOURCES
;
548 WorkingBuffer
= Buffer
;
549 WorkingBufferSize
= BufferSize
;
551 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
553 Length
= BlockSize
- UnderRun
;
554 TransactionComplete
= FALSE
;
556 Status
= EFI_SUCCESS
;
559 // Offset starts in the middle of an Lba, so do read modify write.
561 Status
= BlockIo
->ReadBlocks (
569 if (EFI_ERROR (Status
)) {
573 if (Length
> BufferSize
) {
575 TransactionComplete
= TRUE
;
578 CopyMem (Data
+ UnderRun
, WorkingBuffer
, Length
);
580 Status
= BlockIo
->WriteBlocks (
587 if (EFI_ERROR (Status
)) {
591 WorkingBuffer
+= Length
;
592 WorkingBufferSize
-= Length
;
593 if (WorkingBufferSize
== 0) {
600 OverRunLba
= Lba
+ DivU64x32Remainder (WorkingBufferSize
, BlockSize
, &OverRun
);
602 if (!TransactionComplete
&& WorkingBufferSize
>= BlockSize
) {
604 // If the DiskIo maps directly to a BlockIo device do the write.
607 WorkingBufferSize
-= OverRun
;
610 // Check buffer alignment
612 IsBufferAligned
= (UINTN
) WorkingBuffer
& (UINTN
) (Media
->IoAlign
- 1);
614 if (Media
->IoAlign
<= 1 || IsBufferAligned
== 0) {
616 // Alignment is satisfied, so write them together
618 Status
= BlockIo
->WriteBlocks (
626 if (EFI_ERROR (Status
)) {
630 WorkingBuffer
+= WorkingBufferSize
;
634 // The buffer parameter is not aligned with the request
635 // So use the allocated instead.
636 // It can fit almost all the cases.
640 if (WorkingBufferSize
<= DataBufferSize
) {
642 // It is the last calling to writeblocks in this loop
644 DataBufferSize
= WorkingBufferSize
;
648 CopyMem (Data
, WorkingBuffer
, DataBufferSize
);
649 Status
= BlockIo
->WriteBlocks (
656 if (EFI_ERROR (Status
)) {
660 WorkingBufferSize
-= DataBufferSize
;
661 WorkingBuffer
+= DataBufferSize
;
662 Lba
+= DATA_BUFFER_BLOCK_NUM
;
663 } while (!LastWrite
);
667 if (!TransactionComplete
&& OverRun
!= 0) {
669 // Last bit is not a complete block, so do a read modify write.
671 Status
= BlockIo
->ReadBlocks (
679 if (EFI_ERROR (Status
)) {
683 CopyMem (Data
, WorkingBuffer
, OverRun
);
685 Status
= BlockIo
->WriteBlocks (
692 if (EFI_ERROR (Status
)) {
698 if (PreData
!= NULL
) {
707 The user Entry Point for module DiskIo. The user code starts with this function.
709 @param[in] ImageHandle The firmware allocated handle for the EFI image.
710 @param[in] SystemTable A pointer to the EFI System Table.
712 @retval EFI_SUCCESS The entry point is executed successfully.
713 @retval other Some error occurs when executing this entry point.
719 IN EFI_HANDLE ImageHandle
,
720 IN EFI_SYSTEM_TABLE
*SystemTable
726 // Install driver model protocol(s).
728 Status
= EfiLibInstallAllDriverProtocols (
731 &gDiskIoDriverBinding
,
733 &gDiskIoComponentName
,
737 ASSERT_EFI_ERROR (Status
);