]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Sd/EmmcDxe/EmmcDxe.c
MdeModulePkg/SdMmc: Add EDKII SD/MMC stack
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / EmmcDxe / EmmcDxe.c
diff --git a/MdeModulePkg/Bus/Sd/EmmcDxe/EmmcDxe.c b/MdeModulePkg/Bus/Sd/EmmcDxe/EmmcDxe.c
new file mode 100644 (file)
index 0000000..f07540f
--- /dev/null
@@ -0,0 +1,1175 @@
+/** @file\r
+  The EmmcDxe driver is used to manage the EMMC device.\r
+\r
+  It produces BlockIo, BlockIo2 and StorageSecurity protocols to allow upper layer\r
+  access the EMMC device.\r
+\r
+  Copyright (c) 2015, 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
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "EmmcDxe.h"\r
+\r
+//\r
+// EmmcDxe Driver Binding Protocol Instance\r
+//\r
+EFI_DRIVER_BINDING_PROTOCOL gEmmcDxeDriverBinding = {\r
+  EmmcDxeDriverBindingSupported,\r
+  EmmcDxeDriverBindingStart,\r
+  EmmcDxeDriverBindingStop,\r
+  0x10,\r
+  NULL,\r
+  NULL\r
+};\r
+\r
+//\r
+// Template for Emmc Partitions.\r
+//\r
+EMMC_PARTITION mEmmcPartitionTemplate = {\r
+  EMMC_PARTITION_SIGNATURE,    // Signature\r
+  FALSE,                       // Enable\r
+  EmmcPartitionUnknown,        // PartitionType\r
+  NULL,                        // Handle\r
+  NULL,                        // DevicePath\r
+  {                            // BlockIo\r
+    EFI_BLOCK_IO_PROTOCOL_REVISION,\r
+    NULL,\r
+    EmmcReset,\r
+    EmmcReadBlocks,\r
+    EmmcWriteBlocks,\r
+    EmmcFlushBlocks\r
+  },\r
+  {                            // BlockIo2\r
+    NULL,\r
+    EmmcResetEx,\r
+    EmmcReadBlocksEx,\r
+    EmmcWriteBlocksEx,\r
+    EmmcFlushBlocksEx\r
+  },\r
+  {                            // BlockMedia\r
+    0,                         // MediaId\r
+    FALSE,                     // RemovableMedia\r
+    TRUE,                      // MediaPresent\r
+    FALSE,                     // LogicPartition\r
+    FALSE,                     // ReadOnly\r
+    FALSE,                     // WritingCache\r
+    0x200,                     // BlockSize\r
+    0,                         // IoAlign\r
+    0                          // LastBlock\r
+  },\r
+  {                            // StorageSecurity\r
+    EmmcSecurityProtocolIn,\r
+    EmmcSecurityProtocolOut\r
+  },\r
+  {\r
+    NULL,\r
+    NULL\r
+  },\r
+  NULL                         // Device\r
+};\r
+\r
+/**\r
+  Decode and print EMMC CSD Register content.\r
+\r
+  @param[in] Csd           Pointer to EMMC_CSD data structure.\r
+\r
+  @retval EFI_SUCCESS      The function completed successfully\r
+**/\r
+EFI_STATUS\r
+DumpCsd (\r
+  IN EMMC_CSD  *Csd\r
+  )\r
+{\r
+  DEBUG((DEBUG_INFO, "== Dump Emmc Csd Register==\n"));\r
+  DEBUG((DEBUG_INFO, "  CSD structure                    0x%x\n", Csd->CsdStructure));\r
+  DEBUG((DEBUG_INFO, "  System specification version     0x%x\n", Csd->SpecVers));\r
+  DEBUG((DEBUG_INFO, "  Data read access-time 1          0x%x\n", Csd->Taac));\r
+  DEBUG((DEBUG_INFO, "  Data read access-time 2          0x%x\n", Csd->Nsac));\r
+  DEBUG((DEBUG_INFO, "  Max. bus clock frequency         0x%x\n", Csd->TranSpeed));\r
+  DEBUG((DEBUG_INFO, "  Device command classes           0x%x\n", Csd->Ccc));\r
+  DEBUG((DEBUG_INFO, "  Max. read data block length      0x%x\n", Csd->ReadBlLen));\r
+  DEBUG((DEBUG_INFO, "  Partial blocks for read allowed  0x%x\n", Csd->ReadBlPartial));\r
+  DEBUG((DEBUG_INFO, "  Write block misalignment         0x%x\n", Csd->WriteBlkMisalign));\r
+  DEBUG((DEBUG_INFO, "  Read block misalignment          0x%x\n", Csd->ReadBlkMisalign));\r
+  DEBUG((DEBUG_INFO, "  DSR implemented                  0x%x\n", Csd->DsrImp));\r
+  DEBUG((DEBUG_INFO, "  Device size                      0x%x\n", Csd->CSizeLow | (Csd->CSizeHigh << 2)));\r
+  DEBUG((DEBUG_INFO, "  Max. read current @ VDD min      0x%x\n", Csd->VddRCurrMin));\r
+  DEBUG((DEBUG_INFO, "  Max. read current @ VDD max      0x%x\n", Csd->VddRCurrMax));\r
+  DEBUG((DEBUG_INFO, "  Max. write current @ VDD min     0x%x\n", Csd->VddWCurrMin));\r
+  DEBUG((DEBUG_INFO, "  Max. write current @ VDD max     0x%x\n", Csd->VddWCurrMax));\r
+  DEBUG((DEBUG_INFO, "  Device size multiplier           0x%x\n", Csd->CSizeMult));\r
+  DEBUG((DEBUG_INFO, "  Erase group size                 0x%x\n", Csd->EraseGrpSize));\r
+  DEBUG((DEBUG_INFO, "  Erase group size multiplier      0x%x\n", Csd->EraseGrpMult));\r
+  DEBUG((DEBUG_INFO, "  Write protect group size         0x%x\n", Csd->WpGrpSize));\r
+  DEBUG((DEBUG_INFO, "  Write protect group enable       0x%x\n", Csd->WpGrpEnable));\r
+  DEBUG((DEBUG_INFO, "  Manufacturer default ECC         0x%x\n", Csd->DefaultEcc));\r
+  DEBUG((DEBUG_INFO, "  Write speed factor               0x%x\n", Csd->R2WFactor));\r
+  DEBUG((DEBUG_INFO, "  Max. write data block length     0x%x\n", Csd->WriteBlLen));\r
+  DEBUG((DEBUG_INFO, "  Partial blocks for write allowed 0x%x\n", Csd->WriteBlPartial));\r
+  DEBUG((DEBUG_INFO, "  Content protection application   0x%x\n", Csd->ContentProtApp));\r
+  DEBUG((DEBUG_INFO, "  File format group                0x%x\n", Csd->FileFormatGrp));\r
+  DEBUG((DEBUG_INFO, "  Copy flag (OTP)                  0x%x\n", Csd->Copy));\r
+  DEBUG((DEBUG_INFO, "  Permanent write protection       0x%x\n", Csd->PermWriteProtect));\r
+  DEBUG((DEBUG_INFO, "  Temporary write protection       0x%x\n", Csd->TmpWriteProtect));\r
+  DEBUG((DEBUG_INFO, "  File format                      0x%x\n", Csd->FileFormat));\r
+  DEBUG((DEBUG_INFO, "  ECC code                         0x%x\n", Csd->Ecc));\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Decode and print EMMC EXT_CSD Register content.\r
+\r
+  @param[in] ExtCsd           Pointer to the EMMC_EXT_CSD data structure.\r
+\r
+  @retval EFI_SUCCESS         The function completed successfully\r
+**/\r
+EFI_STATUS\r
+DumpExtCsd (\r
+  IN EMMC_EXT_CSD  *ExtCsd\r
+  )\r
+{\r
+  DEBUG((DEBUG_INFO, "==Dump Emmc ExtCsd Register==\n"));\r
+  DEBUG((DEBUG_INFO, "  Supported Command Sets                 0x%x\n", ExtCsd->CmdSet));\r
+  DEBUG((DEBUG_INFO, "  HPI features                           0x%x\n", ExtCsd->HpiFeatures));\r
+  DEBUG((DEBUG_INFO, "  Background operations support          0x%x\n", ExtCsd->BkOpsSupport));\r
+  DEBUG((DEBUG_INFO, "  Background operations status           0x%x\n", ExtCsd->BkopsStatus));\r
+  DEBUG((DEBUG_INFO, "  Number of correctly programmed sectors 0x%x\n", *((UINT32*)&ExtCsd->CorrectlyPrgSectorsNum[0])));\r
+  DEBUG((DEBUG_INFO, "  Initialization time after partitioning 0x%x\n", ExtCsd->IniTimeoutAp));\r
+  DEBUG((DEBUG_INFO, "  TRIM Multiplier                        0x%x\n", ExtCsd->TrimMult));\r
+  DEBUG((DEBUG_INFO, "  Secure Feature support                 0x%x\n", ExtCsd->SecFeatureSupport));\r
+  DEBUG((DEBUG_INFO, "  Secure Erase Multiplier                0x%x\n", ExtCsd->SecEraseMult));\r
+  DEBUG((DEBUG_INFO, "  Secure TRIM Multiplier                 0x%x\n", ExtCsd->SecTrimMult));\r
+  DEBUG((DEBUG_INFO, "  Boot information                       0x%x\n", ExtCsd->BootInfo));\r
+  DEBUG((DEBUG_INFO, "  Boot partition size                    0x%x\n", ExtCsd->BootSizeMult));\r
+  DEBUG((DEBUG_INFO, "  Access size                            0x%x\n", ExtCsd->AccSize));\r
+  DEBUG((DEBUG_INFO, "  High-capacity erase unit size          0x%x\n", ExtCsd->HcEraseGrpSize));\r
+  DEBUG((DEBUG_INFO, "  High-capacity erase timeout            0x%x\n", ExtCsd->EraseTimeoutMult));\r
+  DEBUG((DEBUG_INFO, "  Reliable write sector count            0x%x\n", ExtCsd->RelWrSecC));\r
+  DEBUG((DEBUG_INFO, "  High-capacity write protect group size 0x%x\n", ExtCsd->HcWpGrpSize));\r
+  DEBUG((DEBUG_INFO, "  Sleep/awake timeout                    0x%x\n", ExtCsd->SATimeout));\r
+  DEBUG((DEBUG_INFO, "  Sector Count                           0x%x\n", *((UINT32*)&ExtCsd->SecCount[0])));\r
+  DEBUG((DEBUG_INFO, "  Partition switching timing             0x%x\n", ExtCsd->PartitionSwitchTime));\r
+  DEBUG((DEBUG_INFO, "  Out-of-interrupt busy timing           0x%x\n", ExtCsd->OutOfInterruptTime));\r
+  DEBUG((DEBUG_INFO, "  I/O Driver Strength                    0x%x\n", ExtCsd->DriverStrength));\r
+  DEBUG((DEBUG_INFO, "  Device type                            0x%x\n", ExtCsd->DeviceType));\r
+  DEBUG((DEBUG_INFO, "  CSD STRUCTURE                          0x%x\n", ExtCsd->CsdStructure));\r
+  DEBUG((DEBUG_INFO, "  Extended CSD revision                  0x%x\n", ExtCsd->ExtCsdRev));\r
+  DEBUG((DEBUG_INFO, "  Command set                            0x%x\n", ExtCsd->CmdSet));\r
+  DEBUG((DEBUG_INFO, "  Command set revision                   0x%x\n", ExtCsd->CmdSetRev));\r
+  DEBUG((DEBUG_INFO, "  Power class                            0x%x\n", ExtCsd->PowerClass));\r
+  DEBUG((DEBUG_INFO, "  High-speed interface timing            0x%x\n", ExtCsd->HsTiming));\r
+  DEBUG((DEBUG_INFO, "  Bus width mode                         0x%x\n", ExtCsd->BusWidth));\r
+  DEBUG((DEBUG_INFO, "  Erased memory content                  0x%x\n", ExtCsd->ErasedMemCont));\r
+  DEBUG((DEBUG_INFO, "  Partition configuration                0x%x\n", ExtCsd->PartitionConfig));\r
+  DEBUG((DEBUG_INFO, "  Boot config protection                 0x%x\n", ExtCsd->BootConfigProt));\r
+  DEBUG((DEBUG_INFO, "  Boot bus Conditions                    0x%x\n", ExtCsd->BootBusConditions));\r
+  DEBUG((DEBUG_INFO, "  High-density erase group definition    0x%x\n", ExtCsd->EraseGroupDef));\r
+  DEBUG((DEBUG_INFO, "  Boot write protection status register  0x%x\n", ExtCsd->BootWpStatus));\r
+  DEBUG((DEBUG_INFO, "  Boot area write protection register    0x%x\n", ExtCsd->BootWp));\r
+  DEBUG((DEBUG_INFO, "  User area write protection register    0x%x\n", ExtCsd->UserWp));\r
+  DEBUG((DEBUG_INFO, "  FW configuration                       0x%x\n", ExtCsd->FwConfig));\r
+  DEBUG((DEBUG_INFO, "  RPMB Size                              0x%x\n", ExtCsd->RpmbSizeMult));\r
+  DEBUG((DEBUG_INFO, "  H/W reset function                     0x%x\n", ExtCsd->RstFunction));\r
+  DEBUG((DEBUG_INFO, "  Partitioning Support                   0x%x\n", ExtCsd->PartitioningSupport));\r
+  DEBUG((DEBUG_INFO, "  Max Enhanced Area Size                 0x%02x%02x%02x\n", \\r
+                        ExtCsd->MaxEnhSizeMult[2], ExtCsd->MaxEnhSizeMult[1], ExtCsd->MaxEnhSizeMult[0]));\r
+  DEBUG((DEBUG_INFO, "  Partitions attribute                   0x%x\n", ExtCsd->PartitionsAttribute));\r
+  DEBUG((DEBUG_INFO, "  Partitioning Setting                   0x%x\n", ExtCsd->PartitionSettingCompleted));\r
+  DEBUG((DEBUG_INFO, "  General Purpose Partition 1 Size       0x%02x%02x%02x\n", \\r
+                        ExtCsd->GpSizeMult[2], ExtCsd->GpSizeMult[1], ExtCsd->GpSizeMult[0]));\r
+  DEBUG((DEBUG_INFO, "  General Purpose Partition 2 Size       0x%02x%02x%02x\n", \\r
+                        ExtCsd->GpSizeMult[5], ExtCsd->GpSizeMult[4], ExtCsd->GpSizeMult[3]));\r
+  DEBUG((DEBUG_INFO, "  General Purpose Partition 3 Size       0x%02x%02x%02x\n", \\r
+                        ExtCsd->GpSizeMult[8], ExtCsd->GpSizeMult[7], ExtCsd->GpSizeMult[6]));\r
+  DEBUG((DEBUG_INFO, "  General Purpose Partition 4 Size       0x%02x%02x%02x\n", \\r
+                        ExtCsd->GpSizeMult[11], ExtCsd->GpSizeMult[10], ExtCsd->GpSizeMult[9]));\r
+  DEBUG((DEBUG_INFO, "  Enhanced User Data Area Size           0x%02x%02x%02x\n", \\r
+                        ExtCsd->EnhSizeMult[2], ExtCsd->EnhSizeMult[1], ExtCsd->EnhSizeMult[0]));\r
+  DEBUG((DEBUG_INFO, "  Enhanced User Data Start Address       0x%x\n", *((UINT32*)&ExtCsd->EnhStartAddr[0])));\r
+  DEBUG((DEBUG_INFO, "  Bad Block Management mode              0x%x\n", ExtCsd->SecBadBlkMgmnt));\r
+  DEBUG((DEBUG_INFO, "  Native sector size                     0x%x\n", ExtCsd->NativeSectorSize));\r
+  DEBUG((DEBUG_INFO, "  Sector size emulation                  0x%x\n", ExtCsd->UseNativeSector));\r
+  DEBUG((DEBUG_INFO, "  Sector size                            0x%x\n", ExtCsd->DataSectorSize));\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Get EMMC device model name.\r
+\r
+  @param[in, out] Device   The pointer to the EMMC_DEVICE data structure.\r
+  @param[in]      Cid      Pointer to EMMC_CID data structure.\r
+\r
+  @retval EFI_SUCCESS      The function completed successfully\r
+\r
+**/\r
+EFI_STATUS\r
+GetEmmcModelName (\r
+  IN OUT EMMC_DEVICE         *Device,\r
+  IN     EMMC_CID            *Cid\r
+  )\r
+{\r
+  CHAR8  String[EMMC_MODEL_NAME_MAX_LEN];\r
+\r
+  ZeroMem (String, sizeof (String));\r
+  CopyMem (String, &Cid->OemId, sizeof (Cid->OemId));\r
+  String[sizeof (Cid->OemId)] = ' ';\r
+  CopyMem (String + sizeof (Cid->OemId) + 1, Cid->ProductName, sizeof (Cid->ProductName));\r
+  String[sizeof (Cid->OemId) + sizeof (Cid->ProductName)] = ' ';\r
+  CopyMem (String + sizeof (Cid->OemId) + sizeof (Cid->ProductName) + 1, Cid->ProductSerialNumber, sizeof (Cid->ProductSerialNumber));\r
+\r
+  AsciiStrToUnicodeStr (String, Device->ModelName);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Discover all partitions in the EMMC device.\r
+\r
+  @param[in] Device          The pointer to the EMMC_DEVICE data structure.\r
+\r
+  @retval EFI_SUCCESS        All the partitions in the device are successfully enumerated.\r
+  @return Others             Some error occurs when enumerating the partitions.\r
+\r
+**/\r
+EFI_STATUS\r
+DiscoverAllPartitions (\r
+  IN EMMC_DEVICE             *Device\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EFI_SD_MMC_PASS_THRU_PROTOCOL     *PassThru;\r
+  EMMC_PARTITION                    *Partition;\r
+  EMMC_CSD                          *Csd;\r
+  EMMC_CID                          *Cid;\r
+  EMMC_EXT_CSD                      *ExtCsd;\r
+  UINT8                             Slot;\r
+  UINT64                            Capacity;\r
+  UINT32                            DevStatus;\r
+  UINT8                             Index;\r
+  UINT32                            SecCount;\r
+  UINT32                            GpSizeMult;\r
+\r
+  PassThru = Device->Private->PassThru;\r
+  Slot     = Device->Slot;\r
+\r
+  Status = EmmcSendStatus (Device, Slot + 1, &DevStatus);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  //\r
+  // Deselect the device to force it enter stby mode before getting CSD\r
+  // register content.\r
+  // Note here we don't judge return status as some EMMC devices return\r
+  // error but the state has been stby.\r
+  //\r
+  EmmcSelect (Device, 0);\r
+\r
+  Status = EmmcSendStatus (Device, Slot + 1, &DevStatus);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Csd    = &Device->Csd;\r
+  Status = EmmcGetCsd (Device, Slot + 1, Csd);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  DumpCsd (Csd);\r
+\r
+  if ((Csd->CSizeLow | Csd->CSizeHigh << 2) == 0xFFF) {\r
+    Device->SectorAddressing = TRUE;\r
+  } else {\r
+    Device->SectorAddressing = FALSE;\r
+  }\r
+\r
+  Cid    = &Device->Cid;\r
+  Status = EmmcGetCid (Device, Slot + 1, Cid);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = EmmcSelect (Device, Slot + 1);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  ExtCsd = &Device->ExtCsd;\r
+  Status = EmmcGetExtCsd (Device, ExtCsd);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  DumpExtCsd (ExtCsd);\r
+\r
+  if (ExtCsd->ExtCsdRev < 5) {\r
+    DEBUG ((EFI_D_ERROR, "The EMMC device version is too low, we don't support!!!\n"));\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  if ((ExtCsd->PartitioningSupport & BIT0) != BIT0) {\r
+    DEBUG ((EFI_D_ERROR, "The EMMC device doesn't support Partition Feature!!!\n"));\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  for (Index = 0; Index < EMMC_MAX_PARTITIONS; Index++) {\r
+    Partition = &Device->Partition[Index];\r
+    CopyMem (Partition, &mEmmcPartitionTemplate, sizeof (EMMC_PARTITION));\r
+    Partition->Device             = Device;\r
+    InitializeListHead (&Partition->Queue);\r
+    Partition->BlockIo.Media      = &Partition->BlockMedia;\r
+    Partition->BlockIo2.Media     = &Partition->BlockMedia;\r
+    Partition->PartitionType      = Index;\r
+    Partition->BlockMedia.IoAlign = Device->Private->PassThru->IoAlign;\r
+    Partition->BlockMedia.BlockSize      = 0x200;\r
+    Partition->BlockMedia.LastBlock      = 0x00;\r
+    Partition->BlockMedia.RemovableMedia = FALSE;\r
+    Partition->BlockMedia.MediaPresent     = TRUE;\r
+    Partition->BlockMedia.LogicalPartition = FALSE;\r
+\r
+    switch (Index) {\r
+      case EmmcPartitionUserData:\r
+        SecCount = *(UINT32*)&ExtCsd->SecCount;\r
+        Capacity = MultU64x32 ((UINT64) SecCount, 0x200);\r
+        break;\r
+      case EmmcPartitionBoot1:\r
+      case EmmcPartitionBoot2:\r
+        Capacity = ExtCsd->BootSizeMult * SIZE_128KB;\r
+        break;\r
+      case EmmcPartitionRPMB:\r
+        Capacity = ExtCsd->RpmbSizeMult * SIZE_128KB;\r
+        break;\r
+      case EmmcPartitionGP1:\r
+        GpSizeMult = (UINT32)(ExtCsd->GpSizeMult[0] | (ExtCsd->GpSizeMult[1] << 8) | (ExtCsd->GpSizeMult[2] << 16));\r
+        Capacity = MultU64x32 (MultU64x32 (MultU64x32 ((UINT64)GpSizeMult, ExtCsd->HcWpGrpSize), ExtCsd->HcEraseGrpSize), SIZE_512KB);\r
+        break;\r
+      case EmmcPartitionGP2:\r
+        GpSizeMult = (UINT32)(ExtCsd->GpSizeMult[3] | (ExtCsd->GpSizeMult[4] << 8) | (ExtCsd->GpSizeMult[5] << 16));\r
+        Capacity = MultU64x32 (MultU64x32 (MultU64x32 ((UINT64)GpSizeMult, ExtCsd->HcWpGrpSize), ExtCsd->HcEraseGrpSize), SIZE_512KB);\r
+        break;\r
+      case EmmcPartitionGP3:\r
+        GpSizeMult = (UINT32)(ExtCsd->GpSizeMult[6] | (ExtCsd->GpSizeMult[7] << 8) | (ExtCsd->GpSizeMult[8] << 16));\r
+        Capacity = MultU64x32 (MultU64x32 (MultU64x32 ((UINT64)GpSizeMult, ExtCsd->HcWpGrpSize), ExtCsd->HcEraseGrpSize), SIZE_512KB);\r
+        break;\r
+      case EmmcPartitionGP4:\r
+        GpSizeMult = (UINT32)(ExtCsd->GpSizeMult[9] | (ExtCsd->GpSizeMult[10] << 8) | (ExtCsd->GpSizeMult[11] << 16));\r
+        Capacity = MultU64x32 (MultU64x32 (MultU64x32 ((UINT64)GpSizeMult, ExtCsd->HcWpGrpSize), ExtCsd->HcEraseGrpSize), SIZE_512KB);\r
+        break;\r
+      default:\r
+        ASSERT (FALSE);\r
+        return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
+    if (Capacity != 0) {\r
+      Partition->Enable = TRUE;\r
+      Partition->BlockMedia.LastBlock = DivU64x32 (Capacity, Partition->BlockMedia.BlockSize) - 1;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Install BlkIo, BlkIo2 and Ssp protocols for the specified partition in the EMMC device.\r
+\r
+  @param[in] Device          The pointer to the EMMC_DEVICE data structure.\r
+  @param[in] Index           The index of the partition.\r
+\r
+  @retval EFI_SUCCESS        The protocols are installed successfully.\r
+  @retval Others             Some error occurs when installing the protocols.\r
+\r
+**/\r
+EFI_STATUS\r
+InstallProtocolOnPartition (\r
+  IN EMMC_DEVICE             *Device,\r
+  IN UINT8                   Index\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EMMC_PARTITION                    *Partition;\r
+  CONTROLLER_DEVICE_PATH            ControlNode;\r
+  EFI_DEVICE_PATH_PROTOCOL          *ParentDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL          *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL          *RemainingDevicePath;\r
+  EFI_HANDLE                        DeviceHandle;\r
+\r
+  //\r
+  // Build device path\r
+  //\r
+  ParentDevicePath = Device->DevicePath;\r
+\r
+  ControlNode.Header.Type    = HARDWARE_DEVICE_PATH;\r
+  ControlNode.Header.SubType = HW_CONTROLLER_DP;\r
+  SetDevicePathNodeLength (&ControlNode.Header, sizeof (CONTROLLER_DEVICE_PATH));\r
+  ControlNode.ControllerNumber = Index;\r
+\r
+  DevicePath = AppendDevicePathNode (ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*)&ControlNode);\r
+  if (DevicePath == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Error;\r
+  }\r
+\r
+  DeviceHandle = NULL;\r
+  RemainingDevicePath = DevicePath;\r
+  Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);\r
+  if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd(RemainingDevicePath)) {\r
+    Status = EFI_ALREADY_STARTED;\r
+    goto Error;\r
+  }\r
+\r
+  Partition = &Device->Partition[Index];\r
+  Partition->DevicePath = DevicePath;\r
+  if (Partition->Enable) {\r
+    //\r
+    // Install BlkIo/BlkIo2/Ssp for the specified partition\r
+    //\r
+    Status = gBS->InstallMultipleProtocolInterfaces (\r
+                    &Partition->Handle,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    Partition->DevicePath,\r
+                    &gEfiBlockIoProtocolGuid,\r
+                    &Partition->BlockIo,\r
+                    &gEfiBlockIo2ProtocolGuid,\r
+                    &Partition->BlockIo2,\r
+                    NULL\r
+                    );\r
+   if (EFI_ERROR (Status)) {\r
+      goto Error;\r
+    }\r
+\r
+    if (((Partition->PartitionType == EmmcPartitionUserData) ||\r
+        (Partition->PartitionType == EmmcPartitionBoot1) ||\r
+        (Partition->PartitionType == EmmcPartitionBoot2)) &&\r
+        ((Device->Csd.Ccc & BIT10) != 0)) {\r
+      Status = gBS->InstallProtocolInterface (\r
+                      &Partition->Handle,\r
+                      &gEfiStorageSecurityCommandProtocolGuid,\r
+                      EFI_NATIVE_INTERFACE,\r
+                      &Partition->StorageSecurity\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        gBS->UninstallMultipleProtocolInterfaces (\r
+               &Partition->Handle,\r
+               &gEfiDevicePathProtocolGuid,\r
+               Partition->DevicePath,\r
+               &gEfiBlockIoProtocolGuid,\r
+               &Partition->BlockIo,\r
+               &gEfiBlockIo2ProtocolGuid,\r
+               &Partition->BlockIo2,\r
+               NULL\r
+               );\r
+        goto Error;\r
+      }\r
+    }\r
+\r
+    gBS->OpenProtocol (\r
+           Device->Private->Controller,\r
+           &gEfiSdMmcPassThruProtocolGuid,\r
+           (VOID **) &(Device->Private->PassThru),\r
+           Device->Private->DriverBindingHandle,\r
+           Partition->Handle,\r
+           EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+           );\r
+  } else {\r
+    Status = EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+Error:\r
+  if (EFI_ERROR (Status) && (DevicePath != NULL)) {\r
+    FreePool (DevicePath);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Scan EMMC Bus to discover the device.\r
+\r
+  @param[in]  Private             The EMMC driver private data structure.\r
+  @param[in]  Slot                The slot number to check device present.\r
+\r
+  @retval EFI_SUCCESS             Successfully to discover the device and attach\r
+                                  SdMmcIoProtocol to it.\r
+  @retval EFI_OUT_OF_RESOURCES    The request could not be completed due to a lack\r
+                                  of resources.\r
+  @retval EFI_ALREADY_STARTED     The device was discovered before.\r
+  @retval Others                  Fail to discover the device.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DiscoverEmmcDevice (\r
+  IN  EMMC_DRIVER_PRIVATE_DATA    *Private,\r
+  IN  UINT8                       Slot,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EMMC_DEVICE                     *Device;\r
+  EFI_DEVICE_PATH_PROTOCOL        *DevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL        *NewDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL        *RemainingEmmcDevPath;\r
+  EFI_DEV_PATH                    *Node;\r
+  EFI_HANDLE                      DeviceHandle;\r
+  EFI_SD_MMC_PASS_THRU_PROTOCOL   *PassThru;\r
+  UINT8                           Index;\r
+\r
+  Device              = NULL;\r
+  DevicePath          = NULL;\r
+  NewDevicePath       = NULL;\r
+  RemainingDevicePath = NULL;\r
+  PassThru = Private->PassThru;\r
+  Device   = &Private->Device[Slot];\r
+\r
+  //\r
+  // Build Device Path to check if the EMMC device present at the slot.\r
+  //\r
+  Status = PassThru->BuildDevicePath (\r
+                       PassThru,\r
+                       Slot,\r
+                       &DevicePath\r
+                       );\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (DevicePath->SubType != MSG_EMMC_DP) {\r
+    Status = EFI_UNSUPPORTED;\r
+    goto Error;\r
+  }\r
+\r
+  NewDevicePath = AppendDevicePathNode (\r
+                    Private->ParentDevicePath,\r
+                    DevicePath\r
+                    );\r
+  if (NewDevicePath == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Error;\r
+  }\r
+\r
+  DeviceHandle         = NULL;\r
+  RemainingEmmcDevPath = NewDevicePath;\r
+  Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingEmmcDevPath, &DeviceHandle);\r
+  //\r
+  // The device path to the EMMC device doesn't exist. It means the corresponding device private data hasn't been initialized.\r
+  //\r
+  if (EFI_ERROR (Status) || (DeviceHandle == NULL) || !IsDevicePathEnd (RemainingEmmcDevPath)) {\r
+    Device->DevicePath = NewDevicePath;\r
+    Device->Slot       = Slot;\r
+    Device->Private    = Private;\r
+    //\r
+    // Expose user area in the Sd memory card to upper layer.\r
+    //\r
+    Status = DiscoverAllPartitions (Device);\r
+    if (EFI_ERROR(Status)) {\r
+      FreePool (NewDevicePath);\r
+      goto Error;\r
+    }\r
+\r
+    Status = gBS->InstallProtocolInterface (\r
+                    &Device->Handle,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    EFI_NATIVE_INTERFACE,\r
+                    Device->DevicePath\r
+                    );\r
+    if (EFI_ERROR(Status)) {\r
+      FreePool (NewDevicePath);\r
+      goto Error;\r
+    }\r
+\r
+    Device->ControllerNameTable = NULL;\r
+    GetEmmcModelName (Device, &Device->Cid);\r
+    AddUnicodeString2 (\r
+      "eng",\r
+      gEmmcDxeComponentName.SupportedLanguages,\r
+      &Device->ControllerNameTable,\r
+      Device->ModelName,\r
+      TRUE\r
+      );\r
+    AddUnicodeString2 (\r
+      "en",\r
+      gEmmcDxeComponentName.SupportedLanguages,\r
+      &Device->ControllerNameTable,\r
+      Device->ModelName,\r
+      FALSE\r
+      );\r
+  }\r
+\r
+  if (RemainingDevicePath == NULL) {\r
+    //\r
+    // Expose all partitions in the Emmc device to upper layer.\r
+    //\r
+    for (Index = 0; Index < EMMC_MAX_PARTITIONS; Index++) {\r
+      InstallProtocolOnPartition (Device, Index);\r
+    }\r
+  } else if (!IsDevicePathEnd (RemainingDevicePath)) {\r
+    //\r
+    // Enumerate the specified partition\r
+    //\r
+    Node = (EFI_DEV_PATH *) RemainingDevicePath;\r
+    if ((DevicePathType (&Node->DevPath) != HARDWARE_DEVICE_PATH) ||\r
+        (DevicePathSubType (&Node->DevPath) != HW_CONTROLLER_DP) ||\r
+        (DevicePathNodeLength (&Node->DevPath) != sizeof (CONTROLLER_DEVICE_PATH))) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Error;\r
+    }\r
+\r
+    Index = (UINT8)Node->Controller.ControllerNumber;\r
+    if (Index >= EMMC_MAX_PARTITIONS) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Error;\r
+    }\r
+\r
+    Status = InstallProtocolOnPartition (Device, Index);\r
+  }\r
+\r
+Error:\r
+  FreePool (DevicePath);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\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
+  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
+                                   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
+                                   bus driver.\r
+\r
+  @retval EFI_SUCCESS              The device specified by ControllerHandle and\r
+                                   RemainingDevicePath is supported by the driver specified by This.\r
+  @retval EFI_ALREADY_STARTED      The device specified by ControllerHandle and\r
+                                   RemainingDevicePath is already being managed by the driver\r
+                                   specified by This.\r
+  @retval EFI_ACCESS_DENIED        The device specified by ControllerHandle and\r
+                                   RemainingDevicePath is already being managed by a different\r
+                                   driver or an application that requires exclusive access.\r
+                                   Currently not implemented.\r
+  @retval EFI_UNSUPPORTED          The device specified by ControllerHandle and\r
+                                   RemainingDevicePath is not supported by the driver specified by This.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EmmcDxeDriverBindingSupported (\r
+  IN EFI_DRIVER_BINDING_PROTOCOL   *This,\r
+  IN EFI_HANDLE                    Controller,\r
+  IN EFI_DEVICE_PATH_PROTOCOL      *RemainingDevicePath\r
+  )\r
+{\r
+  EFI_STATUS                       Status;\r
+  EFI_DEVICE_PATH_PROTOCOL         *ParentDevicePath;\r
+  EFI_SD_MMC_PASS_THRU_PROTOCOL    *PassThru;\r
+  UINT8                            Slot;\r
+\r
+  //\r
+  // Test EFI_SD_MMC_PASS_THRU_PROTOCOL on the controller handle.\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  Controller,\r
+                  &gEfiSdMmcPassThruProtocolGuid,\r
+                  (VOID**) &PassThru,\r
+                  This->DriverBindingHandle,\r
+                  Controller,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+\r
+  if (Status == EFI_ALREADY_STARTED) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Test RemainingDevicePath is valid or not.\r
+  //\r
+  if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {\r
+    Status = PassThru->GetSlotNumber (PassThru, RemainingDevicePath, &Slot);\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
+             &gEfiSdMmcPassThruProtocolGuid,\r
+             This->DriverBindingHandle,\r
+             Controller\r
+             );\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Close the I/O Abstraction(s) used to perform the supported test\r
+  //\r
+  gBS->CloseProtocol (\r
+         Controller,\r
+         &gEfiSdMmcPassThruProtocolGuid,\r
+         This->DriverBindingHandle,\r
+         Controller\r
+         );\r
+\r
+  //\r
+  // Open the EFI Device Path protocol needed to perform the supported test\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  Controller,\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  (VOID **) &ParentDevicePath,\r
+                  This->DriverBindingHandle,\r
+                  Controller,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  return Status;\r
+}\r
+\r
+/**\r
+  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
+  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
+\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
+                                   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
+                                   RemainingDevicePath is created by this driver.\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
+  @retval EFI_SUCCESS              The device was started.\r
+  @retval EFI_DEVICE_ERROR         The device could not be started due to a device error.Currently not implemented.\r
+  @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a lack of resources.\r
+  @retval Others                   The driver failded to start the device.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EmmcDxeDriverBindingStart (\r
+  IN EFI_DRIVER_BINDING_PROTOCOL   *This,\r
+  IN EFI_HANDLE                    Controller,\r
+  IN EFI_DEVICE_PATH_PROTOCOL      *RemainingDevicePath\r
+  )\r
+{\r
+  EFI_STATUS                       Status;\r
+  EFI_SD_MMC_PASS_THRU_PROTOCOL    *PassThru;\r
+  EFI_DEVICE_PATH_PROTOCOL         *ParentDevicePath;\r
+  EMMC_DRIVER_PRIVATE_DATA         *Private;\r
+  UINT8                            Slot;\r
+\r
+  Private  = NULL;\r
+  PassThru = NULL;\r
+  Status = gBS->OpenProtocol (\r
+                  Controller,\r
+                  &gEfiSdMmcPassThruProtocolGuid,\r
+                  (VOID **) &PassThru,\r
+                  This->DriverBindingHandle,\r
+                  Controller,\r
+                  EFI_OPEN_PROTOCOL_BY_DRIVER\r
+                  );\r
+  if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Check EFI_ALREADY_STARTED to reuse the original EMMC_DRIVER_PRIVATE_DATA.\r
+  //\r
+  if (Status != EFI_ALREADY_STARTED) {\r
+    Private = AllocateZeroPool (sizeof (EMMC_DRIVER_PRIVATE_DATA));\r
+    if (Private == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Error;\r
+    }\r
+\r
+    Status = gBS->OpenProtocol (\r
+                    Controller,\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    (VOID **) &ParentDevicePath,\r
+                    This->DriverBindingHandle,\r
+                    Controller,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+    Private->PassThru            = PassThru;\r
+    Private->Controller          = Controller;\r
+    Private->ParentDevicePath    = ParentDevicePath;\r
+    Private->DriverBindingHandle = This->DriverBindingHandle;\r
+\r
+    Status = gBS->InstallProtocolInterface (\r
+                    &Controller,\r
+                    &gEfiCallerIdGuid,\r
+                    EFI_NATIVE_INTERFACE,\r
+                    Private\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Error;\r
+    }\r
+  } else {\r
+    Status = gBS->OpenProtocol (\r
+                    Controller,\r
+                    &gEfiCallerIdGuid,\r
+                    (VOID **) &Private,\r
+                    This->DriverBindingHandle,\r
+                    Controller,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Error;\r
+    }\r
+  }\r
+\r
+  if (RemainingDevicePath == NULL) {\r
+    Slot = 0xFF;\r
+    while (TRUE) {\r
+      Status = PassThru->GetNextSlot (PassThru, &Slot);\r
+      if (EFI_ERROR (Status)) {\r
+        //\r
+        // Cannot find more legal slots.\r
+        //\r
+        Status = EFI_SUCCESS;\r
+        break;\r
+      }\r
+\r
+      Status = DiscoverEmmcDevice (Private, Slot, NULL);\r
+      if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
+        break;\r
+      }\r
+    }\r
+  } else if (!IsDevicePathEnd (RemainingDevicePath)) {\r
+    Status = PassThru->GetSlotNumber (PassThru, RemainingDevicePath, &Slot);\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = DiscoverEmmcDevice (Private, Slot, NextDevicePathNode (RemainingDevicePath));\r
+    }\r
+  }\r
+\r
+Error:\r
+  if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
+    gBS->CloseProtocol (\r
+           Controller,\r
+           &gEfiSdMmcPassThruProtocolGuid,\r
+           This->DriverBindingHandle,\r
+           Controller\r
+           );\r
+\r
+    if (Private != NULL) {\r
+      gBS->UninstallMultipleProtocolInterfaces (\r
+             Controller,\r
+             &gEfiCallerIdGuid,\r
+             Private,\r
+             NULL\r
+             );\r
+      FreePool (Private);\r
+    }\r
+  }\r
+  return Status;\r
+}\r
+\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
+  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
+  2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
+     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
+  @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
+                                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
+                                if NumberOfChildren is 0.\r
+\r
+  @retval EFI_SUCCESS           The device was stopped.\r
+  @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device error.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EmmcDxeDriverBindingStop (\r
+  IN  EFI_DRIVER_BINDING_PROTOCOL     *This,\r
+  IN  EFI_HANDLE                      Controller,\r
+  IN  UINTN                           NumberOfChildren,\r
+  IN  EFI_HANDLE                      *ChildHandleBuffer\r
+  )\r
+{\r
+  EFI_STATUS                             Status;\r
+  BOOLEAN                                AllChildrenStopped;\r
+  UINTN                                  Index;\r
+  EFI_DEVICE_PATH_PROTOCOL               *DevicePath;\r
+  EMMC_DRIVER_PRIVATE_DATA               *Private;\r
+  EMMC_DEVICE                            *Device;\r
+  EMMC_PARTITION                         *Partition;\r
+  EFI_BLOCK_IO_PROTOCOL                  *BlockIo;\r
+  EFI_BLOCK_IO2_PROTOCOL                 *BlockIo2;\r
+  EFI_STORAGE_SECURITY_COMMAND_PROTOCOL  *StorageSecurity;\r
+  LIST_ENTRY                             *Link;\r
+  LIST_ENTRY                             *NextLink;\r
+  EMMC_REQUEST                           *Request;\r
+\r
+  BlockIo  = NULL;\r
+  BlockIo2 = NULL;\r
+  if (NumberOfChildren == 0) {\r
+    Status = gBS->OpenProtocol (\r
+                    Controller,\r
+                    &gEfiCallerIdGuid,\r
+                    (VOID **) &Private,\r
+                    This->DriverBindingHandle,\r
+                    Controller,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+\r
+    for (Index = 0; Index < EMMC_MAX_DEVICES; Index++) {\r
+      Device = &Private->Device[Index];\r
+      Status = gBS->OpenProtocol (\r
+                      Device->Handle,\r
+                      &gEfiDevicePathProtocolGuid,\r
+                      (VOID **) &DevicePath,\r
+                      This->DriverBindingHandle,\r
+                      Controller,\r
+                      EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        continue;\r
+      }\r
+      ASSERT (DevicePath == Device->DevicePath);\r
+      gBS->UninstallProtocolInterface (\r
+             Device->Handle,\r
+             &gEfiDevicePathProtocolGuid,\r
+             DevicePath\r
+             );\r
+      FreePool (Device->DevicePath);\r
+    }\r
+\r
+    gBS->UninstallProtocolInterface (\r
+          Controller,\r
+          &gEfiCallerIdGuid,\r
+          Private\r
+          );\r
+    gBS->CloseProtocol (\r
+          Controller,\r
+          &gEfiSdMmcPassThruProtocolGuid,\r
+          This->DriverBindingHandle,\r
+          Controller\r
+          );\r
+    FreePool (Private);\r
+\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  AllChildrenStopped = TRUE;\r
+\r
+  for (Index = 0; Index < NumberOfChildren; Index++) {\r
+    Status = gBS->OpenProtocol (\r
+                    ChildHandleBuffer[Index],\r
+                    &gEfiBlockIoProtocolGuid,\r
+                    (VOID **) &BlockIo,\r
+                    This->DriverBindingHandle,\r
+                    Controller,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      Status = gBS->OpenProtocol (\r
+                      ChildHandleBuffer[Index],\r
+                      &gEfiBlockIo2ProtocolGuid,\r
+                      (VOID **) &BlockIo2,\r
+                      This->DriverBindingHandle,\r
+                      Controller,\r
+                      EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        AllChildrenStopped = FALSE;\r
+        continue;\r
+      }\r
+    }\r
+\r
+    if (BlockIo != NULL) {\r
+      Partition = EMMC_PARTITION_DATA_FROM_BLKIO (BlockIo);\r
+    } else {\r
+      ASSERT (BlockIo2 != NULL);\r
+      Partition = EMMC_PARTITION_DATA_FROM_BLKIO2 (BlockIo2);\r
+    }\r
+\r
+    for (Link = GetFirstNode (&Partition->Queue);\r
+         !IsNull (&Partition->Queue, Link);\r
+         Link = NextLink) {\r
+      NextLink = GetNextNode (&Partition->Queue, Link);\r
+\r
+      RemoveEntryList (Link);\r
+      Request = EMMC_REQUEST_FROM_LINK (Link);\r
+\r
+      gBS->CloseEvent (Request->Event);\r
+      Request->Token->TransactionStatus = EFI_ABORTED;\r
+\r
+      if (Request->IsEnd) {\r
+        gBS->SignalEvent (Request->Token->Event);\r
+      }\r
+\r
+      FreePool (Request);\r
+    }\r
+\r
+    //\r
+    // Close the child handle\r
+    //\r
+    Status = gBS->CloseProtocol (\r
+                    Controller,\r
+                    &gEfiSdMmcPassThruProtocolGuid,\r
+                    This->DriverBindingHandle,\r
+                    ChildHandleBuffer[Index]\r
+                    );\r
+\r
+    Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                    ChildHandleBuffer[Index],\r
+                    &gEfiDevicePathProtocolGuid,\r
+                    Partition->DevicePath,\r
+                    &gEfiBlockIoProtocolGuid,\r
+                    &Partition->BlockIo,\r
+                    &gEfiBlockIo2ProtocolGuid,\r
+                    &Partition->BlockIo2,\r
+                    NULL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      AllChildrenStopped = FALSE;\r
+      gBS->OpenProtocol (\r
+             Controller,\r
+             &gEfiSdMmcPassThruProtocolGuid,\r
+             (VOID **)&Partition->Device->Private->PassThru,\r
+             This->DriverBindingHandle,\r
+             ChildHandleBuffer[Index],\r
+             EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+             );\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // If Storage Security Command Protocol is installed, then uninstall this protocol.\r
+    //\r
+    Status = gBS->OpenProtocol (\r
+                    ChildHandleBuffer[Index],\r
+                    &gEfiStorageSecurityCommandProtocolGuid,\r
+                    (VOID **) &StorageSecurity,\r
+                    This->DriverBindingHandle,\r
+                    Controller,\r
+                    EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                    );\r
+\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = gBS->UninstallProtocolInterface (\r
+                      ChildHandleBuffer[Index],\r
+                      &gEfiStorageSecurityCommandProtocolGuid,\r
+                      &Partition->StorageSecurity\r
+                      );\r
+      if (EFI_ERROR (Status)) {\r
+        gBS->OpenProtocol (\r
+          Controller,\r
+          &gEfiSdMmcPassThruProtocolGuid,\r
+          (VOID **) &Partition->Device->Private->PassThru,\r
+          This->DriverBindingHandle,\r
+          ChildHandleBuffer[Index],\r
+          EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+          );\r
+        AllChildrenStopped = FALSE;\r
+        continue;\r
+      }\r
+    }\r
+\r
+    FreePool (Partition->DevicePath);\r
+  }\r
+\r
+  if (!AllChildrenStopped) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  The user Entry Point for module EmmcDxe. The user code starts with this function.\r
+\r
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.\r
+  @param[in] SystemTable    A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS       The entry point is executed successfully.\r
+  @retval other             Some errors occur when executing this entry point.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeEmmcDxe (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+\r
+  //\r
+  // Install driver model protocol(s).\r
+  //\r
+  Status = EfiLibInstallDriverBindingComponentName2 (\r
+             ImageHandle,\r
+             SystemTable,\r
+             &gEmmcDxeDriverBinding,\r
+             ImageHandle,\r
+             &gEmmcDxeComponentName,\r
+             &gEmmcDxeComponentName2\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
+}\r
+\r