Skip to content

Commit 5762712

Browse files
Bring SPIFFS up to latest version (#7276)
Manually merged the PATCH files from upstream up until Jan 26, 2020. Looks to only fix a single bug related to reading past the end of a file in certain cases. Other new features (secure erase) are included to make it easier to merge any future releases, but are not enabled in the core. Merges included: 20fc6df0ab2bee391961b5f823a327887abc96b4 da1cf494796d68bb5c02ba70cf78a904db08a170 13935985cdde6d6b4ec77f2685264242ee55e7ac ec68ba8208d7550860e4e78299d58a529b88bf85 f59d958e0b2f169b549e5cdc293a70bc6873cd45 f7d3e9f2b207958dfc2c01cf3fc42f98a4b9b239 554b59c147d96223ccce18374989f191f7222b45 07e013056dd976d12ae36db5c54a275e1497f6c8 d0d44c4908efff7a443ed9b1174d9173fb784a72 3cb24dbfd32a396dca46a7ca56a8e75bdcb97078 8172b40813ff36aa0df4c88a6e9c76e5300d7e32
1 parent 7c008e3 commit 5762712

File tree

5 files changed

+66
-14
lines changed

5 files changed

+66
-14
lines changed

cores/esp8266/spiffs/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ What spiffs does not:
4949
- Presently, it does not detect or handle bad blocks.
5050
- One configuration, one binary. There's no generic spiffs binary that handles all types of configurations.
5151

52+
## NOTICE
53+
54+
0.4.0 is under construction. This is a full rewrite and will change the underlying structure. Hence, it will not be compatible with earlier versions of the filesystem. The API is the same, with minor modifications. Some config flags will be removed (as they are mandatory in 0.4.0) and some features might fall away until 0.4.1. If you have any worries or questions, it can be discussed in issue [#179](https://github.com/pellepl/spiffs/issues/179)
5255

5356
## MORE INFO
5457

cores/esp8266/spiffs/spiffs_check.cpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,17 @@ static s32_t spiffs_delete_obj_lazy(spiffs *fs, spiffs_obj_id obj_id) {
163163
return SPIFFS_OK;
164164
}
165165
SPIFFS_CHECK_RES(res);
166-
u8_t flags = 0xff & ~SPIFFS_PH_FLAG_IXDELE;
166+
u8_t flags = 0xff;
167+
#if SPIFFS_NO_BLIND_WRITES
168+
res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,
169+
0, SPIFFS_PAGE_TO_PADDR(fs, objix_hdr_pix) + offsetof(spiffs_page_header, flags),
170+
sizeof(flags), &flags);
171+
SPIFFS_CHECK_RES(res);
172+
#endif
173+
flags &= ~SPIFFS_PH_FLAG_IXDELE;
167174
res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,
168175
0, SPIFFS_PAGE_TO_PADDR(fs, objix_hdr_pix) + offsetof(spiffs_page_header, flags),
169-
sizeof(u8_t),
170-
(u8_t *)&flags);
176+
sizeof(flags), &flags);
171177
return res;
172178
}
173179

@@ -425,10 +431,17 @@ static s32_t spiffs_lookup_check_validate(spiffs *fs, spiffs_obj_id lu_obj_id, s
425431
// just finalize
426432
SPIFFS_CHECK_DBG("LU: FIXUP: unfinalized page is referred, finalizing\n");
427433
CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_LOOKUP, p_hdr->obj_id, p_hdr->span_ix);
428-
u8_t flags = 0xff & ~SPIFFS_PH_FLAG_FINAL;
434+
u8_t flags = 0xff;
435+
#if SPIFFS_NO_BLIND_WRITES
436+
res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,
437+
0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix) + offsetof(spiffs_page_header, flags),
438+
sizeof(flags), &flags);
439+
SPIFFS_CHECK_RES(res);
440+
#endif
441+
flags &= ~SPIFFS_PH_FLAG_FINAL;
429442
res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
430443
0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix) + offsetof(spiffs_page_header, flags),
431-
sizeof(u8_t), (u8_t*)&flags);
444+
sizeof(flags), &flags);
432445
}
433446
}
434447
}

cores/esp8266/spiffs/spiffs_config.h

+21
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ typedef uint8_t u8_t;
326326
#define SPIFFS_IX_MAP 1
327327
#endif
328328

329+
// By default SPIFFS in some cases relies on the property of NOR flash that bits
330+
// cannot be set from 0 to 1 by writing and that controllers will ignore such
331+
// bit changes. This results in fewer reads as SPIFFS can in some cases perform
332+
// blind writes, with all bits set to 1 and only those it needs reset set to 0.
333+
// Most of the chips and controllers allow this behavior, so the default is to
334+
// use this technique. If your controller is one of the rare ones that don't,
335+
// turn this option on and SPIFFS will perform a read-modify-write instead.
336+
#ifndef SPIFFS_NO_BLIND_WRITES
337+
#define SPIFFS_NO_BLIND_WRITES 0
338+
#endif
339+
329340
// Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
330341
// in the api. This function will visualize all filesystem using given printf
331342
// function.
@@ -354,11 +365,20 @@ typedef uint8_t u8_t;
354365
#endif
355366
#endif
356367

