]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Ata/AtaBusDxe/AtaBus.c
AtaBusDxe: Fix ReadBlockEx andWriteBlockEx to still signal event when the BufferSize...
[mirror_edk2.git] / MdeModulePkg / Bus / Ata / AtaBusDxe / AtaBus.c
index 6b8257e1b6d42961ec674aa176c26b7cda67882d..26783169ea2cdafe7cbf0687387021d7b8debadf 100644 (file)
@@ -1,10 +1,10 @@
 /** @file\r
   This file implements protocol interfaces for ATA bus driver.\r
-  \r
+\r
   This file implements protocol interfaces: Driver Binding protocol,\r
   Block IO protocol and DiskInfo protocol.\r
-    \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+\r
+  Copyright (c) 2009 - 2014, 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,6 +18,8 @@
 \r
 #include "AtaBus.h"\r
 \r
+UINT8   mMorControl;\r
+\r
 //\r
 // ATA Bus Driver Binding Protocol Instance\r
 //\r
@@ -44,6 +46,13 @@ ATA_DEVICE gAtaDeviceTemplate = {
     AtaBlockIoWriteBlocks,\r
     AtaBlockIoFlushBlocks\r
   },\r
+  {                            // BlockIo2\r
+    NULL,\r
+    AtaBlockIoResetEx,\r
+    AtaBlockIoReadBlocksEx,\r
+    AtaBlockIoWriteBlocksEx,\r
+    AtaBlockIoFlushBlocksEx\r
+  },\r
   {                            // BlockMedia\r
     0,                         // MediaId\r
     FALSE,                     // RemovableMedia\r
@@ -51,7 +60,7 @@ ATA_DEVICE gAtaDeviceTemplate = {
     FALSE,                     // LogicPartition\r
     FALSE,                     // ReadOnly\r
     FALSE,                     // WritingCache\r
-    0x200,                     // BlockSize \r
+    0x200,                     // BlockSize\r
     0,                         // IoAlign\r
     0,                         // LastBlock\r
     0,                         // LowestAlignedLba\r
@@ -65,6 +74,10 @@ ATA_DEVICE gAtaDeviceTemplate = {
     AtaDiskInfoWhichIde\r
   },\r
   NULL,                        // DevicePath\r
+  {\r
+    AtaStorageSecurityReceiveData,\r
+    AtaStorageSecuritySendData\r
+  },\r
   NULL,                        // AtaBusDriverData\r
   0,                           // Port\r
   0,                           // PortMultiplierPort\r
@@ -75,7 +88,10 @@ ATA_DEVICE gAtaDeviceTemplate = {
   FALSE,                       // Lba48Bit\r
   NULL,                        // IdentifyData\r
   NULL,                        // ControllerNameTable\r
-  {L'\0', }                    // ModelName\r
+  {L'\0', },                   // ModelName\r
+  {NULL, NULL},                // AtaTaskList\r
+  {NULL, NULL},                // AtaSubTaskList\r
+  FALSE                        // Abort\r
 };\r
 \r
 /**\r
@@ -117,7 +133,7 @@ FreeAlignedBuffer (
   )\r
 {\r
   if (Buffer != NULL) {\r
-    FreePages (Buffer, EFI_SIZE_TO_PAGES (BufferSize));\r
+    FreeAlignedPages (Buffer, EFI_SIZE_TO_PAGES (BufferSize));\r
   }\r
 }\r
 \r
@@ -135,12 +151,50 @@ ReleaseAtaResources (
   IN ATA_DEVICE  *AtaDevice\r
   )\r
 {\r
+  ATA_BUS_ASYN_SUB_TASK *SubTask;\r
+  ATA_BUS_ASYN_TASK     *AtaTask;\r
+  LIST_ENTRY            *Entry;\r
+  LIST_ENTRY            *DelEntry;\r
+  EFI_TPL               OldTpl;\r
+\r
   FreeUnicodeStringTable (AtaDevice->ControllerNameTable);\r
-  FreeAlignedBuffer (AtaDevice->Asb, sizeof (*AtaDevice->Asb));\r
-  FreeAlignedBuffer (AtaDevice->IdentifyData, sizeof (*AtaDevice->IdentifyData));\r
+  FreeAlignedBuffer (AtaDevice->Asb, sizeof (EFI_ATA_STATUS_BLOCK));\r
+  FreeAlignedBuffer (AtaDevice->IdentifyData, sizeof (ATA_IDENTIFY_DATA));\r
   if (AtaDevice->DevicePath != NULL) {\r
     FreePool (AtaDevice->DevicePath);\r
   }\r
+  OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+  if (!IsListEmpty (&AtaDevice->AtaSubTaskList)) {\r
+    //\r
+    // Free the Subtask list.\r
+    //\r
+    for(Entry = AtaDevice->AtaSubTaskList.ForwardLink;\r
+        Entry != (&AtaDevice->AtaSubTaskList);\r
+       ) {\r
+      DelEntry = Entry;\r
+      Entry    = Entry->ForwardLink;\r
+      SubTask  = ATA_ASYN_SUB_TASK_FROM_ENTRY (DelEntry);\r
+\r
+      RemoveEntryList (DelEntry);\r
+      FreeAtaSubTask (SubTask);\r
+    }\r
+  }\r
+  if (!IsListEmpty (&AtaDevice->AtaTaskList)) {\r
+    //\r
+    // Free the Subtask list.\r
+    //\r
+    for(Entry = AtaDevice->AtaTaskList.ForwardLink;\r
+        Entry != (&AtaDevice->AtaTaskList);\r
+       ) {\r
+      DelEntry = Entry;\r
+      Entry    = Entry->ForwardLink;\r
+      AtaTask  = ATA_ASYN_TASK_FROM_ENTRY (DelEntry);\r
+\r
+      RemoveEntryList (DelEntry);\r
+      FreePool (AtaTask);\r
+    }\r
+  }\r
+  gBS->RestoreTPL (OldTpl);\r
   FreePool (AtaDevice);\r
 }\r
 \r
@@ -149,7 +203,7 @@ ReleaseAtaResources (
   Registers an ATA device.\r
 \r
   This function allocates an ATA device structure for the ATA device specified by\r
-  Port and PortMultiplierPort if the ATA device is identified as a valid one. \r
+  Port and PortMultiplierPort if the ATA device is identified as a valid one.\r
   Then it will create child handle and install Block IO and Disk Info protocol on\r
   it.\r
 \r
@@ -183,7 +237,7 @@ RegisterAtaDevice (
   RemainingDevicePath = NULL;\r
 \r
   //\r
-  // Build device path \r
+  // Build device path\r
   //\r
   AtaPassThru = AtaBusDriverData->AtaPassThru;\r
   Status = AtaPassThru->BuildDevicePath (AtaPassThru, Port, PortMultiplierPort, &NewDevicePathNode);\r
@@ -209,7 +263,7 @@ RegisterAtaDevice (
   //\r
   // Allocate ATA device from the template.\r
   //\r
-  AtaDevice = AllocateCopyPool (sizeof (gAtaDeviceTemplate), &gAtaDeviceTemplate);\r
+  AtaDevice = AllocateCopyPool (sizeof (ATA_DEVICE), &gAtaDeviceTemplate);\r
   if (AtaDevice == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
@@ -218,30 +272,46 @@ RegisterAtaDevice (
   //\r
   // Initializes ATA device structures and allocates the required buffer.\r
   //\r
-  AtaDevice->BlockIo.Media = &AtaDevice->BlockMedia;\r
-  AtaDevice->AtaBusDriverData = AtaBusDriverData;\r
-  AtaDevice->DevicePath = DevicePath;\r
-  AtaDevice->Port = Port;\r
+  AtaDevice->BlockIo.Media      = &AtaDevice->BlockMedia;\r
+  AtaDevice->BlockIo2.Media     = &AtaDevice->BlockMedia;\r
+  AtaDevice->AtaBusDriverData   = AtaBusDriverData;\r
+  AtaDevice->DevicePath         = DevicePath;\r
+  AtaDevice->Port               = Port;\r
   AtaDevice->PortMultiplierPort = PortMultiplierPort;\r
-  AtaDevice->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (*AtaDevice->Asb));\r
+  AtaDevice->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (EFI_ATA_STATUS_BLOCK));\r
   if (AtaDevice->Asb == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
   }\r
-  AtaDevice->IdentifyData = AllocateAlignedBuffer (AtaDevice, sizeof (*AtaDevice->IdentifyData));\r
+  AtaDevice->IdentifyData = AllocateAlignedBuffer (AtaDevice, sizeof (ATA_IDENTIFY_DATA));\r
   if (AtaDevice->IdentifyData == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
   }\r
 \r
   //\r
-  // Try to identify the ATA device via the ATA pass through command. \r
+  // Initial Ata Task List\r
+  //\r
+  InitializeListHead (&AtaDevice->AtaTaskList);\r
+  InitializeListHead (&AtaDevice->AtaSubTaskList);\r
+\r
+  //\r
+  // Report Status Code to indicate the ATA device will be enabled\r
+  //\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+    EFI_PROGRESS_CODE,\r
+    (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_ENABLE),\r
+    AtaBusDriverData->ParentDevicePath\r
+    );\r
+\r
+  //\r
+  // Try to identify the ATA device via the ATA pass through command.\r
   //\r
   Status = DiscoverAtaDevice (AtaDevice);\r
   if (EFI_ERROR (Status)) {\r
     goto Done;\r
   }\r
-  \r
+\r
   //\r
   // Build controller name for Component Name (2) protocol.\r
   //\r
@@ -281,6 +351,8 @@ RegisterAtaDevice (
                   AtaDevice->DevicePath,\r
                   &gEfiBlockIoProtocolGuid,\r
                   &AtaDevice->BlockIo,\r
+                  &gEfiBlockIo2ProtocolGuid,\r
+                  &AtaDevice->BlockIo2,\r
                   &gEfiDiskInfoProtocolGuid,\r
                   &AtaDevice->DiskInfo,\r
                   NULL\r
@@ -289,6 +361,35 @@ RegisterAtaDevice (
     goto Done;\r
   }\r
 \r
+  //\r
+  // See if the ata device support trust computing feature or not.\r
+  // If yes, then install Storage Security Protocol at the ata device handle.\r
+  //\r
+  if ((AtaDevice->IdentifyData->trusted_computing_support & BIT0) != 0) {\r
+    DEBUG ((EFI_D_INFO, "Found TCG support in Port %x PortMultiplierPort %x\n", Port, PortMultiplierPort));\r
+    Status = gBS->InstallProtocolInterface (\r
+                    &AtaDevice->Handle,\r
+                    &gEfiStorageSecurityCommandProtocolGuid,\r
+                    EFI_NATIVE_INTERFACE,\r
+                    &AtaDevice->StorageSecurity\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
+    }\r
+    DEBUG ((EFI_D_INFO, "Successfully Install Storage Security Protocol on the ATA device\n"));\r
+  }\r
+\r
+\r
+  if (((mMorControl & 0x01) == 0x01) && ((AtaDevice->IdentifyData->trusted_computing_support & BIT0) != 0)) {\r
+    DEBUG ((EFI_D_INFO,\r
+            "mMorControl = %x, AtaDevice->IdentifyData->trusted_computing_support & BIT0 = %x\n",\r
+            mMorControl,\r
+            (AtaDevice->IdentifyData->trusted_computing_support & BIT0)\r
+            ));\r
+    DEBUG ((EFI_D_INFO, "Try to lock device by sending TPer Reset command...\n"));\r
+    InitiateTPerReset(AtaDevice);\r
+  }\r
+\r
   gBS->OpenProtocol (\r
          AtaBusDriverData->Controller,\r
          &gEfiAtaPassThruProtocolGuid,\r
@@ -304,8 +405,8 @@ Done:
   }\r
 \r
   if (EFI_ERROR (Status) && (AtaDevice != NULL)) {\r
-    ReleaseAtaResources (AtaDevice);  \r
-    DEBUG ((DEBUG_ERROR | DEBUG_INIT, "Failed to initialize Port %x PortMultiplierPort %x, status = %r\n", Port, PortMultiplierPort, Status));\r
+    ReleaseAtaResources (AtaDevice);\r
+    DEBUG ((EFI_D_ERROR | EFI_D_INIT, "Failed to initialize Port %x PortMultiplierPort %x, status = %r\n", Port, PortMultiplierPort, Status));\r
   }\r
   return Status;\r
 }\r
@@ -314,8 +415,8 @@ Done:
 /**\r
   Unregisters an ATA device.\r
 \r
-  This function removes the protocols installed on the controller handle and \r
-  frees the resources allocated for the ATA device. \r
+  This function removes the protocols installed on the controller handle and\r
+  frees the resources allocated for the ATA device.\r
 \r
   @param  This                  The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.\r
   @param  Controller            The controller handle of the ATA device.\r
@@ -332,10 +433,15 @@ UnregisterAtaDevice (
   IN  EFI_HANDLE                     Handle\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
-  EFI_BLOCK_IO_PROTOCOL       *BlockIo;\r
-  ATA_DEVICE                  *AtaDevice;\r
-  EFI_ATA_PASS_THRU_PROTOCOL  *AtaPassThru;\r
+  EFI_STATUS                               Status;\r
+  EFI_BLOCK_IO_PROTOCOL                    *BlockIo;\r
+  EFI_BLOCK_IO2_PROTOCOL                   *BlockIo2;\r
+  ATA_DEVICE                               *AtaDevice;\r
+  EFI_ATA_PASS_THRU_PROTOCOL               *AtaPassThru;\r
+  EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    *StorageSecurity;\r
+\r
+  BlockIo2             =     NULL;\r
+  BlockIo              =     NULL;\r
 \r
   Status = gBS->OpenProtocol (\r
                   Handle,\r
@@ -346,10 +452,31 @@ UnregisterAtaDevice (
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    //\r
+    // Locate BlockIo2 protocol\r
+    //\r
+    Status = gBS->OpenProtocol (\r
+                    Handle,\r
+                    &gEfiBlockIo2ProtocolGuid,\r
+                    (VOID **) &BlockIo2,\r
+                    This->DriverBindingHandle,\r
+                    Controller,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
   }\r
 \r
-  AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (BlockIo);\r
+  //\r
+  // Get AtaDevice data.\r
+  //\r
+  if (BlockIo != NULL) {\r
+    AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (BlockIo);\r
+  } else {\r
+    ASSERT (BlockIo2 != NULL);\r
+    AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (BlockIo2);\r
+  }\r
 \r
   //\r
   // Close the child handle\r
@@ -361,12 +488,18 @@ UnregisterAtaDevice (
          Handle\r
          );\r
 \r
+  //\r
+  // The Ata Bus driver installs the BlockIo and BlockIo2 in the DriverBindingStart().\r
+  // Here should uninstall both of them.\r
+  //\r
   Status = gBS->UninstallMultipleProtocolInterfaces (\r
                   Handle,\r
                   &gEfiDevicePathProtocolGuid,\r
                   AtaDevice->DevicePath,\r
                   &gEfiBlockIoProtocolGuid,\r
                   &AtaDevice->BlockIo,\r
+                  &gEfiBlockIo2ProtocolGuid,\r
+                  &AtaDevice->BlockIo2,\r
                   &gEfiDiskInfoProtocolGuid,\r
                   &AtaDevice->DiskInfo,\r
                   NULL\r
@@ -384,41 +517,71 @@ UnregisterAtaDevice (
     return Status;\r
   }\r
 \r
-  ReleaseAtaResources (AtaDevice);\r
+  //\r
+  // If Storage Security Command Protocol is installed, then uninstall this protocol.\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  Handle,\r
+                  &gEfiStorageSecurityCommandProtocolGuid,\r
+                  (VOID **) &StorageSecurity,\r
+                  This->DriverBindingHandle,\r
+                  Controller,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
 \r
-  return Status;\r
+  if (!EFI_ERROR (Status)) {\r
+    Status = gBS->UninstallProtocolInterface (\r
+                    Handle,\r
+                    &gEfiStorageSecurityCommandProtocolGuid,\r
+                    &AtaDevice->StorageSecurity\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->OpenProtocol (\r
+        Controller,\r
+        &gEfiAtaPassThruProtocolGuid,\r
+        (VOID **) &AtaPassThru,\r
+        This->DriverBindingHandle,\r
+        Handle,\r
+        EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+        );\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  ReleaseAtaResources (AtaDevice);\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
 \r
 /**\r
-  Tests to see if this driver supports a given controller. If a child device is provided, \r
+  Tests to see if this driver supports a given controller. If a child device is provided,\r
   it further tests to see if this driver supports creating a handle for the specified child device.\r
 \r
-  This function checks to see if the driver specified by This supports the device specified by \r
-  ControllerHandle. Drivers will typically use the device path attached to \r
-  ControllerHandle and/or the services from the bus I/O abstraction attached to \r
-  ControllerHandle to determine if the driver supports ControllerHandle. This function \r
-  may be called many times during platform initialization. In order to reduce boot times, the tests \r
-  performed by this function must be very small, and take as little time as possible to execute. This \r
-  function must not change the state of any hardware devices, and this function must be aware that the \r
-  device specified by ControllerHandle may already be managed by the same driver or a \r
-  different driver. This function must match its calls to AllocatePages() with FreePages(), \r
-  AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().  \r
-  Since ControllerHandle may have been previously started by the same driver, if a protocol is \r
-  already in the opened state, then it must not be closed with CloseProtocol(). This is required \r
+  This function checks to see if the driver specified by This supports the device specified by\r
+  ControllerHandle. Drivers will typically use the device path attached to\r
+  ControllerHandle and/or the services from the bus I/O abstraction attached to\r
+  ControllerHandle to determine if the driver supports ControllerHandle. This function\r
+  may be called many times during platform initialization. In order to reduce boot times, the tests\r
+  performed by this function must be very small, and take as little time as possible to execute. This\r
+  function must not change the state of any hardware devices, and this function must be aware that the\r
+  device specified by ControllerHandle may already be managed by the same driver or a\r
+  different driver. This function must match its calls to AllocatePages() with FreePages(),\r
+  AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
+  Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
+  already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
   to guarantee the state of ControllerHandle is not modified by this function.\r
 \r
   @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
-  @param[in]  ControllerHandle     The handle of the controller to test. This handle \r
-                                   must support a protocol interface that supplies \r
+  @param[in]  ControllerHandle     The handle of the controller to test. This handle\r
+                                   must support a protocol interface that supplies\r
                                    an I/O abstraction to the driver.\r
-  @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This \r
-                                   parameter is ignored by device drivers, and is optional for bus \r
-                                   drivers. For bus drivers, if this parameter is not NULL, then \r
-                                   the bus driver must determine if the bus controller specified \r
-                                   by ControllerHandle and the child controller specified \r
-                                   by RemainingDevicePath are both supported by this \r
+  @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This\r
+                                   parameter is ignored by device drivers, and is optional for bus\r
+                                   drivers. For bus drivers, if this parameter is not NULL, then\r
+                                   the bus driver must determine if the bus controller specified\r
+                                   by ControllerHandle and the child controller specified\r
+                                   by RemainingDevicePath are both supported by this\r
                                    bus driver.\r
 \r
   @retval EFI_SUCCESS              The device specified by ControllerHandle and\r
@@ -446,7 +609,7 @@ AtaBusDriverBindingSupported (
   EFI_ATA_PASS_THRU_PROTOCOL        *AtaPassThru;\r
   UINT16                            Port;\r
   UINT16                            PortMultiplierPort;\r
\r
+\r
   //\r
   // Test EFI_ATA_PASS_THRU_PROTOCOL on controller handle.\r
   //\r
@@ -467,12 +630,37 @@ AtaBusDriverBindingSupported (
     return Status;\r
   }\r
 \r
+  //\r
+  // Test to see if this ATA Pass Thru Protocol is for a LOGICAL channel\r
+  //\r
+  if ((AtaPassThru->Mode->Attributes & EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL) == 0) {\r
+    //\r
+    // Close the I/O Abstraction(s) used to perform the supported test\r
+    //\r
+    gBS->CloseProtocol (\r
+          Controller,\r
+          &gEfiAtaPassThruProtocolGuid,\r
+          This->DriverBindingHandle,\r
+          Controller\r
+          );\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   //\r
   // Test RemainingDevicePath is valid or not.\r
   //\r
   if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {\r
     Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);\r
     if (EFI_ERROR (Status)) {\r
+      //\r
+      // Close the I/O Abstraction(s) used to perform the supported test\r
+      //\r
+      gBS->CloseProtocol (\r
+            Controller,\r
+            &gEfiAtaPassThruProtocolGuid,\r
+            This->DriverBindingHandle,\r
+            Controller\r
+            );\r
       return Status;\r
     }\r
   }\r
@@ -506,28 +694,28 @@ AtaBusDriverBindingSupported (
   Starts a device controller or a bus controller.\r
 \r
   The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
-  As a result, much of the error checking on the parameters to Start() has been moved into this \r
-  common boot service. It is legal to call Start() from other locations, \r
+  As a result, much of the error checking on the parameters to Start() has been moved into this\r
+  common boot service. It is legal to call Start() from other locations,\r
   but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
   1. ControllerHandle must be a valid EFI_HANDLE.\r
   2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
      EFI_DEVICE_PATH_PROTOCOL.\r
   3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
-     have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.  \r
+     have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
 \r
   @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
-  @param[in]  ControllerHandle     The handle of the controller to start. This handle \r
-                                   must support a protocol interface that supplies \r
+  @param[in]  ControllerHandle     The handle of the controller to start. This handle\r
+                                   must support a protocol interface that supplies\r
                                    an I/O abstraction to the driver.\r
-  @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This \r
-                                   parameter is ignored by device drivers, and is optional for bus \r
-                                   drivers. For a bus driver, if this parameter is NULL, then handles \r
-                                   for all the children of Controller are created by this driver.  \r
-                                   If this parameter is not NULL and the first Device Path Node is \r
-                                   not the End of Device Path Node, then only the handle for the \r
-                                   child device specified by the first Device Path Node of \r
+  @param[in]  RemainingDevicePath  A pointer to the remaining portion of a device path.  This\r
+                                   parameter is ignored by device drivers, and is optional for bus\r
+                                   drivers. For a bus driver, if this parameter is NULL, then handles\r
+                                   for all the children of Controller are created by this driver.\r
+                                   If this parameter is not NULL and the first Device Path Node is\r
+                                   not the End of Device Path Node, then only the handle for the\r
+                                   child device specified by the first Device Path Node of\r
                                    RemainingDevicePath is created by this driver.\r
-                                   If the first Device Path Node of RemainingDevicePath is \r
+                                   If the first Device Path Node of RemainingDevicePath is\r
                                    the End of Device Path Node, no child handle is created by this\r
                                    driver.\r
 \r
@@ -566,6 +754,15 @@ AtaBusDriverBindingStart (
     return Status;\r
   }\r
 \r
+  //\r
+  // Report Status Code to indicate ATA bus starts\r
+  //\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+    EFI_PROGRESS_CODE,\r
+    (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_INIT),\r
+    ParentDevicePath\r
+    );\r
+\r
   Status = gBS->OpenProtocol (\r
                   Controller,\r
                   &gEfiAtaPassThruProtocolGuid,\r
@@ -589,7 +786,7 @@ AtaBusDriverBindingStart (
     }\r
 \r
     AtaBusDriverData->AtaPassThru = AtaPassThru;\r
-    AtaBusDriverData->Controller = Controller;\r
+    AtaBusDriverData->Controller  = Controller;\r
     AtaBusDriverData->ParentDevicePath = ParentDevicePath;\r
     AtaBusDriverData->DriverBindingHandle = This->DriverBindingHandle;\r
 \r
@@ -618,6 +815,15 @@ AtaBusDriverBindingStart (
     }\r
   }\r
 \r
+  //\r
+  // Report Status Code to indicate detecting devices on bus\r
+  //\r
+  REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+    EFI_PROGRESS_CODE,\r
+    (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_DETECT),\r
+    ParentDevicePath\r
+    );\r
+\r
   if (RemainingDevicePath == NULL) {\r
     Port = 0xFFFF;\r
     while (TRUE) {\r
@@ -628,7 +834,7 @@ AtaBusDriverBindingStart (
         //\r
         break;\r
       }\r
-      \r
+\r
       PortMultiplierPort = 0xFFFF;\r
       while (TRUE) {\r
         Status = AtaPassThru->GetNextDevice (AtaPassThru, Port, &PortMultiplierPort);\r
@@ -649,7 +855,7 @@ AtaBusDriverBindingStart (
       Status = RegisterAtaDevice (AtaBusDriverData,Port, PortMultiplierPort);\r
     }\r
   }\r
-  \r
+\r
   return Status;\r
 \r
 ErrorExit:\r
@@ -678,10 +884,10 @@ ErrorExit:
 \r
 /**\r
   Stops a device controller or a bus controller.\r
-  \r
-  The Stop() function is designed to be invoked from the EFI boot service DisconnectController(). \r
-  As a result, much of the error checking on the parameters to Stop() has been moved \r
-  into this common boot service. It is legal to call Stop() from other locations, \r
+\r
+  The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
+  As a result, much of the error checking on the parameters to Stop() has been moved\r
+  into this common boot service. It is legal to call Stop() from other locations,\r
   but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
   1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
      same driver's Start() function.\r
@@ -689,13 +895,13 @@ ErrorExit:
      EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
      Start() function, and the Start() function must have called OpenProtocol() on\r
      ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
-  \r
+\r
   @param[in]  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
-  @param[in]  ControllerHandle  A handle to the device being stopped. The handle must \r
-                                support a bus specific I/O protocol for the driver \r
+  @param[in]  ControllerHandle  A handle to the device being stopped. The handle must\r
+                                support a bus specific I/O protocol for the driver\r
                                 to use to stop the device.\r
   @param[in]  NumberOfChildren  The number of child device handles in ChildHandleBuffer.\r
-  @param[in]  ChildHandleBuffer An array of child handles to be freed. May be NULL \r
+  @param[in]  ChildHandleBuffer An array of child handles to be freed. May be NULL\r
                                 if NumberOfChildren is 0.\r
 \r
   @retval EFI_SUCCESS           The device was stopped.\r
@@ -789,7 +995,7 @@ AtaBlockIoReset (
 \r
   AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);\r
 \r
-  Status = ResetAtaDevice (AtaDevice);  \r
+  Status = ResetAtaDevice (AtaDevice);\r
 \r
   if (EFI_ERROR (Status)) {\r
     Status = EFI_DEVICE_ERROR;\r
@@ -803,13 +1009,18 @@ AtaBlockIoReset (
 /**\r
   Read/Write BufferSize bytes from Lba from/into Buffer.\r
 \r
-  @param  This       Indicates a pointer to the calling context.\r
-  @param  MediaId    The media ID that the read/write request is for.\r
-  @param  Lba        The starting logical block address to be read/written. The caller is\r
-                     responsible for reading/writing to only legitimate locations.\r
-  @param  BufferSize Size of Buffer, must be a multiple of device block size.\r
-  @param  Buffer     A pointer to the destination/source buffer for the data.\r
-  @param  IsWrite    Indicates whether it is a write operation.\r
+  @param[in]       This       Indicates a pointer to the calling context. Either be\r
+                              block I/O or block I/O2.\r
+  @param[in]       MediaId    The media ID that the read/write request is for.\r
+  @param[in]       Lba        The starting logical block address to be read/written.\r
+                              The caller is responsible for reading/writing to only\r
+                              legitimate locations.\r
+  @param[in, out]  Token      A pointer to the token associated with the transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.\r
+  @param[out]      Buffer     A pointer to the destination/source buffer for the data.\r
+  @param[in]       IsBlockIo2 Indicate the calling is from BlockIO or BlockIO2. TURE is\r
+                              from BlockIO2, FALSE is for BlockIO.\r
+  @param[in]       IsWrite    Indicates whether it is a write operation.\r
 \r
   @retval EFI_SUCCESS           The data was read/written correctly to the device.\r
   @retval EFI_WRITE_PROTECTED   The device can not be read/written to.\r
@@ -817,18 +1028,20 @@ AtaBlockIoReset (
   @retval EFI_NO_MEDIA          There is no media in the device.\r
   @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.\r
   @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.\r
-  @retval EFI_INVALID_PARAMETER The read/write request contains LBAs that are not valid, \r
+  @retval EFI_INVALID_PARAMETER The read/write request contains LBAs that are not valid,\r
                                 or the buffer is not on proper alignment.\r
 \r
 **/\r
 EFI_STATUS\r
 BlockIoReadWrite (\r
-  IN  EFI_BLOCK_IO_PROTOCOL   *This,\r
-  IN  UINT32                  MediaId,\r
-  IN  EFI_LBA                 Lba,\r
-  IN  UINTN                   BufferSize,\r
-  OUT VOID                    *Buffer,\r
-  IN  BOOLEAN                 IsWrite\r
+  IN     VOID                    *This,\r
+  IN     UINT32                  MediaId,\r
+  IN     EFI_LBA                 Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN     *Token,\r
+  IN     UINTN                   BufferSize,\r
+  OUT    VOID                    *Buffer,\r
+  IN     BOOLEAN                 IsBlockIo2,\r
+  IN     BOOLEAN                 IsWrite\r
   )\r
 {\r
   ATA_DEVICE                        *AtaDevice;\r
@@ -839,6 +1052,18 @@ BlockIoReadWrite (
   UINTN                             NumberOfBlocks;\r
   UINTN                             IoAlign;\r
 \r
+  if (IsBlockIo2) {\r
+   Media     = ((EFI_BLOCK_IO2_PROTOCOL *) This)->Media;\r
+   AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);\r
+  } else {\r
+   Media     = ((EFI_BLOCK_IO_PROTOCOL *) This)->Media;\r
+   AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);\r
+  }\r
+\r
+  if (MediaId != Media->MediaId) {\r
+    return EFI_MEDIA_CHANGED;\r
+  }\r
+\r
   //\r
   // Check parameters.\r
   //\r
