]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/UsbMassStorage: Add 16 byte SCSI cmds support if the last LBA can not...
authorerictian <erictian@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 14 May 2012 07:50:07 +0000 (07:50 +0000)
committererictian <erictian@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 14 May 2012 07:50:07 +0000 (07:50 +0000)
Signed-off-by: Feng Tian <feng.tian@intel.com>
Reviewed-by: Li Elvin <elvin.li@intel.com>
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13311 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMass.h
MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.h
MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassImpl.c

index d3c4476ecec6cd645a61048a1c34223868a071ee..291cc13b61406878c0a41b40886e57f413719fd7 100644 (file)
@@ -2,7 +2,7 @@
   Definition of USB Mass Storage Class and its value, USB Mass Transport Protocol, \r
   and other common definitions.\r
 \r
-Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -18,7 +18,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 \r
 #include <Uefi.h>\r
-\r
+#include <IndustryStandard/Scsi.h>\r
 #include <Protocol/BlockIo.h>\r
 #include <Protocol/UsbIo.h>\r
 #include <Protocol/DevicePath.h>\r
@@ -209,6 +209,7 @@ struct _USB_MASS_DEVICE {
   VOID                      *Context;\r
   EFI_DISK_INFO_PROTOCOL    DiskInfo;\r
   USB_BOOT_INQUIRY_DATA     InquiryData;\r
+  BOOLEAN                   Cdb16Byte;\r
 };\r
 \r
 #endif\r
index 46330153e22bd2f42bc18542017e4d0d1840b7dc..9e0343da6918276648a39f037bf1622c75476e98 100644 (file)
@@ -386,6 +386,87 @@ UsbBootInquiry (
   return Status;\r
 }\r
 \r
