XenPvBlkDxe.c 15 KB

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