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 - 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.
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
,
53 Test to see if this driver supports ControllerHandle.
55 @param This Protocol instance pointer.
56 @param ControllerHandle Handle of device to test
57 @param RemainingDevicePath Optional parameter use to pick a specific child
60 @retval EFI_SUCCESS This driver supports this device
61 @retval EFI_ALREADY_STARTED This driver is already running on this device
62 @retval other This driver does not support this device
67 DiskIoDriverBindingSupported (
68 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
69 IN EFI_HANDLE ControllerHandle
,
70 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
74 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
77 // Open the IO Abstraction(s) needed to perform the supported test.
79 Status
= gBS
->OpenProtocol (
81 &gEfiBlockIoProtocolGuid
,
83 This
->DriverBindingHandle
,
85 EFI_OPEN_PROTOCOL_BY_DRIVER
87 if (EFI_ERROR (Status
)) {
92 // Close the I/O Abstraction(s) used to perform the supported test.
96 &gEfiBlockIoProtocolGuid
,
97 This
->DriverBindingHandle
,
105 Start this driver on ControllerHandle by opening a Block IO protocol and
106 installing a Disk IO protocol on ControllerHandle.
108 @param This Protocol instance pointer.
109 @param ControllerHandle Handle of device to bind driver to
110 @param RemainingDevicePath Optional parameter use to pick a specific child
113 @retval EFI_SUCCESS This driver is added to ControllerHandle
114 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
115 @retval other This driver does not support this device
120 DiskIoDriverBindingStart (
121 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
122 IN EFI_HANDLE ControllerHandle
,
123 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
127 DISK_IO_PRIVATE_DATA
*Private
;
132 // Connect to the Block IO interface on ControllerHandle.
134 Status
= gBS
->OpenProtocol (
136 &gEfiBlockIoProtocolGuid
,
137 (VOID
**) &gDiskIoPrivateDataTemplate
.BlockIo
,
138 This
->DriverBindingHandle
,
140 EFI_OPEN_PROTOCOL_BY_DRIVER
142 if (EFI_ERROR (Status
)) {
147 // Initialize the Disk IO device instance.
149 Private
= AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA
), &gDiskIoPrivateDataTemplate
);
150 if (Private
== NULL
) {
151 Status
= EFI_OUT_OF_RESOURCES
;
156 // Install protocol interfaces for the Disk IO device.
158 Status
= gBS
->InstallProtocolInterface (
160 &gEfiDiskIoProtocolGuid
,
161 EFI_NATIVE_INTERFACE
,
166 if (EFI_ERROR (Status
)) {
168 if (Private
!= NULL
) {
174 &gEfiBlockIoProtocolGuid
,
175 This
->DriverBindingHandle
,
185 Stop this driver on ControllerHandle by removing Disk IO protocol and closing
186 the Block IO protocol on ControllerHandle.
188 @param This Protocol instance pointer.
189 @param ControllerHandle Handle of device to stop driver on
190 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
191 children is zero stop the entire bus driver.
192 @param ChildHandleBuffer List of Child Handles to Stop.
194 @retval EFI_SUCCESS This driver is removed ControllerHandle
195 @retval other This driver was not removed from this device
200 DiskIoDriverBindingStop (
201 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
202 IN EFI_HANDLE ControllerHandle
,
203 IN UINTN NumberOfChildren
,
204 IN EFI_HANDLE
*ChildHandleBuffer
208 EFI_DISK_IO_PROTOCOL
*DiskIo
;
209 DISK_IO_PRIVATE_DATA
*Private
;
212 // Get our context back.
214 Status
= gBS
->OpenProtocol (
216 &gEfiDiskIoProtocolGuid
,
218 This
->DriverBindingHandle
,
220 EFI_OPEN_PROTOCOL_GET_PROTOCOL
222 if (EFI_ERROR (Status
)) {
223 return EFI_UNSUPPORTED
;
226 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (DiskIo
);
228 Status
= gBS
->UninstallProtocolInterface (
230 &gEfiDiskIoProtocolGuid
,
233 if (!EFI_ERROR (Status
)) {
234 Status
= gBS
->CloseProtocol (
236 &gEfiBlockIoProtocolGuid
,
237 This
->DriverBindingHandle
,
242 if (!EFI_ERROR (Status
)) {
252 Read BufferSize bytes from Offset into Buffer.
253 Reads may support reads that are not aligned on
254 sector boundaries. There are three cases:
255 UnderRun - The first byte is not on a sector boundary or the read request is
256 less than a sector in length.
257 Aligned - A read of N contiguous sectors.
258 OverRun - The last byte is not on a sector boundary.
260 @param This Protocol instance pointer.
261 @param MediaId Id of the media, changes every time the media is replaced.
262 @param Offset The starting byte offset to read from
263 @param BufferSize Size of Buffer
264 @param Buffer Buffer containing read data
266 @retval EFI_SUCCESS The data was read correctly from the device.
267 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
268 @retval EFI_NO_MEDIA There is no media in the device.
269 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
270 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
271 valid for the device.
277 IN EFI_DISK_IO_PROTOCOL
*This
,
285 DISK_IO_PRIVATE_DATA
*Private
;
286 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
287 EFI_BLOCK_IO_MEDIA
*Media
;
293 BOOLEAN TransactionComplete
;
294 UINTN WorkingBufferSize
;
295 UINT8
*WorkingBuffer
;
299 UINTN IsBufferAligned
;
300 UINTN DataBufferSize
;
303 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (This
);
305 BlockIo
= Private
->BlockIo
;
306 Media
= BlockIo
->Media
;
307 BlockSize
= Media
->BlockSize
;
309 if (Media
->MediaId
!= MediaId
) {
310 return EFI_MEDIA_CHANGED
;
313 WorkingBuffer
= Buffer
;
314 WorkingBufferSize
= BufferSize
;
317 // Allocate a temporary buffer for operation
319 DataBufferSize
= BlockSize
* DATA_BUFFER_BLOCK_NUM
;
321 if (Media
->IoAlign
> 1) {
322 PreData
= AllocatePool (DataBufferSize
+ Media
->IoAlign
);
323 Data
= PreData
- ((UINTN
) PreData
& (Media
->IoAlign
- 1)) + Media
->IoAlign
;
325 PreData
= AllocatePool (DataBufferSize
);
329 if (PreData
== NULL
) {
330 return EFI_OUT_OF_RESOURCES
;
333 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
335 Length
= BlockSize
- UnderRun
;
336 TransactionComplete
= FALSE
;
338 Status
= EFI_SUCCESS
;
341 // Offset starts in the middle of an Lba, so read the entire block.
343 Status
= BlockIo
->ReadBlocks (
351 if (EFI_ERROR (Status
)) {
355 if (Length
> BufferSize
) {
357 TransactionComplete
= TRUE
;
360 CopyMem (WorkingBuffer
, Data
+ UnderRun
, Length
);
362 WorkingBuffer
+= Length
;
364 WorkingBufferSize
-= Length
;
365 if (WorkingBufferSize
== 0) {
372 OverRunLba
= Lba
+ DivU64x32Remainder (WorkingBufferSize
, BlockSize
, &OverRun
);
374 if (!TransactionComplete
&& WorkingBufferSize
>= BlockSize
) {
376 // If the DiskIo maps directly to a BlockIo device do the read.
379 WorkingBufferSize
-= OverRun
;
382 // Check buffer alignment
384 IsBufferAligned
= (UINTN
) WorkingBuffer
& (UINTN
) (Media
->IoAlign
- 1);
386 if (Media
->IoAlign
<= 1 || IsBufferAligned
== 0) {
388 // Alignment is satisfied, so read them together
390 Status
= BlockIo
->ReadBlocks (
398 if (EFI_ERROR (Status
)) {
402 WorkingBuffer
+= WorkingBufferSize
;
406 // Use the allocated buffer instead of the original buffer
407 // to avoid alignment issue.
408 // Here, the allocated buffer (8-byte align) can satisfy the alignment
412 if (WorkingBufferSize
<= DataBufferSize
) {
414 // It is the last calling to readblocks in this loop
416 DataBufferSize
= WorkingBufferSize
;
420 Status
= BlockIo
->ReadBlocks (
427 if (EFI_ERROR (Status
)) {
431 CopyMem (WorkingBuffer
, Data
, DataBufferSize
);
432 WorkingBufferSize
-= DataBufferSize
;
433 WorkingBuffer
+= DataBufferSize
;
434 Lba
+= DATA_BUFFER_BLOCK_NUM
;
439 if (!TransactionComplete
&& OverRun
!= 0) {
441 // Last read is not a complete block.
443 Status
= BlockIo
->ReadBlocks (
451 if (EFI_ERROR (Status
)) {
455 CopyMem (WorkingBuffer
, Data
, OverRun
);
459 if (PreData
!= NULL
) {
468 Writes BufferSize bytes from Buffer into Offset.
469 Writes may require a read modify write to support writes that are not
470 aligned on sector boundaries. There are three cases:
471 UnderRun - The first byte is not on a sector boundary or the write request
472 is less than a sector in length. Read modify write is required.
473 Aligned - A write of N contiguous sectors.
474 OverRun - The last byte is not on a sector boundary. Read modified write
477 @param This Protocol instance pointer.
478 @param MediaId Id of the media, changes every time the media is replaced.
479 @param Offset The starting byte offset to read from
480 @param BufferSize Size of Buffer
481 @param Buffer Buffer containing read data
483 @retval EFI_SUCCESS The data was written correctly to the device.
484 @retval EFI_WRITE_PROTECTED The device can not be written to.
485 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
486 @retval EFI_NO_MEDIA There is no media in the device.
487 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
488 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not
489 valid for the device.
495 IN EFI_DISK_IO_PROTOCOL
*This
,
503 DISK_IO_PRIVATE_DATA
*Private
;
504 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
505 EFI_BLOCK_IO_MEDIA
*Media
;
511 BOOLEAN TransactionComplete
;
512 UINTN WorkingBufferSize
;
513 UINT8
*WorkingBuffer
;
517 UINTN IsBufferAligned
;
518 UINTN DataBufferSize
;
521 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (This
);
523 BlockIo
= Private
->BlockIo
;
524 Media
= BlockIo
->Media
;
525 BlockSize
= Media
->BlockSize
;
527 if (Media
->ReadOnly
) {
528 return EFI_WRITE_PROTECTED
;
531 if (Media
->MediaId
!= MediaId
) {
532 return EFI_MEDIA_CHANGED
;
535 DataBufferSize
= BlockSize
* DATA_BUFFER_BLOCK_NUM
;
537 if (Media
->IoAlign
> 1) {
538 PreData
= AllocatePool (DataBufferSize
+ Media
->IoAlign
);
539 Data
= PreData
- ((UINTN
) PreData
& (Media
->IoAlign
- 1)) + Media
->IoAlign
;
541 PreData
= AllocatePool (DataBufferSize
);
545 if (PreData
== NULL
) {
546 return EFI_OUT_OF_RESOURCES
;
549 WorkingBuffer
= Buffer
;
550 WorkingBufferSize
= BufferSize
;
552 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
554 Length
= BlockSize
- UnderRun
;
555 TransactionComplete
= FALSE
;
557 Status
= EFI_SUCCESS
;
560 // Offset starts in the middle of an Lba, so do read modify write.
562 Status
= BlockIo
->ReadBlocks (
570 if (EFI_ERROR (Status
)) {
574 if (Length
> BufferSize
) {
576 TransactionComplete
= TRUE
;
579 CopyMem (Data
+ UnderRun
, WorkingBuffer
, Length
);
581 Status
= BlockIo
->WriteBlocks (
588 if (EFI_ERROR (Status
)) {
592 WorkingBuffer
+= Length
;
593 WorkingBufferSize
-= Length
;
594 if (WorkingBufferSize
== 0) {
601 OverRunLba
= Lba
+ DivU64x32Remainder (WorkingBufferSize
, BlockSize
, &OverRun
);
603 if (!TransactionComplete
&& WorkingBufferSize
>= BlockSize
) {
605 // If the DiskIo maps directly to a BlockIo device do the write.
608 WorkingBufferSize
-= OverRun
;
611 // Check buffer alignment
613 IsBufferAligned
= (UINTN
) WorkingBuffer
& (UINTN
) (Media
->IoAlign
- 1);
615 if (Media
->IoAlign
<= 1 || IsBufferAligned
== 0) {
617 // Alignment is satisfied, so write them together
619 Status
= BlockIo
->WriteBlocks (
627 if (EFI_ERROR (Status
)) {
631 WorkingBuffer
+= WorkingBufferSize
;
635 // The buffer parameter is not aligned with the request
636 // So use the allocated instead.
637 // It can fit almost all the cases.
641 if (WorkingBufferSize
<= DataBufferSize
) {
643 // It is the last calling to writeblocks in this loop
645 DataBufferSize
= WorkingBufferSize
;
649 CopyMem (Data
, WorkingBuffer
, DataBufferSize
);
650 Status
= BlockIo
->WriteBlocks (
657 if (EFI_ERROR (Status
)) {
661 WorkingBufferSize
-= DataBufferSize
;
662 WorkingBuffer
+= DataBufferSize
;
663 Lba
+= DATA_BUFFER_BLOCK_NUM
;
664 } while (!LastWrite
);
668 if (!TransactionComplete
&& OverRun
!= 0) {
670 // Last bit is not a complete block, so do a read modify write.
672 Status
= BlockIo
->ReadBlocks (
680 if (EFI_ERROR (Status
)) {
684 CopyMem (Data
, WorkingBuffer
, OverRun
);
686 Status
= BlockIo
->WriteBlocks (
693 if (EFI_ERROR (Status
)) {
699 if (PreData
!= NULL
) {
708 The user Entry Point for module DiskIo. The user code starts with this function.
710 @param[in] ImageHandle The firmware allocated handle for the EFI image.
711 @param[in] SystemTable A pointer to the EFI System Table.
713 @retval EFI_SUCCESS The entry point is executed successfully.
714 @retval other Some error occurs when executing this entry point.
720 IN EFI_HANDLE ImageHandle
,
721 IN EFI_SYSTEM_TABLE
*SystemTable
727 // Install driver model protocol(s).
729 Status
= EfiLibInstallDriverBindingComponentName2 (
732 &gDiskIoDriverBinding
,
734 &gDiskIoComponentName
,
735 &gDiskIoComponentName2
737 ASSERT_EFI_ERROR (Status
);