]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/XenPvBlkDxe/XenPvBlkDxe.c
OvmfPkg/Csm/LegacyBiosDxe: Fix Legacy16GetTableAddress call for E820 data
[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 ///
21 /// Driver Binding Protocol instance
22 ///
23 EFI_DRIVER_BINDING_PROTOCOL gXenPvBlkDxeDriverBinding = {
24 XenPvBlkDxeDriverBindingSupported,
25 XenPvBlkDxeDriverBindingStart,
26 XenPvBlkDxeDriverBindingStop,
27 XEN_PV_BLK_DXE_VERSION,
28 NULL,
29 NULL
30 };
31
32
33 /**
34 Unloads an image.
35
36 @param ImageHandle Handle that identifies the image to be unloaded.
37
38 @retval EFI_SUCCESS The image has been unloaded.
39 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
40
41 **/
42 EFI_STATUS
43 EFIAPI
44 XenPvBlkDxeUnload (
45 IN EFI_HANDLE ImageHandle
46 )
47 {
48 EFI_STATUS Status;
49
50 EFI_HANDLE *HandleBuffer;
51 UINTN HandleCount;
52 UINTN Index;
53
54
55 //
56 // Retrieve array of all handles in the handle database
57 //
58 Status = gBS->LocateHandleBuffer (
59 AllHandles,
60 NULL,
61 NULL,
62 &HandleCount,
63 &HandleBuffer
64 );
65 if (EFI_ERROR (Status)) {
66 return Status;
67 }
68
69 //
70 // Disconnect the current driver from handles in the handle database
71 //
72 for (Index = 0; Index < HandleCount; Index++) {
73 gBS->DisconnectController (HandleBuffer[Index], gImageHandle, NULL);
74 }
75
76 //
77 // Free the array of handles
78 //
79 FreePool (HandleBuffer);
80
81
82 //
83 // Uninstall protocols installed in the driver entry point
84 //
85 Status = gBS->UninstallMultipleProtocolInterfaces (
86 ImageHandle,
87 &gEfiDriverBindingProtocolGuid, &gXenPvBlkDxeDriverBinding,
88 &gEfiComponentNameProtocolGuid, &gXenPvBlkDxeComponentName,
89 &gEfiComponentName2ProtocolGuid, &gXenPvBlkDxeComponentName2,
90 NULL
91 );
92 if (EFI_ERROR (Status)) {
93 return Status;
94 }
95
96 return EFI_SUCCESS;
97 }
98
99 /**
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.
103
104 @param ImageHandle The firmware allocated handle for the UEFI image.
105 @param SystemTable A pointer to the EFI System Table.
106
107 @retval EFI_SUCCESS The operation completed successfully.
108 @retval Others An unexpected error occurred.
109 **/
110 EFI_STATUS
111 EFIAPI
112 XenPvBlkDxeDriverEntryPoint (
113 IN EFI_HANDLE ImageHandle,
114 IN EFI_SYSTEM_TABLE *SystemTable
115 )
116 {
117 EFI_STATUS Status;
118
119 //
120 // Install UEFI Driver Model protocol(s).
121 //
122 Status = EfiLibInstallDriverBindingComponentName2 (
123 ImageHandle,
124 SystemTable,
125 &gXenPvBlkDxeDriverBinding,
126 ImageHandle,
127 &gXenPvBlkDxeComponentName,
128 &gXenPvBlkDxeComponentName2
129 );
130 ASSERT_EFI_ERROR (Status);
131
132 return Status;
133 }
134
135
136 /**
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.
139
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.
153
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
164 bus driver.
165
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
170 specified by This.
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.
177 **/
178 EFI_STATUS
179 EFIAPI
180 XenPvBlkDxeDriverBindingSupported (
181 IN EFI_DRIVER_BINDING_PROTOCOL *This,
182 IN EFI_HANDLE ControllerHandle,
183 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
184 )
185 {
186 EFI_STATUS Status;
187 XENBUS_PROTOCOL *XenBusIo;
188
189 Status = gBS->OpenProtocol (
190 ControllerHandle,
191 &gXenBusProtocolGuid,
192 (VOID **)&XenBusIo,
193 This->DriverBindingHandle,
194 ControllerHandle,
195 EFI_OPEN_PROTOCOL_BY_DRIVER
196 );
197 if (EFI_ERROR (Status)) {
198 return Status;
199 }
200 if (AsciiStrCmp (XenBusIo->Type, "vbd") == 0) {
201 Status = EFI_SUCCESS;
202 } else {
203 Status = EFI_UNSUPPORTED;
204 }
205
206 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,
207 This->DriverBindingHandle, ControllerHandle);
208
209 return Status;
210 }
211
212 /**
213 Starts a device controller.
214
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.
224
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
239 driver.
240
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 failded to start the device.
245
246 **/
247 EFI_STATUS
248 EFIAPI
249 XenPvBlkDxeDriverBindingStart (
250 IN EFI_DRIVER_BINDING_PROTOCOL *This,
251 IN EFI_HANDLE ControllerHandle,
252 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
253 )
254 {
255 EFI_STATUS Status;
256 XENBUS_PROTOCOL *XenBusIo;
257 XEN_BLOCK_FRONT_DEVICE *Dev;
258 EFI_BLOCK_IO_MEDIA *Media;
259
260 Status = gBS->OpenProtocol (
261 ControllerHandle,
262 &gXenBusProtocolGuid,
263 (VOID **)&XenBusIo,
264 This->DriverBindingHandle,
265 ControllerHandle,
266 EFI_OPEN_PROTOCOL_BY_DRIVER
267 );
268 if (EFI_ERROR (Status)) {
269 return Status;
270 }
271
272 Status = XenPvBlockFrontInitialization (XenBusIo, XenBusIo->Node, &Dev);
273 if (EFI_ERROR (Status)) {
274 goto CloseProtocol;
275 }
276
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;
282 }
283 Media->MediaPresent = TRUE;
284 Media->ReadOnly = !Dev->MediaInfo.ReadWrite;
285 if (Dev->MediaInfo.CdRom) {
286 //
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
290 //
291 Media->BlockSize = 2048;
292 Media->LastBlock = DivU64x32 (Dev->MediaInfo.Sectors,
293 Media->BlockSize / Dev->MediaInfo.SectorSize) - 1;
294 } else {
295 Media->BlockSize = Dev->MediaInfo.SectorSize;
296 Media->LastBlock = Dev->MediaInfo.Sectors - 1;
297 }
298 ASSERT (Media->BlockSize % 512 == 0);
299 Dev->BlockIo.Media = Media;
300
301 Status = gBS->InstallMultipleProtocolInterfaces (
302 &ControllerHandle,
303 &gEfiBlockIoProtocolGuid, &Dev->BlockIo,
304 NULL
305 );
306 if (EFI_ERROR (Status)) {
307 DEBUG ((EFI_D_ERROR, "XenPvBlk: install protocol fail: %r\n", Status));
308 goto UninitBlockFront;
309 }
310
311 return EFI_SUCCESS;
312
313 UninitBlockFront:
314 FreePool (Media);
315 XenPvBlockFrontShutdown (Dev);
316 CloseProtocol:
317 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,
318 This->DriverBindingHandle, ControllerHandle);
319 return Status;
320 }
321
322 /**
323 Stops a device controller.
324
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.
335
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.
343
344 @retval EFI_SUCCESS The device was stopped.
345 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
346
347 **/
348 EFI_STATUS
349 EFIAPI
350 XenPvBlkDxeDriverBindingStop (
351 IN EFI_DRIVER_BINDING_PROTOCOL *This,
352 IN EFI_HANDLE ControllerHandle,
353 IN UINTN NumberOfChildren,
354 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
355 )
356 {
357 EFI_BLOCK_IO_PROTOCOL *BlockIo;
358 XEN_BLOCK_FRONT_DEVICE *Dev;
359 EFI_BLOCK_IO_MEDIA *Media;
360 EFI_STATUS Status;
361
362 Status = gBS->OpenProtocol (
363 ControllerHandle, &gEfiBlockIoProtocolGuid,
364 (VOID **)&BlockIo,
365 This->DriverBindingHandle, ControllerHandle,
366 EFI_OPEN_PROTOCOL_GET_PROTOCOL
367 );
368 if (EFI_ERROR (Status)) {
369 return Status;
370 }
371
372 Status = gBS->UninstallProtocolInterface (ControllerHandle,
373 &gEfiBlockIoProtocolGuid, BlockIo);
374 if (EFI_ERROR (Status)) {
375 return Status;
376 }
377
378 Media = BlockIo->Media;
379 Dev = XEN_BLOCK_FRONT_FROM_BLOCK_IO (BlockIo);
380 XenPvBlockFrontShutdown (Dev);
381
382 FreePool (Media);
383
384 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,
385 This->DriverBindingHandle, ControllerHandle);
386
387 return EFI_SUCCESS;
388 }