BlockIo.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /** @file
  2. BlockIo implementation for Xen PV Block driver.
  3. This file is implementing the interface between the actual driver in
  4. BlockFront.c to the BlockIo protocol.
  5. Copyright (C) 2014, Citrix Ltd.
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "XenPvBlkDxe.h"
  9. #include "BlockFront.h"
  10. ///
  11. /// Block I/O Media structure
  12. ///
  13. GLOBAL_REMOVE_IF_UNREFERENCED
  14. EFI_BLOCK_IO_MEDIA gXenPvBlkDxeBlockIoMedia = {
  15. 0, // MediaId
  16. FALSE, // RemovableMedia
  17. FALSE, // MediaPresent
  18. FALSE, // LogicalPartition
  19. TRUE, // ReadOnly
  20. FALSE, // WriteCaching
  21. 512, // BlockSize
  22. 512, // IoAlign, BlockFront does not support less than 512 bits-aligned.
  23. 0, // LastBlock
  24. 0, // LowestAlignedLba
  25. 0, // LogicalBlocksPerPhysicalBlock
  26. 0 // OptimalTransferLengthGranularity
  27. };
  28. ///
  29. /// Block I/O Protocol instance
  30. ///
  31. GLOBAL_REMOVE_IF_UNREFERENCED
  32. EFI_BLOCK_IO_PROTOCOL gXenPvBlkDxeBlockIo = {
  33. EFI_BLOCK_IO_PROTOCOL_REVISION3, // Revision
  34. &gXenPvBlkDxeBlockIoMedia, // Media
  35. XenPvBlkDxeBlockIoReset, // Reset
  36. XenPvBlkDxeBlockIoReadBlocks, // ReadBlocks
  37. XenPvBlkDxeBlockIoWriteBlocks, // WriteBlocks
  38. XenPvBlkDxeBlockIoFlushBlocks // FlushBlocks
  39. };
  40. /**
  41. Read/Write BufferSize bytes from Lba into Buffer.
  42. This function is common to XenPvBlkDxeBlockIoReadBlocks and
  43. XenPvBlkDxeBlockIoWriteBlocks.
  44. @param This Indicates a pointer to the calling context.
  45. @param MediaId Id of the media, changes every time the media is replaced.
  46. @param Lba The starting Logical Block Address to read from/write to.
  47. @param BufferSize Size of Buffer, must be a multiple of device block size.
  48. @param Buffer A pointer to the destination/source buffer for the data.
  49. @param IsWrite Indicate if the operation is write or read.
  50. @return See description of XenPvBlkDxeBlockIoReadBlocks and
  51. XenPvBlkDxeBlockIoWriteBlocks.
  52. **/
  53. STATIC
  54. EFI_STATUS
  55. XenPvBlkDxeBlockIoReadWriteBlocks (
  56. IN EFI_BLOCK_IO_PROTOCOL *This,
  57. IN UINT32 MediaId,
  58. IN EFI_LBA Lba,
  59. IN UINTN BufferSize,
  60. IN OUT VOID *Buffer,
  61. IN BOOLEAN IsWrite
  62. )
  63. {
  64. XEN_BLOCK_FRONT_IO IoData;
  65. EFI_BLOCK_IO_MEDIA *Media = This->Media;
  66. UINTN Sector;
  67. EFI_STATUS Status;
  68. if (Buffer == NULL) {
  69. return EFI_INVALID_PARAMETER;
  70. }
  71. if (BufferSize == 0) {
  72. return EFI_SUCCESS;
  73. }
  74. if (BufferSize % Media->BlockSize != 0) {
  75. DEBUG ((
  76. DEBUG_ERROR,
  77. "XenPvBlkDxe: Bad buffer size: 0x%Lx\n",
  78. (UINT64)BufferSize
  79. ));
  80. return EFI_BAD_BUFFER_SIZE;
  81. }
  82. if ((Lba > Media->LastBlock) ||
  83. ((BufferSize / Media->BlockSize) - 1 > Media->LastBlock - Lba))
  84. {
  85. DEBUG ((
  86. DEBUG_ERROR,
  87. "XenPvBlkDxe: %a with invalid LBA: 0x%Lx, size: 0x%Lx\n",
  88. IsWrite ? "Write" : "Read",
  89. Lba,
  90. (UINT64)BufferSize
  91. ));
  92. return EFI_INVALID_PARAMETER;
  93. }
  94. if (IsWrite && Media->ReadOnly) {
  95. return EFI_WRITE_PROTECTED;
  96. }
  97. if ((Media->IoAlign > 1) && (UINTN)Buffer & (Media->IoAlign - 1)) {
  98. //
  99. // Grub2 does not appear to respect IoAlign of 512, so reallocate the
  100. // buffer here.
  101. //
  102. VOID *NewBuffer;
  103. //
  104. // Try again with a properly aligned buffer.
  105. //
  106. NewBuffer = AllocateAlignedPages (
  107. (BufferSize + EFI_PAGE_SIZE) / EFI_PAGE_SIZE,
  108. Media->IoAlign
  109. );
  110. if (!IsWrite) {
  111. Status = XenPvBlkDxeBlockIoReadBlocks (
  112. This,
  113. MediaId,
  114. Lba,
  115. BufferSize,
  116. NewBuffer
  117. );
  118. CopyMem (Buffer, NewBuffer, BufferSize);
  119. } else {
  120. CopyMem (NewBuffer, Buffer, BufferSize);
  121. Status = XenPvBlkDxeBlockIoWriteBlocks (
  122. This,
  123. MediaId,
  124. Lba,
  125. BufferSize,
  126. NewBuffer
  127. );
  128. }
  129. FreeAlignedPages (NewBuffer, (BufferSize + EFI_PAGE_SIZE) / EFI_PAGE_SIZE);
  130. return Status;
  131. }
  132. IoData.Dev = XEN_BLOCK_FRONT_FROM_BLOCK_IO (This);
  133. Sector = (UINTN)MultU64x32 (Lba, Media->BlockSize / 512);
  134. while (BufferSize > 0) {
  135. if (((UINTN)Buffer & EFI_PAGE_MASK) == 0) {
  136. IoData.Size = MIN (
  137. BLKIF_MAX_SEGMENTS_PER_REQUEST * EFI_PAGE_SIZE,
  138. BufferSize
  139. );
  140. } else {
  141. IoData.Size = MIN (
  142. (BLKIF_MAX_SEGMENTS_PER_REQUEST - 1) * EFI_PAGE_SIZE,
  143. BufferSize
  144. );
  145. }
  146. IoData.Buffer = Buffer;
  147. IoData.Sector = Sector;
  148. BufferSize -= IoData.Size;
  149. Buffer = (VOID *)((UINTN)Buffer + IoData.Size);
  150. Sector += IoData.Size / 512;
  151. Status = XenPvBlockIo (&IoData, IsWrite);
  152. if (EFI_ERROR (Status)) {
  153. DEBUG ((
  154. DEBUG_ERROR,
  155. "XenPvBlkDxe: Error during %a operation.\n",
  156. IsWrite ? "write" : "read"
  157. ));
  158. return Status;
  159. }
  160. }
  161. return EFI_SUCCESS;
  162. }
  163. /**
  164. Read BufferSize bytes from Lba into Buffer.
  165. @param This Indicates a pointer to the calling context.
  166. @param MediaId Id of the media, changes every time the media is replaced.
  167. @param Lba The starting Logical Block Address to read from
  168. @param BufferSize Size of Buffer, must be a multiple of device block size.
  169. @param Buffer A pointer to the destination buffer for the data. The caller is
  170. responsible for either having implicit or explicit ownership of the buffer.
  171. @retval EFI_SUCCESS The data was read correctly from the device.
  172. @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
  173. @retval EFI_NO_MEDIA There is no media in the device.
  174. @retval EFI_MEDIA_CHANGED The MediaId does not match the current device.
  175. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  176. @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
  177. or the buffer is not on proper alignment.
  178. **/
  179. EFI_STATUS
  180. EFIAPI
  181. XenPvBlkDxeBlockIoReadBlocks (
  182. IN EFI_BLOCK_IO_PROTOCOL *This,
  183. IN UINT32 MediaId,
  184. IN EFI_LBA Lba,
  185. IN UINTN BufferSize,
  186. OUT VOID *Buffer
  187. )
  188. {
  189. return XenPvBlkDxeBlockIoReadWriteBlocks (
  190. This,
  191. MediaId,
  192. Lba,
  193. BufferSize,
  194. Buffer,
  195. FALSE
  196. );
  197. }
  198. /**
  199. Write BufferSize bytes from Lba into Buffer.
  200. @param This Indicates a pointer to the calling context.
  201. @param MediaId The media ID that the write request is for.
  202. @param Lba The starting logical block address to be written. The caller is
  203. responsible for writing to only legitimate locations.
  204. @param BufferSize Size of Buffer, must be a multiple of device block size.
  205. @param Buffer A pointer to the source buffer for the data.
  206. @retval EFI_SUCCESS The data was written correctly to the device.
  207. @retval EFI_WRITE_PROTECTED The device can not be written to.
  208. @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
  209. @retval EFI_NO_MEDIA There is no media in the device.
  210. @retval EFI_MEDIA_CHANGED The MediaId does not match the current device.
  211. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  212. @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
  213. or the buffer is not on proper alignment.
  214. **/
  215. EFI_STATUS
  216. EFIAPI
  217. XenPvBlkDxeBlockIoWriteBlocks (
  218. IN EFI_BLOCK_IO_PROTOCOL *This,
  219. IN UINT32 MediaId,
  220. IN EFI_LBA Lba,
  221. IN UINTN BufferSize,
  222. IN VOID *Buffer
  223. )
  224. {
  225. return XenPvBlkDxeBlockIoReadWriteBlocks (
  226. This,
  227. MediaId,
  228. Lba,
  229. BufferSize,
  230. Buffer,
  231. TRUE
  232. );
  233. }
  234. /**
  235. Flush the Block Device.
  236. @param This Indicates a pointer to the calling context.
  237. @retval EFI_SUCCESS All outstanding data was written to the device
  238. @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
  239. @retval EFI_NO_MEDIA There is no media in the device.
  240. **/
  241. EFI_STATUS
  242. EFIAPI
  243. XenPvBlkDxeBlockIoFlushBlocks (
  244. IN EFI_BLOCK_IO_PROTOCOL *This
  245. )
  246. {
  247. XenPvBlockSync (XEN_BLOCK_FRONT_FROM_BLOCK_IO (This));
  248. return EFI_SUCCESS;
  249. }
  250. /**
  251. Reset the block device hardware.
  252. @param[in] This Indicates a pointer to the calling context.
  253. @param[in] ExtendedVerification Not used.
  254. @retval EFI_SUCCESS The device was reset.
  255. **/
  256. EFI_STATUS
  257. EFIAPI
  258. XenPvBlkDxeBlockIoReset (
  259. IN EFI_BLOCK_IO_PROTOCOL *This,
  260. IN BOOLEAN ExtendedVerification
  261. )
  262. {
  263. //
  264. // Since the initialization of the devices is done, then the device is
  265. // working correctly.
  266. //
  267. return EFI_SUCCESS;
  268. }