From a04fb9bda2c0e9546481493088b20e6e33e9adba Mon Sep 17 00:00:00 2001 From: armink Date: Sun, 7 Jul 2019 16:54:46 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E7=9A=84=20GC=20=E9=80=9F=E5=BA=A6=EF=BC=8C?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E5=A4=A7=E8=87=B4=E6=8F=90=E5=8D=87=205=20?= =?UTF-8?q?=E5=80=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- easyflash/inc/easyflash.h | 2 +- easyflash/src/ef_env.c | 36 ++++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/easyflash/inc/easyflash.h b/easyflash/inc/easyflash.h index 711859d..9819a06 100644 --- a/easyflash/inc/easyflash.h +++ b/easyflash/inc/easyflash.h @@ -98,7 +98,7 @@ EfErrCode easyflash_init(void); #ifdef EF_USING_ENV /* only supported on ef_env.c */ -size_t ef_get_env_blob(const char *key, void *value_buf, size_t buf_len, size_t *value_len); +size_t ef_get_env_blob(const char *key, void *value_buf, size_t buf_len, size_t *saved_value_len); EfErrCode ef_set_env_blob(const char *key, const void *value_buf, size_t buf_len); /* ef_env.c, ef_env_legacy_wl.c and ef_env_legacy.c */ diff --git a/easyflash/src/ef_env.c b/easyflash/src/ef_env.c index b630f4a..89439b6 100644 --- a/easyflash/src/ef_env.c +++ b/easyflash/src/ef_env.c @@ -240,6 +240,8 @@ static size_t default_env_set_size = 0; static bool init_ok = false; /* request a GC check */ static bool gc_request = false; +/* is in recovery check status when first reboot */ +static bool in_recovery_check = false; #ifdef EF_ENV_USING_CACHE /* ENV cache table */ @@ -482,6 +484,14 @@ static uint32_t find_next_env_addr(uint32_t start, uint32_t end) uint32_t start_bak = start, i; uint32_t magic; +#ifdef EF_ENV_USING_CACHE + uint32_t empty_env; + + if (get_sector_from_cache(EF_ALIGN_DOWN(start, SECTOR_SIZE), &empty_env) && start == empty_env) { + return FAILED_ADDR; + } +#endif /* EF_ENV_USING_CACHE */ + for (; start < end; start += (sizeof(buf) - sizeof(uint32_t))) { ef_port_read(start, (uint32_t *) buf, sizeof(buf)); for (i = 0; i < sizeof(buf) - sizeof(uint32_t) && start + i < end; i++) { @@ -1035,12 +1045,12 @@ static EfErrCode del_env(const char *key, env_meta_data_t old_env, bool complete } else { result = write_status(old_env->addr.start, status_table, ENV_STATUS_NUM, ENV_DELETED); -#ifdef EF_ENV_USING_CACHE if (!last_is_complete_del && result == EF_NO_ERR) { +#ifdef EF_ENV_USING_CACHE /* only delete the ENV in flash and cache when only using del_env(key, env, true) in ef_del_env() */ update_env_cache(key, strlen(key), FAILED_ADDR); - } #endif /* EF_ENV_USING_CACHE */ + } last_is_complete_del = false; } @@ -1071,14 +1081,16 @@ static EfErrCode move_env(env_meta_data_t env) } if ((env_addr = alloc_env(§or, env->len)) != FAILED_ADDR) { - struct env_meta_data env_bak; - char name[EF_ENV_NAME_MAX + 1] = { 0 }; - strncpy(name, env->name, env->name_len); - /* check the ENV in flash is already create success */ - if (find_env_no_cache(name, &env_bak)) { - /* already create success, don't need to duplicate */ - result = EF_NO_ERR; - goto __exit; + if (in_recovery_check) { + struct env_meta_data env_bak; + char name[EF_ENV_NAME_MAX + 1] = { 0 }; + strncpy(name, env->name, env->name_len); + /* check the ENV in flash is already create success */ + if (find_env_no_cache(name, &env_bak)) { + /* already create success, don't need to duplicate */ + result = EF_NO_ERR; + goto __exit; + } } } else { return EF_ENV_FULL; @@ -1680,6 +1692,8 @@ EfErrCode ef_load_env(void) /* lock the ENV cache */ ef_port_env_lock(); + + in_recovery_check = true; /* check all sector header */ sector_iterator(§or, SECTOR_STORE_UNUSED, &check_failed_count, NULL, check_sec_hdr_cb, false); /* all sector header check failed */ @@ -1698,6 +1712,8 @@ __retry: goto __retry; } + in_recovery_check = false; + /* unlock the ENV cache */ ef_port_env_unlock();