2 This driver produce a BlockIo protocol instance for a Xen PV block device.
4 This driver support XenBus protocol of type 'vbd'. Every function that
5 comsume XenBus protocol are in BlockFront, which the implementation to access
6 a Xen PV device. The BlockIo implementation is in it's one file and will call
9 Copyright (C) 2014, Citrix Ltd.
11 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include "XenPvBlkDxe.h"
17 #include "BlockFront.h"
21 /// Driver Binding Protocol instance
23 EFI_DRIVER_BINDING_PROTOCOL gXenPvBlkDxeDriverBinding
= {
24 XenPvBlkDxeDriverBindingSupported
,
25 XenPvBlkDxeDriverBindingStart
,
26 XenPvBlkDxeDriverBindingStop
,
27 XEN_PV_BLK_DXE_VERSION
,
36 @param ImageHandle Handle that identifies the image to be unloaded.
38 @retval EFI_SUCCESS The image has been unloaded.
39 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
45 IN EFI_HANDLE ImageHandle
50 EFI_HANDLE
*HandleBuffer
;
56 // Retrieve array of all handles in the handle database
58 Status
= gBS
->LocateHandleBuffer (
65 if (EFI_ERROR (Status
)) {
70 // Disconnect the current driver from handles in the handle database
72 for (Index
= 0; Index
< HandleCount
; Index
++) {
73 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
77 // Free the array of handles
79 FreePool (HandleBuffer
);
83 // Uninstall protocols installed in the driver entry point
85 Status
= gBS
->UninstallMultipleProtocolInterfaces (
87 &gEfiDriverBindingProtocolGuid
, &gXenPvBlkDxeDriverBinding
,
88 &gEfiComponentNameProtocolGuid
, &gXenPvBlkDxeComponentName
,
89 &gEfiComponentName2ProtocolGuid
, &gXenPvBlkDxeComponentName2
,
92 if (EFI_ERROR (Status
)) {
100 This is the declaration of an EFI image entry point. This entry point is
101 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
102 both device drivers and bus drivers.
104 @param ImageHandle The firmware allocated handle for the UEFI image.
105 @param SystemTable A pointer to the EFI System Table.
107 @retval EFI_SUCCESS The operation completed successfully.
108 @retval Others An unexpected error occurred.
112 XenPvBlkDxeDriverEntryPoint (
113 IN EFI_HANDLE ImageHandle
,
114 IN EFI_SYSTEM_TABLE
*SystemTable
120 // Install UEFI Driver Model protocol(s).
122 Status
= EfiLibInstallDriverBindingComponentName2 (
125 &gXenPvBlkDxeDriverBinding
,
127 &gXenPvBlkDxeComponentName
,
128 &gXenPvBlkDxeComponentName2
130 ASSERT_EFI_ERROR (Status
);
137 Tests to see if this driver supports a given controller. If a child device is provided,
138 it further tests to see if this driver supports creating a handle for the specified child device.
140 This function checks to see if the driver specified by This supports the device specified by
141 ControllerHandle. Drivers will typically use the device path attached to
142 ControllerHandle and/or the services from the bus I/O abstraction attached to
143 ControllerHandle to determine if the driver supports ControllerHandle. This function
144 may be called many times during platform initialization. In order to reduce boot times, the tests
145 performed by this function must be very small, and take as little time as possible to execute. This
146 function must not change the state of any hardware devices, and this function must be aware that the
147 device specified by ControllerHandle may already be managed by the same driver or a
148 different driver. This function must match its calls to AllocatePages() with FreePages(),
149 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
150 Because ControllerHandle may have been previously started by the same driver, if a protocol is
151 already in the opened state, then it must not be closed with CloseProtocol(). This is required
152 to guarantee the state of ControllerHandle is not modified by this function.
154 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
155 @param[in] ControllerHandle The handle of the controller to test. This handle
156 must support a protocol interface that supplies
157 an I/O abstraction to the driver.
158 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
159 parameter is ignored by device drivers, and is optional for bus
160 drivers. For bus drivers, if this parameter is not NULL, then
161 the bus driver must determine if the bus controller specified
162 by ControllerHandle and the child controller specified
163 by RemainingDevicePath are both supported by this
166 @retval EFI_SUCCESS The device specified by ControllerHandle and
167 RemainingDevicePath is supported by the driver specified by This.
168 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
169 RemainingDevicePath is already being managed by the driver
171 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
172 RemainingDevicePath is already being managed by a different
173 driver or an application that requires exclusive access.
174 Currently not implemented.
175 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
176 RemainingDevicePath is not supported by the driver specified by This.
180 XenPvBlkDxeDriverBindingSupported (
181 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
182 IN EFI_HANDLE ControllerHandle
,
183 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
187 XENBUS_PROTOCOL
*XenBusIo
;
189 Status
= gBS
->OpenProtocol (
191 &gXenBusProtocolGuid
,
193 This
->DriverBindingHandle
,
195 EFI_OPEN_PROTOCOL_BY_DRIVER
197 if (EFI_ERROR (Status
)) {
200 if (AsciiStrCmp (XenBusIo
->Type
, "vbd") == 0) {
201 Status
= EFI_SUCCESS
;
203 Status
= EFI_UNSUPPORTED
;
206 gBS
->CloseProtocol (ControllerHandle
, &gXenBusProtocolGuid
,
207 This
->DriverBindingHandle
, ControllerHandle
);
213 Starts a device controller.
215 The Start() function is designed to be invoked from the EFI boot service ConnectController().
216 As a result, much of the error checking on the parameters to Start() has been moved into this
217 common boot service. It is legal to call Start() from other locations,
218 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
219 1. ControllerHandle must be a valid EFI_HANDLE.
220 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
221 EFI_DEVICE_PATH_PROTOCOL.
222 3. Prior to calling Start(), the Supported() function for the driver specified by This must
223 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
225 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
226 @param[in] ControllerHandle The handle of the controller to start. This handle
227 must support a protocol interface that supplies
228 an I/O abstraction to the driver.
229 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
230 parameter is ignored by device drivers, and is optional for bus
231 drivers. For a bus driver, if this parameter is NULL, then handles
232 for all the children of Controller are created by this driver.
233 If this parameter is not NULL and the first Device Path Node is
234 not the End of Device Path Node, then only the handle for the
235 child device specified by the first Device Path Node of
236 RemainingDevicePath is created by this driver.
237 If the first Device Path Node of RemainingDevicePath is
238 the End of Device Path Node, no child handle is created by this
241 @retval EFI_SUCCESS The device was started.
242 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
243 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
244 @retval Others The driver failed to start the device.
249 XenPvBlkDxeDriverBindingStart (
250 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
251 IN EFI_HANDLE ControllerHandle
,
252 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
256 XENBUS_PROTOCOL
*XenBusIo
;
257 XEN_BLOCK_FRONT_DEVICE
*Dev
;
258 EFI_BLOCK_IO_MEDIA
*Media
;
260 Status
= gBS
->OpenProtocol (
262 &gXenBusProtocolGuid
,
264 This
->DriverBindingHandle
,
266 EFI_OPEN_PROTOCOL_BY_DRIVER
268 if (EFI_ERROR (Status
)) {
272 Status
= XenPvBlockFrontInitialization (XenBusIo
, XenBusIo
->Node
, &Dev
);
273 if (EFI_ERROR (Status
)) {
277 CopyMem (&Dev
->BlockIo
, &gXenPvBlkDxeBlockIo
, sizeof (EFI_BLOCK_IO_PROTOCOL
));
278 Media
= AllocateCopyPool (sizeof (EFI_BLOCK_IO_MEDIA
),
279 &gXenPvBlkDxeBlockIoMedia
);
280 if (Dev
->MediaInfo
.VDiskInfo
& VDISK_REMOVABLE
) {
281 Media
->RemovableMedia
= TRUE
;
283 Media
->MediaPresent
= TRUE
;
284 Media
->ReadOnly
= !Dev
->MediaInfo
.ReadWrite
;
285 if (Dev
->MediaInfo
.CdRom
) {
287 // If it's a cdrom, the blocksize value need to be 2048 for OVMF to
288 // recognize it as a cdrom:
289 // MdeModulePkg/Universal/Disk/PartitionDxe/ElTorito.c
291 Media
->BlockSize
= 2048;
292 Media
->LastBlock
= DivU64x32 (Dev
->MediaInfo
.Sectors
,
293 Media
->BlockSize
/ Dev
->MediaInfo
.SectorSize
) - 1;
295 Media
->BlockSize
= Dev
->MediaInfo
.SectorSize
;
296 Media
->LastBlock
= Dev
->MediaInfo
.Sectors
- 1;
298 ASSERT (Media
->BlockSize
% 512 == 0);
299 Dev
->BlockIo
.Media
= Media
;
301 Status
= gBS
->InstallMultipleProtocolInterfaces (
303 &gEfiBlockIoProtocolGuid
, &Dev
->BlockIo
,
306 if (EFI_ERROR (Status
)) {
307 DEBUG ((EFI_D_ERROR
, "XenPvBlk: install protocol fail: %r\n", Status
));
308 goto UninitBlockFront
;
315 XenPvBlockFrontShutdown (Dev
);
317 gBS
->CloseProtocol (ControllerHandle
, &gXenBusProtocolGuid
,
318 This
->DriverBindingHandle
, ControllerHandle
);
323 Stops a device controller.
325 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
326 As a result, much of the error checking on the parameters to Stop() has been moved
327 into this common boot service. It is legal to call Stop() from other locations,
328 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
329 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
330 same driver's Start() function.
331 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
332 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
333 Start() function, and the Start() function must have called OpenProtocol() on
334 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
336 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
337 @param[in] ControllerHandle A handle to the device being stopped. The handle must
338 support a bus specific I/O protocol for the driver
339 to use to stop the device.
340 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
341 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
342 if NumberOfChildren is 0.
344 @retval EFI_SUCCESS The device was stopped.
345 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
350 XenPvBlkDxeDriverBindingStop (
351 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
352 IN EFI_HANDLE ControllerHandle
,
353 IN UINTN NumberOfChildren
,
354 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
357 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
358 XEN_BLOCK_FRONT_DEVICE
*Dev
;
359 EFI_BLOCK_IO_MEDIA
*Media
;
362 Status
= gBS
->OpenProtocol (
363 ControllerHandle
, &gEfiBlockIoProtocolGuid
,
365 This
->DriverBindingHandle
, ControllerHandle
,
366 EFI_OPEN_PROTOCOL_GET_PROTOCOL
368 if (EFI_ERROR (Status
)) {
372 Status
= gBS
->UninstallProtocolInterface (ControllerHandle
,
373 &gEfiBlockIoProtocolGuid
, BlockIo
);
374 if (EFI_ERROR (Status
)) {
378 Media
= BlockIo
->Media
;
379 Dev
= XEN_BLOCK_FRONT_FROM_BLOCK_IO (BlockIo
);
380 XenPvBlockFrontShutdown (Dev
);
384 gBS
->CloseProtocol (ControllerHandle
, &gXenBusProtocolGuid
,
385 This
->DriverBindingHandle
, ControllerHandle
);