diff mbox series

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

Message ID 20210209114849.3.I10fb7cded95e008428ff08f4f1e6f785cefc4d0f@changeid
State Accepted
Commit 25d90ad45ab336bab6a21f0668b8c98a2939ff32
Delegated to: Tom Rini
Headers show
Series env: sf: add support of command env erase | expand

Commit Message

Patrick Delaunay Feb. 9, 2021, 10:48 a.m. UTC
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(+)

Comments

Tom Rini April 18, 2021, 12:45 p.m. UTC | #1
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!
diff mbox series

Patch

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,
 };