ImageBackingStore.cpp 7.8 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 <BlueSCSI_platform.h>
  25. #include "BlueSCSI_log.h"
  26. #include "BlueSCSI_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. log("Invalid format for raw filename: ", filename);
  49. return;
  50. }
  51. if ((scsi_block_size % SD_SECTOR_SIZE) != 0)
  52. {
  53. log("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. log("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. log("---- 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. m_endsector = begin + sectorcount - 1;
  101. m_fsfile.flush(); // Note: m_fsfile is also kept open as a fallback.
  102. }
  103. }
  104. }
  105. bool ImageBackingStore::isOpen()
  106. {
  107. if (m_israw)
  108. return (m_blockdev != NULL);
  109. else if (m_isrom)
  110. return (m_romhdr.imagesize > 0);
  111. else
  112. return m_fsfile.isOpen();
  113. }
  114. bool ImageBackingStore::isWritable()
  115. {
  116. return !(m_isrom && m_isreadonly_attr);
  117. }
  118. bool ImageBackingStore::isRom()
  119. {
  120. return m_isrom;
  121. }
  122. bool ImageBackingStore::isRaw()
  123. {
  124. return m_israw;
  125. }
  126. bool ImageBackingStore::close()
  127. {
  128. if (m_israw)
  129. {
  130. m_blockdev = nullptr;
  131. return true;
  132. }
  133. else if (m_isrom)
  134. {
  135. m_romhdr.imagesize = 0;
  136. return true;
  137. }
  138. else
  139. {
  140. return m_fsfile.close();
  141. }
  142. }
  143. uint64_t ImageBackingStore::size()
  144. {
  145. if (m_israw && m_blockdev)
  146. {
  147. return (uint64_t)(m_endsector - m_bgnsector + 1) * SD_SECTOR_SIZE;
  148. }
  149. else if (m_isrom)
  150. {
  151. return m_romhdr.imagesize;
  152. }
  153. else
  154. {
  155. return m_fsfile.size();
  156. }
  157. }
  158. bool ImageBackingStore::contiguousRange(uint32_t* bgnSector, uint32_t* endSector)
  159. {
  160. if (m_israw && m_blockdev)
  161. {
  162. *bgnSector = m_bgnsector;
  163. *endSector = m_endsector;
  164. return true;
  165. }
  166. else if (m_isrom)
  167. {
  168. *bgnSector = 0;
  169. *endSector = 0;
  170. return true;
  171. }
  172. else
  173. {
  174. return m_fsfile.contiguousRange(bgnSector, endSector);
  175. }
  176. }
  177. bool ImageBackingStore::seek(uint64_t pos)
  178. {
  179. uint32_t sectornum = pos / SD_SECTOR_SIZE;
  180. if (m_israw && (uint64_t)sectornum * SD_SECTOR_SIZE != pos)
  181. {
  182. debuglog("---- Unaligned access to image, falling back to SdFat access mode");
  183. m_israw = false;
  184. }
  185. if (m_israw)
  186. {
  187. m_cursector = m_bgnsector + sectornum;
  188. return (m_cursector <= m_endsector);
  189. }
  190. else if (m_isrom)
  191. {
  192. uint32_t sectornum = pos / SD_SECTOR_SIZE;
  193. assert((uint64_t)sectornum * SD_SECTOR_SIZE == pos);
  194. m_cursector = sectornum;
  195. return m_cursector * SD_SECTOR_SIZE < m_romhdr.imagesize;
  196. }
  197. else
  198. {
  199. return m_fsfile.seek(pos);
  200. }
  201. }
  202. ssize_t ImageBackingStore::read(void* buf, size_t count)
  203. {
  204. uint32_t sectorcount = count / SD_SECTOR_SIZE;
  205. if (m_israw && (uint64_t)sectorcount * SD_SECTOR_SIZE != count)
  206. {
  207. debuglog("---- Unaligned access to image, falling back to SdFat access mode");
  208. m_israw = false;
  209. }
  210. if (m_israw && m_blockdev)
  211. {
  212. if (m_blockdev->readSectors(m_cursector, (uint8_t*)buf, sectorcount))
  213. {
  214. m_cursector += sectorcount;
  215. return count;
  216. }
  217. else
  218. {
  219. return -1;
  220. }
  221. }
  222. else if (m_isrom)
  223. {
  224. assert((uint64_t)sectorcount * SD_SECTOR_SIZE == count);
  225. uint32_t start = m_cursector * SD_SECTOR_SIZE;
  226. if (romDriveRead((uint8_t*)buf, start, count))
  227. {
  228. m_cursector += sectorcount;
  229. return count;
  230. }
  231. else
  232. {
  233. return -1;
  234. }
  235. }
  236. else
  237. {
  238. return m_fsfile.read(buf, count);
  239. }
  240. }
  241. ssize_t ImageBackingStore::write(const void* buf, size_t count)
  242. {
  243. uint32_t sectorcount = count / SD_SECTOR_SIZE;
  244. if (m_israw && (uint64_t)sectorcount * SD_SECTOR_SIZE != count)
  245. {
  246. debuglog("---- Unaligned access to image, falling back to SdFat access mode");
  247. m_israw = false;
  248. }
  249. if (m_israw && m_blockdev)
  250. {
  251. if (m_blockdev->writeSectors(m_cursector, (const uint8_t*)buf, sectorcount))
  252. {
  253. m_cursector += sectorcount;
  254. return count;
  255. }
  256. else
  257. {
  258. return 0;
  259. }
  260. }
  261. else if (m_isrom)
  262. {
  263. log("ERROR: attempted to write to ROM drive");
  264. return 0;
  265. }
  266. else if (m_isreadonly_attr)
  267. {
  268. log("ERROR: attempted to write to a read only image");
  269. return 0;
  270. }
  271. else
  272. {
  273. return m_fsfile.write(buf, count);
  274. }
  275. }
  276. void ImageBackingStore::flush()
  277. {
  278. if (!m_israw && !m_isrom && !m_isreadonly_attr)
  279. {
  280. m_fsfile.flush();
  281. }
  282. }
  283. void ImageBackingStore::getName(char * name, size_t len)
  284. {
  285. if(m_isrom)
  286. name = (char*)"ROM:";
  287. else if(m_israw)
  288. name = (char*)"RAW:";
  289. else
  290. m_fsfile.getName(name, len);
  291. }
  292. uint64_t ImageBackingStore::position()
  293. {
  294. if (!m_israw && !m_isrom)
  295. {
  296. return m_fsfile.curPosition();
  297. }
  298. else
  299. {
  300. return 0;
  301. }
  302. }