[PATCH 1/1] env: sf: add support for env erase

One new function env_sf_erase() is added to support clearing flash region(s) holding environment configuration.
It treats redundant configuration properly when U-Boot config option SYS_REDUNDAND_ENVIRONMENT is active.
Unfortunately, none of the #if CONFIG_IS_ENABLED() sections can be avoided.
Signed-off-by: Harry Waschkeit Harry.Waschkeit@men.de --- This patch is based on patch ...
"[PATCH v2 1/1] env: sf: single function env_sf_save()" (https://lists.denx.de/pipermail/u-boot/2021-February/439418.html)
... or ...
"[PATCH v3 1/1] env: sf: single function env_sf_save()" (https://lists.denx.de/pipermail/u-boot/2021-February/439577.html)
... which only added the "reviewed-by" from Stefan Roese compared to v2.
env/sf.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+)
diff --git a/env/sf.c b/env/sf.c index 199114fd3b..eedf59f2df 100644 --- a/env/sf.c +++ b/env/sf.c @@ -157,6 +157,86 @@ static int env_sf_save(void) return ret; }
+#if CONFIG_IS_ENABLED(CMD_ERASEENV) +static int env_sf_erase(void) +{ + char *saved_buffer = NULL; + u32 saved_size, saved_offset, sector; + ulong offset; + ulong offsets[] = { + CONFIG_ENV_OFFSET, +#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT) + CONFIG_ENV_OFFSET_REDUND +#endif + }; + int i; + int ret; + + ret = setup_flash_device(); + if (ret) + return ret; + + /* get temporary storage if sector is larger than env (i.e. embedded) */ + if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { + saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; + saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); + if (!saved_buffer) { + ret = -ENOMEM; + goto done; + } + } + + sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); + + /* simply erase both environments, retaining non-env data (if any) */ + for (i = 0; i < ARRAY_SIZE(offsets); i++) { + offset = offsets[i]; + + if (saved_buffer) { + saved_offset = offset + CONFIG_ENV_SIZE; + ret = spi_flash_read(env_flash, saved_offset, + saved_size, saved_buffer); + if (ret) + goto done; + } + + /* next print will only happen with redundant environments */ + if (i) + puts("Redund:"); + + puts("Erasing SPI flash..."); + ret = spi_flash_erase(env_flash, offset, + sector * CONFIG_ENV_SECT_SIZE); + if (ret) + goto done; + + if (saved_buffer) { + puts("Writing non-environment data to SPI flash..."); + ret = spi_flash_write(env_flash, saved_offset, + saved_size, saved_buffer); + if (ret) + goto done; + } + + puts("done\n"); + } + +#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT) + /* here we know that both env sections are cleared */ + env_new_offset = CONFIG_ENV_OFFSET; + env_offset = CONFIG_ENV_OFFSET_REDUND; + + gd->env_valid = ENV_INVALID; +#endif + + done: + if (saved_buffer) + free(saved_buffer); + + return ret; +} +#endif /* CONFIG_IS_ENABLED(CMD_ERASEENV) */ + #if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT) static int env_sf_load(void) { @@ -263,4 +343,7 @@ U_BOOT_ENV_LOCATION(sf) = { #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0) .init = env_sf_init, #endif +#if CONFIG_IS_ENABLED(CMD_ERASEENV) + .erase = env_sf_erase, +#endif };
participants (1)
-
Harry Waschkeit