]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Disk/PartitionDxe/Partition.c
ArmPlatformPkg/ArmPlatformLibNull: remove bogus PCD dependencies
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / PartitionDxe / Partition.c
index 9de8312caec248afa01f30d430ef1172248c5c04..f6030e0897947c96ae98b0d35046b61d001cb893 100644 (file)
@@ -1,10 +1,10 @@
 /** @file\r
   Partition driver that produces logical BlockIo devices from a physical\r
   BlockIo device. The logical BlockIo devices are based on the format\r
-  of the raw block devices media. Currently "El Torito CD-ROM", Legacy\r
+  of the raw block devices media. Currently "El Torito CD-ROM", UDF, Legacy\r
   MBR, and GPT partition schemes are supported.\r
 \r
-Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, 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
@@ -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
@@ -37,6 +45,7 @@ PARTITION_DETECT_ROUTINE mPartitionDetectRoutineTable[] = {
   PartitionInstallGptChildHandles,\r
   PartitionInstallElToritoChildHandles,\r
   PartitionInstallMbrChildHandles,\r
+  PartitionInstallUdfChildHandles,\r
   NULL\r
 };\r
 \r
@@ -157,28 +166,8 @@ PartitionDriverBindingSupported (
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
                   );\r
- if (EFI_ERROR (Status)) {\r
-   return Status;\r
- }\r
-  \r
- Status = gBS->OpenProtocol (\r
-                 ControllerHandle,\r
-                 &gEfiBlockIo2ProtocolGuid,\r
-                 NULL,\r
-                 This->DriverBindingHandle,\r
-                 ControllerHandle,\r
-                 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
-                 );\r
-  if (EFI_ERROR (Status)) {\r
-    //\r
-    // According to UEFI Spec 2.3.1, if a driver is written for a disk device, \r
-    // then the EFI_BLOCK_IO_PROTOCOL and EFI_BLOCK_IO2_PROTOCOAL must be implemented.\r
-    // Currently, SCSI disk driver only produce the EFI_BLOCK_IO_PROTOCOL, it will\r
-    // not be updated until the non blocking SCSI Pass Thru Protocol is provided.\r
-    // If there is no EFI_BLOCK_IO2_PROTOCOL, skip here.\r
-    //    \r
-  } \r
-  return EFI_SUCCESS;  \r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -209,11 +198,13 @@ PartitionDriverBindingStart (
   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
@@ -251,16 +242,10 @@ PartitionDriverBindingStart (
                   (VOID **) &BlockIo2,\r
                   This->DriverBindingHandle,\r
                   ControllerHandle,\r
-                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    //\r
-    // According to UEFI Spec 2.3.1, if a driver is written for a disk device, \r
-    // then the EFI_BLOCK_IO_PROTOCOL and EFI_BLOCK_IO2_PROTOCOAL must be implemented.\r
-    // Currently, SCSI disk driver only produce the EFI_BLOCK_IO_PROTOCOL, it will\r
-    // not be updated until the non blocking SCSI Pass Thru Protocol is provided.\r
-    // If there is no EFI_BLOCK_IO2_PROTOCOL, skip here.\r
-    //\r
+    BlockIo2 = NULL;\r
   }\r
 \r
   //\r
@@ -278,6 +263,9 @@ PartitionDriverBindingStart (
     goto Exit;\r
   }\r
 \r
+  //\r
+  // Get the DiskIo and DiskIo2.\r
+  //\r
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiDiskIoProtocolGuid,\r
@@ -298,6 +286,18 @@ PartitionDriverBindingStart (
 \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
   // Try to read blocks when there's media or it is removable physical partition.\r
   //\r
@@ -306,9 +306,9 @@ PartitionDriverBindingStart (
   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
-    // the partitions described by the media.\r
+    // Try for GPT, then El Torito, then UDF, and then legacy MBR partition\r
+    // types. If the media supports a given partition type install child handles\r
+    // to represent the partitions described by the media.\r
     //\r
     Routine = &mPartitionDetectRoutineTable[0];\r
     while (*Routine != NULL) {\r
@@ -316,6 +316,7 @@ PartitionDriverBindingStart (
                    This,\r
                    ControllerHandle,\r
                    DiskIo,\r
+                   DiskIo2,\r
                    BlockIo,\r
                    BlockIo2,\r
                    ParentDevicePath\r
@@ -349,11 +350,11 @@ PartitionDriverBindingStart (
           ControllerHandle\r
           );\r
     //\r
-    // Close Parent BlockIO2 if has.\r
+    // Close Parent DiskIo2 if has.\r
     //    \r
     gBS->CloseProtocol (\r
            ControllerHandle,\r
-           &gEfiBlockIo2ProtocolGuid,\r
+           &gEfiDiskIo2ProtocolGuid,\r
            This->DriverBindingHandle,\r
            ControllerHandle\r
            );\r
@@ -407,6 +408,16 @@ PartitionDriverBindingStop (
   Private = NULL;\r
 \r
   if (NumberOfChildren == 0) {\r
+    //\r
+    // In the case of re-entry of the PartitionDriverBindingStop, the\r
+    // NumberOfChildren may not reflect the actual number of children on the\r
+    // bus driver. Hence, additional check is needed here.\r
+    //\r
+    if (HasChildren (ControllerHandle)) {\r
+      DEBUG((EFI_D_ERROR, "PartitionDriverBindingStop: Still has child.\n"));\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+\r
     //\r
     // Close the bus driver\r
     //\r
@@ -421,7 +432,7 @@ PartitionDriverBindingStop (
     //    \r
     gBS->CloseProtocol (\r
            ControllerHandle,\r
-           &gEfiBlockIo2ProtocolGuid,\r
+           &gEfiDiskIo2ProtocolGuid,\r
            This->DriverBindingHandle,\r
            ControllerHandle\r
            );\r
@@ -459,41 +470,67 @@ PartitionDriverBindingStop (
 \r
 \r
     Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo);\r
+    if (Private->InStop) {\r
+      //\r
+      // If the child handle is going to be stopped again during the re-entry\r
+      // of DriverBindingStop, just do nothing.\r
+      //\r
+      break;\r
+    }\r
+    Private->InStop = TRUE;\r
 \r
-    Status = gBS->CloseProtocol (\r
-                    ControllerHandle,\r
-                    &gEfiDiskIoProtocolGuid,\r
-                    This->DriverBindingHandle,\r
-                    ChildHandleBuffer[Index]\r
-                    );\r
+    BlockIo->FlushBlocks (BlockIo);\r
+\r
+    if (BlockIo2 != NULL) {\r
+      Status = BlockIo2->FlushBlocksEx (BlockIo2, NULL);\r
+      DEBUG((EFI_D_ERROR, "PartitionDriverBindingStop: FlushBlocksEx returned with %r\n", Status));\r
+    } else {\r
+      Status = EFI_SUCCESS;\r
+    }\r
+\r
+    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
-                       &gEfiBlockIo2ProtocolGuid,\r
-                       &Private->BlockIo2,\r
-                       Private->EspGuid,\r
-                       NULL,\r
-                       NULL\r
-                       );\r
+      //\r
+      // Some device drivers might re-install the BlockIO(2) protocols for a\r
+      // media change condition. Therefore, if the FlushBlocksEx returned with\r
+      // EFI_MEDIA_CHANGED, just let the BindingStop fail to avoid potential\r
+      // reference of already stopped child handle.\r
+      //\r
+      if (Status != EFI_MEDIA_CHANGED) {\r
+        Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                         ChildHandleBuffer[Index],\r
+                         &gEfiDevicePathProtocolGuid,\r
+                         Private->DevicePath,\r
+                         &gEfiBlockIoProtocolGuid,\r
+                         &Private->BlockIo,\r
+                         &gEfiBlockIo2ProtocolGuid,\r
+                         &Private->BlockIo2,\r
+                         &gEfiPartitionInfoProtocolGuid,\r
+                         &Private->PartitionInfo,\r
+                         Private->EspGuid,\r
+                         NULL,\r
+                         NULL\r
+                         );\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
+                       &gEfiPartitionInfoProtocolGuid,\r
+                       &Private->PartitionInfo,\r
                        Private->EspGuid,\r
                        NULL,\r
                        NULL\r
@@ -501,6 +538,7 @@ PartitionDriverBindingStop (
     }\r
 \r
     if (EFI_ERROR (Status)) {\r
+      Private->InStop = FALSE;\r
       gBS->OpenProtocol (\r
              ControllerHandle,\r
              &gEfiDiskIoProtocolGuid,\r
@@ -516,6 +554,9 @@ PartitionDriverBindingStop (
 \r
     if (EFI_ERROR (Status)) {\r
       AllChildrenStopped = FALSE;\r
+      if (Status == EFI_MEDIA_CHANGED) {\r
+        break;\r
+      }\r
     }\r
   }\r
 \r
@@ -576,11 +617,15 @@ ProbeMediaStatus (
   )\r
 {\r
   EFI_STATUS                 Status;\r
+  UINT8                      Buffer[1];\r
 \r
   //\r
-  // Read 1 byte from offset 0 but passing NULL as buffer pointer\r
+  // Read 1 byte from offset 0 to check if the MediaId is still valid.\r
+  // The reading operation is synchronious thus it is not worth it to\r
+  // allocate a buffer from the pool. The destination buffer for the\r
+  // data is in the stack.\r
   //\r
-  Status = DiskIo->ReadDisk (DiskIo, MediaId, 0, 1, NULL);\r
+  Status = DiskIo->ReadDisk (DiskIo, MediaId, 0, 1, (VOID*)Buffer);\r
   if ((Status == EFI_NO_MEDIA) || (Status == EFI_MEDIA_CHANGED)) {\r
     return Status;\r
   }\r
@@ -712,6 +757,38 @@ 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
@@ -740,6 +817,68 @@ PartitionResetEx (
                                     );\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
@@ -784,43 +923,38 @@ PartitionReadBlocksEx (
   OUT    VOID                   *Buffer\r
   )\r
 {\r
+  EFI_STATUS              Status;\r
   PARTITION_PRIVATE_DATA  *Private;\r
   UINT64                  Offset;\r
-  UINT32                  UnderRun;\r
-\r
-  if (Token == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
+  PARTITION_ACCESS_TASK   *Task;\r
 \r
   Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
+\r
   if (BufferSize % Private->BlockSize != 0) {\r
-    return EFI_BAD_BUFFER_SIZE;\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 EFI_INVALID_PARAMETER;\r
+    return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_INVALID_PARAMETER);\r
   }\r
 \r
-  //\r
-  // Since the BlockIO2 call Parent BlockIO2 directly, so here the offset must\r
-  // be multiple of BlockSize. If the Spec will be updated the DiskIO to support\r
-  // BlockIO2, this limitation will be removed and call DiskIO here.\r
-  //\r
-  Lba = DivU64x32Remainder (Offset, Private->BlockSize, &UnderRun);\r
-  if (UnderRun != 0) {\r
-    return EFI_UNSUPPORTED;\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
-  //\r
-  // Because some partitions have different block size from their parent\r
-  // device, in that case the Block I/O2 couldn't be called.\r
-  //\r
-  if (Private->BlockSize != Private->ParentBlockIo->Media->BlockSize) {\r
-    return EFI_UNSUPPORTED;\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 Private->ParentBlockIo2->ReadBlocksEx (Private->ParentBlockIo2, MediaId, Lba, Token, BufferSize, Buffer);\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -865,43 +999,37 @@ PartitionWriteBlocksEx (
   IN     VOID                   *Buffer\r
   )\r
 {\r
+  EFI_STATUS              Status;\r
   PARTITION_PRIVATE_DATA  *Private;\r
   UINT64                  Offset;\r
-  UINT32                  UnderRun;\r
-\r
-  if (Token == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
+  PARTITION_ACCESS_TASK   *Task;\r
 \r
   Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
+\r
   if (BufferSize % Private->BlockSize != 0) {\r
-    return EFI_BAD_BUFFER_SIZE;\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 EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Since the BlockIO2 call Parent BlockIO2 directly, so here the offset must\r
-  // be multiple of BlockSize. If the Spec will be updated the DiskIO to support\r
-  // BlockIO2, this limitation will be removed and call DiskIO here.\r
-  //\r
-  Lba = DivU64x32Remainder (Offset, Private->BlockSize, &UnderRun);\r
-  if (UnderRun != 0) {\r
-    return EFI_UNSUPPORTED;\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
-  //\r
-  // Because some kinds of partition have different block size from their parent,\r
-  // in that case it couldn't call parent Block I/O2. \r
-  //\r
-  if (Private->BlockSize != Private->ParentBlockIo->Media->BlockSize) {\r
-    return EFI_UNSUPPORTED;\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
-\r
-  return Private->ParentBlockIo2->WriteBlocksEx (Private->ParentBlockIo2, MediaId, Lba, Token, BufferSize, Buffer);\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -912,7 +1040,7 @@ PartitionWriteBlocksEx (
   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
+  @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
@@ -933,19 +1061,27 @@ PartitionFlushBlocksEx (
   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
-  //\r
-  // Because some kinds of partition have different block size from their parent,\r
-  // in that case it couldn't call parent Block I/O2. \r
-  //\r
-  if (Private->BlockSize != Private->ParentBlockIo->Media->BlockSize) {\r
-    return EFI_UNSUPPORTED;\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
-  return Private->ParentBlockIo2->FlushBlocksEx (Private->ParentBlockIo2, Token);\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
@@ -956,14 +1092,15 @@ PartitionFlushBlocksEx (
   @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]  PartitionInfo     Child Partition Information interface.\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
@@ -974,14 +1111,15 @@ 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_PARTITION_INFO_PROTOCOL  *PartitionInfo,\r
   IN  EFI_LBA                      Start,\r
   IN  EFI_LBA                      End,\r
-  IN  UINT32                       BlockSize,\r
-  IN  BOOLEAN                      InstallEspGuid\r
+  IN  UINT32                       BlockSize\r
   )\r
 {\r
   EFI_STATUS              Status;\r
@@ -1002,6 +1140,7 @@ PartitionInstallChildHandle (
   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
@@ -1019,7 +1158,8 @@ PartitionInstallChildHandle (
   //\r
   // Set the BlockIO2 into Private Data.\r
   //\r
-  if (Private->ParentBlockIo2 != NULL) {\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
@@ -1041,20 +1181,24 @@ PartitionInstallChildHandle (
 \r
   Private->Media.BlockSize = (UINT32) BlockSize;\r
 \r
-  //\r
-  // For BlockIO2, it should keep the same alignment with the parent BlockIO2's.\r
-  //\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
   //\r
-  // Per UEFI Spec, LowestAlignedLba and LogicalBlocksPerPhysicalBlock must be 0\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->BlockIo.Media->LowestAlignedLba              = 0;\r
-    Private->BlockIo.Media->LogicalBlocksPerPhysicalBlock = 0;\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
@@ -1064,7 +1208,12 @@ PartitionInstallChildHandle (
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  if (InstallEspGuid) {\r
+  //\r
+  // Set the PartitionInfo into Private Data.\r
+  //\r
+  CopyMem (&Private->PartitionInfo, PartitionInfo, sizeof (EFI_PARTITION_INFO_PROTOCOL));\r
+\r
+  if ((PartitionInfo->System == 1)) {\r
     Private->EspGuid = &gEfiPartTypeSystemPartGuid;\r
   } else {\r
     //\r
@@ -1075,17 +1224,9 @@ PartitionInstallChildHandle (
 \r
   //\r
   // Create the new handle. \r
-  // BlockIO2 will be installed on the condition that the blocksize of parent BlockIO \r
-  // is same with the child BlockIO's. Instead of calling the DiskIO, the child BlockIO2 \r
-  // directly call the parent BlockIO and doesn't handle the different block size issue.\r
-  // If SPEC will update the DiskIO to support the Non-Blocking model, the BlockIO2 will call\r
-  // DiskIO to handle the blocksize unequal issue and the limitation will be remove from\r
-  // here.\r
   //\r
   Private->Handle = NULL;\r
-  if ((Private->ParentBlockIo2 != NULL) &&\r
-      (Private->ParentBlockIo2->Media->BlockSize == BlockSize)\r
-     ) {\r
+  if (Private->DiskIo2 != NULL) {\r
     Status = gBS->InstallMultipleProtocolInterfaces (\r
                     &Private->Handle,\r
                     &gEfiDevicePathProtocolGuid,\r
@@ -1094,6 +1235,8 @@ PartitionInstallChildHandle (
                     &Private->BlockIo,\r
                     &gEfiBlockIo2ProtocolGuid,\r
                     &Private->BlockIo2,\r
+                    &gEfiPartitionInfoProtocolGuid,\r
+                    &Private->PartitionInfo,\r
                     Private->EspGuid,\r
                     NULL,\r
                     NULL\r
@@ -1105,6 +1248,8 @@ PartitionInstallChildHandle (
                     Private->DevicePath,\r
                     &gEfiBlockIoProtocolGuid,\r
                     &Private->BlockIo,\r
+                    &gEfiPartitionInfoProtocolGuid,\r
+                    &Private->PartitionInfo,\r
                     Private->EspGuid,\r
                     NULL,\r
                     NULL\r
@@ -1168,3 +1313,41 @@ InitializePartition (
   return Status;\r
 }\r
 \r
+\r
+/**\r
+  Test to see if there is any child on ControllerHandle.\r
+\r
+  @param[in]  ControllerHandle    Handle of device to test.\r
+\r
+  @retval TRUE                    There are children on the ControllerHandle.\r
+  @retval FALSE                   No child is on the ControllerHandle.\r
+\r
+**/\r
+BOOLEAN\r
+HasChildren (\r
+  IN EFI_HANDLE           ControllerHandle\r
+  )\r
+{\r
+  EFI_OPEN_PROTOCOL_INFORMATION_ENTRY  *OpenInfoBuffer;\r
+  UINTN                                EntryCount;\r
+  EFI_STATUS                           Status;\r
+  UINTN                                Index;\r
+\r
+  Status = gBS->OpenProtocolInformation (\r
+                  ControllerHandle,\r
+                  &gEfiDiskIoProtocolGuid,\r
+                  &OpenInfoBuffer,\r
+                  &EntryCount\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  for (Index = 0; Index < EntryCount; Index++) {\r
+    if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r
+      break;\r
+    }\r
+  }\r
+  FreePool (OpenInfoBuffer);\r
+\r
+  return (BOOLEAN) (Index < EntryCount);\r
+}\r
+\r