BlueSCSI_initiator.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * BlueSCSI
  3. * Copyright (c) 2022 Rabbit Hole Computing
  4. *
  5. * Main program for initiator mode.
  6. */
  7. #include "BlueSCSI_config.h"
  8. #include "BlueSCSI_log.h"
  9. #include "BlueSCSI_log_trace.h"
  10. #include "BlueSCSI_initiator.h"
  11. #include <BlueSCSI_platform.h>
  12. #include "SdFat.h"
  13. #include <scsi2sd.h>
  14. extern "C" {
  15. #include <scsi.h>
  16. }
  17. #ifndef PLATFORM_HAS_INITIATOR_MODE
  18. void scsiInitiatorInit()
  19. {
  20. }
  21. void scsiInitiatorMainLoop()
  22. {
  23. }
  24. int scsiInitiatorRunCommand(const uint8_t *command, size_t cmdlen,
  25. uint8_t *bufIn, size_t bufInLen,
  26. const uint8_t *bufOut, size_t bufOutLen)
  27. {
  28. return -1;
  29. }
  30. bool scsiInitiatorReadCapacity(int target_id, uint32_t *sectorcount, uint32_t *sectorsize)
  31. {
  32. return false;
  33. }
  34. #else
  35. /*************************************
  36. * High level initiator mode logic *
  37. *************************************/
  38. static struct {
  39. // Bitmap of all drives that have been imaged
  40. uint32_t drives_imaged;
  41. // Is imaging a drive in progress, or are we scanning?
  42. bool imaging;
  43. // Information about currently selected drive
  44. int target_id;
  45. uint32_t sectorsize;
  46. uint32_t sectorcount;
  47. uint32_t sectorcount_all;
  48. uint32_t sectors_done;
  49. uint32_t max_sector_per_transfer;
  50. // Retry information for sector reads.
  51. // If a large read fails, retry is done sector-by-sector.
  52. int retrycount;
  53. uint32_t failposition;
  54. FsFile target_file;
  55. } g_initiator_state;
  56. extern SdFs SD;
  57. // Initialization of initiator mode
  58. void scsiInitiatorInit()
  59. {
  60. scsiHostPhyReset();
  61. g_initiator_state.drives_imaged = 0;
  62. g_initiator_state.imaging = false;
  63. g_initiator_state.target_id = -1;
  64. g_initiator_state.sectorsize = 0;
  65. g_initiator_state.sectorcount = 0;
  66. g_initiator_state.sectors_done = 0;
  67. g_initiator_state.retrycount = 0;
  68. g_initiator_state.failposition = 0;
  69. g_initiator_state.max_sector_per_transfer = 512;
  70. }
  71. // Update progress bar LED during transfers
  72. static void scsiInitiatorUpdateLed()
  73. {
  74. // Update status indicator, the led blinks every 5 seconds and is on the longer the more data has been transferred
  75. const int period = 256;
  76. int phase = (millis() % period);
  77. int duty = g_initiator_state.sectors_done * period / g_initiator_state.sectorcount;
  78. // Minimum and maximum time to verify that the blink is visible
  79. if (duty < 50) duty = 50;
  80. if (duty > period - 50) duty = period - 50;
  81. if (phase <= duty)
  82. {
  83. LED_ON();
  84. }
  85. else
  86. {
  87. LED_OFF();
  88. }
  89. }
  90. // High level logic of the initiator mode
  91. void scsiInitiatorMainLoop()
  92. {
  93. if (!g_initiator_state.imaging)
  94. {
  95. // Scan for SCSI drives one at a time
  96. g_initiator_state.target_id = (g_initiator_state.target_id + 1) % 8;
  97. g_initiator_state.sectors_done = 0;
  98. g_initiator_state.retrycount = 0;
  99. g_initiator_state.max_sector_per_transfer = 512;
  100. if (!(g_initiator_state.drives_imaged & (1 << g_initiator_state.target_id)))
  101. {
  102. delay(1000);
  103. uint8_t inquiry_data[36];
  104. LED_ON();
  105. bool startstopok =
  106. scsiTestUnitReady(g_initiator_state.target_id) &&
  107. scsiStartStopUnit(g_initiator_state.target_id, true);
  108. bool readcapok = startstopok &&
  109. scsiInitiatorReadCapacity(g_initiator_state.target_id,
  110. &g_initiator_state.sectorcount,
  111. &g_initiator_state.sectorsize);
  112. bool inquiryok = startstopok &&
  113. scsiInquiry(g_initiator_state.target_id, inquiry_data);
  114. LED_OFF();
  115. if (readcapok)
  116. {
  117. bluelog("SCSI id ", g_initiator_state.target_id,
  118. " capacity ", (int)g_initiator_state.sectorcount,
  119. " sectors x ", (int)g_initiator_state.sectorsize, " bytes");
  120. g_initiator_state.sectorcount_all = g_initiator_state.sectorcount;
  121. uint64_t total_bytes = (uint64_t)g_initiator_state.sectorcount * g_initiator_state.sectorsize;
  122. bluelog("Drive total size is ", (int)(total_bytes / (1024 * 1024)), " MiB");
  123. if (total_bytes >= 0xFFFFFFFF && SD.fatType() != FAT_TYPE_EXFAT)
  124. {
  125. // Note: the FAT32 limit is 4 GiB - 1 byte
  126. bluelog("Image files equal or larger than 4 GiB are only possible on exFAT filesystem");
  127. bluelog("Please reformat the SD card with exFAT format to image this drive fully");
  128. g_initiator_state.sectorcount = (uint32_t)0xFFFFFFFF / g_initiator_state.sectorsize;
  129. bluelog("Will image first 4 GiB - 1 = ", (int)g_initiator_state.sectorcount, " sectors");
  130. }
  131. }
  132. else if (startstopok)
  133. {
  134. bluelog("SCSI id ", g_initiator_state.target_id, " responds but ReadCapacity command failed");
  135. bluelog("Possibly SCSI-1 drive? Attempting to read up to 1 GB.");
  136. g_initiator_state.sectorsize = 512;
  137. g_initiator_state.sectorcount = g_initiator_state.sectorcount_all = 2097152;
  138. g_initiator_state.max_sector_per_transfer = 128;
  139. }
  140. else
  141. {
  142. bluedbg("Failed to connect to SCSI id ", g_initiator_state.target_id);
  143. g_initiator_state.sectorsize = 0;
  144. g_initiator_state.sectorcount = g_initiator_state.sectorcount_all = 0;
  145. }
  146. const char *filename_format = "HD00_imaged.hda";
  147. if (inquiryok)
  148. {
  149. if ((inquiry_data[0] & 0x1F) == 5)
  150. {
  151. filename_format = "CD00_imaged.iso";
  152. }
  153. }
  154. if (g_initiator_state.sectorcount > 0)
  155. {
  156. char filename[32] = {0};
  157. strncpy(filename, filename_format, sizeof(filename) - 1);
  158. filename[2] += g_initiator_state.target_id;
  159. SD.remove(filename);
  160. g_initiator_state.target_file = SD.open(filename, O_RDWR | O_CREAT | O_TRUNC);
  161. if (!g_initiator_state.target_file.isOpen())
  162. {
  163. bluelog("Failed to open file for writing: ", filename);
  164. return;
  165. }
  166. if (SD.fatType() == FAT_TYPE_EXFAT)
  167. {
  168. // Only preallocate on exFAT, on FAT32 preallocating can result in false garbage data in the
  169. // file if write is interrupted.
  170. bluelog("Preallocating image file");
  171. g_initiator_state.target_file.preAllocate((uint64_t)g_initiator_state.sectorcount * g_initiator_state.sectorsize);
  172. }
  173. bluelog("Starting to copy drive data to ", filename);
  174. g_initiator_state.imaging = true;
  175. }
  176. }
  177. }
  178. else
  179. {
  180. // Copy sectors from SCSI drive to file
  181. if (g_initiator_state.sectors_done >= g_initiator_state.sectorcount)
  182. {
  183. scsiStartStopUnit(g_initiator_state.target_id, false);
  184. bluelog("Finished imaging drive with id ", g_initiator_state.target_id);
  185. LED_OFF();
  186. if (g_initiator_state.sectorcount != g_initiator_state.sectorcount_all)
  187. {
  188. bluelog("NOTE: Image size was limited to first 4 GiB due to SD card filesystem limit");
  189. bluelog("Please reformat the SD card with exFAT format to image this drive fully");
  190. }
  191. g_initiator_state.drives_imaged |= (1 << g_initiator_state.target_id);
  192. g_initiator_state.imaging = false;
  193. g_initiator_state.target_file.close();
  194. return;
  195. }
  196. scsiInitiatorUpdateLed();
  197. // How many sectors to read in one batch?
  198. int numtoread = g_initiator_state.sectorcount - g_initiator_state.sectors_done;
  199. if (numtoread > g_initiator_state.max_sector_per_transfer)
  200. numtoread = g_initiator_state.max_sector_per_transfer;
  201. // Retry sector-by-sector after failure
  202. if (g_initiator_state.sectors_done < g_initiator_state.failposition)
  203. numtoread = 1;
  204. uint32_t time_start = millis();
  205. bool status = scsiInitiatorReadDataToFile(g_initiator_state.target_id,
  206. g_initiator_state.sectors_done, numtoread, g_initiator_state.sectorsize,
  207. g_initiator_state.target_file);
  208. if (!status)
  209. {
  210. bluelog("Failed to transfer ", numtoread, " sectors starting at ", (int)g_initiator_state.sectors_done);
  211. if (g_initiator_state.retrycount < 5)
  212. {
  213. bluelog("Retrying.. ", g_initiator_state.retrycount, "/5");
  214. delay(200);
  215. scsiHostPhyReset();
  216. delay(200);
  217. g_initiator_state.retrycount++;
  218. g_initiator_state.target_file.seek((uint64_t)g_initiator_state.sectors_done * g_initiator_state.sectorsize);
  219. if (g_initiator_state.retrycount > 1 && numtoread > 1)
  220. {
  221. bluelog("Multiple failures, retrying sector-by-sector");
  222. g_initiator_state.failposition = g_initiator_state.sectors_done + numtoread;
  223. }
  224. }
  225. else
  226. {
  227. bluelog("Retry limit exceeded, skipping one sector");
  228. g_initiator_state.retrycount = 0;
  229. g_initiator_state.sectors_done++;
  230. g_initiator_state.target_file.seek((uint64_t)g_initiator_state.sectors_done * g_initiator_state.sectorsize);
  231. }
  232. }
  233. else
  234. {
  235. g_initiator_state.retrycount = 0;
  236. g_initiator_state.sectors_done += numtoread;
  237. g_initiator_state.target_file.flush();
  238. int speed_kbps = numtoread * g_initiator_state.sectorsize / (millis() - time_start);
  239. bluelog("SCSI read succeeded, sectors done: ",
  240. (int)g_initiator_state.sectors_done, " / ", (int)g_initiator_state.sectorcount,
  241. " speed ", speed_kbps, " kB/s");
  242. }
  243. }
  244. }
  245. /*************************************
  246. * Low level command implementations *
  247. *************************************/
  248. int scsiInitiatorRunCommand(int target_id,
  249. const uint8_t *command, size_t cmdLen,
  250. uint8_t *bufIn, size_t bufInLen,
  251. const uint8_t *bufOut, size_t bufOutLen,
  252. bool returnDataPhase)
  253. {
  254. if (!scsiHostPhySelect(target_id))
  255. {
  256. bluedbg("------ Target ", target_id, " did not respond");
  257. scsiHostPhyRelease();
  258. return -1;
  259. }
  260. SCSI_PHASE phase;
  261. int status = -1;
  262. while ((phase = (SCSI_PHASE)scsiHostPhyGetPhase()) != BUS_FREE)
  263. {
  264. if (phase == MESSAGE_IN)
  265. {
  266. uint8_t dummy = 0;
  267. scsiHostRead(&dummy, 1);
  268. }
  269. else if (phase == MESSAGE_OUT)
  270. {
  271. uint8_t identify_msg = 0x80;
  272. scsiHostWrite(&identify_msg, 1);
  273. }
  274. else if (phase == COMMAND)
  275. {
  276. scsiHostWrite(command, cmdLen);
  277. }
  278. else if (phase == DATA_IN)
  279. {
  280. if (returnDataPhase) return 0;
  281. if (bufInLen == 0)
  282. {
  283. bluelog("DATA_IN phase but no data to receive!");
  284. status = -3;
  285. break;
  286. }
  287. if (scsiHostRead(bufIn, bufInLen) == 0)
  288. {
  289. bluelog("scsiHostRead failed, tried to read ", (int)bufInLen, " bytes");
  290. status = -2;
  291. break;
  292. }
  293. }
  294. else if (phase == DATA_OUT)
  295. {
  296. if (returnDataPhase) return 0;
  297. if (bufOutLen == 0)
  298. {
  299. bluelog("DATA_OUT phase but no data to send!");
  300. status = -3;
  301. break;
  302. }
  303. if (scsiHostWrite(bufOut, bufOutLen) < bufOutLen)
  304. {
  305. bluelog("scsiHostWrite failed, was writing ", bytearray(bufOut, bufOutLen));
  306. status = -2;
  307. break;
  308. }
  309. }
  310. else if (phase == STATUS)
  311. {
  312. uint8_t tmp = -1;
  313. scsiHostRead(&tmp, 1);
  314. status = tmp;
  315. bluedbg("------ STATUS: ", tmp);
  316. }
  317. }
  318. scsiHostPhyRelease();
  319. return status;
  320. }
  321. bool scsiInitiatorReadCapacity(int target_id, uint32_t *sectorcount, uint32_t *sectorsize)
  322. {
  323. uint8_t command[10] = {0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  324. uint8_t response[8] = {0};
  325. int status = scsiInitiatorRunCommand(target_id,
  326. command, sizeof(command),
  327. response, sizeof(response),
  328. NULL, 0);
  329. if (status == 0)
  330. {
  331. *sectorcount = ((uint32_t)response[0] << 24)
  332. | ((uint32_t)response[1] << 16)
  333. | ((uint32_t)response[2] << 8)
  334. | ((uint32_t)response[3] << 0);
  335. *sectorcount += 1; // SCSI reports last sector address
  336. *sectorsize = ((uint32_t)response[4] << 24)
  337. | ((uint32_t)response[5] << 16)
  338. | ((uint32_t)response[6] << 8)
  339. | ((uint32_t)response[7] << 0);
  340. return true;
  341. }
  342. else if (status == 2)
  343. {
  344. uint8_t sense_key;
  345. scsiRequestSense(target_id, &sense_key);
  346. bluelog("READ CAPACITY on target ", target_id, " failed, sense key ", sense_key);
  347. return false;
  348. }
  349. else
  350. {
  351. *sectorcount = *sectorsize = 0;
  352. return false;
  353. }
  354. }
  355. // Execute REQUEST SENSE command to get more information about error status
  356. bool scsiRequestSense(int target_id, uint8_t *sense_key)
  357. {
  358. uint8_t command[6] = {0x03, 0, 0, 0, 18, 0};
  359. uint8_t response[18] = {0};
  360. int status = scsiInitiatorRunCommand(target_id,
  361. command, sizeof(command),
  362. response, sizeof(response),
  363. NULL, 0);
  364. bluedbg("RequestSense response: ", bytearray(response, 18));
  365. *sense_key = response[2];
  366. return status == 0;
  367. }
  368. // Execute UNIT START STOP command to load/unload media
  369. bool scsiStartStopUnit(int target_id, bool start)
  370. {
  371. uint8_t command[6] = {0x1B, 0, 0, 0, 0, 0};
  372. uint8_t response[4] = {0};
  373. if (start) command[4] |= 1;
  374. int status = scsiInitiatorRunCommand(target_id,
  375. command, sizeof(command),
  376. response, sizeof(response),
  377. NULL, 0);
  378. if (status == 2)
  379. {
  380. uint8_t sense_key;
  381. scsiRequestSense(target_id, &sense_key);
  382. bluelog("START STOP UNIT on target ", target_id, " failed, sense key ", sense_key);
  383. }
  384. return status == 0;
  385. }
  386. // Execute INQUIRY command
  387. bool scsiInquiry(int target_id, uint8_t inquiry_data[36])
  388. {
  389. uint8_t command[6] = {0x12, 0, 0, 0, 36, 0};
  390. int status = scsiInitiatorRunCommand(target_id,
  391. command, sizeof(command),
  392. inquiry_data, 36,
  393. NULL, 0);
  394. return status == 0;
  395. }
  396. // Execute TEST UNIT READY command and handle unit attention state
  397. bool scsiTestUnitReady(int target_id)
  398. {
  399. for (int retries = 0; retries < 2; retries++)
  400. {
  401. uint8_t command[6] = {0x00, 0, 0, 0, 0, 0};
  402. int status = scsiInitiatorRunCommand(target_id,
  403. command, sizeof(command),
  404. NULL, 0,
  405. NULL, 0);
  406. if (status == 0)
  407. {
  408. return true;
  409. }
  410. else if (status == -1)
  411. {
  412. // No response to select
  413. return false;
  414. }
  415. else if (status == 2)
  416. {
  417. uint8_t sense_key;
  418. scsiRequestSense(target_id, &sense_key);
  419. if (sense_key == 6)
  420. {
  421. uint8_t inquiry[36];
  422. bluelog("Target ", target_id, " reports UNIT_ATTENTION, running INQUIRY");
  423. scsiInquiry(target_id, inquiry);
  424. }
  425. else if (sense_key == 2)
  426. {
  427. bluelog("Target ", target_id, " reports NOT_READY, running STARTSTOPUNIT");
  428. scsiStartStopUnit(target_id, true);
  429. }
  430. }
  431. else
  432. {
  433. bluelog("Target ", target_id, " TEST UNIT READY response: ", status);
  434. }
  435. }
  436. return false;
  437. }
  438. // This uses callbacks to run SD and SCSI transfers in parallel
  439. static struct {
  440. uint32_t bytes_sd; // Number of bytes that have been transferred on SD card side
  441. uint32_t bytes_sd_scheduled; // Number of bytes scheduled for transfer on SD card side
  442. uint32_t bytes_scsi; // Number of bytes that have been scheduled for transfer on SCSI side
  443. uint32_t bytes_scsi_done; // Number of bytes that have been transferred on SCSI side
  444. uint32_t bytes_per_sector;
  445. bool all_ok;
  446. } g_initiator_transfer;
  447. static void initiatorReadSDCallback(uint32_t bytes_complete)
  448. {
  449. if (g_initiator_transfer.bytes_scsi_done < g_initiator_transfer.bytes_scsi)
  450. {
  451. // How many bytes remaining in the transfer?
  452. uint32_t remain = g_initiator_transfer.bytes_scsi - g_initiator_transfer.bytes_scsi_done;
  453. uint32_t len = remain;
  454. // Limit maximum amount of data transferred at one go, to give enough callbacks to SD driver.
  455. // Select the limit based on total bytes in the transfer.
  456. // Transfer size is reduced towards the end of transfer to reduce the dead time between
  457. // end of SCSI transfer and the SD write completing.
  458. uint32_t limit = g_initiator_transfer.bytes_scsi / 8;
  459. uint32_t bytesPerSector = g_initiator_transfer.bytes_per_sector;
  460. if (limit < PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE) limit = PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE;
  461. if (limit > PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE) limit = PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE;
  462. if (limit > len) limit = PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE;
  463. if (limit < bytesPerSector) limit = bytesPerSector;
  464. if (len > limit)
  465. {
  466. len = limit;
  467. }
  468. // Split read so that it doesn't wrap around buffer edge
  469. uint32_t bufsize = sizeof(scsiDev.data);
  470. uint32_t start = (g_initiator_transfer.bytes_scsi_done % bufsize);
  471. if (start + len > bufsize)
  472. len = bufsize - start;
  473. // Don't overwrite data that has not yet been written to SD card
  474. uint32_t sd_ready_cnt = g_initiator_transfer.bytes_sd + bytes_complete;
  475. if (g_initiator_transfer.bytes_scsi_done + len > sd_ready_cnt + bufsize)
  476. len = sd_ready_cnt + bufsize - g_initiator_transfer.bytes_scsi_done;
  477. if (sd_ready_cnt == g_initiator_transfer.bytes_sd_scheduled &&
  478. g_initiator_transfer.bytes_sd_scheduled + bytesPerSector <= g_initiator_transfer.bytes_scsi_done)
  479. {
  480. // Current SD transfer is complete, it is better we return now and offer a chance for the next
  481. // transfer to begin.
  482. return;
  483. }
  484. // Keep transfers a multiple of sector size.
  485. if (remain >= bytesPerSector && len % bytesPerSector != 0)
  486. {
  487. len -= len % bytesPerSector;
  488. }
  489. if (len == 0)
  490. return;
  491. // bluedbg("SCSI read ", (int)start, " + ", (int)len, ", sd ready cnt ", (int)sd_ready_cnt, " ", (int)bytes_complete, ", scsi done ", (int)g_initiator_transfer.bytes_scsi_done);
  492. if (scsiHostRead(&scsiDev.data[start], len) != len)
  493. {
  494. bluelog("Read failed at byte ", (int)g_initiator_transfer.bytes_scsi_done);
  495. g_initiator_transfer.all_ok = false;
  496. }
  497. g_initiator_transfer.bytes_scsi_done += len;
  498. }
  499. }
  500. static void scsiInitiatorWriteDataToSd(FsFile &file, bool use_callback)
  501. {
  502. // Figure out longest continuous block in buffer
  503. uint32_t bufsize = sizeof(scsiDev.data);
  504. uint32_t start = g_initiator_transfer.bytes_sd % bufsize;
  505. uint32_t len = g_initiator_transfer.bytes_scsi_done - g_initiator_transfer.bytes_sd;
  506. if (start + len > bufsize) len = bufsize - start;
  507. // Try to do writes in multiple of 512 bytes
  508. // This allows better performance for SD card access.
  509. if (len >= 512) len &= ~511;
  510. // Start writing to SD card and simultaneously reading more from SCSI bus
  511. uint8_t *buf = &scsiDev.data[start];
  512. // bluedbg("SD write ", (int)start, " + ", (int)len);
  513. if (use_callback)
  514. {
  515. bluescsiplatform_set_sd_callback(&initiatorReadSDCallback, buf);
  516. }
  517. g_initiator_transfer.bytes_sd_scheduled = g_initiator_transfer.bytes_sd + len;
  518. if (file.write(buf, len) != len)
  519. {
  520. bluelog("scsiInitiatorReadDataToFile: SD card write failed");
  521. g_initiator_transfer.all_ok = false;
  522. }
  523. bluescsiplatform_set_sd_callback(NULL, NULL);
  524. g_initiator_transfer.bytes_sd += len;
  525. }
  526. bool scsiInitiatorReadDataToFile(int target_id, uint32_t start_sector, uint32_t sectorcount, uint32_t sectorsize,
  527. FsFile &file)
  528. {
  529. int status = -1;
  530. if (start_sector < 0xFFFFFF && sectorcount <= 256)
  531. {
  532. // Use READ6 command for compatibility with old SCSI1 drives
  533. uint8_t command[6] = {0x08,
  534. (uint8_t)(start_sector >> 16),
  535. (uint8_t)(start_sector >> 8),
  536. (uint8_t)start_sector,
  537. (uint8_t)sectorcount,
  538. 0x00
  539. };
  540. // Start executing command, return in data phase
  541. status = scsiInitiatorRunCommand(target_id, command, sizeof(command), NULL, 0, NULL, 0, true);
  542. }
  543. else
  544. {
  545. // Use READ10 command for larger number of blocks
  546. uint8_t command[10] = {0x28, 0x00,
  547. (uint8_t)(start_sector >> 24), (uint8_t)(start_sector >> 16),
  548. (uint8_t)(start_sector >> 8), (uint8_t)start_sector,
  549. 0x00,
  550. (uint8_t)(sectorcount >> 8), (uint8_t)(sectorcount),
  551. 0x00
  552. };
  553. // Start executing command, return in data phase
  554. status = scsiInitiatorRunCommand(target_id, command, sizeof(command), NULL, 0, NULL, 0, true);
  555. }
  556. if (status != 0)
  557. {
  558. uint8_t sense_key;
  559. scsiRequestSense(target_id, &sense_key);
  560. bluelog("scsiInitiatorReadDataToFile: READ failed: ", status, " sense key ", sense_key);
  561. scsiHostPhyRelease();
  562. return false;
  563. }
  564. SCSI_PHASE phase;
  565. g_initiator_transfer.bytes_scsi = sectorcount * sectorsize;
  566. g_initiator_transfer.bytes_per_sector = sectorsize;
  567. g_initiator_transfer.bytes_sd = 0;
  568. g_initiator_transfer.bytes_sd_scheduled = 0;
  569. g_initiator_transfer.bytes_scsi_done = 0;
  570. g_initiator_transfer.all_ok = true;
  571. while (true)
  572. {
  573. phase = (SCSI_PHASE)scsiHostPhyGetPhase();
  574. if (phase != DATA_IN && phase != BUS_BUSY)
  575. {
  576. break;
  577. }
  578. // Read next block from SCSI bus if buffer empty
  579. if (g_initiator_transfer.bytes_sd == g_initiator_transfer.bytes_scsi_done)
  580. {
  581. initiatorReadSDCallback(0);
  582. }
  583. else
  584. {
  585. // Write data to SD card and simultaneously read more from SCSI
  586. scsiInitiatorUpdateLed();
  587. scsiInitiatorWriteDataToSd(file, true);
  588. }
  589. }
  590. // Write any remaining buffered data
  591. while (g_initiator_transfer.bytes_sd < g_initiator_transfer.bytes_scsi_done)
  592. {
  593. scsiInitiatorWriteDataToSd(file, false);
  594. }
  595. if (g_initiator_transfer.bytes_sd != g_initiator_transfer.bytes_scsi)
  596. {
  597. bluelog("SCSI read from sector ", (int)start_sector, " was incomplete: expected ",
  598. (int)g_initiator_transfer.bytes_scsi, " got ", (int)g_initiator_transfer.bytes_sd, " bytes");
  599. g_initiator_transfer.all_ok = false;
  600. }
  601. while ((phase = (SCSI_PHASE)scsiHostPhyGetPhase()) != BUS_FREE)
  602. {
  603. if (phase == MESSAGE_IN)
  604. {
  605. uint8_t dummy = 0;
  606. scsiHostRead(&dummy, 1);
  607. }
  608. else if (phase == MESSAGE_OUT)
  609. {
  610. uint8_t identify_msg = 0x80;
  611. scsiHostWrite(&identify_msg, 1);
  612. }
  613. else if (phase == STATUS)
  614. {
  615. uint8_t tmp = 0;
  616. scsiHostRead(&tmp, 1);
  617. status = tmp;
  618. bluedbg("------ STATUS: ", tmp);
  619. }
  620. }
  621. scsiHostPhyRelease();
  622. return status == 0 && g_initiator_transfer.all_ok;
  623. }
  624. #endif