ImageBackingStore.cpp 9.0 KB

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