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. 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
,
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
;
130 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
134 // Connect to the Block IO interface on ControllerHandle.
136 Status
= gBS
->OpenProtocol (
138 &gEfiBlockIoProtocolGuid
,
139 (VOID
**) &gDiskIoPrivateDataTemplate
.BlockIo
,
140 This
->DriverBindingHandle
,
142 EFI_OPEN_PROTOCOL_BY_DRIVER
144 if (EFI_ERROR (Status
)) {
149 // Initialize the Disk IO device instance.
151 Private
= AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA
), &gDiskIoPrivateDataTemplate
);
152 if (Private
== NULL
) {
153 Status
= EFI_OUT_OF_RESOURCES
;
158 // Install protocol interfaces for the Disk IO device.
160 Status
= gBS
->InstallProtocolInterface (
162 &gEfiDiskIoProtocolGuid
,
163 EFI_NATIVE_INTERFACE
,
168 if (EFI_ERROR (Status
)) {
170 if (Private
!= NULL
) {
176 &gEfiBlockIoProtocolGuid
,
177 This
->DriverBindingHandle
,
183 gBS
->RestoreTPL (OldTpl
);
189 Stop this driver on ControllerHandle by removing Disk IO protocol and closing
190 the Block IO protocol on ControllerHandle.
192 @param This Protocol instance pointer.
193 @param ControllerHandle Handle of device to stop driver on
194 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
195 children is zero stop the entire bus driver.
196 @param ChildHandleBuffer List of Child Handles to Stop.
198 @retval EFI_SUCCESS This driver is removed ControllerHandle
199 @retval other This driver was not removed from this device
204 DiskIoDriverBindingStop (
205 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
206 IN EFI_HANDLE ControllerHandle
,
207 IN UINTN NumberOfChildren
,
208 IN EFI_HANDLE
*ChildHandleBuffer
212 EFI_DISK_IO_PROTOCOL
*DiskIo
;
213 DISK_IO_PRIVATE_DATA
*Private
;
216 // Get our context back.
218 Status
= gBS
->OpenProtocol (
220 &gEfiDiskIoProtocolGuid
,
222 This
->DriverBindingHandle
,
224 EFI_OPEN_PROTOCOL_GET_PROTOCOL
226 if (EFI_ERROR (Status
)) {
227 return EFI_UNSUPPORTED
;
230 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (DiskIo
);
232 Status
= gBS
->UninstallProtocolInterface (
234 &gEfiDiskIoProtocolGuid
,
237 if (!EFI_ERROR (Status
)) {
238 Status
= gBS
->CloseProtocol (
240 &gEfiBlockIoProtocolGuid
,
241 This
->DriverBindingHandle
,
246 if (!EFI_ERROR (Status
)) {
256 Read BufferSize bytes from Offset into Buffer.
257 Reads may support reads that are not aligned on
258 sector boundaries. There are three cases:
259 UnderRun - The first byte is not on a sector boundary or the read request is
260 less than a sector in length.
261 Aligned - A read of N contiguous sectors.
262 OverRun - The last byte is not on a sector boundary.
264 @param This Protocol instance pointer.
265 @param MediaId Id of the media, changes every time the media is replaced.
266 @param Offset The starting byte offset to read from
267 @param BufferSize Size of Buffer
268 @param Buffer Buffer containing read data
270 @retval EFI_SUCCESS The data was read correctly from the device.
271 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
272 @retval EFI_NO_MEDIA There is no media in the device.
273 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
274 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
275 valid for the device.
281 IN EFI_DISK_IO_PROTOCOL
*This
,
289 DISK_IO_PRIVATE_DATA
*Private
;
290 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
291 EFI_BLOCK_IO_MEDIA
*Media
;
297 BOOLEAN TransactionComplete
;
298 UINTN WorkingBufferSize
;
299 UINT8
*WorkingBuffer
;
303 UINTN IsBufferAligned
;
304 UINTN DataBufferSize
;
307 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (This
);
309 BlockIo
= Private
->BlockIo
;
310 Media
= BlockIo
->Media
;
311 BlockSize
= Media
->BlockSize
;
313 if (Media
->MediaId
!= MediaId
) {
314 return EFI_MEDIA_CHANGED
;
317 WorkingBuffer
= Buffer
;
318 WorkingBufferSize
= BufferSize
;
321 // Allocate a temporary buffer for operation
323 DataBufferSize
= BlockSize
* DATA_BUFFER_BLOCK_NUM
;
325 if (Media
->IoAlign
> 1) {
326 PreData
= AllocatePool (DataBufferSize
+ Media
->IoAlign
);
327 Data
= PreData
- ((UINTN
) PreData
& (Media
->IoAlign
- 1)) + Media
->IoAlign
;
329 PreData
= AllocatePool (DataBufferSize
);
333 if (PreData
== NULL
) {
334 return EFI_OUT_OF_RESOURCES
;
337 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
339 Length
= BlockSize
- UnderRun
;
340 TransactionComplete
= FALSE
;
342 Status
= EFI_SUCCESS
;
345 // Offset starts in the middle of an Lba, so read the entire block.
347 Status
= BlockIo
->ReadBlocks (
355 if (EFI_ERROR (Status
)) {
359 if (Length
> BufferSize
) {
361 TransactionComplete
= TRUE
;
364 CopyMem (WorkingBuffer
, Data
+ UnderRun
, Length
);
366 WorkingBuffer
+= Length
;
368 WorkingBufferSize
-= Length
;
369 if (WorkingBufferSize
== 0) {
376 OverRunLba
= Lba
+ DivU64x32Remainder (WorkingBufferSize
, BlockSize
, &OverRun
);
378 if (!TransactionComplete
&& WorkingBufferSize
>= BlockSize
) {
380 // If the DiskIo maps directly to a BlockIo device do the read.
383 WorkingBufferSize
-= OverRun
;
386 // Check buffer alignment
388 IsBufferAligned
= (UINTN
) WorkingBuffer
& (UINTN
) (Media
->IoAlign
- 1);
390 if (Media
->IoAlign
<= 1 || IsBufferAligned
== 0) {
392 // Alignment is satisfied, so read them together
394 Status
= BlockIo
->ReadBlocks (
402 if (EFI_ERROR (Status
)) {
406 WorkingBuffer
+= WorkingBufferSize
;
410 // Use the allocated buffer instead of the original buffer
411 // to avoid alignment issue.
412 // Here, the allocated buffer (8-byte align) can satisfy the alignment
416 if (WorkingBufferSize
<= DataBufferSize
) {
418 // It is the last calling to readblocks in this loop
420 DataBufferSize
= WorkingBufferSize
;
424 Status
= BlockIo
->ReadBlocks (
431 if (EFI_ERROR (Status
)) {
435 CopyMem (WorkingBuffer
, Data
, DataBufferSize
);
436 WorkingBufferSize
-= DataBufferSize
;
437 WorkingBuffer
+= DataBufferSize
;
438 Lba
+= DATA_BUFFER_BLOCK_NUM
;
443 if (!TransactionComplete
&& OverRun
!= 0) {
445 // Last read is not a complete block.
447 Status
= BlockIo
->ReadBlocks (
455 if (EFI_ERROR (Status
)) {
459 CopyMem (WorkingBuffer
, Data
, OverRun
);
463 if (PreData
!= NULL
) {
472 Writes BufferSize bytes from Buffer into Offset.
473 Writes may require a read modify write to support writes that are not
474 aligned on sector boundaries. There are three cases:
475 UnderRun - The first byte is not on a sector boundary or the write request
476 is less than a sector in length. Read modify write is required.
477 Aligned - A write of N contiguous sectors.
478 OverRun - The last byte is not on a sector boundary. Read modified write
481 @param This Protocol instance pointer.
482 @param MediaId Id of the media, changes every time the media is replaced.
483 @param Offset The starting byte offset to read from
484 @param BufferSize Size of Buffer
485 @param Buffer Buffer containing read data
487 @retval EFI_SUCCESS The data was written correctly to the device.
488 @retval EFI_WRITE_PROTECTED The device can not be written to.
489 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
490 @retval EFI_NO_MEDIA There is no media in the device.
491 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
492 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not
493 valid for the device.
499 IN EFI_DISK_IO_PROTOCOL
*This
,
507 DISK_IO_PRIVATE_DATA
*Private
;
508 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
509 EFI_BLOCK_IO_MEDIA
*Media
;
515 BOOLEAN TransactionComplete
;
516 UINTN WorkingBufferSize
;
517 UINT8
*WorkingBuffer
;
521 UINTN IsBufferAligned
;
522 UINTN DataBufferSize
;
525 Private
= DISK_IO_PRIVATE_DATA_FROM_THIS (This
);
527 BlockIo
= Private
->BlockIo
;
528 Media
= BlockIo
->Media
;
529 BlockSize
= Media
->BlockSize
;
531 if (Media
->ReadOnly
) {
532 return EFI_WRITE_PROTECTED
;
535 if (Media
->MediaId
!= MediaId
) {
536 return EFI_MEDIA_CHANGED
;
539 DataBufferSize
= BlockSize
* DATA_BUFFER_BLOCK_NUM
;
541 if (Media
->IoAlign
> 1) {
542 PreData
= AllocatePool (DataBufferSize
+ Media
->IoAlign
);
543 Data
= PreData
- ((UINTN
) PreData
& (Media
->IoAlign
- 1)) + Media
->IoAlign
;
545 PreData
= AllocatePool (DataBufferSize
);
549 if (PreData
== NULL
) {
550 return EFI_OUT_OF_RESOURCES
;
553 WorkingBuffer
= Buffer
;
554 WorkingBufferSize
= BufferSize
;
556 Lba
= DivU64x32Remainder (Offset
, BlockSize
, &UnderRun
);
558 Length
= BlockSize
- UnderRun
;
559 TransactionComplete
= FALSE
;
561 Status
= EFI_SUCCESS
;
564 // Offset starts in the middle of an Lba, so do read modify write.
566 Status
= BlockIo
->ReadBlocks (
574 if (EFI_ERROR (Status
)) {
578 if (Length
> BufferSize
) {
580 TransactionComplete
= TRUE
;
583 CopyMem (Data
+ UnderRun
, WorkingBuffer
, Length
);
585 Status
= BlockIo
->WriteBlocks (
592 if (EFI_ERROR (Status
)) {
596 WorkingBuffer
+= Length
;
597 WorkingBufferSize
-= Length
;
598 if (WorkingBufferSize
== 0) {
605 OverRunLba
= Lba
+ DivU64x32Remainder (WorkingBufferSize
, BlockSize
, &OverRun
);
607 if (!TransactionComplete
&& WorkingBufferSize
>= BlockSize
) {
609 // If the DiskIo maps directly to a BlockIo device do the write.
612 WorkingBufferSize
-= OverRun
;
615 // Check buffer alignment
617 IsBufferAligned
= (UINTN
) WorkingBuffer
& (UINTN
) (Media
->IoAlign
- 1);
619 if (Media
->IoAlign
<= 1 || IsBufferAligned
== 0) {
621 // Alignment is satisfied, so write them together
623 Status
= BlockIo
->WriteBlocks (
631 if (EFI_ERROR (Status
)) {
635 WorkingBuffer
+= WorkingBufferSize
;
639 // The buffer parameter is not aligned with the request
640 // So use the allocated instead.
641 // It can fit almost all the cases.
645 if (WorkingBufferSize
<= DataBufferSize
) {
647 // It is the last calling to writeblocks in this loop
649 DataBufferSize
= WorkingBufferSize
;
653 CopyMem (Data
, WorkingBuffer
, DataBufferSize
);
654 Status
= BlockIo
->WriteBlocks (
661 if (EFI_ERROR (Status
)) {
665 WorkingBufferSize
-= DataBufferSize
;
666 WorkingBuffer
+= DataBufferSize
;
667 Lba
+= DATA_BUFFER_BLOCK_NUM
;
668 } while (!LastWrite
);
672 if (!TransactionComplete
&& OverRun
!= 0) {
674 // Last bit is not a complete block, so do a read modify write.
676 Status
= BlockIo
->ReadBlocks (
684 if (EFI_ERROR (Status
)) {
688 CopyMem (Data
, WorkingBuffer
, OverRun
);
690 Status
= BlockIo
->WriteBlocks (
697 if (EFI_ERROR (Status
)) {
703 if (PreData
!= NULL
) {
712 The user Entry Point for module DiskIo. The user code starts with this function.
714 @param[in] ImageHandle The firmware allocated handle for the EFI image.
715 @param[in] SystemTable A pointer to the EFI System Table.
717 @retval EFI_SUCCESS The entry point is executed successfully.
718 @retval other Some error occurs when executing this entry point.
724 IN EFI_HANDLE ImageHandle
,
725 IN EFI_SYSTEM_TABLE
*SystemTable
731 // Install driver model protocol(s).
733 Status
= EfiLibInstallDriverBindingComponentName2 (
736 &gDiskIoDriverBinding
,
738 &gDiskIoComponentName
,
739 &gDiskIoComponentName2
741 ASSERT_EFI_ERROR (Status
);