]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Sd/EmmcDxe/EmmcBlockIo.c
MdeModulePkg/EmmcDxe: demote DEBUG print to DEBUG_BLKIO
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / EmmcDxe / EmmcBlockIo.c
CommitLineData
48555339
FT
1/** @file\r
2 The helper functions for BlockIo and BlockIo2 protocol.\r
3\r
b92efc9f 4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r
48555339
FT
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "EmmcDxe.h"\r
16\r
17/**\r
18 Nonblocking I/O callback funtion when the event is signaled.\r
19\r
20 @param[in] Event The Event this notify function registered to.\r
21 @param[in] Context Pointer to the context data registered to the\r
22 Event.\r
23\r
24**/\r
25VOID\r
26EFIAPI\r
27AsyncIoCallback (\r
28 IN EFI_EVENT Event,\r
29 IN VOID *Context\r
30 )\r
31{\r
32 EMMC_REQUEST *Request;\r
33 EFI_STATUS Status;\r
34\r
35 Status = gBS->CloseEvent (Event);\r
36 if (EFI_ERROR (Status)) {\r
37 return;\r
38 }\r
39\r
40 Request = (EMMC_REQUEST *) Context;\r
41\r
42 DEBUG_CODE_BEGIN ();\r
43 DEBUG ((EFI_D_INFO, "Emmc Async Request: CmdIndex[%d] Arg[%08x] %r\n",\r
44 Request->SdMmcCmdBlk.CommandIndex, Request->SdMmcCmdBlk.CommandArgument,\r
45 Request->Packet.TransactionStatus));\r
46 DEBUG_CODE_END ();\r
47\r
48 if (EFI_ERROR (Request->Packet.TransactionStatus)) {\r
49 Request->Token->TransactionStatus = Request->Packet.TransactionStatus;\r
50 }\r
51\r
52 RemoveEntryList (&Request->Link);\r
53\r
54 if (Request->IsEnd) {\r
55 gBS->SignalEvent (Request->Token->Event);\r
56 }\r
57\r
58 FreePool (Request);\r
59}\r
60\r
61/**\r
62 Send command SELECT to the device to select/deselect the device.\r
63\r
64 @param[in] Device A pointer to the EMMC_DEVICE instance.\r
65 @param[in] Rca The relative device address to use.\r
66\r
67 @retval EFI_SUCCESS The request is executed successfully.\r
68 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
69 @retval Others The request could not be executed successfully.\r
70\r
71**/\r
72EFI_STATUS\r
73EmmcSelect (\r
74 IN EMMC_DEVICE *Device,\r
75 IN UINT16 Rca\r
76 )\r
77{\r
78 EFI_STATUS Status;\r
79 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
80 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;\r
81 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;\r
82 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;\r
83\r
84 PassThru = Device->Private->PassThru;\r
85\r
86 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));\r
87 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));\r
88 ZeroMem (&Packet, sizeof (Packet));\r
89 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;\r
90 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;\r
91 Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
92\r
93 SdMmcCmdBlk.CommandIndex = EMMC_SELECT_DESELECT_CARD;\r
94 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
95 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
96 SdMmcCmdBlk.CommandArgument = (UINT32)Rca << 16;\r
97\r
98 Status = PassThru->PassThru (PassThru, Device->Slot, &Packet, NULL);\r
99\r
100 return Status;\r
101}\r
102\r
103/**\r
104 Send command SEND_STATUS to the device to get device status.\r
105\r
106 @param[in] Device A pointer to the EMMC_DEVICE instance.\r
107 @param[in] Rca The relative device address to use.\r
108 @param[out] DevStatus The buffer to store the device status.\r
109\r
110 @retval EFI_SUCCESS The request is executed successfully.\r
111 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
112 @retval Others The request could not be executed successfully.\r
113\r
114**/\r
115EFI_STATUS\r
116EmmcSendStatus (\r
117 IN EMMC_DEVICE *Device,\r
118 IN UINT16 Rca,\r
119 OUT UINT32 *DevStatus\r
120 )\r
121{\r
122 EFI_STATUS Status;\r
123 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
124 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;\r
125 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;\r
126 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;\r
127\r
128 PassThru = Device->Private->PassThru;\r
129\r
130 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));\r
131 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));\r
132 ZeroMem (&Packet, sizeof (Packet));\r
133 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;\r
134 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;\r
135 Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
136\r
137 SdMmcCmdBlk.CommandIndex = EMMC_SEND_STATUS;\r
138 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
139 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
140 SdMmcCmdBlk.CommandArgument = (UINT32)Rca << 16;\r
141\r
142 Status = PassThru->PassThru (PassThru, Device->Slot, &Packet, NULL);\r
143 if (!EFI_ERROR (Status)) {\r
144 CopyMem (DevStatus, &SdMmcStatusBlk.Resp0, sizeof (UINT32));\r
145 }\r
146\r
147 return Status;\r
148}\r
149\r
150/**\r
151 Send command SEND_CSD to the device to get the CSD register data.\r
152\r
153 @param[in] Device A pointer to the EMMC_DEVICE instance.\r
154 @param[in] Rca The relative device address to use.\r
155 @param[out] Csd The buffer to store the EMMC_CSD register data.\r
156\r
157 @retval EFI_SUCCESS The request is executed successfully.\r
158 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
159 @retval Others The request could not be executed successfully.\r
160\r
161**/\r
162EFI_STATUS\r
163EmmcGetCsd (\r
164 IN EMMC_DEVICE *Device,\r
165 IN UINT16 Rca,\r
166 OUT EMMC_CSD *Csd\r
167 )\r
168{\r
169 EFI_STATUS Status;\r
170 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
171 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;\r
172 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;\r
173 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;\r
174\r
175 PassThru = Device->Private->PassThru;\r
176\r
177 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));\r
178 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));\r
179 ZeroMem (&Packet, sizeof (Packet));\r
180 ZeroMem (Csd, sizeof (EMMC_CSD));\r
181\r
182 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;\r
183 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;\r
184 Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
185\r
186 SdMmcCmdBlk.CommandIndex = EMMC_SEND_CSD;\r
187 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
188 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR2;\r
189 SdMmcCmdBlk.CommandArgument = (UINT32)Rca << 16;\r
190\r
191 Status = PassThru->PassThru (PassThru, Device->Slot, &Packet, NULL);\r
192 if (!EFI_ERROR (Status)) {\r
193 //\r
194 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.\r
195 //\r
196 CopyMem (((UINT8*)Csd) + 1, &SdMmcStatusBlk.Resp0, sizeof (EMMC_CSD) - 1);\r
197 }\r
198\r
199 return Status;\r
200}\r
201\r
202/**\r
203 Send command SEND_CID to the device to get the CID register data.\r
204\r
205 @param[in] Device A pointer to the EMMC_DEVICE instance.\r
206 @param[in] Rca The relative device address to use.\r
207 @param[out] Cid The buffer to store the EMMC_CID register data.\r
208\r
209 @retval EFI_SUCCESS The request is executed successfully.\r
210 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
211 @retval Others The request could not be executed successfully.\r
212\r
213**/\r
214EFI_STATUS\r
215EmmcGetCid (\r
216 IN EMMC_DEVICE *Device,\r
217 IN UINT16 Rca,\r
218 OUT EMMC_CID *Cid\r
219 )\r
220{\r
221 EFI_STATUS Status;\r
222 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
223 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;\r
224 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;\r
225 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;\r
226\r
227 PassThru = Device->Private->PassThru;\r
228\r
229 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));\r
230 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));\r
231 ZeroMem (&Packet, sizeof (Packet));\r
232 ZeroMem (Cid, sizeof (EMMC_CID));\r
233\r
234 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;\r
235 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;\r
236 Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
237\r
238 SdMmcCmdBlk.CommandIndex = EMMC_SEND_CID;\r
239 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
240 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR2;\r
241 SdMmcCmdBlk.CommandArgument = (UINT32)Rca << 16;\r
242\r
243 Status = PassThru->PassThru (PassThru, Device->Slot, &Packet, NULL);\r
244 if (!EFI_ERROR (Status)) {\r
245 //\r
246 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.\r
247 //\r
248 CopyMem (((UINT8*)Cid) + 1, &SdMmcStatusBlk.Resp0, sizeof (EMMC_CID) - 1);\r
249 }\r
250\r
251 return Status;\r
252}\r
253\r
254/**\r
255 Send command SEND_EXT_CSD to the device to get the EXT_CSD register data.\r
256\r
257 @param[in] Device A pointer to the EMMC_DEVICE instance.\r
258 @param[out] ExtCsd The buffer to store the EXT_CSD register data.\r
259\r
260 @retval EFI_SUCCESS The request is executed successfully.\r
261 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
262 @retval Others The request could not be executed successfully.\r
263\r
264**/\r
265EFI_STATUS\r
266EmmcGetExtCsd (\r
267 IN EMMC_DEVICE *Device,\r
268 OUT EMMC_EXT_CSD *ExtCsd\r
269 )\r
270{\r
271 EFI_STATUS Status;\r
272 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
273 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;\r
274 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;\r
275 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;\r
276\r
277 PassThru = Device->Private->PassThru;\r
278\r
279 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));\r
280 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));\r
281 ZeroMem (&Packet, sizeof (Packet));\r
282 ZeroMem (ExtCsd, sizeof (EMMC_EXT_CSD));\r
283 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;\r
284 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;\r
285 Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
286\r
287 SdMmcCmdBlk.CommandIndex = EMMC_SEND_EXT_CSD;\r
288 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAdtc;\r
289 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
290 SdMmcCmdBlk.CommandArgument = 0x00000000;\r
291 Packet.InDataBuffer = ExtCsd;\r
292 Packet.InTransferLength = sizeof (EMMC_EXT_CSD);\r
293\r
294 Status = PassThru->PassThru (PassThru, Device->Slot, &Packet, NULL);\r
295\r
296 return Status;\r
297}\r
298\r
299/**\r
300 Set the specified EXT_CSD register field through sync or async I/O request.\r
301\r
302 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
303 @param[in] Offset The offset of the specified field in EXT_CSD register.\r
304 @param[in] Value The byte value written to the field specified by Offset.\r
305 @param[in] Token A pointer to the token associated with the transaction.\r
306 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.\r
307 This parameter is only meaningful in async I/O request.\r
308\r
309 @retval EFI_SUCCESS The request is executed successfully.\r
310 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
311 @retval Others The request could not be executed successfully.\r
312\r
313**/\r
314EFI_STATUS\r
315EmmcSetExtCsd (\r
316 IN EMMC_PARTITION *Partition,\r
317 IN UINT8 Offset,\r
318 IN UINT8 Value,\r
319 IN EFI_BLOCK_IO2_TOKEN *Token,\r
320 IN BOOLEAN IsEnd\r
321 )\r
322{\r
323 EFI_STATUS Status;\r
324 EMMC_DEVICE *Device;\r
325 EMMC_REQUEST *SetExtCsdReq;\r
326 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
327 UINT32 CommandArgument;\r
328 EFI_TPL OldTpl;\r
329\r
330 SetExtCsdReq = NULL;\r
331\r
332 Device = Partition->Device;\r
333 PassThru = Device->Private->PassThru;\r
334\r
335 SetExtCsdReq = AllocateZeroPool (sizeof (EMMC_REQUEST));\r
336 if (SetExtCsdReq == NULL) {\r
337 Status = EFI_OUT_OF_RESOURCES;\r
338 goto Error;\r
339 }\r
340\r
341 SetExtCsdReq->Signature = EMMC_REQUEST_SIGNATURE;\r
3b1d8241 342 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
343 InsertTailList (&Partition->Queue, &SetExtCsdReq->Link);\r
344 gBS->RestoreTPL (OldTpl);\r
345 SetExtCsdReq->Packet.SdMmcCmdBlk = &SetExtCsdReq->SdMmcCmdBlk;\r
346 SetExtCsdReq->Packet.SdMmcStatusBlk = &SetExtCsdReq->SdMmcStatusBlk;\r
347 SetExtCsdReq->Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
348\r
349 SetExtCsdReq->SdMmcCmdBlk.CommandIndex = EMMC_SWITCH;\r
350 SetExtCsdReq->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
351 SetExtCsdReq->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1b;\r
352 //\r
353 // Write the Value to the field specified by Offset.\r
354 //\r
355 CommandArgument = (Value << 8) | (Offset << 16) | BIT24 | BIT25;\r
356 SetExtCsdReq->SdMmcCmdBlk.CommandArgument = CommandArgument;\r
357\r
358 SetExtCsdReq->IsEnd = IsEnd;\r
359 SetExtCsdReq->Token = Token;\r
360\r
361 if ((Token != NULL) && (Token->Event != NULL)) {\r
362 Status = gBS->CreateEvent (\r
363 EVT_NOTIFY_SIGNAL,\r
3b1d8241 364 TPL_NOTIFY,\r
48555339
FT
365 AsyncIoCallback,\r
366 SetExtCsdReq,\r
367 &SetExtCsdReq->Event\r
368 );\r
369 if (EFI_ERROR (Status)) {\r
370 goto Error;\r
371 }\r
372 } else {\r
373 SetExtCsdReq->Event = NULL;\r
374 }\r
375\r
376 Status = PassThru->PassThru (PassThru, Device->Slot, &SetExtCsdReq->Packet, SetExtCsdReq->Event);\r
377\r
378Error:\r
379 if ((Token != NULL) && (Token->Event != NULL)) {\r
380 //\r
381 // For asynchronous operation, only free request and event in error case.\r
382 // The request and event will be freed in asynchronous callback for success case.\r
383 //\r
384 if (EFI_ERROR (Status) && (SetExtCsdReq != NULL)) {\r
3b1d8241 385 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
386 RemoveEntryList (&SetExtCsdReq->Link);\r
387 gBS->RestoreTPL (OldTpl);\r
388 if (SetExtCsdReq->Event != NULL) {\r
389 gBS->CloseEvent (SetExtCsdReq->Event);\r
390 }\r
391 FreePool (SetExtCsdReq);\r
392 }\r
393 } else {\r
394 //\r
395 // For synchronous operation, free request whatever the execution result is.\r
396 //\r
397 if (SetExtCsdReq != NULL) {\r
3b1d8241 398 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
399 RemoveEntryList (&SetExtCsdReq->Link);\r
400 gBS->RestoreTPL (OldTpl);\r
401 FreePool (SetExtCsdReq);\r
402 }\r
403 }\r
404\r
405 return Status;\r
406}\r
407\r
408/**\r
409 Set the number of blocks for a block read/write cmd through sync or async I/O request.\r
410\r
411 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
412 @param[in] BlockNum The number of blocks for transfer.\r
413 @param[in] Token A pointer to the token associated with the transaction.\r
414 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.\r
415 This parameter is only meaningful in async I/O request.\r
416\r
417 @retval EFI_SUCCESS The request is executed successfully.\r
418 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
419 @retval Others The request could not be executed successfully.\r
420\r
421**/\r
422EFI_STATUS\r
423EmmcSetBlkCount (\r
424 IN EMMC_PARTITION *Partition,\r
425 IN UINT16 BlockNum,\r
426 IN EFI_BLOCK_IO2_TOKEN *Token,\r
427 IN BOOLEAN IsEnd\r
428 )\r
429{\r
430 EFI_STATUS Status;\r
431 EMMC_DEVICE *Device;\r
432 EMMC_REQUEST *SetBlkCntReq;\r
433 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
434 EFI_TPL OldTpl;\r
435\r
436 SetBlkCntReq = NULL;\r
437\r
438 Device = Partition->Device;\r
439 PassThru = Device->Private->PassThru;\r
440\r
441 SetBlkCntReq = AllocateZeroPool (sizeof (EMMC_REQUEST));\r
442 if (SetBlkCntReq == NULL) {\r
443 Status = EFI_OUT_OF_RESOURCES;\r
444 goto Error;\r
445 }\r
446\r
447 SetBlkCntReq->Signature = EMMC_REQUEST_SIGNATURE;\r
3b1d8241 448 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
449 InsertTailList (&Partition->Queue, &SetBlkCntReq->Link);\r
450 gBS->RestoreTPL (OldTpl);\r
451 SetBlkCntReq->Packet.SdMmcCmdBlk = &SetBlkCntReq->SdMmcCmdBlk;\r
452 SetBlkCntReq->Packet.SdMmcStatusBlk = &SetBlkCntReq->SdMmcStatusBlk;\r
453 SetBlkCntReq->Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
454\r
455 SetBlkCntReq->SdMmcCmdBlk.CommandIndex = EMMC_SET_BLOCK_COUNT;\r
456 SetBlkCntReq->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
457 SetBlkCntReq->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
458 SetBlkCntReq->SdMmcCmdBlk.CommandArgument = BlockNum;\r
459\r
460 SetBlkCntReq->IsEnd = IsEnd;\r
461 SetBlkCntReq->Token = Token;\r
462\r
463 if ((Token != NULL) && (Token->Event != NULL)) {\r
464 Status = gBS->CreateEvent (\r
465 EVT_NOTIFY_SIGNAL,\r
3b1d8241 466 TPL_NOTIFY,\r
48555339
FT
467 AsyncIoCallback,\r
468 SetBlkCntReq,\r
469 &SetBlkCntReq->Event\r
470 );\r
471 if (EFI_ERROR (Status)) {\r
472 goto Error;\r
473 }\r
474 } else {\r
475 SetBlkCntReq->Event = NULL;\r
476 }\r
477\r
478 Status = PassThru->PassThru (PassThru, Device->Slot, &SetBlkCntReq->Packet, SetBlkCntReq->Event);\r
479\r
480Error:\r
481 if ((Token != NULL) && (Token->Event != NULL)) {\r
482 //\r
483 // For asynchronous operation, only free request and event in error case.\r
484 // The request and event will be freed in asynchronous callback for success case.\r
485 //\r
486 if (EFI_ERROR (Status) && (SetBlkCntReq != NULL)) {\r
3b1d8241 487 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
488 RemoveEntryList (&SetBlkCntReq->Link);\r
489 gBS->RestoreTPL (OldTpl);\r
490 if (SetBlkCntReq->Event != NULL) {\r
491 gBS->CloseEvent (SetBlkCntReq->Event);\r
492 }\r
493 FreePool (SetBlkCntReq);\r
494 }\r
495 } else {\r
496 //\r
497 // For synchronous operation, free request whatever the execution result is.\r
498 //\r
499 if (SetBlkCntReq != NULL) {\r
3b1d8241 500 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
501 RemoveEntryList (&SetBlkCntReq->Link);\r
502 gBS->RestoreTPL (OldTpl);\r
503 FreePool (SetBlkCntReq);\r
504 }\r
505 }\r
506\r
507 return Status;\r
508}\r
509\r
510/**\r
511 Read blocks through security protocol cmds with the way of sync or async I/O request.\r
512\r
513 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
514 @param[in] SecurityProtocolId The value of the "Security Protocol" parameter of\r
515 the security protocol command to be sent.\r
516 @param[in] SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
517 of the security protocol command to be sent.\r
518 @param[in] PayloadBufferSize Size in bytes of the payload data buffer.\r
519 @param[out] PayloadBuffer A pointer to a destination buffer to store the security\r
520 protocol command specific payload data for the security\r
521 protocol command. The caller is responsible for having\r
522 either implicit or explicit ownership of the buffer.\r
523 @param[in] IsRead Indicates it is a read or write operation.\r
524 @param[in] Timeout The timeout value, in 100ns units.\r
525 @param[in] Token A pointer to the token associated with the transaction.\r
526 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.\r
527 This parameter is only meaningful in async I/O request.\r
528\r
529 @retval EFI_SUCCESS The request is executed successfully.\r
530 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
531 @retval Others The request could not be executed successfully.\r
532\r
533**/\r
534EFI_STATUS\r
535EmmcProtocolInOut (\r
536 IN EMMC_PARTITION *Partition,\r
537 IN UINT8 SecurityProtocol,\r
538 IN UINT16 SecurityProtocolSpecificData,\r
539 IN UINTN PayloadBufferSize,\r
540 OUT VOID *PayloadBuffer,\r
541 IN BOOLEAN IsRead,\r
542 IN UINT64 Timeout,\r
543 IN EFI_BLOCK_IO2_TOKEN *Token,\r
544 IN BOOLEAN IsEnd\r
545 )\r
546{\r
547 EFI_STATUS Status;\r
548 EMMC_DEVICE *Device;\r
549 EMMC_REQUEST *ProtocolReq;\r
550 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
551 EFI_TPL OldTpl;\r
552\r
553 ProtocolReq = NULL;\r
554\r
555 Device = Partition->Device;\r
556 PassThru = Device->Private->PassThru;\r
557\r
558 ProtocolReq = AllocateZeroPool (sizeof (EMMC_REQUEST));\r
559 if (ProtocolReq == NULL) {\r
560 Status = EFI_OUT_OF_RESOURCES;\r
561 goto Error;\r
562 }\r
563\r
564 ProtocolReq->Signature = EMMC_REQUEST_SIGNATURE;\r
3b1d8241 565 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
566 InsertTailList (&Partition->Queue, &ProtocolReq->Link);\r
567 gBS->RestoreTPL (OldTpl);\r
568 ProtocolReq->Packet.SdMmcCmdBlk = &ProtocolReq->SdMmcCmdBlk;\r
569 ProtocolReq->Packet.SdMmcStatusBlk = &ProtocolReq->SdMmcStatusBlk;\r
570\r
571 if (IsRead) {\r
572 ProtocolReq->Packet.InDataBuffer = PayloadBuffer;\r
573 ProtocolReq->Packet.InTransferLength = (UINT32)PayloadBufferSize;\r
574\r
575 ProtocolReq->SdMmcCmdBlk.CommandIndex = EMMC_PROTOCOL_RD;\r
576 ProtocolReq->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAdtc;\r
577 ProtocolReq->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
578 } else {\r
579 ProtocolReq->Packet.OutDataBuffer = PayloadBuffer;\r
580 ProtocolReq->Packet.OutTransferLength = (UINT32)PayloadBufferSize;\r
581\r
582 ProtocolReq->SdMmcCmdBlk.CommandIndex = EMMC_PROTOCOL_WR;\r
583 ProtocolReq->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAdtc;\r
584 ProtocolReq->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
585 }\r
586\r
587 ProtocolReq->SdMmcCmdBlk.CommandArgument = (SecurityProtocol << 8) | (SecurityProtocolSpecificData << 16);\r
588 //\r
589 // Convert to 1 microsecond unit.\r
590 //\r
591 ProtocolReq->Packet.Timeout = DivU64x32 (Timeout, 10) + 1;\r
592\r
593 ProtocolReq->IsEnd = IsEnd;\r
594 ProtocolReq->Token = Token;\r
595\r
596 if ((Token != NULL) && (Token->Event != NULL)) {\r
597 Status = gBS->CreateEvent (\r
598 EVT_NOTIFY_SIGNAL,\r
3b1d8241 599 TPL_NOTIFY,\r
48555339
FT
600 AsyncIoCallback,\r
601 ProtocolReq,\r
602 &ProtocolReq->Event\r
603 );\r
604 if (EFI_ERROR (Status)) {\r
605 goto Error;\r
606 }\r
607 } else {\r
608 ProtocolReq->Event = NULL;\r
609 }\r
610\r
611 Status = PassThru->PassThru (PassThru, Device->Slot, &ProtocolReq->Packet, ProtocolReq->Event);\r
612\r
613Error:\r
614 if ((Token != NULL) && (Token->Event != NULL)) {\r
615 //\r
616 // For asynchronous operation, only free request and event in error case.\r
617 // The request and event will be freed in asynchronous callback for success case.\r
618 //\r
619 if (EFI_ERROR (Status) && (ProtocolReq != NULL)) {\r
3b1d8241 620 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
621 RemoveEntryList (&ProtocolReq->Link);\r
622 gBS->RestoreTPL (OldTpl);\r
623 if (ProtocolReq->Event != NULL) {\r
624 gBS->CloseEvent (ProtocolReq->Event);\r
625 }\r
626 FreePool (ProtocolReq);\r
627 }\r
628 } else {\r
629 //\r
630 // For synchronous operation, free request whatever the execution result is.\r
631 //\r
632 if (ProtocolReq != NULL) {\r
3b1d8241 633 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
634 RemoveEntryList (&ProtocolReq->Link);\r
635 gBS->RestoreTPL (OldTpl);\r
636 FreePool (ProtocolReq);\r
637 }\r
638 }\r
639\r
640 return Status;\r
641}\r
642\r
643/**\r
644 Read/write multiple blocks through sync or async I/O request.\r
645\r
646 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
647 @param[in] Lba The starting logical block address to be read/written.\r
648 The caller is responsible for reading/writing to only\r
649 legitimate locations.\r
650 @param[in] Buffer A pointer to the destination/source buffer for the data.\r
651 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r
652 @param[in] IsRead Indicates it is a read or write operation.\r
653 @param[in] Token A pointer to the token associated with the transaction.\r
654 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.\r
655 This parameter is only meaningful in async I/O request.\r
656\r
657 @retval EFI_SUCCESS The request is executed successfully.\r
658 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
659 @retval Others The request could not be executed successfully.\r
660\r
661**/\r
662EFI_STATUS\r
663EmmcRwMultiBlocks (\r
664 IN EMMC_PARTITION *Partition,\r
665 IN EFI_LBA Lba,\r
666 IN VOID *Buffer,\r
667 IN UINTN BufferSize,\r
668 IN BOOLEAN IsRead,\r
669 IN EFI_BLOCK_IO2_TOKEN *Token,\r
670 IN BOOLEAN IsEnd\r
671 )\r
672{\r
673 EFI_STATUS Status;\r
674 EMMC_DEVICE *Device;\r
675 EMMC_REQUEST *RwMultiBlkReq;\r
676 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
677 EFI_TPL OldTpl;\r
678\r
679 RwMultiBlkReq = NULL;\r
680\r
681 Device = Partition->Device;\r
682 PassThru = Device->Private->PassThru;\r
683\r
684 RwMultiBlkReq = AllocateZeroPool (sizeof (EMMC_REQUEST));\r
685 if (RwMultiBlkReq == NULL) {\r
686 Status = EFI_OUT_OF_RESOURCES;\r
687 goto Error;\r
688 }\r
689\r
690 RwMultiBlkReq->Signature = EMMC_REQUEST_SIGNATURE;\r
3b1d8241 691 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
692 InsertTailList (&Partition->Queue, &RwMultiBlkReq->Link);\r
693 gBS->RestoreTPL (OldTpl);\r
694 RwMultiBlkReq->Packet.SdMmcCmdBlk = &RwMultiBlkReq->SdMmcCmdBlk;\r
695 RwMultiBlkReq->Packet.SdMmcStatusBlk = &RwMultiBlkReq->SdMmcStatusBlk;\r
696 //\r
697 // Calculate timeout value through the below formula.\r
698 // Timeout = (transfer size) / (2MB/s).\r
699 // Taking 2MB/s as divisor is because it's nearest to the eMMC lowest\r
700 // transfer speed (2.4MB/s).\r
701 // Refer to eMMC 5.0 spec section 6.9.1 for details.\r
702 //\r
703 RwMultiBlkReq->Packet.Timeout = (BufferSize / (2 * 1024 * 1024) + 1) * 1000 * 1000;\r
704\r
705 if (IsRead) {\r
706 RwMultiBlkReq->Packet.InDataBuffer = Buffer;\r
707 RwMultiBlkReq->Packet.InTransferLength = (UINT32)BufferSize;\r
708\r
709 RwMultiBlkReq->SdMmcCmdBlk.CommandIndex = EMMC_READ_MULTIPLE_BLOCK;\r
710 RwMultiBlkReq->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAdtc;\r
711 RwMultiBlkReq->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
712 } else {\r
713 RwMultiBlkReq->Packet.OutDataBuffer = Buffer;\r
714 RwMultiBlkReq->Packet.OutTransferLength = (UINT32)BufferSize;\r
715\r
716 RwMultiBlkReq->SdMmcCmdBlk.CommandIndex = EMMC_WRITE_MULTIPLE_BLOCK;\r
717 RwMultiBlkReq->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAdtc;\r
718 RwMultiBlkReq->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
719 }\r
720\r
721 if (Partition->Device->SectorAddressing) {\r
722 RwMultiBlkReq->SdMmcCmdBlk.CommandArgument = (UINT32)Lba;\r
723 } else {\r
724 RwMultiBlkReq->SdMmcCmdBlk.CommandArgument = (UINT32)MultU64x32 (Lba, Partition->BlockMedia.BlockSize);\r
725 }\r
726\r
727 RwMultiBlkReq->IsEnd = IsEnd;\r
728 RwMultiBlkReq->Token = Token;\r
729\r
730 if ((Token != NULL) && (Token->Event != NULL)) {\r
731 Status = gBS->CreateEvent (\r
732 EVT_NOTIFY_SIGNAL,\r
3b1d8241 733 TPL_NOTIFY,\r
48555339
FT
734 AsyncIoCallback,\r
735 RwMultiBlkReq,\r
736 &RwMultiBlkReq->Event\r
737 );\r
738 if (EFI_ERROR (Status)) {\r
739 goto Error;\r
740 }\r
741 } else {\r
742 RwMultiBlkReq->Event = NULL;\r
743 }\r
744\r
745 Status = PassThru->PassThru (PassThru, Device->Slot, &RwMultiBlkReq->Packet, RwMultiBlkReq->Event);\r
746\r
747Error:\r
748 if ((Token != NULL) && (Token->Event != NULL)) {\r
749 //\r
750 // For asynchronous operation, only free request and event in error case.\r
751 // The request and event will be freed in asynchronous callback for success case.\r
752 //\r
753 if (EFI_ERROR (Status) && (RwMultiBlkReq != NULL)) {\r
3b1d8241 754 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
755 RemoveEntryList (&RwMultiBlkReq->Link);\r
756 gBS->RestoreTPL (OldTpl);\r
757 if (RwMultiBlkReq->Event != NULL) {\r
758 gBS->CloseEvent (RwMultiBlkReq->Event);\r
759 }\r
760 FreePool (RwMultiBlkReq);\r
761 }\r
762 } else {\r
763 //\r
764 // For synchronous operation, free request whatever the execution result is.\r
765 //\r
766 if (RwMultiBlkReq != NULL) {\r
3b1d8241 767 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
768 RemoveEntryList (&RwMultiBlkReq->Link);\r
769 gBS->RestoreTPL (OldTpl);\r
770 FreePool (RwMultiBlkReq);\r
771 }\r
772 }\r
773\r
774 return Status;\r
775}\r
776\r
777/**\r
778 This function transfers data from/to EMMC device.\r
779\r
780 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
781 @param[in] MediaId The media ID that the read/write request is for.\r
782 @param[in] Lba The starting logical block address to be read/written.\r
783 The caller is responsible for reading/writing to only\r
784 legitimate locations.\r
785 @param[in, out] Buffer A pointer to the destination/source buffer for the data.\r
786 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r
787 @param[in] IsRead Indicates it is a read or write operation.\r
788 @param[in, out] Token A pointer to the token associated with the transaction.\r
789\r
790 @retval EFI_SUCCESS The data was read/written correctly to the device.\r
791 @retval EFI_WRITE_PROTECTED The device can not be read/written to.\r
792 @retval EFI_DEVICE_ERROR The device reported an error while performing the read/write.\r
793 @retval EFI_NO_MEDIA There is no media in the device.\r
794 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
795 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
796 @retval EFI_INVALID_PARAMETER The read/write request contains LBAs that are not valid,\r
797 or the buffer is not on proper alignment.\r
798\r
799**/\r
800EFI_STATUS\r
801EmmcReadWrite (\r
802 IN EMMC_PARTITION *Partition,\r
803 IN UINT32 MediaId,\r
804 IN EFI_LBA Lba,\r
805 IN OUT VOID *Buffer,\r
806 IN UINTN BufferSize,\r
807 IN BOOLEAN IsRead,\r
808 IN OUT EFI_BLOCK_IO2_TOKEN *Token\r
809 )\r
810{\r
811 EFI_STATUS Status;\r
812 EMMC_DEVICE *Device;\r
813 EFI_BLOCK_IO_MEDIA *Media;\r
814 UINTN BlockSize;\r
815 UINTN BlockNum;\r
816 UINTN IoAlign;\r
817 UINT8 PartitionConfig;\r
818 UINTN Remaining;\r
819 UINT32 MaxBlock;\r
820 BOOLEAN LastRw;\r
821\r
822 Status = EFI_SUCCESS;\r
823 Device = Partition->Device;\r
824 Media = &Partition->BlockMedia;\r
825 LastRw = FALSE;\r
826\r
827 if (MediaId != Media->MediaId) {\r
828 return EFI_MEDIA_CHANGED;\r
829 }\r
830\r
831 if (!IsRead && Media->ReadOnly) {\r
832 return EFI_WRITE_PROTECTED;\r
833 }\r
834\r
835 //\r
836 // Check parameters.\r
837 //\r
838 if (Buffer == NULL) {\r
839 return EFI_INVALID_PARAMETER;\r
840 }\r
841\r
842 if (BufferSize == 0) {\r
843 if ((Token != NULL) && (Token->Event != NULL)) {\r
844 Token->TransactionStatus = EFI_SUCCESS;\r
845 gBS->SignalEvent (Token->Event);\r
846 }\r
847 return EFI_SUCCESS;\r
848 }\r
849\r
850 BlockSize = Media->BlockSize;\r
851 if ((BufferSize % BlockSize) != 0) {\r
852 return EFI_BAD_BUFFER_SIZE;\r
853 }\r
854\r
855 BlockNum = BufferSize / BlockSize;\r
856 if ((Lba + BlockNum - 1) > Media->LastBlock) {\r
857 return EFI_INVALID_PARAMETER;\r
858 }\r
859\r
860 IoAlign = Media->IoAlign;\r
861 if (IoAlign > 0 && (((UINTN) Buffer & (IoAlign - 1)) != 0)) {\r
862 return EFI_INVALID_PARAMETER;\r
863 }\r
864\r
865 if ((Token != NULL) && (Token->Event != NULL)) {\r
866 Token->TransactionStatus = EFI_SUCCESS;\r
867 }\r
868 //\r
869 // Check if needs to switch partition access.\r
870 //\r
871 PartitionConfig = Device->ExtCsd.PartitionConfig;\r
872 if ((PartitionConfig & 0x7) != Partition->PartitionType) {\r
873 PartitionConfig &= (UINT8)~0x7;\r
874 PartitionConfig |= Partition->PartitionType;\r
875 Status = EmmcSetExtCsd (Partition, OFFSET_OF (EMMC_EXT_CSD, PartitionConfig), PartitionConfig, Token, FALSE);\r
876 if (EFI_ERROR (Status)) {\r
877 return Status;\r
878 }\r
879 Device->ExtCsd.PartitionConfig = PartitionConfig;\r
880 }\r
881 //\r
882 // Start to execute data transfer. The max block number in single cmd is 65535 blocks.\r
883 //\r
884 Remaining = BlockNum;\r
885 MaxBlock = 0xFFFF;\r
886\r
887 while (Remaining > 0) {\r
888 if (Remaining <= MaxBlock) {\r
889 BlockNum = Remaining;\r
890 LastRw = TRUE;\r
891 } else {\r
892 BlockNum = MaxBlock;\r
893 }\r
894 Status = EmmcSetBlkCount (Partition, (UINT16)BlockNum, Token, FALSE);\r
895 if (EFI_ERROR (Status)) {\r
896 return Status;\r
897 }\r
898\r
899 BufferSize = BlockNum * BlockSize;\r
900 Status = EmmcRwMultiBlocks (Partition, Lba, Buffer, BufferSize, IsRead, Token, LastRw);\r
901 if (EFI_ERROR (Status)) {\r
902 return Status;\r
903 }\r
9dca2105
AB
904 DEBUG ((DEBUG_BLKIO,\r
905 "Emmc%a(): Part %d Lba 0x%x BlkNo 0x%x Event %p with %r\n",\r
906 IsRead ? "Read " : "Write", Partition->PartitionType, Lba, BlockNum,\r
907 (Token != NULL) ? Token->Event : NULL, Status));\r
48555339
FT
908\r
909 Lba += BlockNum;\r
910 Buffer = (UINT8*)Buffer + BufferSize;\r
911 Remaining -= BlockNum;\r
912 }\r
913\r
914 return Status;\r
915}\r
916\r
917/**\r
918 Reset the Block Device.\r
919\r
920 @param This Indicates a pointer to the calling context.\r
921 @param ExtendedVerification Driver may perform diagnostics on reset.\r
922\r
923 @retval EFI_SUCCESS The device was reset.\r
924 @retval EFI_DEVICE_ERROR The device is not functioning properly and could\r
925 not be reset.\r
926\r
927**/\r
928EFI_STATUS\r
929EFIAPI\r
930EmmcReset (\r
931 IN EFI_BLOCK_IO_PROTOCOL *This,\r
932 IN BOOLEAN ExtendedVerification\r
933 )\r
934{\r
935 EFI_STATUS Status;\r
936 EMMC_PARTITION *Partition;\r
937 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
938\r
939 Partition = EMMC_PARTITION_DATA_FROM_BLKIO (This);\r
940\r
941 PassThru = Partition->Device->Private->PassThru;\r
942 Status = PassThru->ResetDevice (PassThru, Partition->Device->Slot);\r
943 if (EFI_ERROR (Status)) {\r
944 Status = EFI_DEVICE_ERROR;\r
945 }\r
946\r
947 return Status;\r
948}\r
949\r
950/**\r
951 Read BufferSize bytes from Lba into Buffer.\r
952\r
953 @param This Indicates a pointer to the calling context.\r
954 @param MediaId Id of the media, changes every time the media is replaced.\r
955 @param Lba The starting Logical Block Address to read from\r
956 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
957 @param Buffer A pointer to the destination buffer for the data. The caller is\r
958 responsible for either having implicit or explicit ownership of the buffer.\r
959\r
960 @retval EFI_SUCCESS The data was read correctly from the device.\r
961 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.\r
962 @retval EFI_NO_MEDIA There is no media in the device.\r
963 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.\r
964 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
965 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
966 or the buffer is not on proper alignment.\r
967\r
968**/\r
969EFI_STATUS\r
970EFIAPI\r
971EmmcReadBlocks (\r
972 IN EFI_BLOCK_IO_PROTOCOL *This,\r
973 IN UINT32 MediaId,\r
974 IN EFI_LBA Lba,\r
975 IN UINTN BufferSize,\r
976 OUT VOID *Buffer\r
977 )\r
978{\r
979 EFI_STATUS Status;\r
980 EMMC_PARTITION *Partition;\r
981\r
982 Partition = EMMC_PARTITION_DATA_FROM_BLKIO (This);\r
983\r
984 Status = EmmcReadWrite (Partition, MediaId, Lba, Buffer, BufferSize, TRUE, NULL);\r
985 return Status;\r
986}\r
987\r
988/**\r
989 Write BufferSize bytes from Lba into Buffer.\r
990\r
991 @param This Indicates a pointer to the calling context.\r
992 @param MediaId The media ID that the write request is for.\r
993 @param Lba The starting logical block address to be written. The caller is\r
994 responsible for writing to only legitimate locations.\r
995 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
996 @param Buffer A pointer to the source buffer for the data.\r
997\r
998 @retval EFI_SUCCESS The data was written correctly to the device.\r
999 @retval EFI_WRITE_PROTECTED The device can not be written to.\r
1000 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r
1001 @retval EFI_NO_MEDIA There is no media in the device.\r
1002 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
1003 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
1004 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
1005 or the buffer is not on proper alignment.\r
1006\r
1007**/\r
1008EFI_STATUS\r
1009EFIAPI\r
1010EmmcWriteBlocks (\r
1011 IN EFI_BLOCK_IO_PROTOCOL *This,\r
1012 IN UINT32 MediaId,\r
1013 IN EFI_LBA Lba,\r
1014 IN UINTN BufferSize,\r
1015 IN VOID *Buffer\r
1016 )\r
1017{\r
1018 EFI_STATUS Status;\r
1019 EMMC_PARTITION *Partition;\r
1020\r
1021 Partition = EMMC_PARTITION_DATA_FROM_BLKIO (This);\r
1022\r
1023 Status = EmmcReadWrite (Partition, MediaId, Lba, Buffer, BufferSize, FALSE, NULL);\r
1024 return Status;\r
1025}\r
1026\r
1027/**\r
1028 Flush the Block Device.\r
1029\r
1030 @param This Indicates a pointer to the calling context.\r
1031\r
1032 @retval EFI_SUCCESS All outstanding data was written to the device\r
1033 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data\r
1034 @retval EFI_NO_MEDIA There is no media in the device.\r
1035\r
1036**/\r
1037EFI_STATUS\r
1038EFIAPI\r
1039EmmcFlushBlocks (\r
1040 IN EFI_BLOCK_IO_PROTOCOL *This\r
1041 )\r
1042{\r
1043 //\r
1044 // return directly\r
1045 //\r
1046 return EFI_SUCCESS;\r
1047}\r
1048\r
1049/**\r
1050 Reset the Block Device.\r
1051\r
1052 @param[in] This Indicates a pointer to the calling context.\r
1053 @param[in] ExtendedVerification Driver may perform diagnostics on reset.\r
1054\r
1055 @retval EFI_SUCCESS The device was reset.\r
1056 @retval EFI_DEVICE_ERROR The device is not functioning properly and could\r
1057 not be reset.\r
1058\r
1059**/\r
1060EFI_STATUS\r
1061EFIAPI\r
1062EmmcResetEx (\r
1063 IN EFI_BLOCK_IO2_PROTOCOL *This,\r
1064 IN BOOLEAN ExtendedVerification\r
1065 )\r
1066{\r
1067 EMMC_PARTITION *Partition;\r
1068 LIST_ENTRY *Link;\r
1069 LIST_ENTRY *NextLink;\r
1070 EMMC_REQUEST *Request;\r
1071 EFI_TPL OldTpl;\r
1072\r
1073 Partition = EMMC_PARTITION_DATA_FROM_BLKIO2 (This);\r
1074\r
3b1d8241 1075 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
1076 for (Link = GetFirstNode (&Partition->Queue);\r
1077 !IsNull (&Partition->Queue, Link);\r
1078 Link = NextLink) {\r
1079 NextLink = GetNextNode (&Partition->Queue, Link);\r
1080 RemoveEntryList (Link);\r
1081\r
1082 Request = EMMC_REQUEST_FROM_LINK (Link);\r
1083\r
1084 gBS->CloseEvent (Request->Event);\r
1085 Request->Token->TransactionStatus = EFI_ABORTED;\r
1086\r
1087 if (Request->IsEnd) {\r
1088 gBS->SignalEvent (Request->Token->Event);\r
1089 }\r
1090\r
1091 FreePool (Request);\r
1092 }\r
1093 gBS->RestoreTPL (OldTpl);\r
1094\r
1095 return EFI_SUCCESS;\r
1096}\r
1097\r
1098/**\r
1099 Read BufferSize bytes from Lba into Buffer.\r
1100\r
1101 @param[in] This Indicates a pointer to the calling context.\r
1102 @param[in] MediaId Id of the media, changes every time the media is replaced.\r
1103 @param[in] Lba The starting Logical Block Address to read from.\r
1104 @param[in, out] Token A pointer to the token associated with the transaction.\r
1105 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r
1106 @param[out] Buffer A pointer to the destination buffer for the data. The caller is\r
1107 responsible for either having implicit or explicit ownership of the buffer.\r
1108\r
1109 @retval EFI_SUCCESS The read request was queued if Event is not NULL.\r
1110 The data was read correctly from the device if\r
1111 the Event is NULL.\r
1112 @retval EFI_DEVICE_ERROR The device reported an error while performing\r
1113 the read.\r
1114 @retval EFI_NO_MEDIA There is no media in the device.\r
1115 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
1116 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the\r
1117 intrinsic block size of the device.\r
1118 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
1119 or the buffer is not on proper alignment.\r
1120 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
1121 of resources.\r
1122\r
1123**/\r
1124EFI_STATUS\r
1125EFIAPI\r
1126EmmcReadBlocksEx (\r
1127 IN EFI_BLOCK_IO2_PROTOCOL *This,\r
1128 IN UINT32 MediaId,\r
1129 IN EFI_LBA Lba,\r
1130 IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r
1131 IN UINTN BufferSize,\r
1132 OUT VOID *Buffer\r
1133 )\r
1134{\r
1135 EFI_STATUS Status;\r
1136 EMMC_PARTITION *Partition;\r
1137\r
1138 Partition = EMMC_PARTITION_DATA_FROM_BLKIO2 (This);\r
1139\r
1140 Status = EmmcReadWrite (Partition, MediaId, Lba, Buffer, BufferSize, TRUE, Token);\r
1141 return Status;\r
1142}\r
1143\r
1144/**\r
1145 Write BufferSize bytes from Lba into Buffer.\r
1146\r
1147 @param[in] This Indicates a pointer to the calling context.\r
1148 @param[in] MediaId The media ID that the write request is for.\r
1149 @param[in] Lba The starting logical block address to be written. The\r
1150 caller is responsible for writing to only legitimate\r
1151 locations.\r
1152 @param[in, out] Token A pointer to the token associated with the transaction.\r
1153 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r
1154 @param[in] Buffer A pointer to the source buffer for the data.\r
1155\r
1156 @retval EFI_SUCCESS The data was written correctly to the device.\r
1157 @retval EFI_WRITE_PROTECTED The device can not be written to.\r
1158 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r
1159 @retval EFI_NO_MEDIA There is no media in the device.\r
1160 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
1161 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
1162 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
1163 or the buffer is not on proper alignment.\r
1164\r
1165**/\r
1166EFI_STATUS\r
1167EFIAPI\r
1168EmmcWriteBlocksEx (\r
1169 IN EFI_BLOCK_IO2_PROTOCOL *This,\r
1170 IN UINT32 MediaId,\r
1171 IN EFI_LBA Lba,\r
1172 IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r
1173 IN UINTN BufferSize,\r
1174 IN VOID *Buffer\r
1175 )\r
1176{\r
1177 EFI_STATUS Status;\r
1178 EMMC_PARTITION *Partition;\r
1179\r
1180 Partition = EMMC_PARTITION_DATA_FROM_BLKIO2 (This);\r
1181\r
1182 Status = EmmcReadWrite (Partition, MediaId, Lba, Buffer, BufferSize, FALSE, Token);\r
1183 return Status;\r
1184}\r
1185\r
1186/**\r
1187 Flush the Block Device.\r
1188\r
1189 @param[in] This Indicates a pointer to the calling context.\r
1190 @param[in, out] Token A pointer to the token associated with the transaction.\r
1191\r
1192 @retval EFI_SUCCESS All outstanding data was written to the device\r
1193 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data\r
1194 @retval EFI_NO_MEDIA There is no media in the device.\r
1195\r
1196**/\r
1197EFI_STATUS\r
1198EFIAPI\r
1199EmmcFlushBlocksEx (\r
1200 IN EFI_BLOCK_IO2_PROTOCOL *This,\r
1201 IN OUT EFI_BLOCK_IO2_TOKEN *Token\r
1202 )\r
1203{\r
1204 //\r
1205 // Signal event and return directly.\r
1206 //\r
1207 if (Token != NULL && Token->Event != NULL) {\r
1208 Token->TransactionStatus = EFI_SUCCESS;\r
1209 gBS->SignalEvent (Token->Event);\r
1210 }\r
1211\r
1212 return EFI_SUCCESS;\r
1213}\r
1214\r
1215/**\r
1216 Send a security protocol command to a device that receives data and/or the result\r
1217 of one or more commands sent by SendData.\r
1218\r
1219 The ReceiveData function sends a security protocol command to the given MediaId.\r
1220 The security protocol command sent is defined by SecurityProtocolId and contains\r
1221 the security protocol specific data SecurityProtocolSpecificData. The function\r
1222 returns the data from the security protocol command in PayloadBuffer.\r
1223\r
1224 For devices supporting the SCSI command set, the security protocol command is sent\r
1225 using the SECURITY PROTOCOL IN command defined in SPC-4.\r
1226\r
1227 For devices supporting the ATA command set, the security protocol command is sent\r
1228 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize\r
1229 is non-zero.\r
1230\r
1231 If the PayloadBufferSize is zero, the security protocol command is sent using the\r
1232 Trusted Non-Data command defined in ATA8-ACS.\r
1233\r
1234 If PayloadBufferSize is too small to store the available data from the security\r
1235 protocol command, the function shall copy PayloadBufferSize bytes into the\r
1236 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.\r
1237\r
1238 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,\r
1239 the function shall return EFI_INVALID_PARAMETER.\r
1240\r
1241 If the given MediaId does not support security protocol commands, the function shall\r
1242 return EFI_UNSUPPORTED. If there is no media in the device, the function returns\r
1243 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,\r
1244 the function returns EFI_MEDIA_CHANGED.\r
1245\r
1246 If the security protocol fails to complete within the Timeout period, the function\r
1247 shall return EFI_TIMEOUT.\r
1248\r
1249 If the security protocol command completes without an error, the function shall\r
1250 return EFI_SUCCESS. If the security protocol command completes with an error, the\r
1251 function shall return EFI_DEVICE_ERROR.\r
1252\r
1253 @param[in] This Indicates a pointer to the calling context.\r
1254 @param[in] MediaId ID of the medium to receive data from.\r
1255 @param[in] Timeout The timeout, in 100ns units, to use for the execution\r
1256 of the security protocol command. A Timeout value of 0\r
1257 means that this function will wait indefinitely for the\r
1258 security protocol command to execute. If Timeout is greater\r
1259 than zero, then this function will return EFI_TIMEOUT\r
1260 if the time required to execute the receive data command\r
1261 is greater than Timeout.\r
1262 @param[in] SecurityProtocolId The value of the "Security Protocol" parameter of\r
1263 the security protocol command to be sent.\r
1264 @param[in] SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
1265 of the security protocol command to be sent.\r
1266 @param[in] PayloadBufferSize Size in bytes of the payload data buffer.\r
1267 @param[out] PayloadBuffer A pointer to a destination buffer to store the security\r
1268 protocol command specific payload data for the security\r
1269 protocol command. The caller is responsible for having\r
1270 either implicit or explicit ownership of the buffer.\r
1271 @param[out] PayloadTransferSize A pointer to a buffer to store the size in bytes of the\r
1272 data written to the payload data buffer.\r
1273 @param[in] IsRead Indicates it is a read or write operation.\r
1274\r
1275 @retval EFI_SUCCESS The security protocol command completed successfully.\r
1276 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available\r
1277 data from the device. The PayloadBuffer contains the truncated data.\r
1278 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.\r
1279 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.\r
1280 @retval EFI_NO_MEDIA There is no media in the device.\r
1281 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
1282 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and\r
1283 PayloadBufferSize is non-zero.\r
1284 @retval EFI_TIMEOUT A timeout occurred while waiting for the security\r
1285 protocol command to execute.\r
1286\r
1287**/\r
1288EFI_STATUS\r
1289EFIAPI\r
1290EmmcSecurityProtocolInOut (\r
1291 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,\r
1292 IN UINT32 MediaId,\r
1293 IN UINT64 Timeout,\r
1294 IN UINT8 SecurityProtocolId,\r
1295 IN UINT16 SecurityProtocolSpecificData,\r
1296 IN UINTN PayloadBufferSize,\r
1297 OUT VOID *PayloadBuffer,\r
1298 OUT UINTN *PayloadTransferSize,\r
1299 IN BOOLEAN IsRead\r
1300 )\r
1301{\r
1302 EFI_STATUS Status;\r
1303 EMMC_PARTITION *Partition;\r
1304 EMMC_DEVICE *Device;\r
1305 EFI_BLOCK_IO_MEDIA *Media;\r
1306 UINTN BlockSize;\r
1307 UINTN BlockNum;\r
1308 UINTN IoAlign;\r
1309 UINTN Remaining;\r
1310 UINT32 MaxBlock;\r
1311 UINT8 PartitionConfig;\r
1312\r
1313 Status = EFI_SUCCESS;\r
1314 Partition = EMMC_PARTITION_DATA_FROM_SSP (This);\r
1315 Device = Partition->Device;\r
1316 Media = &Partition->BlockMedia;\r
1317\r
1318 if (PayloadTransferSize != NULL) {\r
1319 *PayloadTransferSize = 0;\r
1320 }\r
1321\r
1322 if ((PayloadBuffer == NULL) && (PayloadBufferSize != 0)) {\r
1323 return EFI_INVALID_PARAMETER;\r
1324 }\r
1325\r
1326 if (MediaId != Media->MediaId) {\r
1327 return EFI_MEDIA_CHANGED;\r
1328 }\r
1329\r
1330 if (PayloadBufferSize == 0) {\r
1331 return EFI_SUCCESS;\r
1332 }\r
1333\r
1334 BlockSize = Media->BlockSize;\r
1335 if ((PayloadBufferSize % BlockSize) != 0) {\r
1336 return EFI_BAD_BUFFER_SIZE;\r
1337 }\r
1338\r
1339 BlockNum = PayloadBufferSize / BlockSize;\r
1340\r
1341 IoAlign = Media->IoAlign;\r
1342 if (IoAlign > 0 && (((UINTN) PayloadBuffer & (IoAlign - 1)) != 0)) {\r
1343 return EFI_INVALID_PARAMETER;\r
1344 }\r
1345\r
1346 //\r
1347 // Security protocol interface is synchronous transfer.\r
1348 // Waiting for async I/O list to be empty before any operation.\r
1349 //\r
1350 while (!IsListEmpty (&Partition->Queue));\r
1351\r
1352 //\r
1353 // Check if needs to switch partition access.\r
1354 //\r
1355 PartitionConfig = Device->ExtCsd.PartitionConfig;\r
1356 if ((PartitionConfig & 0x7) != Partition->PartitionType) {\r
1357 PartitionConfig &= (UINT8)~0x7;\r
1358 PartitionConfig |= Partition->PartitionType;\r
1359 Status = EmmcSetExtCsd (Partition, OFFSET_OF (EMMC_EXT_CSD, PartitionConfig), PartitionConfig, NULL, FALSE);\r
1360 if (EFI_ERROR (Status)) {\r
1361 return Status;\r
1362 }\r
1363 Device->ExtCsd.PartitionConfig = PartitionConfig;\r
1364 }\r
1365 //\r
1366 // Start to execute data transfer. The max block number in single cmd is 65535 blocks.\r
1367 //\r
1368 Remaining = BlockNum;\r
1369 MaxBlock = 0xFFFF;\r
1370\r
1371 while (Remaining > 0) {\r
1372 if (Remaining <= MaxBlock) {\r
1373 BlockNum = Remaining;\r
1374 } else {\r
1375 BlockNum = MaxBlock;\r
1376 }\r
1377\r
1378 Status = EmmcSetBlkCount (Partition, (UINT16)BlockNum, NULL, FALSE);\r
1379 if (EFI_ERROR (Status)) {\r
1380 return Status;\r
1381 }\r
1382\r
1383 PayloadBufferSize = BlockNum * BlockSize;\r
1384 Status = EmmcProtocolInOut (Partition, SecurityProtocolId, SecurityProtocolSpecificData, PayloadBufferSize, PayloadBuffer, IsRead, Timeout, NULL, FALSE);\r
1385 if (EFI_ERROR (Status)) {\r
1386 return Status;\r
1387 }\r
1388\r
1389 PayloadBuffer = (UINT8*)PayloadBuffer + PayloadBufferSize;\r
1390 Remaining -= BlockNum;\r
1391 if (PayloadTransferSize != NULL) {\r
1392 *PayloadTransferSize += PayloadBufferSize;\r
1393 }\r
1394 }\r
1395\r
1396 return Status;\r
1397}\r
1398\r
1399/**\r
1400 Send a security protocol command to a device that receives data and/or the result\r
1401 of one or more commands sent by SendData.\r
1402\r
1403 The ReceiveData function sends a security protocol command to the given MediaId.\r
1404 The security protocol command sent is defined by SecurityProtocolId and contains\r
1405 the security protocol specific data SecurityProtocolSpecificData. The function\r
1406 returns the data from the security protocol command in PayloadBuffer.\r
1407\r
1408 For devices supporting the SCSI command set, the security protocol command is sent\r
1409 using the SECURITY PROTOCOL IN command defined in SPC-4.\r
1410\r
1411 For devices supporting the ATA command set, the security protocol command is sent\r
1412 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize\r
1413 is non-zero.\r
1414\r
1415 If the PayloadBufferSize is zero, the security protocol command is sent using the\r
1416 Trusted Non-Data command defined in ATA8-ACS.\r
1417\r
1418 If PayloadBufferSize is too small to store the available data from the security\r
1419 protocol command, the function shall copy PayloadBufferSize bytes into the\r
1420 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.\r
1421\r
1422 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,\r
1423 the function shall return EFI_INVALID_PARAMETER.\r
1424\r
1425 If the given MediaId does not support security protocol commands, the function shall\r
1426 return EFI_UNSUPPORTED. If there is no media in the device, the function returns\r
1427 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,\r
1428 the function returns EFI_MEDIA_CHANGED.\r
1429\r
1430 If the security protocol fails to complete within the Timeout period, the function\r
1431 shall return EFI_TIMEOUT.\r
1432\r
1433 If the security protocol command completes without an error, the function shall\r
1434 return EFI_SUCCESS. If the security protocol command completes with an error, the\r
1435 function shall return EFI_DEVICE_ERROR.\r
1436\r
1437 @param This Indicates a pointer to the calling context.\r
1438 @param MediaId ID of the medium to receive data from.\r
1439 @param Timeout The timeout, in 100ns units, to use for the execution\r
1440 of the security protocol command. A Timeout value of 0\r
1441 means that this function will wait indefinitely for the\r
1442 security protocol command to execute. If Timeout is greater\r
1443 than zero, then this function will return EFI_TIMEOUT\r
1444 if the time required to execute the receive data command\r
1445 is greater than Timeout.\r
1446 @param SecurityProtocolId The value of the "Security Protocol" parameter of\r
1447 the security protocol command to be sent.\r
1448 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
1449 of the security protocol command to be sent.\r
1450 @param PayloadBufferSize Size in bytes of the payload data buffer.\r
1451 @param PayloadBuffer A pointer to a destination buffer to store the security\r
1452 protocol command specific payload data for the security\r
1453 protocol command. The caller is responsible for having\r
1454 either implicit or explicit ownership of the buffer.\r
1455 @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the\r
1456 data written to the payload data buffer.\r
1457\r
1458 @retval EFI_SUCCESS The security protocol command completed successfully.\r
1459 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available\r
1460 data from the device. The PayloadBuffer contains the truncated data.\r
1461 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.\r
1462 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.\r
1463 @retval EFI_NO_MEDIA There is no media in the device.\r
1464 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
1465 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and\r
1466 PayloadBufferSize is non-zero.\r
1467 @retval EFI_TIMEOUT A timeout occurred while waiting for the security\r
1468 protocol command to execute.\r
1469\r
1470**/\r
1471EFI_STATUS\r
1472EFIAPI\r
1473EmmcSecurityProtocolIn (\r
1474 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,\r
1475 IN UINT32 MediaId,\r
1476 IN UINT64 Timeout,\r
1477 IN UINT8 SecurityProtocolId,\r
1478 IN UINT16 SecurityProtocolSpecificData,\r
1479 IN UINTN PayloadBufferSize,\r
1480 OUT VOID *PayloadBuffer,\r
1481 OUT UINTN *PayloadTransferSize\r
1482 )\r
1483{\r
1484 EFI_STATUS Status;\r
1485\r
1486 if ((PayloadTransferSize == NULL) && PayloadBufferSize != 0) {\r
1487 return EFI_INVALID_PARAMETER;\r
1488 }\r
1489\r
1490 Status = EmmcSecurityProtocolInOut (\r
1491 This,\r
1492 MediaId,\r
1493 Timeout,\r
1494 SecurityProtocolId,\r
1495 SecurityProtocolSpecificData,\r
1496 PayloadBufferSize,\r
1497 PayloadBuffer,\r
1498 PayloadTransferSize,\r
1499 TRUE\r
1500 );\r
1501\r
1502 return Status;\r
1503}\r
1504\r
1505/**\r
1506 Send a security protocol command to a device.\r
1507\r
1508 The SendData function sends a security protocol command containing the payload\r
1509 PayloadBuffer to the given MediaId. The security protocol command sent is\r
1510 defined by SecurityProtocolId and contains the security protocol specific data\r
1511 SecurityProtocolSpecificData. If the underlying protocol command requires a\r
1512 specific padding for the command payload, the SendData function shall add padding\r
1513 bytes to the command payload to satisfy the padding requirements.\r
1514\r
1515 For devices supporting the SCSI command set, the security protocol command is sent\r
1516 using the SECURITY PROTOCOL OUT command defined in SPC-4.\r
1517\r
1518 For devices supporting the ATA command set, the security protocol command is sent\r
1519 using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize\r
1520 is non-zero. If the PayloadBufferSize is zero, the security protocol command is\r
1521 sent using the Trusted Non-Data command defined in ATA8-ACS.\r
1522\r
1523 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall\r
1524 return EFI_INVALID_PARAMETER.\r
1525\r
1526 If the given MediaId does not support security protocol commands, the function\r
1527 shall return EFI_UNSUPPORTED. If there is no media in the device, the function\r
1528 returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the\r
1529 device, the function returns EFI_MEDIA_CHANGED.\r
1530\r
1531 If the security protocol fails to complete within the Timeout period, the function\r
1532 shall return EFI_TIMEOUT.\r
1533\r
1534 If the security protocol command completes without an error, the function shall return\r
1535 EFI_SUCCESS. If the security protocol command completes with an error, the function\r
1536 shall return EFI_DEVICE_ERROR.\r
1537\r
1538 @param This Indicates a pointer to the calling context.\r
1539 @param MediaId ID of the medium to receive data from.\r
1540 @param Timeout The timeout, in 100ns units, to use for the execution\r
1541 of the security protocol command. A Timeout value of 0\r
1542 means that this function will wait indefinitely for the\r
1543 security protocol command to execute. If Timeout is greater\r
1544 than zero, then this function will return EFI_TIMEOUT\r
1545 if the time required to execute the receive data command\r
1546 is greater than Timeout.\r
1547 @param SecurityProtocolId The value of the "Security Protocol" parameter of\r
1548 the security protocol command to be sent.\r
1549 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
1550 of the security protocol command to be sent.\r
1551 @param PayloadBufferSize Size in bytes of the payload data buffer.\r
1552 @param PayloadBuffer A pointer to a destination buffer to store the security\r
1553 protocol command specific payload data for the security\r
1554 protocol command.\r
1555\r
1556 @retval EFI_SUCCESS The security protocol command completed successfully.\r
1557 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.\r
1558 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.\r
1559 @retval EFI_NO_MEDIA There is no media in the device.\r
1560 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
1561 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.\r
1562 @retval EFI_TIMEOUT A timeout occurred while waiting for the security\r
1563 protocol command to execute.\r
1564\r
1565**/\r
1566EFI_STATUS\r
1567EFIAPI\r
1568EmmcSecurityProtocolOut (\r
1569 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,\r
1570 IN UINT32 MediaId,\r
1571 IN UINT64 Timeout,\r
1572 IN UINT8 SecurityProtocolId,\r
1573 IN UINT16 SecurityProtocolSpecificData,\r
1574 IN UINTN PayloadBufferSize,\r
1575 IN VOID *PayloadBuffer\r
1576 )\r
1577{\r
1578 EFI_STATUS Status;\r
1579\r
1580 Status = EmmcSecurityProtocolInOut (\r
1581 This,\r
1582 MediaId,\r
1583 Timeout,\r
1584 SecurityProtocolId,\r
1585 SecurityProtocolSpecificData,\r
1586 PayloadBufferSize,\r
1587 PayloadBuffer,\r
1588 NULL,\r
1589 FALSE\r
1590 );\r
1591\r
1592 return Status;\r
1593}\r
1594\r
275d5136
FT
1595/**\r
1596 Set the erase start address through sync or async I/O request.\r
1597\r
1598 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
1599 @param[in] StartLba The starting logical block address to be erased.\r
1600 @param[in] Token A pointer to the token associated with the transaction.\r
1601 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.\r
1602 This parameter is only meaningful in async I/O request.\r
1603\r
1604 @retval EFI_SUCCESS The request is executed successfully.\r
1605 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
1606 @retval Others The request could not be executed successfully.\r
1607\r
1608**/\r
1609EFI_STATUS\r
1610EmmcEraseBlockStart (\r
1611 IN EMMC_PARTITION *Partition,\r
1612 IN EFI_LBA StartLba,\r
1613 IN EFI_BLOCK_IO2_TOKEN *Token,\r
1614 IN BOOLEAN IsEnd\r
1615 )\r
1616{\r
1617 EFI_STATUS Status;\r
1618 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
1619 EMMC_DEVICE *Device;\r
1620 EMMC_REQUEST *EraseBlockStart;\r
1621 EFI_TPL OldTpl;\r
1622\r
1623 EraseBlockStart = NULL;\r
1624\r
1625 Device = Partition->Device;\r
1626 PassThru = Device->Private->PassThru;\r
1627\r
1628 EraseBlockStart = AllocateZeroPool (sizeof (EMMC_REQUEST));\r
1629 if (EraseBlockStart == NULL) {\r
1630 Status = EFI_OUT_OF_RESOURCES;\r
1631 goto Error;\r
1632 }\r
1633\r
1634 EraseBlockStart->Signature = EMMC_REQUEST_SIGNATURE;\r
3b1d8241 1635 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136
FT
1636 InsertTailList (&Partition->Queue, &EraseBlockStart->Link);\r
1637 gBS->RestoreTPL (OldTpl);\r
1638 EraseBlockStart->Packet.SdMmcCmdBlk = &EraseBlockStart->SdMmcCmdBlk;\r
1639 EraseBlockStart->Packet.SdMmcStatusBlk = &EraseBlockStart->SdMmcStatusBlk;\r
1640 EraseBlockStart->Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
1641\r
1642 EraseBlockStart->SdMmcCmdBlk.CommandIndex = EMMC_ERASE_GROUP_START;\r
1643 EraseBlockStart->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
1644 EraseBlockStart->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
1645\r
1646 if (Device->SectorAddressing) {\r
1647 EraseBlockStart->SdMmcCmdBlk.CommandArgument = (UINT32)StartLba;\r
1648 } else {\r
1649 EraseBlockStart->SdMmcCmdBlk.CommandArgument = (UINT32)MultU64x32 (StartLba, Partition->BlockMedia.BlockSize);\r
1650 }\r
1651\r
1652 EraseBlockStart->IsEnd = IsEnd;\r
1653 EraseBlockStart->Token = Token;\r
1654\r
1655 if ((Token != NULL) && (Token->Event != NULL)) {\r
1656 Status = gBS->CreateEvent (\r
1657 EVT_NOTIFY_SIGNAL,\r
3b1d8241 1658 TPL_NOTIFY,\r
275d5136
FT
1659 AsyncIoCallback,\r
1660 EraseBlockStart,\r
1661 &EraseBlockStart->Event\r
1662 );\r
1663 if (EFI_ERROR (Status)) {\r
1664 goto Error;\r
1665 }\r
1666 } else {\r
1667 EraseBlockStart->Event = NULL;\r
1668 }\r
1669\r
1670 Status = PassThru->PassThru (PassThru, Device->Slot, &EraseBlockStart->Packet, EraseBlockStart->Event);\r
1671\r
1672Error:\r
1673 if ((Token != NULL) && (Token->Event != NULL)) {\r
1674 //\r
1675 // For asynchronous operation, only free request and event in error case.\r
1676 // The request and event will be freed in asynchronous callback for success case.\r
1677 //\r
1678 if (EFI_ERROR (Status) && (EraseBlockStart != NULL)) {\r
3b1d8241 1679 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136 1680 RemoveEntryList (&EraseBlockStart->Link);\r
3b1d8241 1681 gBS->RestoreTPL (OldTpl);\r
275d5136
FT
1682 if (EraseBlockStart->Event != NULL) {\r
1683 gBS->CloseEvent (EraseBlockStart->Event);\r
1684 }\r
1685 FreePool (EraseBlockStart);\r
1686 }\r
1687 } else {\r
1688 //\r
1689 // For synchronous operation, free request whatever the execution result is.\r
1690 //\r
1691 if (EraseBlockStart != NULL) {\r
3b1d8241 1692 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136 1693 RemoveEntryList (&EraseBlockStart->Link);\r
3b1d8241 1694 gBS->RestoreTPL (OldTpl);\r
275d5136
FT
1695 FreePool (EraseBlockStart);\r
1696 }\r
1697 }\r
1698\r
1699 return Status;\r
1700}\r
1701\r
1702/**\r
1703 Set the erase end address through sync or async I/O request.\r
1704\r
1705 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
1706 @param[in] EndLba The ending logical block address to be erased.\r
1707 @param[in] Token A pointer to the token associated with the transaction.\r
1708 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.\r
1709 This parameter is only meaningful in async I/O request.\r
1710\r
1711 @retval EFI_SUCCESS The request is executed successfully.\r
1712 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
1713 @retval Others The request could not be executed successfully.\r
1714\r
1715**/\r
1716EFI_STATUS\r
1717EmmcEraseBlockEnd (\r
1718 IN EMMC_PARTITION *Partition,\r
1719 IN EFI_LBA EndLba,\r
1720 IN EFI_BLOCK_IO2_TOKEN *Token,\r
1721 IN BOOLEAN IsEnd\r
1722 )\r
1723{\r
1724 EFI_STATUS Status;\r
1725 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
1726 EMMC_DEVICE *Device;\r
1727 EMMC_REQUEST *EraseBlockEnd;\r
1728 EFI_TPL OldTpl;\r
1729\r
1730 EraseBlockEnd = NULL;\r
1731\r
1732 Device = Partition->Device;\r
1733 PassThru = Device->Private->PassThru;\r
1734\r
1735 EraseBlockEnd = AllocateZeroPool (sizeof (EMMC_REQUEST));\r
1736 if (EraseBlockEnd == NULL) {\r
1737 Status = EFI_OUT_OF_RESOURCES;\r
1738 goto Error;\r
1739 }\r
1740\r
1741 EraseBlockEnd->Signature = EMMC_REQUEST_SIGNATURE;\r
3b1d8241 1742 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136
FT
1743 InsertTailList (&Partition->Queue, &EraseBlockEnd->Link);\r
1744 gBS->RestoreTPL (OldTpl);\r
1745 EraseBlockEnd->Packet.SdMmcCmdBlk = &EraseBlockEnd->SdMmcCmdBlk;\r
1746 EraseBlockEnd->Packet.SdMmcStatusBlk = &EraseBlockEnd->SdMmcStatusBlk;\r
1747 EraseBlockEnd->Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
1748\r
1749 EraseBlockEnd->SdMmcCmdBlk.CommandIndex = EMMC_ERASE_GROUP_END;\r
1750 EraseBlockEnd->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
1751 EraseBlockEnd->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;\r
1752\r
1753 if (Device->SectorAddressing) {\r
1754 EraseBlockEnd->SdMmcCmdBlk.CommandArgument = (UINT32)EndLba;\r
1755 } else {\r
1756 EraseBlockEnd->SdMmcCmdBlk.CommandArgument = (UINT32)MultU64x32 (EndLba, Partition->BlockMedia.BlockSize);\r
1757 }\r
1758\r
1759 EraseBlockEnd->IsEnd = IsEnd;\r
1760 EraseBlockEnd->Token = Token;\r
1761\r
1762 if ((Token != NULL) && (Token->Event != NULL)) {\r
1763 Status = gBS->CreateEvent (\r
1764 EVT_NOTIFY_SIGNAL,\r
3b1d8241 1765 TPL_NOTIFY,\r
275d5136
FT
1766 AsyncIoCallback,\r
1767 EraseBlockEnd,\r
1768 &EraseBlockEnd->Event\r
1769 );\r
1770 if (EFI_ERROR (Status)) {\r
1771 goto Error;\r
1772 }\r
1773 } else {\r
1774 EraseBlockEnd->Event = NULL;\r
1775 }\r
1776\r
1777 Status = PassThru->PassThru (PassThru, Device->Slot, &EraseBlockEnd->Packet, EraseBlockEnd->Event);\r
1778\r
1779Error:\r
1780 if ((Token != NULL) && (Token->Event != NULL)) {\r
1781 //\r
1782 // For asynchronous operation, only free request and event in error case.\r
1783 // The request and event will be freed in asynchronous callback for success case.\r
1784 //\r
1785 if (EFI_ERROR (Status) && (EraseBlockEnd != NULL)) {\r
3b1d8241 1786 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136 1787 RemoveEntryList (&EraseBlockEnd->Link);\r
3b1d8241 1788 gBS->RestoreTPL (OldTpl);\r
275d5136
FT
1789 if (EraseBlockEnd->Event != NULL) {\r
1790 gBS->CloseEvent (EraseBlockEnd->Event);\r
1791 }\r
1792 FreePool (EraseBlockEnd);\r
1793 }\r
1794 } else {\r
1795 //\r
1796 // For synchronous operation, free request whatever the execution result is.\r
1797 //\r
1798 if (EraseBlockEnd != NULL) {\r
3b1d8241 1799 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136 1800 RemoveEntryList (&EraseBlockEnd->Link);\r
3b1d8241 1801 gBS->RestoreTPL (OldTpl);\r
275d5136
FT
1802 FreePool (EraseBlockEnd);\r
1803 }\r
1804 }\r
1805\r
1806 return Status;\r
1807}\r
1808\r
1809/**\r
1810 Erase specified blocks through sync or async I/O request.\r
1811\r
1812 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
1813 @param[in] Token A pointer to the token associated with the transaction.\r
1814 @param[in] IsEnd A boolean to show whether it's the last cmd in a series of cmds.\r
1815 This parameter is only meaningful in async I/O request.\r
1816\r
1817 @retval EFI_SUCCESS The request is executed successfully.\r
1818 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
1819 @retval Others The request could not be executed successfully.\r
1820\r
1821**/\r
1822EFI_STATUS\r
1823EmmcEraseBlock (\r
1824 IN EMMC_PARTITION *Partition,\r
1825 IN EFI_BLOCK_IO2_TOKEN *Token,\r
1826 IN BOOLEAN IsEnd\r
1827 )\r
1828{\r
1829 EFI_STATUS Status;\r
1830 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
1831 EMMC_DEVICE *Device;\r
1832 EMMC_REQUEST *EraseBlock;\r
1833 EFI_TPL OldTpl;\r
1834\r
1835 EraseBlock = NULL;\r
1836\r
1837 Device = Partition->Device;\r
1838 PassThru = Device->Private->PassThru;\r
1839\r
1840 EraseBlock = AllocateZeroPool (sizeof (EMMC_REQUEST));\r
1841 if (EraseBlock == NULL) {\r
1842 Status = EFI_OUT_OF_RESOURCES;\r
1843 goto Error;\r
1844 }\r
1845\r
1846 EraseBlock->Signature = EMMC_REQUEST_SIGNATURE;\r
3b1d8241 1847 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136
FT
1848 InsertTailList (&Partition->Queue, &EraseBlock->Link);\r
1849 gBS->RestoreTPL (OldTpl);\r
1850 EraseBlock->Packet.SdMmcCmdBlk = &EraseBlock->SdMmcCmdBlk;\r
1851 EraseBlock->Packet.SdMmcStatusBlk = &EraseBlock->SdMmcStatusBlk;\r
1852 EraseBlock->Packet.Timeout = EMMC_GENERIC_TIMEOUT;\r
1853\r
1854 EraseBlock->SdMmcCmdBlk.CommandIndex = EMMC_ERASE;\r
1855 EraseBlock->SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;\r
1856 EraseBlock->SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1b;\r
b92efc9f
HW
1857 if ((Device->ExtCsd.SecFeatureSupport & BIT4) != 0) {\r
1858 //\r
1859 // Perform a Trim operation which applies the erase operation to write blocks\r
1860 // instead of erase groups. (Spec JESD84-B51, eMMC Electrical Standard 5.1,\r
1861 // Section 6.6.10 and 6.10.4)\r
1862 //\r
1863 EraseBlock->SdMmcCmdBlk.CommandArgument = 1;\r
1864 }\r
275d5136
FT
1865\r
1866 EraseBlock->IsEnd = IsEnd;\r
1867 EraseBlock->Token = Token;\r
1868\r
1869 if ((Token != NULL) && (Token->Event != NULL)) {\r
1870 Status = gBS->CreateEvent (\r
1871 EVT_NOTIFY_SIGNAL,\r
3b1d8241 1872 TPL_NOTIFY,\r
275d5136
FT
1873 AsyncIoCallback,\r
1874 EraseBlock,\r
1875 &EraseBlock->Event\r
1876 );\r
1877 if (EFI_ERROR (Status)) {\r
1878 goto Error;\r
1879 }\r
1880 } else {\r
1881 EraseBlock->Event = NULL;\r
1882 }\r
1883\r
1884 Status = PassThru->PassThru (PassThru, Device->Slot, &EraseBlock->Packet, EraseBlock->Event);\r
1885\r
1886Error:\r
1887 if ((Token != NULL) && (Token->Event != NULL)) {\r
1888 //\r
1889 // For asynchronous operation, only free request and event in error case.\r
1890 // The request and event will be freed in asynchronous callback for success case.\r
1891 //\r
1892 if (EFI_ERROR (Status) && (EraseBlock != NULL)) {\r
3b1d8241 1893 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136 1894 RemoveEntryList (&EraseBlock->Link);\r
3b1d8241 1895 gBS->RestoreTPL (OldTpl);\r
275d5136
FT
1896 if (EraseBlock->Event != NULL) {\r
1897 gBS->CloseEvent (EraseBlock->Event);\r
1898 }\r
1899 FreePool (EraseBlock);\r
1900 }\r
1901 } else {\r
1902 //\r
1903 // For synchronous operation, free request whatever the execution result is.\r
1904 //\r
1905 if (EraseBlock != NULL) {\r
3b1d8241 1906 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
275d5136 1907 RemoveEntryList (&EraseBlock->Link);\r
3b1d8241 1908 gBS->RestoreTPL (OldTpl);\r
275d5136
FT
1909 FreePool (EraseBlock);\r
1910 }\r
1911 }\r
1912\r
1913 return Status;\r
1914}\r
1915\r
b92efc9f
HW
1916/**\r
1917 Write zeros to specified blocks.\r
1918\r
1919 @param[in] Partition A pointer to the EMMC_PARTITION instance.\r
1920 @param[in] StartLba The starting logical block address to write zeros.\r
1921 @param[in] Size The size in bytes to fill with zeros. This must be a multiple of\r
1922 the physical block size of the device.\r
1923\r
1924 @retval EFI_SUCCESS The request is executed successfully.\r
1925 @retval EFI_OUT_OF_RESOURCES The request could not be executed due to a lack of resources.\r
1926 @retval Others The request could not be executed successfully.\r
1927\r
1928**/\r
1929EFI_STATUS\r
1930EmmcWriteZeros (\r
1931 IN EMMC_PARTITION *Partition,\r
1932 IN EFI_LBA StartLba,\r
1933 IN UINTN Size\r
1934 )\r
1935{\r
1936 EFI_STATUS Status;\r
1937 UINT8 *Buffer;\r
1938 UINT32 MediaId;\r
1939\r
1940 Buffer = AllocateZeroPool (Size);\r
1941 if (Buffer == NULL) {\r
1942 return EFI_OUT_OF_RESOURCES;\r
1943 }\r
1944\r
1945 MediaId = Partition->BlockMedia.MediaId;\r
1946\r
1947 Status = EmmcReadWrite (Partition, MediaId, StartLba, Buffer, Size, FALSE, NULL);\r
1948 FreePool (Buffer);\r
1949\r
1950 return Status;\r
1951}\r
1952\r
275d5136
FT
1953/**\r
1954 Erase a specified number of device blocks.\r
1955\r
1956 @param[in] This Indicates a pointer to the calling context.\r
1957 @param[in] MediaId The media ID that the erase request is for.\r
1958 @param[in] Lba The starting logical block address to be\r
1959 erased. The caller is responsible for erasing\r
1960 only legitimate locations.\r
1961 @param[in, out] Token A pointer to the token associated with the\r
1962 transaction.\r
1963 @param[in] Size The size in bytes to be erased. This must be\r
1964 a multiple of the physical block size of the\r
1965 device.\r
1966\r
1967 @retval EFI_SUCCESS The erase request was queued if Event is not\r
1968 NULL. The data was erased correctly to the\r
1969 device if the Event is NULL.to the device.\r
1970 @retval EFI_WRITE_PROTECTED The device cannot be erased due to write\r
1971 protection.\r
1972 @retval EFI_DEVICE_ERROR The device reported an error while attempting\r
1973 to perform the erase operation.\r
1974 @retval EFI_INVALID_PARAMETER The erase request contains LBAs that are not\r
1975 valid.\r
1976 @retval EFI_NO_MEDIA There is no media in the device.\r
1977 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
1978\r
1979**/\r
1980EFI_STATUS\r
1981EFIAPI\r
1982EmmcEraseBlocks (\r
1983 IN EFI_ERASE_BLOCK_PROTOCOL *This,\r
1984 IN UINT32 MediaId,\r
1985 IN EFI_LBA Lba,\r
1986 IN OUT EFI_ERASE_BLOCK_TOKEN *Token,\r
1987 IN UINTN Size\r
1988 )\r
1989{\r
1990 EFI_STATUS Status;\r
1991 EFI_BLOCK_IO_MEDIA *Media;\r
1992 UINTN BlockSize;\r
1993 UINTN BlockNum;\r
b92efc9f 1994 EFI_LBA FirstLba;\r
275d5136 1995 EFI_LBA LastLba;\r
b92efc9f
HW
1996 EFI_LBA StartGroupLba;\r
1997 EFI_LBA EndGroupLba;\r
1998 UINT32 EraseGroupSize;\r
1999 UINT32 Remainder;\r
2000 UINTN WriteZeroSize;\r
275d5136
FT
2001 UINT8 PartitionConfig;\r
2002 EMMC_PARTITION *Partition;\r
2003 EMMC_DEVICE *Device;\r
2004\r
2005 Status = EFI_SUCCESS;\r
2006 Partition = EMMC_PARTITION_DATA_FROM_ERASEBLK (This);\r
2007 Device = Partition->Device;\r
2008 Media = &Partition->BlockMedia;\r
2009\r
2010 if (MediaId != Media->MediaId) {\r
2011 return EFI_MEDIA_CHANGED;\r
2012 }\r
2013\r
2014 if (Media->ReadOnly) {\r
2015 return EFI_WRITE_PROTECTED;\r
2016 }\r
2017\r
2018 //\r
2019 // Check parameters.\r
2020 //\r
2021 BlockSize = Media->BlockSize;\r
2022 if ((Size % BlockSize) != 0) {\r
2023 return EFI_INVALID_PARAMETER;\r
2024 }\r
2025\r
2026 BlockNum = Size / BlockSize;\r
2027 if ((Lba + BlockNum - 1) > Media->LastBlock) {\r
2028 return EFI_INVALID_PARAMETER;\r
2029 }\r
2030\r
2031 if ((Token != NULL) && (Token->Event != NULL)) {\r
2032 Token->TransactionStatus = EFI_SUCCESS;\r
2033 }\r
2034\r
b92efc9f
HW
2035 FirstLba = Lba;\r
2036 LastLba = Lba + BlockNum - 1;\r
275d5136
FT
2037\r
2038 //\r
2039 // Check if needs to switch partition access.\r
2040 //\r
2041 PartitionConfig = Device->ExtCsd.PartitionConfig;\r
2042 if ((PartitionConfig & 0x7) != Partition->PartitionType) {\r
2043 PartitionConfig &= (UINT8)~0x7;\r
2044 PartitionConfig |= Partition->PartitionType;\r
2045 Status = EmmcSetExtCsd (Partition, OFFSET_OF (EMMC_EXT_CSD, PartitionConfig), PartitionConfig, (EFI_BLOCK_IO2_TOKEN*)Token, FALSE);\r
2046 if (EFI_ERROR (Status)) {\r
2047 return Status;\r
2048 }\r
2049 Device->ExtCsd.PartitionConfig = PartitionConfig;\r
2050 }\r
2051\r
b92efc9f
HW
2052 if ((Device->ExtCsd.SecFeatureSupport & BIT4) == 0) {\r
2053 //\r
2054 // If the Trim operation is not supported by the device, handle the erase\r
2055 // of blocks that do not form a complete erase group separately.\r
2056 //\r
2057 EraseGroupSize = This->EraseLengthGranularity;\r
2058\r
2059 DivU64x32Remainder (FirstLba, EraseGroupSize, &Remainder);\r
2060 StartGroupLba = (Remainder == 0) ? FirstLba : (FirstLba + EraseGroupSize - Remainder);\r
2061\r
2062 DivU64x32Remainder (LastLba + 1, EraseGroupSize, &Remainder);\r
2063 EndGroupLba = LastLba + 1 - Remainder;\r
2064\r
2065 //\r
2066 // If the size to erase is smaller than the erase group size, the whole\r
2067 // erase operation is performed by writting zeros.\r
2068 //\r
2069 if (BlockNum < EraseGroupSize) {\r
2070 Status = EmmcWriteZeros (Partition, FirstLba, Size);\r
2071 if (EFI_ERROR (Status)) {\r
2072 return Status;\r
2073 }\r
2074\r
2075 DEBUG ((\r
2076 DEBUG_INFO,\r
2077 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",\r
2078 Lba,\r
2079 BlockNum,\r
2080 (Token != NULL) ? Token->Event : NULL,\r
2081 Status\r
2082 ));\r
2083\r
2084 if ((Token != NULL) && (Token->Event != NULL)) {\r
2085 Token->TransactionStatus = EFI_SUCCESS;\r
2086 gBS->SignalEvent (Token->Event);\r
2087 }\r
2088 return EFI_SUCCESS;\r
2089 }\r
2090\r
2091 //\r
2092 // If the starting LBA to erase is not aligned with the start of an erase\r
2093 // group, write zeros to erase the data from starting LBA to the end of the\r
2094 // current erase group.\r
2095 //\r
2096 if (StartGroupLba > FirstLba) {\r
2097 WriteZeroSize = (UINTN)(StartGroupLba - FirstLba) * BlockSize;\r
2098 Status = EmmcWriteZeros (Partition, FirstLba, WriteZeroSize);\r
2099 if (EFI_ERROR (Status)) {\r
2100 return Status;\r
2101 }\r
2102 }\r
2103\r
2104 //\r
2105 // If the ending LBA to erase is not aligned with the end of an erase\r
2106 // group, write zeros to erase the data from the start of the erase group\r
2107 // to the ending LBA.\r
2108 //\r
2109 if (EndGroupLba <= LastLba) {\r
2110 WriteZeroSize = (UINTN)(LastLba + 1 - EndGroupLba) * BlockSize;\r
2111 Status = EmmcWriteZeros (Partition, EndGroupLba, WriteZeroSize);\r
2112 if (EFI_ERROR (Status)) {\r
2113 return Status;\r
2114 }\r
2115 }\r
2116\r
2117 //\r
2118 // Check whether there is erase group to erase.\r
2119 //\r
2120 if (EndGroupLba <= StartGroupLba) {\r
2121 DEBUG ((\r
2122 DEBUG_INFO,\r
2123 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",\r
2124 Lba,\r
2125 BlockNum,\r
2126 (Token != NULL) ? Token->Event : NULL,\r
2127 Status\r
2128 ));\r
2129\r
2130 if ((Token != NULL) && (Token->Event != NULL)) {\r
2131 Token->TransactionStatus = EFI_SUCCESS;\r
2132 gBS->SignalEvent (Token->Event);\r
2133 }\r
2134 return EFI_SUCCESS;\r
2135 }\r
2136\r
2137 FirstLba = StartGroupLba;\r
2138 LastLba = EndGroupLba - 1;\r
2139 }\r
2140\r
2141 Status = EmmcEraseBlockStart (Partition, FirstLba, (EFI_BLOCK_IO2_TOKEN*)Token, FALSE);\r
275d5136
FT
2142 if (EFI_ERROR (Status)) {\r
2143 return Status;\r
2144 }\r
2145\r
2146 Status = EmmcEraseBlockEnd (Partition, LastLba, (EFI_BLOCK_IO2_TOKEN*)Token, FALSE);\r
2147 if (EFI_ERROR (Status)) {\r
2148 return Status;\r
2149 }\r
2150\r
2151 Status = EmmcEraseBlock (Partition, (EFI_BLOCK_IO2_TOKEN*)Token, TRUE);\r
2152 if (EFI_ERROR (Status)) {\r
2153 return Status;\r
2154 }\r
2155\r
b92efc9f
HW
2156 DEBUG ((\r
2157 DEBUG_INFO,\r
2158 "EmmcEraseBlocks(): Lba 0x%x BlkNo 0x%x Event %p with %r\n",\r
2159 Lba,\r
2160 BlockNum,\r
2161 (Token != NULL) ? Token->Event : NULL,\r
2162 Status\r
2163 ));\r
275d5136
FT
2164\r
2165 return Status;\r
2166}\r
2167\r