2 Implementation of the command set of USB Mass Storage Specification
3 for Bootability, Revision 1.0.
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Execute REQUEST SENSE Command to retrieve sense data from device.
15 @param UsbMass The device whose sense data is requested.
17 @retval EFI_SUCCESS The command is executed successfully.
18 @retval EFI_DEVICE_ERROR Failed to request sense.
19 @retval EFI_NO_RESPONSE The device media doesn't response this request.
20 @retval EFI_INVALID_PARAMETER The command has some invalid parameters.
21 @retval EFI_WRITE_PROTECTED The device is write protected.
22 @retval EFI_MEDIA_CHANGED The device media has been changed.
27 IN USB_MASS_DEVICE
*UsbMass
30 USB_BOOT_REQUEST_SENSE_CMD SenseCmd
;
31 USB_BOOT_REQUEST_SENSE_DATA SenseData
;
32 EFI_BLOCK_IO_MEDIA
*Media
;
33 USB_MASS_TRANSPORT
*Transport
;
37 Transport
= UsbMass
->Transport
;
40 // Request the sense data from the device
42 ZeroMem (&SenseCmd
, sizeof (USB_BOOT_REQUEST_SENSE_CMD
));
43 ZeroMem (&SenseData
, sizeof (USB_BOOT_REQUEST_SENSE_DATA
));
45 SenseCmd
.OpCode
= USB_BOOT_REQUEST_SENSE_OPCODE
;
46 SenseCmd
.Lun
= (UINT8
)(USB_BOOT_LUN (UsbMass
->Lun
));
47 SenseCmd
.AllocLen
= (UINT8
)sizeof (USB_BOOT_REQUEST_SENSE_DATA
);
49 Status
= Transport
->ExecCommand (
52 sizeof (USB_BOOT_REQUEST_SENSE_CMD
),
55 sizeof (USB_BOOT_REQUEST_SENSE_DATA
),
57 USB_BOOT_GENERAL_CMD_TIMEOUT
,
60 if (EFI_ERROR (Status
) || (CmdResult
!= USB_MASS_CMD_SUCCESS
)) {
61 DEBUG ((DEBUG_ERROR
, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status
, CmdResult
));
62 if (!EFI_ERROR (Status
)) {
63 Status
= EFI_DEVICE_ERROR
;
70 // If sense data is retrieved successfully, interpret the sense data
71 // and update the media status if necessary.
73 Media
= &UsbMass
->BlockIoMedia
;
75 switch (USB_BOOT_SENSE_KEY (SenseData
.SenseKey
)) {
76 case USB_BOOT_SENSE_NO_SENSE
:
77 if (SenseData
.Asc
== USB_BOOT_ASC_NO_ADDITIONAL_SENSE_INFORMATION
) {
79 // It is not an error if a device does not have additional sense information
83 Status
= EFI_NO_RESPONSE
;
88 case USB_BOOT_SENSE_RECOVERED
:
90 // Suppose hardware can handle this case, and recover later by itself
92 Status
= EFI_NOT_READY
;
95 case USB_BOOT_SENSE_NOT_READY
:
96 Status
= EFI_DEVICE_ERROR
;
97 if (SenseData
.Asc
== USB_BOOT_ASC_NO_MEDIA
) {
98 Media
->MediaPresent
= FALSE
;
99 Status
= EFI_NO_MEDIA
;
100 } else if (SenseData
.Asc
== USB_BOOT_ASC_NOT_READY
) {
101 Status
= EFI_NOT_READY
;
106 case USB_BOOT_SENSE_ILLEGAL_REQUEST
:
107 Status
= EFI_INVALID_PARAMETER
;
110 case USB_BOOT_SENSE_UNIT_ATTENTION
:
111 Status
= EFI_DEVICE_ERROR
;
112 if (SenseData
.Asc
== USB_BOOT_ASC_MEDIA_CHANGE
) {
114 // If MediaChange, reset ReadOnly and new MediaId
116 Status
= EFI_MEDIA_CHANGED
;
117 Media
->ReadOnly
= FALSE
;
119 } else if (SenseData
.Asc
== USB_BOOT_ASC_NOT_READY
) {
120 Status
= EFI_NOT_READY
;
121 } else if (SenseData
.Asc
== USB_BOOT_ASC_NO_MEDIA
) {
122 Status
= EFI_NOT_READY
;
127 case USB_BOOT_SENSE_DATA_PROTECT
:
128 Status
= EFI_WRITE_PROTECTED
;
129 Media
->ReadOnly
= TRUE
;
133 Status
= EFI_DEVICE_ERROR
;
139 "UsbBootRequestSense: (%r) with error code (%x) sense key %x/%x/%x\n",
142 USB_BOOT_SENSE_KEY (SenseData
.SenseKey
),
151 Execute the USB mass storage bootability commands.
153 This function executes the USB mass storage bootability commands.
154 If execution failed, retrieve the error by REQUEST_SENSE, then
155 update the device's status, such as ReadyOnly.
157 @param UsbMass The device to issue commands to
158 @param Cmd The command to execute
159 @param CmdLen The length of the command
160 @param DataDir The direction of data transfer
161 @param Data The buffer to hold the data
162 @param DataLen The length of expected data
163 @param Timeout The timeout used to transfer
165 @retval EFI_SUCCESS Command is executed successfully
166 @retval Others Command execution failed.
171 IN USB_MASS_DEVICE
*UsbMass
,
174 IN EFI_USB_DATA_DIRECTION DataDir
,
180 USB_MASS_TRANSPORT
*Transport
;
184 Transport
= UsbMass
->Transport
;
185 Status
= Transport
->ExecCommand (
197 if (Status
== EFI_TIMEOUT
) {
198 DEBUG ((DEBUG_ERROR
, "UsbBootExecCmd: %r to Exec 0x%x Cmd\n", Status
, *(UINT8
*)Cmd
));
203 // If ExecCommand() returns no error and CmdResult is success,
204 // then the command transfer is successful.
206 if ((CmdResult
== USB_MASS_CMD_SUCCESS
) && !EFI_ERROR (Status
)) {
211 // If command execution failed, then retrieve error info via sense request.
213 DEBUG ((DEBUG_ERROR
, "UsbBootExecCmd: %r to Exec 0x%x Cmd (Result = %x)\n", Status
, *(UINT8
*)Cmd
, CmdResult
));
214 return UsbBootRequestSense (UsbMass
);
218 Execute the USB mass storage bootability commands with retrial.
220 This function executes USB mass storage bootability commands.
221 If the device isn't ready, wait for it. If the device is ready
222 and error occurs, retry the command again until it exceeds the
223 limit of retrial times.
225 @param UsbMass The device to issue commands to
226 @param Cmd The command to execute
227 @param CmdLen The length of the command
228 @param DataDir The direction of data transfer
229 @param Data The buffer to hold the data
230 @param DataLen The length of expected data
231 @param Timeout The timeout used to transfer
233 @retval EFI_SUCCESS The command is executed successfully.
234 @retval EFI_NO_MEDIA The device media is removed.
235 @retval Others Command execution failed after retrial.
239 UsbBootExecCmdWithRetry (
240 IN USB_MASS_DEVICE
*UsbMass
,
243 IN EFI_USB_DATA_DIRECTION DataDir
,
251 EFI_EVENT TimeoutEvt
;
254 Status
= EFI_SUCCESS
;
255 Status
= gBS
->CreateEvent (
262 if (EFI_ERROR (Status
)) {
266 Status
= gBS
->SetTimer (TimeoutEvt
, TimerRelative
, EFI_TIMER_PERIOD_SECONDS (60));
267 if (EFI_ERROR (Status
)) {
272 // Execute the cmd and retry if it fails.
274 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvt
))) {
275 Status
= UsbBootExecCmd (
284 if ((Status
== EFI_SUCCESS
) || (Status
== EFI_NO_MEDIA
)) {
289 // If the sense data shows the drive is not ready, we need execute the cmd again.
290 // We limit the upper boundary to 60 seconds.
292 if (Status
== EFI_NOT_READY
) {
297 // If the status is other error, then just retry 5 times.
299 if (Retry
++ >= USB_BOOT_COMMAND_RETRY
) {
305 if (TimeoutEvt
!= NULL
) {
306 gBS
->CloseEvent (TimeoutEvt
);
313 Execute TEST UNIT READY command to check if the device is ready.
315 @param UsbMass The device to test
317 @retval EFI_SUCCESS The device is ready.
318 @retval Others Device not ready.
323 IN USB_MASS_DEVICE
*UsbMass
326 USB_BOOT_TEST_UNIT_READY_CMD TestCmd
;
328 ZeroMem (&TestCmd
, sizeof (USB_BOOT_TEST_UNIT_READY_CMD
));
330 TestCmd
.OpCode
= USB_BOOT_TEST_UNIT_READY_OPCODE
;
331 TestCmd
.Lun
= (UINT8
)(USB_BOOT_LUN (UsbMass
->Lun
));
333 return UsbBootExecCmdWithRetry (
336 (UINT8
)sizeof (USB_BOOT_TEST_UNIT_READY_CMD
),
340 USB_BOOT_GENERAL_CMD_TIMEOUT
345 Execute INQUIRY Command to request information regarding parameters of
346 the device be sent to the host computer.
348 @param UsbMass The device to inquire.
350 @retval EFI_SUCCESS INQUIRY Command is executed successfully.
351 @retval Others INQUIRY Command is not executed successfully.
356 IN USB_MASS_DEVICE
*UsbMass
359 USB_BOOT_INQUIRY_CMD InquiryCmd
;
360 EFI_BLOCK_IO_MEDIA
*Media
;
363 Media
= &(UsbMass
->BlockIoMedia
);
365 ZeroMem (&InquiryCmd
, sizeof (USB_BOOT_INQUIRY_CMD
));
366 ZeroMem (&UsbMass
->InquiryData
, sizeof (USB_BOOT_INQUIRY_DATA
));
368 InquiryCmd
.OpCode
= USB_BOOT_INQUIRY_OPCODE
;
369 InquiryCmd
.Lun
= (UINT8
)(USB_BOOT_LUN (UsbMass
->Lun
));
370 InquiryCmd
.AllocLen
= (UINT8
)sizeof (USB_BOOT_INQUIRY_DATA
);
372 Status
= UsbBootExecCmdWithRetry (
375 (UINT8
)sizeof (USB_BOOT_INQUIRY_CMD
),
377 &UsbMass
->InquiryData
,
378 sizeof (USB_BOOT_INQUIRY_DATA
),
379 USB_BOOT_GENERAL_CMD_TIMEOUT
381 if (EFI_ERROR (Status
)) {
386 // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit
387 // from the inquiry data.
389 UsbMass
->Pdt
= (UINT8
)(USB_BOOT_PDT (UsbMass
->InquiryData
.Pdt
));
390 Media
->RemovableMedia
= (BOOLEAN
)(USB_BOOT_REMOVABLE (UsbMass
->InquiryData
.Removable
));
392 // Set block size to the default value of 512 Bytes, in case no media is present at first time.
394 Media
->BlockSize
= 0x0200;
400 Execute READ CAPACITY 16 bytes command to request information regarding
401 the capacity of the installed medium of the device.
403 This function executes READ CAPACITY 16 bytes command to get the capacity
404 of the USB mass storage media, including the presence, block size,
405 and last block number.
407 @param UsbMass The device to retireve disk gemotric.
409 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
410 @retval EFI_NOT_READY The returned block size is zero.
411 @retval Other READ CAPACITY 16 bytes command execution failed.
415 UsbBootReadCapacity16 (
416 IN USB_MASS_DEVICE
*UsbMass
419 UINT8 CapacityCmd
[16];
420 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData
;
421 EFI_BLOCK_IO_MEDIA
*Media
;
425 Media
= &UsbMass
->BlockIoMedia
;
427 Media
->MediaPresent
= FALSE
;
428 Media
->LastBlock
= 0;
429 Media
->BlockSize
= 0;
431 ZeroMem (CapacityCmd
, sizeof (CapacityCmd
));
432 ZeroMem (&CapacityData
, sizeof (CapacityData
));
434 CapacityCmd
[0] = EFI_SCSI_OP_READ_CAPACITY16
;
435 CapacityCmd
[1] = 0x10;
437 // Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO.
439 ZeroMem ((CapacityCmd
+ 2), 8);
441 CapacityCmd
[13] = sizeof (CapacityData
);
443 Status
= UsbBootExecCmdWithRetry (
446 (UINT8
)sizeof (CapacityCmd
),
449 sizeof (CapacityData
),
450 USB_BOOT_GENERAL_CMD_TIMEOUT
452 if (EFI_ERROR (Status
)) {
457 // Get the information on media presence, block size, and last block number
458 // from READ CAPACITY data.
460 Media
->MediaPresent
= TRUE
;
461 Media
->LastBlock
= SwapBytes64 (ReadUnaligned64 ((CONST UINT64
*)&(CapacityData
.LastLba7
)));
463 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*)&(CapacityData
.BlockSize3
)));
465 Media
->LowestAlignedLba
= (CapacityData
.LowestAlignLogic2
<< 8) |
466 CapacityData
.LowestAlignLogic1
;
467 Media
->LogicalBlocksPerPhysicalBlock
= (1 << CapacityData
.LogicPerPhysical
);
468 if (BlockSize
== 0) {
472 return UsbBootRequestSense (UsbMass
);
474 Media
->BlockSize
= BlockSize
;
481 Execute READ CAPACITY command to request information regarding
482 the capacity of the installed medium of the device.
484 This function executes READ CAPACITY command to get the capacity
485 of the USB mass storage media, including the presence, block size,
486 and last block number.
488 @param UsbMass The device to retireve disk gemotric.
490 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
491 @retval EFI_NOT_READY The returned block size is zero.
492 @retval Other READ CAPACITY command execution failed.
496 UsbBootReadCapacity (
497 IN USB_MASS_DEVICE
*UsbMass
500 USB_BOOT_READ_CAPACITY_CMD CapacityCmd
;
501 USB_BOOT_READ_CAPACITY_DATA CapacityData
;
502 EFI_BLOCK_IO_MEDIA
*Media
;
506 Media
= &UsbMass
->BlockIoMedia
;
508 ZeroMem (&CapacityCmd
, sizeof (USB_BOOT_READ_CAPACITY_CMD
));
509 ZeroMem (&CapacityData
, sizeof (USB_BOOT_READ_CAPACITY_DATA
));
511 CapacityCmd
.OpCode
= USB_BOOT_READ_CAPACITY_OPCODE
;
512 CapacityCmd
.Lun
= (UINT8
)(USB_BOOT_LUN (UsbMass
->Lun
));
514 Status
= UsbBootExecCmdWithRetry (
517 (UINT8
)sizeof (USB_BOOT_READ_CAPACITY_CMD
),
520 sizeof (USB_BOOT_READ_CAPACITY_DATA
),
521 USB_BOOT_GENERAL_CMD_TIMEOUT
523 if (EFI_ERROR (Status
)) {
528 // Get the information on media presence, block size, and last block number
529 // from READ CAPACITY data.
531 Media
->MediaPresent
= TRUE
;
532 Media
->LastBlock
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*)CapacityData
.LastLba
));
534 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*)CapacityData
.BlockLen
));
535 if (BlockSize
== 0) {
539 return UsbBootRequestSense (UsbMass
);
541 Media
->BlockSize
= BlockSize
;
544 if (Media
->LastBlock
== 0xFFFFFFFF) {
545 Status
= UsbBootReadCapacity16 (UsbMass
);
546 if (!EFI_ERROR (Status
)) {
547 UsbMass
->Cdb16Byte
= TRUE
;
555 Retrieves SCSI mode sense information via MODE SENSE(6) command.
557 @param UsbMass The device whose sense data is requested.
559 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.
560 @retval Other Command execution failed.
565 IN USB_MASS_DEVICE
*UsbMass
569 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd
;
570 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader
;
571 EFI_BLOCK_IO_MEDIA
*Media
;
573 Media
= &UsbMass
->BlockIoMedia
;
575 ZeroMem (&ModeSenseCmd
, sizeof (USB_SCSI_MODE_SENSE6_CMD
));
576 ZeroMem (&ModeParaHeader
, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
));
579 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec
581 ModeSenseCmd
.OpCode
= USB_SCSI_MODE_SENSE6_OPCODE
;
582 ModeSenseCmd
.Lun
= (UINT8
)USB_BOOT_LUN (UsbMass
->Lun
);
583 ModeSenseCmd
.PageCode
= 0x3F;
584 ModeSenseCmd
.AllocateLen
= (UINT8
)sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
);
586 Status
= UsbBootExecCmdWithRetry (
589 (UINT8
)sizeof (USB_SCSI_MODE_SENSE6_CMD
),
592 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
),
593 USB_BOOT_GENERAL_CMD_TIMEOUT
597 // Format of device-specific parameter byte of the mode parameter header is defined in
598 // Section 8.2.10 of SCSI-2 Spec.
599 // BIT7 of this byte is indicates whether the medium is write protected.
601 if (!EFI_ERROR (Status
)) {
602 Media
->ReadOnly
= (BOOLEAN
)((ModeParaHeader
.DevicePara
& BIT7
) != 0);
609 Get the parameters for the USB mass storage media.
611 This function get the parameters for the USB mass storage media,
612 It is used both to initialize the media during the Start() phase
613 of Driver Binding Protocol and to re-initialize it when the media is
614 changed. Although the RemoveableMedia is unlikely to change,
615 it is also included here.
617 @param UsbMass The device to retrieve disk gemotric.
619 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.
620 @retval Other Failed to get the parameters.
625 IN USB_MASS_DEVICE
*UsbMass
628 EFI_BLOCK_IO_MEDIA
*Media
;
631 Media
= &(UsbMass
->BlockIoMedia
);
633 Status
= UsbBootInquiry (UsbMass
);
634 if (EFI_ERROR (Status
)) {
635 DEBUG ((DEBUG_ERROR
, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status
));
640 // According to USB Mass Storage Specification for Bootability, only following
641 // 4 Peripheral Device Types are in spec.
643 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
644 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
645 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
646 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
))
648 DEBUG ((DEBUG_ERROR
, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
649 return EFI_UNSUPPORTED
;
653 // Don't use the Removable bit in inquiry data to test whether the media
654 // is removable because many flash disks wrongly set this bit.
656 if ((UsbMass
->Pdt
== USB_PDT_CDROM
) || (UsbMass
->Pdt
== USB_PDT_OPTICAL
)) {
658 // CD-Rom device and Non-CD optical device
660 UsbMass
->OpticalStorage
= TRUE
;
662 // Default value 2048 Bytes, in case no media present at first time
664 Media
->BlockSize
= 0x0800;
667 Status
= UsbBootDetectMedia (UsbMass
);
673 Detect whether the removable media is present and whether it has changed.
675 @param UsbMass The device to check.
677 @retval EFI_SUCCESS The media status is successfully checked.
678 @retval Other Failed to detect media.
683 IN USB_MASS_DEVICE
*UsbMass
686 EFI_BLOCK_IO_MEDIA OldMedia
;
687 EFI_BLOCK_IO_MEDIA
*Media
;
691 Media
= &UsbMass
->BlockIoMedia
;
693 CopyMem (&OldMedia
, &(UsbMass
->BlockIoMedia
), sizeof (EFI_BLOCK_IO_MEDIA
));
695 CmdSet
= ((EFI_USB_INTERFACE_DESCRIPTOR
*)(UsbMass
->Context
))->InterfaceSubClass
;
697 Status
= UsbBootIsUnitReady (UsbMass
);
698 if (EFI_ERROR (Status
)) {
699 DEBUG ((DEBUG_ERROR
, "UsbBootDetectMedia: UsbBootIsUnitReady (%r)\n", Status
));
704 // EFI_SUCCESS: all good.
705 // EFI_NO_MEDIA: media is not present.
707 // For either EFI_NO_MEDIA, or HW error, skip to get WriteProtected and capacity information.
709 if (!EFI_ERROR (Status
)) {
710 if ((UsbMass
->Pdt
!= USB_PDT_CDROM
) && (CmdSet
== USB_MASS_STORE_SCSI
)) {
712 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
713 // according to Section 4 of USB Mass Storage Specification for Bootability.
714 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
715 // could get the information of Write Protected.
716 // Since not all device support this command, skip if fail.
718 UsbScsiModeSense (UsbMass
);
721 Status
= UsbBootReadCapacity (UsbMass
);
722 if (EFI_ERROR (Status
)) {
723 DEBUG ((DEBUG_ERROR
, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status
));
727 if (EFI_ERROR (Status
) && (Status
!= EFI_NO_MEDIA
)) {
729 // For NoMedia, BlockIo is still needed.
735 // Simply reject device whose block size is unacceptable small (==0) or large (>64K).
737 if ((Media
->BlockSize
== 0) || (Media
->BlockSize
> USB_BOOT_MAX_CARRY_SIZE
)) {
738 return EFI_DEVICE_ERROR
;
742 // Detect whether it is necessary to reinstall the Block I/O Protocol.
744 // MediaId may change in RequestSense for MediaChanged
745 // MediaPresent may change in RequestSense for NoMedia
746 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
747 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
749 if ((Media
->MediaId
!= OldMedia
.MediaId
) ||
750 (Media
->MediaPresent
!= OldMedia
.MediaPresent
) ||
751 (Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
752 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
753 (Media
->LastBlock
!= OldMedia
.LastBlock
))
756 // This function is called from:
757 // Block I/O Protocol APIs, which run at TPL_CALLBACK.
758 // DriverBindingStart(), which raises to TPL_CALLBACK.
759 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK
);
762 // When it is called from DriverBindingStart(), below reinstall fails.
763 // So ignore the return status check.
765 gBS
->ReinstallProtocolInterface (
767 &gEfiBlockIoProtocolGuid
,
773 // Reset MediaId after reinstalling Block I/O Protocol.
775 if (Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
776 if (Media
->MediaPresent
) {
783 if ((Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
784 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
785 (Media
->LastBlock
!= OldMedia
.LastBlock
))
790 Status
= Media
->MediaPresent
? EFI_MEDIA_CHANGED
: EFI_NO_MEDIA
;
797 Read or write some blocks from the device.
799 @param UsbMass The USB mass storage device to access
800 @param Write TRUE for write operation.
801 @param Lba The start block number
802 @param TotalBlock Total block number to read or write
803 @param Buffer The buffer to read to or write from
805 @retval EFI_SUCCESS Data are read into the buffer or writen into the device.
806 @retval Others Failed to read or write all the data
810 UsbBootReadWriteBlocks (
811 IN USB_MASS_DEVICE
*UsbMass
,
818 USB_BOOT_READ_WRITE_10_CMD Cmd
;
826 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
827 CountMax
= USB_BOOT_MAX_CARRY_SIZE
/ BlockSize
;
828 Status
= EFI_SUCCESS
;
830 while (TotalBlock
> 0) {
832 // Split the total blocks into smaller pieces to ease the pressure
833 // on the device. We must split the total block because the READ10
834 // command only has 16 bit transfer length (in the unit of block).
836 Count
= (UINT32
)MIN (TotalBlock
, CountMax
);
837 Count
= MIN (MAX_UINT16
, Count
);
838 ByteSize
= Count
* BlockSize
;
841 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
843 Timeout
= (UINT32
)USB_BOOT_GENERAL_CMD_TIMEOUT
;
846 // Fill in the command then execute
848 ZeroMem (&Cmd
, sizeof (USB_BOOT_READ_WRITE_10_CMD
));
850 Cmd
.OpCode
= Write
? USB_BOOT_WRITE10_OPCODE
: USB_BOOT_READ10_OPCODE
;
851 Cmd
.Lun
= (UINT8
)(USB_BOOT_LUN (UsbMass
->Lun
));
852 WriteUnaligned32 ((UINT32
*)Cmd
.Lba
, SwapBytes32 (Lba
));
853 WriteUnaligned16 ((UINT16
*)Cmd
.TransferLen
, SwapBytes16 ((UINT16
)Count
));
855 Status
= UsbBootExecCmdWithRetry (
858 (UINT8
)sizeof (USB_BOOT_READ_WRITE_10_CMD
),
859 Write
? EfiUsbDataOut
: EfiUsbDataIn
,
864 if (EFI_ERROR (Status
)) {
870 "UsbBoot%sBlocks: LBA (0x%lx), Blk (0x%x)\n",
871 Write
? L
"Write" : L
"Read",
884 Read or write some blocks from the device by SCSI 16 byte cmd.
886 @param UsbMass The USB mass storage device to access
887 @param Write TRUE for write operation.
888 @param Lba The start block number
889 @param TotalBlock Total block number to read or write
890 @param Buffer The buffer to read to or write from
892 @retval EFI_SUCCESS Data are read into the buffer or writen into the device.
893 @retval Others Failed to read or write all the data
896 UsbBootReadWriteBlocks16 (
897 IN USB_MASS_DEVICE
*UsbMass
,
912 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
913 CountMax
= USB_BOOT_MAX_CARRY_SIZE
/ BlockSize
;
914 Status
= EFI_SUCCESS
;
916 while (TotalBlock
> 0) {
918 // Split the total blocks into smaller pieces.
920 Count
= (UINT32
)MIN (TotalBlock
, CountMax
);
921 ByteSize
= Count
* BlockSize
;
924 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
926 Timeout
= (UINT32
)USB_BOOT_GENERAL_CMD_TIMEOUT
;
929 // Fill in the command then execute
931 ZeroMem (Cmd
, sizeof (Cmd
));
933 Cmd
[0] = Write
? EFI_SCSI_OP_WRITE16
: EFI_SCSI_OP_READ16
;
934 Cmd
[1] = (UINT8
)((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
935 WriteUnaligned64 ((UINT64
*)&Cmd
[2], SwapBytes64 (Lba
));
936 WriteUnaligned32 ((UINT32
*)&Cmd
[10], SwapBytes32 (Count
));
938 Status
= UsbBootExecCmdWithRetry (
942 Write
? EfiUsbDataOut
: EfiUsbDataIn
,
947 if (EFI_ERROR (Status
)) {
953 "UsbBoot%sBlocks16: LBA (0x%lx), Blk (0x%x)\n",
954 Write
? L
"Write" : L
"Read",
967 Use the USB clear feature control transfer to clear the endpoint stall condition.
969 @param UsbIo The USB I/O Protocol instance
970 @param EndpointAddr The endpoint to clear stall for
972 @retval EFI_SUCCESS The endpoint stall condition is cleared.
973 @retval Others Failed to clear the endpoint stall condition.
977 UsbClearEndpointStall (
978 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
979 IN UINT8 EndpointAddr
982 EFI_USB_DEVICE_REQUEST Request
;
987 Request
.RequestType
= 0x02;
988 Request
.Request
= USB_REQ_CLEAR_FEATURE
;
989 Request
.Value
= USB_FEATURE_ENDPOINT_HALT
;
990 Request
.Index
= EndpointAddr
;
992 Timeout
= USB_BOOT_GENERAL_CMD_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
994 Status
= UsbIo
->UsbControlTransfer (