[PATCH 0/3] env: sf: add support of command env erase

Hi,
I add support of env erase command for SPI flash backend (sf).
To erase the environment, this patch only invalidate the content at CONFIG_ENV_OFFSET and at CONFIG_ENV_OFFSET_REDUND (force a bad CRC).
The 2 first patchs of the serie are preliminary and clenaup steps.
Patrick
Patrick Delaunay (3): env: add ENV_ERASE_PTR macro env: sf: update the use of macro ENV_SAVE_PTR env: sf: add support of command env erase
env/ext4.c | 3 +-- env/mmc.c | 6 +----- env/sf.c | 36 +++++++++++++++++++++++++++++++++++- include/env_internal.h | 1 + 4 files changed, 38 insertions(+), 8 deletions(-)

Add ENV_ERASE_PTR macro to handle erase opts and remove the associated ifdef.
This patch is a extension of previous commit 82b2f4135719 ("env_internal.h: add alternative ENV_SAVE_PTR macro").
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com ---
env/ext4.c | 3 +-- env/mmc.c | 6 +----- include/env_internal.h | 1 + 3 files changed, 3 insertions(+), 7 deletions(-)
diff --git a/env/ext4.c b/env/ext4.c index e666f7b945..caa3e05a57 100644 --- a/env/ext4.c +++ b/env/ext4.c @@ -187,6 +187,5 @@ U_BOOT_ENV_LOCATION(ext4) = { ENV_NAME("EXT4") .load = env_ext4_load, .save = ENV_SAVE_PTR(env_ext4_save), - .erase = CONFIG_IS_ENABLED(CMD_ERASEENV) ? env_ext4_erase : - NULL, + .erase = ENV_ERASE_PTR(env_ext4_erase), }; diff --git a/env/mmc.c b/env/mmc.c index ee376c3e0c..d959ba6d80 100644 --- a/env/mmc.c +++ b/env/mmc.c @@ -232,7 +232,6 @@ fini: return ret; }
-#if defined(CONFIG_CMD_ERASEENV) static inline int erase_env(struct mmc *mmc, unsigned long size, unsigned long offset) { @@ -278,7 +277,6 @@ static int env_mmc_erase(void)
return ret; } -#endif /* CONFIG_CMD_ERASEENV */ #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
static inline int read_env(struct mmc *mmc, unsigned long size, @@ -393,8 +391,6 @@ U_BOOT_ENV_LOCATION(mmc) = { .load = env_mmc_load, #ifndef CONFIG_SPL_BUILD .save = env_save_ptr(env_mmc_save), -#if defined(CONFIG_CMD_ERASEENV) - .erase = env_mmc_erase, -#endif + .erase = ENV_ERASE_PTR(env_mmc_erase) #endif }; diff --git a/include/env_internal.h b/include/env_internal.h index 708c833a55..b7bddcb00d 100644 --- a/include/env_internal.h +++ b/include/env_internal.h @@ -211,6 +211,7 @@ struct env_driver { #endif
#define ENV_SAVE_PTR(x) (CONFIG_IS_ENABLED(SAVEENV) ? (x) : NULL) +#define ENV_ERASE_PTR(x) (CONFIG_IS_ENABLED(CMD_ERASEENV) ? (x) : NULL)
extern struct hsearch_data env_htab;

On Tue, Feb 09, 2021 at 11:48:50AM +0100, Patrick Delaunay wrote:
Add ENV_ERASE_PTR macro to handle erase opts and remove the associated ifdef.
This patch is a extension of previous commit 82b2f4135719 ("env_internal.h: add alternative ENV_SAVE_PTR macro").
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com
Applied to u-boot/master, thanks!

Remove CONFIG_IS_ENABLED(SAVEENV) as it is already tested in the ENV_SAVE_PTR macro.
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com ---
env/sf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/env/sf.c b/env/sf.c index 42d762714e..3f2fbbec12 100644 --- a/env/sf.c +++ b/env/sf.c @@ -405,6 +405,6 @@ U_BOOT_ENV_LOCATION(sf) = { .location = ENVL_SPI_FLASH, ENV_NAME("SPIFlash") .load = env_sf_load, - .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL, + .save = ENV_SAVE_PTR(env_sf_save), .init = env_sf_init, };

On Tue, Feb 09, 2021 at 11:48:51AM +0100, Patrick Delaunay wrote:
Remove CONFIG_IS_ENABLED(SAVEENV) as it is already tested in the ENV_SAVE_PTR macro.
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com
Applied to u-boot/master, thanks!

Add support of opts erase for env in SPI flash; this opts is used by command 'env erase'.
This command only fills the env offset by 0x0 (bit flip to 0) and the saved environment becomes invalid (with bad CRC).
It doesn't erase the sector here to avoid issue when the sector is larger than the env (i.e. embedded when CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE).
The needed sector erase will be managed in the next "env save" command, using the opt ".save", before to update the environment in SPI flash.
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com ---
env/sf.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
diff --git a/env/sf.c b/env/sf.c index 3f2fbbec12..6b61a4b8de 100644 --- a/env/sf.c +++ b/env/sf.c @@ -27,9 +27,18 @@ #define INITENV #endif
+#define OFFSET_INVALID (~(u32)0) + #ifdef CONFIG_ENV_OFFSET_REDUND +#define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND + static ulong env_offset = CONFIG_ENV_OFFSET; static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; + +#else + +#define ENV_OFFSET_REDUND OFFSET_INVALID + #endif /* CONFIG_ENV_OFFSET_REDUND */
DECLARE_GLOBAL_DATA_PTR; @@ -279,6 +288,30 @@ out: } #endif
+static int env_sf_erase(void) +{ + int ret; + env_t env; + + ret = setup_flash_device(); + if (ret) + return ret; + + memset(&env, 0, sizeof(env_t)); + ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env); + if (ret) + goto done; + + if (ENV_OFFSET_REDUND != OFFSET_INVALID) + ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env); + +done: + spi_flash_free(env_flash); + env_flash = NULL; + + return ret; +} + #if CONFIG_ENV_ADDR != 0x0 __weak void *env_sf_get_env_addr(void) { @@ -406,5 +439,6 @@ U_BOOT_ENV_LOCATION(sf) = { ENV_NAME("SPIFlash") .load = env_sf_load, .save = ENV_SAVE_PTR(env_sf_save), + .erase = ENV_ERASE_PTR(env_sf_erase), .init = env_sf_init, };

On Tue, Feb 09, 2021 at 11:48:52AM +0100, Patrick Delaunay wrote:
Add support of opts erase for env in SPI flash; this opts is used by command 'env erase'.
This command only fills the env offset by 0x0 (bit flip to 0) and the saved environment becomes invalid (with bad CRC).
It doesn't erase the sector here to avoid issue when the sector is larger than the env (i.e. embedded when CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE).
The needed sector erase will be managed in the next "env save" command, using the opt ".save", before to update the environment in SPI flash.
Signed-off-by: Patrick Delaunay patrick.delaunay@foss.st.com
Applied to u-boot/master, thanks!
participants (2)
-
Patrick Delaunay
-
Tom Rini