From d69b8390d43e851c84d294f585d5534ef54402b4 Mon Sep 17 00:00:00 2001
From: sheinz <shein@bk.ru>
Date: Fri, 22 Jul 2016 14:09:50 +0300
Subject: [PATCH] SPIFFS: Fix PR review comments.

 * Enable SPIFFS_USE_MAGIC
 * Enable SPIFFS_USE_MAGIC_LENGTH
 * Enable SPIFFS_FILEHDL_OFFSET
 * Rebuild mkspiffs if spiffs_config.h is changed
 * Emulate NOR flash in mkspiffs
 * Build spiffs image in 'flash' and 'test' targets
---
 common.mk                         |  4 ++--
 extras/spiffs/component.mk        |  8 ++++++++
 extras/spiffs/esp_spiffs.c        | 25 +++++++++--------------
 extras/spiffs/mkspiffs/Makefile   |  4 ++++
 extras/spiffs/mkspiffs/mkspiffs.c | 34 +++++++++++++++----------------
 extras/spiffs/spiffs_config.h     |  8 ++++----
 6 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/common.mk b/common.mk
index f4bfe06..c3923c8 100644
--- a/common.mk
+++ b/common.mk
@@ -68,7 +68,7 @@ Q := @
 vecho := @echo
 endif
 
-.PHONY: all clean flash erase_flash
+.PHONY: all clean flash erase_flash test size rebuild
 
 all: $(PROGRAM_OUT) $(FW_FILE_1) $(FW_FILE_2) $(FW_FILE)
 
@@ -209,7 +209,7 @@ $(FW_FILE): $(PROGRAM_OUT) $(FIRMWARE_DIR)
 	$(vecho) "FW $@"
 	$(Q) $(ESPTOOL) elf2image --version=2 $(ESPTOOL_ARGS) $< -o $(FW_FILE)
 
-flash: $(FW_FILE)
+flash: all
 	$(ESPTOOL) -p $(ESPPORT) --baud $(ESPBAUD) write_flash $(ESPTOOL_ARGS) \
 		0x0 $(RBOOT_BIN) 0x1000 $(RBOOT_CONF) 0x2000 $(FW_FILE) $(SPIFFS_ESPTOOL_ARGS)
 
diff --git a/extras/spiffs/component.mk b/extras/spiffs/component.mk
index a0a8928..f7f5a49 100644
--- a/extras/spiffs/component.mk
+++ b/extras/spiffs/component.mk
@@ -40,6 +40,9 @@ $$(SPIFFS_IMAGE): $$(MKSPIFFS) $$(SPIFFS_FILE_LIST)
 $$(spiffs_ROOT)spiffs_config.h: Makefile
 	$$(Q) touch $$@
 
+$$(MKSPIFFS)_MAKE:
+	$$(MAKE) -C $$(MKSPIFFS_DIR) SPIFFS_SIZE=$(SPIFFS_SIZE)
+
 # if SPIFFS_SIZE in Makefile is changed rebuild mkspiffs
 $$(MKSPIFFS): Makefile
 	$$(MAKE) -C $$(MKSPIFFS_DIR) clean
@@ -51,6 +54,11 @@ clean_spiffs_img:
 clean_mkspiffs:
 	$$(Q) $$(MAKE) -C $$(MKSPIFFS_DIR) clean
 
+# run make for mkspiffs always
+all: $$(MKSPIFFS)_MAKE
+
+.PHONY: $$(MKSPIFFS)_MAKE
+
 SPIFFS_ESPTOOL_ARGS = $(SPIFFS_BASE_ADDR) $$(SPIFFS_IMAGE)
 endef
 
diff --git a/extras/spiffs/esp_spiffs.c b/extras/spiffs/esp_spiffs.c
index 1574be2..9ae4dea 100644
--- a/extras/spiffs/esp_spiffs.c
+++ b/extras/spiffs/esp_spiffs.c
@@ -94,6 +94,8 @@ int32_t esp_spiffs_mount()
     config.hal_write_f = esp_spiffs_write;
     config.hal_erase_f = esp_spiffs_erase;
 
