BlueSCSI.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * BlueSCSI v2
  3. * Copyright (c) 2023 Eric Helgeson, Androda, and contributors.
  4. *
  5. * This project is based on ZuluSCSI, BlueSCSI v1, and SCSI2SD:
  6. *
  7. * ZuluSCSI
  8. * Copyright (c) 2022 Rabbit Hole Computing
  9. *
  10. * This project is based on BlueSCSI:
  11. *
  12. * BlueSCSI
  13. * Copyright (c) 2021 Eric Helgeson, Androda
  14. *
  15. * This file is free software: you may copy, redistribute and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation, either version 2 of the License, or (at your
  18. * option) any later version.
  19. *
  20. * This file is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see https://github.com/erichelgeson/bluescsi.
  27. *
  28. * This file incorporates work covered by the following copyright and
  29. * permission notice:
  30. *
  31. * Copyright (c) 2019 komatsu
  32. *
  33. * Permission to use, copy, modify, and/or distribute this software
  34. * for any purpose with or without fee is hereby granted, provided
  35. * that the above copyright notice and this permission notice appear
  36. * in all copies.
  37. *
  38. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  39. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  40. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  41. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  42. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  43. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  44. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  45. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  46. */
  47. #include <SdFat.h>
  48. #include <minIni.h>
  49. #include <minIni_cache.h>
  50. #include <string.h>
  51. #include <strings.h>
  52. #include <ctype.h>
  53. #include "BlueSCSI_config.h"
  54. #include "BlueSCSI_platform.h"
  55. #include "BlueSCSI_log.h"
  56. #include "BlueSCSI_log_trace.h"
  57. #include "BlueSCSI_disk.h"
  58. #include "BlueSCSI_initiator.h"
  59. #include "ROMDrive.h"
  60. SdFs SD;
  61. FsFile g_logfile;
  62. static bool g_romdrive_active;
  63. static bool g_sdcard_present;
  64. /************************************/
  65. /* Status reporting by blinking led */
  66. /************************************/
  67. #define BLINK_STATUS_OK 1
  68. #define BLINK_ERROR_NO_IMAGES 3
  69. #define BLINK_ERROR_NO_SD_CARD 5
  70. void blinkStatus(int count)
  71. {
  72. for (int i = 0; i < count; i++)
  73. {
  74. LED_ON();
  75. delay(250);
  76. LED_OFF();
  77. delay(250);
  78. }
  79. }
  80. extern "C" void s2s_ledOn()
  81. {
  82. LED_ON();
  83. }
  84. extern "C" void s2s_ledOff()
  85. {
  86. LED_OFF();
  87. }
  88. /**************/
  89. /* Log saving */
  90. /**************/
  91. void save_logfile(bool always = false)
  92. {
  93. static uint32_t prev_log_pos = 0;
  94. static uint32_t prev_log_len = 0;
  95. static uint32_t prev_log_save = 0;
  96. uint32_t loglen = log_get_buffer_len();
  97. if (loglen != prev_log_len && g_sdcard_present)
  98. {
  99. // When debug is off, save log at most every LOG_SAVE_INTERVAL_MS
  100. // When debug is on, save after every SCSI command.
  101. if (always || g_log_debug || (LOG_SAVE_INTERVAL_MS > 0 && (uint32_t)(millis() - prev_log_save) > LOG_SAVE_INTERVAL_MS))
  102. {
  103. g_logfile.write(log_get_buffer(&prev_log_pos));
  104. g_logfile.flush();
  105. prev_log_len = loglen;
  106. prev_log_save = millis();
  107. }
  108. }
  109. }
  110. void init_logfile()
  111. {
  112. static bool first_open_after_boot = true;
  113. bool truncate = first_open_after_boot;
  114. int flags = O_WRONLY | O_CREAT | (truncate ? O_TRUNC : O_APPEND);
  115. g_logfile = SD.open(LOGFILE, flags);
  116. if (!g_logfile.isOpen())
  117. {
  118. log("Failed to open log file: ", SD.sdErrorCode());
  119. }
  120. save_logfile(true);
  121. first_open_after_boot = false;
  122. }
  123. const char * fatTypeToChar(int fatType)
  124. {
  125. switch (fatType)
  126. {
  127. case FAT_TYPE_EXFAT:
  128. return "exFAT";
  129. case FAT_TYPE_FAT32:
  130. return "FAT32";
  131. case FAT_TYPE_FAT16:
  132. return "FAT16";
  133. case FAT_TYPE_FAT12:
  134. return "FAT12";
  135. default:
  136. return "Unknown";
  137. }
  138. }
  139. void print_sd_info()
  140. {
  141. log(" ");
  142. log("=== SD Card Info ===");
  143. uint64_t size = (uint64_t)SD.vol()->clusterCount() * SD.vol()->bytesPerCluster();
  144. log("SD card detected, ", fatTypeToChar((int)SD.vol()->fatType()),
  145. " volume size: ", (int)(size / 1024 / 1024), " MB");
  146. cid_t sd_cid;
  147. if(SD.card()->readCID(&sd_cid))
  148. {
  149. char sdname[6] = {sd_cid.pnm[0], sd_cid.pnm[1], sd_cid.pnm[2], sd_cid.pnm[3], sd_cid.pnm[4], 0};
  150. log("SD Name: ", sdname, ", MID: ", (uint8_t)sd_cid.mid, ", OID: ", (uint8_t)sd_cid.oid[0], " ", (uint8_t)sd_cid.oid[1]);
  151. debuglog("SD Date: ", (int)sd_cid.mdtMonth(), "/", sd_cid.mdtYear());
  152. debuglog("SD Serial: ", sd_cid.psn());
  153. }
  154. }
  155. /*********************************/
  156. /* Harddisk image file handling */
  157. /*********************************/
  158. const char * typeToChar(int deviceType)
  159. {
  160. switch (deviceType)
  161. {
  162. case S2S_CFG_OPTICAL:
  163. return "Optical";
  164. case S2S_CFG_FIXED:
  165. return "Fixed";
  166. case S2S_CFG_FLOPPY_14MB:
  167. return "Floppy1.4MB";
  168. case S2S_CFG_MO:
  169. return "MO";
  170. case S2S_CFG_SEQUENTIAL:
  171. return "Tape";
  172. case S2S_CFG_REMOVEABLE:
  173. return "Removable";
  174. default:
  175. return "Unknown";
  176. }
  177. }
  178. const char * quirksToChar(int quirks)
  179. {
  180. switch (quirks)
  181. {
  182. case S2S_CFG_QUIRKS_APPLE:
  183. return "Apple";
  184. case S2S_CFG_QUIRKS_OMTI:
  185. return "OMTI";
  186. case S2S_CFG_QUIRKS_VMS:
  187. return "VMS";
  188. case S2S_CFG_QUIRKS_XEBEC:
  189. return "XEBEC";
  190. case S2S_CFG_QUIRKS_NONE:
  191. return "None";
  192. default:
  193. return "Unknown";
  194. }
  195. }
  196. // Iterate over the root path in the SD card looking for candidate image files.
  197. bool findHDDImages()
  198. {
  199. char imgdir[MAX_FILE_PATH];
  200. ini_gets("SCSI", "Dir", "/", imgdir, sizeof(imgdir), CONFIGFILE);
  201. int dirindex = 0;
  202. log(" ");
  203. log("=== Finding images in ", imgdir, " ===");
  204. SdFile root;
  205. root.open(imgdir);
  206. if (!root.isOpen())
  207. {
  208. log("Could not open directory: ", imgdir);
  209. }
  210. SdFile file;
  211. bool imageReady;
  212. bool foundImage = false;
  213. int usedDefaultId = 0;
  214. while (1)
  215. {
  216. if (!file.openNext(&root, O_READ))
  217. {
  218. // Check for additional directories with ini keys Dir1..Dir9
  219. while (dirindex < 10)
  220. {
  221. dirindex++;
  222. char key[5] = "Dir0";
  223. key[3] += dirindex;
  224. if (ini_gets("SCSI", key, "", imgdir, sizeof(imgdir), CONFIGFILE) != 0)
  225. {
  226. break;
  227. }
  228. }
  229. if (imgdir[0] != '\0')
  230. {
  231. log("== Finding HDD images in additional Dir", (int)dirindex, " = \"", imgdir, "\" ==");
  232. root.open(imgdir);
  233. if (!root.isOpen())
  234. {
  235. log("-- Could not open directory: ", imgdir);
  236. }
  237. continue;
  238. }
  239. else
  240. {
  241. break;
  242. }
  243. }
  244. char name[MAX_FILE_PATH+1];
  245. if(!file.isDir())
  246. {
  247. file.getName(name, MAX_FILE_PATH+1);
  248. file.close();
  249. bool is_hd = (tolower(name[0]) == 'h' && tolower(name[1]) == 'd');
  250. bool is_cd = (tolower(name[0]) == 'c' && tolower(name[1]) == 'd');
  251. bool is_fd = (tolower(name[0]) == 'f' && tolower(name[1]) == 'd');
  252. bool is_mo = (tolower(name[0]) == 'm' && tolower(name[1]) == 'o');
  253. bool is_re = (tolower(name[0]) == 'r' && tolower(name[1]) == 'e');
  254. bool is_tp = (tolower(name[0]) == 't' && tolower(name[1]) == 'p');
  255. if(strcasecmp(name, "CLEAR_ROM") == 0)
  256. {
  257. romDriveClear();
  258. continue;
  259. }
  260. if (is_hd || is_cd || is_fd || is_mo || is_re || is_tp)
  261. {
  262. // Check file extension
  263. // We accept anything except known compressed files
  264. bool is_compressed = false;
  265. const char *extension = strrchr(name, '.');
  266. if (extension)
  267. {
  268. const char *archive_exts[] = {
  269. ".tar", ".tgz", ".gz", ".bz2", ".tbz2", ".xz", ".zst", ".z",
  270. ".zip", ".zipx", ".rar", ".lzh", ".lha", ".lzo", ".lz4", ".arj",
  271. ".dmg", ".hqx", ".cpt", ".7z", ".s7z",
  272. NULL
  273. };
  274. for (int i = 0; archive_exts[i]; i++)
  275. {
  276. if (strcasecmp(extension, archive_exts[i]) == 0)
  277. {
  278. is_compressed = true;
  279. break;
  280. }
  281. }
  282. }
  283. if (is_compressed)
  284. {
  285. log("-- Ignoring compressed file ", name);
  286. continue;
  287. }
  288. if (strcasecmp(extension, ".cue") == 0)
  289. {
  290. continue; // .cue will be handled with corresponding .bin
  291. }
  292. // Check if the image should be loaded to microcontroller flash ROM drive
  293. bool is_romdrive = false;
  294. if (extension && strcasecmp(extension, ".rom") == 0)
  295. {
  296. is_romdrive = true;
  297. }
  298. else if (extension && strcasecmp(extension, ".rom_loaded") == 0)
  299. {
  300. // Already loaded ROM drive, ignore the image
  301. continue;
  302. }
  303. // Defaults for Hard Disks
  304. int id = 1; // 0 and 3 are common in Macs for physical HD and CD, so avoid them.
  305. int lun = 0;
  306. int blk = 512;
  307. if (is_cd)
  308. {
  309. // Use 2048 as the default sector size for CD-ROMs
  310. blk = 2048;
  311. }
  312. // Parse SCSI device ID
  313. int file_name_length = strlen(name);
  314. if(file_name_length > 2) { // HD[N]
  315. int tmp_id = name[HDIMG_ID_POS] - '0';
  316. if(tmp_id > -1 && tmp_id < 8)
  317. {
  318. id = tmp_id; // If valid id, set it, else use default
  319. }
  320. else
  321. {
  322. id = usedDefaultId++;
  323. }
  324. }
  325. // Parse SCSI LUN number
  326. if(file_name_length > 3) // HD0[N]
  327. {
  328. int tmp_lun = name[HDIMG_LUN_POS] - '0';
  329. if(tmp_lun > -1 && tmp_lun < NUM_SCSILUN)
  330. {
  331. lun = tmp_lun; // If valid id, set it, else use default
  332. }
  333. }
  334. // Parse block size (HD00_NNNN)
  335. const char *blksize = strchr(name, '_');
  336. if (blksize)
  337. {
  338. int blktmp = strtoul(blksize + 1, NULL, 10);
  339. if (blktmp == 256 || blktmp == 512 || blktmp == 1024 ||
  340. blktmp == 2048 || blktmp == 4096 || blktmp == 8192)
  341. {
  342. blk = blktmp;
  343. }
  344. }
  345. // Add the directory name to get the full file path
  346. char fullname[MAX_FILE_PATH * 2 + 2] = {0};
  347. strncpy(fullname, imgdir, MAX_FILE_PATH);
  348. if (fullname[strlen(fullname) - 1] != '/') strcat(fullname, "/");
  349. strcat(fullname, name);
  350. // Check whether this SCSI ID has been configured yet
  351. const S2S_TargetCfg* cfg = s2s_getConfigById(id);
  352. if (cfg)
  353. {
  354. log("-- Ignoring ", fullname, ", SCSI ID ", id, " is already in use!");
  355. continue;
  356. }
  357. // Apple computers reserve ID 7, so warn the user this configuration wont work
  358. if(id == 7 && cfg->quirks == S2S_CFG_QUIRKS_APPLE )
  359. {
  360. log("-- Ignoring ", fullname, ", SCSI ID ", id, " Quirks set to Apple so can not use SCSI ID 7!");
  361. continue;
  362. }
  363. // Type mapping based on filename.
  364. // If type is FIXED, the type can still be overridden in .ini file.
  365. S2S_CFG_TYPE type = S2S_CFG_FIXED;
  366. if (is_cd) type = S2S_CFG_OPTICAL;
  367. if (is_fd) type = S2S_CFG_FLOPPY_14MB;
  368. if (is_mo) type = S2S_CFG_MO;
  369. if (is_re) type = S2S_CFG_REMOVEABLE;
  370. if (is_tp) type = S2S_CFG_SEQUENTIAL;
  371. // Open the image file
  372. if (id < NUM_SCSIID && is_romdrive)
  373. {
  374. log("== Loading ROM drive from ", fullname, " for ID: ", id);
  375. imageReady = scsiDiskProgramRomDrive(fullname, id, blk, type);
  376. if (imageReady)
  377. {
  378. foundImage = true;
  379. }
  380. }
  381. else if(id < NUM_SCSIID && lun < NUM_SCSILUN)
  382. {
  383. log("== Opening ", fullname, " for ID: ", id, " LUN: ", lun);
  384. imageReady = scsiDiskOpenHDDImage(id, fullname, id, lun, blk, type);
  385. if(imageReady)
  386. {
  387. foundImage = true;
  388. log("---- Image ready");
  389. }
  390. else
  391. {
  392. log("---- Failed to load image");
  393. }
  394. }
  395. else
  396. {
  397. log("-- Invalid lun or id for image ", fullname);
  398. }
  399. }
  400. }
  401. }
  402. if(usedDefaultId > 0)
  403. {
  404. log("-- ", usedDefaultId, " images did not specify a SCSI ID and were assigned one if possible.");
  405. }
  406. root.close();
  407. g_romdrive_active = scsiDiskActivateRomDrive();
  408. // Print SCSI drive map
  409. log(" ");
  410. log("=== Configured SCSI Devices ===");
  411. for (int i = 0; i < NUM_SCSIID; i++)
  412. {
  413. const S2S_TargetCfg* cfg = s2s_getConfigByIndex(i);
  414. if (cfg && (cfg->scsiId & S2S_CFG_TARGET_ENABLED))
  415. {
  416. int capacity_kB = ((uint64_t)cfg->scsiSectors * cfg->bytesPerSector) / 1024;
  417. log("* ID: ", (int)(cfg->scsiId & 7),
  418. ", BlockSize: ", (int)cfg->bytesPerSector,
  419. ", Type: ", typeToChar((int)cfg->deviceType),
  420. ", Quirks: ", quirksToChar((int)cfg->quirks),
  421. ", Size: ", capacity_kB, "kB");
  422. }
  423. }
  424. return foundImage;
  425. }
  426. /************************/
  427. /* Config file loading */
  428. /************************/
  429. void readSCSIDeviceConfig()
  430. {
  431. log(" ");
  432. log("=== Global Config ===");
  433. s2s_configInit(&scsiDev.boardCfg);
  434. for (int i = 0; i < NUM_SCSIID; i++)
  435. {
  436. scsiDiskLoadConfig(i);
  437. }
  438. }
  439. /*********************************/
  440. /* Main SCSI handling loop */
  441. /*********************************/
  442. static bool mountSDCard()
  443. {
  444. // Prepare for mounting new SD card by closing all old files.
  445. // When switching between FAT and exFAT cards the pointers
  446. // are invalidated and accessing old files results in crash.
  447. invalidate_ini_cache();
  448. g_logfile.close();
  449. scsiDiskCloseSDCardImages();
  450. // Check for the common case, FAT filesystem as first partition
  451. if (SD.begin(SD_CONFIG))
  452. {
  453. reload_ini_cache(CONFIGFILE);
  454. return true;
  455. }
  456. // Do we have any kind of card?
  457. if (!SD.card() || SD.sdErrorCode() != 0)
  458. return false;
  459. // Try to mount the whole card as FAT (without partition table)
  460. if (static_cast<FsVolume*>(&SD)->begin(SD.card(), true, 0))
  461. return true;
  462. // Failed to mount FAT filesystem, but card can still be accessed as raw image
  463. return true;
  464. }
  465. static void reinitSCSI()
  466. {
  467. if (ini_getbool("SCSI", "Debug", 0, CONFIGFILE))
  468. {
  469. g_log_debug = true;
  470. }
  471. #ifdef PLATFORM_HAS_INITIATOR_MODE
  472. if (platform_is_initiator_mode_enabled())
  473. {
  474. // Initialize scsiDev to zero values even though it is not used
  475. scsiInit();
  476. // Initializer initiator mode state machine
  477. scsiInitiatorInit();
  478. blinkStatus(BLINK_STATUS_OK);
  479. return;
  480. }
  481. #endif
  482. scsiDiskResetImages();
  483. readSCSIDeviceConfig();
  484. findHDDImages();
  485. // Error if there are 0 image files
  486. if (scsiDiskCheckAnyImagesConfigured())
  487. {
  488. // Ok, there is an image, turn LED on for the time it takes to perform init
  489. LED_ON();
  490. delay(100);
  491. }
  492. else
  493. {
  494. #if RAW_FALLBACK_ENABLE
  495. log("No images found, enabling RAW fallback partition");
  496. scsiDiskOpenHDDImage(RAW_FALLBACK_SCSI_ID, "RAW:0:0xFFFFFFFF", RAW_FALLBACK_SCSI_ID, 0,
  497. RAW_FALLBACK_BLOCKSIZE);
  498. #else
  499. log("No valid image files found!");
  500. #endif
  501. blinkStatus(BLINK_ERROR_NO_IMAGES);
  502. }
  503. scsiPhyReset();
  504. scsiDiskInit();
  505. scsiInit();
  506. }
  507. extern "C" void bluescsi_setup(void)
  508. {
  509. platform_init();
  510. platform_late_init();
  511. g_sdcard_present = mountSDCard();
  512. if(!g_sdcard_present)
  513. {
  514. log("SD card init failed, sdErrorCode: ", (int)SD.sdErrorCode(),
  515. " sdErrorData: ", (int)SD.sdErrorData());
  516. if (romDriveCheckPresent())
  517. {
  518. reinitSCSI();
  519. if (g_romdrive_active)
  520. {
  521. log("Enabled ROM drive without SD card");
  522. return;
  523. }
  524. }
  525. do
  526. {
  527. blinkStatus(BLINK_ERROR_NO_SD_CARD);
  528. delay(1000);
  529. platform_reset_watchdog();
  530. g_sdcard_present = mountSDCard();
  531. } while (!g_sdcard_present);
  532. log("SD card init succeeded after retry");
  533. }
  534. if (g_sdcard_present)
  535. {
  536. if (SD.clusterCount() == 0)
  537. {
  538. log("SD card without filesystem!");
  539. }
  540. print_sd_info();
  541. reinitSCSI();
  542. }
  543. log(" ");
  544. log("Initialization complete!");
  545. if (g_sdcard_present)
  546. {
  547. init_logfile();
  548. if (ini_getbool("SCSI", "DisableStatusLED", false, CONFIGFILE))
  549. {
  550. platform_disable_led();
  551. }
  552. }
  553. // Counterpart for the LED_ON in reinitSCSI().
  554. LED_OFF();
  555. }
  556. extern "C" void bluescsi_main_loop(void)
  557. {
  558. static uint32_t sd_card_check_time = 0;
  559. static uint32_t last_request_time = 0;
  560. platform_reset_watchdog();
  561. platform_poll();
  562. diskEjectButtonUpdate(true);
  563. #ifdef PLATFORM_HAS_INITIATOR_MODE
  564. if (platform_is_initiator_mode_enabled())
  565. {
  566. scsiInitiatorMainLoop();
  567. save_logfile();
  568. }
  569. else
  570. #endif
  571. {
  572. scsiPoll();
  573. scsiDiskPoll();
  574. scsiLogPhaseChange(scsiDev.phase);
  575. // Save log periodically during status phase if there are new messages.
  576. // In debug mode, also save every 2 seconds if no SCSI requests come in.
  577. // SD card writing takes a while, during which the code can't handle new
  578. // SCSI requests, so normally we only want to save during a phase where
  579. // the host is waiting for us. But for debugging issues where no requests
  580. // come through or a request hangs, it's useful to force saving of log.
  581. if (scsiDev.phase == STATUS || (g_log_debug && (uint32_t)(millis() - last_request_time) > 2000))
  582. {
  583. save_logfile();
  584. last_request_time = millis();
  585. }
  586. }
  587. if (g_sdcard_present)
  588. {
  589. // Check SD card status for hotplug
  590. if (scsiDev.phase == BUS_FREE &&
  591. (uint32_t)(millis() - sd_card_check_time) > 5000)
  592. {
  593. sd_card_check_time = millis();
  594. uint32_t ocr;
  595. if (!SD.card()->readOCR(&ocr))
  596. {
  597. if (!SD.card()->readOCR(&ocr))
  598. {
  599. g_sdcard_present = false;
  600. log("SD card removed, trying to reinit");
  601. }
  602. }
  603. }
  604. }
  605. if (!g_sdcard_present)
  606. {
  607. // Try to remount SD card
  608. do
  609. {
  610. g_sdcard_present = mountSDCard();
  611. if (g_sdcard_present)
  612. {
  613. log("SD card reinit succeeded");
  614. print_sd_info();
  615. reinitSCSI();
  616. init_logfile();
  617. }
  618. else if (!g_romdrive_active)
  619. {
  620. blinkStatus(BLINK_ERROR_NO_SD_CARD);
  621. delay(1000);
  622. platform_reset_watchdog();
  623. platform_poll();
  624. }
  625. } while (!g_sdcard_present && !g_romdrive_active);
  626. }
  627. else
  628. {
  629. }
  630. }