123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
-
-
-
- #include "Marlin.h"
- #if ENABLED(SDSUPPORT)
-
- #include "SdVolume.h"
-
- #if !USE_MULTIPLE_CARDS
-
- uint32_t SdVolume::cacheBlockNumber_;
- cache_t SdVolume::cacheBuffer_;
- Sd2Card* SdVolume::sdCard_;
- bool SdVolume::cacheDirty_;
- uint32_t SdVolume::cacheMirrorBlock_;
- #endif
-
-
- bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
-
- uint32_t bgnCluster;
-
- uint32_t endCluster;
-
- uint32_t fatEnd = clusterCount_ + 1;
-
-
- bool setStart;
-
-
- if (*curCluster) {
-
- bgnCluster = *curCluster + 1;
-
-
- setStart = false;
- }
- else {
-
- bgnCluster = allocSearchStart_;
-
-
- setStart = count == 1;
- }
-
- endCluster = bgnCluster;
-
-
- for (uint32_t n = 0;; n++, endCluster++) {
-
- if (n >= clusterCount_) goto FAIL;
-
-
- if (endCluster > fatEnd) {
- bgnCluster = endCluster = 2;
- }
- uint32_t f;
- if (!fatGet(endCluster, &f)) goto FAIL;
-
- if (f != 0) {
-
- bgnCluster = endCluster + 1;
- }
- else if ((endCluster - bgnCluster + 1) == count) {
-
- break;
- }
- }
-
- if (!fatPutEOC(endCluster)) goto FAIL;
-
-
- while (endCluster > bgnCluster) {
- if (!fatPut(endCluster - 1, endCluster)) goto FAIL;
- endCluster--;
- }
- if (*curCluster != 0) {
-
- if (!fatPut(*curCluster, bgnCluster)) goto FAIL;
- }
-
- *curCluster = bgnCluster;
-
-
- if (setStart) allocSearchStart_ = bgnCluster + 1;
-
- return true;
- FAIL:
- return false;
- }
-
- bool SdVolume::cacheFlush() {
- if (cacheDirty_) {
- if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) {
- goto FAIL;
- }
-
- if (cacheMirrorBlock_) {
- if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) {
- goto FAIL;
- }
- cacheMirrorBlock_ = 0;
- }
- cacheDirty_ = 0;
- }
- return true;
- FAIL:
- return false;
- }
-
- bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
- if (cacheBlockNumber_ != blockNumber) {
- if (!cacheFlush()) goto FAIL;
- if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) goto FAIL;
- cacheBlockNumber_ = blockNumber;
- }
- if (dirty) cacheDirty_ = true;
- return true;
- FAIL:
- return false;
- }
-
-
- bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
- uint32_t s = 0;
- do {
- if (!fatGet(cluster, &cluster)) goto FAIL;
- s += 512UL << clusterSizeShift_;
- } while (!isEOC(cluster));
- *size = s;
- return true;
- FAIL:
- return false;
- }
-
-
- bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
- uint32_t lba;
- if (cluster > (clusterCount_ + 1)) goto FAIL;
- if (FAT12_SUPPORT && fatType_ == 12) {
- uint16_t index = cluster;
- index += index >> 1;
- lba = fatStartBlock_ + (index >> 9);
- if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto FAIL;
- index &= 0x1FF;
- uint16_t tmp = cacheBuffer_.data[index];
- index++;
- if (index == 512) {
- if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) goto FAIL;
- index = 0;
- }
- tmp |= cacheBuffer_.data[index] << 8;
- *value = cluster & 1 ? tmp >> 4 : tmp & 0xFFF;
- return true;
- }
- if (fatType_ == 16) {
- lba = fatStartBlock_ + (cluster >> 8);
- }
- else if (fatType_ == 32) {
- lba = fatStartBlock_ + (cluster >> 7);
- }
- else {
- goto FAIL;
- }
- if (lba != cacheBlockNumber_) {
- if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto FAIL;
- }
- if (fatType_ == 16) {
- *value = cacheBuffer_.fat16[cluster & 0xFF];
- }
- else {
- *value = cacheBuffer_.fat32[cluster & 0x7F] & FAT32MASK;
- }
- return true;
- FAIL:
- return false;
- }
-
-
- bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
- uint32_t lba;
-
- if (cluster < 2) goto FAIL;
-
-
- if (cluster > (clusterCount_ + 1)) goto FAIL;
-
- if (FAT12_SUPPORT && fatType_ == 12) {
- uint16_t index = cluster;
- index += index >> 1;
- lba = fatStartBlock_ + (index >> 9);
- if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto FAIL;
-
- if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
- index &= 0x1FF;
- uint8_t tmp = value;
- if (cluster & 1) {
- tmp = (cacheBuffer_.data[index] & 0XF) | tmp << 4;
- }
- cacheBuffer_.data[index] = tmp;
- index++;
- if (index == 512) {
- lba++;
- index = 0;
- if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto FAIL;
-
- if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
- }
- tmp = value >> 4;
- if (!(cluster & 1)) {
- tmp = ((cacheBuffer_.data[index] & 0xF0)) | tmp >> 4;
- }
- cacheBuffer_.data[index] = tmp;
- return true;
- }
- if (fatType_ == 16) {
- lba = fatStartBlock_ + (cluster >> 8);
- }
- else if (fatType_ == 32) {
- lba = fatStartBlock_ + (cluster >> 7);
- }
- else {
- goto FAIL;
- }
- if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto FAIL;
-
- if (fatType_ == 16) {
- cacheBuffer_.fat16[cluster & 0xFF] = value;
- }
- else {
- cacheBuffer_.fat32[cluster & 0x7F] = value;
- }
-
- if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
- return true;
- FAIL:
- return false;
- }
-
-
- bool SdVolume::freeChain(uint32_t cluster) {
- uint32_t next;
-
-
- allocSearchStart_ = 2;
-
- do {
- if (!fatGet(cluster, &next)) goto FAIL;
-
-
- if (!fatPut(cluster, 0)) goto FAIL;
-
- cluster = next;
- } while (!isEOC(cluster));
-
- return true;
- FAIL:
- return false;
- }
-
-
- int32_t SdVolume::freeClusterCount() {
- uint32_t free = 0;
- uint16_t n;
- uint32_t todo = clusterCount_ + 2;
-
- if (fatType_ == 16) {
- n = 256;
- }
- else if (fatType_ == 32) {
- n = 128;
- }
- else {
-
- 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) {
- for (uint16_t i = 0; i < n; i++) {
- if (cacheBuffer_.fat16[i] == 0) free++;
- }
- }
- else {
- for (uint16_t i = 0; i < n; i++) {
- if (cacheBuffer_.fat32[i] == 0) free++;
- }
- }
- }
- return free;
- }
-
-
- bool SdVolume::init(Sd2Card* dev, uint8_t part) {
- uint32_t totalBlocks;
- uint32_t volumeStartBlock = 0;
- fat32_boot_t* fbs;
-
- sdCard_ = dev;
- fatType_ = 0;
- allocSearchStart_ = 2;
- cacheDirty_ = 0;
- cacheMirrorBlock_ = 0;
- cacheBlockNumber_ = 0xFFFFFFFF;
-
-
-
- if (part) {
- if (part > 4)goto FAIL;
- if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto FAIL;
- part_t* p = &cacheBuffer_.mbr.part[part - 1];
- if ((p->boot & 0x7F) != 0 ||
- p->totalSectors < 100 ||
- p->firstSector == 0) {
-
- goto FAIL;
- }
- volumeStartBlock = p->firstSector;
- }
- if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto FAIL;
- fbs = &cacheBuffer_.fbs32;
- if (fbs->bytesPerSector != 512 ||
- fbs->fatCount == 0 ||
- fbs->reservedSectorCount == 0 ||
- fbs->sectorsPerCluster == 0) {
-
- goto FAIL;
- }
- fatCount_ = fbs->fatCount;
- blocksPerCluster_ = fbs->sectorsPerCluster;
-
- clusterSizeShift_ = 0;
- while (blocksPerCluster_ != _BV(clusterSizeShift_)) {
-
- if (clusterSizeShift_++ > 7) goto FAIL;
- }
- blocksPerFat_ = fbs->sectorsPerFat16 ?
- fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
-
- fatStartBlock_ = volumeStartBlock + fbs->reservedSectorCount;
-
-
- rootDirEntryCount_ = fbs->rootDirEntryCount;
-
-
- rootDirStart_ = fatStartBlock_ + fbs->fatCount * blocksPerFat_;
-
-
- dataStartBlock_ = rootDirStart_ + ((32 * fbs->rootDirEntryCount + 511) / 512);
-
-
- totalBlocks = fbs->totalSectors16 ?
- fbs->totalSectors16 : fbs->totalSectors32;
-
-
- clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
-
-
- clusterCount_ >>= clusterSizeShift_;
-
-
- if (clusterCount_ < 4085) {
- fatType_ = 12;
- if (!FAT12_SUPPORT) goto FAIL;
- }
- else if (clusterCount_ < 65525) {
- fatType_ = 16;
- }
- else {
- rootDirStart_ = fbs->fat32RootCluster;
- fatType_ = 32;
- }
- return true;
- FAIL:
- return false;
- }
- #endif
|