]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/XenPvBlkDxe/XenPvBlkDxe.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / XenPvBlkDxe / XenPvBlkDxe.c
1 /** @file
2 This driver produce a BlockIo protocol instance for a Xen PV block device.
3
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
7 BlockFront functions.
8
9 Copyright (C) 2014, Citrix Ltd.
10
11 SPDX-License-Identifier: BSD-2-Clause-Patent
12
13 **/
14
15 #include "XenPvBlkDxe.h"
16
17 #include "BlockFront.h"
18
19 ///
20 /// Driver Binding Protocol instance
21 ///
22 EFI_DRIVER_BINDING_PROTOCOL gXenPvBlkDxeDriverBinding = {
23 XenPvBlkDxeDriverBindingSupported,
24 XenPvBlkDxeDriverBindingStart,
25 XenPvBlkDxeDriverBindingStop,
26 XEN_PV_BLK_DXE_VERSION,
27 NULL,
28 NULL
29 };
30
31 /**
32 Unloads an image.
33
34 @param ImageHandle Handle that identifies the image to be unloaded.
35
36 @retval EFI_SUCCESS The image has been unloaded.
37 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
38
39 **/
40 EFI_STATUS
41 EFIAPI
42 XenPvBlkDxeUnload (
43 IN EFI_HANDLE ImageHandle
44 )
45 {
46 EFI_STATUS Status;
47
48 EFI_HANDLE *HandleBuffer;
49 UINTN HandleCount;
50 UINTN Index;
51
52 //
53 // Retrieve array of all handles in the handle database
54 //
55 Status = gBS->LocateHandleBuffer (
56 AllHandles,
57 NULL,
58 NULL,
59 &HandleCount,
60 &HandleBuffer
61 );
62 if (EFI_ERROR (Status)) {
63 return Status;
64 }
65
66 //
67 // Disconnect the current driver from handles in the handle database
68 //
69 for (Index = 0; Index < HandleCount; Index++) {
70 gBS->DisconnectController (HandleBuffer[Index], gImageHandle, NULL);
71 }
72
73 //
74 // Free the array of handles
75 //
76 FreePool (HandleBuffer);
77
78 //
79 // Uninstall protocols installed in the driver entry point
80 //
81 Status = gBS->UninstallMultipleProtocolInterfaces (
82 ImageHandle,
83 &gEfiDriverBindingProtocolGuid,
84 &gXenPvBlkDxeDriverBinding,
85 &gEfiComponentNameProtocolGuid,
86 &gXenPvBlkDxeComponentName,
87 &gEfiComponentName2ProtocolGuid,
88 &gXenPvBlkDxeComponentName2,
89 NULL
90 );
91 if (EFI_ERROR (Status)) {
92 return Status;
93 }
94
95 return EFI_SUCCESS;
96 }
97
98 /**
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.
102
103 @param ImageHandle The firmware allocated handle for the UEFI image.
104 @param SystemTable A pointer to the EFI System Table.
105
106 @retval EFI_SUCCESS The operation completed successfully.
107 @retval Others An unexpected error occurred.
108 **/
109 EFI_STATUS
110 EFIAPI
111 XenPvBlkDxeDriverEntryPoint (
112 IN EFI_HANDLE ImageHandle,
113 IN EFI_SYSTEM_TABLE *SystemTable
114 )
115 {
116 EFI_STATUS Status;
117
118 //
119 // Install UEFI Driver Model protocol(s).
120 //
121 Status = EfiLibInstallDriverBindingComponentName2 (
122 ImageHandle,
123 SystemTable,
124 &gXenPvBlkDxeDriverBinding,
125 ImageHandle,
126 &gXenPvBlkDxeComponentName,
127 &gXenPvBlkDxeComponentName2
128 );
129 ASSERT_EFI_ERROR (Status);
130
131 return Status;
132 }
133
134 /**
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.
137
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.
151
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
162 bus driver.
163
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
168 specified by This.
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.
175 **/
176 EFI_STATUS
177 EFIAPI
178 XenPvBlkDxeDriverBindingSupported (
179 IN EFI_DRIVER_BINDING_PROTOCOL *This,
180 IN EFI_HANDLE ControllerHandle,
181 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
182 )
183 {
184 EFI_STATUS Status;
185 XENBUS_PROTOCOL *XenBusIo;
186
187 Status = gBS->OpenProtocol (
188 ControllerHandle,
189 &gXenBusProtocolGuid,
190 (VOID **)&XenBusIo,
191 This->DriverBindingHandle,
192 ControllerHandle,
193 EFI_OPEN_PROTOCOL_BY_DRIVER
194 );
195 if (EFI_ERROR (Status)) {
196 return Status;
197 }
198
199 if (AsciiStrCmp (XenBusIo->Type, "vbd") == 0) {
200 Status = EFI_SUCCESS;
201 } else {
202 Status = EFI_UNSUPPORTED;
203 }
204
205 gBS->CloseProtocol (
206 ControllerHandle,
207 &gXenBusProtocolGuid,
208 This->DriverBindingHandle,
209 ControllerHandle
210 );
211
212 return Status;
213 }
214
215 /**
216 Starts a device controller.
217
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.
227
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
242 driver.
243
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.
248
249 **/
250 EFI_STATUS
251 EFIAPI
252 XenPvBlkDxeDriverBindingStart (
253 IN EFI_DRIVER_BINDING_PROTOCOL *This,
254 IN EFI_HANDLE ControllerHandle,
255 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
256 )
257 {
258 EFI_STATUS Status;
259 XENBUS_PROTOCOL *XenBusIo;
260 XEN_BLOCK_FRONT_DEVICE *Dev;
261 EFI_BLOCK_IO_MEDIA *Media;
262
263 Status = gBS->OpenProtocol (
264 ControllerHandle,
265 &gXenBusProtocolGuid,
266 (VOID **)&XenBusIo,
267 This->DriverBindingHandle,
268 ControllerHandle,
269 EFI_OPEN_PROTOCOL_BY_DRIVER
270 );
271 if (EFI_ERROR (Status)) {
272 return Status;
273 }
274
275 Status = XenPvBlockFrontInitialization (XenBusIo, XenBusIo->Node, &Dev);
276 if (EFI_ERROR (Status)) {
277 goto CloseProtocol;
278 }
279
280 CopyMem (&Dev->BlockIo, &gXenPvBlkDxeBlockIo, sizeof (EFI_BLOCK_IO_PROTOCOL));
281 Media = AllocateCopyPool (
282 sizeof (EFI_BLOCK_IO_MEDIA),
283 &gXenPvBlkDxeBlockIoMedia
284 );
285 if (Dev->MediaInfo.VDiskInfo & VDISK_REMOVABLE) {
286 Media->RemovableMedia = TRUE;
287 }
288
289 Media->MediaPresent = TRUE;
290 Media->ReadOnly = !Dev->MediaInfo.ReadWrite;
291 if (Dev->MediaInfo.CdRom) {
292 //
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
296 //
297 Media->BlockSize = 2048;
298 Media->LastBlock = DivU64x32 (
299 Dev->MediaInfo.Sectors,
300 Media->BlockSize / Dev->MediaInfo.SectorSize
301 ) - 1;
302 } else {
303 Media->BlockSize = Dev->MediaInfo.SectorSize;
304 Media->LastBlock = Dev->MediaInfo.Sectors - 1;
305 }
306
307 ASSERT (Media->BlockSize % 512 == 0);
308 Dev->BlockIo.Media = Media;
309
310 Status = gBS->InstallMultipleProtocolInterfaces (
311 &ControllerHandle,
312 &gEfiBlockIoProtocolGuid,
313 &Dev->BlockIo,
314 NULL
315 );
316 if (EFI_ERROR (Status)) {
317 DEBUG ((DEBUG_ERROR, "XenPvBlk: install protocol fail: %r\n", Status));
318 goto UninitBlockFront;
319 }
320
321 return EFI_SUCCESS;
322
323 UninitBlockFront:
324 FreePool (Media);
325 XenPvBlockFrontShutdown (Dev);
326 CloseProtocol:
327 gBS->CloseProtocol (
328 ControllerHandle,
329 &gXenBusProtocolGuid,
330 This->DriverBindingHandle,
331 ControllerHandle
332 );
333 return Status;
334 }
335
336 /**
337 Stops a device controller.
338
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.
349
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.
357
358 @retval EFI_SUCCESS The device was stopped.
359 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
360
361 **/
362 EFI_STATUS
363 EFIAPI
364 XenPvBlkDxeDriverBindingStop (
365 IN EFI_DRIVER_BINDING_PROTOCOL *This,
366 IN EFI_HANDLE ControllerHandle,
367 IN UINTN NumberOfChildren,
368 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
369 )
370 {
371 EFI_BLOCK_IO_PROTOCOL *BlockIo;
372 XEN_BLOCK_FRONT_DEVICE *Dev;
373 EFI_BLOCK_IO_MEDIA *Media;
374 EFI_STATUS Status;
375
376 Status = gBS->OpenProtocol (
377 ControllerHandle,
378 &gEfiBlockIoProtocolGuid,
379 (VOID **)&BlockIo,
380 This->DriverBindingHandle,
381 ControllerHandle,
382 EFI_OPEN_PROTOCOL_GET_PROTOCOL
383 );
384 if (EFI_ERROR (Status)) {
385 return Status;
386 }
387
388 Status = gBS->UninstallProtocolInterface (
389 ControllerHandle,
390 &gEfiBlockIoProtocolGuid,
391 BlockIo
392 );
393 if (EFI_ERROR (Status)) {
394 return Status;
395 }
396
397 Media = BlockIo->Media;
398 Dev = XEN_BLOCK_FRONT_FROM_BLOCK_IO (BlockIo);
399 XenPvBlockFrontShutdown (Dev);
400
401 FreePool (Media);
402
403 gBS->CloseProtocol (
404 ControllerHandle,
405 &gXenBusProtocolGuid,
406 This->DriverBindingHandle,
407 ControllerHandle
408 );
409
410 return EFI_SUCCESS;
411 }