ImageBackingStore.cpp 8.3 KB

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