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