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 ((EFI_D_ERROR
, "UsbMassReadBlocks: UsbBootReadBlocks (%r) -> Reset\n", Status
));
176 UsbMassReset (This
, TRUE
);
180 gBS
->RestoreTPL (OldTpl
);
186 Writes a specified number of blocks to the device.
188 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
189 It writes a specified number of blocks to the device.
190 All blocks are written, or an error is returned.
192 @param This Indicates a pointer to the calling context.
193 @param MediaId The media ID that the write request is for.
194 @param Lba The starting logical block address to be written.
195 @param BufferSize The size of the Buffer in bytes.
196 This must be a multiple of the intrinsic block size of the device.
197 @param Buffer Pointer to the source buffer for the data.
199 @retval EFI_SUCCESS The data were written correctly to the device.
200 @retval EFI_WRITE_PROTECTED The device cannot be written to.
201 @retval EFI_NO_MEDIA There is no media in the device.
202 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
203 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.
204 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic
205 block size of the device.
206 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
207 or the buffer is not on proper alignment.
213 IN EFI_BLOCK_IO_PROTOCOL
*This
,
220 USB_MASS_DEVICE
*UsbMass
;
221 EFI_BLOCK_IO_MEDIA
*Media
;
227 // Raise TPL to TPL_CALLBACK to serialize all its operations
228 // to protect shared data structures.
230 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
231 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (This
);
232 Media
= &UsbMass
->BlockIoMedia
;
235 // If it is a removable media, such as CD-Rom or Usb-Floppy,
236 // need to detect the media before each read/write. Some of
237 // USB Flash is marked as removable media.
239 if (Media
->RemovableMedia
) {
240 Status
= UsbBootDetectMedia (UsbMass
);
241 if (EFI_ERROR (Status
)) {
246 if (!(Media
->MediaPresent
)) {
247 Status
= EFI_NO_MEDIA
;
251 if (MediaId
!= Media
->MediaId
) {
252 Status
= EFI_MEDIA_CHANGED
;
256 if (BufferSize
== 0) {
257 Status
= EFI_SUCCESS
;
261 if (Buffer
== NULL
) {
262 Status
= EFI_INVALID_PARAMETER
;
267 // BufferSize must be a multiple of the intrinsic block size of the device.
269 if ((BufferSize
% Media
->BlockSize
) != 0) {
270 Status
= EFI_BAD_BUFFER_SIZE
;
274 TotalBlock
= BufferSize
/ Media
->BlockSize
;
277 // Make sure the range to write is valid.
279 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
280 Status
= EFI_INVALID_PARAMETER
;
285 // Try to write the data even the device is marked as ReadOnly,
286 // and clear the status should the write succeed.
288 if (UsbMass
->Cdb16Byte
) {
289 Status
= UsbBootReadWriteBlocks16 (UsbMass
, TRUE
, Lba
, TotalBlock
, Buffer
);
291 Status
= UsbBootReadWriteBlocks (UsbMass
, TRUE
, (UINT32
) Lba
, TotalBlock
, Buffer
);
294 if (EFI_ERROR (Status
)) {
295 DEBUG ((EFI_D_ERROR
, "UsbMassWriteBlocks: UsbBootWriteBlocks (%r) -> Reset\n", Status
));
296 UsbMassReset (This
, TRUE
);
300 gBS
->RestoreTPL (OldTpl
);
305 Flushes all modified data to a physical block device.
307 This function implements EFI_BLOCK_IO_PROTOCOL.FlushBlocks().
308 USB mass storage device doesn't support write cache,
309 so return EFI_SUCCESS directly.
311 @param This Indicates a pointer to the calling context.
313 @retval EFI_SUCCESS All outstanding data were written correctly to the device.
314 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.
315 @retval EFI_NO_MEDIA There is no media in the device.
321 IN EFI_BLOCK_IO_PROTOCOL
*This
328 Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
330 @param UsbMass The USB mass storage device
332 @retval EFI_SUCCESS The media parameters are updated successfully.
333 @retval Others Failed to get the media parameters.
338 IN USB_MASS_DEVICE
*UsbMass
341 EFI_BLOCK_IO_MEDIA
*Media
;
344 Media
= &UsbMass
->BlockIoMedia
;
347 // Fields of EFI_BLOCK_IO_MEDIA are defined in UEFI 2.0 spec,
348 // section for Block I/O Protocol.
350 Media
->MediaPresent
= FALSE
;
351 Media
->LogicalPartition
= FALSE
;
352 Media
->ReadOnly
= FALSE
;
353 Media
->WriteCaching
= FALSE
;
357 Status
= UsbBootGetParams (UsbMass
);
358 DEBUG ((DEBUG_INFO
, "UsbMassInitMedia: UsbBootGetParams (%r)\n", Status
));
359 if (Status
== EFI_MEDIA_CHANGED
) {
361 // Some USB storage devices may report MEDIA_CHANGED sense key when hot-plugged.
362 // Treat it as SUCCESS
364 Status
= EFI_SUCCESS
;
370 Initilize 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
++) {
494 DEBUG ((EFI_D_INFO
, "UsbMassInitMultiLun: Start to initialize No.%d logic unit\n", Index
));
497 UsbMass
= AllocateZeroPool (sizeof (USB_MASS_DEVICE
));
498 ASSERT (UsbMass
!= NULL
);
500 UsbMass
->Signature
= USB_MASS_SIGNATURE
;
501 UsbMass
->UsbIo
= UsbIo
;
502 UsbMass
->BlockIo
.Media
= &UsbMass
->BlockIoMedia
;
503 UsbMass
->BlockIo
.Reset
= UsbMassReset
;
504 UsbMass
->BlockIo
.ReadBlocks
= UsbMassReadBlocks
;
505 UsbMass
->BlockIo
.WriteBlocks
= UsbMassWriteBlocks
;
506 UsbMass
->BlockIo
.FlushBlocks
= UsbMassFlushBlocks
;
507 UsbMass
->OpticalStorage
= FALSE
;
508 UsbMass
->Transport
= Transport
;
509 UsbMass
->Context
= Context
;
510 UsbMass
->Lun
= Index
;
513 // Initialize the media parameter data for EFI_BLOCK_IO_MEDIA of Block I/O Protocol.
515 Status
= UsbMassInitMedia (UsbMass
);
516 if ((EFI_ERROR (Status
)) && (Status
!= EFI_NO_MEDIA
)) {
517 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: UsbMassInitMedia (%r)\n", Status
));
523 // Create a device path node for device logic unit, and append it.
525 LunNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
526 LunNode
.Header
.SubType
= MSG_DEVICE_LOGICAL_UNIT_DP
;
527 LunNode
.Lun
= UsbMass
->Lun
;
529 SetDevicePathNodeLength (&LunNode
.Header
, sizeof (LunNode
));
531 UsbMass
->DevicePath
= AppendDevicePathNode (DevicePath
, &LunNode
.Header
);
533 if (UsbMass
->DevicePath
== NULL
) {
534 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: failed to create device logic unit device path\n"));
535 Status
= EFI_OUT_OF_RESOURCES
;
540 InitializeDiskInfo (UsbMass
);
543 // Create a new handle for each LUN, and install Block I/O Protocol and Device Path Protocol.
545 Status
= gBS
->InstallMultipleProtocolInterfaces (
546 &UsbMass
->Controller
,
547 &gEfiDevicePathProtocolGuid
,
549 &gEfiBlockIoProtocolGuid
,
551 &gEfiDiskInfoProtocolGuid
,
556 if (EFI_ERROR (Status
)) {
557 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: InstallMultipleProtocolInterfaces (%r)\n", Status
));
558 FreePool (UsbMass
->DevicePath
);
564 // Open USB I/O Protocol by child to setup a parent-child relationship.
566 Status
= gBS
->OpenProtocol (
568 &gEfiUsbIoProtocolGuid
,
570 This
->DriverBindingHandle
,
572 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
575 if (EFI_ERROR (Status
)) {
576 DEBUG ((EFI_D_ERROR
, "UsbMassInitMultiLun: OpenUsbIoProtocol By Child (%r)\n", Status
));
577 gBS
->UninstallMultipleProtocolInterfaces (
578 &UsbMass
->Controller
,
579 &gEfiDevicePathProtocolGuid
,
581 &gEfiBlockIoProtocolGuid
,
583 &gEfiDiskInfoProtocolGuid
,
587 FreePool (UsbMass
->DevicePath
);
591 ReturnStatus
= EFI_SUCCESS
;
592 DEBUG ((EFI_D_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 ((EFI_D_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 ((EFI_D_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
) {
684 &gEfiUsbIoProtocolGuid
,
685 This
->DriverBindingHandle
,
694 Check whether the controller is a supported USB mass storage.
696 @param This The USB mass storage driver binding protocol.
697 @param Controller The controller handle to check.
698 @param RemainingDevicePath The remaining device path.
700 @retval EFI_SUCCESS The driver supports this controller.
701 @retval other This device isn't supported.
706 USBMassDriverBindingSupported (
707 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
708 IN EFI_HANDLE Controller
,
709 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
712 EFI_USB_IO_PROTOCOL
*UsbIo
;
713 EFI_USB_INTERFACE_DESCRIPTOR Interface
;
714 USB_MASS_TRANSPORT
*Transport
;
718 Status
= gBS
->OpenProtocol (
720 &gEfiUsbIoProtocolGuid
,
722 This
->DriverBindingHandle
,
724 EFI_OPEN_PROTOCOL_BY_DRIVER
726 if (EFI_ERROR (Status
)) {
731 // Get the interface descriptor to check the USB class and find a transport
734 Status
= UsbIo
->UsbGetInterfaceDescriptor (UsbIo
, &Interface
);
735 if (EFI_ERROR (Status
)) {
739 Status
= EFI_UNSUPPORTED
;
741 if (Interface
.InterfaceClass
!= USB_MASS_STORE_CLASS
) {
746 // Traverse the USB_MASS_TRANSPORT arrary and try to find the
747 // matching transport method.
748 // If not found, return EFI_UNSUPPORTED.
749 // If found, execute USB_MASS_TRANSPORT.Init() to initialize the transport context.
751 for (Index
= 0; Index
< USB_MASS_TRANSPORT_COUNT
; Index
++) {
752 Transport
= mUsbMassTransport
[Index
];
753 if (Interface
.InterfaceProtocol
== Transport
->Protocol
) {
754 Status
= Transport
->Init (UsbIo
, NULL
);
762 &gEfiUsbIoProtocolGuid
,
763 This
->DriverBindingHandle
,
771 Starts the USB mass storage device with this driver.
773 This function consumes USB I/O Portocol, intializes USB mass storage device,
774 installs Block I/O Protocol, and submits Asynchronous Interrupt
775 Transfer to manage the USB mass storage device.
777 @param This The USB mass storage driver binding protocol.
778 @param Controller The USB mass storage device to start on
779 @param RemainingDevicePath The remaining device path.
781 @retval EFI_SUCCESS This driver supports this device.
782 @retval EFI_UNSUPPORTED This driver does not support this device.
783 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
784 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
785 @retval EFI_ALREADY_STARTED This driver has been started.
790 USBMassDriverBindingStart (
791 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
792 IN EFI_HANDLE Controller
,
793 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
796 USB_MASS_TRANSPORT
*Transport
;
797 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
801 EFI_USB_IO_PROTOCOL
*UsbIo
;
804 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
810 Status
= UsbMassInitTransport (This
, Controller
, &Transport
, &Context
, &MaxLun
);
812 if (EFI_ERROR (Status
)) {
813 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitTransport (%r)\n", Status
));
818 // Initialize data for device that does not support multiple LUNSs.
820 Status
= UsbMassInitNonLun (This
, Controller
, Transport
, Context
);
821 if (EFI_ERROR (Status
)) {
822 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitNonLun (%r)\n", Status
));
826 // Open device path to prepare for appending Device Logic Unit node.
828 Status
= gBS
->OpenProtocol (
830 &gEfiDevicePathProtocolGuid
,
831 (VOID
**) &DevicePath
,
832 This
->DriverBindingHandle
,
834 EFI_OPEN_PROTOCOL_BY_DRIVER
837 if (EFI_ERROR (Status
)) {
838 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenDevicePathProtocol By Driver (%r)\n", Status
));
842 Status
= gBS
->OpenProtocol (
844 &gEfiUsbIoProtocolGuid
,
846 This
->DriverBindingHandle
,
848 EFI_OPEN_PROTOCOL_BY_DRIVER
851 if (EFI_ERROR (Status
)) {
852 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: OpenUsbIoProtocol By Driver (%r)\n", Status
));
855 &gEfiDevicePathProtocolGuid
,
856 This
->DriverBindingHandle
,
863 // Initialize data for device that supports multiple LUNs.
864 // EFI_SUCCESS is returned if at least 1 LUN is initialized successfully.
866 Status
= UsbMassInitMultiLun (This
, Controller
, Transport
, Context
, DevicePath
, MaxLun
);
867 if (EFI_ERROR (Status
)) {
870 &gEfiDevicePathProtocolGuid
,
871 This
->DriverBindingHandle
,
876 &gEfiUsbIoProtocolGuid
,
877 This
->DriverBindingHandle
,
880 DEBUG ((EFI_D_ERROR
, "USBMassDriverBindingStart: UsbMassInitMultiLun (%r) with Maxlun=%d\n", Status
, MaxLun
));
884 gBS
->RestoreTPL (OldTpl
);
890 Stop controlling the device.
892 @param This The USB mass storage driver binding
893 @param Controller The device controller controlled by the driver.
894 @param NumberOfChildren The number of children of this device
895 @param ChildHandleBuffer The buffer of children handle.
897 @retval EFI_SUCCESS The driver stopped from controlling the device.
898 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
899 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.
900 @retval Others Failed to stop the driver
905 USBMassDriverBindingStop (
906 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
907 IN EFI_HANDLE Controller
,
908 IN UINTN NumberOfChildren
,
909 IN EFI_HANDLE
*ChildHandleBuffer
913 USB_MASS_DEVICE
*UsbMass
;
914 EFI_USB_IO_PROTOCOL
*UsbIo
;
915 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
917 BOOLEAN AllChildrenStopped
;
920 // This is a bus driver stop function since multi-lun is supported.
921 // There are three kinds of device handles that might be passed:
922 // 1st is a handle with USB I/O & Block I/O installed (non-multi-lun)
923 // 2nd is a handle with Device Path & USB I/O installed (multi-lun root)
924 // 3rd is a handle with Device Path & USB I/O & Block I/O installed (multi-lun).
926 if (NumberOfChildren
== 0) {
928 // A handle without any children, might be 1st and 2nd type.
930 Status
= gBS
->OpenProtocol (
932 &gEfiBlockIoProtocolGuid
,
934 This
->DriverBindingHandle
,
936 EFI_OPEN_PROTOCOL_GET_PROTOCOL
939 if (EFI_ERROR(Status
)) {
941 // This is a 2nd type handle(multi-lun root), it needs to close devicepath
942 // and usbio protocol.
946 &gEfiDevicePathProtocolGuid
,
947 This
->DriverBindingHandle
,
952 &gEfiUsbIoProtocolGuid
,
953 This
->DriverBindingHandle
,
956 DEBUG ((EFI_D_INFO
, "Success to stop multi-lun root handle\n"));
961 // This is a 1st type handle(non-multi-lun), which only needs to uninstall
962 // Block I/O Protocol, close USB I/O Protocol and free mass device.
964 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
967 // Uninstall Block I/O protocol from the device handle,
968 // then call the transport protocol to stop itself.
970 Status
= gBS
->UninstallMultipleProtocolInterfaces (
972 &gEfiBlockIoProtocolGuid
,
974 &gEfiDiskInfoProtocolGuid
,
978 if (EFI_ERROR (Status
)) {
984 &gEfiUsbIoProtocolGuid
,
985 This
->DriverBindingHandle
,
989 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
992 DEBUG ((EFI_D_INFO
, "Success to stop non-multi-lun root handle\n"));
997 // This is a 3rd type handle(multi-lun), which needs uninstall
998 // Block I/O Protocol and Device Path Protocol, close USB I/O Protocol and
999 // free mass device for all children.
1001 AllChildrenStopped
= TRUE
;
1003 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1005 Status
= gBS
->OpenProtocol (
1006 ChildHandleBuffer
[Index
],
1007 &gEfiBlockIoProtocolGuid
,
1009 This
->DriverBindingHandle
,
1011 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1013 if (EFI_ERROR (Status
)) {
1014 AllChildrenStopped
= FALSE
;
1015 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when opening blockio\n", (UINT32
)Index
));
1019 UsbMass
= USB_MASS_DEVICE_FROM_BLOCK_IO (BlockIo
);
1021 gBS
->CloseProtocol (
1023 &gEfiUsbIoProtocolGuid
,
1024 This
->DriverBindingHandle
,
1025 ChildHandleBuffer
[Index
]
1028 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1029 ChildHandleBuffer
[Index
],
1030 &gEfiDevicePathProtocolGuid
,
1031 UsbMass
->DevicePath
,
1032 &gEfiBlockIoProtocolGuid
,
1034 &gEfiDiskInfoProtocolGuid
,
1039 if (EFI_ERROR (Status
)) {
1041 // Fail to uninstall Block I/O Protocol and Device Path Protocol, so re-open USB I/O Protocol by child.
1043 AllChildrenStopped
= FALSE
;
1044 DEBUG ((EFI_D_ERROR
, "Fail to stop No.%d multi-lun child handle when uninstalling blockio and devicepath\n", (UINT32
)Index
));
1048 &gEfiUsbIoProtocolGuid
,
1050 This
->DriverBindingHandle
,
1051 ChildHandleBuffer
[Index
],
1052 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1056 // Succeed to stop this multi-lun handle, so go on with next child.
1058 if (((Index
+ 1) == NumberOfChildren
) && AllChildrenStopped
) {
1059 UsbMass
->Transport
->CleanUp (UsbMass
->Context
);
1065 if (!AllChildrenStopped
) {
1066 return EFI_DEVICE_ERROR
;
1069 DEBUG ((EFI_D_INFO
, "Success to stop all %d multi-lun children handles\n", (UINT32
) NumberOfChildren
));
1074 Entrypoint of USB Mass Storage Driver.
1076 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding
1077 Protocol together with Component Name Protocols.
1079 @param ImageHandle The firmware allocated handle for the EFI image.
1080 @param SystemTable A pointer to the EFI System Table.
1082 @retval EFI_SUCCESS The entry point is executed successfully.
1087 USBMassStorageEntryPoint (
1088 IN EFI_HANDLE ImageHandle
,
1089 IN EFI_SYSTEM_TABLE
*SystemTable
1095 // Install driver binding protocol
1097 Status
= EfiLibInstallDriverBindingComponentName2 (
1100 &gUSBMassDriverBinding
,
1102 &gUsbMassStorageComponentName
,
1103 &gUsbMassStorageComponentName2
1105 ASSERT_EFI_ERROR (Status
);