]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/XenPvBlkDxe/XenPvBlkDxe.c
OvmfPkg/XenPvBlkDxe: fix VS2010 build failures
[mirror_edk2.git] / OvmfPkg / XenPvBlkDxe / XenPvBlkDxe.c
CommitLineData
de671da8
AP
1/** @file\r
2 This driver produce a BlockIo protocol instance for a Xen PV block device.\r
3\r
4 This driver support XenBus protocol of type 'vbd'. Every function that\r
5 comsume XenBus protocol are in BlockFront, which the implementation to access\r
6 a Xen PV device. The BlockIo implementation is in it's one file and will call\r
7 BlockFront functions.\r
8\r
9 Copyright (C) 2014, Citrix Ltd.\r
10\r
11 This program and the accompanying materials\r
12 are licensed and made available under the terms and conditions of the BSD License\r
13 which accompanies this distribution. The full text of the license may be found at\r
14 http://opensource.org/licenses/bsd-license.php\r
15\r
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18\r
19**/\r
20\r
21#include "XenPvBlkDxe.h"\r
22\r
5cce8524
ST
23#include "BlockFront.h"\r
24\r
de671da8
AP
25\r
26///\r
27/// Driver Binding Protocol instance\r
28///\r
29EFI_DRIVER_BINDING_PROTOCOL gXenPvBlkDxeDriverBinding = {\r
30 XenPvBlkDxeDriverBindingSupported,\r
31 XenPvBlkDxeDriverBindingStart,\r
32 XenPvBlkDxeDriverBindingStop,\r
33 XEN_PV_BLK_DXE_VERSION,\r
34 NULL,\r
35 NULL\r
36};\r
37\r
38\r
39/**\r
40 Unloads an image.\r
41\r
42 @param ImageHandle Handle that identifies the image to be unloaded.\r
43\r
44 @retval EFI_SUCCESS The image has been unloaded.\r
45 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.\r
46\r
47**/\r
48EFI_STATUS\r
49EFIAPI\r
50XenPvBlkDxeUnload (\r
51 IN EFI_HANDLE ImageHandle\r
52 )\r
53{\r
54 EFI_STATUS Status;\r
55\r
56 EFI_HANDLE *HandleBuffer;\r
57 UINTN HandleCount;\r
58 UINTN Index;\r
59\r
60\r
61 //\r
62 // Retrieve array of all handles in the handle database\r
63 //\r
64 Status = gBS->LocateHandleBuffer (\r
65 AllHandles,\r
66 NULL,\r
67 NULL,\r
68 &HandleCount,\r
69 &HandleBuffer\r
70 );\r
71 if (EFI_ERROR (Status)) {\r
72 return Status;\r
73 }\r
74\r
75 //\r
76 // Disconnect the current driver from handles in the handle database\r
77 //\r
78 for (Index = 0; Index < HandleCount; Index++) {\r
79 gBS->DisconnectController (HandleBuffer[Index], gImageHandle, NULL);\r
80 }\r
81\r
82 //\r
83 // Free the array of handles\r
84 //\r
85 FreePool (HandleBuffer);\r
86\r
87\r
88 //\r
89 // Uninstall protocols installed in the driver entry point\r
90 //\r
91 Status = gBS->UninstallMultipleProtocolInterfaces (\r
92 ImageHandle,\r
93 &gEfiDriverBindingProtocolGuid, &gXenPvBlkDxeDriverBinding,\r
94 &gEfiComponentNameProtocolGuid, &gXenPvBlkDxeComponentName,\r
95 &gEfiComponentName2ProtocolGuid, &gXenPvBlkDxeComponentName2,\r
96 NULL\r
97 );\r
98 if (EFI_ERROR (Status)) {\r
99 return Status;\r
100 }\r
101\r
102 return EFI_SUCCESS;\r
103}\r
104\r
105/**\r
106 This is the declaration of an EFI image entry point. This entry point is\r
107 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
108 both device drivers and bus drivers.\r
109\r
110 @param ImageHandle The firmware allocated handle for the UEFI image.\r
111 @param SystemTable A pointer to the EFI System Table.\r
112\r
113 @retval EFI_SUCCESS The operation completed successfully.\r
114 @retval Others An unexpected error occurred.\r
115**/\r
116EFI_STATUS\r
117EFIAPI\r
118XenPvBlkDxeDriverEntryPoint (\r
119 IN EFI_HANDLE ImageHandle,\r
120 IN EFI_SYSTEM_TABLE *SystemTable\r
121 )\r
122{\r
123 EFI_STATUS Status;\r
124\r
125 //\r
126 // Install UEFI Driver Model protocol(s).\r
127 //\r
128 Status = EfiLibInstallDriverBindingComponentName2 (\r
129 ImageHandle,\r
130 SystemTable,\r
131 &gXenPvBlkDxeDriverBinding,\r
132 ImageHandle,\r
133 &gXenPvBlkDxeComponentName,\r
134 &gXenPvBlkDxeComponentName2\r
135 );\r
136 ASSERT_EFI_ERROR (Status);\r
137\r
138 return Status;\r
139}\r
140\r
141\r
142/**\r
143 Tests to see if this driver supports a given controller. If a child device is provided,\r
144 it further tests to see if this driver supports creating a handle for the specified child device.\r
145\r
146 This function checks to see if the driver specified by This supports the device specified by\r
147 ControllerHandle. Drivers will typically use the device path attached to\r
148 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
149 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
150 may be called many times during platform initialization. In order to reduce boot times, the tests\r
151 performed by this function must be very small, and take as little time as possible to execute. This\r
152 function must not change the state of any hardware devices, and this function must be aware that the\r
153 device specified by ControllerHandle may already be managed by the same driver or a\r
154 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
155 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
156 Because ControllerHandle may have been previously started by the same driver, if a protocol is\r
157 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
158 to guarantee the state of ControllerHandle is not modified by this function.\r
159\r
160 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
161 @param[in] ControllerHandle The handle of the controller to test. This handle\r
162 must support a protocol interface that supplies\r
163 an I/O abstraction to the driver.\r
164 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
165 parameter is ignored by device drivers, and is optional for bus\r
166 drivers. For bus drivers, if this parameter is not NULL, then\r
167 the bus driver must determine if the bus controller specified\r
168 by ControllerHandle and the child controller specified\r
169 by RemainingDevicePath are both supported by this\r
170 bus driver.\r
171\r
172 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
173 RemainingDevicePath is supported by the driver specified by This.\r
174 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
175 RemainingDevicePath is already being managed by the driver\r
176 specified by This.\r
177 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
178 RemainingDevicePath is already being managed by a different\r
179 driver or an application that requires exclusive access.\r
180 Currently not implemented.\r
181 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
182 RemainingDevicePath is not supported by the driver specified by This.\r
183**/\r
184EFI_STATUS\r
185EFIAPI\r
186XenPvBlkDxeDriverBindingSupported (\r
187 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
188 IN EFI_HANDLE ControllerHandle,\r
189 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
190 )\r
191{\r
192 EFI_STATUS Status;\r
193 XENBUS_PROTOCOL *XenBusIo;\r
194\r
195 Status = gBS->OpenProtocol (\r
196 ControllerHandle,\r
197 &gXenBusProtocolGuid,\r
198 (VOID **)&XenBusIo,\r
199 This->DriverBindingHandle,\r
200 ControllerHandle,\r
201 EFI_OPEN_PROTOCOL_BY_DRIVER\r
202 );\r
203 if (EFI_ERROR (Status)) {\r
204 return Status;\r
205 }\r
206 if (AsciiStrCmp (XenBusIo->Type, "vbd") == 0) {\r
207 Status = EFI_SUCCESS;\r
208 } else {\r
209 Status = EFI_UNSUPPORTED;\r
210 }\r
211\r
212 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,\r
213 This->DriverBindingHandle, ControllerHandle);\r
214\r
215 return Status;\r
216}\r
217\r
218/**\r
219 Starts a device controller.\r
220\r
221 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
222 As a result, much of the error checking on the parameters to Start() has been moved into this\r
223 common boot service. It is legal to call Start() from other locations,\r
224 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
225 1. ControllerHandle must be a valid EFI_HANDLE.\r
226 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
227 EFI_DEVICE_PATH_PROTOCOL.\r
228 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
229 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
230\r
231 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
232 @param[in] ControllerHandle The handle of the controller to start. This handle\r
233 must support a protocol interface that supplies\r
234 an I/O abstraction to the driver.\r
235 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
236 parameter is ignored by device drivers, and is optional for bus\r
237 drivers. For a bus driver, if this parameter is NULL, then handles\r
238 for all the children of Controller are created by this driver.\r
239 If this parameter is not NULL and the first Device Path Node is\r
240 not the End of Device Path Node, then only the handle for the\r
241 child device specified by the first Device Path Node of\r
242 RemainingDevicePath is created by this driver.\r
243 If the first Device Path Node of RemainingDevicePath is\r
244 the End of Device Path Node, no child handle is created by this\r
245 driver.\r
246\r
247 @retval EFI_SUCCESS The device was started.\r
248 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
249 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
250 @retval Others The driver failded to start the device.\r
251\r
252**/\r
253EFI_STATUS\r
254EFIAPI\r
255XenPvBlkDxeDriverBindingStart (\r
256 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
257 IN EFI_HANDLE ControllerHandle,\r
258 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
259 )\r
260{\r
261 EFI_STATUS Status;\r
262 XENBUS_PROTOCOL *XenBusIo;\r
5cce8524 263 XEN_BLOCK_FRONT_DEVICE *Dev;\r
5de8a35c 264 EFI_BLOCK_IO_MEDIA *Media;\r
de671da8
AP
265\r
266 Status = gBS->OpenProtocol (\r
267 ControllerHandle,\r
268 &gXenBusProtocolGuid,\r
269 (VOID **)&XenBusIo,\r
270 This->DriverBindingHandle,\r
271 ControllerHandle,\r
272 EFI_OPEN_PROTOCOL_BY_DRIVER\r
273 );\r
274 if (EFI_ERROR (Status)) {\r
275 return Status;\r
276 }\r
277\r
5cce8524
ST
278 Status = XenPvBlockFrontInitialization (XenBusIo, XenBusIo->Node, &Dev);\r
279 if (EFI_ERROR (Status)) {\r
280 goto CloseProtocol;\r
281 }\r
282\r
5de8a35c
AP
283 CopyMem (&Dev->BlockIo, &gXenPvBlkDxeBlockIo, sizeof (EFI_BLOCK_IO_PROTOCOL));\r
284 Media = AllocateCopyPool (sizeof (EFI_BLOCK_IO_MEDIA),\r
285 &gXenPvBlkDxeBlockIoMedia);\r
286 if (Dev->MediaInfo.VDiskInfo & VDISK_REMOVABLE) {\r
287 Media->RemovableMedia = TRUE;\r
288 }\r
289 Media->MediaPresent = TRUE;\r
290 Media->ReadOnly = !Dev->MediaInfo.ReadWrite;\r
291 if (Dev->MediaInfo.CdRom) {\r
292 //\r
293 // If it's a cdrom, the blocksize value need to be 2048 for OVMF to\r
294 // recognize it as a cdrom:\r
295 // MdeModulePkg/Universal/Disk/PartitionDxe/ElTorito.c\r
296 //\r
297 Media->BlockSize = 2048;\r
298 Media->LastBlock = DivU64x32 (Dev->MediaInfo.Sectors,\r
299 Media->BlockSize / Dev->MediaInfo.SectorSize) - 1;\r
300 } else {\r
301 Media->BlockSize = Dev->MediaInfo.SectorSize;\r
302 Media->LastBlock = Dev->MediaInfo.Sectors - 1;\r
303 }\r
304 ASSERT (Media->BlockSize % 512 == 0);\r
305 Dev->BlockIo.Media = Media;\r
306\r
307 Status = gBS->InstallMultipleProtocolInterfaces (\r
308 &ControllerHandle,\r
309 &gEfiBlockIoProtocolGuid, &Dev->BlockIo,\r
310 NULL\r
311 );\r
312 if (EFI_ERROR (Status)) {\r
313 DEBUG ((EFI_D_ERROR, "XenPvBlk: install protocol fail: %r\n", Status));\r
314 goto UninitBlockFront;\r
315 }\r
316\r
de671da8 317 return EFI_SUCCESS;\r
5cce8524 318\r
5de8a35c
AP
319UninitBlockFront:\r
320 FreePool (Media);\r
321 XenPvBlockFrontShutdown (Dev);\r
5cce8524
ST
322CloseProtocol:\r
323 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,\r
324 This->DriverBindingHandle, ControllerHandle);\r
325 return Status;\r
de671da8
AP
326}\r
327\r
328/**\r
329 Stops a device controller.\r
330\r
331 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
332 As a result, much of the error checking on the parameters to Stop() has been moved\r
333 into this common boot service. It is legal to call Stop() from other locations,\r
334 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
335 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
336 same driver's Start() function.\r
337 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
338 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
339 Start() function, and the Start() function must have called OpenProtocol() on\r
340 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
341\r
342 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
343 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
344 support a bus specific I/O protocol for the driver\r
345 to use to stop the device.\r
346 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
347 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
348 if NumberOfChildren is 0.\r
349\r
350 @retval EFI_SUCCESS The device was stopped.\r
351 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
352\r
353**/\r
354EFI_STATUS\r
355EFIAPI\r
356XenPvBlkDxeDriverBindingStop (\r
357 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
358 IN EFI_HANDLE ControllerHandle,\r
359 IN UINTN NumberOfChildren,\r
360 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
361 )\r
362{\r
5de8a35c
AP
363 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
364 XEN_BLOCK_FRONT_DEVICE *Dev;\r
365 EFI_BLOCK_IO_MEDIA *Media;\r
366 EFI_STATUS Status;\r
367\r
368 Status = gBS->OpenProtocol (\r
369 ControllerHandle, &gEfiBlockIoProtocolGuid,\r
370 (VOID **)&BlockIo,\r
371 This->DriverBindingHandle, ControllerHandle,\r
372 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
373 );\r
374 if (EFI_ERROR (Status)) {\r
375 return Status;\r
376 }\r
377\r
378 Status = gBS->UninstallProtocolInterface (ControllerHandle,\r
379 &gEfiBlockIoProtocolGuid, BlockIo);\r
380 if (EFI_ERROR (Status)) {\r
381 return Status;\r
382 }\r
383\r
384 Media = BlockIo->Media;\r
385 Dev = XEN_BLOCK_FRONT_FROM_BLOCK_IO (BlockIo);\r
386 XenPvBlockFrontShutdown (Dev);\r
387\r
388 FreePool (Media);\r
389\r
de671da8
AP
390 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,\r
391 This->DriverBindingHandle, ControllerHandle);\r
392\r
393 return EFI_SUCCESS;\r
394}\r