No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

596 líneas
16KB

  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 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. //------------------------------------------------------------------------------
  22. #if !USE_MULTIPLE_CARDS
  23. // raw block cache
  24. uint8_t SdVolume::m_fatCount; // number of FATs on volume
  25. uint32_t SdVolume::m_blocksPerFat; // FAT size in blocks
  26. cache_t SdVolume::m_cacheBuffer; // 512 byte cache for Sd2Card
  27. uint32_t SdVolume::m_cacheBlockNumber; // current block number
  28. uint8_t SdVolume::m_cacheStatus; // status of cache block
  29. #if USE_SEPARATE_FAT_CACHE
  30. cache_t SdVolume::m_cacheFatBuffer; // 512 byte cache for FAT
  31. uint32_t SdVolume::m_cacheFatBlockNumber; // current Fat block number
  32. uint8_t SdVolume::m_cacheFatStatus; // status of cache Fatblock
  33. #endif // USE_SEPARATE_FAT_CACHE
  34. Sd2Card* SdVolume::m_sdCard; // pointer to SD card object
  35. #endif // USE_MULTIPLE_CARDS
  36. //------------------------------------------------------------------------------
  37. // find a contiguous group of clusters
  38. bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
  39. // start of group
  40. uint32_t bgnCluster;
  41. // end of group
  42. uint32_t endCluster;
  43. // last cluster of FAT
  44. uint32_t fatEnd = m_clusterCount + 1;
  45. // flag to save place to start next search
  46. bool setStart;
  47. // set search start cluster
  48. if (*curCluster) {
  49. // try to make file contiguous
  50. bgnCluster = *curCluster + 1;
  51. // don't save new start location
  52. setStart = false;
  53. } else {
  54. // start at likely place for free cluster
  55. bgnCluster = m_allocSearchStart;
  56. // save next search start if no holes.
  57. setStart = true;
  58. }
  59. // end of group
  60. endCluster = bgnCluster;
  61. // search the FAT for free clusters
  62. for (uint32_t n = 0;; n++, endCluster++) {
  63. // can't find space checked all clusters
  64. if (n >= m_clusterCount) {
  65. DBG_FAIL_MACRO;
  66. goto fail;
  67. }
  68. // past end - start from beginning of FAT
  69. if (endCluster > fatEnd) {
  70. bgnCluster = endCluster = 2;
  71. }
  72. uint32_t f;
  73. if (!fatGet(endCluster, &f)) {
  74. DBG_FAIL_MACRO;
  75. goto fail;
  76. }
  77. if (f != 0) {
  78. // don't update search start if unallocated clusters before endCluster.
  79. if (bgnCluster != endCluster) setStart = false;
  80. // cluster in use try next cluster as bgnCluster
  81. bgnCluster = endCluster + 1;
  82. } else if ((endCluster - bgnCluster + 1) == count) {
  83. // done - found space
  84. break;
  85. }
  86. }
  87. // remember possible next free cluster
  88. if (setStart) m_allocSearchStart = endCluster + 1;
  89. // mark end of chain
  90. if (!fatPutEOC(endCluster)) {
  91. DBG_FAIL_MACRO;
  92. goto fail;
  93. }
  94. // link clusters
  95. while (endCluster > bgnCluster) {
  96. if (!fatPut(endCluster - 1, endCluster)) {
  97. DBG_FAIL_MACRO;
  98. goto fail;
  99. }
  100. endCluster--;
  101. }
  102. if (*curCluster != 0) {
  103. // connect chains
  104. if (!fatPut(*curCluster, bgnCluster)) {
  105. DBG_FAIL_MACRO;
  106. goto fail;
  107. }
  108. }
  109. // return first cluster number to caller
  110. *curCluster = bgnCluster;
  111. return true;
  112. fail:
  113. return false;
  114. }
  115. //==============================================================================
  116. // cache functions
  117. #if USE_SEPARATE_FAT_CACHE
  118. //------------------------------------------------------------------------------
  119. cache_t* SdVolume::cacheFetch(uint32_t blockNumber, uint8_t options) {
  120. return cacheFetchData(blockNumber, options);
  121. }
  122. //------------------------------------------------------------------------------
  123. cache_t* SdVolume::cacheFetchData(uint32_t blockNumber, uint8_t options) {
  124. if (m_cacheBlockNumber != blockNumber) {
  125. if (!cacheWriteData()) {
  126. DBG_FAIL_MACRO;
  127. goto fail;
  128. }
  129. if (!(options & CACHE_OPTION_NO_READ)) {
  130. if (!m_sdCard->readBlock(blockNumber, m_cacheBuffer.data)) {
  131. DBG_FAIL_MACRO;
  132. goto fail;
  133. }
  134. }
  135. m_cacheStatus = 0;
  136. m_cacheBlockNumber = blockNumber;
  137. }
  138. m_cacheStatus |= options & CACHE_STATUS_MASK;
  139. return &m_cacheBuffer;
  140. fail:
  141. return 0;
  142. }
  143. //------------------------------------------------------------------------------
  144. cache_t* SdVolume::cacheFetchFat(uint32_t blockNumber, uint8_t options) {
  145. if (m_cacheFatBlockNumber != blockNumber) {
  146. if (!cacheWriteFat()) {
  147. DBG_FAIL_MACRO;
  148. goto fail;
  149. }
  150. if (!(options & CACHE_OPTION_NO_READ)) {
  151. if (!m_sdCard->readBlock(blockNumber, m_cacheFatBuffer.data)) {
  152. DBG_FAIL_MACRO;
  153. goto fail;
  154. }
  155. }
  156. m_cacheFatStatus = 0;
  157. m_cacheFatBlockNumber = blockNumber;
  158. }
  159. m_cacheFatStatus |= options & CACHE_STATUS_MASK;
  160. return &m_cacheFatBuffer;
  161. fail:
  162. return 0;
  163. }
  164. //------------------------------------------------------------------------------
  165. bool SdVolume::cacheSync() {
  166. return cacheWriteData() && cacheWriteFat();
  167. }
  168. //------------------------------------------------------------------------------
  169. bool SdVolume::cacheWriteData() {
  170. if (m_cacheStatus & CACHE_STATUS_DIRTY) {
  171. if (!m_sdCard->writeBlock(m_cacheBlockNumber, m_cacheBuffer.data)) {
  172. DBG_FAIL_MACRO;
  173. goto fail;
  174. }
  175. m_cacheStatus &= ~CACHE_STATUS_DIRTY;
  176. }
  177. return true;
  178. fail:
  179. return false;
  180. }
  181. //------------------------------------------------------------------------------
  182. bool SdVolume::cacheWriteFat() {
  183. if (m_cacheFatStatus & CACHE_STATUS_DIRTY) {
  184. if (!m_sdCard->writeBlock(m_cacheFatBlockNumber, m_cacheFatBuffer.data)) {
  185. DBG_FAIL_MACRO;
  186. goto fail;
  187. }
  188. // mirror second FAT
  189. if (m_fatCount > 1) {
  190. uint32_t lbn = m_cacheFatBlockNumber + m_blocksPerFat;
  191. if (!m_sdCard->writeBlock(lbn, m_cacheFatBuffer.data)) {
  192. DBG_FAIL_MACRO;
  193. goto fail;
  194. }
  195. }
  196. m_cacheFatStatus &= ~CACHE_STATUS_DIRTY;
  197. }
  198. return true;
  199. fail:
  200. return false;
  201. }
  202. #else // USE_SEPARATE_FAT_CACHE
  203. //------------------------------------------------------------------------------
  204. cache_t* SdVolume::cacheFetch(uint32_t blockNumber, uint8_t options) {
  205. if (m_cacheBlockNumber != blockNumber) {
  206. if (!cacheSync()) {
  207. DBG_FAIL_MACRO;
  208. goto fail;
  209. }
  210. if (!(options & CACHE_OPTION_NO_READ)) {
  211. if (!m_sdCard->readBlock(blockNumber, m_cacheBuffer.data)) {
  212. DBG_FAIL_MACRO;
  213. goto fail;
  214. }
  215. }
  216. m_cacheStatus = 0;
  217. m_cacheBlockNumber = blockNumber;
  218. }
  219. m_cacheStatus |= options & CACHE_STATUS_MASK;
  220. return &m_cacheBuffer;
  221. fail:
  222. return 0;
  223. }
  224. //------------------------------------------------------------------------------
  225. cache_t* SdVolume::cacheFetchFat(uint32_t blockNumber, uint8_t options) {
  226. return cacheFetch(blockNumber, options | CACHE_STATUS_FAT_BLOCK);
  227. }
  228. //------------------------------------------------------------------------------
  229. bool SdVolume::cacheSync() {
  230. if (m_cacheStatus & CACHE_STATUS_DIRTY) {
  231. if (!m_sdCard->writeBlock(m_cacheBlockNumber, m_cacheBuffer.data)) {
  232. DBG_FAIL_MACRO;
  233. goto fail;
  234. }
  235. // mirror second FAT
  236. if ((m_cacheStatus & CACHE_STATUS_FAT_BLOCK) && m_fatCount > 1) {
  237. uint32_t lbn = m_cacheBlockNumber + m_blocksPerFat;
  238. if (!m_sdCard->writeBlock(lbn, m_cacheBuffer.data)) {
  239. DBG_FAIL_MACRO;
  240. goto fail;
  241. }
  242. }
  243. m_cacheStatus &= ~CACHE_STATUS_DIRTY;
  244. }
  245. return true;
  246. fail:
  247. return false;
  248. }
  249. //------------------------------------------------------------------------------
  250. bool SdVolume::cacheWriteData() {
  251. return cacheSync();
  252. }
  253. #endif // USE_SEPARATE_FAT_CACHE
  254. //------------------------------------------------------------------------------
  255. void SdVolume::cacheInvalidate() {
  256. m_cacheBlockNumber = 0XFFFFFFFF;
  257. m_cacheStatus = 0;
  258. }
  259. //==============================================================================
  260. //------------------------------------------------------------------------------
  261. uint32_t SdVolume::clusterStartBlock(uint32_t cluster) const {
  262. return m_dataStartBlock + ((cluster - 2) << m_clusterSizeShift);
  263. }
  264. //------------------------------------------------------------------------------
  265. // Fetch a FAT entry
  266. bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
  267. uint32_t lba;
  268. cache_t* pc;
  269. // error if reserved cluster of beyond FAT
  270. if (cluster < 2 || cluster > (m_clusterCount + 1)) {
  271. DBG_FAIL_MACRO;
  272. goto fail;
  273. }
  274. if (FAT12_SUPPORT && m_fatType == 12) {
  275. uint16_t index = cluster;
  276. index += index >> 1;
  277. lba = m_fatStartBlock + (index >> 9);
  278. pc = cacheFetchFat(lba, CACHE_FOR_READ);
  279. if (!pc) {
  280. DBG_FAIL_MACRO;
  281. goto fail;
  282. }
  283. index &= 0X1FF;
  284. uint16_t tmp = pc->data[index];
  285. index++;
  286. if (index == 512) {
  287. pc = cacheFetchFat(lba + 1, CACHE_FOR_READ);
  288. if (!pc) {
  289. DBG_FAIL_MACRO;
  290. goto fail;
  291. }
  292. index = 0;
  293. }
  294. tmp |= pc->data[index] << 8;
  295. *value = cluster & 1 ? tmp >> 4 : tmp & 0XFFF;
  296. return true;
  297. }
  298. if (m_fatType == 16) {
  299. lba = m_fatStartBlock + (cluster >> 8);
  300. } else if (m_fatType == 32) {
  301. lba = m_fatStartBlock + (cluster >> 7);
  302. } else {
  303. DBG_FAIL_MACRO;
  304. goto fail;
  305. }
  306. pc = cacheFetchFat(lba, CACHE_FOR_READ);
  307. if (!pc) {
  308. DBG_FAIL_MACRO;
  309. goto fail;
  310. }
  311. if (m_fatType == 16) {
  312. *value = pc->fat16[cluster & 0XFF];
  313. } else {
  314. *value = pc->fat32[cluster & 0X7F] & FAT32MASK;
  315. }
  316. return true;
  317. fail:
  318. return false;
  319. }
  320. //------------------------------------------------------------------------------
  321. // Store a FAT entry
  322. bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
  323. uint32_t lba;
  324. cache_t* pc;
  325. // error if reserved cluster of beyond FAT
  326. if (cluster < 2 || cluster > (m_clusterCount + 1)) {
  327. DBG_FAIL_MACRO;
  328. goto fail;
  329. }
  330. if (FAT12_SUPPORT && m_fatType == 12) {
  331. uint16_t index = cluster;
  332. index += index >> 1;
  333. lba = m_fatStartBlock + (index >> 9);
  334. pc = cacheFetchFat(lba, CACHE_FOR_WRITE);
  335. if (!pc) {
  336. DBG_FAIL_MACRO;
  337. goto fail;
  338. }
  339. index &= 0X1FF;
  340. uint8_t tmp = value;
  341. if (cluster & 1) {
  342. tmp = (pc->data[index] & 0XF) | tmp << 4;
  343. }
  344. pc->data[index] = tmp;
  345. index++;
  346. if (index == 512) {
  347. lba++;
  348. index = 0;
  349. pc = cacheFetchFat(lba, CACHE_FOR_WRITE);
  350. if (!pc) {
  351. DBG_FAIL_MACRO;
  352. goto fail;
  353. }
  354. }
  355. tmp = value >> 4;
  356. if (!(cluster & 1)) {
  357. tmp = ((pc->data[index] & 0XF0)) | tmp >> 4;
  358. }
  359. pc->data[index] = tmp;
  360. return true;
  361. }
  362. if (m_fatType == 16) {
  363. lba = m_fatStartBlock + (cluster >> 8);
  364. } else if (m_fatType == 32) {
  365. lba = m_fatStartBlock + (cluster >> 7);
  366. } else {
  367. DBG_FAIL_MACRO;
  368. goto fail;
  369. }
  370. pc = cacheFetchFat(lba, CACHE_FOR_WRITE);
  371. if (!pc) {
  372. DBG_FAIL_MACRO;
  373. goto fail;
  374. }
  375. // store entry
  376. if (m_fatType == 16) {
  377. pc->fat16[cluster & 0XFF] = value;
  378. } else {
  379. pc->fat32[cluster & 0X7F] = value;
  380. }
  381. return true;
  382. fail:
  383. return false;
  384. }
  385. //------------------------------------------------------------------------------
  386. // free a cluster chain
  387. bool SdVolume::freeChain(uint32_t cluster) {
  388. uint32_t next;
  389. do {
  390. if (!fatGet(cluster, &next)) {
  391. DBG_FAIL_MACRO;
  392. goto fail;
  393. }
  394. // free cluster
  395. if (!fatPut(cluster, 0)) {
  396. DBG_FAIL_MACRO;
  397. goto fail;
  398. }
  399. if (cluster < m_allocSearchStart) m_allocSearchStart = cluster;
  400. cluster = next;
  401. } while (!isEOC(cluster));
  402. return true;
  403. fail:
  404. return false;
  405. }
  406. //------------------------------------------------------------------------------
  407. /** Volume free space in clusters.
  408. *
  409. * \return Count of free clusters for success or -1 if an error occurs.
  410. */
  411. int32_t SdVolume::freeClusterCount() {
  412. uint32_t free = 0;
  413. uint32_t lba;
  414. uint32_t todo = m_clusterCount + 2;
  415. uint16_t n;
  416. if (FAT12_SUPPORT && m_fatType == 12) {
  417. for (unsigned i = 2; i < todo; i++) {
  418. uint32_t c;
  419. if (!fatGet(i, &c)) {
  420. DBG_FAIL_MACRO;
  421. goto fail;
  422. }
  423. if (c == 0) free++;
  424. }
  425. } else if (m_fatType == 16 || m_fatType == 32) {
  426. lba = m_fatStartBlock;
  427. while (todo) {
  428. cache_t* pc = cacheFetchFat(lba++, CACHE_FOR_READ);
  429. if (!pc) {
  430. DBG_FAIL_MACRO;
  431. goto fail;
  432. }
  433. n = m_fatType == 16 ? 256 : 128;
  434. if (todo < n) n = todo;
  435. if (m_fatType == 16) {
  436. for (uint16_t i = 0; i < n; i++) {
  437. if (pc->fat16[i] == 0) free++;
  438. }
  439. } else {
  440. for (uint16_t i = 0; i < n; i++) {
  441. if (pc->fat32[i] == 0) free++;
  442. }
  443. }
  444. todo -= n;
  445. }
  446. } else {
  447. // invalid FAT type
  448. DBG_FAIL_MACRO;
  449. goto fail;
  450. }
  451. return free;
  452. fail:
  453. return -1;
  454. }
  455. //------------------------------------------------------------------------------
  456. /** Initialize a FAT volume.
  457. *
  458. * \param[in] dev The SD card where the volume is located.
  459. *
  460. * \param[in] part The partition to be used. Legal values for \a part are
  461. * 1-4 to use the corresponding partition on a device formatted with
  462. * a MBR, Master Boot Record, or zero if the device is formatted as
  463. * a super floppy with the FAT boot sector in block zero.
  464. *
  465. * \return The value one, true, is returned for success and
  466. * the value zero, false, is returned for failure. Reasons for
  467. * failure include not finding a valid partition, not finding a valid
  468. * FAT file system in the specified partition or an I/O error.
  469. */
  470. bool SdVolume::init(Sd2Card* dev, uint8_t part) {
  471. uint8_t tmp;
  472. uint32_t totalBlocks;
  473. uint32_t volumeStartBlock = 0;
  474. fat32_boot_t* fbs;
  475. cache_t* pc;
  476. m_sdCard = dev;
  477. m_fatType = 0;
  478. m_allocSearchStart = 2;
  479. m_cacheStatus = 0; // cacheSync() will write block if true
  480. m_cacheBlockNumber = 0XFFFFFFFF;
  481. #if USE_SEPARATE_FAT_CACHE
  482. m_cacheFatStatus = 0; // cacheSync() will write block if true
  483. m_cacheFatBlockNumber = 0XFFFFFFFF;
  484. #endif // USE_SEPARATE_FAT_CACHE
  485. // if part == 0 assume super floppy with FAT boot sector in block zero
  486. // if part > 0 assume mbr volume with partition table
  487. if (part) {
  488. if (part > 4) {
  489. DBG_FAIL_MACRO;
  490. goto fail;
  491. }
  492. pc = cacheFetch(volumeStartBlock, CACHE_FOR_READ);
  493. if (!pc) {
  494. DBG_FAIL_MACRO;
  495. goto fail;
  496. }
  497. part_t* p = &pc->mbr.part[part-1];
  498. if ((p->boot & 0X7F) != 0 || p->firstSector == 0) {
  499. // not a valid partition
  500. DBG_FAIL_MACRO;
  501. goto fail;
  502. }
  503. volumeStartBlock = p->firstSector;
  504. }
  505. pc = cacheFetch(volumeStartBlock, CACHE_FOR_READ);
  506. if (!pc) {
  507. DBG_FAIL_MACRO;
  508. goto fail;
  509. }
  510. fbs = &(pc->fbs32);
  511. if (fbs->bytesPerSector != 512 ||
  512. fbs->fatCount == 0 ||
  513. fbs->reservedSectorCount == 0) {
  514. // not valid FAT volume
  515. DBG_FAIL_MACRO;
  516. goto fail;
  517. }
  518. m_fatCount = fbs->fatCount;
  519. m_blocksPerCluster = fbs->sectorsPerCluster;
  520. m_clusterBlockMask = m_blocksPerCluster - 1;
  521. // determine shift that is same as multiply by m_blocksPerCluster
  522. m_clusterSizeShift = 0;
  523. for (tmp = 1; m_blocksPerCluster != tmp; m_clusterSizeShift++) {
  524. tmp <<= 1;
  525. if (tmp == 0) {
  526. DBG_FAIL_MACRO;
  527. goto fail;
  528. }
  529. }
  530. m_blocksPerFat = fbs->sectorsPerFat16 ?
  531. fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
  532. m_fatStartBlock = volumeStartBlock + fbs->reservedSectorCount;
  533. // count for FAT16 zero for FAT32
  534. m_rootDirEntryCount = fbs->rootDirEntryCount;
  535. // directory start for FAT16 dataStart for FAT32
  536. m_rootDirStart = m_fatStartBlock + fbs->fatCount * m_blocksPerFat;
  537. // data start for FAT16 and FAT32
  538. m_dataStartBlock = m_rootDirStart + ((32 * fbs->rootDirEntryCount + 511)/512);
  539. // total blocks for FAT16 or FAT32
  540. totalBlocks = fbs->totalSectors16 ?
  541. fbs->totalSectors16 : fbs->totalSectors32;
  542. // total data blocks
  543. m_clusterCount = totalBlocks - (m_dataStartBlock - volumeStartBlock);
  544. // divide by cluster size to get cluster count
  545. m_clusterCount >>= m_clusterSizeShift;
  546. // FAT type is determined by cluster count
  547. if (m_clusterCount < 4085) {
  548. m_fatType = 12;
  549. if (!FAT12_SUPPORT) {
  550. DBG_FAIL_MACRO;
  551. goto fail;
  552. }
  553. } else if (m_clusterCount < 65525) {
  554. m_fatType = 16;
  555. } else {
  556. m_rootDirStart = fbs->fat32RootCluster;
  557. m_fatType = 32;
  558. }
  559. return true;
  560. fail:
  561. return false;
  562. }