2 USB Mass Storage Driver that manages USB Mass Storage Device and produces Block I/O Protocol.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #define USB_MASS_TRANSPORT_COUNT 3
13 // Array of USB transport interfaces.
15 USB_MASS_TRANSPORT
*mUsbMassTransport
[USB_MASS_TRANSPORT_COUNT
] = {
21 EFI_DRIVER_BINDING_PROTOCOL gUSBMassDriverBinding
= {
22 USBMassDriverBindingSupported
,
23 USBMassDriverBindingStart
,
24 USBMassDriverBindingStop
,
31 Reset the block device.
33 This function implements EFI_BLOCK_IO_PROTOCOL.Reset().
34 It resets the block device hardware.
35 ExtendedVerification is ignored in this implementation.
37 @param This Indicates a pointer to the calling context.
38 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
39 verification operation of the device during reset.
41 @retval EFI_SUCCESS The block device was reset.
42 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be reset.
48 IN EFI_BLOCK_IO_PROTOCOL
*This
,
49 IN BOOLEAN ExtendedVerification
52 USB_MASS_DEVICE
*UsbMass
;
57 // Raise TPL to TPL_CALLBACK to serialize all its operations
58 // to protect shared data structures.
60 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
62 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (This
);
63 Status
= UsbMass
->Transport
->Reset (UsbMass
->Context
, ExtendedVerification
);
65 gBS
->RestoreTPL (OldTpl
);
71 Reads the requested number of blocks from the device.
73 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks().
74 It reads the requested number of blocks from the device.
75 All the blocks are read, or an error is returned.
77 @param This Indicates a pointer to the calling context.
78 @param MediaId The media ID that the read request is for.
79 @param Lba The starting logical block address to read from on the device.
80 @param BufferSize The size of the Buffer in bytes.
81 This must be a multiple of the intrinsic block size of the device.
82 @param Buffer A pointer to the destination buffer for the data. The caller is
83 responsible for either having implicit or explicit ownership of the buffer.
85 @retval EFI_SUCCESS The data was read correctly from the device.
86 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.
87 @retval EFI_NO_MEDIA There is no media in the device.
88 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
89 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.
90 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
91 or the buffer is not on proper alignment.
97 IN EFI_BLOCK_IO_PROTOCOL
*This
,
104 USB_MASS_DEVICE
*UsbMass
;
105 EFI_BLOCK_IO_MEDIA
*Media
;
111 // Raise TPL to TPL_CALLBACK to serialize all its operations
112 // to protect shared data structures.
114 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
115 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (This
);
116 Media
= &UsbMass
->BlockIoMedia
;
119 // If it is a removable media, such as CD-Rom or Usb-Floppy,
120 // need to detect the media before each read/write. While some of
121 // Usb-Flash is marked as removable media.
123 if (Media
->RemovableMedia
) {
124 Status
= UsbBootDetectMedia (UsbMass
);
125 if (EFI_ERROR (Status
)) {
130 if (!(Media
->MediaPresent
)) {
131 Status
= EFI_NO_MEDIA
;
135 if (MediaId
!= Media
->MediaId
) {
136 Status
= EFI_MEDIA_CHANGED
;
140 if (BufferSize
== 0) {
141 Status
= EFI_SUCCESS
;
145 if (Buffer
== NULL
) {
146 Status
= EFI_INVALID_PARAMETER
;
151 // BufferSize must be a multiple of the intrinsic block size of the device.
153 if ((BufferSize
% Media
->BlockSize
) != 0) {
154 Status
= EFI_BAD_BUFFER_SIZE
;
158 TotalBlock
= BufferSize
/ Media
->BlockSize
;
161 // Make sure the range to read is valid.
163 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
164 Status
= EFI_INVALID_PARAMETER
;
168 if (UsbMass
->Cdb16Byte
) {
169 Status
= UsbBootReadWriteBlocks16 (UsbMass
, FALSE
, Lba
, TotalBlock
, Buffer
);
171 Status
= UsbBootReadWriteBlocks (UsbMass
, FALSE
, (UINT32
)Lba
, TotalBlock
, Buffer
);
174 if (EFI_ERROR (Status
)) {
175 DEBUG ((DEBUG_ERROR
, "UsbMassReadBlocks: UsbBootReadBlocks (%r) -> Reset\n", Status
));
176 UsbMassReset (This
, TRUE
);
180 gBS
->RestoreTPL (OldTpl
);
185 Writes a specified number of blocks to the device.
187 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
188 It writes a specified number of blocks to the device.
189 All blocks are written, or an error is returned.
191 @param This Indicates a pointer to the calling context.
192 @param MediaId The media ID that the write request is for.
193 @param Lba The starting logical block address to be written.
194 @param BufferSize The size of the Buffer in bytes.
195 This must be a multiple of the intrinsic block size of the device.
196 @param Buffer Pointer to the source buffer for the data.
198 @retval EFI_SUCCESS The data were written correctly to the device.
199 @retval EFI_WRITE_PROTECTED The device cannot be written to.
200 @retval EFI_NO_MEDIA There is no media in the device.
201 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
202 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.
203 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic
204 block size of the device.
205 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
206 or the buffer is not on proper alignment.
212 IN EFI_BLOCK_IO_PROTOCOL
*This
,
219 USB_MASS_DEVICE
*UsbMass
;
220 EFI_BLOCK_IO_MEDIA
*Media
;
226 // Raise TPL to TPL_CALLBACK to serialize all its operations
227 // to protect shared data structures.
229 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
230 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (This
);
231 Media
= &UsbMass
->BlockIoMedia
;
234 // If it is a removable media, such as CD-Rom or Usb-Floppy,
235 // need to detect the media before each read/write. Some of
236 // USB Flash is marked as removable media.
238 if (Media
->RemovableMedia
) {
239 Status
= UsbBootDetectMedia (UsbMass
);
240 if (EFI_ERROR (Status
)) {
245 if (!(Media
->MediaPresent
)) {
246 Status
= EFI_NO_MEDIA
;
250 if (MediaId
!= Media
->MediaId
) {
251 Status
= EFI_MEDIA_CHANGED
;
255 if (BufferSize
== 0) {
256 Status
= EFI_SUCCESS
;
260 if (Buffer
== NULL
) {
261 Status
= EFI_INVALID_PARAMETER
;
266 // BufferSize must be a multiple of the intrinsic block size of the device.
268 if ((BufferSize
% Media
->BlockSize
) != 0) {
269 Status
= EFI_BAD_BUFFER_SIZE
;
273 TotalBlock
= BufferSize
/ Media
->BlockSize
;
276 // Make sure the range to write is valid.
278 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
279 Status
= EFI_INVALID_PARAMETER
;
284 // Try to write the data even the device is marked as ReadOnly,
285 // and clear the status should the write succeed.
287 if (UsbMass
->Cdb16Byte
) {
288 Status
= UsbBootReadWriteBlocks16 (UsbMass
, TRUE
, Lba
, TotalBlock
, Buffer
);
290 Status
= UsbBootReadWriteBlocks (UsbMass
, TRUE
, (UINT32
)Lba
, TotalBlock
, Buffer
);
293 if (EFI_ERROR (Status
)) {
294 DEBUG ((DEBUG_ERROR
, "UsbMassWriteBlocks: UsbBootWriteBlocks (%r) -> Reset\n", Status
));
295 UsbMassReset (This
, TRUE
);
299 gBS
->RestoreTPL (OldTpl
);
304 Flushes all modified data to a physical block device.
306 This function implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks().
307 USB mass storage device doesn't support write cache,
308 so return EFI_SUCCESS directly.
310 @param This Indicates a pointer to the calling context.
312 @retval EFI_SUCCESS All outstanding data were written correctly to the device.
313 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.
314 @retval EFI_NO_MEDIA There is no media in the device.
320 IN EFI_BLOCK_IO_PROTOCOL
*This
327 Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
329 @param UsbMass The USB mass storage device
331 @retval EFI_SUCCESS The media parameters are updated successfully.
332 @retval Others Failed to get the media parameters.
337 IN USB_MASS_DEVICE
*UsbMass
340 EFI_BLOCK_IO_MEDIA
*Media
;
343 Media
= &UsbMass
->BlockIoMedia
;
346 // Fields of EFI_BLOCK_IO_MEDIA are defined in UEFI 2.0 spec,
347 // section for Block I/O Protocol.
349 Media
->MediaPresent
= FALSE
;
350 Media
->LogicalPartition
= FALSE
;
351 Media
->ReadOnly
= FALSE
;
352 Media
->WriteCaching
= FALSE
;
356 Status
= UsbBootGetParams (UsbMass
);
357 DEBUG ((DEBUG_INFO
, "UsbMassInitMedia: UsbBootGetParams (%r)\n", Status
));
358 if (Status
== EFI_MEDIA_CHANGED
) {
360 // Some USB storage devices may report MEDIA_CHANGED sense key when hot-plugged.
361 // Treat it as SUCCESS
363 Status
= EFI_SUCCESS
;
370 Initialize the USB Mass Storage transport.
372 This function tries to find the matching USB Mass Storage transport
373 protocol for USB device. If found, initializes the matching transport.
375 @param This The USB mass driver's driver binding.
376 @param Controller The device to test.
377 @param Transport The pointer to pointer to USB_MASS_TRANSPORT.
378 @param Context The parameter for USB_MASS_DEVICE.Context.
379 @param MaxLun Get the MaxLun if is BOT dev.
381 @retval EFI_SUCCESS The initialization is successful.
382 @retval EFI_UNSUPPORTED No matching transport protocol is found.
383 @retval Others Failed to initialize dev.
387 UsbMassInitTransport (
388 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
389 IN EFI_HANDLE Controller
,
390 OUT USB_MASS_TRANSPORT
**Transport
,
395 EFI_USB_IO_PROTOCOL
*UsbIo
;
396 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
400 Status
= gBS
->OpenProtocol (
402 &gEfiUsbIoProtocolGuid
,
404 This
->DriverBindingHandle
,
406 EFI_OPEN_PROTOCOL_BY_DRIVER
409 if (EFI_ERROR (Status
)) {
413 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
414 if (EFI_ERROR (Status
)) {
418 Status
= EFI_UNSUPPORTED
;
421 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
422 // matching transport protocol.
423 // If not found, return EFI_UNSUPPORTED.
424 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
426 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
427 *Transport
= mUsbMassTransport
[Index
];
429 if (Interface
.InterfaceProtocol
== (*Transport
)->Protocol
) {
430 Status
= (*Transport
)->Init (UsbIo
, Context
);
435 if (EFI_ERROR (Status
)) {
440 // For BOT device, try to get its max LUN.
441 // If max LUN is 0, then it is a non-lun device.
442 // Otherwise, it is a multi-lun device.
444 if ((*Transport
)->Protocol
== USB_MASS_STORE_BOT
) {
445 (*Transport
)->GetMaxLun (*Context
, MaxLun
);
451 &gEfiUsbIoProtocolGuid
,
452 This
->DriverBindingHandle
,
459 Initialize data for device that supports multiple LUNSs.
461 @param This The Driver Binding Protocol instance.
462 @param Controller The device to initialize.
463 @param Transport Pointer to USB_MASS_TRANSPORT.
464 @param Context Parameter for USB_MASS_DEVICE.Context.
465 @param DevicePath The remaining device path.
466 @param MaxLun The max LUN number.
468 @retval EFI_SUCCESS At least one LUN is initialized successfully.
469 @retval EFI_NOT_FOUND Fail to initialize any of multiple LUNs.
473 UsbMassInitMultiLun (
474 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
475 IN EFI_HANDLE Controller
,
476 IN USB_MASS_TRANSPORT
*Transport
,
478 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
482 USB_MASS_DEVICE
*UsbMass
;
483 EFI_USB_IO_PROTOCOL
*UsbIo
;
484 DEVICE_LOGICAL_UNIT_DEVICE_PATH LunNode
;
487 EFI_STATUS ReturnStatus
;
490 ReturnStatus
= EFI_NOT_FOUND
;
492 for (Index
= 0; Index
<= MaxLun
; Index
++) {
493 DEBUG ((DEBUG_INFO
, "UsbMassInitMultiLun: Start to initialize No.%d logic unit\n", Index
));
496 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
497 ASSERT (UsbMass
!= NULL
);
499 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
500 UsbMass
->UsbIo
= UsbIo
;
501 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
502 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
503 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
504 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
505 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
506 UsbMass
->OpticalStorage
= FALSE
;
507 UsbMass
->Transport
= Transport
;
508 UsbMass
->Context
= Context
;
509 UsbMass
->Lun
= Index
;
512 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
514 Status
= UsbMassInitMedia (UsbMass
);
515 if ((EFI_ERROR (Status
)) && (Status
!= EFI_NO_MEDIA
)) {
516 DEBUG ((DEBUG_ERROR
, "UsbMassInitMultiLun: UsbMassInitMedia (%r)\n", Status
));
522 // Create a device path node for device logic unit, and append it.
524 LunNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
525 LunNode
.Header
.SubType
= MSG_DEVICE_LOGICAL_UNIT_DP
;
526 LunNode
.Lun
= UsbMass
->Lun
;
528 SetDevicePathNodeLength (&LunNode
.Header
, sizeof (LunNode
));
530 UsbMass
->DevicePath
= AppendDevicePathNode (DevicePath
, &LunNode
.Header
);
532 if (UsbMass
->DevicePath
== NULL
) {
533 DEBUG ((DEBUG_ERROR
, "UsbMassInitMultiLun: failed to create device logic unit device path\n"));
534 Status
= EFI_OUT_OF_RESOURCES
;
539 InitializeDiskInfo (UsbMass
);
542 // Create a new handle for each LUN, and install Block I/O Protocol and Device Path Protocol.
544 Status
= gBS
->InstallMultipleProtocolInterfaces (
545 &UsbMass
->Controller
,
546 &gEfiDevicePathProtocolGuid
,
548 &gEfiBlockIoProtocolGuid
,
550 &gEfiDiskInfoProtocolGuid
,
555 if (EFI_ERROR (Status
)) {
556 DEBUG ((DEBUG_ERROR
, "UsbMassInitMultiLun: InstallMultipleProtocolInterfaces (%r)\n", Status
));
557 FreePool (UsbMass
->DevicePath
);
563 // Open USB I/O Protocol by child to setup a parent-child relationship.
565 Status
= gBS
->OpenProtocol (
567 &gEfiUsbIoProtocolGuid
,
569 This
->DriverBindingHandle
,
571 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
574 if (EFI_ERROR (Status
)) {
575 DEBUG ((DEBUG_ERROR
, "UsbMassInitMultiLun: OpenUsbIoProtocol By Child (%r)\n", Status
));
576 gBS
->UninstallMultipleProtocolInterfaces (
578 &gEfiDevicePathProtocolGuid
,
580 &gEfiBlockIoProtocolGuid
,
582 &gEfiDiskInfoProtocolGuid
,
586 FreePool (UsbMass
->DevicePath
);
591 ReturnStatus
= EFI_SUCCESS
;
592 DEBUG ((DEBUG_INFO
, "UsbMassInitMultiLun: Success to initialize No.%d logic unit\n", Index
));
599 Initialize data for device that does not support multiple LUNSs.
601 @param This The Driver Binding Protocol instance.
602 @param Controller The device to initialize.
603 @param Transport Pointer to USB_MASS_TRANSPORT.
604 @param Context Parameter for USB_MASS_DEVICE.Context.
606 @retval EFI_SUCCESS Initialization succeeds.
607 @retval Other Initialization fails.
612 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
613 IN EFI_HANDLE Controller
,
614 IN USB_MASS_TRANSPORT
*Transport
,
618 USB_MASS_DEVICE
*UsbMass
;
619 EFI_USB_IO_PROTOCOL
*UsbIo
;
623 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
624 ASSERT (UsbMass
!= NULL
);
626 Status
= gBS
->OpenProtocol (
628 &gEfiUsbIoProtocolGuid
,
630 This
->DriverBindingHandle
,
632 EFI_OPEN_PROTOCOL_BY_DRIVER
635 if (EFI_ERROR (Status
)) {
636 DEBUG ((DEBUG_ERROR
, "UsbMassInitNonLun: OpenUsbIoProtocol By Driver (%r)\n", Status
));
640 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
641 UsbMass
->Controller
= Controller
;
642 UsbMass
->UsbIo
= UsbIo
;
643 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
644 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
645 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
646 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
647 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
648 UsbMass
->OpticalStorage
= FALSE
;
649 UsbMass
->Transport
= Transport
;
650 UsbMass
->Context
= Context
;
653 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
655 Status
= UsbMassInitMedia (UsbMass
);
656 if ((EFI_ERROR (Status
)) && (Status
!= EFI_NO_MEDIA
)) {
657 DEBUG ((DEBUG_ERROR
, "UsbMassInitNonLun: UsbMassInitMedia (%r)\n", Status
));
661 InitializeDiskInfo (UsbMass
);
663 Status
= gBS
->InstallMultipleProtocolInterfaces (
665 &gEfiBlockIoProtocolGuid
,
667 &gEfiDiskInfoProtocolGuid
,
671 if (EFI_ERROR (Status
)) {
678 if (UsbMass
!= NULL
) {
685 &gEfiUsbIoProtocolGuid
,
686 This
->DriverBindingHandle
,
695 Check whether the controller is a supported USB mass storage.
697 @param This The USB mass storage driver binding protocol.
698 @param Controller The controller handle to check.
699 @param RemainingDevicePath The remaining device path.
701 @retval EFI_SUCCESS The driver supports this controller.
702 @retval other This device isn't supported.
707 USBMassDriverBindingSupported (
708 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
709 IN EFI_HANDLE Controller
,
710 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
713 EFI_USB_IO_PROTOCOL
*UsbIo
;
714 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
715 USB_MASS_TRANSPORT
*Transport
;
719 Status
= gBS
->OpenProtocol (
721 &gEfiUsbIoProtocolGuid
,
723 This
->DriverBindingHandle
,
725 EFI_OPEN_PROTOCOL_BY_DRIVER
727 if (EFI_ERROR (Status
)) {
732 // Get the interface descriptor to check the USB class and find a transport
735 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
736 if (EFI_ERROR (Status
)) {
740 Status
= EFI_UNSUPPORTED
;
742 if (Interface
.InterfaceClass
!= USB_MASS_STORE_CLASS
) {
747 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
748 // matching transport method.
749 // If not found, return EFI_UNSUPPORTED.
750 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
752 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
753 Transport
= mUsbMassTransport
[Index
];
754 if (Interface
.InterfaceProtocol
== Transport
->Protocol
) {
755 Status
= Transport
->Init (UsbIo
, NULL
);
763 &gEfiUsbIoProtocolGuid
,
764 This
->DriverBindingHandle
,
772 Starts the USB mass storage device with this driver.
774 This function consumes USB I/O Protocol, initializes USB mass storage device,
775 installs Block I/O Protocol, and submits Asynchronous Interrupt
776 Transfer to manage the USB mass storage device.
778 @param This The USB mass storage driver binding protocol.
779 @param Controller The USB mass storage device to start on
780 @param RemainingDevicePath The remaining device path.
782 @retval EFI_SUCCESS This driver supports this device.
783 @retval EFI_UNSUPPORTED This driver does not support this device.
784 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
785 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
786 @retval EFI_ALREADY_STARTED This driver has been started.
791 USBMassDriverBindingStart (
792 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
793 IN EFI_HANDLE Controller
,
794 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
797 USB_MASS_TRANSPORT
*Transport
;
798 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
802 EFI_USB_IO_PROTOCOL
*UsbIo
;
805 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
811 Status
= UsbMassInitTransport (This
, Controller
, &Transport
, &Context
, &MaxLun
);
813 if (EFI_ERROR (Status
)) {
814 DEBUG ((DEBUG_ERROR
, "USBMassDriverBindingStart: UsbMassInitTransport (%r)\n", Status
));
820 // Initialize data for device that does not support multiple LUNSs.
822 Status
= UsbMassInitNonLun (This
, Controller
, Transport
, Context
);
823 if (EFI_ERROR (Status
)) {
824 DEBUG ((DEBUG_ERROR
, "USBMassDriverBindingStart: UsbMassInitNonLun (%r)\n", Status
));
828 // Open device path to prepare for appending Device Logic Unit node.
830 Status
= gBS
->OpenProtocol (
832 &gEfiDevicePathProtocolGuid
,
833 (VOID
**)&DevicePath
,
834 This
->DriverBindingHandle
,
836 EFI_OPEN_PROTOCOL_BY_DRIVER
839 if (EFI_ERROR (Status
)) {
840 DEBUG ((DEBUG_ERROR
, "USBMassDriverBindingStart: OpenDevicePathProtocol By Driver (%r)\n", Status
));
844 Status
= gBS
->OpenProtocol (
846 &gEfiUsbIoProtocolGuid
,
848 This
->DriverBindingHandle
,
850 EFI_OPEN_PROTOCOL_BY_DRIVER
853 if (EFI_ERROR (Status
)) {
854 DEBUG ((DEBUG_ERROR
, "USBMassDriverBindingStart: OpenUsbIoProtocol By Driver (%r)\n", Status
));
857 &gEfiDevicePathProtocolGuid
,
858 This
->DriverBindingHandle
,
865 // Initialize data for device that supports multiple LUNs.
866 // EFI_SUCCESS is returned if at least 1 LUN is initialized successfully.
868 Status
= UsbMassInitMultiLun (This
, Controller
, Transport
, Context
, DevicePath
, MaxLun
);
869 if (EFI_ERROR (Status
)) {
872 &gEfiDevicePathProtocolGuid
,
873 This
->DriverBindingHandle
,
878 &gEfiUsbIoProtocolGuid
,
879 This
->DriverBindingHandle
,
882 DEBUG ((DEBUG_ERROR
, "USBMassDriverBindingStart: UsbMassInitMultiLun (%r) with Maxlun=%d\n", Status
, MaxLun
));
887 gBS
->RestoreTPL (OldTpl
);
892 Stop controlling the device.
894 @param This The USB mass storage driver binding
895 @param Controller The device controller controlled by the driver.
896 @param NumberOfChildren The number of children of this device
897 @param ChildHandleBuffer The buffer of children handle.
899 @retval EFI_SUCCESS The driver stopped from controlling the device.
900 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
901 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.
902 @retval Others Failed to stop the driver
907 USBMassDriverBindingStop (
908 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
909 IN EFI_HANDLE Controller
,
910 IN UINTN NumberOfChildren
,
911 IN EFI_HANDLE
*ChildHandleBuffer
915 USB_MASS_DEVICE
*UsbMass
;
916 EFI_USB_IO_PROTOCOL
*UsbIo
;
917 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
919 BOOLEAN AllChildrenStopped
;
922 // This is a bus driver stop function since multi-lun is supported.
923 // There are three kinds of device handles that might be passed:
924 // 1st is a handle with USB I/O & Block I/O installed (non-multi-lun)
925 // 2nd is a handle with Device Path & USB I/O installed (multi-lun root)
926 // 3rd is a handle with Device Path & USB I/O & Block I/O installed (multi-lun).
928 if (NumberOfChildren
== 0) {
930 // A handle without any children, might be 1st and 2nd type.
932 Status
= gBS
->OpenProtocol (
934 &gEfiBlockIoProtocolGuid
,
936 This
->DriverBindingHandle
,
938 EFI_OPEN_PROTOCOL_GET_PROTOCOL
941 if (EFI_ERROR (Status
)) {
943 // This is a 2nd type handle(multi-lun root), it needs to close devicepath
944 // and usbio protocol.
948 &gEfiDevicePathProtocolGuid
,
949 This
->DriverBindingHandle
,
954 &gEfiUsbIoProtocolGuid
,
955 This
->DriverBindingHandle
,
958 DEBUG ((DEBUG_INFO
, "Success to stop multi-lun root handle\n"));
963 // This is a 1st type handle(non-multi-lun), which only needs to uninstall
964 // Block I/O Protocol, close USB I/O Protocol and free mass device.
966 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
969 // Uninstall Block I/O protocol from the device handle,
970 // then call the transport protocol to stop itself.
972 Status
= gBS
->UninstallMultipleProtocolInterfaces (
974 &gEfiBlockIoProtocolGuid
,
976 &gEfiDiskInfoProtocolGuid
,
980 if (EFI_ERROR (Status
)) {
986 &gEfiUsbIoProtocolGuid
,
987 This
->DriverBindingHandle
,
991 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
994 DEBUG ((DEBUG_INFO
, "Success to stop non-multi-lun root handle\n"));
999 // This is a 3rd type handle(multi-lun), which needs uninstall
1000 // Block I/O Protocol and Device Path Protocol, close USB I/O Protocol and
1001 // free mass device for all children.
1003 AllChildrenStopped
= TRUE
;
1005 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1006 Status
= gBS
->OpenProtocol (
1007 ChildHandleBuffer
[Index
],
1008 &gEfiBlockIoProtocolGuid
,
1010 This
->DriverBindingHandle
,
1012 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1014 if (EFI_ERROR (Status
)) {
1015 AllChildrenStopped
= FALSE
;
1016 DEBUG ((DEBUG_ERROR
, "Fail to stop No.%d multi-lun child handle when opening blockio\n", (UINT32
)Index
));
1020 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
1022 gBS
->CloseProtocol (
1024 &gEfiUsbIoProtocolGuid
,
1025 This
->DriverBindingHandle
,
1026 ChildHandleBuffer
[Index
]
1029 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1030 ChildHandleBuffer
[Index
],
1031 &gEfiDevicePathProtocolGuid
,
1032 UsbMass
->DevicePath
,
1033 &gEfiBlockIoProtocolGuid
,
1035 &gEfiDiskInfoProtocolGuid
,
1040 if (EFI_ERROR (Status
)) {
1042 // Fail to uninstall Block I/O Protocol and Device Path Protocol, so re-open USB I/O Protocol by child.
1044 AllChildrenStopped
= FALSE
;
1045 DEBUG ((DEBUG_ERROR
, "Fail to stop No.%d multi-lun child handle when uninstalling blockio and devicepath\n", (UINT32
)Index
));
1049 &gEfiUsbIoProtocolGuid
,
1051 This
->DriverBindingHandle
,
1052 ChildHandleBuffer
[Index
],
1053 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1057 // Succeed to stop this multi-lun handle, so go on with next child.
1059 if (((Index
+ 1) == NumberOfChildren
) && AllChildrenStopped
) {
1060 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
1067 if (!AllChildrenStopped
) {
1068 return EFI_DEVICE_ERROR
;
1071 DEBUG ((DEBUG_INFO
, "Success to stop all %d multi-lun children handles\n", (UINT32
)NumberOfChildren
));
1076 Entrypoint of USB Mass Storage Driver.
1078 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding
1079 Protocol together with Component Name Protocols.
1081 @param ImageHandle The firmware allocated handle for the EFI image.
1082 @param SystemTable A pointer to the EFI System Table.
1084 @retval EFI_SUCCESS The entry point is executed successfully.
1089 USBMassStorageEntryPoint (
1090 IN EFI_HANDLE ImageHandle
,
1091 IN EFI_SYSTEM_TABLE
*SystemTable
1097 // Install driver binding protocol
1099 Status
= EfiLibInstallDriverBindingComponentName2 (
1102 &gUSBMassDriverBinding
,
1104 &gUsbMassStorageComponentName
,
1105 &gUsbMassStorageComponentName2
1107 ASSERT_EFI_ERROR (Status
);