MdeModulePkg/UsbMass: Merge UsbBoot(Read|Write)Blocks(16)

The change doesn't have functionality impact.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
This commit is contained in:
Ruiyu Ni 2018-09-11 18:08:02 +08:00
parent 76d1c03cbd
commit e59db6a732
3 changed files with 80 additions and 252 deletions

View File

@ -792,26 +792,28 @@ UsbBootDetectMedia (
/** /**
Read some blocks from the device. Read or write some blocks from the device.
@param UsbMass The USB mass storage device to read from @param UsbMass The USB mass storage device to access
@param Write TRUE for write operation.
@param Lba The start block number @param Lba The start block number
@param TotalBlock Total block number to read @param TotalBlock Total block number to read or write
@param Buffer The buffer to read to @param Buffer The buffer to read to or write from
@retval EFI_SUCCESS Data are read into the buffer @retval EFI_SUCCESS Data are read into the buffer or writen into the device.
@retval Others Failed to read all the data @retval Others Failed to read or write all the data
**/ **/
EFI_STATUS EFI_STATUS
UsbBootReadBlocks ( UsbBootReadWriteBlocks (
IN USB_MASS_DEVICE *UsbMass, IN USB_MASS_DEVICE *UsbMass,
IN BOOLEAN Write,
IN UINT32 Lba, IN UINT32 Lba,
IN UINTN TotalBlock, IN UINTN TotalBlock,
OUT UINT8 *Buffer IN OUT UINT8 *Buffer
) )
{ {
USB_BOOT_READ10_CMD ReadCmd; USB_BOOT_READ_WRITE_10_CMD Cmd;
EFI_STATUS Status; EFI_STATUS Status;
UINT16 Count; UINT16 Count;
UINT16 CountMax; UINT16 CountMax;
@ -840,17 +842,17 @@ UsbBootReadBlocks (
// //
// Fill in the command then execute // Fill in the command then execute
// //
ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD)); ZeroMem (&Cmd, sizeof (USB_BOOT_READ_WRITE_10_CMD));
ReadCmd.OpCode = USB_BOOT_READ10_OPCODE; Cmd.OpCode = Write ? USB_BOOT_WRITE10_OPCODE : USB_BOOT_READ10_OPCODE;
ReadCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun)); Cmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba)); WriteUnaligned32 ((UINT32 *) Cmd.Lba, SwapBytes32 (Lba));
WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count)); WriteUnaligned16 ((UINT16 *) Cmd.TransferLen, SwapBytes16 (Count));
Status = UsbBootExecCmdWithRetry ( Status = UsbBootExecCmdWithRetry (
UsbMass, UsbMass,
&ReadCmd, &Cmd,
(UINT8) sizeof (USB_BOOT_READ10_CMD), (UINT8) sizeof (USB_BOOT_READ_WRITE_10_CMD),
EfiUsbDataIn, EfiUsbDataIn,
Buffer, Buffer,
ByteSize, ByteSize,
@ -859,86 +861,11 @@ UsbBootReadBlocks (
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return Status; return Status;
} }
DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count)); DEBUG ((
Lba += Count; DEBUG_BLKIO, "UsbBoot%sBlocks: LBA (0x%lx), Blk (0x%x)\n",
Buffer += Count * BlockSize; Write ? L"Write" : L"Read",
TotalBlock -= Count; Lba, Count
} ));
return Status;
}
/**
Write some blocks to the device.
@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
UsbBootWriteBlocks (
IN USB_MASS_DEVICE *UsbMass,
IN UINT32 Lba,
IN UINTN TotalBlock,
IN UINT8 *Buffer
)
{
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) {
//
// Split the total blocks into smaller pieces to ease the pressure
// 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 < 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 write10 command block
//
ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));
WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;
WriteCmd.Lun = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));
WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));
WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));
Status = UsbBootExecCmdWithRetry (
UsbMass,
&WriteCmd,
(UINT8) sizeof (USB_BOOT_WRITE10_CMD),
EfiUsbDataOut,
Buffer,
ByteSize,
Timeout
);
if (EFI_ERROR (Status)) {
return Status;
}
DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));
Lba += Count; Lba += Count;
Buffer += Count * BlockSize; Buffer += Count * BlockSize;
TotalBlock -= Count; TotalBlock -= Count;
@ -948,26 +875,27 @@ UsbBootWriteBlocks (
} }
/** /**
Read some blocks from the device by SCSI 16 byte cmd. Read or write some blocks from the device by SCSI 16 byte cmd.
@param UsbMass The USB mass storage device to read from @param UsbMass The USB mass storage device to access
@param Write TRUE for write operation.
@param Lba The start block number @param Lba The start block number
@param TotalBlock Total block number to read @param TotalBlock Total block number to read or write
@param Buffer The buffer to read to @param Buffer The buffer to read to or write from
@retval EFI_SUCCESS Data are read into the buffer
@retval Others Failed to read all the data
@retval EFI_SUCCESS Data are read into the buffer or writen into the device.
@retval Others Failed to read or write all the data
**/ **/
EFI_STATUS EFI_STATUS
UsbBootReadBlocks16 ( UsbBootReadWriteBlocks16 (
IN USB_MASS_DEVICE *UsbMass, IN USB_MASS_DEVICE *UsbMass,
IN BOOLEAN Write,
IN UINT64 Lba, IN UINT64 Lba,
IN UINTN TotalBlock, IN UINTN TotalBlock,
OUT UINT8 *Buffer IN OUT UINT8 *Buffer
) )
{ {
UINT8 ReadCmd[16]; UINT8 Cmd[16];
EFI_STATUS Status; EFI_STATUS Status;
UINT16 Count; UINT16 Count;
UINT16 CountMax; UINT16 CountMax;
@ -994,17 +922,17 @@ UsbBootReadBlocks16 (
// //
// Fill in the command then execute // Fill in the command then execute
// //
ZeroMem (ReadCmd, sizeof (ReadCmd)); ZeroMem (Cmd, sizeof (Cmd));
ReadCmd[0] = EFI_SCSI_OP_READ16; Cmd[0] = Write ? EFI_SCSI_OP_WRITE16 : EFI_SCSI_OP_READ16;
ReadCmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0)); Cmd[1] = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));
WriteUnaligned64 ((UINT64 *) &ReadCmd[2], SwapBytes64 (Lba)); WriteUnaligned64 ((UINT64 *) &Cmd[2], SwapBytes64 (Lba));
WriteUnaligned32 ((UINT32 *) &ReadCmd[10], SwapBytes32 (Count)); WriteUnaligned32 ((UINT32 *) &Cmd[10], SwapBytes32 (Count));
Status = UsbBootExecCmdWithRetry ( Status = UsbBootExecCmdWithRetry (
UsbMass, UsbMass,
ReadCmd, Cmd,
(UINT8) sizeof (ReadCmd), (UINT8) sizeof (Cmd),
EfiUsbDataIn, EfiUsbDataIn,
Buffer, Buffer,
ByteSize, ByteSize,
@ -1013,83 +941,11 @@ UsbBootReadBlocks16 (
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return Status; return Status;
} }
DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba, Count)); DEBUG ((
Lba += Count; DEBUG_BLKIO, "UsbBoot%sBlocks16: LBA (0x%lx), Blk (0x%x)\n",
Buffer += Count * BlockSize; Write ? L"Write" : L"Read",
TotalBlock -= Count; Lba, 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; Lba += Count;
Buffer += Count * BlockSize; Buffer += Count * BlockSize;
TotalBlock -= Count; TotalBlock -= Count;

View File

@ -161,17 +161,7 @@ typedef struct {
UINT8 TransferLen[2]; ///< Transfer length UINT8 TransferLen[2]; ///< Transfer length
UINT8 Reserverd1; UINT8 Reserverd1;
UINT8 Pad[2]; UINT8 Pad[2];
} USB_BOOT_READ10_CMD; } USB_BOOT_READ_WRITE_10_CMD;
typedef struct {
UINT8 OpCode;
UINT8 Lun;
UINT8 Lba[4];
UINT8 Reserved0;
UINT8 TransferLen[2];
UINT8 Reserverd1;
UINT8 Pad[2];
} USB_BOOT_WRITE10_CMD;
typedef struct { typedef struct {
UINT8 OpCode; UINT8 OpCode;
@ -292,66 +282,48 @@ UsbBootReadBlocks (
); );
/** /**
Write some blocks to the device. Read or write some blocks from the device.
@param UsbMass The USB mass storage device to write to @param UsbMass The USB mass storage device to access
@param Write TRUE for write operation.
@param Lba The start block number @param Lba The start block number
@param TotalBlock Total block number to write @param TotalBlock Total block number to read or write
@param Buffer Pointer to the source buffer for the data. @param Buffer The buffer to read to or write from
@retval EFI_SUCCESS Data are written into the buffer @retval EFI_SUCCESS Data are read into the buffer or writen into the device.
@retval Others Failed to write all the data @retval Others Failed to read or write all the data
**/ **/
EFI_STATUS EFI_STATUS
UsbBootWriteBlocks ( UsbBootReadWriteBlocks (
IN USB_MASS_DEVICE *UsbMass, IN USB_MASS_DEVICE *UsbMass,
IN BOOLEAN Write,
IN UINT32 Lba, IN UINT32 Lba,
IN UINTN TotalBlock, IN UINTN TotalBlock,
IN UINT8 *Buffer IN OUT UINT8 *Buffer
); );
/** /**
Read some blocks from the device by SCSI 16 byte cmd. Read or write some blocks from the device by SCSI 16 byte cmd.
@param UsbMass The USB mass storage device to read from @param UsbMass The USB mass storage device to access
@param Write TRUE for write operation.
@param Lba The start block number @param Lba The start block number
@param TotalBlock Total block number to read @param TotalBlock Total block number to read or write
@param Buffer The buffer to read to @param Buffer The buffer to read to or write from
@retval EFI_SUCCESS Data are read into the buffer
@retval Others Failed to read all the data
@retval EFI_SUCCESS Data are read into the buffer or writen into the device.
@retval Others Failed to read or write all the data
**/ **/
EFI_STATUS EFI_STATUS
UsbBootReadBlocks16 ( UsbBootReadWriteBlocks16 (
IN USB_MASS_DEVICE *UsbMass, IN USB_MASS_DEVICE *UsbMass,
IN BOOLEAN Write,
IN UINT64 Lba, IN UINT64 Lba,
IN UINTN TotalBlock, IN UINTN TotalBlock,
OUT UINT8 *Buffer IN OUT UINT8 *Buffer
); );
/**
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
);
/** /**
Use the USB clear feature control transfer to clear the endpoint stall condition. Use the USB clear feature control transfer to clear the endpoint stall condition.

View File

@ -172,9 +172,9 @@ UsbMassReadBlocks (
} }
if (UsbMass->Cdb16Byte) { if (UsbMass->Cdb16Byte) {
Status = UsbBootReadBlocks16 (UsbMass, Lba, TotalBlock, Buffer); Status = UsbBootReadWriteBlocks16 (UsbMass, FALSE, Lba, TotalBlock, Buffer);
} else { } else {
Status = UsbBootReadBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer); Status = UsbBootReadWriteBlocks (UsbMass, FALSE, (UINT32) Lba, TotalBlock, Buffer);
} }
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
@ -292,9 +292,9 @@ UsbMassWriteBlocks (
// and clear the status should the write succeed. // and clear the status should the write succeed.
// //
if (UsbMass->Cdb16Byte) { if (UsbMass->Cdb16Byte) {
Status = UsbBootWriteBlocks16 (UsbMass, Lba, TotalBlock, Buffer); Status = UsbBootReadWriteBlocks16 (UsbMass, TRUE, Lba, TotalBlock, Buffer);
} else { } else {
Status = UsbBootWriteBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer); Status = UsbBootReadWriteBlocks (UsbMass, TRUE, (UINT32) Lba, TotalBlock, Buffer);
} }
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {