X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FBus%2FUsb%2FUsbMassStorageDxe%2FUsbMassBoot.c;h=dd5950c54c291ba09ad11651ed5175ad73f650c2;hp=1b200bb4ec9f89b20857de2811853ba1de066f98;hb=d1102dba7210b95e41d06c2338a22ba6af248645;hpb=1ccdbf2a3e61fe9494fcd39432107ba0eb74f584 diff --git a/MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c b/MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c index 1b200bb4ec..dd5950c54c 100644 --- a/MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c +++ b/MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c @@ -2,8 +2,8 @@ Implementation of the command set of USB Mass Storage Specification for Bootability, Revision 1.0. -Copyright (c) 2007 - 2010, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -13,14 +13,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ -#include "UsbMassImpl.h" +#include "UsbMass.h" /** Execute REQUEST SENSE Command to retrieve sense data from device. @param UsbMass The device whose sense data is requested. - @retval EFI_SUCCESS The command is excuted successfully. + @retval EFI_SUCCESS The command is executed successfully. @retval EFI_DEVICE_ERROR Failed to request sense. @retval EFI_NO_RESPONSE The device media doesn't response this request. @retval EFI_INVALID_PARAMETER The command has some invalid parameters. @@ -50,7 +50,7 @@ UsbBootRequestSense ( SenseCmd.OpCode = USB_BOOT_REQUEST_SENSE_OPCODE; SenseCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); - SenseCmd.AllocLen = sizeof (USB_BOOT_REQUEST_SENSE_DATA); + SenseCmd.AllocLen = (UINT8) sizeof (USB_BOOT_REQUEST_SENSE_DATA); Status = Transport->ExecCommand ( UsbMass->Context, @@ -80,7 +80,14 @@ UsbBootRequestSense ( switch (USB_BOOT_SENSE_KEY (SenseData.SenseKey)) { case USB_BOOT_SENSE_NO_SENSE: - Status = EFI_NO_RESPONSE; + if (SenseData.Asc == USB_BOOT_ASC_NO_ADDITIONAL_SENSE_INFORMATION) { + // + // It is not an error if a device does not have additional sense information + // + Status = EFI_SUCCESS; + } else { + Status = EFI_NO_RESPONSE; + } break; case USB_BOOT_SENSE_RECOVERED: @@ -113,6 +120,10 @@ UsbBootRequestSense ( Status = EFI_MEDIA_CHANGED; Media->ReadOnly = FALSE; Media->MediaId++; + } else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) { + Status = EFI_NOT_READY; + } else if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) { + Status = EFI_NOT_READY; } break; @@ -126,8 +137,9 @@ UsbBootRequestSense ( break; } - DEBUG ((EFI_D_INFO, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n", + DEBUG ((EFI_D_INFO, "UsbBootRequestSense: (%r) with error code (%x) sense key %x/%x/%x\n", Status, + SenseData.ErrorCode, USB_BOOT_SENSE_KEY (SenseData.SenseKey), SenseData.Asc, SenseData.Ascq @@ -152,7 +164,7 @@ UsbBootRequestSense ( @param DataLen The length of expected data @param Timeout The timeout used to transfer - @retval EFI_SUCCESS Command is excuted successfully + @retval EFI_SUCCESS Command is executed successfully @retval Others Command execution failed. **/ @@ -183,6 +195,12 @@ UsbBootExecCmd ( Timeout, &CmdResult ); + + if (Status == EFI_TIMEOUT) { + DEBUG ((EFI_D_ERROR, "UsbBootExecCmd: %r to Exec 0x%x Cmd\n", Status, *(UINT8 *)Cmd)); + return EFI_TIMEOUT; + } + // // If ExecCommand() returns no error and CmdResult is success, // then the commnad transfer is successful. @@ -191,12 +209,10 @@ UsbBootExecCmd ( return EFI_SUCCESS; } - DEBUG ((EFI_D_INFO, "UsbBootExecCmd: Fail to Exec 0x%x Cmd /w %r\n", - *(UINT8 *)Cmd ,Status)); - // // If command execution failed, then retrieve error info via sense request. // + DEBUG ((EFI_D_ERROR, "UsbBootExecCmd: %r to Exec 0x%x Cmd (Result = %x)\n", Status, *(UINT8 *)Cmd, CmdResult)); return UsbBootRequestSense (UsbMass); } @@ -208,7 +224,7 @@ UsbBootExecCmd ( If the device isn't ready, wait for it. If the device is ready and error occurs, retry the command again until it exceeds the limit of retrial times. - + @param UsbMass The device to issue commands to @param Cmd The command to execute @param CmdLen The length of the command @@ -218,7 +234,7 @@ UsbBootExecCmd ( @param Timeout The timeout used to transfer @retval EFI_SUCCESS The command is executed successfully. - @retval EFI_MEDIA_CHANGED The device media has been changed. + @retval EFI_NO_MEDIA The device media is removed. @retval Others Command execution failed after retrial. **/ @@ -235,11 +251,30 @@ UsbBootExecCmdWithRetry ( { EFI_STATUS Status; UINTN Retry; - UINT8 Terminate; + EFI_EVENT TimeoutEvt; + + Retry = 0; + Status = EFI_SUCCESS; + Status = gBS->CreateEvent ( + EVT_TIMER, + TPL_CALLBACK, + NULL, + NULL, + &TimeoutEvt + ); + if (EFI_ERROR (Status)) { + return Status; + } - Status = EFI_SUCCESS; + Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS(60)); + if (EFI_ERROR (Status)) { + goto EXIT; + } - for (Retry = 0, Terminate = 0; Retry < USB_BOOT_COMMAND_RETRY; Retry++) { + // + // Execute the cmd and retry if it fails. + // + while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) { Status = UsbBootExecCmd ( UsbMass, Cmd, @@ -249,16 +284,27 @@ UsbBootExecCmdWithRetry ( DataLen, Timeout ); - if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED) { + if (Status == EFI_SUCCESS || Status == EFI_NO_MEDIA) { break; } // - // If the device isn't ready, just wait for it without limit on retrial times. + // If the sense data shows the drive is not ready, we need execute the cmd again. + // We limit the upper boundary to 60 seconds. // - if (Status == EFI_NOT_READY && Terminate < 3) { - Retry = 0; - Terminate++; + if (Status == EFI_NOT_READY) { + continue; } + // + // If the status is other error, then just retry 5 times. + // + if (Retry++ >= USB_BOOT_COMMAND_RETRY) { + break; + } + } + +EXIT: + if (TimeoutEvt != NULL) { + gBS->CloseEvent (TimeoutEvt); } return Status; @@ -289,7 +335,7 @@ UsbBootIsUnitReady ( return UsbBootExecCmdWithRetry ( UsbMass, &TestCmd, - sizeof (USB_BOOT_TEST_UNIT_READY_CMD), + (UINT8) sizeof (USB_BOOT_TEST_UNIT_READY_CMD), EfiUsbNoData, NULL, 0, @@ -314,25 +360,24 @@ UsbBootInquiry ( ) { USB_BOOT_INQUIRY_CMD InquiryCmd; - USB_BOOT_INQUIRY_DATA InquiryData; EFI_BLOCK_IO_MEDIA *Media; EFI_STATUS Status; Media = &(UsbMass->BlockIoMedia); ZeroMem (&InquiryCmd, sizeof (USB_BOOT_INQUIRY_CMD)); - ZeroMem (&InquiryData, sizeof (USB_BOOT_INQUIRY_DATA)); + ZeroMem (&UsbMass->InquiryData, sizeof (USB_BOOT_INQUIRY_DATA)); InquiryCmd.OpCode = USB_BOOT_INQUIRY_OPCODE; InquiryCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); - InquiryCmd.AllocLen = sizeof (InquiryData); + InquiryCmd.AllocLen = (UINT8) sizeof (USB_BOOT_INQUIRY_DATA); Status = UsbBootExecCmdWithRetry ( UsbMass, &InquiryCmd, - sizeof (USB_BOOT_INQUIRY_CMD), + (UINT8) sizeof (USB_BOOT_INQUIRY_CMD), EfiUsbDataIn, - &InquiryData, + &UsbMass->InquiryData, sizeof (USB_BOOT_INQUIRY_DATA), USB_BOOT_GENERAL_CMD_TIMEOUT ); @@ -344,8 +389,8 @@ UsbBootInquiry ( // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit // from the inquiry data. // - UsbMass->Pdt = (UINT8) (USB_BOOT_PDT (InquiryData.Pdt)); - Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (InquiryData.Removable)); + UsbMass->Pdt = (UINT8) (USB_BOOT_PDT (UsbMass->InquiryData.Pdt)); + Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (UsbMass->InquiryData.Removable)); // // Set block size to the default value of 512 Bytes, in case no media is present at first time. // @@ -354,6 +399,87 @@ UsbBootInquiry ( return Status; } +/** + Execute READ CAPACITY 16 bytes command to request information regarding + the capacity of the installed medium of the device. + + This function executes READ CAPACITY 16 bytes command to get the capacity + of the USB mass storage media, including the presence, block size, + and last block number. + + @param UsbMass The device to retireve disk gemotric. + + @retval EFI_SUCCESS The disk geometry is successfully retrieved. + @retval EFI_NOT_READY The returned block size is zero. + @retval Other READ CAPACITY 16 bytes command execution failed. + +**/ +EFI_STATUS +UsbBootReadCapacity16 ( + IN USB_MASS_DEVICE *UsbMass + ) +{ + UINT8 CapacityCmd[16]; + EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData; + EFI_BLOCK_IO_MEDIA *Media; + EFI_STATUS Status; + UINT32 BlockSize; + + Media = &UsbMass->BlockIoMedia; + + Media->MediaPresent = FALSE; + Media->LastBlock = 0; + Media->BlockSize = 0; + + ZeroMem (CapacityCmd, sizeof (CapacityCmd)); + ZeroMem (&CapacityData, sizeof (CapacityData)); + + CapacityCmd[0] = EFI_SCSI_OP_READ_CAPACITY16; + CapacityCmd[1] = 0x10; + // + // Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO. + // + ZeroMem ((CapacityCmd + 2), 8); + + CapacityCmd[13] = sizeof (CapacityData); + + Status = UsbBootExecCmdWithRetry ( + UsbMass, + CapacityCmd, + (UINT8) sizeof (CapacityCmd), + EfiUsbDataIn, + &CapacityData, + sizeof (CapacityData), + USB_BOOT_GENERAL_CMD_TIMEOUT + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Get the information on media presence, block size, and last block number + // from READ CAPACITY data. + // + Media->MediaPresent = TRUE; + Media->LastBlock = SwapBytes64 (ReadUnaligned64 ((CONST UINT64 *) &(CapacityData.LastLba7))); + + BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) &(CapacityData.BlockSize3))); + + Media->LowestAlignedLba = (CapacityData.LowestAlignLogic2 << 8) | + CapacityData.LowestAlignLogic1; + Media->LogicalBlocksPerPhysicalBlock = (1 << CapacityData.LogicPerPhysical); + if (BlockSize == 0) { + // + // Get sense data + // + return UsbBootRequestSense (UsbMass); + } else { + Media->BlockSize = BlockSize; + } + + return Status; +} + /** Execute READ CAPACITY command to request information regarding @@ -368,7 +494,7 @@ UsbBootInquiry ( @retval EFI_SUCCESS The disk geometry is successfully retrieved. @retval EFI_NOT_READY The returned block size is zero. @retval Other READ CAPACITY command execution failed. - + **/ EFI_STATUS UsbBootReadCapacity ( @@ -392,7 +518,7 @@ UsbBootReadCapacity ( Status = UsbBootExecCmdWithRetry ( UsbMass, &CapacityCmd, - sizeof (USB_BOOT_READ_CAPACITY_CMD), + (UINT8) sizeof (USB_BOOT_READ_CAPACITY_CMD), EfiUsbDataIn, &CapacityData, sizeof (USB_BOOT_READ_CAPACITY_DATA), @@ -412,15 +538,19 @@ UsbBootReadCapacity ( BlockSize = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen)); if (BlockSize == 0) { // - // Get sense data + // Get sense data // return UsbBootRequestSense (UsbMass); } else { Media->BlockSize = BlockSize; } - DEBUG ((EFI_D_INFO, "UsbBootReadCapacity Success LBA=%ld BlockSize=%d\n", - Media->LastBlock, Media->BlockSize)); + if (Media->LastBlock == 0xFFFFFFFF) { + Status = UsbBootReadCapacity16 (UsbMass); + if (!EFI_ERROR (Status)) { + UsbMass->Cdb16Byte = TRUE; + } + } return Status; } @@ -460,7 +590,7 @@ UsbScsiModeSense ( Status = UsbBootExecCmdWithRetry ( UsbMass, &ModeSenseCmd, - sizeof (USB_SCSI_MODE_SENSE6_CMD), + (UINT8) sizeof (USB_SCSI_MODE_SENSE6_CMD), EfiUsbDataIn, &ModeParaHeader, sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER), @@ -502,10 +632,8 @@ UsbBootGetParams ( { EFI_BLOCK_IO_MEDIA *Media; EFI_STATUS Status; - UINT8 CmdSet; Media = &(UsbMass->BlockIoMedia); - CmdSet = ((EFI_USB_INTERFACE_DESCRIPTOR *) (UsbMass->Context))->InterfaceSubClass; Status = UsbBootInquiry (UsbMass); if (EFI_ERROR (Status)) { @@ -513,6 +641,18 @@ UsbBootGetParams ( return Status; } + // + // According to USB Mass Storage Specification for Bootability, only following + // 4 Peripheral Device Types are in spec. + // + if ((UsbMass->Pdt != USB_PDT_DIRECT_ACCESS) && + (UsbMass->Pdt != USB_PDT_CDROM) && + (UsbMass->Pdt != USB_PDT_OPTICAL) && + (UsbMass->Pdt != USB_PDT_SIMPLE_DIRECT)) { + DEBUG ((EFI_D_ERROR, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass->Pdt)); + return EFI_UNSUPPORTED; + } + // // Don't use the Removable bit in inquiry data to test whether the media // is removable because many flash disks wrongly set this bit. @@ -528,18 +668,9 @@ UsbBootGetParams ( Media->BlockSize = 0x0800; } - if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) { - // - // ModeSense is required for the device with PDT of 0x00/0x07/0x0E, - // which is from [MassStorageBootabilitySpec-Page7]. - // ModeSense(10) is useless here, while ModeSense(6) defined in SCSI - // could get the information of WriteProtected. - // Since not all device support this command, so skip if fail. - // - UsbScsiModeSense (UsbMass); - } + Status = UsbBootDetectMedia (UsbMass); - return UsbBootReadCapacity (UsbMass); + return Status; } @@ -560,7 +691,6 @@ UsbBootDetectMedia ( EFI_BLOCK_IO_MEDIA OldMedia; EFI_BLOCK_IO_MEDIA *Media; UINT8 CmdSet; - EFI_TPL OldTpl; EFI_STATUS Status; Media = &UsbMass->BlockIoMedia; @@ -571,29 +701,41 @@ UsbBootDetectMedia ( Status = UsbBootIsUnitReady (UsbMass); if (EFI_ERROR (Status)) { - goto ON_ERROR; + DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootIsUnitReady (%r)\n", Status)); } - if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) { - // - // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E, - // according to Section 4 of USB Mass Storage Specification for Bootability. - // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI - // could get the information of Write Protected. - // Since not all device support this command, skip if fail. - // - UsbScsiModeSense (UsbMass); - } + // + // Status could be: + // EFI_SUCCESS: all good. + // EFI_NO_MEDIA: media is not present. + // others: HW error. + // For either EFI_NO_MEDIA, or HW error, skip to get WriteProtected and capacity information. + // + if (!EFI_ERROR (Status)) { + if ((UsbMass->Pdt != USB_PDT_CDROM) && (CmdSet == USB_MASS_STORE_SCSI)) { + // + // MODE SENSE is required for the device with PDT of 0x00/0x07/0x0E, + // according to Section 4 of USB Mass Storage Specification for Bootability. + // MODE SENSE(10) is useless here, while MODE SENSE(6) defined in SCSI + // could get the information of Write Protected. + // Since not all device support this command, skip if fail. + // + UsbScsiModeSense (UsbMass); + } - Status = UsbBootReadCapacity (UsbMass); - if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status)); - goto ON_ERROR; + Status = UsbBootReadCapacity (UsbMass); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "UsbBootDetectMedia: UsbBootReadCapacity (%r)\n", Status)); + } } - return EFI_SUCCESS; + if (EFI_ERROR (Status) && Status != EFI_NO_MEDIA) { + // + // For NoMedia, BlockIo is still needed. + // + return Status; + } -ON_ERROR: // // Detect whether it is necessary to reinstall the Block I/O Protocol. // @@ -609,12 +751,15 @@ ON_ERROR: (Media->LastBlock != OldMedia.LastBlock)) { // - // This function is called by Block I/O Protocol APIs, which run at TPL_NOTIFY. - // Here we temporarily restore TPL to TPL_CALLBACK to invoke ReinstallProtocolInterface(). - // - OldTpl = EfiGetCurrentTpl (); - gBS->RestoreTPL (TPL_CALLBACK); + // This function is called from: + // Block I/O Protocol APIs, which run at TPL_CALLBACK. + // DriverBindingStart(), which raises to TPL_CALLBACK. + ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK); + // + // When it is called from DriverBindingStart(), below reinstall fails. + // So ignore the return status check. + // gBS->ReinstallProtocolInterface ( UsbMass->Controller, &gEfiBlockIoProtocolGuid, @@ -622,11 +767,8 @@ ON_ERROR: &UsbMass->BlockIo ); - ASSERT (EfiGetCurrentTpl () == TPL_CALLBACK); - gBS->RaiseTPL (OldTpl); - // - // Update MediaId after reinstalling Block I/O Protocol. + // Reset MediaId after reinstalling Block I/O Protocol. // if (Media->MediaPresent != OldMedia.MediaPresent) { if (Media->MediaPresent) { @@ -641,6 +783,8 @@ ON_ERROR: (Media->LastBlock != OldMedia.LastBlock)) { Media->MediaId++; } + + Status = Media->MediaPresent ? EFI_MEDIA_CHANGED : EFI_NO_MEDIA; } return Status; @@ -670,11 +814,13 @@ UsbBootReadBlocks ( USB_BOOT_READ10_CMD ReadCmd; EFI_STATUS Status; UINT16 Count; + UINT16 CountMax; UINT32 BlockSize; UINT32 ByteSize; UINT32 Timeout; BlockSize = UsbMass->BlockIoMedia.BlockSize; + CountMax = (UINT16)(USB_BOOT_MAX_CARRY_SIZE / BlockSize); Status = EFI_SUCCESS; while (TotalBlock > 0) { @@ -683,7 +829,7 @@ UsbBootReadBlocks ( // on the device. We must split the total block because the READ10 // command only has 16 bit transfer length (in the unit of block). // - Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS); + Count = (UINT16)((TotalBlock < CountMax) ? TotalBlock : CountMax); ByteSize = (UINT32)Count * BlockSize; // @@ -704,7 +850,7 @@ UsbBootReadBlocks ( Status = UsbBootExecCmdWithRetry ( UsbMass, &ReadCmd, - sizeof (USB_BOOT_READ10_CMD), + (UINT8) sizeof (USB_BOOT_READ10_CMD), EfiUsbDataIn, Buffer, ByteSize, @@ -713,7 +859,7 @@ UsbBootReadBlocks ( if (EFI_ERROR (Status)) { return Status; } - + DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count)); Lba += Count; Buffer += Count * BlockSize; TotalBlock -= Count; @@ -746,11 +892,13 @@ UsbBootWriteBlocks ( USB_BOOT_WRITE10_CMD WriteCmd; EFI_STATUS Status; UINT16 Count; + UINT16 CountMax; UINT32 BlockSize; UINT32 ByteSize; UINT32 Timeout; BlockSize = UsbMass->BlockIoMedia.BlockSize; + CountMax = (UINT16)(USB_BOOT_MAX_CARRY_SIZE / BlockSize); Status = EFI_SUCCESS; while (TotalBlock > 0) { @@ -759,7 +907,7 @@ UsbBootWriteBlocks ( // on the device. We must split the total block because the WRITE10 // command only has 16 bit transfer length (in the unit of block). // - Count = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS); + Count = (UINT16)((TotalBlock < CountMax) ? TotalBlock : CountMax); ByteSize = (UINT32)Count * BlockSize; // @@ -780,7 +928,7 @@ UsbBootWriteBlocks ( Status = UsbBootExecCmdWithRetry ( UsbMass, &WriteCmd, - sizeof (USB_BOOT_WRITE10_CMD), + (UINT8) sizeof (USB_BOOT_WRITE10_CMD), EfiUsbDataOut, Buffer, ByteSize, @@ -789,7 +937,159 @@ UsbBootWriteBlocks ( if (EFI_ERROR (Status)) { return Status; } + DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count)); + + Lba += Count; + Buffer += Count * BlockSize; + TotalBlock -= Count; + } + + return Status; +} + +/** + Read some blocks from the device by SCSI 16 byte cmd. + + @param UsbMass The USB mass storage device to read from + @param Lba The start block number + @param TotalBlock Total block number to read + @param Buffer The buffer to read to + + @retval EFI_SUCCESS Data are read into the buffer + @retval Others Failed to read all the data + +**/ +EFI_STATUS +UsbBootReadBlocks16 ( + IN USB_MASS_DEVICE *UsbMass, + IN UINT64 Lba, + IN UINTN TotalBlock, + OUT UINT8 *Buffer + ) +{ + UINT8 ReadCmd[16]; + EFI_STATUS Status; + UINT16 Count; + UINT16 CountMax; + UINT32 BlockSize; + UINT32 ByteSize; + UINT32 Timeout; + + BlockSize = UsbMass->BlockIoMedia.BlockSize; + CountMax = (UINT16)(USB_BOOT_MAX_CARRY_SIZE / BlockSize); + Status = EFI_SUCCESS; + + while (TotalBlock > 0) { + // + // Split the total blocks into smaller pieces. + // + Count = (UINT16)((TotalBlock < CountMax) ? TotalBlock : CountMax); + ByteSize = (UINT32)Count * BlockSize; + + // + // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1] + // + Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT; + + // + // Fill in the command then execute + // + ZeroMem (ReadCmd, sizeof (ReadCmd)); + + ReadCmd[0] = EFI_SCSI_OP_READ16; + ReadCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0)); + WriteUnaligned64 ((UINT64 *) &ReadCmd[2], SwapBytes64 (Lba)); + WriteUnaligned32 ((UINT32 *) &ReadCmd[10], SwapBytes32 (Count)); + + Status = UsbBootExecCmdWithRetry ( + UsbMass, + ReadCmd, + (UINT8) sizeof (ReadCmd), + EfiUsbDataIn, + Buffer, + ByteSize, + Timeout + ); + if (EFI_ERROR (Status)) { + return Status; + } + DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba, Count)); + Lba += Count; + Buffer += Count * BlockSize; + TotalBlock -= Count; + } + + return Status; +} + + +/** + Write some blocks to the device by SCSI 16 byte cmd. + + @param UsbMass The USB mass storage device to write to + @param Lba The start block number + @param TotalBlock Total block number to write + @param Buffer Pointer to the source buffer for the data. + + @retval EFI_SUCCESS Data are written into the buffer + @retval Others Failed to write all the data + +**/ +EFI_STATUS +UsbBootWriteBlocks16 ( + IN USB_MASS_DEVICE *UsbMass, + IN UINT64 Lba, + IN UINTN TotalBlock, + IN UINT8 *Buffer + ) +{ + UINT8 WriteCmd[16]; + EFI_STATUS Status; + UINT16 Count; + UINT16 CountMax; + UINT32 BlockSize; + UINT32 ByteSize; + UINT32 Timeout; + + BlockSize = UsbMass->BlockIoMedia.BlockSize; + CountMax = (UINT16)(USB_BOOT_MAX_CARRY_SIZE / BlockSize); + Status = EFI_SUCCESS; + + while (TotalBlock > 0) { + // + // Split the total blocks into smaller pieces. + // + Count = (UINT16)((TotalBlock < CountMax) ? TotalBlock : CountMax); + ByteSize = (UINT32)Count * BlockSize; + + // + // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1] + // + Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT; + // + // Fill in the write16 command block + // + ZeroMem (WriteCmd, sizeof (WriteCmd)); + + WriteCmd[0] = EFI_SCSI_OP_WRITE16; + WriteCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0)); + WriteUnaligned64 ((UINT64 *) &WriteCmd[2], SwapBytes64 (Lba)); + WriteUnaligned32 ((UINT32 *) &WriteCmd[10], SwapBytes32 (Count)); + + Status = UsbBootExecCmdWithRetry ( + UsbMass, + WriteCmd, + (UINT8) sizeof (WriteCmd), + EfiUsbDataOut, + Buffer, + ByteSize, + Timeout + ); + if (EFI_ERROR (Status)) { + return Status; + } + DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba, Count)); Lba += Count; Buffer += Count * BlockSize; TotalBlock -= Count;