From d2944bd91807ecc8f178d851fd35c3d21e880de0 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Wed, 6 May 2020 09:03:00 -0700 Subject: [PATCH] Bring SPIFFS up to latest version 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 --- cores/esp8266/spiffs/README.md | 3 +++ cores/esp8266/spiffs/spiffs_check.cpp | 23 ++++++++++++++++++----- cores/esp8266/spiffs/spiffs_config.h | 21 +++++++++++++++++++++ cores/esp8266/spiffs/spiffs_nucleus.cpp | 25 ++++++++++++++++++++----- cores/esp8266/spiffs/spiffs_nucleus.h | 8 ++++---- 5 files changed, 66 insertions(+), 14 deletions(-) diff --git a/cores/esp8266/spiffs/README.md b/cores/esp8266/spiffs/README.md index 64ffcc36b3..e75ec5c30f 100644 --- a/cores/esp8266/spiffs/README.md +++ b/cores/esp8266/spiffs/README.md @@ -49,6 +49,9 @@ What spiffs does not: - Presently, it does not detect or handle bad blocks. - One configuration, one binary. There's no generic spiffs binary that handles all types of configurations. +## NOTICE + +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) ## MORE INFO diff --git a/cores/esp8266/spiffs/spiffs_check.cpp b/cores/esp8266/spiffs/spiffs_check.cpp index 258efb30c7..73477ed6a0 100644 --- a/cores/esp8266/spiffs/spiffs_check.cpp +++ b/cores/esp8266/spiffs/spiffs_check.cpp @@ -163,11 +163,17 @@ static s32_t spiffs_delete_obj_lazy(spiffs *fs, spiffs_obj_id obj_id) { return SPIFFS_OK; } SPIFFS_CHECK_RES(res); - u8_t flags = 0xff & ~SPIFFS_PH_FLAG_IXDELE; + u8_t flags = 0xff; +#if SPIFFS_NO_BLIND_WRITES + res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, + 0, SPIFFS_PAGE_TO_PADDR(fs, objix_hdr_pix) + offsetof(spiffs_page_header, flags), + sizeof(flags), &flags); + SPIFFS_CHECK_RES(res); +#endif + flags &= ~SPIFFS_PH_FLAG_IXDELE; res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT, 0, SPIFFS_PAGE_TO_PADDR(fs, objix_hdr_pix) + offsetof(spiffs_page_header, flags), - sizeof(u8_t), - (u8_t *)&flags); + sizeof(flags), &flags); return res; } @@ -425,10 +431,17 @@ static s32_t spiffs_lookup_check_validate(spiffs *fs, spiffs_obj_id lu_obj_id, s // just finalize SPIFFS_CHECK_DBG("LU: FIXUP: unfinalized page is referred, finalizing\n"); CHECK_CB(fs, SPIFFS_CHECK_LOOKUP, SPIFFS_CHECK_FIX_LOOKUP, p_hdr->obj_id, p_hdr->span_ix); - u8_t flags = 0xff & ~SPIFFS_PH_FLAG_FINAL; + u8_t flags = 0xff; +#if SPIFFS_NO_BLIND_WRITES + res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ, + 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix) + offsetof(spiffs_page_header, flags), + sizeof(flags), &flags); + SPIFFS_CHECK_RES(res); +#endif + flags &= ~SPIFFS_PH_FLAG_FINAL; res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT, 0, SPIFFS_PAGE_TO_PADDR(fs, cur_pix) + offsetof(spiffs_page_header, flags), - sizeof(u8_t), (u8_t*)&flags); + sizeof(flags), &flags); } } } diff --git a/cores/esp8266/spiffs/spiffs_config.h b/cores/esp8266/spiffs/spiffs_config.h index c3343fe2fe..c0d87c47bf 100644 --- a/cores/esp8266/spiffs/spiffs_config.h +++ b/cores/esp8266/spiffs/spiffs_config.h @@ -326,6 +326,17 @@ typedef uint8_t u8_t; #define SPIFFS_IX_MAP 1 #endif +// By default SPIFFS in some cases relies on the property of NOR flash that bits +// cannot be set from 0 to 1 by writing and that controllers will ignore such +// bit changes. This results in fewer reads as SPIFFS can in some cases perform +// blind writes, with all bits set to 1 and only those it needs reset set to 0. +// Most of the chips and controllers allow this behavior, so the default is to +// use this technique. If your controller is one of the rare ones that don't, +// turn this option on and SPIFFS will perform a read-modify-write instead. +#ifndef SPIFFS_NO_BLIND_WRITES +#define SPIFFS_NO_BLIND_WRITES 0 +#endif + // Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function // in the api. This function will visualize all filesystem using given printf // function. @@ -354,11 +365,20 @@ typedef uint8_t u8_t; #endif #endif +#ifndef SPIFFS_SECURE_ERASE +#define SPIFFS_SECURE_ERASE 0 +#endif + // Types depending on configuration such as the amount of flash bytes // given to spiffs file system in total (spiffs_file_system_size), // the logical block size (log_block_size), and the logical page size // (log_page_size) +// +// Set SPIFFS_TYPES_OVERRIDE if you wish to have your own +// definitions for these types (for example, if you want them +// to be u32_t) +#ifndef SPIFFS_TYPES_OVERRIDE // Block index type. Make sure the size of this type can hold // the highest number of all blocks - i.e. spiffs_file_system_size / log_block_size typedef u16_t spiffs_block_ix; @@ -373,5 +393,6 @@ typedef u16_t spiffs_obj_id; // hold the largest possible span index on the system - // i.e. (spiffs_file_system_size / log_page_size) - 1 typedef u16_t spiffs_span_ix; +#endif #endif /* SPIFFS_CONFIG_H_ */ diff --git a/cores/esp8266/spiffs/spiffs_nucleus.cpp b/cores/esp8266/spiffs/spiffs_nucleus.cpp index 35238f351b..81a45f6eb9 100644 --- a/cores/esp8266/spiffs/spiffs_nucleus.cpp +++ b/cores/esp8266/spiffs/spiffs_nucleus.cpp @@ -879,8 +879,6 @@ s32_t spiffs_page_delete( spiffs *fs, spiffs_page_ix pix) { s32_t res; - spiffs_page_header hdr; - hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED); // mark deleted entry in source object lookup spiffs_obj_id d_obj_id = SPIFFS_OBJ_ID_DELETED; res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_DELE, @@ -893,12 +891,29 @@ s32_t spiffs_page_delete( fs->stats_p_deleted++; fs->stats_p_allocated--; +#if SPIFFS_SECURE_ERASE + // Secure erase + unsigned char data[SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header)]; + bzero(data, sizeof(data)); + res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_DELE, + 0, + SPIFFS_PAGE_TO_PADDR(fs, pix) + sizeof(spiffs_page_header), sizeof(data), data); + SPIFFS_CHECK_RES(res); +#endif + // mark deleted in source page + u8_t flags = 0xff; +#if SPIFFS_NO_BLIND_WRITES + res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ, + 0, SPIFFS_PAGE_TO_PADDR(fs, pix) + offsetof(spiffs_page_header, flags), + sizeof(flags), &flags); + SPIFFS_CHECK_RES(res); +#endif + flags &= ~(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED); res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_DELE, 0, SPIFFS_PAGE_TO_PADDR(fs, pix) + offsetof(spiffs_page_header, flags), - sizeof(u8_t), - (u8_t *)&hdr.flags); + sizeof(flags), &flags); return res; } @@ -2027,7 +2042,7 @@ s32_t spiffs_object_read( // remaining data in page len_to_read = MIN(len_to_read, SPIFFS_DATA_PAGE_SIZE(fs) - (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs))); // remaining data in file - len_to_read = MIN(len_to_read, fd->size); + len_to_read = MIN(len_to_read, fd->size - cur_offset); 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, (u32_t)(SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)))); if (len_to_read <= 0) { diff --git a/cores/esp8266/spiffs/spiffs_nucleus.h b/cores/esp8266/spiffs/spiffs_nucleus.h index cc195c7eca..a8b4af062e 100644 --- a/cores/esp8266/spiffs/spiffs_nucleus.h +++ b/cores/esp8266/spiffs/spiffs_nucleus.h @@ -147,8 +147,8 @@ extern "C" { -#if defined(__GNUC__) || defined(__clang__) - /* For GCC and clang */ +#if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__) + /* For GCC, clang and TI compilers */ #define SPIFFS_PACKED __attribute__((packed)) #elif defined(__ICCARM__) || defined(__CC_ARM) /* For IAR ARM and Keil MDK-ARM compilers */ @@ -266,8 +266,8 @@ extern "C" { #define SPIFFS_FH_OFFS(fs, fh) ((fh) != 0 ? ((fh) + (fs)->cfg.fh_ix_offset) : 0) #define SPIFFS_FH_UNOFFS(fs, fh) ((fh) != 0 ? ((fh) - (fs)->cfg.fh_ix_offset) : 0) #else -#define SPIFFS_FH_OFFS(fs, fh) (fh) -#define SPIFFS_FH_UNOFFS(fs, fh) (fh) +#define SPIFFS_FH_OFFS(fs, fh) ((spiffs_file)(fh)) +#define SPIFFS_FH_UNOFFS(fs, fh) ((spiffs_file)(fh)) #endif