2016-04-26 20:23:39 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 14:00:57 +00:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-04-26 20:23:39 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2016-04-26 20:23:39 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-23 03:20:14 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-04-26 20:23:39 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-10-24 22:13:10 +00:00
|
|
|
* sd/SdVolume.cpp
|
|
|
|
*
|
2016-04-26 20:23:39 +00:00
|
|
|
* Arduino SdFat Library
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2009 by William Greiman
|
2016-04-26 20:23:39 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Arduino Sd2Card Library
|
|
|
|
*/
|
2017-09-06 11:28:32 +00:00
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
|
|
|
|
#include "SdVolume.h"
|
2017-09-06 11:28:32 +00:00
|
|
|
|
2020-01-03 01:01:38 +00:00
|
|
|
#include "../MarlinCore.h"
|
2017-09-06 11:28:32 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
#if !USE_MULTIPLE_CARDS
|
|
|
|
// raw block cache
|
|
|
|
uint32_t SdVolume::cacheBlockNumber_; // current block number
|
|
|
|
cache_t SdVolume::cacheBuffer_; // 512 byte cache for Sd2Card
|
|
|
|
Sd2Card* SdVolume::sdCard_; // pointer to SD card object
|
|
|
|
bool SdVolume::cacheDirty_; // cacheFlush() will write block if true
|
|
|
|
uint32_t SdVolume::cacheMirrorBlock_; // mirror block for second FAT
|
|
|
|
#endif // USE_MULTIPLE_CARDS
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
// find a contiguous group of clusters
|
|
|
|
bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
|
2020-05-31 04:59:29 +00:00
|
|
|
if (ENABLED(SDCARD_READONLY)) return false;
|
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
// start of group
|
|
|
|
uint32_t bgnCluster;
|
|
|
|
// end of group
|
|
|
|
uint32_t endCluster;
|
|
|
|
// last cluster of FAT
|
|
|
|
uint32_t fatEnd = clusterCount_ + 1;
|
|
|
|
|
|
|
|
// flag to save place to start next search
|
|
|
|
bool setStart;
|
|
|
|
|
|
|
|
// set search start cluster
|
|
|
|
if (*curCluster) {
|
|
|
|
// try to make file contiguous
|
|
|
|
bgnCluster = *curCluster + 1;
|
|
|
|
|
|
|
|
// don't save new start location
|
|
|
|
setStart = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// start at likely place for free cluster
|
|
|
|
bgnCluster = allocSearchStart_;
|
|
|
|
|
|
|
|
// save next search start if one cluster
|
|
|
|
setStart = count == 1;
|
|
|
|
}
|
|
|
|
// end of group
|
|
|
|
endCluster = bgnCluster;
|
|
|
|
|
|
|
|
// search the FAT for free clusters
|
|
|
|
for (uint32_t n = 0;; n++, endCluster++) {
|
|
|
|
// can't find space checked all clusters
|
2017-11-15 06:06:20 +00:00
|
|
|
if (n >= clusterCount_) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
|
|
|
|
// past end - start from beginning of FAT
|
|
|
|
if (endCluster > fatEnd) {
|
|
|
|
bgnCluster = endCluster = 2;
|
|
|
|
}
|
|
|
|
uint32_t f;
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!fatGet(endCluster, &f)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
|
|
|
|
if (f != 0) {
|
|
|
|
// cluster in use try next cluster as bgnCluster
|
|
|
|
bgnCluster = endCluster + 1;
|
|
|
|
}
|
|
|
|
else if ((endCluster - bgnCluster + 1) == count) {
|
|
|
|
// done - found space
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// mark end of chain
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!fatPutEOC(endCluster)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
|
|
|
|
// link clusters
|
|
|
|
while (endCluster > bgnCluster) {
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!fatPut(endCluster - 1, endCluster)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
endCluster--;
|
|
|
|
}
|
|
|
|
if (*curCluster != 0) {
|
|
|
|
// connect chains
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!fatPut(*curCluster, bgnCluster)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
}
|
|
|
|
// return first cluster number to caller
|
|
|
|
*curCluster = bgnCluster;
|
|
|
|
|
|
|
|
// remember possible next free cluster
|
|
|
|
if (setStart) allocSearchStart_ = bgnCluster + 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
bool SdVolume::cacheFlush() {
|
2020-05-31 04:59:29 +00:00
|
|
|
#if DISABLED(SDCARD_READONLY)
|
|
|
|
if (cacheDirty_) {
|
|
|
|
if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data))
|
2017-11-15 06:06:20 +00:00
|
|
|
return false;
|
2020-05-31 04:59:29 +00:00
|
|
|
|
|
|
|
// mirror FAT tables
|
|
|
|
if (cacheMirrorBlock_) {
|
|
|
|
if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data))
|
|
|
|
return false;
|
|
|
|
cacheMirrorBlock_ = 0;
|
|
|
|
}
|
|
|
|
cacheDirty_ = 0;
|
2016-04-26 20:23:39 +00:00
|
|
|
}
|
2020-05-31 04:59:29 +00:00
|
|
|
#endif
|
2016-04-26 20:23:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
|
|
|
|
if (cacheBlockNumber_ != blockNumber) {
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!cacheFlush()) return false;
|
|
|
|
if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
cacheBlockNumber_ = blockNumber;
|
|
|
|
}
|
|
|
|
if (dirty) cacheDirty_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
// return the size in bytes of a cluster chain
|
|
|
|
bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
|
|
|
|
uint32_t s = 0;
|
|
|
|
do {
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!fatGet(cluster, &cluster)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
s += 512UL << clusterSizeShift_;
|
|
|
|
} while (!isEOC(cluster));
|
|
|
|
*size = s;
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
// Fetch a FAT entry
|
|
|
|
bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
|
|
|
|
uint32_t lba;
|
2017-11-15 06:06:20 +00:00
|
|
|
if (cluster > (clusterCount_ + 1)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
if (FAT12_SUPPORT && fatType_ == 12) {
|
|
|
|
uint16_t index = cluster;
|
|
|
|
index += index >> 1;
|
|
|
|
lba = fatStartBlock_ + (index >> 9);
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!cacheRawBlock(lba, CACHE_FOR_READ)) return false;
|
2017-08-09 18:23:14 +00:00
|
|
|
index &= 0x1FF;
|
2016-04-26 20:23:39 +00:00
|
|
|
uint16_t tmp = cacheBuffer_.data[index];
|
|
|
|
index++;
|
|
|
|
if (index == 512) {
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
tmp |= cacheBuffer_.data[index] << 8;
|
2017-08-09 18:23:14 +00:00
|
|
|
*value = cluster & 1 ? tmp >> 4 : tmp & 0xFFF;
|
2016-04-26 20:23:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
|
|
|
if (fatType_ == 16)
|
2016-04-26 20:23:39 +00:00
|
|
|
lba = fatStartBlock_ + (cluster >> 8);
|
2017-11-15 06:06:20 +00:00
|
|
|
else if (fatType_ == 32)
|
2016-04-26 20:23:39 +00:00
|
|
|
lba = fatStartBlock_ + (cluster >> 7);
|
2017-11-15 06:06:20 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (lba != cacheBlockNumber_ && !cacheRawBlock(lba, CACHE_FOR_READ))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*value = (fatType_ == 16) ? cacheBuffer_.fat16[cluster & 0xFF] : (cacheBuffer_.fat32[cluster & 0x7F] & FAT32MASK);
|
2016-04-26 20:23:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
// Store a FAT entry
|
|
|
|
bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
|
2020-05-31 04:59:29 +00:00
|
|
|
if (ENABLED(SDCARD_READONLY)) return false;
|
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
uint32_t lba;
|
|
|
|
// error if reserved cluster
|
2017-11-15 06:06:20 +00:00
|
|
|
if (cluster < 2) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
|
|
|
|
// error if not in FAT
|
2017-11-15 06:06:20 +00:00
|
|
|
if (cluster > (clusterCount_ + 1)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
|
|
|
|
if (FAT12_SUPPORT && fatType_ == 12) {
|
|
|
|
uint16_t index = cluster;
|
|
|
|
index += index >> 1;
|
|
|
|
lba = fatStartBlock_ + (index >> 9);
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
// mirror second FAT
|
|
|
|
if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
|
2017-08-09 18:23:14 +00:00
|
|
|
index &= 0x1FF;
|
2016-04-26 20:23:39 +00:00
|
|
|
uint8_t tmp = value;
|
|
|
|
if (cluster & 1) {
|
2017-12-15 21:17:52 +00:00
|
|
|
tmp = (cacheBuffer_.data[index] & 0xF) | tmp << 4;
|
2016-04-26 20:23:39 +00:00
|
|
|
}
|
|
|
|
cacheBuffer_.data[index] = tmp;
|
|
|
|
index++;
|
|
|
|
if (index == 512) {
|
|
|
|
lba++;
|
|
|
|
index = 0;
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
// mirror second FAT
|
|
|
|
if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
|
|
|
|
}
|
|
|
|
tmp = value >> 4;
|
|
|
|
if (!(cluster & 1)) {
|
2017-08-09 18:23:14 +00:00
|
|
|
tmp = ((cacheBuffer_.data[index] & 0xF0)) | tmp >> 4;
|
2016-04-26 20:23:39 +00:00
|
|
|
}
|
|
|
|
cacheBuffer_.data[index] = tmp;
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
|
|
|
if (fatType_ == 16)
|
2016-04-26 20:23:39 +00:00
|
|
|
lba = fatStartBlock_ + (cluster >> 8);
|
2017-11-15 06:06:20 +00:00
|
|
|
else if (fatType_ == 32)
|
2016-04-26 20:23:39 +00:00
|
|
|
lba = fatStartBlock_ + (cluster >> 7);
|
2017-11-15 06:06:20 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
|
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
// store entry
|
2017-11-15 06:06:20 +00:00
|
|
|
if (fatType_ == 16)
|
2017-08-09 18:23:14 +00:00
|
|
|
cacheBuffer_.fat16[cluster & 0xFF] = value;
|
2017-11-15 06:06:20 +00:00
|
|
|
else
|
2017-08-09 18:23:14 +00:00
|
|
|
cacheBuffer_.fat32[cluster & 0x7F] = value;
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
// mirror second FAT
|
|
|
|
if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
// free a cluster chain
|
|
|
|
bool SdVolume::freeChain(uint32_t cluster) {
|
|
|
|
// clear free cluster location
|
|
|
|
allocSearchStart_ = 2;
|
|
|
|
|
|
|
|
do {
|
2017-11-15 06:06:20 +00:00
|
|
|
uint32_t next;
|
|
|
|
if (!fatGet(cluster, &next)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
|
|
|
|
// free cluster
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!fatPut(cluster, 0)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
|
|
|
|
cluster = next;
|
|
|
|
} while (!isEOC(cluster));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
/** Volume free space in clusters.
|
|
|
|
*
|
|
|
|
* \return Count of free clusters for success or -1 if an error occurs.
|
|
|
|
*/
|
|
|
|
int32_t SdVolume::freeClusterCount() {
|
|
|
|
uint32_t free = 0;
|
|
|
|
uint16_t n;
|
|
|
|
uint32_t todo = clusterCount_ + 2;
|
|
|
|
|
2017-11-15 06:06:20 +00:00
|
|
|
if (fatType_ == 16)
|
2016-04-26 20:23:39 +00:00
|
|
|
n = 256;
|
2017-11-15 06:06:20 +00:00
|
|
|
else if (fatType_ == 32)
|
2016-04-26 20:23:39 +00:00
|
|
|
n = 128;
|
2017-11-15 06:06:20 +00:00
|
|
|
else // put FAT12 here
|
2016-04-26 20:23:39 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) {
|
|
|
|
if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1;
|
|
|
|
NOMORE(n, todo);
|
|
|
|
if (fatType_ == 16) {
|
2017-11-15 06:06:20 +00:00
|
|
|
for (uint16_t i = 0; i < n; i++)
|
2016-04-26 20:23:39 +00:00
|
|
|
if (cacheBuffer_.fat16[i] == 0) free++;
|
|
|
|
}
|
|
|
|
else {
|
2017-11-15 06:06:20 +00:00
|
|
|
for (uint16_t i = 0; i < n; i++)
|
2016-04-26 20:23:39 +00:00
|
|
|
if (cacheBuffer_.fat32[i] == 0) free++;
|
|
|
|
}
|
2020-01-28 00:16:44 +00:00
|
|
|
#ifdef ESP32
|
2020-02-01 09:50:26 +00:00
|
|
|
// Needed to reset the idle task watchdog timer on ESP32 as reading the complete FAT may easily
|
2020-01-28 00:16:44 +00:00
|
|
|
// block for 10+ seconds. yield() is insufficient since it blocks lower prio tasks (e.g., idle).
|
|
|
|
static millis_t nextTaskTime = 0;
|
|
|
|
const millis_t ms = millis();
|
2020-01-30 18:22:42 +00:00
|
|
|
if (ELAPSED(ms, nextTaskTime)) {
|
2020-01-28 00:16:44 +00:00
|
|
|
vTaskDelay(1); // delay 1 tick (Minimum. Usually 10 or 1 ms depending on skdconfig.h)
|
|
|
|
nextTaskTime = ms + 1000; // tickle the task manager again in 1 second
|
|
|
|
}
|
|
|
|
#endif // ESP32
|
2016-04-26 20:23:39 +00:00
|
|
|
}
|
|
|
|
return free;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
|
2016-04-26 20:23:39 +00:00
|
|
|
/** Initialize a FAT volume.
|
|
|
|
*
|
|
|
|
* \param[in] dev The SD card where the volume is located.
|
|
|
|
*
|
|
|
|
* \param[in] part The partition to be used. Legal values for \a part are
|
|
|
|
* 1-4 to use the corresponding partition on a device formatted with
|
|
|
|
* a MBR, Master Boot Record, or zero if the device is formatted as
|
|
|
|
* a super floppy with the FAT boot sector in block zero.
|
|
|
|
*
|
2017-11-15 06:06:20 +00:00
|
|
|
* \return true for success, false for failure.
|
|
|
|
* Reasons for failure include not finding a valid partition, not finding a valid
|
2016-04-26 20:23:39 +00:00
|
|
|
* FAT file system in the specified partition or an I/O error.
|
|
|
|
*/
|
|
|
|
bool SdVolume::init(Sd2Card* dev, uint8_t part) {
|
2017-11-15 06:06:20 +00:00
|
|
|
uint32_t totalBlocks, volumeStartBlock = 0;
|
2016-04-26 20:23:39 +00:00
|
|
|
fat32_boot_t* fbs;
|
|
|
|
|
|
|
|
sdCard_ = dev;
|
|
|
|
fatType_ = 0;
|
|
|
|
allocSearchStart_ = 2;
|
|
|
|
cacheDirty_ = 0; // cacheFlush() will write block if true
|
|
|
|
cacheMirrorBlock_ = 0;
|
2017-08-09 18:23:14 +00:00
|
|
|
cacheBlockNumber_ = 0xFFFFFFFF;
|
2016-04-26 20:23:39 +00:00
|
|
|
|
|
|
|
// if part == 0 assume super floppy with FAT boot sector in block zero
|
|
|
|
// if part > 0 assume mbr volume with partition table
|
|
|
|
if (part) {
|
2017-11-15 06:06:20 +00:00
|
|
|
if (part > 4) return false;
|
|
|
|
if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
part_t* p = &cacheBuffer_.mbr.part[part - 1];
|
2017-11-15 06:06:20 +00:00
|
|
|
if ((p->boot & 0x7F) != 0 || p->totalSectors < 100 || p->firstSector == 0)
|
|
|
|
return false; // not a valid partition
|
2016-04-26 20:23:39 +00:00
|
|
|
volumeStartBlock = p->firstSector;
|
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
fbs = &cacheBuffer_.fbs32;
|
|
|
|
if (fbs->bytesPerSector != 512 ||
|
|
|
|
fbs->fatCount == 0 ||
|
|
|
|
fbs->reservedSectorCount == 0 ||
|
|
|
|
fbs->sectorsPerCluster == 0) {
|
|
|
|
// not valid FAT volume
|
2017-11-15 06:06:20 +00:00
|
|
|
return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
}
|
|
|
|
fatCount_ = fbs->fatCount;
|
|
|
|
blocksPerCluster_ = fbs->sectorsPerCluster;
|
|
|
|
// determine shift that is same as multiply by blocksPerCluster_
|
|
|
|
clusterSizeShift_ = 0;
|
|
|
|
while (blocksPerCluster_ != _BV(clusterSizeShift_)) {
|
|
|
|
// error if not power of 2
|
2017-11-15 06:06:20 +00:00
|
|
|
if (clusterSizeShift_++ > 7) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
}
|
|
|
|
blocksPerFat_ = fbs->sectorsPerFat16 ?
|
|
|
|
fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
|
|
|
|
|
|
|
|
fatStartBlock_ = volumeStartBlock + fbs->reservedSectorCount;
|
|
|
|
|
|
|
|
// count for FAT16 zero for FAT32
|
|
|
|
rootDirEntryCount_ = fbs->rootDirEntryCount;
|
|
|
|
|
|
|
|
// directory start for FAT16 dataStart for FAT32
|
|
|
|
rootDirStart_ = fatStartBlock_ + fbs->fatCount * blocksPerFat_;
|
|
|
|
|
|
|
|
// data start for FAT16 and FAT32
|
|
|
|
dataStartBlock_ = rootDirStart_ + ((32 * fbs->rootDirEntryCount + 511) / 512);
|
|
|
|
|
|
|
|
// total blocks for FAT16 or FAT32
|
|
|
|
totalBlocks = fbs->totalSectors16 ?
|
|
|
|
fbs->totalSectors16 : fbs->totalSectors32;
|
|
|
|
|
|
|
|
// total data blocks
|
|
|
|
clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
|
|
|
|
|
|
|
|
// divide by cluster size to get cluster count
|
|
|
|
clusterCount_ >>= clusterSizeShift_;
|
|
|
|
|
|
|
|
// FAT type is determined by cluster count
|
|
|
|
if (clusterCount_ < 4085) {
|
|
|
|
fatType_ = 12;
|
2017-11-15 06:06:20 +00:00
|
|
|
if (!FAT12_SUPPORT) return false;
|
2016-04-26 20:23:39 +00:00
|
|
|
}
|
2017-11-15 06:06:20 +00:00
|
|
|
else if (clusterCount_ < 65525)
|
2016-04-26 20:23:39 +00:00
|
|
|
fatType_ = 16;
|
|
|
|
else {
|
|
|
|
rootDirStart_ = fbs->fat32RootCluster;
|
|
|
|
fatType_ = 32;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-06 11:28:32 +00:00
|
|
|
|
|
|
|
#endif // SDSUPPORT
|