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
;
126 case USB_BOOT_SENSE_DATA_PROTECT
:
127 Status
= EFI_WRITE_PROTECTED
;
128 Media
->ReadOnly
= TRUE
;
132 Status
= EFI_DEVICE_ERROR
;
136 DEBUG ((EFI_D_INFO
, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n",
138 USB_BOOT_SENSE_KEY (SenseData
.SenseKey
),
148 Execute the USB mass storage bootability commands.
150 This function executes the USB mass storage bootability commands.
151 If execution failed, retrieve the error by REQUEST_SENSE, then
152 update the device's status, such as ReadyOnly.
154 @param UsbMass The device to issue commands to
155 @param Cmd The command to execute
156 @param CmdLen The length of the command
157 @param DataDir The direction of data transfer
158 @param Data The buffer to hold the data
159 @param DataLen The length of expected data
160 @param Timeout The timeout used to transfer
162 @retval EFI_SUCCESS Command is executed successfully
163 @retval Others Command execution failed.
168 IN USB_MASS_DEVICE
*UsbMass
,
171 IN EFI_USB_DATA_DIRECTION DataDir
,
177 USB_MASS_TRANSPORT
*Transport
;
181 Transport
= UsbMass
->Transport
;
182 Status
= Transport
->ExecCommand (
194 if (Status
== EFI_TIMEOUT
) {
195 DEBUG ((EFI_D_ERROR
, "UsbBootExecCmd: Timeout to Exec 0x%x Cmd\n", *(UINT8
*)Cmd
));
200 // If ExecCommand() returns no error and CmdResult is success,
201 // then the commnad transfer is successful.
203 if ((CmdResult
== USB_MASS_CMD_SUCCESS
) && !EFI_ERROR (Status
)) {
208 // If command execution failed, then retrieve error info via sense request.
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_MEDIA_CHANGED The device media has been changed.
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_MEDIA_CHANGED
|| 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. Althought 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
;
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
) && (Status
!= EFI_MEDIA_CHANGED
)) {
702 if ((UsbMass
->Pdt
!= USB_PDT_CDROM
) && (CmdSet
== USB_MASS_STORE_SCSI
)) {
704 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
705 // according to Section 4 of USB Mass Storage Specification for Bootability.
706 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
707 // could get the information of Write Protected.
708 // Since not all device support this command, skip if fail.
710 UsbScsiModeSense (UsbMass
);
713 Status
= UsbBootReadCapacity (UsbMass
);
714 if (EFI_ERROR (Status
)) {
715 DEBUG ((EFI_D_ERROR
, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status
));
723 // Detect whether it is necessary to reinstall the Block I/O Protocol.
725 // MediaId may change in RequestSense for MediaChanged
726 // MediaPresent may change in RequestSense for NoMedia
727 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
728 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
730 if ((Media
->MediaId
!= OldMedia
.MediaId
) ||
731 (Media
->MediaPresent
!= OldMedia
.MediaPresent
) ||
732 (Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
733 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
734 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
737 // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY.
738 // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface().
740 OldTpl
= EfiGetCurrentTpl ();
741 gBS
->RestoreTPL (TPL_CALLBACK
);
743 gBS
->ReinstallProtocolInterface (
745 &gEfiBlockIoProtocolGuid
,
750 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK
);
751 gBS
->RaiseTPL (OldTpl
);
754 // Update MediaId after reinstalling Block I/O Protocol.
756 if (Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
757 if (Media
->MediaPresent
) {
764 if ((Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
765 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
766 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
776 Read some blocks from the device.
778 @param UsbMass The USB mass storage device to read from
779 @param Lba The start block number
780 @param TotalBlock Total block number to read
781 @param Buffer The buffer to read to
783 @retval EFI_SUCCESS Data are read into the buffer
784 @retval Others Failed to read all the data
789 IN USB_MASS_DEVICE
*UsbMass
,
795 USB_BOOT_READ10_CMD ReadCmd
;
802 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
803 Status
= EFI_SUCCESS
;
805 while (TotalBlock
> 0) {
807 // Split the total blocks into smaller pieces to ease the pressure
808 // on the device. We must split the total block because the READ10
809 // command only has 16 bit transfer length (in the unit of block).
811 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
812 ByteSize
= (UINT32
)Count
* BlockSize
;
815 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
817 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
820 // Fill in the command then execute
822 ZeroMem (&ReadCmd
, sizeof (USB_BOOT_READ10_CMD
));
824 ReadCmd
.OpCode
= USB_BOOT_READ10_OPCODE
;
825 ReadCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
826 WriteUnaligned32 ((UINT32
*) ReadCmd
.Lba
, SwapBytes32 (Lba
));
827 WriteUnaligned16 ((UINT16
*) ReadCmd
.TransferLen
, SwapBytes16 (Count
));
829 Status
= UsbBootExecCmdWithRetry (
832 (UINT8
) sizeof (USB_BOOT_READ10_CMD
),
838 if (EFI_ERROR (Status
)) {
841 DEBUG ((EFI_D_BLKIO
, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba
, Count
));
843 Buffer
+= Count
* BlockSize
;
852 Write some blocks to the device.
854 @param UsbMass The USB mass storage device to write to
855 @param Lba The start block number
856 @param TotalBlock Total block number to write
857 @param Buffer Pointer to the source buffer for the data.
859 @retval EFI_SUCCESS Data are written into the buffer
860 @retval Others Failed to write all the data
865 IN USB_MASS_DEVICE
*UsbMass
,
871 USB_BOOT_WRITE10_CMD WriteCmd
;
878 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
879 Status
= EFI_SUCCESS
;
881 while (TotalBlock
> 0) {
883 // Split the total blocks into smaller pieces to ease the pressure
884 // on the device. We must split the total block because the WRITE10
885 // command only has 16 bit transfer length (in the unit of block).
887 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
888 ByteSize
= (UINT32
)Count
* BlockSize
;
891 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
893 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
896 // Fill in the write10 command block
898 ZeroMem (&WriteCmd
, sizeof (USB_BOOT_WRITE10_CMD
));
900 WriteCmd
.OpCode
= USB_BOOT_WRITE10_OPCODE
;
901 WriteCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
902 WriteUnaligned32 ((UINT32
*) WriteCmd
.Lba
, SwapBytes32 (Lba
));
903 WriteUnaligned16 ((UINT16
*) WriteCmd
.TransferLen
, SwapBytes16 (Count
));
905 Status
= UsbBootExecCmdWithRetry (
908 (UINT8
) sizeof (USB_BOOT_WRITE10_CMD
),
914 if (EFI_ERROR (Status
)) {
917 DEBUG ((EFI_D_BLKIO
, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba
, Count
));
920 Buffer
+= Count
* BlockSize
;
928 Read some blocks from the device by SCSI 16 byte cmd.
930 @param UsbMass The USB mass storage device to read from
931 @param Lba The start block number
932 @param TotalBlock Total block number to read
933 @param Buffer The buffer to read to
935 @retval EFI_SUCCESS Data are read into the buffer
936 @retval Others Failed to read all the data
940 UsbBootReadBlocks16 (
941 IN USB_MASS_DEVICE
*UsbMass
,
954 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
955 Status
= EFI_SUCCESS
;
957 while (TotalBlock
> 0) {
959 // Split the total blocks into smaller pieces.
961 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
962 ByteSize
= (UINT32
)Count
* BlockSize
;
965 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
967 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
970 // Fill in the command then execute
972 ZeroMem (ReadCmd
, sizeof (ReadCmd
));
974 ReadCmd
[0] = EFI_SCSI_OP_READ16
;
975 ReadCmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
976 WriteUnaligned64 ((UINT64
*) &ReadCmd
[2], SwapBytes64 (Lba
));
977 WriteUnaligned32 ((UINT32
*) &ReadCmd
[10], SwapBytes32 (Count
));
979 Status
= UsbBootExecCmdWithRetry (
982 (UINT8
) sizeof (ReadCmd
),
988 if (EFI_ERROR (Status
)) {
991 DEBUG ((EFI_D_BLKIO
, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba
, Count
));
993 Buffer
+= Count
* BlockSize
;
1002 Write some blocks to the device by SCSI 16 byte cmd.
1004 @param UsbMass The USB mass storage device to write to
1005 @param Lba The start block number
1006 @param TotalBlock Total block number to write
1007 @param Buffer Pointer to the source buffer for the data.
1009 @retval EFI_SUCCESS Data are written into the buffer
1010 @retval Others Failed to write all the data
1014 UsbBootWriteBlocks16 (
1015 IN USB_MASS_DEVICE
*UsbMass
,
1017 IN UINTN TotalBlock
,
1028 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
1029 Status
= EFI_SUCCESS
;
1031 while (TotalBlock
> 0) {
1033 // Split the total blocks into smaller pieces.
1035 Count
= (UINT16
)((TotalBlock
< USB_BOOT_IO_BLOCKS
) ? TotalBlock
: USB_BOOT_IO_BLOCKS
);
1036 ByteSize
= (UINT32
)Count
* BlockSize
;
1039 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
1041 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
1044 // Fill in the write16 command block
1046 ZeroMem (WriteCmd
, sizeof (WriteCmd
));
1048 WriteCmd
[0] = EFI_SCSI_OP_WRITE16
;
1049 WriteCmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
1050 WriteUnaligned64 ((UINT64
*) &WriteCmd
[2], SwapBytes64 (Lba
));
1051 WriteUnaligned32 ((UINT32
*) &WriteCmd
[10], SwapBytes32 (Count
));
1053 Status
= UsbBootExecCmdWithRetry (
1056 (UINT8
) sizeof (WriteCmd
),
1062 if (EFI_ERROR (Status
)) {
1065 DEBUG ((EFI_D_BLKIO
, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba
, Count
));
1067 Buffer
+= Count
* BlockSize
;
1068 TotalBlock
-= Count
;
1075 Use the USB clear feature control transfer to clear the endpoint stall condition.
1077 @param UsbIo The USB I/O Protocol instance
1078 @param EndpointAddr The endpoint to clear stall for
1080 @retval EFI_SUCCESS The endpoint stall condition is cleared.
1081 @retval Others Failed to clear the endpoint stall condition.
1085 UsbClearEndpointStall (
1086 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1087 IN UINT8 EndpointAddr
1090 EFI_USB_DEVICE_REQUEST Request
;
1095 Request
.RequestType
= 0x02;
1096 Request
.Request
= USB_REQ_CLEAR_FEATURE
;
1097 Request
.Value
= USB_FEATURE_ENDPOINT_HALT
;
1098 Request
.Index
= EndpointAddr
;
1100 Timeout
= USB_BOOT_GENERAL_CMD_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
1102 Status
= UsbIo
->UsbControlTransfer (