2 The helper functions for BlockIo and BlockIo2 protocol.
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Nonblocking I/O callback funtion when the event is signaled.
20 @param[in] Event The Event this notify function registered to.
21 @param[in] Context Pointer to the context data registered to the
32 EMMC_REQUEST
*Request
;
35 Status
= gBS
->CloseEvent (Event
);
36 if (EFI_ERROR (Status
)) {
40 Request
= (EMMC_REQUEST
*) Context
;
43 DEBUG ((EFI_D_INFO
, "Emmc Async Request: CmdIndex[%d] Arg[%08x] %r\n",
44 Request
->SdMmcCmdBlk
.CommandIndex
, Request
->SdMmcCmdBlk
.CommandArgument
,
45 Request
->Packet
.TransactionStatus
));
48 if (EFI_ERROR (Request
->Packet
.TransactionStatus
)) {
49 Request
->Token
->TransactionStatus
= Request
->Packet
.TransactionStatus
;
52 RemoveEntryList (&Request
->Link
);
55 gBS
->SignalEvent (Request
->Token
->Event
);
62 Send command SELECT to the device to select/deselect the device.
64 @param[in] Device A pointer to the EMMC_DEVICE instance.
65 @param[in] Rca The relative device address to use.
67 @retval EFI_SUCCESS The request is executed successfully.
68 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
69 @retval Others The request could not be executed successfully.
74 IN EMMC_DEVICE
*Device
,
79 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
80 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk
;
81 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk
;
82 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet
;
84 PassThru
= Device
->Private
->PassThru
;
86 ZeroMem (&SdMmcCmdBlk
, sizeof (SdMmcCmdBlk
));
87 ZeroMem (&SdMmcStatusBlk
, sizeof (SdMmcStatusBlk
));
88 ZeroMem (&Packet
, sizeof (Packet
));
89 Packet
.SdMmcCmdBlk
= &SdMmcCmdBlk
;
90 Packet
.SdMmcStatusBlk
= &SdMmcStatusBlk
;
91 Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
93 SdMmcCmdBlk
.CommandIndex
= EMMC_SELECT_DESELECT_CARD
;
94 SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
95 SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
96 SdMmcCmdBlk
.CommandArgument
= (UINT32
)Rca
<< 16;
98 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &Packet
, NULL
);
104 Send command SEND_STATUS to the device to get device status.
106 @param[in] Device A pointer to the EMMC_DEVICE instance.
107 @param[in] Rca The relative device address to use.
108 @param[out] DevStatus The buffer to store the device status.
110 @retval EFI_SUCCESS The request is executed successfully.
111 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
112 @retval Others The request could not be executed successfully.
117 IN EMMC_DEVICE
*Device
,
119 OUT UINT32
*DevStatus
123 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
124 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk
;
125 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk
;
126 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet
;
128 PassThru
= Device
->Private
->PassThru
;
130 ZeroMem (&SdMmcCmdBlk
, sizeof (SdMmcCmdBlk
));
131 ZeroMem (&SdMmcStatusBlk
, sizeof (SdMmcStatusBlk
));
132 ZeroMem (&Packet
, sizeof (Packet
));
133 Packet
.SdMmcCmdBlk
= &SdMmcCmdBlk
;
134 Packet
.SdMmcStatusBlk
= &SdMmcStatusBlk
;
135 Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
137 SdMmcCmdBlk
.CommandIndex
= EMMC_SEND_STATUS
;
138 SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
139 SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
140 SdMmcCmdBlk
.CommandArgument
= (UINT32
)Rca
<< 16;
142 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &Packet
, NULL
);
143 if (!EFI_ERROR (Status
)) {
144 CopyMem (DevStatus
, &SdMmcStatusBlk
.Resp0
, sizeof (UINT32
));
151 Send command SEND_CSD to the device to get the CSD register data.
153 @param[in] Device A pointer to the EMMC_DEVICE instance.
154 @param[in] Rca The relative device address to use.
155 @param[out] Csd The buffer to store the EMMC_CSD register data.
157 @retval EFI_SUCCESS The request is executed successfully.
158 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
159 @retval Others The request could not be executed successfully.
164 IN EMMC_DEVICE
*Device
,
170 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
171 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk
;
172 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk
;
173 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet
;
175 PassThru
= Device
->Private
->PassThru
;
177 ZeroMem (&SdMmcCmdBlk
, sizeof (SdMmcCmdBlk
));
178 ZeroMem (&SdMmcStatusBlk
, sizeof (SdMmcStatusBlk
));
179 ZeroMem (&Packet
, sizeof (Packet
));
180 ZeroMem (Csd
, sizeof (EMMC_CSD
));
182 Packet
.SdMmcCmdBlk
= &SdMmcCmdBlk
;
183 Packet
.SdMmcStatusBlk
= &SdMmcStatusBlk
;
184 Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
186 SdMmcCmdBlk
.CommandIndex
= EMMC_SEND_CSD
;
187 SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
188 SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR2
;
189 SdMmcCmdBlk
.CommandArgument
= (UINT32
)Rca
<< 16;
191 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &Packet
, NULL
);
192 if (!EFI_ERROR (Status
)) {
194 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
196 CopyMem (((UINT8
*)Csd
) + 1, &SdMmcStatusBlk
.Resp0
, sizeof (EMMC_CSD
) - 1);
203 Send command SEND_CID to the device to get the CID register data.
205 @param[in] Device A pointer to the EMMC_DEVICE instance.
206 @param[in] Rca The relative device address to use.
207 @param[out] Cid The buffer to store the EMMC_CID register data.
209 @retval EFI_SUCCESS The request is executed successfully.
210 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
211 @retval Others The request could not be executed successfully.
216 IN EMMC_DEVICE
*Device
,
222 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
223 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk
;
224 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk
;
225 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet
;
227 PassThru
= Device
->Private
->PassThru
;
229 ZeroMem (&SdMmcCmdBlk
, sizeof (SdMmcCmdBlk
));
230 ZeroMem (&SdMmcStatusBlk
, sizeof (SdMmcStatusBlk
));
231 ZeroMem (&Packet
, sizeof (Packet
));
232 ZeroMem (Cid
, sizeof (EMMC_CID
));
234 Packet
.SdMmcCmdBlk
= &SdMmcCmdBlk
;
235 Packet
.SdMmcStatusBlk
= &SdMmcStatusBlk
;
236 Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
238 SdMmcCmdBlk
.CommandIndex
= EMMC_SEND_CID
;
239 SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
240 SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR2
;
241 SdMmcCmdBlk
.CommandArgument
= (UINT32
)Rca
<< 16;
243 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &Packet
, NULL
);
244 if (!EFI_ERROR (Status
)) {
246 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
248 CopyMem (((UINT8
*)Cid
) + 1, &SdMmcStatusBlk
.Resp0
, sizeof (EMMC_CID
) - 1);
255 Send command SEND_EXT_CSD to the device to get the EXT_CSD register data.
257 @param[in] Device A pointer to the EMMC_DEVICE instance.
258 @param[out] ExtCsd The buffer to store the EXT_CSD register data.
260 @retval EFI_SUCCESS The request is executed successfully.
261 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
262 @retval Others The request could not be executed successfully.
267 IN EMMC_DEVICE
*Device
,
268 OUT EMMC_EXT_CSD
*ExtCsd
272 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
273 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk
;
274 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk
;
275 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet
;
277 PassThru
= Device
->Private
->PassThru
;
279 ZeroMem (&SdMmcCmdBlk
, sizeof (SdMmcCmdBlk
));
280 ZeroMem (&SdMmcStatusBlk
, sizeof (SdMmcStatusBlk
));
281 ZeroMem (&Packet
, sizeof (Packet
));
282 ZeroMem (ExtCsd
, sizeof (EMMC_EXT_CSD
));
283 Packet
.SdMmcCmdBlk
= &SdMmcCmdBlk
;
284 Packet
.SdMmcStatusBlk
= &SdMmcStatusBlk
;
285 Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
287 SdMmcCmdBlk
.CommandIndex
= EMMC_SEND_EXT_CSD
;
288 SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAdtc
;
289 SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
290 SdMmcCmdBlk
.CommandArgument
= 0x00000000;
291 Packet
.InDataBuffer
= ExtCsd
;
292 Packet
.InTransferLength
= sizeof (EMMC_EXT_CSD
);
294 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &Packet
, NULL
);
300 Set the specified EXT_CSD register field through sync or async I/O request.
302 @param[in] Partition A pointer to the EMMC_PARTITION instance.
303 @param[in] Offset The offset of the specified field in EXT_CSD register.
304 @param[in] Value The byte value written to the field specified by Offset.
305 @param[in] Token A pointer to the token associated with the transaction.
306 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
307 This parameter is only meaningful in async I/O request.
309 @retval EFI_SUCCESS The request is executed successfully.
310 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
311 @retval Others The request could not be executed successfully.
316 IN EMMC_PARTITION
*Partition
,
319 IN EFI_BLOCK_IO2_TOKEN
*Token
,
325 EMMC_REQUEST
*SetExtCsdReq
;
326 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
327 UINT32 CommandArgument
;
332 Device
= Partition
->Device
;
333 PassThru
= Device
->Private
->PassThru
;
335 SetExtCsdReq
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
336 if (SetExtCsdReq
== NULL
) {
337 Status
= EFI_OUT_OF_RESOURCES
;
341 SetExtCsdReq
->Signature
= EMMC_REQUEST_SIGNATURE
;
342 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
343 InsertTailList (&Partition
->Queue
, &SetExtCsdReq
->Link
);
344 gBS
->RestoreTPL (OldTpl
);
345 SetExtCsdReq
->Packet
.SdMmcCmdBlk
= &SetExtCsdReq
->SdMmcCmdBlk
;
346 SetExtCsdReq
->Packet
.SdMmcStatusBlk
= &SetExtCsdReq
->SdMmcStatusBlk
;
347 SetExtCsdReq
->Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
349 SetExtCsdReq
->SdMmcCmdBlk
.CommandIndex
= EMMC_SWITCH
;
350 SetExtCsdReq
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
351 SetExtCsdReq
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1b
;
353 // Write the Value to the field specified by Offset.
355 CommandArgument
= (Value
<< 8) | (Offset
<< 16) | BIT24
| BIT25
;
356 SetExtCsdReq
->SdMmcCmdBlk
.CommandArgument
= CommandArgument
;
358 SetExtCsdReq
->IsEnd
= IsEnd
;
359 SetExtCsdReq
->Token
= Token
;
361 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
362 Status
= gBS
->CreateEvent (
369 if (EFI_ERROR (Status
)) {
373 SetExtCsdReq
->Event
= NULL
;
376 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &SetExtCsdReq
->Packet
, SetExtCsdReq
->Event
);
379 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
381 // For asynchronous operation, only free request and event in error case.
382 // The request and event will be freed in asynchronous callback for success case.
384 if (EFI_ERROR (Status
) && (SetExtCsdReq
!= NULL
)) {
385 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
386 RemoveEntryList (&SetExtCsdReq
->Link
);
387 gBS
->RestoreTPL (OldTpl
);
388 if (SetExtCsdReq
->Event
!= NULL
) {
389 gBS
->CloseEvent (SetExtCsdReq
->Event
);
391 FreePool (SetExtCsdReq
);
395 // For synchronous operation, free request whatever the execution result is.
397 if (SetExtCsdReq
!= NULL
) {
398 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
399 RemoveEntryList (&SetExtCsdReq
->Link
);
400 gBS
->RestoreTPL (OldTpl
);
401 FreePool (SetExtCsdReq
);
409 Set the number of blocks for a block read/write cmd through sync or async I/O request.
411 @param[in] Partition A pointer to the EMMC_PARTITION instance.
412 @param[in] BlockNum The number of blocks for transfer.
413 @param[in] Token A pointer to the token associated with the transaction.
414 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
415 This parameter is only meaningful in async I/O request.
417 @retval EFI_SUCCESS The request is executed successfully.
418 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
419 @retval Others The request could not be executed successfully.
424 IN EMMC_PARTITION
*Partition
,
426 IN EFI_BLOCK_IO2_TOKEN
*Token
,
432 EMMC_REQUEST
*SetBlkCntReq
;
433 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
438 Device
= Partition
->Device
;
439 PassThru
= Device
->Private
->PassThru
;
441 SetBlkCntReq
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
442 if (SetBlkCntReq
== NULL
) {
443 Status
= EFI_OUT_OF_RESOURCES
;
447 SetBlkCntReq
->Signature
= EMMC_REQUEST_SIGNATURE
;
448 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
449 InsertTailList (&Partition
->Queue
, &SetBlkCntReq
->Link
);
450 gBS
->RestoreTPL (OldTpl
);
451 SetBlkCntReq
->Packet
.SdMmcCmdBlk
= &SetBlkCntReq
->SdMmcCmdBlk
;
452 SetBlkCntReq
->Packet
.SdMmcStatusBlk
= &SetBlkCntReq
->SdMmcStatusBlk
;
453 SetBlkCntReq
->Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
455 SetBlkCntReq
->SdMmcCmdBlk
.CommandIndex
= EMMC_SET_BLOCK_COUNT
;
456 SetBlkCntReq
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
457 SetBlkCntReq
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
458 SetBlkCntReq
->SdMmcCmdBlk
.CommandArgument
= BlockNum
;
460 SetBlkCntReq
->IsEnd
= IsEnd
;
461 SetBlkCntReq
->Token
= Token
;
463 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
464 Status
= gBS
->CreateEvent (
471 if (EFI_ERROR (Status
)) {
475 SetBlkCntReq
->Event
= NULL
;
478 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &SetBlkCntReq
->Packet
, SetBlkCntReq
->Event
);
481 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
483 // For asynchronous operation, only free request and event in error case.
484 // The request and event will be freed in asynchronous callback for success case.
486 if (EFI_ERROR (Status
) && (SetBlkCntReq
!= NULL
)) {
487 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
488 RemoveEntryList (&SetBlkCntReq
->Link
);
489 gBS
->RestoreTPL (OldTpl
);
490 if (SetBlkCntReq
->Event
!= NULL
) {
491 gBS
->CloseEvent (SetBlkCntReq
->Event
);
493 FreePool (SetBlkCntReq
);
497 // For synchronous operation, free request whatever the execution result is.
499 if (SetBlkCntReq
!= NULL
) {
500 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
501 RemoveEntryList (&SetBlkCntReq
->Link
);
502 gBS
->RestoreTPL (OldTpl
);
503 FreePool (SetBlkCntReq
);
511 Read blocks through security protocol cmds with the way of sync or async I/O request.
513 @param[in] Partition A pointer to the EMMC_PARTITION instance.
514 @param[in] SecurityProtocolId The value of the "Security Protocol" parameter of
515 the security protocol command to be sent.
516 @param[in] SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
517 of the security protocol command to be sent.
518 @param[in] PayloadBufferSize Size in bytes of the payload data buffer.
519 @param[out] PayloadBuffer A pointer to a destination buffer to store the security
520 protocol command specific payload data for the security
521 protocol command. The caller is responsible for having
522 either implicit or explicit ownership of the buffer.
523 @param[in] IsRead Indicates it is a read or write operation.
524 @param[in] Timeout The timeout value, in 100ns units.
525 @param[in] Token A pointer to the token associated with the transaction.
526 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
527 This parameter is only meaningful in async I/O request.
529 @retval EFI_SUCCESS The request is executed successfully.
530 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
531 @retval Others The request could not be executed successfully.
536 IN EMMC_PARTITION
*Partition
,
537 IN UINT8 SecurityProtocol
,
538 IN UINT16 SecurityProtocolSpecificData
,
539 IN UINTN PayloadBufferSize
,
540 OUT VOID
*PayloadBuffer
,
543 IN EFI_BLOCK_IO2_TOKEN
*Token
,
549 EMMC_REQUEST
*ProtocolReq
;
550 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
555 Device
= Partition
->Device
;
556 PassThru
= Device
->Private
->PassThru
;
558 ProtocolReq
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
559 if (ProtocolReq
== NULL
) {
560 Status
= EFI_OUT_OF_RESOURCES
;
564 ProtocolReq
->Signature
= EMMC_REQUEST_SIGNATURE
;
565 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
566 InsertTailList (&Partition
->Queue
, &ProtocolReq
->Link
);
567 gBS
->RestoreTPL (OldTpl
);
568 ProtocolReq
->Packet
.SdMmcCmdBlk
= &ProtocolReq
->SdMmcCmdBlk
;
569 ProtocolReq
->Packet
.SdMmcStatusBlk
= &ProtocolReq
->SdMmcStatusBlk
;
572 ProtocolReq
->Packet
.InDataBuffer
= PayloadBuffer
;
573 ProtocolReq
->Packet
.InTransferLength
= (UINT32
)PayloadBufferSize
;
575 ProtocolReq
->SdMmcCmdBlk
.CommandIndex
= EMMC_PROTOCOL_RD
;
576 ProtocolReq
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAdtc
;
577 ProtocolReq
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
579 ProtocolReq
->Packet
.OutDataBuffer
= PayloadBuffer
;
580 ProtocolReq
->Packet
.OutTransferLength
= (UINT32
)PayloadBufferSize
;
582 ProtocolReq
->SdMmcCmdBlk
.CommandIndex
= EMMC_PROTOCOL_WR
;
583 ProtocolReq
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAdtc
;
584 ProtocolReq
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
587 ProtocolReq
->SdMmcCmdBlk
.CommandArgument
= (SecurityProtocol
<< 8) | (SecurityProtocolSpecificData
<< 16);
589 // Convert to 1 microsecond unit.
591 ProtocolReq
->Packet
.Timeout
= DivU64x32 (Timeout
, 10) + 1;
593 ProtocolReq
->IsEnd
= IsEnd
;
594 ProtocolReq
->Token
= Token
;
596 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
597 Status
= gBS
->CreateEvent (
604 if (EFI_ERROR (Status
)) {
608 ProtocolReq
->Event
= NULL
;
611 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &ProtocolReq
->Packet
, ProtocolReq
->Event
);
614 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
616 // For asynchronous operation, only free request and event in error case.
617 // The request and event will be freed in asynchronous callback for success case.
619 if (EFI_ERROR (Status
) && (ProtocolReq
!= NULL
)) {
620 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
621 RemoveEntryList (&ProtocolReq
->Link
);
622 gBS
->RestoreTPL (OldTpl
);
623 if (ProtocolReq
->Event
!= NULL
) {
624 gBS
->CloseEvent (ProtocolReq
->Event
);
626 FreePool (ProtocolReq
);
630 // For synchronous operation, free request whatever the execution result is.
632 if (ProtocolReq
!= NULL
) {
633 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
634 RemoveEntryList (&ProtocolReq
->Link
);
635 gBS
->RestoreTPL (OldTpl
);
636 FreePool (ProtocolReq
);
644 Read/write multiple blocks through sync or async I/O request.
646 @param[in] Partition A pointer to the EMMC_PARTITION instance.
647 @param[in] Lba The starting logical block address to be read/written.
648 The caller is responsible for reading/writing to only
649 legitimate locations.
650 @param[in] Buffer A pointer to the destination/source buffer for the data.
651 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
652 @param[in] IsRead Indicates it is a read or write operation.
653 @param[in] Token A pointer to the token associated with the transaction.
654 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
655 This parameter is only meaningful in async I/O request.
657 @retval EFI_SUCCESS The request is executed successfully.
658 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
659 @retval Others The request could not be executed successfully.
664 IN EMMC_PARTITION
*Partition
,
669 IN EFI_BLOCK_IO2_TOKEN
*Token
,
675 EMMC_REQUEST
*RwMultiBlkReq
;
676 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
679 RwMultiBlkReq
= NULL
;
681 Device
= Partition
->Device
;
682 PassThru
= Device
->Private
->PassThru
;
684 RwMultiBlkReq
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
685 if (RwMultiBlkReq
== NULL
) {
686 Status
= EFI_OUT_OF_RESOURCES
;
690 RwMultiBlkReq
->Signature
= EMMC_REQUEST_SIGNATURE
;
691 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
692 InsertTailList (&Partition
->Queue
, &RwMultiBlkReq
->Link
);
693 gBS
->RestoreTPL (OldTpl
);
694 RwMultiBlkReq
->Packet
.SdMmcCmdBlk
= &RwMultiBlkReq
->SdMmcCmdBlk
;
695 RwMultiBlkReq
->Packet
.SdMmcStatusBlk
= &RwMultiBlkReq
->SdMmcStatusBlk
;
697 // Calculate timeout value through the below formula.
698 // Timeout = (transfer size) / (2MB/s).
699 // Taking 2MB/s as divisor is because it's nearest to the eMMC lowest
700 // transfer speed (2.4MB/s).
701 // Refer to eMMC 5.0 spec section 6.9.1 for details.
703 RwMultiBlkReq
->Packet
.Timeout
= (BufferSize
/ (2 * 1024 * 1024) + 1) * 1000 * 1000;
706 RwMultiBlkReq
->Packet
.InDataBuffer
= Buffer
;
707 RwMultiBlkReq
->Packet
.InTransferLength
= (UINT32
)BufferSize
;
709 RwMultiBlkReq
->SdMmcCmdBlk
.CommandIndex
= EMMC_READ_MULTIPLE_BLOCK
;
710 RwMultiBlkReq
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAdtc
;
711 RwMultiBlkReq
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
713 RwMultiBlkReq
->Packet
.OutDataBuffer
= Buffer
;
714 RwMultiBlkReq
->Packet
.OutTransferLength
= (UINT32
)BufferSize
;
716 RwMultiBlkReq
->SdMmcCmdBlk
.CommandIndex
= EMMC_WRITE_MULTIPLE_BLOCK
;
717 RwMultiBlkReq
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAdtc
;
718 RwMultiBlkReq
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
721 if (Partition
->Device
->SectorAddressing
) {
722 RwMultiBlkReq
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)Lba
;
724 RwMultiBlkReq
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)MultU64x32 (Lba
, Partition
->BlockMedia
.BlockSize
);
727 RwMultiBlkReq
->IsEnd
= IsEnd
;
728 RwMultiBlkReq
->Token
= Token
;
730 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
731 Status
= gBS
->CreateEvent (
736 &RwMultiBlkReq
->Event
738 if (EFI_ERROR (Status
)) {
742 RwMultiBlkReq
->Event
= NULL
;
745 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &RwMultiBlkReq
->Packet
, RwMultiBlkReq
->Event
);
748 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
750 // For asynchronous operation, only free request and event in error case.
751 // The request and event will be freed in asynchronous callback for success case.
753 if (EFI_ERROR (Status
) && (RwMultiBlkReq
!= NULL
)) {
754 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
755 RemoveEntryList (&RwMultiBlkReq
->Link
);
756 gBS
->RestoreTPL (OldTpl
);
757 if (RwMultiBlkReq
->Event
!= NULL
) {
758 gBS
->CloseEvent (RwMultiBlkReq
->Event
);
760 FreePool (RwMultiBlkReq
);
764 // For synchronous operation, free request whatever the execution result is.
766 if (RwMultiBlkReq
!= NULL
) {
767 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
768 RemoveEntryList (&RwMultiBlkReq
->Link
);
769 gBS
->RestoreTPL (OldTpl
);
770 FreePool (RwMultiBlkReq
);
778 This function transfers data from/to EMMC device.
780 @param[in] Partition A pointer to the EMMC_PARTITION instance.
781 @param[in] MediaId The media ID that the read/write request is for.
782 @param[in] Lba The starting logical block address to be read/written.
783 The caller is responsible for reading/writing to only
784 legitimate locations.
785 @param[in, out] Buffer A pointer to the destination/source buffer for the data.
786 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
787 @param[in] IsRead Indicates it is a read or write operation.
788 @param[in, out] Token A pointer to the token associated with the transaction.
790 @retval EFI_SUCCESS The data was read/written correctly to the device.
791 @retval EFI_WRITE_PROTECTED The device can not be read/written to.
792 @retval EFI_DEVICE_ERROR The device reported an error while performing the read/write.
793 @retval EFI_NO_MEDIA There is no media in the device.
794 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
795 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
796 @retval EFI_INVALID_PARAMETER The read/write request contains LBAs that are not valid,
797 or the buffer is not on proper alignment.
802 IN EMMC_PARTITION
*Partition
,
808 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
813 EFI_BLOCK_IO_MEDIA
*Media
;
817 UINT8 PartitionConfig
;
822 Status
= EFI_SUCCESS
;
823 Device
= Partition
->Device
;
824 Media
= &Partition
->BlockMedia
;
827 if (MediaId
!= Media
->MediaId
) {
828 return EFI_MEDIA_CHANGED
;
831 if (!IsRead
&& Media
->ReadOnly
) {
832 return EFI_WRITE_PROTECTED
;
838 if (Buffer
== NULL
) {
839 return EFI_INVALID_PARAMETER
;
842 if (BufferSize
== 0) {
843 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
844 Token
->TransactionStatus
= EFI_SUCCESS
;
845 gBS
->SignalEvent (Token
->Event
);
850 BlockSize
= Media
->BlockSize
;
851 if ((BufferSize
% BlockSize
) != 0) {
852 return EFI_BAD_BUFFER_SIZE
;
855 BlockNum
= BufferSize
/ BlockSize
;
856 if ((Lba
+ BlockNum
- 1) > Media
->LastBlock
) {
857 return EFI_INVALID_PARAMETER
;
860 IoAlign
= Media
->IoAlign
;
861 if (IoAlign
> 0 && (((UINTN
) Buffer
& (IoAlign
- 1)) != 0)) {
862 return EFI_INVALID_PARAMETER
;
865 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
866 Token
->TransactionStatus
= EFI_SUCCESS
;
869 // Check if needs to switch partition access.
871 PartitionConfig
= Device
->ExtCsd
.PartitionConfig
;
872 if ((PartitionConfig
& 0x7) != Partition
->PartitionType
) {
873 PartitionConfig
&= (UINT8
)~0x7;
874 PartitionConfig
|= Partition
->PartitionType
;
875 Status
= EmmcSetExtCsd (Partition
, OFFSET_OF (EMMC_EXT_CSD
, PartitionConfig
), PartitionConfig
, Token
, FALSE
);
876 if (EFI_ERROR (Status
)) {
879 Device
->ExtCsd
.PartitionConfig
= PartitionConfig
;
882 // Start to execute data transfer. The max block number in single cmd is 65535 blocks.
884 Remaining
= BlockNum
;
887 while (Remaining
> 0) {
888 if (Remaining
<= MaxBlock
) {
889 BlockNum
= Remaining
;
894 Status
= EmmcSetBlkCount (Partition
, (UINT16
)BlockNum
, Token
, FALSE
);
895 if (EFI_ERROR (Status
)) {
899 BufferSize
= BlockNum
* BlockSize
;
900 Status
= EmmcRwMultiBlocks (Partition
, Lba
, Buffer
, BufferSize
, IsRead
, Token
, LastRw
);
901 if (EFI_ERROR (Status
)) {
905 "Emmc%a(): Part %d Lba 0x%x BlkNo 0x%x Event %p with %r\n",
906 IsRead
? "Read " : "Write", Partition
->PartitionType
, Lba
, BlockNum
,
907 (Token
!= NULL
) ? Token
->Event
: NULL
, Status
));
910 Buffer
= (UINT8
*)Buffer
+ BufferSize
;
911 Remaining
-= BlockNum
;
918 Reset the Block Device.
920 @param This Indicates a pointer to the calling context.
921 @param ExtendedVerification Driver may perform diagnostics on reset.
923 @retval EFI_SUCCESS The device was reset.
924 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
931 IN EFI_BLOCK_IO_PROTOCOL
*This
,
932 IN BOOLEAN ExtendedVerification
936 EMMC_PARTITION
*Partition
;
937 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
939 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO (This
);
941 PassThru
= Partition
->Device
->Private
->PassThru
;
942 Status
= PassThru
->ResetDevice (PassThru
, Partition
->Device
->Slot
);
943 if (EFI_ERROR (Status
)) {
944 Status
= EFI_DEVICE_ERROR
;
951 Read BufferSize bytes from Lba into Buffer.
953 @param This Indicates a pointer to the calling context.
954 @param MediaId Id of the media, changes every time the media is replaced.
955 @param Lba The starting Logical Block Address to read from
956 @param BufferSize Size of Buffer, must be a multiple of device block size.
957 @param Buffer A pointer to the destination buffer for the data. The caller is
958 responsible for either having implicit or explicit ownership of the buffer.
960 @retval EFI_SUCCESS The data was read correctly from the device.
961 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
962 @retval EFI_NO_MEDIA There is no media in the device.
963 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
964 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
965 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
966 or the buffer is not on proper alignment.
972 IN EFI_BLOCK_IO_PROTOCOL
*This
,
980 EMMC_PARTITION
*Partition
;
982 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO (This
);
984 Status
= EmmcReadWrite (Partition
, MediaId
, Lba
, Buffer
, BufferSize
, TRUE
, NULL
);
989 Write BufferSize bytes from Lba into Buffer.
991 @param This Indicates a pointer to the calling context.
992 @param MediaId The media ID that the write request is for.
993 @param Lba The starting logical block address to be written. The caller is
994 responsible for writing to only legitimate locations.
995 @param BufferSize Size of Buffer, must be a multiple of device block size.
996 @param Buffer A pointer to the source buffer for the data.
998 @retval EFI_SUCCESS The data was written correctly to the device.
999 @retval EFI_WRITE_PROTECTED The device can not be written to.
1000 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1001 @retval EFI_NO_MEDIA There is no media in the device.
1002 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1003 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1004 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1005 or the buffer is not on proper alignment.
1011 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1014 IN UINTN BufferSize
,
1019 EMMC_PARTITION
*Partition
;
1021 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO (This
);
1023 Status
= EmmcReadWrite (Partition
, MediaId
, Lba
, Buffer
, BufferSize
, FALSE
, NULL
);
1028 Flush the Block Device.
1030 @param This Indicates a pointer to the calling context.
1032 @retval EFI_SUCCESS All outstanding data was written to the device
1033 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
1034 @retval EFI_NO_MEDIA There is no media in the device.
1040 IN EFI_BLOCK_IO_PROTOCOL
*This
1050 Reset the Block Device.
1052 @param[in] This Indicates a pointer to the calling context.
1053 @param[in] ExtendedVerification Driver may perform diagnostics on reset.
1055 @retval EFI_SUCCESS The device was reset.
1056 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
1063 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1064 IN BOOLEAN ExtendedVerification
1067 EMMC_PARTITION
*Partition
;
1069 LIST_ENTRY
*NextLink
;
1070 EMMC_REQUEST
*Request
;
1073 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO2 (This
);
1075 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1076 for (Link
= GetFirstNode (&Partition
->Queue
);
1077 !IsNull (&Partition
->Queue
, Link
);
1079 NextLink
= GetNextNode (&Partition
->Queue
, Link
);
1080 RemoveEntryList (Link
);
1082 Request
= EMMC_REQUEST_FROM_LINK (Link
);
1084 gBS
->CloseEvent (Request
->Event
);
1085 Request
->Token
->TransactionStatus
= EFI_ABORTED
;
1087 if (Request
->IsEnd
) {
1088 gBS
->SignalEvent (Request
->Token
->Event
);
1093 gBS
->RestoreTPL (OldTpl
);
1099 Read BufferSize bytes from Lba into Buffer.
1101 @param[in] This Indicates a pointer to the calling context.
1102 @param[in] MediaId Id of the media, changes every time the media is replaced.
1103 @param[in] Lba The starting Logical Block Address to read from.
1104 @param[in, out] Token A pointer to the token associated with the transaction.
1105 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
1106 @param[out] Buffer A pointer to the destination buffer for the data. The caller is
1107 responsible for either having implicit or explicit ownership of the buffer.
1109 @retval EFI_SUCCESS The read request was queued if Event is not NULL.
1110 The data was read correctly from the device if
1112 @retval EFI_DEVICE_ERROR The device reported an error while performing
1114 @retval EFI_NO_MEDIA There is no media in the device.
1115 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1116 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
1117 intrinsic block size of the device.
1118 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1119 or the buffer is not on proper alignment.
1120 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
1127 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1130 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
1131 IN UINTN BufferSize
,
1136 EMMC_PARTITION
*Partition
;
1138 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO2 (This
);
1140 Status
= EmmcReadWrite (Partition
, MediaId
, Lba
, Buffer
, BufferSize
, TRUE
, Token
);
1145 Write BufferSize bytes from Lba into Buffer.
1147 @param[in] This Indicates a pointer to the calling context.
1148 @param[in] MediaId The media ID that the write request is for.
1149 @param[in] Lba The starting logical block address to be written. The
1150 caller is responsible for writing to only legitimate
1152 @param[in, out] Token A pointer to the token associated with the transaction.
1153 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
1154 @param[in] Buffer A pointer to the source buffer for the data.
1156 @retval EFI_SUCCESS The data was written correctly to the device.
1157 @retval EFI_WRITE_PROTECTED The device can not be written to.
1158 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1159 @retval EFI_NO_MEDIA There is no media in the device.
1160 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1161 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1162 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1163 or the buffer is not on proper alignment.
1169 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1172 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
1173 IN UINTN BufferSize
,
1178 EMMC_PARTITION
*Partition
;
1180 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO2 (This
);
1182 Status
= EmmcReadWrite (Partition
, MediaId
, Lba
, Buffer
, BufferSize
, FALSE
, Token
);
1187 Flush the Block Device.
1189 @param[in] This Indicates a pointer to the calling context.
1190 @param[in, out] Token A pointer to the token associated with the transaction.
1192 @retval EFI_SUCCESS All outstanding data was written to the device
1193 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
1194 @retval EFI_NO_MEDIA There is no media in the device.
1200 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1201 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
1205 // Signal event and return directly.
1207 if (Token
!= NULL
&& Token
->Event
!= NULL
) {
1208 Token
->TransactionStatus
= EFI_SUCCESS
;
1209 gBS
->SignalEvent (Token
->Event
);
1216 Send a security protocol command to a device that receives data and/or the result
1217 of one or more commands sent by SendData.
1219 The ReceiveData function sends a security protocol command to the given MediaId.
1220 The security protocol command sent is defined by SecurityProtocolId and contains
1221 the security protocol specific data SecurityProtocolSpecificData. The function
1222 returns the data from the security protocol command in PayloadBuffer.
1224 For devices supporting the SCSI command set, the security protocol command is sent
1225 using the SECURITY PROTOCOL IN command defined in SPC-4.
1227 For devices supporting the ATA command set, the security protocol command is sent
1228 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
1231 If the PayloadBufferSize is zero, the security protocol command is sent using the
1232 Trusted Non-Data command defined in ATA8-ACS.
1234 If PayloadBufferSize is too small to store the available data from the security
1235 protocol command, the function shall copy PayloadBufferSize bytes into the
1236 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
1238 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
1239 the function shall return EFI_INVALID_PARAMETER.
1241 If the given MediaId does not support security protocol commands, the function shall
1242 return EFI_UNSUPPORTED. If there is no media in the device, the function returns
1243 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
1244 the function returns EFI_MEDIA_CHANGED.
1246 If the security protocol fails to complete within the Timeout period, the function
1247 shall return EFI_TIMEOUT.
1249 If the security protocol command completes without an error, the function shall
1250 return EFI_SUCCESS. If the security protocol command completes with an error, the
1251 function shall return EFI_DEVICE_ERROR.
1253 @param[in] This Indicates a pointer to the calling context.
1254 @param[in] MediaId ID of the medium to receive data from.
1255 @param[in] Timeout The timeout, in 100ns units, to use for the execution
1256 of the security protocol command. A Timeout value of 0
1257 means that this function will wait indefinitely for the
1258 security protocol command to execute. If Timeout is greater
1259 than zero, then this function will return EFI_TIMEOUT
1260 if the time required to execute the receive data command
1261 is greater than Timeout.
1262 @param[in] SecurityProtocolId The value of the "Security Protocol" parameter of
1263 the security protocol command to be sent.
1264 @param[in] SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
1265 of the security protocol command to be sent.
1266 @param[in] PayloadBufferSize Size in bytes of the payload data buffer.
1267 @param[out] PayloadBuffer A pointer to a destination buffer to store the security
1268 protocol command specific payload data for the security
1269 protocol command. The caller is responsible for having
1270 either implicit or explicit ownership of the buffer.
1271 @param[out] PayloadTransferSize A pointer to a buffer to store the size in bytes of the
1272 data written to the payload data buffer.
1273 @param[in] IsRead Indicates it is a read or write operation.
1275 @retval EFI_SUCCESS The security protocol command completed successfully.
1276 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
1277 data from the device. The PayloadBuffer contains the truncated data.
1278 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
1279 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
1280 @retval EFI_NO_MEDIA There is no media in the device.
1281 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1282 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
1283 PayloadBufferSize is non-zero.
1284 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
1285 protocol command to execute.
1290 EmmcSecurityProtocolInOut (
1291 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*This
,
1294 IN UINT8 SecurityProtocolId
,
1295 IN UINT16 SecurityProtocolSpecificData
,
1296 IN UINTN PayloadBufferSize
,
1297 OUT VOID
*PayloadBuffer
,
1298 OUT UINTN
*PayloadTransferSize
,
1303 EMMC_PARTITION
*Partition
;
1304 EMMC_DEVICE
*Device
;
1305 EFI_BLOCK_IO_MEDIA
*Media
;
1311 UINT8 PartitionConfig
;
1313 Status
= EFI_SUCCESS
;
1314 Partition
= EMMC_PARTITION_DATA_FROM_SSP (This
);
1315 Device
= Partition
->Device
;
1316 Media
= &Partition
->BlockMedia
;
1318 if (PayloadTransferSize
!= NULL
) {
1319 *PayloadTransferSize
= 0;
1322 if ((PayloadBuffer
== NULL
) && (PayloadBufferSize
!= 0)) {
1323 return EFI_INVALID_PARAMETER
;
1326 if (MediaId
!= Media
->MediaId
) {
1327 return EFI_MEDIA_CHANGED
;
1330 if (PayloadBufferSize
== 0) {
1334 BlockSize
= Media
->BlockSize
;
1335 if ((PayloadBufferSize
% BlockSize
) != 0) {
1336 return EFI_BAD_BUFFER_SIZE
;
1339 BlockNum
= PayloadBufferSize
/ BlockSize
;
1341 IoAlign
= Media
->IoAlign
;
1342 if (IoAlign
> 0 && (((UINTN
) PayloadBuffer
& (IoAlign
- 1)) != 0)) {
1343 return EFI_INVALID_PARAMETER
;
1347 // Security protocol interface is synchronous transfer.
1348 // Waiting for async I/O list to be empty before any operation.
1350 while (!IsListEmpty (&Partition
->Queue
));
1353 // Check if needs to switch partition access.
1355 PartitionConfig
= Device
->ExtCsd
.PartitionConfig
;
1356 if ((PartitionConfig
& 0x7) != Partition
->PartitionType
) {
1357 PartitionConfig
&= (UINT8
)~0x7;
1358 PartitionConfig
|= Partition
->PartitionType
;
1359 Status
= EmmcSetExtCsd (Partition
, OFFSET_OF (EMMC_EXT_CSD
, PartitionConfig
), PartitionConfig
, NULL
, FALSE
);
1360 if (EFI_ERROR (Status
)) {
1363 Device
->ExtCsd
.PartitionConfig
= PartitionConfig
;
1366 // Start to execute data transfer. The max block number in single cmd is 65535 blocks.
1368 Remaining
= BlockNum
;
1371 while (Remaining
> 0) {
1372 if (Remaining
<= MaxBlock
) {
1373 BlockNum
= Remaining
;
1375 BlockNum
= MaxBlock
;
1378 Status
= EmmcSetBlkCount (Partition
, (UINT16
)BlockNum
, NULL
, FALSE
);
1379 if (EFI_ERROR (Status
)) {
1383 PayloadBufferSize
= BlockNum
* BlockSize
;
1384 Status
= EmmcProtocolInOut (Partition
, SecurityProtocolId
, SecurityProtocolSpecificData
, PayloadBufferSize
, PayloadBuffer
, IsRead
, Timeout
, NULL
, FALSE
);
1385 if (EFI_ERROR (Status
)) {
1389 PayloadBuffer
= (UINT8
*)PayloadBuffer
+ PayloadBufferSize
;
1390 Remaining
-= BlockNum
;
1391 if (PayloadTransferSize
!= NULL
) {
1392 *PayloadTransferSize
+= PayloadBufferSize
;
1400 Send a security protocol command to a device that receives data and/or the result
1401 of one or more commands sent by SendData.
1403 The ReceiveData function sends a security protocol command to the given MediaId.
1404 The security protocol command sent is defined by SecurityProtocolId and contains
1405 the security protocol specific data SecurityProtocolSpecificData. The function
1406 returns the data from the security protocol command in PayloadBuffer.
1408 For devices supporting the SCSI command set, the security protocol command is sent
1409 using the SECURITY PROTOCOL IN command defined in SPC-4.
1411 For devices supporting the ATA command set, the security protocol command is sent
1412 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
1415 If the PayloadBufferSize is zero, the security protocol command is sent using the
1416 Trusted Non-Data command defined in ATA8-ACS.
1418 If PayloadBufferSize is too small to store the available data from the security
1419 protocol command, the function shall copy PayloadBufferSize bytes into the
1420 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
1422 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
1423 the function shall return EFI_INVALID_PARAMETER.
1425 If the given MediaId does not support security protocol commands, the function shall
1426 return EFI_UNSUPPORTED. If there is no media in the device, the function returns
1427 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
1428 the function returns EFI_MEDIA_CHANGED.
1430 If the security protocol fails to complete within the Timeout period, the function
1431 shall return EFI_TIMEOUT.
1433 If the security protocol command completes without an error, the function shall
1434 return EFI_SUCCESS. If the security protocol command completes with an error, the
1435 function shall return EFI_DEVICE_ERROR.
1437 @param This Indicates a pointer to the calling context.
1438 @param MediaId ID of the medium to receive data from.
1439 @param Timeout The timeout, in 100ns units, to use for the execution
1440 of the security protocol command. A Timeout value of 0
1441 means that this function will wait indefinitely for the
1442 security protocol command to execute. If Timeout is greater
1443 than zero, then this function will return EFI_TIMEOUT
1444 if the time required to execute the receive data command
1445 is greater than Timeout.
1446 @param SecurityProtocolId The value of the "Security Protocol" parameter of
1447 the security protocol command to be sent.
1448 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
1449 of the security protocol command to be sent.
1450 @param PayloadBufferSize Size in bytes of the payload data buffer.
1451 @param PayloadBuffer A pointer to a destination buffer to store the security
1452 protocol command specific payload data for the security
1453 protocol command. The caller is responsible for having
1454 either implicit or explicit ownership of the buffer.
1455 @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the
1456 data written to the payload data buffer.
1458 @retval EFI_SUCCESS The security protocol command completed successfully.
1459 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
1460 data from the device. The PayloadBuffer contains the truncated data.
1461 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
1462 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
1463 @retval EFI_NO_MEDIA There is no media in the device.
1464 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1465 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
1466 PayloadBufferSize is non-zero.
1467 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
1468 protocol command to execute.
1473 EmmcSecurityProtocolIn (
1474 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*This
,
1477 IN UINT8 SecurityProtocolId
,
1478 IN UINT16 SecurityProtocolSpecificData
,
1479 IN UINTN PayloadBufferSize
,
1480 OUT VOID
*PayloadBuffer
,
1481 OUT UINTN
*PayloadTransferSize
1486 if ((PayloadTransferSize
== NULL
) && PayloadBufferSize
!= 0) {
1487 return EFI_INVALID_PARAMETER
;
1490 Status
= EmmcSecurityProtocolInOut (
1495 SecurityProtocolSpecificData
,
1498 PayloadTransferSize
,
1506 Send a security protocol command to a device.
1508 The SendData function sends a security protocol command containing the payload
1509 PayloadBuffer to the given MediaId. The security protocol command sent is
1510 defined by SecurityProtocolId and contains the security protocol specific data
1511 SecurityProtocolSpecificData. If the underlying protocol command requires a
1512 specific padding for the command payload, the SendData function shall add padding
1513 bytes to the command payload to satisfy the padding requirements.
1515 For devices supporting the SCSI command set, the security protocol command is sent
1516 using the SECURITY PROTOCOL OUT command defined in SPC-4.
1518 For devices supporting the ATA command set, the security protocol command is sent
1519 using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
1520 is non-zero. If the PayloadBufferSize is zero, the security protocol command is
1521 sent using the Trusted Non-Data command defined in ATA8-ACS.
1523 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
1524 return EFI_INVALID_PARAMETER.
1526 If the given MediaId does not support security protocol commands, the function
1527 shall return EFI_UNSUPPORTED. If there is no media in the device, the function
1528 returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
1529 device, the function returns EFI_MEDIA_CHANGED.
1531 If the security protocol fails to complete within the Timeout period, the function
1532 shall return EFI_TIMEOUT.
1534 If the security protocol command completes without an error, the function shall return
1535 EFI_SUCCESS. If the security protocol command completes with an error, the function
1536 shall return EFI_DEVICE_ERROR.
1538 @param This Indicates a pointer to the calling context.
1539 @param MediaId ID of the medium to receive data from.
1540 @param Timeout The timeout, in 100ns units, to use for the execution
1541 of the security protocol command. A Timeout value of 0
1542 means that this function will wait indefinitely for the
1543 security protocol command to execute. If Timeout is greater
1544 than zero, then this function will return EFI_TIMEOUT
1545 if the time required to execute the receive data command
1546 is greater than Timeout.
1547 @param SecurityProtocolId The value of the "Security Protocol" parameter of
1548 the security protocol command to be sent.
1549 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
1550 of the security protocol command to be sent.
1551 @param PayloadBufferSize Size in bytes of the payload data buffer.
1552 @param PayloadBuffer A pointer to a destination buffer to store the security
1553 protocol command specific payload data for the security
1556 @retval EFI_SUCCESS The security protocol command completed successfully.
1557 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
1558 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
1559 @retval EFI_NO_MEDIA There is no media in the device.
1560 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1561 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
1562 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
1563 protocol command to execute.
1568 EmmcSecurityProtocolOut (
1569 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*This
,
1572 IN UINT8 SecurityProtocolId
,
1573 IN UINT16 SecurityProtocolSpecificData
,
1574 IN UINTN PayloadBufferSize
,
1575 IN VOID
*PayloadBuffer
1580 Status
= EmmcSecurityProtocolInOut (
1585 SecurityProtocolSpecificData
,
1596 Set the erase start address through sync or async I/O request.
1598 @param[in] Partition A pointer to the EMMC_PARTITION instance.
1599 @param[in] StartLba The starting logical block address to be erased.
1600 @param[in] Token A pointer to the token associated with the transaction.
1601 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
1602 This parameter is only meaningful in async I/O request.
1604 @retval EFI_SUCCESS The request is executed successfully.
1605 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
1606 @retval Others The request could not be executed successfully.
1610 EmmcEraseBlockStart (
1611 IN EMMC_PARTITION
*Partition
,
1612 IN EFI_LBA StartLba
,
1613 IN EFI_BLOCK_IO2_TOKEN
*Token
,
1618 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
1619 EMMC_DEVICE
*Device
;
1620 EMMC_REQUEST
*EraseBlockStart
;
1623 EraseBlockStart
= NULL
;
1625 Device
= Partition
->Device
;
1626 PassThru
= Device
->Private
->PassThru
;
1628 EraseBlockStart
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
1629 if (EraseBlockStart
== NULL
) {
1630 Status
= EFI_OUT_OF_RESOURCES
;
1634 EraseBlockStart
->Signature
= EMMC_REQUEST_SIGNATURE
;
1635 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1636 InsertTailList (&Partition
->Queue
, &EraseBlockStart
->Link
);
1637 gBS
->RestoreTPL (OldTpl
);
1638 EraseBlockStart
->Packet
.SdMmcCmdBlk
= &EraseBlockStart
->SdMmcCmdBlk
;
1639 EraseBlockStart
->Packet
.SdMmcStatusBlk
= &EraseBlockStart
->SdMmcStatusBlk
;
1640 EraseBlockStart
->Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
1642 EraseBlockStart
->SdMmcCmdBlk
.CommandIndex
= EMMC_ERASE_GROUP_START
;
1643 EraseBlockStart
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
1644 EraseBlockStart
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
1646 if (Device
->SectorAddressing
) {
1647 EraseBlockStart
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)StartLba
;
1649 EraseBlockStart
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)MultU64x32 (StartLba
, Partition
->BlockMedia
.BlockSize
);
1652 EraseBlockStart
->IsEnd
= IsEnd
;
1653 EraseBlockStart
->Token
= Token
;
1655 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1656 Status
= gBS
->CreateEvent (
1661 &EraseBlockStart
->Event
1663 if (EFI_ERROR (Status
)) {
1667 EraseBlockStart
->Event
= NULL
;
1670 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &EraseBlockStart
->Packet
, EraseBlockStart
->Event
);
1673 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1675 // For asynchronous operation, only free request and event in error case.
1676 // The request and event will be freed in asynchronous callback for success case.
1678 if (EFI_ERROR (Status
) && (EraseBlockStart
!= NULL
)) {
1679 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1680 RemoveEntryList (&EraseBlockStart
->Link
);
1681 gBS
->RestoreTPL (OldTpl
);
1682 if (EraseBlockStart
->Event
!= NULL
) {
1683 gBS
->CloseEvent (EraseBlockStart
->Event
);
1685 FreePool (EraseBlockStart
);
1689 // For synchronous operation, free request whatever the execution result is.
1691 if (EraseBlockStart
!= NULL
) {
1692 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1693 RemoveEntryList (&EraseBlockStart
->Link
);
1694 gBS
->RestoreTPL (OldTpl
);
1695 FreePool (EraseBlockStart
);
1703 Set the erase end address through sync or async I/O request.
1705 @param[in] Partition A pointer to the EMMC_PARTITION instance.
1706 @param[in] EndLba The ending logical block address to be erased.
1707 @param[in] Token A pointer to the token associated with the transaction.
1708 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
1709 This parameter is only meaningful in async I/O request.
1711 @retval EFI_SUCCESS The request is executed successfully.
1712 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
1713 @retval Others The request could not be executed successfully.
1718 IN EMMC_PARTITION
*Partition
,
1720 IN EFI_BLOCK_IO2_TOKEN
*Token
,
1725 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
1726 EMMC_DEVICE
*Device
;
1727 EMMC_REQUEST
*EraseBlockEnd
;
1730 EraseBlockEnd
= NULL
;
1732 Device
= Partition
->Device
;
1733 PassThru
= Device
->Private
->PassThru
;
1735 EraseBlockEnd
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
1736 if (EraseBlockEnd
== NULL
) {
1737 Status
= EFI_OUT_OF_RESOURCES
;
1741 EraseBlockEnd
->Signature
= EMMC_REQUEST_SIGNATURE
;
1742 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1743 InsertTailList (&Partition
->Queue
, &EraseBlockEnd
->Link
);
1744 gBS
->RestoreTPL (OldTpl
);
1745 EraseBlockEnd
->Packet
.SdMmcCmdBlk
= &EraseBlockEnd
->SdMmcCmdBlk
;
1746 EraseBlockEnd
->Packet
.SdMmcStatusBlk
= &EraseBlockEnd
->SdMmcStatusBlk
;
1747 EraseBlockEnd
->Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
1749 EraseBlockEnd
->SdMmcCmdBlk
.CommandIndex
= EMMC_ERASE_GROUP_END
;
1750 EraseBlockEnd
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
1751 EraseBlockEnd
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
1753 if (Device
->SectorAddressing
) {
1754 EraseBlockEnd
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)EndLba
;
1756 EraseBlockEnd
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)MultU64x32 (EndLba
, Partition
->BlockMedia
.BlockSize
);
1759 EraseBlockEnd
->IsEnd
= IsEnd
;
1760 EraseBlockEnd
->Token
= Token
;
1762 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1763 Status
= gBS
->CreateEvent (
1768 &EraseBlockEnd
->Event
1770 if (EFI_ERROR (Status
)) {
1774 EraseBlockEnd
->Event
= NULL
;
1777 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &EraseBlockEnd
->Packet
, EraseBlockEnd
->Event
);
1780 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1782 // For asynchronous operation, only free request and event in error case.
1783 // The request and event will be freed in asynchronous callback for success case.
1785 if (EFI_ERROR (Status
) && (EraseBlockEnd
!= NULL
)) {
1786 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1787 RemoveEntryList (&EraseBlockEnd
->Link
);
1788 gBS
->RestoreTPL (OldTpl
);
1789 if (EraseBlockEnd
->Event
!= NULL
) {
1790 gBS
->CloseEvent (EraseBlockEnd
->Event
);
1792 FreePool (EraseBlockEnd
);
1796 // For synchronous operation, free request whatever the execution result is.
1798 if (EraseBlockEnd
!= NULL
) {
1799 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1800 RemoveEntryList (&EraseBlockEnd
->Link
);
1801 gBS
->RestoreTPL (OldTpl
);
1802 FreePool (EraseBlockEnd
);
1810 Erase specified blocks through sync or async I/O request.
1812 @param[in] Partition A pointer to the EMMC_PARTITION instance.
1813 @param[in] Token A pointer to the token associated with the transaction.
1814 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
1815 This parameter is only meaningful in async I/O request.
1817 @retval EFI_SUCCESS The request is executed successfully.
1818 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
1819 @retval Others The request could not be executed successfully.
1824 IN EMMC_PARTITION
*Partition
,
1825 IN EFI_BLOCK_IO2_TOKEN
*Token
,
1830 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
1831 EMMC_DEVICE
*Device
;
1832 EMMC_REQUEST
*EraseBlock
;
1837 Device
= Partition
->Device
;
1838 PassThru
= Device
->Private
->PassThru
;
1840 EraseBlock
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
1841 if (EraseBlock
== NULL
) {
1842 Status
= EFI_OUT_OF_RESOURCES
;
1846 EraseBlock
->Signature
= EMMC_REQUEST_SIGNATURE
;
1847 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1848 InsertTailList (&Partition
->Queue
, &EraseBlock
->Link
);
1849 gBS
->RestoreTPL (OldTpl
);
1850 EraseBlock
->Packet
.SdMmcCmdBlk
= &EraseBlock
->SdMmcCmdBlk
;
1851 EraseBlock
->Packet
.SdMmcStatusBlk
= &EraseBlock
->SdMmcStatusBlk
;
1852 EraseBlock
->Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
1854 EraseBlock
->SdMmcCmdBlk
.CommandIndex
= EMMC_ERASE
;
1855 EraseBlock
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
1856 EraseBlock
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1b
;
1857 if ((Device
->ExtCsd
.SecFeatureSupport
& BIT4
) != 0) {
1859 // Perform a Trim operation which applies the erase operation to write blocks
1860 // instead of erase groups. (Spec JESD84-B51, eMMC Electrical Standard 5.1,
1861 // Section 6.6.10 and 6.10.4)
1863 EraseBlock
->SdMmcCmdBlk
.CommandArgument
= 1;
1866 EraseBlock
->IsEnd
= IsEnd
;
1867 EraseBlock
->Token
= Token
;
1869 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1870 Status
= gBS
->CreateEvent (
1877 if (EFI_ERROR (Status
)) {
1881 EraseBlock
->Event
= NULL
;
1884 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &EraseBlock
->Packet
, EraseBlock
->Event
);
1887 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1889 // For asynchronous operation, only free request and event in error case.
1890 // The request and event will be freed in asynchronous callback for success case.
1892 if (EFI_ERROR (Status
) && (EraseBlock
!= NULL
)) {
1893 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1894 RemoveEntryList (&EraseBlock
->Link
);
1895 gBS
->RestoreTPL (OldTpl
);
1896 if (EraseBlock
->Event
!= NULL
) {
1897 gBS
->CloseEvent (EraseBlock
->Event
);
1899 FreePool (EraseBlock
);
1903 // For synchronous operation, free request whatever the execution result is.
1905 if (EraseBlock
!= NULL
) {
1906 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1907 RemoveEntryList (&EraseBlock
->Link
);
1908 gBS
->RestoreTPL (OldTpl
);
1909 FreePool (EraseBlock
);
1917 Write zeros to specified blocks.
1919 @param[in] Partition A pointer to the EMMC_PARTITION instance.
1920 @param[in] StartLba The starting logical block address to write zeros.
1921 @param[in] Size The size in bytes to fill with zeros. This must be a multiple of
1922 the physical block size of the device.
1924 @retval EFI_SUCCESS The request is executed successfully.
1925 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
1926 @retval Others The request could not be executed successfully.
1931 IN EMMC_PARTITION
*Partition
,
1932 IN EFI_LBA StartLba
,
1940 Buffer
= AllocateZeroPool (Size
);
1941 if (Buffer
== NULL
) {
1942 return EFI_OUT_OF_RESOURCES
;
1945 MediaId
= Partition
->BlockMedia
.MediaId
;
1947 Status
= EmmcReadWrite (Partition
, MediaId
, StartLba
, Buffer
, Size
, FALSE
, NULL
);
1954 Erase a specified number of device blocks.
1956 @param[in] This Indicates a pointer to the calling context.
1957 @param[in] MediaId The media ID that the erase request is for.
1958 @param[in] Lba The starting logical block address to be
1959 erased. The caller is responsible for erasing
1960 only legitimate locations.
1961 @param[in, out] Token A pointer to the token associated with the
1963 @param[in] Size The size in bytes to be erased. This must be
1964 a multiple of the physical block size of the
1967 @retval EFI_SUCCESS The erase request was queued if Event is not
1968 NULL. The data was erased correctly to the
1969 device if the Event is NULL.to the device.
1970 @retval EFI_WRITE_PROTECTED The device cannot be erased due to write
1972 @retval EFI_DEVICE_ERROR The device reported an error while attempting
1973 to perform the erase operation.
1974 @retval EFI_INVALID_PARAMETER The erase request contains LBAs that are not
1976 @retval EFI_NO_MEDIA There is no media in the device.
1977 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1983 IN EFI_ERASE_BLOCK_PROTOCOL
*This
,
1986 IN OUT EFI_ERASE_BLOCK_TOKEN
*Token
,
1991 EFI_BLOCK_IO_MEDIA
*Media
;
1996 EFI_LBA StartGroupLba
;
1997 EFI_LBA EndGroupLba
;
1998 UINT32 EraseGroupSize
;
2000 UINTN WriteZeroSize
;
2001 UINT8 PartitionConfig
;
2002 EMMC_PARTITION
*Partition
;
2003 EMMC_DEVICE
*Device
;
2005 Status
= EFI_SUCCESS
;
2006 Partition
= EMMC_PARTITION_DATA_FROM_ERASEBLK (This
);
2007 Device
= Partition
->Device
;
2008 Media
= &Partition
->BlockMedia
;
2010 if (MediaId
!= Media
->MediaId
) {
2011 return EFI_MEDIA_CHANGED
;
2014 if (Media
->ReadOnly
) {
2015 return EFI_WRITE_PROTECTED
;
2019 // Check parameters.
2021 BlockSize
= Media
->BlockSize
;
2022 if ((Size
% BlockSize
) != 0) {
2023 return EFI_INVALID_PARAMETER
;
2026 BlockNum
= Size
/ BlockSize
;
2027 if ((Lba
+ BlockNum
- 1) > Media
->LastBlock
) {
2028 return EFI_INVALID_PARAMETER
;
2031 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
2032 Token
->TransactionStatus
= EFI_SUCCESS
;
2036 LastLba
= Lba
+ BlockNum
- 1;
2039 // Check if needs to switch partition access.
2041 PartitionConfig
= Device
->ExtCsd
.PartitionConfig
;
2042 if ((PartitionConfig
& 0x7) != Partition
->PartitionType
) {
2043 PartitionConfig
&= (UINT8
)~0x7;
2044 PartitionConfig
|= Partition
->PartitionType
;
2045 Status
= EmmcSetExtCsd (Partition
, OFFSET_OF (EMMC_EXT_CSD
, PartitionConfig
), PartitionConfig
, (EFI_BLOCK_IO2_TOKEN
*)Token
, FALSE
);
2046 if (EFI_ERROR (Status
)) {
2049 Device
->ExtCsd
.PartitionConfig
= PartitionConfig
;
2052 if ((Device
->ExtCsd
.SecFeatureSupport
& BIT4
) == 0) {
2054 // If the Trim operation is not supported by the device, handle the erase
2055 // of blocks that do not form a complete erase group separately.
2057 EraseGroupSize
= This
->EraseLengthGranularity
;
2059 DivU64x32Remainder (FirstLba
, EraseGroupSize
, &Remainder
);
2060 StartGroupLba
= (Remainder
== 0) ? FirstLba
: (FirstLba
+ EraseGroupSize
- Remainder
);
2062 DivU64x32Remainder (LastLba
+ 1, EraseGroupSize
, &Remainder
);
2063 EndGroupLba
= LastLba
+ 1 - Remainder
;
2066 // If the size to erase is smaller than the erase group size, the whole
2067 // erase operation is performed by writting zeros.
2069 if (BlockNum
< EraseGroupSize
) {
2070 Status
= EmmcWriteZeros (Partition
, FirstLba
, Size
);
2071 if (EFI_ERROR (Status
)) {
2077 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",
2080 (Token
!= NULL
) ? Token
->Event
: NULL
,
2084 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
2085 Token
->TransactionStatus
= EFI_SUCCESS
;
2086 gBS
->SignalEvent (Token
->Event
);
2092 // If the starting LBA to erase is not aligned with the start of an erase
2093 // group, write zeros to erase the data from starting LBA to the end of the
2094 // current erase group.
2096 if (StartGroupLba
> FirstLba
) {
2097 WriteZeroSize
= (UINTN
)(StartGroupLba
- FirstLba
) * BlockSize
;
2098 Status
= EmmcWriteZeros (Partition
, FirstLba
, WriteZeroSize
);
2099 if (EFI_ERROR (Status
)) {
2105 // If the ending LBA to erase is not aligned with the end of an erase
2106 // group, write zeros to erase the data from the start of the erase group
2107 // to the ending LBA.
2109 if (EndGroupLba
<= LastLba
) {
2110 WriteZeroSize
= (UINTN
)(LastLba
+ 1 - EndGroupLba
) * BlockSize
;
2111 Status
= EmmcWriteZeros (Partition
, EndGroupLba
, WriteZeroSize
);
2112 if (EFI_ERROR (Status
)) {
2118 // Check whether there is erase group to erase.
2120 if (EndGroupLba
<= StartGroupLba
) {
2123 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",
2126 (Token
!= NULL
) ? Token
->Event
: NULL
,
2130 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
2131 Token
->TransactionStatus
= EFI_SUCCESS
;
2132 gBS
->SignalEvent (Token
->Event
);
2137 FirstLba
= StartGroupLba
;
2138 LastLba
= EndGroupLba
- 1;
2141 Status
= EmmcEraseBlockStart (Partition
, FirstLba
, (EFI_BLOCK_IO2_TOKEN
*)Token
, FALSE
);
2142 if (EFI_ERROR (Status
)) {
2146 Status
= EmmcEraseBlockEnd (Partition
, LastLba
, (EFI_BLOCK_IO2_TOKEN
*)Token
, FALSE
);
2147 if (EFI_ERROR (Status
)) {
2151 Status
= EmmcEraseBlock (Partition
, (EFI_BLOCK_IO2_TOKEN
*)Token
, TRUE
);
2152 if (EFI_ERROR (Status
)) {
2158 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",
2161 (Token
!= NULL
) ? Token
->Event
: NULL
,