diff --git a/app/platform/flash_api.c b/app/platform/flash_api.c index 7a918041b8..5f9772f6ab 100644 --- a/app/platform/flash_api.c +++ b/app/platform/flash_api.c @@ -4,8 +4,8 @@ * 2014-12-31 *******************************************************************************/ #include "user_config.h" -#include "osapi.h" #include "flash_api.h" +#include "spi_flash.h" #include "c_stdio.h" static volatile const uint8_t flash_init_data[128] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR = @@ -103,40 +103,43 @@ uint8_t flash_rom_get_size_type(void) uint32_t flash_rom_get_size_byte(void) { static uint32_t flash_size = 0; - switch (flash_rom_getinfo().size) + if (flash_size == 0) { - case SIZE_2MBIT: - // 2Mbit, 256kByte - flash_size = 256 * 1024; - break; - case SIZE_4MBIT: - // 4Mbit, 512kByte - flash_size = 512 * 1024; - break; - case SIZE_8MBIT: - // 8Mbit, 1MByte - flash_size = 1 * 1024 * 1024; - break; - case SIZE_16MBIT: - // 16Mbit, 2MByte - flash_size = 2 * 1024 * 1024; - break; - case SIZE_32MBIT: - // 32Mbit, 4MByte - flash_size = 4 * 1024 * 1024; - break; - case SIZE_64MBIT: - // 64Mbit, 8MByte - flash_size = 8 * 1024 * 1024; - break; - case SIZE_128MBIT: - // 128Mbit, 16MByte - flash_size = 16 * 1024 * 1024; - break; - default: - // Unknown flash size, fall back mode. - flash_size = 512 * 1024; - break; + switch (flash_rom_getinfo().size) + { + case SIZE_2MBIT: + // 2Mbit, 256kByte + flash_size = 256 * 1024; + break; + case SIZE_4MBIT: + // 4Mbit, 512kByte + flash_size = 512 * 1024; + break; + case SIZE_8MBIT: + // 8Mbit, 1MByte + flash_size = 1 * 1024 * 1024; + break; + case SIZE_16MBIT: + // 16Mbit, 2MByte + flash_size = 2 * 1024 * 1024; + break; + case SIZE_32MBIT: + // 32Mbit, 4MByte + flash_size = 4 * 1024 * 1024; + break; + case SIZE_64MBIT: + // 64Mbit, 8MByte + flash_size = 8 * 1024 * 1024; + break; + case SIZE_128MBIT: + // 128Mbit, 16MByte + flash_size = 16 * 1024 * 1024; + break; + default: + // Unknown flash size, fall back mode. + flash_size = 512 * 1024; + break; + } } return flash_size; } @@ -146,21 +149,21 @@ bool flash_rom_set_size_type(uint8_t size) // Dangerous, here are dinosaur infested!!!!! // Reboot required!!! // If you don't know what you're doing, your nodemcu may turn into stone ... - FLASH_DEBUG("\nBEGIN SET FLASH HEADER\n"); + NODE_DBG("\nBEGIN SET FLASH HEADER\n"); uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR; if (SPI_FLASH_RESULT_OK == spi_flash_read(0, (uint32 *)data, SPI_FLASH_SEC_SIZE)) { ((SPIFlashInfo *)(&data[0]))->size = size; if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector(0 * SPI_FLASH_SEC_SIZE)) { - FLASH_DEBUG("\nERASE SUCCESS\n"); + NODE_DBG("\nERASE SUCCESS\n"); } if (SPI_FLASH_RESULT_OK == spi_flash_write(0, (uint32 *)data, SPI_FLASH_SEC_SIZE)) { - FLASH_DEBUG("\nWRITE SUCCESS, %u\n", size); + NODE_DBG("\nWRITE SUCCESS, %u\n", size); } } - FLASH_DEBUG("\nEND SET FLASH HEADER\n"); + NODE_DBG("\nEND SET FLASH HEADER\n"); return true; } @@ -278,7 +281,7 @@ bool flash_rom_set_speed(uint32_t speed) // Dangerous, here are dinosaur infested!!!!! // Reboot required!!! // If you don't know what you're doing, your nodemcu may turn into stone ... - FLASH_DEBUG("\nBEGIN SET FLASH HEADER\n"); + NODE_DBG("\nBEGIN SET FLASH HEADER\n"); uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR; uint8_t speed_type = SPEED_40MHZ; if (speed < 26700000) @@ -302,14 +305,14 @@ bool flash_rom_set_speed(uint32_t speed) ((SPIFlashInfo *)(&data[0]))->speed = speed_type; if (SPI_FLASH_RESULT_OK == spi_flash_erase_sector(0 * SPI_FLASH_SEC_SIZE)) { - FLASH_DEBUG("\nERASE SUCCESS\n"); + NODE_DBG("\nERASE SUCCESS\n"); } if (SPI_FLASH_RESULT_OK == spi_flash_write(0, (uint32 *)data, SPI_FLASH_SEC_SIZE)) { - FLASH_DEBUG("\nWRITE SUCCESS, %u\n", speed_type); + NODE_DBG("\nWRITE SUCCESS, %u\n", speed_type); } } - FLASH_DEBUG("\nEND SET FLASH HEADER\n"); + NODE_DBG("\nEND SET FLASH HEADER\n"); return true; } @@ -390,7 +393,7 @@ uint8_t byte_of_aligned_array(const uint8_t *aligned_array, uint32_t index) { if ( (((uint32_t)aligned_array) % 4) != 0 ) { - FLASH_DEBUG("aligned_array is not 4-byte aligned.\n"); + NODE_DBG("aligned_array is not 4-byte aligned.\n"); return 0; } volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 4 ]; @@ -402,7 +405,7 @@ uint16_t word_of_aligned_array(const uint16_t *aligned_array, uint32_t index) { if ( (((uint32_t)aligned_array) % 4) != 0 ) { - FLASH_DEBUG("aligned_array is not 4-byte aligned.\n"); + NODE_DBG("aligned_array is not 4-byte aligned.\n"); return 0; } volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 2 ]; diff --git a/app/platform/flash_api.h b/app/platform/flash_api.h index 7c09aeb099..0063ee1c4b 100644 --- a/app/platform/flash_api.h +++ b/app/platform/flash_api.h @@ -1,8 +1,8 @@ #ifndef __FLASH_API_H__ #define __FLASH_API_H__ +#include "ets_sys.h" #include "user_config.h" -#include "user_interface.h" -#include "spi_flash.h" +#include "cpu_esp8266.h" #define FLASH_ADDRESS_START_MAP (INTERNAL_FLASH_START_ADDRESS) @@ -22,8 +22,6 @@ #define FLASH_SIZE_8MBYTE (FLASH_SIZE_64MBIT / 8) #define FLASH_SIZE_16MBYTE (FLASH_SIZE_128MBIT/ 8) -#define FLASH_DEBUG - #define FLASH_SAFEMODE_ENTER() \ do { \ extern SpiFlashChip * flashchip; \ @@ -87,7 +85,7 @@ typedef struct } size : 4; uint32_t entry_point; uint32_t memory_offset; - uint32_t segment_size; + uint32_t segment_size; } ICACHE_STORE_TYPEDEF_ATTR SPIFlashInfo; uint32_t flash_detect_size_byte(void); diff --git a/app/spiffs/docs/INTEGRATION b/app/spiffs/docs/INTEGRATION index 20ff70d052..085ed8fc18 100644 --- a/app/spiffs/docs/INTEGRATION +++ b/app/spiffs/docs/INTEGRATION @@ -303,4 +303,4 @@ Having these figures you can disable SPIFFS_BUFFER_HELP again to save flash. * HOW TO CONFIG -TODO +TODO \ No newline at end of file diff --git a/app/spiffs/docs/TODO b/app/spiffs/docs/TODO index c947316a86..88709f022f 100644 --- a/app/spiffs/docs/TODO +++ b/app/spiffs/docs/TODO @@ -1,15 +1 @@ -* When mending lost pages, also see if they fit into length specified in object index header - -SPIFFS2 thoughts - -* Instead of exact object id:s in the object lookup tables, use a hash of span index and object id. - Eg. object id xor:ed with bit-reversed span index. - This should decrease number of actual pages that needs to be visited when looking thru the obj lut. - -* Logical number of each block. When moving stuff in a garbage collected page, the free - page is assigned the same number as the garbage collected. Thus, object index pages do not have to - be rewritten. - -* Steal one page, use as a bit parity page. When starting an fs modification operation, write one bit - as zero. When ending, write another bit as zero. On mount, if number of zeroes in page is uneven, a - check is automatically run. \ No newline at end of file +* When mending lost pages, also see if they fit into length specified in object index header \ No newline at end of file diff --git a/app/spiffs/spiffs.c b/app/spiffs/spiffs.c index c2c3ddc4a7..bcb11d5c13 100644 --- a/app/spiffs/spiffs.c +++ b/app/spiffs/spiffs.c @@ -4,7 +4,7 @@ spiffs fs; -#define LOG_PAGE_SIZE (256*2) +#define LOG_PAGE_SIZE 256 static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2]; static u8_t spiffs_fds[32*4]; @@ -49,9 +49,9 @@ void myspiffs_mount() { cfg.phys_addr &= 0xFFFFC000; // align to 4 sector. cfg.phys_size = INTERNAL_FLASH_SIZE - ( ( u32_t )cfg.phys_addr - INTERNAL_FLASH_START_ADDRESS ); cfg.phys_erase_block = INTERNAL_FLASH_SECTOR_SIZE; // according to datasheet - cfg.log_block_size = INTERNAL_FLASH_SECTOR_SIZE * 16; // let us not complicate things + cfg.log_block_size = INTERNAL_FLASH_SECTOR_SIZE; // let us not complicate things cfg.log_page_size = LOG_PAGE_SIZE; // as we said - SPIFFS_DBG("fs.start:%x,max:%x\n",cfg.phys_addr,cfg.phys_size); + NODE_DBG("fs.start:%x,max:%x\n",cfg.phys_addr,cfg.phys_size); cfg.hal_read_f = my_spiffs_read; cfg.hal_write_f = my_spiffs_write; @@ -66,7 +66,7 @@ void myspiffs_mount() { sizeof(spiffs_cache), // myspiffs_check_callback); 0); - SPIFFS_DBG("mount res: %i\n", res); + NODE_DBG("mount res: %i\n", res); } void myspiffs_unmount() { @@ -77,20 +77,7 @@ void myspiffs_unmount() { // Returns 1 if OK, 0 for error int myspiffs_format( void ) { -#if 1 - myspiffs_unmount(); - myspiffs_mount(); - myspiffs_unmount(); - if(0 == SPIFFS_format(&fs)) - { - myspiffs_mount(); - return 1; - } - else - { - return 0; - } -#else + SPIFFS_unmount(&fs); u32_t sect_first, sect_last; sect_first = ( u32_t )platform_flash_get_first_free_block_address( NULL ); sect_first += 0x3000; @@ -98,14 +85,12 @@ int myspiffs_format( void ) sect_first = platform_flash_get_sector_of_address(sect_first); sect_last = INTERNAL_FLASH_SIZE + INTERNAL_FLASH_START_ADDRESS - 4; sect_last = platform_flash_get_sector_of_address(sect_last); - SPIFFS_DBG("sect_first: %x, sect_last: %x\n", sect_first, sect_last); + NODE_DBG("sect_first: %x, sect_last: %x\n", sect_first, sect_last); while( sect_first <= sect_last ) if( platform_flash_erase_sector( sect_first ++ ) == PLATFORM_ERR ) return 0; myspiffs_mount(); - return 1; -#endif } int myspiffs_check( void ) @@ -117,11 +102,7 @@ int myspiffs_check( void ) } int myspiffs_open(const char *name, int flags){ - int res = SPIFFS_open(&fs, (char *)name, (spiffs_flags)flags, 0); - if (res < 0) { - SPIFFS_DBG("open errno %i\n", SPIFFS_errno(&fs)); - } - return res; + return (int)SPIFFS_open(&fs, (char *)name, (spiffs_flags)flags, 0); } int myspiffs_close( int fd ){ @@ -137,7 +118,7 @@ size_t myspiffs_write( int fd, const void* ptr, size_t len ){ #endif int res = SPIFFS_write(&fs, (spiffs_file)fd, (void *)ptr, len); if (res < 0) { - SPIFFS_DBG("write errno %i\n", SPIFFS_errno(&fs)); + NODE_DBG("write errno %i\n", SPIFFS_errno(&fs)); return 0; } return res; @@ -145,7 +126,7 @@ size_t myspiffs_write( int fd, const void* ptr, size_t len ){ size_t myspiffs_read( int fd, void* ptr, size_t len){ int res = SPIFFS_read(&fs, (spiffs_file)fd, ptr, len); if (res < 0) { - SPIFFS_DBG("read errno %i\n", SPIFFS_errno(&fs)); + NODE_DBG("read errno %i\n", SPIFFS_errno(&fs)); return 0; } return res; @@ -165,7 +146,7 @@ int myspiffs_getc( int fd ){ if(!myspiffs_eof(fd)){ res = SPIFFS_read(&fs, (spiffs_file)fd, &c, 1); if (res != 1) { - SPIFFS_DBG("getc errno %i\n", SPIFFS_errno(&fs)); + NODE_DBG("getc errno %i\n", SPIFFS_errno(&fs)); return (int)EOF; } else { return (int)c; @@ -198,13 +179,13 @@ void test_spiffs() { // Surely, I've mounted spiffs before entering here spiffs_file fd = SPIFFS_open(&fs, "my_file", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); - if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) SPIFFS_DBG("errno %i\n", SPIFFS_errno(&fs)); + if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) NODE_DBG("errno %i\n", SPIFFS_errno(&fs)); SPIFFS_close(&fs, fd); fd = SPIFFS_open(&fs, "my_file", SPIFFS_RDWR, 0); - if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) SPIFFS_DBG("errno %i\n", SPIFFS_errno(&fs)); + if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) NODE_DBG("errno %i\n", SPIFFS_errno(&fs)); SPIFFS_close(&fs, fd); - SPIFFS_DBG("--> %s <--\n", buf); + NODE_DBG("--> %s <--\n", buf); } #endif diff --git a/app/spiffs/spiffs.h b/app/spiffs/spiffs.h index 6f9b46974e..7132b92d43 100644 --- a/app/spiffs/spiffs.h +++ b/app/spiffs/spiffs.h @@ -12,7 +12,7 @@ #if defined(__cplusplus) extern "C" { #endif -#include "eagle_soc.h" + #include "c_stdio.h" #include "spiffs_config.h" @@ -41,19 +41,11 @@ extern "C" { #define SPIFFS_ERR_NOT_WRITABLE -10021 #define SPIFFS_ERR_NOT_READABLE -10022 #define SPIFFS_ERR_CONFLICTING_NAME -10023 -#define SPIFFS_ERR_NOT_CONFIGURED -10024 - -#define SPIFFS_ERR_NOT_A_FS -10025 -#define SPIFFS_ERR_MOUNTED -10026 -#define SPIFFS_ERR_ERASE_FAIL -10027 -#define SPIFFS_ERR_MAGIC_NOT_POSSIBLE -10028 - #define SPIFFS_ERR_INTERNAL -10050 #define SPIFFS_ERR_TEST -10100 -#define SPIFFS_WDT_CLEAR(no_arg) WRITE_PERI_REG(0x60000914, 0x73) // spiffs file descriptor index type. must be signed typedef s16_t spiffs_file; @@ -223,11 +215,6 @@ typedef struct { // check callback function spiffs_check_callback check_cb_f; - - // mounted flag - u8_t mounted; - // config magic - u32_t config_magic; } spiffs; /* spiffs file status struct */ @@ -255,10 +242,7 @@ typedef struct { // functions /** - * Initializes the file system dynamic parameters and mounts the filesystem. - * If SPIFFS_USE_MAGIC is enabled the mounting may fail with SPIFFS_ERR_NOT_A_FS - * if the flash does not contain a recognizable file system. - * In this case, SPIFFS_format must be called prior to remounting. + * Initializes the file system dynamic parameters and mounts the filesystem * @param fs the file system struct * @param config the physical and logical configuration of the file system * @param work a memory work buffer comprising 2*config->log_page_size @@ -457,24 +441,6 @@ s32_t SPIFFS_check(spiffs *fs); */ s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used); -/** - * Formats the entire file system. All data will be lost. - * The filesystem must not be mounted when calling this. - * - * NB: formatting is awkward. Due to backwards compatibility, SPIFFS_mount - * MUST be called prior to formatting in order to configure the filesystem. - * If SPIFFS_mount succeeds, SPIFFS_unmount must be called before calling - * SPIFFS_format. - * If SPIFFS_mount fails, SPIFFS_format can be called directly without calling - * SPIFFS_unmount first. - */ -s32_t SPIFFS_format(spiffs *fs); - -/** - * Returns nonzero if spiffs is mounted, or zero if unmounted. - */ -u8_t SPIFFS_mounted(spiffs *fs); - /** * Check if EOF reached. * @param fs the file system struct diff --git a/app/spiffs/spiffs_cache.c b/app/spiffs/spiffs_cache.c index b246117cdf..6de0e493ac 100644 --- a/app/spiffs/spiffs_cache.c +++ b/app/spiffs/spiffs_cache.c @@ -195,7 +195,7 @@ s32_t spiffs_phys_wr( cache->last_access++; cp->last_access = cache->last_access; - if (cp->flags & SPIFFS_CACHE_FLAG_WRTHRU) { + if (cp->flags && SPIFFS_CACHE_FLAG_WRTHRU) { // page is being updated, no write-cache, just pass thru return fs->cfg.hal_write_f(addr, len, src); } else { diff --git a/app/spiffs/spiffs_config.h b/app/spiffs/spiffs_config.h index fef614bf9b..d80e7d22e7 100644 --- a/app/spiffs/spiffs_config.h +++ b/app/spiffs/spiffs_config.h @@ -30,19 +30,19 @@ typedef uint8_t u8_t; // Set generic spiffs debug output call. #ifndef SPIFFS_DGB -#define SPIFFS_DBG(...) //c_printf(__VA_ARGS__) +#define SPIFFS_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for garbage collecting. #ifndef SPIFFS_GC_DGB -#define SPIFFS_GC_DBG(...) //c_printf(__VA_ARGS__) +#define SPIFFS_GC_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for caching. #ifndef SPIFFS_CACHE_DGB -#define SPIFFS_CACHE_DBG(...) //c_printf(__VA_ARGS__) +#define SPIFFS_CACHE_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for system consistency checks. #ifndef SPIFFS_CHECK_DGB -#define SPIFFS_CHECK_DBG(...) //c_printf(__VA_ARGS__) +#define SPIFFS_CHECK_DBG(...) //printf(__VA_ARGS__) #endif // Enable/disable API functions to determine exact number of bytes @@ -119,22 +119,14 @@ typedef uint8_t u8_t; #define SPIFFS_COPY_BUFFER_STACK (64) #endif -// Enable this to have an identifiable spiffs filesystem. This will look for -// a magic in all sectors to determine if this is a valid spiffs system or -// not on mount point. If not, SPIFFS_format must be called prior to mounting -// again. -#ifndef SPIFFS_USE_MAGIC -#define SPIFFS_USE_MAGIC (1) -#endif - // SPIFFS_LOCK and SPIFFS_UNLOCK protects spiffs from reentrancy on api level // These should be defined on a multithreaded system -// define this to enter a mutex if you're running on a multithreaded system +// define this to entering a mutex if you're running on a multithreaded system #ifndef SPIFFS_LOCK #define SPIFFS_LOCK(fs) #endif -// define this to exit a mutex if you're running on a multithreaded system +// define this to exiting a mutex if you're running on a multithreaded system #ifndef SPIFFS_UNLOCK #define SPIFFS_UNLOCK(fs) #endif @@ -167,12 +159,7 @@ typedef uint8_t u8_t; #endif #endif -// Enable this if your target needs aligned data for index tables -#ifndef SPIFFS_ALIGNED_OBJECT_INDEX_TABLES -#define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 1 -#endif - -// Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function +// Set SPFIFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function // in the api. This function will visualize all filesystem using given printf // function. #ifndef SPIFFS_TEST_VISUALISATION diff --git a/app/spiffs/spiffs_gc.c b/app/spiffs/spiffs_gc.c index a072d94610..2ad31d0745 100644 --- a/app/spiffs/spiffs_gc.c +++ b/app/spiffs/spiffs_gc.c @@ -8,11 +8,31 @@ static s32_t spiffs_gc_erase_block( spiffs *fs, spiffs_block_ix bix) { s32_t res; + u32_t addr = SPIFFS_BLOCK_TO_PADDR(fs, bix); + s32_t size = SPIFFS_CFG_LOG_BLOCK_SZ(fs); SPIFFS_GC_DBG("gc: erase block %i\n", bix); - res = spiffs_erase_block(fs, bix); + + // here we ignore res, just try erasing the block + while (size > 0) { + SPIFFS_GC_DBG("gc: erase %08x:%08x\n", addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs)); + (void)fs->cfg.hal_erase_f(addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs)); + addr += SPIFFS_CFG_PHYS_ERASE_SZ(fs); + size -= SPIFFS_CFG_PHYS_ERASE_SZ(fs); + } + fs->free_blocks++; + + // register erase count for this block + res = _spiffs_wr(fs, SPIFFS_OP_C_WRTHRU | SPIFFS_OP_T_OBJ_LU2, 0, + SPIFFS_ERASE_COUNT_PADDR(fs, bix), + sizeof(spiffs_obj_id), (u8_t *)&fs->max_erase_count); SPIFFS_CHECK_RES(res); + fs->max_erase_count++; + if (fs->max_erase_count == SPIFFS_OBJ_ID_IX_FLAG) { + fs->max_erase_count = 0; + } + #if SPIFFS_CACHE { u32_t i; @@ -110,15 +130,12 @@ s32_t spiffs_gc_check( } u32_t needed_pages = (len + SPIFFS_DATA_PAGE_SIZE(fs) - 1) / SPIFFS_DATA_PAGE_SIZE(fs); -// if (fs->free_blocks <= 2 && (s32_t)needed_pages > free_pages) { -// SPIFFS_GC_DBG("gc: full freeblk:%i needed:%i free:%i dele:%i\n", fs->free_blocks, needed_pages, free_pages, fs->stats_p_deleted); -// return SPIFFS_ERR_FULL; -// } - if ((s32_t)needed_pages > (s32_t)(free_pages + fs->stats_p_deleted)) { - SPIFFS_GC_DBG("gc_check: full freeblk:%i needed:%i free:%i dele:%i\n", fs->free_blocks, needed_pages, free_pages, fs->stats_p_deleted); + if (fs->free_blocks <= 2 && (s32_t)needed_pages > free_pages) { return SPIFFS_ERR_FULL; } + //printf("gcing started %i dirty, blocks %i free, want %i bytes\n", fs->stats_p_allocated + fs->stats_p_deleted, fs->free_blocks, len); + do { SPIFFS_GC_DBG("\ngc_check #%i: run gc free_blocks:%i pfree:%i pallo:%i pdele:%i [%i] len:%i of %i\n", tries, @@ -128,13 +145,11 @@ s32_t spiffs_gc_check( spiffs_block_ix *cands; int count; spiffs_block_ix cand; - s32_t prev_free_pages = free_pages; - // if the fs is crammed, ignore block age when selecting candidate - kind of a bad state - res = spiffs_gc_find_candidate(fs, &cands, &count, free_pages <= 0); + res = spiffs_gc_find_candidate(fs, &cands, &count); SPIFFS_CHECK_RES(res); if (count == 0) { SPIFFS_GC_DBG("gc_check: no candidates, return\n"); - return (s32_t)needed_pages < free_pages ? SPIFFS_OK : SPIFFS_ERR_FULL; + return res; } #if SPIFFS_GC_STATS fs->stats_gc_runs++; @@ -161,12 +176,6 @@ s32_t spiffs_gc_check( (SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs)) * (fs->block_count - 2) - fs->stats_p_allocated - fs->stats_p_deleted; - if (prev_free_pages <= 0 && prev_free_pages == free_pages) { - // abort early to reduce wear, at least tried once - SPIFFS_GC_DBG("gc_check: early abort, no result on gc when fs crammed\n"); - break; - } - } while (++tries < SPIFFS_GC_MAX_RUNS && (fs->free_blocks <= 2 || (s32_t)len > free_pages*(s32_t)SPIFFS_DATA_PAGE_SIZE(fs))); @@ -225,8 +234,7 @@ s32_t spiffs_gc_erase_page_stats( s32_t spiffs_gc_find_candidate( spiffs *fs, spiffs_block_ix **block_candidates, - int *candidate_count, - char fs_crammed) { + int *candidate_count) { s32_t res = SPIFFS_OK; u32_t blocks = fs->block_count; spiffs_block_ix cur_block = 0; @@ -299,7 +307,7 @@ s32_t spiffs_gc_find_candidate( s32_t score = deleted_pages_in_block * SPIFFS_GC_HEUR_W_DELET + used_pages_in_block * SPIFFS_GC_HEUR_W_USED + - erase_age * (fs_crammed ? 0 : SPIFFS_GC_HEUR_W_ERASE_AGE); + erase_age * SPIFFS_GC_HEUR_W_ERASE_AGE; int cand_ix = 0; SPIFFS_GC_DBG("gc_check: bix:%i del:%i use:%i score:%i\n", cur_block, deleted_pages_in_block, used_pages_in_block, score); while (cand_ix < max_candidates) { diff --git a/app/spiffs/spiffs_hydrogen.c b/app/spiffs/spiffs_hydrogen.c index b93c4fa87f..3ca01463ee 100644 --- a/app/spiffs/spiffs_hydrogen.c +++ b/app/spiffs/spiffs_hydrogen.c @@ -21,36 +21,6 @@ u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages) { #endif #endif -u8_t SPIFFS_mounted(spiffs *fs) { - return SPIFFS_CHECK_MOUNT(fs); -} - -s32_t SPIFFS_format(spiffs *fs) { - SPIFFS_API_CHECK_CFG(fs); - if (SPIFFS_CHECK_MOUNT(fs)) { - fs->err_code = SPIFFS_ERR_MOUNTED; - return -1; - } - - s32_t res; - SPIFFS_LOCK(fs); - - spiffs_block_ix bix = 0; - while (bix < fs->block_count) { - fs->max_erase_count = 0; - res = spiffs_erase_block(fs, bix); - if (res != SPIFFS_OK) { - res = SPIFFS_ERR_ERASE_FAIL; - } - SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - bix++; - } - - SPIFFS_UNLOCK(fs); - - return 0; -} - s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work, u8_t *fd_space, u32_t fd_space_size, void *cache, u32_t cache_size, @@ -95,16 +65,7 @@ s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work, spiffs_cache_init(fs); #endif - s32_t res; - -#if SPIFFS_USE_MAGIC - res = SPIFFS_CHECK_MAGIC_POSSIBLE(fs) ? SPIFFS_OK : SPIFFS_ERR_MAGIC_NOT_POSSIBLE; - SPIFFS_API_CHECK_RES_UNLOCK(fs, res); -#endif - - fs->config_magic = SPIFFS_CONFIG_MAGIC; - - res = spiffs_obj_lu_scan(fs); + s32_t res = spiffs_obj_lu_scan(fs); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); SPIFFS_DBG("page index byte len: %i\n", SPIFFS_CFG_LOG_PAGE_SZ(fs)); @@ -118,15 +79,13 @@ s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work, fs->check_cb_f = check_cb_f; - fs->mounted = 1; - SPIFFS_UNLOCK(fs); return 0; } void SPIFFS_unmount(spiffs *fs) { - if (!SPIFFS_CHECK_CFG(fs) || !SPIFFS_CHECK_MOUNT(fs)) return; + if (!SPIFFS_CHECK_MOUNT(fs)) return; SPIFFS_LOCK(fs); u32_t i; spiffs_fd *fds = (spiffs_fd *)fs->fd_space; @@ -139,8 +98,7 @@ void SPIFFS_unmount(spiffs *fs) { spiffs_fd_return(fs, cur_fd->file_nbr); } } - fs->mounted = 0; - + fs->block_count = 0; SPIFFS_UNLOCK(fs); } @@ -154,7 +112,6 @@ void SPIFFS_clearerr(spiffs *fs) { s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode) { (void)mode; - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); spiffs_obj_id obj_id; @@ -170,7 +127,6 @@ s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode) { spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode mode) { (void)mode; - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -229,7 +185,6 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode } spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -259,7 +214,6 @@ spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_fl } s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, u32_t len) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -328,7 +282,6 @@ static s32_t spiffs_hydro_write(spiffs *fs, spiffs_fd *fd, void *buf, u32_t offs } s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, u32_t len) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -447,7 +400,6 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, u32_t len) { } s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -492,7 +444,6 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) { } s32_t SPIFFS_remove(spiffs *fs, char *path) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -526,7 +477,6 @@ s32_t SPIFFS_remove(spiffs *fs, char *path) { } s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -575,7 +525,6 @@ static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spi } s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -593,7 +542,6 @@ s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s) { } s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -647,7 +595,6 @@ static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh) { } s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); s32_t res = SPIFFS_OK; #if SPIFFS_CACHE_WR @@ -661,11 +608,6 @@ s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) { } void SPIFFS_close(spiffs *fs, spiffs_file fh) { - if (!SPIFFS_CHECK_CFG((fs))) { - (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED; - return; - } - if (!SPIFFS_CHECK_MOUNT(fs)) { fs->err_code = SPIFFS_ERR_NOT_MOUNTED; return; @@ -681,7 +623,6 @@ void SPIFFS_close(spiffs *fs, spiffs_file fh) { } s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -723,17 +664,10 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { spiffs_DIR *SPIFFS_opendir(spiffs *fs, char *name, spiffs_DIR *d) { (void)name; - - if (!SPIFFS_CHECK_CFG((fs))) { - (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED; - return 0; - } - if (!SPIFFS_CHECK_MOUNT(fs)) { fs->err_code = SPIFFS_ERR_NOT_MOUNTED; return 0; } - d->fs = fs; d->block = 0; d->entry = 0; @@ -809,14 +743,12 @@ struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) { } s32_t SPIFFS_closedir(spiffs_DIR *d) { - SPIFFS_API_CHECK_CFG(d->fs); SPIFFS_API_CHECK_MOUNT(d->fs); return 0; } s32_t SPIFFS_check(spiffs *fs) { s32_t res; - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -834,7 +766,6 @@ s32_t SPIFFS_check(spiffs *fs) { s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) { s32_t res = SPIFFS_OK; - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -857,13 +788,11 @@ s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) { } s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) { - s32_t res = SPIFFS_OK; - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); spiffs_fd *fd; - + s32_t res; res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES(fs, res); @@ -878,13 +807,11 @@ s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) { } s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) { - s32_t res = SPIFFS_OK; - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); spiffs_fd *fd; - + s32_t res; res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES(fs, res); @@ -899,13 +826,11 @@ s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) { } s32_t SPIFFS_size(spiffs *fs, spiffs_file fh) { - s32_t res = SPIFFS_OK; - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); spiffs_fd *fd; - + s32_t res; res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES(fs, res); @@ -922,7 +847,6 @@ s32_t SPIFFS_size(spiffs *fs, spiffs_file fh) { #if SPIFFS_TEST_VISUALISATION s32_t SPIFFS_vis(spiffs *fs) { s32_t res = SPIFFS_OK; - SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -936,7 +860,6 @@ s32_t SPIFFS_vis(spiffs *fs) { int cur_entry = 0; while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) { - SPIFFS_WDT_CLEAR(); int entry_offset = obj_lookup_page * entries_per_page; res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work); diff --git a/app/spiffs/spiffs_nucleus.c b/app/spiffs/spiffs_nucleus.c index 1036fcffa2..4ab63b233f 100644 --- a/app/spiffs/spiffs_nucleus.c +++ b/app/spiffs/spiffs_nucleus.c @@ -142,20 +142,15 @@ s32_t spiffs_obj_lu_find_entry_visitor( cur_block++; cur_block_addr = cur_block * SPIFFS_CFG_LOG_BLOCK_SZ(fs); if (cur_block >= fs->block_count) { - if (flags & SPIFFS_VIS_NO_WRAP) { - return SPIFFS_VIS_END; - } else { - // block wrap - cur_block = 0; - cur_block_addr = 0; - } + // block wrap + cur_block = 0; + cur_block_addr = 0; } } // check each block while (res == SPIFFS_OK && entry_count > 0) { int obj_lookup_page = cur_entry / entries_per_page; - SPIFFS_WDT_CLEAR(); // check each object lookup page while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) { int entry_offset = obj_lookup_page * entries_per_page; @@ -166,7 +161,6 @@ s32_t spiffs_obj_lu_find_entry_visitor( cur_entry - entry_offset < entries_per_page && // for non-last obj lookup pages cur_entry < (int)SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) // for last obj lookup page { - SPIFFS_WDT_CLEAR(); if ((flags & SPIFFS_VIS_CHECK_ID) == 0 || obj_lu_buf[cur_entry-entry_offset] == obj_id) { if (block_ix) *block_ix = cur_block; if (lu_entry) *lu_entry = cur_entry; @@ -219,45 +213,6 @@ s32_t spiffs_obj_lu_find_entry_visitor( return SPIFFS_VIS_END; } -s32_t spiffs_erase_block( - spiffs *fs, - spiffs_block_ix bix) { - s32_t res; - u32_t addr = SPIFFS_BLOCK_TO_PADDR(fs, bix); - s32_t size = SPIFFS_CFG_LOG_BLOCK_SZ(fs); - - // here we ignore res, just try erasing the block - while (size > 0) { - SPIFFS_DBG("erase %08x:%08x\n", addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs)); - (void)fs->cfg.hal_erase_f(addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs)); - addr += SPIFFS_CFG_PHYS_ERASE_SZ(fs); - size -= SPIFFS_CFG_PHYS_ERASE_SZ(fs); - } - fs->free_blocks++; - - // register erase count for this block - res = _spiffs_wr(fs, SPIFFS_OP_C_WRTHRU | SPIFFS_OP_T_OBJ_LU2, 0, - SPIFFS_ERASE_COUNT_PADDR(fs, bix), - sizeof(spiffs_obj_id), (u8_t *)&fs->max_erase_count); - SPIFFS_CHECK_RES(res); - -#if SPIFFS_USE_MAGIC - // finally, write magic - spiffs_obj_id magic = SPIFFS_MAGIC(fs); - res = _spiffs_wr(fs, SPIFFS_OP_C_WRTHRU | SPIFFS_OP_T_OBJ_LU2, 0, - SPIFFS_MAGIC_PADDR(fs, bix), - sizeof(spiffs_obj_id), (u8_t *)&magic); - SPIFFS_CHECK_RES(res); -#endif - - fs->max_erase_count++; - if (fs->max_erase_count == SPIFFS_OBJ_ID_IX_FLAG) { - fs->max_erase_count = 0; - } - - return res; -} - static s32_t spiffs_obj_lu_scan_v( spiffs *fs, @@ -283,45 +238,40 @@ static s32_t spiffs_obj_lu_scan_v( return SPIFFS_VIS_COUNTINUE; } - // Scans thru all obj lu and counts free, deleted and used pages // Find the maximum block erase count -// Checks magic if enabled s32_t spiffs_obj_lu_scan( spiffs *fs) { s32_t res; spiffs_block_ix bix; int entry; -#if SPIFFS_USE_MAGIC - spiffs_block_ix unerased_bix = (spiffs_block_ix)-1; -#endif - // find out erase count - // if enabled, check magic + fs->free_blocks = 0; + fs->stats_p_allocated = 0; + fs->stats_p_deleted = 0; + + res = spiffs_obj_lu_find_entry_visitor(fs, + 0, + 0, + 0, + 0, + spiffs_obj_lu_scan_v, + 0, + 0, + &bix, + &entry); + + if (res == SPIFFS_VIS_END) { + res = SPIFFS_OK; + } + + SPIFFS_CHECK_RES(res); + bix = 0; spiffs_obj_id erase_count_final; spiffs_obj_id erase_count_min = SPIFFS_OBJ_ID_FREE; spiffs_obj_id erase_count_max = 0; while (bix < fs->block_count) { - SPIFFS_WDT_CLEAR(); -#if SPIFFS_USE_MAGIC - spiffs_obj_id magic; - res = _spiffs_rd(fs, - SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ, - 0, SPIFFS_MAGIC_PADDR(fs, bix) , - sizeof(spiffs_obj_id), (u8_t *)&magic); - - SPIFFS_CHECK_RES(res); - if (magic != SPIFFS_MAGIC(fs)) { - if (unerased_bix == (spiffs_block_ix)-1) { - // allow one unerased block as it might be powered down during an erase - unerased_bix = bix; - } else { - // more than one unerased block, bail out - SPIFFS_CHECK_RES(SPIFFS_ERR_NOT_A_FS); - } - } -#endif spiffs_obj_id erase_count; res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ, @@ -347,38 +297,6 @@ s32_t spiffs_obj_lu_scan( fs->max_erase_count = erase_count_final; -#if SPIFFS_USE_MAGIC - if (unerased_bix != (spiffs_block_ix)-1) { - // found one unerased block, remedy - SPIFFS_DBG("mount: erase block %i\n", bix); - res = spiffs_erase_block(fs, unerased_bix); - SPIFFS_CHECK_RES(res); - } -#endif - - // count blocks - - fs->free_blocks = 0; - fs->stats_p_allocated = 0; - fs->stats_p_deleted = 0; - - res = spiffs_obj_lu_find_entry_visitor(fs, - 0, - 0, - 0, - 0, - spiffs_obj_lu_scan_v, - 0, - 0, - &bix, - &entry); - - if (res == SPIFFS_VIS_END) { - res = SPIFFS_OK; - } - - SPIFFS_CHECK_RES(res); - return res; } @@ -805,7 +723,6 @@ void spiffs_cb_object_event( u32_t i; spiffs_fd *fds = (spiffs_fd *)fs->fd_space; for (i = 0; i < fs->fd_count; i++) { - SPIFFS_WDT_CLEAR(); spiffs_fd *cur_fd = &fds[i]; if (cur_fd->file_nbr == 0 || (cur_fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) != obj_id) continue; if (spix == 0) { @@ -922,9 +839,9 @@ s32_t spiffs_object_append(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) { // write all data while (res == SPIFFS_OK && written < len) { - SPIFFS_WDT_CLEAR(); // calculate object index page span index cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix); + // handle storing and loading of object indices if (cur_objix_spix != prev_objix_spix) { // new object index page @@ -1154,7 +1071,6 @@ s32_t spiffs_object_modify(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) { // write all data while (res == SPIFFS_OK && written < len) { - SPIFFS_WDT_CLEAR(); // calculate object index page span index cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix); @@ -1632,7 +1548,6 @@ s32_t spiffs_object_read( spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work; while (cur_offset < offset + len) { - SPIFFS_WDT_CLEAR(); cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix); if (prev_objix_spix != cur_objix_spix) { // load current object index (header) page @@ -1781,7 +1696,6 @@ s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *co state.compaction = 0; state.conflicting_name = conflicting_name; while (res == SPIFFS_OK && free_obj_id == SPIFFS_OBJ_ID_FREE) { - SPIFFS_WDT_CLEAR(); if (state.max_obj_id - state.min_obj_id <= (spiffs_obj_id)SPIFFS_CFG_LOG_PAGE_SZ(fs)*8) { // possible to represent in bitmap u32_t i, j; @@ -1794,7 +1708,6 @@ s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *co SPIFFS_CHECK_RES(res); // traverse bitmask until found free obj_id for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs); i++) { - SPIFFS_WDT_CLEAR(); u8_t mask = fs->work[i]; if (mask == 0xff) { continue; @@ -1816,7 +1729,6 @@ s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *co u8_t min_count = 0xff; for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs)/sizeof(u8_t); i++) { - SPIFFS_WDT_CLEAR(); if (map[i] < min_count) { min_count = map[i]; min_i = i; @@ -1869,7 +1781,6 @@ s32_t spiffs_fd_find_new(spiffs *fs, spiffs_fd **fd) { u32_t i; spiffs_fd *fds = (spiffs_fd *)fs->fd_space; for (i = 0; i < fs->fd_count; i++) { - SPIFFS_WDT_CLEAR(); spiffs_fd *cur_fd = &fds[i]; if (cur_fd->file_nbr == 0) { cur_fd->file_nbr = i+1; diff --git a/app/spiffs/spiffs_nucleus.h b/app/spiffs/spiffs_nucleus.h index 5d905fe908..cc414432c9 100644 --- a/app/spiffs/spiffs_nucleus.h +++ b/app/spiffs/spiffs_nucleus.h @@ -131,10 +131,6 @@ #define SPIFFS_OBJ_ID_DELETED ((spiffs_obj_id)0) #define SPIFFS_OBJ_ID_FREE ((spiffs_obj_id)-1) -#define SPIFFS_MAGIC(fs) ((spiffs_obj_id)(0x20140529 ^ SPIFFS_CFG_LOG_PAGE_SZ(fs))) - -#define SPIFFS_CONFIG_MAGIC (0x20090315) - #if SPIFFS_SINGLETON == 0 #define SPIFFS_CFG_LOG_PAGE_SZ(fs) \ ((fs)->cfg.log_page_size) @@ -193,18 +189,9 @@ // returns data size in a data page #define SPIFFS_DATA_PAGE_SIZE(fs) \ ( SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header) ) -// returns physical address for block's erase count, -// always in the physical last entry of the last object lookup page +// returns physical address for block's erase count #define SPIFFS_ERASE_COUNT_PADDR(fs, bix) \ ( SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_OBJ_LOOKUP_PAGES(fs) * SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_obj_id) ) -// returns physical address for block's magic, -// always in the physical second last entry of the last object lookup page -#define SPIFFS_MAGIC_PADDR(fs, bix) \ - ( SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_OBJ_LOOKUP_PAGES(fs) * SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_obj_id)*2 ) -// checks if there is any room for magic in the object luts -#define SPIFFS_CHECK_MAGIC_POSSIBLE(fs) \ - ( (SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) % (SPIFFS_CFG_LOG_PAGE_SZ(fs)/sizeof(spiffs_obj_id))) * sizeof(spiffs_obj_id) \ - <= (SPIFFS_CFG_LOG_PAGE_SZ(fs)-sizeof(spiffs_obj_id)*2) ) // define helpers object @@ -251,10 +238,7 @@ #define SPIFFS_CHECK_MOUNT(fs) \ - ((fs)->mounted != 0) - -#define SPIFFS_CHECK_CFG(fs) \ - ((fs)->config_magic == SPIFFS_CONFIG_MAGIC) + ((fs)->block_count > 0) #define SPIFFS_CHECK_RES(res) \ do { \ @@ -267,12 +251,6 @@ return -1; \ } -#define SPIFFS_API_CHECK_CFG(fs) \ - if (!SPIFFS_CHECK_CFG((fs))) { \ - (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED; \ - return -1; \ - } - #define SPIFFS_API_CHECK_RES(fs, res) \ if ((res) < SPIFFS_OK) { \ (fs)->err_code = (res); \ @@ -403,8 +381,6 @@ typedef struct { // object structs // page header, part of each page except object lookup pages -// NB: this is always aligned when the data page is an object index, -// as in this case struct spiffs_page_object_ix is used typedef struct __attribute(( packed )) { // object id spiffs_obj_id obj_id; @@ -415,15 +391,11 @@ typedef struct __attribute(( packed )) { } spiffs_page_header; // object index header page header -typedef struct __attribute(( packed )) -#if SPIFFS_ALIGNED_OBJECT_INDEX_TABLES - __attribute(( aligned(sizeof(spiffs_page_ix)) )) -#endif -{ +typedef struct __attribute(( packed )) { // common page header spiffs_page_header p_hdr; // alignment - u8_t _align[4 - (sizeof(spiffs_page_header)&3)==0 ? 4 : (sizeof(spiffs_page_header)&3)]; + u8_t _align[4 - ((sizeof(spiffs_page_header)+sizeof(spiffs_obj_type)+SPIFFS_OBJ_NAME_LEN)&3)==0 ? 4 : ((sizeof(spiffs_page_header)+sizeof(spiffs_obj_type)+SPIFFS_OBJ_NAME_LEN)&3)]; // size of object u32_t size; // type of object @@ -506,10 +478,6 @@ s32_t spiffs_obj_lu_find_entry_visitor( spiffs_block_ix *block_ix, int *lu_entry); -s32_t spiffs_erase_block( - spiffs *fs, - spiffs_block_ix bix); - // --------------- s32_t spiffs_obj_lu_scan( @@ -657,8 +625,7 @@ s32_t spiffs_gc_erase_page_stats( s32_t spiffs_gc_find_candidate( spiffs *fs, spiffs_block_ix **block_candidate, - int *candidate_count, - char fs_crammed); + int *candidate_count); s32_t spiffs_gc_clean( spiffs *fs, diff --git a/app/spiffs/test/params_test.h b/app/spiffs/test/params_test.h index ad51f57370..241367f3c3 100644 --- a/app/spiffs/test/params_test.h +++ b/app/spiffs/test/params_test.h @@ -15,9 +15,6 @@ // spiffs file system offset in emulated spi flash #define SPIFFS_PHYS_ADDR (4*1024*1024) -// test using filesystem magic -//#define SPIFFS_USE_MAGIC 1 - #define SECTOR_SIZE 65536 #define LOG_BLOCK (SECTOR_SIZE*2) #define LOG_PAGE (SECTOR_SIZE/256) diff --git a/app/spiffs/test/test_bugreports.c b/app/spiffs/test/test_bugreports.c index 70ce9feebe..b577e6f7fc 100644 --- a/app/spiffs/test/test_bugreports.c +++ b/app/spiffs/test/test_bugreports.c @@ -17,6 +17,7 @@ #include #include + SUITE(bug_tests) void setup() { _setup_test_only(); @@ -26,7 +27,7 @@ void teardown() { } TEST(nodemcu_full_fs_1) { - fs_reset_specific(0, 0, 4096*20, 4096, 4096, 256); + fs_reset_specific(0, 4096*20, 4096, 4096, 256); int res; spiffs_file fd; @@ -85,7 +86,7 @@ TEST(nodemcu_full_fs_1) { } TEST_END(nodemcu_full_fs_1) TEST(nodemcu_full_fs_2) { - fs_reset_specific(0, 0, 4096*22, 4096, 4096, 256); + fs_reset_specific(0, 4096*22, 4096, 4096, 256); int res; spiffs_file fd; @@ -115,8 +116,6 @@ TEST(nodemcu_full_fs_2) { SPIFFS_clearerr(FS); printf(" create small file\n"); fd = SPIFFS_open(FS, "test2.txt", SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0); -#if 0 - // before gc in v3.1 TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_OK); TEST_CHECK(fd > 0); @@ -131,17 +130,12 @@ TEST(nodemcu_full_fs_2) { printf(" >>> file %s size: %i\n", s.name, s.size); TEST_CHECK(s.size == 0); SPIFFS_clearerr(FS); -#else - TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_FULL); - SPIFFS_clearerr(FS); -#endif + printf(" remove files\n"); res = SPIFFS_remove(FS, "test1.txt"); TEST_CHECK(res == SPIFFS_OK); -#if 0 res = SPIFFS_remove(FS, "test2.txt"); TEST_CHECK(res == SPIFFS_OK); -#endif printf(" create medium file\n"); fd = SPIFFS_open(FS, "test3.txt", SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0); @@ -163,169 +157,4 @@ TEST(nodemcu_full_fs_2) { } TEST_END(nodemcu_full_fs_2) -TEST(magic_test) { - // one obj lu page, not full - fs_reset_specific(0, 0, 4096*16, 4096, 4096*1, 128); - TEST_CHECK(SPIFFS_CHECK_MAGIC_POSSIBLE(FS)); - // one obj lu page, full - fs_reset_specific(0, 0, 4096*16, 4096, 4096*2, 128); - TEST_CHECK(!SPIFFS_CHECK_MAGIC_POSSIBLE(FS)); - // two obj lu pages, not full - fs_reset_specific(0, 0, 4096*16, 4096, 4096*4, 128); - TEST_CHECK(SPIFFS_CHECK_MAGIC_POSSIBLE(FS)); - - return TEST_RES_OK; - -} TEST_END(magic_test) - -TEST(nodemcu_309) { - fs_reset_specific(0, 0, 4096*20, 4096, 4096, 256); - - int res; - spiffs_file fd; - int j; - - for (j = 1; j <= 3; j++) { - char fname[32]; - sprintf(fname, "20K%i.txt", j); - fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_DIRECT, 0); - TEST_CHECK(fd > 0); - int i; - spiffs_stat s; - res = SPIFFS_OK; - u8_t err = 0; - for (i = 1; i <= 1280; i++) { - char *buf = "0123456789ABCDE\n"; - res = SPIFFS_write(FS, fd, buf, strlen(buf)); - if (!err && res < 0) { - printf("err @ %i,%i\n", i, j); - err = 1; - } - } - } - - int errno = SPIFFS_errno(FS); - TEST_CHECK(errno == SPIFFS_ERR_FULL); - - u32_t total; - u32_t used; - - SPIFFS_info(FS, &total, &used); - printf("total:%i\nused:%i\nremain:%i\nerrno:%i\n", total, used, total-used, errno); - TEST_CHECK(total-used < 11000); - - spiffs_DIR d; - struct spiffs_dirent e; - struct spiffs_dirent *pe = &e; - - SPIFFS_opendir(FS, "/", &d); - int spoon_guard = 0; - while ((pe = SPIFFS_readdir(&d, pe))) { - printf("%s [%04x] size:%i\n", pe->name, pe->obj_id, pe->size); - TEST_CHECK(spoon_guard++ < 3); - } - TEST_CHECK(spoon_guard == 3); - SPIFFS_closedir(&d); - - return TEST_RES_OK; - -} TEST_END(nodemcu_309) - - -TEST(robert) { - // create a clean file system starting at address 0, 2 megabytes big, - // sector size 65536, block size 65536, page size 256 - fs_reset_specific(0, 0, 1024*1024*2, 65536, 65536, 256); - - int res; - spiffs_file fd; - char fname[32]; - - sprintf(fname, "test.txt"); - fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0); - TEST_CHECK(fd > 0); - int i; - res = SPIFFS_OK; - char buf[500]; - memset(buf, 0xaa, 500); - res = SPIFFS_write(FS, fd, buf, 500); - TEST_CHECK(res >= SPIFFS_OK); - SPIFFS_close(FS, fd); - - int errno = SPIFFS_errno(FS); - TEST_CHECK(errno == SPIFFS_OK); - - //SPIFFS_vis(FS); - // unmount - SPIFFS_unmount(FS); - - // remount - res = fs_mount_specific(0, 1024*1024*2, 65536, 65536, 256); - TEST_CHECK(res== SPIFFS_OK); - - //SPIFFS_vis(FS); - - spiffs_stat s; - TEST_CHECK(SPIFFS_stat(FS, fname, &s) == SPIFFS_OK); - printf("file %s stat size %i\n", s.name, s.size); - TEST_CHECK(s.size == 500); - - return TEST_RES_OK; - -} TEST_END(robert) - - -TEST(spiffs_12) { - fs_reset_specific(0x4024c000, 0x4024c000 + 0, 192*1024, 4096, 4096*2, 256); - - int res; - spiffs_file fd; - int j = 1; - - while (1) { - char fname[32]; - sprintf(fname, "file%i.txt", j); - fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_DIRECT, 0); - if (fd <=0) break; - - int i; - res = SPIFFS_OK; - for (i = 1; i <= 100; i++) { - char *buf = "0123456789ABCDE\n"; - res = SPIFFS_write(FS, fd, buf, strlen(buf)); - if (res < 0) break; - } - SPIFFS_close(FS, fd); - j++; - } - - int errno = SPIFFS_errno(FS); - TEST_CHECK(errno == SPIFFS_ERR_FULL); - - u32_t total; - u32_t used; - - SPIFFS_info(FS, &total, &used); - printf("total:%i (%iK)\nused:%i (%iK)\nremain:%i (%iK)\nerrno:%i\n", total, total/1024, used, used/1024, total-used, (total-used)/1024, errno); - - spiffs_DIR d; - struct spiffs_dirent e; - struct spiffs_dirent *pe = &e; - - SPIFFS_opendir(FS, "/", &d); - while ((pe = SPIFFS_readdir(&d, pe))) { - printf("%s [%04x] size:%i\n", pe->name, pe->obj_id, pe->size); - } - SPIFFS_closedir(&d); - - //SPIFFS_vis(FS); - - //dump_page(FS, 0); - //dump_page(FS, 1); - - return TEST_RES_OK; - -} TEST_END(spiffs_12) - - SUITE_END(bug_tests) diff --git a/app/spiffs/test/test_hydrogen.c b/app/spiffs/test/test_hydrogen.c index e0764ffec2..fdba160237 100644 --- a/app/spiffs/test/test_hydrogen.c +++ b/app/spiffs/test/test_hydrogen.c @@ -592,6 +592,7 @@ TEST(simultaneous_write_append) { } TEST_END(simultaneous_write_append) + TEST(file_uniqueness) { int res; diff --git a/app/spiffs/test/test_spiffs.c b/app/spiffs/test/test_spiffs.c index 5b8613fa73..203356634b 100644 --- a/app/spiffs/test/test_spiffs.c +++ b/app/spiffs/test/test_spiffs.c @@ -24,12 +24,9 @@ #include #include -#define AREA(x) area[(x) - addr_offset] - static unsigned char area[PHYS_FLASH_SIZE]; -static u32_t addr_offset = 0; -static int erases[PHYS_FLASH_SIZE/SECTOR_SIZE]; +static int erases[256]; static char _path[256]; static u32_t bytes_rd = 0; static u32_t bytes_wr = 0; @@ -91,7 +88,7 @@ static s32_t _read(u32_t addr, u32_t size, u8_t *dst) { printf("FATAL read addr too high %08x + %08x > %08x\n", addr, size, SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE); exit(0); } - memcpy(dst, &AREA(addr), size); + memcpy(dst, &area[addr], size); return 0; } @@ -120,14 +117,14 @@ static s32_t _write(u32_t addr, u32_t size, u8_t *src) { for (i = 0; i < size; i++) { if (((addr + i) & (__fs.cfg.log_page_size-1)) != offsetof(spiffs_page_header, flags)) { - if (check_valid_flash && ((AREA(addr + i) ^ src[i]) & src[i])) { - printf("trying to write %02x to %02x at addr %08x\n", src[i], AREA(addr + i), addr+i); + if (check_valid_flash && ((area[addr + i] ^ src[i]) & src[i])) { + printf("trying to write %02x to %02x at addr %08x\n", src[i], area[addr + i], addr+i); spiffs_page_ix pix = (addr + i) / LOG_PAGE; dump_page(&__fs, pix); return -1; } } - AREA(addr + i) &= src[i]; + area[addr + i] &= src[i]; } return 0; } @@ -142,7 +139,7 @@ static s32_t _erase(u32_t addr, u32_t size) { return -1; } erases[(addr-__fs.cfg.phys_addr)/__fs.cfg.phys_erase_block]++; - memset(&AREA(addr), 0xff, size); + memset(&area[addr], 0xff, size); return 0; } @@ -168,7 +165,7 @@ void hexdump(u32_t addr, u32_t len) { if (a-32+j < addr) printf(" "); else { - printf("%c", (AREA(a-32+j) < 32 || AREA(a-32+j) >= 0x7f) ? '.' : AREA(a-32+j)); + printf("%c", (area[a-32+j] < 32 || area[a-32+j] >= 0x7f) ? '.' : area[a-32+j]); } } } @@ -177,7 +174,7 @@ void hexdump(u32_t addr, u32_t len) { if (a < addr) { printf(" "); } else { - printf("%02x", AREA(a)); + printf("%02x", area[a]); } } int j; @@ -186,7 +183,7 @@ void hexdump(u32_t addr, u32_t len) { if (a-32+j < addr) printf(" "); else { - printf("%c", (AREA(a-32+j) < 32 || AREA(a-32+j) >= 0x7f) ? '.' : AREA(a-32+j)); + printf("%c", (area[a-32+j] < 32 || area[a-32+j] >= 0x7f) ? '.' : area[a-32+j]); } } printf("\n"); @@ -201,9 +198,9 @@ void dump_page(spiffs *fs, spiffs_page_ix p) { } else { u32_t obj_id_addr = SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs , p)) + SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, p) * sizeof(spiffs_obj_id); - spiffs_obj_id obj_id = *((spiffs_obj_id *)&AREA(obj_id_addr)); + spiffs_obj_id obj_id = *((spiffs_obj_id *)&area[obj_id_addr]); // data page - spiffs_page_header *ph = (spiffs_page_header *)&AREA(addr); + spiffs_page_header *ph = (spiffs_page_header *)&area[addr]; printf("DATA %04x:%04x ", obj_id, ph->span_ix); printf("%s", ((ph->flags & SPIFFS_PH_FLAG_FINAL) == 0) ? "FIN " : "fin "); printf("%s", ((ph->flags & SPIFFS_PH_FLAG_DELET) == 0) ? "DEL " : "del "); @@ -215,7 +212,7 @@ void dump_page(spiffs *fs, spiffs_page_ix p) { printf("OBJ_IX"); if (ph->span_ix == 0) { printf("_HDR "); - spiffs_page_object_ix_header *oix_hdr = (spiffs_page_object_ix_header *)&AREA(addr); + spiffs_page_object_ix_header *oix_hdr = (spiffs_page_object_ix_header *)&area[addr]; printf("'%s' %i bytes type:%02x", oix_hdr->name, oix_hdr->size, oix_hdr->type); } } else { @@ -231,14 +228,14 @@ void dump_page(spiffs *fs, spiffs_page_ix p) { void area_write(u32_t addr, u8_t *buf, u32_t size) { int i; for (i = 0; i < size; i++) { - AREA(addr + i) = *buf++; + area[addr + i] = *buf++; } } void area_read(u32_t addr, u8_t *buf, u32_t size) { int i; for (i = 0; i < size; i++) { - *buf++ = AREA(addr + i); + *buf++ = area[addr + i]; } } @@ -316,13 +313,12 @@ static void spiffs_check_cb_f(spiffs_check_type type, spiffs_check_report report } } -void fs_set_addr_offset(u32_t offset) { - addr_offset = offset; -} - -s32_t fs_mount_specific(u32_t phys_addr, u32_t phys_size, +void fs_reset_specific(u32_t phys_addr, u32_t phys_size, u32_t phys_sector_size, u32_t log_block_size, u32_t log_page_size) { + memset(area, 0xcc, sizeof(area)); + memset(&area[phys_addr], 0xff, phys_size); + spiffs_config c; c.hal_erase_f = _erase; c.hal_read_f = _read; @@ -333,35 +329,10 @@ s32_t fs_mount_specific(u32_t phys_addr, u32_t phys_size, c.phys_erase_block = phys_sector_size; c.phys_size = phys_size; - return SPIFFS_mount(&__fs, &c, _work, _fds, sizeof(_fds), _cache, sizeof(_cache), spiffs_check_cb_f); -} - -void fs_reset_specific(u32_t addr_offset, u32_t phys_addr, u32_t phys_size, - u32_t phys_sector_size, - u32_t log_block_size, u32_t log_page_size) { - fs_set_addr_offset(addr_offset); - memset(area, 0xcc, sizeof(area)); - memset(&AREA(phys_addr), 0xff, phys_size); - memset(&__fs, 0, sizeof(__fs)); - memset(erases,0,sizeof(erases)); memset(_cache,0,sizeof(_cache)); - s32_t res = fs_mount_specific(phys_addr, phys_size, phys_sector_size, log_block_size, log_page_size); - -#if SPIFFS_USE_MAGIC - if (res == SPIFFS_OK) { - SPIFFS_unmount(&__fs); - } - res = SPIFFS_format(&__fs); - if (res != SPIFFS_OK) { - printf("format failed, %i\n", SPIFFS_errno(&__fs)); - } - res = SPIFFS_mount(&__fs, &c, _work, _fds, sizeof(_fds), _cache, sizeof(_cache), spiffs_check_cb_f); - if (res != SPIFFS_OK) { - printf("mount failed, %i\n", SPIFFS_errno(&__fs)); - } -#endif + SPIFFS_mount(&__fs, &c, _work, _fds, sizeof(_fds), _cache, sizeof(_cache), spiffs_check_cb_f); clear_flash_ops_log(); log_flash_ops = 1; @@ -369,7 +340,7 @@ void fs_reset_specific(u32_t addr_offset, u32_t phys_addr, u32_t phys_size, } void fs_reset() { - fs_reset_specific(0, SPIFFS_PHYS_ADDR, SPIFFS_FLASH_SIZE, SECTOR_SIZE, LOG_BLOCK, LOG_PAGE); + fs_reset_specific(SPIFFS_PHYS_ADDR, SPIFFS_FLASH_SIZE, SECTOR_SIZE, LOG_BLOCK, LOG_PAGE); } void set_flash_ops_log(int enable) { @@ -603,8 +574,6 @@ void _teardown() { printf(" cache hits : %i (sum %i)\n", (FS)->cache_hits, chits_tot); printf(" cache misses : %i (sum %i)\n", (FS)->cache_misses, cmiss_tot); printf(" cache utiliz : %f\n", ((float)chits_tot/(float)(chits_tot + cmiss_tot))); - chits_tot = 0; - cmiss_tot = 0; #endif #endif dump_flash_access_stats(); @@ -617,8 +586,8 @@ void _teardown() { SPIFFS_check(FS); clear_test_path(); - //hexdump_mem(&AREA(SPIFFS_PHYS_ADDR - 16), 32); - //hexdump_mem(&AREA(SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE - 16), 32); + //hexdump_mem(&area[SPIFFS_PHYS_ADDR - 16], 32); + //hexdump_mem(&area[SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE - 16], 32); } u32_t tfile_get_size(tfile_size s) { diff --git a/app/spiffs/test/test_spiffs.h b/app/spiffs/test/test_spiffs.h index 3fd1991ce2..14c603f0ff 100644 --- a/app/spiffs/test/test_spiffs.h +++ b/app/spiffs/test/test_spiffs.h @@ -57,13 +57,9 @@ typedef struct { void fs_reset(); -void fs_reset_specific(u32_t addr_offset, u32_t phys_addr, u32_t phys_size, +void fs_reset_specific(u32_t phys_addr, u32_t phys_size, u32_t phys_sector_size, u32_t log_block_size, u32_t log_page_size); -s32_t fs_mount_specific(u32_t phys_addr, u32_t phys_size, - u32_t phys_sector_size, - u32_t log_block_size, u32_t log_page_size); -void fs_set_addr_offset(u32_t offset); int read_and_verify(char *name); int read_and_verify_fd(spiffs_file fd, char *name); void dump_page(spiffs *fs, spiffs_page_ix p); diff --git a/app/spiffs/test/testrunner.c b/app/spiffs/test/testrunner.c index 80e9c72e9f..ad4fd1fbf0 100644 --- a/app/spiffs/test/testrunner.c +++ b/app/spiffs/test/testrunner.c @@ -28,8 +28,6 @@ static struct { test_res *stopped; test_res *stopped_last; FILE *spec; - char incl_filter[256]; - char excl_filter[256]; } test_main; void test_init(void (*on_stop)(test *t)) { @@ -43,7 +41,7 @@ static char check_spec(char *name) { size_t sz; ssize_t read; while ((read = getline(&line, &sz, test_main.spec)) != -1) { - if (strncmp(line, name, strlen(line)-1) == 0) { + if (strncmp(line, name, strlen(name)) == 0) { free(line); return 1; } @@ -55,21 +53,9 @@ static char check_spec(char *name) { } } -static char check_incl_filter(char *name) { - if (strlen(test_main.incl_filter)== 0) return 1; - return strstr(name, test_main.incl_filter) == 0 ? 0 : 1; -} - -static char check_excl_filter(char *name) { - if (strlen(test_main.excl_filter)== 0) return 1; - return strstr(name, test_main.excl_filter) == 0 ? 1 : 0; -} - void add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t)) { if (f == 0) return; if (!check_spec(name)) return; - if (!check_incl_filter(name)) return; - if (!check_excl_filter(name)) return; DBGT("adding test %s\n", name); test *t = malloc(sizeof(test)); memset(t, 0, sizeof(test)); @@ -108,36 +94,16 @@ static void dump_res(test_res **head) { } } -int run_tests(int argc, char **args) { +void run_tests(int argc, char **args) { memset(&test_main, 0, sizeof(test_main)); - int arg; - int incl_filter = 0; - int excl_filter = 0; - for (arg = 1; arg < argc; arg++) { - if (strlen(args[arg]) == 0) continue; - if (0 == strcmp("-f", args[arg])) { - incl_filter = 1; - continue; - } - if (0 == strcmp("-e", args[arg])) { - excl_filter = 1; - continue; - } - if (incl_filter) { - strcpy(test_main.incl_filter, args[arg]); - incl_filter = 0; - } else if (excl_filter) { - strcpy(test_main.excl_filter, args[arg]); - excl_filter = 0; - } else { - printf("running tests from %s\n", args[arg]); - FILE *fd = fopen(args[1], "r"); - if (fd == NULL) { - printf("%s not found\n", args[arg]); - return -2; - } - test_main.spec = fd; + if (argc > 1) { + printf("running tests from %s\n", args[1]); + FILE *fd = fopen(args[1], "r"); + if (fd == NULL) { + printf("%s not found\n", args[1]); + exit(EXIT_FAILURE); } + test_main.spec = fd; } DBGT("adding suites...\n"); @@ -149,7 +115,7 @@ int run_tests(int argc, char **args) { if (test_main.test_count == 0) { printf("No tests to run\n"); - return 0; + return; } int fd_success = open("_tests_ok", O_APPEND | O_TRUNC | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); @@ -167,7 +133,6 @@ int run_tests(int argc, char **args) { DBGT("TEST %i/%i : running test %s\n", i, test_main.test_count, cur_t->name); i++; int res = cur_t->f(cur_t); - cur_t->test_result = res; cur_t->teardown(cur_t); int fd = res == TEST_RES_OK ? fd_success : fd_bad; write(fd, cur_t->name, strlen(cur_t->name)); @@ -204,9 +169,7 @@ int run_tests(int argc, char **args) { dump_res(&test_main.stopped); if (ok < test_main.test_count) { printf("\nFAILED\n"); - return -1; } else { printf("\nALL TESTS OK\n"); - return 0; } } diff --git a/app/spiffs/test/testrunner.h b/app/spiffs/test/testrunner.h index be9b55581f..20ae606ce1 100644 --- a/app/spiffs/test/testrunner.h +++ b/app/spiffs/test/testrunner.h @@ -48,8 +48,8 @@ void add_suites() { } */ -#ifndef TESTRUNNER_H_ -#define TESTRUNNER_H_ +#ifndef TESTS_H_ +#define TESTS_H_ #define TEST_RES_OK 0 #define TEST_RES_FAIL -1 @@ -66,7 +66,6 @@ typedef struct test_s { void (*setup)(struct test_s *t); void (*teardown)(struct test_s *t); struct test_s *_next; - unsigned char test_result; } test; typedef struct test_res_s { @@ -78,30 +77,6 @@ typedef struct test_res_s { printf(" TEST FAIL %s:%i\n", __FILE__, __LINE__); \ goto __fail_stop; \ } -#define TEST_CHECK_EQ(x, y) if ((x) != (y)) { \ - printf(" TEST FAIL %s:%i, %i != %i\n", __FILE__, __LINE__, (x), (y)); \ - goto __fail_stop; \ -} -#define TEST_CHECK_NEQ(x, y) if ((x) == (y)) { \ - printf(" TEST FAIL %s:%i, %i == %i\n", __FILE__, __LINE__, (x), (y)); \ - goto __fail_stop; \ -} -#define TEST_CHECK_GT(x, y) if ((x) <= (y)) { \ - printf(" TEST FAIL %s:%i, %i <= %i\n", __FILE__, __LINE__, (x), (y)); \ - goto __fail_stop; \ -} -#define TEST_CHECK_LT(x, y) if ((x) >= (y)) { \ - printf(" TEST FAIL %s:%i, %i >= %i\n", __FILE__, __LINE__, (x), (y)); \ - goto __fail_stop; \ -} -#define TEST_CHECK_GE(x, y) if ((x) < (y)) { \ - printf(" TEST FAIL %s:%i, %i < %i\n", __FILE__, __LINE__, (x), (y)); \ - goto __fail_stop; \ -} -#define TEST_CHECK_LE(x, y) if ((x) > (y)) { \ - printf(" TEST FAIL %s:%i, %i > %i\n", __FILE__, __LINE__, (x), (y)); \ - goto __fail_stop; \ -} #define TEST_ASSERT(x) if (!(x)) { \ printf(" TEST ASSERT %s:%i\n", __FILE__, __LINE__); \ goto __fail_assert; \ @@ -130,7 +105,6 @@ typedef struct test_res_s { void add_suites(); void test_init(void (*on_stop)(test *t)); void add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t)); -// returns 0 if all tests ok, -1 if any test failed, -2 on badness -int run_tests(int argc, char **args); +void run_tests(int argc, char **args); -#endif /* TESTRUNNER_H_ */ +#endif /* TESTS_H_ */ diff --git a/app/user/user_main.c b/app/user/user_main.c index a3ccc4569b..88938da8ce 100644 --- a/app/user/user_main.c +++ b/app/user/user_main.c @@ -61,15 +61,13 @@ void nodemcu_init(void) #if defined(FLASH_SAFE_API) if( flash_safe_get_size_byte() != flash_rom_get_size_byte()) { - NODE_ERR("File system initialization ...\n"); - NODE_ERR("This will take a minute, don't power off.\n"); + NODE_ERR("Self adjust flash size.\n"); // Fit hardware real flash size. flash_rom_set_size_byte(flash_safe_get_size_byte()); // Flash init data at FLASHSIZE - 0x04000 Byte. flash_init_data_default(); // Flash blank data at FLASHSIZE - 0x02000 Byte. flash_init_data_blank(); - if( !fs_format() ) { NODE_ERR( "\ni*** ERROR ***: unable to format. FS might be compromised.\n" ); @@ -78,8 +76,7 @@ void nodemcu_init(void) else{ NODE_ERR( "format done.\n" ); } - // Unmount filesystem because mounted by format. - fs_unmount(); + fs_unmount(); // mounted by format. } #endif // defined(FLASH_SAFE_API) @@ -105,22 +102,6 @@ void nodemcu_init(void) // test_romfs(); #elif defined ( BUILD_SPIFFS ) fs_mount(); - if(SPIFFS_mounted == 0) - { - NODE_ERR("File system broken, formating ...\n"); - NODE_ERR("This will take a minute, don't power off.\n"); - if( !fs_format() ) - { - NODE_ERR( "\ni*** ERROR ***: unable to format. FS might be compromised.\n" ); - NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" ); - } - else{ - NODE_ERR( "format done.\n" ); - } - // Ensure filesystem mounted. - fs_unmount(); - fs_mount(); - } // test_spiffs(); #endif // endpoint_setup();