My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SdVolume.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Arduino SdFat Library
  2. * Copyright (C) 2009 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include "Marlin.h"
  21. #if ENABLED(SDSUPPORT)
  22. #include "SdVolume.h"
  23. //------------------------------------------------------------------------------
  24. #if !USE_MULTIPLE_CARDS
  25. // raw block cache
  26. uint32_t SdVolume::cacheBlockNumber_; // current block number
  27. cache_t SdVolume::cacheBuffer_; // 512 byte cache for Sd2Card
  28. Sd2Card* SdVolume::sdCard_; // pointer to SD card object
  29. bool SdVolume::cacheDirty_; // cacheFlush() will write block if true
  30. uint32_t SdVolume::cacheMirrorBlock_; // mirror block for second FAT
  31. #endif // USE_MULTIPLE_CARDS
  32. //------------------------------------------------------------------------------
  33. // find a contiguous group of clusters
  34. bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
  35. // start of group
  36. uint32_t bgnCluster;
  37. // end of group
  38. uint32_t endCluster;
  39. // last cluster of FAT
  40. uint32_t fatEnd = clusterCount_ + 1;
  41. // flag to save place to start next search
  42. bool setStart;
  43. // set search start cluster
  44. if (*curCluster) {
  45. // try to make file contiguous
  46. bgnCluster = *curCluster + 1;
  47. // don't save new start location
  48. setStart = false;
  49. }
  50. else {
  51. // start at likely place for free cluster
  52. bgnCluster = allocSearchStart_;
  53. // save next search start if one cluster
  54. setStart = count == 1;
  55. }
  56. // end of group
  57. endCluster = bgnCluster;
  58. // search the FAT for free clusters
  59. for (uint32_t n = 0;; n++, endCluster++) {
  60. // can't find space checked all clusters
  61. if (n >= clusterCount_) goto fail;
  62. // past end - start from beginning of FAT
  63. if (endCluster > fatEnd) {
  64. bgnCluster = endCluster = 2;
  65. }
  66. uint32_t f;
  67. if (!fatGet(endCluster, &f)) goto fail;
  68. if (f != 0) {
  69. // cluster in use try next cluster as bgnCluster
  70. bgnCluster = endCluster + 1;
  71. }
  72. else if ((endCluster - bgnCluster + 1) == count) {
  73. // done - found space
  74. break;
  75. }
  76. }
  77. // mark end of chain
  78. if (!fatPutEOC(endCluster)) goto fail;
  79. // link clusters
  80. while (endCluster > bgnCluster) {
  81. if (!fatPut(endCluster - 1, endCluster)) goto fail;
  82. endCluster--;
  83. }
  84. if (*curCluster != 0) {
  85. // connect chains
  86. if (!fatPut(*curCluster, bgnCluster)) goto fail;
  87. }
  88. // return first cluster number to caller
  89. *curCluster = bgnCluster;
  90. // remember possible next free cluster
  91. if (setStart) allocSearchStart_ = bgnCluster + 1;
  92. return true;
  93. fail:
  94. return false;
  95. }
  96. //------------------------------------------------------------------------------
  97. bool SdVolume::cacheFlush() {
  98. if (cacheDirty_) {
  99. if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) {
  100. goto fail;
  101. }
  102. // mirror FAT tables
  103. if (cacheMirrorBlock_) {
  104. if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) {
  105. goto fail;
  106. }
  107. cacheMirrorBlock_ = 0;
  108. }
  109. cacheDirty_ = 0;
  110. }
  111. return true;
  112. fail:
  113. return false;
  114. }
  115. //------------------------------------------------------------------------------
  116. bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
  117. if (cacheBlockNumber_ != blockNumber) {
  118. if (!cacheFlush()) goto fail;
  119. if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) goto fail;
  120. cacheBlockNumber_ = blockNumber;
  121. }
  122. if (dirty) cacheDirty_ = true;
  123. return true;
  124. fail:
  125. return false;
  126. }
  127. //------------------------------------------------------------------------------
  128. // return the size in bytes of a cluster chain
  129. bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
  130. uint32_t s = 0;
  131. do {
  132. if (!fatGet(cluster, &cluster)) goto fail;
  133. s += 512UL << clusterSizeShift_;
  134. } while (!isEOC(cluster));
  135. *size = s;
  136. return true;
  137. fail:
  138. return false;
  139. }
  140. //------------------------------------------------------------------------------
  141. // Fetch a FAT entry
  142. bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
  143. uint32_t lba;
  144. if (cluster > (clusterCount_ + 1)) goto fail;
  145. if (FAT12_SUPPORT && fatType_ == 12) {
  146. uint16_t index = cluster;
  147. index += index >> 1;
  148. lba = fatStartBlock_ + (index >> 9);
  149. if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail;
  150. index &= 0X1FF;
  151. uint16_t tmp = cacheBuffer_.data[index];
  152. index++;
  153. if (index == 512) {
  154. if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) goto fail;
  155. index = 0;
  156. }
  157. tmp |= cacheBuffer_.data[index] << 8;
  158. *value = cluster & 1 ? tmp >> 4 : tmp & 0XFFF;
  159. return true;
  160. }
  161. if (fatType_ == 16) {
  162. lba = fatStartBlock_ + (cluster >> 8);
  163. }
  164. else if (fatType_ == 32) {
  165. lba = fatStartBlock_ + (cluster >> 7);
  166. }
  167. else {
  168. goto fail;
  169. }
  170. if (lba != cacheBlockNumber_) {
  171. if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail;
  172. }
  173. if (fatType_ == 16) {
  174. *value = cacheBuffer_.fat16[cluster & 0XFF];
  175. }
  176. else {
  177. *value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK;
  178. }
  179. return true;
  180. fail:
  181. return false;
  182. }
  183. //------------------------------------------------------------------------------
  184. // Store a FAT entry
  185. bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
  186. uint32_t lba;
  187. // error if reserved cluster
  188. if (cluster < 2) goto fail;
  189. // error if not in FAT
  190. if (cluster > (clusterCount_ + 1)) goto fail;
  191. if (FAT12_SUPPORT && fatType_ == 12) {
  192. uint16_t index = cluster;
  193. index += index >> 1;
  194. lba = fatStartBlock_ + (index >> 9);
  195. if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
  196. // mirror second FAT
  197. if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
  198. index &= 0X1FF;
  199. uint8_t tmp = value;
  200. if (cluster & 1) {
  201. tmp = (cacheBuffer_.data[index] & 0XF) | tmp << 4;
  202. }
  203. cacheBuffer_.data[index] = tmp;
  204. index++;
  205. if (index == 512) {
  206. lba++;
  207. index = 0;
  208. if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
  209. // mirror second FAT
  210. if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
  211. }
  212. tmp = value >> 4;
  213. if (!(cluster & 1)) {
  214. tmp = ((cacheBuffer_.data[index] & 0XF0)) | tmp >> 4;
  215. }
  216. cacheBuffer_.data[index] = tmp;
  217. return true;
  218. }
  219. if (fatType_ == 16) {
  220. lba = fatStartBlock_ + (cluster >> 8);
  221. }
  222. else if (fatType_ == 32) {
  223. lba = fatStartBlock_ + (cluster >> 7);
  224. }
  225. else {
  226. goto fail;
  227. }
  228. if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
  229. // store entry
  230. if (fatType_ == 16) {
  231. cacheBuffer_.fat16[cluster & 0XFF] = value;
  232. }
  233. else {
  234. cacheBuffer_.fat32[cluster & 0X7F] = value;
  235. }
  236. // mirror second FAT
  237. if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
  238. return true;
  239. fail:
  240. return false;
  241. }
  242. //------------------------------------------------------------------------------
  243. // free a cluster chain
  244. bool SdVolume::freeChain(uint32_t cluster) {
  245. uint32_t next;
  246. // clear free cluster location
  247. allocSearchStart_ = 2;
  248. do {
  249. if (!fatGet(cluster, &next)) goto fail;
  250. // free cluster
  251. if (!fatPut(cluster, 0)) goto fail;
  252. cluster = next;
  253. } while (!isEOC(cluster));
  254. return true;
  255. fail:
  256. return false;
  257. }
  258. //------------------------------------------------------------------------------
  259. /** Volume free space in clusters.
  260. *
  261. * \return Count of free clusters for success or -1 if an error occurs.
  262. */
  263. int32_t SdVolume::freeClusterCount() {
  264. uint32_t free = 0;
  265. uint16_t n;
  266. uint32_t todo = clusterCount_ + 2;
  267. if (fatType_ == 16) {
  268. n = 256;
  269. }
  270. else if (fatType_ == 32) {
  271. n = 128;
  272. }
  273. else {
  274. // put FAT12 here
  275. return -1;
  276. }
  277. for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) {
  278. if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1;
  279. if (todo < n) n = todo;
  280. if (fatType_ == 16) {
  281. for (uint16_t i = 0; i < n; i++) {
  282. if (cacheBuffer_.fat16[i] == 0) free++;
  283. }
  284. }
  285. else {
  286. for (uint16_t i = 0; i < n; i++) {
  287. if (cacheBuffer_.fat32[i] == 0) free++;
  288. }
  289. }
  290. }
  291. return free;
  292. }
  293. //------------------------------------------------------------------------------
  294. /** Initialize a FAT volume.
  295. *
  296. * \param[in] dev The SD card where the volume is located.
  297. *
  298. * \param[in] part The partition to be used. Legal values for \a part are
  299. * 1-4 to use the corresponding partition on a device formatted with
  300. * a MBR, Master Boot Record, or zero if the device is formatted as
  301. * a super floppy with the FAT boot sector in block zero.
  302. *
  303. * \return The value one, true, is returned for success and
  304. * the value zero, false, is returned for failure. Reasons for
  305. * failure include not finding a valid partition, not finding a valid
  306. * FAT file system in the specified partition or an I/O error.
  307. */
  308. bool SdVolume::init(Sd2Card* dev, uint8_t part) {
  309. uint32_t totalBlocks;
  310. uint32_t volumeStartBlock = 0;
  311. fat32_boot_t* fbs;
  312. sdCard_ = dev;
  313. fatType_ = 0;
  314. allocSearchStart_ = 2;
  315. cacheDirty_ = 0; // cacheFlush() will write block if true
  316. cacheMirrorBlock_ = 0;
  317. cacheBlockNumber_ = 0XFFFFFFFF;
  318. // if part == 0 assume super floppy with FAT boot sector in block zero
  319. // if part > 0 assume mbr volume with partition table
  320. if (part) {
  321. if (part > 4)goto fail;
  322. if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail;
  323. part_t* p = &cacheBuffer_.mbr.part[part - 1];
  324. if ((p->boot & 0X7F) != 0 ||
  325. p->totalSectors < 100 ||
  326. p->firstSector == 0) {
  327. // not a valid partition
  328. goto fail;
  329. }
  330. volumeStartBlock = p->firstSector;
  331. }
  332. if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail;
  333. fbs = &cacheBuffer_.fbs32;
  334. if (fbs->bytesPerSector != 512 ||
  335. fbs->fatCount == 0 ||
  336. fbs->reservedSectorCount == 0 ||
  337. fbs->sectorsPerCluster == 0) {
  338. // not valid FAT volume
  339. goto fail;
  340. }
  341. fatCount_ = fbs->fatCount;
  342. blocksPerCluster_ = fbs->sectorsPerCluster;
  343. // determine shift that is same as multiply by blocksPerCluster_
  344. clusterSizeShift_ = 0;
  345. while (blocksPerCluster_ != BIT(clusterSizeShift_)) {
  346. // error if not power of 2
  347. if (clusterSizeShift_++ > 7) goto fail;
  348. }
  349. blocksPerFat_ = fbs->sectorsPerFat16 ?
  350. fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
  351. fatStartBlock_ = volumeStartBlock + fbs->reservedSectorCount;
  352. // count for FAT16 zero for FAT32
  353. rootDirEntryCount_ = fbs->rootDirEntryCount;
  354. // directory start for FAT16 dataStart for FAT32
  355. rootDirStart_ = fatStartBlock_ + fbs->fatCount * blocksPerFat_;
  356. // data start for FAT16 and FAT32
  357. dataStartBlock_ = rootDirStart_ + ((32 * fbs->rootDirEntryCount + 511) / 512);
  358. // total blocks for FAT16 or FAT32
  359. totalBlocks = fbs->totalSectors16 ?
  360. fbs->totalSectors16 : fbs->totalSectors32;
  361. // total data blocks
  362. clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
  363. // divide by cluster size to get cluster count
  364. clusterCount_ >>= clusterSizeShift_;
  365. // FAT type is determined by cluster count
  366. if (clusterCount_ < 4085) {
  367. fatType_ = 12;
  368. if (!FAT12_SUPPORT) goto fail;
  369. }
  370. else if (clusterCount_ < 65525) {
  371. fatType_ = 16;
  372. }
  373. else {
  374. rootDirStart_ = fbs->fat32RootCluster;
  375. fatType_ = 32;
  376. }
  377. return true;
  378. fail:
  379. return false;
  380. }
  381. #endif