BlockIo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /**@file
  2. Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "Host.h"
  6. #define EMU_BLOCK_IO_PRIVATE_SIGNATURE SIGNATURE_32 ('E', 'M', 'b', 'k')
  7. typedef struct {
  8. UINTN Signature;
  9. EMU_IO_THUNK_PROTOCOL *Thunk;
  10. char *Filename;
  11. UINTN ReadMode;
  12. UINTN Mode;
  13. int fd;
  14. BOOLEAN RemovableMedia;
  15. BOOLEAN WriteProtected;
  16. UINT64 NumberOfBlocks;
  17. UINT32 BlockSize;
  18. EMU_BLOCK_IO_PROTOCOL EmuBlockIo;
  19. EFI_BLOCK_IO_MEDIA *Media;
  20. } EMU_BLOCK_IO_PRIVATE;
  21. #define EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS(a) \
  22. CR(a, EMU_BLOCK_IO_PRIVATE, EmuBlockIo, EMU_BLOCK_IO_PRIVATE_SIGNATURE)
  23. EFI_STATUS
  24. EmuBlockIoReset (
  25. IN EMU_BLOCK_IO_PROTOCOL *This,
  26. IN BOOLEAN ExtendedVerification
  27. );
  28. /*++
  29. This function extends the capability of SetFilePointer to accept 64 bit parameters
  30. **/
  31. EFI_STATUS
  32. SetFilePointer64 (
  33. IN EMU_BLOCK_IO_PRIVATE *Private,
  34. IN INT64 DistanceToMove,
  35. OUT UINT64 *NewFilePointer,
  36. IN INT32 MoveMethod
  37. )
  38. {
  39. EFI_STATUS Status;
  40. off_t res;
  41. off_t offset = DistanceToMove;
  42. Status = EFI_SUCCESS;
  43. res = lseek (Private->fd, offset, (int)MoveMethod);
  44. if (res == -1) {
  45. Status = EFI_INVALID_PARAMETER;
  46. }
  47. if (NewFilePointer != NULL) {
  48. *NewFilePointer = res;
  49. }
  50. return Status;
  51. }
  52. EFI_STATUS
  53. EmuBlockIoOpenDevice (
  54. IN EMU_BLOCK_IO_PRIVATE *Private
  55. )
  56. {
  57. EFI_STATUS Status;
  58. UINT64 FileSize;
  59. struct statfs buf;
  60. //
  61. // If the device is already opened, close it
  62. //
  63. if (Private->fd >= 0) {
  64. EmuBlockIoReset (&Private->EmuBlockIo, FALSE);
  65. }
  66. //
  67. // Open the device
  68. //
  69. Private->fd = open (Private->Filename, Private->Mode, 0644);
  70. if (Private->fd < 0) {
  71. printf ("EmuOpenBlock: Could not open %s: %s\n", Private->Filename, strerror (errno));
  72. Private->Media->MediaPresent = FALSE;
  73. Status = EFI_NO_MEDIA;
  74. goto Done;
  75. }
  76. if (!Private->Media->MediaPresent) {
  77. //
  78. // BugBug: try to emulate if a CD appears - notify drivers to check it out
  79. //
  80. Private->Media->MediaPresent = TRUE;
  81. }
  82. //
  83. // get the size of the file
  84. //
  85. Status = SetFilePointer64 (Private, 0, &FileSize, SEEK_END);
  86. if (EFI_ERROR (Status)) {
  87. printf ("EmuOpenBlock: Could not get filesize of %s\n", Private->Filename);
  88. Status = EFI_UNSUPPORTED;
  89. goto Done;
  90. }
  91. if (FileSize == 0) {
  92. // lseek fails on a real device. ioctl calls are OS specific
  93. #if __APPLE__
  94. {
  95. UINT32 BlockSize;
  96. if (ioctl (Private->fd, DKIOCGETBLOCKSIZE, &BlockSize) == 0) {
  97. Private->Media->BlockSize = BlockSize;
  98. }
  99. if (ioctl (Private->fd, DKIOCGETBLOCKCOUNT, &Private->NumberOfBlocks) == 0) {
  100. if ((Private->NumberOfBlocks == 0) && (BlockSize == 0x800)) {
  101. // A DVD is ~ 4.37 GB so make up a number
  102. Private->Media->LastBlock = (0x100000000ULL/0x800) - 1;
  103. } else {
  104. Private->Media->LastBlock = Private->NumberOfBlocks - 1;
  105. }
  106. }
  107. ioctl (Private->fd, DKIOCGETMAXBLOCKCOUNTWRITE, &Private->Media->OptimalTransferLengthGranularity);
  108. }
  109. #else
  110. {
  111. size_t BlockSize;
  112. UINT64 DiskSize;
  113. if (ioctl (Private->fd, BLKSSZGET, &BlockSize) == 0) {
  114. Private->Media->BlockSize = BlockSize;
  115. }
  116. if (ioctl (Private->fd, BLKGETSIZE64, &DiskSize) == 0) {
  117. Private->NumberOfBlocks = DivU64x32 (DiskSize, (UINT32)BlockSize);
  118. Private->Media->LastBlock = Private->NumberOfBlocks - 1;
  119. }
  120. }
  121. #endif
  122. } else {
  123. Private->Media->BlockSize = Private->BlockSize;
  124. Private->NumberOfBlocks = DivU64x32 (FileSize, Private->Media->BlockSize);
  125. Private->Media->LastBlock = Private->NumberOfBlocks - 1;
  126. if (fstatfs (Private->fd, &buf) == 0) {
  127. #if __APPLE__
  128. Private->Media->OptimalTransferLengthGranularity = buf.f_iosize/buf.f_bsize;
  129. #else
  130. Private->Media->OptimalTransferLengthGranularity = buf.f_bsize/buf.f_bsize;
  131. #endif
  132. }
  133. }
  134. DEBUG ((DEBUG_INIT, "%HEmuOpenBlock: opened %a%N\n", Private->Filename));
  135. Status = EFI_SUCCESS;
  136. Done:
  137. if (EFI_ERROR (Status)) {
  138. if (Private->fd >= 0) {
  139. EmuBlockIoReset (&Private->EmuBlockIo, FALSE);
  140. }
  141. }
  142. return Status;
  143. }
  144. EFI_STATUS
  145. EmuBlockIoCreateMapping (
  146. IN EMU_BLOCK_IO_PROTOCOL *This,
  147. IN EFI_BLOCK_IO_MEDIA *Media
  148. )
  149. {
  150. EFI_STATUS Status;
  151. EMU_BLOCK_IO_PRIVATE *Private;
  152. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  153. Private->Media = Media;
  154. Media->MediaId = 0;
  155. Media->RemovableMedia = Private->RemovableMedia;
  156. Media->MediaPresent = TRUE;
  157. Media->LogicalPartition = FALSE;
  158. Media->ReadOnly = Private->WriteProtected;
  159. Media->WriteCaching = FALSE;
  160. Media->IoAlign = 1;
  161. Media->LastBlock = 0; // Filled in by OpenDevice
  162. // EFI_BLOCK_IO_PROTOCOL_REVISION2
  163. Media->LowestAlignedLba = 0;
  164. Media->LogicalBlocksPerPhysicalBlock = 0;
  165. // EFI_BLOCK_IO_PROTOCOL_REVISION3
  166. Media->OptimalTransferLengthGranularity = 0;
  167. Status = EmuBlockIoOpenDevice (Private);
  168. return Status;
  169. }
  170. EFI_STATUS
  171. EmuBlockIoError (
  172. IN EMU_BLOCK_IO_PRIVATE *Private
  173. )
  174. {
  175. EFI_STATUS Status;
  176. BOOLEAN ReinstallBlockIoFlag;
  177. switch (errno) {
  178. case EAGAIN:
  179. Status = EFI_NO_MEDIA;
  180. Private->Media->ReadOnly = FALSE;
  181. Private->Media->MediaPresent = FALSE;
  182. ReinstallBlockIoFlag = FALSE;
  183. break;
  184. case EACCES:
  185. Private->Media->ReadOnly = FALSE;
  186. Private->Media->MediaPresent = TRUE;
  187. Private->Media->MediaId += 1;
  188. ReinstallBlockIoFlag = TRUE;
  189. Status = EFI_MEDIA_CHANGED;
  190. break;
  191. case EROFS:
  192. Private->Media->ReadOnly = TRUE;
  193. ReinstallBlockIoFlag = FALSE;
  194. Status = EFI_WRITE_PROTECTED;
  195. break;
  196. default:
  197. ReinstallBlockIoFlag = FALSE;
  198. Status = EFI_DEVICE_ERROR;
  199. break;
  200. }
  201. return Status;
  202. }
  203. EFI_STATUS
  204. EmuBlockIoReadWriteCommon (
  205. IN EMU_BLOCK_IO_PRIVATE *Private,
  206. IN UINT32 MediaId,
  207. IN EFI_LBA Lba,
  208. IN UINTN BufferSize,
  209. IN VOID *Buffer,
  210. IN CHAR8 *CallerName
  211. )
  212. {
  213. EFI_STATUS Status;
  214. UINTN BlockSize;
  215. UINT64 LastBlock;
  216. INT64 DistanceToMove;
  217. UINT64 DistanceMoved;
  218. if (Private->fd < 0) {
  219. Status = EmuBlockIoOpenDevice (Private);
  220. if (EFI_ERROR (Status)) {
  221. return Status;
  222. }
  223. }
  224. if (!Private->Media->MediaPresent) {
  225. DEBUG ((DEBUG_INIT, "%s: No Media\n", CallerName));
  226. return EFI_NO_MEDIA;
  227. }
  228. if (Private->Media->MediaId != MediaId) {
  229. return EFI_MEDIA_CHANGED;
  230. }
  231. if ((UINTN)Buffer % Private->Media->IoAlign != 0) {
  232. return EFI_INVALID_PARAMETER;
  233. }
  234. //
  235. // Verify buffer size
  236. //
  237. BlockSize = Private->Media->BlockSize;
  238. if (BufferSize == 0) {
  239. DEBUG ((DEBUG_INIT, "%s: Zero length read\n", CallerName));
  240. return EFI_SUCCESS;
  241. }
  242. if ((BufferSize % BlockSize) != 0) {
  243. DEBUG ((DEBUG_INIT, "%s: Invalid read size\n", CallerName));
  244. return EFI_BAD_BUFFER_SIZE;
  245. }
  246. LastBlock = Lba + (BufferSize / BlockSize) - 1;
  247. if (LastBlock > Private->Media->LastBlock) {
  248. DEBUG ((DEBUG_INIT, "ReadBlocks: Attempted to read off end of device\n"));
  249. return EFI_INVALID_PARAMETER;
  250. }
  251. //
  252. // Seek to End of File
  253. //
  254. DistanceToMove = MultU64x32 (Lba, BlockSize);
  255. Status = SetFilePointer64 (Private, DistanceToMove, &DistanceMoved, SEEK_SET);
  256. if (EFI_ERROR (Status)) {
  257. DEBUG ((DEBUG_INIT, "WriteBlocks: SetFilePointer failed\n"));
  258. return EmuBlockIoError (Private);
  259. }
  260. return EFI_SUCCESS;
  261. }
  262. /**
  263. Read BufferSize bytes from Lba into Buffer.
  264. This function reads the requested number of blocks from the device. All the
  265. blocks are read, or an error is returned.
  266. If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and
  267. non-blocking I/O is being used, the Event associated with this request will
  268. not be signaled.
  269. @param[in] This Indicates a pointer to the calling context.
  270. @param[in] MediaId Id of the media, changes every time the media is
  271. replaced.
  272. @param[in] Lba The starting Logical Block Address to read from.
  273. @param[in, out] Token A pointer to the token associated with the transaction.
  274. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  275. @param[out] Buffer A pointer to the destination buffer for the data. The
  276. caller is responsible for either having implicit or
  277. explicit ownership of the buffer.
  278. @retval EFI_SUCCESS The read request was queued if Token->Event is
  279. not NULL.The data was read correctly from the
  280. device if the Token->Event is NULL.
  281. @retval EFI_DEVICE_ERROR The device reported an error while performing
  282. the read.
  283. @retval EFI_NO_MEDIA There is no media in the device.
  284. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  285. @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
  286. intrinsic block size of the device.
  287. @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
  288. or the buffer is not on proper alignment.
  289. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  290. of resources.
  291. **/
  292. EFI_STATUS
  293. EmuBlockIoReadBlocks (
  294. IN EMU_BLOCK_IO_PROTOCOL *This,
  295. IN UINT32 MediaId,
  296. IN EFI_LBA LBA,
  297. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  298. IN UINTN BufferSize,
  299. OUT VOID *Buffer
  300. )
  301. {
  302. EFI_STATUS Status;
  303. EMU_BLOCK_IO_PRIVATE *Private;
  304. ssize_t len;
  305. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  306. Status = EmuBlockIoReadWriteCommon (Private, MediaId, LBA, BufferSize, Buffer, "UnixReadBlocks");
  307. if (EFI_ERROR (Status)) {
  308. goto Done;
  309. }
  310. len = read (Private->fd, Buffer, BufferSize);
  311. if (len != BufferSize) {
  312. DEBUG ((DEBUG_INIT, "ReadBlocks: ReadFile failed.\n"));
  313. Status = EmuBlockIoError (Private);
  314. goto Done;
  315. }
  316. //
  317. // If we read then media is present.
  318. //
  319. Private->Media->MediaPresent = TRUE;
  320. Status = EFI_SUCCESS;
  321. Done:
  322. if (Token != NULL) {
  323. if (Token->Event != NULL) {
  324. // Caller is responsible for signaling EFI Event
  325. Token->TransactionStatus = Status;
  326. return EFI_SUCCESS;
  327. }
  328. }
  329. return Status;
  330. }
  331. /**
  332. Write BufferSize bytes from Lba into Buffer.
  333. This function writes the requested number of blocks to the device. All blocks
  334. are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,
  335. EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is
  336. being used, the Event associated with this request will not be signaled.
  337. @param[in] This Indicates a pointer to the calling context.
  338. @param[in] MediaId The media ID that the write request is for.
  339. @param[in] Lba The starting logical block address to be written. The
  340. caller is responsible for writing to only legitimate
  341. locations.
  342. @param[in, out] Token A pointer to the token associated with the transaction.
  343. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  344. @param[in] Buffer A pointer to the source buffer for the data.
  345. @retval EFI_SUCCESS The write request was queued if Event is not NULL.
  346. The data was written correctly to the device if
  347. the Event is NULL.
  348. @retval EFI_WRITE_PROTECTED The device can not be written to.
  349. @retval EFI_NO_MEDIA There is no media in the device.
  350. @retval EFI_MEDIA_CHANGED The MediaId does not match the current device.
  351. @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
  352. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  353. @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
  354. or the buffer is not on proper alignment.
  355. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  356. of resources.
  357. **/
  358. EFI_STATUS
  359. EmuBlockIoWriteBlocks (
  360. IN EMU_BLOCK_IO_PROTOCOL *This,
  361. IN UINT32 MediaId,
  362. IN EFI_LBA LBA,
  363. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  364. IN UINTN BufferSize,
  365. IN VOID *Buffer
  366. )
  367. {
  368. EMU_BLOCK_IO_PRIVATE *Private;
  369. ssize_t len;
  370. EFI_STATUS Status;
  371. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  372. Status = EmuBlockIoReadWriteCommon (Private, MediaId, LBA, BufferSize, Buffer, "UnixWriteBlocks");
  373. if (EFI_ERROR (Status)) {
  374. goto Done;
  375. }
  376. len = write (Private->fd, Buffer, BufferSize);
  377. if (len != BufferSize) {
  378. DEBUG ((DEBUG_INIT, "ReadBlocks: WriteFile failed.\n"));
  379. Status = EmuBlockIoError (Private);
  380. goto Done;
  381. }
  382. //
  383. // If the write succeeded, we are not write protected and media is present.
  384. //
  385. Private->Media->MediaPresent = TRUE;
  386. Private->Media->ReadOnly = FALSE;
  387. Status = EFI_SUCCESS;
  388. Done:
  389. if (Token != NULL) {
  390. if (Token->Event != NULL) {
  391. // Caller is responsible for signaling EFI Event
  392. Token->TransactionStatus = Status;
  393. return EFI_SUCCESS;
  394. }
  395. }
  396. return Status;
  397. }
  398. /**
  399. Flush the Block Device.
  400. If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED
  401. is returned and non-blocking I/O is being used, the Event associated with
  402. this request will not be signaled.
  403. @param[in] This Indicates a pointer to the calling context.
  404. @param[in,out] Token A pointer to the token associated with the transaction
  405. @retval EFI_SUCCESS The flush request was queued if Event is not NULL.
  406. All outstanding data was written correctly to the
  407. device if the Event is NULL.
  408. @retval EFI_DEVICE_ERROR The device reported an error while writing back
  409. the data.
  410. @retval EFI_WRITE_PROTECTED The device cannot be written to.
  411. @retval EFI_NO_MEDIA There is no media in the device.
  412. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  413. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  414. of resources.
  415. **/
  416. EFI_STATUS
  417. EmuBlockIoFlushBlocks (
  418. IN EMU_BLOCK_IO_PROTOCOL *This,
  419. IN OUT EFI_BLOCK_IO2_TOKEN *Token
  420. )
  421. {
  422. EMU_BLOCK_IO_PRIVATE *Private;
  423. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  424. if (Private->fd >= 0) {
  425. fsync (Private->fd);
  426. #if __APPLE__
  427. fcntl (Private->fd, F_FULLFSYNC);
  428. #endif
  429. }
  430. if (Token != NULL) {
  431. if (Token->Event != NULL) {
  432. // Caller is responsible for signaling EFI Event
  433. Token->TransactionStatus = EFI_SUCCESS;
  434. return EFI_SUCCESS;
  435. }
  436. }
  437. return EFI_SUCCESS;
  438. }
  439. /**
  440. Reset the block device hardware.
  441. @param[in] This Indicates a pointer to the calling context.
  442. @param[in] ExtendedVerification Indicates that the driver may perform a more
  443. exhaustive verification operation of the device
  444. during reset.
  445. @retval EFI_SUCCESS The device was reset.
  446. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  447. not be reset.
  448. **/
  449. EFI_STATUS
  450. EmuBlockIoReset (
  451. IN EMU_BLOCK_IO_PROTOCOL *This,
  452. IN BOOLEAN ExtendedVerification
  453. )
  454. {
  455. EMU_BLOCK_IO_PRIVATE *Private;
  456. Private = EMU_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  457. if (Private->fd >= 0) {
  458. close (Private->fd);
  459. Private->fd = -1;
  460. }
  461. return EFI_SUCCESS;
  462. }
  463. char *
  464. StdDupUnicodeToAscii (
  465. IN CHAR16 *Str
  466. )
  467. {
  468. UINTN Size;
  469. char *Ascii;
  470. char *Ptr;
  471. Size = StrLen (Str) + 1;
  472. Ascii = malloc (Size);
  473. if (Ascii == NULL) {
  474. return NULL;
  475. }
  476. for (Ptr = Ascii; *Str != '\0'; Ptr++, Str++) {
  477. *Ptr = *Str;
  478. }
  479. *Ptr = 0;
  480. return Ascii;
  481. }
  482. EMU_BLOCK_IO_PROTOCOL gEmuBlockIoProtocol = {
  483. GasketEmuBlockIoReset,
  484. GasketEmuBlockIoReadBlocks,
  485. GasketEmuBlockIoWriteBlocks,
  486. GasketEmuBlockIoFlushBlocks,
  487. GasketEmuBlockIoCreateMapping
  488. };
  489. EFI_STATUS
  490. EmuBlockIoThunkOpen (
  491. IN EMU_IO_THUNK_PROTOCOL *This
  492. )
  493. {
  494. EMU_BLOCK_IO_PRIVATE *Private;
  495. char *Str;
  496. if (This->Private != NULL) {
  497. return EFI_ALREADY_STARTED;
  498. }
  499. if (!CompareGuid (This->Protocol, &gEmuBlockIoProtocolGuid)) {
  500. return EFI_UNSUPPORTED;
  501. }
  502. Private = malloc (sizeof (EMU_BLOCK_IO_PRIVATE));
  503. if (Private == NULL) {
  504. return EFI_OUT_OF_RESOURCES;
  505. }
  506. Private->Signature = EMU_BLOCK_IO_PRIVATE_SIGNATURE;
  507. Private->Thunk = This;
  508. CopyMem (&Private->EmuBlockIo, &gEmuBlockIoProtocol, sizeof (gEmuBlockIoProtocol));
  509. Private->fd = -1;
  510. Private->BlockSize = 512;
  511. Private->Filename = StdDupUnicodeToAscii (This->ConfigString);
  512. if (Private->Filename == NULL) {
  513. return EFI_OUT_OF_RESOURCES;
  514. }
  515. Str = strstr (Private->Filename, ":");
  516. if (Str == NULL) {
  517. Private->RemovableMedia = FALSE;
  518. Private->WriteProtected = FALSE;
  519. } else {
  520. for (*Str++ = '\0'; *Str != 0; Str++) {
  521. if ((*Str == 'R') || (*Str == 'F')) {
  522. Private->RemovableMedia = (BOOLEAN)(*Str == 'R');
  523. }
  524. if ((*Str == 'O') || (*Str == 'W')) {
  525. Private->WriteProtected = (BOOLEAN)(*Str == 'O');
  526. }
  527. if (*Str == ':') {
  528. Private->BlockSize = strtol (++Str, NULL, 0);
  529. break;
  530. }
  531. }
  532. }
  533. Private->Mode = Private->WriteProtected ? O_RDONLY : O_RDWR;
  534. This->Interface = &Private->EmuBlockIo;
  535. This->Private = Private;
  536. return EFI_SUCCESS;
  537. }
  538. EFI_STATUS
  539. EmuBlockIoThunkClose (
  540. IN EMU_IO_THUNK_PROTOCOL *This
  541. )
  542. {
  543. EMU_BLOCK_IO_PRIVATE *Private;
  544. if (!CompareGuid (This->Protocol, &gEmuBlockIoProtocolGuid)) {
  545. return EFI_UNSUPPORTED;
  546. }
  547. Private = This->Private;
  548. if (This->Private != NULL) {
  549. if (Private->Filename != NULL) {
  550. free (Private->Filename);
  551. }
  552. free (This->Private);
  553. This->Private = NULL;
  554. }
  555. return EFI_SUCCESS;
  556. }
  557. EMU_IO_THUNK_PROTOCOL gBlockIoThunkIo = {
  558. &gEmuBlockIoProtocolGuid,
  559. NULL,
  560. NULL,
  561. 0,
  562. GasketBlockIoThunkOpen,
  563. GasketBlockIoThunkClose,
  564. NULL
  565. };