FatPartition.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. * Copyright (c) 2011-2022 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <string.h>
  26. #define DBG_FILE "FatPartition.cpp"
  27. #include "../common/DebugMacros.h"
  28. #include "../common/PartitionTable.h"
  29. #include "FatLib.h"
  30. //------------------------------------------------------------------------------
  31. bool FatPartition::allocateCluster(uint32_t current, uint32_t* next) {
  32. uint32_t find;
  33. bool setStart;
  34. if (m_allocSearchStart < current) {
  35. // Try to keep file contiguous. Start just after current cluster.
  36. find = current;
  37. setStart = false;
  38. } else {
  39. find = m_allocSearchStart;
  40. setStart = true;
  41. }
  42. while (1) {
  43. find++;
  44. if (find > m_lastCluster) {
  45. if (setStart) {
  46. // Can't find space, checked all clusters.
  47. DBG_FAIL_MACRO;
  48. goto fail;
  49. }
  50. find = m_allocSearchStart;
  51. setStart = true;
  52. continue;
  53. }
  54. if (find == current) {
  55. // Can't find space, already searched clusters after current.
  56. DBG_FAIL_MACRO;
  57. goto fail;
  58. }
  59. uint32_t f;
  60. int8_t fg = fatGet(find, &f);
  61. if (fg < 0) {
  62. DBG_FAIL_MACRO;
  63. goto fail;
  64. }
  65. if (fg && f == 0) {
  66. break;
  67. }
  68. }
  69. if (setStart) {
  70. m_allocSearchStart = find;
  71. }
  72. // Mark end of chain.
  73. if (!fatPutEOC(find)) {
  74. DBG_FAIL_MACRO;
  75. goto fail;
  76. }
  77. if (current) {
  78. // Link clusters.
  79. if (!fatPut(current, find)) {
  80. DBG_FAIL_MACRO;
  81. goto fail;
  82. }
  83. }
  84. updateFreeClusterCount(-1);
  85. *next = find;
  86. return true;
  87. fail:
  88. return false;
  89. }
  90. //------------------------------------------------------------------------------
  91. // find a contiguous group of clusters
  92. bool FatPartition::allocContiguous(uint32_t count, uint32_t* firstCluster) {
  93. // flag to save place to start next search
  94. bool setStart = true;
  95. // start of group
  96. uint32_t bgnCluster;
  97. // end of group
  98. uint32_t endCluster;
  99. // Start at cluster after last allocated cluster.
  100. endCluster = bgnCluster = m_allocSearchStart + 1;
  101. // search the FAT for free clusters
  102. while (1) {
  103. if (endCluster > m_lastCluster) {
  104. // Can't find space.
  105. DBG_FAIL_MACRO;
  106. goto fail;
  107. }
  108. uint32_t f;
  109. int8_t fg = fatGet(endCluster, &f);
  110. if (fg < 0) {
  111. DBG_FAIL_MACRO;
  112. goto fail;
  113. }
  114. if (f || fg == 0) {
  115. // don't update search start if unallocated clusters before endCluster.
  116. if (bgnCluster != endCluster) {
  117. setStart = false;
  118. }
  119. // cluster in use try next cluster as bgnCluster
  120. bgnCluster = endCluster + 1;
  121. } else if ((endCluster - bgnCluster + 1) == count) {
  122. // done - found space
  123. break;
  124. }
  125. endCluster++;
  126. }
  127. // Remember possible next free cluster.
  128. if (setStart) {
  129. m_allocSearchStart = endCluster;
  130. }
  131. // mark end of chain
  132. if (!fatPutEOC(endCluster)) {
  133. DBG_FAIL_MACRO;
  134. goto fail;
  135. }
  136. // link clusters
  137. while (endCluster > bgnCluster) {
  138. if (!fatPut(endCluster - 1, endCluster)) {
  139. DBG_FAIL_MACRO;
  140. goto fail;
  141. }
  142. endCluster--;
  143. }
  144. // Maintain count of free clusters.
  145. updateFreeClusterCount(-count);
  146. // return first cluster number to caller
  147. *firstCluster = bgnCluster;
  148. return true;
  149. fail:
  150. return false;
  151. }
  152. //------------------------------------------------------------------------------
  153. // Fetch a FAT entry - return -1 error, 0 EOC, else 1.
  154. int8_t FatPartition::fatGet(uint32_t cluster, uint32_t* value) {
  155. uint32_t sector;
  156. uint32_t next;
  157. uint8_t* pc;
  158. // error if reserved cluster of beyond FAT
  159. if (cluster < 2 || cluster > m_lastCluster) {
  160. DBG_FAIL_MACRO;
  161. goto fail;
  162. }
  163. if (fatType() == 32) {
  164. sector = m_fatStartSector + (cluster >> (m_bytesPerSectorShift - 2));
  165. pc = fatCachePrepare(sector, FsCache::CACHE_FOR_READ);
  166. if (!pc) {
  167. DBG_FAIL_MACRO;
  168. goto fail;
  169. }
  170. uint16_t offset = (cluster << 2) & m_sectorMask;
  171. next = getLe32(pc + offset);
  172. } else if (fatType() == 16) {
  173. cluster &= 0XFFFF;
  174. sector = m_fatStartSector + (cluster >> (m_bytesPerSectorShift - 1) );
  175. pc = fatCachePrepare(sector, FsCache::CACHE_FOR_READ);
  176. if (!pc) {
  177. DBG_FAIL_MACRO;
  178. goto fail;
  179. }
  180. uint16_t offset = (cluster << 1) & m_sectorMask;
  181. next = getLe16(pc + offset);
  182. } else if (FAT12_SUPPORT && fatType() == 12) {
  183. uint16_t index = cluster;
  184. index += index >> 1;
  185. sector = m_fatStartSector + (index >> m_bytesPerSectorShift);
  186. pc = fatCachePrepare(sector, FsCache::CACHE_FOR_READ);
  187. if (!pc) {
  188. DBG_FAIL_MACRO;
  189. goto fail;
  190. }
  191. index &= m_sectorMask;
  192. uint16_t tmp = pc[index];
  193. index++;
  194. if (index == m_bytesPerSector) {
  195. pc = fatCachePrepare(sector + 1, FsCache::CACHE_FOR_READ);
  196. if (!pc) {
  197. DBG_FAIL_MACRO;
  198. goto fail;
  199. }
  200. index = 0;
  201. }
  202. tmp |= pc[index] << 8;
  203. next = cluster & 1 ? tmp >> 4 : tmp & 0XFFF;
  204. } else {
  205. DBG_FAIL_MACRO;
  206. goto fail;
  207. }
  208. if (isEOC(next)) {
  209. return 0;
  210. }
  211. *value = next;
  212. return 1;
  213. fail:
  214. return -1;
  215. }
  216. //------------------------------------------------------------------------------
  217. // Store a FAT entry
  218. bool FatPartition::fatPut(uint32_t cluster, uint32_t value) {
  219. uint32_t sector;
  220. uint8_t* pc;
  221. // error if reserved cluster of beyond FAT
  222. if (cluster < 2 || cluster > m_lastCluster) {
  223. DBG_FAIL_MACRO;
  224. goto fail;
  225. }
  226. if (fatType() == 32) {
  227. sector = m_fatStartSector + (cluster >> (m_bytesPerSectorShift - 2));
  228. pc = fatCachePrepare(sector, FsCache::CACHE_FOR_WRITE);
  229. if (!pc) {
  230. DBG_FAIL_MACRO;
  231. goto fail;
  232. }
  233. uint16_t offset = (cluster << 2) & m_sectorMask;
  234. setLe32(pc + offset, value);
  235. return true;
  236. }
  237. if (fatType() == 16) {
  238. cluster &= 0XFFFF;
  239. sector = m_fatStartSector + (cluster >> (m_bytesPerSectorShift - 1) );
  240. pc = fatCachePrepare(sector, FsCache::CACHE_FOR_WRITE);
  241. if (!pc) {
  242. DBG_FAIL_MACRO;
  243. goto fail;
  244. }
  245. uint16_t offset = (cluster << 1) & m_sectorMask;
  246. setLe16(pc + offset, value);
  247. return true;
  248. }
  249. if (FAT12_SUPPORT && fatType() == 12) {
  250. uint16_t index = cluster;
  251. index += index >> 1;
  252. sector = m_fatStartSector + (index >> m_bytesPerSectorShift);
  253. pc = fatCachePrepare(sector, FsCache::CACHE_FOR_WRITE);
  254. if (!pc) {
  255. DBG_FAIL_MACRO;
  256. goto fail;
  257. }
  258. index &= m_sectorMask;
  259. uint8_t tmp = value;
  260. if (cluster & 1) {
  261. tmp = (pc[index] & 0XF) | tmp << 4;
  262. }
  263. pc[index] = tmp;
  264. index++;
  265. if (index == m_bytesPerSector) {
  266. sector++;
  267. index = 0;
  268. pc = fatCachePrepare(sector, FsCache::CACHE_FOR_WRITE);
  269. if (!pc) {
  270. DBG_FAIL_MACRO;
  271. goto fail;
  272. }
  273. }
  274. tmp = value >> 4;
  275. if (!(cluster & 1)) {
  276. tmp = ((pc[index] & 0XF0)) | tmp >> 4;
  277. }
  278. pc[index] = tmp;
  279. return true;
  280. } else {
  281. DBG_FAIL_MACRO;
  282. goto fail;
  283. }
  284. fail:
  285. return false;
  286. }
  287. //------------------------------------------------------------------------------
  288. // free a cluster chain
  289. bool FatPartition::freeChain(uint32_t cluster) {
  290. uint32_t next;
  291. int8_t fg;
  292. do {
  293. fg = fatGet(cluster, &next);
  294. if (fg < 0) {
  295. DBG_FAIL_MACRO;
  296. goto fail;
  297. }
  298. // free cluster
  299. if (!fatPut(cluster, 0)) {
  300. DBG_FAIL_MACRO;
  301. goto fail;
  302. }
  303. // Add one to count of free clusters.
  304. updateFreeClusterCount(1);
  305. if (cluster < m_allocSearchStart) {
  306. m_allocSearchStart = cluster - 1;
  307. }
  308. cluster = next;
  309. } while (fg);
  310. return true;
  311. fail:
  312. return false;
  313. }
  314. //------------------------------------------------------------------------------
  315. int32_t FatPartition::freeClusterCount() {
  316. #if MAINTAIN_FREE_CLUSTER_COUNT
  317. if (m_freeClusterCount >= 0) {
  318. return m_freeClusterCount;
  319. }
  320. #endif // MAINTAIN_FREE_CLUSTER_COUNT
  321. uint32_t free = 0;
  322. uint32_t sector;
  323. uint32_t todo = m_lastCluster + 1;
  324. uint16_t n;
  325. if (FAT12_SUPPORT && fatType() == 12) {
  326. for (unsigned i = 2; i < todo; i++) {
  327. uint32_t c;
  328. int8_t fg = fatGet(i, &c);
  329. if (fg < 0) {
  330. DBG_FAIL_MACRO;
  331. goto fail;
  332. }
  333. if (fg && c == 0) {
  334. free++;
  335. }
  336. }
  337. } else if (fatType() == 16 || fatType() == 32) {
  338. sector = m_fatStartSector;
  339. while (todo) {
  340. uint8_t* pc = fatCachePrepare(sector++, FsCache::CACHE_FOR_READ);
  341. if (!pc) {
  342. DBG_FAIL_MACRO;
  343. goto fail;
  344. }
  345. n = fatType() == 16 ? m_bytesPerSector/2 : m_bytesPerSector/4;
  346. if (todo < n) {
  347. n = todo;
  348. }
  349. if (fatType() == 16) {
  350. uint16_t* p16 = reinterpret_cast<uint16_t*>(pc);
  351. for (uint16_t i = 0; i < n; i++) {
  352. if (p16[i] == 0) {
  353. free++;
  354. }
  355. }
  356. } else {
  357. uint32_t* p32 = reinterpret_cast<uint32_t*>(pc);
  358. for (uint16_t i = 0; i < n; i++) {
  359. if (p32[i] == 0) {
  360. free++;
  361. }
  362. }
  363. }
  364. todo -= n;
  365. }
  366. } else {
  367. // invalid FAT type
  368. DBG_FAIL_MACRO;
  369. goto fail;
  370. }
  371. setFreeClusterCount(free);
  372. return free;
  373. fail:
  374. return -1;
  375. }
  376. //------------------------------------------------------------------------------
  377. bool FatPartition::init(FsBlockDevice* dev, uint8_t part, uint32_t volStart) {
  378. uint32_t clusterCount;
  379. uint32_t totalSectors;
  380. m_blockDev = dev;
  381. pbs_t* pbs;
  382. BpbFat32_t* bpb;
  383. uint8_t tmp;
  384. m_fatType = 0;
  385. m_allocSearchStart = 1;
  386. m_cache.init(dev);
  387. #if USE_SEPARATE_FAT_CACHE
  388. m_fatCache.init(dev);
  389. #endif // USE_SEPARATE_FAT_CACHE
  390. // if part == 0 assume super floppy with FAT boot sector in sector zero
  391. // if part > 0 read MBR/GPT partition table
  392. if (part) {
  393. volStart = partitionTableGetVolumeStartSector(m_cache, part);
  394. if (!volStart) {
  395. DBG_FAIL_MACRO;
  396. goto fail;
  397. }
  398. }
  399. pbs = reinterpret_cast<pbs_t*>
  400. (dataCachePrepare(volStart, FsCache::CACHE_FOR_READ));
  401. if (!pbs) {
  402. DBG_FAIL_MACRO;
  403. goto fail;
  404. }
  405. bpb = reinterpret_cast<BpbFat32_t*>(pbs->bpb);
  406. if (bpb->fatCount != 2 || getLe16(bpb->bytesPerSector) != m_bytesPerSector) {
  407. DBG_FAIL_MACRO;
  408. goto fail;
  409. }
  410. m_sectorsPerCluster = bpb->sectorsPerCluster;
  411. m_clusterSectorMask = m_sectorsPerCluster - 1;
  412. // determine shift that is same as multiply by m_sectorsPerCluster
  413. m_sectorsPerClusterShift = 0;
  414. for (tmp = 1; m_sectorsPerCluster != tmp; tmp <<= 1) {
  415. if (tmp == 0) {
  416. DBG_FAIL_MACRO;
  417. goto fail;
  418. }
  419. m_sectorsPerClusterShift++;
  420. }
  421. m_sectorsPerFat = getLe16(bpb->sectorsPerFat16);
  422. if (m_sectorsPerFat == 0) {
  423. m_sectorsPerFat = getLe32(bpb->sectorsPerFat32);
  424. }
  425. m_fatStartSector = volStart + getLe16(bpb->reservedSectorCount);
  426. // count for FAT16 zero for FAT32
  427. m_rootDirEntryCount = getLe16(bpb->rootDirEntryCount);
  428. // directory start for FAT16 dataStart for FAT32
  429. m_rootDirStart = m_fatStartSector + 2 * m_sectorsPerFat;
  430. // data start for FAT16 and FAT32
  431. m_dataStartSector = m_rootDirStart +
  432. ((FS_DIR_SIZE*m_rootDirEntryCount + m_bytesPerSector - 1)/m_bytesPerSector);
  433. // total sectors for FAT16 or FAT32
  434. totalSectors = getLe16(bpb->totalSectors16);
  435. if (totalSectors == 0) {
  436. totalSectors = getLe32(bpb->totalSectors32);
  437. }
  438. // total data sectors
  439. clusterCount = totalSectors - (m_dataStartSector - volStart);
  440. // divide by cluster size to get cluster count
  441. clusterCount >>= m_sectorsPerClusterShift;
  442. m_lastCluster = clusterCount + 1;
  443. // Indicate unknown number of free clusters.
  444. setFreeClusterCount(-1);
  445. // FAT type is determined by cluster count
  446. if (clusterCount < 4085) {
  447. m_fatType = 12;
  448. if (!FAT12_SUPPORT) {
  449. DBG_FAIL_MACRO;
  450. goto fail;
  451. }
  452. } else if (clusterCount < 65525) {
  453. m_fatType = 16;
  454. } else {
  455. m_rootDirStart = getLe32(bpb->fat32RootCluster);
  456. m_fatType = 32;
  457. }
  458. m_cache.setMirrorOffset(m_sectorsPerFat);
  459. #if USE_SEPARATE_FAT_CACHE
  460. m_fatCache.setMirrorOffset(m_sectorsPerFat);
  461. #endif // USE_SEPARATE_FAT_CACHE
  462. return true;
  463. fail:
  464. return false;
  465. }