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
)) {
904 DEBUG ((EFI_D_INFO
, "Emmc%a(): Part %d Lba 0x%x BlkNo 0x%x Event %p with %r\n", IsRead
? "Read " : "Write", Partition
->PartitionType
, Lba
, BlockNum
, (Token
!= NULL
) ? Token
->Event
: NULL
, Status
));
907 Buffer
= (UINT8
*)Buffer
+ BufferSize
;
908 Remaining
-= BlockNum
;
915 Reset the Block Device.
917 @param This Indicates a pointer to the calling context.
918 @param ExtendedVerification Driver may perform diagnostics on reset.
920 @retval EFI_SUCCESS The device was reset.
921 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
928 IN EFI_BLOCK_IO_PROTOCOL
*This
,
929 IN BOOLEAN ExtendedVerification
933 EMMC_PARTITION
*Partition
;
934 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
936 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO (This
);
938 PassThru
= Partition
->Device
->Private
->PassThru
;
939 Status
= PassThru
->ResetDevice (PassThru
, Partition
->Device
->Slot
);
940 if (EFI_ERROR (Status
)) {
941 Status
= EFI_DEVICE_ERROR
;
948 Read BufferSize bytes from Lba into Buffer.
950 @param This Indicates a pointer to the calling context.
951 @param MediaId Id of the media, changes every time the media is replaced.
952 @param Lba The starting Logical Block Address to read from
953 @param BufferSize Size of Buffer, must be a multiple of device block size.
954 @param Buffer A pointer to the destination buffer for the data. The caller is
955 responsible for either having implicit or explicit ownership of the buffer.
957 @retval EFI_SUCCESS The data was read correctly from the device.
958 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
959 @retval EFI_NO_MEDIA There is no media in the device.
960 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
961 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
962 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
963 or the buffer is not on proper alignment.
969 IN EFI_BLOCK_IO_PROTOCOL
*This
,
977 EMMC_PARTITION
*Partition
;
979 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO (This
);
981 Status
= EmmcReadWrite (Partition
, MediaId
, Lba
, Buffer
, BufferSize
, TRUE
, NULL
);
986 Write BufferSize bytes from Lba into Buffer.
988 @param This Indicates a pointer to the calling context.
989 @param MediaId The media ID that the write request is for.
990 @param Lba The starting logical block address to be written. The caller is
991 responsible for writing to only legitimate locations.
992 @param BufferSize Size of Buffer, must be a multiple of device block size.
993 @param Buffer A pointer to the source buffer for the data.
995 @retval EFI_SUCCESS The data was written correctly to the device.
996 @retval EFI_WRITE_PROTECTED The device can not be written to.
997 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
998 @retval EFI_NO_MEDIA There is no media in the device.
999 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1000 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1001 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1002 or the buffer is not on proper alignment.
1008 IN EFI_BLOCK_IO_PROTOCOL
*This
,
1011 IN UINTN BufferSize
,
1016 EMMC_PARTITION
*Partition
;
1018 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO (This
);
1020 Status
= EmmcReadWrite (Partition
, MediaId
, Lba
, Buffer
, BufferSize
, FALSE
, NULL
);
1025 Flush the Block Device.
1027 @param This Indicates a pointer to the calling context.
1029 @retval EFI_SUCCESS All outstanding data was written to the device
1030 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
1031 @retval EFI_NO_MEDIA There is no media in the device.
1037 IN EFI_BLOCK_IO_PROTOCOL
*This
1047 Reset the Block Device.
1049 @param[in] This Indicates a pointer to the calling context.
1050 @param[in] ExtendedVerification Driver may perform diagnostics on reset.
1052 @retval EFI_SUCCESS The device was reset.
1053 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
1060 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1061 IN BOOLEAN ExtendedVerification
1064 EMMC_PARTITION
*Partition
;
1066 LIST_ENTRY
*NextLink
;
1067 EMMC_REQUEST
*Request
;
1070 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO2 (This
);
1072 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1073 for (Link
= GetFirstNode (&Partition
->Queue
);
1074 !IsNull (&Partition
->Queue
, Link
);
1076 NextLink
= GetNextNode (&Partition
->Queue
, Link
);
1077 RemoveEntryList (Link
);
1079 Request
= EMMC_REQUEST_FROM_LINK (Link
);
1081 gBS
->CloseEvent (Request
->Event
);
1082 Request
->Token
->TransactionStatus
= EFI_ABORTED
;
1084 if (Request
->IsEnd
) {
1085 gBS
->SignalEvent (Request
->Token
->Event
);
1090 gBS
->RestoreTPL (OldTpl
);
1096 Read BufferSize bytes from Lba into Buffer.
1098 @param[in] This Indicates a pointer to the calling context.
1099 @param[in] MediaId Id of the media, changes every time the media is replaced.
1100 @param[in] Lba The starting Logical Block Address to read from.
1101 @param[in, out] Token A pointer to the token associated with the transaction.
1102 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
1103 @param[out] Buffer A pointer to the destination buffer for the data. The caller is
1104 responsible for either having implicit or explicit ownership of the buffer.
1106 @retval EFI_SUCCESS The read request was queued if Event is not NULL.
1107 The data was read correctly from the device if
1109 @retval EFI_DEVICE_ERROR The device reported an error while performing
1111 @retval EFI_NO_MEDIA There is no media in the device.
1112 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1113 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
1114 intrinsic block size of the device.
1115 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1116 or the buffer is not on proper alignment.
1117 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
1124 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1127 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
1128 IN UINTN BufferSize
,
1133 EMMC_PARTITION
*Partition
;
1135 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO2 (This
);
1137 Status
= EmmcReadWrite (Partition
, MediaId
, Lba
, Buffer
, BufferSize
, TRUE
, Token
);
1142 Write BufferSize bytes from Lba into Buffer.
1144 @param[in] This Indicates a pointer to the calling context.
1145 @param[in] MediaId The media ID that the write request is for.
1146 @param[in] Lba The starting logical block address to be written. The
1147 caller is responsible for writing to only legitimate
1149 @param[in, out] Token A pointer to the token associated with the transaction.
1150 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
1151 @param[in] Buffer A pointer to the source buffer for the data.
1153 @retval EFI_SUCCESS The data was written correctly to the device.
1154 @retval EFI_WRITE_PROTECTED The device can not be written to.
1155 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1156 @retval EFI_NO_MEDIA There is no media in the device.
1157 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
1158 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1159 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1160 or the buffer is not on proper alignment.
1166 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1169 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
1170 IN UINTN BufferSize
,
1175 EMMC_PARTITION
*Partition
;
1177 Partition
= EMMC_PARTITION_DATA_FROM_BLKIO2 (This
);
1179 Status
= EmmcReadWrite (Partition
, MediaId
, Lba
, Buffer
, BufferSize
, FALSE
, Token
);
1184 Flush the Block Device.
1186 @param[in] This Indicates a pointer to the calling context.
1187 @param[in, out] Token A pointer to the token associated with the transaction.
1189 @retval EFI_SUCCESS All outstanding data was written to the device
1190 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
1191 @retval EFI_NO_MEDIA There is no media in the device.
1197 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1198 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
1202 // Signal event and return directly.
1204 if (Token
!= NULL
&& Token
->Event
!= NULL
) {
1205 Token
->TransactionStatus
= EFI_SUCCESS
;
1206 gBS
->SignalEvent (Token
->Event
);
1213 Send a security protocol command to a device that receives data and/or the result
1214 of one or more commands sent by SendData.
1216 The ReceiveData function sends a security protocol command to the given MediaId.
1217 The security protocol command sent is defined by SecurityProtocolId and contains
1218 the security protocol specific data SecurityProtocolSpecificData. The function
1219 returns the data from the security protocol command in PayloadBuffer.
1221 For devices supporting the SCSI command set, the security protocol command is sent
1222 using the SECURITY PROTOCOL IN command defined in SPC-4.
1224 For devices supporting the ATA command set, the security protocol command is sent
1225 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
1228 If the PayloadBufferSize is zero, the security protocol command is sent using the
1229 Trusted Non-Data command defined in ATA8-ACS.
1231 If PayloadBufferSize is too small to store the available data from the security
1232 protocol command, the function shall copy PayloadBufferSize bytes into the
1233 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
1235 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
1236 the function shall return EFI_INVALID_PARAMETER.
1238 If the given MediaId does not support security protocol commands, the function shall
1239 return EFI_UNSUPPORTED. If there is no media in the device, the function returns
1240 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
1241 the function returns EFI_MEDIA_CHANGED.
1243 If the security protocol fails to complete within the Timeout period, the function
1244 shall return EFI_TIMEOUT.
1246 If the security protocol command completes without an error, the function shall
1247 return EFI_SUCCESS. If the security protocol command completes with an error, the
1248 function shall return EFI_DEVICE_ERROR.
1250 @param[in] This Indicates a pointer to the calling context.
1251 @param[in] MediaId ID of the medium to receive data from.
1252 @param[in] Timeout The timeout, in 100ns units, to use for the execution
1253 of the security protocol command. A Timeout value of 0
1254 means that this function will wait indefinitely for the
1255 security protocol command to execute. If Timeout is greater
1256 than zero, then this function will return EFI_TIMEOUT
1257 if the time required to execute the receive data command
1258 is greater than Timeout.
1259 @param[in] SecurityProtocolId The value of the "Security Protocol" parameter of
1260 the security protocol command to be sent.
1261 @param[in] SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
1262 of the security protocol command to be sent.
1263 @param[in] PayloadBufferSize Size in bytes of the payload data buffer.
1264 @param[out] PayloadBuffer A pointer to a destination buffer to store the security
1265 protocol command specific payload data for the security
1266 protocol command. The caller is responsible for having
1267 either implicit or explicit ownership of the buffer.
1268 @param[out] PayloadTransferSize A pointer to a buffer to store the size in bytes of the
1269 data written to the payload data buffer.
1270 @param[in] IsRead Indicates it is a read or write operation.
1272 @retval EFI_SUCCESS The security protocol command completed successfully.
1273 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
1274 data from the device. The PayloadBuffer contains the truncated data.
1275 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
1276 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
1277 @retval EFI_NO_MEDIA There is no media in the device.
1278 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1279 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
1280 PayloadBufferSize is non-zero.
1281 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
1282 protocol command to execute.
1287 EmmcSecurityProtocolInOut (
1288 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*This
,
1291 IN UINT8 SecurityProtocolId
,
1292 IN UINT16 SecurityProtocolSpecificData
,
1293 IN UINTN PayloadBufferSize
,
1294 OUT VOID
*PayloadBuffer
,
1295 OUT UINTN
*PayloadTransferSize
,
1300 EMMC_PARTITION
*Partition
;
1301 EMMC_DEVICE
*Device
;
1302 EFI_BLOCK_IO_MEDIA
*Media
;
1308 UINT8 PartitionConfig
;
1310 Status
= EFI_SUCCESS
;
1311 Partition
= EMMC_PARTITION_DATA_FROM_SSP (This
);
1312 Device
= Partition
->Device
;
1313 Media
= &Partition
->BlockMedia
;
1315 if (PayloadTransferSize
!= NULL
) {
1316 *PayloadTransferSize
= 0;
1319 if ((PayloadBuffer
== NULL
) && (PayloadBufferSize
!= 0)) {
1320 return EFI_INVALID_PARAMETER
;
1323 if (MediaId
!= Media
->MediaId
) {
1324 return EFI_MEDIA_CHANGED
;
1327 if (PayloadBufferSize
== 0) {
1331 BlockSize
= Media
->BlockSize
;
1332 if ((PayloadBufferSize
% BlockSize
) != 0) {
1333 return EFI_BAD_BUFFER_SIZE
;
1336 BlockNum
= PayloadBufferSize
/ BlockSize
;
1338 IoAlign
= Media
->IoAlign
;
1339 if (IoAlign
> 0 && (((UINTN
) PayloadBuffer
& (IoAlign
- 1)) != 0)) {
1340 return EFI_INVALID_PARAMETER
;
1344 // Security protocol interface is synchronous transfer.
1345 // Waiting for async I/O list to be empty before any operation.
1347 while (!IsListEmpty (&Partition
->Queue
));
1350 // Check if needs to switch partition access.
1352 PartitionConfig
= Device
->ExtCsd
.PartitionConfig
;
1353 if ((PartitionConfig
& 0x7) != Partition
->PartitionType
) {
1354 PartitionConfig
&= (UINT8
)~0x7;
1355 PartitionConfig
|= Partition
->PartitionType
;
1356 Status
= EmmcSetExtCsd (Partition
, OFFSET_OF (EMMC_EXT_CSD
, PartitionConfig
), PartitionConfig
, NULL
, FALSE
);
1357 if (EFI_ERROR (Status
)) {
1360 Device
->ExtCsd
.PartitionConfig
= PartitionConfig
;
1363 // Start to execute data transfer. The max block number in single cmd is 65535 blocks.
1365 Remaining
= BlockNum
;
1368 while (Remaining
> 0) {
1369 if (Remaining
<= MaxBlock
) {
1370 BlockNum
= Remaining
;
1372 BlockNum
= MaxBlock
;
1375 Status
= EmmcSetBlkCount (Partition
, (UINT16
)BlockNum
, NULL
, FALSE
);
1376 if (EFI_ERROR (Status
)) {
1380 PayloadBufferSize
= BlockNum
* BlockSize
;
1381 Status
= EmmcProtocolInOut (Partition
, SecurityProtocolId
, SecurityProtocolSpecificData
, PayloadBufferSize
, PayloadBuffer
, IsRead
, Timeout
, NULL
, FALSE
);
1382 if (EFI_ERROR (Status
)) {
1386 PayloadBuffer
= (UINT8
*)PayloadBuffer
+ PayloadBufferSize
;
1387 Remaining
-= BlockNum
;
1388 if (PayloadTransferSize
!= NULL
) {
1389 *PayloadTransferSize
+= PayloadBufferSize
;
1397 Send a security protocol command to a device that receives data and/or the result
1398 of one or more commands sent by SendData.
1400 The ReceiveData function sends a security protocol command to the given MediaId.
1401 The security protocol command sent is defined by SecurityProtocolId and contains
1402 the security protocol specific data SecurityProtocolSpecificData. The function
1403 returns the data from the security protocol command in PayloadBuffer.
1405 For devices supporting the SCSI command set, the security protocol command is sent
1406 using the SECURITY PROTOCOL IN command defined in SPC-4.
1408 For devices supporting the ATA command set, the security protocol command is sent
1409 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
1412 If the PayloadBufferSize is zero, the security protocol command is sent using the
1413 Trusted Non-Data command defined in ATA8-ACS.
1415 If PayloadBufferSize is too small to store the available data from the security
1416 protocol command, the function shall copy PayloadBufferSize bytes into the
1417 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
1419 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
1420 the function shall return EFI_INVALID_PARAMETER.
1422 If the given MediaId does not support security protocol commands, the function shall
1423 return EFI_UNSUPPORTED. If there is no media in the device, the function returns
1424 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
1425 the function returns EFI_MEDIA_CHANGED.
1427 If the security protocol fails to complete within the Timeout period, the function
1428 shall return EFI_TIMEOUT.
1430 If the security protocol command completes without an error, the function shall
1431 return EFI_SUCCESS. If the security protocol command completes with an error, the
1432 function shall return EFI_DEVICE_ERROR.
1434 @param This Indicates a pointer to the calling context.
1435 @param MediaId ID of the medium to receive data from.
1436 @param Timeout The timeout, in 100ns units, to use for the execution
1437 of the security protocol command. A Timeout value of 0
1438 means that this function will wait indefinitely for the
1439 security protocol command to execute. If Timeout is greater
1440 than zero, then this function will return EFI_TIMEOUT
1441 if the time required to execute the receive data command
1442 is greater than Timeout.
1443 @param SecurityProtocolId The value of the "Security Protocol" parameter of
1444 the security protocol command to be sent.
1445 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
1446 of the security protocol command to be sent.
1447 @param PayloadBufferSize Size in bytes of the payload data buffer.
1448 @param PayloadBuffer A pointer to a destination buffer to store the security
1449 protocol command specific payload data for the security
1450 protocol command. The caller is responsible for having
1451 either implicit or explicit ownership of the buffer.
1452 @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the
1453 data written to the payload data buffer.
1455 @retval EFI_SUCCESS The security protocol command completed successfully.
1456 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
1457 data from the device. The PayloadBuffer contains the truncated data.
1458 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
1459 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
1460 @retval EFI_NO_MEDIA There is no media in the device.
1461 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1462 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
1463 PayloadBufferSize is non-zero.
1464 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
1465 protocol command to execute.
1470 EmmcSecurityProtocolIn (
1471 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*This
,
1474 IN UINT8 SecurityProtocolId
,
1475 IN UINT16 SecurityProtocolSpecificData
,
1476 IN UINTN PayloadBufferSize
,
1477 OUT VOID
*PayloadBuffer
,
1478 OUT UINTN
*PayloadTransferSize
1483 if ((PayloadTransferSize
== NULL
) && PayloadBufferSize
!= 0) {
1484 return EFI_INVALID_PARAMETER
;
1487 Status
= EmmcSecurityProtocolInOut (
1492 SecurityProtocolSpecificData
,
1495 PayloadTransferSize
,
1503 Send a security protocol command to a device.
1505 The SendData function sends a security protocol command containing the payload
1506 PayloadBuffer to the given MediaId. The security protocol command sent is
1507 defined by SecurityProtocolId and contains the security protocol specific data
1508 SecurityProtocolSpecificData. If the underlying protocol command requires a
1509 specific padding for the command payload, the SendData function shall add padding
1510 bytes to the command payload to satisfy the padding requirements.
1512 For devices supporting the SCSI command set, the security protocol command is sent
1513 using the SECURITY PROTOCOL OUT command defined in SPC-4.
1515 For devices supporting the ATA command set, the security protocol command is sent
1516 using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
1517 is non-zero. If the PayloadBufferSize is zero, the security protocol command is
1518 sent using the Trusted Non-Data command defined in ATA8-ACS.
1520 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
1521 return EFI_INVALID_PARAMETER.
1523 If the given MediaId does not support security protocol commands, the function
1524 shall return EFI_UNSUPPORTED. If there is no media in the device, the function
1525 returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
1526 device, the function returns EFI_MEDIA_CHANGED.
1528 If the security protocol fails to complete within the Timeout period, the function
1529 shall return EFI_TIMEOUT.
1531 If the security protocol command completes without an error, the function shall return
1532 EFI_SUCCESS. If the security protocol command completes with an error, the function
1533 shall return EFI_DEVICE_ERROR.
1535 @param This Indicates a pointer to the calling context.
1536 @param MediaId ID of the medium to receive data from.
1537 @param Timeout The timeout, in 100ns units, to use for the execution
1538 of the security protocol command. A Timeout value of 0
1539 means that this function will wait indefinitely for the
1540 security protocol command to execute. If Timeout is greater
1541 than zero, then this function will return EFI_TIMEOUT
1542 if the time required to execute the receive data command
1543 is greater than Timeout.
1544 @param SecurityProtocolId The value of the "Security Protocol" parameter of
1545 the security protocol command to be sent.
1546 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
1547 of the security protocol command to be sent.
1548 @param PayloadBufferSize Size in bytes of the payload data buffer.
1549 @param PayloadBuffer A pointer to a destination buffer to store the security
1550 protocol command specific payload data for the security
1553 @retval EFI_SUCCESS The security protocol command completed successfully.
1554 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
1555 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
1556 @retval EFI_NO_MEDIA There is no media in the device.
1557 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1558 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
1559 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
1560 protocol command to execute.
1565 EmmcSecurityProtocolOut (
1566 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*This
,
1569 IN UINT8 SecurityProtocolId
,
1570 IN UINT16 SecurityProtocolSpecificData
,
1571 IN UINTN PayloadBufferSize
,
1572 IN VOID
*PayloadBuffer
1577 Status
= EmmcSecurityProtocolInOut (
1582 SecurityProtocolSpecificData
,
1593 Set the erase start address through sync or async I/O request.
1595 @param[in] Partition A pointer to the EMMC_PARTITION instance.
1596 @param[in] StartLba The starting logical block address to be erased.
1597 @param[in] Token A pointer to the token associated with the transaction.
1598 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
1599 This parameter is only meaningful in async I/O request.
1601 @retval EFI_SUCCESS The request is executed successfully.
1602 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
1603 @retval Others The request could not be executed successfully.
1607 EmmcEraseBlockStart (
1608 IN EMMC_PARTITION
*Partition
,
1609 IN EFI_LBA StartLba
,
1610 IN EFI_BLOCK_IO2_TOKEN
*Token
,
1615 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
1616 EMMC_DEVICE
*Device
;
1617 EMMC_REQUEST
*EraseBlockStart
;
1620 EraseBlockStart
= NULL
;
1622 Device
= Partition
->Device
;
1623 PassThru
= Device
->Private
->PassThru
;
1625 EraseBlockStart
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
1626 if (EraseBlockStart
== NULL
) {
1627 Status
= EFI_OUT_OF_RESOURCES
;
1631 EraseBlockStart
->Signature
= EMMC_REQUEST_SIGNATURE
;
1632 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1633 InsertTailList (&Partition
->Queue
, &EraseBlockStart
->Link
);
1634 gBS
->RestoreTPL (OldTpl
);
1635 EraseBlockStart
->Packet
.SdMmcCmdBlk
= &EraseBlockStart
->SdMmcCmdBlk
;
1636 EraseBlockStart
->Packet
.SdMmcStatusBlk
= &EraseBlockStart
->SdMmcStatusBlk
;
1637 EraseBlockStart
->Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
1639 EraseBlockStart
->SdMmcCmdBlk
.CommandIndex
= EMMC_ERASE_GROUP_START
;
1640 EraseBlockStart
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
1641 EraseBlockStart
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
1643 if (Device
->SectorAddressing
) {
1644 EraseBlockStart
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)StartLba
;
1646 EraseBlockStart
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)MultU64x32 (StartLba
, Partition
->BlockMedia
.BlockSize
);
1649 EraseBlockStart
->IsEnd
= IsEnd
;
1650 EraseBlockStart
->Token
= Token
;
1652 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1653 Status
= gBS
->CreateEvent (
1658 &EraseBlockStart
->Event
1660 if (EFI_ERROR (Status
)) {
1664 EraseBlockStart
->Event
= NULL
;
1667 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &EraseBlockStart
->Packet
, EraseBlockStart
->Event
);
1670 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1672 // For asynchronous operation, only free request and event in error case.
1673 // The request and event will be freed in asynchronous callback for success case.
1675 if (EFI_ERROR (Status
) && (EraseBlockStart
!= NULL
)) {
1676 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1677 RemoveEntryList (&EraseBlockStart
->Link
);
1678 gBS
->RestoreTPL (OldTpl
);
1679 if (EraseBlockStart
->Event
!= NULL
) {
1680 gBS
->CloseEvent (EraseBlockStart
->Event
);
1682 FreePool (EraseBlockStart
);
1686 // For synchronous operation, free request whatever the execution result is.
1688 if (EraseBlockStart
!= NULL
) {
1689 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1690 RemoveEntryList (&EraseBlockStart
->Link
);
1691 gBS
->RestoreTPL (OldTpl
);
1692 FreePool (EraseBlockStart
);
1700 Set the erase end address through sync or async I/O request.
1702 @param[in] Partition A pointer to the EMMC_PARTITION instance.
1703 @param[in] EndLba The ending logical block address to be erased.
1704 @param[in] Token A pointer to the token associated with the transaction.
1705 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
1706 This parameter is only meaningful in async I/O request.
1708 @retval EFI_SUCCESS The request is executed successfully.
1709 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
1710 @retval Others The request could not be executed successfully.
1715 IN EMMC_PARTITION
*Partition
,
1717 IN EFI_BLOCK_IO2_TOKEN
*Token
,
1722 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
1723 EMMC_DEVICE
*Device
;
1724 EMMC_REQUEST
*EraseBlockEnd
;
1727 EraseBlockEnd
= NULL
;
1729 Device
= Partition
->Device
;
1730 PassThru
= Device
->Private
->PassThru
;
1732 EraseBlockEnd
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
1733 if (EraseBlockEnd
== NULL
) {
1734 Status
= EFI_OUT_OF_RESOURCES
;
1738 EraseBlockEnd
->Signature
= EMMC_REQUEST_SIGNATURE
;
1739 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1740 InsertTailList (&Partition
->Queue
, &EraseBlockEnd
->Link
);
1741 gBS
->RestoreTPL (OldTpl
);
1742 EraseBlockEnd
->Packet
.SdMmcCmdBlk
= &EraseBlockEnd
->SdMmcCmdBlk
;
1743 EraseBlockEnd
->Packet
.SdMmcStatusBlk
= &EraseBlockEnd
->SdMmcStatusBlk
;
1744 EraseBlockEnd
->Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
1746 EraseBlockEnd
->SdMmcCmdBlk
.CommandIndex
= EMMC_ERASE_GROUP_END
;
1747 EraseBlockEnd
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
1748 EraseBlockEnd
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1
;
1750 if (Device
->SectorAddressing
) {
1751 EraseBlockEnd
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)EndLba
;
1753 EraseBlockEnd
->SdMmcCmdBlk
.CommandArgument
= (UINT32
)MultU64x32 (EndLba
, Partition
->BlockMedia
.BlockSize
);
1756 EraseBlockEnd
->IsEnd
= IsEnd
;
1757 EraseBlockEnd
->Token
= Token
;
1759 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1760 Status
= gBS
->CreateEvent (
1765 &EraseBlockEnd
->Event
1767 if (EFI_ERROR (Status
)) {
1771 EraseBlockEnd
->Event
= NULL
;
1774 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &EraseBlockEnd
->Packet
, EraseBlockEnd
->Event
);
1777 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1779 // For asynchronous operation, only free request and event in error case.
1780 // The request and event will be freed in asynchronous callback for success case.
1782 if (EFI_ERROR (Status
) && (EraseBlockEnd
!= NULL
)) {
1783 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1784 RemoveEntryList (&EraseBlockEnd
->Link
);
1785 gBS
->RestoreTPL (OldTpl
);
1786 if (EraseBlockEnd
->Event
!= NULL
) {
1787 gBS
->CloseEvent (EraseBlockEnd
->Event
);
1789 FreePool (EraseBlockEnd
);
1793 // For synchronous operation, free request whatever the execution result is.
1795 if (EraseBlockEnd
!= NULL
) {
1796 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1797 RemoveEntryList (&EraseBlockEnd
->Link
);
1798 gBS
->RestoreTPL (OldTpl
);
1799 FreePool (EraseBlockEnd
);
1807 Erase specified blocks through sync or async I/O request.
1809 @param[in] Partition A pointer to the EMMC_PARTITION instance.
1810 @param[in] Token A pointer to the token associated with the transaction.
1811 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.
1812 This parameter is only meaningful in async I/O request.
1814 @retval EFI_SUCCESS The request is executed successfully.
1815 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
1816 @retval Others The request could not be executed successfully.
1821 IN EMMC_PARTITION
*Partition
,
1822 IN EFI_BLOCK_IO2_TOKEN
*Token
,
1827 EFI_SD_MMC_PASS_THRU_PROTOCOL
*PassThru
;
1828 EMMC_DEVICE
*Device
;
1829 EMMC_REQUEST
*EraseBlock
;
1834 Device
= Partition
->Device
;
1835 PassThru
= Device
->Private
->PassThru
;
1837 EraseBlock
= AllocateZeroPool (sizeof (EMMC_REQUEST
));
1838 if (EraseBlock
== NULL
) {
1839 Status
= EFI_OUT_OF_RESOURCES
;
1843 EraseBlock
->Signature
= EMMC_REQUEST_SIGNATURE
;
1844 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1845 InsertTailList (&Partition
->Queue
, &EraseBlock
->Link
);
1846 gBS
->RestoreTPL (OldTpl
);
1847 EraseBlock
->Packet
.SdMmcCmdBlk
= &EraseBlock
->SdMmcCmdBlk
;
1848 EraseBlock
->Packet
.SdMmcStatusBlk
= &EraseBlock
->SdMmcStatusBlk
;
1849 EraseBlock
->Packet
.Timeout
= EMMC_GENERIC_TIMEOUT
;
1851 EraseBlock
->SdMmcCmdBlk
.CommandIndex
= EMMC_ERASE
;
1852 EraseBlock
->SdMmcCmdBlk
.CommandType
= SdMmcCommandTypeAc
;
1853 EraseBlock
->SdMmcCmdBlk
.ResponseType
= SdMmcResponseTypeR1b
;
1854 if ((Device
->ExtCsd
.SecFeatureSupport
& BIT4
) != 0) {
1856 // Perform a Trim operation which applies the erase operation to write blocks
1857 // instead of erase groups. (Spec JESD84-B51, eMMC Electrical Standard 5.1,
1858 // Section 6.6.10 and 6.10.4)
1860 EraseBlock
->SdMmcCmdBlk
.CommandArgument
= 1;
1863 EraseBlock
->IsEnd
= IsEnd
;
1864 EraseBlock
->Token
= Token
;
1866 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1867 Status
= gBS
->CreateEvent (
1874 if (EFI_ERROR (Status
)) {
1878 EraseBlock
->Event
= NULL
;
1881 Status
= PassThru
->PassThru (PassThru
, Device
->Slot
, &EraseBlock
->Packet
, EraseBlock
->Event
);
1884 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1886 // For asynchronous operation, only free request and event in error case.
1887 // The request and event will be freed in asynchronous callback for success case.
1889 if (EFI_ERROR (Status
) && (EraseBlock
!= NULL
)) {
1890 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1891 RemoveEntryList (&EraseBlock
->Link
);
1892 gBS
->RestoreTPL (OldTpl
);
1893 if (EraseBlock
->Event
!= NULL
) {
1894 gBS
->CloseEvent (EraseBlock
->Event
);
1896 FreePool (EraseBlock
);
1900 // For synchronous operation, free request whatever the execution result is.
1902 if (EraseBlock
!= NULL
) {
1903 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1904 RemoveEntryList (&EraseBlock
->Link
);
1905 gBS
->RestoreTPL (OldTpl
);
1906 FreePool (EraseBlock
);
1914 Write zeros to specified blocks.
1916 @param[in] Partition A pointer to the EMMC_PARTITION instance.
1917 @param[in] StartLba The starting logical block address to write zeros.
1918 @param[in] Size The size in bytes to fill with zeros. This must be a multiple of
1919 the physical block size of the device.
1921 @retval EFI_SUCCESS The request is executed successfully.
1922 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.
1923 @retval Others The request could not be executed successfully.
1928 IN EMMC_PARTITION
*Partition
,
1929 IN EFI_LBA StartLba
,
1937 Buffer
= AllocateZeroPool (Size
);
1938 if (Buffer
== NULL
) {
1939 return EFI_OUT_OF_RESOURCES
;
1942 MediaId
= Partition
->BlockMedia
.MediaId
;
1944 Status
= EmmcReadWrite (Partition
, MediaId
, StartLba
, Buffer
, Size
, FALSE
, NULL
);
1951 Erase a specified number of device blocks.
1953 @param[in] This Indicates a pointer to the calling context.
1954 @param[in] MediaId The media ID that the erase request is for.
1955 @param[in] Lba The starting logical block address to be
1956 erased. The caller is responsible for erasing
1957 only legitimate locations.
1958 @param[in, out] Token A pointer to the token associated with the
1960 @param[in] Size The size in bytes to be erased. This must be
1961 a multiple of the physical block size of the
1964 @retval EFI_SUCCESS The erase request was queued if Event is not
1965 NULL. The data was erased correctly to the
1966 device if the Event is NULL.to the device.
1967 @retval EFI_WRITE_PROTECTED The device cannot be erased due to write
1969 @retval EFI_DEVICE_ERROR The device reported an error while attempting
1970 to perform the erase operation.
1971 @retval EFI_INVALID_PARAMETER The erase request contains LBAs that are not
1973 @retval EFI_NO_MEDIA There is no media in the device.
1974 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1980 IN EFI_ERASE_BLOCK_PROTOCOL
*This
,
1983 IN OUT EFI_ERASE_BLOCK_TOKEN
*Token
,
1988 EFI_BLOCK_IO_MEDIA
*Media
;
1993 EFI_LBA StartGroupLba
;
1994 EFI_LBA EndGroupLba
;
1995 UINT32 EraseGroupSize
;
1997 UINTN WriteZeroSize
;
1998 UINT8 PartitionConfig
;
1999 EMMC_PARTITION
*Partition
;
2000 EMMC_DEVICE
*Device
;
2002 Status
= EFI_SUCCESS
;
2003 Partition
= EMMC_PARTITION_DATA_FROM_ERASEBLK (This
);
2004 Device
= Partition
->Device
;
2005 Media
= &Partition
->BlockMedia
;
2007 if (MediaId
!= Media
->MediaId
) {
2008 return EFI_MEDIA_CHANGED
;
2011 if (Media
->ReadOnly
) {
2012 return EFI_WRITE_PROTECTED
;
2016 // Check parameters.
2018 BlockSize
= Media
->BlockSize
;
2019 if ((Size
% BlockSize
) != 0) {
2020 return EFI_INVALID_PARAMETER
;
2023 BlockNum
= Size
/ BlockSize
;
2024 if ((Lba
+ BlockNum
- 1) > Media
->LastBlock
) {
2025 return EFI_INVALID_PARAMETER
;
2028 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
2029 Token
->TransactionStatus
= EFI_SUCCESS
;
2033 LastLba
= Lba
+ BlockNum
- 1;
2036 // Check if needs to switch partition access.
2038 PartitionConfig
= Device
->ExtCsd
.PartitionConfig
;
2039 if ((PartitionConfig
& 0x7) != Partition
->PartitionType
) {
2040 PartitionConfig
&= (UINT8
)~0x7;
2041 PartitionConfig
|= Partition
->PartitionType
;
2042 Status
= EmmcSetExtCsd (Partition
, OFFSET_OF (EMMC_EXT_CSD
, PartitionConfig
), PartitionConfig
, (EFI_BLOCK_IO2_TOKEN
*)Token
, FALSE
);
2043 if (EFI_ERROR (Status
)) {
2046 Device
->ExtCsd
.PartitionConfig
= PartitionConfig
;
2049 if ((Device
->ExtCsd
.SecFeatureSupport
& BIT4
) == 0) {
2051 // If the Trim operation is not supported by the device, handle the erase
2052 // of blocks that do not form a complete erase group separately.
2054 EraseGroupSize
= This
->EraseLengthGranularity
;
2056 DivU64x32Remainder (FirstLba
, EraseGroupSize
, &Remainder
);
2057 StartGroupLba
= (Remainder
== 0) ? FirstLba
: (FirstLba
+ EraseGroupSize
- Remainder
);
2059 DivU64x32Remainder (LastLba
+ 1, EraseGroupSize
, &Remainder
);
2060 EndGroupLba
= LastLba
+ 1 - Remainder
;
2063 // If the size to erase is smaller than the erase group size, the whole
2064 // erase operation is performed by writting zeros.
2066 if (BlockNum
< EraseGroupSize
) {
2067 Status
= EmmcWriteZeros (Partition
, FirstLba
, Size
);
2068 if (EFI_ERROR (Status
)) {
2074 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",
2077 (Token
!= NULL
) ? Token
->Event
: NULL
,
2081 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
2082 Token
->TransactionStatus
= EFI_SUCCESS
;
2083 gBS
->SignalEvent (Token
->Event
);
2089 // If the starting LBA to erase is not aligned with the start of an erase
2090 // group, write zeros to erase the data from starting LBA to the end of the
2091 // current erase group.
2093 if (StartGroupLba
> FirstLba
) {
2094 WriteZeroSize
= (UINTN
)(StartGroupLba
- FirstLba
) * BlockSize
;
2095 Status
= EmmcWriteZeros (Partition
, FirstLba
, WriteZeroSize
);
2096 if (EFI_ERROR (Status
)) {
2102 // If the ending LBA to erase is not aligned with the end of an erase
2103 // group, write zeros to erase the data from the start of the erase group
2104 // to the ending LBA.
2106 if (EndGroupLba
<= LastLba
) {
2107 WriteZeroSize
= (UINTN
)(LastLba
+ 1 - EndGroupLba
) * BlockSize
;
2108 Status
= EmmcWriteZeros (Partition
, EndGroupLba
, WriteZeroSize
);
2109 if (EFI_ERROR (Status
)) {
2115 // Check whether there is erase group to erase.
2117 if (EndGroupLba
<= StartGroupLba
) {
2120 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",
2123 (Token
!= NULL
) ? Token
->Event
: NULL
,
2127 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
2128 Token
->TransactionStatus
= EFI_SUCCESS
;
2129 gBS
->SignalEvent (Token
->Event
);
2134 FirstLba
= StartGroupLba
;
2135 LastLba
= EndGroupLba
- 1;
2138 Status
= EmmcEraseBlockStart (Partition
, FirstLba
, (EFI_BLOCK_IO2_TOKEN
*)Token
, FALSE
);
2139 if (EFI_ERROR (Status
)) {
2143 Status
= EmmcEraseBlockEnd (Partition
, LastLba
, (EFI_BLOCK_IO2_TOKEN
*)Token
, FALSE
);
2144 if (EFI_ERROR (Status
)) {
2148 Status
= EmmcEraseBlock (Partition
, (EFI_BLOCK_IO2_TOKEN
*)Token
, TRUE
);
2149 if (EFI_ERROR (Status
)) {
2155 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",
2158 (Token
!= NULL
) ? Token
->Event
: NULL
,