2 Partition driver that produces logical BlockIo devices from a physical
3 BlockIo device. The logical BlockIo devices are based on the format
4 of the raw block devices media. Currently "El Torito CD-ROM", UDF, Legacy
5 MBR, and GPT partition schemes are supported.
7 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "Partition.h"
22 // Partition Driver Global Variables.
24 EFI_DRIVER_BINDING_PROTOCOL gPartitionDriverBinding
= {
25 PartitionDriverBindingSupported
,
26 PartitionDriverBindingStart
,
27 PartitionDriverBindingStop
,
29 // Grub4Dos copies the BPB of the first partition to the MBR. If the
30 // DriverBindingStart() of the Fat driver gets run before that of Partition
31 // driver only the first partition can be recognized.
32 // Let the driver binding version of Partition driver be higher than that of
33 // Fat driver to make sure the DriverBindingStart() of the Partition driver
34 // gets run before that of Fat driver so that all the partitions can be recognized.
42 // Prioritized function list to detect partition table.
44 PARTITION_DETECT_ROUTINE mPartitionDetectRoutineTable
[] = {
45 PartitionInstallGptChildHandles
,
46 PartitionInstallElToritoChildHandles
,
47 PartitionInstallMbrChildHandles
,
48 PartitionInstallUdfChildHandles
,
53 Test to see if this driver supports ControllerHandle. Any ControllerHandle
54 than contains a BlockIo and DiskIo protocol or a BlockIo2 protocol can be
57 @param[in] This Protocol instance pointer.
58 @param[in] ControllerHandle Handle of device to test.
59 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
62 @retval EFI_SUCCESS This driver supports this device
63 @retval EFI_ALREADY_STARTED This driver is already running on this device
64 @retval other This driver does not support this device
69 PartitionDriverBindingSupported (
70 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
71 IN EFI_HANDLE ControllerHandle
,
72 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
76 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
77 EFI_DISK_IO_PROTOCOL
*DiskIo
;
81 // Check RemainingDevicePath validation
83 if (RemainingDevicePath
!= NULL
) {
85 // Check if RemainingDevicePath is the End of Device Path Node,
86 // if yes, go on checking other conditions
88 if (!IsDevicePathEnd (RemainingDevicePath
)) {
90 // If RemainingDevicePath isn't the End of Device Path Node,
91 // check its validation
93 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
94 if (Node
->DevPath
.Type
!= MEDIA_DEVICE_PATH
||
95 Node
->DevPath
.SubType
!= MEDIA_HARDDRIVE_DP
||
96 DevicePathNodeLength (&Node
->DevPath
) != sizeof (HARDDRIVE_DEVICE_PATH
)) {
97 return EFI_UNSUPPORTED
;
103 // Open the IO Abstraction(s) needed to perform the supported test
105 Status
= gBS
->OpenProtocol (
107 &gEfiDiskIoProtocolGuid
,
109 This
->DriverBindingHandle
,
111 EFI_OPEN_PROTOCOL_BY_DRIVER
113 if (Status
== EFI_ALREADY_STARTED
) {
116 if (EFI_ERROR (Status
)) {
120 // Close the I/O Abstraction(s) used to perform the supported test
124 &gEfiDiskIoProtocolGuid
,
125 This
->DriverBindingHandle
,
130 // Open the EFI Device Path protocol needed to perform the supported test
132 Status
= gBS
->OpenProtocol (
134 &gEfiDevicePathProtocolGuid
,
135 (VOID
**) &ParentDevicePath
,
136 This
->DriverBindingHandle
,
138 EFI_OPEN_PROTOCOL_BY_DRIVER
140 if (Status
== EFI_ALREADY_STARTED
) {
144 if (EFI_ERROR (Status
)) {
149 // Close protocol, don't use device path protocol in the Support() function
153 &gEfiDevicePathProtocolGuid
,
154 This
->DriverBindingHandle
,
159 // Open the IO Abstraction(s) needed to perform the supported test
161 Status
= gBS
->OpenProtocol (
163 &gEfiBlockIoProtocolGuid
,
165 This
->DriverBindingHandle
,
167 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
174 Start this driver on ControllerHandle by opening a Block IO or a Block IO2
175 or both, and Disk IO protocol, reading Device Path, and creating a child
176 handle with a Disk IO and device path protocol.
178 @param[in] This Protocol instance pointer.
179 @param[in] ControllerHandle Handle of device to bind driver to
180 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
183 @retval EFI_SUCCESS This driver is added to ControllerHandle
184 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
185 @retval other This driver does not support this device
190 PartitionDriverBindingStart (
191 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
192 IN EFI_HANDLE ControllerHandle
,
193 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
197 EFI_STATUS OpenStatus
;
198 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
199 EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
;
200 EFI_DISK_IO_PROTOCOL
*DiskIo
;
201 EFI_DISK_IO2_PROTOCOL
*DiskIo2
;
202 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
203 PARTITION_DETECT_ROUTINE
*Routine
;
204 BOOLEAN MediaPresent
;
208 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
210 // Check RemainingDevicePath validation
212 if (RemainingDevicePath
!= NULL
) {
214 // Check if RemainingDevicePath is the End of Device Path Node,
215 // if yes, return EFI_SUCCESS
217 if (IsDevicePathEnd (RemainingDevicePath
)) {
218 Status
= EFI_SUCCESS
;
224 // Try to open BlockIO and BlockIO2. If BlockIO would be opened, continue,
225 // otherwise, return error.
227 Status
= gBS
->OpenProtocol (
229 &gEfiBlockIoProtocolGuid
,
231 This
->DriverBindingHandle
,
233 EFI_OPEN_PROTOCOL_GET_PROTOCOL
235 if (EFI_ERROR (Status
)) {
239 Status
= gBS
->OpenProtocol (
241 &gEfiBlockIo2ProtocolGuid
,
243 This
->DriverBindingHandle
,
245 EFI_OPEN_PROTOCOL_GET_PROTOCOL
247 if (EFI_ERROR (Status
)) {
252 // Get the Device Path Protocol on ControllerHandle's handle.
254 Status
= gBS
->OpenProtocol (
256 &gEfiDevicePathProtocolGuid
,
257 (VOID
**) &ParentDevicePath
,
258 This
->DriverBindingHandle
,
260 EFI_OPEN_PROTOCOL_BY_DRIVER
262 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
267 // Get the DiskIo and DiskIo2.
269 Status
= gBS
->OpenProtocol (
271 &gEfiDiskIoProtocolGuid
,
273 This
->DriverBindingHandle
,
275 EFI_OPEN_PROTOCOL_BY_DRIVER
277 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
280 &gEfiDevicePathProtocolGuid
,
281 This
->DriverBindingHandle
,
289 Status
= gBS
->OpenProtocol (
291 &gEfiDiskIo2ProtocolGuid
,
293 This
->DriverBindingHandle
,
295 EFI_OPEN_PROTOCOL_BY_DRIVER
297 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
302 // Try to read blocks when there's media or it is removable physical partition.
304 Status
= EFI_UNSUPPORTED
;
305 MediaPresent
= BlockIo
->Media
->MediaPresent
;
306 if (BlockIo
->Media
->MediaPresent
||
307 (BlockIo
->Media
->RemovableMedia
&& !BlockIo
->Media
->LogicalPartition
)) {
309 // Try for GPT, then El Torito, then UDF, and then legacy MBR partition
310 // types. If the media supports a given partition type install child handles
311 // to represent the partitions described by the media.
313 Routine
= &mPartitionDetectRoutineTable
[0];
314 while (*Routine
!= NULL
) {
315 Status
= (*Routine
) (
324 if (!EFI_ERROR (Status
) || Status
== EFI_MEDIA_CHANGED
|| Status
== EFI_NO_MEDIA
) {
331 // In the case that the driver is already started (OpenStatus == EFI_ALREADY_STARTED),
332 // the DevicePathProtocol and the DiskIoProtocol are not actually opened by the
333 // driver. So don't try to close them. Otherwise, we will break the dependency
334 // between the controller and the driver set up before.
336 // In the case that when the media changes on a device it will Reinstall the
337 // BlockIo interaface. This will cause a call to our Stop(), and a subsequent
338 // reentrant call to our Start() successfully. We should leave the device open
339 // when this happen. The "media change" case includes either the status is
340 // EFI_MEDIA_CHANGED or it is a "media" to "no media" change.
342 if (EFI_ERROR (Status
) &&
343 !EFI_ERROR (OpenStatus
) &&
344 Status
!= EFI_MEDIA_CHANGED
&&
345 !(MediaPresent
&& Status
== EFI_NO_MEDIA
)) {
348 &gEfiDiskIoProtocolGuid
,
349 This
->DriverBindingHandle
,
353 // Close Parent DiskIo2 if has.
357 &gEfiDiskIo2ProtocolGuid
,
358 This
->DriverBindingHandle
,
364 &gEfiDevicePathProtocolGuid
,
365 This
->DriverBindingHandle
,
371 gBS
->RestoreTPL (OldTpl
);
376 Stop this driver on ControllerHandle. Support stopping any child handles
377 created by this driver.
379 @param This Protocol instance pointer.
380 @param ControllerHandle Handle of device to stop driver on
381 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
382 children is zero stop the entire bus driver.
383 @param ChildHandleBuffer List of Child Handles to Stop.
385 @retval EFI_SUCCESS This driver is removed ControllerHandle
386 @retval other This driver was not removed from this device
391 PartitionDriverBindingStop (
392 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
393 IN EFI_HANDLE ControllerHandle
,
394 IN UINTN NumberOfChildren
,
395 IN EFI_HANDLE
*ChildHandleBuffer
400 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
401 EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
;
402 BOOLEAN AllChildrenStopped
;
403 PARTITION_PRIVATE_DATA
*Private
;
404 EFI_DISK_IO_PROTOCOL
*DiskIo
;
410 if (NumberOfChildren
== 0) {
412 // In the case of re-entry of the PartitionDriverBindingStop, the
413 // NumberOfChildren may not reflect the actual number of children on the
414 // bus driver. Hence, additional check is needed here.
416 if (HasChildren (ControllerHandle
)) {
417 DEBUG((EFI_D_ERROR
, "PartitionDriverBindingStop: Still has child.\n"));
418 return EFI_DEVICE_ERROR
;
422 // Close the bus driver
426 &gEfiDiskIoProtocolGuid
,
427 This
->DriverBindingHandle
,
431 // Close Parent BlockIO2 if has.
435 &gEfiDiskIo2ProtocolGuid
,
436 This
->DriverBindingHandle
,
442 &gEfiDevicePathProtocolGuid
,
443 This
->DriverBindingHandle
,
449 AllChildrenStopped
= TRUE
;
450 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
452 ChildHandleBuffer
[Index
],
453 &gEfiBlockIoProtocolGuid
,
455 This
->DriverBindingHandle
,
457 EFI_OPEN_PROTOCOL_GET_PROTOCOL
460 // Try to locate BlockIo2.
463 ChildHandleBuffer
[Index
],
464 &gEfiBlockIo2ProtocolGuid
,
466 This
->DriverBindingHandle
,
468 EFI_OPEN_PROTOCOL_GET_PROTOCOL
472 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo
);
473 if (Private
->InStop
) {
475 // If the child handle is going to be stopped again during the re-entry
476 // of DriverBindingStop, just do nothing.
480 Private
->InStop
= TRUE
;
482 BlockIo
->FlushBlocks (BlockIo
);
484 if (BlockIo2
!= NULL
) {
485 Status
= BlockIo2
->FlushBlocksEx (BlockIo2
, NULL
);
486 DEBUG((EFI_D_ERROR
, "PartitionDriverBindingStop: FlushBlocksEx returned with %r\n", Status
));
488 Status
= EFI_SUCCESS
;
493 &gEfiDiskIoProtocolGuid
,
494 This
->DriverBindingHandle
,
495 ChildHandleBuffer
[Index
]
498 // All Software protocols have be freed from the handle so remove it.
499 // Remove the BlockIo Protocol if has.
500 // Remove the BlockIo2 Protocol if has.
502 if (BlockIo2
!= NULL
) {
504 // Some device drivers might re-install the BlockIO(2) protocols for a
505 // media change condition. Therefore, if the FlushBlocksEx returned with
506 // EFI_MEDIA_CHANGED, just let the BindingStop fail to avoid potential
507 // reference of already stopped child handle.
509 if (Status
!= EFI_MEDIA_CHANGED
) {
510 Status
= gBS
->UninstallMultipleProtocolInterfaces (
511 ChildHandleBuffer
[Index
],
512 &gEfiDevicePathProtocolGuid
,
514 &gEfiBlockIoProtocolGuid
,
516 &gEfiBlockIo2ProtocolGuid
,
518 &gEfiPartitionInfoProtocolGuid
,
519 &Private
->PartitionInfo
,
526 Status
= gBS
->UninstallMultipleProtocolInterfaces (
527 ChildHandleBuffer
[Index
],
528 &gEfiDevicePathProtocolGuid
,
530 &gEfiBlockIoProtocolGuid
,
532 &gEfiPartitionInfoProtocolGuid
,
533 &Private
->PartitionInfo
,
540 if (EFI_ERROR (Status
)) {
541 Private
->InStop
= FALSE
;
544 &gEfiDiskIoProtocolGuid
,
546 This
->DriverBindingHandle
,
547 ChildHandleBuffer
[Index
],
548 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
551 FreePool (Private
->DevicePath
);
555 if (EFI_ERROR (Status
)) {
556 AllChildrenStopped
= FALSE
;
557 if (Status
== EFI_MEDIA_CHANGED
) {
563 if (!AllChildrenStopped
) {
564 return EFI_DEVICE_ERROR
;
572 Reset the Block Device.
574 @param This Protocol instance pointer.
575 @param ExtendedVerification Driver may perform diagnostics on reset.
577 @retval EFI_SUCCESS The device was reset.
578 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
585 IN EFI_BLOCK_IO_PROTOCOL
*This
,
586 IN BOOLEAN ExtendedVerification
589 PARTITION_PRIVATE_DATA
*Private
;
591 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
593 return Private
->ParentBlockIo
->Reset (
594 Private
->ParentBlockIo
,
600 Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED
601 for no media or media change case. Otherwise DefaultStatus is returned.
603 @param DiskIo Pointer to the DiskIo instance.
604 @param MediaId Id of the media, changes every time the media is replaced.
605 @param DefaultStatus The default status to return when it's not the no media
606 or media change case.
608 @retval EFI_NO_MEDIA There is no media.
609 @retval EFI_MEDIA_CHANGED The media was changed.
610 @retval others The default status to return.
614 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
616 IN EFI_STATUS DefaultStatus
623 // Read 1 byte from offset 0 to check if the MediaId is still valid.
624 // The reading operation is synchronious thus it is not worth it to
625 // allocate a buffer from the pool. The destination buffer for the
626 // data is in the stack.
628 Status
= DiskIo
->ReadDisk (DiskIo
, MediaId
, 0, 1, (VOID
*)Buffer
);
629 if ((Status
== EFI_NO_MEDIA
) || (Status
== EFI_MEDIA_CHANGED
)) {
632 return DefaultStatus
;
636 Read by using the Disk IO protocol on the parent device. Lba addresses
637 must be converted to byte offsets.
639 @param This Protocol instance pointer.
640 @param MediaId Id of the media, changes every time the media is replaced.
641 @param Lba The starting Logical Block Address to read from
642 @param BufferSize Size of Buffer, must be a multiple of device block size.
643 @param Buffer Buffer containing read data
645 @retval EFI_SUCCESS The data was read correctly from the device.
646 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
647 @retval EFI_NO_MEDIA There is no media in the device.
648 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
649 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
650 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
651 valid for the device.
656 PartitionReadBlocks (
657 IN EFI_BLOCK_IO_PROTOCOL
*This
,
664 PARTITION_PRIVATE_DATA
*Private
;
667 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
669 if (BufferSize
% Private
->BlockSize
!= 0) {
670 return ProbeMediaStatus (Private
->DiskIo
, MediaId
, EFI_BAD_BUFFER_SIZE
);
673 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
674 if (Offset
+ BufferSize
> Private
->End
) {
675 return ProbeMediaStatus (Private
->DiskIo
, MediaId
, EFI_INVALID_PARAMETER
);
678 // Because some kinds of partition have different block size from their parent
679 // device, we call the Disk IO protocol on the parent device, not the Block IO
682 return Private
->DiskIo
->ReadDisk (Private
->DiskIo
, MediaId
, Offset
, BufferSize
, Buffer
);
686 Write by using the Disk IO protocol on the parent device. Lba addresses
687 must be converted to byte offsets.
689 @param[in] This Protocol instance pointer.
690 @param[in] MediaId Id of the media, changes every time the media is replaced.
691 @param[in] Lba The starting Logical Block Address to read from
692 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
693 @param[in] Buffer Buffer containing data to be written to device.
695 @retval EFI_SUCCESS The data was written correctly to the device.
696 @retval EFI_WRITE_PROTECTED The device can not be written to.
697 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
698 @retval EFI_NO_MEDIA There is no media in the device.
699 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
700 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
701 @retval EFI_INVALID_PARAMETER The write request contains a LBA that is not
702 valid for the device.
707 PartitionWriteBlocks (
708 IN EFI_BLOCK_IO_PROTOCOL
*This
,
715 PARTITION_PRIVATE_DATA
*Private
;
718 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
720 if (BufferSize
% Private
->BlockSize
!= 0) {
721 return ProbeMediaStatus (Private
->DiskIo
, MediaId
, EFI_BAD_BUFFER_SIZE
);
724 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
725 if (Offset
+ BufferSize
> Private
->End
) {
726 return ProbeMediaStatus (Private
->DiskIo
, MediaId
, EFI_INVALID_PARAMETER
);
729 // Because some kinds of partition have different block size from their parent
730 // device, we call the Disk IO protocol on the parent device, not the Block IO
733 return Private
->DiskIo
->WriteDisk (Private
->DiskIo
, MediaId
, Offset
, BufferSize
, Buffer
);
738 Flush the parent Block Device.
740 @param This Protocol instance pointer.
742 @retval EFI_SUCCESS All outstanding data was written to the device
743 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
744 @retval EFI_NO_MEDIA There is no media in the device.
749 PartitionFlushBlocks (
750 IN EFI_BLOCK_IO_PROTOCOL
*This
753 PARTITION_PRIVATE_DATA
*Private
;
755 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
757 return Private
->ParentBlockIo
->FlushBlocks (Private
->ParentBlockIo
);
761 Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED
762 for no media or media change case. Otherwise DefaultStatus is returned.
764 @param DiskIo2 Pointer to the DiskIo2 instance.
765 @param MediaId Id of the media, changes every time the media is replaced.
766 @param DefaultStatus The default status to return when it's not the no media
767 or media change case.
769 @retval EFI_NO_MEDIA There is no media.
770 @retval EFI_MEDIA_CHANGED The media was changed.
771 @retval others The default status to return.
775 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
777 IN EFI_STATUS DefaultStatus
783 // Read 1 byte from offset 0 but passing NULL as buffer pointer
785 Status
= DiskIo2
->ReadDiskEx (DiskIo2
, MediaId
, 0, NULL
, 1, NULL
);
786 if ((Status
== EFI_NO_MEDIA
) || (Status
== EFI_MEDIA_CHANGED
)) {
789 return DefaultStatus
;
793 Reset the Block Device throught Block I/O2 protocol.
795 @param This Protocol instance pointer.
796 @param ExtendedVerification Driver may perform diagnostics on reset.
798 @retval EFI_SUCCESS The device was reset.
799 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
806 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
807 IN BOOLEAN ExtendedVerification
810 PARTITION_PRIVATE_DATA
*Private
;
812 Private
= PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This
);
814 return Private
->ParentBlockIo2
->Reset (
815 Private
->ParentBlockIo2
,
821 The general callback for the DiskIo2 interfaces.
822 @param Event Event whose notification function is being invoked.
823 @param Context The pointer to the notification function's context,
824 which points to the PARTITION_ACCESS_TASK instance.
828 PartitionOnAccessComplete (
833 PARTITION_ACCESS_TASK
*Task
;
835 Task
= (PARTITION_ACCESS_TASK
*) Context
;
837 gBS
->CloseEvent (Event
);
839 Task
->BlockIo2Token
->TransactionStatus
= Task
->DiskIo2Token
.TransactionStatus
;
840 gBS
->SignalEvent (Task
->BlockIo2Token
->Event
);
846 Create a new PARTITION_ACCESS_TASK instance.
848 @param Token Pointer to the EFI_BLOCK_IO2_TOKEN.
850 @return Pointer to the created PARTITION_ACCESS_TASK instance or NULL upon failure.
852 PARTITION_ACCESS_TASK
*
853 PartitionCreateAccessTask (
854 IN EFI_BLOCK_IO2_TOKEN
*Token
858 PARTITION_ACCESS_TASK
*Task
;
860 Task
= AllocatePool (sizeof (*Task
));
865 Status
= gBS
->CreateEvent (
868 PartitionOnAccessComplete
,
870 &Task
->DiskIo2Token
.Event
872 if (EFI_ERROR (Status
)) {
877 Task
->BlockIo2Token
= Token
;
883 Read BufferSize bytes from Lba into Buffer.
885 This function reads the requested number of blocks from the device. All the
886 blocks are read, or an error is returned.
887 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and
888 non-blocking I/O is being used, the Event associated with this request will
891 @param[in] This Indicates a pointer to the calling context.
892 @param[in] MediaId Id of the media, changes every time the media is
894 @param[in] Lba The starting Logical Block Address to read from.
895 @param[in, out] Token A pointer to the token associated with the transaction.
896 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
897 @param[out] Buffer A pointer to the destination buffer for the data. The
898 caller is responsible for either having implicit or
899 explicit ownership of the buffer.
901 @retval EFI_SUCCESS The read request was queued if Token->Event is
902 not NULL.The data was read correctly from the
903 device if the Token->Event is NULL.
904 @retval EFI_DEVICE_ERROR The device reported an error while performing
906 @retval EFI_NO_MEDIA There is no media in the device.
907 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
908 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
909 intrinsic block size of the device.
910 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
911 or the buffer is not on proper alignment.
912 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
917 PartitionReadBlocksEx (
918 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
921 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
927 PARTITION_PRIVATE_DATA
*Private
;
929 PARTITION_ACCESS_TASK
*Task
;
931 Private
= PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This
);
933 if (BufferSize
% Private
->BlockSize
!= 0) {
934 return ProbeMediaStatusEx (Private
->DiskIo2
, MediaId
, EFI_BAD_BUFFER_SIZE
);
937 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
938 if (Offset
+ BufferSize
> Private
->End
) {
939 return ProbeMediaStatusEx (Private
->DiskIo2
, MediaId
, EFI_INVALID_PARAMETER
);
942 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
943 Task
= PartitionCreateAccessTask (Token
);
945 return EFI_OUT_OF_RESOURCES
;
948 Status
= Private
->DiskIo2
->ReadDiskEx (Private
->DiskIo2
, MediaId
, Offset
, &Task
->DiskIo2Token
, BufferSize
, Buffer
);
949 if (EFI_ERROR (Status
)) {
950 gBS
->CloseEvent (Task
->DiskIo2Token
.Event
);
954 Status
= Private
->DiskIo2
->ReadDiskEx (Private
->DiskIo2
, MediaId
, Offset
, NULL
, BufferSize
, Buffer
);
961 Write BufferSize bytes from Lba into Buffer.
963 This function writes the requested number of blocks to the device. All blocks
964 are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,
965 EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is
966 being used, the Event associated with this request will not be signaled.
968 @param[in] This Indicates a pointer to the calling context.
969 @param[in] MediaId The media ID that the write request is for.
970 @param[in] Lba The starting logical block address to be written. The
971 caller is responsible for writing to only legitimate
973 @param[in, out] Token A pointer to the token associated with the transaction.
974 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
975 @param[in] Buffer A pointer to the source buffer for the data.
977 @retval EFI_SUCCESS The write request was queued if Event is not NULL.
978 The data was written correctly to the device if
980 @retval EFI_WRITE_PROTECTED The device can not be written to.
981 @retval EFI_NO_MEDIA There is no media in the device.
982 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
983 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
984 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
985 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
986 or the buffer is not on proper alignment.
987 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
993 PartitionWriteBlocksEx (
994 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
997 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
1003 PARTITION_PRIVATE_DATA
*Private
;
1005 PARTITION_ACCESS_TASK
*Task
;
1007 Private
= PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This
);
1009 if (BufferSize
% Private
->BlockSize
!= 0) {
1010 return ProbeMediaStatusEx (Private
->DiskIo2
, MediaId
, EFI_BAD_BUFFER_SIZE
);
1013 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
1014 if (Offset
+ BufferSize
> Private
->End
) {
1015 return ProbeMediaStatusEx (Private
->DiskIo2
, MediaId
, EFI_INVALID_PARAMETER
);
1018 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1019 Task
= PartitionCreateAccessTask (Token
);
1021 return EFI_OUT_OF_RESOURCES
;
1024 Status
= Private
->DiskIo2
->WriteDiskEx (Private
->DiskIo2
, MediaId
, Offset
, &Task
->DiskIo2Token
, BufferSize
, Buffer
);
1025 if (EFI_ERROR (Status
)) {
1026 gBS
->CloseEvent (Task
->DiskIo2Token
.Event
);
1030 Status
= Private
->DiskIo2
->WriteDiskEx (Private
->DiskIo2
, MediaId
, Offset
, NULL
, BufferSize
, Buffer
);
1036 Flush the Block Device.
1038 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED
1039 is returned and non-blocking I/O is being used, the Event associated with
1040 this request will not be signaled.
1042 @param[in] This Indicates a pointer to the calling context.
1043 @param[in, out] Token A pointer to the token associated with the transaction
1045 @retval EFI_SUCCESS The flush request was queued if Event is not NULL.
1046 All outstanding data was written correctly to the
1047 device if the Event is NULL.
1048 @retval EFI_DEVICE_ERROR The device reported an error while writting back
1050 @retval EFI_WRITE_PROTECTED The device cannot be written to.
1051 @retval EFI_NO_MEDIA There is no media in the device.
1052 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1053 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
1059 PartitionFlushBlocksEx (
1060 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1061 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
1065 PARTITION_PRIVATE_DATA
*Private
;
1066 PARTITION_ACCESS_TASK
*Task
;
1068 Private
= PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This
);
1070 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1071 Task
= PartitionCreateAccessTask (Token
);
1073 return EFI_OUT_OF_RESOURCES
;
1076 Status
= Private
->DiskIo2
->FlushDiskEx (Private
->DiskIo2
, &Task
->DiskIo2Token
);
1077 if (EFI_ERROR (Status
)) {
1078 gBS
->CloseEvent (Task
->DiskIo2Token
.Event
);
1082 Status
= Private
->DiskIo2
->FlushDiskEx (Private
->DiskIo2
, NULL
);
1089 Create a child handle for a logical block device that represents the
1090 bytes Start to End of the Parent Block IO device.
1092 @param[in] This Protocol instance pointer.
1093 @param[in] ParentHandle Parent Handle for new child.
1094 @param[in] ParentDiskIo Parent DiskIo interface.
1095 @param[in] ParentDiskIo2 Parent DiskIo2 interface.
1096 @param[in] ParentBlockIo Parent BlockIo interface.
1097 @param[in] ParentBlockIo2 Parent BlockIo2 interface.
1098 @param[in] ParentDevicePath Parent Device Path.
1099 @param[in] DevicePathNode Child Device Path node.
1100 @param[in] PartitionInfo Child Partition Information interface.
1101 @param[in] Start Start Block.
1102 @param[in] End End Block.
1103 @param[in] BlockSize Child block size.
1105 @retval EFI_SUCCESS A child handle was added.
1106 @retval other A child handle was not added.
1110 PartitionInstallChildHandle (
1111 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1112 IN EFI_HANDLE ParentHandle
,
1113 IN EFI_DISK_IO_PROTOCOL
*ParentDiskIo
,
1114 IN EFI_DISK_IO2_PROTOCOL
*ParentDiskIo2
,
1115 IN EFI_BLOCK_IO_PROTOCOL
*ParentBlockIo
,
1116 IN EFI_BLOCK_IO2_PROTOCOL
*ParentBlockIo2
,
1117 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1118 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
,
1119 IN EFI_PARTITION_INFO_PROTOCOL
*PartitionInfo
,
1126 PARTITION_PRIVATE_DATA
*Private
;
1128 Status
= EFI_SUCCESS
;
1129 Private
= AllocateZeroPool (sizeof (PARTITION_PRIVATE_DATA
));
1130 if (Private
== NULL
) {
1131 return EFI_OUT_OF_RESOURCES
;
1134 Private
->Signature
= PARTITION_PRIVATE_DATA_SIGNATURE
;
1136 Private
->Start
= MultU64x32 (Start
, ParentBlockIo
->Media
->BlockSize
);
1137 Private
->End
= MultU64x32 (End
+ 1, ParentBlockIo
->Media
->BlockSize
);
1139 Private
->BlockSize
= BlockSize
;
1140 Private
->ParentBlockIo
= ParentBlockIo
;
1141 Private
->ParentBlockIo2
= ParentBlockIo2
;
1142 Private
->DiskIo
= ParentDiskIo
;
1143 Private
->DiskIo2
= ParentDiskIo2
;
1146 // Set the BlockIO into Private Data.
1148 Private
->BlockIo
.Revision
= ParentBlockIo
->Revision
;
1150 Private
->BlockIo
.Media
= &Private
->Media
;
1151 CopyMem (Private
->BlockIo
.Media
, ParentBlockIo
->Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
1153 Private
->BlockIo
.Reset
= PartitionReset
;
1154 Private
->BlockIo
.ReadBlocks
= PartitionReadBlocks
;
1155 Private
->BlockIo
.WriteBlocks
= PartitionWriteBlocks
;
1156 Private
->BlockIo
.FlushBlocks
= PartitionFlushBlocks
;
1159 // Set the BlockIO2 into Private Data.
1161 if (Private
->DiskIo2
!= NULL
) {
1162 ASSERT (Private
->ParentBlockIo2
!= NULL
);
1163 Private
->BlockIo2
.Media
= &Private
->Media2
;
1164 CopyMem (Private
->BlockIo2
.Media
, ParentBlockIo2
->Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
1166 Private
->BlockIo2
.Reset
= PartitionResetEx
;
1167 Private
->BlockIo2
.ReadBlocksEx
= PartitionReadBlocksEx
;
1168 Private
->BlockIo2
.WriteBlocksEx
= PartitionWriteBlocksEx
;
1169 Private
->BlockIo2
.FlushBlocksEx
= PartitionFlushBlocksEx
;
1172 Private
->Media
.IoAlign
= 0;
1173 Private
->Media
.LogicalPartition
= TRUE
;
1174 Private
->Media
.LastBlock
= DivU64x32 (
1177 ParentBlockIo
->Media
->BlockSize
1182 Private
->Media
.BlockSize
= (UINT32
) BlockSize
;
1184 Private
->Media2
.IoAlign
= 0;
1185 Private
->Media2
.LogicalPartition
= TRUE
;
1186 Private
->Media2
.LastBlock
= Private
->Media
.LastBlock
;
1187 Private
->Media2
.BlockSize
= (UINT32
) BlockSize
;
1190 // Per UEFI Spec, LowestAlignedLba, LogicalBlocksPerPhysicalBlock and OptimalTransferLengthGranularity must be 0
1191 // for logical partitions.
1193 if (Private
->BlockIo
.Revision
>= EFI_BLOCK_IO_PROTOCOL_REVISION2
) {
1194 Private
->Media
.LowestAlignedLba
= 0;
1195 Private
->Media
.LogicalBlocksPerPhysicalBlock
= 0;
1196 Private
->Media2
.LowestAlignedLba
= 0;
1197 Private
->Media2
.LogicalBlocksPerPhysicalBlock
= 0;
1198 if (Private
->BlockIo
.Revision
>= EFI_BLOCK_IO_PROTOCOL_REVISION3
) {
1199 Private
->Media
.OptimalTransferLengthGranularity
= 0;
1200 Private
->Media2
.OptimalTransferLengthGranularity
= 0;
1204 Private
->DevicePath
= AppendDevicePathNode (ParentDevicePath
, DevicePathNode
);
1206 if (Private
->DevicePath
== NULL
) {
1208 return EFI_OUT_OF_RESOURCES
;
1212 // Set the PartitionInfo into Private Data.
1214 CopyMem (&Private
->PartitionInfo
, PartitionInfo
, sizeof (EFI_PARTITION_INFO_PROTOCOL
));
1216 if ((PartitionInfo
->System
== 1)) {
1217 Private
->EspGuid
= &gEfiPartTypeSystemPartGuid
;
1220 // If NULL InstallMultipleProtocolInterfaces will ignore it.
1222 Private
->EspGuid
= NULL
;
1226 // Create the new handle.
1228 Private
->Handle
= NULL
;
1229 if (Private
->DiskIo2
!= NULL
) {
1230 Status
= gBS
->InstallMultipleProtocolInterfaces (
1232 &gEfiDevicePathProtocolGuid
,
1233 Private
->DevicePath
,
1234 &gEfiBlockIoProtocolGuid
,
1236 &gEfiBlockIo2ProtocolGuid
,
1238 &gEfiPartitionInfoProtocolGuid
,
1239 &Private
->PartitionInfo
,
1245 Status
= gBS
->InstallMultipleProtocolInterfaces (
1247 &gEfiDevicePathProtocolGuid
,
1248 Private
->DevicePath
,
1249 &gEfiBlockIoProtocolGuid
,
1251 &gEfiPartitionInfoProtocolGuid
,
1252 &Private
->PartitionInfo
,
1259 if (!EFI_ERROR (Status
)) {
1261 // Open the Parent Handle for the child
1263 Status
= gBS
->OpenProtocol (
1265 &gEfiDiskIoProtocolGuid
,
1266 (VOID
**) &ParentDiskIo
,
1267 This
->DriverBindingHandle
,
1269 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1272 FreePool (Private
->DevicePath
);
1281 The user Entry Point for module Partition. The user code starts with this function.
1283 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1284 @param[in] SystemTable A pointer to the EFI System Table.
1286 @retval EFI_SUCCESS The entry point is executed successfully.
1287 @retval other Some error occurs when executing this entry point.
1292 InitializePartition (
1293 IN EFI_HANDLE ImageHandle
,
1294 IN EFI_SYSTEM_TABLE
*SystemTable
1300 // Install driver model protocol(s).
1302 Status
= EfiLibInstallDriverBindingComponentName2 (
1305 &gPartitionDriverBinding
,
1307 &gPartitionComponentName
,
1308 &gPartitionComponentName2
1310 ASSERT_EFI_ERROR (Status
);
1318 Test to see if there is any child on ControllerHandle.
1320 @param[in] ControllerHandle Handle of device to test.
1322 @retval TRUE There are children on the ControllerHandle.
1323 @retval FALSE No child is on the ControllerHandle.
1328 IN EFI_HANDLE ControllerHandle
1331 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
1336 Status
= gBS
->OpenProtocolInformation (
1338 &gEfiDiskIoProtocolGuid
,
1342 ASSERT_EFI_ERROR (Status
);
1344 for (Index
= 0; Index
< EntryCount
; Index
++) {
1345 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
1349 FreePool (OpenInfoBuffer
);
1351 return (BOOLEAN
) (Index
< EntryCount
);