2 Implementation of the command set of USB Mass Storage Specification
3 for Bootability, Revision 1.0.
5 Copyright (c) 2007 - 2016, 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 Status
= EFI_NO_RESPONSE
;
86 case USB_BOOT_SENSE_RECOVERED
:
88 // Suppose hardware can handle this case, and recover later by itself
90 Status
= EFI_NOT_READY
;
93 case USB_BOOT_SENSE_NOT_READY
:
94 Status
= EFI_DEVICE_ERROR
;
95 if (SenseData
.Asc
== USB_BOOT_ASC_NO_MEDIA
) {
96 Media
->MediaPresent
= FALSE
;
97 Status
= EFI_NO_MEDIA
;
98 } else if (SenseData
.Asc
== USB_BOOT_ASC_NOT_READY
) {
99 Status
= EFI_NOT_READY
;
103 case USB_BOOT_SENSE_ILLEGAL_REQUEST
:
104 Status
= EFI_INVALID_PARAMETER
;
107 case USB_BOOT_SENSE_UNIT_ATTENTION
:
108 Status
= EFI_DEVICE_ERROR
;
109 if (SenseData
.Asc
== USB_BOOT_ASC_MEDIA_CHANGE
) {
111 // If MediaChange, reset ReadOnly and new MediaId
113 Status
= EFI_MEDIA_CHANGED
;
114 Media
->ReadOnly
= FALSE
;
119 case USB_BOOT_SENSE_DATA_PROTECT
:
120 Status
= EFI_WRITE_PROTECTED
;
121 Media
->ReadOnly
= TRUE
;
125 Status
= EFI_DEVICE_ERROR
;
129 DEBUG ((EFI_D_INFO
, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n",
131 USB_BOOT_SENSE_KEY (SenseData
.SenseKey
),
141 Execute the USB mass storage bootability commands.
143 This function executes the USB mass storage bootability commands.
144 If execution failed, retrieve the error by REQUEST_SENSE, then
145 update the device's status, such as ReadyOnly.
147 @param UsbMass The device to issue commands to
148 @param Cmd The command to execute
149 @param CmdLen The length of the command
150 @param DataDir The direction of data transfer
151 @param Data The buffer to hold the data
152 @param DataLen The length of expected data
153 @param Timeout The timeout used to transfer
155 @retval EFI_SUCCESS Command is executed successfully
156 @retval Others Command execution failed.
161 IN USB_MASS_DEVICE
*UsbMass
,
164 IN EFI_USB_DATA_DIRECTION DataDir
,
170 USB_MASS_TRANSPORT
*Transport
;
174 Transport
= UsbMass
->Transport
;
175 Status
= Transport
->ExecCommand (
187 if (Status
== EFI_TIMEOUT
) {
188 DEBUG ((EFI_D_ERROR
, "UsbBootExecCmd: Timeout to Exec 0x%x Cmd\n", *(UINT8
*)Cmd
));
192 if (Status
== EFI_DEVICE_ERROR
) {
193 DEBUG ((EFI_D_ERROR
, "UsbBootExecCmd: Device Error to Exec 0x%x Cmd\n", *(UINT8
*)Cmd
));
194 return EFI_DEVICE_ERROR
;
198 // If ExecCommand() returns no error and CmdResult is success,
199 // then the commnad transfer is successful.
201 if ((CmdResult
== USB_MASS_CMD_SUCCESS
) && !EFI_ERROR (Status
)) {
206 // If command execution failed, then retrieve error info via sense request.
208 return UsbBootRequestSense (UsbMass
);
213 Execute the USB mass storage bootability commands with retrial.
215 This function executes USB mass storage bootability commands.
216 If the device isn't ready, wait for it. If the device is ready
217 and error occurs, retry the command again until it exceeds the
218 limit of retrial times.
220 @param UsbMass The device to issue commands to
221 @param Cmd The command to execute
222 @param CmdLen The length of the command
223 @param DataDir The direction of data transfer
224 @param Data The buffer to hold the data
225 @param DataLen The length of expected data
226 @param Timeout The timeout used to transfer
228 @retval EFI_SUCCESS The command is executed successfully.
229 @retval EFI_MEDIA_CHANGED The device media has been changed.
230 @retval Others Command execution failed after retrial.
234 UsbBootExecCmdWithRetry (
235 IN USB_MASS_DEVICE
*UsbMass
,
238 IN EFI_USB_DATA_DIRECTION DataDir
,
246 EFI_EVENT TimeoutEvt
;
249 Status
= EFI_SUCCESS
;
250 Status
= gBS
->CreateEvent (
257 if (EFI_ERROR (Status
)) {
261 Status
= gBS
->SetTimer (TimeoutEvt
, TimerRelative
, EFI_TIMER_PERIOD_SECONDS(60));
262 if (EFI_ERROR (Status
)) {
267 // Execute the cmd and retry if it fails.
269 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvt
))) {
270 Status
= UsbBootExecCmd (
279 if (Status
== EFI_SUCCESS
|| Status
== EFI_MEDIA_CHANGED
|| Status
== EFI_NO_MEDIA
|| Status
== EFI_DEVICE_ERROR
) {
283 // If the sense data shows the drive is not ready, we need execute the cmd again.
284 // We limit the upper boundary to 60 seconds.
286 if (Status
== EFI_NOT_READY
) {
290 // If the status is other error, then just retry 5 times.
292 if (Retry
++ >= USB_BOOT_COMMAND_RETRY
) {
298 if (TimeoutEvt
!= NULL
) {
299 gBS
->CloseEvent (TimeoutEvt
);
307 Execute TEST UNIT READY command to check if the device is ready.
309 @param UsbMass The device to test
311 @retval EFI_SUCCESS The device is ready.
312 @retval Others Device not ready.
317 IN USB_MASS_DEVICE
*UsbMass
320 USB_BOOT_TEST_UNIT_READY_CMD TestCmd
;
322 ZeroMem (&TestCmd
, sizeof (USB_BOOT_TEST_UNIT_READY_CMD
));
324 TestCmd
.OpCode
= USB_BOOT_TEST_UNIT_READY_OPCODE
;
325 TestCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
327 return UsbBootExecCmdWithRetry (
330 (UINT8
) sizeof (USB_BOOT_TEST_UNIT_READY_CMD
),
334 USB_BOOT_GENERAL_CMD_TIMEOUT
340 Execute INQUIRY Command to request information regarding parameters of
341 the device be sent to the host computer.
343 @param UsbMass The device to inquire.
345 @retval EFI_SUCCESS INQUIRY Command is executed successfully.
346 @retval Others INQUIRY Command is not executed successfully.
351 IN USB_MASS_DEVICE
*UsbMass
354 USB_BOOT_INQUIRY_CMD InquiryCmd
;
355 EFI_BLOCK_IO_MEDIA
*Media
;
358 Media
= &(UsbMass
->BlockIoMedia
);
360 ZeroMem (&InquiryCmd
, sizeof (USB_BOOT_INQUIRY_CMD
));
361 ZeroMem (&UsbMass
->InquiryData
, sizeof (USB_BOOT_INQUIRY_DATA
));
363 InquiryCmd
.OpCode
= USB_BOOT_INQUIRY_OPCODE
;
364 InquiryCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
365 InquiryCmd
.AllocLen
= (UINT8
) sizeof (USB_BOOT_INQUIRY_DATA
);
367 Status
= UsbBootExecCmdWithRetry (
370 (UINT8
) sizeof (USB_BOOT_INQUIRY_CMD
),
372 &UsbMass
->InquiryData
,
373 sizeof (USB_BOOT_INQUIRY_DATA
),
374 USB_BOOT_GENERAL_CMD_TIMEOUT
376 if (EFI_ERROR (Status
)) {
381 // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit
382 // from the inquiry data.
384 UsbMass
->Pdt
= (UINT8
) (USB_BOOT_PDT (UsbMass
->InquiryData
.Pdt
));
385 Media
->RemovableMedia
= (BOOLEAN
) (USB_BOOT_REMOVABLE (UsbMass
->InquiryData
.Removable
));
387 // Set block size to the default value of 512 Bytes, in case no media is present at first time.
389 Media
->BlockSize
= 0x0200;
395 Execute READ CAPACITY 16 bytes command to request information regarding
396 the capacity of the installed medium of the device.
398 This function executes READ CAPACITY 16 bytes command to get the capacity
399 of the USB mass storage media, including the presence, block size,
400 and last block number.
402 @param UsbMass The device to retireve disk gemotric.
404 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
405 @retval EFI_NOT_READY The returned block size is zero.
406 @retval Other READ CAPACITY 16 bytes command execution failed.
410 UsbBootReadCapacity16 (
411 IN USB_MASS_DEVICE
*UsbMass
414 UINT8 CapacityCmd
[16];
415 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData
;
416 EFI_BLOCK_IO_MEDIA
*Media
;
420 Media
= &UsbMass
->BlockIoMedia
;
422 Media
->MediaPresent
= FALSE
;
423 Media
->LastBlock
= 0;
424 Media
->BlockSize
= 0;
426 ZeroMem (CapacityCmd
, sizeof (CapacityCmd
));
427 ZeroMem (&CapacityData
, sizeof (CapacityData
));
429 CapacityCmd
[0] = EFI_SCSI_OP_READ_CAPACITY16
;
430 CapacityCmd
[1] = 0x10;
432 // Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO.
434 ZeroMem ((CapacityCmd
+ 2), 8);
436 CapacityCmd
[13] = sizeof (CapacityData
);
438 Status
= UsbBootExecCmdWithRetry (
441 (UINT8
) sizeof (CapacityCmd
),
444 sizeof (CapacityData
),
445 USB_BOOT_GENERAL_CMD_TIMEOUT
447 if (EFI_ERROR (Status
)) {
452 // Get the information on media presence, block size, and last block number
453 // from READ CAPACITY data.
455 Media
->MediaPresent
= TRUE
;
456 Media
->LastBlock
= SwapBytes64 (ReadUnaligned64 ((CONST UINT64
*) &(CapacityData
.LastLba7
)));
458 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) &(CapacityData
.BlockSize3
)));
460 Media
->LowestAlignedLba
= (CapacityData
.LowestAlignLogic2
<< 8) |
461 CapacityData
.LowestAlignLogic1
;
462 Media
->LogicalBlocksPerPhysicalBlock
= (1 << CapacityData
.LogicPerPhysical
);
463 if (BlockSize
== 0) {
467 return UsbBootRequestSense (UsbMass
);
469 Media
->BlockSize
= BlockSize
;
477 Execute READ CAPACITY command to request information regarding
478 the capacity of the installed medium of the device.
480 This function executes READ CAPACITY command to get the capacity
481 of the USB mass storage media, including the presence, block size,
482 and last block number.
484 @param UsbMass The device to retireve disk gemotric.
486 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
487 @retval EFI_NOT_READY The returned block size is zero.
488 @retval Other READ CAPACITY command execution failed.
492 UsbBootReadCapacity (
493 IN USB_MASS_DEVICE
*UsbMass
496 USB_BOOT_READ_CAPACITY_CMD CapacityCmd
;
497 USB_BOOT_READ_CAPACITY_DATA CapacityData
;
498 EFI_BLOCK_IO_MEDIA
*Media
;
502 Media
= &UsbMass
->BlockIoMedia
;
504 ZeroMem (&CapacityCmd
, sizeof (USB_BOOT_READ_CAPACITY_CMD
));
505 ZeroMem (&CapacityData
, sizeof (USB_BOOT_READ_CAPACITY_DATA
));
507 CapacityCmd
.OpCode
= USB_BOOT_READ_CAPACITY_OPCODE
;
508 CapacityCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
510 Status
= UsbBootExecCmdWithRetry (
513 (UINT8
) sizeof (USB_BOOT_READ_CAPACITY_CMD
),
516 sizeof (USB_BOOT_READ_CAPACITY_DATA
),
517 USB_BOOT_GENERAL_CMD_TIMEOUT
519 if (EFI_ERROR (Status
)) {
524 // Get the information on media presence, block size, and last block number
525 // from READ CAPACITY data.
527 Media
->MediaPresent
= TRUE
;
528 Media
->LastBlock
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) CapacityData
.LastLba
));
530 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) CapacityData
.BlockLen
));
531 if (BlockSize
== 0) {
535 return UsbBootRequestSense (UsbMass
);
537 Media
->BlockSize
= BlockSize
;
540 if (Media
->LastBlock
== 0xFFFFFFFF) {
541 Status
= UsbBootReadCapacity16 (UsbMass
);
542 if (!EFI_ERROR (Status
)) {
543 UsbMass
->Cdb16Byte
= TRUE
;
551 Retrieves SCSI mode sense information via MODE SENSE(6) command.
553 @param UsbMass The device whose sense data is requested.
555 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.
556 @retval Other Command execution failed.
561 IN USB_MASS_DEVICE
*UsbMass
565 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd
;
566 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader
;
567 EFI_BLOCK_IO_MEDIA
*Media
;
569 Media
= &UsbMass
->BlockIoMedia
;
571 ZeroMem (&ModeSenseCmd
, sizeof (USB_SCSI_MODE_SENSE6_CMD
));
572 ZeroMem (&ModeParaHeader
, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
));
575 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec
577 ModeSenseCmd
.OpCode
= USB_SCSI_MODE_SENSE6_OPCODE
;
578 ModeSenseCmd
.Lun
= (UINT8
) USB_BOOT_LUN (UsbMass
->Lun
);
579 ModeSenseCmd
.PageCode
= 0x3F;
580 ModeSenseCmd
.AllocateLen
= (UINT8
) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
);
582 Status
= UsbBootExecCmdWithRetry (
585 (UINT8
) sizeof (USB_SCSI_MODE_SENSE6_CMD
),
588 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
),
589 USB_BOOT_GENERAL_CMD_TIMEOUT
593 // Format of device-specific parameter byte of the mode parameter header is defined in
594 // Section 8.2.10 of SCSI-2 Spec.
595 // BIT7 of this byte is indicates whether the medium is write protected.
597 if (!EFI_ERROR (Status
)) {
598 Media
->ReadOnly
= (BOOLEAN
) ((ModeParaHeader
.DevicePara
& BIT7
) != 0);
606 Get the parameters for the USB mass storage media.
608 This function get the parameters for the USB mass storage media,
609 It is used both to initialize the media during the Start() phase
610 of Driver Binding Protocol and to re-initialize it when the media is
611 changed. Althought the RemoveableMedia is unlikely to change,
612 it is also included here.
614 @param UsbMass The device to retrieve disk gemotric.
616 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.
617 @retval Other Failed to get the parameters.
622 IN USB_MASS_DEVICE
*UsbMass
625 EFI_BLOCK_IO_MEDIA
*Media
;
628 Media
= &(UsbMass
->BlockIoMedia
);
630 Status
= UsbBootInquiry (UsbMass
);
631 if (EFI_ERROR (Status
)) {
632 DEBUG ((EFI_D_ERROR
, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status
));
637 // According to USB Mass Storage Specification for Bootability, only following
638 // 4 Peripheral Device Types are in spec.
640 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
641 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
642 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
643 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
644 DEBUG ((EFI_D_ERROR
, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
645 return EFI_UNSUPPORTED
;
649 // Don't use the Removable bit in inquiry data to test whether the media
650 // is removable because many flash disks wrongly set this bit.
652 if ((UsbMass
->Pdt
== USB_PDT_CDROM
) || (UsbMass
->Pdt
== USB_PDT_OPTICAL
)) {
654 // CD-Rom device and Non-CD optical device
656 UsbMass
->OpticalStorage
= TRUE
;
658 // Default value 2048 Bytes, in case no media present at first time
660 Media
->BlockSize
= 0x0800;
663 Status
= UsbBootDetectMedia (UsbMass
);
670 Detect whether the removable media is present and whether it has changed.
672 @param UsbMass The device to check.
674 @retval EFI_SUCCESS The media status is successfully checked.
675 @retval Other Failed to detect media.
680 IN USB_MASS_DEVICE
*UsbMass
683 EFI_BLOCK_IO_MEDIA OldMedia
;
684 EFI_BLOCK_IO_MEDIA
*Media
;
689 Media
= &UsbMass
->BlockIoMedia
;
691 CopyMem (&OldMedia
, &(UsbMass
->BlockIoMedia
), sizeof (EFI_BLOCK_IO_MEDIA
));
693 CmdSet
= ((EFI_USB_INTERFACE_DESCRIPTOR
*) (UsbMass
->Context
))->InterfaceSubClass
;
695 Status
= UsbBootIsUnitReady (UsbMass
);
696 if (EFI_ERROR (Status
) && (Status
!= EFI_MEDIA_CHANGED
)) {
700 if ((UsbMass
->Pdt
!= USB_PDT_CDROM
) && (CmdSet
== USB_MASS_STORE_SCSI
)) {
702 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
703 // according to Section 4 of USB Mass Storage Specification for Bootability.
704 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
705 // could get the information of Write Protected.
706 // Since not all device support this command, skip if fail.
708 UsbScsiModeSense (UsbMass
);
711 Status
= UsbBootReadCapacity (UsbMass
);
712 if (EFI_ERROR (Status
)) {
713 DEBUG ((EFI_D_ERROR
, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status
));
721 // Detect whether it is necessary to reinstall the Block I/O Protocol.
723 // MediaId may change in RequestSense for MediaChanged
724 // MediaPresent may change in RequestSense for NoMedia
725 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
726 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
728 if ((Media
->MediaId
!= OldMedia
.MediaId
) ||
729 (Media
->MediaPresent
!= OldMedia
.MediaPresent
) ||
730 (Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
731 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
732 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
735 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.
736 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().
738 OldTpl
= EfiGetCurrentTpl ();
739 gBS
->RestoreTPL (TPL_CALLBACK
);
741 gBS
->ReinstallProtocolInterface (
743 &gEfiBlockIoProtocolGuid
,
748 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK
);
749 gBS
->RaiseTPL (OldTpl
);
752 // Update MediaId after reinstalling Block I/O Protocol.
754 if (Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
755 if (Media
->MediaPresent
) {
762 if ((Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
763 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
764 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
774 Read some blocks from the device.
776 @param UsbMass The USB mass storage device to read from
777 @param Lba The start block number
778 @param TotalBlock Total block number to read
779 @param Buffer The buffer to read to
781 @retval EFI_SUCCESS Data are read into the buffer
782 @retval Others Failed to read all the data
787 IN USB_MASS_DEVICE
*UsbMass
,
793 USB_BOOT_READ10_CMD ReadCmd
;
800 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
801 Status
= EFI_SUCCESS
;
803 while (TotalBlock
> 0) {
805 // Split the total blocks into smaller pieces to ease the pressure
806 // on the device. We must split the total block because the READ10
807 // command only has 16 bit transfer length (in the unit of block).
809 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
810 ByteSize
= (UINT32
)Count
* BlockSize
;
813 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
815 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
818 // Fill in the command then execute
820 ZeroMem (&ReadCmd
, sizeof (USB_BOOT_READ10_CMD
));
822 ReadCmd
.OpCode
= USB_BOOT_READ10_OPCODE
;
823 ReadCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
824 WriteUnaligned32 ((UINT32
*) ReadCmd
.Lba
, SwapBytes32 (Lba
));
825 WriteUnaligned16 ((UINT16
*) ReadCmd
.TransferLen
, SwapBytes16 (Count
));
827 Status
= UsbBootExecCmdWithRetry (
830 (UINT8
) sizeof (USB_BOOT_READ10_CMD
),
836 if (EFI_ERROR (Status
)) {
839 DEBUG ((EFI_D_BLKIO
, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba
, Count
));
841 Buffer
+= Count
* BlockSize
;
850 Write some blocks to the device.
852 @param UsbMass The USB mass storage device to write to
853 @param Lba The start block number
854 @param TotalBlock Total block number to write
855 @param Buffer Pointer to the source buffer for the data.
857 @retval EFI_SUCCESS Data are written into the buffer
858 @retval Others Failed to write all the data
863 IN USB_MASS_DEVICE
*UsbMass
,
869 USB_BOOT_WRITE10_CMD WriteCmd
;
876 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
877 Status
= EFI_SUCCESS
;
879 while (TotalBlock
> 0) {
881 // Split the total blocks into smaller pieces to ease the pressure
882 // on the device. We must split the total block because the WRITE10
883 // command only has 16 bit transfer length (in the unit of block).
885 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
886 ByteSize
= (UINT32
)Count
* BlockSize
;
889 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
891 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
894 // Fill in the write10 command block
896 ZeroMem (&WriteCmd
, sizeof (USB_BOOT_WRITE10_CMD
));
898 WriteCmd
.OpCode
= USB_BOOT_WRITE10_OPCODE
;
899 WriteCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
900 WriteUnaligned32 ((UINT32
*) WriteCmd
.Lba
, SwapBytes32 (Lba
));
901 WriteUnaligned16 ((UINT16
*) WriteCmd
.TransferLen
, SwapBytes16 (Count
));
903 Status
= UsbBootExecCmdWithRetry (
906 (UINT8
) sizeof (USB_BOOT_WRITE10_CMD
),
912 if (EFI_ERROR (Status
)) {
915 DEBUG ((EFI_D_BLKIO
, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba
, Count
));
918 Buffer
+= Count
* BlockSize
;
926 Read some blocks from the device by SCSI 16 byte cmd.
928 @param UsbMass The USB mass storage device to read from
929 @param Lba The start block number
930 @param TotalBlock Total block number to read
931 @param Buffer The buffer to read to
933 @retval EFI_SUCCESS Data are read into the buffer
934 @retval Others Failed to read all the data
938 UsbBootReadBlocks16 (
939 IN USB_MASS_DEVICE
*UsbMass
,
952 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
953 Status
= EFI_SUCCESS
;
955 while (TotalBlock
> 0) {
957 // Split the total blocks into smaller pieces.
959 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
960 ByteSize
= (UINT32
)Count
* BlockSize
;
963 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
965 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
968 // Fill in the command then execute
970 ZeroMem (ReadCmd
, sizeof (ReadCmd
));
972 ReadCmd
[0] = EFI_SCSI_OP_READ16
;
973 ReadCmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
974 WriteUnaligned64 ((UINT64
*) &ReadCmd
[2], SwapBytes64 (Lba
));
975 WriteUnaligned32 ((UINT32
*) &ReadCmd
[10], SwapBytes32 (Count
));
977 Status
= UsbBootExecCmdWithRetry (
980 (UINT8
) sizeof (ReadCmd
),
986 if (EFI_ERROR (Status
)) {
989 DEBUG ((EFI_D_BLKIO
, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba
, Count
));
991 Buffer
+= Count
* BlockSize
;
1000 Write some blocks to the device by SCSI 16 byte cmd.
1002 @param UsbMass The USB mass storage device to write to
1003 @param Lba The start block number
1004 @param TotalBlock Total block number to write
1005 @param Buffer Pointer to the source buffer for the data.
1007 @retval EFI_SUCCESS Data are written into the buffer
1008 @retval Others Failed to write all the data
1012 UsbBootWriteBlocks16 (
1013 IN USB_MASS_DEVICE
*UsbMass
,
1015 IN UINTN TotalBlock
,
1026 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
1027 Status
= EFI_SUCCESS
;
1029 while (TotalBlock
> 0) {
1031 // Split the total blocks into smaller pieces.
1033 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
1034 ByteSize
= (UINT32
)Count
* BlockSize
;
1037 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
1039 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
1042 // Fill in the write16 command block
1044 ZeroMem (WriteCmd
, sizeof (WriteCmd
));
1046 WriteCmd
[0] = EFI_SCSI_OP_WRITE16
;
1047 WriteCmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
1048 WriteUnaligned64 ((UINT64
*) &WriteCmd
[2], SwapBytes64 (Lba
));
1049 WriteUnaligned32 ((UINT32
*) &WriteCmd
[10], SwapBytes32 (Count
));
1051 Status
= UsbBootExecCmdWithRetry (
1054 (UINT8
) sizeof (WriteCmd
),
1060 if (EFI_ERROR (Status
)) {
1063 DEBUG ((EFI_D_BLKIO
, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba
, Count
));
1065 Buffer
+= Count
* BlockSize
;
1066 TotalBlock
-= Count
;
1073 Use the USB clear feature control transfer to clear the endpoint stall condition.
1075 @param UsbIo The USB I/O Protocol instance
1076 @param EndpointAddr The endpoint to clear stall for
1078 @retval EFI_SUCCESS The endpoint stall condition is cleared.
1079 @retval Others Failed to clear the endpoint stall condition.
1083 UsbClearEndpointStall (
1084 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1085 IN UINT8 EndpointAddr
1088 EFI_USB_DEVICE_REQUEST Request
;
1093 Request
.RequestType
= 0x02;
1094 Request
.Request
= USB_REQ_CLEAR_FEATURE
;
1095 Request
.Value
= USB_FEATURE_ENDPOINT_HALT
;
1096 Request
.Index
= EndpointAddr
;
1098 Timeout
= USB_BOOT_GENERAL_CMD_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
1100 Status
= UsbIo
->UsbControlTransfer (