EmuBlockIo.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /**@file
  2. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. bbe
  5. **/
  6. #include "EmuBlockIo.h"
  7. /**
  8. Reset the block device hardware.
  9. @param[in] This Indicates a pointer to the calling context.
  10. @param[in] ExtendedVerification Indicates that the driver may perform a more
  11. exhausive verfication operation of the device
  12. during reset.
  13. @retval EFI_SUCCESS The device was reset.
  14. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  15. not be reset.
  16. **/
  17. EFI_STATUS
  18. EFIAPI
  19. EmuBlockIo2Reset (
  20. IN EFI_BLOCK_IO2_PROTOCOL *This,
  21. IN BOOLEAN ExtendedVerification
  22. )
  23. {
  24. EFI_STATUS Status;
  25. EMU_BLOCK_IO_PRIVATE *Private;
  26. EFI_TPL OldTpl;
  27. Private = EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This);
  28. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  29. Status = Private->Io->Reset (Private->Io, ExtendedVerification);
  30. gBS->RestoreTPL (OldTpl);
  31. return Status;
  32. }
  33. /**
  34. Read BufferSize bytes from Lba into Buffer.
  35. This function reads the requested number of blocks from the device. All the
  36. blocks are read, or an error is returned.
  37. If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and
  38. non-blocking I/O is being used, the Event associated with this request will
  39. not be signaled.
  40. @param[in] This Indicates a pointer to the calling context.
  41. @param[in] MediaId Id of the media, changes every time the media is
  42. replaced.
  43. @param[in] Lba The starting Logical Block Address to read from.
  44. @param[in, out] Token A pointer to the token associated with the transaction.
  45. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  46. @param[out] Buffer A pointer to the destination buffer for the data. The
  47. caller is responsible for either having implicit or
  48. explicit ownership of the buffer.
  49. @retval EFI_SUCCESS The read request was queued if Token->Event is
  50. not NULL.The data was read correctly from the
  51. device if the Token->Event is NULL.
  52. @retval EFI_DEVICE_ERROR The device reported an error while performing
  53. the read.
  54. @retval EFI_NO_MEDIA There is no media in the device.
  55. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  56. @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
  57. intrinsic block size of the device.
  58. @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
  59. or the buffer is not on proper alignment.
  60. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  61. of resources.
  62. **/
  63. EFI_STATUS
  64. EFIAPI
  65. EmuBlockIo2ReadBlocksEx (
  66. IN EFI_BLOCK_IO2_PROTOCOL *This,
  67. IN UINT32 MediaId,
  68. IN EFI_LBA LBA,
  69. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  70. IN UINTN BufferSize,
  71. OUT VOID *Buffer
  72. )
  73. {
  74. EFI_STATUS Status;
  75. EMU_BLOCK_IO_PRIVATE *Private;
  76. EFI_TPL OldTpl;
  77. Private = EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This);
  78. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  79. Status = Private->Io->ReadBlocks (Private->Io, MediaId, LBA, Token, BufferSize, Buffer);
  80. gBS->RestoreTPL (OldTpl);
  81. return Status;
  82. }
  83. /**
  84. Write BufferSize bytes from Lba into Buffer.
  85. This function writes the requested number of blocks to the device. All blocks
  86. are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,
  87. EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is
  88. being used, the Event associated with this request will not be signaled.
  89. @param[in] This Indicates a pointer to the calling context.
  90. @param[in] MediaId The media ID that the write request is for.
  91. @param[in] Lba The starting logical block address to be written. The
  92. caller is responsible for writing to only legitimate
  93. locations.
  94. @param[in, out] Token A pointer to the token associated with the transaction.
  95. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  96. @param[in] Buffer A pointer to the source buffer for the data.
  97. @retval EFI_SUCCESS The write request was queued if Event is not NULL.
  98. The data was written correctly to the device if
  99. the Event is NULL.
  100. @retval EFI_WRITE_PROTECTED The device can not be written to.
  101. @retval EFI_NO_MEDIA There is no media in the device.
  102. @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
  103. @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
  104. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  105. @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
  106. or the buffer is not on proper alignment.
  107. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  108. of resources.
  109. **/
  110. EFI_STATUS
  111. EFIAPI
  112. EmuBlockIo2WriteBlocksEx (
  113. IN EFI_BLOCK_IO2_PROTOCOL *This,
  114. IN UINT32 MediaId,
  115. IN EFI_LBA LBA,
  116. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  117. IN UINTN BufferSize,
  118. IN VOID *Buffer
  119. )
  120. {
  121. EFI_STATUS Status;
  122. EMU_BLOCK_IO_PRIVATE *Private;
  123. EFI_TPL OldTpl;
  124. Private = EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This);
  125. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  126. Status = Private->Io->WriteBlocks (Private->Io, MediaId, LBA, Token, BufferSize, Buffer);
  127. gBS->RestoreTPL (OldTpl);
  128. return Status;
  129. }
  130. /**
  131. Flush the Block Device.
  132. If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED
  133. is returned and non-blocking I/O is being used, the Event associated with
  134. this request will not be signaled.
  135. @param[in] This Indicates a pointer to the calling context.
  136. @param[in,out] Token A pointer to the token associated with the transaction
  137. @retval EFI_SUCCESS The flush request was queued if Event is not NULL.
  138. All outstanding data was written correctly to the
  139. device if the Event is NULL.
  140. @retval EFI_DEVICE_ERROR The device reported an error while writting back
  141. the data.
  142. @retval EFI_WRITE_PROTECTED The device cannot be written to.
  143. @retval EFI_NO_MEDIA There is no media in the device.
  144. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  145. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  146. of resources.
  147. **/
  148. EFI_STATUS
  149. EFIAPI
  150. EmuBlockIo2Flush (
  151. IN EFI_BLOCK_IO2_PROTOCOL *This,
  152. IN OUT EFI_BLOCK_IO2_TOKEN *Token
  153. )
  154. {
  155. EFI_STATUS Status;
  156. EMU_BLOCK_IO_PRIVATE *Private;
  157. EFI_TPL OldTpl;
  158. Private = EMU_BLOCK_IO2_PRIVATE_DATA_FROM_THIS (This);
  159. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  160. Status = Private->Io->FlushBlocks (Private->Io, Token);
  161. gBS->RestoreTPL (OldTpl);
  162. return Status;
  163. }
  164. /**
  165. Reset the Block Device.
  166. @param This Indicates a pointer to the calling context.
  167. @param ExtendedVerification Driver may perform diagnostics on reset.
  168. @retval EFI_SUCCESS The device was reset.
  169. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  170. not be reset.
  171. **/
  172. EFI_STATUS
  173. EFIAPI
  174. EmuBlockIoReset (
  175. IN EFI_BLOCK_IO_PROTOCOL *This,
  176. IN BOOLEAN ExtendedVerification
  177. )
  178. {
  179. EFI_STATUS Status;
  180. EMU_BLOCK_IO_PRIVATE *Private;
  181. EFI_TPL OldTpl;
  182. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  183. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  184. Status = Private->Io->Reset (Private->Io, ExtendedVerification);
  185. gBS->RestoreTPL (OldTpl);
  186. return Status;
  187. }
  188. /**
  189. Read BufferSize bytes from Lba into Buffer.
  190. @param This Indicates a pointer to the calling context.
  191. @param MediaId Id of the media, changes every time the media is replaced.
  192. @param Lba The starting Logical Block Address to read from
  193. @param BufferSize Size of Buffer, must be a multiple of device block size.
  194. @param Buffer A pointer to the destination buffer for the data. The caller is
  195. responsible for either having implicit or explicit ownership of the buffer.
  196. @retval EFI_SUCCESS The data was read correctly from the device.
  197. @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
  198. @retval EFI_NO_MEDIA There is no media in the device.
  199. @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
  200. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  201. @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
  202. or the buffer is not on proper alignment.
  203. **/
  204. EFI_STATUS
  205. EFIAPI
  206. EmuBlockIoReadBlocks (
  207. IN EFI_BLOCK_IO_PROTOCOL *This,
  208. IN UINT32 MediaId,
  209. IN EFI_LBA Lba,
  210. IN UINTN BufferSize,
  211. OUT VOID *Buffer
  212. )
  213. {
  214. EFI_STATUS Status;
  215. EMU_BLOCK_IO_PRIVATE *Private;
  216. EFI_TPL OldTpl;
  217. EFI_BLOCK_IO2_TOKEN Token;
  218. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  219. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  220. Token.Event = NULL;
  221. Status = Private->Io->ReadBlocks (Private->Io, MediaId, Lba, &Token, BufferSize, Buffer);
  222. gBS->RestoreTPL (OldTpl);
  223. return Status;
  224. }
  225. /**
  226. Write BufferSize bytes from Lba into Buffer.
  227. @param This Indicates a pointer to the calling context.
  228. @param MediaId The media ID that the write request is for.
  229. @param Lba The starting logical block address to be written. The caller is
  230. responsible for writing to only legitimate locations.
  231. @param BufferSize Size of Buffer, must be a multiple of device block size.
  232. @param Buffer A pointer to the source buffer for the data.
  233. @retval EFI_SUCCESS The data was written correctly to the device.
  234. @retval EFI_WRITE_PROTECTED The device can not be written to.
  235. @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
  236. @retval EFI_NO_MEDIA There is no media in the device.
  237. @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
  238. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  239. @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
  240. or the buffer is not on proper alignment.
  241. **/
  242. EFI_STATUS
  243. EFIAPI
  244. EmuBlockIoWriteBlocks (
  245. IN EFI_BLOCK_IO_PROTOCOL *This,
  246. IN UINT32 MediaId,
  247. IN EFI_LBA Lba,
  248. IN UINTN BufferSize,
  249. IN VOID *Buffer
  250. )
  251. {
  252. EFI_STATUS Status;
  253. EMU_BLOCK_IO_PRIVATE *Private;
  254. EFI_TPL OldTpl;
  255. EFI_BLOCK_IO2_TOKEN Token;
  256. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  257. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  258. Token.Event = NULL;
  259. Status = Private->Io->WriteBlocks (Private->Io, MediaId, Lba, &Token, BufferSize, Buffer);
  260. gBS->RestoreTPL (OldTpl);
  261. return Status;
  262. }
  263. /**
  264. Flush the Block Device.
  265. @param This Indicates a pointer to the calling context.
  266. @retval EFI_SUCCESS All outstanding data was written to the device
  267. @retval EFI_DEVICE_ERROR The device reported an error while writting back the data
  268. @retval EFI_NO_MEDIA There is no media in the device.
  269. **/
  270. EFI_STATUS
  271. EFIAPI
  272. EmuBlockIoFlushBlocks (
  273. IN EFI_BLOCK_IO_PROTOCOL *This
  274. )
  275. {
  276. EFI_STATUS Status;
  277. EMU_BLOCK_IO_PRIVATE *Private;
  278. EFI_TPL OldTpl;
  279. EFI_BLOCK_IO2_TOKEN Token;
  280. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  281. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  282. Token.Event = NULL;
  283. Status = Private->Io->FlushBlocks (Private->Io, &Token);
  284. gBS->RestoreTPL (OldTpl);
  285. return Status;
  286. }
  287. /**
  288. Tests to see if this driver supports a given controller. If a child device is provided,
  289. it further tests to see if this driver supports creating a handle for the specified child device.
  290. This function checks to see if the driver specified by This supports the device specified by
  291. ControllerHandle. Drivers will typically use the device path attached to
  292. ControllerHandle and/or the services from the bus I/O abstraction attached to
  293. ControllerHandle to determine if the driver supports ControllerHandle. This function
  294. may be called many times during platform initialization. In order to reduce boot times, the tests
  295. performed by this function must be very small, and take as little time as possible to execute. This
  296. function must not change the state of any hardware devices, and this function must be aware that the
  297. device specified by ControllerHandle may already be managed by the same driver or a
  298. different driver. This function must match its calls to AllocatePages() with FreePages(),
  299. AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
  300. Because ControllerHandle may have been previously started by the same driver, if a protocol is
  301. already in the opened state, then it must not be closed with CloseProtocol(). This is required
  302. to guarantee the state of ControllerHandle is not modified by this function.
  303. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  304. @param[in] ControllerHandle The handle of the controller to test. This handle
  305. must support a protocol interface that supplies
  306. an I/O abstraction to the driver.
  307. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  308. parameter is ignored by device drivers, and is optional for bus
  309. drivers. For bus drivers, if this parameter is not NULL, then
  310. the bus driver must determine if the bus controller specified
  311. by ControllerHandle and the child controller specified
  312. by RemainingDevicePath are both supported by this
  313. bus driver.
  314. @retval EFI_SUCCESS The device specified by ControllerHandle and
  315. RemainingDevicePath is supported by the driver specified by This.
  316. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  317. RemainingDevicePath is already being managed by the driver
  318. specified by This.
  319. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  320. RemainingDevicePath is already being managed by a different
  321. driver or an application that requires exclusive access.
  322. Currently not implemented.
  323. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  324. RemainingDevicePath is not supported by the driver specified by This.
  325. **/
  326. EFI_STATUS
  327. EFIAPI
  328. EmuBlockIoDriverBindingSupported (
  329. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  330. IN EFI_HANDLE Handle,
  331. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  332. )
  333. {
  334. EFI_STATUS Status;
  335. EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
  336. //
  337. // Open the IO Abstraction(s) needed to perform the supported test
  338. //
  339. Status = gBS->OpenProtocol (
  340. Handle,
  341. &gEmuIoThunkProtocolGuid,
  342. (VOID **)&EmuIoThunk,
  343. This->DriverBindingHandle,
  344. Handle,
  345. EFI_OPEN_PROTOCOL_BY_DRIVER
  346. );
  347. if (EFI_ERROR (Status)) {
  348. return Status;
  349. }
  350. //
  351. // Make sure GUID is for a File System handle.
  352. //
  353. Status = EFI_UNSUPPORTED;
  354. if (CompareGuid (EmuIoThunk->Protocol, &gEmuBlockIoProtocolGuid)) {
  355. Status = EFI_SUCCESS;
  356. }
  357. //
  358. // Close the I/O Abstraction(s) used to perform the supported test
  359. //
  360. gBS->CloseProtocol (
  361. Handle,
  362. &gEmuIoThunkProtocolGuid,
  363. This->DriverBindingHandle,
  364. Handle
  365. );
  366. return Status;
  367. }
  368. /**
  369. Starts a device controller or a bus controller.
  370. The Start() function is designed to be invoked from the EFI boot service ConnectController().
  371. As a result, much of the error checking on the parameters to Start() has been moved into this
  372. common boot service. It is legal to call Start() from other locations,
  373. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  374. 1. ControllerHandle must be a valid EFI_HANDLE.
  375. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
  376. EFI_DEVICE_PATH_PROTOCOL.
  377. 3. Prior to calling Start(), the Supported() function for the driver specified by This must
  378. have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
  379. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  380. @param[in] ControllerHandle The handle of the controller to start. This handle
  381. must support a protocol interface that supplies
  382. an I/O abstraction to the driver.
  383. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  384. parameter is ignored by device drivers, and is optional for bus
  385. drivers. For a bus driver, if this parameter is NULL, then handles
  386. for all the children of Controller are created by this driver.
  387. If this parameter is not NULL and the first Device Path Node is
  388. not the End of Device Path Node, then only the handle for the
  389. child device specified by the first Device Path Node of
  390. RemainingDevicePath is created by this driver.
  391. If the first Device Path Node of RemainingDevicePath is
  392. the End of Device Path Node, no child handle is created by this
  393. driver.
  394. @retval EFI_SUCCESS The device was started.
  395. @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
  396. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  397. @retval Others The driver failded to start the device.
  398. **/
  399. EFI_STATUS
  400. EFIAPI
  401. EmuBlockIoDriverBindingStart (
  402. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  403. IN EFI_HANDLE Handle,
  404. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  405. )
  406. {
  407. EFI_STATUS Status;
  408. EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
  409. EMU_BLOCK_IO_PRIVATE *Private = NULL;
  410. //
  411. // Grab the protocols we need
  412. //
  413. Status = gBS->OpenProtocol (
  414. Handle,
  415. &gEmuIoThunkProtocolGuid,
  416. (void *)&EmuIoThunk,
  417. This->DriverBindingHandle,
  418. Handle,
  419. EFI_OPEN_PROTOCOL_BY_DRIVER
  420. );
  421. if (EFI_ERROR (Status)) {
  422. return Status;
  423. }
  424. if (!CompareGuid (EmuIoThunk->Protocol, &gEmuBlockIoProtocolGuid)) {
  425. Status = EFI_UNSUPPORTED;
  426. goto Done;
  427. }
  428. Status = EmuIoThunk->Open (EmuIoThunk);
  429. if (EFI_ERROR (Status)) {
  430. goto Done;
  431. }
  432. Private = AllocatePool (sizeof (EMU_BLOCK_IO_PRIVATE));
  433. if (Private == NULL) {
  434. goto Done;
  435. }
  436. Private->Signature = EMU_BLOCK_IO_PRIVATE_SIGNATURE;
  437. Private->IoThunk = EmuIoThunk;
  438. Private->Io = EmuIoThunk->Interface;
  439. Private->EfiHandle = Handle;
  440. Private->BlockIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;
  441. Private->BlockIo.Media = &Private->Media;
  442. Private->BlockIo.Reset = EmuBlockIoReset;
  443. Private->BlockIo.ReadBlocks = EmuBlockIoReadBlocks;
  444. Private->BlockIo.WriteBlocks = EmuBlockIoWriteBlocks;
  445. Private->BlockIo.FlushBlocks = EmuBlockIoFlushBlocks;
  446. Private->BlockIo2.Media = &Private->Media;
  447. Private->BlockIo2.Reset = EmuBlockIo2Reset;
  448. Private->BlockIo2.ReadBlocksEx = EmuBlockIo2ReadBlocksEx;
  449. Private->BlockIo2.WriteBlocksEx = EmuBlockIo2WriteBlocksEx;
  450. Private->BlockIo2.FlushBlocksEx = EmuBlockIo2Flush;
  451. Private->ControllerNameTable = NULL;
  452. Status = Private->Io->CreateMapping (Private->Io, &Private->Media);
  453. if (EFI_ERROR (Status)) {
  454. goto Done;
  455. }
  456. AddUnicodeString2 (
  457. "eng",
  458. gEmuBlockIoComponentName.SupportedLanguages,
  459. &Private->ControllerNameTable,
  460. EmuIoThunk->ConfigString,
  461. TRUE
  462. );
  463. AddUnicodeString2 (
  464. "en",
  465. gEmuBlockIoComponentName2.SupportedLanguages,
  466. &Private->ControllerNameTable,
  467. EmuIoThunk->ConfigString,
  468. FALSE
  469. );
  470. Status = gBS->InstallMultipleProtocolInterfaces (
  471. &Handle,
  472. &gEfiBlockIoProtocolGuid,
  473. &Private->BlockIo,
  474. &gEfiBlockIo2ProtocolGuid,
  475. &Private->BlockIo2,
  476. NULL
  477. );
  478. Done:
  479. if (EFI_ERROR (Status)) {
  480. if (Private != NULL) {
  481. if (Private->ControllerNameTable != NULL) {
  482. FreeUnicodeStringTable (Private->ControllerNameTable);
  483. }
  484. gBS->FreePool (Private);
  485. }
  486. gBS->CloseProtocol (
  487. Handle,
  488. &gEmuIoThunkProtocolGuid,
  489. This->DriverBindingHandle,
  490. Handle
  491. );
  492. }
  493. return Status;
  494. }
  495. /**
  496. Stops a device controller or a bus controller.
  497. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
  498. As a result, much of the error checking on the parameters to Stop() has been moved
  499. into this common boot service. It is legal to call Stop() from other locations,
  500. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  501. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
  502. same driver's Start() function.
  503. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
  504. EFI_HANDLE. In addition, all of these handles must have been created in this driver's
  505. Start() function, and the Start() function must have called OpenProtocol() on
  506. ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  507. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  508. @param[in] ControllerHandle A handle to the device being stopped. The handle must
  509. support a bus specific I/O protocol for the driver
  510. to use to stop the device.
  511. @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
  512. @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
  513. if NumberOfChildren is 0.
  514. @retval EFI_SUCCESS The device was stopped.
  515. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  516. **/
  517. EFI_STATUS
  518. EFIAPI
  519. EmuBlockIoDriverBindingStop (
  520. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  521. IN EFI_HANDLE Handle,
  522. IN UINTN NumberOfChildren,
  523. IN EFI_HANDLE *ChildHandleBuffer
  524. )
  525. {
  526. EFI_BLOCK_IO_PROTOCOL *BlockIo;
  527. EFI_STATUS Status;
  528. EMU_BLOCK_IO_PRIVATE *Private;
  529. //
  530. // Get our context back
  531. //
  532. Status = gBS->OpenProtocol (
  533. Handle,
  534. &gEfiBlockIoProtocolGuid,
  535. (void *)&BlockIo,
  536. This->DriverBindingHandle,
  537. Handle,
  538. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  539. );
  540. if (EFI_ERROR (Status)) {
  541. return EFI_UNSUPPORTED;
  542. }
  543. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (BlockIo);
  544. Status = gBS->UninstallMultipleProtocolInterfaces (
  545. Private->EfiHandle,
  546. &gEfiBlockIoProtocolGuid,
  547. &Private->BlockIo,
  548. &gEfiBlockIo2ProtocolGuid,
  549. &Private->BlockIo2,
  550. NULL
  551. );
  552. if (!EFI_ERROR (Status)) {
  553. Status = gBS->CloseProtocol (
  554. Handle,
  555. &gEmuIoThunkProtocolGuid,
  556. This->DriverBindingHandle,
  557. Handle
  558. );
  559. ASSERT_EFI_ERROR (Status);
  560. //
  561. // Destroy the IO interface.
  562. //
  563. Status = Private->IoThunk->Close (Private->IoThunk);
  564. ASSERT_EFI_ERROR (Status);
  565. //
  566. // Free our instance data
  567. //
  568. FreeUnicodeStringTable (Private->ControllerNameTable);
  569. FreePool (Private);
  570. }
  571. return Status;
  572. }
  573. EFI_DRIVER_BINDING_PROTOCOL gEmuBlockIoDriverBinding = {
  574. EmuBlockIoDriverBindingSupported,
  575. EmuBlockIoDriverBindingStart,
  576. EmuBlockIoDriverBindingStop,
  577. 0xa,
  578. NULL,
  579. NULL
  580. };
  581. /**
  582. The user Entry Point for module EmuBlockIo . The user code starts with this function.
  583. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  584. @param[in] SystemTable A pointer to the EFI System Table.
  585. @retval EFI_SUCCESS The entry point is executed successfully.
  586. @retval other Some error occurs when executing this entry point.
  587. **/
  588. EFI_STATUS
  589. EFIAPI
  590. InitializeEmuBlockIo (
  591. IN EFI_HANDLE ImageHandle,
  592. IN EFI_SYSTEM_TABLE *SystemTable
  593. )
  594. {
  595. EFI_STATUS Status;
  596. Status = EfiLibInstallAllDriverProtocols2 (
  597. ImageHandle,
  598. SystemTable,
  599. &gEmuBlockIoDriverBinding,
  600. ImageHandle,
  601. &gEmuBlockIoComponentName,
  602. &gEmuBlockIoComponentName2,
  603. NULL,
  604. NULL,
  605. &gEmuBlockIoDriverDiagnostics,
  606. &gEmuBlockIoDriverDiagnostics2
  607. );
  608. ASSERT_EFI_ERROR (Status);
  609. return Status;
  610. }