+    config.fh_ix_offset = 3;
+
     printf("SPIFFS size: %d\n", SPIFFS_SIZE);
     printf("SPIFFS memory, work_buf_size=%d, fds_buf_size=%d, cache_buf_size=%d\n",
             work_buf.size, fds_buf.size, cache_buf.size);
@@ -109,15 +111,11 @@ int32_t esp_spiffs_mount()
     return err;
 }
 
-#define FD_OFFSET 3
-
 // This implementation replaces implementation in core/newlib_syscals.c
 long _write_r(struct _reent *r, int fd, const char *ptr, int len )
 {
     if(fd != r->_stdout->_file) {
-        long ret = SPIFFS_write(&fs, (spiffs_file)(fd - FD_OFFSET),
-                (char*)ptr, len);
-        return ret;
+        return SPIFFS_write(&fs, (spiffs_file)fd, (char*)ptr, len);
     }
     for(int i = 0; i < len; i++) {
         /* Auto convert CR to CRLF, ignore other LFs (compatible with Espressif SDK behaviour) */
@@ -136,8 +134,7 @@ long _read_r( struct _reent *r, int fd, char *ptr, int len )
     int ch, i;
 
     if(fd != r->_stdin->_file) {
-        long ret = SPIFFS_read(&fs, (spiffs_file)(fd - FD_OFFSET), ptr, len);
-        return ret;
+        return SPIFFS_read(&fs, (spiffs_file)fd, ptr, len);
     }
     uart_rxfifo_wait(0, 1);
     for(i = 0; i < len; i++) {
@@ -157,17 +154,15 @@ int _open_r(struct _reent *r, const char *pathname, int flags, int mode)
     if (flags & O_TRUNC)    spiffs_flags |= SPIFFS_TRUNC;
     if (flags & O_RDONLY)   spiffs_flags |= SPIFFS_RDONLY;
     if (flags & O_WRONLY)   spiffs_flags |= SPIFFS_WRONLY;
+    if (flags & O_EXCL)     spiffs_flags |= SPIFFS_EXCL; 
+    /* if (flags & O_DIRECT)   spiffs_flags |= SPIFFS_DIRECT; no support in newlib */
 
-    int ret = SPIFFS_open(&fs, pathname, spiffs_flags, mode);
-    if (ret > 0) {
-        return ret + FD_OFFSET;
-    }
-    return ret;
+    return SPIFFS_open(&fs, pathname, spiffs_flags, mode);
 }
 
 int _close_r(struct _reent *r, int fd)
 {
-    return SPIFFS_close(&fs, (spiffs_file)(fd - FD_OFFSET));
+    return SPIFFS_close(&fs, (spiffs_file)fd);
 }
 
 int _unlink_r(struct _reent *r, const char *path)
@@ -180,7 +175,7 @@ int _fstat_r(struct _reent *r, int fd, void *buf)
     spiffs_stat s;
     struct stat *sb = (struct stat*)buf;
 
-    int result = SPIFFS_fstat(&fs, (spiffs_file)(fd - FD_OFFSET), &s);
+    int result = SPIFFS_fstat(&fs, (spiffs_file)fd, &s);
     sb->st_size = s.size;
 
     return result;
@@ -199,5 +194,5 @@ int _stat_r(struct _reent *r, const char *pathname, void *buf)
 
 off_t _lseek_r(struct _reent *r, int fd, off_t offset, int whence)
 {
-    return SPIFFS_lseek(&fs, (spiffs_file)(fd - FD_OFFSET), offset, whence);
+    return SPIFFS_lseek(&fs, (spiffs_file)fd, offset, whence);
 }
diff --git a/extras/spiffs/mkspiffs/Makefile b/extras/spiffs/mkspiffs/Makefile
index a9a9722..0b8b49e 100644
--- a/extras/spiffs/mkspiffs/Makefile
+++ b/extras/spiffs/mkspiffs/Makefile
@@ -32,6 +32,10 @@ CFLAGS += -DSPIFFS_SIZE=$(SPIFFS_SIZE)
 
 all: mkspiffs
 
+$(OBJECTS): $(SOURCES)
+
+$(OBJECTS): ../spiffs_config.h
+
 mkspiffs: $(OBJECTS)
 
 clean:
diff --git a/extras/spiffs/mkspiffs/mkspiffs.c b/extras/spiffs/mkspiffs/mkspiffs.c
index 65b90e0..a1a0513 100644
--- a/extras/spiffs/mkspiffs/mkspiffs.c
+++ b/extras/spiffs/mkspiffs/mkspiffs.c
@@ -59,7 +59,12 @@ static s32_t _read_data(u32_t addr, u32_t size, u8_t *dst)
 
 static s32_t _write_data(u32_t addr, u32_t size, u8_t *src)
 {
-    memcpy((uint8_t*)image + addr, src, size);
+    uint32_t i;
+    uint8_t *dst = image + addr;
+
+    for (i = 0; i < size; i++) {
+        dst[i] &= src[i];  // mimic NOR flash, flip only 1 to 0
+    }
     return  SPIFFS_OK;
 }
 
@@ -94,12 +99,7 @@ static bool init_spiffs(bool allocate_mem)
     int32_t err = SPIFFS_mount(&fs, &config, work_buf, fds_buf, fdsBufSize,
             cache_buf, cacheBufSize, 0);
 
-    if (err != SPIFFS_OK) {
-        printf("Error spiffs mount: %d\n", err);
-        return false;
-    }
-
-    return true;
+    return err == SPIFFS_OK;
 }
 
 static bool format_spiffs()
@@ -222,20 +222,18 @@ int main(int argc, char *argv[])
         return -1;
     }
 
