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

SdVolume.cpp 12KB

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