]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbMassStorageDxe/UsbMassBoot.c
MdeModulePkg/UsbMass: Fix hot-plug USB CDROM can't be recognized
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMassStorageDxe / UsbMassBoot.c
index 71deef051e39b666538f44f27fd576f4804712ee..12e68d21497a4504ea37c5038600cc5df22cabb6 100644 (file)
@@ -2,8 +2,8 @@
   Implementation of the command set of USB Mass Storage Specification\r
   for Bootability, Revision 1.0.\r
 \r
-Copyright (c) 2007 - 2008, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2007 - 2018, 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
 http://opensource.org/licenses/bsd-license.php\r
@@ -13,14 +13,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include "UsbMassImpl.h"\r
+#include "UsbMass.h"\r
 \r
 /**\r
   Execute REQUEST SENSE Command to retrieve sense data from device.\r
 \r
   @param  UsbMass                The device whose sense data is requested.\r
 \r
-  @retval EFI_SUCCESS            The command is excuted successfully.\r
+  @retval EFI_SUCCESS            The command is executed successfully.\r
   @retval EFI_DEVICE_ERROR       Failed to request sense.\r
   @retval EFI_NO_RESPONSE        The device media doesn't response this request.\r
   @retval EFI_INVALID_PARAMETER  The command has some invalid parameters.\r
@@ -50,7 +50,7 @@ UsbBootRequestSense (
 \r
   SenseCmd.OpCode   = USB_BOOT_REQUEST_SENSE_OPCODE;\r
   SenseCmd.Lun      = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
-  SenseCmd.AllocLen = sizeof (USB_BOOT_REQUEST_SENSE_DATA);\r
+  SenseCmd.AllocLen = (UINT8) sizeof (USB_BOOT_REQUEST_SENSE_DATA);\r
 \r
   Status = Transport->ExecCommand (\r
                         UsbMass->Context,\r
@@ -65,6 +65,9 @@ UsbBootRequestSense (
                         );\r
   if (EFI_ERROR (Status) || CmdResult != USB_MASS_CMD_SUCCESS) {\r
     DEBUG ((EFI_D_ERROR, "UsbBootRequestSense: (%r) CmdResult=0x%x\n", Status, CmdResult));\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = EFI_DEVICE_ERROR;\r
+    }\r
     return Status;\r
   }\r
 \r
@@ -77,7 +80,14 @@ UsbBootRequestSense (
   switch (USB_BOOT_SENSE_KEY (SenseData.SenseKey)) {\r
 \r
   case USB_BOOT_SENSE_NO_SENSE:\r
-    Status = EFI_NO_RESPONSE;\r
+    if (SenseData.Asc == USB_BOOT_ASC_NO_ADDITIONAL_SENSE_INFORMATION) {\r
+      //\r
+      // It is not an error if a device does not have additional sense information\r
+      //\r
+      Status = EFI_SUCCESS;\r
+    } else {\r
+      Status = EFI_NO_RESPONSE;\r
+    }\r
     break;\r
 \r
   case USB_BOOT_SENSE_RECOVERED:\r
@@ -89,10 +99,10 @@ UsbBootRequestSense (
 \r
   case USB_BOOT_SENSE_NOT_READY:\r
     Status = EFI_DEVICE_ERROR;\r
-    if (SenseData.ASC == USB_BOOT_ASC_NO_MEDIA) {\r
+    if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) {\r
       Media->MediaPresent = FALSE;\r
       Status = EFI_NO_MEDIA;\r
-    } else if (SenseData.ASC == USB_BOOT_ASC_NOT_READY) {\r
+    } else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) {\r
       Status = EFI_NOT_READY;\r
     }\r
     break;\r
@@ -103,13 +113,19 @@ UsbBootRequestSense (
 \r
   case USB_BOOT_SENSE_UNIT_ATTENTION:\r
     Status = EFI_DEVICE_ERROR;\r
-    if (SenseData.ASC == USB_BOOT_ASC_MEDIA_CHANGE) {\r
+    if (SenseData.Asc == USB_BOOT_ASC_MEDIA_CHANGE) {\r
       //\r
       // If MediaChange, reset ReadOnly and new MediaId\r
       //\r
       Status = EFI_MEDIA_CHANGED;\r
       Media->ReadOnly = FALSE;\r
       Media->MediaId++;\r
+    } else if (SenseData.Asc == USB_BOOT_ASC_NOT_READY) {\r
+      Status = EFI_NOT_READY;\r
+    } else if (SenseData.Asc == USB_BOOT_ASC_NO_MEDIA) {\r
+      Status = EFI_NOT_READY;\r
+    } else if (SenseData.Asc == USB_BOOT_ASC_POWER_ON) {\r
+      Status = EFI_SUCCESS;\r
     }\r
     break;\r
 \r
@@ -126,8 +142,8 @@ UsbBootRequestSense (
   DEBUG ((EFI_D_INFO, "UsbBootRequestSense: (%r) with sense key %x/%x/%x\n",\r
           Status,\r
           USB_BOOT_SENSE_KEY (SenseData.SenseKey),\r
-          SenseData.ASC,\r
-          SenseData.ASCQ\r
+          SenseData.Asc,\r
+          SenseData.Ascq\r
           ));\r
 \r
   return Status;\r
@@ -149,7 +165,7 @@ UsbBootRequestSense (
   @param  DataLen                The length of expected data\r
   @param  Timeout                The timeout used to transfer\r
 \r
-  @retval EFI_SUCCESS            Command is excuted successfully\r
+  @retval EFI_SUCCESS            Command is executed successfully\r
   @retval Others                 Command execution failed.\r
 \r
 **/\r
