AzulSCSI.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /*
  2. * AzulSCSI
  3. * Copyright (c) 2022 Rabbit Hole Computing
  4. *
  5. * This project is based on BlueSCSI:
  6. *
  7. * BlueSCSI
  8. * Copyright (c) 2021 Eric Helgeson, Androda
  9. *
  10. * This file is free software: you may copy, redistribute and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation, either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * This file is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see https://github.com/erichelgeson/bluescsi.
  22. *
  23. * This file incorporates work covered by the following copyright and
  24. * permission notice:
  25. *
  26. * Copyright (c) 2019 komatsu
  27. *
  28. * Permission to use, copy, modify, and/or distribute this software
  29. * for any purpose with or without fee is hereby granted, provided
  30. * that the above copyright notice and this permission notice appear
  31. * in all copies.
  32. *
  33. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  34. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  35. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  36. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  37. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  38. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  39. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  40. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  41. */
  42. #include <SdFat.h>
  43. #include <minIni.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46. #include "AzulSCSI_config.h"
  47. #include "AzulSCSI_platform.h"
  48. #include "AzulSCSI_log.h"
  49. SdFs SD;
  50. FsFile g_logfile;
  51. /***********************************/
  52. /* Error reporting by blinking led */
  53. /***********************************/
  54. #define BLINK_ERROR_NO_IMAGES 3
  55. #define BLINK_ERROR_NO_SD_CARD 5
  56. void blinkStatus(int count)
  57. {
  58. for (int i = 0; i < count; i++)
  59. {
  60. LED_ON();
  61. delay(250);
  62. LED_OFF();
  63. delay(250);
  64. }
  65. }
  66. /**************/
  67. /* Log saving */
  68. /**************/
  69. void save_logfile(bool always = false)
  70. {
  71. static uint32_t prev_log_pos = 0;
  72. static uint32_t prev_log_len = 0;
  73. static uint32_t prev_log_save = 0;
  74. uint32_t loglen = azlog_get_buffer_len();
  75. if (loglen != prev_log_len)
  76. {
  77. // When debug is off, save log at most every LOG_SAVE_INTERVAL_MS
  78. // When debug is on, save after every SCSI command.
  79. if (always || g_azlog_debug || (LOG_SAVE_INTERVAL_MS > 0 && (uint32_t)(millis() - prev_log_save) > LOG_SAVE_INTERVAL_MS))
  80. {
  81. g_logfile.write(azlog_get_buffer(&prev_log_pos));
  82. g_logfile.flush();
  83. prev_log_len = loglen;
  84. prev_log_save = millis();
  85. }
  86. }
  87. }
  88. void init_logfile()
  89. {
  90. static bool first_open_after_boot = true;
  91. bool truncate = first_open_after_boot;
  92. int flags = O_WRONLY | O_CREAT | (truncate ? O_TRUNC : O_APPEND);
  93. g_logfile = SD.open(LOGFILE, flags);
  94. save_logfile(true);
  95. first_open_after_boot = false;
  96. }
  97. /*********************************/
  98. /* Global state for SCSI code */
  99. /*********************************/
  100. volatile bool g_busreset = false;
  101. uint8_t g_sensekey = 0; // Error information
  102. uint8_t g_sense_asc = 0; // Additional error code
  103. uint8_t g_sense_ascq = 0; // Additional error code qualifier
  104. uint8_t g_scsi_id_mask; // Mask list of responding SCSI IDs
  105. uint8_t g_scsi_id; // Currently responding SCSI-ID
  106. uint8_t g_scsi_lun; // Logical unit number currently responding
  107. uint8_t g_scsi_sts; // Status byte
  108. uint8_t g_scsi_buffer[READBUFFER_SIZE] __attribute__((aligned(4)));
  109. /*********************************/
  110. /* SCSI Drive Vendor information */
  111. /*********************************/
  112. // Quirks for specific SCSI hosts
  113. enum scsi_quirks_t {
  114. SCSI_QUIRKS_STANDARD = 0,
  115. SCSI_QUIRKS_SHARP = 1,
  116. SCSI_QUIRKS_NEC_PC98 = 2
  117. } g_scsi_quirks;
  118. struct {
  119. uint32_t ARBITRATION_DELAY_US;
  120. uint32_t SELECTION_DELAY_US;
  121. uint32_t COMMAND_DELAY_US;
  122. uint32_t DATA_DELAY_US;
  123. uint32_t STATUS_DELAY_US;
  124. uint32_t MESSAGE_DELAY_US;
  125. uint32_t REQ_TYPE_SETUP_NS;
  126. } g_scsi_timing;
  127. uint8_t SCSI_INFO_BUF[36] = {
  128. 0x00, //device type
  129. 0x00, //RMB = 0
  130. 0x01, //ISO, ECMA, ANSI version
  131. 0x01, //Response data format
  132. 35 - 4, //Additional data length
  133. 0, 0, //Reserve
  134. 0x00, //Support function
  135. 'Q', 'U', 'A', 'N', 'T', 'U', 'M', ' ', // vendor 8
  136. 'F', 'I', 'R', 'E', 'B', 'A', 'L', 'L', '1', ' ', ' ',' ', ' ', ' ', ' ', ' ', // product 16
  137. '1', '.', '0', ' ' // version 4
  138. };
  139. void readSCSIDeviceConfig()
  140. {
  141. char tmp[32];
  142. g_scsi_quirks = (scsi_quirks_t)ini_getl("SCSI", "Quirks", SCSI_QUIRKS_STANDARD, CONFIGFILE);
  143. const char *default_vendor = DEFAULT_VENDOR;
  144. const char *default_product = DEFAULT_PRODUCT;
  145. const char *default_version = DEFAULT_VERSION;
  146. if (g_scsi_quirks == SCSI_QUIRKS_NEC_PC98)
  147. {
  148. default_vendor = "NECITSU ";
  149. default_product = "ArdSCSino ";
  150. default_version = "0010";
  151. }
  152. memset(tmp, 0, sizeof(tmp));
  153. ini_gets("SCSI", "Vendor", default_vendor, tmp, sizeof(tmp), CONFIGFILE);
  154. memcpy(&(SCSI_INFO_BUF[8]), tmp, 8);
  155. memset(tmp, 0, sizeof(tmp));
  156. ini_gets("SCSI", "Product", default_product, tmp, sizeof(tmp), CONFIGFILE);
  157. memcpy(&(SCSI_INFO_BUF[16]), tmp, 16);
  158. memset(tmp, 0, sizeof(tmp));
  159. ini_gets("SCSI", "Version", default_version, tmp, sizeof(tmp), CONFIGFILE);
  160. memcpy(&(SCSI_INFO_BUF[32]), tmp, 4);
  161. if (ini_getbool("SCSI", "Debug", 0, CONFIGFILE))
  162. {
  163. g_azlog_debug = true;
  164. }
  165. g_scsi_timing.ARBITRATION_DELAY_US = ini_getl("SCSI", "ARBITRATION_DELAY_US", DEFAULT_SCSI_DELAY_US, CONFIGFILE);
  166. g_scsi_timing.SELECTION_DELAY_US = ini_getl("SCSI", "SELECTION_DELAY_US", DEFAULT_SCSI_DELAY_US, CONFIGFILE);
  167. g_scsi_timing.COMMAND_DELAY_US = ini_getl("SCSI", "COMMAND_DELAY_US", DEFAULT_SCSI_DELAY_US, CONFIGFILE);
  168. g_scsi_timing.DATA_DELAY_US = ini_getl("SCSI", "DATA_DELAY_US", DEFAULT_SCSI_DELAY_US, CONFIGFILE);
  169. g_scsi_timing.STATUS_DELAY_US = ini_getl("SCSI", "STATUS_DELAY_US", DEFAULT_SCSI_DELAY_US, CONFIGFILE);
  170. g_scsi_timing.MESSAGE_DELAY_US = ini_getl("SCSI", "MESSAGE_DELAY_US", DEFAULT_SCSI_DELAY_US, CONFIGFILE);
  171. g_scsi_timing.REQ_TYPE_SETUP_NS = ini_getl("SCSI", "REQ_TYPE_SETUP_NS", DEFAULT_REQ_TYPE_SETUP_NS, CONFIGFILE);
  172. }
  173. /*********************************/
  174. /* Harddisk image file handling */
  175. /*********************************/
  176. // Information about active HDD image
  177. typedef struct hddimg_struct
  178. {
  179. FsFile m_file; // File object
  180. uint64_t m_fileSize; // File size
  181. size_t m_blocksize; // SCSI BLOCK size
  182. } HDDIMG;
  183. HDDIMG g_hddimg[NUM_SCSIID][NUM_SCSILUN]; // Allocate storage for all images
  184. static HDDIMG *g_currentimg = NULL; // Pointer to active image based on LUN/ID
  185. bool hddimageOpen(HDDIMG *h,const char *image_name,int id,int lun,int blocksize)
  186. {
  187. h->m_fileSize = 0;
  188. h->m_blocksize = blocksize;
  189. h->m_file = SD.open(image_name, O_RDWR);
  190. if(h->m_file.isOpen())
  191. {
  192. h->m_fileSize = h->m_file.size();
  193. azlog("Opened image file ", image_name, " fileSize: ", (int)(h->m_fileSize / 1024), " kB");
  194. if (h->m_file.contiguousRange(NULL, NULL))
  195. {
  196. azlog("Image file is contiguous.");
  197. }
  198. else
  199. {
  200. azlog("WARNING: file ", image_name, " is not contiguous. This will increase read latency.");
  201. }
  202. if(h->m_fileSize > 0)
  203. {
  204. return true; // File opened
  205. }
  206. else
  207. {
  208. h->m_file.close();
  209. h->m_fileSize = h->m_blocksize = 0; // no file
  210. azlog("Error: image is empty");
  211. }
  212. }
  213. return false;
  214. }
  215. // Iterate over the root path in the SD card looking for candidate image files.
  216. void findHDDImages()
  217. {
  218. g_scsi_id_mask = 0x00;
  219. SdFile root;
  220. root.open("/");
  221. SdFile file;
  222. bool imageReady;
  223. int usedDefaultId = 0;
  224. while (1) {
  225. if (!file.openNext(&root, O_READ)) break;
  226. char name[MAX_FILE_PATH+1];
  227. if(!file.isDir()) {
  228. file.getName(name, MAX_FILE_PATH+1);
  229. file.close();
  230. if (tolower(name[0]) == 'h' && tolower(name[1]) == 'd') {
  231. // Defaults for Hard Disks
  232. int id = 1; // 0 and 3 are common in Macs for physical HD and CD, so avoid them.
  233. int lun = 0;
  234. int blk = 512;
  235. // Positionally read in and coerase the chars to integers.
  236. // We only require the minimum and read in the next if provided.
  237. int file_name_length = strlen(name);
  238. if(file_name_length > 2) { // HD[N]
  239. int tmp_id = name[HDIMG_ID_POS] - '0';
  240. if(tmp_id > -1 && tmp_id < 8)
  241. {
  242. id = tmp_id; // If valid id, set it, else use default
  243. }
  244. else
  245. {
  246. id = usedDefaultId++;
  247. }
  248. }
  249. if(file_name_length > 3) { // HD0[N]
  250. int tmp_lun = name[HDIMG_LUN_POS] - '0';
  251. if(tmp_lun > -1 && tmp_lun < 2) {
  252. lun = tmp_lun; // If valid id, set it, else use default
  253. }
  254. }
  255. int blk1 = 0, blk2 = 0, blk3 = 0, blk4 = 0;
  256. if(file_name_length > 8) { // HD00_[111]
  257. blk1 = name[HDIMG_BLK_POS] - '0';
  258. blk2 = name[HDIMG_BLK_POS+1] - '0';
  259. blk3 = name[HDIMG_BLK_POS+2] - '0';
  260. if(file_name_length > 9) // HD00_NNN[1]
  261. blk4 = name[HDIMG_BLK_POS+3] - '0';
  262. }
  263. if(blk1 == 2 && blk2 == 5 && blk3 == 6) {
  264. blk = 256;
  265. } else if(blk1 == 1 && blk2 == 0 && blk3 == 2 && blk4 == 4) {
  266. blk = 1024;
  267. } else if(blk1 == 2 && blk2 == 0 && blk3 == 4 && blk4 == 8) {
  268. blk = 2048;
  269. }
  270. if(id < NUM_SCSIID && lun < NUM_SCSILUN) {
  271. HDDIMG *h = &g_hddimg[id][lun];
  272. azlog("Trying to open ", name, " for id:", id, " lun:", lun);
  273. imageReady = hddimageOpen(h,name,id,lun,blk);
  274. if(imageReady) { // Marked as a responsive ID
  275. g_scsi_id_mask |= 1<<id;
  276. }
  277. } else {
  278. azlog("Invalid lun or id for image ", name);
  279. }
  280. } else {
  281. azlog("Skipping file ", name);
  282. }
  283. }
  284. }
  285. if(usedDefaultId > 0) {
  286. azlog("Some images did not specify a SCSI ID. Last file will be used at ID ", usedDefaultId);
  287. }
  288. root.close();
  289. // Error if there are 0 image files
  290. if(g_scsi_id_mask==0) {
  291. azlog("ERROR: No valid images found!");
  292. blinkStatus(BLINK_ERROR_NO_IMAGES);
  293. }
  294. // Print SCSI drive map
  295. azlog("SCSI drive map:");
  296. azlog_raw("ID");
  297. for (int lun = 0; lun < NUM_SCSILUN; lun++)
  298. {
  299. azlog_raw(":LUN", lun);
  300. }
  301. azlog_raw(":\n");
  302. for (int id = 0; id < NUM_SCSIID; id++)
  303. {
  304. azlog_raw(" ", id);
  305. for (int lun = 0; lun < NUM_SCSILUN; lun++)
  306. {
  307. HDDIMG *h = &g_hddimg[id][lun];
  308. if (h->m_file)
  309. {
  310. azlog_raw((h->m_blocksize<1000) ? ": " : ":");
  311. azlog_raw((int)h->m_blocksize);
  312. }
  313. else
  314. {
  315. azlog_raw(":----");
  316. }
  317. }
  318. azlog_raw(":\n");
  319. }
  320. }
  321. /*********************************/
  322. /* SCSI bus communication */
  323. /*********************************/
  324. #define active 1
  325. #define inactive 0
  326. #define SCSI_WAIT_ACTIVE(pin) \
  327. if (!SCSI_IN(pin)) { \
  328. if (!SCSI_IN(pin)) { \
  329. while(!SCSI_IN(pin) && !g_busreset); \
  330. } \
  331. }
  332. #define SCSI_WAIT_INACTIVE(pin) \
  333. if (SCSI_IN(pin)) { \
  334. if (SCSI_IN(pin)) { \
  335. while(SCSI_IN(pin) && !g_busreset); \
  336. } \
  337. }
  338. /*
  339. * Read by handshake.
  340. */
  341. inline uint8_t readHandshake(void)
  342. {
  343. SCSI_OUT(REQ,active);
  344. SCSI_WAIT_ACTIVE(ACK);
  345. delay_100ns(); // ACK.Fall to DB output delay 100ns(MAX) (DTC-510B)
  346. uint8_t r = SCSI_IN_DATA();
  347. SCSI_OUT(REQ, inactive);
  348. SCSI_WAIT_INACTIVE(ACK);
  349. return r;
  350. }
  351. /*
  352. * Write with a handshake.
  353. */
  354. inline void writeHandshake(uint8_t d)
  355. {
  356. SCSI_OUT_DATA(d);
  357. delay_100ns(); // DB hold time before REQ (DTC-510B)
  358. SCSI_OUT(REQ, active);
  359. SCSI_WAIT_ACTIVE(ACK);
  360. SCSI_RELEASE_DATA_REQ(); // Release data and REQ
  361. SCSI_WAIT_INACTIVE(ACK);
  362. }
  363. /*
  364. * Data in phase.
  365. * Send len uint8_ts of data array p.
  366. */
  367. void writeDataPhase(int len, const uint8_t* p)
  368. {
  369. SCSI_OUT(MSG,inactive);
  370. SCSI_OUT(CD ,inactive);
  371. SCSI_OUT(IO , active);
  372. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  373. for (int i = 0; i < len; i++) {
  374. if (g_busreset) break;
  375. writeHandshake(p[i]);
  376. }
  377. }
  378. /*
  379. * Data in phase.
  380. * Send len blocks while reading from SD card.
  381. */
  382. void writeDataPhase_FromSD(uint32_t adds, uint32_t len)
  383. {
  384. uint32_t pos = adds * g_currentimg->m_blocksize;
  385. g_currentimg->m_file.seek(pos);
  386. SCSI_OUT(MSG,inactive);
  387. SCSI_OUT(CD ,inactive);
  388. SCSI_OUT(IO , active);
  389. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  390. while (len > 0)
  391. {
  392. uint32_t max_transfer_len = READBUFFER_SIZE / g_currentimg->m_blocksize;
  393. uint32_t transfer_len = (len < max_transfer_len) ? len : max_transfer_len;
  394. len -= transfer_len;
  395. transfer_len *= g_currentimg->m_blocksize;
  396. #if STREAM_SD_TRANSFERS
  397. azplatform_prepare_stream(g_scsi_buffer);
  398. g_currentimg->m_file.read(g_scsi_buffer, transfer_len);
  399. if (g_busreset) return;
  400. size_t status = azplatform_finish_stream();
  401. if (status == 0)
  402. {
  403. // Streaming did not happen, send data now
  404. azdbg("Streaming from SD failed, using fallback");
  405. writeDataPhase(transfer_len, g_scsi_buffer);
  406. }
  407. else if (status != transfer_len)
  408. {
  409. azlog("Streaming failed halfway: ", (int)status, "/", (int)transfer_len, " bytes, data may be corrupt, aborting!");
  410. g_scsi_sts |= 2;
  411. return;
  412. }
  413. #else
  414. g_currentimg->m_file.read(g_scsi_buffer, transfer_len);
  415. writeDataPhase(transfer_len, g_scsi_buffer);
  416. #endif
  417. }
  418. }
  419. /*
  420. * Data out phase.
  421. * len block read
  422. */
  423. void readDataPhase(int len, uint8_t* p)
  424. {
  425. SCSI_OUT(MSG,inactive);
  426. SCSI_OUT(CD ,inactive);
  427. SCSI_OUT(IO ,inactive);
  428. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  429. for(int i = 0; i < len; i++)
  430. {
  431. if (g_busreset) break;
  432. p[i] = readHandshake();
  433. }
  434. }
  435. /*
  436. * Data out phase.
  437. * Write to SD card while reading len block.
  438. */
  439. void readDataPhase_ToSD(uint32_t adds, uint32_t len)
  440. {
  441. uint32_t pos = adds * g_currentimg->m_blocksize;
  442. g_currentimg->m_file.seek(pos);
  443. SCSI_OUT(MSG,inactive);
  444. SCSI_OUT(CD ,inactive);
  445. SCSI_OUT(IO ,inactive);
  446. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  447. while (len > 0)
  448. {
  449. uint32_t max_transfer_len = READBUFFER_SIZE / g_currentimg->m_blocksize;
  450. uint32_t transfer_len = (len < max_transfer_len) ? len : max_transfer_len;
  451. len -= transfer_len;
  452. transfer_len *= g_currentimg->m_blocksize;
  453. #if STREAM_SD_TRANSFERS
  454. azplatform_prepare_stream(g_scsi_buffer);
  455. g_currentimg->m_file.write(g_scsi_buffer, transfer_len);
  456. pos += transfer_len;
  457. if (g_busreset) return;
  458. size_t status = azplatform_finish_stream();
  459. if (status == 0)
  460. {
  461. // Streaming did not happen, rewrite
  462. azdbg("Streaming to SD failed, using fallback");
  463. g_currentimg->m_file.seek(pos - transfer_len);
  464. readDataPhase(transfer_len, g_scsi_buffer);
  465. g_currentimg->m_file.write(g_scsi_buffer, transfer_len);
  466. }
  467. else if (status != transfer_len)
  468. {
  469. azlog("Streaming to SD failed halfway, data may be corrupt, aborting!");
  470. g_scsi_sts |= 2;
  471. return;
  472. }
  473. #else
  474. readDataPhase(transfer_len, g_scsi_buffer);
  475. g_currentimg->m_file.write(g_scsi_buffer, transfer_len);
  476. #endif
  477. }
  478. g_currentimg->m_file.flush();
  479. }
  480. /*********************************/
  481. /* SCSI commands */
  482. /*********************************/
  483. // https://www.staff.uni-mainz.de/tacke/scsi/SCSI2-08.html#8.2.16
  484. uint8_t onTestUnitReady()
  485. {
  486. if(!g_currentimg)
  487. {
  488. g_sensekey = 2; // Not ready
  489. g_sense_asc = 0x3A; // Medium not present
  490. g_sense_ascq = 0;
  491. return 0x02; // Check condition
  492. }
  493. return 0x00;
  494. }
  495. // INQUIRY command processing.
  496. uint8_t onInquiryCommand(uint8_t len)
  497. {
  498. uint8_t response[sizeof(SCSI_INFO_BUF)];
  499. int responselen = sizeof(SCSI_INFO_BUF);
  500. memcpy(response, SCSI_INFO_BUF, responselen);
  501. // Select device type based on LUN
  502. if (g_scsi_lun >= NUM_SCSILUN)
  503. {
  504. response[0] = 0x7F; // Unsupported LUN
  505. }
  506. else if (!g_currentimg)
  507. {
  508. response[0] = 0x3F; // Unconnected LUN
  509. }
  510. writeDataPhase(len < responselen ? len : responselen, response);
  511. return 0x00;
  512. }
  513. // REQUEST SENSE command processing.
  514. // Refer to https://www.staff.uni-mainz.de/tacke/scsi/SCSI2-08.html#8.2.14
  515. void onRequestSenseCommand(uint8_t len)
  516. {
  517. uint8_t buf[18] = {
  518. 0xF0, //CheckCondition
  519. 0, //Segment number
  520. g_sensekey, //Sense key
  521. 0, 0, 0, 0, //information
  522. 17 - 7 , //Additional data length
  523. 0, 0, 0, 0, // Command specific
  524. g_sense_asc,
  525. g_sense_ascq,
  526. 0,
  527. 0, 0, 0
  528. };
  529. g_sensekey = 0;
  530. g_sense_asc = 0;
  531. g_sense_ascq = 0;
  532. writeDataPhase(len < 18 ? len : 18, buf);
  533. }
  534. // READ CAPACITY command processing.
  535. uint8_t onReadCapacityCommand(uint8_t pmi)
  536. {
  537. if(!g_currentimg) return 0x02; // Image file absent
  538. uint32_t bl = g_currentimg->m_blocksize;
  539. uint32_t bc = g_currentimg->m_fileSize / bl;
  540. uint8_t buf[8] = {
  541. (uint8_t)(bc >> 24), (uint8_t)(bc >> 16), (uint8_t)(bc >> 8), (uint8_t)(bc),
  542. (uint8_t)(bl >> 24), (uint8_t)(bl >> 16), (uint8_t)(bl >> 8), (uint8_t)(bl)
  543. };
  544. writeDataPhase(8, buf);
  545. return 0x00;
  546. }
  547. // READ6 / 10 Command processing.
  548. uint8_t onReadCommand(uint32_t adds, uint32_t len)
  549. {
  550. azdbg("Read at ", adds, " ", (int)len, " blocks");
  551. if(!g_currentimg) return 0x02; // Image file absent
  552. LED_ON();
  553. writeDataPhase_FromSD(adds, len);
  554. LED_OFF();
  555. return 0x00;
  556. }
  557. // WRITE6 / 10 Command processing.
  558. uint8_t onWriteCommand(uint32_t adds, uint32_t len)
  559. {
  560. azdbg("Write at ", adds, " ", (int)len, " blocks");
  561. if(!g_currentimg) return 0x02; // Image file absent
  562. LED_ON();
  563. readDataPhase_ToSD(adds, len);
  564. LED_OFF();
  565. return 0;
  566. }
  567. // MODE SENSE command processing for NEC_PC98
  568. uint8_t onModeSenseCommand_NEC_PC98(uint8_t dbd, int cmd2, uint32_t len)
  569. {
  570. if(!g_currentimg) return 0x02; // Image file absent
  571. uint8_t buf[512];
  572. int pageCode = cmd2 & 0x3F;
  573. // Assuming sector size 512, number of sectors 25, number of heads 8 as default settings
  574. int size = g_currentimg->m_fileSize;
  575. int cylinders = (int)(size >> 9);
  576. cylinders >>= 3;
  577. cylinders /= 25;
  578. int sectorsize = 512;
  579. int sectors = 25;
  580. int heads = 8;
  581. // Sector size
  582. int disksize = 0;
  583. for(disksize = 16; disksize > 0; --(disksize)) {
  584. if ((1 << disksize) == sectorsize)
  585. break;
  586. }
  587. // Number of blocks
  588. // uint32_t diskblocks = (uint32_t)(size >> disksize);
  589. memset(buf, 0, sizeof(buf));
  590. uint32_t a = 4;
  591. if(dbd == 0) {
  592. uint32_t bl = g_currentimg->m_blocksize;
  593. uint32_t bc = g_currentimg->m_fileSize / bl;
  594. uint8_t c[8] = {
  595. 0,// Density code
  596. (uint8_t)(bc >> 16), (uint8_t)(bc >> 8), (uint8_t)bc,
  597. 0, //Reserve
  598. (uint8_t)(bl >> 16), (uint8_t)(bl >> 8), (uint8_t)(bl)
  599. };
  600. memcpy(&buf[4], c, 8);
  601. a += 8;
  602. buf[3] = 0x08;
  603. }
  604. switch(pageCode) {
  605. case 0x3F:
  606. {
  607. buf[a + 0] = 0x01;
  608. buf[a + 1] = 0x06;
  609. a += 8;
  610. }
  611. case 0x03: // drive parameters
  612. {
  613. buf[a + 0] = 0x80 | 0x03; // Page code
  614. buf[a + 1] = 0x16; // Page length
  615. buf[a + 2] = (uint8_t)(heads >> 8);// number of sectors / track
  616. buf[a + 3] = (uint8_t)(heads);// number of sectors / track
  617. buf[a + 10] = (uint8_t)(sectors >> 8);// number of sectors / track
  618. buf[a + 11] = (uint8_t)(sectors);// number of sectors / track
  619. int size = 1 << disksize;
  620. buf[a + 12] = (uint8_t)(size >> 8);// number of sectors / track
  621. buf[a + 13] = (uint8_t)(size);// number of sectors / track
  622. a += 24;
  623. if(pageCode != 0x3F) {
  624. break;
  625. }
  626. }
  627. case 0x04: // drive parameters
  628. {
  629. azdbg("onModeSenseCommand_NEC_PC98: AddDrive");
  630. buf[a + 0] = 0x04; // Page code
  631. buf[a + 1] = 0x12; // Page length
  632. buf[a + 2] = (cylinders >> 16);// Cylinder length
  633. buf[a + 3] = (cylinders >> 8);
  634. buf[a + 4] = cylinders;
  635. buf[a + 5] = heads; // Number of heads
  636. a += 20;
  637. if(pageCode != 0x3F) {
  638. break;
  639. }
  640. }
  641. default:
  642. break;
  643. }
  644. buf[0] = a - 1;
  645. writeDataPhase(len < a ? len : a, buf);
  646. return 0x00;
  647. }
  648. uint8_t onModeSenseCommand(uint8_t dbd, int cmd2, uint32_t len)
  649. {
  650. if (g_scsi_quirks == SCSI_QUIRKS_NEC_PC98)
  651. {
  652. return onModeSenseCommand_NEC_PC98(dbd, cmd2, len);
  653. }
  654. if(!g_currentimg) return 0x02; // No image file
  655. uint8_t buf[512];
  656. memset(buf, 0, sizeof(buf));
  657. int pageCode = cmd2 & 0x3F;
  658. uint32_t a = 4;
  659. if(dbd == 0) {
  660. uint32_t bl = g_currentimg->m_blocksize;
  661. uint32_t bc = g_currentimg->m_fileSize / bl;
  662. uint8_t c[8] = {
  663. 0,//Density code
  664. (uint8_t)(bc >> 16), (uint8_t)(bc >> 8), (uint8_t)bc,
  665. 0, //Reserve
  666. (uint8_t)(bl >> 16), (uint8_t)(bl >> 8), (uint8_t)bl
  667. };
  668. memcpy(&buf[4], c, 8);
  669. a += 8;
  670. buf[3] = 0x08;
  671. }
  672. switch(pageCode) {
  673. case 0x3F:
  674. case 0x03: //Drive parameters
  675. buf[a + 0] = 0x03; //Page code
  676. buf[a + 1] = 0x16; // Page length
  677. buf[a + 11] = 0x3F;//Number of sectors / track
  678. a += 24;
  679. if(pageCode != 0x3F) {
  680. break;
  681. }
  682. case 0x04: //Drive parameters
  683. {
  684. uint32_t bc = g_currentimg->m_fileSize / g_currentimg->m_file;
  685. buf[a + 0] = 0x04; //Page code
  686. buf[a + 1] = 0x16; // Page length
  687. buf[a + 2] = bc >> 16;// Cylinder length
  688. buf[a + 3] = bc >> 8;
  689. buf[a + 4] = bc;
  690. buf[a + 5] = 1; //Number of heads
  691. a += 24;
  692. }
  693. if(pageCode != 0x3F) {
  694. break;
  695. }
  696. default:
  697. break;
  698. }
  699. buf[0] = a - 1;
  700. writeDataPhase(len < a ? len : a, buf);
  701. return 0x00;
  702. }
  703. /*
  704. * dtc510b_setDriveparameter for SCSI_QUIRKS_SHARP
  705. */
  706. typedef struct __attribute__((packed)) dtc500_cmd_c2_param_struct
  707. {
  708. uint8_t StepPlusWidth; // Default is 13.6usec (11)
  709. uint8_t StepPeriod; // Default is 3 msec.(60)
  710. uint8_t StepMode; // Default is Bufferd (0)
  711. uint8_t MaximumHeadAdress; // Default is 4 heads (3)
  712. uint8_t HighCylinderAddressuint8_t; // Default set to 0 (0)
  713. uint8_t LowCylinderAddressuint8_t; // Default is 153 cylinders (152)
  714. uint8_t ReduceWrietCurrent; // Default is above Cylinder 128 (127)
  715. uint8_t DriveType_SeekCompleteOption;// (0)
  716. uint8_t Reserved8; // (0)
  717. uint8_t Reserved9; // (0)
  718. } DTC510_CMD_C2_PARAM;
  719. static uint8_t dtc510b_setDriveparameter(void)
  720. {
  721. DTC510_CMD_C2_PARAM DriveParameter;
  722. uint16_t maxCylinder;
  723. uint16_t numLAD;
  724. // int StepPeriodMsec;
  725. // receive paramter
  726. writeDataPhase(sizeof(DriveParameter),(uint8_t *)(&DriveParameter));
  727. maxCylinder =
  728. (((uint16_t)DriveParameter.HighCylinderAddressuint8_t)<<8) |
  729. (DriveParameter.LowCylinderAddressuint8_t);
  730. numLAD = maxCylinder * (DriveParameter.MaximumHeadAdress+1);
  731. // StepPeriodMsec = DriveParameter.StepPeriod*50;
  732. azdbg(" StepPlusWidth : ",DriveParameter.StepPlusWidth);
  733. azdbg(" StepPeriod : ",DriveParameter.StepPeriod );
  734. azdbg(" StepMode : ",DriveParameter.StepMode );
  735. azdbg(" MaximumHeadAdress : ",DriveParameter.MaximumHeadAdress);
  736. azdbg(" CylinderAddress : ",maxCylinder);
  737. azdbg(" ReduceWriteCurrent : ",DriveParameter.ReduceWrietCurrent);
  738. azdbg(" DriveType/SeekCompleteOption : ",DriveParameter.DriveType_SeekCompleteOption);
  739. azdbg(" Maximum LAD : ",numLAD-1);
  740. return 0;
  741. }
  742. // Read the command, returns 0 on bus reset.
  743. int readSCSICommand(uint8_t cmd[12])
  744. {
  745. SCSI_OUT(MSG,inactive);
  746. SCSI_OUT(CD , active);
  747. SCSI_OUT(IO ,inactive);
  748. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  749. cmd[0] = readHandshake();
  750. if (g_busreset) return 0;
  751. static const int cmd_class_len[8]={6,10,10,6,6,12,6,6};
  752. int cmdlen = cmd_class_len[cmd[0] >> 5];
  753. for (int i = 1; i < cmdlen; i++)
  754. {
  755. cmd[i] = readHandshake();
  756. if (g_busreset) return 0;
  757. }
  758. azdbg("Got command (", cmdlen, " bytes): ", bytearray(cmd, cmdlen));
  759. return cmdlen;
  760. }
  761. // ATN message handling, returns false on abort/reset
  762. bool onATNMessage()
  763. {
  764. bool syncenable = false;
  765. int syncperiod = 50;
  766. int syncoffset = 0;
  767. SCSI_OUT(MSG, active);
  768. SCSI_OUT(CD , active);
  769. SCSI_OUT(IO ,inactive);
  770. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  771. uint8_t msg[256];
  772. memset(msg, 0x00, sizeof(msg));
  773. uint8_t response[64];
  774. size_t responselen = 0;
  775. memset(response, 0x00, sizeof(response));
  776. int msg_bytes = 0;
  777. while(SCSI_IN(ATN) && msg_bytes < (int)sizeof(msg)) {
  778. if (g_busreset)
  779. {
  780. return false;
  781. }
  782. msg[msg_bytes] = readHandshake();
  783. msg_bytes++;
  784. }
  785. if (msg_bytes == 0)
  786. {
  787. return false;
  788. }
  789. azdbg("Received MSG ", (int)msg_bytes, " bytes: ", bytearray(msg, msg_bytes));
  790. for (int i = 0; i < msg_bytes; i++)
  791. {
  792. // ABORT
  793. if (msg[i] == 0x06) {
  794. azdbg("MSG_ABORT");
  795. return false;
  796. }
  797. // BUS DEVICE RESET
  798. if (msg[i] == 0x0C) {
  799. azdbg("MSG_BUS_DEVICE_RESET");
  800. return false;
  801. }
  802. // IDENTIFY
  803. if (msg[i] >= 0x80) {
  804. azdbg("MSG_IDENTIFY");
  805. }
  806. // Extended message
  807. if (msg[i] == 0x01) {
  808. azdbg("MSG_EXTENDED");
  809. // Check only when synchronous transfer is possible
  810. if (!syncenable || msg[i + 2] != 0x01) {
  811. response[0] = 0x07;
  812. responselen = 1;
  813. break;
  814. }
  815. // Transfer period factor(50 x 4 = Limited to 200ns)
  816. syncperiod = msg[i + 3];
  817. if (syncperiod > 50) {
  818. syncperiod = 50;
  819. }
  820. // REQ/ACK offset(Limited to 16)
  821. syncoffset = msg[i + 4];
  822. if (syncoffset > 16) {
  823. syncoffset = 16;
  824. }
  825. // STDR response message generation
  826. response[0] = 0x01;
  827. response[1] = 0x03;
  828. response[2] = 0x01;
  829. response[3] = syncperiod;
  830. response[4] = syncoffset;
  831. responselen = 5;
  832. break;
  833. }
  834. }
  835. if (responselen > 0)
  836. {
  837. azdbg("Sending MSG response, ", (int)responselen, " bytes: ", bytearray(response, responselen));
  838. SCSI_OUT(MSG, active);
  839. SCSI_OUT(CD , active);
  840. SCSI_OUT(IO , active);
  841. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  842. for (int i = 0; i < responselen; i++)
  843. {
  844. writeHandshake(response[i]);
  845. }
  846. }
  847. return true;
  848. }
  849. /*********************************/
  850. /* Main SCSI handling loop */
  851. /*********************************/
  852. void scsi_loop()
  853. {
  854. SCSI_RELEASE_OUTPUTS();
  855. if (g_busreset)
  856. {
  857. g_busreset = false;
  858. }
  859. // Wait until RST = H, BSY = H, SEL = L
  860. uint32_t start = millis();
  861. while (SCSI_IN(BSY) || !SCSI_IN(SEL) || SCSI_IN(RST))
  862. {
  863. if ((uint32_t)(millis() - start) > 1000)
  864. {
  865. // Service main loop while waiting for request
  866. return;
  867. }
  868. }
  869. // BSY+ SEL-
  870. // If the ID to respond is not driven, wait for the next
  871. uint8_t scsiid = SCSI_IN_DATA() & g_scsi_id_mask;
  872. if (scsiid == 0) {
  873. return; // Not for us
  874. }
  875. g_busreset = false;
  876. delay_us(g_scsi_timing.ARBITRATION_DELAY_US);
  877. // Set BSY to-when selected
  878. SCSI_OUT(BSY, active);
  879. azdbg("------------ SCSI device selected");
  880. // Ask for a TARGET-ID to respond
  881. g_scsi_id = 0;
  882. for(int i = 7; i >= 0; i--)
  883. {
  884. if (scsiid & (1<<i))
  885. {
  886. g_scsi_id = i;
  887. break;
  888. }
  889. }
  890. // Wait until SEL becomes inactive
  891. while (SCSI_IN(SEL))
  892. {
  893. if (g_busreset)
  894. {
  895. SCSI_RELEASE_OUTPUTS();
  896. return;
  897. }
  898. }
  899. delay_us(g_scsi_timing.SELECTION_DELAY_US);
  900. if(SCSI_IN(ATN))
  901. {
  902. if (!onATNMessage())
  903. {
  904. // Abort/reset message
  905. SCSI_RELEASE_OUTPUTS();
  906. return;
  907. }
  908. }
  909. delay_us(g_scsi_timing.COMMAND_DELAY_US);
  910. uint8_t cmd[12];
  911. int cmdlen = readSCSICommand(cmd);
  912. delay_us(g_scsi_timing.DATA_DELAY_US);
  913. if (cmdlen == 0)
  914. {
  915. SCSI_RELEASE_OUTPUTS();
  916. return;
  917. }
  918. // LUN selection
  919. g_scsi_lun = cmd[1] >> 5;
  920. // HDD Image selection
  921. g_currentimg = (HDDIMG *)0; // None
  922. if( (g_scsi_lun <= NUM_SCSILUN) )
  923. {
  924. g_currentimg = &(g_hddimg[g_scsi_id][g_scsi_lun]); // There is an image
  925. if(!(g_currentimg->m_file.isOpen()))
  926. g_currentimg = (HDDIMG *)0; // Image absent
  927. }
  928. g_scsi_sts = 0;
  929. azdbg("CMD ", cmd[0], " (", cmdlen, " bytes): ", "ID", (int)g_scsi_id, ", LUN", (int)g_scsi_lun);
  930. switch(cmd[0]) {
  931. case 0x00:
  932. azdbg("[Test Unit Ready]");
  933. g_scsi_sts |= onTestUnitReady();
  934. break;
  935. case 0x01: azdbg("[Rezero Unit]"); break;
  936. case 0x03:
  937. azdbg("[RequestSense]");
  938. onRequestSenseCommand(cmd[4]);
  939. break;
  940. case 0x04: azdbg("[FormatUnit]"); break;
  941. case 0x06: azdbg("[FormatUnit]"); break;
  942. case 0x07: azdbg("[ReassignBlocks]"); break;
  943. case 0x08:
  944. azdbg("[Read6]");
  945. g_scsi_sts |= onReadCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
  946. break;
  947. case 0x0A:
  948. azdbg("[Write6]");
  949. g_scsi_sts |= onWriteCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
  950. break;
  951. case 0x0B: azdbg("[Seek6]"); break;
  952. case 0x12:
  953. azdbg("[Inquiry]");
  954. g_scsi_sts |= onInquiryCommand(cmd[4]);
  955. break;
  956. case 0x1A:
  957. azdbg("[ModeSense6]");
  958. g_scsi_sts |= onModeSenseCommand(cmd[1]&0x80, cmd[2], cmd[4]);
  959. break;
  960. case 0x1B: azdbg("[StartStopUnit]"); break;
  961. case 0x1E: azdbg("[PreAllowMed.Removal]"); break;
  962. case 0x25:
  963. azdbg("[ReadCapacity]");
  964. g_scsi_sts |= onReadCapacityCommand(cmd[8]);
  965. break;
  966. case 0x28:
  967. azdbg("[Read10]");
  968. g_scsi_sts |= onReadCommand(((uint32_t)cmd[2] << 24) | ((uint32_t)cmd[3] << 16) | ((uint32_t)cmd[4] << 8) | cmd[5], ((uint32_t)cmd[7] << 8) | cmd[8]);
  969. break;
  970. case 0x2A:
  971. azdbg("[Write10]");
  972. g_scsi_sts |= onWriteCommand(((uint32_t)cmd[2] << 24) | ((uint32_t)cmd[3] << 16) | ((uint32_t)cmd[4] << 8) | cmd[5], ((uint32_t)cmd[7] << 8) | cmd[8]);
  973. break;
  974. case 0x2B: azdbg("[Seek10]"); break;
  975. case 0x5A:
  976. azdbg("[ModeSense10]");
  977. onModeSenseCommand(cmd[1] & 0x80, cmd[2], ((uint32_t)cmd[7] << 8) | cmd[8]);
  978. break;
  979. case 0xc2:
  980. if (g_scsi_quirks == SCSI_QUIRKS_SHARP)
  981. {
  982. azdbg("[DTC510B setDriveParameter]");
  983. g_scsi_sts |= dtc510b_setDriveparameter();
  984. }
  985. else
  986. {
  987. g_scsi_sts |= 0x02;
  988. g_sensekey = 5;
  989. }
  990. break;
  991. default:
  992. azdbg("[Unknown CMD: ", cmd[0], "]");
  993. g_scsi_sts |= 0x02;
  994. g_sensekey = 5;
  995. break;
  996. }
  997. if (g_busreset) {
  998. SCSI_RELEASE_OUTPUTS();
  999. return;
  1000. }
  1001. delay_us(g_scsi_timing.STATUS_DELAY_US);
  1002. azdbg("Status: ", g_scsi_sts);
  1003. SCSI_OUT(MSG,inactive);
  1004. SCSI_OUT(CD , active);
  1005. SCSI_OUT(IO , active);
  1006. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  1007. writeHandshake(g_scsi_sts);
  1008. if(g_busreset) {
  1009. SCSI_RELEASE_OUTPUTS();
  1010. return;
  1011. }
  1012. delay_us(g_scsi_timing.MESSAGE_DELAY_US);
  1013. SCSI_OUT(MSG, active);
  1014. SCSI_OUT(CD , active);
  1015. SCSI_OUT(IO , active);
  1016. delay_ns(g_scsi_timing.REQ_TYPE_SETUP_NS);
  1017. writeHandshake(0);
  1018. save_logfile();
  1019. azdbg("------------ Command complete");
  1020. SCSI_RELEASE_OUTPUTS();
  1021. }
  1022. void onBusReset(void)
  1023. {
  1024. if (g_busreset)
  1025. {
  1026. // Previous reset is not yet handled
  1027. return;
  1028. }
  1029. int filterlen = 100;
  1030. if (g_scsi_quirks == SCSI_QUIRKS_SHARP)
  1031. {
  1032. // SASI I / F for X1 turbo has RST pulse write cycle +2 clock
  1033. // Active about 1.25 us
  1034. filterlen = 2;
  1035. }
  1036. while (filterlen > 0)
  1037. {
  1038. delay_100ns();
  1039. if (!SCSI_IN(RST)) return;
  1040. filterlen--;
  1041. }
  1042. SCSI_RELEASE_OUTPUTS();
  1043. azdbg("BUSRESET");
  1044. g_busreset = true;
  1045. }
  1046. int main(void)
  1047. {
  1048. azplatform_init();
  1049. azplatform_set_rst_callback(&onBusReset);
  1050. if(!SD.begin(SD_CONFIG))
  1051. {
  1052. azlog("SD card init failed, sdErrorCode: ", (int)SD.sdErrorCode(),
  1053. " sdErrorData: ", (int)SD.sdErrorData());
  1054. do
  1055. {
  1056. blinkStatus(BLINK_ERROR_NO_SD_CARD);
  1057. delay(1000);
  1058. } while (!SD.begin(SD_CONFIG));
  1059. azlog("SD card init succeeded after retry");
  1060. }
  1061. uint64_t size = (uint64_t)SD.vol()->clusterCount() * SD.vol()->bytesPerCluster();
  1062. azlog("SD card init succeeded, FAT", (int)SD.vol()->fatType(),
  1063. " volume size: ", (int)(size / 1024 / 1024), " MB");
  1064. readSCSIDeviceConfig();
  1065. findHDDImages();
  1066. azlog("Initialization complete!");
  1067. azlog("Platform: ", g_azplatform_name);
  1068. azlog("FW Version: ", g_azlog_firmwareversion);
  1069. init_logfile();
  1070. if (g_scsi_id_mask != 0)
  1071. {
  1072. // Ok, there is an image
  1073. blinkStatus(1);
  1074. }
  1075. while (1)
  1076. {
  1077. scsi_loop();
  1078. // Check SD card status for hotplug
  1079. uint32_t ocr;
  1080. if (!SD.card()->readOCR(&ocr))
  1081. {
  1082. if (!SD.card()->readOCR(&ocr))
  1083. {
  1084. azlog("SD card removed, trying to reinit");
  1085. do
  1086. {
  1087. blinkStatus(BLINK_ERROR_NO_SD_CARD);
  1088. delay(1000);
  1089. } while (!SD.begin(SD_CONFIG));
  1090. azlog("SD card reinit succeeded");
  1091. readSCSIDeviceConfig();
  1092. findHDDImages();
  1093. if (g_scsi_id_mask != 0)
  1094. {
  1095. blinkStatus(1);
  1096. }
  1097. }
  1098. }
  1099. }
  1100. }