Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

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