-    if (init_spiffs(/*allocate_mem=*/true)) {
-        if (format_spiffs()) {
-            if (process_directory(argv[1])) {
-                if (!write_image(argv[2])) {
-                    printf("Error writing image\n");
-                }       
-            } else {
-                printf("Error processing direcotry\n");
-            }
+    init_spiffs(/*allocate_mem=*/true);
+
+    if (format_spiffs()) {
+        if (process_directory(argv[1])) {
+            if (!write_image(argv[2])) {
+                printf("Error writing image\n");
+            }       
         } else {
-            printf("Error formating spiffs\n");
+            printf("Error processing direcotry\n");
         }
     } else {
-        printf("Error initialising SPIFFS\n");
+        printf("Error formating spiffs\n");
     }
       
     spiffs_free();
diff --git a/extras/spiffs/spiffs_config.h b/extras/spiffs/spiffs_config.h
index 7b8c1a7..b782d48 100644
--- a/extras/spiffs/spiffs_config.h
+++ b/extras/spiffs/spiffs_config.h
@@ -125,7 +125,7 @@ typedef unsigned char u8_t;
 // not on mount point. If not, SPIFFS_format must be called prior to mounting
 // again.
 #ifndef SPIFFS_USE_MAGIC
-#define SPIFFS_USE_MAGIC                (0)
+#define SPIFFS_USE_MAGIC                (1)
 #endif
 
 #if SPIFFS_USE_MAGIC
@@ -135,7 +135,7 @@ typedef unsigned char u8_t;
 // be accepted for mounting with a configuration defining the filesystem as 2
 // megabytes.
 #ifndef SPIFFS_USE_MAGIC_LENGTH
-#define SPIFFS_USE_MAGIC_LENGTH         (0)
+#define SPIFFS_USE_MAGIC_LENGTH         (1)
 #endif
 #endif
 
@@ -174,7 +174,7 @@ typedef unsigned char u8_t;
 #define SPIFFS_CFG_LOG_PAGE_SZ(ignore)    (256)
 #endif
 #ifndef SPIFFS_CFG_LOG_BLOCK_SZ
-#define SPIFFS_CFG_LOG_BLOCK_SZ(ignore)   (4*1024)
+#define SPIFFS_CFG_LOG_BLOCK_SZ(ignore)   (8*1024)
 #endif
 #endif
 
@@ -195,7 +195,7 @@ typedef unsigned char u8_t;
 // NB: This adds config field fh_ix_offset in the configuration struct when
 // mounting, which must be defined.
 #ifndef SPIFFS_FILEHDL_OFFSET
-#define SPIFFS_FILEHDL_OFFSET                 0
+#define SPIFFS_FILEHDL_OFFSET                 1
 #endif
 
 // Enable this to compile a read only version of spiffs.