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