2 The EmmcDxe driver is used to manage the EMMC device.
4 It produces BlockIo, BlockIo2 and StorageSecurity protocols to allow upper layer
5 access the EMMC device.
7 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
15 // EmmcDxe Driver Binding Protocol Instance
17 EFI_DRIVER_BINDING_PROTOCOL gEmmcDxeDriverBinding
= {
18 EmmcDxeDriverBindingSupported
,
19 EmmcDxeDriverBindingStart
,
20 EmmcDxeDriverBindingStop
,
27 // Template for Emmc Partitions.
29 EMMC_PARTITION mEmmcPartitionTemplate
= {
30 EMMC_PARTITION_SIGNATURE
, // Signature
32 EmmcPartitionUnknown
, // PartitionType
36 EFI_BLOCK_IO_PROTOCOL_REVISION
,
52 FALSE
, // RemovableMedia
54 FALSE
, // LogicPartition
56 FALSE
, // WritingCache
62 EmmcSecurityProtocolIn
,
63 EmmcSecurityProtocolOut
66 EFI_ERASE_BLOCK_PROTOCOL_REVISION
,
71 EFI_DISK_INFO_SD_MMC_INTERFACE_GUID
,
74 EmmcDiskInfoSenseData
,
85 Decode and print EMMC CSD Register content.
87 @param[in] Csd Pointer to EMMC_CSD data structure.
89 @retval EFI_SUCCESS The function completed successfully
96 DEBUG ((DEBUG_INFO
, "== Dump Emmc Csd Register==\n"));
97 DEBUG ((DEBUG_INFO
, " CSD structure 0x%x\n", Csd
->CsdStructure
));
98 DEBUG ((DEBUG_INFO
, " System specification version 0x%x\n", Csd
->SpecVers
));
99 DEBUG ((DEBUG_INFO
, " Data read access-time 1 0x%x\n", Csd
->Taac
));
100 DEBUG ((DEBUG_INFO
, " Data read access-time 2 0x%x\n", Csd
->Nsac
));
101 DEBUG ((DEBUG_INFO
, " Max. bus clock frequency 0x%x\n", Csd
->TranSpeed
));
102 DEBUG ((DEBUG_INFO
, " Device command classes 0x%x\n", Csd
->Ccc
));
103 DEBUG ((DEBUG_INFO
, " Max. read data block length 0x%x\n", Csd
->ReadBlLen
));
104 DEBUG ((DEBUG_INFO
, " Partial blocks for read allowed 0x%x\n", Csd
->ReadBlPartial
));
105 DEBUG ((DEBUG_INFO
, " Write block misalignment 0x%x\n", Csd
->WriteBlkMisalign
));
106 DEBUG ((DEBUG_INFO
, " Read block misalignment 0x%x\n", Csd
->ReadBlkMisalign
));
107 DEBUG ((DEBUG_INFO
, " DSR implemented 0x%x\n", Csd
->DsrImp
));
108 DEBUG ((DEBUG_INFO
, " Device size 0x%x\n", Csd
->CSizeLow
| (Csd
->CSizeHigh
<< 2)));
109 DEBUG ((DEBUG_INFO
, " Max. read current @ VDD min 0x%x\n", Csd
->VddRCurrMin
));
110 DEBUG ((DEBUG_INFO
, " Max. read current @ VDD max 0x%x\n", Csd
->VddRCurrMax
));
111 DEBUG ((DEBUG_INFO
, " Max. write current @ VDD min 0x%x\n", Csd
->VddWCurrMin
));
112 DEBUG ((DEBUG_INFO
, " Max. write current @ VDD max 0x%x\n", Csd
->VddWCurrMax
));
113 DEBUG ((DEBUG_INFO
, " Device size multiplier 0x%x\n", Csd
->CSizeMult
));
114 DEBUG ((DEBUG_INFO
, " Erase group size 0x%x\n", Csd
->EraseGrpSize
));
115 DEBUG ((DEBUG_INFO
, " Erase group size multiplier 0x%x\n", Csd
->EraseGrpMult
));
116 DEBUG ((DEBUG_INFO
, " Write protect group size 0x%x\n", Csd
->WpGrpSize
));
117 DEBUG ((DEBUG_INFO
, " Write protect group enable 0x%x\n", Csd
->WpGrpEnable
));
118 DEBUG ((DEBUG_INFO
, " Manufacturer default ECC 0x%x\n", Csd
->DefaultEcc
));
119 DEBUG ((DEBUG_INFO
, " Write speed factor 0x%x\n", Csd
->R2WFactor
));
120 DEBUG ((DEBUG_INFO
, " Max. write data block length 0x%x\n", Csd
->WriteBlLen
));
121 DEBUG ((DEBUG_INFO
, " Partial blocks for write allowed 0x%x\n", Csd
->WriteBlPartial
));
122 DEBUG ((DEBUG_INFO
, " Content protection application 0x%x\n", Csd
->ContentProtApp
));
123 DEBUG ((DEBUG_INFO
, " File format group 0x%x\n", Csd
->FileFormatGrp
));
124 DEBUG ((DEBUG_INFO
, " Copy flag (OTP) 0x%x\n", Csd
->Copy
));
125 DEBUG ((DEBUG_INFO
, " Permanent write protection 0x%x\n", Csd
->PermWriteProtect
));
126 DEBUG ((DEBUG_INFO
, " Temporary write protection 0x%x\n", Csd
->TmpWriteProtect
));
127 DEBUG ((DEBUG_INFO
, " File format 0x%x\n", Csd
->FileFormat
));
128 DEBUG ((DEBUG_INFO
, " ECC code 0x%x\n", Csd
->Ecc
));
134 Decode and print EMMC EXT_CSD Register content.
136 @param[in] ExtCsd Pointer to the EMMC_EXT_CSD data structure.
138 @retval EFI_SUCCESS The function completed successfully
142 IN EMMC_EXT_CSD
*ExtCsd
145 DEBUG ((DEBUG_INFO
, "==Dump Emmc ExtCsd Register==\n"));
146 DEBUG ((DEBUG_INFO
, " Supported Command Sets 0x%x\n", ExtCsd
->CmdSet
));
147 DEBUG ((DEBUG_INFO
, " HPI features 0x%x\n", ExtCsd
->HpiFeatures
));
148 DEBUG ((DEBUG_INFO
, " Background operations support 0x%x\n", ExtCsd
->BkOpsSupport
));
149 DEBUG ((DEBUG_INFO
, " Background operations status 0x%x\n", ExtCsd
->BkopsStatus
));
150 DEBUG ((DEBUG_INFO
, " Number of correctly programmed sectors 0x%x\n", *((UINT32
*)&ExtCsd
->CorrectlyPrgSectorsNum
[0])));
151 DEBUG ((DEBUG_INFO
, " Initialization time after partitioning 0x%x\n", ExtCsd
->IniTimeoutAp
));
152 DEBUG ((DEBUG_INFO
, " TRIM Multiplier 0x%x\n", ExtCsd
->TrimMult
));
153 DEBUG ((DEBUG_INFO
, " Secure Feature support 0x%x\n", ExtCsd
->SecFeatureSupport
));
154 DEBUG ((DEBUG_INFO
, " Secure Erase Multiplier 0x%x\n", ExtCsd
->SecEraseMult
));
155 DEBUG ((DEBUG_INFO
, " Secure TRIM Multiplier 0x%x\n", ExtCsd
->SecTrimMult
));
156 DEBUG ((DEBUG_INFO
, " Boot information 0x%x\n", ExtCsd
->BootInfo
));
157 DEBUG ((DEBUG_INFO
, " Boot partition size 0x%x\n", ExtCsd
->BootSizeMult
));
158 DEBUG ((DEBUG_INFO
, " Access size 0x%x\n", ExtCsd
->AccSize
));
159 DEBUG ((DEBUG_INFO
, " High-capacity erase unit size 0x%x\n", ExtCsd
->HcEraseGrpSize
));
160 DEBUG ((DEBUG_INFO
, " High-capacity erase timeout 0x%x\n", ExtCsd
->EraseTimeoutMult
));
161 DEBUG ((DEBUG_INFO
, " Reliable write sector count 0x%x\n", ExtCsd
->RelWrSecC
));
162 DEBUG ((DEBUG_INFO
, " High-capacity write protect group size 0x%x\n", ExtCsd
->HcWpGrpSize
));
163 DEBUG ((DEBUG_INFO
, " Sleep/awake timeout 0x%x\n", ExtCsd
->SATimeout
));
164 DEBUG ((DEBUG_INFO
, " Sector Count 0x%x\n", *((UINT32
*)&ExtCsd
->SecCount
[0])));
165 DEBUG ((DEBUG_INFO
, " Partition switching timing 0x%x\n", ExtCsd
->PartitionSwitchTime
));
166 DEBUG ((DEBUG_INFO
, " Out-of-interrupt busy timing 0x%x\n", ExtCsd
->OutOfInterruptTime
));
167 DEBUG ((DEBUG_INFO
, " I/O Driver Strength 0x%x\n", ExtCsd
->DriverStrength
));
168 DEBUG ((DEBUG_INFO
, " Device type 0x%x\n", ExtCsd
->DeviceType
));
169 DEBUG ((DEBUG_INFO
, " CSD STRUCTURE 0x%x\n", ExtCsd
->CsdStructure
));
170 DEBUG ((DEBUG_INFO
, " Extended CSD revision 0x%x\n", ExtCsd
->ExtCsdRev
));
171 DEBUG ((DEBUG_INFO
, " Command set 0x%x\n", ExtCsd
->CmdSet
));
172 DEBUG ((DEBUG_INFO
, " Command set revision 0x%x\n", ExtCsd
->CmdSetRev
));
173 DEBUG ((DEBUG_INFO
, " Power class 0x%x\n", ExtCsd
->PowerClass
));
174 DEBUG ((DEBUG_INFO
, " High-speed interface timing 0x%x\n", ExtCsd
->HsTiming
));
175 DEBUG ((DEBUG_INFO
, " Bus width mode 0x%x\n", ExtCsd
->BusWidth
));
176 DEBUG ((DEBUG_INFO
, " Erased memory content 0x%x\n", ExtCsd
->ErasedMemCont
));
177 DEBUG ((DEBUG_INFO
, " Partition configuration 0x%x\n", ExtCsd
->PartitionConfig
));
178 DEBUG ((DEBUG_INFO
, " Boot config protection 0x%x\n", ExtCsd
->BootConfigProt
));
179 DEBUG ((DEBUG_INFO
, " Boot bus Conditions 0x%x\n", ExtCsd
->BootBusConditions
));
180 DEBUG ((DEBUG_INFO
, " High-density erase group definition 0x%x\n", ExtCsd
->EraseGroupDef
));
181 DEBUG ((DEBUG_INFO
, " Boot write protection status register 0x%x\n", ExtCsd
->BootWpStatus
));
182 DEBUG ((DEBUG_INFO
, " Boot area write protection register 0x%x\n", ExtCsd
->BootWp
));
183 DEBUG ((DEBUG_INFO
, " User area write protection register 0x%x\n", ExtCsd
->UserWp
));
184 DEBUG ((DEBUG_INFO
, " FW configuration 0x%x\n", ExtCsd
->FwConfig
));
185 DEBUG ((DEBUG_INFO
, " RPMB Size 0x%x\n", ExtCsd
->RpmbSizeMult
));
186 DEBUG ((DEBUG_INFO
, " H/W reset function 0x%x\n", ExtCsd
->RstFunction
));
187 DEBUG ((DEBUG_INFO
, " Partitioning Support 0x%x\n", ExtCsd
->PartitioningSupport
));
190 " Max Enhanced Area Size 0x%02x%02x%02x\n", \
191 ExtCsd
->MaxEnhSizeMult
[2],
192 ExtCsd
->MaxEnhSizeMult
[1],
193 ExtCsd
->MaxEnhSizeMult
[0]
195 DEBUG ((DEBUG_INFO
, " Partitions attribute 0x%x\n", ExtCsd
->PartitionsAttribute
));
196 DEBUG ((DEBUG_INFO
, " Partitioning Setting 0x%x\n", ExtCsd
->PartitionSettingCompleted
));
199 " General Purpose Partition 1 Size 0x%02x%02x%02x\n", \
200 ExtCsd
->GpSizeMult
[2],
201 ExtCsd
->GpSizeMult
[1],
202 ExtCsd
->GpSizeMult
[0]
206 " General Purpose Partition 2 Size 0x%02x%02x%02x\n", \
207 ExtCsd
->GpSizeMult
[5],
208 ExtCsd
->GpSizeMult
[4],
209 ExtCsd
->GpSizeMult
[3]
213 " General Purpose Partition 3 Size 0x%02x%02x%02x\n", \
214 ExtCsd
->GpSizeMult
[8],
215 ExtCsd
->GpSizeMult
[7],
216 ExtCsd
->GpSizeMult
[6]
220 " General Purpose Partition 4 Size 0x%02x%02x%02x\n", \
221 ExtCsd
->GpSizeMult
[11],
222 ExtCsd
->GpSizeMult
[10],
223 ExtCsd
->GpSizeMult
[9]
227 " Enhanced User Data Area Size 0x%02x%02x%02x\n", \
228 ExtCsd
->EnhSizeMult
[2],
229 ExtCsd
->EnhSizeMult
[1],
230 ExtCsd
->EnhSizeMult
[0]
232 DEBUG ((DEBUG_INFO
, " Enhanced User Data Start Address 0x%x\n", *((UINT32
*)&ExtCsd
->EnhStartAddr
[0])));
233 DEBUG ((DEBUG_INFO
, " Bad Block Management mode 0x%x\n", ExtCsd
->SecBadBlkMgmnt
));
234 DEBUG ((DEBUG_INFO
, " Native sector size 0x%x\n", ExtCsd
->NativeSectorSize
));
235 DEBUG ((DEBUG_INFO
, " Sector size emulation 0x%x\n", ExtCsd
->UseNativeSector
));
236 DEBUG ((DEBUG_INFO
, " Sector size 0x%x\n", ExtCsd
->DataSectorSize
));
242 Get EMMC device model name.
244 @param[in, out] Device The pointer to the EMMC_DEVICE data structure.
245 @param[in] Cid Pointer to EMMC_CID data structure.
247 @retval EFI_SUCCESS The function completed successfully
252 IN OUT EMMC_DEVICE
*Device
,
256 CHAR8 String
[EMMC_MODEL_NAME_MAX_LEN
];
258 ZeroMem (String
, sizeof (String
));
259 CopyMem (String
, &Cid
->OemId
, sizeof (Cid
->OemId
));
260 String
[sizeof (Cid
->OemId
)] = ' ';
261 CopyMem (String
+ sizeof (Cid
->OemId
) + 1, Cid
->ProductName
, sizeof (Cid
->ProductName
));
262 String
[sizeof (Cid
->OemId
) + sizeof (Cid
->ProductName
)] = ' ';
263 CopyMem (String
+ sizeof (Cid
->OemId
) + sizeof (Cid
->ProductName
) + 1, Cid
->ProductSerialNumber
, sizeof (Cid
->ProductSerialNumber
));
265 AsciiStrToUnicodeStrS (String
, Device
->ModelName
, sizeof (Device
->ModelName
) / sizeof (Device
->ModelName
[0]));
271 Discover all partitions in the EMMC device.
273 @param[in] Device The pointer to the EMMC_DEVICE data structure.
275 @retval EFI_SUCCESS All the partitions in the device are successfully enumerated.
276 @return Others Some error occurs when enumerating the partitions.
280 DiscoverAllPartitions (
281 IN EMMC_DEVICE
*Device
285 EMMC_PARTITION
*Partition
;
288 EMMC_EXT_CSD
*ExtCsd
;
298 Status
= EmmcSendStatus (Device
, Slot
+ 1, &DevStatus
);
299 if (EFI_ERROR (Status
)) {
304 // Deselect the device to force it enter stby mode before getting CSD
306 // Note here we don't judge return status as some EMMC devices return
307 // error but the state has been stby.
309 EmmcSelect (Device
, 0);
311 Status
= EmmcSendStatus (Device
, Slot
+ 1, &DevStatus
);
312 if (EFI_ERROR (Status
)) {
317 Status
= EmmcGetCsd (Device
, Slot
+ 1, Csd
);
318 if (EFI_ERROR (Status
)) {
324 if ((Csd
->CSizeLow
| Csd
->CSizeHigh
<< 2) == 0xFFF) {
325 Device
->SectorAddressing
= TRUE
;
327 Device
->SectorAddressing
= FALSE
;
331 Status
= EmmcGetCid (Device
, Slot
+ 1, Cid
);
332 if (EFI_ERROR (Status
)) {
336 Status
= EmmcSelect (Device
, Slot
+ 1);
337 if (EFI_ERROR (Status
)) {
341 ExtCsd
= &Device
->ExtCsd
;
342 Status
= EmmcGetExtCsd (Device
, ExtCsd
);
343 if (EFI_ERROR (Status
)) {
349 if (ExtCsd
->ExtCsdRev
< 5) {
350 DEBUG ((DEBUG_ERROR
, "The EMMC device version is too low, we don't support!!!\n"));
351 return EFI_UNSUPPORTED
;
354 if ((ExtCsd
->PartitioningSupport
& BIT0
) != BIT0
) {
355 DEBUG ((DEBUG_ERROR
, "The EMMC device doesn't support Partition Feature!!!\n"));
356 return EFI_UNSUPPORTED
;
359 for (Index
= 0; Index
< EMMC_MAX_PARTITIONS
; Index
++) {
360 Partition
= &Device
->Partition
[Index
];
361 CopyMem (Partition
, &mEmmcPartitionTemplate
, sizeof (EMMC_PARTITION
));
362 Partition
->Device
= Device
;
363 InitializeListHead (&Partition
->Queue
);
364 Partition
->BlockIo
.Media
= &Partition
->BlockMedia
;
365 Partition
->BlockIo2
.Media
= &Partition
->BlockMedia
;
366 Partition
->PartitionType
= Index
;
367 Partition
->BlockMedia
.IoAlign
= Device
->Private
->PassThru
->IoAlign
;
368 Partition
->BlockMedia
.BlockSize
= 0x200;
369 Partition
->BlockMedia
.LastBlock
= 0x00;
370 Partition
->BlockMedia
.RemovableMedia
= FALSE
;
371 Partition
->BlockMedia
.MediaPresent
= TRUE
;
372 Partition
->BlockMedia
.LogicalPartition
= FALSE
;
375 case EmmcPartitionUserData
:
376 SecCount
= *(UINT32
*)&ExtCsd
->SecCount
;
377 Capacity
= MultU64x32 ((UINT64
)SecCount
, 0x200);
379 case EmmcPartitionBoot1
:
380 case EmmcPartitionBoot2
:
381 Capacity
= ExtCsd
->BootSizeMult
* SIZE_128KB
;
383 case EmmcPartitionRPMB
:
384 Capacity
= ExtCsd
->RpmbSizeMult
* SIZE_128KB
;
386 case EmmcPartitionGP1
:
387 GpSizeMult
= (UINT32
)(ExtCsd
->GpSizeMult
[0] | (ExtCsd
->GpSizeMult
[1] << 8) | (ExtCsd
->GpSizeMult
[2] << 16));
388 Capacity
= MultU64x32 (MultU64x32 (MultU64x32 ((UINT64
)GpSizeMult
, ExtCsd
->HcWpGrpSize
), ExtCsd
->HcEraseGrpSize
), SIZE_512KB
);
390 case EmmcPartitionGP2
:
391 GpSizeMult
= (UINT32
)(ExtCsd
->GpSizeMult
[3] | (ExtCsd
->GpSizeMult
[4] << 8) | (ExtCsd
->GpSizeMult
[5] << 16));
392 Capacity
= MultU64x32 (MultU64x32 (MultU64x32 ((UINT64
)GpSizeMult
, ExtCsd
->HcWpGrpSize
), ExtCsd
->HcEraseGrpSize
), SIZE_512KB
);
394 case EmmcPartitionGP3
:
395 GpSizeMult
= (UINT32
)(ExtCsd
->GpSizeMult
[6] | (ExtCsd
->GpSizeMult
[7] << 8) | (ExtCsd
->GpSizeMult
[8] << 16));
396 Capacity
= MultU64x32 (MultU64x32 (MultU64x32 ((UINT64
)GpSizeMult
, ExtCsd
->HcWpGrpSize
), ExtCsd
->HcEraseGrpSize
), SIZE_512KB
);
398 case EmmcPartitionGP4
:
399 GpSizeMult
= (UINT32
)(ExtCsd
->GpSizeMult
[9] | (ExtCsd
->GpSizeMult
[10] << 8) | (ExtCsd
->GpSizeMult
[11] << 16));
400 Capacity
= MultU64x32 (MultU64x32 (MultU64x32 ((UINT64
)GpSizeMult
, ExtCsd
->HcWpGrpSize
), ExtCsd
->HcEraseGrpSize
), SIZE_512KB
);
404 return EFI_INVALID_PARAMETER
;
408 Partition
->Enable
= TRUE
;
409 Partition
->BlockMedia
.LastBlock
= DivU64x32 (Capacity
, Partition
->BlockMedia
.BlockSize
) - 1;
412 if ((ExtCsd
->EraseGroupDef
& BIT0
) == 0) {
413 if (Csd
->WriteBlLen
< 9) {
414 Partition
->EraseBlock
.EraseLengthGranularity
= 1;
416 Partition
->EraseBlock
.EraseLengthGranularity
= (Csd
->EraseGrpMult
+ 1) * (Csd
->EraseGrpSize
+ 1) * (1 << (Csd
->WriteBlLen
- 9));
419 Partition
->EraseBlock
.EraseLengthGranularity
= 1024 * ExtCsd
->HcEraseGrpSize
;
427 Install BlkIo, BlkIo2 and Ssp protocols for the specified partition in the EMMC device.
429 @param[in] Device The pointer to the EMMC_DEVICE data structure.
430 @param[in] Index The index of the partition.
432 @retval EFI_SUCCESS The protocols are installed successfully.
433 @retval Others Some error occurs when installing the protocols.
437 InstallProtocolOnPartition (
438 IN EMMC_DEVICE
*Device
,
443 EMMC_PARTITION
*Partition
;
444 CONTROLLER_DEVICE_PATH ControlNode
;
445 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
446 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
447 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
448 EFI_HANDLE DeviceHandle
;
453 ParentDevicePath
= Device
->DevicePath
;
455 ControlNode
.Header
.Type
= HARDWARE_DEVICE_PATH
;
456 ControlNode
.Header
.SubType
= HW_CONTROLLER_DP
;
457 SetDevicePathNodeLength (&ControlNode
.Header
, sizeof (CONTROLLER_DEVICE_PATH
));
458 ControlNode
.ControllerNumber
= Index
;
460 DevicePath
= AppendDevicePathNode (ParentDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)&ControlNode
);
461 if (DevicePath
== NULL
) {
462 Status
= EFI_OUT_OF_RESOURCES
;
467 RemainingDevicePath
= DevicePath
;
468 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &DeviceHandle
);
469 if (!EFI_ERROR (Status
) && (DeviceHandle
!= NULL
) && IsDevicePathEnd (RemainingDevicePath
)) {
470 Status
= EFI_ALREADY_STARTED
;
474 Partition
= &Device
->Partition
[Index
];
475 Partition
->DevicePath
= DevicePath
;
476 if (Partition
->Enable
) {
478 // Install BlkIo/BlkIo2/Ssp for the specified partition
480 if (Partition
->PartitionType
!= EmmcPartitionRPMB
) {
481 Status
= gBS
->InstallMultipleProtocolInterfaces (
483 &gEfiDevicePathProtocolGuid
,
484 Partition
->DevicePath
,
485 &gEfiBlockIoProtocolGuid
,
487 &gEfiBlockIo2ProtocolGuid
,
488 &Partition
->BlockIo2
,
489 &gEfiEraseBlockProtocolGuid
,
490 &Partition
->EraseBlock
,
491 &gEfiDiskInfoProtocolGuid
,
492 &Partition
->DiskInfo
,
495 if (EFI_ERROR (Status
)) {
499 if (((Partition
->PartitionType
== EmmcPartitionUserData
) ||
500 (Partition
->PartitionType
== EmmcPartitionBoot1
) ||
501 (Partition
->PartitionType
== EmmcPartitionBoot2
)) &&
502 ((Device
->Csd
.Ccc
& BIT10
) != 0))
504 Status
= gBS
->InstallProtocolInterface (
506 &gEfiStorageSecurityCommandProtocolGuid
,
507 EFI_NATIVE_INTERFACE
,
508 &Partition
->StorageSecurity
510 if (EFI_ERROR (Status
)) {
511 gBS
->UninstallMultipleProtocolInterfaces (
513 &gEfiDevicePathProtocolGuid
,
514 Partition
->DevicePath
,
515 &gEfiBlockIoProtocolGuid
,
517 &gEfiBlockIo2ProtocolGuid
,
518 &Partition
->BlockIo2
,
519 &gEfiEraseBlockProtocolGuid
,
520 &Partition
->EraseBlock
,
521 &gEfiDiskInfoProtocolGuid
,
522 &Partition
->DiskInfo
,
530 Device
->Private
->Controller
,
531 &gEfiSdMmcPassThruProtocolGuid
,
532 (VOID
**)&(Device
->Private
->PassThru
),
533 Device
->Private
->DriverBindingHandle
,
535 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
539 Status
= EFI_INVALID_PARAMETER
;
543 if (EFI_ERROR (Status
) && (DevicePath
!= NULL
)) {
544 FreePool (DevicePath
);
551 Scan EMMC Bus to discover the device.
553 @param[in] Private The EMMC driver private data structure.
554 @param[in] Slot The slot number to check device present.
555 @param[in] RemainingDevicePath The pointer to the remaining device path.
557 @retval EFI_SUCCESS Successfully to discover the device and attach
558 SdMmcIoProtocol to it.
559 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
561 @retval EFI_ALREADY_STARTED The device was discovered before.
562 @retval Others Fail to discover the device.
568 IN EMMC_DRIVER_PRIVATE_DATA
*Private
,
570 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
575 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
576 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
577 EFI_DEVICE_PATH_PROTOCOL
*RemainingEmmcDevPath
;
579 EFI_HANDLE DeviceHandle
;
580 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
585 NewDevicePath
= NULL
;
586 RemainingDevicePath
= NULL
;
587 PassThru
= Private
->PassThru
;
588 Device
= &Private
->Device
[Slot
];
591 // Build Device Path to check if the EMMC device present at the slot.
593 Status
= PassThru
->BuildDevicePath (
598 if (EFI_ERROR (Status
)) {
602 if (DevicePath
->SubType
!= MSG_EMMC_DP
) {
603 Status
= EFI_UNSUPPORTED
;
607 NewDevicePath
= AppendDevicePathNode (
608 Private
->ParentDevicePath
,
611 if (NewDevicePath
== NULL
) {
612 Status
= EFI_OUT_OF_RESOURCES
;
617 RemainingEmmcDevPath
= NewDevicePath
;
618 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingEmmcDevPath
, &DeviceHandle
);
620 // The device path to the EMMC device doesn't exist. It means the corresponding device private data hasn't been initialized.
622 if (EFI_ERROR (Status
) || (DeviceHandle
== NULL
) || !IsDevicePathEnd (RemainingEmmcDevPath
)) {
623 Device
->DevicePath
= NewDevicePath
;
625 Device
->Private
= Private
;
627 // Expose user area in the Sd memory card to upper layer.
629 Status
= DiscoverAllPartitions (Device
);
630 if (EFI_ERROR (Status
)) {
631 FreePool (NewDevicePath
);
635 Status
= gBS
->InstallProtocolInterface (
637 &gEfiDevicePathProtocolGuid
,
638 EFI_NATIVE_INTERFACE
,
641 if (EFI_ERROR (Status
)) {
642 FreePool (NewDevicePath
);
646 Device
->ControllerNameTable
= NULL
;
647 GetEmmcModelName (Device
, &Device
->Cid
);
650 gEmmcDxeComponentName
.SupportedLanguages
,
651 &Device
->ControllerNameTable
,
657 gEmmcDxeComponentName2
.SupportedLanguages
,
658 &Device
->ControllerNameTable
,
664 if (RemainingDevicePath
== NULL
) {
666 // Expose all partitions in the Emmc device to upper layer.
668 for (Index
= 0; Index
< EMMC_MAX_PARTITIONS
; Index
++) {
669 InstallProtocolOnPartition (Device
, Index
);
671 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
673 // Enumerate the specified partition
675 Node
= (EFI_DEV_PATH
*)RemainingDevicePath
;
676 if ((DevicePathType (&Node
->DevPath
) != HARDWARE_DEVICE_PATH
) ||
677 (DevicePathSubType (&Node
->DevPath
) != HW_CONTROLLER_DP
) ||
678 (DevicePathNodeLength (&Node
->DevPath
) != sizeof (CONTROLLER_DEVICE_PATH
)))
680 Status
= EFI_INVALID_PARAMETER
;
684 Index
= (UINT8
)Node
->Controller
.ControllerNumber
;
685 if (Index
>= EMMC_MAX_PARTITIONS
) {
686 Status
= EFI_INVALID_PARAMETER
;
690 Status
= InstallProtocolOnPartition (Device
, Index
);
694 FreePool (DevicePath
);
700 Tests to see if this driver supports a given controller. If a child device is provided,
701 it further tests to see if this driver supports creating a handle for the specified child device.
703 This function checks to see if the driver specified by This supports the device specified by
704 ControllerHandle. Drivers will typically use the device path attached to
705 ControllerHandle and/or the services from the bus I/O abstraction attached to
706 ControllerHandle to determine if the driver supports ControllerHandle. This function
707 may be called many times during platform initialization. In order to reduce boot times, the tests
708 performed by this function must be very small, and take as little time as possible to execute. This
709 function must not change the state of any hardware devices, and this function must be aware that the
710 device specified by ControllerHandle may already be managed by the same driver or a
711 different driver. This function must match its calls to AllocatePages() with FreePages(),
712 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
713 Since ControllerHandle may have been previously started by the same driver, if a protocol is
714 already in the opened state, then it must not be closed with CloseProtocol(). This is required
715 to guarantee the state of ControllerHandle is not modified by this function.
717 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
718 @param[in] ControllerHandle The handle of the controller to test. This handle
719 must support a protocol interface that supplies
720 an I/O abstraction to the driver.
721 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
722 parameter is ignored by device drivers, and is optional for bus
723 drivers. For bus drivers, if this parameter is not NULL, then
724 the bus driver must determine if the bus controller specified
725 by ControllerHandle and the child controller specified
726 by RemainingDevicePath are both supported by this
729 @retval EFI_SUCCESS The device specified by ControllerHandle and
730 RemainingDevicePath is supported by the driver specified by This.
731 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
732 RemainingDevicePath is already being managed by the driver
734 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
735 RemainingDevicePath is already being managed by a different
736 driver or an application that requires exclusive access.
737 Currently not implemented.
738 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
739 RemainingDevicePath is not supported by the driver specified by This.
743 EmmcDxeDriverBindingSupported (
744 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
745 IN EFI_HANDLE Controller
,
746 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
750 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
751 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
755 // Test EFI_SD_MMC_PASS_THRU_PROTOCOL on the controller handle.
757 Status
= gBS
->OpenProtocol (
759 &gEfiSdMmcPassThruProtocolGuid
,
761 This
->DriverBindingHandle
,
763 EFI_OPEN_PROTOCOL_BY_DRIVER
766 if (Status
== EFI_ALREADY_STARTED
) {
770 if (EFI_ERROR (Status
)) {
775 // Test RemainingDevicePath is valid or not.
777 if ((RemainingDevicePath
!= NULL
) && !IsDevicePathEnd (RemainingDevicePath
)) {
778 Status
= PassThru
->GetSlotNumber (PassThru
, RemainingDevicePath
, &Slot
);
779 if (EFI_ERROR (Status
)) {
781 // Close the I/O Abstraction(s) used to perform the supported test
785 &gEfiSdMmcPassThruProtocolGuid
,
786 This
->DriverBindingHandle
,
794 // Close the I/O Abstraction(s) used to perform the supported test
798 &gEfiSdMmcPassThruProtocolGuid
,
799 This
->DriverBindingHandle
,
804 // Open the EFI Device Path protocol needed to perform the supported test
806 Status
= gBS
->OpenProtocol (
808 &gEfiDevicePathProtocolGuid
,
809 (VOID
**)&ParentDevicePath
,
810 This
->DriverBindingHandle
,
812 EFI_OPEN_PROTOCOL_GET_PROTOCOL
818 Starts a device controller or a bus controller.
820 The Start() function is designed to be invoked from the EFI boot service ConnectController().
821 As a result, much of the error checking on the parameters to Start() has been moved into this
822 common boot service. It is legal to call Start() from other locations,
823 but the following calling restrictions must be followed or the system behavior will not be deterministic.
824 1. ControllerHandle must be a valid EFI_HANDLE.
825 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
826 EFI_DEVICE_PATH_PROTOCOL.
827 3. Prior to calling Start(), the Supported() function for the driver specified by This must
828 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
830 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
831 @param[in] ControllerHandle The handle of the controller to start. This handle
832 must support a protocol interface that supplies
833 an I/O abstraction to the driver.
834 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
835 parameter is ignored by device drivers, and is optional for bus
836 drivers. For a bus driver, if this parameter is NULL, then handles
837 for all the children of Controller are created by this driver.
838 If this parameter is not NULL and the first Device Path Node is
839 not the End of Device Path Node, then only the handle for the
840 child device specified by the first Device Path Node of
841 RemainingDevicePath is created by this driver.
842 If the first Device Path Node of RemainingDevicePath is
843 the End of Device Path Node, no child handle is created by this
846 @retval EFI_SUCCESS The device was started.
847 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
848 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
849 @retval Others The driver failed to start the device.
854 EmmcDxeDriverBindingStart (
855 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
856 IN EFI_HANDLE Controller
,
857 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
861 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
862 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
863 EMMC_DRIVER_PRIVATE_DATA
*Private
;
868 Status
= gBS
->OpenProtocol (
870 &gEfiSdMmcPassThruProtocolGuid
,
872 This
->DriverBindingHandle
,
874 EFI_OPEN_PROTOCOL_BY_DRIVER
876 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
881 // Check EFI_ALREADY_STARTED to reuse the original EMMC_DRIVER_PRIVATE_DATA.
883 if (Status
!= EFI_ALREADY_STARTED
) {
884 Private
= AllocateZeroPool (sizeof (EMMC_DRIVER_PRIVATE_DATA
));
885 if (Private
== NULL
) {
886 Status
= EFI_OUT_OF_RESOURCES
;
890 Status
= gBS
->OpenProtocol (
892 &gEfiDevicePathProtocolGuid
,
893 (VOID
**)&ParentDevicePath
,
894 This
->DriverBindingHandle
,
896 EFI_OPEN_PROTOCOL_GET_PROTOCOL
898 ASSERT_EFI_ERROR (Status
);
899 Private
->PassThru
= PassThru
;
900 Private
->Controller
= Controller
;
901 Private
->ParentDevicePath
= ParentDevicePath
;
902 Private
->DriverBindingHandle
= This
->DriverBindingHandle
;
904 Status
= gBS
->InstallProtocolInterface (
907 EFI_NATIVE_INTERFACE
,
910 if (EFI_ERROR (Status
)) {
914 Status
= gBS
->OpenProtocol (
918 This
->DriverBindingHandle
,
920 EFI_OPEN_PROTOCOL_GET_PROTOCOL
922 if (EFI_ERROR (Status
)) {
927 if (RemainingDevicePath
== NULL
) {
930 Status
= PassThru
->GetNextSlot (PassThru
, &Slot
);
931 if (EFI_ERROR (Status
)) {
933 // Cannot find more legal slots.
935 Status
= EFI_SUCCESS
;
939 Status
= DiscoverEmmcDevice (Private
, Slot
, NULL
);
940 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
944 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
945 Status
= PassThru
->GetSlotNumber (PassThru
, RemainingDevicePath
, &Slot
);
946 if (!EFI_ERROR (Status
)) {
947 Status
= DiscoverEmmcDevice (Private
, Slot
, NextDevicePathNode (RemainingDevicePath
));
952 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
955 &gEfiSdMmcPassThruProtocolGuid
,
956 This
->DriverBindingHandle
,
960 if (Private
!= NULL
) {
961 gBS
->UninstallMultipleProtocolInterfaces (
975 Stops a device controller or a bus controller.
977 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
978 As a result, much of the error checking on the parameters to Stop() has been moved
979 into this common boot service. It is legal to call Stop() from other locations,
980 but the following calling restrictions must be followed or the system behavior will not be deterministic.
981 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
982 same driver's Start() function.
983 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
984 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
985 Start() function, and the Start() function must have called OpenProtocol() on
986 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
988 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
989 @param[in] ControllerHandle A handle to the device being stopped. The handle must
990 support a bus specific I/O protocol for the driver
991 to use to stop the device.
992 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
993 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
994 if NumberOfChildren is 0.
996 @retval EFI_SUCCESS The device was stopped.
997 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1002 EmmcDxeDriverBindingStop (
1003 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1004 IN EFI_HANDLE Controller
,
1005 IN UINTN NumberOfChildren
,
1006 IN EFI_HANDLE
*ChildHandleBuffer
1010 BOOLEAN AllChildrenStopped
;
1012 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1013 EMMC_DRIVER_PRIVATE_DATA
*Private
;
1014 EMMC_DEVICE
*Device
;
1015 EMMC_PARTITION
*Partition
;
1016 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1017 EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
;
1018 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*StorageSecurity
;
1020 LIST_ENTRY
*NextLink
;
1021 EMMC_REQUEST
*Request
;
1025 if (NumberOfChildren
== 0) {
1026 Status
= gBS
->OpenProtocol (
1030 This
->DriverBindingHandle
,
1032 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1034 if (EFI_ERROR (Status
)) {
1035 return EFI_DEVICE_ERROR
;
1038 for (Index
= 0; Index
< EMMC_MAX_DEVICES
; Index
++) {
1039 Device
= &Private
->Device
[Index
];
1040 Status
= gBS
->OpenProtocol (
1042 &gEfiDevicePathProtocolGuid
,
1043 (VOID
**)&DevicePath
,
1044 This
->DriverBindingHandle
,
1046 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1048 if (EFI_ERROR (Status
)) {
1052 ASSERT (DevicePath
== Device
->DevicePath
);
1053 gBS
->UninstallProtocolInterface (
1055 &gEfiDevicePathProtocolGuid
,
1058 FreePool (Device
->DevicePath
);
1061 gBS
->UninstallProtocolInterface (
1066 gBS
->CloseProtocol (
1068 &gEfiSdMmcPassThruProtocolGuid
,
1069 This
->DriverBindingHandle
,
1077 AllChildrenStopped
= TRUE
;
1079 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1080 Status
= gBS
->OpenProtocol (
1081 ChildHandleBuffer
[Index
],
1082 &gEfiBlockIoProtocolGuid
,
1084 This
->DriverBindingHandle
,
1086 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1088 if (EFI_ERROR (Status
)) {
1089 Status
= gBS
->OpenProtocol (
1090 ChildHandleBuffer
[Index
],
1091 &gEfiBlockIo2ProtocolGuid
,
1093 This
->DriverBindingHandle
,
1095 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1097 if (EFI_ERROR (Status
)) {
1098 AllChildrenStopped
= FALSE
;
1103 if (BlockIo
!= NULL
) {
1104 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO (BlockIo
);
1106 ASSERT (BlockIo2
!= NULL
);
1107 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO2 (BlockIo2
);
1110 for (Link
= GetFirstNode (&Partition
->Queue
);
1111 !IsNull (&Partition
->Queue
, Link
);
1114 NextLink
= GetNextNode (&Partition
->Queue
, Link
);
1116 RemoveEntryList (Link
);
1117 Request
= EMMC_REQUEST_FROM_LINK (Link
);
1119 gBS
->CloseEvent (Request
->Event
);
1120 Request
->Token
->TransactionStatus
= EFI_ABORTED
;
1122 if (Request
->IsEnd
) {
1123 gBS
->SignalEvent (Request
->Token
->Event
);
1130 // Close the child handle
1132 Status
= gBS
->CloseProtocol (
1134 &gEfiSdMmcPassThruProtocolGuid
,
1135 This
->DriverBindingHandle
,
1136 ChildHandleBuffer
[Index
]
1139 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1140 ChildHandleBuffer
[Index
],
1141 &gEfiDevicePathProtocolGuid
,
1142 Partition
->DevicePath
,
1143 &gEfiBlockIoProtocolGuid
,
1144 &Partition
->BlockIo
,
1145 &gEfiBlockIo2ProtocolGuid
,
1146 &Partition
->BlockIo2
,
1147 &gEfiEraseBlockProtocolGuid
,
1148 &Partition
->EraseBlock
,
1149 &gEfiDiskInfoProtocolGuid
,
1150 &Partition
->DiskInfo
,
1153 if (EFI_ERROR (Status
)) {
1154 AllChildrenStopped
= FALSE
;
1157 &gEfiSdMmcPassThruProtocolGuid
,
1158 (VOID
**)&Partition
->Device
->Private
->PassThru
,
1159 This
->DriverBindingHandle
,
1160 ChildHandleBuffer
[Index
],
1161 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1167 // If Storage Security Command Protocol is installed, then uninstall this protocol.
1169 Status
= gBS
->OpenProtocol (
1170 ChildHandleBuffer
[Index
],
1171 &gEfiStorageSecurityCommandProtocolGuid
,
1172 (VOID
**)&StorageSecurity
,
1173 This
->DriverBindingHandle
,
1175 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1178 if (!EFI_ERROR (Status
)) {
1179 Status
= gBS
->UninstallProtocolInterface (
1180 ChildHandleBuffer
[Index
],
1181 &gEfiStorageSecurityCommandProtocolGuid
,
1182 &Partition
->StorageSecurity
1184 if (EFI_ERROR (Status
)) {
1187 &gEfiSdMmcPassThruProtocolGuid
,
1188 (VOID
**)&Partition
->Device
->Private
->PassThru
,
1189 This
->DriverBindingHandle
,
1190 ChildHandleBuffer
[Index
],
1191 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1193 AllChildrenStopped
= FALSE
;
1198 FreePool (Partition
->DevicePath
);
1201 if (!AllChildrenStopped
) {
1202 return EFI_DEVICE_ERROR
;
1209 The user Entry Point for module EmmcDxe. The user code starts with this function.
1211 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1212 @param[in] SystemTable A pointer to the EFI System Table.
1214 @retval EFI_SUCCESS The entry point is executed successfully.
1215 @retval other Some errors occur when executing this entry point.
1221 IN EFI_HANDLE ImageHandle
,
1222 IN EFI_SYSTEM_TABLE
*SystemTable
1228 // Install driver model protocol(s).
1230 Status
= EfiLibInstallDriverBindingComponentName2 (
1233 &gEmmcDxeDriverBinding
,
1235 &gEmmcDxeComponentName
,
1236 &gEmmcDxeComponentName2
1238 ASSERT_EFI_ERROR (Status
);