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