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", Legacy
5 MBR, and GPT partition schemes are supported.
7 Copyright (c) 2006 - 2009, Intel Corporation. <BR>
8 All rights reserved. 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
,
34 // Prioritized function list to detect partition table.
36 PARTITION_DETECT_ROUTINE mPartitionDetectRoutineTable
[] = {
37 PartitionInstallGptChildHandles
,
38 PartitionInstallElToritoChildHandles
,
39 PartitionInstallMbrChildHandles
,
46 Test to see if this driver supports ControllerHandle. Any ControllerHandle
47 than contains a BlockIo and DiskIo protocol can be supported.
49 @param This Protocol instance pointer.
50 @param ControllerHandle Handle of device to test
51 @param RemainingDevicePath Optional parameter use to pick a specific child
54 @retval EFI_SUCCESS This driver supports this device
55 @retval EFI_ALREADY_STARTED This driver is already running on this device
56 @retval other This driver does not support this device
61 PartitionDriverBindingSupported (
62 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
63 IN EFI_HANDLE ControllerHandle
,
64 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
68 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
69 EFI_DISK_IO_PROTOCOL
*DiskIo
;
73 // Check RemainingDevicePath validation
75 if (RemainingDevicePath
!= NULL
) {
77 // Check if RemainingDevicePath is the End of Device Path Node,
78 // if yes, go on checking other conditions
80 if (!IsDevicePathEnd (RemainingDevicePath
)) {
82 // If RemainingDevicePath isn't the End of Device Path Node,
83 // check its validation
85 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
86 if (Node
->DevPath
.Type
!= MEDIA_DEVICE_PATH
||
87 Node
->DevPath
.SubType
!= MEDIA_HARDDRIVE_DP
||
88 DevicePathNodeLength (&Node
->DevPath
) != sizeof (HARDDRIVE_DEVICE_PATH
)) {
89 return EFI_UNSUPPORTED
;
95 // Open the IO Abstraction(s) needed to perform the supported test
97 Status
= gBS
->OpenProtocol (
99 &gEfiDiskIoProtocolGuid
,
101 This
->DriverBindingHandle
,
103 EFI_OPEN_PROTOCOL_BY_DRIVER
105 if (Status
== EFI_ALREADY_STARTED
) {
109 if (EFI_ERROR (Status
)) {
113 // Close the I/O Abstraction(s) used to perform the supported test
117 &gEfiDiskIoProtocolGuid
,
118 This
->DriverBindingHandle
,
123 // Open the EFI Device Path protocol needed to perform the supported test
125 Status
= gBS
->OpenProtocol (
127 &gEfiDevicePathProtocolGuid
,
128 (VOID
**) &ParentDevicePath
,
129 This
->DriverBindingHandle
,
131 EFI_OPEN_PROTOCOL_BY_DRIVER
133 if (Status
== EFI_ALREADY_STARTED
) {
137 if (EFI_ERROR (Status
)) {
142 // Close protocol, don't use device path protocol in the Support() function
146 &gEfiDevicePathProtocolGuid
,
147 This
->DriverBindingHandle
,
152 // Open the IO Abstraction(s) needed to perform the supported test
154 Status
= gBS
->OpenProtocol (
156 &gEfiBlockIoProtocolGuid
,
158 This
->DriverBindingHandle
,
160 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
168 Start this driver on ControllerHandle by opening a Block IO and Disk IO
169 protocol, reading Device Path, and creating a child handle with a
170 Disk IO and device path protocol.
172 @param This Protocol instance pointer.
173 @param ControllerHandle Handle of device to bind driver to
174 @param 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_DISK_IO_PROTOCOL
*DiskIo
;
194 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
195 PARTITION_DETECT_ROUTINE
*Routine
;
196 BOOLEAN MediaPresent
;
199 // Check RemainingDevicePath validation
201 if (RemainingDevicePath
!= NULL
) {
203 // Check if RemainingDevicePath is the End of Device Path Node,
204 // if yes, return EFI_SUCCESS
206 if (IsDevicePathEnd (RemainingDevicePath
)) {
211 Status
= gBS
->OpenProtocol (
213 &gEfiBlockIoProtocolGuid
,
215 This
->DriverBindingHandle
,
217 EFI_OPEN_PROTOCOL_GET_PROTOCOL
219 if (EFI_ERROR (Status
)) {
223 // Get the Device Path Protocol on ControllerHandle's handle
225 Status
= gBS
->OpenProtocol (
227 &gEfiDevicePathProtocolGuid
,
228 (VOID
**) &ParentDevicePath
,
229 This
->DriverBindingHandle
,
231 EFI_OPEN_PROTOCOL_BY_DRIVER
233 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
237 Status
= gBS
->OpenProtocol (
239 &gEfiDiskIoProtocolGuid
,
241 This
->DriverBindingHandle
,
243 EFI_OPEN_PROTOCOL_BY_DRIVER
245 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
248 &gEfiDevicePathProtocolGuid
,
249 This
->DriverBindingHandle
,
258 // Try to read blocks when there's media or it is removable physical partition.
260 Status
= EFI_UNSUPPORTED
;
261 MediaPresent
= BlockIo
->Media
->MediaPresent
;
262 if (BlockIo
->Media
->MediaPresent
||
263 (BlockIo
->Media
->RemovableMedia
&& !BlockIo
->Media
->LogicalPartition
)) {
265 // Try for GPT, then El Torito, and then legacy MBR partition types. If the
266 // media supports a given partition type install child handles to represent
267 // the partitions described by the media.
269 Routine
= &mPartitionDetectRoutineTable
[0];
270 while (*Routine
!= NULL
) {
271 Status
= (*Routine
) (
278 if (!EFI_ERROR (Status
) || Status
== EFI_MEDIA_CHANGED
|| Status
== EFI_NO_MEDIA
) {
285 // In the case that the driver is already started (OpenStatus == EFI_ALREADY_STARTED),
286 // the DevicePathProtocol and the DiskIoProtocol are not actually opened by the
287 // driver. So don't try to close them. Otherwise, we will break the dependency
288 // between the controller and the driver set up before.
290 // In the case that when the media changes on a device it will Reinstall the
291 // BlockIo interaface. This will cause a call to our Stop(), and a subsequent
292 // reentrant call to our Start() successfully. We should leave the device open
293 // when this happen. The "media change" case includes either the status is
294 // EFI_MEDIA_CHANGED or it is a "media" to "no media" change.
296 if (EFI_ERROR (Status
) &&
297 !EFI_ERROR (OpenStatus
) &&
298 Status
!= EFI_MEDIA_CHANGED
&&
299 !(MediaPresent
&& Status
== EFI_NO_MEDIA
)) {
302 &gEfiDiskIoProtocolGuid
,
303 This
->DriverBindingHandle
,
309 &gEfiDevicePathProtocolGuid
,
310 This
->DriverBindingHandle
,
320 Stop this driver on ControllerHandle. Support stopping any child handles
321 created by this driver.
323 @param This Protocol instance pointer.
324 @param ControllerHandle Handle of device to stop driver on
325 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
326 children is zero stop the entire bus driver.
327 @param ChildHandleBuffer List of Child Handles to Stop.
329 @retval EFI_SUCCESS This driver is removed ControllerHandle
330 @retval other This driver was not removed from this device
335 PartitionDriverBindingStop (
336 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
337 IN EFI_HANDLE ControllerHandle
,
338 IN UINTN NumberOfChildren
,
339 IN EFI_HANDLE
*ChildHandleBuffer
344 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
345 BOOLEAN AllChildrenStopped
;
346 PARTITION_PRIVATE_DATA
*Private
;
347 EFI_DISK_IO_PROTOCOL
*DiskIo
;
349 if (NumberOfChildren
== 0) {
351 // Close the bus driver
355 &gEfiDiskIoProtocolGuid
,
356 This
->DriverBindingHandle
,
362 &gEfiDevicePathProtocolGuid
,
363 This
->DriverBindingHandle
,
370 AllChildrenStopped
= TRUE
;
371 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
372 Status
= gBS
->OpenProtocol (
373 ChildHandleBuffer
[Index
],
374 &gEfiBlockIoProtocolGuid
,
376 This
->DriverBindingHandle
,
378 EFI_OPEN_PROTOCOL_GET_PROTOCOL
380 if (!EFI_ERROR (Status
)) {
382 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo
);
385 // All Software protocols have be freed from the handle so remove it.
387 BlockIo
->FlushBlocks (BlockIo
);
389 Status
= gBS
->CloseProtocol (
391 &gEfiDiskIoProtocolGuid
,
392 This
->DriverBindingHandle
,
393 ChildHandleBuffer
[Index
]
396 Status
= gBS
->UninstallMultipleProtocolInterfaces (
397 ChildHandleBuffer
[Index
],
398 &gEfiDevicePathProtocolGuid
,
400 &gEfiBlockIoProtocolGuid
,
406 if (EFI_ERROR (Status
)) {
409 &gEfiDiskIoProtocolGuid
,
411 This
->DriverBindingHandle
,
412 ChildHandleBuffer
[Index
],
413 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
416 FreePool (Private
->DevicePath
);
422 if (EFI_ERROR (Status
)) {
423 AllChildrenStopped
= FALSE
;
427 if (!AllChildrenStopped
) {
428 return EFI_DEVICE_ERROR
;
436 Reset the Block Device.
438 @param This Protocol instance pointer.
439 @param ExtendedVerification Driver may perform diagnostics on reset.
441 @retval EFI_SUCCESS The device was reset.
442 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
449 IN EFI_BLOCK_IO_PROTOCOL
*This
,
450 IN BOOLEAN ExtendedVerification
453 PARTITION_PRIVATE_DATA
*Private
;
455 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
457 return Private
->ParentBlockIo
->Reset (
458 Private
->ParentBlockIo
,
465 Read by using the Disk IO protocol on the parent device. Lba addresses
466 must be converted to byte offsets.
468 @param This Protocol instance pointer.
469 @param MediaId Id of the media, changes every time the media is replaced.
470 @param Lba The starting Logical Block Address to read from
471 @param BufferSize Size of Buffer, must be a multiple of device block size.
472 @param Buffer Buffer containing read data
474 @retval EFI_SUCCESS The data was read correctly from the device.
475 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
476 @retval EFI_NO_MEDIA There is no media in the device.
477 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
478 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
479 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
480 valid for the device.
485 PartitionReadBlocks (
486 IN EFI_BLOCK_IO_PROTOCOL
*This
,
493 PARTITION_PRIVATE_DATA
*Private
;
496 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
498 if (BufferSize
% Private
->BlockSize
!= 0) {
499 return EFI_BAD_BUFFER_SIZE
;
502 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
503 if (Offset
+ BufferSize
> Private
->End
) {
504 return EFI_INVALID_PARAMETER
;
507 // Because some kinds of partition have different block size from their parent
508 // device, we call the Disk IO protocol on the parent device, not the Block IO
511 return Private
->DiskIo
->ReadDisk (Private
->DiskIo
, MediaId
, Offset
, BufferSize
, Buffer
);
515 Write by using the Disk IO protocol on the parent device. Lba addresses
516 must be converted to byte offsets.
518 @param This Protocol instance pointer.
519 @param MediaId Id of the media, changes every time the media is replaced.
520 @param Lba The starting Logical Block Address to read from
521 @param BufferSize Size of Buffer, must be a multiple of device block size.
522 @param Buffer Buffer containing read data
524 @retval EFI_SUCCESS The data was written correctly to the device.
525 @retval EFI_WRITE_PROTECTED The device can not be written to.
526 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
527 @retval EFI_NO_MEDIA There is no media in the device.
528 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
529 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
530 @retval EFI_INVALID_PARAMETER The write request contains a LBA that is not
531 valid for the device.
536 PartitionWriteBlocks (
537 IN EFI_BLOCK_IO_PROTOCOL
*This
,
544 PARTITION_PRIVATE_DATA
*Private
;
547 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
549 if (BufferSize
% Private
->BlockSize
!= 0) {
550 return EFI_BAD_BUFFER_SIZE
;
553 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
554 if (Offset
+ BufferSize
> Private
->End
) {
555 return EFI_INVALID_PARAMETER
;
558 // Because some kinds of partition have different block size from their parent
559 // device, we call the Disk IO protocol on the parent device, not the Block IO
562 return Private
->DiskIo
->WriteDisk (Private
->DiskIo
, MediaId
, Offset
, BufferSize
, Buffer
);
567 Flush the parent Block Device.
569 @param This Protocol instance pointer.
571 @retval EFI_SUCCESS All outstanding data was written to the device
572 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
573 @retval EFI_NO_MEDIA There is no media in the device.
578 PartitionFlushBlocks (
579 IN EFI_BLOCK_IO_PROTOCOL
*This
582 PARTITION_PRIVATE_DATA
*Private
;
584 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
586 return Private
->ParentBlockIo
->FlushBlocks (Private
->ParentBlockIo
);
592 Create a child handle for a logical block device that represents the
593 bytes Start to End of the Parent Block IO device.
595 @param[in] This Protocol instance pointer
596 @param[in] ParentHandle Parent Handle for new child
597 @param[in] ParentDiskIo Parent DiskIo interface
598 @param[in] ParentBlockIo Parent BlockIo interface
599 @param[in] ParentDevicePath Parent Device Path
600 @param[in] DevicePathNode Child Device Path node
601 @param[in] Start Start Block
602 @param[in] End End Block
603 @param[in] BlockSize Child block size
604 @param[in] InstallEspGuid Flag to install EFI System Partition GUID on handle
606 @retval EFI_SUCCESS A child handle was added
607 @retval other A child handle was not added
611 PartitionInstallChildHandle (
612 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
613 IN EFI_HANDLE ParentHandle
,
614 IN EFI_DISK_IO_PROTOCOL
*ParentDiskIo
,
615 IN EFI_BLOCK_IO_PROTOCOL
*ParentBlockIo
,
616 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
617 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
,
621 IN BOOLEAN InstallEspGuid
625 PARTITION_PRIVATE_DATA
*Private
;
627 Private
= AllocateZeroPool (sizeof (PARTITION_PRIVATE_DATA
));
628 if (Private
== NULL
) {
629 return EFI_OUT_OF_RESOURCES
;
632 Private
->Signature
= PARTITION_PRIVATE_DATA_SIGNATURE
;
634 Private
->Start
= MultU64x32 (Start
, ParentBlockIo
->Media
->BlockSize
);
635 Private
->End
= MultU64x32 (End
+ 1, ParentBlockIo
->Media
->BlockSize
);
637 Private
->BlockSize
= BlockSize
;
638 Private
->ParentBlockIo
= ParentBlockIo
;
639 Private
->DiskIo
= ParentDiskIo
;
641 Private
->BlockIo
.Revision
= ParentBlockIo
->Revision
;
643 Private
->BlockIo
.Media
= &Private
->Media
;
644 CopyMem (Private
->BlockIo
.Media
, ParentBlockIo
->Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
645 Private
->Media
.LogicalPartition
= TRUE
;
646 Private
->Media
.LastBlock
= DivU64x32 (
649 ParentBlockIo
->Media
->BlockSize
654 Private
->Media
.BlockSize
= (UINT32
) BlockSize
;
656 Private
->BlockIo
.Reset
= PartitionReset
;
657 Private
->BlockIo
.ReadBlocks
= PartitionReadBlocks
;
658 Private
->BlockIo
.WriteBlocks
= PartitionWriteBlocks
;
659 Private
->BlockIo
.FlushBlocks
= PartitionFlushBlocks
;
661 Private
->DevicePath
= AppendDevicePathNode (ParentDevicePath
, DevicePathNode
);
663 if (Private
->DevicePath
== NULL
) {
665 return EFI_OUT_OF_RESOURCES
;
668 if (InstallEspGuid
) {
669 Private
->EspGuid
= &gEfiPartTypeSystemPartGuid
;
672 // If NULL InstallMultipleProtocolInterfaces will ignore it.
674 Private
->EspGuid
= NULL
;
677 // Create the new handle
679 Private
->Handle
= NULL
;
680 Status
= gBS
->InstallMultipleProtocolInterfaces (
682 &gEfiDevicePathProtocolGuid
,
684 &gEfiBlockIoProtocolGuid
,
691 if (!EFI_ERROR (Status
)) {
693 // Open the Parent Handle for the child
695 Status
= gBS
->OpenProtocol (
697 &gEfiDiskIoProtocolGuid
,
698 (VOID
**) &ParentDiskIo
,
699 This
->DriverBindingHandle
,
701 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
704 FreePool (Private
->DevicePath
);
713 The user Entry Point for module Partition. The user code starts with this function.
715 @param[in] ImageHandle The firmware allocated handle for the EFI image.
716 @param[in] SystemTable A pointer to the EFI System Table.
718 @retval EFI_SUCCESS The entry point is executed successfully.
719 @retval other Some error occurs when executing this entry point.
724 InitializePartition (
725 IN EFI_HANDLE ImageHandle
,
726 IN EFI_SYSTEM_TABLE
*SystemTable
732 // Install driver model protocol(s).
734 Status
= EfiLibInstallDriverBindingComponentName2 (
737 &gPartitionDriverBinding
,
739 &gPartitionComponentName
,
740 &gPartitionComponentName2
742 ASSERT_EFI_ERROR (Status
);