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 ((EFI_D_ERROR
, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status
, CmdResult
));
62 if (!EFI_ERROR (Status
)) {
63 Status
= EFI_DEVICE_ERROR
;
69 // If sense data is retrieved successfully, interpret the sense data
70 // and update the media status if necessary.
72 Media
= &UsbMass
->BlockIoMedia
;
74 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
;
87 case USB_BOOT_SENSE_RECOVERED
:
89 // Suppose hardware can handle this case, and recover later by itself
91 Status
= EFI_NOT_READY
;
94 case USB_BOOT_SENSE_NOT_READY
:
95 Status
= EFI_DEVICE_ERROR
;
96 if (SenseData
.Asc
== USB_BOOT_ASC_NO_MEDIA
) {
97 Media
->MediaPresent
= FALSE
;
98 Status
= EFI_NO_MEDIA
;
99 } else if (SenseData
.Asc
== USB_BOOT_ASC_NOT_READY
) {
100 Status
= EFI_NOT_READY
;
104 case USB_BOOT_SENSE_ILLEGAL_REQUEST
:
105 Status
= EFI_INVALID_PARAMETER
;
108 case USB_BOOT_SENSE_UNIT_ATTENTION
:
109 Status
= EFI_DEVICE_ERROR
;
110 if (SenseData
.Asc
== USB_BOOT_ASC_MEDIA_CHANGE
) {
112 // If MediaChange, reset ReadOnly and new MediaId
114 Status
= EFI_MEDIA_CHANGED
;
115 Media
->ReadOnly
= FALSE
;
117 } else if (SenseData
.Asc
== USB_BOOT_ASC_NOT_READY
) {
118 Status
= EFI_NOT_READY
;
119 } else if (SenseData
.Asc
== USB_BOOT_ASC_NO_MEDIA
) {
120 Status
= EFI_NOT_READY
;
124 case USB_BOOT_SENSE_DATA_PROTECT
:
125 Status
= EFI_WRITE_PROTECTED
;
126 Media
->ReadOnly
= TRUE
;
130 Status
= EFI_DEVICE_ERROR
;
134 DEBUG ((EFI_D_INFO
, "UsbBootRequestSense: (%r) with error code (%x) sense key %x/%x/%x\n",
137 USB_BOOT_SENSE_KEY (SenseData
.SenseKey
),
147 Execute the USB mass storage bootability commands.
149 This function executes the USB mass storage bootability commands.
150 If execution failed, retrieve the error by REQUEST_SENSE, then
151 update the device's status, such as ReadyOnly.
153 @param UsbMass The device to issue commands to
154 @param Cmd The command to execute
155 @param CmdLen The length of the command
156 @param DataDir The direction of data transfer
157 @param Data The buffer to hold the data
158 @param DataLen The length of expected data
159 @param Timeout The timeout used to transfer
161 @retval EFI_SUCCESS Command is executed successfully
162 @retval Others Command execution failed.
167 IN USB_MASS_DEVICE
*UsbMass
,
170 IN EFI_USB_DATA_DIRECTION DataDir
,
176 USB_MASS_TRANSPORT
*Transport
;
180 Transport
= UsbMass
->Transport
;
181 Status
= Transport
->ExecCommand (
193 if (Status
== EFI_TIMEOUT
) {
194 DEBUG ((EFI_D_ERROR
, "UsbBootExecCmd: %r to Exec 0x%x Cmd\n", Status
, *(UINT8
*)Cmd
));
199 // If ExecCommand() returns no error and CmdResult is success,
200 // then the command transfer is successful.
202 if ((CmdResult
== USB_MASS_CMD_SUCCESS
) && !EFI_ERROR (Status
)) {
207 // If command execution failed, then retrieve error info via sense request.
209 DEBUG ((EFI_D_ERROR
, "UsbBootExecCmd: %r to Exec 0x%x Cmd (Result = %x)\n", Status
, *(UINT8
*)Cmd
, CmdResult
));
210 return UsbBootRequestSense (UsbMass
);
215 Execute the USB mass storage bootability commands with retrial.
217 This function executes USB mass storage bootability commands.
218 If the device isn't ready, wait for it. If the device is ready
219 and error occurs, retry the command again until it exceeds the
220 limit of retrial times.
222 @param UsbMass The device to issue commands to
223 @param Cmd The command to execute
224 @param CmdLen The length of the command
225 @param DataDir The direction of data transfer
226 @param Data The buffer to hold the data
227 @param DataLen The length of expected data
228 @param Timeout The timeout used to transfer
230 @retval EFI_SUCCESS The command is executed successfully.
231 @retval EFI_NO_MEDIA The device media is removed.
232 @retval Others Command execution failed after retrial.
236 UsbBootExecCmdWithRetry (
237 IN USB_MASS_DEVICE
*UsbMass
,
240 IN EFI_USB_DATA_DIRECTION DataDir
,
248 EFI_EVENT TimeoutEvt
;
251 Status
= EFI_SUCCESS
;
252 Status
= gBS
->CreateEvent (
259 if (EFI_ERROR (Status
)) {
263 Status
= gBS
->SetTimer (TimeoutEvt
, TimerRelative
, EFI_TIMER_PERIOD_SECONDS(60));
264 if (EFI_ERROR (Status
)) {
269 // Execute the cmd and retry if it fails.
271 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvt
))) {
272 Status
= UsbBootExecCmd (
281 if (Status
== EFI_SUCCESS
|| Status
== EFI_NO_MEDIA
) {
285 // If the sense data shows the drive is not ready, we need execute the cmd again.
286 // We limit the upper boundary to 60 seconds.
288 if (Status
== EFI_NOT_READY
) {
292 // If the status is other error, then just retry 5 times.
294 if (Retry
++ >= USB_BOOT_COMMAND_RETRY
) {
300 if (TimeoutEvt
!= NULL
) {
301 gBS
->CloseEvent (TimeoutEvt
);
309 Execute TEST UNIT READY command to check if the device is ready.
311 @param UsbMass The device to test
313 @retval EFI_SUCCESS The device is ready.
314 @retval Others Device not ready.
319 IN USB_MASS_DEVICE
*UsbMass
322 USB_BOOT_TEST_UNIT_READY_CMD TestCmd
;
324 ZeroMem (&TestCmd
, sizeof (USB_BOOT_TEST_UNIT_READY_CMD
));
326 TestCmd
.OpCode
= USB_BOOT_TEST_UNIT_READY_OPCODE
;
327 TestCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
329 return UsbBootExecCmdWithRetry (
332 (UINT8
) sizeof (USB_BOOT_TEST_UNIT_READY_CMD
),
336 USB_BOOT_GENERAL_CMD_TIMEOUT
342 Execute INQUIRY Command to request information regarding parameters of
343 the device be sent to the host computer.
345 @param UsbMass The device to inquire.
347 @retval EFI_SUCCESS INQUIRY Command is executed successfully.
348 @retval Others INQUIRY Command is not executed successfully.
353 IN USB_MASS_DEVICE
*UsbMass
356 USB_BOOT_INQUIRY_CMD InquiryCmd
;
357 EFI_BLOCK_IO_MEDIA
*Media
;
360 Media
= &(UsbMass
->BlockIoMedia
);
362 ZeroMem (&InquiryCmd
, sizeof (USB_BOOT_INQUIRY_CMD
));
363 ZeroMem (&UsbMass
->InquiryData
, sizeof (USB_BOOT_INQUIRY_DATA
));
365 InquiryCmd
.OpCode
= USB_BOOT_INQUIRY_OPCODE
;
366 InquiryCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
367 InquiryCmd
.AllocLen
= (UINT8
) sizeof (USB_BOOT_INQUIRY_DATA
);
369 Status
= UsbBootExecCmdWithRetry (
372 (UINT8
) sizeof (USB_BOOT_INQUIRY_CMD
),
374 &UsbMass
->InquiryData
,
375 sizeof (USB_BOOT_INQUIRY_DATA
),
376 USB_BOOT_GENERAL_CMD_TIMEOUT
378 if (EFI_ERROR (Status
)) {
383 // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit
384 // from the inquiry data.
386 UsbMass
->Pdt
= (UINT8
) (USB_BOOT_PDT (UsbMass
->InquiryData
.Pdt
));
387 Media
->RemovableMedia
= (BOOLEAN
) (USB_BOOT_REMOVABLE (UsbMass
->InquiryData
.Removable
));
389 // Set block size to the default value of 512 Bytes, in case no media is present at first time.
391 Media
->BlockSize
= 0x0200;
397 Execute READ CAPACITY 16 bytes command to request information regarding
398 the capacity of the installed medium of the device.
400 This function executes READ CAPACITY 16 bytes command to get the capacity
401 of the USB mass storage media, including the presence, block size,
402 and last block number.
404 @param UsbMass The device to retireve disk gemotric.
406 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
407 @retval EFI_NOT_READY The returned block size is zero.
408 @retval Other READ CAPACITY 16 bytes command execution failed.
412 UsbBootReadCapacity16 (
413 IN USB_MASS_DEVICE
*UsbMass
416 UINT8 CapacityCmd
[16];
417 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData
;
418 EFI_BLOCK_IO_MEDIA
*Media
;
422 Media
= &UsbMass
->BlockIoMedia
;
424 Media
->MediaPresent
= FALSE
;
425 Media
->LastBlock
= 0;
426 Media
->BlockSize
= 0;
428 ZeroMem (CapacityCmd
, sizeof (CapacityCmd
));
429 ZeroMem (&CapacityData
, sizeof (CapacityData
));
431 CapacityCmd
[0] = EFI_SCSI_OP_READ_CAPACITY16
;
432 CapacityCmd
[1] = 0x10;
434 // Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO.
436 ZeroMem ((CapacityCmd
+ 2), 8);
438 CapacityCmd
[13] = sizeof (CapacityData
);
440 Status
= UsbBootExecCmdWithRetry (
443 (UINT8
) sizeof (CapacityCmd
),
446 sizeof (CapacityData
),
447 USB_BOOT_GENERAL_CMD_TIMEOUT
449 if (EFI_ERROR (Status
)) {
454 // Get the information on media presence, block size, and last block number
455 // from READ CAPACITY data.
457 Media
->MediaPresent
= TRUE
;
458 Media
->LastBlock
= SwapBytes64 (ReadUnaligned64 ((CONST UINT64
*) &(CapacityData
.LastLba7
)));
460 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) &(CapacityData
.BlockSize3
)));
462 Media
->LowestAlignedLba
= (CapacityData
.LowestAlignLogic2
<< 8) |
463 CapacityData
.LowestAlignLogic1
;
464 Media
->LogicalBlocksPerPhysicalBlock
= (1 << CapacityData
.LogicPerPhysical
);
465 if (BlockSize
== 0) {
469 return UsbBootRequestSense (UsbMass
);
471 Media
->BlockSize
= BlockSize
;
479 Execute READ CAPACITY command to request information regarding
480 the capacity of the installed medium of the device.
482 This function executes READ CAPACITY command to get the capacity
483 of the USB mass storage media, including the presence, block size,
484 and last block number.
486 @param UsbMass The device to retireve disk gemotric.
488 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
489 @retval EFI_NOT_READY The returned block size is zero.
490 @retval Other READ CAPACITY command execution failed.
494 UsbBootReadCapacity (
495 IN USB_MASS_DEVICE
*UsbMass
498 USB_BOOT_READ_CAPACITY_CMD CapacityCmd
;
499 USB_BOOT_READ_CAPACITY_DATA CapacityData
;
500 EFI_BLOCK_IO_MEDIA
*Media
;
504 Media
= &UsbMass
->BlockIoMedia
;
506 ZeroMem (&CapacityCmd
, sizeof (USB_BOOT_READ_CAPACITY_CMD
));
507 ZeroMem (&CapacityData
, sizeof (USB_BOOT_READ_CAPACITY_DATA
));
509 CapacityCmd
.OpCode
= USB_BOOT_READ_CAPACITY_OPCODE
;
510 CapacityCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
512 Status
= UsbBootExecCmdWithRetry (
515 (UINT8
) sizeof (USB_BOOT_READ_CAPACITY_CMD
),
518 sizeof (USB_BOOT_READ_CAPACITY_DATA
),
519 USB_BOOT_GENERAL_CMD_TIMEOUT
521 if (EFI_ERROR (Status
)) {
526 // Get the information on media presence, block size, and last block number
527 // from READ CAPACITY data.
529 Media
->MediaPresent
= TRUE
;
530 Media
->LastBlock
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) CapacityData
.LastLba
));
532 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) CapacityData
.BlockLen
));
533 if (BlockSize
== 0) {
537 return UsbBootRequestSense (UsbMass
);
539 Media
->BlockSize
= BlockSize
;
542 if (Media
->LastBlock
== 0xFFFFFFFF) {
543 Status
= UsbBootReadCapacity16 (UsbMass
);
544 if (!EFI_ERROR (Status
)) {
545 UsbMass
->Cdb16Byte
= TRUE
;
553 Retrieves SCSI mode sense information via MODE SENSE(6) command.
555 @param UsbMass The device whose sense data is requested.
557 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.
558 @retval Other Command execution failed.
563 IN USB_MASS_DEVICE
*UsbMass
567 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd
;
568 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader
;
569 EFI_BLOCK_IO_MEDIA
*Media
;
571 Media
= &UsbMass
->BlockIoMedia
;
573 ZeroMem (&ModeSenseCmd
, sizeof (USB_SCSI_MODE_SENSE6_CMD
));
574 ZeroMem (&ModeParaHeader
, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
));
577 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec
579 ModeSenseCmd
.OpCode
= USB_SCSI_MODE_SENSE6_OPCODE
;
580 ModeSenseCmd
.Lun
= (UINT8
) USB_BOOT_LUN (UsbMass
->Lun
);
581 ModeSenseCmd
.PageCode
= 0x3F;
582 ModeSenseCmd
.AllocateLen
= (UINT8
) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
);
584 Status
= UsbBootExecCmdWithRetry (
587 (UINT8
) sizeof (USB_SCSI_MODE_SENSE6_CMD
),
590 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
),
591 USB_BOOT_GENERAL_CMD_TIMEOUT
595 // Format of device-specific parameter byte of the mode parameter header is defined in
596 // Section 8.2.10 of SCSI-2 Spec.
597 // BIT7 of this byte is indicates whether the medium is write protected.
599 if (!EFI_ERROR (Status
)) {
600 Media
->ReadOnly
= (BOOLEAN
) ((ModeParaHeader
.DevicePara
& BIT7
) != 0);
608 Get the parameters for the USB mass storage media.
610 This function get the parameters for the USB mass storage media,
611 It is used both to initialize the media during the Start() phase
612 of Driver Binding Protocol and to re-initialize it when the media is
613 changed. Although the RemoveableMedia is unlikely to change,
614 it is also included here.
616 @param UsbMass The device to retrieve disk gemotric.
618 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.
619 @retval Other Failed to get the parameters.
624 IN USB_MASS_DEVICE
*UsbMass
627 EFI_BLOCK_IO_MEDIA
*Media
;
630 Media
= &(UsbMass
->BlockIoMedia
);
632 Status
= UsbBootInquiry (UsbMass
);
633 if (EFI_ERROR (Status
)) {
634 DEBUG ((EFI_D_ERROR
, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status
));
639 // According to USB Mass Storage Specification for Bootability, only following
640 // 4 Peripheral Device Types are in spec.
642 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
643 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
644 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
645 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
646 DEBUG ((EFI_D_ERROR
, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
647 return EFI_UNSUPPORTED
;
651 // Don't use the Removable bit in inquiry data to test whether the media
652 // is removable because many flash disks wrongly set this bit.
654 if ((UsbMass
->Pdt
== USB_PDT_CDROM
) || (UsbMass
->Pdt
== USB_PDT_OPTICAL
)) {
656 // CD-Rom device and Non-CD optical device
658 UsbMass
->OpticalStorage
= TRUE
;
660 // Default value 2048 Bytes, in case no media present at first time
662 Media
->BlockSize
= 0x0800;
665 Status
= UsbBootDetectMedia (UsbMass
);
672 Detect whether the removable media is present and whether it has changed.
674 @param UsbMass The device to check.
676 @retval EFI_SUCCESS The media status is successfully checked.
677 @retval Other Failed to detect media.
682 IN USB_MASS_DEVICE
*UsbMass
685 EFI_BLOCK_IO_MEDIA OldMedia
;
686 EFI_BLOCK_IO_MEDIA
*Media
;
690 Media
= &UsbMass
->BlockIoMedia
;
692 CopyMem (&OldMedia
, &(UsbMass
->BlockIoMedia
), sizeof (EFI_BLOCK_IO_MEDIA
));
694 CmdSet
= ((EFI_USB_INTERFACE_DESCRIPTOR
*) (UsbMass
->Context
))->InterfaceSubClass
;
696 Status
= UsbBootIsUnitReady (UsbMass
);
697 if (EFI_ERROR (Status
)) {
698 DEBUG ((EFI_D_ERROR
, "UsbBootDetectMedia: UsbBootIsUnitReady (%r)\n", Status
));
703 // EFI_SUCCESS: all good.
704 // EFI_NO_MEDIA: media is not present.
706 // For either EFI_NO_MEDIA, or HW error, skip to get WriteProtected and capacity information.
708 if (!EFI_ERROR (Status
)) {
709 if ((UsbMass
->Pdt
!= USB_PDT_CDROM
) && (CmdSet
== USB_MASS_STORE_SCSI
)) {
711 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
712 // according to Section 4 of USB Mass Storage Specification for Bootability.
713 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
714 // could get the information of Write Protected.
715 // Since not all device support this command, skip if fail.
717 UsbScsiModeSense (UsbMass
);
720 Status
= UsbBootReadCapacity (UsbMass
);
721 if (EFI_ERROR (Status
)) {
722 DEBUG ((EFI_D_ERROR
, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status
));
726 if (EFI_ERROR (Status
) && Status
!= EFI_NO_MEDIA
) {
728 // For NoMedia, BlockIo is still needed.
734 // Simply reject device whose block size is unacceptable small (==0) or large (>64K).
736 if ((Media
->BlockSize
== 0) || (Media
->BlockSize
> USB_BOOT_MAX_CARRY_SIZE
)) {
737 return EFI_DEVICE_ERROR
;
741 // Detect whether it is necessary to reinstall the Block I/O Protocol.
743 // MediaId may change in RequestSense for MediaChanged
744 // MediaPresent may change in RequestSense for NoMedia
745 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
746 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
748 if ((Media
->MediaId
!= OldMedia
.MediaId
) ||
749 (Media
->MediaPresent
!= OldMedia
.MediaPresent
) ||
750 (Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
751 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
752 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
755 // This function is called from:
756 // Block I/O Protocol APIs, which run at TPL_CALLBACK.
757 // DriverBindingStart(), which raises to TPL_CALLBACK.
758 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK
);
761 // When it is called from DriverBindingStart(), below reinstall fails.
762 // So ignore the return status check.
764 gBS
->ReinstallProtocolInterface (
766 &gEfiBlockIoProtocolGuid
,
772 // Reset MediaId after reinstalling Block I/O Protocol.
774 if (Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
775 if (Media
->MediaPresent
) {
782 if ((Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
783 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
784 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
788 Status
= Media
->MediaPresent
? EFI_MEDIA_CHANGED
: EFI_NO_MEDIA
;
796 Read or write some blocks from the device.
798 @param UsbMass The USB mass storage device to access
799 @param Write TRUE for write operation.
800 @param Lba The start block number
801 @param TotalBlock Total block number to read or write
802 @param Buffer The buffer to read to or write from
804 @retval EFI_SUCCESS Data are read into the buffer or writen into the device.
805 @retval Others Failed to read or write all the data
809 UsbBootReadWriteBlocks (
810 IN USB_MASS_DEVICE
*UsbMass
,
817 USB_BOOT_READ_WRITE_10_CMD Cmd
;
825 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
826 CountMax
= USB_BOOT_MAX_CARRY_SIZE
/ BlockSize
;
827 Status
= EFI_SUCCESS
;
829 while (TotalBlock
> 0) {
831 // Split the total blocks into smaller pieces to ease the pressure
832 // on the device. We must split the total block because the READ10
833 // command only has 16 bit transfer length (in the unit of block).
835 Count
= (UINT32
)MIN (TotalBlock
, CountMax
);
836 Count
= MIN (MAX_UINT16
, Count
);
837 ByteSize
= Count
* BlockSize
;
840 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
842 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
845 // Fill in the command then execute
847 ZeroMem (&Cmd
, sizeof (USB_BOOT_READ_WRITE_10_CMD
));
849 Cmd
.OpCode
= Write
? USB_BOOT_WRITE10_OPCODE
: USB_BOOT_READ10_OPCODE
;
850 Cmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
851 WriteUnaligned32 ((UINT32
*) Cmd
.Lba
, SwapBytes32 (Lba
));
852 WriteUnaligned16 ((UINT16
*) Cmd
.TransferLen
, SwapBytes16 ((UINT16
)Count
));
854 Status
= UsbBootExecCmdWithRetry (
857 (UINT8
) sizeof (USB_BOOT_READ_WRITE_10_CMD
),
858 Write
? EfiUsbDataOut
: EfiUsbDataIn
,
863 if (EFI_ERROR (Status
)) {
867 DEBUG_BLKIO
, "UsbBoot%sBlocks: LBA (0x%lx), Blk (0x%x)\n",
868 Write
? L
"Write" : L
"Read",
880 Read or write some blocks from the device by SCSI 16 byte cmd.
882 @param UsbMass The USB mass storage device to access
883 @param Write TRUE for write operation.
884 @param Lba The start block number
885 @param TotalBlock Total block number to read or write
886 @param Buffer The buffer to read to or write from
888 @retval EFI_SUCCESS Data are read into the buffer or writen into the device.
889 @retval Others Failed to read or write all the data
892 UsbBootReadWriteBlocks16 (
893 IN USB_MASS_DEVICE
*UsbMass
,
908 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
909 CountMax
= USB_BOOT_MAX_CARRY_SIZE
/ BlockSize
;
910 Status
= EFI_SUCCESS
;
912 while (TotalBlock
> 0) {
914 // Split the total blocks into smaller pieces.
916 Count
= (UINT32
)MIN (TotalBlock
, CountMax
);
917 ByteSize
= Count
* BlockSize
;
920 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
922 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
925 // Fill in the command then execute
927 ZeroMem (Cmd
, sizeof (Cmd
));
929 Cmd
[0] = Write
? EFI_SCSI_OP_WRITE16
: EFI_SCSI_OP_READ16
;
930 Cmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
931 WriteUnaligned64 ((UINT64
*) &Cmd
[2], SwapBytes64 (Lba
));
932 WriteUnaligned32 ((UINT32
*) &Cmd
[10], SwapBytes32 (Count
));
934 Status
= UsbBootExecCmdWithRetry (
937 (UINT8
) sizeof (Cmd
),
938 Write
? EfiUsbDataOut
: EfiUsbDataIn
,
943 if (EFI_ERROR (Status
)) {
947 DEBUG_BLKIO
, "UsbBoot%sBlocks16: LBA (0x%lx), Blk (0x%x)\n",
948 Write
? L
"Write" : L
"Read",
960 Use the USB clear feature control transfer to clear the endpoint stall condition.
962 @param UsbIo The USB I/O Protocol instance
963 @param EndpointAddr The endpoint to clear stall for
965 @retval EFI_SUCCESS The endpoint stall condition is cleared.
966 @retval Others Failed to clear the endpoint stall condition.
970 UsbClearEndpointStall (
971 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
972 IN UINT8 EndpointAddr
975 EFI_USB_DEVICE_REQUEST Request
;
980 Request
.RequestType
= 0x02;
981 Request
.Request
= USB_REQ_CLEAR_FEATURE
;
982 Request
.Value
= USB_FEATURE_ENDPOINT_HALT
;
983 Request
.Index
= EndpointAddr
;
985 Timeout
= USB_BOOT_GENERAL_CMD_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
987 Status
= UsbIo
->UsbControlTransfer (