368+
#ifndef SPIFFS_SECURE_ERASE
369+
#define SPIFFS_SECURE_ERASE 0
370+
#endif
371+
357372
// Types depending on configuration such as the amount of flash bytes
358373
// given to spiffs file system in total (spiffs_file_system_size),
359374
// the logical block size (log_block_size), and the logical page size
360375
// (log_page_size)
376+
//
377+
// Set SPIFFS_TYPES_OVERRIDE if you wish to have your own
378+
// definitions for these types (for example, if you want them
379+
// to be u32_t)
361380

381+
#ifndef SPIFFS_TYPES_OVERRIDE
362382
// Block index type. Make sure the size of this type can hold
363383
// the highest number of all blocks - i.e. spiffs_file_system_size / log_block_size
364384
typedef u16_t spiffs_block_ix;
@@ -373,5 +393,6 @@ typedef u16_t spiffs_obj_id;
373393
// hold the largest possible span index on the system -
374394
// i.e. (spiffs_file_system_size / log_page_size) - 1
375395
typedef u16_t spiffs_span_ix;
396+
#endif
376397

377398
#endif /* SPIFFS_CONFIG_H_ */

cores/esp8266/spiffs/spiffs_nucleus.cpp

+20-5
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,6 @@ s32_t spiffs_page_delete(
879879
spiffs *fs,
880880
spiffs_page_ix pix) {
881881
s32_t res;
882-
spiffs_page_header hdr;
883-
hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED);
884882
// mark deleted entry in source object lookup
885883
spiffs_obj_id d_obj_id = SPIFFS_OBJ_ID_DELETED;
886884
res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_DELE,
@@ -893,12 +891,29 @@ s32_t spiffs_page_delete(
893891
fs->stats_p_deleted++;
894892
fs->stats_p_allocated--;
895893

894+
#if SPIFFS_SECURE_ERASE
895+
// Secure erase
896+
unsigned char data[SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header)];
897+
bzero(data, sizeof(data));
898+
res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_DELE,
899+
0,
900+
SPIFFS_PAGE_TO_PADDR(fs, pix) + sizeof(spiffs_page_header), sizeof(data), data);
901+
SPIFFS_CHECK_RES(res);
902+
#endif
903+
896904
// mark deleted in source page
905+
u8_t flags = 0xff;
906+
#if SPIFFS_NO_BLIND_WRITES
907+
res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,
908+
0, SPIFFS_PAGE_TO_PADDR(fs, pix) + offsetof(spiffs_page_header, flags),
909+
sizeof(flags), &flags);
910+
SPIFFS_CHECK_RES(res);
911+
#endif
912+
flags &= ~(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED);
897913
res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_DELE,
898914
0,
899915
SPIFFS_PAGE_TO_PADDR(fs, pix) + offsetof(spiffs_page_header, flags),
900-
sizeof(u8_t),
901-
(u8_t *)&hdr.flags);
916+
sizeof(flags), &flags);
902917

903918
return res;
904919
}
@@ -2027,7 +2042,7 @@ s32_t spiffs_object_read(
20272042
// remaining data in page
20282043
len_to_read = MIN(len_to_read, SPIFFS_DATA_PAGE_SIZE(fs) - (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)));
20292044
// remaining data in file
2030-
len_to_read = MIN(len_to_read, fd->size);
2045+
len_to_read = MIN(len_to_read, fd->size - cur_offset);
20312046
SPIFFS_DBG("read: offset:" _SPIPRIi " rd:" _SPIPRIi " data spix:" _SPIPRIsp " is data_pix:" _SPIPRIpg " addr:" _SPIPRIad "\n", cur_offset, len_to_read, data_spix, data_pix,
20322047
(u32_t)(SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs))));
20332048
if (len_to_read <= 0) {

cores/esp8266/spiffs/spiffs_nucleus.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ extern "C" {
147147

148148

149149

150-
#if defined(__GNUC__) || defined(__clang__)
151-
/* For GCC and clang */
150+
#if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__)
151+
/* For GCC, clang and TI compilers */
152152
#define SPIFFS_PACKED __attribute__((packed))
153153
#elif defined(__ICCARM__) || defined(__CC_ARM)
154154
/* For IAR ARM and Keil MDK-ARM compilers */
@@ -266,8 +266,8 @@ extern "C" {
266266
#define SPIFFS_FH_OFFS(fs, fh) ((fh) != 0 ? ((fh) + (fs)->cfg.fh_ix_offset) : 0)
267267
#define SPIFFS_FH_UNOFFS(fs, fh) ((fh) != 0 ? ((fh) - (fs)->cfg.fh_ix_offset) : 0)
268268
#else
269-
#define SPIFFS_FH_OFFS(fs, fh) (fh)
270-
#define SPIFFS_FH_UNOFFS(fs, fh) (fh)
269+
#define SPIFFS_FH_OFFS(fs, fh) ((spiffs_file)(fh))
270+
#define SPIFFS_FH_UNOFFS(fs, fh) ((spiffs_file)(fh))
271271
#endif
272272

273273

0 commit comments

Comments
 (0)