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
17 #include <Protocol/BlockIo.h>
18 #include <Protocol/BlockIo2.h>
20 #include <Protocol/ComponentName.h>
21 #include <Protocol/DevicePath.h>
22 #include <Protocol/DriverBinding.h>
23 #include <Protocol/DiskIo.h>
24 #include <Protocol/DiskIo2.h>
25 #include <Protocol/PartitionInfo.h>
26 #include <Library/DebugLib.h>
27 #include <Library/UefiDriverEntryPoint.h>
28 #include <Library/BaseLib.h>
29 #include <Library/UefiLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/UefiBootServicesTableLib.h>
33 #include <Library/DevicePathLib.h>
35 #include <IndustryStandard/Mbr.h>
36 #include <IndustryStandard/ElTorito.h>
37 #include <IndustryStandard/Udf.h>
40 // Partition private data
42 #define PARTITION_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('P', 'a', 'r', 't')
47 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
48 EFI_BLOCK_IO_PROTOCOL BlockIo
;
49 EFI_BLOCK_IO2_PROTOCOL BlockIo2
;
50 EFI_BLOCK_IO_MEDIA Media
;
51 EFI_BLOCK_IO_MEDIA Media2
;// For BlockIO2
52 EFI_PARTITION_INFO_PROTOCOL PartitionInfo
;
54 EFI_DISK_IO_PROTOCOL
*DiskIo
;
55 EFI_DISK_IO2_PROTOCOL
*DiskIo2
;
56 EFI_BLOCK_IO_PROTOCOL
*ParentBlockIo
;
57 EFI_BLOCK_IO2_PROTOCOL
*ParentBlockIo2
;
64 } PARTITION_PRIVATE_DATA
;
67 EFI_DISK_IO2_TOKEN DiskIo2Token
;
68 EFI_BLOCK_IO2_TOKEN
*BlockIo2Token
;
69 } PARTITION_ACCESS_TASK
;
71 #define PARTITION_DEVICE_FROM_BLOCK_IO_THIS(a) CR (a, PARTITION_PRIVATE_DATA, BlockIo, PARTITION_PRIVATE_DATA_SIGNATURE)
72 #define PARTITION_DEVICE_FROM_BLOCK_IO2_THIS(a) CR (a, PARTITION_PRIVATE_DATA, BlockIo2, PARTITION_PRIVATE_DATA_SIGNATURE)
77 extern EFI_DRIVER_BINDING_PROTOCOL gPartitionDriverBinding
;
78 extern EFI_COMPONENT_NAME_PROTOCOL gPartitionComponentName
;
79 extern EFI_COMPONENT_NAME2_PROTOCOL gPartitionComponentName2
;
82 // Extract INT32 from char array
84 #define UNPACK_INT32(a) (INT32)( (((UINT8 *) a)[0] << 0) | \
85 (((UINT8 *) a)[1] << 8) | \
86 (((UINT8 *) a)[2] << 16) | \
87 (((UINT8 *) a)[3] << 24) )
90 // Extract UINT32 from char array
92 #define UNPACK_UINT32(a) (UINT32)( (((UINT8 *) a)[0] << 0) | \
93 (((UINT8 *) a)[1] << 8) | \
94 (((UINT8 *) a)[2] << 16) | \
95 (((UINT8 *) a)[3] << 24) )
98 // GPT Partition Entry Status
104 } EFI_PARTITION_ENTRY_STATUS
;
107 // Function Prototypes
111 Test to see if this driver supports ControllerHandle. Any ControllerHandle
112 than contains a BlockIo and DiskIo protocol can be supported.
114 @param This Protocol instance pointer.
115 @param ControllerHandle Handle of device to test
116 @param RemainingDevicePath Optional parameter use to pick a specific child
119 @retval EFI_SUCCESS This driver supports this device
120 @retval EFI_ALREADY_STARTED This driver is already running on this device
121 @retval other This driver does not support this device
126 PartitionDriverBindingSupported (
127 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
128 IN EFI_HANDLE ControllerHandle
,
129 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
133 Start this driver on ControllerHandle by opening a Block IO and Disk IO
134 protocol, reading Device Path, and creating a child handle with a
135 Disk IO and device path protocol.
137 @param This Protocol instance pointer.
138 @param ControllerHandle Handle of device to bind driver to
139 @param RemainingDevicePath Optional parameter use to pick a specific child
142 @retval EFI_SUCCESS This driver is added to ControllerHandle
143 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
144 @retval other This driver does not support this device
149 PartitionDriverBindingStart (
150 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
151 IN EFI_HANDLE ControllerHandle
,
152 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
156 Stop this driver on ControllerHandle. Support stopping any child handles
157 created by this driver.
159 @param This Protocol instance pointer.
160 @param ControllerHandle Handle of device to stop driver on
161 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
162 children is zero stop the entire bus driver.
163 @param ChildHandleBuffer List of Child Handles to Stop.
165 @retval EFI_SUCCESS This driver is removed ControllerHandle
166 @retval other This driver was not removed from this device
171 PartitionDriverBindingStop (
172 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
173 IN EFI_HANDLE ControllerHandle
,
174 IN UINTN NumberOfChildren
,
175 IN EFI_HANDLE
*ChildHandleBuffer
179 // EFI Component Name Functions
183 Retrieves a Unicode string that is the user readable name of the driver.
185 This function retrieves the user readable name of a driver in the form of a
186 Unicode string. If the driver specified by This has a user readable name in
187 the language specified by Language, then a pointer to the driver name is
188 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
189 by This does not support the language specified by Language,
190 then EFI_UNSUPPORTED is returned.
192 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
193 EFI_COMPONENT_NAME_PROTOCOL instance.
195 @param Language[in] A pointer to a Null-terminated ASCII string
196 array indicating the language. This is the
197 language of the driver name that the caller is
198 requesting, and it must match one of the
199 languages specified in SupportedLanguages. The
200 number of languages supported by a driver is up
201 to the driver writer. Language is specified
202 in RFC 4646 or ISO 639-2 language code format.
204 @param DriverName[out] A pointer to the Unicode string to return.
205 This Unicode string is the name of the
206 driver specified by This in the language
207 specified by Language.
209 @retval EFI_SUCCESS The Unicode string for the Driver specified by
210 This and the language specified by Language was
211 returned in DriverName.
213 @retval EFI_INVALID_PARAMETER Language is NULL.
215 @retval EFI_INVALID_PARAMETER DriverName is NULL.
217 @retval EFI_UNSUPPORTED The driver specified by This does not support
218 the language specified by Language.
223 PartitionComponentNameGetDriverName (
224 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
226 OUT CHAR16
**DriverName
230 Retrieves a Unicode string that is the user readable name of the controller
231 that is being managed by a driver.
233 This function retrieves the user readable name of the controller specified by
234 ControllerHandle and ChildHandle in the form of a Unicode string. If the
235 driver specified by This has a user readable name in the language specified by
236 Language, then a pointer to the controller name is returned in ControllerName,
237 and EFI_SUCCESS is returned. If the driver specified by This is not currently
238 managing the controller specified by ControllerHandle and ChildHandle,
239 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
240 support the language specified by Language, then EFI_UNSUPPORTED is returned.
242 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
243 EFI_COMPONENT_NAME_PROTOCOL instance.
245 @param ControllerHandle[in] The handle of a controller that the driver
246 specified by This is managing. This handle
247 specifies the controller whose name is to be
250 @param ChildHandle[in] The handle of the child controller to retrieve
251 the name of. This is an optional parameter that
252 may be NULL. It will be NULL for device
253 drivers. It will also be NULL for a bus drivers
254 that wish to retrieve the name of the bus
255 controller. It will not be NULL for a bus
256 driver that wishes to retrieve the name of a
259 @param Language[in] A pointer to a Null-terminated ASCII string
260 array indicating the language. This is the
261 language of the driver name that the caller is
262 requesting, and it must match one of the
263 languages specified in SupportedLanguages. The
264 number of languages supported by a driver is up
265 to the driver writer. Language is specified in
266 RFC 4646 or ISO 639-2 language code format.
268 @param ControllerName[out] A pointer to the Unicode string to return.
269 This Unicode string is the name of the
270 controller specified by ControllerHandle and
271 ChildHandle in the language specified by
272 Language from the point of view of the driver
275 @retval EFI_SUCCESS The Unicode string for the user readable name in
276 the language specified by Language for the
277 driver specified by This was returned in
280 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
282 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
285 @retval EFI_INVALID_PARAMETER Language is NULL.
287 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
289 @retval EFI_UNSUPPORTED The driver specified by This is not currently
290 managing the controller specified by
291 ControllerHandle and ChildHandle.
293 @retval EFI_UNSUPPORTED The driver specified by This does not support
294 the language specified by Language.
299 PartitionComponentNameGetControllerName (
300 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
301 IN EFI_HANDLE ControllerHandle
,
302 IN EFI_HANDLE ChildHandle OPTIONAL
,
304 OUT CHAR16
**ControllerName
308 Create a child handle for a logical block device that represents the
309 bytes Start to End of the Parent Block IO device.
311 @param[in] This Protocol instance pointer.
312 @param[in] ParentHandle Parent Handle for new child.
313 @param[in] ParentDiskIo Parent DiskIo interface.
314 @param[in] ParentDiskIo2 Parent DiskIo2 interface.
315 @param[in] ParentBlockIo Parent BlockIo interface.
316 @param[in] ParentBlockIo2 Parent BlockIo2 interface.
317 @param[in] ParentDevicePath Parent Device Path.
318 @param[in] DevicePathNode Child Device Path node.
319 @param[in] PartitionInfo Child Partition Information interface.
320 @param[in] Start Start Block.
321 @param[in] End End Block.
322 @param[in] BlockSize Child block size.
323 @param[in] TypeGuid Parition Type Guid.
325 @retval EFI_SUCCESS A child handle was added.
326 @retval other A child handle was not added.
330 PartitionInstallChildHandle (
331 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
332 IN EFI_HANDLE ParentHandle
,
333 IN EFI_DISK_IO_PROTOCOL
*ParentDiskIo
,
334 IN EFI_DISK_IO2_PROTOCOL
*ParentDiskIo2
,
335 IN EFI_BLOCK_IO_PROTOCOL
*ParentBlockIo
,
336 IN EFI_BLOCK_IO2_PROTOCOL
*ParentBlockIo2
,
337 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
338 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
,
339 IN EFI_PARTITION_INFO_PROTOCOL
*PartitionInfo
,
343 IN EFI_GUID
*TypeGuid
347 Test to see if there is any child on ControllerHandle.
349 @param[in] ControllerHandle Handle of device to test.
351 @retval TRUE There are children on the ControllerHandle.
352 @retval FALSE No child is on the ControllerHandle.
357 IN EFI_HANDLE ControllerHandle
361 Install child handles if the Handle supports GPT partition structure.
363 @param[in] This Calling context.
364 @param[in] Handle Parent Handle.
365 @param[in] DiskIo Parent DiskIo interface.
366 @param[in] DiskIo2 Parent DiskIo2 interface.
367 @param[in] BlockIo Parent BlockIo interface.
368 @param[in] BlockIo2 Parent BlockIo2 interface.
369 @param[in] DevicePath Parent Device Path.
371 @retval EFI_SUCCESS Valid GPT disk.
372 @retval EFI_MEDIA_CHANGED Media changed Detected.
373 @retval EFI_INVALID_PARAMETER If both BlockIo and BlockIo2 are NULL;
374 @retval other Not a valid GPT disk.
378 PartitionInstallGptChildHandles (
379 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
380 IN EFI_HANDLE Handle
,
381 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
382 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
383 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
384 IN EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
,
385 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
389 Install child handles if the Handle supports El Torito format.
391 @param[in] This Calling context.
392 @param[in] Handle Parent Handle.
393 @param[in] DiskIo Parent DiskIo interface.
394 @param[in] DiskIo2 Parent DiskIo2 interface.
395 @param[in] BlockIo Parent BlockIo interface.
396 @param[in] BlockIo2 Parent BlockIo2 interface.
397 @param[in] DevicePath Parent Device Path
400 @retval EFI_SUCCESS Child handle(s) was added.
401 @retval EFI_MEDIA_CHANGED Media changed Detected.
402 @retval other no child handle was added.
406 PartitionInstallElToritoChildHandles (
407 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
408 IN EFI_HANDLE Handle
,
409 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
410 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
411 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
412 IN EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
,
413 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
417 Install child handles if the Handle supports MBR format.
419 @param[in] This Calling context.
420 @param[in] Handle Parent Handle.
421 @param[in] DiskIo Parent DiskIo interface.
422 @param[in] DiskIo2 Parent DiskIo2 interface.
423 @param[in] BlockIo Parent BlockIo interface.
424 @param[in] BlockIo2 Parent BlockIo2 interface.
425 @param[in] DevicePath Parent Device Path.
427 @retval EFI_SUCCESS A child handle was added.
428 @retval EFI_MEDIA_CHANGED Media change was detected.
429 @retval Others MBR partition was not found.
433 PartitionInstallMbrChildHandles (
434 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
435 IN EFI_HANDLE Handle
,
436 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
437 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
438 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
439 IN EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
,
440 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
444 Install child handles if the Handle supports UDF/ECMA-167 volume format.
446 @param[in] This Calling context.
447 @param[in] Handle Parent Handle.
448 @param[in] DiskIo Parent DiskIo interface.
449 @param[in] DiskIo2 Parent DiskIo2 interface.
450 @param[in] BlockIo Parent BlockIo interface.
451 @param[in] BlockIo2 Parent BlockIo2 interface.
452 @param[in] DevicePath Parent Device Path
455 @retval EFI_SUCCESS Child handle(s) was added.
456 @retval EFI_MEDIA_CHANGED Media changed Detected.
457 @retval other no child handle was added.
461 PartitionInstallUdfChildHandles (
462 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
463 IN EFI_HANDLE Handle
,
464 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
465 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
466 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
467 IN EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
,
468 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
473 (*PARTITION_DETECT_ROUTINE
) (
474 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
475 IN EFI_HANDLE Handle
,
476 IN EFI_DISK_IO_PROTOCOL
*DiskIo
,
477 IN EFI_DISK_IO2_PROTOCOL
*DiskIo2
,
478 IN EFI_BLOCK_IO_PROTOCOL
*BlockIo
,
479 IN EFI_BLOCK_IO2_PROTOCOL
*BlockIo2
,
480 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath