You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

SCSI.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * SCSI command processing routines, for SCSI commands issued by the host. Mass Storage
  29. * devices use a thin "Bulk-Only Transport" protocol for issuing commands and status information,
  30. * which wrap around standard SCSI device commands for controlling the actual storage medium.
  31. */
  32. #define INCLUDE_FROM_SCSI_C
  33. #include "SCSI.h"
  34. /** Structure to hold the SCSI response data to a SCSI INQUIRY command. This gives information about the device's
  35. * features and capabilities.
  36. */
  37. static const SCSI_Inquiry_Response_t InquiryData =
  38. {
  39. .DeviceType = DEVICE_TYPE_BLOCK,
  40. .PeripheralQualifier = 0,
  41. .Removable = true,
  42. .Version = 0,
  43. .ResponseDataFormat = 2,
  44. .NormACA = false,
  45. .TrmTsk = false,
  46. .AERC = false,
  47. .AdditionalLength = 0x1F,
  48. .SoftReset = false,
  49. .CmdQue = false,
  50. .Linked = false,
  51. .Sync = false,
  52. .WideBus16Bit = false,
  53. .WideBus32Bit = false,
  54. .RelAddr = false,
  55. .VendorID = "LUFA",
  56. .ProductID = "Bootloader",
  57. .RevisionID = {'0','.','0','0'},
  58. };
  59. /** Structure to hold the sense data for the last issued SCSI command, which is returned to the host after a SCSI REQUEST SENSE
  60. * command is issued. This gives information on exactly why the last command failed to complete.
  61. */
  62. static SCSI_Request_Sense_Response_t SenseData =
  63. {
  64. .ResponseCode = 0x70,
  65. .AdditionalLength = 0x0A,
  66. };
  67. /** Main routine to process the SCSI command located in the Command Block Wrapper read from the host. This dispatches
  68. * to the appropriate SCSI command handling routine if the issued command is supported by the device, else it returns
  69. * a command failure due to a ILLEGAL REQUEST.
  70. *
  71. * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
  72. *
  73. * \return Boolean \c true if the command completed successfully, \c false otherwise
  74. */
  75. bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  76. {
  77. bool CommandSuccess = false;
  78. /* Run the appropriate SCSI command hander function based on the passed command */
  79. switch (MSInterfaceInfo->State.CommandBlock.SCSICommandData[0])
  80. {
  81. case SCSI_CMD_INQUIRY:
  82. CommandSuccess = SCSI_Command_Inquiry(MSInterfaceInfo);
  83. break;
  84. case SCSI_CMD_REQUEST_SENSE:
  85. CommandSuccess = SCSI_Command_Request_Sense(MSInterfaceInfo);
  86. break;
  87. case SCSI_CMD_READ_CAPACITY_10:
  88. CommandSuccess = SCSI_Command_Read_Capacity_10(MSInterfaceInfo);
  89. break;
  90. case SCSI_CMD_WRITE_10:
  91. CommandSuccess = SCSI_Command_ReadWrite_10(MSInterfaceInfo, DATA_WRITE);
  92. break;
  93. case SCSI_CMD_READ_10:
  94. CommandSuccess = SCSI_Command_ReadWrite_10(MSInterfaceInfo, DATA_READ);
  95. break;
  96. case SCSI_CMD_MODE_SENSE_6:
  97. CommandSuccess = SCSI_Command_ModeSense_6(MSInterfaceInfo);
  98. break;
  99. case SCSI_CMD_START_STOP_UNIT:
  100. #if !defined(NO_APP_START_ON_EJECT)
  101. /* If the user ejected the volume, signal bootloader exit at next opportunity. */
  102. RunBootloader = ((MSInterfaceInfo->State.CommandBlock.SCSICommandData[4] & 0x03) != 0x02);
  103. #endif
  104. case SCSI_CMD_SEND_DIAGNOSTIC:
  105. case SCSI_CMD_TEST_UNIT_READY:
  106. case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
  107. case SCSI_CMD_VERIFY_10:
  108. /* These commands should just succeed, no handling required */
  109. CommandSuccess = true;
  110. MSInterfaceInfo->State.CommandBlock.DataTransferLength = 0;
  111. break;
  112. default:
  113. /* Update the SENSE key to reflect the invalid command */
  114. SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,
  115. SCSI_ASENSE_INVALID_COMMAND,
  116. SCSI_ASENSEQ_NO_QUALIFIER);
  117. break;
  118. }
  119. /* Check if command was successfully processed */
  120. if (CommandSuccess)
  121. {
  122. SCSI_SET_SENSE(SCSI_SENSE_KEY_GOOD,
  123. SCSI_ASENSE_NO_ADDITIONAL_INFORMATION,
  124. SCSI_ASENSEQ_NO_QUALIFIER);
  125. return true;
  126. }
  127. return false;
  128. }
  129. /** Command processing for an issued SCSI INQUIRY command. This command returns information about the device's features
  130. * and capabilities to the host.
  131. *
  132. * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
  133. *
  134. * \return Boolean \c true if the command completed successfully, \c false otherwise.
  135. */
  136. static bool SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  137. {
  138. uint16_t AllocationLength = SwapEndian_16(*(uint16_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[3]);
  139. uint16_t BytesTransferred = MIN(AllocationLength, sizeof(InquiryData));
  140. /* Only the standard INQUIRY data is supported, check if any optional INQUIRY bits set */
  141. if ((MSInterfaceInfo->State.CommandBlock.SCSICommandData[1] & ((1 << 0) | (1 << 1))) ||
  142. MSInterfaceInfo->State.CommandBlock.SCSICommandData[2])
  143. {
  144. /* Optional but unsupported bits set - update the SENSE key and fail the request */
  145. SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,
  146. SCSI_ASENSE_INVALID_FIELD_IN_CDB,
  147. SCSI_ASENSEQ_NO_QUALIFIER);
  148. return false;
  149. }
  150. Endpoint_Write_Stream_LE(&InquiryData, BytesTransferred, NULL);
  151. /* Pad out remaining bytes with 0x00 */
  152. Endpoint_Null_Stream((AllocationLength - BytesTransferred), NULL);
  153. /* Finalize the stream transfer to send the last packet */
  154. Endpoint_ClearIN();
  155. /* Succeed the command and update the bytes transferred counter */
  156. MSInterfaceInfo->State.CommandBlock.DataTransferLength -= BytesTransferred;
  157. return true;
  158. }
  159. /** Command processing for an issued SCSI REQUEST SENSE command. This command returns information about the last issued command,
  160. * including the error code and additional error information so that the host can determine why a command failed to complete.
  161. *
  162. * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
  163. *
  164. * \return Boolean \c true if the command completed successfully, \c false otherwise.
  165. */
  166. static bool SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  167. {
  168. uint8_t AllocationLength = MSInterfaceInfo->State.CommandBlock.SCSICommandData[4];
  169. uint8_t BytesTransferred = MIN(AllocationLength, sizeof(SenseData));
  170. Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NULL);
  171. Endpoint_Null_Stream((AllocationLength - BytesTransferred), NULL);
  172. Endpoint_ClearIN();
  173. /* Succeed the command and update the bytes transferred counter */
  174. MSInterfaceInfo->State.CommandBlock.DataTransferLength -= BytesTransferred;
  175. return true;
  176. }
  177. /** Command processing for an issued SCSI READ CAPACITY (10) command. This command returns information about the device's capacity
  178. * on the selected Logical Unit (drive), as a number of OS-sized blocks.
  179. *
  180. * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
  181. *
  182. * \return Boolean \c true if the command completed successfully, \c false otherwise.
  183. */
  184. static bool SCSI_Command_Read_Capacity_10(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  185. {
  186. Endpoint_Write_32_BE(LUN_MEDIA_BLOCKS - 1);
  187. Endpoint_Write_32_BE(SECTOR_SIZE_BYTES);
  188. Endpoint_ClearIN();
  189. /* Succeed the command and update the bytes transferred counter */
  190. MSInterfaceInfo->State.CommandBlock.DataTransferLength -= 8;
  191. return true;
  192. }
  193. /** Command processing for an issued SCSI READ (10) or WRITE (10) command. This command reads in the block start address
  194. * and total number of blocks to process, then calls the appropriate low-level Dataflash routine to handle the actual
  195. * reading and writing of the data.
  196. *
  197. * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
  198. * \param[in] IsDataRead Indicates if the command is a READ (10) command or WRITE (10) command (DATA_READ or DATA_WRITE)
  199. *
  200. * \return Boolean \c true if the command completed successfully, \c false otherwise.
  201. */
  202. static bool SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
  203. const bool IsDataRead)
  204. {
  205. uint16_t BlockAddress;
  206. uint16_t TotalBlocks;
  207. /* Load in the 32-bit block address (SCSI uses big-endian, so have to reverse the byte order) */
  208. BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
  209. /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
  210. TotalBlocks = SwapEndian_16(*(uint16_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
  211. /* Check if the block address is outside the maximum allowable value for the LUN */
  212. if (BlockAddress >= LUN_MEDIA_BLOCKS)
  213. {
  214. /* Block address is invalid, update SENSE key and return command fail */
  215. SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,
  216. SCSI_ASENSE_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE,
  217. SCSI_ASENSEQ_NO_QUALIFIER);
  218. return false;
  219. }
  220. /* Determine if the packet is a READ (10) or WRITE (10) command, call appropriate function */
  221. for (uint16_t i = 0; i < TotalBlocks; i++)
  222. {
  223. if (IsDataRead == DATA_READ)
  224. VirtualFAT_ReadBlock(BlockAddress + i);
  225. else
  226. VirtualFAT_WriteBlock(BlockAddress + i);
  227. }
  228. /* Update the bytes transferred counter and succeed the command */
  229. MSInterfaceInfo->State.CommandBlock.DataTransferLength -= ((uint32_t)TotalBlocks * SECTOR_SIZE_BYTES);
  230. return true;
  231. }
  232. /** Command processing for an issued SCSI MODE SENSE (6) command. This command returns various informational pages about
  233. * the SCSI device, as well as the device's Write Protect status.
  234. *
  235. * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
  236. *
  237. * \return Boolean \c true if the command completed successfully, \c false otherwise.
  238. */
  239. static bool SCSI_Command_ModeSense_6(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
  240. {
  241. /* Send an empty header response indicating Write Protect flag is off */
  242. Endpoint_Write_32_LE(0);
  243. Endpoint_ClearIN();
  244. /* Update the bytes transferred counter and succeed the command */
  245. MSInterfaceInfo->State.CommandBlock.DataTransferLength -= 4;
  246. return true;
  247. }