3 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #include "EmuBlockIo.h"
12 Reset the block device hardware.
14 @param[in] This Indicates a pointer to the calling context.
15 @param[in] ExtendedVerification Indicates that the driver may perform a more
16 exhausive verfication operation of the device
19 @retval EFI_SUCCESS The device was reset.
20 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
27 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
28 IN BOOLEAN ExtendedVerification
32 EMU_BLOCK_IO_PRIVATE
*Private
;
35 Private
= EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This
);
37 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
39 Status
= Private
->Io
->Reset (Private
->Io
, ExtendedVerification
);
41 gBS
->RestoreTPL (OldTpl
);
46 Read BufferSize bytes from Lba into Buffer.
48 This function reads the requested number of blocks from the device. All the
49 blocks are read, or an error is returned.
50 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and
51 non-blocking I/O is being used, the Event associated with this request will
54 @param[in] This Indicates a pointer to the calling context.
55 @param[in] MediaId Id of the media, changes every time the media is
57 @param[in] Lba The starting Logical Block Address to read from.
58 @param[in, out] Token A pointer to the token associated with the transaction.
59 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
60 @param[out] Buffer A pointer to the destination buffer for the data. The
61 caller is responsible for either having implicit or
62 explicit ownership of the buffer.
64 @retval EFI_SUCCESS The read request was queued if Token->Event is
65 not NULL.The data was read correctly from the
66 device if the Token->Event is NULL.
67 @retval EFI_DEVICE_ERROR The device reported an error while performing
69 @retval EFI_NO_MEDIA There is no media in the device.
70 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
71 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
72 intrinsic block size of the device.
73 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
74 or the buffer is not on proper alignment.
75 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
80 EmuBlockIo2ReadBlocksEx (
81 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
84 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
90 EMU_BLOCK_IO_PRIVATE
*Private
;
93 Private
= EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This
);
95 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
97 Status
= Private
->Io
->ReadBlocks (Private
->Io
, MediaId
, LBA
, Token
, BufferSize
, Buffer
);
99 gBS
->RestoreTPL (OldTpl
);
105 Write BufferSize bytes from Lba into Buffer.
107 This function writes the requested number of blocks to the device. All blocks
108 are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,
109 EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is
110 being used, the Event associated with this request will not be signaled.
112 @param[in] This Indicates a pointer to the calling context.
113 @param[in] MediaId The media ID that the write request is for.
114 @param[in] Lba The starting logical block address to be written. The
115 caller is responsible for writing to only legitimate
117 @param[in, out] Token A pointer to the token associated with the transaction.
118 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
119 @param[in] Buffer A pointer to the source buffer for the data.
121 @retval EFI_SUCCESS The write request was queued if Event is not NULL.
122 The data was written correctly to the device if
124 @retval EFI_WRITE_PROTECTED The device can not be written to.
125 @retval EFI_NO_MEDIA There is no media in the device.
126 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
127 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
128 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
129 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
130 or the buffer is not on proper alignment.
131 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
137 EmuBlockIo2WriteBlocksEx (
138 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
141 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
147 EMU_BLOCK_IO_PRIVATE
*Private
;
150 Private
= EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This
);
152 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
154 Status
= Private
->Io
->WriteBlocks (Private
->Io
, MediaId
, LBA
, Token
, BufferSize
, Buffer
);
156 gBS
->RestoreTPL (OldTpl
);
163 Flush the Block Device.
165 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED
166 is returned and non-blocking I/O is being used, the Event associated with
167 this request will not be signaled.
169 @param[in] This Indicates a pointer to the calling context.
170 @param[in,out] Token A pointer to the token associated with the transaction
172 @retval EFI_SUCCESS The flush request was queued if Event is not NULL.
173 All outstanding data was written correctly to the
174 device if the Event is NULL.
175 @retval EFI_DEVICE_ERROR The device reported an error while writting back
177 @retval EFI_WRITE_PROTECTED The device cannot be written to.
178 @retval EFI_NO_MEDIA There is no media in the device.
179 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
180 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
187 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
188 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
192 EMU_BLOCK_IO_PRIVATE
*Private
;
195 Private
= EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This
);
197 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
199 Status
= Private
->Io
->FlushBlocks (Private
->Io
, Token
);
201 gBS
->RestoreTPL (OldTpl
);
208 Reset the Block Device.
210 @param This Indicates a pointer to the calling context.
211 @param ExtendedVerification Driver may perform diagnostics on reset.
213 @retval EFI_SUCCESS The device was reset.
214 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
221 IN EFI_BLOCK_IO_PROTOCOL
*This
,
222 IN BOOLEAN ExtendedVerification
226 EMU_BLOCK_IO_PRIVATE
*Private
;
229 Private
= EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This
);
231 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
233 Status
= Private
->Io
->Reset (Private
->Io
, ExtendedVerification
);
235 gBS
->RestoreTPL (OldTpl
);
241 Read BufferSize bytes from Lba into Buffer.
243 @param This Indicates a pointer to the calling context.
244 @param MediaId Id of the media, changes every time the media is replaced.
245 @param Lba The starting Logical Block Address to read from
246 @param BufferSize Size of Buffer, must be a multiple of device block size.
247 @param Buffer A pointer to the destination buffer for the data. The caller is
248 responsible for either having implicit or explicit ownership of the buffer.
250 @retval EFI_SUCCESS The data was read correctly from the device.
251 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
252 @retval EFI_NO_MEDIA There is no media in the device.
253 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
254 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
255 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
256 or the buffer is not on proper alignment.
261 EmuBlockIoReadBlocks (
262 IN EFI_BLOCK_IO_PROTOCOL
*This
,
270 EMU_BLOCK_IO_PRIVATE
*Private
;
272 EFI_BLOCK_IO2_TOKEN Token
;
274 Private
= EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This
);
276 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
279 Status
= Private
->Io
->ReadBlocks (Private
->Io
, MediaId
, Lba
, &Token
, BufferSize
, Buffer
);
281 gBS
->RestoreTPL (OldTpl
);
287 Write BufferSize bytes from Lba into Buffer.
289 @param This Indicates a pointer to the calling context.
290 @param MediaId The media ID that the write request is for.
291 @param Lba The starting logical block address to be written. The caller is
292 responsible for writing to only legitimate locations.
293 @param BufferSize Size of Buffer, must be a multiple of device block size.
294 @param Buffer A pointer to the source buffer for the data.
296 @retval EFI_SUCCESS The data was written correctly to the device.
297 @retval EFI_WRITE_PROTECTED The device can not be written to.
298 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
299 @retval EFI_NO_MEDIA There is no media in the device.
300 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
301 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
302 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
303 or the buffer is not on proper alignment.
308 EmuBlockIoWriteBlocks (
309 IN EFI_BLOCK_IO_PROTOCOL
*This
,
317 EMU_BLOCK_IO_PRIVATE
*Private
;
319 EFI_BLOCK_IO2_TOKEN Token
;
321 Private
= EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This
);
323 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
326 Status
= Private
->Io
->WriteBlocks (Private
->Io
, MediaId
, Lba
, &Token
, BufferSize
, Buffer
);
328 gBS
->RestoreTPL (OldTpl
);
333 Flush the Block Device.
335 @param This Indicates a pointer to the calling context.
337 @retval EFI_SUCCESS All outstanding data was written to the device
338 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
339 @retval EFI_NO_MEDIA There is no media in the device.
344 EmuBlockIoFlushBlocks (
345 IN EFI_BLOCK_IO_PROTOCOL
*This
349 EMU_BLOCK_IO_PRIVATE
*Private
;
351 EFI_BLOCK_IO2_TOKEN Token
;
353 Private
= EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This
);
355 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
358 Status
= Private
->Io
->FlushBlocks (Private
->Io
, &Token
);
360 gBS
->RestoreTPL (OldTpl
);
367 Tests to see if this driver supports a given controller. If a child device is provided,
368 it further tests to see if this driver supports creating a handle for the specified child device.
370 This function checks to see if the driver specified by This supports the device specified by
371 ControllerHandle. Drivers will typically use the device path attached to
372 ControllerHandle and/or the services from the bus I/O abstraction attached to
373 ControllerHandle to determine if the driver supports ControllerHandle. This function
374 may be called many times during platform initialization. In order to reduce boot times, the tests
375 performed by this function must be very small, and take as little time as possible to execute. This
376 function must not change the state of any hardware devices, and this function must be aware that the
377 device specified by ControllerHandle may already be managed by the same driver or a
378 different driver. This function must match its calls to AllocatePages() with FreePages(),
379 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
380 Because ControllerHandle may have been previously started by the same driver, if a protocol is
381 already in the opened state, then it must not be closed with CloseProtocol(). This is required
382 to guarantee the state of ControllerHandle is not modified by this function.
384 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
385 @param[in] ControllerHandle The handle of the controller to test. This handle
386 must support a protocol interface that supplies
387 an I/O abstraction to the driver.
388 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
389 parameter is ignored by device drivers, and is optional for bus
390 drivers. For bus drivers, if this parameter is not NULL, then
391 the bus driver must determine if the bus controller specified
392 by ControllerHandle and the child controller specified
393 by RemainingDevicePath are both supported by this
396 @retval EFI_SUCCESS The device specified by ControllerHandle and
397 RemainingDevicePath is supported by the driver specified by This.
398 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
399 RemainingDevicePath is already being managed by the driver
401 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
402 RemainingDevicePath is already being managed by a different
403 driver or an application that requires exclusive access.
404 Currently not implemented.
405 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
406 RemainingDevicePath is not supported by the driver specified by This.
410 EmuBlockIoDriverBindingSupported (
411 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
412 IN EFI_HANDLE Handle
,
413 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
417 EMU_IO_THUNK_PROTOCOL
*EmuIoThunk
;
420 // Open the IO Abstraction(s) needed to perform the supported test
422 Status
= gBS
->OpenProtocol (
424 &gEmuIoThunkProtocolGuid
,
425 (VOID
**)&EmuIoThunk
,
426 This
->DriverBindingHandle
,
428 EFI_OPEN_PROTOCOL_BY_DRIVER
430 if (EFI_ERROR (Status
)) {
435 // Make sure GUID is for a File System handle.
437 Status
= EFI_UNSUPPORTED
;
438 if (CompareGuid (EmuIoThunk
->Protocol
, &gEmuBlockIoProtocolGuid
)) {
439 Status
= EFI_SUCCESS
;
443 // Close the I/O Abstraction(s) used to perform the supported test
447 &gEmuIoThunkProtocolGuid
,
448 This
->DriverBindingHandle
,
456 Starts a device controller or a bus controller.
458 The Start() function is designed to be invoked from the EFI boot service ConnectController().
459 As a result, much of the error checking on the parameters to Start() has been moved into this
460 common boot service. It is legal to call Start() from other locations,
461 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
462 1. ControllerHandle must be a valid EFI_HANDLE.
463 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
464 EFI_DEVICE_PATH_PROTOCOL.
465 3. Prior to calling Start(), the Supported() function for the driver specified by This must
466 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
468 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
469 @param[in] ControllerHandle The handle of the controller to start. This handle
470 must support a protocol interface that supplies
471 an I/O abstraction to the driver.
472 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
473 parameter is ignored by device drivers, and is optional for bus
474 drivers. For a bus driver, if this parameter is NULL, then handles
475 for all the children of Controller are created by this driver.
476 If this parameter is not NULL and the first Device Path Node is
477 not the End of Device Path Node, then only the handle for the
478 child device specified by the first Device Path Node of
479 RemainingDevicePath is created by this driver.
480 If the first Device Path Node of RemainingDevicePath is
481 the End of Device Path Node, no child handle is created by this
484 @retval EFI_SUCCESS The device was started.
485 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
486 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
487 @retval Others The driver failded to start the device.
492 EmuBlockIoDriverBindingStart (
493 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
494 IN EFI_HANDLE Handle
,
495 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
499 EMU_IO_THUNK_PROTOCOL
*EmuIoThunk
;
500 EMU_BLOCK_IO_PRIVATE
*Private
= NULL
;
503 // Grab the protocols we need
506 Status
= gBS
->OpenProtocol (
508 &gEmuIoThunkProtocolGuid
,
510 This
->DriverBindingHandle
,
512 EFI_OPEN_PROTOCOL_BY_DRIVER
514 if (EFI_ERROR (Status
)) {
518 if (!CompareGuid (EmuIoThunk
->Protocol
, &gEmuBlockIoProtocolGuid
)) {
519 Status
= EFI_UNSUPPORTED
;
523 Status
= EmuIoThunk
->Open (EmuIoThunk
);
524 if (EFI_ERROR (Status
)) {
528 Private
= AllocatePool (sizeof (EMU_BLOCK_IO_PRIVATE
));
529 if (Private
== NULL
) {
533 Private
->Signature
= EMU_BLOCK_IO_PRIVATE_SIGNATURE
;
534 Private
->IoThunk
= EmuIoThunk
;
535 Private
->Io
= EmuIoThunk
->Interface
;
536 Private
->EfiHandle
= Handle
;
538 Private
->BlockIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION2
;
539 Private
->BlockIo
.Media
= &Private
->Media
;
540 Private
->BlockIo
.Reset
= EmuBlockIoReset
;
541 Private
->BlockIo
.ReadBlocks
= EmuBlockIoReadBlocks
;
542 Private
->BlockIo
.WriteBlocks
= EmuBlockIoWriteBlocks
;
543 Private
->BlockIo
.FlushBlocks
= EmuBlockIoFlushBlocks
;
545 Private
->BlockIo2
.Media
= &Private
->Media
;
546 Private
->BlockIo2
.Reset
= EmuBlockIo2Reset
;
547 Private
->BlockIo2
.ReadBlocksEx
= EmuBlockIo2ReadBlocksEx
;
548 Private
->BlockIo2
.WriteBlocksEx
= EmuBlockIo2WriteBlocksEx
;
549 Private
->BlockIo2
.FlushBlocksEx
= EmuBlockIo2Flush
;
551 Private
->ControllerNameTable
= NULL
;
553 Status
= Private
->Io
->CreateMapping (Private
->Io
, &Private
->Media
);
554 if (EFI_ERROR (Status
)) {
560 gEmuBlockIoComponentName
.SupportedLanguages
,
561 &Private
->ControllerNameTable
,
562 EmuIoThunk
->ConfigString
,
568 gEmuBlockIoComponentName2
.SupportedLanguages
,
569 &Private
->ControllerNameTable
,
570 EmuIoThunk
->ConfigString
,
574 Status
= gBS
->InstallMultipleProtocolInterfaces (
576 &gEfiBlockIoProtocolGuid
, &Private
->BlockIo
,
577 &gEfiBlockIo2ProtocolGuid
, &Private
->BlockIo2
,
582 if (EFI_ERROR (Status
)) {
583 if (Private
!= NULL
) {
584 if (Private
->ControllerNameTable
!= NULL
) {
585 FreeUnicodeStringTable (Private
->ControllerNameTable
);
588 gBS
->FreePool (Private
);
594 &gEmuIoThunkProtocolGuid
,
595 This
->DriverBindingHandle
,
605 Stops a device controller or a bus controller.
607 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
608 As a result, much of the error checking on the parameters to Stop() has been moved
609 into this common boot service. It is legal to call Stop() from other locations,
610 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
611 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
612 same driver's Start() function.
613 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
614 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
615 Start() function, and the Start() function must have called OpenProtocol() on
616 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
618 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
619 @param[in] ControllerHandle A handle to the device being stopped. The handle must
620 support a bus specific I/O protocol for the driver
621 to use to stop the device.
622 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
623 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
624 if NumberOfChildren is 0.
626 @retval EFI_SUCCESS The device was stopped.
627 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
632 EmuBlockIoDriverBindingStop (
633 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
634 IN EFI_HANDLE Handle
,
635 IN UINTN NumberOfChildren
,
636 IN EFI_HANDLE
*ChildHandleBuffer
639 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
641 EMU_BLOCK_IO_PRIVATE
*Private
;
644 // Get our context back
646 Status
= gBS
->OpenProtocol (
648 &gEfiBlockIoProtocolGuid
,
650 This
->DriverBindingHandle
,
652 EFI_OPEN_PROTOCOL_GET_PROTOCOL
654 if (EFI_ERROR (Status
)) {
655 return EFI_UNSUPPORTED
;
658 Private
= EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (BlockIo
);
660 Status
= gBS
->UninstallMultipleProtocolInterfaces (
662 &gEfiBlockIoProtocolGuid
, &Private
->BlockIo
,
663 &gEfiBlockIo2ProtocolGuid
, &Private
->BlockIo2
,
666 if (!EFI_ERROR (Status
)) {
667 Status
= gBS
->CloseProtocol (
669 &gEmuIoThunkProtocolGuid
,
670 This
->DriverBindingHandle
,
673 ASSERT_EFI_ERROR (Status
);
675 // Destroy the IO interface.
677 Status
= Private
->IoThunk
->Close (Private
->IoThunk
);
678 ASSERT_EFI_ERROR (Status
);
680 // Free our instance data
682 FreeUnicodeStringTable (Private
->ControllerNameTable
);
693 EFI_DRIVER_BINDING_PROTOCOL gEmuBlockIoDriverBinding
= {
694 EmuBlockIoDriverBindingSupported
,
695 EmuBlockIoDriverBindingStart
,
696 EmuBlockIoDriverBindingStop
,
706 The user Entry Point for module EmuBlockIo . The user code starts with this function.
708 @param[in] ImageHandle The firmware allocated handle for the EFI image.
709 @param[in] SystemTable A pointer to the EFI System Table.
711 @retval EFI_SUCCESS The entry point is executed successfully.
712 @retval other Some error occurs when executing this entry point.
717 InitializeEmuBlockIo (
718 IN EFI_HANDLE ImageHandle
,
719 IN EFI_SYSTEM_TABLE
*SystemTable
724 Status
= EfiLibInstallAllDriverProtocols2 (
727 &gEmuBlockIoDriverBinding
,
729 &gEmuBlockIoComponentName
,
730 &gEmuBlockIoComponentName2
,
733 &gEmuBlockIoDriverDiagnostics
,
734 &gEmuBlockIoDriverDiagnostics2
736 ASSERT_EFI_ERROR (Status
);