mode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 void pageIn(int pc, int dataIdx, const uint8_t* pageData, int pageLen)
  229. {
  230. memcpy(&scsiDev.data[dataIdx], pageData, pageLen);
  231. if (pc == 0x01) // Mask out (un)changable values
  232. {
  233. memset(&scsiDev.data[dataIdx+2], 0, pageLen - 2);
  234. }
  235. }
  236. static void doModeSense(
  237. int sixByteCmd, int dbd, int pc, int pageCode, int allocLength)
  238. {
  239. ////////////// Mode Parameter Header
  240. ////////////////////////////////////
  241. // Skip the Mode Data Length, we set that last.
  242. int idx = 1;
  243. if (!sixByteCmd) ++idx;
  244. uint8_t mediumType = 0;
  245. uint8_t deviceSpecificParam = 0;
  246. uint8_t density = 0;
  247. switch (scsiDev.target->cfg->deviceType)
  248. {
  249. case S2S_CFG_FIXED:
  250. case S2S_CFG_REMOVEABLE:
  251. mediumType = 0; // We should support various floppy types here!
  252. // Contains cache bits (0) and a Write-Protect bit.
  253. deviceSpecificParam =
  254. (blockDev.state & DISK_WP) ? 0x80 : 0;
  255. density = 0; // reserved for direct access
  256. break;
  257. case S2S_CFG_FLOPPY_14MB:
  258. mediumType = 0x1E; // 90mm/3.5"
  259. deviceSpecificParam =
  260. (blockDev.state & DISK_WP) ? 0x80 : 0;
  261. density = 0; // reserved for direct access
  262. break;
  263. case S2S_CFG_OPTICAL:
  264. mediumType = 0x02; // 120mm CDROM, data only.
  265. deviceSpecificParam = 0;
  266. density = 0x01; // User data only, 2048bytes per sector.
  267. break;
  268. case S2S_CFG_SEQUENTIAL:
  269. mediumType = 0; // reserved
  270. deviceSpecificParam =
  271. (blockDev.state & DISK_WP) ? 0x80 : 0;
  272. density = 0x13; // DAT Data Storage, X3B5/88-185A
  273. break;
  274. case S2S_CFG_MO:
  275. mediumType = 0x03; // Optical reversible or erasable medium
  276. deviceSpecificParam =
  277. (blockDev.state & DISK_WP) ? 0x80 : 0;
  278. density = 0x00; // Default
  279. break;
  280. };
  281. scsiDev.data[idx++] = mediumType;
  282. scsiDev.data[idx++] = deviceSpecificParam;
  283. if (sixByteCmd)
  284. {
  285. if (dbd)
  286. {
  287. scsiDev.data[idx++] = 0; // No block descriptor
  288. }
  289. else
  290. {
  291. // One block descriptor of length 8 bytes.
  292. scsiDev.data[idx++] = 8;
  293. }
  294. }
  295. else
  296. {
  297. scsiDev.data[idx++] = 0; // Reserved
  298. scsiDev.data[idx++] = 0; // Reserved
  299. if (dbd)
  300. {
  301. scsiDev.data[idx++] = 0; // No block descriptor
  302. scsiDev.data[idx++] = 0; // No block descriptor
  303. }
  304. else
  305. {
  306. // One block descriptor of length 8 bytes.
  307. scsiDev.data[idx++] = 0;
  308. scsiDev.data[idx++] = 8;
  309. }
  310. }
  311. ////////////// Block Descriptor
  312. ////////////////////////////////////
  313. if (!dbd)
  314. {
  315. scsiDev.data[idx++] = density;
  316. // Number of blocks
  317. // Zero == all remaining blocks shall have the medium
  318. // characteristics specified.
  319. scsiDev.data[idx++] = 0;
  320. scsiDev.data[idx++] = 0;
  321. scsiDev.data[idx++] = 0;
  322. scsiDev.data[idx++] = 0; // reserved
  323. // Block length
  324. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  325. scsiDev.data[idx++] = bytesPerSector >> 16;
  326. scsiDev.data[idx++] = bytesPerSector >> 8;
  327. scsiDev.data[idx++] = bytesPerSector & 0xFF;
  328. }
  329. int pageFound = 0;
  330. if (pageCode == 0x01 || pageCode == 0x3F)
  331. {
  332. pageFound = 1;
  333. if ((scsiDev.compatMode >= COMPAT_SCSI2))
  334. {
  335. pageIn(pc, idx, ReadWriteErrorRecoveryPage, sizeof(ReadWriteErrorRecoveryPage));
  336. idx += sizeof(ReadWriteErrorRecoveryPage);
  337. }
  338. else
  339. {
  340. pageIn(pc, idx, ReadWriteErrorRecoveryPage_SCSI1, sizeof(ReadWriteErrorRecoveryPage_SCSI1));
  341. idx += sizeof(ReadWriteErrorRecoveryPage_SCSI1);
  342. }
  343. }
  344. if (pageCode == 0x02 || pageCode == 0x3F)
  345. {
  346. pageFound = 1;
  347. if ((scsiDev.compatMode >= COMPAT_SCSI2))
  348. {
  349. pageIn(pc, idx, DisconnectReconnectPage, sizeof(DisconnectReconnectPage));
  350. idx += sizeof(DisconnectReconnectPage);
  351. }
  352. else
  353. {
  354. pageIn(pc, idx, DisconnectReconnectPage_SCSI1, sizeof(DisconnectReconnectPage_SCSI1));
  355. idx += sizeof(DisconnectReconnectPage_SCSI1);
  356. }
  357. }
  358. if ((pageCode == 0x03 || pageCode == 0x3F) &&
  359. (scsiDev.target->cfg->deviceType != S2S_CFG_OPTICAL))
  360. {
  361. pageFound = 1;
  362. pageIn(pc, idx, FormatDevicePage, sizeof(FormatDevicePage));
  363. if (pc != 0x01)
  364. {
  365. uint16_t sectorsPerTrack = scsiDev.target->cfg->sectorsPerTrack;
  366. scsiDev.data[idx+10] = sectorsPerTrack >> 8;
  367. scsiDev.data[idx+11] = sectorsPerTrack & 0xFF;
  368. // Fill out the configured bytes-per-sector
  369. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  370. scsiDev.data[idx+12] = bytesPerSector >> 8;
  371. scsiDev.data[idx+13] = bytesPerSector & 0xFF;
  372. }
  373. else
  374. {
  375. // Set a mask for the changeable values.
  376. scsiDev.data[idx+12] = 0xFF;
  377. scsiDev.data[idx+13] = 0xFF;
  378. }
  379. idx += sizeof(FormatDevicePage);
  380. }
  381. if ((pageCode == 0x04 || pageCode == 0x3F) &&
  382. (scsiDev.target->cfg->deviceType != S2S_CFG_OPTICAL))
  383. {
  384. pageFound = 1;
  385. if ((scsiDev.compatMode >= COMPAT_SCSI2))
  386. {
  387. pageIn(pc, idx, RigidDiskDriveGeometry, sizeof(RigidDiskDriveGeometry));
  388. }
  389. else
  390. {
  391. pageIn(pc, idx, RigidDiskDriveGeometry_SCSI1, sizeof(RigidDiskDriveGeometry_SCSI1));
  392. }
  393. if (pc != 0x01)
  394. {
  395. // Need to fill out the number of cylinders.
  396. uint32_t cyl;
  397. uint8_t head;
  398. uint32_t sector;
  399. LBA2CHS(
  400. getScsiCapacity(
  401. scsiDev.target->cfg->sdSectorStart,
  402. scsiDev.target->liveCfg.bytesPerSector,
  403. scsiDev.target->cfg->scsiSectors),
  404. &cyl,
  405. &head,
  406. &sector,
  407. scsiDev.target->cfg->headsPerCylinder,
  408. scsiDev.target->cfg->sectorsPerTrack);
  409. scsiDev.data[idx+2] = cyl >> 16;
  410. scsiDev.data[idx+3] = cyl >> 8;
  411. scsiDev.data[idx+4] = cyl;
  412. memcpy(&scsiDev.data[idx+6], &scsiDev.data[idx+2], 3);
  413. memcpy(&scsiDev.data[idx+9], &scsiDev.data[idx+2], 3);
  414. scsiDev.data[idx+5] = scsiDev.target->cfg->headsPerCylinder;
  415. }
  416. if ((scsiDev.compatMode >= COMPAT_SCSI2))
  417. {
  418. idx += sizeof(RigidDiskDriveGeometry);
  419. }
  420. else
  421. {
  422. idx += sizeof(RigidDiskDriveGeometry_SCSI1);
  423. }
  424. }
  425. if ((pageCode == 0x05 || pageCode == 0x3F) &&
  426. (scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB))
  427. {
  428. pageFound = 1;
  429. pageIn(pc, idx, FlexibleDiskDriveGeometry, sizeof(FlexibleDiskDriveGeometry));
  430. idx += sizeof(FlexibleDiskDriveGeometry);
  431. }
  432. // DON'T output the following pages for SCSI1 hosts. They get upset when
  433. // we have more data to send than the allocation length provided.
  434. // (ie. Try not to output any more pages below this comment)
  435. if ((scsiDev.compatMode >= COMPAT_SCSI2) &&
  436. (pageCode == 0x08 || pageCode == 0x3F))
  437. {
  438. pageFound = 1;
  439. pageIn(pc, idx, CachingPage, sizeof(CachingPage));
  440. idx += sizeof(CachingPage);
  441. }
  442. if ((scsiDev.compatMode >= COMPAT_SCSI2)
  443. && (pageCode == 0x0A || pageCode == 0x3F))
  444. {
  445. pageFound = 1;
  446. pageIn(pc, idx, ControlModePage, sizeof(ControlModePage));
  447. idx += sizeof(ControlModePage);
  448. }
  449. idx += modeSenseCDDevicePage(pc, idx, pageCode, &pageFound);
  450. idx += modeSenseCDAudioControlPage(pc, idx, pageCode, &pageFound);
  451. if ((scsiDev.target->cfg->deviceType == S2S_CFG_SEQUENTIAL) &&
  452. (pageCode == 0x10 || pageCode == 0x3F))
  453. {
  454. pageFound = 1;
  455. pageIn(
  456. pc,
  457. idx,
  458. SequentialDeviceConfigPage,
  459. sizeof(SequentialDeviceConfigPage));
  460. idx += sizeof(SequentialDeviceConfigPage);
  461. }
  462. idx += modeSenseCDCapabilitiesPage(pc, idx, pageCode, &pageFound);
  463. if ((scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_APPLE) &&
  464. (idx + sizeof(AppleVendorPage) <= allocLength) &&
  465. (pageCode == 0x30 || pageCode == 0x3F))
  466. {
  467. pageFound = 1;
  468. pageIn(pc, idx, AppleVendorPage, sizeof(AppleVendorPage));
  469. idx += sizeof(AppleVendorPage);
  470. }
  471. if (pageCode == 0x38) // Don't send unless requested
  472. {
  473. pageFound = 1;
  474. pageIn(pc, idx, CCSCachingPage, sizeof(CCSCachingPage));
  475. idx += sizeof(CCSCachingPage);
  476. }
  477. // SCSI 2 standard says page 0 is always last.
  478. if (pageCode == 0x00 || pageCode == 0x3F)
  479. {
  480. pageFound = 1;
  481. pageIn(pc, idx, OperatingPage, sizeof(OperatingPage));
  482. // Note inverted logic for the flag.
  483. scsiDev.data[idx+2] =
  484. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_UNIT_ATTENTION) ? 0x80 : 0x90;
  485. scsiDev.data[idx+3] = getDeviceTypeQualifier();
  486. idx += sizeof(OperatingPage);
  487. }
  488. if (!pageFound)
  489. {
  490. // Unknown Page Code
  491. pageFound = 0;
  492. scsiDev.status = CHECK_CONDITION;
  493. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  494. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  495. scsiDev.phase = STATUS;
  496. }
  497. else
  498. {
  499. // Go back and fill out the mode data length
  500. if (sixByteCmd)
  501. {
  502. // Cannot currently exceed limits. yay
  503. scsiDev.data[0] = idx - 1;
  504. }
  505. else
  506. {
  507. scsiDev.data[0] = ((idx - 2) >> 8);
  508. scsiDev.data[1] = (idx - 2);
  509. }
  510. scsiDev.dataLen = idx > allocLength ? allocLength : idx;
  511. scsiDev.phase = DATA_IN;
  512. }
  513. }
  514. // Callback after the DATA OUT phase is complete.
  515. static void doModeSelect(void)
  516. {
  517. if (scsiDev.status == GOOD) // skip if we've already encountered an error
  518. {
  519. // scsiDev.dataLen bytes are in scsiDev.data
  520. int idx;
  521. int blockDescLen;
  522. if (scsiDev.cdb[0] == 0x55)
  523. {
  524. blockDescLen =
  525. (((uint16_t)scsiDev.data[6]) << 8) |scsiDev.data[7];
  526. idx = 8;
  527. }
  528. else
  529. {
  530. blockDescLen = scsiDev.data[3];
  531. idx = 4;
  532. }
  533. // The unwritten rule. Blocksizes are normally set using the
  534. // block descriptor value, not by changing page 0x03.
  535. if (blockDescLen >= 8)
  536. {
  537. uint32_t bytesPerSector =
  538. (((uint32_t)scsiDev.data[idx+5]) << 16) |
  539. (((uint32_t)scsiDev.data[idx+6]) << 8) |
  540. scsiDev.data[idx+7];
  541. if ((bytesPerSector < MIN_SECTOR_SIZE) ||
  542. (bytesPerSector > MAX_SECTOR_SIZE))
  543. {
  544. goto bad;
  545. }
  546. else
  547. {
  548. scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;
  549. if (bytesPerSector != scsiDev.target->cfg->bytesPerSector)
  550. {
  551. s2s_configSave(scsiDev.target->targetId, bytesPerSector);
  552. }
  553. }
  554. }
  555. idx += blockDescLen;
  556. while (idx < scsiDev.dataLen)
  557. {
  558. // Change from SCSI2SD: if code page is 0x0 (vendor-specific) it
  559. // will not follow the normal page mode format and cannot be
  560. // parsed, but isn't necessarily an error. Instead, just treat it
  561. // as an 'end of data' field and allow normal command completion.
  562. int pageCode = scsiDev.data[idx] & 0x3F;
  563. if (pageCode == 0) goto out;
  564. int pageLen = scsiDev.data[idx + 1];
  565. if (idx + 2 + pageLen > scsiDev.dataLen) goto bad;
  566. switch (pageCode)
  567. {
  568. case 0x03: // Format Device Page
  569. {
  570. if (pageLen != 0x16) goto bad;
  571. // Fill out the configured bytes-per-sector
  572. uint16_t bytesPerSector =
  573. (((uint16_t)scsiDev.data[idx+12]) << 8) |
  574. scsiDev.data[idx+13];
  575. // Sane values only, ok ?
  576. if ((bytesPerSector < MIN_SECTOR_SIZE) ||
  577. (bytesPerSector > MAX_SECTOR_SIZE))
  578. {
  579. goto bad;
  580. }
  581. scsiDev.target->liveCfg.bytesPerSector = bytesPerSector;
  582. if (scsiDev.cdb[1] & 1) // SP Save Pages flag
  583. {
  584. s2s_configSave(scsiDev.target->targetId, bytesPerSector);
  585. }
  586. }
  587. break;
  588. case 0x0E: // CD audio control page
  589. {
  590. if (!modeSelectCDAudioControlPage(pageLen, idx)) goto bad;
  591. }
  592. break;
  593. //default:
  594. // Easiest to just ignore for now. We'll get here when changing
  595. // the SCSI block size via the descriptor header.
  596. }
  597. idx += 2 + pageLen;
  598. }
  599. }
  600. goto out;
  601. bad:
  602. scsiDev.status = CHECK_CONDITION;
  603. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  604. scsiDev.target->sense.asc = INVALID_FIELD_IN_PARAMETER_LIST;
  605. out:
  606. scsiDev.phase = STATUS;
  607. }
  608. int scsiModeCommand()
  609. {
  610. int commandHandled = 1;
  611. uint8_t command = scsiDev.cdb[0];
  612. // We don't currently support the setting of any parameters.
  613. // (ie. no MODE SELECT(6) or MODE SELECT(10) commands)
  614. if (command == 0x1A)
  615. {
  616. // MODE SENSE(6)
  617. int dbd = scsiDev.cdb[1] & 0x08; // Disable block descriptors
  618. int pc = scsiDev.cdb[2] >> 6; // Page Control
  619. int pageCode = scsiDev.cdb[2] & 0x3F;
  620. int allocLength = scsiDev.cdb[4];
  621. // SCSI1 standard: (CCS X3T9.2/86-52)
  622. // "An Allocation Length of zero indicates that no MODE SENSE data shall
  623. // be transferred. This condition shall not be considered as an error."
  624. doModeSense(1, dbd, pc, pageCode, allocLength);
  625. }
  626. else if (command == 0x5A)
  627. {
  628. // MODE SENSE(10)
  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 =
  633. (((uint16_t) scsiDev.cdb[7]) << 8) +
  634. scsiDev.cdb[8];
  635. doModeSense(0, dbd, pc, pageCode, allocLength);
  636. }
  637. else if (command == 0x15)
  638. {
  639. // MODE SELECT(6)
  640. int len = scsiDev.cdb[4];
  641. if (len == 0)
  642. {
  643. // If len == 0, then transfer no data. From the SCSI 2 standard:
  644. // A parameter list length of zero indicates that no data shall
  645. // be transferred. This condition shall not be considered as an
  646. // error.
  647. scsiDev.phase = STATUS;
  648. }
  649. else
  650. {
  651. scsiDev.dataLen = len;
  652. scsiDev.phase = DATA_OUT;
  653. scsiDev.postDataOutHook = doModeSelect;
  654. }
  655. }
  656. else if (command == 0x55)
  657. {
  658. // MODE SELECT(10)
  659. int allocLength = (((uint16_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  660. if (allocLength == 0)
  661. {
  662. // If len == 0, then transfer no data. From the SCSI 2 standard:
  663. // A parameter list length of zero indicates that no data shall
  664. // be transferred. This condition shall not be considered as an
  665. // error.
  666. scsiDev.phase = STATUS;
  667. }
  668. else
  669. {
  670. scsiDev.dataLen = allocLength;
  671. scsiDev.phase = DATA_OUT;
  672. scsiDev.postDataOutHook = doModeSelect;
  673. }
  674. }
  675. else
  676. {
  677. commandHandled = 0;
  678. }
  679. return commandHandled;
  680. }