2 Implementation of the command set of USB Mass Storage Specification
3 for Bootability, Revision 1.0.
5 Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Execute REQUEST SENSE Command to retrieve sense data from device.
21 @param UsbMass The device whose sense data is requested.
23 @retval EFI_SUCCESS The command is executed successfully.
24 @retval EFI_DEVICE_ERROR Failed to request sense.
25 @retval EFI_NO_RESPONSE The device media doesn't response this request.
26 @retval EFI_INVALID_PARAMETER The command has some invalid parameters.
27 @retval EFI_WRITE_PROTECTED The device is write protected.
28 @retval EFI_MEDIA_CHANGED The device media has been changed.
33 IN USB_MASS_DEVICE
*UsbMass
36 USB_BOOT_REQUEST_SENSE_CMD SenseCmd
;
37 USB_BOOT_REQUEST_SENSE_DATA SenseData
;
38 EFI_BLOCK_IO_MEDIA
*Media
;
39 USB_MASS_TRANSPORT
*Transport
;
43 Transport
= UsbMass
->Transport
;
46 // Request the sense data from the device
48 ZeroMem (&SenseCmd
, sizeof (USB_BOOT_REQUEST_SENSE_CMD
));
49 ZeroMem (&SenseData
, sizeof (USB_BOOT_REQUEST_SENSE_DATA
));
51 SenseCmd
.OpCode
= USB_BOOT_REQUEST_SENSE_OPCODE
;
52 SenseCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
53 SenseCmd
.AllocLen
= (UINT8
) sizeof (USB_BOOT_REQUEST_SENSE_DATA
);
55 Status
= Transport
->ExecCommand (
58 sizeof (USB_BOOT_REQUEST_SENSE_CMD
),
61 sizeof (USB_BOOT_REQUEST_SENSE_DATA
),
63 USB_BOOT_GENERAL_CMD_TIMEOUT
,
66 if (EFI_ERROR (Status
) || CmdResult
!= USB_MASS_CMD_SUCCESS
) {
67 DEBUG ((EFI_D_ERROR
, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status
, CmdResult
));
68 if (!EFI_ERROR (Status
)) {
69 Status
= EFI_DEVICE_ERROR
;
75 // If sense data is retrieved successfully, interpret the sense data
76 // and update the media status if necessary.
78 Media
= &UsbMass
->BlockIoMedia
;
80 switch (USB_BOOT_SENSE_KEY (SenseData
.SenseKey
)) {
82 case USB_BOOT_SENSE_NO_SENSE
:
83 if (SenseData
.Asc
== USB_BOOT_ASC_NO_ADDITIONAL_SENSE_INFORMATION
) {
85 // It is not an error if a device does not have additional sense information
89 Status
= EFI_NO_RESPONSE
;
93 case USB_BOOT_SENSE_RECOVERED
:
95 // Suppose hardware can handle this case, and recover later by itself
97 Status
= EFI_NOT_READY
;
100 case USB_BOOT_SENSE_NOT_READY
:
101 Status
= EFI_DEVICE_ERROR
;
102 if (SenseData
.Asc
== USB_BOOT_ASC_NO_MEDIA
) {
103 Media
->MediaPresent
= FALSE
;
104 Status
= EFI_NO_MEDIA
;
105 } else if (SenseData
.Asc
== USB_BOOT_ASC_NOT_READY
) {
106 Status
= EFI_NOT_READY
;
110 case USB_BOOT_SENSE_ILLEGAL_REQUEST
:
111 Status
= EFI_INVALID_PARAMETER
;
114 case USB_BOOT_SENSE_UNIT_ATTENTION
:
115 Status
= EFI_DEVICE_ERROR
;
116 if (SenseData
.Asc
== USB_BOOT_ASC_MEDIA_CHANGE
) {
118 // If MediaChange, reset ReadOnly and new MediaId
120 Status
= EFI_MEDIA_CHANGED
;
121 Media
->ReadOnly
= FALSE
;
123 } else if (SenseData
.Asc
== USB_BOOT_ASC_NOT_READY
) {
124 Status
= EFI_NOT_READY
;
125 } else if (SenseData
.Asc
== USB_BOOT_ASC_NO_MEDIA
) {
126 Status
= EFI_NOT_READY
;
130 case USB_BOOT_SENSE_DATA_PROTECT
:
131 Status
= EFI_WRITE_PROTECTED
;
132 Media
->ReadOnly
= TRUE
;
136 Status
= EFI_DEVICE_ERROR
;
140 DEBUG ((EFI_D_INFO
, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n",
142 USB_BOOT_SENSE_KEY (SenseData
.SenseKey
),
152 Execute the USB mass storage bootability commands.
154 This function executes the USB mass storage bootability commands.
155 If execution failed, retrieve the error by REQUEST_SENSE, then
156 update the device's status, such as ReadyOnly.
158 @param UsbMass The device to issue commands to
159 @param Cmd The command to execute
160 @param CmdLen The length of the command
161 @param DataDir The direction of data transfer
162 @param Data The buffer to hold the data
163 @param DataLen The length of expected data
164 @param Timeout The timeout used to transfer
166 @retval EFI_SUCCESS Command is executed successfully
167 @retval Others Command execution failed.
172 IN USB_MASS_DEVICE
*UsbMass
,
175 IN EFI_USB_DATA_DIRECTION DataDir
,
181 USB_MASS_TRANSPORT
*Transport
;
185 Transport
= UsbMass
->Transport
;
186 Status
= Transport
->ExecCommand (
198 if (Status
== EFI_TIMEOUT
) {
199 DEBUG ((EFI_D_ERROR
, "UsbBootExecCmd: Timeout to Exec 0x%x Cmd\n", *(UINT8
*)Cmd
));
204 // If ExecCommand() returns no error and CmdResult is success,
205 // then the commnad transfer is successful.
207 if ((CmdResult
== USB_MASS_CMD_SUCCESS
) && !EFI_ERROR (Status
)) {
212 // If command execution failed, then retrieve error info via sense request.
214 return UsbBootRequestSense (UsbMass
);
219 Execute the USB mass storage bootability commands with retrial.
221 This function executes USB mass storage bootability commands.
222 If the device isn't ready, wait for it. If the device is ready
223 and error occurs, retry the command again until it exceeds the
224 limit of retrial times.
226 @param UsbMass The device to issue commands to
227 @param Cmd The command to execute
228 @param CmdLen The length of the command
229 @param DataDir The direction of data transfer
230 @param Data The buffer to hold the data
231 @param DataLen The length of expected data
232 @param Timeout The timeout used to transfer
234 @retval EFI_SUCCESS The command is executed successfully.
235 @retval EFI_MEDIA_CHANGED The device media has been changed.
236 @retval Others Command execution failed after retrial.
240 UsbBootExecCmdWithRetry (
241 IN USB_MASS_DEVICE
*UsbMass
,
244 IN EFI_USB_DATA_DIRECTION DataDir
,
252 EFI_EVENT TimeoutEvt
;
255 Status
= EFI_SUCCESS
;
256 Status
= gBS
->CreateEvent (
263 if (EFI_ERROR (Status
)) {
267 Status
= gBS
->SetTimer (TimeoutEvt
, TimerRelative
, EFI_TIMER_PERIOD_SECONDS(60));
268 if (EFI_ERROR (Status
)) {
273 // Execute the cmd and retry if it fails.
275 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvt
))) {
276 Status
= UsbBootExecCmd (
285 if (Status
== EFI_SUCCESS
|| Status
== EFI_MEDIA_CHANGED
|| 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
) {
296 // If the status is other error, then just retry 5 times.
298 if (Retry
++ >= USB_BOOT_COMMAND_RETRY
) {
304 if (TimeoutEvt
!= NULL
) {
305 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
346 Execute INQUIRY Command to request information regarding parameters of
347 the device be sent to the host computer.
349 @param UsbMass The device to inquire.
351 @retval EFI_SUCCESS INQUIRY Command is executed successfully.
352 @retval Others INQUIRY Command is not executed successfully.
357 IN USB_MASS_DEVICE
*UsbMass
360 USB_BOOT_INQUIRY_CMD InquiryCmd
;
361 EFI_BLOCK_IO_MEDIA
*Media
;
364 Media
= &(UsbMass
->BlockIoMedia
);
366 ZeroMem (&InquiryCmd
, sizeof (USB_BOOT_INQUIRY_CMD
));
367 ZeroMem (&UsbMass
->InquiryData
, sizeof (USB_BOOT_INQUIRY_DATA
));
369 InquiryCmd
.OpCode
= USB_BOOT_INQUIRY_OPCODE
;
370 InquiryCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
371 InquiryCmd
.AllocLen
= (UINT8
) sizeof (USB_BOOT_INQUIRY_DATA
);
373 Status
= UsbBootExecCmdWithRetry (
376 (UINT8
) sizeof (USB_BOOT_INQUIRY_CMD
),
378 &UsbMass
->InquiryData
,
379 sizeof (USB_BOOT_INQUIRY_DATA
),
380 USB_BOOT_GENERAL_CMD_TIMEOUT
382 if (EFI_ERROR (Status
)) {
387 // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit
388 // from the inquiry data.
390 UsbMass
->Pdt
= (UINT8
) (USB_BOOT_PDT (UsbMass
->InquiryData
.Pdt
));
391 Media
->RemovableMedia
= (BOOLEAN
) (USB_BOOT_REMOVABLE (UsbMass
->InquiryData
.Removable
));
393 // Set block size to the default value of 512 Bytes, in case no media is present at first time.
395 Media
->BlockSize
= 0x0200;
401 Execute READ CAPACITY 16 bytes command to request information regarding
402 the capacity of the installed medium of the device.
404 This function executes READ CAPACITY 16 bytes command to get the capacity
405 of the USB mass storage media, including the presence, block size,
406 and last block number.
408 @param UsbMass The device to retireve disk gemotric.
410 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
411 @retval EFI_NOT_READY The returned block size is zero.
412 @retval Other READ CAPACITY 16 bytes command execution failed.
416 UsbBootReadCapacity16 (
417 IN USB_MASS_DEVICE
*UsbMass
420 UINT8 CapacityCmd
[16];
421 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData
;
422 EFI_BLOCK_IO_MEDIA
*Media
;
426 Media
= &UsbMass
->BlockIoMedia
;
428 Media
->MediaPresent
= FALSE
;
429 Media
->LastBlock
= 0;
430 Media
->BlockSize
= 0;
432 ZeroMem (CapacityCmd
, sizeof (CapacityCmd
));
433 ZeroMem (&CapacityData
, sizeof (CapacityData
));
435 CapacityCmd
[0] = EFI_SCSI_OP_READ_CAPACITY16
;
436 CapacityCmd
[1] = 0x10;
438 // Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO.
440 ZeroMem ((CapacityCmd
+ 2), 8);
442 CapacityCmd
[13] = sizeof (CapacityData
);
444 Status
= UsbBootExecCmdWithRetry (
447 (UINT8
) sizeof (CapacityCmd
),
450 sizeof (CapacityData
),
451 USB_BOOT_GENERAL_CMD_TIMEOUT
453 if (EFI_ERROR (Status
)) {
458 // Get the information on media presence, block size, and last block number
459 // from READ CAPACITY data.
461 Media
->MediaPresent
= TRUE
;
462 Media
->LastBlock
= SwapBytes64 (ReadUnaligned64 ((CONST UINT64
*) &(CapacityData
.LastLba7
)));
464 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) &(CapacityData
.BlockSize3
)));
466 Media
->LowestAlignedLba
= (CapacityData
.LowestAlignLogic2
<< 8) |
467 CapacityData
.LowestAlignLogic1
;
468 Media
->LogicalBlocksPerPhysicalBlock
= (1 << CapacityData
.LogicPerPhysical
);
469 if (BlockSize
== 0) {
473 return UsbBootRequestSense (UsbMass
);
475 Media
->BlockSize
= BlockSize
;
483 Execute READ CAPACITY command to request information regarding
484 the capacity of the installed medium of the device.
486 This function executes READ CAPACITY command to get the capacity
487 of the USB mass storage media, including the presence, block size,
488 and last block number.
490 @param UsbMass The device to retireve disk gemotric.
492 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
493 @retval EFI_NOT_READY The returned block size is zero.
494 @retval Other READ CAPACITY command execution failed.
498 UsbBootReadCapacity (
499 IN USB_MASS_DEVICE
*UsbMass
502 USB_BOOT_READ_CAPACITY_CMD CapacityCmd
;
503 USB_BOOT_READ_CAPACITY_DATA CapacityData
;
504 EFI_BLOCK_IO_MEDIA
*Media
;
508 Media
= &UsbMass
->BlockIoMedia
;
510 ZeroMem (&CapacityCmd
, sizeof (USB_BOOT_READ_CAPACITY_CMD
));
511 ZeroMem (&CapacityData
, sizeof (USB_BOOT_READ_CAPACITY_DATA
));
513 CapacityCmd
.OpCode
= USB_BOOT_READ_CAPACITY_OPCODE
;
514 CapacityCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
516 Status
= UsbBootExecCmdWithRetry (
519 (UINT8
) sizeof (USB_BOOT_READ_CAPACITY_CMD
),
522 sizeof (USB_BOOT_READ_CAPACITY_DATA
),
523 USB_BOOT_GENERAL_CMD_TIMEOUT
525 if (EFI_ERROR (Status
)) {
530 // Get the information on media presence, block size, and last block number
531 // from READ CAPACITY data.
533 Media
->MediaPresent
= TRUE
;
534 Media
->LastBlock
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) CapacityData
.LastLba
));
536 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) CapacityData
.BlockLen
));
537 if (BlockSize
== 0) {
541 return UsbBootRequestSense (UsbMass
);
543 Media
->BlockSize
= BlockSize
;
546 if (Media
->LastBlock
== 0xFFFFFFFF) {
547 Status
= UsbBootReadCapacity16 (UsbMass
);
548 if (!EFI_ERROR (Status
)) {
549 UsbMass
->Cdb16Byte
= TRUE
;
557 Retrieves SCSI mode sense information via MODE SENSE(6) command.
559 @param UsbMass The device whose sense data is requested.
561 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.
562 @retval Other Command execution failed.
567 IN USB_MASS_DEVICE
*UsbMass
571 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd
;
572 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader
;
573 EFI_BLOCK_IO_MEDIA
*Media
;
575 Media
= &UsbMass
->BlockIoMedia
;
577 ZeroMem (&ModeSenseCmd
, sizeof (USB_SCSI_MODE_SENSE6_CMD
));
578 ZeroMem (&ModeParaHeader
, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
));
581 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec
583 ModeSenseCmd
.OpCode
= USB_SCSI_MODE_SENSE6_OPCODE
;
584 ModeSenseCmd
.Lun
= (UINT8
) USB_BOOT_LUN (UsbMass
->Lun
);
585 ModeSenseCmd
.PageCode
= 0x3F;
586 ModeSenseCmd
.AllocateLen
= (UINT8
) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
);
588 Status
= UsbBootExecCmdWithRetry (
591 (UINT8
) sizeof (USB_SCSI_MODE_SENSE6_CMD
),
594 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
),
595 USB_BOOT_GENERAL_CMD_TIMEOUT
599 // Format of device-specific parameter byte of the mode parameter header is defined in
600 // Section 8.2.10 of SCSI-2 Spec.
601 // BIT7 of this byte is indicates whether the medium is write protected.
603 if (!EFI_ERROR (Status
)) {
604 Media
->ReadOnly
= (BOOLEAN
) ((ModeParaHeader
.DevicePara
& BIT7
) != 0);
612 Get the parameters for the USB mass storage media.
614 This function get the parameters for the USB mass storage media,
615 It is used both to initialize the media during the Start() phase
616 of Driver Binding Protocol and to re-initialize it when the media is
617 changed. Althought the RemoveableMedia is unlikely to change,
618 it is also included here.
620 @param UsbMass The device to retrieve disk gemotric.
622 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.
623 @retval Other Failed to get the parameters.
628 IN USB_MASS_DEVICE
*UsbMass
631 EFI_BLOCK_IO_MEDIA
*Media
;
634 Media
= &(UsbMass
->BlockIoMedia
);
636 Status
= UsbBootInquiry (UsbMass
);
637 if (EFI_ERROR (Status
)) {
638 DEBUG ((EFI_D_ERROR
, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status
));
643 // According to USB Mass Storage Specification for Bootability, only following
644 // 4 Peripheral Device Types are in spec.
646 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
647 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
648 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
649 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
650 DEBUG ((EFI_D_ERROR
, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
651 return EFI_UNSUPPORTED
;
655 // Don't use the Removable bit in inquiry data to test whether the media
656 // is removable because many flash disks wrongly set this bit.
658 if ((UsbMass
->Pdt
== USB_PDT_CDROM
) || (UsbMass
->Pdt
== USB_PDT_OPTICAL
)) {
660 // CD-Rom device and Non-CD optical device
662 UsbMass
->OpticalStorage
= TRUE
;
664 // Default value 2048 Bytes, in case no media present at first time
666 Media
->BlockSize
= 0x0800;
669 Status
= UsbBootDetectMedia (UsbMass
);
676 Detect whether the removable media is present and whether it has changed.
678 @param UsbMass The device to check.
680 @retval EFI_SUCCESS The media status is successfully checked.
681 @retval Other Failed to detect media.
686 IN USB_MASS_DEVICE
*UsbMass
689 EFI_BLOCK_IO_MEDIA OldMedia
;
690 EFI_BLOCK_IO_MEDIA
*Media
;
695 Media
= &UsbMass
->BlockIoMedia
;
697 CopyMem (&OldMedia
, &(UsbMass
->BlockIoMedia
), sizeof (EFI_BLOCK_IO_MEDIA
));
699 CmdSet
= ((EFI_USB_INTERFACE_DESCRIPTOR
*) (UsbMass
->Context
))->InterfaceSubClass
;
701 Status
= UsbBootIsUnitReady (UsbMass
);
702 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
706 if ((UsbMass
->Pdt
!= USB_PDT_CDROM
) && (CmdSet
== USB_MASS_STORE_SCSI
)) {
708 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
709 // according to Section 4 of USB Mass Storage Specification for Bootability.
710 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
711 // could get the information of Write Protected.
712 // Since not all device support this command, skip if fail.
714 UsbScsiModeSense (UsbMass
);
717 Status
= UsbBootReadCapacity (UsbMass
);
718 if (EFI_ERROR (Status
)) {
719 DEBUG ((EFI_D_ERROR
, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status
));
727 // Detect whether it is necessary to reinstall the Block I/O Protocol.
729 // MediaId may change in RequestSense for MediaChanged
730 // MediaPresent may change in RequestSense for NoMedia
731 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
732 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
734 if ((Media
->MediaId
!= OldMedia
.MediaId
) ||
735 (Media
->MediaPresent
!= OldMedia
.MediaPresent
) ||
736 (Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
737 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
738 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
741 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.
742 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().
744 OldTpl
= EfiGetCurrentTpl ();
745 gBS
->RestoreTPL (TPL_CALLBACK
);
747 gBS
->ReinstallProtocolInterface (
749 &gEfiBlockIoProtocolGuid
,
754 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK
);
755 gBS
->RaiseTPL (OldTpl
);
758 // Update MediaId after reinstalling Block I/O Protocol.
760 if (Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
761 if (Media
->MediaPresent
) {
768 if ((Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
769 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
770 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
780 Read some blocks from the device.
782 @param UsbMass The USB mass storage device to read from
783 @param Lba The start block number
784 @param TotalBlock Total block number to read
785 @param Buffer The buffer to read to
787 @retval EFI_SUCCESS Data are read into the buffer
788 @retval Others Failed to read all the data
793 IN USB_MASS_DEVICE
*UsbMass
,
799 USB_BOOT_READ10_CMD ReadCmd
;
806 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
807 Status
= EFI_SUCCESS
;
809 while (TotalBlock
> 0) {
811 // Split the total blocks into smaller pieces to ease the pressure
812 // on the device. We must split the total block because the READ10
813 // command only has 16 bit transfer length (in the unit of block).
815 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
816 ByteSize
= (UINT32
)Count
* BlockSize
;
819 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
821 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
824 // Fill in the command then execute
826 ZeroMem (&ReadCmd
, sizeof (USB_BOOT_READ10_CMD
));
828 ReadCmd
.OpCode
= USB_BOOT_READ10_OPCODE
;
829 ReadCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
830 WriteUnaligned32 ((UINT32
*) ReadCmd
.Lba
, SwapBytes32 (Lba
));
831 WriteUnaligned16 ((UINT16
*) ReadCmd
.TransferLen
, SwapBytes16 (Count
));
833 Status
= UsbBootExecCmdWithRetry (
836 (UINT8
) sizeof (USB_BOOT_READ10_CMD
),
842 if (EFI_ERROR (Status
)) {
845 DEBUG ((EFI_D_BLKIO
, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba
, Count
));
847 Buffer
+= Count
* BlockSize
;
856 Write some blocks to the device.
858 @param UsbMass The USB mass storage device to write to
859 @param Lba The start block number
860 @param TotalBlock Total block number to write
861 @param Buffer Pointer to the source buffer for the data.
863 @retval EFI_SUCCESS Data are written into the buffer
864 @retval Others Failed to write all the data
869 IN USB_MASS_DEVICE
*UsbMass
,
875 USB_BOOT_WRITE10_CMD WriteCmd
;
882 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
883 Status
= EFI_SUCCESS
;
885 while (TotalBlock
> 0) {
887 // Split the total blocks into smaller pieces to ease the pressure
888 // on the device. We must split the total block because the WRITE10
889 // command only has 16 bit transfer length (in the unit of block).
891 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
892 ByteSize
= (UINT32
)Count
* BlockSize
;
895 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
897 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
900 // Fill in the write10 command block
902 ZeroMem (&WriteCmd
, sizeof (USB_BOOT_WRITE10_CMD
));
904 WriteCmd
.OpCode
= USB_BOOT_WRITE10_OPCODE
;
905 WriteCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
906 WriteUnaligned32 ((UINT32
*) WriteCmd
.Lba
, SwapBytes32 (Lba
));
907 WriteUnaligned16 ((UINT16
*) WriteCmd
.TransferLen
, SwapBytes16 (Count
));
909 Status
= UsbBootExecCmdWithRetry (
912 (UINT8
) sizeof (USB_BOOT_WRITE10_CMD
),
918 if (EFI_ERROR (Status
)) {
921 DEBUG ((EFI_D_BLKIO
, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba
, Count
));
924 Buffer
+= Count
* BlockSize
;
932 Read some blocks from the device by SCSI 16 byte cmd.
934 @param UsbMass The USB mass storage device to read from
935 @param Lba The start block number
936 @param TotalBlock Total block number to read
937 @param Buffer The buffer to read to
939 @retval EFI_SUCCESS Data are read into the buffer
940 @retval Others Failed to read all the data
944 UsbBootReadBlocks16 (
945 IN USB_MASS_DEVICE
*UsbMass
,
958 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
959 Status
= EFI_SUCCESS
;
961 while (TotalBlock
> 0) {
963 // Split the total blocks into smaller pieces.
965 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
966 ByteSize
= (UINT32
)Count
* BlockSize
;
969 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
971 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
974 // Fill in the command then execute
976 ZeroMem (ReadCmd
, sizeof (ReadCmd
));
978 ReadCmd
[0] = EFI_SCSI_OP_READ16
;
979 ReadCmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
980 WriteUnaligned64 ((UINT64
*) &ReadCmd
[2], SwapBytes64 (Lba
));
981 WriteUnaligned32 ((UINT32
*) &ReadCmd
[10], SwapBytes32 (Count
));
983 Status
= UsbBootExecCmdWithRetry (
986 (UINT8
) sizeof (ReadCmd
),
992 if (EFI_ERROR (Status
)) {
995 DEBUG ((EFI_D_BLKIO
, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba
, Count
));
997 Buffer
+= Count
* BlockSize
;
1006 Write some blocks to the device by SCSI 16 byte cmd.
1008 @param UsbMass The USB mass storage device to write to
1009 @param Lba The start block number
1010 @param TotalBlock Total block number to write
1011 @param Buffer Pointer to the source buffer for the data.
1013 @retval EFI_SUCCESS Data are written into the buffer
1014 @retval Others Failed to write all the data
1018 UsbBootWriteBlocks16 (
1019 IN USB_MASS_DEVICE
*UsbMass
,
1021 IN UINTN TotalBlock
,
1032 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
1033 Status
= EFI_SUCCESS
;
1035 while (TotalBlock
> 0) {
1037 // Split the total blocks into smaller pieces.
1039 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
1040 ByteSize
= (UINT32
)Count
* BlockSize
;
1043 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
1045 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
1048 // Fill in the write16 command block
1050 ZeroMem (WriteCmd
, sizeof (WriteCmd
));
1052 WriteCmd
[0] = EFI_SCSI_OP_WRITE16
;
1053 WriteCmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
1054 WriteUnaligned64 ((UINT64
*) &WriteCmd
[2], SwapBytes64 (Lba
));
1055 WriteUnaligned32 ((UINT32
*) &WriteCmd
[10], SwapBytes32 (Count
));
1057 Status
= UsbBootExecCmdWithRetry (
1060 (UINT8
) sizeof (WriteCmd
),
1066 if (EFI_ERROR (Status
)) {
1069 DEBUG ((EFI_D_BLKIO
, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba
, Count
));
1071 Buffer
+= Count
* BlockSize
;
1072 TotalBlock
-= Count
;
1079 Use the USB clear feature control transfer to clear the endpoint stall condition.
1081 @param UsbIo The USB I/O Protocol instance
1082 @param EndpointAddr The endpoint to clear stall for
1084 @retval EFI_SUCCESS The endpoint stall condition is cleared.
1085 @retval Others Failed to clear the endpoint stall condition.
1089 UsbClearEndpointStall (
1090 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1091 IN UINT8 EndpointAddr
1094 EFI_USB_DEVICE_REQUEST Request
;
1099 Request
.RequestType
= 0x02;
1100 Request
.Request
= USB_REQ_CLEAR_FEATURE
;
1101 Request
.Value
= USB_FEATURE_ENDPOINT_HALT
;
1102 Request
.Index
= EndpointAddr
;
1104 Timeout
= USB_BOOT_GENERAL_CMD_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
1106 Status
= UsbIo
->UsbControlTransfer (