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) 2018 Qualcomm Datacenter Technologies, Inc.
8 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include "Partition.h"
17 // Partition Driver Global Variables.
19 EFI_DRIVER_BINDING_PROTOCOL gPartitionDriverBinding
= {
20 PartitionDriverBindingSupported
,
21 PartitionDriverBindingStart
,
22 PartitionDriverBindingStop
,
24 // Grub4Dos copies the BPB of the first partition to the MBR. If the
25 // DriverBindingStart() of the Fat driver gets run before that of Partition
26 // driver only the first partition can be recognized.
27 // Let the driver binding version of Partition driver be higher than that of
28 // Fat driver to make sure the DriverBindingStart() of the Partition driver
29 // gets run before that of Fat driver so that all the partitions can be recognized.
37 // Prioritized function list to detect partition table.
39 PARTITION_DETECT_ROUTINE mPartitionDetectRoutineTable
[] = {
40 PartitionInstallGptChildHandles
,
41 PartitionInstallMbrChildHandles
,
42 PartitionInstallUdfChildHandles
,
47 Test to see if this driver supports ControllerHandle. Any ControllerHandle
48 than contains a BlockIo and DiskIo protocol or a BlockIo2 protocol can be
51 @param[in] This Protocol instance pointer.
52 @param[in] ControllerHandle Handle of device to test.
53 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
56 @retval EFI_SUCCESS This driver supports this device
57 @retval EFI_ALREADY_STARTED This driver is already running on this device
58 @retval other This driver does not support this device
63 PartitionDriverBindingSupported (
64 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
65 IN EFI_HANDLE ControllerHandle
,
66 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
70 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
71 EFI_DISK_IO_PROTOCOL
*DiskIo
;
75 // Check RemainingDevicePath validation
77 if (RemainingDevicePath
!= NULL
) {
79 // Check if RemainingDevicePath is the End of Device Path Node,
80 // if yes, go on checking other conditions
82 if (!IsDevicePathEnd (RemainingDevicePath
)) {
84 // If RemainingDevicePath isn't the End of Device Path Node,
85 // check its validation
87 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
88 if (Node
->DevPath
.Type
!= MEDIA_DEVICE_PATH
||
89 Node
->DevPath
.SubType
!= MEDIA_HARDDRIVE_DP
||
90 DevicePathNodeLength (&Node
->DevPath
) != sizeof (HARDDRIVE_DEVICE_PATH
)) {
91 return EFI_UNSUPPORTED
;
97 // Open the IO Abstraction(s) needed to perform the supported test
99 Status
= gBS
->OpenProtocol (
101 &gEfiDiskIoProtocolGuid
,
103 This
->DriverBindingHandle
,
105 EFI_OPEN_PROTOCOL_BY_DRIVER
107 if (Status
== EFI_ALREADY_STARTED
) {
110 if (EFI_ERROR (Status
)) {
114 // Close the I/O Abstraction(s) used to perform the supported test
118 &gEfiDiskIoProtocolGuid
,
119 This
->DriverBindingHandle
,
124 // Open the EFI Device Path protocol needed to perform the supported test
126 Status
= gBS
->OpenProtocol (
128 &gEfiDevicePathProtocolGuid
,
129 (VOID
**) &ParentDevicePath
,
130 This
->DriverBindingHandle
,
132 EFI_OPEN_PROTOCOL_BY_DRIVER
134 if (Status
== EFI_ALREADY_STARTED
) {
138 if (EFI_ERROR (Status
)) {
143 // Close protocol, don't use device path protocol in the Support() function
147 &gEfiDevicePathProtocolGuid
,
148 This
->DriverBindingHandle
,
153 // Open the IO Abstraction(s) needed to perform the supported test
155 Status
= gBS
->OpenProtocol (
157 &gEfiBlockIoProtocolGuid
,
159 This
->DriverBindingHandle
,
161 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
168 Start this driver on ControllerHandle by opening a Block IO or a Block IO2
169 or both, and Disk IO protocol, reading Device Path, and creating a child
170 handle with a Disk IO and device path protocol.
172 @param[in] This Protocol instance pointer.
173 @param[in] ControllerHandle Handle of device to bind driver to
174 @param[in] RemainingDevicePath Optional parameter use to pick a specific child
177 @retval EFI_SUCCESS This driver is added to ControllerHandle
178 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
179 @retval other This driver does not support this device
184 PartitionDriverBindingStart (
185 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
186 IN EFI_HANDLE ControllerHandle
,
187 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
191 EFI_STATUS OpenStatus
;
192 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
193 EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
;
194 EFI_DISK_IO_PROTOCOL
*DiskIo
;
195 EFI_DISK_IO2_PROTOCOL
*DiskIo2
;
196 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
197 PARTITION_DETECT_ROUTINE
*Routine
;
198 BOOLEAN MediaPresent
;
202 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
204 // Check RemainingDevicePath validation
206 if (RemainingDevicePath
!= NULL
) {
208 // Check if RemainingDevicePath is the End of Device Path Node,
209 // if yes, return EFI_SUCCESS
211 if (IsDevicePathEnd (RemainingDevicePath
)) {
212 Status
= EFI_SUCCESS
;
218 // Try to open BlockIO and BlockIO2. If BlockIO would be opened, continue,
219 // otherwise, return error.
221 Status
= gBS
->OpenProtocol (
223 &gEfiBlockIoProtocolGuid
,
225 This
->DriverBindingHandle
,
227 EFI_OPEN_PROTOCOL_GET_PROTOCOL
229 if (EFI_ERROR (Status
)) {
233 Status
= gBS
->OpenProtocol (
235 &gEfiBlockIo2ProtocolGuid
,
237 This
->DriverBindingHandle
,
239 EFI_OPEN_PROTOCOL_GET_PROTOCOL
241 if (EFI_ERROR (Status
)) {
246 // Get the Device Path Protocol on ControllerHandle's handle.
248 Status
= gBS
->OpenProtocol (
250 &gEfiDevicePathProtocolGuid
,
251 (VOID
**) &ParentDevicePath
,
252 This
->DriverBindingHandle
,
254 EFI_OPEN_PROTOCOL_BY_DRIVER
256 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
261 // Get the DiskIo and DiskIo2.
263 Status
= gBS
->OpenProtocol (
265 &gEfiDiskIoProtocolGuid
,
267 This
->DriverBindingHandle
,
269 EFI_OPEN_PROTOCOL_BY_DRIVER
271 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
274 &gEfiDevicePathProtocolGuid
,
275 This
->DriverBindingHandle
,
283 Status
= gBS
->OpenProtocol (
285 &gEfiDiskIo2ProtocolGuid
,
287 This
->DriverBindingHandle
,
289 EFI_OPEN_PROTOCOL_BY_DRIVER
291 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
296 // Try to read blocks when there's media or it is removable physical partition.
298 Status
= EFI_UNSUPPORTED
;
299 MediaPresent
= BlockIo
->Media
->MediaPresent
;
300 if (BlockIo
->Media
->MediaPresent
||
301 (BlockIo
->Media
->RemovableMedia
&& !BlockIo
->Media
->LogicalPartition
)) {
303 // Try for GPT, then legacy MBR partition types, and then UDF and El Torito.
304 // If the media supports a given partition type install child handles to
305 // represent the partitions described by the media.
307 Routine
= &mPartitionDetectRoutineTable
[0];
308 while (*Routine
!= NULL
) {
309 Status
= (*Routine
) (
318 if (!EFI_ERROR (Status
) || Status
== EFI_MEDIA_CHANGED
|| Status
== EFI_NO_MEDIA
) {
325 // In the case that the driver is already started (OpenStatus == EFI_ALREADY_STARTED),
326 // the DevicePathProtocol and the DiskIoProtocol are not actually opened by the
327 // driver. So don't try to close them. Otherwise, we will break the dependency
328 // between the controller and the driver set up before.
330 // In the case that when the media changes on a device it will Reinstall the
331 // BlockIo interaface. This will cause a call to our Stop(), and a subsequent
332 // reentrant call to our Start() successfully. We should leave the device open
333 // when this happen. The "media change" case includes either the status is
334 // EFI_MEDIA_CHANGED or it is a "media" to "no media" change.
336 if (EFI_ERROR (Status
) &&
337 !EFI_ERROR (OpenStatus
) &&
338 Status
!= EFI_MEDIA_CHANGED
&&
339 !(MediaPresent
&& Status
== EFI_NO_MEDIA
)) {
342 &gEfiDiskIoProtocolGuid
,
343 This
->DriverBindingHandle
,
347 // Close Parent DiskIo2 if has.
351 &gEfiDiskIo2ProtocolGuid
,
352 This
->DriverBindingHandle
,
358 &gEfiDevicePathProtocolGuid
,
359 This
->DriverBindingHandle
,
365 gBS
->RestoreTPL (OldTpl
);
370 Stop this driver on ControllerHandle. Support stopping any child handles
371 created by this driver.
373 @param This Protocol instance pointer.
374 @param ControllerHandle Handle of device to stop driver on
375 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
376 children is zero stop the entire bus driver.
377 @param ChildHandleBuffer List of Child Handles to Stop.
379 @retval EFI_SUCCESS This driver is removed ControllerHandle
380 @retval other This driver was not removed from this device
385 PartitionDriverBindingStop (
386 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
387 IN EFI_HANDLE ControllerHandle
,
388 IN UINTN NumberOfChildren
,
389 IN EFI_HANDLE
*ChildHandleBuffer
394 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
395 EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
;
396 BOOLEAN AllChildrenStopped
;
397 PARTITION_PRIVATE_DATA
*Private
;
398 EFI_DISK_IO_PROTOCOL
*DiskIo
;
405 if (NumberOfChildren
== 0) {
407 // In the case of re-entry of the PartitionDriverBindingStop, the
408 // NumberOfChildren may not reflect the actual number of children on the
409 // bus driver. Hence, additional check is needed here.
411 if (HasChildren (ControllerHandle
)) {
412 DEBUG((EFI_D_ERROR
, "PartitionDriverBindingStop: Still has child.\n"));
413 return EFI_DEVICE_ERROR
;
417 // Close the bus driver
421 &gEfiDiskIoProtocolGuid
,
422 This
->DriverBindingHandle
,
426 // Close Parent BlockIO2 if has.
430 &gEfiDiskIo2ProtocolGuid
,
431 This
->DriverBindingHandle
,
437 &gEfiDevicePathProtocolGuid
,
438 This
->DriverBindingHandle
,
444 AllChildrenStopped
= TRUE
;
445 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
447 ChildHandleBuffer
[Index
],
448 &gEfiBlockIoProtocolGuid
,
450 This
->DriverBindingHandle
,
452 EFI_OPEN_PROTOCOL_GET_PROTOCOL
455 // Try to locate BlockIo2.
458 ChildHandleBuffer
[Index
],
459 &gEfiBlockIo2ProtocolGuid
,
461 This
->DriverBindingHandle
,
463 EFI_OPEN_PROTOCOL_GET_PROTOCOL
467 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo
);
468 if (Private
->InStop
) {
470 // If the child handle is going to be stopped again during the re-entry
471 // of DriverBindingStop, just do nothing.
475 Private
->InStop
= TRUE
;
477 BlockIo
->FlushBlocks (BlockIo
);
479 if (BlockIo2
!= NULL
) {
480 Status
= BlockIo2
->FlushBlocksEx (BlockIo2
, NULL
);
481 DEBUG((EFI_D_ERROR
, "PartitionDriverBindingStop: FlushBlocksEx returned with %r\n", Status
));
483 Status
= EFI_SUCCESS
;
488 &gEfiDiskIoProtocolGuid
,
489 This
->DriverBindingHandle
,
490 ChildHandleBuffer
[Index
]
493 if (IsZeroGuid (&Private
->TypeGuid
)) {
496 TypeGuid
= &Private
->TypeGuid
;
500 // All Software protocols have be freed from the handle so remove it.
501 // Remove the BlockIo Protocol if has.
502 // Remove the BlockIo2 Protocol if has.
504 if (BlockIo2
!= NULL
) {
506 // Some device drivers might re-install the BlockIO(2) protocols for a
507 // media change condition. Therefore, if the FlushBlocksEx returned with
508 // EFI_MEDIA_CHANGED, just let the BindingStop fail to avoid potential
509 // reference of already stopped child handle.
511 if (Status
!= EFI_MEDIA_CHANGED
) {
512 Status
= gBS
->UninstallMultipleProtocolInterfaces (
513 ChildHandleBuffer
[Index
],
514 &gEfiDevicePathProtocolGuid
,
516 &gEfiBlockIoProtocolGuid
,
518 &gEfiBlockIo2ProtocolGuid
,
520 &gEfiPartitionInfoProtocolGuid
,
521 &Private
->PartitionInfo
,
528 Status
= gBS
->UninstallMultipleProtocolInterfaces (
529 ChildHandleBuffer
[Index
],
530 &gEfiDevicePathProtocolGuid
,
532 &gEfiBlockIoProtocolGuid
,
534 &gEfiPartitionInfoProtocolGuid
,
535 &Private
->PartitionInfo
,
542 if (EFI_ERROR (Status
)) {
543 Private
->InStop
= FALSE
;
546 &gEfiDiskIoProtocolGuid
,
548 This
->DriverBindingHandle
,
549 ChildHandleBuffer
[Index
],
550 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
553 FreePool (Private
->DevicePath
);
557 if (EFI_ERROR (Status
)) {
558 AllChildrenStopped
= FALSE
;
559 if (Status
== EFI_MEDIA_CHANGED
) {
565 if (!AllChildrenStopped
) {
566 return EFI_DEVICE_ERROR
;
574 Reset the Block Device.
576 @param This Protocol instance pointer.
577 @param ExtendedVerification Driver may perform diagnostics on reset.
579 @retval EFI_SUCCESS The device was reset.
580 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
587 IN EFI_BLOCK_IO_PROTOCOL
*This
,
588 IN BOOLEAN ExtendedVerification
591 PARTITION_PRIVATE_DATA
*Private
;
593 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
595 return Private
->ParentBlockIo
->Reset (
596 Private
->ParentBlockIo
,
602 Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED
603 for no media or media change case. Otherwise DefaultStatus is returned.
605 @param DiskIo Pointer to the DiskIo instance.
606 @param MediaId Id of the media, changes every time the media is replaced.
607 @param DefaultStatus The default status to return when it's not the no media
608 or media change case.
610 @retval EFI_NO_MEDIA There is no media.
611 @retval EFI_MEDIA_CHANGED The media was changed.
612 @retval others The default status to return.
616 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
618 IN EFI_STATUS DefaultStatus
625 // Read 1 byte from offset 0 to check if the MediaId is still valid.
626 // The reading operation is synchronious thus it is not worth it to
627 // allocate a buffer from the pool. The destination buffer for the
628 // data is in the stack.
630 Status
= DiskIo
->ReadDisk (DiskIo
, MediaId
, 0, 1, (VOID
*)Buffer
);
631 if ((Status
== EFI_NO_MEDIA
) || (Status
== EFI_MEDIA_CHANGED
)) {
634 return DefaultStatus
;
638 Read by using the Disk IO protocol on the parent device. Lba addresses
639 must be converted to byte offsets.
641 @param This Protocol instance pointer.
642 @param MediaId Id of the media, changes every time the media is replaced.
643 @param Lba The starting Logical Block Address to read from
644 @param BufferSize Size of Buffer, must be a multiple of device block size.
645 @param Buffer Buffer containing read data
647 @retval EFI_SUCCESS The data was read correctly from the device.
648 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
649 @retval EFI_NO_MEDIA There is no media in the device.
650 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
651 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
652 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
653 valid for the device.
658 PartitionReadBlocks (
659 IN EFI_BLOCK_IO_PROTOCOL
*This
,
666 PARTITION_PRIVATE_DATA
*Private
;
669 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
671 if (BufferSize
% Private
->BlockSize
!= 0) {
672 return ProbeMediaStatus (Private
->DiskIo
, MediaId
, EFI_BAD_BUFFER_SIZE
);
675 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
676 if (Offset
+ BufferSize
> Private
->End
) {
677 return ProbeMediaStatus (Private
->DiskIo
, MediaId
, EFI_INVALID_PARAMETER
);
680 // Because some kinds of partition have different block size from their parent
681 // device, we call the Disk IO protocol on the parent device, not the Block IO
684 return Private
->DiskIo
->ReadDisk (Private
->DiskIo
, MediaId
, Offset
, BufferSize
, Buffer
);
688 Write by using the Disk IO protocol on the parent device. Lba addresses
689 must be converted to byte offsets.
691 @param[in] This Protocol instance pointer.
692 @param[in] MediaId Id of the media, changes every time the media is replaced.
693 @param[in] Lba The starting Logical Block Address to read from
694 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
695 @param[in] Buffer Buffer containing data to be written to device.
697 @retval EFI_SUCCESS The data was written correctly to the device.
698 @retval EFI_WRITE_PROTECTED The device can not be written to.
699 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
700 @retval EFI_NO_MEDIA There is no media in the device.
701 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
702 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
703 @retval EFI_INVALID_PARAMETER The write request contains a LBA that is not
704 valid for the device.
709 PartitionWriteBlocks (
710 IN EFI_BLOCK_IO_PROTOCOL
*This
,
717 PARTITION_PRIVATE_DATA
*Private
;
720 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
722 if (BufferSize
% Private
->BlockSize
!= 0) {
723 return ProbeMediaStatus (Private
->DiskIo
, MediaId
, EFI_BAD_BUFFER_SIZE
);
726 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
727 if (Offset
+ BufferSize
> Private
->End
) {
728 return ProbeMediaStatus (Private
->DiskIo
, MediaId
, EFI_INVALID_PARAMETER
);
731 // Because some kinds of partition have different block size from their parent
732 // device, we call the Disk IO protocol on the parent device, not the Block IO
735 return Private
->DiskIo
->WriteDisk (Private
->DiskIo
, MediaId
, Offset
, BufferSize
, Buffer
);
740 Flush the parent Block Device.
742 @param This Protocol instance pointer.
744 @retval EFI_SUCCESS All outstanding data was written to the device
745 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
746 @retval EFI_NO_MEDIA There is no media in the device.
751 PartitionFlushBlocks (
752 IN EFI_BLOCK_IO_PROTOCOL
*This
755 PARTITION_PRIVATE_DATA
*Private
;
757 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
759 return Private
->ParentBlockIo
->FlushBlocks (Private
->ParentBlockIo
);
763 Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED
764 for no media or media change case. Otherwise DefaultStatus is returned.
766 @param DiskIo2 Pointer to the DiskIo2 instance.
767 @param MediaId Id of the media, changes every time the media is replaced.
768 @param DefaultStatus The default status to return when it's not the no media
769 or media change case.
771 @retval EFI_NO_MEDIA There is no media.
772 @retval EFI_MEDIA_CHANGED The media was changed.
773 @retval others The default status to return.
777 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
779 IN EFI_STATUS DefaultStatus
786 // Read 1 byte from offset 0 to check if the MediaId is still valid.
787 // The reading operation is synchronious thus it is not worth it to
788 // allocate a buffer from the pool. The destination buffer for the
789 // data is in the stack.
791 Status
= DiskIo2
->ReadDiskEx (DiskIo2
, MediaId
, 0, NULL
, 1, (VOID
*)Buffer
);
792 if ((Status
== EFI_NO_MEDIA
) || (Status
== EFI_MEDIA_CHANGED
)) {
795 return DefaultStatus
;
799 Reset the Block Device throught Block I/O2 protocol.
801 @param This Protocol instance pointer.
802 @param ExtendedVerification Driver may perform diagnostics on reset.
804 @retval EFI_SUCCESS The device was reset.
805 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
812 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
813 IN BOOLEAN ExtendedVerification
816 PARTITION_PRIVATE_DATA
*Private
;
818 Private
= PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This
);
820 return Private
->ParentBlockIo2
->Reset (
821 Private
->ParentBlockIo2
,
827 The general callback for the DiskIo2 interfaces.
828 @param Event Event whose notification function is being invoked.
829 @param Context The pointer to the notification function's context,
830 which points to the PARTITION_ACCESS_TASK instance.
834 PartitionOnAccessComplete (
839 PARTITION_ACCESS_TASK
*Task
;
841 Task
= (PARTITION_ACCESS_TASK
*) Context
;
843 gBS
->CloseEvent (Event
);
845 Task
->BlockIo2Token
->TransactionStatus
= Task
->DiskIo2Token
.TransactionStatus
;
846 gBS
->SignalEvent (Task
->BlockIo2Token
->Event
);
852 Create a new PARTITION_ACCESS_TASK instance.
854 @param Token Pointer to the EFI_BLOCK_IO2_TOKEN.
856 @return Pointer to the created PARTITION_ACCESS_TASK instance or NULL upon failure.
858 PARTITION_ACCESS_TASK
*
859 PartitionCreateAccessTask (
860 IN EFI_BLOCK_IO2_TOKEN
*Token
864 PARTITION_ACCESS_TASK
*Task
;
866 Task
= AllocatePool (sizeof (*Task
));
871 Status
= gBS
->CreateEvent (
874 PartitionOnAccessComplete
,
876 &Task
->DiskIo2Token
.Event
878 if (EFI_ERROR (Status
)) {
883 Task
->BlockIo2Token
= Token
;
889 Read BufferSize bytes from Lba into Buffer.
891 This function reads the requested number of blocks from the device. All the
892 blocks are read, or an error is returned.
893 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and
894 non-blocking I/O is being used, the Event associated with this request will
897 @param[in] This Indicates a pointer to the calling context.
898 @param[in] MediaId Id of the media, changes every time the media is
900 @param[in] Lba The starting Logical Block Address to read from.
901 @param[in, out] Token A pointer to the token associated with the transaction.
902 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
903 @param[out] Buffer A pointer to the destination buffer for the data. The
904 caller is responsible for either having implicit or
905 explicit ownership of the buffer.
907 @retval EFI_SUCCESS The read request was queued if Token->Event is
908 not NULL.The data was read correctly from the
909 device if the Token->Event is NULL.
910 @retval EFI_DEVICE_ERROR The device reported an error while performing
912 @retval EFI_NO_MEDIA There is no media in the device.
913 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
914 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
915 intrinsic block size of the device.
916 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
917 or the buffer is not on proper alignment.
918 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
923 PartitionReadBlocksEx (
924 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
927 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
933 PARTITION_PRIVATE_DATA
*Private
;
935 PARTITION_ACCESS_TASK
*Task
;
937 Private
= PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This
);
939 if (BufferSize
% Private
->BlockSize
!= 0) {
940 return ProbeMediaStatusEx (Private
->DiskIo2
, MediaId
, EFI_BAD_BUFFER_SIZE
);
943 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
944 if (Offset
+ BufferSize
> Private
->End
) {
945 return ProbeMediaStatusEx (Private
->DiskIo2
, MediaId
, EFI_INVALID_PARAMETER
);
948 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
949 Task
= PartitionCreateAccessTask (Token
);
951 return EFI_OUT_OF_RESOURCES
;
954 Status
= Private
->DiskIo2
->ReadDiskEx (Private
->DiskIo2
, MediaId
, Offset
, &Task
->DiskIo2Token
, BufferSize
, Buffer
);
955 if (EFI_ERROR (Status
)) {
956 gBS
->CloseEvent (Task
->DiskIo2Token
.Event
);
960 Status
= Private
->DiskIo2
->ReadDiskEx (Private
->DiskIo2
, MediaId
, Offset
, NULL
, BufferSize
, Buffer
);
967 Write BufferSize bytes from Lba into Buffer.
969 This function writes the requested number of blocks to the device. All blocks
970 are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,
971 EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is
972 being used, the Event associated with this request will not be signaled.
974 @param[in] This Indicates a pointer to the calling context.
975 @param[in] MediaId The media ID that the write request is for.
976 @param[in] Lba The starting logical block address to be written. The
977 caller is responsible for writing to only legitimate
979 @param[in, out] Token A pointer to the token associated with the transaction.
980 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
981 @param[in] Buffer A pointer to the source buffer for the data.
983 @retval EFI_SUCCESS The write request was queued if Event is not NULL.
984 The data was written correctly to the device if
986 @retval EFI_WRITE_PROTECTED The device can not be written to.
987 @retval EFI_NO_MEDIA There is no media in the device.
988 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
989 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
990 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
991 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
992 or the buffer is not on proper alignment.
993 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
999 PartitionWriteBlocksEx (
1000 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1003 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
,
1004 IN UINTN BufferSize
,
1009 PARTITION_PRIVATE_DATA
*Private
;
1011 PARTITION_ACCESS_TASK
*Task
;
1013 Private
= PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This
);
1015 if (BufferSize
% Private
->BlockSize
!= 0) {
1016 return ProbeMediaStatusEx (Private
->DiskIo2
, MediaId
, EFI_BAD_BUFFER_SIZE
);
1019 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
1020 if (Offset
+ BufferSize
> Private
->End
) {
1021 return ProbeMediaStatusEx (Private
->DiskIo2
, MediaId
, EFI_INVALID_PARAMETER
);
1024 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1025 Task
= PartitionCreateAccessTask (Token
);
1027 return EFI_OUT_OF_RESOURCES
;
1030 Status
= Private
->DiskIo2
->WriteDiskEx (Private
->DiskIo2
, MediaId
, Offset
, &Task
->DiskIo2Token
, BufferSize
, Buffer
);
1031 if (EFI_ERROR (Status
)) {
1032 gBS
->CloseEvent (Task
->DiskIo2Token
.Event
);
1036 Status
= Private
->DiskIo2
->WriteDiskEx (Private
->DiskIo2
, MediaId
, Offset
, NULL
, BufferSize
, Buffer
);
1042 Flush the Block Device.
1044 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED
1045 is returned and non-blocking I/O is being used, the Event associated with
1046 this request will not be signaled.
1048 @param[in] This Indicates a pointer to the calling context.
1049 @param[in, out] Token A pointer to the token associated with the transaction
1051 @retval EFI_SUCCESS The flush request was queued if Event is not NULL.
1052 All outstanding data was written correctly to the
1053 device if the Event is NULL.
1054 @retval EFI_DEVICE_ERROR The device reported an error while writting back
1056 @retval EFI_WRITE_PROTECTED The device cannot be written to.
1057 @retval EFI_NO_MEDIA There is no media in the device.
1058 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1059 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
1065 PartitionFlushBlocksEx (
1066 IN EFI_BLOCK_IO2_PROTOCOL
*This
,
1067 IN OUT EFI_BLOCK_IO2_TOKEN
*Token
1071 PARTITION_PRIVATE_DATA
*Private
;
1072 PARTITION_ACCESS_TASK
*Task
;
1074 Private
= PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This
);
1076 if ((Token
!= NULL
) && (Token
->Event
!= NULL
)) {
1077 Task
= PartitionCreateAccessTask (Token
);
1079 return EFI_OUT_OF_RESOURCES
;
1082 Status
= Private
->DiskIo2
->FlushDiskEx (Private
->DiskIo2
, &Task
->DiskIo2Token
);
1083 if (EFI_ERROR (Status
)) {
1084 gBS
->CloseEvent (Task
->DiskIo2Token
.Event
);
1088 Status
= Private
->DiskIo2
->FlushDiskEx (Private
->DiskIo2
, NULL
);
1095 Create a child handle for a logical block device that represents the
1096 bytes Start to End of the Parent Block IO device.
1098 @param[in] This Protocol instance pointer.
1099 @param[in] ParentHandle Parent Handle for new child.
1100 @param[in] ParentDiskIo Parent DiskIo interface.
1101 @param[in] ParentDiskIo2 Parent DiskIo2 interface.
1102 @param[in] ParentBlockIo Parent BlockIo interface.
1103 @param[in] ParentBlockIo2 Parent BlockIo2 interface.
1104 @param[in] ParentDevicePath Parent Device Path.
1105 @param[in] DevicePathNode Child Device Path node.
1106 @param[in] PartitionInfo Child Partition Information interface.
1107 @param[in] Start Start Block.
1108 @param[in] End End Block.
1109 @param[in] BlockSize Child block size.
1110 @param[in] TypeGuid Partition GUID Type.
1112 @retval EFI_SUCCESS A child handle was added.
1113 @retval other A child handle was not added.
1117 PartitionInstallChildHandle (
1118 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1119 IN EFI_HANDLE ParentHandle
,
1120 IN EFI_DISK_IO_PROTOCOL
*ParentDiskIo
,
1121 IN EFI_DISK_IO2_PROTOCOL
*ParentDiskIo2
,
1122 IN EFI_BLOCK_IO_PROTOCOL
*ParentBlockIo
,
1123 IN EFI_BLOCK_IO2_PROTOCOL
*ParentBlockIo2
,
1124 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1125 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
,
1126 IN EFI_PARTITION_INFO_PROTOCOL
*PartitionInfo
,
1129 IN UINT32 BlockSize
,
1130 IN EFI_GUID
*TypeGuid
1134 PARTITION_PRIVATE_DATA
*Private
;
1136 Status
= EFI_SUCCESS
;
1137 Private
= AllocateZeroPool (sizeof (PARTITION_PRIVATE_DATA
));
1138 if (Private
== NULL
) {
1139 return EFI_OUT_OF_RESOURCES
;
1142 Private
->Signature
= PARTITION_PRIVATE_DATA_SIGNATURE
;
1144 Private
->Start
= MultU64x32 (Start
, ParentBlockIo
->Media
->BlockSize
);
1145 Private
->End
= MultU64x32 (End
+ 1, ParentBlockIo
->Media
->BlockSize
);
1147 Private
->BlockSize
= BlockSize
;
1148 Private
->ParentBlockIo
= ParentBlockIo
;
1149 Private
->ParentBlockIo2
= ParentBlockIo2
;
1150 Private
->DiskIo
= ParentDiskIo
;
1151 Private
->DiskIo2
= ParentDiskIo2
;
1154 // Set the BlockIO into Private Data.
1156 Private
->BlockIo
.Revision
= ParentBlockIo
->Revision
;
1158 Private
->BlockIo
.Media
= &Private
->Media
;
1159 CopyMem (Private
->BlockIo
.Media
, ParentBlockIo
->Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
1161 Private
->BlockIo
.Reset
= PartitionReset
;
1162 Private
->BlockIo
.ReadBlocks
= PartitionReadBlocks
;
1163 Private
->BlockIo
.WriteBlocks
= PartitionWriteBlocks
;
1164 Private
->BlockIo
.FlushBlocks
= PartitionFlushBlocks
;
1167 // Set the BlockIO2 into Private Data.
1169 if (Private
->DiskIo2
!= NULL
) {
1170 ASSERT (Private
->ParentBlockIo2
!= NULL
);
1171 Private
->BlockIo2
.Media
= &Private
->Media2
;
1172 CopyMem (Private
->BlockIo2
.Media
, ParentBlockIo2
->Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
1174 Private
->BlockIo2
.Reset
= PartitionResetEx
;
1175 Private
->BlockIo2
.ReadBlocksEx
= PartitionReadBlocksEx
;
1176 Private
->BlockIo2
.WriteBlocksEx
= PartitionWriteBlocksEx
;
1177 Private
->BlockIo2
.FlushBlocksEx
= PartitionFlushBlocksEx
;
1180 Private
->Media
.IoAlign
= 0;
1181 Private
->Media
.LogicalPartition
= TRUE
;
1182 Private
->Media
.LastBlock
= DivU64x32 (
1185 ParentBlockIo
->Media
->BlockSize
1190 Private
->Media
.BlockSize
= (UINT32
) BlockSize
;
1192 Private
->Media2
.IoAlign
= 0;
1193 Private
->Media2
.LogicalPartition
= TRUE
;
1194 Private
->Media2
.LastBlock
= Private
->Media
.LastBlock
;
1195 Private
->Media2
.BlockSize
= (UINT32
) BlockSize
;
1198 // Per UEFI Spec, LowestAlignedLba, LogicalBlocksPerPhysicalBlock and OptimalTransferLengthGranularity must be 0
1199 // for logical partitions.
1201 if (Private
->BlockIo
.Revision
>= EFI_BLOCK_IO_PROTOCOL_REVISION2
) {
1202 Private
->Media
.LowestAlignedLba
= 0;
1203 Private
->Media
.LogicalBlocksPerPhysicalBlock
= 0;
1204 Private
->Media2
.LowestAlignedLba
= 0;
1205 Private
->Media2
.LogicalBlocksPerPhysicalBlock
= 0;
1206 if (Private
->BlockIo
.Revision
>= EFI_BLOCK_IO_PROTOCOL_REVISION3
) {
1207 Private
->Media
.OptimalTransferLengthGranularity
= 0;
1208 Private
->Media2
.OptimalTransferLengthGranularity
= 0;
1212 Private
->DevicePath
= AppendDevicePathNode (ParentDevicePath
, DevicePathNode
);
1214 if (Private
->DevicePath
== NULL
) {
1216 return EFI_OUT_OF_RESOURCES
;
1220 // Set the PartitionInfo into Private Data.
1222 CopyMem (&Private
->PartitionInfo
, PartitionInfo
, sizeof (EFI_PARTITION_INFO_PROTOCOL
));
1224 if (TypeGuid
!= NULL
) {
1225 CopyGuid(&(Private
->TypeGuid
), TypeGuid
);
1227 ZeroMem ((VOID
*)&(Private
->TypeGuid
), sizeof (EFI_GUID
));
1231 // Create the new handle.
1233 Private
->Handle
= NULL
;
1234 if (Private
->DiskIo2
!= NULL
) {
1235 Status
= gBS
->InstallMultipleProtocolInterfaces (
1237 &gEfiDevicePathProtocolGuid
,
1238 Private
->DevicePath
,
1239 &gEfiBlockIoProtocolGuid
,
1241 &gEfiBlockIo2ProtocolGuid
,
1243 &gEfiPartitionInfoProtocolGuid
,
1244 &Private
->PartitionInfo
,
1250 Status
= gBS
->InstallMultipleProtocolInterfaces (
1252 &gEfiDevicePathProtocolGuid
,
1253 Private
->DevicePath
,
1254 &gEfiBlockIoProtocolGuid
,
1256 &gEfiPartitionInfoProtocolGuid
,
1257 &Private
->PartitionInfo
,
1264 if (!EFI_ERROR (Status
)) {
1266 // Open the Parent Handle for the child
1268 Status
= gBS
->OpenProtocol (
1270 &gEfiDiskIoProtocolGuid
,
1271 (VOID
**) &ParentDiskIo
,
1272 This
->DriverBindingHandle
,
1274 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
1277 FreePool (Private
->DevicePath
);
1286 The user Entry Point for module Partition. The user code starts with this function.
1288 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1289 @param[in] SystemTable A pointer to the EFI System Table.
1291 @retval EFI_SUCCESS The entry point is executed successfully.
1292 @retval other Some error occurs when executing this entry point.
1297 InitializePartition (
1298 IN EFI_HANDLE ImageHandle
,
1299 IN EFI_SYSTEM_TABLE
*SystemTable
1305 // Install driver model protocol(s).
1307 Status
= EfiLibInstallDriverBindingComponentName2 (
1310 &gPartitionDriverBinding
,
1312 &gPartitionComponentName
,
1313 &gPartitionComponentName2
1315 ASSERT_EFI_ERROR (Status
);
1323 Test to see if there is any child on ControllerHandle.
1325 @param[in] ControllerHandle Handle of device to test.
1327 @retval TRUE There are children on the ControllerHandle.
1328 @retval FALSE No child is on the ControllerHandle.
1333 IN EFI_HANDLE ControllerHandle
1336 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
1341 Status
= gBS
->OpenProtocolInformation (
1343 &gEfiDiskIoProtocolGuid
,
1347 ASSERT_EFI_ERROR (Status
);
1349 for (Index
= 0; Index
< EntryCount
; Index
++) {
1350 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
1354 FreePool (OpenInfoBuffer
);
1356 return (BOOLEAN
) (Index
< EntryCount
);