@@ -847,19 +1072,18 @@ BlockIoReadWrite (
   }\r
 \r
   if (BufferSize == 0) {\r
+    if ((Token != NULL) && (Token->Event != NULL)) {\r
+      Token->TransactionStatus = EFI_SUCCESS;\r
+      gBS->SignalEvent (Token->Event);\r
+    }\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  Media = This->Media;\r
-  if (MediaId != Media->MediaId) {\r
-    return EFI_MEDIA_CHANGED;\r
-  }\r
-\r
   BlockSize = Media->BlockSize;\r
   if ((BufferSize % BlockSize) != 0) {\r
     return EFI_BAD_BUFFER_SIZE;\r
   }\r
-  \r
+\r
   NumberOfBlocks  = BufferSize / BlockSize;\r
   if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -872,13 +1096,11 @@ BlockIoReadWrite (
 \r
   OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
 \r
-  AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);\r
-  \r
   //\r
   // Invoke low level AtaDevice Access Routine.\r
   //\r
-  Status = AccessAtaDevice (AtaDevice, Buffer, Lba, NumberOfBlocks, IsWrite);\r
\r
+  Status = AccessAtaDevice (AtaDevice, Buffer, Lba, NumberOfBlocks, IsWrite, Token);\r
+\r
   gBS->RestoreTPL (OldTpl);\r
 \r
   return Status;\r
@@ -900,7 +1122,7 @@ BlockIoReadWrite (
   @retval EFI_NO_MEDIA          There is no media in the device.\r
   @retval EFI_MEDIA_CHANGED     The MediaId does not matched the current device.\r
   @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.\r
-  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid, \r
+  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
                                 or the buffer is not on proper alignment.\r
 \r
 **/\r
@@ -914,7 +1136,7 @@ AtaBlockIoReadBlocks (
   OUT VOID                    *Buffer\r
   )\r
 {\r
-  return BlockIoReadWrite (This, MediaId, Lba, BufferSize, Buffer, FALSE);\r
+  return BlockIoReadWrite ((VOID *) This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, FALSE);\r
 }\r
 \r
 \r
@@ -934,7 +1156,7 @@ AtaBlockIoReadBlocks (
   @retval EFI_NO_MEDIA          There is no media in the device.\r
   @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.\r
   @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.\r
-  @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid, \r
+  @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
                                 or the buffer is not on proper alignment.\r
 \r
 **/\r
@@ -948,7 +1170,7 @@ AtaBlockIoWriteBlocks (
   IN  VOID                    *Buffer\r
   )\r
 {\r
-  return BlockIoReadWrite (This, MediaId, Lba, BufferSize, Buffer, TRUE);\r
+  return BlockIoReadWrite ((VOID *) This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, TRUE);\r
 }\r
 \r
 \r
@@ -974,10 +1196,152 @@ AtaBlockIoFlushBlocks (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Reset the Block Device.\r
+\r
+  @param[in]  This                 Indicates a pointer to the calling context.\r
+  @param[in]  ExtendedVerification Driver may perform diagnostics on reset.\r
+\r
+  @retval EFI_SUCCESS          The device was reset.\r
+  @retval EFI_DEVICE_ERROR     The device is not functioning properly and could\r
+                               not be reset.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaBlockIoResetEx (\r
+  IN  EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN  BOOLEAN                 ExtendedVerification\r
+  )\r
+{\r
+  EFI_STATUS      Status;\r
+  ATA_DEVICE      *AtaDevice;\r
+  EFI_TPL         OldTpl;\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
+  AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);\r
+\r
+  AtaTerminateNonBlockingTask (AtaDevice);\r
+\r
+  Status = ResetAtaDevice (AtaDevice);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Read BufferSize bytes from Lba into Buffer.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in]       MediaId    Id of the media, changes every time the media is replaced.\r
+  @param[in]       Lba        The starting Logical Block Address to read from.\r
+  @param[in, out]  Token      A pointer to the token associated with the transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.\r
+  @param[out]      Buffer     A pointer to the destination buffer for the data. The caller is\r
+                              responsible for either having implicit or explicit ownership of the buffer.\r
+\r
+  @retval EFI_SUCCESS           The read request was queued if Event is not NULL.\r
+                                The data was read correctly from the device if\r
+                                the Event is NULL.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing\r
+                                the read.\r
+  @retval EFI_NO_MEDIA          There is no media in the device.\r
+  @retval EFI_MEDIA_CHANGED     The MediaId is not for the current media.\r
+  @retval EFI_BAD_BUFFER_SIZE   The BufferSize parameter is not a multiple of the\r
+                                intrinsic block size of the device.\r
+  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
+                                or the buffer is not on proper alignment.\r
+  @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a lack\r
+                                of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaBlockIoReadBlocksEx (\r
+  IN  EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN  UINT32                  MediaId,\r
+  IN  EFI_LBA                 Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN  *Token,\r
+  IN  UINTN                   BufferSize,\r
+  OUT VOID                    *Buffer\r
+  )\r
+{\r
+  return BlockIoReadWrite ((VOID *) This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, FALSE);\r
+}\r
+\r
+\r
+/**\r
+  Write BufferSize bytes from Lba into Buffer.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in]       MediaId    The media ID that the write request is for.\r
+  @param[in]       Lba        The starting logical block address to be written. The\r
+                              caller is responsible for writing to only legitimate\r
+                              locations.\r
+  @param[in, out]  Token      A pointer to the token associated with the transaction.\r
+  @param[in]       BufferSize Size of Buffer, must be a multiple of device block size.\r
+  @param[in]       Buffer     A pointer to the source buffer for the data.\r
+\r
+  @retval EFI_SUCCESS           The data was written correctly to the device.\r
+  @retval EFI_WRITE_PROTECTED   The device can not be written to.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error while performing the write.\r
+  @retval EFI_NO_MEDIA          There is no media in the device.\r
+  @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.\r
+  @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.\r
+  @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
+                                or the buffer is not on proper alignment.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaBlockIoWriteBlocksEx (\r
+  IN  EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN  UINT32                  MediaId,\r
+  IN  EFI_LBA                 Lba,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN  *Token,\r
+  IN  UINTN                   BufferSize,\r
+  IN  VOID                    *Buffer\r
+  )\r
+{\r
+  return BlockIoReadWrite ((VOID *) This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, TRUE);\r
+}\r
+\r
+\r
+/**\r
+  Flush the Block Device.\r
+\r
+  @param[in]       This       Indicates a pointer to the calling context.\r
+  @param[in, out]  Token      A pointer to the token associated with the transaction.\r
+\r
+  @retval EFI_SUCCESS       All outstanding data was written to the device\r
+  @retval EFI_DEVICE_ERROR  The device reported an error while writing back the data\r
+  @retval EFI_NO_MEDIA      There is no media in the device.\r
 \r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaBlockIoFlushBlocksEx (\r
+  IN  EFI_BLOCK_IO2_PROTOCOL  *This,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN  *Token\r
+  )\r
+{\r
+  //\r
+  // Signal event and return directly.\r
+  //\r
+  if (Token != NULL && Token->Event != NULL) {\r
+    Token->TransactionStatus = EFI_SUCCESS;\r
+    gBS->SignalEvent (Token->Event);\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
 /**\r
   Provides inquiry information for the controller type.\r
-  \r
+\r
   This function is used by the IDE bus driver to get inquiry data.  Data format\r
   of Identify data is defined by the Interface GUID.\r
 \r
@@ -986,9 +1350,9 @@ AtaBlockIoFlushBlocks (
   @param[in, out] InquiryDataSize  Pointer to the value for the inquiry data size.\r
 \r
   @retval EFI_SUCCESS            The command was accepted without any errors.\r
-  @retval EFI_NOT_FOUND          Device does not support this data class \r
-  @retval EFI_DEVICE_ERROR       Error reading InquiryData from device \r
-  @retval EFI_BUFFER_TOO_SMALL   InquiryDataSize not big enough \r
+  @retval EFI_NOT_FOUND          Device does not support this data class\r
+  @retval EFI_DEVICE_ERROR       Error reading InquiryData from device\r
+  @retval EFI_BUFFER_TOO_SMALL   InquiryDataSize not big enough\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1009,16 +1373,16 @@ AtaDiskInfoInquiry (
   This function is used by the IDE bus driver to get identify data.  Data format\r
   of Identify data is defined by the Interface GUID.\r
 \r
-  @param[in]      This              Pointer to the EFI_DISK_INFO_PROTOCOL \r
+  @param[in]      This              Pointer to the EFI_DISK_INFO_PROTOCOL\r
                                     instance.\r
   @param[in, out] IdentifyData      Pointer to a buffer for the identify data.\r
   @param[in, out] IdentifyDataSize  Pointer to the value for the identify data\r
                                     size.\r
 \r
   @retval EFI_SUCCESS            The command was accepted without any errors.\r
-  @retval EFI_NOT_FOUND          Device does not support this data class \r
-  @retval EFI_DEVICE_ERROR       Error reading IdentifyData from device \r
-  @retval EFI_BUFFER_TOO_SMALL   IdentifyDataSize not big enough \r
+  @retval EFI_NOT_FOUND          Device does not support this data class\r
+  @retval EFI_DEVICE_ERROR       Error reading IdentifyData from device\r
+  @retval EFI_BUFFER_TOO_SMALL   IdentifyDataSize not big enough\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1035,11 +1399,11 @@ AtaDiskInfoIdentify (
   AtaDevice = ATA_DEVICE_FROM_DISK_INFO (This);\r
 \r
   Status = EFI_BUFFER_TOO_SMALL;\r
-  if (*IdentifyDataSize >= sizeof (*AtaDevice->IdentifyData)) {\r
+  if (*IdentifyDataSize >= sizeof (ATA_IDENTIFY_DATA)) {\r
     Status = EFI_SUCCESS;\r
-    CopyMem (IdentifyData, AtaDevice->IdentifyData, sizeof (*AtaDevice->IdentifyData));\r
+    CopyMem (IdentifyData, AtaDevice->IdentifyData, sizeof (ATA_IDENTIFY_DATA));\r
   }\r
-  *IdentifyDataSize = sizeof (*AtaDevice->IdentifyData);\r
+  *IdentifyDataSize = sizeof (ATA_IDENTIFY_DATA);\r
 \r
   return Status;\r
 }\r
@@ -1047,8 +1411,8 @@ AtaDiskInfoIdentify (
 \r
 /**\r
   Provides sense data information for the controller type.\r
-  \r
-  This function is used by the IDE bus driver to get sense data. \r
+\r
+  This function is used by the IDE bus driver to get sense data.\r
   Data format of Sense data is defined by the Interface GUID.\r
 \r
   @param[in]      This             Pointer to the EFI_DISK_INFO_PROTOCOL instance.\r
@@ -1078,7 +1442,7 @@ AtaDiskInfoSenseData (
 /**\r
   This function is used by the IDE bus driver to get controller information.\r
 \r
-  @param[in]  This         Pointer to the EFI_DISK_INFO_PROTOCOL instance. \r
+  @param[in]  This         Pointer to the EFI_DISK_INFO_PROTOCOL instance.\r
   @param[out] IdeChannel   Pointer to the Ide Channel number.  Primary or secondary.\r
   @param[out] IdeDevice    Pointer to the Ide Device number.  Master or slave.\r
 \r
@@ -1103,6 +1467,232 @@ AtaDiskInfoWhichIde (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Send a security protocol command to a device that receives data and/or the result\r
+  of one or more commands sent by SendData.\r
+\r
+  The ReceiveData function sends a security protocol command to the given MediaId.\r
+  The security protocol command sent is defined by SecurityProtocolId and contains\r
+  the security protocol specific data SecurityProtocolSpecificData. The function\r
+  returns the data from the security protocol command in PayloadBuffer.\r
+\r
+  For devices supporting the SCSI command set, the security protocol command is sent\r
+  using the SECURITY PROTOCOL IN command defined in SPC-4.\r
+\r
+  For devices supporting the ATA command set, the security protocol command is sent\r
+  using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize\r
+  is non-zero.\r
+\r
+  If the PayloadBufferSize is zero, the security protocol command is sent using the\r
+  Trusted Non-Data command defined in ATA8-ACS.\r
+\r
+  If PayloadBufferSize is too small to store the available data from the security\r
+  protocol command, the function shall copy PayloadBufferSize bytes into the\r
+  PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.\r
+\r
+  If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,\r
+  the function shall return EFI_INVALID_PARAMETER.\r
+\r
+  If the given MediaId does not support security protocol commands, the function shall\r
+  return EFI_UNSUPPORTED. If there is no media in the device, the function returns\r
+  EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,\r
+  the function returns EFI_MEDIA_CHANGED.\r
+\r
+  If the security protocol fails to complete within the Timeout period, the function\r
+  shall return EFI_TIMEOUT.\r
+\r
+  If the security protocol command completes without an error, the function shall\r
+  return EFI_SUCCESS. If the security protocol command completes with an error, the\r
+  function shall return EFI_DEVICE_ERROR.\r
+\r
+  @param  This                         Indicates a pointer to the calling context.\r
+  @param  MediaId                      ID of the medium to receive data from.\r
+  @param  Timeout                      The timeout, in 100ns units, to use for the execution\r
+                                       of the security protocol command. A Timeout value of 0\r
+                                       means that this function will wait indefinitely for the\r
+                                       security protocol command to execute. If Timeout is greater\r
+                                       than zero, then this function will return EFI_TIMEOUT\r
+                                       if the time required to execute the receive data command\r
+                                       is greater than Timeout.\r
+  @param  SecurityProtocolId           The value of the "Security Protocol" parameter of\r
+                                       the security protocol command to be sent.\r
+  @param  SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
+                                       of the security protocol command to be sent.\r
+  @param  PayloadBufferSize            Size in bytes of the payload data buffer.\r
+  @param  PayloadBuffer                A pointer to a destination buffer to store the security\r
+                                       protocol command specific payload data for the security\r
+                                       protocol command. The caller is responsible for having\r
+                                       either implicit or explicit ownership of the buffer.\r
+  @param  PayloadTransferSize          A pointer to a buffer to store the size in bytes of the\r
+                                       data written to the payload data buffer.\r
+\r
+  @retval EFI_SUCCESS                  The security protocol command completed successfully.\r
+  @retval EFI_WARN_BUFFER_TOO_SMALL    The PayloadBufferSize was too small to store the available\r
+                                       data from the device. The PayloadBuffer contains the truncated data.\r
+  @retval EFI_UNSUPPORTED              The given MediaId does not support security protocol commands.\r
+  @retval EFI_DEVICE_ERROR             The security protocol command completed with an error.\r
+  @retval EFI_NO_MEDIA                 There is no media in the device.\r
+  @retval EFI_MEDIA_CHANGED            The MediaId is not for the current media.\r
+  @retval EFI_INVALID_PARAMETER        The PayloadBuffer or PayloadTransferSize is NULL and\r
+                                       PayloadBufferSize is non-zero.\r
+  @retval EFI_TIMEOUT                  A timeout occurred while waiting for the security\r
+                                       protocol command to execute.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaStorageSecurityReceiveData (\r
+  IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    *This,\r
+  IN UINT32                                   MediaId,\r
+  IN UINT64                                   Timeout,\r
+  IN UINT8                                    SecurityProtocolId,\r
+  IN UINT16                                   SecurityProtocolSpecificData,\r
+  IN UINTN                                    PayloadBufferSize,\r
+  OUT VOID                                    *PayloadBuffer,\r
+  OUT UINTN                                   *PayloadTransferSize\r
+  )\r
+{\r
+  EFI_STATUS                       Status;\r
+  ATA_DEVICE                       *Private;\r
+  EFI_TPL                          OldTpl;\r
+\r
+  DEBUG ((EFI_D_INFO, "EFI Storage Security Protocol - Read"));\r
+  if ((PayloadBuffer == NULL || PayloadTransferSize == NULL) && PayloadBufferSize != 0) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status  = EFI_SUCCESS;\r
+  Private = ATA_DEVICE_FROM_STORAGE_SECURITY (This);\r
+\r
+  if (MediaId != Private->BlockIo.Media->MediaId) {\r
+    return EFI_MEDIA_CHANGED;\r
+  }\r
+\r
+  if (!Private->BlockIo.Media->MediaPresent) {\r
+    return EFI_NO_MEDIA;\r
+  }\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
+  Status = TrustTransferAtaDevice (\r
+             Private,\r
+             PayloadBuffer,\r
+             SecurityProtocolId,\r
+             SecurityProtocolSpecificData,\r
+             PayloadBufferSize,\r
+             FALSE,\r
+             Timeout,\r
+             PayloadTransferSize\r
+             );\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Send a security protocol command to a device.\r
+\r
+  The SendData function sends a security protocol command containing the payload\r
+  PayloadBuffer to the given MediaId. The security protocol command sent is\r
+  defined by SecurityProtocolId and contains the security protocol specific data\r
+  SecurityProtocolSpecificData. If the underlying protocol command requires a\r
+  specific padding for the command payload, the SendData function shall add padding\r
+  bytes to the command payload to satisfy the padding requirements.\r
+\r
+  For devices supporting the SCSI command set, the security protocol command is sent\r
+  using the SECURITY PROTOCOL OUT command defined in SPC-4.\r
+\r
+  For devices supporting the ATA command set, the security protocol command is sent\r
+  using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize\r
+  is non-zero. If the PayloadBufferSize is zero, the security protocol command is\r
+  sent using the Trusted Non-Data command defined in ATA8-ACS.\r
+\r
+  If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall\r
+  return EFI_INVALID_PARAMETER.\r
+\r
+  If the given MediaId does not support security protocol commands, the function\r
+  shall return EFI_UNSUPPORTED. If there is no media in the device, the function\r
+  returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the\r
+  device, the function returns EFI_MEDIA_CHANGED.\r
+\r
+  If the security protocol fails to complete within the Timeout period, the function\r
+  shall return EFI_TIMEOUT.\r
+\r
+  If the security protocol command completes without an error, the function shall return\r
+  EFI_SUCCESS. If the security protocol command completes with an error, the function\r
+  shall return EFI_DEVICE_ERROR.\r
+\r
+  @param  This                         Indicates a pointer to the calling context.\r
+  @param  MediaId                      ID of the medium to receive data from.\r
+  @param  Timeout                      The timeout, in 100ns units, to use for the execution\r
+                                       of the security protocol command. A Timeout value of 0\r
+                                       means that this function will wait indefinitely for the\r
+                                       security protocol command to execute. If Timeout is greater\r
+                                       than zero, then this function will return EFI_TIMEOUT\r
+                                       if the time required to execute the receive data command\r
+                                       is greater than Timeout.\r
+  @param  SecurityProtocolId           The value of the "Security Protocol" parameter of\r
+                                       the security protocol command to be sent.\r
+  @param  SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
+                                       of the security protocol command to be sent.\r
+  @param  PayloadBufferSize            Size in bytes of the payload data buffer.\r
+  @param  PayloadBuffer                A pointer to a destination buffer to store the security\r
+                                       protocol command specific payload data for the security\r
+                                       protocol command.\r
+\r
+  @retval EFI_SUCCESS                  The security protocol command completed successfully.\r
+  @retval EFI_UNSUPPORTED              The given MediaId does not support security protocol commands.\r
+  @retval EFI_DEVICE_ERROR             The security protocol command completed with an error.\r
+  @retval EFI_NO_MEDIA                 There is no media in the device.\r
+  @retval EFI_MEDIA_CHANGED            The MediaId is not for the current media.\r
+  @retval EFI_INVALID_PARAMETER        The PayloadBuffer is NULL and PayloadBufferSize is non-zero.\r
+  @retval EFI_TIMEOUT                  A timeout occurred while waiting for the security\r
+                                       protocol command to execute.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AtaStorageSecuritySendData (\r
+  IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    *This,\r
+  IN UINT32                                   MediaId,\r
+  IN UINT64                                   Timeout,\r
+  IN UINT8                                    SecurityProtocolId,\r
+  IN UINT16                                   SecurityProtocolSpecificData,\r
+  IN UINTN                                    PayloadBufferSize,\r
+  IN VOID                                     *PayloadBuffer\r
+  )\r
+{\r
+  EFI_STATUS                       Status;\r
+  ATA_DEVICE                       *Private;\r
+  EFI_TPL                          OldTpl;\r
+\r
+  DEBUG ((EFI_D_INFO, "EFI Storage Security Protocol - Send"));\r
+  if ((PayloadBuffer == NULL) && (PayloadBufferSize != 0)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status  = EFI_SUCCESS;\r
+  Private = ATA_DEVICE_FROM_STORAGE_SECURITY (This);\r
+\r
+  if (MediaId != Private->BlockIo.Media->MediaId) {\r
+    return EFI_MEDIA_CHANGED;\r
+  }\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+  Status = TrustTransferAtaDevice (\r
+             Private,\r
+             PayloadBuffer,\r
+             SecurityProtocolId,\r
+             SecurityProtocolSpecificData,\r
+             PayloadBufferSize,\r
+             TRUE,\r
+             Timeout,\r
+             NULL\r
+             );\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+  return Status;\r
+}\r
 \r
 /**\r
   The user Entry Point for module AtaBus. The user code starts with this function.\r
@@ -1122,6 +1712,7 @@ InitializeAtaBus(
   )\r
 {\r
   EFI_STATUS              Status;\r
+  UINTN                   DataSize;\r
 \r
   //\r
   // Install driver model protocol(s).\r
@@ -1136,5 +1727,194 @@ InitializeAtaBus(
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  //\r
+  // Get the MorControl bit.\r
+  //\r
+  DataSize = sizeof (mMorControl);\r
+  Status = gRT->GetVariable (\r
+                  MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
+                  &gEfiMemoryOverwriteControlDataGuid,\r
+                  NULL,\r
+                  &DataSize,\r
+                  &mMorControl\r
+                  );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_INFO, "AtaBus:gEfiMemoryOverwriteControlDataGuid doesn't exist!!***\n"));\r
+    mMorControl = 0;\r
+    Status      = EFI_SUCCESS;\r
+  } else {\r
+    DEBUG ((EFI_D_INFO, "AtaBus:Get the gEfiMemoryOverwriteControlDataGuid = %x!!***\n", mMorControl));\r
+  }\r
+\r
   return Status;\r
 }\r
+\r
+/**\r
+  Send TPer Reset command to reset eDrive to lock all protected bands.\r
+  Typically, there are 2 mechanism for resetting eDrive. They are:\r
+  1. TPer Reset through IEEE 1667 protocol.\r
+  2. TPer Reset through native TCG protocol.\r
+  This routine will detect what protocol the attached eDrive comform to, TCG or\r
+  IEEE 1667 protocol. Then send out TPer Reset command separately.\r
+\r
+  @param[in] AtaDevice    ATA_DEVICE pointer.\r
+\r
+**/\r
+VOID\r
+InitiateTPerReset (\r
+  IN   ATA_DEVICE       *AtaDevice\r
+  )\r
+{\r
+\r
+  EFI_STATUS                                   Status;\r
+  UINT8                                        *Buffer;\r
+  UINTN                                        XferSize;\r
+  UINTN                                        Len;\r
+  UINTN                                        Index;\r
+  BOOLEAN                                      TcgFlag;\r
+  BOOLEAN                                      IeeeFlag;\r
+  EFI_BLOCK_IO_PROTOCOL                        *BlockIo;\r
+  EFI_STORAGE_SECURITY_COMMAND_PROTOCOL        *Ssp;\r
+  SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA  *Data;\r
+\r
+  Buffer        = NULL;\r
+  TcgFlag       = FALSE;\r
+  IeeeFlag      = FALSE;\r
+  Ssp           = &AtaDevice->StorageSecurity;\r
+  BlockIo       = &AtaDevice->BlockIo;\r
+\r
+  //\r
+  // ATA8-ACS 7.57.6.1 indicates the Transfer Length field requirements a multiple of 512.\r
+  // If the length of the TRUSTED RECEIVE parameter data is greater than the Transfer Length,\r
+  // then the device shall return the TRUSTED RECEIVE parameter data truncated to the requested Transfer Length.\r
+  //\r
+  Len           = ROUNDUP512(sizeof(SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA));\r
+  Buffer        = AllocateZeroPool(Len);\r
+\r
+  if (Buffer == NULL) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // When the Security Protocol field is set to 00h, and SP Specific is set to 0000h in a TRUSTED RECEIVE\r
+  // command, the device basic information data shall be returned.\r
+  //\r
+  Status = Ssp->ReceiveData (\r
+                  Ssp,\r
+                  BlockIo->Media->MediaId,\r
+                  100000000,                    // Timeout 10-sec\r
+                  0,                            // SecurityProtocol\r
+                  0,                            // SecurityProtocolSpecifcData\r
+                  Len,                          // PayloadBufferSize,\r
+                  Buffer,                       // PayloadBuffer\r
+                  &XferSize\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    goto Exit;\r
+  }\r
+\r
+  //\r
+  // In returned data, the ListLength field indicates the total length, in bytes,\r
+  // of the supported security protocol list.\r
+  //\r
+  Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)Buffer;\r
+  Len  = ROUNDUP512(sizeof (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA) +\r
+                    (Data->SupportedSecurityListLength[0] << 8) +\r
+                    (Data->SupportedSecurityListLength[1])\r
+                    );\r
+\r
+  //\r
+  // Free original buffer and allocate new buffer.\r
+  //\r
+  FreePool(Buffer);\r
+  Buffer = AllocateZeroPool(Len);\r
+  if (Buffer == NULL) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Read full supported security protocol list from device.\r
+  //\r
+  Status = Ssp->ReceiveData (\r
+                  Ssp,\r
+                  BlockIo->Media->MediaId,\r
+                  100000000,                    // Timeout 10-sec\r
+                  0,                            // SecurityProtocol\r
+                  0,                            // SecurityProtocolSpecifcData\r
+                  Len,                          // PayloadBufferSize,\r
+                  Buffer,                       // PayloadBuffer\r
+                  &XferSize\r
+                  );\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    goto Exit;\r
+  }\r
+\r
+  Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)Buffer;\r
+  Len  = (Data->SupportedSecurityListLength[0] << 8) + Data->SupportedSecurityListLength[1];\r
+\r
+  //\r
+  // Iterate full supported security protocol list to check if TCG or IEEE 1667 protocol\r
+  // is supported.\r
+  //\r
+  for (Index = 0; Index < Len; Index++) {\r
+    if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_TCG) {\r
+      //\r
+      // Found a  TCG device.\r
+      //\r
+      TcgFlag = TRUE;\r
+      DEBUG ((EFI_D_INFO, "This device is a TCG protocol device\n"));\r
+      break;\r
+    }\r
+\r
+    if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_IEEE1667) {\r
+      //\r
+      // Found a IEEE 1667 device.\r
+      //\r
+      IeeeFlag = TRUE;\r
+      DEBUG ((EFI_D_INFO, "This device is a IEEE 1667 protocol device\n"));\r
+      break;\r
+    }\r
+  }\r
+\r
+  if (!TcgFlag && !IeeeFlag) {\r
+    DEBUG ((EFI_D_INFO, "Neither a TCG nor IEEE 1667 protocol device is found\n"));\r
+    goto Exit;\r
+  }\r
+\r
+  if (TcgFlag) {\r
+    //\r
+    // As long as TCG protocol is supported, send out a TPer Reset\r
+    // TCG command to the device via the TrustedSend command with a non-zero Transfer Length.\r
+    //\r
+    Status = Ssp->SendData (\r
+                    Ssp,\r
+                    BlockIo->Media->MediaId,\r
+                    100000000,                    // Timeout 10-sec\r
+                    SECURITY_PROTOCOL_TCG,        // SecurityProtocol\r
+                    0x0400,                       // SecurityProtocolSpecifcData\r
+                    512,                          // PayloadBufferSize,\r
+                    Buffer                        // PayloadBuffer\r
+                    );\r
+\r
+    if (!EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_INFO, "Send TPer Reset Command Successfully !\n"));\r
+    } else {\r
+      DEBUG ((EFI_D_INFO, "Send TPer Reset Command Fail !\n"));\r
+    }\r
+  }\r
+\r
+  if (IeeeFlag) {\r
+    //\r
+    // TBD : Perform a TPer Reset via IEEE 1667 Protocol\r
+    //\r
+    DEBUG ((EFI_D_INFO, "IEEE 1667 Protocol didn't support yet!\n"));\r
+  }\r
+\r
+Exit:\r
+\r
+  if (Buffer != NULL) {\r
+    FreePool(Buffer);\r
+  }\r
+}\r