WinBlockIo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /**@file
  2. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "WinHost.h"
  6. #define WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE SIGNATURE_32 ('N', 'T', 'b', 'k')
  7. typedef struct {
  8. UINTN Signature;
  9. EMU_IO_THUNK_PROTOCOL *Thunk;
  10. CHAR16 *FileName;
  11. BOOLEAN Removable;
  12. BOOLEAN Readonly;
  13. HANDLE NtHandle;
  14. UINT32 BlockSize;
  15. EFI_BLOCK_IO_MEDIA *Media;
  16. EMU_BLOCK_IO_PROTOCOL EmuBlockIo;
  17. } WIN_NT_BLOCK_IO_PRIVATE;
  18. #define WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS(a) \
  19. CR(a, WIN_NT_BLOCK_IO_PRIVATE, EmuBlockIo, WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE)
  20. EFI_STATUS
  21. WinNtBlockIoReset (
  22. IN EMU_BLOCK_IO_PROTOCOL *This,
  23. IN BOOLEAN ExtendedVerification
  24. );
  25. EFI_STATUS
  26. SetFilePointer64 (
  27. IN WIN_NT_BLOCK_IO_PRIVATE *Private,
  28. IN INT64 DistanceToMove,
  29. OUT UINT64 *NewFilePointer,
  30. IN DWORD MoveMethod
  31. )
  32. /*++
  33. This function extends the capability of SetFilePointer to accept 64 bit parameters
  34. --*/
  35. {
  36. EFI_STATUS Status;
  37. LARGE_INTEGER LargeInt;
  38. LargeInt.QuadPart = DistanceToMove;
  39. Status = EFI_SUCCESS;
  40. LargeInt.LowPart = SetFilePointer (
  41. Private->NtHandle,
  42. LargeInt.LowPart,
  43. &LargeInt.HighPart,
  44. MoveMethod
  45. );
  46. if ((LargeInt.LowPart == -1) && (GetLastError () != NO_ERROR)) {
  47. Status = EFI_INVALID_PARAMETER;
  48. }
  49. if (NewFilePointer != NULL) {
  50. *NewFilePointer = LargeInt.QuadPart;
  51. }
  52. return Status;
  53. }
  54. EFI_STATUS
  55. WinNtBlockIoOpenDevice (
  56. IN WIN_NT_BLOCK_IO_PRIVATE *Private,
  57. IN EFI_BLOCK_IO_MEDIA *Media
  58. )
  59. {
  60. EFI_STATUS Status;
  61. UINT64 FileSize;
  62. //
  63. // If the device is already opened, close it
  64. //
  65. if (Private->NtHandle != INVALID_HANDLE_VALUE) {
  66. WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);
  67. }
  68. //
  69. // Open the device
  70. //
  71. Private->NtHandle = CreateFile (
  72. Private->FileName,
  73. GENERIC_READ | (Private->Readonly ? 0 : GENERIC_WRITE),
  74. FILE_SHARE_READ | FILE_SHARE_WRITE,
  75. NULL,
  76. OPEN_ALWAYS, // Create if it doesn't exist
  77. 0,
  78. NULL
  79. );
  80. if (Private->NtHandle == INVALID_HANDLE_VALUE) {
  81. DEBUG ((DEBUG_INFO, "OpenBlock: Could not open %S, %x\n", Private->FileName, GetLastError ()));
  82. Media->MediaPresent = FALSE;
  83. Status = EFI_NO_MEDIA;
  84. goto Done;
  85. }
  86. //
  87. // get the size of the file
  88. //
  89. Status = SetFilePointer64 (Private, 0, &FileSize, FILE_END);
  90. if (EFI_ERROR (Status)) {
  91. DEBUG ((DEBUG_ERROR, "OpenBlock: Could not get filesize of %s\n", Private->FileName));
  92. Status = EFI_UNSUPPORTED;
  93. goto Done;
  94. }
  95. Media->LastBlock = DivU64x32 (FileSize, (UINT32)Private->BlockSize) - 1;
  96. DEBUG ((DEBUG_INIT, "OpenBlock: opened %S\n", Private->FileName));
  97. Status = EFI_SUCCESS;
  98. Done:
  99. if (EFI_ERROR (Status)) {
  100. if (Private->NtHandle != INVALID_HANDLE_VALUE) {
  101. WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);
  102. }
  103. }
  104. return Status;
  105. }
  106. EFI_STATUS
  107. EFIAPI
  108. WinNtBlockIoCreateMapping (
  109. IN EMU_BLOCK_IO_PROTOCOL *This,
  110. IN EFI_BLOCK_IO_MEDIA *Media
  111. )
  112. {
  113. WIN_NT_BLOCK_IO_PRIVATE *Private;
  114. Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  115. Media->MediaId = 0;
  116. Media->RemovableMedia = Private->Removable;
  117. Media->MediaPresent = TRUE;
  118. Media->LogicalPartition = FALSE;
  119. Media->ReadOnly = Private->Readonly;
  120. Media->WriteCaching = FALSE;
  121. Media->IoAlign = 1;
  122. Media->LastBlock = 0; // Filled in by OpenDevice
  123. Media->BlockSize = Private->BlockSize;
  124. // EFI_BLOCK_IO_PROTOCOL_REVISION2
  125. Media->LowestAlignedLba = 0;
  126. Media->LogicalBlocksPerPhysicalBlock = 0;
  127. // EFI_BLOCK_IO_PROTOCOL_REVISION3
  128. Media->OptimalTransferLengthGranularity = 0;
  129. //
  130. // Remember the Media pointer.
  131. //
  132. Private->Media = Media;
  133. return WinNtBlockIoOpenDevice (Private, Media);
  134. }
  135. EFI_STATUS
  136. WinNtBlockIoError (
  137. IN WIN_NT_BLOCK_IO_PRIVATE *Private
  138. )
  139. /*++
  140. Routine Description:
  141. TODO: Add function description
  142. Arguments:
  143. Private - TODO: add argument description
  144. Returns:
  145. TODO: add return values
  146. --*/
  147. {
  148. EFI_BLOCK_IO_MEDIA *Media;
  149. EFI_STATUS Status;
  150. Media = Private->Media;
  151. switch (GetLastError ()) {
  152. case ERROR_NOT_READY:
  153. Media->ReadOnly = FALSE;
  154. Media->MediaPresent = FALSE;
  155. Status = EFI_NO_MEDIA;
  156. break;
  157. case ERROR_WRONG_DISK:
  158. Media->ReadOnly = FALSE;
  159. Media->MediaPresent = TRUE;
  160. Media->MediaId++;
  161. Status = EFI_MEDIA_CHANGED;
  162. break;
  163. case ERROR_WRITE_PROTECT:
  164. Media->ReadOnly = TRUE;
  165. Status = EFI_WRITE_PROTECTED;
  166. break;
  167. default:
  168. Status = EFI_DEVICE_ERROR;
  169. break;
  170. }
  171. if ((Status == EFI_NO_MEDIA) || (Status == EFI_MEDIA_CHANGED)) {
  172. WinNtBlockIoReset (&Private->EmuBlockIo, FALSE);
  173. }
  174. return Status;
  175. }
  176. EFI_STATUS
  177. WinNtSignalToken (
  178. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  179. IN EFI_STATUS Status
  180. )
  181. {
  182. if (Token != NULL) {
  183. if (Token->Event != NULL) {
  184. // Caller is responcible for signaling EFI Event
  185. Token->TransactionStatus = Status;
  186. return EFI_SUCCESS;
  187. }
  188. }
  189. return Status;
  190. }
  191. /**
  192. Read BufferSize bytes from Lba into Buffer.
  193. This function reads the requested number of blocks from the device. All the
  194. blocks are read, or an error is returned.
  195. If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and
  196. non-blocking I/O is being used, the Event associated with this request will
  197. not be signaled.
  198. @param[in] This Indicates a pointer to the calling context.
  199. @param[in] MediaId Id of the media, changes every time the media is
  200. replaced.
  201. @param[in] Lba The starting Logical Block Address to read from.
  202. @param[in, out] Token A pointer to the token associated with the transaction.
  203. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  204. @param[out] Buffer A pointer to the destination buffer for the data. The
  205. caller is responsible for either having implicit or
  206. explicit ownership of the buffer.
  207. @retval EFI_SUCCESS The read request was queued if Token->Event is
  208. not NULL.The data was read correctly from the
  209. device if the Token->Event is NULL.
  210. @retval EFI_DEVICE_ERROR The device reported an error while performing
  211. the read.
  212. @retval EFI_NO_MEDIA There is no media in the device.
  213. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  214. @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
  215. intrinsic block size of the device.
  216. @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
  217. or the buffer is not on proper alignment.
  218. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  219. of resources.
  220. **/
  221. EFI_STATUS
  222. WinNtBlockIoReadBlocks (
  223. IN EMU_BLOCK_IO_PROTOCOL *This,
  224. IN UINT32 MediaId,
  225. IN EFI_LBA Lba,
  226. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  227. IN UINTN BufferSize,
  228. OUT VOID *Buffer
  229. )
  230. {
  231. WIN_NT_BLOCK_IO_PRIVATE *Private;
  232. BOOL Flag;
  233. EFI_STATUS Status;
  234. DWORD BytesRead;
  235. UINT64 DistanceToMove;
  236. UINT64 DistanceMoved;
  237. Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  238. //
  239. // Seek to proper position
  240. //
  241. DistanceToMove = MultU64x32 (Lba, (UINT32)Private->BlockSize);
  242. Status = SetFilePointer64 (Private, DistanceToMove, &DistanceMoved, FILE_BEGIN);
  243. if (EFI_ERROR (Status) || (DistanceToMove != DistanceMoved)) {
  244. DEBUG ((DEBUG_INIT, "ReadBlocks: SetFilePointer failed\n"));
  245. return WinNtBlockIoError (Private->Media);
  246. }
  247. Flag = ReadFile (Private->NtHandle, Buffer, (DWORD)BufferSize, (LPDWORD)&BytesRead, NULL);
  248. if (!Flag || (BytesRead != BufferSize)) {
  249. return WinNtBlockIoError (Private->Media);
  250. }
  251. Private->Media->MediaPresent = TRUE;
  252. return WinNtSignalToken (Token, EFI_SUCCESS);
  253. }
  254. /**
  255. Write BufferSize bytes from Lba into Buffer.
  256. This function writes the requested number of blocks to the device. All blocks
  257. are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,
  258. EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is
  259. being used, the Event associated with this request will not be signaled.
  260. @param[in] This Indicates a pointer to the calling context.
  261. @param[in] MediaId The media ID that the write request is for.
  262. @param[in] Lba The starting logical block address to be written. The
  263. caller is responsible for writing to only legitimate
  264. locations.
  265. @param[in, out] Token A pointer to the token associated with the transaction.
  266. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  267. @param[in] Buffer A pointer to the source buffer for the data.
  268. @retval EFI_SUCCESS The write request was queued if Event is not NULL.
  269. The data was written correctly to the device if
  270. the Event is NULL.
  271. @retval EFI_WRITE_PROTECTED The device can not be written to.
  272. @retval EFI_NO_MEDIA There is no media in the device.
  273. @retval EFI_MEDIA_CHANGED The MediaId does not match the current device.
  274. @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
  275. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  276. @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
  277. or the buffer is not on proper alignment.
  278. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  279. of resources.
  280. **/
  281. EFI_STATUS
  282. WinNtBlockIoWriteBlocks (
  283. IN EMU_BLOCK_IO_PROTOCOL *This,
  284. IN UINT32 MediaId,
  285. IN EFI_LBA Lba,
  286. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  287. IN UINTN BufferSize,
  288. IN VOID *Buffer
  289. )
  290. {
  291. WIN_NT_BLOCK_IO_PRIVATE *Private;
  292. UINTN BytesWritten;
  293. BOOL Success;
  294. EFI_STATUS Status;
  295. UINT64 DistanceToMove;
  296. UINT64 DistanceMoved;
  297. Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  298. //
  299. // Seek to proper position
  300. //
  301. DistanceToMove = MultU64x32 (Lba, (UINT32)Private->BlockSize);
  302. Status = SetFilePointer64 (Private, DistanceToMove, &DistanceMoved, FILE_BEGIN);
  303. if (EFI_ERROR (Status) || (DistanceToMove != DistanceMoved)) {
  304. DEBUG ((DEBUG_INIT, "WriteBlocks: SetFilePointer failed\n"));
  305. return WinNtBlockIoError (Private->Media);
  306. }
  307. Success = WriteFile (Private->NtHandle, Buffer, (DWORD)BufferSize, (LPDWORD)&BytesWritten, NULL);
  308. if (!Success || (BytesWritten != BufferSize)) {
  309. return WinNtBlockIoError (Private->Media);
  310. }
  311. //
  312. // If the write succeeded, we are not write protected and media is present.
  313. //
  314. Private->Media->MediaPresent = TRUE;
  315. Private->Media->ReadOnly = FALSE;
  316. return WinNtSignalToken (Token, EFI_SUCCESS);
  317. }
  318. /**
  319. Flush the Block Device.
  320. If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED
  321. is returned and non-blocking I/O is being used, the Event associated with
  322. this request will not be signaled.
  323. @param[in] This Indicates a pointer to the calling context.
  324. @param[in,out] Token A pointer to the token associated with the transaction
  325. @retval EFI_SUCCESS The flush request was queued if Event is not NULL.
  326. All outstanding data was written correctly to the
  327. device if the Event is NULL.
  328. @retval EFI_DEVICE_ERROR The device reported an error while writing back
  329. the data.
  330. @retval EFI_WRITE_PROTECTED The device cannot be written to.
  331. @retval EFI_NO_MEDIA There is no media in the device.
  332. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  333. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  334. of resources.
  335. **/
  336. EFI_STATUS
  337. WinNtBlockIoFlushBlocks (
  338. IN EMU_BLOCK_IO_PROTOCOL *This,
  339. IN OUT EFI_BLOCK_IO2_TOKEN *Token
  340. )
  341. {
  342. return WinNtSignalToken (Token, EFI_SUCCESS);
  343. }
  344. /**
  345. Reset the block device hardware.
  346. @param[in] This Indicates a pointer to the calling context.
  347. @param[in] ExtendedVerification Indicates that the driver may perform a more
  348. exhausive verfication operation of the device
  349. during reset.
  350. @retval EFI_SUCCESS The device was reset.
  351. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  352. not be reset.
  353. **/
  354. EFI_STATUS
  355. WinNtBlockIoReset (
  356. IN EMU_BLOCK_IO_PROTOCOL *This,
  357. IN BOOLEAN ExtendedVerification
  358. )
  359. {
  360. WIN_NT_BLOCK_IO_PRIVATE *Private;
  361. Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (This);
  362. if (Private->NtHandle != INVALID_HANDLE_VALUE) {
  363. CloseHandle (Private->NtHandle);
  364. Private->NtHandle = INVALID_HANDLE_VALUE;
  365. }
  366. return EFI_SUCCESS;
  367. }
  368. EMU_BLOCK_IO_PROTOCOL gEmuBlockIoProtocol = {
  369. WinNtBlockIoReset,
  370. WinNtBlockIoReadBlocks,
  371. WinNtBlockIoWriteBlocks,
  372. WinNtBlockIoFlushBlocks,
  373. WinNtBlockIoCreateMapping
  374. };
  375. EFI_STATUS
  376. EFIAPI
  377. WinNtBlockIoThunkOpen (
  378. IN EMU_IO_THUNK_PROTOCOL *This
  379. )
  380. {
  381. WIN_NT_BLOCK_IO_PRIVATE *Private;
  382. CHAR16 *Str;
  383. Private = AllocatePool (sizeof (*Private));
  384. if (Private == NULL) {
  385. return EFI_OUT_OF_RESOURCES;
  386. }
  387. Private->Signature = WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE;
  388. Private->Thunk = This;
  389. CopyMem (&Private->EmuBlockIo, &gEmuBlockIoProtocol, sizeof (gEmuBlockIoProtocol));
  390. Private->BlockSize = 512;
  391. Private->NtHandle = INVALID_HANDLE_VALUE;
  392. Private->FileName = AllocateCopyPool (StrSize (This->ConfigString), This->ConfigString);
  393. if (Private->FileName == NULL) {
  394. return EFI_OUT_OF_RESOURCES;
  395. }
  396. //
  397. // Parse ConfigString
  398. // <ConfigString> := <FileName> ':' [RF][OW] ':' <BlockSize>
  399. //
  400. Str = StrStr (Private->FileName, L":");
  401. if (Str == NULL) {
  402. Private->Removable = FALSE;
  403. Private->Readonly = FALSE;
  404. } else {
  405. for (*Str++ = L'\0'; *Str != L'\0'; Str++) {
  406. if ((*Str == 'R') || (*Str == 'F')) {
  407. Private->Removable = (BOOLEAN)(*Str == L'R');
  408. }
  409. if ((*Str == 'O') || (*Str == 'W')) {
  410. Private->Readonly = (BOOLEAN)(*Str == L'O');
  411. }
  412. if (*Str == ':') {
  413. Private->BlockSize = wcstol (++Str, NULL, 0);
  414. break;
  415. }
  416. }
  417. }
  418. This->Interface = &Private->EmuBlockIo;
  419. This->Private = Private;
  420. return EFI_SUCCESS;
  421. }
  422. EFI_STATUS
  423. EFIAPI
  424. WinNtBlockIoThunkClose (
  425. IN EMU_IO_THUNK_PROTOCOL *This
  426. )
  427. {
  428. WIN_NT_BLOCK_IO_PRIVATE *Private;
  429. Private = This->Private;
  430. if (Private != NULL) {
  431. if (Private->FileName != NULL) {
  432. FreePool (Private->FileName);
  433. }
  434. FreePool (Private);
  435. }
  436. return EFI_SUCCESS;
  437. }
  438. EMU_IO_THUNK_PROTOCOL mWinNtBlockIoThunkIo = {
  439. &gEmuBlockIoProtocolGuid,
  440. NULL,
  441. NULL,
  442. 0,
  443. WinNtBlockIoThunkOpen,
  444. WinNtBlockIoThunkClose,
  445. NULL
  446. };