+/**\r
+  Execute READ CAPACITY 16 bytes command to request information regarding\r
+  the capacity of the installed medium of the device.\r
+\r
+  This function executes READ CAPACITY 16 bytes command to get the capacity\r
+  of the USB mass storage media, including the presence, block size,\r
+  and last block number.\r
+\r
+  @param  UsbMass                The device to retireve disk gemotric.\r
+\r
+  @retval EFI_SUCCESS            The disk geometry is successfully retrieved.\r
+  @retval EFI_NOT_READY          The returned block size is zero.\r
+  @retval Other                  READ CAPACITY 16 bytes command execution failed.\r
\r
+**/\r
+EFI_STATUS\r
+UsbBootReadCapacity16 (\r
+  IN USB_MASS_DEVICE            *UsbMass\r
+  )\r
+{\r
+  UINT8                         CapacityCmd[16];\r
+  EFI_SCSI_DISK_CAPACITY_DATA16 CapacityData;\r
+  EFI_BLOCK_IO_MEDIA            *Media;\r
+  EFI_STATUS                    Status;\r
+  UINT32                        BlockSize;\r
+\r
+  Media   = &UsbMass->BlockIoMedia;\r
+\r
+  Media->MediaPresent = FALSE;\r
+  Media->LastBlock    = 0;\r
+  Media->BlockSize    = 0;\r
+\r
+  ZeroMem (CapacityCmd, sizeof (CapacityCmd));\r
+  ZeroMem (&CapacityData, sizeof (CapacityData));\r
+\r
+  CapacityCmd[0]  = EFI_SCSI_OP_READ_CAPACITY16;\r
+  CapacityCmd[1]  = 0x10;\r
+  //\r
+  // Partial medium indicator, set the bytes 2 ~ 9 of the Cdb as ZERO.\r
+  //\r
+  ZeroMem ((CapacityCmd + 2), 8);\r
+\r
+  CapacityCmd[13] = sizeof (CapacityData);\r
+  \r
+  Status = UsbBootExecCmdWithRetry (\r
+             UsbMass,\r
+             CapacityCmd,\r
+             (UINT8) sizeof (CapacityCmd),\r
+             EfiUsbDataIn,\r
+             &CapacityData,\r
+             sizeof (CapacityData),\r
+             USB_BOOT_GENERAL_CMD_TIMEOUT\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Get the information on media presence, block size, and last block number\r
+  // from READ CAPACITY data.\r
+  //\r
+  Media->MediaPresent = TRUE;\r
+  Media->LastBlock    = SwapBytes64 (ReadUnaligned64 ((CONST UINT64 *) &(CapacityData.LastLba7)));\r
+\r
+  BlockSize           = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) &(CapacityData.BlockSize3)));\r
+  \r
+  Media->LowestAlignedLba = (CapacityData.LowestAlignLogic2 << 8) |\r
+                             CapacityData.LowestAlignLogic1;\r
+  Media->LogicalBlocksPerPhysicalBlock  = (1 << CapacityData.LogicPerPhysical);\r
+  if (BlockSize == 0) {\r
+    //\r
+    //  Get sense data  \r
+    //\r
+    return UsbBootRequestSense (UsbMass);\r
+  } else {\r
+    Media->BlockSize = BlockSize;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
 \r
 /**\r
   Execute READ CAPACITY command to request information regarding\r
@@ -451,6 +532,13 @@ UsbBootReadCapacity (
     Media->BlockSize = BlockSize;\r
   }\r
 \r
+  if (Media->LastBlock == 0xFFFFFFFF) {\r
+    Status = UsbBootReadCapacity16 (UsbMass);\r
+    if (!EFI_ERROR (Status)) {\r
+      UsbMass->Cdb16Byte = TRUE;\r
+    }\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -731,7 +819,7 @@ UsbBootReadBlocks (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-    DEBUG ((EFI_D_BLKIO, "UsbBootReadBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, TotalBlock));\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
@@ -807,7 +895,155 @@ UsbBootWriteBlocks (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-    DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, TotalBlock));\r
+    DEBUG ((EFI_D_BLKIO, "UsbBootWriteBlocks: LBA (0x%x), Blk (0x%x)\n", Lba, Count));\r
+\r
+    Lba        += Count;\r
+    Buffer     += Count * BlockSize;\r
+    TotalBlock -= Count;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Read some blocks from the device by SCSI 16 byte cmd.\r
+\r
+  @param  UsbMass                The USB mass storage device to read from\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
+\r
+**/\r
+EFI_STATUS\r
+UsbBootReadBlocks16 (\r
+  IN  USB_MASS_DEVICE       *UsbMass,\r
+  IN  UINT64                Lba,\r
+  IN  UINTN                 TotalBlock,\r
+  OUT UINT8                 *Buffer\r
+  )\r
+{\r
+  UINT8                     ReadCmd[16];\r
+  EFI_STATUS                Status;\r
+  UINT16                    Count;\r
+  UINT32                    BlockSize;\r
+  UINT32                    ByteSize;\r
+  UINT32                    Timeout;\r
+\r
+  BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
+  Status    = EFI_SUCCESS;\r
+\r
+  while (TotalBlock > 0) {\r
+    //\r
+    // Split the total blocks into smaller pieces.\r
+    //\r
+    Count     = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\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 command then execute\r
+    //\r
+    ZeroMem (ReadCmd, sizeof (ReadCmd));\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
+\r
+    Status = UsbBootExecCmdWithRetry (\r
+               UsbMass,\r
+               ReadCmd,\r
+               (UINT8) sizeof (ReadCmd),\r
+               EfiUsbDataIn,\r
+               Buffer,\r
+               ByteSize,\r
+               Timeout\r
+               );\r
+    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
+  UINT32                BlockSize;\r
+  UINT32                ByteSize;\r
+  UINT32                Timeout;\r
+\r
+  BlockSize = UsbMass->BlockIoMedia.BlockSize;\r
+  Status    = EFI_SUCCESS;\r
+\r
+  while (TotalBlock > 0) {\r
+    //\r
+    // Split the total blocks into smaller pieces.\r
+    //\r
+    Count     = (UINT16)((TotalBlock < USB_BOOT_IO_BLOCKS) ? TotalBlock : USB_BOOT_IO_BLOCKS);\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
     Lba        += Count;\r
     Buffer     += Count * BlockSize;\r
     TotalBlock -= Count;\r
index 9961b7a0f504c42b6db253cc7cefa690ceecfb24..c4082558fa930c93ae9b22af5bc5d1e676570343 100644 (file)
@@ -2,7 +2,7 @@
   Definition of the command set of USB Mass Storage Specification\r
   for Bootability, Revision 1.0.\r
 \r
-Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -310,6 +310,47 @@ UsbBootWriteBlocks (
   IN  UINT8                   *Buffer\r
   );\r
 \r
+/**\r
+  Read some blocks from the device by SCSI 16 byte cmd.\r
+\r
+  @param  UsbMass                The USB mass storage device to read from\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
+\r
+**/\r
+EFI_STATUS\r
+UsbBootReadBlocks16 (\r
+  IN  USB_MASS_DEVICE       *UsbMass,\r
+  IN  UINT64                Lba,\r
+  IN  UINTN                 TotalBlock,\r
+  OUT UINT8                 *Buffer\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
+\r
 /**\r
   Use the USB clear feature control transfer to clear the endpoint stall condition.\r
 \r
index 84cf31abce76e99976d93c5110693d9904bf6ee9..4cbedfade6caec49755982f504f5f2fe59f7d00b 100644 (file)
@@ -171,7 +171,12 @@ UsbMassReadBlocks (
     goto ON_EXIT;\r
   }\r
 \r
-  Status = UsbBootReadBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer);\r
+  if (UsbMass->Cdb16Byte) {\r
+    Status = UsbBootReadBlocks16 (UsbMass, Lba, TotalBlock, Buffer);\r
+  } else {\r
+    Status = UsbBootReadBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer);\r
+  }\r
+\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "UsbMassReadBlocks: UsbBootReadBlocks (%r) -> Reset\n", Status));\r
     UsbMassReset (This, TRUE);\r
@@ -286,7 +291,12 @@ UsbMassWriteBlocks (
   // Try to write the data even the device is marked as ReadOnly,\r
   // and clear the status should the write succeed.\r
   //\r
-  Status = UsbBootWriteBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer);\r
+  if (UsbMass->Cdb16Byte) {\r
+    Status = UsbBootWriteBlocks16 (UsbMass, Lba, TotalBlock, Buffer);\r
+  } else {\r
+    Status = UsbBootWriteBlocks (UsbMass, (UINT32) Lba, TotalBlock, Buffer);\r
+  }  \r
+\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "UsbMassWriteBlocks: UsbBootWriteBlocks (%r) -> Reset\n", Status));\r
     UsbMassReset (This, TRUE);\r