]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | DiskIo driver that lays on every BlockIo protocol in the system.\r | |
3 | DiskIo converts a block oriented device to a byte oriented device.\r | |
4 | \r | |
5 | Disk access may have to handle unaligned request about sector boundaries.\r | |
6 | There are three cases:\r | |
7 | UnderRun - The first byte is not on a sector boundary or the read request is\r | |
8 | less than a sector in length.\r | |
9 | Aligned - A read of N contiguous sectors.\r | |
10 | OverRun - The last byte is not on a sector boundary.\r | |
11 | \r | |
12 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
13 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "DiskIo.h"\r | |
18 | \r | |
19 | //\r | |
20 | // Driver binding protocol implementation for DiskIo driver.\r | |
21 | //\r | |
22 | EFI_DRIVER_BINDING_PROTOCOL gDiskIoDriverBinding = {\r | |
23 | DiskIoDriverBindingSupported,\r | |
24 | DiskIoDriverBindingStart,\r | |
25 | DiskIoDriverBindingStop,\r | |
26 | 0xa,\r | |
27 | NULL,\r | |
28 | NULL\r | |
29 | };\r | |
30 | \r | |
31 | //\r | |
32 | // Template for DiskIo private data structure.\r | |
33 | // The pointer to BlockIo protocol interface is assigned dynamically.\r | |
34 | //\r | |
35 | DISK_IO_PRIVATE_DATA gDiskIoPrivateDataTemplate = {\r | |
36 | DISK_IO_PRIVATE_DATA_SIGNATURE,\r | |
37 | {\r | |
38 | EFI_DISK_IO_PROTOCOL_REVISION,\r | |
39 | DiskIoReadDisk,\r | |
40 | DiskIoWriteDisk\r | |
41 | },\r | |
42 | {\r | |
43 | EFI_DISK_IO2_PROTOCOL_REVISION,\r | |
44 | DiskIo2Cancel,\r | |
45 | DiskIo2ReadDiskEx,\r | |
46 | DiskIo2WriteDiskEx,\r | |
47 | DiskIo2FlushDiskEx\r | |
48 | }\r | |
49 | };\r | |
50 | \r | |
51 | /**\r | |
52 | Test to see if this driver supports ControllerHandle.\r | |
53 | \r | |
54 | @param This Protocol instance pointer.\r | |
55 | @param ControllerHandle Handle of device to test\r | |
56 | @param RemainingDevicePath Optional parameter use to pick a specific child\r | |
57 | device to start.\r | |
58 | \r | |
59 | @retval EFI_SUCCESS This driver supports this device\r | |
60 | @retval EFI_ALREADY_STARTED This driver is already running on this device\r | |
61 | @retval other This driver does not support this device\r | |
62 | \r | |
63 | **/\r | |
64 | EFI_STATUS\r | |
65 | EFIAPI\r | |
66 | DiskIoDriverBindingSupported (\r | |
67 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
68 | IN EFI_HANDLE ControllerHandle,\r | |
69 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r | |
70 | )\r | |
71 | {\r | |
72 | EFI_STATUS Status;\r | |
73 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
74 | \r | |
75 | //\r | |
76 | // Open the IO Abstraction(s) needed to perform the supported test.\r | |
77 | //\r | |
78 | Status = gBS->OpenProtocol (\r | |
79 | ControllerHandle,\r | |
80 | &gEfiBlockIoProtocolGuid,\r | |
81 | (VOID **)&BlockIo,\r | |
82 | This->DriverBindingHandle,\r | |
83 | ControllerHandle,\r | |
84 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
85 | );\r | |
86 | if (EFI_ERROR (Status)) {\r | |
87 | return Status;\r | |
88 | }\r | |
89 | \r | |
90 | //\r | |
91 | // Close the I/O Abstraction(s) used to perform the supported test.\r | |
92 | //\r | |
93 | gBS->CloseProtocol (\r | |
94 | ControllerHandle,\r | |
95 | &gEfiBlockIoProtocolGuid,\r | |
96 | This->DriverBindingHandle,\r | |
97 | ControllerHandle\r | |
98 | );\r | |
99 | return EFI_SUCCESS;\r | |
100 | }\r | |
101 | \r | |
102 | /**\r | |
103 | Start this driver on ControllerHandle by opening a Block IO protocol and\r | |
104 | installing a Disk IO protocol on ControllerHandle.\r | |
105 | \r | |
106 | @param This Protocol instance pointer.\r | |
107 | @param ControllerHandle Handle of device to bind driver to\r | |
108 | @param RemainingDevicePath Optional parameter use to pick a specific child\r | |
109 | device to start.\r | |
110 | \r | |
111 | @retval EFI_SUCCESS This driver is added to ControllerHandle\r | |
112 | @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r | |
113 | @retval other This driver does not support this device\r | |
114 | \r | |
115 | **/\r | |
116 | EFI_STATUS\r | |
117 | EFIAPI\r | |
118 | DiskIoDriverBindingStart (\r | |
119 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
120 | IN EFI_HANDLE ControllerHandle,\r | |
121 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r | |
122 | )\r | |
123 | {\r | |
124 | EFI_STATUS Status;\r | |
125 | DISK_IO_PRIVATE_DATA *Instance;\r | |
126 | EFI_TPL OldTpl;\r | |
127 | \r | |
128 | Instance = NULL;\r | |
129 | \r | |
130 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r | |
131 | \r | |
132 | //\r | |
133 | // Connect to the Block IO and Block IO2 interface on ControllerHandle.\r | |
134 | //\r | |
135 | Status = gBS->OpenProtocol (\r | |
136 | ControllerHandle,\r | |
137 | &gEfiBlockIoProtocolGuid,\r | |
138 | (VOID **)&gDiskIoPrivateDataTemplate.BlockIo,\r | |
139 | This->DriverBindingHandle,\r | |
140 | ControllerHandle,\r | |
141 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
142 | );\r | |
143 | if (EFI_ERROR (Status)) {\r | |
144 | goto ErrorExit1;\r | |
145 | }\r | |
146 | \r | |
147 | Status = gBS->OpenProtocol (\r | |
148 | ControllerHandle,\r | |
149 | &gEfiBlockIo2ProtocolGuid,\r | |
150 | (VOID **)&gDiskIoPrivateDataTemplate.BlockIo2,\r | |
151 | This->DriverBindingHandle,\r | |
152 | ControllerHandle,\r | |
153 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
154 | );\r | |
155 | if (EFI_ERROR (Status)) {\r | |
156 | gDiskIoPrivateDataTemplate.BlockIo2 = NULL;\r | |
157 | }\r | |
158 | \r | |
159 | //\r | |
160 | // Initialize the Disk IO device instance.\r | |
161 | //\r | |
162 | Instance = AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA), &gDiskIoPrivateDataTemplate);\r | |
163 | if (Instance == NULL) {\r | |
164 | Status = EFI_OUT_OF_RESOURCES;\r | |
165 | goto ErrorExit;\r | |
166 | }\r | |
167 | \r | |
168 | //\r | |
169 | // The BlockSize and IoAlign of BlockIo and BlockIo2 should equal.\r | |
170 | //\r | |
171 | ASSERT (\r | |
172 | (Instance->BlockIo2 == NULL) ||\r | |
173 | ((Instance->BlockIo->Media->IoAlign == Instance->BlockIo2->Media->IoAlign) &&\r | |
174 | (Instance->BlockIo->Media->BlockSize == Instance->BlockIo2->Media->BlockSize)\r | |
175 | )\r | |
176 | );\r | |
177 | \r | |
178 | InitializeListHead (&Instance->TaskQueue);\r | |
179 | EfiInitializeLock (&Instance->TaskQueueLock, TPL_NOTIFY);\r | |
180 | Instance->SharedWorkingBuffer = AllocateAlignedPages (\r | |
181 | EFI_SIZE_TO_PAGES (PcdGet32 (PcdDiskIoDataBufferBlockNum) * Instance->BlockIo->Media->BlockSize),\r | |
182 | Instance->BlockIo->Media->IoAlign\r | |
183 | );\r | |
184 | if (Instance->SharedWorkingBuffer == NULL) {\r | |
185 | Status = EFI_OUT_OF_RESOURCES;\r | |
186 | goto ErrorExit;\r | |
187 | }\r | |
188 | \r | |
189 | //\r | |
190 | // Install protocol interfaces for the Disk IO device.\r | |
191 | //\r | |
192 | if (Instance->BlockIo2 != NULL) {\r | |
193 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
194 | &ControllerHandle,\r | |
195 | &gEfiDiskIoProtocolGuid,\r | |
196 | &Instance->DiskIo,\r | |
197 | &gEfiDiskIo2ProtocolGuid,\r | |
198 | &Instance->DiskIo2,\r | |
199 | NULL\r | |
200 | );\r | |
201 | } else {\r | |
202 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
203 | &ControllerHandle,\r | |
204 | &gEfiDiskIoProtocolGuid,\r | |
205 | &Instance->DiskIo,\r | |
206 | NULL\r | |
207 | );\r | |
208 | }\r | |
209 | \r | |
210 | ErrorExit:\r | |
211 | if (EFI_ERROR (Status)) {\r | |
212 | if ((Instance != NULL) && (Instance->SharedWorkingBuffer != NULL)) {\r | |
213 | FreeAlignedPages (\r | |
214 | Instance->SharedWorkingBuffer,\r | |
215 | EFI_SIZE_TO_PAGES (PcdGet32 (PcdDiskIoDataBufferBlockNum) * Instance->BlockIo->Media->BlockSize)\r | |
216 | );\r | |
217 | }\r | |
218 | \r | |
219 | if (Instance != NULL) {\r | |
220 | FreePool (Instance);\r | |
221 | }\r | |
222 | \r | |
223 | gBS->CloseProtocol (\r | |
224 | ControllerHandle,\r | |
225 | &gEfiBlockIoProtocolGuid,\r | |
226 | This->DriverBindingHandle,\r | |
227 | ControllerHandle\r | |
228 | );\r | |
229 | }\r | |
230 | \r | |
231 | ErrorExit1:\r | |
232 | gBS->RestoreTPL (OldTpl);\r | |
233 | return Status;\r | |
234 | }\r | |
235 | \r | |
236 | /**\r | |
237 | Stop this driver on ControllerHandle by removing Disk IO protocol and closing\r | |
238 | the Block IO protocol on ControllerHandle.\r | |
239 | \r | |
240 | @param This Protocol instance pointer.\r | |
241 | @param ControllerHandle Handle of device to stop driver on\r | |
242 | @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r | |
243 | children is zero stop the entire bus driver.\r | |
244 | @param ChildHandleBuffer List of Child Handles to Stop.\r | |
245 | \r | |
246 | @retval EFI_SUCCESS This driver is removed ControllerHandle\r | |
247 | @retval other This driver was not removed from this device\r | |
248 | \r | |
249 | **/\r | |
250 | EFI_STATUS\r | |
251 | EFIAPI\r | |
252 | DiskIoDriverBindingStop (\r | |
253 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
254 | IN EFI_HANDLE ControllerHandle,\r | |
255 | IN UINTN NumberOfChildren,\r | |
256 | IN EFI_HANDLE *ChildHandleBuffer\r | |
257 | )\r | |
258 | {\r | |
259 | EFI_STATUS Status;\r | |
260 | EFI_DISK_IO_PROTOCOL *DiskIo;\r | |
261 | EFI_DISK_IO2_PROTOCOL *DiskIo2;\r | |
262 | DISK_IO_PRIVATE_DATA *Instance;\r | |
263 | BOOLEAN AllTaskDone;\r | |
264 | \r | |
265 | //\r | |
266 | // Get our context back.\r | |
267 | //\r | |
268 | Status = gBS->OpenProtocol (\r | |
269 | ControllerHandle,\r | |
270 | &gEfiDiskIoProtocolGuid,\r | |
271 | (VOID **)&DiskIo,\r | |
272 | This->DriverBindingHandle,\r | |
273 | ControllerHandle,\r | |
274 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
275 | );\r | |
276 | if (EFI_ERROR (Status)) {\r | |
277 | return Status;\r | |
278 | }\r | |
279 | \r | |
280 | Status = gBS->OpenProtocol (\r | |
281 | ControllerHandle,\r | |
282 | &gEfiDiskIo2ProtocolGuid,\r | |
283 | (VOID **)&DiskIo2,\r | |
284 | This->DriverBindingHandle,\r | |
285 | ControllerHandle,\r | |
286 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
287 | );\r | |
288 | if (EFI_ERROR (Status)) {\r | |
289 | DiskIo2 = NULL;\r | |
290 | }\r | |
291 | \r | |
292 | Instance = DISK_IO_PRIVATE_DATA_FROM_DISK_IO (DiskIo);\r | |
293 | \r | |
294 | if (DiskIo2 != NULL) {\r | |
295 | //\r | |
296 | // Call BlockIo2::Reset() to terminate any in-flight non-blocking I/O requests\r | |
297 | //\r | |
298 | ASSERT (Instance->BlockIo2 != NULL);\r | |
299 | Status = Instance->BlockIo2->Reset (Instance->BlockIo2, FALSE);\r | |
300 | if (EFI_ERROR (Status)) {\r | |
301 | return Status;\r | |
302 | }\r | |
303 | \r | |
304 | Status = gBS->UninstallMultipleProtocolInterfaces (\r | |
305 | ControllerHandle,\r | |
306 | &gEfiDiskIoProtocolGuid,\r | |
307 | &Instance->DiskIo,\r | |
308 | &gEfiDiskIo2ProtocolGuid,\r | |
309 | &Instance->DiskIo2,\r | |
310 | NULL\r | |
311 | );\r | |
312 | } else {\r | |
313 | Status = gBS->UninstallMultipleProtocolInterfaces (\r | |
314 | ControllerHandle,\r | |
315 | &gEfiDiskIoProtocolGuid,\r | |
316 | &Instance->DiskIo,\r | |
317 | NULL\r | |
318 | );\r | |
319 | }\r | |
320 | \r | |
321 | if (!EFI_ERROR (Status)) {\r | |
322 | do {\r | |
323 | EfiAcquireLock (&Instance->TaskQueueLock);\r | |
324 | AllTaskDone = IsListEmpty (&Instance->TaskQueue);\r | |
325 | EfiReleaseLock (&Instance->TaskQueueLock);\r | |
326 | } while (!AllTaskDone);\r | |
327 | \r | |
328 | FreeAlignedPages (\r | |
329 | Instance->SharedWorkingBuffer,\r | |
330 | EFI_SIZE_TO_PAGES (PcdGet32 (PcdDiskIoDataBufferBlockNum) * Instance->BlockIo->Media->BlockSize)\r | |
331 | );\r | |
332 | \r | |
333 | Status = gBS->CloseProtocol (\r | |
334 | ControllerHandle,\r | |
335 | &gEfiBlockIoProtocolGuid,\r | |
336 | This->DriverBindingHandle,\r | |
337 | ControllerHandle\r | |
338 | );\r | |
339 | ASSERT_EFI_ERROR (Status);\r | |
340 | if (DiskIo2 != NULL) {\r | |
341 | Status = gBS->CloseProtocol (\r | |
342 | ControllerHandle,\r | |
343 | &gEfiBlockIo2ProtocolGuid,\r | |
344 | This->DriverBindingHandle,\r | |
345 | ControllerHandle\r | |
346 | );\r | |
347 | ASSERT_EFI_ERROR (Status);\r | |
348 | }\r | |
349 | \r | |
350 | FreePool (Instance);\r | |
351 | }\r | |
352 | \r | |
353 | return Status;\r | |
354 | }\r | |
355 | \r | |
356 | /**\r | |
357 | Destroy the sub task.\r | |
358 | \r | |
359 | @param Instance Pointer to the DISK_IO_PRIVATE_DATA.\r | |
360 | @param Subtask Subtask.\r | |
361 | \r | |
362 | @return LIST_ENTRY * Pointer to the next link of subtask.\r | |
363 | **/\r | |
364 | LIST_ENTRY *\r | |
365 | DiskIoDestroySubtask (\r | |
366 | IN DISK_IO_PRIVATE_DATA *Instance,\r | |
367 | IN DISK_IO_SUBTASK *Subtask\r | |
368 | )\r | |
369 | {\r | |
370 | LIST_ENTRY *Link;\r | |
371 | \r | |
372 | if (Subtask->Task != NULL) {\r | |
373 | EfiAcquireLock (&Subtask->Task->SubtasksLock);\r | |
374 | }\r | |
375 | \r | |
376 | Link = RemoveEntryList (&Subtask->Link);\r | |
377 | if (Subtask->Task != NULL) {\r | |
378 | EfiReleaseLock (&Subtask->Task->SubtasksLock);\r | |
379 | }\r | |
380 | \r | |
381 | if (!Subtask->Blocking) {\r | |
382 | if (Subtask->WorkingBuffer != NULL) {\r | |
383 | FreeAlignedPages (\r | |
384 | Subtask->WorkingBuffer,\r | |
385 | Subtask->Length < Instance->BlockIo->Media->BlockSize\r | |
386 | ? EFI_SIZE_TO_PAGES (Instance->BlockIo->Media->BlockSize)\r | |
387 | : EFI_SIZE_TO_PAGES (Subtask->Length)\r | |
388 | );\r | |
389 | }\r | |
390 | \r | |
391 | if (Subtask->BlockIo2Token.Event != NULL) {\r | |
392 | gBS->CloseEvent (Subtask->BlockIo2Token.Event);\r | |
393 | }\r | |
394 | }\r | |
395 | \r | |
396 | FreePool (Subtask);\r | |
397 | \r | |
398 | return Link;\r | |
399 | }\r | |
400 | \r | |
401 | /**\r | |
402 | The callback for the BlockIo2 ReadBlocksEx/WriteBlocksEx.\r | |
403 | @param Event Event whose notification function is being invoked.\r | |
404 | @param Context The pointer to the notification function's context,\r | |
405 | which points to the DISK_IO_SUBTASK instance.\r | |
406 | **/\r | |
407 | VOID\r | |
408 | EFIAPI\r | |
409 | DiskIo2OnReadWriteComplete (\r | |
410 | IN EFI_EVENT Event,\r | |
411 | IN VOID *Context\r | |
412 | )\r | |
413 | {\r | |
414 | DISK_IO_SUBTASK *Subtask;\r | |
415 | DISK_IO2_TASK *Task;\r | |
416 | EFI_STATUS TransactionStatus;\r | |
417 | DISK_IO_PRIVATE_DATA *Instance;\r | |
418 | \r | |
419 | Subtask = (DISK_IO_SUBTASK *)Context;\r | |
420 | TransactionStatus = Subtask->BlockIo2Token.TransactionStatus;\r | |
421 | Task = Subtask->Task;\r | |
422 | Instance = Task->Instance;\r | |
423 | \r | |
424 | ASSERT (Subtask->Signature == DISK_IO_SUBTASK_SIGNATURE);\r | |
425 | ASSERT (Instance->Signature == DISK_IO_PRIVATE_DATA_SIGNATURE);\r | |
426 | ASSERT (Task->Signature == DISK_IO2_TASK_SIGNATURE);\r | |
427 | \r | |
428 | if ((Subtask->WorkingBuffer != NULL) && !EFI_ERROR (TransactionStatus) &&\r | |
429 | (Task->Token != NULL) && !Subtask->Write\r | |
430 | )\r | |
431 | {\r | |
432 | CopyMem (Subtask->Buffer, Subtask->WorkingBuffer + Subtask->Offset, Subtask->Length);\r | |
433 | }\r | |
434 | \r | |
435 | DiskIoDestroySubtask (Instance, Subtask);\r | |
436 | \r | |
437 | if (EFI_ERROR (TransactionStatus) || IsListEmpty (&Task->Subtasks)) {\r | |
438 | if (Task->Token != NULL) {\r | |
439 | //\r | |
440 | // Signal error status once the subtask is failed.\r | |
441 | // Or signal the last status once the last subtask is finished.\r | |
442 | //\r | |
443 | Task->Token->TransactionStatus = TransactionStatus;\r | |
444 | gBS->SignalEvent (Task->Token->Event);\r | |
445 | \r | |
446 | //\r | |
447 | // Mark token to NULL indicating the Task is a dead task.\r | |
448 | //\r | |
449 | Task->Token = NULL;\r | |
450 | }\r | |
451 | }\r | |
452 | }\r | |
453 | \r | |
454 | /**\r | |
455 | Create the subtask.\r | |
456 | \r | |
457 | @param Write TRUE: Write request; FALSE: Read request.\r | |
458 | @param Lba The starting logical block address to read from on the device.\r | |
459 | @param Offset The starting byte offset to read from the LBA.\r | |
460 | @param Length The number of bytes to read from the device.\r | |
461 | @param WorkingBuffer The aligned buffer to hold the data for reading or writing.\r | |
462 | @param Buffer The buffer to hold the data for reading or writing.\r | |
463 | @param Blocking TRUE: Blocking request; FALSE: Non-blocking request.\r | |
464 | \r | |
465 | @return A pointer to the created subtask.\r | |
466 | **/\r | |
467 | DISK_IO_SUBTASK *\r | |
468 | DiskIoCreateSubtask (\r | |
469 | IN BOOLEAN Write,\r | |
470 | IN UINT64 Lba,\r | |
471 | IN UINT32 Offset,\r | |
472 | IN UINTN Length,\r | |
473 | IN VOID *WorkingBuffer OPTIONAL,\r | |
474 | IN VOID *Buffer,\r | |
475 | IN BOOLEAN Blocking\r | |
476 | )\r | |
477 | {\r | |
478 | DISK_IO_SUBTASK *Subtask;\r | |
479 | EFI_STATUS Status;\r | |
480 | \r | |
481 | Subtask = AllocateZeroPool (sizeof (DISK_IO_SUBTASK));\r | |
482 | if (Subtask == NULL) {\r | |
483 | return NULL;\r | |
484 | }\r | |
485 | \r | |
486 | Subtask->Signature = DISK_IO_SUBTASK_SIGNATURE;\r | |
487 | Subtask->Write = Write;\r | |
488 | Subtask->Lba = Lba;\r | |
489 | Subtask->Offset = Offset;\r | |
490 | Subtask->Length = Length;\r | |
491 | Subtask->WorkingBuffer = WorkingBuffer;\r | |
492 | Subtask->Buffer = Buffer;\r | |
493 | Subtask->Blocking = Blocking;\r | |
494 | if (!Blocking) {\r | |
495 | Status = gBS->CreateEvent (\r | |
496 | EVT_NOTIFY_SIGNAL,\r | |
497 | TPL_NOTIFY,\r | |
498 | DiskIo2OnReadWriteComplete,\r | |
499 | Subtask,\r | |
500 | &Subtask->BlockIo2Token.Event\r | |
501 | );\r | |
502 | if (EFI_ERROR (Status)) {\r | |
503 | FreePool (Subtask);\r | |
504 | return NULL;\r | |
505 | }\r | |
506 | }\r | |
507 | \r | |
508 | DEBUG ((\r | |
509 | DEBUG_BLKIO,\r | |
510 | " %c:Lba/Offset/Length/WorkingBuffer/Buffer = %016lx/%08x/%08x/%08x/%08x\n",\r | |
511 | Write ? 'W' : 'R',\r | |
512 | Lba,\r | |
513 | Offset,\r | |
514 | Length,\r | |
515 | WorkingBuffer,\r | |
516 | Buffer\r | |
517 | ));\r | |
518 | \r | |
519 | return Subtask;\r | |
520 | }\r | |
521 | \r | |
522 | /**\r | |
523 | Create the subtask list.\r | |
524 | \r | |
525 | @param Instance Pointer to the DISK_IO_PRIVATE_DATA.\r | |
526 | @param Write TRUE: Write request; FALSE: Read request.\r | |
527 | @param Offset The starting byte offset to read from the device.\r | |
528 | @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.\r | |
529 | @param Buffer A pointer to the buffer for the data.\r | |
530 | @param Blocking TRUE: Blocking request; FALSE: Non-blocking request.\r | |
531 | @param SharedWorkingBuffer The aligned buffer to hold the data for reading or writing.\r | |
532 | @param Subtasks The subtask list header.\r | |
533 | \r | |
534 | @retval TRUE The subtask list is created successfully.\r | |
535 | @retval FALSE The subtask list is not created.\r | |
536 | **/\r | |
537 | BOOLEAN\r | |
538 | DiskIoCreateSubtaskList (\r | |
539 | IN DISK_IO_PRIVATE_DATA *Instance,\r | |
540 | IN BOOLEAN Write,\r | |
541 | IN UINT64 Offset,\r | |
542 | IN UINTN BufferSize,\r | |
543 | IN VOID *Buffer,\r | |
544 | IN BOOLEAN Blocking,\r | |
545 | IN VOID *SharedWorkingBuffer,\r | |
546 | IN OUT LIST_ENTRY *Subtasks\r | |
547 | )\r | |
548 | {\r | |
549 | UINT32 BlockSize;\r | |
550 | UINT32 IoAlign;\r | |
551 | UINT64 Lba;\r | |
552 | UINT64 OverRunLba;\r | |
553 | UINT32 UnderRun;\r | |
554 | UINT32 OverRun;\r | |
555 | UINT8 *BufferPtr;\r | |
556 | UINTN Length;\r | |
557 | UINTN DataBufferSize;\r | |
558 | DISK_IO_SUBTASK *Subtask;\r | |
559 | VOID *WorkingBuffer;\r | |
560 | LIST_ENTRY *Link;\r | |
561 | \r | |
562 | DEBUG ((DEBUG_BLKIO, "DiskIo: Create subtasks for task: Offset/BufferSize/Buffer = %016lx/%08x/%08x\n", Offset, BufferSize, Buffer));\r | |
563 | \r | |
564 | BlockSize = Instance->BlockIo->Media->BlockSize;\r | |
565 | IoAlign = Instance->BlockIo->Media->IoAlign;\r | |
566 | if (IoAlign == 0) {\r | |
567 | IoAlign = 1;\r | |
568 | }\r | |
569 | \r | |
570 | Lba = DivU64x32Remainder (Offset, BlockSize, &UnderRun);\r | |
571 | BufferPtr = (UINT8 *)Buffer;\r | |
572 | \r | |
573 | //\r | |
574 | // Special handling for zero BufferSize\r | |
575 | //\r | |
576 | if (BufferSize == 0) {\r | |
577 | Subtask = DiskIoCreateSubtask (Write, Lba, UnderRun, 0, NULL, BufferPtr, Blocking);\r | |
578 | if (Subtask == NULL) {\r | |
579 | goto Done;\r | |
580 | }\r | |
581 | \r | |
582 | InsertTailList (Subtasks, &Subtask->Link);\r | |
583 | return TRUE;\r | |
584 | }\r | |
585 | \r | |
586 | if (UnderRun != 0) {\r | |
587 | Length = MIN (BlockSize - UnderRun, BufferSize);\r | |
588 | if (Blocking) {\r | |
589 | WorkingBuffer = SharedWorkingBuffer;\r | |
590 | } else {\r | |
591 | WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BlockSize), IoAlign);\r | |
592 | if (WorkingBuffer == NULL) {\r | |
593 | goto Done;\r | |
594 | }\r | |
595 | }\r | |
596 | \r | |
597 | if (Write) {\r | |
598 | //\r | |
599 | // A half write operation can be splitted to a blocking block-read and half write operation\r | |
600 | // This can simplify the sub task processing logic\r | |
601 | //\r | |
602 | Subtask = DiskIoCreateSubtask (FALSE, Lba, 0, BlockSize, NULL, WorkingBuffer, TRUE);\r | |
603 | if (Subtask == NULL) {\r | |
604 | goto Done;\r | |
605 | }\r | |
606 | \r | |
607 | InsertTailList (Subtasks, &Subtask->Link);\r | |
608 | }\r | |
609 | \r | |
610 | Subtask = DiskIoCreateSubtask (Write, Lba, UnderRun, Length, WorkingBuffer, BufferPtr, Blocking);\r | |
611 | if (Subtask == NULL) {\r | |
612 | goto Done;\r | |
613 | }\r | |
614 | \r | |
615 | InsertTailList (Subtasks, &Subtask->Link);\r | |
616 | \r | |
617 | BufferPtr += Length;\r | |
618 | Offset += Length;\r | |
619 | BufferSize -= Length;\r | |
620 | Lba++;\r | |
621 | }\r | |
622 | \r | |
623 | OverRunLba = Lba + DivU64x32Remainder (BufferSize, BlockSize, &OverRun);\r | |
624 | BufferSize -= OverRun;\r | |
625 | \r | |
626 | if (OverRun != 0) {\r | |
627 | if (Blocking) {\r | |
628 | WorkingBuffer = SharedWorkingBuffer;\r | |
629 | } else {\r | |
630 | WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BlockSize), IoAlign);\r | |
631 | if (WorkingBuffer == NULL) {\r | |
632 | goto Done;\r | |
633 | }\r | |
634 | }\r | |
635 | \r | |
636 | if (Write) {\r | |
637 | //\r | |
638 | // A half write operation can be splitted to a blocking block-read and half write operation\r | |
639 | // This can simplify the sub task processing logic\r | |
640 | //\r | |
641 | Subtask = DiskIoCreateSubtask (FALSE, OverRunLba, 0, BlockSize, NULL, WorkingBuffer, TRUE);\r | |
642 | if (Subtask == NULL) {\r | |
643 | goto Done;\r | |
644 | }\r | |
645 | \r | |
646 | InsertTailList (Subtasks, &Subtask->Link);\r | |
647 | }\r | |
648 | \r | |
649 | Subtask = DiskIoCreateSubtask (Write, OverRunLba, 0, OverRun, WorkingBuffer, BufferPtr + BufferSize, Blocking);\r | |
650 | if (Subtask == NULL) {\r | |
651 | goto Done;\r | |
652 | }\r | |
653 | \r | |
654 | InsertTailList (Subtasks, &Subtask->Link);\r | |
655 | }\r | |
656 | \r | |
657 | if (OverRunLba > Lba) {\r | |
658 | //\r | |
659 | // If the DiskIo maps directly to a BlockIo device do the read.\r | |
660 | //\r | |
661 | if (ALIGN_POINTER (BufferPtr, IoAlign) == BufferPtr) {\r | |
662 | Subtask = DiskIoCreateSubtask (Write, Lba, 0, BufferSize, NULL, BufferPtr, Blocking);\r | |
663 | if (Subtask == NULL) {\r | |
664 | goto Done;\r | |
665 | }\r | |
666 | \r | |
667 | InsertTailList (Subtasks, &Subtask->Link);\r | |
668 | \r | |
669 | BufferPtr += BufferSize;\r | |
670 | Offset += BufferSize;\r | |
671 | BufferSize -= BufferSize;\r | |
672 | } else {\r | |
673 | if (Blocking) {\r | |
674 | //\r | |
675 | // Use the allocated buffer instead of the original buffer\r | |
676 | // to avoid alignment issue.\r | |
677 | //\r | |
678 | for ( ; Lba < OverRunLba; Lba += PcdGet32 (PcdDiskIoDataBufferBlockNum)) {\r | |
679 | DataBufferSize = MIN (BufferSize, PcdGet32 (PcdDiskIoDataBufferBlockNum) * BlockSize);\r | |
680 | \r | |
681 | Subtask = DiskIoCreateSubtask (Write, Lba, 0, DataBufferSize, SharedWorkingBuffer, BufferPtr, Blocking);\r | |
682 | if (Subtask == NULL) {\r | |
683 | goto Done;\r | |
684 | }\r | |
685 | \r | |
686 | InsertTailList (Subtasks, &Subtask->Link);\r | |
687 | \r | |
688 | BufferPtr += DataBufferSize;\r | |
689 | Offset += DataBufferSize;\r | |
690 | BufferSize -= DataBufferSize;\r | |
691 | }\r | |
692 | } else {\r | |
693 | WorkingBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), IoAlign);\r | |
694 | if (WorkingBuffer == NULL) {\r | |
695 | //\r | |
696 | // If there is not enough memory, downgrade to blocking access\r | |
697 | //\r | |
698 | DEBUG ((DEBUG_VERBOSE, "DiskIo: No enough memory so downgrade to blocking access\n"));\r | |
699 | if (!DiskIoCreateSubtaskList (Instance, Write, Offset, BufferSize, BufferPtr, TRUE, SharedWorkingBuffer, Subtasks)) {\r | |
700 | goto Done;\r | |
701 | }\r | |
702 | } else {\r | |
703 | Subtask = DiskIoCreateSubtask (Write, Lba, 0, BufferSize, WorkingBuffer, BufferPtr, Blocking);\r | |
704 | if (Subtask == NULL) {\r | |
705 | goto Done;\r | |
706 | }\r | |
707 | \r | |
708 | InsertTailList (Subtasks, &Subtask->Link);\r | |
709 | }\r | |
710 | \r | |
711 | BufferPtr += BufferSize;\r | |
712 | Offset += BufferSize;\r | |
713 | BufferSize -= BufferSize;\r | |
714 | }\r | |
715 | }\r | |
716 | }\r | |
717 | \r | |
718 | ASSERT (BufferSize == 0);\r | |
719 | \r | |
720 | return TRUE;\r | |
721 | \r | |
722 | Done:\r | |
723 | //\r | |
724 | // Remove all the subtasks.\r | |
725 | //\r | |
726 | for (Link = GetFirstNode (Subtasks); !IsNull (Subtasks, Link); ) {\r | |
727 | Subtask = CR (Link, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE);\r | |
728 | Link = DiskIoDestroySubtask (Instance, Subtask);\r | |
729 | }\r | |
730 | \r | |
731 | return FALSE;\r | |
732 | }\r | |
733 | \r | |
734 | /**\r | |
735 | Terminate outstanding asynchronous requests to a device.\r | |
736 | \r | |
737 | @param This Indicates a pointer to the calling context.\r | |
738 | \r | |
739 | @retval EFI_SUCCESS All outstanding requests were successfully terminated.\r | |
740 | @retval EFI_DEVICE_ERROR The device reported an error while performing the cancel\r | |
741 | operation.\r | |
742 | **/\r | |
743 | EFI_STATUS\r | |
744 | EFIAPI\r | |
745 | DiskIo2Cancel (\r | |
746 | IN EFI_DISK_IO2_PROTOCOL *This\r | |
747 | )\r | |
748 | {\r | |
749 | DISK_IO_PRIVATE_DATA *Instance;\r | |
750 | DISK_IO2_TASK *Task;\r | |
751 | LIST_ENTRY *Link;\r | |
752 | \r | |
753 | Instance = DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This);\r | |
754 | \r | |
755 | EfiAcquireLock (&Instance->TaskQueueLock);\r | |
756 | \r | |
757 | for (Link = GetFirstNode (&Instance->TaskQueue)\r | |
758 | ; !IsNull (&Instance->TaskQueue, Link)\r | |
759 | ; Link = GetNextNode (&Instance->TaskQueue, Link)\r | |
760 | )\r | |
761 | {\r | |
762 | Task = CR (Link, DISK_IO2_TASK, Link, DISK_IO2_TASK_SIGNATURE);\r | |
763 | \r | |
764 | if (Task->Token != NULL) {\r | |
765 | Task->Token->TransactionStatus = EFI_ABORTED;\r | |
766 | gBS->SignalEvent (Task->Token->Event);\r | |
767 | //\r | |
768 | // Set Token to NULL so that the further BlockIo2 responses will be ignored\r | |
769 | //\r | |
770 | Task->Token = NULL;\r | |
771 | }\r | |
772 | }\r | |
773 | \r | |
774 | EfiReleaseLock (&Instance->TaskQueueLock);\r | |
775 | \r | |
776 | return EFI_SUCCESS;\r | |
777 | }\r | |
778 | \r | |
779 | /**\r | |
780 | Remove the completed tasks from Instance->TaskQueue. Completed tasks are those who don't have any subtasks.\r | |
781 | \r | |
782 | @param Instance Pointer to the DISK_IO_PRIVATE_DATA.\r | |
783 | \r | |
784 | @retval TRUE The Instance->TaskQueue is empty after the completed tasks are removed.\r | |
785 | @retval FALSE The Instance->TaskQueue is not empty after the completed tasks are removed.\r | |
786 | **/\r | |
787 | BOOLEAN\r | |
788 | DiskIo2RemoveCompletedTask (\r | |
789 | IN DISK_IO_PRIVATE_DATA *Instance\r | |
790 | )\r | |
791 | {\r | |
792 | BOOLEAN QueueEmpty;\r | |
793 | LIST_ENTRY *Link;\r | |
794 | DISK_IO2_TASK *Task;\r | |
795 | \r | |
796 | QueueEmpty = TRUE;\r | |
797 | \r | |
798 | EfiAcquireLock (&Instance->TaskQueueLock);\r | |
799 | for (Link = GetFirstNode (&Instance->TaskQueue); !IsNull (&Instance->TaskQueue, Link); ) {\r | |
800 | Task = CR (Link, DISK_IO2_TASK, Link, DISK_IO2_TASK_SIGNATURE);\r | |
801 | if (IsListEmpty (&Task->Subtasks)) {\r | |
802 | Link = RemoveEntryList (&Task->Link);\r | |
803 | ASSERT (Task->Token == NULL);\r | |
804 | FreePool (Task);\r | |
805 | } else {\r | |
806 | Link = GetNextNode (&Instance->TaskQueue, Link);\r | |
807 | QueueEmpty = FALSE;\r | |
808 | }\r | |
809 | }\r | |
810 | \r | |
811 | EfiReleaseLock (&Instance->TaskQueueLock);\r | |
812 | \r | |
813 | return QueueEmpty;\r | |
814 | }\r | |
815 | \r | |
816 | /**\r | |
817 | Common routine to access the disk.\r | |
818 | \r | |
819 | @param Instance Pointer to the DISK_IO_PRIVATE_DATA.\r | |
820 | @param Write TRUE: Write operation; FALSE: Read operation.\r | |
821 | @param MediaId ID of the medium to access.\r | |
822 | @param Offset The starting byte offset on the logical block I/O device to access.\r | |
823 | @param Token A pointer to the token associated with the transaction.\r | |
824 | If this field is NULL, synchronous/blocking IO is performed.\r | |
825 | @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.\r | |
826 | @param Buffer A pointer to the destination buffer for the data.\r | |
827 | The caller is responsible either having implicit or explicit ownership of the buffer.\r | |
828 | **/\r | |
829 | EFI_STATUS\r | |
830 | DiskIo2ReadWriteDisk (\r | |
831 | IN DISK_IO_PRIVATE_DATA *Instance,\r | |
832 | IN BOOLEAN Write,\r | |
833 | IN UINT32 MediaId,\r | |
834 | IN UINT64 Offset,\r | |
835 | IN EFI_DISK_IO2_TOKEN *Token,\r | |
836 | IN UINTN BufferSize,\r | |
837 | IN UINT8 *Buffer\r | |
838 | )\r | |
839 | {\r | |
840 | EFI_STATUS Status;\r | |
841 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
842 | EFI_BLOCK_IO2_PROTOCOL *BlockIo2;\r | |
843 | EFI_BLOCK_IO_MEDIA *Media;\r | |
844 | LIST_ENTRY *Link;\r | |
845 | LIST_ENTRY *NextLink;\r | |
846 | LIST_ENTRY Subtasks;\r | |
847 | DISK_IO_SUBTASK *Subtask;\r | |
848 | DISK_IO2_TASK *Task;\r | |
849 | EFI_TPL OldTpl;\r | |
850 | BOOLEAN Blocking;\r | |
851 | BOOLEAN SubtaskBlocking;\r | |
852 | LIST_ENTRY *SubtasksPtr;\r | |
853 | \r | |
854 | Task = NULL;\r | |
855 | BlockIo = Instance->BlockIo;\r | |
856 | BlockIo2 = Instance->BlockIo2;\r | |
857 | Media = BlockIo->Media;\r | |
858 | Status = EFI_SUCCESS;\r | |
859 | Blocking = (BOOLEAN)((Token == NULL) || (Token->Event == NULL));\r | |
860 | \r | |
861 | if (Blocking) {\r | |
862 | //\r | |
863 | // Wait till pending async task is completed.\r | |
864 | //\r | |
865 | while (!DiskIo2RemoveCompletedTask (Instance)) {\r | |
866 | }\r | |
867 | \r | |
868 | SubtasksPtr = &Subtasks;\r | |
869 | } else {\r | |
870 | DiskIo2RemoveCompletedTask (Instance);\r | |
871 | Task = AllocatePool (sizeof (DISK_IO2_TASK));\r | |
872 | if (Task == NULL) {\r | |
873 | return EFI_OUT_OF_RESOURCES;\r | |
874 | }\r | |
875 | \r | |
876 | EfiAcquireLock (&Instance->TaskQueueLock);\r | |
877 | InsertTailList (&Instance->TaskQueue, &Task->Link);\r | |
878 | EfiReleaseLock (&Instance->TaskQueueLock);\r | |
879 | \r | |
880 | Task->Signature = DISK_IO2_TASK_SIGNATURE;\r | |
881 | Task->Instance = Instance;\r | |
882 | Task->Token = Token;\r | |
883 | EfiInitializeLock (&Task->SubtasksLock, TPL_NOTIFY);\r | |
884 | \r | |
885 | SubtasksPtr = &Task->Subtasks;\r | |
886 | }\r | |
887 | \r | |
888 | InitializeListHead (SubtasksPtr);\r | |
889 | if (!DiskIoCreateSubtaskList (Instance, Write, Offset, BufferSize, Buffer, Blocking, Instance->SharedWorkingBuffer, SubtasksPtr)) {\r | |
890 | if (Task != NULL) {\r | |
891 | FreePool (Task);\r | |
892 | }\r | |
893 | \r | |
894 | return EFI_OUT_OF_RESOURCES;\r | |
895 | }\r | |
896 | \r | |
897 | ASSERT (!IsListEmpty (SubtasksPtr));\r | |
898 | \r | |
899 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r | |
900 | for ( Link = GetFirstNode (SubtasksPtr), NextLink = GetNextNode (SubtasksPtr, Link)\r | |
901 | ; !IsNull (SubtasksPtr, Link)\r | |
902 | ; Link = NextLink, NextLink = GetNextNode (SubtasksPtr, NextLink)\r | |
903 | )\r | |
904 | {\r | |
905 | Subtask = CR (Link, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE);\r | |
906 | Subtask->Task = Task;\r | |
907 | SubtaskBlocking = Subtask->Blocking;\r | |
908 | \r | |
909 | ASSERT ((Subtask->Length % Media->BlockSize == 0) || (Subtask->Length < Media->BlockSize));\r | |
910 | \r | |
911 | if (Subtask->Write) {\r | |
912 | //\r | |
913 | // Write\r | |
914 | //\r | |
915 | if (Subtask->WorkingBuffer != NULL) {\r | |
916 | //\r | |
917 | // A sub task before this one should be a block read operation, causing the WorkingBuffer filled with the entire one block data.\r | |
918 | //\r | |
919 | CopyMem (Subtask->WorkingBuffer + Subtask->Offset, Subtask->Buffer, Subtask->Length);\r | |
920 | }\r | |
921 | \r | |
922 | if (SubtaskBlocking) {\r | |
923 | Status = BlockIo->WriteBlocks (\r | |
924 | BlockIo,\r | |
925 | MediaId,\r | |
926 | Subtask->Lba,\r | |
927 | (Subtask->Length % Media->BlockSize == 0) ? Subtask->Length : Media->BlockSize,\r | |
928 | (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer\r | |
929 | );\r | |
930 | } else {\r | |
931 | Status = BlockIo2->WriteBlocksEx (\r | |
932 | BlockIo2,\r | |
933 | MediaId,\r | |
934 | Subtask->Lba,\r | |
935 | &Subtask->BlockIo2Token,\r | |
936 | (Subtask->Length % Media->BlockSize == 0) ? Subtask->Length : Media->BlockSize,\r | |
937 | (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer\r | |
938 | );\r | |
939 | }\r | |
940 | } else {\r | |
941 | //\r | |
942 | // Read\r | |
943 | //\r | |
944 | if (SubtaskBlocking) {\r | |
945 | Status = BlockIo->ReadBlocks (\r | |
946 | BlockIo,\r | |
947 | MediaId,\r | |
948 | Subtask->Lba,\r | |
949 | (Subtask->Length % Media->BlockSize == 0) ? Subtask->Length : Media->BlockSize,\r | |
950 | (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer\r | |
951 | );\r | |
952 | if (!EFI_ERROR (Status) && (Subtask->WorkingBuffer != NULL)) {\r | |
953 | CopyMem (Subtask->Buffer, Subtask->WorkingBuffer + Subtask->Offset, Subtask->Length);\r | |
954 | }\r | |
955 | } else {\r | |
956 | Status = BlockIo2->ReadBlocksEx (\r | |
957 | BlockIo2,\r | |
958 | MediaId,\r | |
959 | Subtask->Lba,\r | |
960 | &Subtask->BlockIo2Token,\r | |
961 | (Subtask->Length % Media->BlockSize == 0) ? Subtask->Length : Media->BlockSize,\r | |
962 | (Subtask->WorkingBuffer != NULL) ? Subtask->WorkingBuffer : Subtask->Buffer\r | |
963 | );\r | |
964 | }\r | |
965 | }\r | |
966 | \r | |
967 | if (SubtaskBlocking || EFI_ERROR (Status)) {\r | |
968 | //\r | |
969 | // Make sure the subtask list only contains non-blocking subtasks.\r | |
970 | // Remove failed non-blocking subtasks as well because the callback won't be called.\r | |
971 | //\r | |
972 | DiskIoDestroySubtask (Instance, Subtask);\r | |
973 | }\r | |
974 | \r | |
975 | if (EFI_ERROR (Status)) {\r | |
976 | break;\r | |
977 | }\r | |
978 | }\r | |
979 | \r | |
980 | gBS->RaiseTPL (TPL_NOTIFY);\r | |
981 | \r | |
982 | //\r | |
983 | // Remove all the remaining subtasks when failure.\r | |
984 | // We shouldn't remove all the tasks because the non-blocking requests have been submitted and cannot be canceled.\r | |
985 | //\r | |
986 | if (EFI_ERROR (Status)) {\r | |
987 | while (!IsNull (SubtasksPtr, NextLink)) {\r | |
988 | Subtask = CR (NextLink, DISK_IO_SUBTASK, Link, DISK_IO_SUBTASK_SIGNATURE);\r | |
989 | NextLink = DiskIoDestroySubtask (Instance, Subtask);\r | |
990 | }\r | |
991 | }\r | |
992 | \r | |
993 | //\r | |
994 | // It's possible that the non-blocking subtasks finish before raising TPL to NOTIFY,\r | |
995 | // so the subtasks list might be empty at this point.\r | |
996 | //\r | |
997 | if (!Blocking && IsListEmpty (SubtasksPtr)) {\r | |
998 | EfiAcquireLock (&Instance->TaskQueueLock);\r | |
999 | RemoveEntryList (&Task->Link);\r | |
1000 | EfiReleaseLock (&Instance->TaskQueueLock);\r | |
1001 | \r | |
1002 | if (!EFI_ERROR (Status) && (Task->Token != NULL)) {\r | |
1003 | //\r | |
1004 | // Task->Token should be set to NULL by the DiskIo2OnReadWriteComplete\r | |
1005 | // It it's not, that means the non-blocking request was downgraded to blocking request.\r | |
1006 | //\r | |
1007 | DEBUG ((DEBUG_VERBOSE, "DiskIo: Non-blocking request was downgraded to blocking request, signal event directly.\n"));\r | |
1008 | Task->Token->TransactionStatus = Status;\r | |
1009 | gBS->SignalEvent (Task->Token->Event);\r | |
1010 | }\r | |
1011 | \r | |
1012 | FreePool (Task);\r | |
1013 | }\r | |
1014 | \r | |
1015 | gBS->RestoreTPL (OldTpl);\r | |
1016 | \r | |
1017 | return Status;\r | |
1018 | }\r | |
1019 | \r | |
1020 | /**\r | |
1021 | Reads a specified number of bytes from a device.\r | |
1022 | \r | |
1023 | @param This Indicates a pointer to the calling context.\r | |
1024 | @param MediaId ID of the medium to be read.\r | |
1025 | @param Offset The starting byte offset on the logical block I/O device to read from.\r | |
1026 | @param Token A pointer to the token associated with the transaction.\r | |
1027 | If this field is NULL, synchronous/blocking IO is performed.\r | |
1028 | @param BufferSize The size in bytes of Buffer. The number of bytes to read from the device.\r | |
1029 | @param Buffer A pointer to the destination buffer for the data.\r | |
1030 | The caller is responsible either having implicit or explicit ownership of the buffer.\r | |
1031 | \r | |
1032 | @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was read correctly from the device.\r | |
1033 | If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.\r | |
1034 | Event will be signaled upon completion.\r | |
1035 | @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r | |
1036 | @retval EFI_NO_MEDIA There is no medium in the device.\r | |
1037 | @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium.\r | |
1038 | @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not valid for the device.\r | |
1039 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r | |
1040 | \r | |
1041 | **/\r | |
1042 | EFI_STATUS\r | |
1043 | EFIAPI\r | |
1044 | DiskIo2ReadDiskEx (\r | |
1045 | IN EFI_DISK_IO2_PROTOCOL *This,\r | |
1046 | IN UINT32 MediaId,\r | |
1047 | IN UINT64 Offset,\r | |
1048 | IN OUT EFI_DISK_IO2_TOKEN *Token,\r | |
1049 | IN UINTN BufferSize,\r | |
1050 | OUT VOID *Buffer\r | |
1051 | )\r | |
1052 | {\r | |
1053 | return DiskIo2ReadWriteDisk (\r | |
1054 | DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This),\r | |
1055 | FALSE,\r | |
1056 | MediaId,\r | |
1057 | Offset,\r | |
1058 | Token,\r | |
1059 | BufferSize,\r | |
1060 | (UINT8 *)Buffer\r | |
1061 | );\r | |
1062 | }\r | |
1063 | \r | |
1064 | /**\r | |
1065 | Writes a specified number of bytes to a device.\r | |
1066 | \r | |
1067 | @param This Indicates a pointer to the calling context.\r | |
1068 | @param MediaId ID of the medium to be written.\r | |
1069 | @param Offset The starting byte offset on the logical block I/O device to write to.\r | |
1070 | @param Token A pointer to the token associated with the transaction.\r | |
1071 | If this field is NULL, synchronous/blocking IO is performed.\r | |
1072 | @param BufferSize The size in bytes of Buffer. The number of bytes to write to the device.\r | |
1073 | @param Buffer A pointer to the buffer containing the data to be written.\r | |
1074 | \r | |
1075 | @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was written correctly to the device.\r | |
1076 | If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.\r | |
1077 | Event will be signaled upon completion.\r | |
1078 | @retval EFI_WRITE_PROTECTED The device cannot be written to.\r | |
1079 | @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation.\r | |
1080 | @retval EFI_NO_MEDIA There is no medium in the device.\r | |
1081 | @retval EFI_MEDIA_CHNAGED The MediaId is not for the current medium.\r | |
1082 | @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not valid for the device.\r | |
1083 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r | |
1084 | \r | |
1085 | **/\r | |
1086 | EFI_STATUS\r | |
1087 | EFIAPI\r | |
1088 | DiskIo2WriteDiskEx (\r | |
1089 | IN EFI_DISK_IO2_PROTOCOL *This,\r | |
1090 | IN UINT32 MediaId,\r | |
1091 | IN UINT64 Offset,\r | |
1092 | IN OUT EFI_DISK_IO2_TOKEN *Token,\r | |
1093 | IN UINTN BufferSize,\r | |
1094 | IN VOID *Buffer\r | |
1095 | )\r | |
1096 | {\r | |
1097 | return DiskIo2ReadWriteDisk (\r | |
1098 | DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This),\r | |
1099 | TRUE,\r | |
1100 | MediaId,\r | |
1101 | Offset,\r | |
1102 | Token,\r | |
1103 | BufferSize,\r | |
1104 | (UINT8 *)Buffer\r | |
1105 | );\r | |
1106 | }\r | |
1107 | \r | |
1108 | /**\r | |
1109 | The callback for the BlockIo2 FlushBlocksEx.\r | |
1110 | @param Event Event whose notification function is being invoked.\r | |
1111 | @param Context The pointer to the notification function's context,\r | |
1112 | which points to the DISK_IO2_FLUSH_TASK instance.\r | |
1113 | **/\r | |
1114 | VOID\r | |
1115 | EFIAPI\r | |
1116 | DiskIo2OnFlushComplete (\r | |
1117 | IN EFI_EVENT Event,\r | |
1118 | IN VOID *Context\r | |
1119 | )\r | |
1120 | {\r | |
1121 | DISK_IO2_FLUSH_TASK *Task;\r | |
1122 | \r | |
1123 | gBS->CloseEvent (Event);\r | |
1124 | \r | |
1125 | Task = (DISK_IO2_FLUSH_TASK *)Context;\r | |
1126 | ASSERT (Task->Signature == DISK_IO2_FLUSH_TASK_SIGNATURE);\r | |
1127 | Task->Token->TransactionStatus = Task->BlockIo2Token.TransactionStatus;\r | |
1128 | gBS->SignalEvent (Task->Token->Event);\r | |
1129 | \r | |
1130 | FreePool (Task);\r | |
1131 | }\r | |
1132 | \r | |
1133 | /**\r | |
1134 | Flushes all modified data to the physical device.\r | |
1135 | \r | |
1136 | @param This Indicates a pointer to the calling context.\r | |
1137 | @param Token A pointer to the token associated with the transaction.\r | |
1138 | If this field is NULL, synchronous/blocking IO is performed.\r | |
1139 | \r | |
1140 | @retval EFI_SUCCESS If Event is NULL (blocking I/O): The data was flushed successfully to the device.\r | |
1141 | If Event is not NULL (asynchronous I/O): The request was successfully queued for processing.\r | |
1142 | Event will be signaled upon completion.\r | |
1143 | @retval EFI_WRITE_PROTECTED The device cannot be written to.\r | |
1144 | @retval EFI_DEVICE_ERROR The device reported an error while performing the write operation.\r | |
1145 | @retval EFI_NO_MEDIA There is no medium in the device.\r | |
1146 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r | |
1147 | **/\r | |
1148 | EFI_STATUS\r | |
1149 | EFIAPI\r | |
1150 | DiskIo2FlushDiskEx (\r | |
1151 | IN EFI_DISK_IO2_PROTOCOL *This,\r | |
1152 | IN OUT EFI_DISK_IO2_TOKEN *Token\r | |
1153 | )\r | |
1154 | {\r | |
1155 | EFI_STATUS Status;\r | |
1156 | DISK_IO2_FLUSH_TASK *Task;\r | |
1157 | DISK_IO_PRIVATE_DATA *Private;\r | |
1158 | \r | |
1159 | Private = DISK_IO_PRIVATE_DATA_FROM_DISK_IO2 (This);\r | |
1160 | \r | |
1161 | if ((Token != NULL) && (Token->Event != NULL)) {\r | |
1162 | Task = AllocatePool (sizeof (DISK_IO2_FLUSH_TASK));\r | |
1163 | if (Task == NULL) {\r | |
1164 | return EFI_OUT_OF_RESOURCES;\r | |
1165 | }\r | |
1166 | \r | |
1167 | Status = gBS->CreateEvent (\r | |
1168 | EVT_NOTIFY_SIGNAL,\r | |
1169 | TPL_CALLBACK,\r | |
1170 | DiskIo2OnFlushComplete,\r | |
1171 | Task,\r | |
1172 | &Task->BlockIo2Token.Event\r | |
1173 | );\r | |
1174 | if (EFI_ERROR (Status)) {\r | |
1175 | FreePool (Task);\r | |
1176 | return Status;\r | |
1177 | }\r | |
1178 | \r | |
1179 | Task->Signature = DISK_IO2_FLUSH_TASK_SIGNATURE;\r | |
1180 | Task->Token = Token;\r | |
1181 | Status = Private->BlockIo2->FlushBlocksEx (Private->BlockIo2, &Task->BlockIo2Token);\r | |
1182 | if (EFI_ERROR (Status)) {\r | |
1183 | gBS->CloseEvent (Task->BlockIo2Token.Event);\r | |
1184 | FreePool (Task);\r | |
1185 | }\r | |
1186 | } else {\r | |
1187 | Status = Private->BlockIo2->FlushBlocksEx (Private->BlockIo2, NULL);\r | |
1188 | }\r | |
1189 | \r | |
1190 | return Status;\r | |
1191 | }\r | |
1192 | \r | |
1193 | /**\r | |
1194 | Read BufferSize bytes from Offset into Buffer.\r | |
1195 | Reads may support reads that are not aligned on\r | |
1196 | sector boundaries. There are three cases:\r | |
1197 | UnderRun - The first byte is not on a sector boundary or the read request is\r | |
1198 | less than a sector in length.\r | |
1199 | Aligned - A read of N contiguous sectors.\r | |
1200 | OverRun - The last byte is not on a sector boundary.\r | |
1201 | \r | |
1202 | @param This Protocol instance pointer.\r | |
1203 | @param MediaId Id of the media, changes every time the media is replaced.\r | |
1204 | @param Offset The starting byte offset to read from\r | |
1205 | @param BufferSize Size of Buffer\r | |
1206 | @param Buffer Buffer containing read data\r | |
1207 | \r | |
1208 | @retval EFI_SUCCESS The data was read correctly from the device.\r | |
1209 | @retval EFI_DEVICE_ERROR The device reported an error while performing the read.\r | |
1210 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
1211 | @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r | |
1212 | @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not\r | |
1213 | valid for the device.\r | |
1214 | \r | |
1215 | **/\r | |
1216 | EFI_STATUS\r | |
1217 | EFIAPI\r | |
1218 | DiskIoReadDisk (\r | |
1219 | IN EFI_DISK_IO_PROTOCOL *This,\r | |
1220 | IN UINT32 MediaId,\r | |
1221 | IN UINT64 Offset,\r | |
1222 | IN UINTN BufferSize,\r | |
1223 | OUT VOID *Buffer\r | |
1224 | )\r | |
1225 | {\r | |
1226 | return DiskIo2ReadWriteDisk (\r | |
1227 | DISK_IO_PRIVATE_DATA_FROM_DISK_IO (This),\r | |
1228 | FALSE,\r | |
1229 | MediaId,\r | |
1230 | Offset,\r | |
1231 | NULL,\r | |
1232 | BufferSize,\r | |
1233 | (UINT8 *)Buffer\r | |
1234 | );\r | |
1235 | }\r | |
1236 | \r | |
1237 | /**\r | |
1238 | Writes BufferSize bytes from Buffer into Offset.\r | |
1239 | Writes may require a read modify write to support writes that are not\r | |
1240 | aligned on sector boundaries. There are three cases:\r | |
1241 | UnderRun - The first byte is not on a sector boundary or the write request\r | |
1242 | is less than a sector in length. Read modify write is required.\r | |
1243 | Aligned - A write of N contiguous sectors.\r | |
1244 | OverRun - The last byte is not on a sector boundary. Read modified write\r | |
1245 | required.\r | |
1246 | \r | |
1247 | @param This Protocol instance pointer.\r | |
1248 | @param MediaId Id of the media, changes every time the media is replaced.\r | |
1249 | @param Offset The starting byte offset to read from\r | |
1250 | @param BufferSize Size of Buffer\r | |
1251 | @param Buffer Buffer containing read data\r | |
1252 | \r | |
1253 | @retval EFI_SUCCESS The data was written correctly to the device.\r | |
1254 | @retval EFI_WRITE_PROTECTED The device can not be written to.\r | |
1255 | @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r | |
1256 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
1257 | @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r | |
1258 | @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not\r | |
1259 | valid for the device.\r | |
1260 | \r | |
1261 | **/\r | |
1262 | EFI_STATUS\r | |
1263 | EFIAPI\r | |
1264 | DiskIoWriteDisk (\r | |
1265 | IN EFI_DISK_IO_PROTOCOL *This,\r | |
1266 | IN UINT32 MediaId,\r | |
1267 | IN UINT64 Offset,\r | |
1268 | IN UINTN BufferSize,\r | |
1269 | IN VOID *Buffer\r | |
1270 | )\r | |
1271 | {\r | |
1272 | return DiskIo2ReadWriteDisk (\r | |
1273 | DISK_IO_PRIVATE_DATA_FROM_DISK_IO (This),\r | |
1274 | TRUE,\r | |
1275 | MediaId,\r | |
1276 | Offset,\r | |
1277 | NULL,\r | |
1278 | BufferSize,\r | |
1279 | (UINT8 *)Buffer\r | |
1280 | );\r | |
1281 | }\r | |
1282 | \r | |
1283 | /**\r | |
1284 | The user Entry Point for module DiskIo. The user code starts with this function.\r | |
1285 | \r | |
1286 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r | |
1287 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
1288 | \r | |
1289 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
1290 | @retval other Some error occurs when executing this entry point.\r | |
1291 | \r | |
1292 | **/\r | |
1293 | EFI_STATUS\r | |
1294 | EFIAPI\r | |
1295 | InitializeDiskIo (\r | |
1296 | IN EFI_HANDLE ImageHandle,\r | |
1297 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1298 | )\r | |
1299 | {\r | |
1300 | EFI_STATUS Status;\r | |
1301 | \r | |
1302 | //\r | |
1303 | // Install driver model protocol(s).\r | |
1304 | //\r | |
1305 | Status = EfiLibInstallDriverBindingComponentName2 (\r | |
1306 | ImageHandle,\r | |
1307 | SystemTable,\r | |
1308 | &gDiskIoDriverBinding,\r | |
1309 | ImageHandle,\r | |
1310 | &gDiskIoComponentName,\r | |
1311 | &gDiskIoComponentName2\r | |
1312 | );\r | |
1313 | ASSERT_EFI_ERROR (Status);\r | |
1314 | \r | |
1315 | return Status;\r | |
1316 | }\r |