@@ -180,6 +196,12 @@ UsbBootExecCmd (
                            Timeout,\r
                            &CmdResult\r
                            );\r
+\r
+  if (Status == EFI_TIMEOUT) {\r
+    DEBUG ((EFI_D_ERROR, "UsbBootExecCmd: Timeout to Exec 0x%x Cmd\n", *(UINT8 *)Cmd));\r
+    return EFI_TIMEOUT;\r
+  }\r
+\r
   //\r
   // If ExecCommand() returns no error and CmdResult is success,\r
   // then the commnad transfer is successful.\r
@@ -188,9 +210,6 @@ UsbBootExecCmd (
     return EFI_SUCCESS;\r
   }\r
 \r
-  DEBUG ((EFI_D_INFO, "UsbBootExecCmd: Fail to Exec 0x%x Cmd /w %r\n",\r
-          *(UINT8 *)Cmd ,Status));\r
-\r
   //\r
   // If command execution failed, then retrieve error info via sense request.\r
   //\r
@@ -232,11 +251,30 @@ UsbBootExecCmdWithRetry (
 {\r
   EFI_STATUS                  Status;\r
   UINTN                       Retry;\r
+  EFI_EVENT                   TimeoutEvt;\r
+\r
+  Retry  = 0;\r
+  Status = EFI_SUCCESS;\r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER,\r
+                  TPL_CALLBACK,\r
+                  NULL,\r
+                  NULL,\r
+                  &TimeoutEvt\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
-  Status  = EFI_SUCCESS;\r
-\r
-  for (Retry = 0; Retry < USB_BOOT_COMMAND_RETRY; Retry++) {\r
+  Status = gBS->SetTimer (TimeoutEvt, TimerRelative, EFI_TIMER_PERIOD_SECONDS(60));\r
+  if (EFI_ERROR (Status)) {\r
+    goto EXIT;\r
+  }\r
 \r
+  //\r
+  // Execute the cmd and retry if it fails.\r
+  //\r
+  while (EFI_ERROR (gBS->CheckEvent (TimeoutEvt))) {\r
     Status = UsbBootExecCmd (\r
                UsbMass,\r
                Cmd,\r
@@ -246,17 +284,29 @@ UsbBootExecCmdWithRetry (
                DataLen,\r
                Timeout\r
                );\r
-    if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED) {\r
+    if (Status == EFI_SUCCESS || Status == EFI_MEDIA_CHANGED || Status == EFI_NO_MEDIA) {\r
       break;\r
     }\r
     //\r
-    // If the device isn't ready, just wait for it without limit on retrial times.\r
+    // If the sense data shows the drive is not ready, we need execute the cmd again.\r
+    // We limit the upper boundary to 60 seconds.\r
     //\r
     if (Status == EFI_NOT_READY) {\r
-      Retry = 0;\r
+      continue;\r
+    }\r
+    //\r
+    // If the status is other error, then just retry 5 times.\r
+    //\r
+    if (Retry++ >= USB_BOOT_COMMAND_RETRY) {\r
+      break;\r
     }\r
   }\r
 \r
+EXIT:\r
+  if (TimeoutEvt != NULL) {\r
+    gBS->CloseEvent (TimeoutEvt);\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -285,7 +335,7 @@ UsbBootIsUnitReady (
   return UsbBootExecCmdWithRetry (\r
            UsbMass,\r
            &TestCmd,\r
-           sizeof (USB_BOOT_TEST_UNIT_READY_CMD),\r
+           (UINT8) sizeof (USB_BOOT_TEST_UNIT_READY_CMD),\r
            EfiUsbNoData,\r
            NULL,\r
            0,\r
@@ -310,25 +360,24 @@ UsbBootInquiry (
   )\r
 {\r
   USB_BOOT_INQUIRY_CMD        InquiryCmd;\r
-  USB_BOOT_INQUIRY_DATA       InquiryData;\r
   EFI_BLOCK_IO_MEDIA          *Media;\r
   EFI_STATUS                  Status;\r
 \r
   Media = &(UsbMass->BlockIoMedia);\r
 \r
   ZeroMem (&InquiryCmd, sizeof (USB_BOOT_INQUIRY_CMD));\r
-  ZeroMem (&InquiryData, sizeof (USB_BOOT_INQUIRY_DATA));\r
+  ZeroMem (&UsbMass->InquiryData, sizeof (USB_BOOT_INQUIRY_DATA));\r
 \r
   InquiryCmd.OpCode   = USB_BOOT_INQUIRY_OPCODE;\r
   InquiryCmd.Lun      = (UINT8) (USB_BOOT_LUN (UsbMass->Lun));\r
-  InquiryCmd.AllocLen = sizeof (InquiryData);\r
+  InquiryCmd.AllocLen = (UINT8) sizeof (USB_BOOT_INQUIRY_DATA);\r
 \r
   Status = UsbBootExecCmdWithRetry (\r
              UsbMass,\r
              &InquiryCmd,\r
-             sizeof (USB_BOOT_INQUIRY_CMD),\r
+             (UINT8) sizeof (USB_BOOT_INQUIRY_CMD),\r
              EfiUsbDataIn,\r
-             &InquiryData,\r
+             &UsbMass->InquiryData,\r
              sizeof (USB_BOOT_INQUIRY_DATA),\r
              USB_BOOT_GENERAL_CMD_TIMEOUT\r
              );\r
@@ -340,8 +389,8 @@ UsbBootInquiry (
   // Get information from PDT (Peripheral Device Type) field and Removable Medium Bit\r
   // from the inquiry data.\r
   //\r
-  UsbMass->Pdt          = (UINT8) (USB_BOOT_PDT (InquiryData.Pdt));\r
-  Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (InquiryData.Removable));\r
+  UsbMass->Pdt          = (UINT8) (USB_BOOT_PDT (UsbMass->InquiryData.Pdt));\r
+  Media->RemovableMedia = (BOOLEAN) (USB_BOOT_REMOVABLE (UsbMass->InquiryData.Removable));\r
   //\r
   // Set block size to the default value of 512 Bytes, in case no media is present at first time.\r
   //\r
@@ -350,6 +399,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
@@ -375,6 +505,7 @@ UsbBootReadCapacity (
   USB_BOOT_READ_CAPACITY_DATA CapacityData;\r
   EFI_BLOCK_IO_MEDIA          *Media;\r
   EFI_STATUS                  Status;\r
+  UINT32                      BlockSize;\r
 \r
   Media   = &UsbMass->BlockIoMedia;\r
 \r
@@ -387,7 +518,7 @@ UsbBootReadCapacity (
   Status = UsbBootExecCmdWithRetry (\r
              UsbMass,\r
              &CapacityCmd,\r
-             sizeof (USB_BOOT_READ_CAPACITY_CMD),\r
+             (UINT8) sizeof (USB_BOOT_READ_CAPACITY_CMD),\r
              EfiUsbDataIn,\r
              &CapacityData,\r
              sizeof (USB_BOOT_READ_CAPACITY_DATA),\r
@@ -403,16 +534,25 @@ UsbBootReadCapacity (
   //\r
   Media->MediaPresent = TRUE;\r
   Media->LastBlock    = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.LastLba));\r
-  Media->BlockSize    = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen));\r
 \r
-  if (Media->BlockSize == 0) {\r
-    return EFI_NOT_READY;\r
+  BlockSize           = SwapBytes32 (ReadUnaligned32 ((CONST UINT32 *) CapacityData.BlockLen));\r
+  if (BlockSize == 0) {\r
+    //\r
+    //  Get sense data  \r
+    //\r
+    return UsbBootRequestSense (UsbMass);\r
+  } else {\r
+    Media->BlockSize = BlockSize;\r
   }\r
 \r
-  DEBUG ((EFI_D_INFO, "UsbBootReadCapacity Success LBA=%ld BlockSize=%d\n",\r
-          Media->LastBlock, Media->BlockSize));\r
+  if (Media->LastBlock == 0xFFFFFFFF) {\r
+    Status = UsbBootReadCapacity16 (UsbMass);\r
+    if (!EFI_ERROR (Status)) {\r
+      UsbMass->Cdb16Byte = TRUE;\r
+    }\r
+  }\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -450,7 +590,7 @@ UsbScsiModeSense (
   Status = UsbBootExecCmdWithRetry (\r
              UsbMass,\r
              &ModeSenseCmd,\r
-             sizeof (USB_SCSI_MODE_SENSE6_CMD),\r
+             (UINT8) sizeof (USB_SCSI_MODE_SENSE6_CMD),\r
              EfiUsbDataIn,\r
              &ModeParaHeader,\r
              sizeof (USB_SCSI_MODE_SENSE6_PARA_HEADER),\r
@@ -503,6 +643,18 @@ UsbBootGetParams (
     return Status;\r
   }\r
 \r
+  //\r
+  // According to USB Mass Storage Specification for Bootability, only following\r
+  // 4 Peripheral Device Types are in spec.\r
+  //\r
+  if ((UsbMass->Pdt != USB_PDT_DIRECT_ACCESS) &&\r
+       (UsbMass->Pdt != USB_PDT_CDROM) &&\r
+       (UsbMass->Pdt != USB_PDT_OPTICAL) &&\r
+       (UsbMass->Pdt != USB_PDT_SIMPLE_DIRECT)) {\r
+    DEBUG ((EFI_D_ERROR, "UsbBootGetParams: Found an unsupported peripheral type[%d]\n", UsbMass->Pdt));\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   //\r
   // Don't use the Removable bit in inquiry data to test whether the media\r
   // is removable because many flash disks wrongly set this bit.\r
@@ -694,7 +846,7 @@ UsbBootReadBlocks (
     Status = UsbBootExecCmdWithRetry (\r
                UsbMass,\r
                &ReadCmd,\r
-               sizeof (USB_BOOT_READ10_CMD),\r
+               (UINT8) sizeof (USB_BOOT_READ10_CMD),\r
                EfiUsbDataIn,\r
                Buffer,\r
                ByteSize,\r
@@ -703,7 +855,7 @@ UsbBootReadBlocks (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\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
@@ -770,7 +922,7 @@ UsbBootWriteBlocks (
     Status = UsbBootExecCmdWithRetry (\r
                UsbMass,\r
                &WriteCmd,\r
-               sizeof (USB_BOOT_WRITE10_CMD),\r
+               (UINT8) sizeof (USB_BOOT_WRITE10_CMD),\r
                EfiUsbDataOut,\r
                Buffer,\r
                ByteSize,\r
@@ -779,7 +931,155 @@ UsbBootWriteBlocks (
     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
+    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