ExFatPartition.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * Copyright (c) 2011-2021 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. #define DBG_FILE "ExFatPartition.cpp"
  26. #include "../common/DebugMacros.h"
  27. #include "ExFatLib.h"
  28. //------------------------------------------------------------------------------
  29. // return 0 if error, 1 if no space, else start cluster.
  30. uint32_t ExFatPartition::bitmapFind(uint32_t cluster, uint32_t count) {
  31. uint32_t start = cluster ? cluster - 2 : m_bitmapStart;
  32. if (start >= m_clusterCount) {
  33. start = 0;
  34. }
  35. uint32_t endAlloc = start;
  36. uint32_t bgnAlloc = start;
  37. uint16_t sectorSize = 1 << m_bytesPerSectorShift;
  38. size_t i = (start >> 3) & (sectorSize - 1);
  39. uint8_t* cache;
  40. uint8_t mask = 1 << (start & 7);
  41. while (true) {
  42. uint32_t sector = m_clusterHeapStartSector +
  43. (endAlloc >> (m_bytesPerSectorShift + 3));
  44. cache = bitmapCachePrepare(sector, FsCache::CACHE_FOR_READ);
  45. if (!cache) {
  46. return 0;
  47. }
  48. for (; i < sectorSize; i++) {
  49. for (; mask; mask <<= 1) {
  50. endAlloc++;
  51. if (!(mask & cache[i])) {
  52. if ((endAlloc - bgnAlloc) == count) {
  53. if (cluster == 0 && count == 1) {
  54. // Start at found sector. bitmapModify may increase this.
  55. m_bitmapStart = bgnAlloc;
  56. }
  57. return bgnAlloc + 2;
  58. }
  59. } else {
  60. bgnAlloc = endAlloc;
  61. }
  62. if (endAlloc == start) {
  63. return 1;
  64. }
  65. if (endAlloc >= m_clusterCount) {
  66. endAlloc = bgnAlloc = 0;
  67. i = sectorSize;
  68. break;
  69. }
  70. }
  71. mask = 1;
  72. }
  73. i = 0;
  74. }
  75. return 0;
  76. }
  77. //------------------------------------------------------------------------------
  78. bool ExFatPartition::bitmapModify(uint32_t cluster,
  79. uint32_t count, bool value) {
  80. uint32_t sector;
  81. uint32_t start = cluster - 2;
  82. size_t i;
  83. uint8_t* cache;
  84. uint8_t mask;
  85. cluster -= 2;
  86. if ((start + count) > m_clusterCount) {
  87. DBG_FAIL_MACRO;
  88. goto fail;
  89. }
  90. if (value) {
  91. if (start <= m_bitmapStart && m_bitmapStart < (start + count)) {
  92. m_bitmapStart = (start + count) < m_clusterCount ? start + count : 0;
  93. }
  94. } else {
  95. if (start < m_bitmapStart) {
  96. m_bitmapStart = start;
  97. }
  98. }
  99. mask = 1 << (start & 7);
  100. sector = m_clusterHeapStartSector +
  101. (start >> (m_bytesPerSectorShift + 3));
  102. i = (start >> 3) & m_sectorMask;
  103. while (true) {
  104. cache = bitmapCachePrepare(sector++, FsCache::CACHE_FOR_WRITE);
  105. if (!cache) {
  106. DBG_FAIL_MACRO;
  107. goto fail;
  108. }
  109. for (; i < m_bytesPerSector; i++) {
  110. for (; mask; mask <<= 1) {
  111. if (value == static_cast<bool>(cache[i] & mask)) {
  112. DBG_FAIL_MACRO;
  113. goto fail;
  114. }
  115. cache[i] ^= mask;
  116. if (--count == 0) {
  117. return true;
  118. }
  119. }
  120. mask = 1;
  121. }
  122. i = 0;
  123. }
  124. fail:
  125. return false;
  126. }
  127. //------------------------------------------------------------------------------
  128. uint32_t ExFatPartition::chainSize(uint32_t cluster) {
  129. uint32_t n = 0;
  130. int8_t status;
  131. do {
  132. status = fatGet(cluster, & cluster);
  133. if (status < 0) return 0;
  134. n++;
  135. } while (status);
  136. return n;
  137. }
  138. //------------------------------------------------------------------------------
  139. uint8_t* ExFatPartition::dirCache(DirPos_t* pos, uint8_t options) {
  140. uint32_t sector = clusterStartSector(pos->cluster);
  141. sector += (m_clusterMask & pos->position) >> m_bytesPerSectorShift;
  142. uint8_t* cache = dataCachePrepare(sector, options);
  143. return cache ? cache + (pos->position & m_sectorMask) : nullptr;
  144. }
  145. //------------------------------------------------------------------------------
  146. // return -1 error, 0 EOC, 1 OK
  147. int8_t ExFatPartition::dirSeek(DirPos_t* pos, uint32_t offset) {
  148. int8_t status;
  149. uint32_t tmp = (m_clusterMask & pos->position) + offset;
  150. pos->position += offset;
  151. tmp >>= bytesPerClusterShift();
  152. while (tmp--) {
  153. if (pos->isContiguous) {
  154. pos->cluster++;
  155. } else {
  156. status = fatGet(pos->cluster, &pos->cluster);
  157. if (status != 1) {
  158. return status;
  159. }
  160. }
  161. }
  162. return 1;
  163. }
  164. //------------------------------------------------------------------------------
  165. // return -1 error, 0 EOC, 1 OK
  166. int8_t ExFatPartition::fatGet(uint32_t cluster, uint32_t* value) {
  167. uint8_t* cache;
  168. uint32_t next;
  169. uint32_t sector;
  170. if (cluster > (m_clusterCount + 1)) {
  171. DBG_FAIL_MACRO;
  172. return -1;
  173. }
  174. sector = m_fatStartSector + (cluster >> (m_bytesPerSectorShift - 2));
  175. cache = dataCachePrepare(sector, FsCache::CACHE_FOR_READ);
  176. if (!cache) {
  177. return -1;
  178. }
  179. next = getLe32(cache + ((cluster << 2) & m_sectorMask));
  180. if (next == EXFAT_EOC) {
  181. return 0;
  182. }
  183. *value = next;
  184. return 1;
  185. }
  186. //------------------------------------------------------------------------------
  187. bool ExFatPartition::fatPut(uint32_t cluster, uint32_t value) {
  188. uint32_t sector;
  189. uint8_t* cache;
  190. if (cluster < 2 || cluster > (m_clusterCount + 1)) {
  191. DBG_FAIL_MACRO;
  192. goto fail;
  193. }
  194. sector = m_fatStartSector + (cluster >> (m_bytesPerSectorShift - 2));
  195. cache = dataCachePrepare(sector, FsCache::CACHE_FOR_WRITE);
  196. if (!cache) {
  197. DBG_FAIL_MACRO;
  198. goto fail;
  199. }
  200. setLe32(cache + ((cluster << 2) & m_sectorMask), value);
  201. return true;
  202. fail:
  203. return false;
  204. }
  205. //------------------------------------------------------------------------------
  206. bool ExFatPartition::freeChain(uint32_t cluster) {
  207. uint32_t next;
  208. uint32_t start = cluster;
  209. int8_t status;
  210. do {
  211. status = fatGet(cluster, &next);
  212. if (status < 0) {
  213. DBG_FAIL_MACRO;
  214. goto fail;
  215. }
  216. if (!fatPut(cluster, 0)) {
  217. DBG_FAIL_MACRO;
  218. goto fail;
  219. }
  220. if (status == 0 || (cluster + 1) != next) {
  221. if (!bitmapModify(start, cluster - start + 1, 0)) {
  222. DBG_FAIL_MACRO;
  223. goto fail;
  224. }
  225. start = next;
  226. }
  227. cluster = next;
  228. } while (status);
  229. return true;
  230. fail:
  231. return false;
  232. }
  233. //------------------------------------------------------------------------------
  234. uint32_t ExFatPartition::freeClusterCount() {
  235. uint32_t nc = 0;
  236. uint32_t sector = m_clusterHeapStartSector;
  237. uint32_t usedCount = 0;
  238. uint8_t* cache;
  239. while (true) {
  240. cache = dataCachePrepare(sector++, FsCache::CACHE_FOR_READ);
  241. if (!cache) {
  242. return 0;
  243. }
  244. for (size_t i = 0; i < m_bytesPerSector; i++) {
  245. if (cache[i] == 0XFF) {
  246. usedCount+= 8;
  247. } else if (cache[i]) {
  248. for (uint8_t mask = 1; mask ; mask <<=1) {
  249. if ((mask & cache[i])) {
  250. usedCount++;
  251. }
  252. }
  253. }
  254. nc += 8;
  255. if (nc >= m_clusterCount) {
  256. return m_clusterCount - usedCount;
  257. }
  258. }
  259. }
  260. }
  261. //------------------------------------------------------------------------------
  262. bool ExFatPartition::init(FsBlockDevice* dev, uint8_t part) {
  263. uint32_t volStart = 0;
  264. uint8_t* cache;
  265. pbs_t* pbs;
  266. BpbExFat_t* bpb;
  267. MbrSector_t* mbr;
  268. MbrPart_t* mp;
  269. m_fatType = 0;
  270. m_blockDev = dev;
  271. cacheInit(m_blockDev);
  272. cache = dataCachePrepare(0, FsCache::CACHE_FOR_READ);
  273. if (part > 4 || !cache) {
  274. DBG_FAIL_MACRO;
  275. goto fail;
  276. }
  277. if (part >= 1) {
  278. mbr = reinterpret_cast<MbrSector_t*>(cache);
  279. mp = &mbr->part[part - 1];
  280. if (mp->type == 0) {
  281. DBG_FAIL_MACRO;
  282. goto fail;
  283. }
  284. volStart = getLe32(mp->relativeSectors);
  285. cache = dataCachePrepare(volStart, FsCache::CACHE_FOR_READ);
  286. if (!cache) {
  287. DBG_FAIL_MACRO;
  288. goto fail;
  289. }
  290. }
  291. pbs = reinterpret_cast<pbs_t*>(cache);
  292. if (strncmp(pbs->oemName, "EXFAT", 5)) {
  293. DBG_FAIL_MACRO;
  294. goto fail;
  295. }
  296. bpb = reinterpret_cast<BpbExFat_t*>(pbs->bpb);
  297. if (bpb->bytesPerSectorShift != m_bytesPerSectorShift) {
  298. DBG_FAIL_MACRO;
  299. goto fail;
  300. }
  301. m_fatStartSector = volStart + getLe32(bpb->fatOffset);
  302. m_fatLength = getLe32(bpb->fatLength);
  303. m_clusterHeapStartSector = volStart + getLe32(bpb->clusterHeapOffset);
  304. m_clusterCount = getLe32(bpb->clusterCount);
  305. m_rootDirectoryCluster = getLe32(bpb->rootDirectoryCluster);
  306. m_sectorsPerClusterShift = bpb->sectorsPerClusterShift;
  307. m_bytesPerCluster = 1UL << (m_bytesPerSectorShift + m_sectorsPerClusterShift);
  308. m_clusterMask = m_bytesPerCluster - 1;
  309. // Set m_bitmapStart to first free cluster.
  310. m_bitmapStart = 0;
  311. bitmapFind(0, 1);
  312. m_fatType = FAT_TYPE_EXFAT;
  313. return true;
  314. fail:
  315. return false;
  316. }
  317. //------------------------------------------------------------------------------
  318. uint32_t ExFatPartition::rootLength() {
  319. uint32_t nc = chainSize(m_rootDirectoryCluster);
  320. return nc << bytesPerClusterShift();
  321. }