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 - 2007, Intel Corporation
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 PARTITION_DETECT_ROUTINE mPartitionDetectRoutineTable
[] = {
35 PartitionInstallGptChildHandles
,
36 PartitionInstallElToritoChildHandles
,
37 PartitionInstallMbrChildHandles
,
44 Test to see if this driver supports ControllerHandle. Any ControllerHandle
45 than contains a BlockIo and DiskIo protocol can be supported.
47 @param This Protocol instance pointer.
48 @param ControllerHandle Handle of device to test
49 @param RemainingDevicePath Optional parameter use to pick a specific child
52 @retval EFI_SUCCESS This driver supports this device
53 @retval EFI_ALREADY_STARTED This driver is already running on this device
54 @retval other This driver does not support this device
59 PartitionDriverBindingSupported (
60 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
61 IN EFI_HANDLE ControllerHandle
,
62 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
66 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
67 EFI_DISK_IO_PROTOCOL
*DiskIo
;
70 if (RemainingDevicePath
!= NULL
) {
71 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
72 if (Node
->DevPath
.Type
!= MEDIA_DEVICE_PATH
||
73 Node
->DevPath
.SubType
!= MEDIA_HARDDRIVE_DP
||
74 DevicePathNodeLength (&Node
->DevPath
) != sizeof (HARDDRIVE_DEVICE_PATH
)
76 return EFI_UNSUPPORTED
;
80 // Open the IO Abstraction(s) needed to perform the supported test
82 Status
= gBS
->OpenProtocol (
84 &gEfiDevicePathProtocolGuid
,
85 (VOID
**) &ParentDevicePath
,
86 This
->DriverBindingHandle
,
88 EFI_OPEN_PROTOCOL_BY_DRIVER
90 if (Status
== EFI_ALREADY_STARTED
) {
94 if (EFI_ERROR (Status
)) {
98 // Close the I/O Abstraction(s) used to perform the supported test
102 &gEfiDevicePathProtocolGuid
,
103 This
->DriverBindingHandle
,
108 // Open the IO Abstraction(s) needed to perform the supported test
110 Status
= gBS
->OpenProtocol (
112 &gEfiDiskIoProtocolGuid
,
114 This
->DriverBindingHandle
,
116 EFI_OPEN_PROTOCOL_BY_DRIVER
118 if (Status
== EFI_ALREADY_STARTED
) {
122 if (EFI_ERROR (Status
)) {
126 // Close the I/O Abstraction(s) used to perform the supported test
130 &gEfiDiskIoProtocolGuid
,
131 This
->DriverBindingHandle
,
136 // Open the IO Abstraction(s) needed to perform the supported test
138 Status
= gBS
->OpenProtocol (
140 &gEfiBlockIoProtocolGuid
,
142 This
->DriverBindingHandle
,
144 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
152 Start this driver on ControllerHandle by opening a Block IO and Disk IO
153 protocol, reading Device Path, and creating a child handle with a
154 Disk IO and device path protocol.
156 @param This Protocol instance pointer.
157 @param ControllerHandle Handle of device to bind driver to
158 @param RemainingDevicePath Optional parameter use to pick a specific child
161 @retval EFI_SUCCESS This driver is added to ControllerHandle
162 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
163 @retval other This driver does not support this device
168 PartitionDriverBindingStart (
169 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
170 IN EFI_HANDLE ControllerHandle
,
171 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
175 EFI_STATUS OpenStatus
;
176 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
177 EFI_DISK_IO_PROTOCOL
*DiskIo
;
178 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
179 PARTITION_DETECT_ROUTINE
*Routine
;
181 Status
= gBS
->OpenProtocol (
183 &gEfiBlockIoProtocolGuid
,
185 This
->DriverBindingHandle
,
187 EFI_OPEN_PROTOCOL_GET_PROTOCOL
189 if (EFI_ERROR (Status
)) {
193 // Get the Device Path Protocol on ControllerHandle's handle
195 Status
= gBS
->OpenProtocol (
197 &gEfiDevicePathProtocolGuid
,
198 (VOID
**) &ParentDevicePath
,
199 This
->DriverBindingHandle
,
201 EFI_OPEN_PROTOCOL_BY_DRIVER
203 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
207 Status
= gBS
->OpenProtocol (
209 &gEfiDiskIoProtocolGuid
,
211 This
->DriverBindingHandle
,
213 EFI_OPEN_PROTOCOL_BY_DRIVER
215 if (EFI_ERROR (Status
) && Status
!= EFI_ALREADY_STARTED
) {
218 &gEfiDevicePathProtocolGuid
,
219 This
->DriverBindingHandle
,
228 // If no media is present, do nothing here.
230 Status
= EFI_UNSUPPORTED
;
231 if (BlockIo
->Media
->MediaPresent
) {
233 // Try for GPT, then El Torito, and then legacy MBR partition types. If the
234 // media supports a given partition type install child handles to represent
235 // the partitions described by the media.
237 Routine
= &mPartitionDetectRoutineTable
[0];
238 while (*Routine
!= NULL
) {
239 Status
= (*Routine
) (
246 if (!EFI_ERROR (Status
) || Status
== EFI_MEDIA_CHANGED
) {
253 // In the case that the driver is already started (OpenStatus == EFI_ALREADY_STARTED),
254 // the DevicePathProtocol and the DiskIoProtocol are not actually opened by the
255 // driver. So don't try to close them. Otherwise, we will break the dependency
256 // between the controller and the driver set up before.
258 if (EFI_ERROR (Status
) && !EFI_ERROR (OpenStatus
) && Status
!= EFI_MEDIA_CHANGED
) {
261 &gEfiDiskIoProtocolGuid
,
262 This
->DriverBindingHandle
,
268 &gEfiDevicePathProtocolGuid
,
269 This
->DriverBindingHandle
,
279 Stop this driver on ControllerHandle. Support stoping any child handles
280 created by this driver.
282 @param This Protocol instance pointer.
283 @param ControllerHandle Handle of device to stop driver on
284 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
285 children is zero stop the entire bus driver.
286 @param ChildHandleBuffer List of Child Handles to Stop.
288 @retval EFI_SUCCESS This driver is removed ControllerHandle
289 @retval other This driver was not removed from this device
294 PartitionDriverBindingStop (
295 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
296 IN EFI_HANDLE ControllerHandle
,
297 IN UINTN NumberOfChildren
,
298 IN EFI_HANDLE
*ChildHandleBuffer
303 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
304 BOOLEAN AllChildrenStopped
;
305 PARTITION_PRIVATE_DATA
*Private
;
306 EFI_DISK_IO_PROTOCOL
*DiskIo
;
308 if (NumberOfChildren
== 0) {
310 // Close the bus driver
314 &gEfiDiskIoProtocolGuid
,
315 This
->DriverBindingHandle
,
321 &gEfiDevicePathProtocolGuid
,
322 This
->DriverBindingHandle
,
329 AllChildrenStopped
= TRUE
;
330 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
331 Status
= gBS
->OpenProtocol (
332 ChildHandleBuffer
[Index
],
333 &gEfiBlockIoProtocolGuid
,
335 This
->DriverBindingHandle
,
337 EFI_OPEN_PROTOCOL_GET_PROTOCOL
339 if (!EFI_ERROR (Status
)) {
341 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo
);
344 // All Software protocols have be freed from the handle so remove it.
346 BlockIo
->FlushBlocks (BlockIo
);
348 Status
= gBS
->CloseProtocol (
350 &gEfiDiskIoProtocolGuid
,
351 This
->DriverBindingHandle
,
352 ChildHandleBuffer
[Index
]
355 Status
= gBS
->UninstallMultipleProtocolInterfaces (
356 ChildHandleBuffer
[Index
],
357 &gEfiDevicePathProtocolGuid
,
359 &gEfiBlockIoProtocolGuid
,
365 if (EFI_ERROR (Status
)) {
368 &gEfiDiskIoProtocolGuid
,
370 This
->DriverBindingHandle
,
371 ChildHandleBuffer
[Index
],
372 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
375 FreePool (Private
->DevicePath
);
381 if (EFI_ERROR (Status
)) {
382 AllChildrenStopped
= FALSE
;
386 if (!AllChildrenStopped
) {
387 return EFI_DEVICE_ERROR
;
395 Reset the Block Device.
397 @param This Protocol instance pointer.
398 @param ExtendedVerification Driver may perform diagnostics on reset.
400 @retval EFI_SUCCESS The device was reset.
401 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
409 IN EFI_BLOCK_IO_PROTOCOL
*This
,
410 IN BOOLEAN ExtendedVerification
413 PARTITION_PRIVATE_DATA
*Private
;
415 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
417 return Private
->ParentBlockIo
->Reset (
418 Private
->ParentBlockIo
,
425 Read by using the Disk IO protocol on the parent device. Lba addresses
426 must be converted to byte offsets.
428 @param This Protocol instance pointer.
429 @param MediaId Id of the media, changes every time the media is replaced.
430 @param Lba The starting Logical Block Address to read from
431 @param BufferSize Size of Buffer, must be a multiple of device block size.
432 @param Buffer Buffer containing read data
434 @retval EFI_SUCCESS The data was read correctly from the device.
435 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
436 @retval EFI_NO_MEDIA There is no media in the device.
437 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
438 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
439 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
440 valid for the device.
446 PartitionReadBlocks (
447 IN EFI_BLOCK_IO_PROTOCOL
*This
,
454 PARTITION_PRIVATE_DATA
*Private
;
457 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
459 if (BufferSize
% Private
->BlockSize
!= 0) {
460 return EFI_BAD_BUFFER_SIZE
;
463 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
464 if (Offset
+ BufferSize
> Private
->End
) {
465 return EFI_INVALID_PARAMETER
;
468 // Because some kinds of partition have different block size from their parent
469 // device, we call the Disk IO protocol on the parent device, not the Block IO
472 return Private
->DiskIo
->ReadDisk (Private
->DiskIo
, MediaId
, Offset
, BufferSize
, Buffer
);
476 Write by using the Disk IO protocol on the parent device. Lba addresses
477 must be converted to byte offsets.
479 @param This Protocol instance pointer.
480 @param MediaId Id of the media, changes every time the media is replaced.
481 @param Lba The starting Logical Block Address to read from
482 @param BufferSize Size of Buffer, must be a multiple of device block size.
483 @param Buffer Buffer containing read data
485 @retval EFI_SUCCESS The data was written correctly to the device.
486 @retval EFI_WRITE_PROTECTED The device can not be written to.
487 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
488 @retval EFI_NO_MEDIA There is no media in the device.
489 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
490 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
491 @retval EFI_INVALID_PARAMETER The write request contains a LBA that is not
492 valid for the device.
498 PartitionWriteBlocks (
499 IN EFI_BLOCK_IO_PROTOCOL
*This
,
506 PARTITION_PRIVATE_DATA
*Private
;
509 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
511 if (BufferSize
% Private
->BlockSize
!= 0) {
512 return EFI_BAD_BUFFER_SIZE
;
515 Offset
= MultU64x32 (Lba
, Private
->BlockSize
) + Private
->Start
;
516 if (Offset
+ BufferSize
> Private
->End
) {
517 return EFI_INVALID_PARAMETER
;
520 // Because some kinds of partition have different block size from their parent
521 // device, we call the Disk IO protocol on the parent device, not the Block IO
524 return Private
->DiskIo
->WriteDisk (Private
->DiskIo
, MediaId
, Offset
, BufferSize
, Buffer
);
529 Flush the parent Block Device.
531 @param This Protocol instance pointer.
533 @retval EFI_SUCCESS All outstanding data was written to the device
534 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
535 @retval EFI_NO_MEDIA There is no media in the device.
541 PartitionFlushBlocks (
542 IN EFI_BLOCK_IO_PROTOCOL
*This
545 PARTITION_PRIVATE_DATA
*Private
;
547 Private
= PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This
);
549 return Private
->ParentBlockIo
->FlushBlocks (Private
->ParentBlockIo
);
555 Create a child handle for a logical block device that represents the
556 bytes Start to End of the Parent Block IO device.
558 @param[in] This Protocol instance pointer.
559 @param[in] This Calling context.
560 @param[in] ParentHandle Parent Handle for new child
561 @param[in] ParentDiskIo Parent DiskIo interface
562 @param[in] ParentBlockIo Parent BlockIo interface
563 @param[in] ParentDevicePath Parent Device Path
564 @param[in] DevicePathNode Child Device Path node
565 @param[in] Start Start Block
566 @param[in] End End Block
567 @param[in] BlockSize Child block size
568 @param[in] InstallEspGuid Flag to install EFI System Partition GUID on handle
570 @retval EFI_SUCCESS A child handle was added
571 @retval other A child handle was not added
575 PartitionInstallChildHandle (
576 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
577 IN EFI_HANDLE ParentHandle
,
578 IN EFI_DISK_IO_PROTOCOL
*ParentDiskIo
,
579 IN EFI_BLOCK_IO_PROTOCOL
*ParentBlockIo
,
580 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
581 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
,
585 IN BOOLEAN InstallEspGuid
589 PARTITION_PRIVATE_DATA
*Private
;
591 Private
= AllocateZeroPool (sizeof (PARTITION_PRIVATE_DATA
));
592 if (Private
== NULL
) {
593 return EFI_OUT_OF_RESOURCES
;
596 Private
->Signature
= PARTITION_PRIVATE_DATA_SIGNATURE
;
598 Private
->Start
= MultU64x32 (Start
, ParentBlockIo
->Media
->BlockSize
);
599 Private
->End
= MultU64x32 (End
+ 1, ParentBlockIo
->Media
->BlockSize
);
601 Private
->BlockSize
= BlockSize
;
602 Private
->ParentBlockIo
= ParentBlockIo
;
603 Private
->DiskIo
= ParentDiskIo
;
605 Private
->BlockIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION
;
607 Private
->BlockIo
.Media
= &Private
->Media
;
608 CopyMem (Private
->BlockIo
.Media
, ParentBlockIo
->Media
, sizeof (EFI_BLOCK_IO_MEDIA
));
609 Private
->Media
.LogicalPartition
= TRUE
;
610 Private
->Media
.LastBlock
= DivU64x32 (
613 ParentBlockIo
->Media
->BlockSize
618 Private
->Media
.BlockSize
= (UINT32
) BlockSize
;
620 Private
->BlockIo
.Reset
= PartitionReset
;
621 Private
->BlockIo
.ReadBlocks
= PartitionReadBlocks
;
622 Private
->BlockIo
.WriteBlocks
= PartitionWriteBlocks
;
623 Private
->BlockIo
.FlushBlocks
= PartitionFlushBlocks
;
625 Private
->DevicePath
= AppendDevicePathNode (ParentDevicePath
, DevicePathNode
);
627 if (Private
->DevicePath
== NULL
) {
629 return EFI_OUT_OF_RESOURCES
;
632 if (InstallEspGuid
) {
633 Private
->EspGuid
= &gEfiPartTypeSystemPartGuid
;
636 // If NULL InstallMultipleProtocolInterfaces will ignore it.
638 Private
->EspGuid
= NULL
;
641 // Create the new handle
643 Private
->Handle
= NULL
;
644 Status
= gBS
->InstallMultipleProtocolInterfaces (
646 &gEfiDevicePathProtocolGuid
,
648 &gEfiBlockIoProtocolGuid
,
655 if (!EFI_ERROR (Status
)) {
657 // Open the Parent Handle for the child
659 Status
= gBS
->OpenProtocol (
661 &gEfiDiskIoProtocolGuid
,
662 (VOID
**) &ParentDiskIo
,
663 This
->DriverBindingHandle
,
665 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
668 FreePool (Private
->DevicePath
);
677 The user Entry Point for module Partition. The user code starts with this function.
679 @param[in] ImageHandle The firmware allocated handle for the EFI image.
680 @param[in] SystemTable A pointer to the EFI System Table.
682 @retval EFI_SUCCESS The entry point is executed successfully.
683 @retval other Some error occurs when executing this entry point.
688 InitializePartition (
689 IN EFI_HANDLE ImageHandle
,
690 IN EFI_SYSTEM_TABLE
*SystemTable
696 // Install driver model protocol(s).
698 Status
= EfiLibInstallDriverBindingComponentName2 (
701 &gPartitionDriverBinding
,
703 &gPartitionComponentName
,
704 &gPartitionComponentName2
706 ASSERT_EFI_ERROR (Status
);