]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Disk/PartitionDxe/Partition.c
Fix 3 bugs in DiskIoDxe and PartitionDxe drivers introduced in DiskIo2 implementation.
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / PartitionDxe / Partition.c
index 41001fa2b2ee433b2bba63570b5b0b98bdb93066..7fb2c0dfe09dcfde76fb463b87d90b186ddb9f60 100644 (file)
@@ -4,8 +4,8 @@
   of the raw block devices media. Currently "El Torito CD-ROM", Legacy\r
   MBR, and GPT partition schemes are supported.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2013, 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
@@ -25,7 +25,15 @@ EFI_DRIVER_BINDING_PROTOCOL gPartitionDriverBinding = {
   PartitionDriverBindingSupported,\r
   PartitionDriverBindingStart,\r
   PartitionDriverBindingStop,\r
-  0xa,\r
+  //\r
+  // Grub4Dos copies the BPB of the first partition to the MBR. If the \r
+  // DriverBindingStart() of the Fat driver gets run before that of Partition \r
+  // driver only the first partition can be recognized.\r
+  // Let the driver binding version of Partition driver be higher than that of\r
+  // Fat driver to make sure the DriverBindingStart() of the Partition driver\r
+  // gets run before that of Fat driver so that all the partitions can be recognized.\r
+  //\r
+  0xb,\r
   NULL,\r
   NULL\r
 };\r
@@ -40,16 +48,15 @@ PARTITION_DETECT_ROUTINE mPartitionDetectRoutineTable[] = {
   NULL\r
 };\r
 \r
