My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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