]>
Commit | Line | Data |
---|---|---|
adbcbf8f | 1 | /** @file\r |
ff61847d | 2 | DiskIo driver that lays on every BlockIo protocol in the system.\r |
adbcbf8f | 3 | DiskIo converts a block oriented device to a byte oriented device.\r |
4 | \r | |
ff61847d | 5 | Disk access may have to handle unaligned request about sector boundaries.\r |
adbcbf8f | 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 | |
e5eed7d3 HT |
12 | Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>\r |
13 | This program and the accompanying materials\r | |
f42be642 | 14 | are licensed and made available under the terms and conditions of the BSD License\r |
15 | which accompanies this distribution. The full text of the license may be found at\r | |
16 | http://opensource.org/licenses/bsd-license.php\r | |
17 | \r | |
18 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
19 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
adbcbf8f | 20 | \r |
21 | **/\r | |
22 | \r | |
23 | #include "DiskIo.h"\r | |
24 | \r | |
ff61847d | 25 | //\r |
26 | // Driver binding protocol implementation for DiskIo driver.\r | |
27 | //\r | |
adbcbf8f | 28 | EFI_DRIVER_BINDING_PROTOCOL gDiskIoDriverBinding = {\r |
29 | DiskIoDriverBindingSupported,\r | |
30 | DiskIoDriverBindingStart,\r | |
31 | DiskIoDriverBindingStop,\r | |
32 | 0xa,\r | |
33 | NULL,\r | |
34 | NULL\r | |
35 | };\r | |
36 | \r | |
ff61847d | 37 | //\r |
38 | // Template for DiskIo private data structure.\r | |
39 | // The pointer to BlockIo protocol interface is assigned dynamically.\r | |
40 | //\r | |
adbcbf8f | 41 | DISK_IO_PRIVATE_DATA gDiskIoPrivateDataTemplate = {\r |
42 | DISK_IO_PRIVATE_DATA_SIGNATURE,\r | |
43 | {\r | |
44 | EFI_DISK_IO_PROTOCOL_REVISION,\r | |
45 | DiskIoReadDisk,\r | |
46 | DiskIoWriteDisk\r | |
47 | },\r | |
48 | NULL\r | |
49 | };\r | |
50 | \r | |
51 | \r | |
52 | /**\r | |
53 | Test to see if this driver supports ControllerHandle. \r | |
54 | \r | |
55 | @param This Protocol instance pointer.\r | |
56 | @param ControllerHandle Handle of device to test\r | |
57 | @param RemainingDevicePath Optional parameter use to pick a specific child\r | |
58 | device to start.\r | |
59 | \r | |
60 | @retval EFI_SUCCESS This driver supports this device\r | |
61 | @retval EFI_ALREADY_STARTED This driver is already running on this device\r | |
62 | @retval other This driver does not support this device\r | |
63 | \r | |
64 | **/\r | |
65 | EFI_STATUS\r | |
66 | EFIAPI\r | |
67 | DiskIoDriverBindingSupported (\r | |
68 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
69 | IN EFI_HANDLE ControllerHandle,\r | |
70 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r | |
71 | )\r | |
72 | {\r | |
73 | EFI_STATUS Status;\r | |
74 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
75 | \r | |
76 | //\r | |
77 | // Open the IO Abstraction(s) needed to perform the supported test.\r | |
78 | //\r | |
79 | Status = gBS->OpenProtocol (\r | |
80 | ControllerHandle,\r | |
81 | &gEfiBlockIoProtocolGuid,\r | |
82 | (VOID **) &BlockIo,\r | |
83 | This->DriverBindingHandle,\r | |
84 | ControllerHandle,\r | |
85 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
86 | );\r | |
87 | if (EFI_ERROR (Status)) {\r | |
88 | return Status;\r | |
89 | }\r | |
90 | \r | |
91 | //\r | |
92 | // Close the I/O Abstraction(s) used to perform the supported test.\r | |
93 | //\r | |
94 | gBS->CloseProtocol (\r | |
95 | ControllerHandle,\r | |
96 | &gEfiBlockIoProtocolGuid,\r | |
97 | This->DriverBindingHandle,\r | |
98 | ControllerHandle\r | |
99 | );\r | |
100 | return EFI_SUCCESS;\r | |
101 | }\r | |
102 | \r | |
103 | \r | |
104 | /**\r | |
105 | Start this driver on ControllerHandle by opening a Block IO protocol and\r | |
106 | installing a Disk IO protocol on ControllerHandle.\r | |
107 | \r | |
108 | @param This Protocol instance pointer.\r | |
109 | @param ControllerHandle Handle of device to bind driver to\r | |
110 | @param RemainingDevicePath Optional parameter use to pick a specific child\r | |
111 | device to start.\r | |
112 | \r | |
113 | @retval EFI_SUCCESS This driver is added to ControllerHandle\r | |
114 | @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r | |
115 | @retval other This driver does not support this device\r | |
116 | \r | |
117 | **/\r | |
118 | EFI_STATUS\r | |
119 | EFIAPI\r | |
120 | DiskIoDriverBindingStart (\r | |
121 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
122 | IN EFI_HANDLE ControllerHandle,\r | |
123 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r | |
124 | )\r | |
125 | {\r | |
126 | EFI_STATUS Status;\r | |
127 | DISK_IO_PRIVATE_DATA *Private;\r | |
15cc67e6 | 128 | EFI_TPL OldTpl;\r |
adbcbf8f | 129 | \r |
15cc67e6 | 130 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r |
adbcbf8f | 131 | Private = NULL;\r |
132 | \r | |
133 | //\r | |
134 | // Connect to the Block IO interface on ControllerHandle.\r | |
135 | //\r | |
136 | Status = gBS->OpenProtocol (\r | |
137 | ControllerHandle,\r | |
138 | &gEfiBlockIoProtocolGuid,\r | |
139 | (VOID **) &gDiskIoPrivateDataTemplate.BlockIo,\r | |
140 | This->DriverBindingHandle,\r | |
141 | ControllerHandle,\r | |
142 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
143 | );\r | |
144 | if (EFI_ERROR (Status)) {\r | |
15cc67e6 | 145 | goto ErrorExit1;\r |
adbcbf8f | 146 | }\r |
147 | \r | |
148 | //\r | |
149 | // Initialize the Disk IO device instance.\r | |
150 | //\r | |
151 | Private = AllocateCopyPool (sizeof (DISK_IO_PRIVATE_DATA), &gDiskIoPrivateDataTemplate);\r | |
152 | if (Private == NULL) {\r | |
153 | Status = EFI_OUT_OF_RESOURCES;\r | |
154 | goto ErrorExit;\r | |
155 | }\r | |
156 | \r | |
157 | //\r | |
158 | // Install protocol interfaces for the Disk IO device.\r | |
159 | //\r | |
160 | Status = gBS->InstallProtocolInterface (\r | |
161 | &ControllerHandle,\r | |
162 | &gEfiDiskIoProtocolGuid,\r | |
163 | EFI_NATIVE_INTERFACE,\r | |
164 | &Private->DiskIo\r | |
165 | );\r | |
166 | \r | |
167 | ErrorExit:\r | |
168 | if (EFI_ERROR (Status)) {\r | |
169 | \r | |
170 | if (Private != NULL) {\r | |
171 | FreePool (Private);\r | |
172 | }\r | |
173 | \r | |
174 | gBS->CloseProtocol (\r | |
175 | ControllerHandle,\r | |
176 | &gEfiBlockIoProtocolGuid,\r | |
177 | This->DriverBindingHandle,\r | |
178 | ControllerHandle\r | |
179 | );\r | |
180 | }\r | |
181 | \r | |
15cc67e6 | 182 | ErrorExit1:\r |
183 | gBS->RestoreTPL (OldTpl);\r | |
adbcbf8f | 184 | return Status;\r |
185 | }\r | |
186 | \r | |
187 | \r | |
188 | /**\r | |
189 | Stop this driver on ControllerHandle by removing Disk IO protocol and closing\r | |
190 | the Block IO protocol on ControllerHandle.\r | |
191 | \r | |
192 | @param This Protocol instance pointer.\r | |
193 | @param ControllerHandle Handle of device to stop driver on\r | |
194 | @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r | |
195 | children is zero stop the entire bus driver.\r | |
196 | @param ChildHandleBuffer List of Child Handles to Stop.\r | |
197 | \r | |
198 | @retval EFI_SUCCESS This driver is removed ControllerHandle\r | |
199 | @retval other This driver was not removed from this device\r | |
200 | \r | |
201 | **/\r | |
202 | EFI_STATUS\r | |
203 | EFIAPI\r | |
204 | DiskIoDriverBindingStop (\r | |
205 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
206 | IN EFI_HANDLE ControllerHandle,\r | |
207 | IN UINTN NumberOfChildren,\r | |
208 | IN EFI_HANDLE *ChildHandleBuffer\r | |
209 | )\r | |
210 | {\r | |
211 | EFI_STATUS Status;\r | |
212 | EFI_DISK_IO_PROTOCOL *DiskIo;\r | |
213 | DISK_IO_PRIVATE_DATA *Private;\r | |
214 | \r | |
215 | //\r | |
216 | // Get our context back.\r | |
217 | //\r | |
218 | Status = gBS->OpenProtocol (\r | |
219 | ControllerHandle,\r | |
220 | &gEfiDiskIoProtocolGuid,\r | |
221 | (VOID **) &DiskIo,\r | |
222 | This->DriverBindingHandle,\r | |
223 | ControllerHandle,\r | |
224 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
225 | );\r | |
226 | if (EFI_ERROR (Status)) {\r | |
227 | return EFI_UNSUPPORTED;\r | |
228 | }\r | |
229 | \r | |
230 | Private = DISK_IO_PRIVATE_DATA_FROM_THIS (DiskIo);\r | |
231 | \r | |
232 | Status = gBS->UninstallProtocolInterface (\r | |
233 | ControllerHandle,\r | |
234 | &gEfiDiskIoProtocolGuid,\r | |
235 | &Private->DiskIo\r | |
236 | );\r | |
237 | if (!EFI_ERROR (Status)) {\r | |
adbcbf8f | 238 | Status = gBS->CloseProtocol (\r |
239 | ControllerHandle,\r | |
240 | &gEfiBlockIoProtocolGuid,\r | |
241 | This->DriverBindingHandle,\r | |
242 | ControllerHandle\r | |
243 | );\r | |
244 | }\r | |
245 | \r | |
246 | if (!EFI_ERROR (Status)) {\r | |
247 | FreePool (Private);\r | |
248 | }\r | |
249 | \r | |
250 | return Status;\r | |
251 | }\r | |
252 | \r | |
253 | \r | |
254 | \r | |
255 | /**\r | |
256 | Read BufferSize bytes from Offset into Buffer.\r | |
257 | Reads may support reads that are not aligned on\r | |
258 | sector boundaries. There are three cases:\r | |
259 | UnderRun - The first byte is not on a sector boundary or the read request is\r | |
260 | less than a sector in length.\r | |
261 | Aligned - A read of N contiguous sectors.\r | |
262 | OverRun - The last byte is not on a sector boundary.\r | |
263 | \r | |
264 | @param This Protocol instance pointer.\r | |
265 | @param MediaId Id of the media, changes every time the media is replaced.\r | |
266 | @param Offset The starting byte offset to read from\r | |
267 | @param BufferSize Size of Buffer\r | |
268 | @param Buffer Buffer containing read data\r | |
269 | \r | |
270 | @retval EFI_SUCCESS The data was read correctly from the device.\r | |
271 | @retval EFI_DEVICE_ERROR The device reported an error while performing the read.\r | |
272 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
273 | @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r | |
274 | @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not\r | |
275 | valid for the device.\r | |
276 | \r | |
277 | **/\r | |
278 | EFI_STATUS\r | |
279 | EFIAPI\r | |
280 | DiskIoReadDisk (\r | |
281 | IN EFI_DISK_IO_PROTOCOL *This,\r | |
282 | IN UINT32 MediaId,\r | |
283 | IN UINT64 Offset,\r | |
284 | IN UINTN BufferSize,\r | |
285 | OUT VOID *Buffer\r | |
286 | )\r | |
287 | {\r | |
288 | EFI_STATUS Status;\r | |
289 | DISK_IO_PRIVATE_DATA *Private;\r | |
290 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
291 | EFI_BLOCK_IO_MEDIA *Media;\r | |
292 | UINT32 BlockSize;\r | |
293 | UINT64 Lba;\r | |
294 | UINT64 OverRunLba;\r | |
295 | UINT32 UnderRun;\r | |
296 | UINT32 OverRun;\r | |
297 | BOOLEAN TransactionComplete;\r | |
298 | UINTN WorkingBufferSize;\r | |
299 | UINT8 *WorkingBuffer;\r | |
300 | UINTN Length;\r | |
301 | UINT8 *Data;\r | |
302 | UINT8 *PreData;\r | |
303 | UINTN IsBufferAligned;\r | |
304 | UINTN DataBufferSize;\r | |
305 | BOOLEAN LastRead;\r | |
306 | \r | |
307 | Private = DISK_IO_PRIVATE_DATA_FROM_THIS (This);\r | |
308 | \r | |
309 | BlockIo = Private->BlockIo;\r | |
310 | Media = BlockIo->Media;\r | |
311 | BlockSize = Media->BlockSize;\r | |
312 | \r | |
313 | if (Media->MediaId != MediaId) {\r | |
314 | return EFI_MEDIA_CHANGED;\r | |
315 | }\r | |
316 | \r | |
317 | WorkingBuffer = Buffer;\r | |
318 | WorkingBufferSize = BufferSize;\r | |
319 | \r | |
320 | //\r | |
321 | // Allocate a temporary buffer for operation\r | |
322 | //\r | |
323 | DataBufferSize = BlockSize * DATA_BUFFER_BLOCK_NUM;\r | |
324 | \r | |
325 | if (Media->IoAlign > 1) {\r | |
326 | PreData = AllocatePool (DataBufferSize + Media->IoAlign);\r | |
327 | Data = PreData - ((UINTN) PreData & (Media->IoAlign - 1)) + Media->IoAlign;\r | |
328 | } else {\r | |
329 | PreData = AllocatePool (DataBufferSize);\r | |
330 | Data = PreData;\r | |
331 | }\r | |
332 | \r | |
333 | if (PreData == NULL) {\r | |
334 | return EFI_OUT_OF_RESOURCES;\r | |
335 | }\r | |
336 | \r | |
337 | Lba = DivU64x32Remainder (Offset, BlockSize, &UnderRun);\r | |
338 | \r | |
339 | Length = BlockSize - UnderRun;\r | |
340 | TransactionComplete = FALSE;\r | |
341 | \r | |
342 | Status = EFI_SUCCESS;\r | |
343 | if (UnderRun != 0) {\r | |
344 | //\r | |
345 | // Offset starts in the middle of an Lba, so read the entire block.\r | |
346 | //\r | |
347 | Status = BlockIo->ReadBlocks (\r | |
348 | BlockIo,\r | |
349 | MediaId,\r | |
350 | Lba,\r | |
351 | BlockSize,\r | |
352 | Data\r | |
353 | );\r | |
354 | \r | |
355 | if (EFI_ERROR (Status)) {\r | |
356 | goto Done;\r | |
357 | }\r | |
358 | \r | |
359 | if (Length > BufferSize) {\r | |
360 | Length = BufferSize;\r | |
361 | TransactionComplete = TRUE;\r | |
362 | }\r | |
363 | \r | |
364 | CopyMem (WorkingBuffer, Data + UnderRun, Length);\r | |
365 | \r | |
366 | WorkingBuffer += Length;\r | |
367 | \r | |
368 | WorkingBufferSize -= Length;\r | |
369 | if (WorkingBufferSize == 0) {\r | |
370 | goto Done;\r | |
371 | }\r | |
372 | \r | |
373 | Lba += 1;\r | |
374 | }\r | |
375 | \r | |
376 | OverRunLba = Lba + DivU64x32Remainder (WorkingBufferSize, BlockSize, &OverRun);\r | |
377 | \r | |
378 | if (!TransactionComplete && WorkingBufferSize >= BlockSize) {\r | |
379 | //\r | |
380 | // If the DiskIo maps directly to a BlockIo device do the read.\r | |
381 | //\r | |
382 | if (OverRun != 0) {\r | |
383 | WorkingBufferSize -= OverRun;\r | |
384 | }\r | |
385 | //\r | |
386 | // Check buffer alignment\r | |
387 | //\r | |
388 | IsBufferAligned = (UINTN) WorkingBuffer & (UINTN) (Media->IoAlign - 1);\r | |
389 | \r | |
390 | if (Media->IoAlign <= 1 || IsBufferAligned == 0) {\r | |
391 | //\r | |
392 | // Alignment is satisfied, so read them together\r | |
393 | //\r | |
394 | Status = BlockIo->ReadBlocks (\r | |
395 | BlockIo,\r | |
396 | MediaId,\r | |
397 | Lba,\r | |
398 | WorkingBufferSize,\r | |
399 | WorkingBuffer\r | |
400 | );\r | |
401 | \r | |
402 | if (EFI_ERROR (Status)) {\r | |
403 | goto Done;\r | |
404 | }\r | |
405 | \r | |
406 | WorkingBuffer += WorkingBufferSize;\r | |
407 | \r | |
408 | } else {\r | |
409 | //\r | |
410 | // Use the allocated buffer instead of the original buffer\r | |
411 | // to avoid alignment issue.\r | |
412 | // Here, the allocated buffer (8-byte align) can satisfy the alignment\r | |
413 | //\r | |
414 | LastRead = FALSE;\r | |
415 | do {\r | |
416 | if (WorkingBufferSize <= DataBufferSize) {\r | |
417 | //\r | |
418 | // It is the last calling to readblocks in this loop\r | |
419 | //\r | |
420 | DataBufferSize = WorkingBufferSize;\r | |
421 | LastRead = TRUE;\r | |
422 | }\r | |
423 | \r | |
424 | Status = BlockIo->ReadBlocks (\r | |
425 | BlockIo,\r | |
426 | MediaId,\r | |
427 | Lba,\r | |
428 | DataBufferSize,\r | |
429 | Data\r | |
430 | );\r | |
431 | if (EFI_ERROR (Status)) {\r | |
432 | goto Done;\r | |
433 | }\r | |
434 | \r | |
435 | CopyMem (WorkingBuffer, Data, DataBufferSize);\r | |
436 | WorkingBufferSize -= DataBufferSize;\r | |
437 | WorkingBuffer += DataBufferSize;\r | |
438 | Lba += DATA_BUFFER_BLOCK_NUM;\r | |
439 | } while (!LastRead);\r | |
440 | }\r | |
441 | }\r | |
442 | \r | |
443 | if (!TransactionComplete && OverRun != 0) {\r | |
444 | //\r | |
445 | // Last read is not a complete block.\r | |
446 | //\r | |
447 | Status = BlockIo->ReadBlocks (\r | |
448 | BlockIo,\r | |
449 | MediaId,\r | |
450 | OverRunLba,\r | |
451 | BlockSize,\r | |
452 | Data\r | |
453 | );\r | |
454 | \r | |
455 | if (EFI_ERROR (Status)) {\r | |
456 | goto Done;\r | |
457 | }\r | |
458 | \r | |
459 | CopyMem (WorkingBuffer, Data, OverRun);\r | |
460 | }\r | |
461 | \r | |
462 | Done:\r | |
463 | if (PreData != NULL) {\r | |
464 | FreePool (PreData);\r | |
465 | }\r | |
466 | \r | |
467 | return Status;\r | |
468 | }\r | |
469 | \r | |
470 | \r | |
471 | /**\r | |
ff61847d | 472 | Writes BufferSize bytes from Buffer into Offset.\r |
adbcbf8f | 473 | Writes may require a read modify write to support writes that are not\r |
474 | aligned on sector boundaries. There are three cases:\r | |
475 | UnderRun - The first byte is not on a sector boundary or the write request\r | |
476 | is less than a sector in length. Read modify write is required.\r | |
477 | Aligned - A write of N contiguous sectors.\r | |
478 | OverRun - The last byte is not on a sector boundary. Read modified write\r | |
479 | required.\r | |
480 | \r | |
481 | @param This Protocol instance pointer.\r | |
482 | @param MediaId Id of the media, changes every time the media is replaced.\r | |
483 | @param Offset The starting byte offset to read from\r | |
484 | @param BufferSize Size of Buffer\r | |
485 | @param Buffer Buffer containing read data\r | |
486 | \r | |
487 | @retval EFI_SUCCESS The data was written correctly to the device.\r | |
488 | @retval EFI_WRITE_PROTECTED The device can not be written to.\r | |
489 | @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r | |
490 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
491 | @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r | |
492 | @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not\r | |
493 | valid for the device.\r | |
494 | \r | |
495 | **/\r | |
496 | EFI_STATUS\r | |
497 | EFIAPI\r | |
498 | DiskIoWriteDisk (\r | |
499 | IN EFI_DISK_IO_PROTOCOL *This,\r | |
500 | IN UINT32 MediaId,\r | |
501 | IN UINT64 Offset,\r | |
502 | IN UINTN BufferSize,\r | |
503 | IN VOID *Buffer\r | |
504 | )\r | |
505 | {\r | |
506 | EFI_STATUS Status;\r | |
507 | DISK_IO_PRIVATE_DATA *Private;\r | |
508 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
509 | EFI_BLOCK_IO_MEDIA *Media;\r | |
510 | UINT32 BlockSize;\r | |
511 | UINT64 Lba;\r | |
512 | UINT64 OverRunLba;\r | |
513 | UINT32 UnderRun;\r | |
514 | UINT32 OverRun;\r | |
515 | BOOLEAN TransactionComplete;\r | |
516 | UINTN WorkingBufferSize;\r | |
517 | UINT8 *WorkingBuffer;\r | |
518 | UINTN Length;\r | |
519 | UINT8 *Data;\r | |
520 | UINT8 *PreData;\r | |
521 | UINTN IsBufferAligned;\r | |
522 | UINTN DataBufferSize;\r | |
523 | BOOLEAN LastWrite;\r | |
524 | \r | |
525 | Private = DISK_IO_PRIVATE_DATA_FROM_THIS (This);\r | |
526 | \r | |
527 | BlockIo = Private->BlockIo;\r | |
528 | Media = BlockIo->Media;\r | |
529 | BlockSize = Media->BlockSize;\r | |
530 | \r | |
531 | if (Media->ReadOnly) {\r | |
532 | return EFI_WRITE_PROTECTED;\r | |
533 | }\r | |
534 | \r | |
535 | if (Media->MediaId != MediaId) {\r | |
536 | return EFI_MEDIA_CHANGED;\r | |
537 | }\r | |
538 | \r | |
539 | DataBufferSize = BlockSize * DATA_BUFFER_BLOCK_NUM;\r | |
540 | \r | |
541 | if (Media->IoAlign > 1) {\r | |
542 | PreData = AllocatePool (DataBufferSize + Media->IoAlign);\r | |
543 | Data = PreData - ((UINTN) PreData & (Media->IoAlign - 1)) + Media->IoAlign;\r | |
544 | } else {\r | |
545 | PreData = AllocatePool (DataBufferSize);\r | |
546 | Data = PreData;\r | |
547 | }\r | |
548 | \r | |
549 | if (PreData == NULL) {\r | |
550 | return EFI_OUT_OF_RESOURCES;\r | |
551 | }\r | |
552 | \r | |
553 | WorkingBuffer = Buffer;\r | |
554 | WorkingBufferSize = BufferSize;\r | |
555 | \r | |
556 | Lba = DivU64x32Remainder (Offset, BlockSize, &UnderRun);\r | |
557 | \r | |
558 | Length = BlockSize - UnderRun;\r | |
559 | TransactionComplete = FALSE;\r | |
560 | \r | |
561 | Status = EFI_SUCCESS;\r | |
562 | if (UnderRun != 0) {\r | |
563 | //\r | |
564 | // Offset starts in the middle of an Lba, so do read modify write.\r | |
565 | //\r | |
566 | Status = BlockIo->ReadBlocks (\r | |
567 | BlockIo,\r | |
568 | MediaId,\r | |
569 | Lba,\r | |
570 | BlockSize,\r | |
571 | Data\r | |
572 | );\r | |
573 | \r | |
574 | if (EFI_ERROR (Status)) {\r | |
575 | goto Done;\r | |
576 | }\r | |
577 | \r | |
578 | if (Length > BufferSize) {\r | |
579 | Length = BufferSize;\r | |
580 | TransactionComplete = TRUE;\r | |
581 | }\r | |
582 | \r | |
583 | CopyMem (Data + UnderRun, WorkingBuffer, Length);\r | |
584 | \r | |
585 | Status = BlockIo->WriteBlocks (\r | |
586 | BlockIo,\r | |
587 | MediaId,\r | |
588 | Lba,\r | |
589 | BlockSize,\r | |
590 | Data\r | |
591 | );\r | |
592 | if (EFI_ERROR (Status)) {\r | |
593 | goto Done;\r | |
594 | }\r | |
595 | \r | |
596 | WorkingBuffer += Length;\r | |
597 | WorkingBufferSize -= Length;\r | |
598 | if (WorkingBufferSize == 0) {\r | |
599 | goto Done;\r | |
600 | }\r | |
601 | \r | |
602 | Lba += 1;\r | |
603 | }\r | |
604 | \r | |
605 | OverRunLba = Lba + DivU64x32Remainder (WorkingBufferSize, BlockSize, &OverRun);\r | |
606 | \r | |
607 | if (!TransactionComplete && WorkingBufferSize >= BlockSize) {\r | |
608 | //\r | |
609 | // If the DiskIo maps directly to a BlockIo device do the write.\r | |
610 | //\r | |
611 | if (OverRun != 0) {\r | |
612 | WorkingBufferSize -= OverRun;\r | |
613 | }\r | |
614 | //\r | |
615 | // Check buffer alignment\r | |
616 | //\r | |
617 | IsBufferAligned = (UINTN) WorkingBuffer & (UINTN) (Media->IoAlign - 1);\r | |
618 | \r | |
619 | if (Media->IoAlign <= 1 || IsBufferAligned == 0) {\r | |
620 | //\r | |
621 | // Alignment is satisfied, so write them together\r | |
622 | //\r | |
623 | Status = BlockIo->WriteBlocks (\r | |
624 | BlockIo,\r | |
625 | MediaId,\r | |
626 | Lba,\r | |
627 | WorkingBufferSize,\r | |
628 | WorkingBuffer\r | |
629 | );\r | |
630 | \r | |
631 | if (EFI_ERROR (Status)) {\r | |
632 | goto Done;\r | |
633 | }\r | |
634 | \r | |
635 | WorkingBuffer += WorkingBufferSize;\r | |
636 | \r | |
637 | } else {\r | |
638 | //\r | |
639 | // The buffer parameter is not aligned with the request\r | |
640 | // So use the allocated instead.\r | |
641 | // It can fit almost all the cases.\r | |
642 | //\r | |
643 | LastWrite = FALSE;\r | |
644 | do {\r | |
645 | if (WorkingBufferSize <= DataBufferSize) {\r | |
646 | //\r | |
647 | // It is the last calling to writeblocks in this loop\r | |
648 | //\r | |
649 | DataBufferSize = WorkingBufferSize;\r | |
650 | LastWrite = TRUE;\r | |
651 | }\r | |
652 | \r | |
653 | CopyMem (Data, WorkingBuffer, DataBufferSize);\r | |
654 | Status = BlockIo->WriteBlocks (\r | |
655 | BlockIo,\r | |
656 | MediaId,\r | |
657 | Lba,\r | |
658 | DataBufferSize,\r | |
659 | Data\r | |
660 | );\r | |
661 | if (EFI_ERROR (Status)) {\r | |
662 | goto Done;\r | |
663 | }\r | |
664 | \r | |
665 | WorkingBufferSize -= DataBufferSize;\r | |
666 | WorkingBuffer += DataBufferSize;\r | |
667 | Lba += DATA_BUFFER_BLOCK_NUM;\r | |
668 | } while (!LastWrite);\r | |
669 | }\r | |
670 | }\r | |
671 | \r | |
672 | if (!TransactionComplete && OverRun != 0) {\r | |
673 | //\r | |
674 | // Last bit is not a complete block, so do a read modify write.\r | |
675 | //\r | |
676 | Status = BlockIo->ReadBlocks (\r | |
677 | BlockIo,\r | |
678 | MediaId,\r | |
679 | OverRunLba,\r | |
680 | BlockSize,\r | |
681 | Data\r | |
682 | );\r | |
683 | \r | |
684 | if (EFI_ERROR (Status)) {\r | |
685 | goto Done;\r | |
686 | }\r | |
687 | \r | |
688 | CopyMem (Data, WorkingBuffer, OverRun);\r | |
689 | \r | |
690 | Status = BlockIo->WriteBlocks (\r | |
691 | BlockIo,\r | |
692 | MediaId,\r | |
693 | OverRunLba,\r | |
694 | BlockSize,\r | |
695 | Data\r | |
696 | );\r | |
697 | if (EFI_ERROR (Status)) {\r | |
698 | goto Done;\r | |
699 | }\r | |
700 | }\r | |
701 | \r | |
702 | Done:\r | |
703 | if (PreData != NULL) {\r | |
704 | FreePool (PreData);\r | |
705 | }\r | |
706 | \r | |
707 | return Status;\r | |
708 | }\r | |
709 | \r | |
710 | \r | |
711 | /**\r | |
712 | The user Entry Point for module DiskIo. The user code starts with this function.\r | |
713 | \r | |
714 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
715 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
716 | \r | |
717 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
718 | @retval other Some error occurs when executing this entry point.\r | |
719 | \r | |
720 | **/\r | |
721 | EFI_STATUS\r | |
722 | EFIAPI\r | |
723 | InitializeDiskIo (\r | |
724 | IN EFI_HANDLE ImageHandle,\r | |
725 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
726 | )\r | |
727 | {\r | |
728 | EFI_STATUS Status;\r | |
729 | \r | |
730 | //\r | |
731 | // Install driver model protocol(s).\r | |
732 | //\r | |
d38a0f44 | 733 | Status = EfiLibInstallDriverBindingComponentName2 (\r |
adbcbf8f | 734 | ImageHandle,\r |
735 | SystemTable,\r | |
736 | &gDiskIoDriverBinding,\r | |
737 | ImageHandle,\r | |
738 | &gDiskIoComponentName,\r | |
d38a0f44 | 739 | &gDiskIoComponentName2\r |
adbcbf8f | 740 | );\r |
741 | ASSERT_EFI_ERROR (Status);\r | |
742 | \r | |
743 | \r | |
744 | return Status;\r | |
745 | }\r | |
746 | \r |