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 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 error code (%x) sense key %x/%x/%x\n",
143 USB_BOOT_SENSE_KEY (SenseData
.SenseKey
),
153 Execute the USB mass storage bootability commands.
155 This function executes the USB mass storage bootability commands.
156 If execution failed, retrieve the error by REQUEST_SENSE, then
157 update the device's status, such as ReadyOnly.
159 @param UsbMass The device to issue commands to
160 @param Cmd The command to execute
161 @param CmdLen The length of the command
162 @param DataDir The direction of data transfer
163 @param Data The buffer to hold the data
164 @param DataLen The length of expected data
165 @param Timeout The timeout used to transfer
167 @retval EFI_SUCCESS Command is executed successfully
168 @retval Others Command execution failed.
173 IN USB_MASS_DEVICE
*UsbMass
,
176 IN EFI_USB_DATA_DIRECTION DataDir
,
182 USB_MASS_TRANSPORT
*Transport
;
186 Transport
= UsbMass
->Transport
;
187 Status
= Transport
->ExecCommand (
199 if (Status
== EFI_TIMEOUT
) {
200 DEBUG ((EFI_D_ERROR
, "UsbBootExecCmd: %r to Exec 0x%x Cmd\n", Status
, *(UINT8
*)Cmd
));
205 // If ExecCommand() returns no error and CmdResult is success,
206 // then the commnad transfer is successful.
208 if ((CmdResult
== USB_MASS_CMD_SUCCESS
) && !EFI_ERROR (Status
)) {
213 // If command execution failed, then retrieve error info via sense request.
215 DEBUG ((EFI_D_ERROR
, "UsbBootExecCmd: %r to Exec 0x%x Cmd (Result = %x)\n", Status
, *(UINT8
*)Cmd
, CmdResult
));
216 return UsbBootRequestSense (UsbMass
);
221 Execute the USB mass storage bootability commands with retrial.
223 This function executes USB mass storage bootability commands.
224 If the device isn't ready, wait for it. If the device is ready
225 and error occurs, retry the command again until it exceeds the
226 limit of retrial times.
228 @param UsbMass The device to issue commands to
229 @param Cmd The command to execute
230 @param CmdLen The length of the command
231 @param DataDir The direction of data transfer
232 @param Data The buffer to hold the data
233 @param DataLen The length of expected data
234 @param Timeout The timeout used to transfer
236 @retval EFI_SUCCESS The command is executed successfully.
237 @retval EFI_NO_MEDIA The device media is removed.
238 @retval Others Command execution failed after retrial.
242 UsbBootExecCmdWithRetry (
243 IN USB_MASS_DEVICE
*UsbMass
,
246 IN EFI_USB_DATA_DIRECTION DataDir
,
254 EFI_EVENT TimeoutEvt
;
257 Status
= EFI_SUCCESS
;
258 Status
= gBS
->CreateEvent (
265 if (EFI_ERROR (Status
)) {
269 Status
= gBS
->SetTimer (TimeoutEvt
, TimerRelative
, EFI_TIMER_PERIOD_SECONDS(60));
270 if (EFI_ERROR (Status
)) {
275 // Execute the cmd and retry if it fails.
277 while (EFI_ERROR (gBS
->CheckEvent (TimeoutEvt
))) {
278 Status
= UsbBootExecCmd (
287 if (Status
== EFI_SUCCESS
|| Status
== EFI_NO_MEDIA
) {
291 // If the sense data shows the drive is not ready, we need execute the cmd again.
292 // We limit the upper boundary to 60 seconds.
294 if (Status
== EFI_NOT_READY
) {
298 // If the status is other error, then just retry 5 times.
300 if (Retry
++ >= USB_BOOT_COMMAND_RETRY
) {
306 if (TimeoutEvt
!= NULL
) {
307 gBS
->CloseEvent (TimeoutEvt
);
315 Execute TEST UNIT READY command to check if the device is ready.
317 @param UsbMass The device to test
319 @retval EFI_SUCCESS The device is ready.
320 @retval Others Device not ready.
325 IN USB_MASS_DEVICE
*UsbMass
328 USB_BOOT_TEST_UNIT_READY_CMD TestCmd
;
330 ZeroMem (&TestCmd
, sizeof (USB_BOOT_TEST_UNIT_READY_CMD
));
332 TestCmd
.OpCode
= USB_BOOT_TEST_UNIT_READY_OPCODE
;
333 TestCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
335 return UsbBootExecCmdWithRetry (
338 (UINT8
) sizeof (USB_BOOT_TEST_UNIT_READY_CMD
),
342 USB_BOOT_GENERAL_CMD_TIMEOUT
348 Execute INQUIRY Command to request information regarding parameters of
349 the device be sent to the host computer.
351 @param UsbMass The device to inquire.
353 @retval EFI_SUCCESS INQUIRY Command is executed successfully.
354 @retval Others INQUIRY Command is not executed successfully.
359 IN USB_MASS_DEVICE
*UsbMass
362 USB_BOOT_INQUIRY_CMD InquiryCmd
;
363 EFI_BLOCK_IO_MEDIA
*Media
;
366 Media
= &(UsbMass
->BlockIoMedia
);
368 ZeroMem (&InquiryCmd
, sizeof (USB_BOOT_INQUIRY_CMD
));
369 ZeroMem (&UsbMass
->InquiryData
, sizeof (USB_BOOT_INQUIRY_DATA
));
371 InquiryCmd
.OpCode
= USB_BOOT_INQUIRY_OPCODE
;
372 InquiryCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
373 InquiryCmd
.AllocLen
= (UINT8
) sizeof (USB_BOOT_INQUIRY_DATA
);
375 Status
= UsbBootExecCmdWithRetry (
378 (UINT8
) sizeof (USB_BOOT_INQUIRY_CMD
),
380 &UsbMass
->InquiryData
,
381 sizeof (USB_BOOT_INQUIRY_DATA
),
382 USB_BOOT_GENERAL_CMD_TIMEOUT
384 if (EFI_ERROR (Status
)) {
389 // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit
390 // from the inquiry data.
392 UsbMass
->Pdt
= (UINT8
) (USB_BOOT_PDT (UsbMass
->InquiryData
.Pdt
));
393 Media
->RemovableMedia
= (BOOLEAN
) (USB_BOOT_REMOVABLE (UsbMass
->InquiryData
.Removable
));
395 // Set block size to the default value of 512 Bytes, in case no media is present at first time.
397 Media
->BlockSize
= 0x0200;
403 Execute READ CAPACITY 16 bytes command to request information regarding
404 the capacity of the installed medium of the device.
406 This function executes READ CAPACITY 16 bytes command to get the capacity
407 of the USB mass storage media, including the presence, block size,
408 and last block number.
410 @param UsbMass The device to retireve disk gemotric.
412 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
413 @retval EFI_NOT_READY The returned block size is zero.
414 @retval Other READ CAPACITY 16 bytes command execution failed.
418 UsbBootReadCapacity16 (
419 IN USB_MASS_DEVICE
*UsbMass
422 UINT8 CapacityCmd
[16];
423 EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData
;
424 EFI_BLOCK_IO_MEDIA
*Media
;
428 Media
= &UsbMass
->BlockIoMedia
;
430 Media
->MediaPresent
= FALSE
;
431 Media
->LastBlock
= 0;
432 Media
->BlockSize
= 0;
434 ZeroMem (CapacityCmd
, sizeof (CapacityCmd
));
435 ZeroMem (&CapacityData
, sizeof (CapacityData
));
437 CapacityCmd
[0] = EFI_SCSI_OP_READ_CAPACITY16
;
438 CapacityCmd
[1] = 0x10;
440 // Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO.
442 ZeroMem ((CapacityCmd
+ 2), 8);
444 CapacityCmd
[13] = sizeof (CapacityData
);
446 Status
= UsbBootExecCmdWithRetry (
449 (UINT8
) sizeof (CapacityCmd
),
452 sizeof (CapacityData
),
453 USB_BOOT_GENERAL_CMD_TIMEOUT
455 if (EFI_ERROR (Status
)) {
460 // Get the information on media presence, block size, and last block number
461 // from READ CAPACITY data.
463 Media
->MediaPresent
= TRUE
;
464 Media
->LastBlock
= SwapBytes64 (ReadUnaligned64 ((CONST UINT64
*) &(CapacityData
.LastLba7
)));
466 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) &(CapacityData
.BlockSize3
)));
468 Media
->LowestAlignedLba
= (CapacityData
.LowestAlignLogic2
<< 8) |
469 CapacityData
.LowestAlignLogic1
;
470 Media
->LogicalBlocksPerPhysicalBlock
= (1 << CapacityData
.LogicPerPhysical
);
471 if (BlockSize
== 0) {
475 return UsbBootRequestSense (UsbMass
);
477 Media
->BlockSize
= BlockSize
;
485 Execute READ CAPACITY command to request information regarding
486 the capacity of the installed medium of the device.
488 This function executes READ CAPACITY command to get the capacity
489 of the USB mass storage media, including the presence, block size,
490 and last block number.
492 @param UsbMass The device to retireve disk gemotric.
494 @retval EFI_SUCCESS The disk geometry is successfully retrieved.
495 @retval EFI_NOT_READY The returned block size is zero.
496 @retval Other READ CAPACITY command execution failed.
500 UsbBootReadCapacity (
501 IN USB_MASS_DEVICE
*UsbMass
504 USB_BOOT_READ_CAPACITY_CMD CapacityCmd
;
505 USB_BOOT_READ_CAPACITY_DATA CapacityData
;
506 EFI_BLOCK_IO_MEDIA
*Media
;
510 Media
= &UsbMass
->BlockIoMedia
;
512 ZeroMem (&CapacityCmd
, sizeof (USB_BOOT_READ_CAPACITY_CMD
));
513 ZeroMem (&CapacityData
, sizeof (USB_BOOT_READ_CAPACITY_DATA
));
515 CapacityCmd
.OpCode
= USB_BOOT_READ_CAPACITY_OPCODE
;
516 CapacityCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
518 Status
= UsbBootExecCmdWithRetry (
521 (UINT8
) sizeof (USB_BOOT_READ_CAPACITY_CMD
),
524 sizeof (USB_BOOT_READ_CAPACITY_DATA
),
525 USB_BOOT_GENERAL_CMD_TIMEOUT
527 if (EFI_ERROR (Status
)) {
532 // Get the information on media presence, block size, and last block number
533 // from READ CAPACITY data.
535 Media
->MediaPresent
= TRUE
;
536 Media
->LastBlock
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) CapacityData
.LastLba
));
538 BlockSize
= SwapBytes32 (ReadUnaligned32 ((CONST UINT32
*) CapacityData
.BlockLen
));
539 if (BlockSize
== 0) {
543 return UsbBootRequestSense (UsbMass
);
545 Media
->BlockSize
= BlockSize
;
548 if (Media
->LastBlock
== 0xFFFFFFFF) {
549 Status
= UsbBootReadCapacity16 (UsbMass
);
550 if (!EFI_ERROR (Status
)) {
551 UsbMass
->Cdb16Byte
= TRUE
;
559 Retrieves SCSI mode sense information via MODE SENSE(6) command.
561 @param UsbMass The device whose sense data is requested.
563 @retval EFI_SUCCESS SCSI mode sense information retrieved successfully.
564 @retval Other Command execution failed.
569 IN USB_MASS_DEVICE
*UsbMass
573 USB_SCSI_MODE_SENSE6_CMD ModeSenseCmd
;
574 USB_SCSI_MODE_SENSE6_PARA_HEADER ModeParaHeader
;
575 EFI_BLOCK_IO_MEDIA
*Media
;
577 Media
= &UsbMass
->BlockIoMedia
;
579 ZeroMem (&ModeSenseCmd
, sizeof (USB_SCSI_MODE_SENSE6_CMD
));
580 ZeroMem (&ModeParaHeader
, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
));
583 // MODE SENSE(6) command is defined in Section 8.2.10 of SCSI-2 Spec
585 ModeSenseCmd
.OpCode
= USB_SCSI_MODE_SENSE6_OPCODE
;
586 ModeSenseCmd
.Lun
= (UINT8
) USB_BOOT_LUN (UsbMass
->Lun
);
587 ModeSenseCmd
.PageCode
= 0x3F;
588 ModeSenseCmd
.AllocateLen
= (UINT8
) sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
);
590 Status
= UsbBootExecCmdWithRetry (
593 (UINT8
) sizeof (USB_SCSI_MODE_SENSE6_CMD
),
596 sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER
),
597 USB_BOOT_GENERAL_CMD_TIMEOUT
601 // Format of device-specific parameter byte of the mode parameter header is defined in
602 // Section 8.2.10 of SCSI-2 Spec.
603 // BIT7 of this byte is indicates whether the medium is write protected.
605 if (!EFI_ERROR (Status
)) {
606 Media
->ReadOnly
= (BOOLEAN
) ((ModeParaHeader
.DevicePara
& BIT7
) != 0);
614 Get the parameters for the USB mass storage media.
616 This function get the parameters for the USB mass storage media,
617 It is used both to initialize the media during the Start() phase
618 of Driver Binding Protocol and to re-initialize it when the media is
619 changed. Althought the RemoveableMedia is unlikely to change,
620 it is also included here.
622 @param UsbMass The device to retrieve disk gemotric.
624 @retval EFI_SUCCESS The disk gemotric is successfully retrieved.
625 @retval Other Failed to get the parameters.
630 IN USB_MASS_DEVICE
*UsbMass
633 EFI_BLOCK_IO_MEDIA
*Media
;
636 Media
= &(UsbMass
->BlockIoMedia
);
638 Status
= UsbBootInquiry (UsbMass
);
639 if (EFI_ERROR (Status
)) {
640 DEBUG ((EFI_D_ERROR
, "UsbBootGetParams: UsbBootInquiry (%r)\n", Status
));
645 // According to USB Mass Storage Specification for Bootability, only following
646 // 4 Peripheral Device Types are in spec.
648 if ((UsbMass
->Pdt
!= USB_PDT_DIRECT_ACCESS
) &&
649 (UsbMass
->Pdt
!= USB_PDT_CDROM
) &&
650 (UsbMass
->Pdt
!= USB_PDT_OPTICAL
) &&
651 (UsbMass
->Pdt
!= USB_PDT_SIMPLE_DIRECT
)) {
652 DEBUG ((EFI_D_ERROR
, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass
->Pdt
));
653 return EFI_UNSUPPORTED
;
657 // Don't use the Removable bit in inquiry data to test whether the media
658 // is removable because many flash disks wrongly set this bit.
660 if ((UsbMass
->Pdt
== USB_PDT_CDROM
) || (UsbMass
->Pdt
== USB_PDT_OPTICAL
)) {
662 // CD-Rom device and Non-CD optical device
664 UsbMass
->OpticalStorage
= TRUE
;
666 // Default value 2048 Bytes, in case no media present at first time
668 Media
->BlockSize
= 0x0800;
671 Status
= UsbBootDetectMedia (UsbMass
);
678 Detect whether the removable media is present and whether it has changed.
680 @param UsbMass The device to check.
682 @retval EFI_SUCCESS The media status is successfully checked.
683 @retval Other Failed to detect media.
688 IN USB_MASS_DEVICE
*UsbMass
691 EFI_BLOCK_IO_MEDIA OldMedia
;
692 EFI_BLOCK_IO_MEDIA
*Media
;
696 Media
= &UsbMass
->BlockIoMedia
;
698 CopyMem (&OldMedia
, &(UsbMass
->BlockIoMedia
), sizeof (EFI_BLOCK_IO_MEDIA
));
700 CmdSet
= ((EFI_USB_INTERFACE_DESCRIPTOR
*) (UsbMass
->Context
))->InterfaceSubClass
;
702 Status
= UsbBootIsUnitReady (UsbMass
);
703 if (EFI_ERROR (Status
)) {
704 DEBUG ((EFI_D_ERROR
, "UsbBootDetectMedia: UsbBootIsUnitReady (%r)\n", Status
));
709 // EFI_SUCCESS: all good.
710 // EFI_NO_MEDIA: media is not present.
712 // For either EFI_NO_MEDIA, or HW error, skip to get WriteProtected and capacity information.
714 if (!EFI_ERROR (Status
)) {
715 if ((UsbMass
->Pdt
!= USB_PDT_CDROM
) && (CmdSet
== USB_MASS_STORE_SCSI
)) {
717 // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E,
718 // according to Section 4 of USB Mass Storage Specification for Bootability.
719 // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI
720 // could get the information of Write Protected.
721 // Since not all device support this command, skip if fail.
723 UsbScsiModeSense (UsbMass
);
726 Status
= UsbBootReadCapacity (UsbMass
);
727 if (EFI_ERROR (Status
)) {
728 DEBUG ((EFI_D_ERROR
, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status
));
732 if (EFI_ERROR (Status
) && Status
!= EFI_NO_MEDIA
) {
734 // For NoMedia, BlockIo is still needed.
740 // Detect whether it is necessary to reinstall the Block I/O Protocol.
742 // MediaId may change in RequestSense for MediaChanged
743 // MediaPresent may change in RequestSense for NoMedia
744 // MediaReadOnly may change in RequestSense for WriteProtected or MediaChanged
745 // MediaPresent/BlockSize/LastBlock may change in ReadCapacity
747 if ((Media
->MediaId
!= OldMedia
.MediaId
) ||
748 (Media
->MediaPresent
!= OldMedia
.MediaPresent
) ||
749 (Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
750 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
751 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
754 // This function is called from:
755 // Block I/O Protocol APIs, which run at TPL_CALLBACK.
756 // DriverBindingStart(), which raises to TPL_CALLBACK.
757 ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK
);
760 // When it is called from DriverBindingStart(), below reinstall fails.
761 // So ignore the return status check.
763 gBS
->ReinstallProtocolInterface (
765 &gEfiBlockIoProtocolGuid
,
771 // Reset MediaId after reinstalling Block I/O Protocol.
773 if (Media
->MediaPresent
!= OldMedia
.MediaPresent
) {
774 if (Media
->MediaPresent
) {
781 if ((Media
->ReadOnly
!= OldMedia
.ReadOnly
) ||
782 (Media
->BlockSize
!= OldMedia
.BlockSize
) ||
783 (Media
->LastBlock
!= OldMedia
.LastBlock
)) {
787 Status
= Media
->MediaPresent
? EFI_MEDIA_CHANGED
: EFI_NO_MEDIA
;
795 Read some blocks from the device.
797 @param UsbMass The USB mass storage device to read from
798 @param Lba The start block number
799 @param TotalBlock Total block number to read
800 @param Buffer The buffer to read to
802 @retval EFI_SUCCESS Data are read into the buffer
803 @retval Others Failed to read all the data
808 IN USB_MASS_DEVICE
*UsbMass
,
814 USB_BOOT_READ10_CMD ReadCmd
;
822 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
823 CountMax
= (UINT16
)(USB_BOOT_MAX_CARRY_SIZE
/ BlockSize
);
824 Status
= EFI_SUCCESS
;
826 while (TotalBlock
> 0) {
828 // Split the total blocks into smaller pieces to ease the pressure
829 // on the device. We must split the total block because the READ10
830 // command only has 16 bit transfer length (in the unit of block).
832 Count
= (UINT16
)((TotalBlock
< CountMax
) ? TotalBlock
: CountMax
);
833 ByteSize
= (UINT32
)Count
* BlockSize
;
836 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
838 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
841 // Fill in the command then execute
843 ZeroMem (&ReadCmd
, sizeof (USB_BOOT_READ10_CMD
));
845 ReadCmd
.OpCode
= USB_BOOT_READ10_OPCODE
;
846 ReadCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
847 WriteUnaligned32 ((UINT32
*) ReadCmd
.Lba
, SwapBytes32 (Lba
));
848 WriteUnaligned16 ((UINT16
*) ReadCmd
.TransferLen
, SwapBytes16 (Count
));
850 Status
= UsbBootExecCmdWithRetry (
853 (UINT8
) sizeof (USB_BOOT_READ10_CMD
),
859 if (EFI_ERROR (Status
)) {
862 DEBUG ((EFI_D_BLKIO
, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba
, Count
));
864 Buffer
+= Count
* BlockSize
;
873 Write some blocks to the device.
875 @param UsbMass The USB mass storage device to write to
876 @param Lba The start block number
877 @param TotalBlock Total block number to write
878 @param Buffer Pointer to the source buffer for the data.
880 @retval EFI_SUCCESS Data are written into the buffer
881 @retval Others Failed to write all the data
886 IN USB_MASS_DEVICE
*UsbMass
,
892 USB_BOOT_WRITE10_CMD WriteCmd
;
900 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
901 CountMax
= (UINT16
)(USB_BOOT_MAX_CARRY_SIZE
/ BlockSize
);
902 Status
= EFI_SUCCESS
;
904 while (TotalBlock
> 0) {
906 // Split the total blocks into smaller pieces to ease the pressure
907 // on the device. We must split the total block because the WRITE10
908 // command only has 16 bit transfer length (in the unit of block).
910 Count
= (UINT16
)((TotalBlock
< CountMax
) ? TotalBlock
: CountMax
);
911 ByteSize
= (UINT32
)Count
* BlockSize
;
914 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
916 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
919 // Fill in the write10 command block
921 ZeroMem (&WriteCmd
, sizeof (USB_BOOT_WRITE10_CMD
));
923 WriteCmd
.OpCode
= USB_BOOT_WRITE10_OPCODE
;
924 WriteCmd
.Lun
= (UINT8
) (USB_BOOT_LUN (UsbMass
->Lun
));
925 WriteUnaligned32 ((UINT32
*) WriteCmd
.Lba
, SwapBytes32 (Lba
));
926 WriteUnaligned16 ((UINT16
*) WriteCmd
.TransferLen
, SwapBytes16 (Count
));
928 Status
= UsbBootExecCmdWithRetry (
931 (UINT8
) sizeof (USB_BOOT_WRITE10_CMD
),
937 if (EFI_ERROR (Status
)) {
940 DEBUG ((EFI_D_BLKIO
, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba
, Count
));
943 Buffer
+= Count
* BlockSize
;
951 Read some blocks from the device by SCSI 16 byte cmd.
953 @param UsbMass The USB mass storage device to read from
954 @param Lba The start block number
955 @param TotalBlock Total block number to read
956 @param Buffer The buffer to read to
958 @retval EFI_SUCCESS Data are read into the buffer
959 @retval Others Failed to read all the data
963 UsbBootReadBlocks16 (
964 IN USB_MASS_DEVICE
*UsbMass
,
978 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
979 CountMax
= (UINT16
)(USB_BOOT_MAX_CARRY_SIZE
/ BlockSize
);
980 Status
= EFI_SUCCESS
;
982 while (TotalBlock
> 0) {
984 // Split the total blocks into smaller pieces.
986 Count
= (UINT16
)((TotalBlock
< CountMax
) ? TotalBlock
: CountMax
);
987 ByteSize
= (UINT32
)Count
* BlockSize
;
990 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
992 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
995 // Fill in the command then execute
997 ZeroMem (ReadCmd
, sizeof (ReadCmd
));
999 ReadCmd
[0] = EFI_SCSI_OP_READ16
;
1000 ReadCmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
1001 WriteUnaligned64 ((UINT64
*) &ReadCmd
[2], SwapBytes64 (Lba
));
1002 WriteUnaligned32 ((UINT32
*) &ReadCmd
[10], SwapBytes32 (Count
));
1004 Status
= UsbBootExecCmdWithRetry (
1007 (UINT8
) sizeof (ReadCmd
),
1013 if (EFI_ERROR (Status
)) {
1016 DEBUG ((EFI_D_BLKIO
, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba
, Count
));
1018 Buffer
+= Count
* BlockSize
;
1019 TotalBlock
-= Count
;
1027 Write some blocks to the device by SCSI 16 byte cmd.
1029 @param UsbMass The USB mass storage device to write to
1030 @param Lba The start block number
1031 @param TotalBlock Total block number to write
1032 @param Buffer Pointer to the source buffer for the data.
1034 @retval EFI_SUCCESS Data are written into the buffer
1035 @retval Others Failed to write all the data
1039 UsbBootWriteBlocks16 (
1040 IN USB_MASS_DEVICE
*UsbMass
,
1042 IN UINTN TotalBlock
,
1054 BlockSize
= UsbMass
->BlockIoMedia
.BlockSize
;
1055 CountMax
= (UINT16
)(USB_BOOT_MAX_CARRY_SIZE
/ BlockSize
);
1056 Status
= EFI_SUCCESS
;
1058 while (TotalBlock
> 0) {
1060 // Split the total blocks into smaller pieces.
1062 Count
= (UINT16
)((TotalBlock
< CountMax
) ? TotalBlock
: CountMax
);
1063 ByteSize
= (UINT32
)Count
* BlockSize
;
1066 // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]
1068 Timeout
= (UINT32
) USB_BOOT_GENERAL_CMD_TIMEOUT
;
1071 // Fill in the write16 command block
1073 ZeroMem (WriteCmd
, sizeof (WriteCmd
));
1075 WriteCmd
[0] = EFI_SCSI_OP_WRITE16
;
1076 WriteCmd
[1] = (UINT8
) ((USB_BOOT_LUN (UsbMass
->Lun
) & 0xE0));
1077 WriteUnaligned64 ((UINT64
*) &WriteCmd
[2], SwapBytes64 (Lba
));
1078 WriteUnaligned32 ((UINT32
*) &WriteCmd
[10], SwapBytes32 (Count
));
1080 Status
= UsbBootExecCmdWithRetry (
1083 (UINT8
) sizeof (WriteCmd
),
1089 if (EFI_ERROR (Status
)) {
1092 DEBUG ((EFI_D_BLKIO
, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba
, Count
));
1094 Buffer
+= Count
* BlockSize
;
1095 TotalBlock
-= Count
;
1102 Use the USB clear feature control transfer to clear the endpoint stall condition.
1104 @param UsbIo The USB I/O Protocol instance
1105 @param EndpointAddr The endpoint to clear stall for
1107 @retval EFI_SUCCESS The endpoint stall condition is cleared.
1108 @retval Others Failed to clear the endpoint stall condition.
1112 UsbClearEndpointStall (
1113 IN EFI_USB_IO_PROTOCOL
*UsbIo
,
1114 IN UINT8 EndpointAddr
1117 EFI_USB_DEVICE_REQUEST Request
;
1122 Request
.RequestType
= 0x02;
1123 Request
.Request
= USB_REQ_CLEAR_FEATURE
;
1124 Request
.Value
= USB_FEATURE_ENDPOINT_HALT
;
1125 Request
.Index
= EndpointAddr
;
1127 Timeout
= USB_BOOT_GENERAL_CMD_TIMEOUT
/ USB_MASS_1_MILLISECOND
;
1129 Status
= UsbIo
->UsbControlTransfer (