forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiffs_api.cpp
514 lines (431 loc) · 14.7 KB
/
spiffs_api.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
spiffs_api.cpp - file system wrapper for SPIFFS
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
This code was influenced by NodeMCU and Sming libraries, and first version of
Arduino wrapper written by Hristo Gochkov.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "FS.h"
#undef max
#undef min
#include "FSImpl.h"
#include "spiffs/spiffs.h"
#include "debug.h"
#include <limits>
extern "C" {
#include "c_types.h"
#include "spi_flash.h"
}
using namespace fs;
extern int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src);
extern int32_t spiffs_hal_erase(uint32_t addr, uint32_t size);
extern int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst);
static int getSpiffsMode(OpenMode openMode, AccessMode accessMode);
static bool isSpiffsFilenameValid(const char* name);
class SPIFFSFileImpl;
class SPIFFSDirImpl;
class SPIFFSImpl : public FSImpl {
public:
SPIFFSImpl(uint32_t start, uint32_t size, uint32_t pageSize, uint32_t blockSize, uint32_t maxOpenFds)
: _fs({0})
, _start(start)
, _size(size)
, _pageSize(pageSize)
, _blockSize(blockSize)
, _maxOpenFds(maxOpenFds)
{
}
FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override;
bool exists(const char* path) override;
DirImplPtr openDir(const char* path) override;
bool rename(const char* pathFrom, const char* pathTo) override {
if (!isSpiffsFilenameValid(pathFrom)) {
DEBUGV("SPIFFSImpl::rename: invalid pathFrom=`%s`\r\n", pathFrom);
return false;
}
if (!isSpiffsFilenameValid(pathTo)) {
DEBUGV("SPIFFSImpl::rename: invalid pathTo=`%s` \r\n", pathTo);
return false;
}
auto rc = SPIFFS_rename(&_fs, pathFrom, pathTo);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_rename: rc=%d, from=`%s`, to=`%s`\r\n", rc,
pathFrom, pathTo);
return false;
}
return true;
}
bool info(FSInfo& info) override {
info.maxOpenFiles = _maxOpenFds;
info.blockSize = _blockSize;
info.pageSize = _pageSize;
info.maxOpenFiles = _maxOpenFds;
info.maxPathLength = SPIFFS_OBJ_NAME_LEN;
auto rc = SPIFFS_info(&_fs, &info.totalBytes, &info.usedBytes);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_info: rc=%d, err=%d\r\n", rc, _fs.err_code);
return false;
}
return true;
}
bool remove(const char* path) override {
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::remove: invalid path=`%s`\r\n", path);
return false;
}
auto rc = SPIFFS_remove(&_fs, path);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_remove: rc=%d path=`%s`\r\n", rc, path);
return false;
}
return true;
}
bool begin() override {
if (SPIFFS_mounted(&_fs) != 0) {
return true;
}
if (_size == 0) {
DEBUGV("SPIFFS size is zero");
return false;
}
if (_tryMount()) {
return true;
}
auto rc = SPIFFS_format(&_fs);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
return false;
}
return _tryMount();
}
bool format() override {
if (_size == 0) {
DEBUGV("SPIFFS size is zero");
return false;
}
bool wasMounted = (SPIFFS_mounted(&_fs) != 0);
if (_tryMount()) {
SPIFFS_unmount(&_fs);
}
auto rc = SPIFFS_format(&_fs);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
return false;
}
if (wasMounted) {
return _tryMount();
}
return true;
}
protected:
friend class SPIFFSFileImpl;
friend class SPIFFSDirImpl;
spiffs* getFs() {
return &_fs;
}
bool _tryMount() {
spiffs_config config = {0};
config.hal_read_f = &spiffs_hal_read;
config.hal_write_f = &spiffs_hal_write;
config.hal_erase_f = &spiffs_hal_erase;
config.phys_size = _size;
config.phys_addr = _start;
config.phys_erase_block = SPI_FLASH_SEC_SIZE;
config.log_block_size = _blockSize;
config.log_page_size = _pageSize;
if (((uint32_t) std::numeric_limits<spiffs_block_ix>::max()) < (_size / _blockSize)) {
DEBUGV("spiffs_block_ix type too small");
abort();
}
if (((uint32_t) std::numeric_limits<spiffs_page_ix>::max()) < (_size / _pageSize)) {
DEBUGV("spiffs_page_ix type too small");
abort();
}
if (((uint32_t) std::numeric_limits<spiffs_obj_id>::max()) < (2 + (_size / (2*_pageSize))*2)) {
DEBUGV("spiffs_obj_id type too small");
abort();
}
if (((uint32_t) std::numeric_limits<spiffs_span_ix>::max()) < (_size / _pageSize - 1)) {
DEBUGV("spiffs_span_ix type too small");
abort();
}
// hack: even though fs is not initialized at this point,
// SPIFFS_buffer_bytes_for_cache uses only fs->config.log_page_size
// suggestion: change SPIFFS_buffer_bytes_for_cache to take
// spiffs_config* instead of spiffs* as an argument
_fs.cfg.log_page_size = config.log_page_size;
size_t workBufSize = 2 * _pageSize;
size_t fdsBufSize = SPIFFS_buffer_bytes_for_filedescs(&_fs, _maxOpenFds);
size_t cacheBufSize = SPIFFS_buffer_bytes_for_cache(&_fs, _maxOpenFds);
if (!_workBuf) {
DEBUGV("SPIFFSImpl: allocating %d+%d+%d=%d bytes\r\n",
workBufSize, fdsBufSize, cacheBufSize,
workBufSize + fdsBufSize + cacheBufSize);
_workBuf.reset(new uint8_t[workBufSize]);
_fdsBuf.reset(new uint8_t[fdsBufSize]);
_cacheBuf.reset(new uint8_t[cacheBufSize]);
}
DEBUGV("SPIFFSImpl: mounting fs @%x, size=%x, block=%x, page=%x\r\n",
_start, _size, _blockSize, _pageSize);
auto err = SPIFFS_mount(&_fs, &config, _workBuf.get(),
_fdsBuf.get(), fdsBufSize, _cacheBuf.get(), cacheBufSize,
&SPIFFSImpl::_check_cb);
DEBUGV("SPIFFSImpl: mount rc=%d\r\n", err);
return err == SPIFFS_OK;
}
static void _check_cb(spiffs_check_type type, spiffs_check_report report,
uint32_t arg1, uint32_t arg2) {
// TODO: spiffs doesn't pass any context pointer along with _check_cb,
// so we can't do anything useful here other than perhaps
// feeding the watchdog
}
spiffs _fs;
uint32_t _start;
uint32_t _size;
uint32_t _pageSize;
uint32_t _blockSize;
uint32_t _maxOpenFds;
std::unique_ptr<uint8_t[]> _workBuf;
std::unique_ptr<uint8_t[]> _fdsBuf;
std::unique_ptr<uint8_t[]> _cacheBuf;
};
#define CHECKFD() while (_fd == 0) { panic(); }
class SPIFFSFileImpl : public FileImpl {
public:
SPIFFSFileImpl(SPIFFSImpl* fs, spiffs_file fd)
: _fs(fs)
, _fd(fd)
, _stat({0})
, _written(false)
{
_getStat();
}
~SPIFFSFileImpl() override {
close();
}
size_t write(const uint8_t *buf, size_t size) override {
CHECKFD();
auto result = SPIFFS_write(_fs->getFs(), _fd, (void*) buf, size);
if (result < 0) {
DEBUGV("SPIFFS_write rc=%d\r\n", result);
return 0;
}
_written = true;
return result;
}
size_t read(uint8_t* buf, size_t size) override {
CHECKFD();
auto result = SPIFFS_read(_fs->getFs(), _fd, (void*) buf, size);
if (result < 0) {
DEBUGV("SPIFFS_read rc=%d\r\n", result);
return 0;
}
return result;
}
void flush() override {
CHECKFD();
auto rc = SPIFFS_fflush(_fs->getFs(), _fd);
if (rc < 0) {
DEBUGV("SPIFFS_fflush rc=%d\r\n", rc);
}
_written = true;
}
bool seek(uint32_t pos, SeekMode mode) override {
CHECKFD();
int32_t offset = static_cast<int32_t>(pos);
if (mode == SeekEnd) {
offset = -offset;
}
auto rc = SPIFFS_lseek(_fs->getFs(), _fd, pos, (int) mode);
if (rc < 0) {
DEBUGV("SPIFFS_lseek rc=%d\r\n", rc);
return false;
}
return true;
}
size_t position() const override {
CHECKFD();
auto result = SPIFFS_lseek(_fs->getFs(), _fd, 0, SPIFFS_SEEK_CUR);
if (result < 0) {
DEBUGV("SPIFFS_tell rc=%d\r\n", result);
return 0;
}
return result;
}
size_t size() const override {
CHECKFD();
if (_written) {
_getStat();
}
return _stat.size;
}
void close() override {
CHECKFD();
SPIFFS_close(_fs->getFs(), _fd);
DEBUGV("SPIFFS_close: fd=%d\r\n", _fd);
}
const char* name() const override {
CHECKFD();
return (const char*) _stat.name;
}
protected:
void _getStat() const{
CHECKFD();
auto rc = SPIFFS_fstat(_fs->getFs(), _fd, &_stat);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_fstat rc=%d\r\n", rc);
_stat = {0};
}
_written = false;
}
SPIFFSImpl* _fs;
spiffs_file _fd;
mutable spiffs_stat _stat;
mutable bool _written;
};
class SPIFFSDirImpl : public DirImpl {
public:
SPIFFSDirImpl(const String& pattern, SPIFFSImpl* fs, spiffs_DIR& dir)
: _pattern(pattern)
, _fs(fs)
, _dir(dir)
, _valid(false)
{
}
~SPIFFSDirImpl() override {
SPIFFS_closedir(&_dir);
}
FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) override {
if (!_valid) {
return FileImplPtr();
}
int mode = getSpiffsMode(openMode, accessMode);
auto fs = _fs->getFs();
spiffs_file fd = SPIFFS_open_by_dirent(fs, &_dirent, mode, 0);
if (fd < 0) {
DEBUGV("SPIFFSDirImpl::openFile: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n",
fd, _dirent.name, openMode, accessMode, fs->err_code);
return FileImplPtr();
}
return std::make_shared<SPIFFSFileImpl>(_fs, fd);
}
const char* fileName() override {
if (!_valid)
return nullptr;
return (const char*) _dirent.name;
}
size_t fileSize() override {
if (!_valid)
return 0;
return _dirent.size;
}
bool next() override {
const int n = _pattern.length();
do {
spiffs_dirent* result = SPIFFS_readdir(&_dir, &_dirent);
_valid = (result != nullptr);
} while(_valid && strncmp((const char*) _dirent.name, _pattern.c_str(), n) != 0);
return _valid;
}
protected:
String _pattern;
SPIFFSImpl* _fs;
spiffs_DIR _dir;
spiffs_dirent _dirent;
bool _valid;
};
FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) {
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::open: invalid path=`%s` \r\n", path);
return FileImplPtr();
}
int mode = getSpiffsMode(openMode, accessMode);
int fd = SPIFFS_open(&_fs, path, mode, 0);
if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE)) {
DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d, trying to remove\r\n",
fd, path, openMode, accessMode, _fs.err_code);
auto rc = SPIFFS_remove(&_fs, path);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFSImpl::open: SPIFFS_ERR_DELETED, but failed to remove path=`%s` openMode=%d accessMode=%d err=%d\r\n",
path, openMode, accessMode, _fs.err_code);
return FileImplPtr();
}
fd = SPIFFS_open(&_fs, path, mode, 0);
}
if (fd < 0) {
DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n",
fd, path, openMode, accessMode, _fs.err_code);
return FileImplPtr();
}
return std::make_shared<SPIFFSFileImpl>(this, fd);
}
bool SPIFFSImpl::exists(const char* path) {
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::exists: invalid path=`%s` \r\n", path);
return false;
}
spiffs_stat stat;
int rc = SPIFFS_stat(&_fs, path, &stat);
return rc == SPIFFS_OK;
}
DirImplPtr SPIFFSImpl::openDir(const char* path) {
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::openDir: invalid path=`%s` \r\n", path);
return DirImplPtr();
}
spiffs_DIR dir;
spiffs_DIR* result = SPIFFS_opendir(&_fs, path, &dir);
if (!result) {
DEBUGV("SPIFFSImpl::openDir: path=`%s` err=%d\r\n", path, _fs.err_code);
return DirImplPtr();
}
return std::make_shared<SPIFFSDirImpl>(path, this, dir);
}
static int getSpiffsMode(OpenMode openMode, AccessMode accessMode) {
int mode = 0;
if (openMode & OM_CREATE) {
mode |= SPIFFS_CREAT;
}
if (openMode & OM_APPEND) {
mode |= SPIFFS_APPEND;
}
if (openMode & OM_TRUNCATE) {
mode |= SPIFFS_TRUNC;
}
if (accessMode & AM_READ) {
mode |= SPIFFS_RDONLY;
}
if (accessMode & AM_WRITE) {
mode |= SPIFFS_WRONLY;
}
return mode;
}
static bool isSpiffsFilenameValid(const char* name) {
if (name == nullptr)
return false;
auto len = strlen(name);
return len > 0 && len < SPIFFS_OBJ_NAME_LEN;
}
// these symbols should be defined in the linker script for each flash layout
extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _SPIFFS_end;
extern "C" uint32_t _SPIFFS_page;
extern "C" uint32_t _SPIFFS_block;
static SPIFFSImpl s_defaultFs(
(uint32_t) (&_SPIFFS_start) - 0x40200000,
(uint32_t) (&_SPIFFS_end) - (uint32_t) (&_SPIFFS_start),
(uint32_t) &_SPIFFS_page,
(uint32_t) &_SPIFFS_block,
5);
FS SPIFFS = FS(FSImplPtr(&s_defaultFs));