ImageBackingStore.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * This file is originally part of ZuluSCSI adopted for BlueSCSI
  3. *
  4. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  5. * Copyright (C) 2023 Eric Helgeson
  6. *
  7. * This file is licensed under the GPL version 3 or any later version. 
  8. *
  9. * https://www.gnu.org/licenses/gpl-3.0.html
  10. * ----
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version. 
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. * GNU General Public License for more details. 
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  23. **/
  24. #include "ImageBackingStore.h"
  25. #include <SdFat.h>
  26. #include <BlueSCSI_platform.h>
  27. #include "BlueSCSI_log.h"
  28. #include "BlueSCSI_config.h"
  29. #include "BlueSCSI_settings.h"
  30. #include <minIni.h>
  31. #include <strings.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. extern bool g_rawdrive_active;
  35. ImageBackingStore::ImageBackingStore()
  36. {
  37. m_iscontiguous = false;
  38. m_israw = false;
  39. g_rawdrive_active = m_israw;
  40. m_isrom = false;
  41. m_isreadonly_attr = false;
  42. m_blockdev = nullptr;
  43. m_bgnsector = m_endsector = m_cursector = 0;
  44. m_isfolder = false;
  45. m_foldername[0] = '\0';
  46. }
  47. ImageBackingStore::ImageBackingStore(const char *filename, uint32_t scsi_block_size): ImageBackingStore()
  48. {
  49. if (strncasecmp(filename, "RAW:", 4) == 0)
  50. {
  51. char *endptr, *endptr2;
  52. m_bgnsector = strtoul(filename + 4, &endptr, 0);
  53. m_endsector = strtoul(endptr + 1, &endptr2, 0);
  54. if (*endptr != ':' || *endptr2 != '\0')
  55. {
  56. logmsg("Invalid format for raw filename: ", filename);
  57. return;
  58. }
  59. if ((scsi_block_size % SD_SECTOR_SIZE) != 0)
  60. {
  61. logmsg("SCSI block size ", (int)scsi_block_size, " is not supported for RAW partitions (must be divisible by 512 bytes)");
  62. return;
  63. }
  64. m_iscontiguous = true;
  65. m_israw = true;
  66. g_rawdrive_active = m_israw;
  67. m_blockdev = SD.card();
  68. uint32_t sectorCount = SD.card()->sectorCount();
  69. if (m_endsector >= sectorCount)
  70. {
  71. logmsg("---- Limiting RAW image mapping to SD card sector count: ", (int)sectorCount);
  72. m_endsector = sectorCount - 1;
  73. }
  74. }
  75. else if (strncasecmp(filename, "ROM:", 4) == 0)
  76. {
  77. if (!romDriveCheckPresent(&m_romhdr))
  78. {
  79. m_romhdr.imagesize = 0;
  80. }
  81. else
  82. {
  83. m_isrom = true;
  84. }
  85. }
  86. else
  87. {
  88. if (SD.open(filename, O_RDONLY).isDir())
  89. {
  90. // Folder that contains .cue sheet and multiple .bin files
  91. m_isfolder = true;
  92. strncpy(m_foldername, filename, sizeof(m_foldername));
  93. m_foldername[sizeof(m_foldername)-1] = '\0';
  94. }
  95. else
  96. {
  97. // Regular image file
  98. _internal_open(filename);
  99. }
  100. }
  101. }
  102. bool ImageBackingStore::_internal_open(const char *filename)
  103. {
  104. m_isreadonly_attr = !!(FS_ATTRIB_READ_ONLY & SD.attrib(filename));
  105. oflag_t open_flag = O_RDWR;
  106. if (m_isreadonly_attr && !m_isfolder)
  107. {
  108. open_flag = O_RDONLY;
  109. logmsg("---- Image file is read-only, writes disabled");
  110. }
  111. if (m_isfolder)
  112. {
  113. char fullpath[MAX_FILE_PATH * 2];
  114. strncpy(fullpath, m_foldername, sizeof(fullpath) - strlen(fullpath));
  115. strncat(fullpath, "/", sizeof(fullpath) - strlen(fullpath));
  116. strncat(fullpath, filename, sizeof(fullpath) - strlen(fullpath));
  117. m_fsfile = SD.open(fullpath, open_flag);
  118. }
  119. else
  120. {
  121. m_fsfile = SD.open(filename, open_flag);
  122. }
  123. if (!m_fsfile.isOpen())
  124. {
  125. return false;
  126. }
  127. uint32_t sectorcount = m_fsfile.dataLength() / SD_SECTOR_SIZE;
  128. uint32_t begin = 0, end = 0;
  129. if (m_fsfile.contiguousRange(&begin, &end) && end >= begin + sectorcount - 1)
  130. {
  131. // Convert to raw mapping, this avoids some unnecessary
  132. // access overhead in SdFat library.
  133. // If non-aligned offsets are later requested, it automatically falls
  134. // back to SdFat access mode.
  135. m_iscontiguous = true;
  136. m_blockdev = SD.card();
  137. m_bgnsector = begin;
  138. if (end != begin + sectorcount)
  139. {
  140. uint32_t allocsize = end - begin + 1;
  141. // Due to issue #80 in BlueSCSI version 1.0.8 and 1.0.9 the allocated size was mistakenly reported to SCSI controller.
  142. // If the drive was formatted using those versions, you may have problems accessing it with newer firmware.
  143. // The old behavior can be restored with setting [SCSI] UseFATAllocSize = 1 in config file.
  144. if (g_scsi_settings.getSystem()->useFATAllocSize)
  145. {
  146. sectorcount = allocsize;
  147. }
  148. }
  149. m_endsector = begin + sectorcount - 1;
  150. m_fsfile.flush(); // Note: m_fsfile is also kept open as a fallback.
  151. }
  152. return true;
  153. }
  154. bool ImageBackingStore::isOpen()
  155. {
  156. if (m_iscontiguous)
  157. return (m_blockdev != NULL);
  158. else if (m_isrom)
  159. return (m_romhdr.imagesize > 0);
  160. else if (m_isfolder)
  161. return m_foldername[0] != '\0';
  162. else
  163. return m_fsfile.isOpen();
  164. }
  165. bool ImageBackingStore::isWritable()
  166. {
  167. return !m_isrom && !m_isreadonly_attr;
  168. }
  169. bool ImageBackingStore::isRaw()
  170. {
  171. return m_israw;
  172. }
  173. bool ImageBackingStore::isRom()
  174. {
  175. return m_isrom;
  176. }
  177. bool ImageBackingStore::isFolder()
  178. {
  179. return m_isfolder;
  180. }
  181. bool ImageBackingStore::isContiguous()
  182. {
  183. return m_iscontiguous;
  184. }
  185. bool ImageBackingStore::close()
  186. {
  187. m_isfolder = false;
  188. if (m_iscontiguous)
  189. {
  190. m_blockdev = nullptr;
  191. return true;
  192. }
  193. else if (m_isrom)
  194. {
  195. m_romhdr.imagesize = 0;
  196. return true;
  197. }
  198. else
  199. {
  200. return m_fsfile.close();
  201. }
  202. }
  203. uint64_t ImageBackingStore::size()
  204. {
  205. if (m_iscontiguous && m_blockdev && m_israw)
  206. {
  207. return (uint64_t)(m_endsector - m_bgnsector + 1) * SD_SECTOR_SIZE;
  208. }
  209. else if (m_isrom)
  210. {
  211. return m_romhdr.imagesize;
  212. }
  213. else
  214. {
  215. return m_fsfile.dataLength();
  216. }
  217. }
  218. bool ImageBackingStore::contiguousRange(uint32_t* bgnSector, uint32_t* endSector)
  219. {
  220. if (m_iscontiguous && m_blockdev)
  221. {
  222. *bgnSector = m_bgnsector;
  223. *endSector = m_endsector;
  224. return true;
  225. }
  226. else if (m_isrom)
  227. {
  228. *bgnSector = 0;
  229. *endSector = 0;
  230. return true;
  231. }
  232. else
  233. {
  234. return m_fsfile.contiguousRange(bgnSector, endSector);
  235. }
  236. }
  237. bool ImageBackingStore::seek(uint64_t pos)
  238. {
  239. uint32_t sectornum = pos / SD_SECTOR_SIZE;
  240. if (m_iscontiguous && (uint64_t)sectornum * SD_SECTOR_SIZE != pos)
  241. {
  242. dbgmsg("---- Unaligned access to image, falling back to SdFat access mode");
  243. m_iscontiguous = false;
  244. }
  245. if (m_iscontiguous)
  246. {
  247. m_cursector = m_bgnsector + sectornum;
  248. return (m_cursector <= m_endsector);
  249. }
  250. else if (m_isrom)
  251. {
  252. uint32_t sectornum = pos / SD_SECTOR_SIZE;
  253. assert((uint64_t)sectornum * SD_SECTOR_SIZE == pos);
  254. m_cursector = sectornum;
  255. return m_cursector * SD_SECTOR_SIZE < m_romhdr.imagesize;
  256. }
  257. else
  258. {
  259. return m_fsfile.seek(pos);
  260. }
  261. }
  262. ssize_t ImageBackingStore::read(void* buf, size_t count)
  263. {
  264. uint32_t sectorcount = count / SD_SECTOR_SIZE;
  265. if (m_iscontiguous && (uint64_t)sectorcount * SD_SECTOR_SIZE != count)
  266. {
  267. dbgmsg("---- Unaligned access to image, falling back to SdFat access mode");
  268. m_iscontiguous = false;
  269. }
  270. if (m_iscontiguous && m_blockdev)
  271. {
  272. if (m_blockdev->readSectors(m_cursector, (uint8_t*)buf, sectorcount))
  273. {
  274. m_cursector += sectorcount;
  275. return count;
  276. }
  277. else
  278. {
  279. return -1;
  280. }
  281. }
  282. else if (m_isrom)
  283. {
  284. uint32_t sectorcount = count / SD_SECTOR_SIZE;
  285. assert((uint64_t)sectorcount * SD_SECTOR_SIZE == count);
  286. uint32_t start = m_cursector * SD_SECTOR_SIZE;
  287. if (romDriveRead((uint8_t*)buf, start, count))
  288. {
  289. m_cursector += sectorcount;
  290. return count;
  291. }
  292. else
  293. {
  294. return -1;
  295. }
  296. }
  297. else
  298. {
  299. return m_fsfile.read(buf, count);
  300. }
  301. }
  302. ssize_t ImageBackingStore::write(const void* buf, size_t count)
  303. {
  304. uint32_t sectorcount = count / SD_SECTOR_SIZE;
  305. if (m_iscontiguous && (uint64_t)sectorcount * SD_SECTOR_SIZE != count)
  306. {
  307. dbgmsg("---- Unaligned access to image, falling back to SdFat access mode");
  308. m_iscontiguous = false;
  309. }
  310. if (m_iscontiguous && m_blockdev)
  311. {
  312. if (m_blockdev->writeSectors(m_cursector, (const uint8_t*)buf, sectorcount))
  313. {
  314. m_cursector += sectorcount;
  315. return count;
  316. }
  317. else
  318. {
  319. return 0;
  320. }
  321. }
  322. else if (m_isrom)
  323. {
  324. logmsg("ERROR: attempted to write to ROM drive");
  325. return 0;
  326. }
  327. else if (m_isreadonly_attr)
  328. {
  329. logmsg("ERROR: attempted to write to a read only image");
  330. return 0;
  331. }
  332. else
  333. {
  334. return m_fsfile.write(buf, count);
  335. }
  336. }
  337. void ImageBackingStore::flush()
  338. {
  339. if (!m_iscontiguous && !m_isrom && !m_isreadonly_attr)
  340. {
  341. m_fsfile.flush();
  342. }
  343. }
  344. uint64_t ImageBackingStore::position()
  345. {
  346. if (!m_iscontiguous && !m_isrom)
  347. {
  348. return m_fsfile.curPosition();
  349. }
  350. else
  351. {
  352. return 0;
  353. }
  354. }
  355. size_t ImageBackingStore::getFilename(char* buf, size_t buflen)
  356. {
  357. if (m_fsfile.isOpen())
  358. {
  359. size_t name_length = m_fsfile.getName(buf, buflen);
  360. if (name_length + 1 > buflen)
  361. return 0;
  362. else
  363. return name_length;
  364. }
  365. return 0;
  366. }
  367. bool ImageBackingStore::selectImageFile(const char *filename)
  368. {
  369. if (!m_isfolder)
  370. {
  371. logmsg("Attempted selectImageFile() but image is not a folder");
  372. return false;
  373. }
  374. return _internal_open(filename);
  375. }
  376. size_t ImageBackingStore::getFoldername(char* buf, size_t buflen)
  377. {
  378. if (m_isfolder)
  379. {
  380. size_t name_length = strlen(m_foldername);
  381. if (name_length + 1 > buflen)
  382. return 0;
  383. strncpy(buf, m_foldername, buflen);
  384. return name_length;
  385. }
  386. return 0;
  387. }