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"
20 /// Driver Binding Protocol instance
22 EFI_DRIVER_BINDING_PROTOCOL gXenPvBlkDxeDriverBinding
= {
23 XenPvBlkDxeDriverBindingSupported
,
24 XenPvBlkDxeDriverBindingStart
,
25 XenPvBlkDxeDriverBindingStop
,
26 XEN_PV_BLK_DXE_VERSION
,
34 @param ImageHandle Handle that identifies the image to be unloaded.
36 @retval EFI_SUCCESS The image has been unloaded.
37 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
43 IN EFI_HANDLE ImageHandle
48 EFI_HANDLE
*HandleBuffer
;
53 // Retrieve array of all handles in the handle database
55 Status
= gBS
->LocateHandleBuffer (
62 if (EFI_ERROR (Status
)) {
67 // Disconnect the current driver from handles in the handle database
69 for (Index
= 0; Index
< HandleCount
; Index
++) {
70 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
74 // Free the array of handles
76 FreePool (HandleBuffer
);
79 // Uninstall protocols installed in the driver entry point
81 Status
= gBS
->UninstallMultipleProtocolInterfaces (
83 &gEfiDriverBindingProtocolGuid
,
84 &gXenPvBlkDxeDriverBinding
,
85 &gEfiComponentNameProtocolGuid
,
86 &gXenPvBlkDxeComponentName
,
87 &gEfiComponentName2ProtocolGuid
,
88 &gXenPvBlkDxeComponentName2
,
91 if (EFI_ERROR (Status
)) {
99 This is the declaration of an EFI image entry point. This entry point is
100 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
101 both device drivers and bus drivers.
103 @param ImageHandle The firmware allocated handle for the UEFI image.
104 @param SystemTable A pointer to the EFI System Table.
106 @retval EFI_SUCCESS The operation completed successfully.
107 @retval Others An unexpected error occurred.
111 XenPvBlkDxeDriverEntryPoint (
112 IN EFI_HANDLE ImageHandle
,
113 IN EFI_SYSTEM_TABLE
*SystemTable
119 // Install UEFI Driver Model protocol(s).
121 Status
= EfiLibInstallDriverBindingComponentName2 (
124 &gXenPvBlkDxeDriverBinding
,
126 &gXenPvBlkDxeComponentName
,
127 &gXenPvBlkDxeComponentName2
129 ASSERT_EFI_ERROR (Status
);
135 Tests to see if this driver supports a given controller. If a child device is provided,
136 it further tests to see if this driver supports creating a handle for the specified child device.
138 This function checks to see if the driver specified by This supports the device specified by
139 ControllerHandle. Drivers will typically use the device path attached to
140 ControllerHandle and/or the services from the bus I/O abstraction attached to
141 ControllerHandle to determine if the driver supports ControllerHandle. This function
142 may be called many times during platform initialization. In order to reduce boot times, the tests
143 performed by this function must be very small, and take as little time as possible to execute. This
144 function must not change the state of any hardware devices, and this function must be aware that the
145 device specified by ControllerHandle may already be managed by the same driver or a
146 different driver. This function must match its calls to AllocatePages() with FreePages(),
147 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
148 Because ControllerHandle may have been previously started by the same driver, if a protocol is
149 already in the opened state, then it must not be closed with CloseProtocol(). This is required
150 to guarantee the state of ControllerHandle is not modified by this function.
152 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
153 @param[in] ControllerHandle The handle of the controller to test. This handle
154 must support a protocol interface that supplies
155 an I/O abstraction to the driver.
156 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
157 parameter is ignored by device drivers, and is optional for bus
158 drivers. For bus drivers, if this parameter is not NULL, then
159 the bus driver must determine if the bus controller specified
160 by ControllerHandle and the child controller specified
161 by RemainingDevicePath are both supported by this
164 @retval EFI_SUCCESS The device specified by ControllerHandle and
165 RemainingDevicePath is supported by the driver specified by This.
166 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
167 RemainingDevicePath is already being managed by the driver
169 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
170 RemainingDevicePath is already being managed by a different
171 driver or an application that requires exclusive access.
172 Currently not implemented.
173 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
174 RemainingDevicePath is not supported by the driver specified by This.
178 XenPvBlkDxeDriverBindingSupported (
179 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
180 IN EFI_HANDLE ControllerHandle
,
181 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
185 XENBUS_PROTOCOL
*XenBusIo
;
187 Status
= gBS
->OpenProtocol (
189 &gXenBusProtocolGuid
,
191 This
->DriverBindingHandle
,
193 EFI_OPEN_PROTOCOL_BY_DRIVER
195 if (EFI_ERROR (Status
)) {
199 if (AsciiStrCmp (XenBusIo
->Type
, "vbd") == 0) {
200 Status
= EFI_SUCCESS
;
202 Status
= EFI_UNSUPPORTED
;
207 &gXenBusProtocolGuid
,
208 This
->DriverBindingHandle
,
216 Starts a device controller.
218 The Start() function is designed to be invoked from the EFI boot service ConnectController().
219 As a result, much of the error checking on the parameters to Start() has been moved into this
220 common boot service. It is legal to call Start() from other locations,
221 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
222 1. ControllerHandle must be a valid EFI_HANDLE.
223 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
224 EFI_DEVICE_PATH_PROTOCOL.
225 3. Prior to calling Start(), the Supported() function for the driver specified by This must
226 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
228 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
229 @param[in] ControllerHandle The handle of the controller to start. This handle
230 must support a protocol interface that supplies
231 an I/O abstraction to the driver.
232 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
233 parameter is ignored by device drivers, and is optional for bus
234 drivers. For a bus driver, if this parameter is NULL, then handles
235 for all the children of Controller are created by this driver.
236 If this parameter is not NULL and the first Device Path Node is
237 not the End of Device Path Node, then only the handle for the
238 child device specified by the first Device Path Node of
239 RemainingDevicePath is created by this driver.
240 If the first Device Path Node of RemainingDevicePath is
241 the End of Device Path Node, no child handle is created by this
244 @retval EFI_SUCCESS The device was started.
245 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
246 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
247 @retval Others The driver failed to start the device.
252 XenPvBlkDxeDriverBindingStart (
253 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
254 IN EFI_HANDLE ControllerHandle
,
255 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
259 XENBUS_PROTOCOL
*XenBusIo
;
260 XEN_BLOCK_FRONT_DEVICE
*Dev
;
261 EFI_BLOCK_IO_MEDIA
*Media
;
263 Status
= gBS
->OpenProtocol (
265 &gXenBusProtocolGuid
,
267 This
->DriverBindingHandle
,
269 EFI_OPEN_PROTOCOL_BY_DRIVER
271 if (EFI_ERROR (Status
)) {
275 Status
= XenPvBlockFrontInitialization (XenBusIo
, XenBusIo
->Node
, &Dev
);
276 if (EFI_ERROR (Status
)) {
280 CopyMem (&Dev
->BlockIo
, &gXenPvBlkDxeBlockIo
, sizeof (EFI_BLOCK_IO_PROTOCOL
));
281 Media
= AllocateCopyPool (
282 sizeof (EFI_BLOCK_IO_MEDIA
),
283 &gXenPvBlkDxeBlockIoMedia
285 if (Dev
->MediaInfo
.VDiskInfo
& VDISK_REMOVABLE
) {
286 Media
->RemovableMedia
= TRUE
;
289 Media
->MediaPresent
= TRUE
;
290 Media
->ReadOnly
= !Dev
->MediaInfo
.ReadWrite
;
291 if (Dev
->MediaInfo
.CdRom
) {
293 // If it's a cdrom, the blocksize value need to be 2048 for OVMF to
294 // recognize it as a cdrom:
295 // MdeModulePkg/Universal/Disk/PartitionDxe/ElTorito.c
297 Media
->BlockSize
= 2048;
298 Media
->LastBlock
= DivU64x32 (
299 Dev
->MediaInfo
.Sectors
,
300 Media
->BlockSize
/ Dev
->MediaInfo
.SectorSize
303 Media
->BlockSize
= Dev
->MediaInfo
.SectorSize
;
304 Media
->LastBlock
= Dev
->MediaInfo
.Sectors
- 1;
307 ASSERT (Media
->BlockSize
% 512 == 0);
308 Dev
->BlockIo
.Media
= Media
;
310 Status
= gBS
->InstallMultipleProtocolInterfaces (
312 &gEfiBlockIoProtocolGuid
,
316 if (EFI_ERROR (Status
)) {
317 DEBUG ((DEBUG_ERROR
, "XenPvBlk: install protocol fail: %r\n", Status
));
318 goto UninitBlockFront
;
325 XenPvBlockFrontShutdown (Dev
);
329 &gXenBusProtocolGuid
,
330 This
->DriverBindingHandle
,
337 Stops a device controller.
339 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
340 As a result, much of the error checking on the parameters to Stop() has been moved
341 into this common boot service. It is legal to call Stop() from other locations,
342 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
343 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
344 same driver's Start() function.
345 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
346 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
347 Start() function, and the Start() function must have called OpenProtocol() on
348 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
350 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
351 @param[in] ControllerHandle A handle to the device being stopped. The handle must
352 support a bus specific I/O protocol for the driver
353 to use to stop the device.
354 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
355 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
356 if NumberOfChildren is 0.
358 @retval EFI_SUCCESS The device was stopped.
359 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
364 XenPvBlkDxeDriverBindingStop (
365 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
366 IN EFI_HANDLE ControllerHandle
,
367 IN UINTN NumberOfChildren
,
368 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
371 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
372 XEN_BLOCK_FRONT_DEVICE
*Dev
;
373 EFI_BLOCK_IO_MEDIA
*Media
;
376 Status
= gBS
->OpenProtocol (
378 &gEfiBlockIoProtocolGuid
,
380 This
->DriverBindingHandle
,
382 EFI_OPEN_PROTOCOL_GET_PROTOCOL
384 if (EFI_ERROR (Status
)) {
388 Status
= gBS
->UninstallProtocolInterface (
390 &gEfiBlockIoProtocolGuid
,
393 if (EFI_ERROR (Status
)) {
397 Media
= BlockIo
->Media
;
398 Dev
= XEN_BLOCK_FRONT_FROM_BLOCK_IO (BlockIo
);
399 XenPvBlockFrontShutdown (Dev
);
405 &gXenBusProtocolGuid
,
406 This
->DriverBindingHandle
,