ImageBackingStore.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. ImageBackingStore::ImageBackingStore()
  33. {
  34. m_israw = false;
  35. m_isrom = false;
  36. m_isreadonly_attr = false;
  37. m_blockdev = nullptr;
  38. m_bgnsector = m_endsector = m_cursector = 0;
  39. }
  40. ImageBackingStore::ImageBackingStore(const char *filename, uint32_t scsi_block_size): ImageBackingStore()
  41. {
  42. if (strncasecmp(filename, "RAW:", 4) == 0)
  43. {
  44. char *endptr, *endptr2;
  45. m_bgnsector = strtoul(filename + 4, &endptr, 0);
  46. m_endsector = strtoul(endptr + 1, &endptr2, 0);
  47. if (*endptr != ':' || *endptr2 != '\0')
  48. {
  49. logmsg("Invalid format for raw filename: ", filename);
  50. return;
  51. }
  52. if ((scsi_block_size % SD_SECTOR_SIZE) != 0)
  53. {
  54. logmsg("SCSI block size ", (int)scsi_block_size, " is not supported for RAW partitions (must be divisible by 512 bytes)");
  55. return;
  56. }
  57. m_israw = true;
  58. m_blockdev = SD.card();
  59. uint32_t sectorCount = SD.card()->sectorCount();
  60. if (m_endsector >= sectorCount)
  61. {
  62. logmsg("---- Limiting RAW image mapping to SD card sector count: ", (int)sectorCount);
  63. m_endsector = sectorCount - 1;
  64. }
  65. }
  66. else if (strncasecmp(filename, "ROM:", 4) == 0)
  67. {
  68. if (!romDriveCheckPresent(&m_romhdr))
  69. {
  70. m_romhdr.imagesize = 0;
  71. }
  72. else
  73. {
  74. m_isrom = true;
  75. }
  76. }
  77. else
  78. {
  79. m_isreadonly_attr = !!(FS_ATTRIB_READ_ONLY & SD.attrib(filename));
  80. if (m_isreadonly_attr)
  81. {
  82. m_fsfile = SD.open(filename, O_RDONLY);
  83. logmsg("---- Image file is read-only, writes disabled");
  84. }
  85. else
  86. {
  87. m_fsfile = SD.open(filename, O_RDWR);
  88. }
  89. uint32_t sectorcount = m_fsfile.size() / SD_SECTOR_SIZE;
  90. uint32_t begin = 0, end = 0;
  91. if (m_fsfile.contiguousRange(&begin, &end) && end >= begin + sectorcount
  92. && (scsi_block_size % SD_SECTOR_SIZE) == 0)
  93. {
  94. // Convert to raw mapping, this avoids some unnecessary
  95. // access overhead in SdFat library.
  96. // If non-aligned offsets are later requested, it automatically falls
  97. // back to SdFat access mode.
  98. m_israw = true;
  99. m_blockdev = SD.card();
  100. m_bgnsector = begin;
  101. if (end != begin + sectorcount)
  102. {
  103. uint32_t allocsize = end - begin + 1;
  104. // Due to issue #80 in ZuluSCSI version 1.0.8 and 1.0.9 the allocated size was mistakenly reported to SCSI controller.
  105. // If the drive was formatted using those versions, you may have problems accessing it with newer firmware.
  106. // The old behavior can be restored with setting [SCSI] UseFATAllocSize = 1 in config file.
  107. if (g_scsi_settings.getSystem()->useFATAllocSize)
  108. {
  109. sectorcount = allocsize;
  110. }
  111. }
  112. m_endsector = begin + sectorcount - 1;
  113. m_fsfile.flush(); // Note: m_fsfile is also kept open as a fallback.
  114. }
  115. }
  116. }
  117. bool ImageBackingStore::isOpen()
  118. {
  119. if (m_israw)
  120. return (m_blockdev != NULL);
  121. else if (m_isrom)
  122. return (m_romhdr.imagesize > 0);
  123. else
  124. return m_fsfile.isOpen();
  125. }
  126. bool ImageBackingStore::isWritable()
  127. {
  128. return !m_isrom && !m_isreadonly_attr;
  129. }
  130. bool ImageBackingStore::isRom()
  131. {
  132. return m_isrom;
  133. }
  134. bool ImageBackingStore::isRaw()
  135. {
  136. return m_israw;
  137. }
  138. bool ImageBackingStore::close()
  139. {
  140. if (m_israw)
  141. {
  142. m_blockdev = nullptr;
  143. return true;
  144. }
  145. else if (m_isrom)
  146. {
  147. m_romhdr.imagesize = 0;
  148. return true;
  149. }
  150. else
  151. {
  152. return m_fsfile.close();
  153. }
  154. }
  155. uint64_t ImageBackingStore::size()
  156. {
  157. if (m_israw && m_blockdev)
  158. {
  159. return (uint64_t)(m_endsector - m_bgnsector + 1) * SD_SECTOR_SIZE;
  160. }
  161. else if (m_isrom)
  162. {
  163. return m_romhdr.imagesize;
  164. }
  165. else
  166. {
  167. return m_fsfile.size();
  168. }
  169. }
  170. bool ImageBackingStore::contiguousRange(uint32_t* bgnSector, uint32_t* endSector)
  171. {
  172. if (m_israw && m_blockdev)
  173. {
  174. *bgnSector = m_bgnsector;
  175. *endSector = m_endsector;
  176. return true;
  177. }
  178. else if (m_isrom)
  179. {
  180. *bgnSector = 0;
  181. *endSector = 0;
  182. return true;
  183. }
  184. else
  185. {
  186. return m_fsfile.contiguousRange(bgnSector, endSector);
  187. }
  188. }
  189. bool ImageBackingStore::seek(uint64_t pos)
  190. {
  191. uint32_t sectornum = pos / SD_SECTOR_SIZE;
  192. if (m_israw && (uint64_t)sectornum * SD_SECTOR_SIZE != pos)
  193. {
  194. dbgmsg("---- Unaligned access to image, falling back to SdFat access mode");
  195. m_israw = false;
  196. }
  197. if (m_israw)
  198. {
  199. m_cursector = m_bgnsector + sectornum;
  200. return (m_cursector <= m_endsector);
  201. }
  202. else if (m_isrom)
  203. {
  204. uint32_t sectornum = pos / SD_SECTOR_SIZE;
  205. assert((uint64_t)sectornum * SD_SECTOR_SIZE == pos);
  206. m_cursector = sectornum;
  207. return m_cursector * SD_SECTOR_SIZE < m_romhdr.imagesize;
  208. }
  209. else
  210. {
  211. return m_fsfile.seek(pos);
  212. }
  213. }
  214. ssize_t ImageBackingStore::read(void* buf, size_t count)
  215. {
  216. uint32_t sectorcount = count / SD_SECTOR_SIZE;
  217. if (m_israw && (uint64_t)sectorcount * SD_SECTOR_SIZE != count)
  218. {
  219. dbgmsg("---- Unaligned access to image, falling back to SdFat access mode");
  220. m_israw = false;
  221. }
  222. if (m_israw && m_blockdev)
  223. {
  224. if (m_blockdev->readSectors(m_cursector, (uint8_t*)buf, sectorcount))
  225. {
  226. m_cursector += sectorcount;
  227. return count;
  228. }
  229. else
  230. {
  231. return -1;
  232. }
  233. }
  234. else if (m_isrom)
  235. {
  236. uint32_t sectorcount = count / SD_SECTOR_SIZE;
  237. assert((uint64_t)sectorcount * SD_SECTOR_SIZE == count);
  238. uint32_t start = m_cursector * SD_SECTOR_SIZE;
  239. if (romDriveRead((uint8_t*)buf, start, count))
  240. {
  241. m_cursector += sectorcount;
  242. return count;
  243. }
  244. else
  245. {
  246. return -1;
  247. }
  248. }
  249. else
  250. {
  251. return m_fsfile.read(buf, count);
  252. }
  253. }
  254. ssize_t ImageBackingStore::write(const void* buf, size_t count)
  255. {
  256. uint32_t sectorcount = count / SD_SECTOR_SIZE;
  257. if (m_israw && (uint64_t)sectorcount * SD_SECTOR_SIZE != count)
  258. {
  259. dbgmsg("---- Unaligned access to image, falling back to SdFat access mode");
  260. m_israw = false;
  261. }
  262. if (m_israw && m_blockdev)
  263. {
  264. if (m_blockdev->writeSectors(m_cursector, (const uint8_t*)buf, sectorcount))
  265. {
  266. m_cursector += sectorcount;
  267. return count;
  268. }
  269. else
  270. {
  271. return 0;
  272. }
  273. }
  274. else if (m_isrom)
  275. {
  276. logmsg("ERROR: attempted to write to ROM drive");
  277. return 0;
  278. }
  279. else if (m_isreadonly_attr)
  280. {
  281. logmsg("ERROR: attempted to write to a read only image");
  282. return 0;
  283. }
  284. else
  285. {
  286. return m_fsfile.write(buf, count);
  287. }
  288. }
  289. void ImageBackingStore::flush()
  290. {
  291. if (!m_israw && !m_isrom && !m_isreadonly_attr)
  292. {
  293. m_fsfile.flush();
  294. }
  295. }
  296. uint64_t ImageBackingStore::position()
  297. {
  298. if (!m_israw && !m_isrom)
  299. {
  300. return m_fsfile.curPosition();
  301. }
  302. else
  303. {
  304. return 0;
  305. }
  306. }
  307. size_t ImageBackingStore::getFilename(char* buf, size_t buflen)
  308. {
  309. if (m_fsfile.isOpen())
  310. {
  311. size_t name_length = m_fsfile.getName(buf, buflen);
  312. if (name_length + 1 > buflen)
  313. return 0;
  314. else
  315. return name_length;
  316. }
  317. return 0;
  318. }