diff --git a/Sming/Services/SpifFS/spiffs_sming.c b/Sming/Services/SpifFS/spiffs_sming.c index 4af5d71998..c7186e127f 100644 --- a/Sming/Services/SpifFS/spiffs_sming.c +++ b/Sming/Services/SpifFS/spiffs_sming.c @@ -56,25 +56,31 @@ bool spiffs_format_internal(spiffs_config *cfg) { if (cfg->phys_addr == 0) { - SYSTEM_ERROR("Can't format file system, wrong address"); + SYSTEM_ERROR("Can't format file system, wrong address given."); + return false; + } + + if (cfg->phys_size == 0) + { + SYSTEM_ERROR("Can't format file system, wrong size given."); return false; } u32_t sect_first, sect_last; sect_first = cfg->phys_addr; sect_first = flashmem_get_sector_of_address(sect_first); - sect_last = cfg->phys_addr + cfg->phys_size; + sect_last = cfg->phys_addr + cfg->phys_size - 1; sect_last = flashmem_get_sector_of_address(sect_last); debugf("sect_first: %x, sect_last: %x\n", sect_first, sect_last); ETS_INTR_LOCK(); - int total = sect_last - sect_first; + int total = sect_last - sect_first + 1; int cur = 0; int last = -1; while( sect_first <= sect_last ) { if(flashmem_erase_sector( sect_first++ )) { - int percent = cur++ * 100 / total; + int percent = ++cur * 100 / total; if (percent > last) debugf("%d%%", percent); last = percent; @@ -85,7 +91,7 @@ bool spiffs_format_internal(spiffs_config *cfg) return false; } } - debugf("formated"); + debugf("formatted"); ETS_INTR_UNLOCK(); return true; diff --git a/Sming/appspecific/rboot/overrides.c b/Sming/appspecific/rboot/overrides.c index fcf3d83333..d0b452fe61 100644 --- a/Sming/appspecific/rboot/overrides.c +++ b/Sming/appspecific/rboot/overrides.c @@ -8,6 +8,8 @@ spiffs_config spiffs_get_storage_config() { spiffs_config cfg = {0}; + u32_t max_allowed_sector, requested_sector; + #ifdef RBOOT_SPIFFS_0 cfg.phys_addr = RBOOT_SPIFFS_0; #elif RBOOT_SPIFFS_1 @@ -15,7 +17,17 @@ spiffs_config spiffs_get_storage_config() #else #error "Define either RBOOT_SPIFFS_0 or RBOOT_SPIFFS_1" #endif - cfg.phys_size = SPIFF_SIZE; + + cfg.phys_addr &= 0xFFFFF000; // get the start address of the sector + + max_allowed_sector = flashmem_get_sector_of_address(INTERNAL_FLASH_SIZE - 1); + requested_sector = flashmem_get_sector_of_address((cfg.phys_addr + SPIFF_SIZE) - 1); + if(requested_sector > max_allowed_sector) { + debug_w("The requested SPIFFS size is too big."); + requested_sector = max_allowed_sector; + } + cfg.phys_size = ((requested_sector + 1) * INTERNAL_FLASH_SECTOR_SIZE) - ( ( u32_t )cfg.phys_addr); // get the max size until the sector end. + cfg.phys_erase_block = INTERNAL_FLASH_SECTOR_SIZE; // according to datasheet cfg.log_block_size = INTERNAL_FLASH_SECTOR_SIZE * 2; // Important to make large cfg.log_page_size = LOG_PAGE_SIZE; // as we said