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