SdVolume.cpp 12 KB

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