mode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  2. // Copyright (C) 2014 Doug Brown <doug@downtowndougbrown.com>
  3. // Copyright (C) 2019 Landon Rodgers <g.landon.rodgers@gmail.com>
  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. #include "scsi.h"
  20. #include "mode.h"
  21. #include "disk.h"
  22. #include "inquiry.h"
  23. #include "BlueSCSI_mode.h"
  24. #include <string.h>
  25. // "Vendor" defined page which was included by Seagate, and required for\r
  26. // Amiga 500 using DKB SpitFire controller.\r
  27. static const uint8_t OperatingPage[] =
  28. {
  29. 0x00, // Page code
  30. 0x02, // Page length
  31. // Bit 4 = unit attension (0 = on, 1 = off).
  32. // Bit 7 = usage bit, EEPROM life exceeded warning = 1.
  33. 0x80,
  34. // Bit 7 = reserved.
  35. // Bits 0:6: Device type qualifier, as per Inquiry data
  36. 0x00
  37. };
  38. static const uint8_t ReadWriteErrorRecoveryPage[] =
  39. {
  40. 0x01, // Page code
  41. 0x0A, // Page length
  42. // VMS 5.5-2 is very particular regarding the mode page values.
  43. // The required values for a SCSI2/NoTCQ device are:
  44. // AWRE=0 ARRE=0 TB=1 RC=0 EER=? PER=1 DTE=1 DCR=?
  45. // See ftp://www.digiater.nl/openvms/decus/vms94b/net94b/scsi_params_dkdriver.txt
  46. // X-Newsgroups: comp.os.vms
  47. // Subject: Re: VMS 6.1 vs. Seagate Disk Drives
  48. // Message-Id: <32g87h$8q@nntpd.lkg.dec.com>
  49. // From: weber@evms.enet.dec.com (Ralph O. Weber -- OpenVMS AXP)
  50. // Date: 12 Aug 1994 16:32:49 GMT
  51. 0x26,
  52. 0x00, // Don't try recovery algorithm during reads
  53. 0x00, // Correction span 0
  54. 0x00, // Head offset count 0,
  55. 0x00, // Data strobe offset count 0,
  56. 0x00, // Reserved
  57. 0x00, // Don't try recovery algorithm during writes
  58. 0x00, // Reserved
  59. 0x00, 0x00 // Recovery time limit 0 (use default)*/
  60. };
  61. static const uint8_t ReadWriteErrorRecoveryPage_SCSI1[] =
  62. {
  63. 0x01, // Page code
  64. 0x06, // Page length
  65. 0x26,
  66. 0x00, // Don't try recovery algorithm during reads
  67. 0x00, // Correction span 0
  68. 0x00, // Head offset count 0,
  69. 0x00, // Data strobe offset count 0,
  70. 0xFF // Reserved
  71. };
  72. static const uint8_t DisconnectReconnectPage[] =
  73. {
  74. 0x02, // Page code
  75. 0x0E, // Page length
  76. 0, // Buffer full ratio
  77. 0, // Buffer empty ratio
  78. 0x00, 10, // Bus inactivity limit, 100us increments. Allow 1ms.
  79. 0x00, 0x00, // Disconnect time limit
  80. 0x00, 0x00, // Connect time limit
  81. 0x00, 0x00, // Maximum burst size
  82. 0x00 ,// DTDC. Not used.
  83. 0x00, 0x00, 0x00 // Reserved
  84. };
  85. static const uint8_t DisconnectReconnectPage_SCSI1[] =
  86. {
  87. 0x02, // Page code
  88. 0x0A, // Page length
  89. 0, // Buffer full ratio
  90. 0, // Buffer empty ratio
  91. 0x00, 10, // Bus inactivity limit, 100us increments. Allow 1ms.
  92. 0x00, 0x00, // Disconnect time limit
  93. 0x00, 0x00, // Connect time limit
  94. 0x00, 0x00 // Maximum burst size
  95. };
  96. static const uint8_t FormatDevicePage[] =
  97. {
  98. 0x03 | 0x80, // Page code | PS (persist) bit.
  99. 0x16, // Page length
  100. 0x00, 0x00, // Single zone
  101. 0x00, 0x00, // No alternate sectors
  102. 0x00, 0x00, // No alternate tracks
  103. 0x00, 0x00, // No alternate tracks per lun
  104. 0x00, 0x00, // Sectors per track, configurable
  105. 0xFF, 0xFF, // Data bytes per physical sector. Configurable.
  106. 0x00, 0x01, // Interleave
  107. 0x00, 0x00, // Track skew factor
  108. 0x00, 0x00, // Cylinder skew factor
  109. 0xC0, // SSEC(set) HSEC(set) RMB SURF
  110. 0x00, 0x00, 0x00 // Reserved
  111. };
  112. static const uint8_t RigidDiskDriveGeometry[] =
  113. {
  114. 0x04, // Page code
  115. 0x16, // Page length
  116. 0xFF, 0xFF, 0xFF, // Number of cylinders
  117. 0x00, // Number of heads (replaced by configured value)
  118. 0xFF, 0xFF, 0xFF, // Starting cylinder-write precompensation
  119. 0xFF, 0xFF, 0xFF, // Starting cylinder-reduced write current
  120. 0x00, 0x1, // Drive step rate (units of 100ns)
  121. 0x00, 0x00, 0x00, // Landing zone cylinder
  122. 0x00, // RPL
  123. 0x00, // Rotational offset
  124. 0x00, // Reserved
  125. 5400 >> 8, 5400 & 0xFF, // Medium rotation rate (RPM)
  126. 0x00, 0x00 // Reserved
  127. };
  128. static const uint8_t FlexibleDiskDriveGeometry[] =
  129. {
  130. 0x05, // Page code
  131. 0x1E, // Page length
  132. 0x01, 0xF4, // Transfer Rate (500kbits)
  133. 0x01, // heads
  134. 18, // sectors per track
  135. 0x20,0x00, // bytes per sector
  136. 0x00, 80, // Cylinders
  137. 0x00, 0x80, // Write-precomp
  138. 0x00, 0x80, // reduced current,
  139. 0x00, 0x00, // Drive step rate
  140. 0x00, // pulse width
  141. 0x00, 0x00, // Head settle delay
  142. 0x00, // motor on delay
  143. 0x00, // motor off delay
  144. 0x00,
  145. 0x00,
  146. 0x00,
  147. 0x00,
  148. 0x00,
  149. 0x00,
  150. 0x00,
  151. 0x00,
  152. 0x00,
  153. 0x00,
  154. 0x00
  155. };
  156. static const uint8_t RigidDiskDriveGeometry_SCSI1[] =
  157. {
  158. 0x04, // Page code
  159. 0x12, // Page length
  160. 0xFF, 0xFF, 0xFF, // Number of cylinders
  161. 0x00, // Number of heads (replaced by configured value)
  162. 0xFF, 0xFF, 0xFF, // Starting cylinder-write precompensation
  163. 0xFF, 0xFF, 0xFF, // Starting cylinder-reduced write current
  164. 0x00, 0x1, // Drive step rate (units of 100ns)
  165. 0x00, 0x00, 0x00, // Landing zone cylinder
  166. 0x00, // RPL
  167. 0x00, // Rotational offset
  168. 0x00 // Reserved
  169. };
  170. static const uint8_t CachingPage[] =
  171. {
  172. 0x08, // Page Code
  173. 0x0A, // Page length
  174. 0x01, // Read cache disable
  175. 0x00, // No useful rention policy.
  176. 0x00, 0x00, // Pre-fetch always disabled
  177. 0x00, 0x00, // Minimum pre-fetch
  178. 0x00, 0x00, // Maximum pre-fetch
  179. 0x00, 0x00, // Maximum pre-fetch ceiling
  180. };
  181. // Old CCS SCSI-1 cache page
  182. static const uint8_t CCSCachingPage[] =
  183. {
  184. 0x38, // Page Code
  185. 0x0E, // Page length
  186. 0x00, // Read cache disable
  187. 0x00, // Prefetch threshold
  188. 0x00, 0x00, // Max threshold / multiplier
  189. 0x00, 0x00, // Min threshold / multiplier
  190. 0x00, 0x00, // Reserved
  191. 0x00, 0x00,
  192. 0x00, 0x00,
  193. 0x00, 0x00,
  194. };
  195. static const uint8_t ControlModePage[] =
  196. {
  197. 0x0A, // Page code
  198. 0x06, // Page length
  199. 0x00, // No logging
  200. 0x01, // Disable tagged queuing
  201. 0x00, // No async event notifications
  202. 0x00, // Reserved
  203. 0x00, 0x00 // AEN holdoff period.
  204. };
  205. static const uint8_t SequentialDeviceConfigPage[] =
  206. {
  207. 0x10, // page code
  208. 0x0E, // Page length
  209. 0x00, // CAP, CAF, Active Format
  210. 0x00, // Active partition
  211. 0x00, // Write buffer full ratio
  212. 0x00, // Read buffer empty ratio
  213. 0x00,0x01, // Write delay time, in 100ms units
  214. 0x00, // Default gap size
  215. 0x10, // auto-generation of default eod (end of data)
  216. 0x00,0x00,0x00, // buffer-size at early warning
  217. 0x00, // No data compression
  218. 0x00 // reserved
  219. };
  220. // Allow Apple 68k Drive Setup to format this drive.
  221. // Code
  222. static const uint8_t AppleVendorPage[] =
  223. {
  224. 0x30, // Page code
  225. 0x16, // Page length
  226. 'A','P','P','L','E',' ','C','O','M','P','U','T','E','R',',',' ','I','N','C',' ',' ',' '
  227. };
  228. static const uint8_t BlueSCSIVendorPage[] =
  229. {
  230. 0x31, // Page code
  231. 42, // Page length
  232. 'B','l','u','e','S','C','S','I',' ','i','s',' ','t','h','e',' ','B','E','S','T',' ',
  233. 'S','T','O','L','E','N',' ','F','R','O','M',' ','B','L','U','E','S','C','S','I',0x00
  234. };
  235. static void pageIn(int pc, int dataIdx, const uint8_t* pageData, int pageLen)
  236. {
  237. memcpy(&scsiDev.data[dataIdx], pageData, pageLen);
  238. if (pc == 0x01) // Mask out (un)changable values
  239. {
  240. memset(&scsiDev.data[dataIdx+2], 0, pageLen - 2);
  241. }
  242. }
  243. static void doModeSense(
  244. int sixByteCmd, int dbd, int pc, int pageCode, int allocLength)
  245. {
  246. ////////////// Mode Parameter Header
  247. ////////////////////////////////////
  248. // Skip the Mode Data Length, we set that last.
  249. int idx = 1;
  250. if (!sixByteCmd) ++idx;
  251. uint8_t mediumType = 0;
  252. uint8_t deviceSpecificParam = 0;
  253. uint8_t density = 0;
  254. switch (scsiDev.target->cfg->deviceType)
  255. {
  256. case S2S_CFG_FIXED:
  257. case S2S_CFG_REMOVEABLE:
  258. mediumType = 0; // We should support various floppy types here!
  259. // Contains cache bits (0) and a Write-Protect bit.
  260. deviceSpecificParam =
  261. (blockDev.state & DISK_WP) ? 0x80 : 0;
  262. density = 0; // reserved for direct access
  263. break;
  264. case S2S_CFG_FLOPPY_14MB:
  265. mediumType = 0x1E; // 90mm/3.5"
  266. deviceSpecificParam =
  267. (blockDev.state & DISK_WP) ? 0x80 : 0;
  268. density = 0; // reserved for direct access
  269. break;
  270. case S2S_CFG_OPTICAL:
  271. mediumType = 0x02; // 120mm CDROM, data only.
  272. deviceSpecificParam = 0;
  273. density = 0x01; // User data only, 2048bytes per sector.
  274. break;
  275. case S2S_CFG_SEQUENTIAL:
  276. mediumType = 0; // reserved
  277. deviceSpecificParam =
  278. (blockDev.state & DISK_WP) ? 0x80 : 0;
  279. density = 0x13; // DAT Data Storage, X3B5/88-185A
  280. break;
  281. case S2S_CFG_MO:
  282. mediumType = 0x03; // Optical reversible or erasable medium
  283. deviceSpecificParam =
  284. (blockDev.state & DISK_WP) ? 0x80 : 0;
  285. density = 0x00; // Default
  286. break;
  287. };
  288. scsiDev.data[idx++] = mediumType;
  289. scsiDev.data[idx++] = deviceSpecificParam;
  290. if (sixByteCmd)
  291. {
  292. if (dbd)
  293. {
  294. scsiDev.data[idx++] = 0; // No block descriptor
  295. }
  296. else
  297. {
  298. // One block descriptor of length 8 bytes.
  299. scsiDev.data[idx++] = 8;
  300. }
  301. }
  302. else
  303. {
  304. scsiDev.data[idx++] = 0; // Reserved
  305. scsiDev.data[idx++] = 0; // Reserved
  306. if (dbd)
  307. {
  308. scsiDev.data[idx++] = 0; // No block descriptor
  309. scsiDev.data[idx++] = 0; // No block descriptor
  310. }
  311. else
  312. {
  313. // One block descriptor of length 8 bytes.
  314. scsiDev.data[idx++] = 0;
  315. scsiDev.data[idx++] = 8;
  316. }
  317. }
  318. ////////////// Block Descriptor
  319. ////////////////////////////////////
  320. if (!dbd)
  321. {
  322. scsiDev.data[idx++] = density;
  323. // Number of blocks
  324. // Zero == all remaining blocks shall have the medium
  325. // characteristics specified.
  326. scsiDev.data[idx++] = 0;
  327. scsiDev.data[idx++] = 0;
  328. scsiDev.data[idx++] = 0;
  329. scsiDev.data[idx++] = 0; // reserved
  330. // Block length
  331. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  332. scsiDev.data[idx++] = bytesPerSector >> 16;
  333. scsiDev.data[idx++] = bytesPerSector >> 8;
  334. scsiDev.data[idx++] = bytesPerSector & 0xFF;
  335. }
  336. int pageFound = 0;
  337. if (pageCode == 0x01 || pageCode == 0x3F)
  338. {
  339. pageFound = 1;
  340. if ((scsiDev.compatMode >= COMPAT_SCSI2))
  341. {
  342. pageIn(pc, idx, ReadWriteErrorRecoveryPage, sizeof(ReadWriteErrorRecoveryPage));
  343. idx += sizeof(ReadWriteErrorRecoveryPage);
  344. }
  345. else
  346. {
  347. pageIn(pc, idx, ReadWriteErrorRecoveryPage_SCSI1, sizeof(ReadWriteErrorRecoveryPage_SCSI1));
  348. idx += sizeof(ReadWriteErrorRecoveryPage_SCSI1);
  349. }
  350. }
  351. if (pageCode == 0x02 || pageCode == 0x3F)
  352. {
  353. pageFound = 1;
  354. if ((scsiDev.compatMode >= COMPAT_SCSI2))
  355. {
  356. pageIn(pc, idx, DisconnectReconnectPage, sizeof(DisconnectReconnectPage));
  357. idx += sizeof(DisconnectReconnectPage);
  358. }
  359. else
  360. {
  361. pageIn(pc, idx, DisconnectReconnectPage_SCSI1, sizeof(DisconnectReconnectPage_SCSI1));
  362. idx += sizeof(DisconnectReconnectPage_SCSI1);
  363. }
  364. }
  365. if ((pageCode == 0x03 || pageCode == 0x3F) &&
  366. (scsiDev.target->cfg->deviceType != S2S_CFG_OPTICAL))
  367. {
  368. pageFound = 1;
  369. pageIn(pc, idx, FormatDevicePage, sizeof(FormatDevicePage));
  370. if (pc != 0x01)
  371. {
  372. uint16_t sectorsPerTrack = scsiDev.target->cfg->sectorsPerTrack;
  373. scsiDev.data[idx+10] = sectorsPerTrack >> 8;
  374. scsiDev.data[idx+11] = sectorsPerTrack & 0xFF;
  375. // Fill out the configured bytes-per-sector
  376. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  377. scsiDev.data[idx+12] = bytesPerSector >> 8;
  378. scsiDev.data[idx+13] = bytesPerSector & 0xFF;
  379. }
  380. else
  381. {
  382. // Set a mask for the changeable values.
  383. scsiDev.data[idx+12] = 0xFF;
  384. scsiDev.data[idx+13] = 0xFF;
  385. }
  386. idx += sizeof(FormatDevicePage);
  387. }
  388. if ((pageCode == 0x04 || pageCode == 0x3F) &&
  389. (scsiDev.target->cfg->deviceType != S2S_CFG_OPTICAL))
  390. {
  391. pageFound = 1;
  392. if ((scsiDev.compatMode >= COMPAT_SCSI2))
  393. {
  394. pageIn(pc, idx, RigidDiskDriveGeometry, sizeof(RigidDiskDriveGeometry));
  395. }
  396. else
  397. {
  398. pageIn(pc, idx, RigidDiskDriveGeometry_SCSI1, sizeof(RigidDiskDriveGeometry_SCSI1));
  399. }
  400. if (pc != 0x01)
  401. {
  402. // Need to fill out the number of cylinders.
  403. uint32_t cyl;
  404. uint8_t head;
  405. uint32_t sector;
  406. LBA2CHS(
  407. getScsiCapacity(
  408. scsiDev.target->cfg->sdSectorStart,
  409. scsiDev.target->liveCfg.bytesPerSector,
  410. scsiDev.target->cfg->scsiSectors),
  411. &cyl,
  412. &head,
  413. &sector,
  414. scsiDev.target->cfg->headsPerCylinder,
  415. scsiDev.target->cfg->sectorsPerTrack);
  416. scsiDev.data[idx+2] = cyl >> 16;
  417. scsiDev.data[idx+3] = cyl >> 8;
  418. scsiDev.data[idx+4] = cyl;
  419. memcpy(&scsiDev.data[idx+6], &scsiDev.data[idx+2], 3);
  420. memcpy(&scsiDev.data[idx+9], &scsiDev.data[idx+2], 3);
  421. scsiDev.data[idx+5] = scsiDev.target->cfg->headsPerCylinder;
  422. }
  423. if ((scsiDev.compatMode >= COMPAT_SCSI2))
  424. {
  425. idx += sizeof(RigidDiskDriveGeometry);
  426. }
  427. else
  428. {
  429. idx += sizeof(RigidDiskDriveGeometry_SCSI1);
  430. }
  431. }
  432. if ((pageCode == 0x05 || pageCode == 0x3F) &&
  433. (scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB))
  434. {
  435. pageFound = 1;
  436. pageIn(pc, idx, FlexibleDiskDriveGeometry, sizeof(FlexibleDiskDriveGeometry));
  437. idx += sizeof(FlexibleDiskDriveGeometry);
  438. }
  439. // DON'T output the following pages for SCSI1 hosts. They get upset when
  440. // we have more data to send than the allocation length provided.
  441. // (ie. Try not to output any more pages below this comment)
  442. if ((scsiDev.compatMode >= COMPAT_SCSI2) &&
  443. (pageCode == 0x08 || pageCode == 0x3F))
  444. {
  445. pageFound = 1;
  446. pageIn(pc, idx, CachingPage, sizeof(CachingPage));
  447. idx += sizeof(CachingPage);
  448. }
  449. if ((scsiDev.compatMode >= COMPAT_SCSI2)
  450. && (pageCode == 0x0A || pageCode == 0x3F))
  451. {
  452. pageFound = 1;
  453. pageIn(pc, idx, ControlModePage, sizeof(ControlModePage));
  454. idx += sizeof(ControlModePage);
  455. }
  456. idx += modeSenseCDDevicePage(pc, idx, pageCode, &pageFound);
  457. idx += modeSenseCDAudioControlPage(pc, idx, pageCode, &pageFound);
  458. if ((scsiDev.target->cfg->deviceType == S2S_CFG_SEQUENTIAL) &&
  459. (pageCode == 0x10 || pageCode == 0x3F))
  460. {
  461. pageFound = 1;
  462. pageIn(
  463. pc,
  464. idx,
  465. SequentialDeviceConfigPage,
  466. sizeof(SequentialDeviceConfigPage));
  467. idx += sizeof(SequentialDeviceConfigPage);
  468. }
  469. idx += modeSenseCDCapabilitiesPage(pc, idx, pageCode, &pageFound);
  470. if ((scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_APPLE) &&
  471. (pageCode == 0x30 || pageCode == 0x3F))
  472. {
  473. pageFound = 1;
  474. pageIn(pc, idx, AppleVendorPage, sizeof(AppleVendorPage));
  475. idx += sizeof(AppleVendorPage);
  476. }
  477. if (pageCode == 0x31 || pageCode == 0x3F)
  478. {
  479. pageFound = 1;
  480. pageIn(pc, idx, BlueSCSIVendorPage, sizeof(BlueSCSIVendorPage));
  481. idx += sizeof(BlueSCSIVendorPage);
  482. }
  483. if (pageCode == 0x38) // Don't send unless requested
  484. {
  485. pageFound = 1;
  486. pageIn(pc, idx, CCSCachingPage, sizeof(CCSCachingPage));
  487. idx += sizeof(CCSCachingPage);
  488. }
  489. // SCSI 2 standard says page 0 is always last.
  490. if (pageCode == 0x00 || pageCode == 0x3F)
  491. {
  492. pageFound = 1;
  493. pageIn(pc, idx, OperatingPage, sizeof(OperatingPage));
  494. // Note inverted logic for the flag.
  495. scsiDev.data[idx+2] =
  496. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_UNIT_ATTENTION) ? 0x80 : 0x90;
  497. scsiDev.data[idx+3] = getDeviceTypeQualifier();
  498. idx += sizeof(OperatingPage);
  499. }
  500. if (!pageFound)
  501. {
  502. // Unknown Page Code
  503. pageFound = 0;
  504. scsiDev.status = CHECK_CONDITION;
  505. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  506. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  507. scsiDev.phase = STATUS;
  508. }
  509. else
  510. {
  511. // Go back and fill out the mode data length
  512. if (sixByteCmd)
  513. {
  514. // Cannot currently exceed limits. yay
  515. scsiDev.data[0] = idx - 1;
  516. }
  517. else
  518. {
  519. scsiDev.data[0] = ((idx - 2) >> 8);
  520. scsiDev.data[1] = (idx - 2);
  521. }
  522. scsiDev.dataLen = idx > allocLength ? allocLength : idx;
  523. scsiDev.phase = DATA_IN;
  524. }
  525. }
  526. // Callback after the DATA OUT phase is complete.
  527. static void doModeSelect(void)
  528. {
  529. if (scsiDev.status == GOOD) // skip if we've already encountered an error
  530. {
  531. // scsiDev.dataLen bytes are in scsiDev.data
  532. int idx;
  533. int blockDescLen;
  534. if (scsiDev.cdb[0] == 0x55)
  535. {
  536. blockDescLen =
  537. (((uint16_t)scsiDev.data[6]) << 8) |scsiDev.data[7];
  538. idx = 8;
  539. }
  540. else
  541. {
  542. blockDescLen = scsiDev.data[3];
  543. idx = 4;
  544. }
  545. // The unwritten rule. Blocksizes are normally set using the
  546. // block descriptor value, not by changing page 0x03.
  547. if (blockDescLen >= 8)
  548. {
  549. uint32_t bytesPerSector =
  550. (((uint32_t)scsiDev.data[idx+5]) << 16) |
  551. (((uint32_t)scsiDev.data[idx+6]) << 8) |
  552. scsiDev.data[idx+7];
  553. if ((bytesPerSector < MIN_SECTOR_SIZE) ||
  554. (bytesPerSector > MAX_SECTOR_SIZE))
  555. {
  556. goto bad;
  557. }
  558. else
  559. {
  560. scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;
  561. if (bytesPerSector != scsiDev.target->cfg->bytesPerSector)
  562. {
  563. s2s_configSave(scsiDev.target->targetId, bytesPerSector);
  564. }
  565. }
  566. }
  567. idx += blockDescLen;
  568. while (idx < scsiDev.dataLen)
  569. {
  570. // Change from SCSI2SD: if code page is 0x0 (vendor-specific) it
  571. // will not follow the normal page mode format and cannot be
  572. // parsed, but isn't necessarily an error. Instead, just treat it
  573. // as an 'end of data' field and allow normal command completion.
  574. int pageCode = scsiDev.data[idx] & 0x3F;
  575. if (pageCode == 0) goto out;
  576. int pageLen = scsiDev.data[idx + 1];
  577. if (idx + 2 + pageLen > scsiDev.dataLen) goto bad;
  578. switch (pageCode)
  579. {
  580. case 0x03: // Format Device Page
  581. {
  582. if (pageLen != 0x16) goto bad;
  583. // Fill out the configured bytes-per-sector
  584. uint16_t bytesPerSector =
  585. (((uint16_t)scsiDev.data[idx+12]) << 8) |
  586. scsiDev.data[idx+13];
  587. // Sane values only, ok ?
  588. if ((bytesPerSector < MIN_SECTOR_SIZE) ||
  589. (bytesPerSector > MAX_SECTOR_SIZE))
  590. {
  591. goto bad;
  592. }
  593. scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;
  594. if (scsiDev.cdb[1] & 1) // SP Save Pages flag
  595. {
  596. s2s_configSave(scsiDev.target->targetId, bytesPerSector);
  597. }
  598. }
  599. break;
  600. case 0x0E: // CD audio control page
  601. {
  602. if (!modeSelectCDAudioControlPage(pageLen, idx)) goto bad;
  603. }
  604. break;
  605. //default:
  606. // Easiest to just ignore for now. We'll get here when changing
  607. // the SCSI block size via the descriptor header.
  608. }
  609. idx += 2 + pageLen;
  610. }
  611. }
  612. goto out;
  613. bad:
  614. scsiDev.status = CHECK_CONDITION;
  615. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  616. scsiDev.target->sense.asc = INVALID_FIELD_IN_PARAMETER_LIST;
  617. out:
  618. scsiDev.phase = STATUS;
  619. }
  620. int scsiModeCommand()
  621. {
  622. int commandHandled = 1;
  623. uint8_t command = scsiDev.cdb[0];
  624. // We don't currently support the setting of any parameters.
  625. // (ie. no MODE SELECT(6) or MODE SELECT(10) commands)
  626. if (command == 0x1A)
  627. {
  628. // MODE SENSE(6)
  629. int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors
  630. int pc = scsiDev.cdb[2] >> 6; // Page Control
  631. int pageCode = scsiDev.cdb[2] & 0x3F;
  632. int allocLength = scsiDev.cdb[4];
  633. // SCSI1 standard: (CCS X3T9.2/86-52)
  634. // "An Allocation Length of zero indicates that no MODE SENSE data shall
  635. // be transferred. This condition shall not be considered as an error."
  636. doModeSense(1, dbd, pc, pageCode, allocLength);
  637. }
  638. else if (command == 0x5A)
  639. {
  640. // MODE SENSE(10)
  641. int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors
  642. int pc = scsiDev.cdb[2] >> 6; // Page Control
  643. int pageCode = scsiDev.cdb[2] & 0x3F;
  644. int allocLength =
  645. (((uint16_t) scsiDev.cdb[7]) << 8) +
  646. scsiDev.cdb[8];
  647. doModeSense(0, dbd, pc, pageCode, allocLength);
  648. }
  649. else if (command == 0x15)
  650. {
  651. // MODE SELECT(6)
  652. int len = scsiDev.cdb[4];
  653. if (len == 0)
  654. {
  655. // If len == 0, then transfer no data. From the SCSI 2 standard:
  656. // A parameter list length of zero indicates that no data shall
  657. // be transferred. This condition shall not be considered as an
  658. // error.
  659. scsiDev.phase = STATUS;
  660. }
  661. else
  662. {
  663. scsiDev.dataLen = len;
  664. scsiDev.phase = DATA_OUT;
  665. scsiDev.postDataOutHook = doModeSelect;
  666. }
  667. }
  668. else if (command == 0x55)
  669. {
  670. // MODE SELECT(10)
  671. int allocLength = (((uint16_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  672. if (allocLength == 0)
  673. {
  674. // If len == 0, then transfer no data. From the SCSI 2 standard:
  675. // A parameter list length of zero indicates that no data shall
  676. // be transferred. This condition shall not be considered as an
  677. // error.
  678. scsiDev.phase = STATUS;
  679. }
  680. else
  681. {
  682. scsiDev.dataLen = allocLength;
  683. scsiDev.phase = DATA_OUT;
  684. scsiDev.postDataOutHook = doModeSelect;
  685. }
  686. }
  687. else
  688. {
  689. commandHandled = 0;
  690. }
  691. return commandHandled;
  692. }