inquiry.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  2. // Copyright (C) 2019 Landon Rodgers <g.landon.rodgers@gmail.com>
  3. //
  4. // This file is part of SCSI2SD.
  5. //
  6. // SCSI2SD is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // SCSI2SD is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  18. #include "scsi.h"
  19. #include "config.h"
  20. #include "inquiry.h"
  21. #include <string.h>
  22. static uint8_t StandardResponse[] =
  23. {
  24. 0x00, // "Direct-access device". AKA standard hard disk
  25. 0x00, // device type modifier
  26. 0x02, // Complies with ANSI SCSI-2.
  27. 0x01, // Response format is compatible with the old CCS format.
  28. 0x1f, // standard length.
  29. 0, 0, // Reserved
  30. 0x18 // Enable sync and linked commands
  31. };
  32. // Vendor set by config 'c','o','d','e','s','r','c',' ',
  33. // prodId set by config'S','C','S','I','2','S','D',' ',' ',' ',' ',' ',' ',' ',' ',' ',
  34. // Revision set by config'2','.','0','a'
  35. static const uint8_t SupportedVitalPages[] =
  36. {
  37. 0x00, // "Direct-access device". AKA standard hard disk
  38. 0x00, // Page Code
  39. 0x00, // Reserved
  40. 0x04, // Page length
  41. 0x00, // Support "Supported vital product data pages"
  42. 0x80, // Support "Unit serial number page"
  43. 0x81, // Support "Implemented operating definition page"
  44. 0x82 // Support "ASCII Implemented operating definition page"
  45. };
  46. static const uint8_t UnitSerialNumber[] =
  47. {
  48. 0x00, // "Direct-access device". AKA standard hard disk
  49. 0x80, // Page Code
  50. 0x00, // Reserved
  51. 0x10, // Page length
  52. 'c','o','d','e','s','r','c','-','1','2','3','4','5','6','7','8'
  53. };
  54. static const uint8_t ImpOperatingDefinition[] =
  55. {
  56. 0x00, // "Direct-access device". AKA standard hard disk
  57. 0x81, // Page Code
  58. 0x00, // Reserved
  59. 0x03, // Page length
  60. 0x03, // Current: SCSI-2 operating definition
  61. 0x03, // Default: SCSI-2 operating definition
  62. 0x03 // Supported (list): SCSI-2 operating definition.
  63. };
  64. static const uint8_t AscImpOperatingDefinition[] =
  65. {
  66. 0x00, // "Direct-access device". AKA standard hard disk
  67. 0x82, // Page Code
  68. 0x00, // Reserved
  69. 0x07, // Page length
  70. 0x06, // Ascii length
  71. 'S','C','S','I','-','2'
  72. };
  73. void s2s_scsiInquiry()
  74. {
  75. uint8_t evpd = scsiDev.cdb[1] & 1; // enable vital product data.
  76. uint8_t pageCode = scsiDev.cdb[2];
  77. uint32_t allocationLength = scsiDev.cdb[4];
  78. // SASI standard, X3T9.3_185_RevE states that 0 == 256 bytes
  79. // BUT SCSI 2 standard says 0 == 0.
  80. if (scsiDev.compatMode <= COMPAT_SCSI1) // excludes COMPAT_SCSI2_DISABLED
  81. {
  82. if (allocationLength == 0) allocationLength = 256;
  83. }
  84. if (!evpd)
  85. {
  86. if (pageCode)
  87. {
  88. // error.
  89. scsiDev.status = CHECK_CONDITION;
  90. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  91. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  92. scsiDev.phase = STATUS;
  93. }
  94. else
  95. {
  96. const S2S_TargetCfg* config = scsiDev.target->cfg;
  97. scsiDev.dataLen =
  98. s2s_getStandardInquiry(
  99. config,
  100. scsiDev.data,
  101. sizeof(scsiDev.data));
  102. scsiDev.phase = DATA_IN;
  103. }
  104. }
  105. else if (pageCode == 0x00)
  106. {
  107. memcpy(scsiDev.data, SupportedVitalPages, sizeof(SupportedVitalPages));
  108. scsiDev.dataLen = sizeof(SupportedVitalPages);
  109. scsiDev.phase = DATA_IN;
  110. }
  111. else if (pageCode == 0x80)
  112. {
  113. memcpy(scsiDev.data, UnitSerialNumber, sizeof(UnitSerialNumber));
  114. scsiDev.dataLen = sizeof(UnitSerialNumber);
  115. const S2S_TargetCfg* config = scsiDev.target->cfg;
  116. memcpy(&scsiDev.data[4], config->serial, sizeof(config->serial));
  117. scsiDev.phase = DATA_IN;
  118. }
  119. else if (pageCode == 0x81)
  120. {
  121. memcpy(
  122. scsiDev.data,
  123. ImpOperatingDefinition,
  124. sizeof(ImpOperatingDefinition));
  125. scsiDev.dataLen = sizeof(ImpOperatingDefinition);
  126. scsiDev.phase = DATA_IN;
  127. }
  128. else if (pageCode == 0x82)
  129. {
  130. memcpy(
  131. scsiDev.data,
  132. AscImpOperatingDefinition,
  133. sizeof(AscImpOperatingDefinition));
  134. scsiDev.dataLen = sizeof(AscImpOperatingDefinition);
  135. scsiDev.phase = DATA_IN;
  136. }
  137. else
  138. {
  139. // error.
  140. scsiDev.status = CHECK_CONDITION;
  141. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  142. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  143. scsiDev.phase = STATUS;
  144. }
  145. if (scsiDev.phase == DATA_IN)
  146. {
  147. // VAX workaround
  148. if (allocationLength == 255 &&
  149. (scsiDev.target->cfg->quirks & S2S_CFG_QUIRKS_VMS))
  150. {
  151. allocationLength = 254;
  152. }
  153. // "real" hard drives send back exactly allocationLenth bytes, padded
  154. // with zeroes. This only seems to happen for Inquiry responses, and not
  155. // other commands that also supply an allocation length such as Mode Sense or
  156. // Request Sense.
  157. // (See below for exception to this rule when 0 allocation length)
  158. if (scsiDev.dataLen < allocationLength)
  159. {
  160. memset(
  161. &scsiDev.data[scsiDev.dataLen],
  162. 0,
  163. allocationLength - scsiDev.dataLen);
  164. }
  165. // Spec 8.2.5 requires us to simply truncate the response if it's
  166. // too big.
  167. scsiDev.dataLen = allocationLength;
  168. // Set the device type as needed.
  169. scsiDev.data[0] = getDeviceTypeQualifier();
  170. switch (scsiDev.target->cfg->deviceType)
  171. {
  172. case S2S_CFG_OPTICAL:
  173. scsiDev.data[1] |= 0x80; // Removable bit.
  174. break;
  175. case S2S_CFG_SEQUENTIAL:
  176. scsiDev.data[1] |= 0x80; // Removable bit.
  177. break;
  178. case S2S_CFG_MO:
  179. scsiDev.data[1] |= 0x80; // Removable bit.
  180. break;
  181. case S2S_CFG_FLOPPY_14MB:
  182. case S2S_CFG_REMOVEABLE:
  183. scsiDev.data[1] |= 0x80; // Removable bit.
  184. break;
  185. case S2S_CFG_NETWORK:
  186. scsiDev.data[2] = 0x01; // Page code.
  187. break;
  188. default:
  189. // Accept defaults for a fixed disk.
  190. break;
  191. }
  192. }
  193. // Set the first byte to indicate LUN presence.
  194. if (scsiDev.lun) // We only support lun 0
  195. {
  196. scsiDev.data[0] = 0x7F;
  197. }
  198. }
  199. uint32_t s2s_getStandardInquiry(
  200. const S2S_TargetCfg* cfg, uint8_t* out, uint32_t maxlen
  201. )
  202. {
  203. uint32_t buflen = sizeof(StandardResponse);
  204. if (buflen > maxlen) buflen = maxlen;
  205. memcpy(out, StandardResponse, buflen);
  206. out[1] = cfg->deviceTypeModifier;
  207. if (!(scsiDev.boardCfg.flags & S2S_CFG_ENABLE_SCSI2))
  208. {
  209. out[2] = 1; // Report only SCSI 1 compliance version
  210. }
  211. if (scsiDev.compatMode >= COMPAT_SCSI2)
  212. {
  213. out[3] = 2; // SCSI 2 response format.
  214. }
  215. memcpy(&out[8], cfg->vendor, sizeof(cfg->vendor));
  216. memcpy(&out[16], cfg->prodId, sizeof(cfg->prodId));
  217. memcpy(&out[32], cfg->revision, sizeof(cfg->revision));
  218. return sizeof(StandardResponse) +
  219. sizeof(cfg->vendor) +
  220. sizeof(cfg->prodId) +
  221. sizeof(cfg->revision);
  222. }
  223. uint8_t getDeviceTypeQualifier()
  224. {
  225. // Set the device type as needed.
  226. switch (scsiDev.target->cfg->deviceType)
  227. {
  228. case S2S_CFG_OPTICAL:
  229. return 0x05;
  230. break;
  231. case S2S_CFG_SEQUENTIAL:
  232. return 0x01;
  233. break;
  234. case S2S_CFG_MO:
  235. return 0x07;
  236. break;
  237. case S2S_CFG_FLOPPY_14MB:
  238. case S2S_CFG_REMOVEABLE:
  239. return 0;
  240. break;
  241. case S2S_CFG_NETWORK:
  242. // processor device
  243. return 0x03;
  244. break;
  245. default:
  246. // Accept defaults for a fixed disk.
  247. return 0;
  248. }
  249. }