ExFatPartition.cpp 11 KB

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