-\r
-\r
 /**\r
   Test to see if this driver supports ControllerHandle. Any ControllerHandle\r
-  than contains a BlockIo and DiskIo protocol can be supported.\r
+  than contains a BlockIo and DiskIo protocol or a BlockIo2 protocol can be\r
+  supported.\r
 \r
-  @param  This                Protocol instance pointer.\r
-  @param  ControllerHandle    Handle of device to test\r
-  @param  RemainingDevicePath Optional parameter use to pick a specific child\r
-                              device to start.\r
+  @param[in]  This                Protocol instance pointer.\r
+  @param[in]  ControllerHandle    Handle of device to test.\r
+  @param[in]  RemainingDevicePath Optional parameter use to pick a specific child\r
+                                  device to start.\r
 \r
   @retval EFI_SUCCESS         This driver supports this device\r
   @retval EFI_ALREADY_STARTED This driver is already running on this device\r
@@ -69,22 +76,35 @@ PartitionDriverBindingSupported (
   EFI_DISK_IO_PROTOCOL      *DiskIo;\r
   EFI_DEV_PATH              *Node;\r
 \r
+  //\r
+  // Check RemainingDevicePath validation\r
+  //\r
   if (RemainingDevicePath != NULL) {\r
-    Node = (EFI_DEV_PATH *) RemainingDevicePath;\r
-    if (Node->DevPath.Type != MEDIA_DEVICE_PATH ||\r
+    //\r
+    // Check if RemainingDevicePath is the End of Device Path Node, \r
+    // if yes, go on checking other conditions\r
+    //\r
+    if (!IsDevicePathEnd (RemainingDevicePath)) {\r
+      //\r
+      // If RemainingDevicePath isn't the End of Device Path Node,\r
+      // check its validation\r
+      //\r
+      Node = (EFI_DEV_PATH *) RemainingDevicePath;\r
+      if (Node->DevPath.Type != MEDIA_DEVICE_PATH ||\r
         Node->DevPath.SubType != MEDIA_HARDDRIVE_DP ||\r
-        DevicePathNodeLength (&Node->DevPath) != sizeof (HARDDRIVE_DEVICE_PATH)\r
-        ) {\r
-      return EFI_UNSUPPORTED;\r
+        DevicePathNodeLength (&Node->DevPath) != sizeof (HARDDRIVE_DEVICE_PATH)) {\r
+        return EFI_UNSUPPORTED;\r
+      }\r
     }\r
   }\r
+\r
   //\r
   // Open the IO Abstraction(s) needed to perform the supported test\r
   //\r
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
-                  &gEfiDevicePathProtocolGuid,\r
-                  (VOID **) &ParentDevicePath,\r
+                  &gEfiDiskIoProtocolGuid,\r
+                  (VOID **) &DiskIo,\r
                   This->DriverBindingHandle,\r
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -92,7 +112,6 @@ PartitionDriverBindingSupported (
   if (Status == EFI_ALREADY_STARTED) {\r
     return EFI_SUCCESS;\r
   }\r
-\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -101,18 +120,18 @@ PartitionDriverBindingSupported (
   //\r
   gBS->CloseProtocol (\r
          ControllerHandle,\r
-         &gEfiDevicePathProtocolGuid,\r
+         &gEfiDiskIoProtocolGuid,\r
          This->DriverBindingHandle,\r
          ControllerHandle\r
          );\r
 \r
   //\r
-  // Open the IO Abstraction(s) needed to perform the supported test\r
+  // Open the EFI Device Path protocol needed to perform the supported test\r
   //\r
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
-                  &gEfiDiskIoProtocolGuid,\r
-                  (VOID **) &DiskIo,\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  (VOID **) &ParentDevicePath,\r
                   This->DriverBindingHandle,\r
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -124,12 +143,13 @@ PartitionDriverBindingSupported (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
-  // Close the I/O Abstraction(s) used to perform the supported test\r
+  // Close protocol, don't use device path protocol in the Support() function\r
   //\r
   gBS->CloseProtocol (\r
         ControllerHandle,\r
-        &gEfiDiskIoProtocolGuid,\r
+        &gEfiDevicePathProtocolGuid,\r
         This->DriverBindingHandle,\r
         ControllerHandle\r
         );\r
@@ -145,20 +165,22 @@ PartitionDriverBindingSupported (
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
                   );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
-  return Status;\r
+  return EFI_SUCCESS;  \r
 }\r
 \r
-\r
 /**\r
-  Start this driver on ControllerHandle by opening a Block IO and Disk IO\r
-  protocol, reading Device Path, and creating a child handle with a\r
-  Disk IO and device path protocol.\r
+  Start this driver on ControllerHandle by opening a Block IO or a Block IO2\r
+  or both, and Disk IO protocol, reading Device Path, and creating a child\r
+  handle with a Disk IO and device path protocol.\r
 \r
-  @param  This                 Protocol instance pointer.\r
-  @param  ControllerHandle     Handle of device to bind driver to\r
-  @param  RemainingDevicePath  Optional parameter use to pick a specific child\r
-                               device to start.\r
+  @param[in]  This                 Protocol instance pointer.\r
+  @param[in]  ControllerHandle     Handle of device to bind driver to\r
+  @param[in]  RemainingDevicePath  Optional parameter use to pick a specific child\r
+                                   device to start.\r
 \r
   @retval EFI_SUCCESS          This driver is added to ControllerHandle\r
   @retval EFI_ALREADY_STARTED  This driver is already running on ControllerHandle\r
@@ -176,10 +198,34 @@ PartitionDriverBindingStart (
   EFI_STATUS                Status;\r
   EFI_STATUS                OpenStatus;\r
   EFI_BLOCK_IO_PROTOCOL     *BlockIo;\r
+  EFI_BLOCK_IO2_PROTOCOL    *BlockIo2;\r
   EFI_DISK_IO_PROTOCOL      *DiskIo;\r
+  EFI_DISK_IO2_PROTOCOL     *DiskIo2;\r
   EFI_DEVICE_PATH_PROTOCOL  *ParentDevicePath;\r
   PARTITION_DETECT_ROUTINE  *Routine;\r
+  BOOLEAN                   MediaPresent;\r
+  EFI_TPL                   OldTpl;\r
 \r
+  BlockIo2 = NULL;\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK); \r
+  //\r
+  // Check RemainingDevicePath validation\r
+  //\r
+  if (RemainingDevicePath != NULL) {\r
+    //\r
+    // Check if RemainingDevicePath is the End of Device Path Node, \r
+    // if yes, return EFI_SUCCESS\r
+    //\r
+    if (IsDevicePathEnd (RemainingDevicePath)) {\r
+      Status = EFI_SUCCESS;\r
+      goto Exit;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Try to open BlockIO and BlockIO2. If BlockIO would be opened, continue,\r
+  // otherwise, return error.\r
+  //\r
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiBlockIoProtocolGuid,\r
@@ -189,10 +235,23 @@ PartitionDriverBindingStart (
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Exit;\r
+  }\r
+\r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiBlockIo2ProtocolGuid,\r
+                  (VOID **) &BlockIo2,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    BlockIo2 = NULL;\r
   }\r
+\r
   //\r
-  // Get the Device Path Protocol on ControllerHandle's handle\r
+  // Get the Device Path Protocol on ControllerHandle's handle.\r
   //\r
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
@@ -203,9 +262,12 @@ PartitionDriverBindingStart (
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
                   );\r
   if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r
-    return Status;\r
+    goto Exit;\r
   }\r
 \r
+  //\r
+  // Get the DiskIo and DiskIo2.\r
+  //\r
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiDiskIoProtocolGuid,\r
@@ -221,16 +283,30 @@ PartitionDriverBindingStart (
           This->DriverBindingHandle,\r
           ControllerHandle\r
           );\r
-    return Status;\r
+    goto Exit;\r
   }\r
 \r
   OpenStatus = Status;\r
 \r
+  Status = gBS->OpenProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiDiskIo2ProtocolGuid,\r
+                  (VOID **) &DiskIo2,\r
+                  This->DriverBindingHandle,\r
+                  ControllerHandle,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r
+    DiskIo2 = NULL;\r
+  }\r
+\r
   //\r
-  // If no media is present, do nothing here.\r
+  // Try to read blocks when there's media or it is removable physical partition.\r
   //\r
-  Status = EFI_UNSUPPORTED;\r
-  if (BlockIo->Media->MediaPresent) {\r
+  Status       = EFI_UNSUPPORTED;\r
+  MediaPresent = BlockIo->Media->MediaPresent;\r
+  if (BlockIo->Media->MediaPresent ||\r
+      (BlockIo->Media->RemovableMedia && !BlockIo->Media->LogicalPartition)) {\r
     //\r
     // Try for GPT, then El Torito, and then legacy MBR partition types. If the\r
     // media supports a given partition type install child handles to represent\r
@@ -242,10 +318,12 @@ PartitionDriverBindingStart (
                    This,\r
                    ControllerHandle,\r
                    DiskIo,\r
+                   DiskIo2,\r
                    BlockIo,\r
+                   BlockIo2,\r
                    ParentDevicePath\r
                    );\r
-      if (!EFI_ERROR (Status) || Status == EFI_MEDIA_CHANGED) {\r
+      if (!EFI_ERROR (Status) || Status == EFI_MEDIA_CHANGED || Status == EFI_NO_MEDIA) {\r
         break;\r
       }\r
       Routine++;\r
@@ -257,13 +335,31 @@ PartitionDriverBindingStart (
   // driver. So don't try to close them. Otherwise, we will break the dependency\r
   // between the controller and the driver set up before.\r
   //\r
-  if (EFI_ERROR (Status) && !EFI_ERROR (OpenStatus) && Status != EFI_MEDIA_CHANGED) {\r
+  // In the case that when the media changes on a device it will Reinstall the \r
+  // BlockIo interaface. This will cause a call to our Stop(), and a subsequent\r
+  // reentrant call to our Start() successfully. We should leave the device open\r
+  // when this happen. The "media change" case includes either the status is\r
+  // EFI_MEDIA_CHANGED or it is a "media" to "no media" change. \r
+  //  \r
+  if (EFI_ERROR (Status)          &&\r
+      !EFI_ERROR (OpenStatus)     &&\r
+      Status != EFI_MEDIA_CHANGED &&\r
+      !(MediaPresent && Status == EFI_NO_MEDIA)) {\r
     gBS->CloseProtocol (\r
           ControllerHandle,\r
           &gEfiDiskIoProtocolGuid,\r
           This->DriverBindingHandle,\r
           ControllerHandle\r
           );\r
+    //\r
+    // Close Parent DiskIo2 if has.\r
+    //    \r
+    gBS->CloseProtocol (\r
+           ControllerHandle,\r
+           &gEfiDiskIo2ProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           ControllerHandle\r
+           );\r
 \r
     gBS->CloseProtocol (\r
           ControllerHandle,\r
@@ -273,10 +369,11 @@ PartitionDriverBindingStart (
           );\r
   }\r
 \r
+Exit:\r
+  gBS->RestoreTPL (OldTpl);\r
   return Status;\r
 }\r
 \r
-\r
 /**\r
   Stop this driver on ControllerHandle. Support stopping any child handles\r
   created by this driver.\r
@@ -303,10 +400,15 @@ PartitionDriverBindingStop (
   EFI_STATUS              Status;\r
   UINTN                   Index;\r
   EFI_BLOCK_IO_PROTOCOL   *BlockIo;\r
+  EFI_BLOCK_IO2_PROTOCOL  *BlockIo2;\r
   BOOLEAN                 AllChildrenStopped;\r
   PARTITION_PRIVATE_DATA  *Private;\r
   EFI_DISK_IO_PROTOCOL    *DiskIo;\r
 \r
+  BlockIo  = NULL;\r
+  BlockIo2 = NULL;\r
+  Private = NULL;\r
+\r
   if (NumberOfChildren == 0) {\r
     //\r
     // Close the bus driver\r
@@ -317,6 +419,15 @@ PartitionDriverBindingStop (
           This->DriverBindingHandle,\r
           ControllerHandle\r
           );\r
+    //\r
+    // Close Parent BlockIO2 if has.\r
+    //    \r
+    gBS->CloseProtocol (\r
+           ControllerHandle,\r
+           &gEfiDiskIo2ProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           ControllerHandle\r
+           );\r
 \r
     gBS->CloseProtocol (\r
           ControllerHandle,\r
@@ -324,60 +435,86 @@ PartitionDriverBindingStop (
           This->DriverBindingHandle,\r
           ControllerHandle\r
           );\r
-\r
     return EFI_SUCCESS;\r
   }\r
 \r
   AllChildrenStopped = TRUE;\r
   for (Index = 0; Index < NumberOfChildren; Index++) {\r
-    Status = gBS->OpenProtocol (\r
-                    ChildHandleBuffer[Index],\r
-                    &gEfiBlockIoProtocolGuid,\r
-                    (VOID **) &BlockIo,\r
-                    This->DriverBindingHandle,\r
-                    ControllerHandle,\r
-                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
-                    );\r
-    if (!EFI_ERROR (Status)) {\r
-\r
-      Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo);\r
+    gBS->OpenProtocol (\r
+           ChildHandleBuffer[Index],\r
+           &gEfiBlockIoProtocolGuid,\r
+           (VOID **) &BlockIo,\r
+           This->DriverBindingHandle,\r
+           ControllerHandle,\r
+           EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+           );\r
+    //\r
+    // Try to locate BlockIo2.\r
+    //\r
+    gBS->OpenProtocol (\r
+           ChildHandleBuffer[Index],\r
+           &gEfiBlockIo2ProtocolGuid,\r
+           (VOID **) &BlockIo2,\r
+           This->DriverBindingHandle,\r
+           ControllerHandle,\r
+           EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+           ); \r
 \r
-      //\r
-      // All Software protocols have be freed from the handle so remove it.\r
-      //\r
-      BlockIo->FlushBlocks (BlockIo);\r
 \r
-      Status = gBS->CloseProtocol (\r
-                      ControllerHandle,\r
-                      &gEfiDiskIoProtocolGuid,\r
-                      This->DriverBindingHandle,\r
-                      ChildHandleBuffer[Index]\r
-                      );\r
+    Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo);\r
 \r
+    Status = gBS->CloseProtocol (\r
+                    ControllerHandle,\r
+                    &gEfiDiskIoProtocolGuid,\r
+                    This->DriverBindingHandle,\r
+                    ChildHandleBuffer[Index]\r
+                    );\r
+    //\r
+    // All Software protocols have be freed from the handle so remove it.\r
+    // Remove the BlockIo Protocol if has.\r
+    // Remove the BlockIo2 Protocol if has.\r
+    //\r
+    if (BlockIo2 != NULL) {\r
+      BlockIo->FlushBlocks (BlockIo);\r
+      BlockIo2->FlushBlocksEx (BlockIo2, NULL);\r
       Status = gBS->UninstallMultipleProtocolInterfaces (\r
-                      ChildHandleBuffer[Index],\r
-                      &gEfiDevicePathProtocolGuid,\r
-                      Private->DevicePath,\r
-                      &gEfiBlockIoProtocolGuid,\r
-                      &Private->BlockIo,\r
-                      Private->EspGuid,\r
-                      NULL,\r
-                      NULL\r
-                      );\r
-      if (EFI_ERROR (Status)) {\r
-        gBS->OpenProtocol (\r
-              ControllerHandle,\r
-              &gEfiDiskIoProtocolGuid,\r
-              (VOID **) &DiskIo,\r
-              This->DriverBindingHandle,\r
-              ChildHandleBuffer[Index],\r
-              EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
-              );\r
-      } else {\r
-        FreePool (Private->DevicePath);\r
-        FreePool (Private);\r
-      }\r
+                       ChildHandleBuffer[Index],\r
+                       &gEfiDevicePathProtocolGuid,\r
+                       Private->DevicePath,\r
+                       &gEfiBlockIoProtocolGuid,\r
+                       &Private->BlockIo,\r
+                       &gEfiBlockIo2ProtocolGuid,\r
+                       &Private->BlockIo2,\r
+                       Private->EspGuid,\r
+                       NULL,\r
+                       NULL\r
+                       );\r
+    } else {\r
+      BlockIo->FlushBlocks (BlockIo);\r
+      Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                       ChildHandleBuffer[Index],\r
+                       &gEfiDevicePathProtocolGuid,\r
+                       Private->DevicePath,\r
+                       &gEfiBlockIoProtocolGuid,\r
+                       &Private->BlockIo,\r
+                       Private->EspGuid,\r
+                       NULL,\r
+                       NULL\r
+                       );\r
+    }\r
 \r
+    if (EFI_ERROR (Status)) {\r
+      gBS->OpenProtocol (\r
+             ControllerHandle,\r
+             &gEfiDiskIoProtocolGuid,\r
+             (VOID **) &DiskIo,\r
+             This->DriverBindingHandle,\r
+             ChildHandleBuffer[Index],\r
+             EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+             );\r
+    } else {\r
+      FreePool (Private->DevicePath);\r
+      FreePool (Private);\r
     }\r
 \r
     if (EFI_ERROR (Status)) {\r
@@ -421,6 +558,37 @@ PartitionReset (
                                   );\r
 }\r
 \r
+/**\r
+  Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED\r
+  for no media or media change case. Otherwise DefaultStatus is returned.\r
+\r
+  @param DiskIo             Pointer to the DiskIo instance.\r
+  @param MediaId            Id of the media, changes every time the media is replaced.\r
+  @param DefaultStatus      The default status to return when it's not the no media\r
+                            or media change case.\r
+\r
+  @retval EFI_NO_MEDIA      There is no media.\r
+  @retval EFI_MEDIA_CHANGED The media was changed.\r
+  @retval others            The default status to return.\r
+**/\r
+EFI_STATUS\r
+ProbeMediaStatus (\r
+  IN EFI_DISK_IO_PROTOCOL    *DiskIo,\r
+  IN UINT32                  MediaId,\r
+  IN EFI_STATUS              DefaultStatus\r
+  )\r
+{\r
+  EFI_STATUS                 Status;\r
+\r
+  //\r
+  // Read 1 byte from offset 0 but passing NULL as buffer pointer\r
+  //\r
+  Status = DiskIo->ReadDisk (DiskIo, MediaId, 0, 1, NULL);\r
+  if ((Status == EFI_NO_MEDIA) || (Status == EFI_MEDIA_CHANGED)) {\r
+    return Status;\r
+  }\r
+  return DefaultStatus;\r
+}\r
 \r
 /**\r
   Read by using the Disk IO protocol on the parent device. Lba addresses\r
@@ -457,12 +625,12 @@ PartitionReadBlocks (
   Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r
 \r
   if (BufferSize % Private->BlockSize != 0) {\r
-    return EFI_BAD_BUFFER_SIZE;\r
+    return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_BAD_BUFFER_SIZE);\r
   }\r
 \r
   Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r
   if (Offset + BufferSize > Private->End) {\r
-    return EFI_INVALID_PARAMETER;\r
+    return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_INVALID_PARAMETER);\r
   }\r
   //\r
   // Because some kinds of partition have different block size from their parent\r
@@ -476,11 +644,11 @@ PartitionReadBlocks (
   Write by using the Disk IO protocol on the parent device. Lba addresses\r
   must be converted to byte offsets.\r
 \r
-  @param  This       Protocol instance pointer.\r
-  @param  MediaId    Id of the media, changes every time the media is replaced.\r
-  @param  Lba        The starting Logical Block Address to read from\r
-  @param  BufferSize Size of Buffer, must be a multiple of device block size.\r
-  @param  Buffer     Buffer containing read data\r
+  @param[in]  This       Protocol instance pointer.\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]  BufferSize Size of Buffer, must be a multiple of device block size.\r
+  @param[in]  Buffer     Buffer containing data to be written to device.\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
@@ -499,7 +667,7 @@ PartitionWriteBlocks (
   IN UINT32                 MediaId,\r
   IN EFI_LBA                Lba,\r
   IN UINTN                  BufferSize,\r
-  OUT VOID                  *Buffer\r
+  IN VOID                  *Buffer\r
   )\r
 {\r
   PARTITION_PRIVATE_DATA  *Private;\r
@@ -508,12 +676,12 @@ PartitionWriteBlocks (
   Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r
 \r
   if (BufferSize % Private->BlockSize != 0) {\r
-    return EFI_BAD_BUFFER_SIZE;\r
+    return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_BAD_BUFFER_SIZE);\r
   }\r
 \r
   Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r
   if (Offset + BufferSize > Private->End) {\r
-    return EFI_INVALID_PARAMETER;\r
+    return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_INVALID_PARAMETER);\r
   }\r
   //\r
   // Because some kinds of partition have different block size from their parent\r
@@ -547,25 +715,353 @@ PartitionFlushBlocks (
   return Private->ParentBlockIo->FlushBlocks (Private->ParentBlockIo);\r
 }\r
 \r
+/**\r
+  Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED\r
+  for no media or media change case. Otherwise DefaultStatus is returned.\r
+\r
+  @param DiskIo2            Pointer to the DiskIo2 instance.\r
+  @param MediaId            Id of the media, changes every time the media is replaced.\r
+  @param DefaultStatus      The default status to return when it's not the no media\r
+                            or media change case.\r
+\r
+  @retval EFI_NO_MEDIA      There is no media.\r
+  @retval EFI_MEDIA_CHANGED The media was changed.\r
+  @retval others            The default status to return.\r
+**/\r
+EFI_STATUS\r
+ProbeMediaStatusEx (\r
+  IN EFI_DISK_IO2_PROTOCOL   *DiskIo2,\r
+  IN UINT32                  MediaId,\r
+  IN EFI_STATUS              DefaultStatus\r
+  )\r
+{\r
+  EFI_STATUS                 Status;\r
+\r
+  //\r
+  // Read 1 byte from offset 0 but passing NULL as buffer pointer\r
+  //\r
+  Status = DiskIo2->ReadDiskEx (DiskIo2, MediaId, 0, NULL, 1, NULL);\r
+  if ((Status == EFI_NO_MEDIA) || (Status == EFI_MEDIA_CHANGED)) {\r
+    return Status;\r
+  }\r
+  return DefaultStatus;\r
+}\r
+\r
+/**\r
+  Reset the Block Device throught Block I/O2 protocol.\r
+\r
+  @param  This                 Protocol instance pointer.\r
+  @param  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
+PartitionResetEx (\r
+  IN EFI_BLOCK_IO2_PROTOCOL *This,\r
+  IN BOOLEAN                ExtendedVerification\r
+  )\r
+{\r
+  PARTITION_PRIVATE_DATA  *Private;\r
+\r
+  Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
+\r
+  return Private->ParentBlockIo2->Reset (\r
+                                    Private->ParentBlockIo2,\r
+                                    ExtendedVerification\r
+                                    );\r
+}\r
+\r
+/**\r
+  The general callback for the DiskIo2 interfaces.\r
+  @param  Event                 Event whose notification function is being invoked.\r
+  @param  Context               The pointer to the notification function's context,\r
+                                which points to the PARTITION_ACCESS_TASK instance.\r
+**/\r
+VOID\r
+EFIAPI\r
+PartitionOnAccessComplete (\r
+  IN EFI_EVENT                 Event,\r
+  IN VOID                      *Context\r
+  )\r
+{\r
+  PARTITION_ACCESS_TASK   *Task;\r
+\r
+  Task = (PARTITION_ACCESS_TASK *) Context;\r
+\r
+  gBS->CloseEvent (Event);\r
+\r
+  Task->BlockIo2Token->TransactionStatus = Task->DiskIo2Token.TransactionStatus;\r
+  gBS->SignalEvent (Task->BlockIo2Token->Event);\r
+\r
+  FreePool (Task);\r
+}\r
+\r
+/**\r
+  Create a new PARTITION_ACCESS_TASK instance.\r
+\r
+  @param  Token  Pointer to the EFI_BLOCK_IO2_TOKEN.\r
+\r
+  @return Pointer to the created PARTITION_ACCESS_TASK instance or NULL upon failure.\r
+**/\r
+PARTITION_ACCESS_TASK *\r
+PartitionCreateAccessTask (\r
+  IN EFI_BLOCK_IO2_TOKEN    *Token\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  PARTITION_ACCESS_TASK     *Task;\r
+\r
+  Task = AllocatePool (sizeof (*Task));\r
+  if (Task == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  PartitionOnAccessComplete,\r
+                  Task,\r
+                  &Task->DiskIo2Token.Event\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (Task);\r
+    return NULL;\r
+  }\r
+\r
+  Task->BlockIo2Token = Token;\r
+\r
+  return Task;\r
+}\r
+\r
+/**\r
+  Read BufferSize bytes from Lba into Buffer.\r
+  \r
+  This function reads the requested number of blocks from the device. All the\r
+  blocks are read, or an error is returned.\r
+  If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and\r
+  non-blocking I/O is being used, the Event associated with this request will\r
+  not be signaled.\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 \r
+                              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 \r
+                              caller is responsible for either having implicit or \r
+                              explicit ownership of the buffer.\r
+\r
+  @retval EFI_SUCCESS           The read request was queued if Token->Event is\r
+                                not NULL.The data was read correctly from the\r
+                                device if the Token->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
+EFI_STATUS\r
+EFIAPI\r
+PartitionReadBlocksEx (\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
+  EFI_STATUS              Status;\r
+  PARTITION_PRIVATE_DATA  *Private;\r
+  UINT64                  Offset;\r
+  PARTITION_ACCESS_TASK   *Task;\r
+\r
+  Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
+\r
+  if (BufferSize % Private->BlockSize != 0) {\r
+    return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_BAD_BUFFER_SIZE);\r
+  }\r
+\r
+  Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r
+  if (Offset + BufferSize > Private->End) {\r
+    return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_INVALID_PARAMETER);\r
+  }\r
+\r
+  if ((Token != NULL) && (Token->Event != NULL)) {\r
+    Task = PartitionCreateAccessTask (Token);\r
+    if (Task == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    Status = Private->DiskIo2->ReadDiskEx (Private->DiskIo2, MediaId, Offset, &Task->DiskIo2Token, BufferSize, Buffer);\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->CloseEvent (Task->DiskIo2Token.Event);\r
+      FreePool (Task);\r
+    }\r
+  } else {\r
+    Status = Private->DiskIo2->ReadDiskEx (Private->DiskIo2, MediaId, Offset, NULL, BufferSize, Buffer);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Write BufferSize bytes from Lba into Buffer.\r
+\r
+  This function writes the requested number of blocks to the device. All blocks\r
+  are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,\r
+  EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is\r
+  being used, the Event associated with this request will not be signaled.\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 write request was queued if Event is not NULL.\r
+                                The data was written correctly to the device if\r
+                                the Event is NULL.\r
+  @retval EFI_WRITE_PROTECTED   The device can not be written to.\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_DEVICE_ERROR      The device reported an error while performing the write.\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
+  @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
+PartitionWriteBlocksEx (\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
+  EFI_STATUS              Status;\r
+  PARTITION_PRIVATE_DATA  *Private;\r
+  UINT64                  Offset;\r
+  PARTITION_ACCESS_TASK   *Task;\r
+\r
+  Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
+\r
+  if (BufferSize % Private->BlockSize != 0) {\r
+    return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_BAD_BUFFER_SIZE);\r
+  }\r
+\r
+  Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r
+  if (Offset + BufferSize > Private->End) {\r
+    return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_INVALID_PARAMETER);\r
+  }\r
+  \r
+  if ((Token != NULL) && (Token->Event != NULL)) {\r
+    Task = PartitionCreateAccessTask (Token);\r
+    if (Task == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    Status =  Private->DiskIo2->WriteDiskEx (Private->DiskIo2, MediaId, Offset, &Task->DiskIo2Token, BufferSize, Buffer);\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->CloseEvent (Task->DiskIo2Token.Event);\r
+      FreePool (Task);\r
+    }\r
+  } else {\r
+    Status = Private->DiskIo2->WriteDiskEx (Private->DiskIo2, MediaId, Offset, NULL, BufferSize, Buffer);\r
+  }\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Flush the Block Device.\r
\r
+  If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED\r
+  is returned and non-blocking I/O is being used, the Event associated with\r
+  this request will not be signaled.  \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          The flush request was queued if Event is not NULL.\r
+                               All outstanding data was written correctly to the\r
+                               device if the Event is NULL.\r
+  @retval EFI_DEVICE_ERROR     The device reported an error while writting back\r
+                               the data.\r
+  @retval EFI_WRITE_PROTECTED  The device cannot be written to.\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_OUT_OF_RESOURCES The request could not be completed due to a lack\r
+                               of resources.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PartitionFlushBlocksEx (\r
+  IN     EFI_BLOCK_IO2_PROTOCOL *This,\r
+  IN OUT EFI_BLOCK_IO2_TOKEN    *Token\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  PARTITION_PRIVATE_DATA  *Private;\r
+  PARTITION_ACCESS_TASK   *Task;\r
+\r
+  Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
+\r
+  if ((Token != NULL) && (Token->Event != NULL)) {\r
+    Task = PartitionCreateAccessTask (Token);\r
+    if (Task == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    Status = Private->DiskIo2->FlushDiskEx (Private->DiskIo2, &Task->DiskIo2Token);\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->CloseEvent (Task->DiskIo2Token.Event);\r
+      FreePool (Task);\r
+    }\r
+  } else {\r
+    Status = Private->DiskIo2->FlushDiskEx (Private->DiskIo2, NULL);\r
+  }\r
+  return Status;\r
+}\r
 \r
 \r
 /**\r
   Create a child handle for a logical block device that represents the\r
   bytes Start to End of the Parent Block IO device.\r
 \r
-  @param[in]  This              Protocol instance pointer\r
-  @param[in]  ParentHandle      Parent Handle for new child\r
-  @param[in]  ParentDiskIo      Parent DiskIo interface\r
-  @param[in]  ParentBlockIo     Parent BlockIo interface\r
-  @param[in]  ParentDevicePath  Parent Device Path\r
-  @param[in]  DevicePathNode    Child Device Path node\r
-  @param[in]  Start             Start Block\r
-  @param[in]  End               End Block\r
-  @param[in]  BlockSize         Child block size\r
-  @param[in]  InstallEspGuid    Flag to install EFI System Partition GUID on handle\r
-\r
-  @retval EFI_SUCCESS       A child handle was added\r
-  @retval other             A child handle was not added\r
+  @param[in]  This              Protocol instance pointer.\r
+  @param[in]  ParentHandle      Parent Handle for new child.\r
+  @param[in]  ParentDiskIo      Parent DiskIo interface.\r
+  @param[in]  ParentDiskIo2     Parent DiskIo2 interface.\r
+  @param[in]  ParentBlockIo     Parent BlockIo interface.\r
+  @param[in]  ParentBlockIo2    Parent BlockIo2 interface.\r
+  @param[in]  ParentDevicePath  Parent Device Path.\r
+  @param[in]  DevicePathNode    Child Device Path node.\r
+  @param[in]  Start             Start Block.\r
+  @param[in]  End               End Block.\r
+  @param[in]  BlockSize         Child block size.\r
+  @param[in]  InstallEspGuid    Flag to install EFI System Partition GUID on handle.\r
+\r
+  @retval EFI_SUCCESS       A child handle was added.\r
+  @retval other             A child handle was not added.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -573,7 +1069,9 @@ PartitionInstallChildHandle (
   IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
   IN  EFI_HANDLE                   ParentHandle,\r
   IN  EFI_DISK_IO_PROTOCOL         *ParentDiskIo,\r
+  IN  EFI_DISK_IO2_PROTOCOL        *ParentDiskIo2,\r
   IN  EFI_BLOCK_IO_PROTOCOL        *ParentBlockIo,\r
+  IN  EFI_BLOCK_IO2_PROTOCOL       *ParentBlockIo2,\r
   IN  EFI_DEVICE_PATH_PROTOCOL     *ParentDevicePath,\r
   IN  EFI_DEVICE_PATH_PROTOCOL     *DevicePathNode,\r
   IN  EFI_LBA                      Start,\r
@@ -585,6 +1083,7 @@ PartitionInstallChildHandle (
   EFI_STATUS              Status;\r
   PARTITION_PRIVATE_DATA  *Private;\r
 \r
+  Status  = EFI_SUCCESS;\r
   Private = AllocateZeroPool (sizeof (PARTITION_PRIVATE_DATA));\r
   if (Private == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -597,29 +1096,70 @@ PartitionInstallChildHandle (
 \r
   Private->BlockSize        = BlockSize;\r
   Private->ParentBlockIo    = ParentBlockIo;\r
+  Private->ParentBlockIo2   = ParentBlockIo2;\r
   Private->DiskIo           = ParentDiskIo;\r
+  Private->DiskIo2          = ParentDiskIo2;\r
 \r
+  //\r
+  // Set the BlockIO into Private Data.\r
+  //\r
   Private->BlockIo.Revision = ParentBlockIo->Revision;\r
-\r
+  \r
   Private->BlockIo.Media    = &Private->Media;\r
   CopyMem (Private->BlockIo.Media, ParentBlockIo->Media, sizeof (EFI_BLOCK_IO_MEDIA));\r
+\r
+  Private->BlockIo.Reset        = PartitionReset;\r
+  Private->BlockIo.ReadBlocks   = PartitionReadBlocks;\r
+  Private->BlockIo.WriteBlocks  = PartitionWriteBlocks;\r
+  Private->BlockIo.FlushBlocks  = PartitionFlushBlocks;\r
+\r
+  //\r
+  // Set the BlockIO2 into Private Data.\r
+  //\r
+  if (Private->DiskIo2 != NULL) {\r
+    ASSERT (Private->ParentBlockIo2 != NULL);\r
+    Private->BlockIo2.Media    = &Private->Media2;\r
+    CopyMem (Private->BlockIo2.Media, ParentBlockIo2->Media, sizeof (EFI_BLOCK_IO_MEDIA));\r
+\r
+    Private->BlockIo2.Reset          = PartitionResetEx;\r
+    Private->BlockIo2.ReadBlocksEx   = PartitionReadBlocksEx;\r
+    Private->BlockIo2.WriteBlocksEx  = PartitionWriteBlocksEx;\r
+    Private->BlockIo2.FlushBlocksEx  = PartitionFlushBlocksEx; \r
+  }\r
+\r
+  Private->Media.IoAlign   = 0;\r
   Private->Media.LogicalPartition = TRUE;\r
   Private->Media.LastBlock = DivU64x32 (\r
                                MultU64x32 (\r
                                  End - Start + 1,\r
                                  ParentBlockIo->Media->BlockSize\r
                                  ),\r
-                               BlockSize\r
+                                BlockSize\r
                                ) - 1;\r
 \r
-  Private->Media.BlockSize      = (UINT32) BlockSize;\r
+  Private->Media.BlockSize = (UINT32) BlockSize;\r
 \r
-  Private->BlockIo.Reset        = PartitionReset;\r
-  Private->BlockIo.ReadBlocks   = PartitionReadBlocks;\r
-  Private->BlockIo.WriteBlocks  = PartitionWriteBlocks;\r
-  Private->BlockIo.FlushBlocks  = PartitionFlushBlocks;\r
+  Private->Media2.IoAlign   = 0;\r
+  Private->Media2.LogicalPartition = TRUE;\r
+  Private->Media2.LastBlock = Private->Media.LastBlock;\r
+  Private->Media2.BlockSize = (UINT32) BlockSize;\r
 \r
-  Private->DevicePath           = AppendDevicePathNode (ParentDevicePath, DevicePathNode);\r
+  //\r
+  // Per UEFI Spec, LowestAlignedLba, LogicalBlocksPerPhysicalBlock and OptimalTransferLengthGranularity must be 0\r
+  //  for logical partitions.\r
+  //\r
+  if (Private->BlockIo.Revision >= EFI_BLOCK_IO_PROTOCOL_REVISION2) {\r
+    Private->Media.LowestAlignedLba               = 0;\r
+    Private->Media.LogicalBlocksPerPhysicalBlock  = 0;\r
+    Private->Media2.LowestAlignedLba              = 0;\r
+    Private->Media2.LogicalBlocksPerPhysicalBlock = 0;\r
+    if (Private->BlockIo.Revision >= EFI_BLOCK_IO_PROTOCOL_REVISION3) {\r
+      Private->Media.OptimalTransferLengthGranularity  = 0;\r
+      Private->Media2.OptimalTransferLengthGranularity = 0;\r
+    }\r
+  }\r
+\r
+  Private->DevicePath     = AppendDevicePathNode (ParentDevicePath, DevicePathNode);\r
 \r
   if (Private->DevicePath == NULL) {\r
     FreePool (Private);\r
@@ -634,20 +1174,36 @@ PartitionInstallChildHandle (
     //\r
     Private->EspGuid = NULL;\r
   }\r
+\r
   //\r
-  // Create the new handle\r
+  // Create the new handle\r
   //\r
   Private->Handle = NULL;\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &Private->Handle,\r
-                  &gEfiDevicePathProtocolGuid,\r
-                  Private->DevicePath,\r
-                  &gEfiBlockIoProtocolGuid,\r
-                  &Private->BlockIo,\r
-                  Private->EspGuid,\r
-                  NULL,\r
-                  NULL\r
-                  );\r
+  if (Private->DiskIo2 != NULL) {\r
+    Status = gBS->InstallMultipleProtocolInterfaces (\r
+                    &Private->Handle,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    Private->DevicePath,\r
+                    &gEfiBlockIoProtocolGuid,\r
+                    &Private->BlockIo,\r
+                    &gEfiBlockIo2ProtocolGuid,\r
+                    &Private->BlockIo2,\r
+                    Private->EspGuid,\r
+                    NULL,\r
+                    NULL\r
+                    );\r
+  } else {    \r
+    Status = gBS->InstallMultipleProtocolInterfaces (\r
+                    &Private->Handle,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    Private->DevicePath,\r
+                    &gEfiBlockIoProtocolGuid,\r
+                    &Private->BlockIo,\r
+                    Private->EspGuid,\r
+                    NULL,\r
+                    NULL\r
+                    );\r
+  }\r
 \r
   if (!EFI_ERROR (Status)) {\r
     //\r