]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
MdeModulePkg/UsbMass: Merge UsbBoot(Read|Write)Blocks(16)
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassBoot.c
index dd5950c54c291ba09ad11651ed5175ad73f650c2..07a376440c65d4f9e90317aa06eb722aa09560c0 100644 (file)
@@ -792,32 +792,34 @@ UsbBootDetectMedia (
 \r
 \r
 /**\r
-  Read some blocks from the device.\r
+  Read or write some blocks from the device.\r
 \r
-  @param  UsbMass                The USB mass storage device to read from\r
+  @param  UsbMass                The USB mass storage device to access\r
+  @param  Write                  TRUE for write operation.\r
   @param  Lba                    The start block number\r
-  @param  TotalBlock             Total block number to read\r
-  @param  Buffer                 The buffer to read to\r
+  @param  TotalBlock             Total block number to read or write\r
+  @param  Buffer                 The buffer to read to or write from\r
 \r
-  @retval EFI_SUCCESS            Data are read into the buffer\r
-  @retval Others                 Failed to read all the data\r
+  @retval EFI_SUCCESS            Data are read into the buffer or writen into the device.\r
+  @retval Others                 Failed to read or write all the data\r
 \r
 **/\r
 EFI_STATUS\r
-UsbBootReadBlocks (\r
+UsbBootReadWriteBlocks (\r
   IN  USB_MASS_DEVICE       *UsbMass,\r
+  IN  BOOLEAN               Write,\r
   IN  UINT32                Lba,\r
   IN  UINTN                 TotalBlock,\r
-  OUT UINT8                 *Buffer\r
+  IN OUT UINT8              *Buffer\r
   )\r
 {\r
-  USB_BOOT_READ10_CMD       ReadCmd;\r
-  EFI_STATUS                Status;\r
-  UINT16                    Count;\r
-  UINT16                    CountMax;\r
-  UINT32                    BlockSize;\r
-  UINT32                    ByteSize;\r
-  UINT32                    Timeout;\r
+  USB_BOOT_READ_WRITE_10_CMD Cmd;\r
+  EFI_STATUS                 Status;\r
+  UINT16                     Count;\r
+  UINT16                     CountMax;\r
+  UINT32                     BlockSize;\r
+  UINT32                     ByteSize;\r
+  UINT32                     Timeout;\r
 \r
   BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
   CountMax = (UINT16)(USB_BOOT_MAX_CARRY_SIZE / BlockSize);\r
@@ -840,17 +842,17 @@ UsbBootReadBlocks (
     //\r
     // Fill in the command then execute\r
     //\r
-    ZeroMem (&ReadCmd, sizeof (USB_BOOT_READ10_CMD));\r
+    ZeroMem (&Cmd, sizeof (USB_BOOT_READ_WRITE_10_CMD));\r
 \r
-    ReadCmd.OpCode  = USB_BOOT_READ10_OPCODE;\r
-    ReadCmd.Lun     = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
-    WriteUnaligned32 ((UINT32 *) ReadCmd.Lba, SwapBytes32 (Lba));\r
-    WriteUnaligned16 ((UINT16 *) ReadCmd.TransferLen, SwapBytes16 (Count));\r
+    Cmd.OpCode  = Write ? USB_BOOT_WRITE10_OPCODE : USB_BOOT_READ10_OPCODE;\r
+    Cmd.Lun     = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
+    WriteUnaligned32 ((UINT32 *) Cmd.Lba, SwapBytes32 (Lba));\r
+    WriteUnaligned16 ((UINT16 *) Cmd.TransferLen, SwapBytes16 (Count));\r
 \r
     Status = UsbBootExecCmdWithRetry (\r
                UsbMass,\r
-               &ReadCmd,\r
-               (UINT8) sizeof (USB_BOOT_READ10_CMD),\r
+               &Cmd,\r
+               (UINT8) sizeof (USB_BOOT_READ_WRITE_10_CMD),\r
                EfiUsbDataIn,\r
                Buffer,\r
                ByteSize,\r
@@ -859,86 +861,11 @@ UsbBootReadBlocks (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-    DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));\r
-    Lba        += Count;\r
-    Buffer     += Count * BlockSize;\r
-    TotalBlock -= Count;\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Write some blocks to the device.\r
-\r
-  @param  UsbMass                The USB mass storage device to write to\r
-  @param  Lba                    The start block number\r
-  @param  TotalBlock             Total block number to write\r
-  @param  Buffer                 Pointer to the source buffer for the data.\r
-\r
-  @retval EFI_SUCCESS            Data are written into the buffer\r
-  @retval Others                 Failed to write all the data\r
-\r
-**/\r
-EFI_STATUS\r
-UsbBootWriteBlocks (\r
-  IN  USB_MASS_DEVICE         *UsbMass,\r
-  IN  UINT32                  Lba,\r
-  IN  UINTN                   TotalBlock,\r
-  IN  UINT8                   *Buffer\r
-  )\r
-{\r
-  USB_BOOT_WRITE10_CMD  WriteCmd;\r
-  EFI_STATUS            Status;\r
-  UINT16                Count;\r
-  UINT16                CountMax;\r
-  UINT32                BlockSize;\r
-  UINT32                ByteSize;\r
-  UINT32                Timeout;\r
-\r
-  BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
-  CountMax = (UINT16)(USB_BOOT_MAX_CARRY_SIZE / BlockSize);\r
-  Status    = EFI_SUCCESS;\r
-\r
-  while (TotalBlock > 0) {\r
-    //\r
-    // Split the total blocks into smaller pieces to ease the pressure\r
-    // on the device. We must split the total block because the WRITE10\r
-    // command only has 16 bit transfer length (in the unit of block).\r
-    //\r
-    Count     = (UINT16)((TotalBlock < CountMax) ? TotalBlock : CountMax);\r
-    ByteSize  = (UINT32)Count * BlockSize;\r
-\r
-    //\r
-    // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r
-    //\r
-    Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r
-\r
-    //\r
-    // Fill in the write10 command block\r
-    //\r
-    ZeroMem (&WriteCmd, sizeof (USB_BOOT_WRITE10_CMD));\r
-\r
-    WriteCmd.OpCode = USB_BOOT_WRITE10_OPCODE;\r
-    WriteCmd.Lun    = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
-    WriteUnaligned32 ((UINT32 *) WriteCmd.Lba, SwapBytes32 (Lba));\r
-    WriteUnaligned16 ((UINT16 *) WriteCmd.TransferLen, SwapBytes16 (Count));\r
-\r
-    Status = UsbBootExecCmdWithRetry (\r
-               UsbMass,\r
-               &WriteCmd,\r
-               (UINT8) sizeof (USB_BOOT_WRITE10_CMD),\r
-               EfiUsbDataOut,\r
-               Buffer,\r
-               ByteSize,\r
-               Timeout\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-    DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));\r
-\r
+    DEBUG ((\r
+      DEBUG_BLKIO, "UsbBoot%sBlocks: LBA (0x%lx), Blk (0x%x)\n",\r
+      Write ? L"Write" : L"Read",\r
+      Lba, Count\r
+      ));\r
     Lba        += Count;\r
     Buffer     += Count * BlockSize;\r
     TotalBlock -= Count;\r
@@ -948,26 +875,27 @@ UsbBootWriteBlocks (
 }\r
 \r
 /**\r
-  Read some blocks from the device by SCSI 16 byte cmd.\r
+  Read or write some blocks from the device by SCSI 16 byte cmd.\r
 \r
-  @param  UsbMass                The USB mass storage device to read from\r
+  @param  UsbMass                The USB mass storage device to access\r
+  @param  Write                  TRUE for write operation.\r
   @param  Lba                    The start block number\r
-  @param  TotalBlock             Total block number to read\r
-  @param  Buffer                 The buffer to read to\r
-\r
-  @retval EFI_SUCCESS            Data are read into the buffer\r
-  @retval Others                 Failed to read all the data\r
+  @param  TotalBlock             Total block number to read or write\r
+  @param  Buffer                 The buffer to read to or write from\r
 \r
+  @retval EFI_SUCCESS            Data are read into the buffer or writen into the device.\r
+  @retval Others                 Failed to read or write all the data\r
 **/\r
 EFI_STATUS\r
-UsbBootReadBlocks16 (\r
+UsbBootReadWriteBlocks16 (\r
   IN  USB_MASS_DEVICE       *UsbMass,\r
+  IN  BOOLEAN               Write,\r
   IN  UINT64                Lba,\r
   IN  UINTN                 TotalBlock,\r
-  OUT UINT8                 *Buffer\r
+  IN OUT UINT8              *Buffer\r
   )\r
 {\r
-  UINT8                     ReadCmd[16];\r
+  UINT8                     Cmd[16];\r
   EFI_STATUS                Status;\r
   UINT16                    Count;\r
   UINT16                    CountMax;\r
@@ -994,17 +922,17 @@ UsbBootReadBlocks16 (
     //\r
     // Fill in the command then execute\r
     //\r
-    ZeroMem (ReadCmd, sizeof (ReadCmd));\r
+    ZeroMem (Cmd, sizeof (Cmd));\r
 \r
-    ReadCmd[0]  = EFI_SCSI_OP_READ16;\r
-    ReadCmd[1]  = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));\r
-    WriteUnaligned64 ((UINT64 *) &ReadCmd[2], SwapBytes64 (Lba));\r
-    WriteUnaligned32 ((UINT32 *) &ReadCmd[10], SwapBytes32 (Count));\r
+    Cmd[0]  = Write ? EFI_SCSI_OP_WRITE16 : EFI_SCSI_OP_READ16;\r
+    Cmd[1]  = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));\r
+    WriteUnaligned64 ((UINT64 *) &Cmd[2], SwapBytes64 (Lba));\r
+    WriteUnaligned32 ((UINT32 *) &Cmd[10], SwapBytes32 (Count));\r
 \r
     Status = UsbBootExecCmdWithRetry (\r
                UsbMass,\r
-               ReadCmd,\r
-               (UINT8) sizeof (ReadCmd),\r
+               Cmd,\r
+               (UINT8) sizeof (Cmd),\r
                EfiUsbDataIn,\r
                Buffer,\r
                ByteSize,\r
@@ -1013,83 +941,11 @@ UsbBootReadBlocks16 (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-    DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks16: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));\r
-    Lba        += Count;\r
-    Buffer     += Count * BlockSize;\r
-    TotalBlock -= Count;\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Write some blocks to the device by SCSI 16 byte cmd.\r
-\r
-  @param  UsbMass                The USB mass storage device to write to\r
-  @param  Lba                    The start block number\r
-  @param  TotalBlock             Total block number to write\r
-  @param  Buffer                 Pointer to the source buffer for the data.\r
-\r
-  @retval EFI_SUCCESS            Data are written into the buffer\r
-  @retval Others                 Failed to write all the data\r
-\r
-**/\r
-EFI_STATUS\r
-UsbBootWriteBlocks16 (\r
-  IN  USB_MASS_DEVICE         *UsbMass,\r
-  IN  UINT64                  Lba,\r
-  IN  UINTN                   TotalBlock,\r
-  IN  UINT8                   *Buffer\r
-  )\r
-{\r
-  UINT8                 WriteCmd[16];\r
-  EFI_STATUS            Status;\r
-  UINT16                Count;\r
-  UINT16                CountMax;\r
-  UINT32                BlockSize;\r
-  UINT32                ByteSize;\r
-  UINT32                Timeout;\r
-\r
-  BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
-  CountMax = (UINT16)(USB_BOOT_MAX_CARRY_SIZE / BlockSize);\r
-  Status    = EFI_SUCCESS;\r
-\r
-  while (TotalBlock > 0) {\r
-    //\r
-    // Split the total blocks into smaller pieces.\r
-    //\r
-    Count     = (UINT16)((TotalBlock < CountMax) ? TotalBlock : CountMax);\r
-    ByteSize  = (UINT32)Count * BlockSize;\r
-\r
-    //\r
-    // USB command's upper limit timeout is 5s. [USB2.0-9.2.6.1]\r
-    //\r
-    Timeout = (UINT32) USB_BOOT_GENERAL_CMD_TIMEOUT;\r
-\r
-    //\r
-    // Fill in the write16 command block\r
-    //\r
-    ZeroMem (WriteCmd, sizeof (WriteCmd));\r
-\r
-    WriteCmd[0]  = EFI_SCSI_OP_WRITE16;\r
-    WriteCmd[1]  = (UINT8) ((USB_BOOT_LUN (UsbMass->Lun) & 0xE0));\r
-    WriteUnaligned64 ((UINT64 *) &WriteCmd[2], SwapBytes64 (Lba));\r
-    WriteUnaligned32 ((UINT32 *) &WriteCmd[10], SwapBytes32 (Count));\r
-\r
-    Status = UsbBootExecCmdWithRetry (\r
-               UsbMass,\r
-               WriteCmd,\r
-               (UINT8) sizeof (WriteCmd),\r
-               EfiUsbDataOut,\r
-               Buffer,\r
-               ByteSize,\r
-               Timeout\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-    DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%lx), Blk (0x%x)\n", Lba, Count));\r
+    DEBUG ((\r
+      DEBUG_BLKIO, "UsbBoot%sBlocks16: LBA (0x%lx), Blk (0x%x)\n",\r
+      Write ? L"Write" : L"Read",\r
+      Lba, Count\r
+      ));\r
     Lba        += Count;\r
     Buffer     += Count * BlockSize;\r
     TotalBlock -= Count;\r