SdVolume.cpp

Sat, 07 Nov 2015 13:23:07 +0100

author
mbayer
date
Sat, 07 Nov 2015 13:23:07 +0100
changeset 0
2c8ba1964db7
permissions
-rw-r--r--

Initial code from reprappro Marlin repository

0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1 /* Arduino SdFat Library
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
2 * Copyright (C) 2009 by William Greiman
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
3 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
4 * This file is part of the Arduino SdFat Library
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
5 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
6 * This Library is free software: you can redistribute it and/or modify
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
8 * the Free Software Foundation, either version 3 of the License, or
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
9 * (at your option) any later version.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
10 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
11 * This Library is distributed in the hope that it will be useful,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
14 * GNU General Public License for more details.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
15 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
17 * along with the Arduino SdFat Library. If not, see
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
18 * <http://www.gnu.org/licenses/>.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
19 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
20 #include "Marlin.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
21 #ifdef SDSUPPORT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
22
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
23 #include "SdVolume.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
24 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
25 #if !USE_MULTIPLE_CARDS
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
26 // raw block cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
27 uint32_t SdVolume::cacheBlockNumber_; // current block number
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
28 cache_t SdVolume::cacheBuffer_; // 512 byte cache for Sd2Card
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
29 Sd2Card* SdVolume::sdCard_; // pointer to SD card object
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
30 bool SdVolume::cacheDirty_; // cacheFlush() will write block if true
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
31 uint32_t SdVolume::cacheMirrorBlock_; // mirror block for second FAT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
32 #endif // USE_MULTIPLE_CARDS
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
33 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
34 // find a contiguous group of clusters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
35 bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
36 // start of group
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
37 uint32_t bgnCluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
38 // end of group
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
39 uint32_t endCluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
40 // last cluster of FAT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
41 uint32_t fatEnd = clusterCount_ + 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
42
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
43 // flag to save place to start next search
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
44 bool setStart;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
45
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
46 // set search start cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
47 if (*curCluster) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
48 // try to make file contiguous
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
49 bgnCluster = *curCluster + 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
50
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
51 // don't save new start location
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
52 setStart = false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
53 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
54 // start at likely place for free cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
55 bgnCluster = allocSearchStart_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
56
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
57 // save next search start if one cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
58 setStart = count == 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
59 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
60 // end of group
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
61 endCluster = bgnCluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
62
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
63 // search the FAT for free clusters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
64 for (uint32_t n = 0;; n++, endCluster++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
65 // can't find space checked all clusters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
66 if (n >= clusterCount_) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
67
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
68 // past end - start from beginning of FAT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
69 if (endCluster > fatEnd) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
70 bgnCluster = endCluster = 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
71 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
72 uint32_t f;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
73 if (!fatGet(endCluster, &f)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
74
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
75 if (f != 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
76 // cluster in use try next cluster as bgnCluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
77 bgnCluster = endCluster + 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
78 } else if ((endCluster - bgnCluster + 1) == count) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
79 // done - found space
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
80 break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
81 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
82 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
83 // mark end of chain
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
84 if (!fatPutEOC(endCluster)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
85
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
86 // link clusters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
87 while (endCluster > bgnCluster) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
88 if (!fatPut(endCluster - 1, endCluster)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
89 endCluster--;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
90 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
91 if (*curCluster != 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
92 // connect chains
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
93 if (!fatPut(*curCluster, bgnCluster)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
94 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
95 // return first cluster number to caller
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
96 *curCluster = bgnCluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
97
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
98 // remember possible next free cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
99 if (setStart) allocSearchStart_ = bgnCluster + 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
100
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
101 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
102
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
103 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
104 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
105 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
106 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
107 bool SdVolume::cacheFlush() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
108 if (cacheDirty_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
109 if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
110 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
111 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
112 // mirror FAT tables
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
113 if (cacheMirrorBlock_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
114 if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
115 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
116 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
117 cacheMirrorBlock_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
118 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
119 cacheDirty_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
120 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
121 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
122
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
123 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
124 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
125 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
126 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
127 bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
128 if (cacheBlockNumber_ != blockNumber) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
129 if (!cacheFlush()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
130 if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
131 cacheBlockNumber_ = blockNumber;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
132 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
133 if (dirty) cacheDirty_ = true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
134 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
135
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
136 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
137 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
138 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
139 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
140 // return the size in bytes of a cluster chain
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
141 bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
142 uint32_t s = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
143 do {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
144 if (!fatGet(cluster, &cluster)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
145 s += 512UL << clusterSizeShift_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
146 } while (!isEOC(cluster));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
147 *size = s;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
148 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
149
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
150 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
151 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
152 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
153 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
154 // Fetch a FAT entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
155 bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
156 uint32_t lba;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
157 if (cluster > (clusterCount_ + 1)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
158 if (FAT12_SUPPORT && fatType_ == 12) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
159 uint16_t index = cluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
160 index += index >> 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
161 lba = fatStartBlock_ + (index >> 9);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
162 if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
163 index &= 0X1FF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
164 uint16_t tmp = cacheBuffer_.data[index];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
165 index++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
166 if (index == 512) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
167 if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
168 index = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
169 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
170 tmp |= cacheBuffer_.data[index] << 8;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
171 *value = cluster & 1 ? tmp >> 4 : tmp & 0XFFF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
172 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
173 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
174 if (fatType_ == 16) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
175 lba = fatStartBlock_ + (cluster >> 8);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
176 } else if (fatType_ == 32) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
177 lba = fatStartBlock_ + (cluster >> 7);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
178 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
179 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
180 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
181 if (lba != cacheBlockNumber_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
182 if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
183 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
184 if (fatType_ == 16) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
185 *value = cacheBuffer_.fat16[cluster & 0XFF];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
186 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
187 *value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
188 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
189 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
190
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
191 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
192 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
193 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
194 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
195 // Store a FAT entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
196 bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
197 uint32_t lba;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
198 // error if reserved cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
199 if (cluster < 2) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
200
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
201 // error if not in FAT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
202 if (cluster > (clusterCount_ + 1)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
203
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
204 if (FAT12_SUPPORT && fatType_ == 12) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
205 uint16_t index = cluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
206 index += index >> 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
207 lba = fatStartBlock_ + (index >> 9);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
208 if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
209 // mirror second FAT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
210 if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
211 index &= 0X1FF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
212 uint8_t tmp = value;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
213 if (cluster & 1) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
214 tmp = (cacheBuffer_.data[index] & 0XF) | tmp << 4;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
215 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
216 cacheBuffer_.data[index] = tmp;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
217 index++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
218 if (index == 512) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
219 lba++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
220 index = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
221 if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
222 // mirror second FAT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
223 if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
224 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
225 tmp = value >> 4;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
226 if (!(cluster & 1)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
227 tmp = ((cacheBuffer_.data[index] & 0XF0)) | tmp >> 4;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
228 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
229 cacheBuffer_.data[index] = tmp;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
230 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
231 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
232 if (fatType_ == 16) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
233 lba = fatStartBlock_ + (cluster >> 8);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
234 } else if (fatType_ == 32) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
235 lba = fatStartBlock_ + (cluster >> 7);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
236 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
237 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
238 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
239 if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
240 // store entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
241 if (fatType_ == 16) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
242 cacheBuffer_.fat16[cluster & 0XFF] = value;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
243 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
244 cacheBuffer_.fat32[cluster & 0X7F] = value;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
245 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
246 // mirror second FAT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
247 if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
248 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
249
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
250 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
251 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
252 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
253 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
254 // free a cluster chain
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
255 bool SdVolume::freeChain(uint32_t cluster) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
256 uint32_t next;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
257
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
258 // clear free cluster location
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
259 allocSearchStart_ = 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
260
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
261 do {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
262 if (!fatGet(cluster, &next)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
263
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
264 // free cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
265 if (!fatPut(cluster, 0)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
266
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
267 cluster = next;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
268 } while (!isEOC(cluster));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
269
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
270 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
271
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
272 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
273 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
274 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
275 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
276 /** Volume free space in clusters.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
277 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
278 * \return Count of free clusters for success or -1 if an error occurs.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
279 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
280 int32_t SdVolume::freeClusterCount() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
281 uint32_t free = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
282 uint16_t n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
283 uint32_t todo = clusterCount_ + 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
284
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
285 if (fatType_ == 16) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
286 n = 256;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
287 } else if (fatType_ == 32) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
288 n = 128;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
289 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
290 // put FAT12 here
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
291 return -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
292 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
293
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
294 for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
295 if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
296 if (todo < n) n = todo;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
297 if (fatType_ == 16) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
298 for (uint16_t i = 0; i < n; i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
299 if (cacheBuffer_.fat16[i] == 0) free++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
300 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
301 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
302 for (uint16_t i = 0; i < n; i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
303 if (cacheBuffer_.fat32[i] == 0) free++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
304 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
305 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
306 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
307 return free;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
308 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
309 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
310 /** Initialize a FAT volume.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
311 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
312 * \param[in] dev The SD card where the volume is located.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
313 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
314 * \param[in] part The partition to be used. Legal values for \a part are
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
315 * 1-4 to use the corresponding partition on a device formatted with
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
316 * a MBR, Master Boot Record, or zero if the device is formatted as
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
317 * a super floppy with the FAT boot sector in block zero.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
318 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
319 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
320 * the value zero, false, is returned for failure. Reasons for
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
321 * failure include not finding a valid partition, not finding a valid
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
322 * FAT file system in the specified partition or an I/O error.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
323 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
324 bool SdVolume::init(Sd2Card* dev, uint8_t part) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
325 uint32_t totalBlocks;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
326 uint32_t volumeStartBlock = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
327 fat32_boot_t* fbs;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
328
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
329 sdCard_ = dev;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
330 fatType_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
331 allocSearchStart_ = 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
332 cacheDirty_ = 0; // cacheFlush() will write block if true
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
333 cacheMirrorBlock_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
334 cacheBlockNumber_ = 0XFFFFFFFF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
335
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
336 // if part == 0 assume super floppy with FAT boot sector in block zero
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
337 // if part > 0 assume mbr volume with partition table
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
338 if (part) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
339 if (part > 4)goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
340 if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
341 part_t* p = &cacheBuffer_.mbr.part[part-1];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
342 if ((p->boot & 0X7F) !=0 ||
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
343 p->totalSectors < 100 ||
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
344 p->firstSector == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
345 // not a valid partition
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
346 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
347 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
348 volumeStartBlock = p->firstSector;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
349 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
350 if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
351 fbs = &cacheBuffer_.fbs32;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
352 if (fbs->bytesPerSector != 512 ||
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
353 fbs->fatCount == 0 ||
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
354 fbs->reservedSectorCount == 0 ||
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
355 fbs->sectorsPerCluster == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
356 // not valid FAT volume
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
357 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
358 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
359 fatCount_ = fbs->fatCount;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
360 blocksPerCluster_ = fbs->sectorsPerCluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
361 // determine shift that is same as multiply by blocksPerCluster_
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
362 clusterSizeShift_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
363 while (blocksPerCluster_ != (1 << clusterSizeShift_)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
364 // error if not power of 2
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
365 if (clusterSizeShift_++ > 7) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
366 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
367 blocksPerFat_ = fbs->sectorsPerFat16 ?
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
368 fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
369
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
370 fatStartBlock_ = volumeStartBlock + fbs->reservedSectorCount;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
371
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
372 // count for FAT16 zero for FAT32
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
373 rootDirEntryCount_ = fbs->rootDirEntryCount;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
374
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
375 // directory start for FAT16 dataStart for FAT32
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
376 rootDirStart_ = fatStartBlock_ + fbs->fatCount * blocksPerFat_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
377
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
378 // data start for FAT16 and FAT32
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
379 dataStartBlock_ = rootDirStart_ + ((32 * fbs->rootDirEntryCount + 511)/512);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
380
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
381 // total blocks for FAT16 or FAT32
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
382 totalBlocks = fbs->totalSectors16 ?
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
383 fbs->totalSectors16 : fbs->totalSectors32;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
384 // total data blocks
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
385 clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
386
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
387 // divide by cluster size to get cluster count
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
388 clusterCount_ >>= clusterSizeShift_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
389
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
390 // FAT type is determined by cluster count
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
391 if (clusterCount_ < 4085) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
392 fatType_ = 12;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
393 if (!FAT12_SUPPORT) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
394 } else if (clusterCount_ < 65525) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
395 fatType_ = 16;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
396 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
397 rootDirStart_ = fbs->fat32RootCluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
398 fatType_ = 32;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
399 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
400 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
401
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
402 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
403 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
404 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
405 #endif

mercurial