inquiry.c 7.1 KB

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